*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 200px;
  --bg: #f5f6fa;
  --surface: #fff;
  --border: #e2e6ea;
  --primary: #4361ee;
  --primary-hover: #3a56d4;
  --text: #212529;
  --muted: #6c757d;
  --green: #2ecc71;
  --red: #e74c3c;
  --orange: #f39c12;
  --row-hover: #f8f9ff;
}

body {
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

/* ── Sidebar ───────────────────────────────────────────────── */
#sidebar {
  background: #1a1d2e;
  color: #c9d1d9;
  padding: 16px 0;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  overflow-y: auto;
}

.logo {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  padding: 8px 20px 20px;
}

#sidebar hr { border-color: #2d3142; margin: 8px 0; }

.nav-link {
  display: block;
  padding: 9px 20px;
  color: #a0aec0;
  text-decoration: none;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover  { background: #2d3142; color: #e2e8f0; }
.nav-link.active { background: #2d3142; color: #fff; border-left-color: var(--primary); }

/* ── Main ──────────────────────────────────────────────────── */
#content {
  margin-left: var(--sidebar-w);
  padding: 28px 32px;
  min-width: 0;
}

.view h1 { font-size: 22px; font-weight: 600; margin-bottom: 6px; }
.view h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
.view-desc { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }

/* ── Toolbar ───────────────────────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.toolbar label { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.toolbar select {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 13px;
  max-width: 220px;
}

.toggle-label { cursor: pointer; user-select: none; }
.toggle-label input[type=checkbox] { accent-color: var(--primary); }

.search-input {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 10px;
  font-size: 13px;
  width: 200px;
  margin-left: auto;
}
.search-input:focus { outline: 2px solid var(--primary); }

/* ── Button groups ─────────────────────────────────────────── */
.btn-group { display: flex; }
.btn-group .btn { border-radius: 0; border-right-width: 0; }
.btn-group .btn:first-child { border-radius: 4px 0 0 4px; }
.btn-group .btn:last-child  { border-radius: 0 4px 4px 0; border-right-width: 1px; }

.btn {
  padding: 5px 14px;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  transition: background 0.15s;
}
.btn:hover { background: #f0f4ff; }
.btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); border-radius: 4px; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--red); }
.btn-sm { padding: 3px 10px; font-size: 12px; }

/* ── Row counter ───────────────────────────────────────────── */
.row-counter {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
  padding: 0 2px;
}

/* ── Dashboard ─────────────────────────────────────────────── */
.dash-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-top: 20px;
}
@media (max-width: 900px) { .dash-grid { grid-template-columns: 1fr; } }

.dash-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  min-width: 0;
}

.dist-table {
  width: 100%;
  border-collapse: collapse;
}

.dist-table th {
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  background: #f8f9fa;
  white-space: nowrap;
}

.dist-table td {
  padding: 8px 10px;
  border-bottom: 1px solid #f0f2f5;
  vertical-align: middle;
}

.dist-table tr:last-child td { border-bottom: none; }
.dist-row { cursor: pointer; }
.dist-row:hover td { background: var(--row-hover); }

.dist-preview {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 260px;
}

.dist-detail td {
  background: #f8f9ff;
  padding: 8px 10px;
}
.dist-detail-body {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 160px;
  overflow-y: auto;
}
.dist-chip {
  display: inline-block;
  background: #e8ecff;
  color: #3a56d4;
  border-radius: 12px;
  padding: 2px 10px;
  font-size: 11px;
  white-space: nowrap;
}

/* ── Tables (reports) ──────────────────────────────────────── */
.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  text-align: left;
}

td {
  padding: 8px 12px;
  border-bottom: 1px solid #f0f2f5;
  vertical-align: top;
}

tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--row-hover); }
tr.expandable { cursor: pointer; }

.kw-cell {
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}

/* ── Position + delta ──────────────────────────────────────── */
.pos { font-variant-numeric: tabular-nums; font-size: 13px; }
.badge-1-3   { color: #1a9e5f; font-weight: 700; }
.badge-4-10  { color: #2980b9; font-weight: 600; }
.badge-11-20 { color: var(--muted); }
.badge-21-50 { color: var(--muted); }
.badge-51    { color: #bbb; }

.delta { font-size: 11px; white-space: nowrap; }
.delta-up   { color: var(--green); }
.delta-down { color: var(--red); }
.delta-flat { color: var(--muted); }

/* ── Period cell ───────────────────────────────────────────── */
.period-cell { font-size: 12px; line-height: 1.6; min-width: 120px; }
.url-slug    { font-weight: 500; color: var(--primary); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px; }
.metrics     { color: var(--muted); font-size: 11px; }
.ctr-cell    { cursor: help; }

/* ── Cannibalization + alert ───────────────────────────────── */
.alert-icon { color: var(--orange); cursor: help; }
.cannibal-badge {
  display: inline-block;
  font-size: 10px;
  background: #fff3cd;
  color: #856404;
  border: 1px solid #ffc107;
  border-radius: 3px;
  padding: 1px 5px;
  margin-left: 4px;
  cursor: help;
}

/* ── Drill-down ────────────────────────────────────────────── */
.drill-td { padding: 0 !important; background: #f8f9ff; }
.drill-row td table { background: transparent; border: none; border-radius: 0; }
.drill-row td table th { background: #f0f2f8; font-size: 11px; }
.drill-row td table td { padding: 5px 12px; border-bottom: 1px solid #edf0f7; }

/* ── Charts ────────────────────────────────────────────────── */
.chart-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}

/* ── CRUD ──────────────────────────────────────────────────── */
.bulk-form {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  align-items: flex-start;
}
.bulk-input {
  flex: 1;
  max-width: 520px;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  line-height: 1.5;
}
.bulk-input:focus { outline: 2px solid var(--primary); }

.error-msg {
  color: var(--red);
  font-size: 13px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: #fff0f0;
  border-radius: 4px;
  border: 1px solid #fcc;
}

/* ── States ────────────────────────────────────────────────── */
.spinner { text-align: center; padding: 40px; color: var(--muted); font-size: 13px; }
.no-data { text-align: center; padding: 32px 20px; color: var(--muted); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; font-size: 13px; }
