:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --border: #e3e5e8;
  --text: #1c1e21;
  --muted: #65676b;
  --accent: #1877f2;
  --accent-bg: #f0f6ff;
  --danger: #e41e3f;
  --ok: #1d7a3a;
  --ok-bg: #e7f7ee;
  --warn: #8a6a00;
  --warn-bg: #fff4d6;
  --err: #9a1c33;
  --err-bg: #fde7eb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  font-size: 14px;
}

/* ============= Layout ============= */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 24px;
  background: white;
  border-bottom: 1px solid var(--border);
}
.brand {
  font-weight: 700; color: var(--text); text-decoration: none; font-size: 18px;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }

.container {
  max-width: 1200px;
  margin: 24px auto;
  padding: 0 24px;
}

/* ============= Cards ============= */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card h2 {
  margin: 0 0 14px 0;
  font-size: 18px;
  font-weight: 600;
}
.card h2 + .row,
.card h2 + form .row { margin-top: 0; }

/* Collapsible cards */
.card.collapsible { position: relative; }
.card.collapsible > h2 { padding-right: 44px; }
.collapse-btn {
  position: absolute;
  top: 18px; right: 18px;
  width: 28px; height: 28px;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: white; color: var(--muted);
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 12px; line-height: 1;
  cursor: pointer;
  transition: background .12s, transform .2s;
}
.collapse-btn:hover { background: #f6f7f9; color: var(--text); }
.card.collapsed .collapse-btn { transform: rotate(-90deg); }
.card.collapsed .card-body { display: none; }
.card.collapsed > h2 { margin-bottom: 0; }

/* ============= Forms ============= */
.row {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
}
input, select, button {
  font: inherit;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
}
input { flex: 1; min-width: 200px; }
input:focus, select:focus { outline: none; border-color: var(--accent); }

button {
  background: var(--accent); color: white; border-color: var(--accent);
  cursor: pointer; font-weight: 500;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
button:hover:not(:disabled) { background: #1366d6; border-color: #1366d6; }
button:disabled { opacity: .55; cursor: not-allowed; }

button.danger {
  background: white; color: var(--danger); border-color: var(--danger);
}
button.danger:hover:not(:disabled) { background: #fde7eb; }

.btn-link {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  color: var(--accent);
  background: white;
  text-decoration: none;
  font-size: 14px;
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
}
.btn-link:hover { background: var(--accent-bg); }

.btn-secondary {
  background: white; color: var(--text); border-color: var(--border);
}
.btn-secondary:hover:not(:disabled) {
  background: #f6f7f9; border-color: var(--muted);
}

/* ============= Tables ============= */
table.data {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}
table.data th, table.data td {
  text-align: left;
  padding: 12px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
table.data th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  font-weight: 600;
  background: #fafbfc;
}
table.data tbody tr:hover { background: #fafbfc; }
table.data a { color: var(--text); text-decoration: none; font-weight: 500; }
table.data a:hover { color: var(--accent); text-decoration: underline; }

/* Competitor table specific column widths */
.competitors-table { table-layout: fixed; }
.competitors-table col.col-name      { width: 22%; }
.competitors-table col.col-url       { width: 56%; }
.competitors-table col.col-country   { width: 8%; }
.competitors-table col.col-actions   { width: 14%; }

/* URL cell — single-line truncate with tooltip on hover */
.url-cell {
  display: block;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--muted);
}
.url-cell:hover { color: var(--text); }

.actions {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  justify-content: flex-end;
}
.actions form { margin: 0; }
.actions button { padding: 6px 12px; font-size: 13px; }

/* Runs table */
.runs-table col.col-runs-id       { width: 70px; }
.runs-table col.col-runs-comp     { width: 22%; }
.runs-table col.col-runs-time     { width: 16%; }
.runs-table col.col-runs-num      { width: 80px; }
.runs-table col.col-runs-status   { width: 100px; }
.runs-table col.col-runs-link     { width: 130px; }
.runs-table a { color: var(--accent); }
.run-actions {
  display: flex; align-items: center; justify-content: flex-end;
  gap: 8px;
}
.icon-btn {
  width: 26px; height: 26px;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 400; line-height: 1;
  background: white; color: var(--muted);
  border: 1px solid var(--border); border-radius: 50%;
  cursor: pointer;
}
.icon-btn:hover {
  background: var(--err-bg); color: var(--danger); border-color: var(--danger);
}

/* ============= Flash messages ============= */
.flashes { max-width: 1200px; margin: 12px auto 0; padding: 0 24px; }
.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 8px; }
.flash-success { background: var(--ok-bg); color: var(--ok); }
.flash-error { background: var(--err-bg); color: var(--err); }

/* ============= Status pills ============= */
.status {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
}
.status-ok { background: var(--ok-bg); color: var(--ok); }
.status-error { background: var(--err-bg); color: var(--err); }
.status-running { background: var(--warn-bg); color: var(--warn); }

/* ============= Ad cards (competitor detail page) ============= */
.ad-page-group {
  margin-bottom: 36px;
  padding: 16px 20px 8px;
  border-left: 4px solid var(--accent);
  background: linear-gradient(90deg, var(--accent-bg) 0%, transparent 100%);
  border-radius: 0 8px 8px 0;
}
.ad-page-group:last-child { margin-bottom: 0; }
.page-heading {
  display: flex; align-items: baseline; gap: 12px;
  font-size: 20px; font-weight: 700;
  margin: 0 0 16px 0;
  color: var(--text);
}
.page-heading .small { font-weight: 400; color: var(--muted); }

.ad-group { margin-bottom: 24px; }
.ad-group:last-child { margin-bottom: 8px; }
.group-heading {
  display: flex; align-items: baseline; gap: 12px;
  font-size: 15px; font-weight: 600;
  margin: 0 0 12px 0; padding-bottom: 6px;
  border-bottom: 1px dashed var(--border);
  color: var(--text);
}
.group-heading .small { font-weight: 400; }

.ads-grid {
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.ad {
  background: #fafbfc;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.ad img {
  width: 100%; display: block;
  max-height: 360px; object-fit: cover;
}
.ad-meta { padding: 12px; }
.ad-meta p {
  margin: 8px 0;
  white-space: pre-wrap;
  font-size: 13px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.ad-meta a.small { color: var(--accent); }

.tag-row {
  display: flex; align-items: center; gap: 6px;
  margin: 8px 0; padding: 6px 0;
  border-top: 1px dashed var(--border);
}
.tag-input {
  flex: 1; min-width: 0;
  padding: 4px 8px; font-size: 13px;
}
.tag-status { min-width: 50px; }

/* ============= Code helper ============= */
code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  background: #f0f2f4;
  padding: 1px 6px;
  border-radius: 4px;
}

/* ============= Hint / note paragraphs ============= */
.note {
  background: var(--accent-bg);
  border: 1px solid #d6e4f9;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text);
  margin: 12px 0 0;
}
.note strong { color: var(--accent); }
.note a { color: var(--accent); }

/* ============= Stats panel ============= */
.stats-section { margin-bottom: 18px; }
.stats-section:last-child { margin-bottom: 0; }
.stats-section h4 {
  margin: 8px 0 10px 0;
  font-size: 13px; font-weight: 600;
  color: var(--muted); text-transform: uppercase;
  letter-spacing: .4px;
}
.stats-bars { display: flex; flex-direction: column; gap: 6px; }
.stats-row {
  display: grid;
  grid-template-columns: 110px 1fr 50px;
  align-items: center; gap: 12px;
}
.stats-label {
  font-size: 13px; font-variant-numeric: tabular-nums;
  color: var(--text);
}
.stats-bar {
  display: block; height: 18px;
  background: #f0f2f4; border-radius: 4px; overflow: hidden;
}
.stats-bar-fill {
  display: block; height: 100%;
  background: linear-gradient(90deg, var(--accent) 0%, #4d97ff 100%);
  border-radius: 4px;
  transition: width .3s;
}
.stats-count {
  font-size: 13px; font-weight: 600;
  text-align: right; font-variant-numeric: tabular-nums;
}
.stats-page-block {
  margin-bottom: 22px; padding-bottom: 18px;
  border-bottom: 1px solid var(--border);
}
.stats-page-block:last-child { margin-bottom: 0; padding-bottom: 0; border-bottom: 0; }
.stats-page-name {
  margin: 0 0 6px 0; font-size: 15px; font-weight: 600;
}

/* ============= Login page ============= */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 400px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: 0 8px 24px rgba(0,0,0,.06);
}
.login-title {
  text-align: center; margin: 0 0 4px 0;
  font-size: 28px; font-weight: 700;
}
.login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.login-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 13px; color: var(--muted);
}
.login-form input {
  padding: 10px 12px; font-size: 14px; flex: none;
}

/* Topbar right-side user info */
.topbar-spacer { flex: 1; }
.topbar-user {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted);
}
.topbar-user a {
  color: var(--muted); text-decoration: none;
  padding: 4px 10px; border-radius: 4px;
}
.topbar-user a:hover { background: #f0f2f4; color: var(--text); }

/* ============= Responsive ============= */
@media (max-width: 720px) {
  .container { padding: 0 12px; }
  .card { padding: 16px; }
  table.data th, table.data td { padding: 8px 6px; font-size: 12px; }
  .actions { flex-wrap: wrap; }
}
