:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e4e7ec;
  --text: #0f172a;
  --text-2: #475467;
  --muted: #5d6779;
  --accent: #2563eb;
  --accent-soft: #eaf1fe;
  --focus: #2563eb;
  --series-1: #2a78d6;
  --series-prev: #7c8594;
  --grid: #eceef2;
  --good: #137a3a;
  --good-bg: #e8f6ee;
  --bad: #b42318;
  --bad-bg: #fdecea;
  --flat: #5d6779;
  --flat-bg: #f0f2f5;
  --bar: #86b6ef;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --c-blue: #2563eb;
  --c-violet: #7c3aed;
  --c-teal: #0f8a7e;
  --c-orange: #dd5a0b;
  --c-green: #16883f;
  --c-pink: #cf2a76;
  --gap-text: #a84507; /* 6:1 on white; --c-orange is too faint for small text */
  --radius: 14px;
  --sidebar: 248px;
  --sidebar-collapsed: 72px;
}
:root[data-dash-theme="dark"] {
  color-scheme: dark;
  --gap-text: #fb923c;
  --bg: #0b0d12;
  --surface: #141821;
  --surface-2: #191e29;
  --border: #262c38;
  --text: #e9ecf2;
  --text-2: #b9c0cc;
  --muted: #949cab;
  --accent: #6ea2f7;
  --accent-soft: #1b2a45;
  --focus: #8ab4ff;
  --series-1: #3987e5;
  --series-prev: #8a93a3;
  --grid: #222834;
  --good: #5fd08a;
  --good-bg: #13301f;
  --bad: #ff8a80;
  --bad-bg: #3a1a1a;
  --flat: #a3abb9;
  --flat-bg: #212733;
  --bar: #2a6bc0;
  --shadow: none;
  --c-blue: #6ea2f7;
  --c-violet: #a78bfa;
  --c-teal: #2dd4bf;
  --c-orange: #fb923c;
  --c-green: #4ade80;
  --c-pink: #f472b6;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-feature-settings: "cv11", "ss01";
}
.num, td.n, .kpi-value { font-variant-numeric: tabular-nums; }
svg.i { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip { position: absolute; left: 12px; top: -48px; background: var(--surface); padding: 8px 12px; border-radius: 8px; z-index: 50; }
.skip:focus { top: 12px; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: 100vh; transition: grid-template-columns 0.2s ease; }
.app.collapsed { grid-template-columns: var(--sidebar-collapsed) minmax(0, 1fr); }
.main { min-width: 0; padding: 28px 32px 40px; max-width: 1360px; width: 100%; }

/* ---------- Sidebar ---------- */
.sidebar {
  position: sticky; top: 0; height: 100vh;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 18px 16px; border-bottom: 1px solid var(--border); min-height: 72px; }
.brand-mark { width: 40px; height: 40px; border-radius: 10px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 17px; flex: none; }
:root[data-dash-theme="dark"] .brand-mark { color: #0b0d12; }
.brand-text { min-width: 0; }
.brand-name { font-weight: 650; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-domain { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-label { font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); padding: 10px 10px 6px; white-space: nowrap; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--text-2); text-decoration: none; font-weight: 500; white-space: nowrap; min-height: 44px;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a[aria-current="true"] { background: var(--accent-soft); color: var(--accent); }
.sidebar-foot { border-top: 1px solid var(--border); padding: 10px; }
.side-btn { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px; border: 0; background: none; border-radius: 10px; color: var(--text-2); cursor: pointer; white-space: nowrap; min-height: 44px; }
.side-btn:hover { background: var(--surface-2); color: var(--text); }
.side-btn[hidden] { display: none; }
.app.collapsed .brand-text, .app.collapsed .nav a span, .app.collapsed .nav-label, .app.collapsed .side-btn span { display: none; }
.app.collapsed .brand { justify-content: center; padding: 18px 0; }
.app.collapsed .nav a, .app.collapsed .side-btn { justify-content: center; padding: 10px 0; }
.app.collapsed .side-btn svg { transform: rotate(180deg); }
.scrim { display: none; }

/* ---------- Top bar ---------- */
.topbar { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 20px; }
.topbar-title { flex: 1; min-width: 0; }
h1 { font-size: 28px; line-height: 1.2; margin: 0; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { margin: 4px 0 0; color: var(--text-2); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  display: grid; place-items: center; cursor: pointer; color: var(--text-2); flex: none;
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.menu-btn { display: none; }
.pill[hidden] { display: none; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--flat-bg); color: var(--text-2); vertical-align: middle; }

/* ---------- Filters ---------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label, .field .lbl { font-size: 12px; font-weight: 600; color: var(--muted); }
.field select, .field input {
  height: 40px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); padding: 0 12px; min-width: 0;
}
.field select { padding-right: 32px; appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; }
.custom { display: flex; gap: 8px; align-items: flex-end; }
.custom[hidden] { display: none; }
.range-note { margin-left: auto; color: var(--text-2); font-size: 13px; text-align: right; }
.range-note strong { color: var(--text); font-weight: 600; }

/* ---------- Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; min-width: 0; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head h2 { font-size: 17px; margin: 0; font-weight: 650; }
.card-sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.section { scroll-margin-top: 16px; }
.stack { display: grid; gap: 20px; }
.grid-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 20px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }

/* KPI cards */
.kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.kpi { padding: 18px 20px; }
.kpi-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.chip { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; color: var(--c); background: color-mix(in srgb, var(--c) 13%, transparent); }
.kpi-label { color: var(--text-2); font-weight: 500; font-size: 14px; }
.kpi-value { font-size: 28px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.2; margin: 2px 0 4px; }
.kpi-prev { color: var(--muted); font-size: 13px; }

.delta { display: inline-flex; align-items: center; gap: 3px; font-size: 12.5px; font-weight: 600; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.delta svg.i { width: 14px; height: 14px; stroke-width: 2.5; }
.delta.up { color: var(--good); background: var(--good-bg); }
.delta.down { color: var(--bad); background: var(--bad-bg); }
.delta.flat { color: var(--flat); background: var(--flat-bg); }
td .delta { padding: 2px 6px; font-size: 12px; }

/* Search visibility */
.vis-list { display: grid; gap: 4px; }
.vis-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.vis-row:last-child { border-bottom: 0; }
.vis-label { color: var(--text-2); font-size: 14px; }
.vis-val { font-weight: 650; font-size: 17px; }
.vis-right { display: flex; align-items: center; gap: 8px; }
.share { margin-top: 14px; }
.share-bar { height: 10px; border-radius: 999px; background: var(--flat-bg); overflow: hidden; margin: 8px 0 6px; }
.share-bar span { display: block; height: 100%; background: var(--series-1); border-radius: 999px; }
.note { color: var(--muted); font-size: 12.5px; margin: 10px 0 0; }

/* AI search */
.ai-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.ai-stat { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.ai-stat-label { color: var(--text-2); font-size: 13px; font-weight: 500; }
.ai-stat-row { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; margin-top: 2px; }
.ai-stat-value { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.25; font-variant-numeric: tabular-nums; }
.ai-stat-prev { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.ai-block { margin-top: 22px; min-width: 0; }
.ai-h3 { font-size: 15px; font-weight: 650; margin: 0 0 2px; }
.ai-block > .table-scroll { margin-top: 8px; }
.ai-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 28px; }
.bars-wrap { margin-top: 10px; }
.bars-wrap svg { display: block; width: 100%; overflow: visible; }
.ai-head { margin-bottom: 0; }
.bars-chart { position: relative; }
.bars-chart[hidden], .bars-wrap .table-scroll[hidden] { display: none; }
.bars-wrap svg { touch-action: pan-y; }
.bars-wrap svg:focus-visible { outline-offset: 4px; }
.bars-wrap svg.hovering .bar:not(.active) { opacity: 0.45; }
.tt-head .tt-key { color: var(--text); font-weight: 600; }
.tt-head + .tt-row { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); }
.from { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
td.from { margin-top: 0; }
.bar { fill: var(--series-1); }
.bar.partial { opacity: 0.55; }
.gap-band { fill: var(--c-orange); opacity: 0.12; }
.gap-label { fill: var(--gap-text); font-size: 11px; font-weight: 600; }
.ai-notes { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 14px; }
.ai-notes p { color: var(--text-2); font-size: 13px; margin: 0 0 8px; }
.ai-notes ul { color: var(--text-2); font-size: 13px; margin: 0; padding-left: 18px; }
.ai-notes li + li { margin-top: 6px; }
.ai-notes details summary { cursor: pointer; color: var(--text); font-size: 13px; font-weight: 600; min-height: 32px; display: flex; align-items: center; }
.callout { background: color-mix(in srgb, var(--c-orange) 10%, transparent); border-radius: 10px; padding: 10px 12px; color: var(--text); font-size: 13px; margin-top: 14px; }
@media (max-width: 1180px) { .ai-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 1080px) { .ai-grid { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 560px) { .ai-stat { padding: 12px; } .ai-stat-value { font-size: 20px; } }

/* Chart */
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 8px; }
.swatch { width: 18px; height: 0; border-top: 2px solid var(--series-1); }
.swatch.prev { border-top: 2px dashed var(--series-prev); }
.chart-wrap { position: relative; margin-top: 8px; }
.chart-wrap svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; }
.chart-wrap svg:focus-visible { outline-offset: 4px; }
.axis text { fill: var(--muted); font-size: 11.5px; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.line-cur { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.line-prev { fill: none; stroke: var(--series-prev); stroke-width: 1.75; stroke-dasharray: 5 4; stroke-linejoin: round; }
.area-cur { fill: var(--series-1); opacity: 0.08; }
.crosshair { stroke: var(--text-2); stroke-width: 1; opacity: 0.5; }
.dot { stroke: var(--surface); stroke-width: 2; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.12); padding: 10px 12px; font-size: 13px; min-width: 170px; z-index: 5;
}
.tooltip[hidden] { display: none; }
.tt-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.tt-row + .tt-row { margin-top: 4px; }
.tt-key { display: inline-flex; align-items: center; gap: 8px; color: var(--text-2); }
.tt-val { font-weight: 650; }
.toggle { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 0 12px; height: 36px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 13px; font-weight: 500; white-space: nowrap; }
.toggle:hover { color: var(--text); background: var(--surface-2); }

/* Tables */
.table-scroll { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 8px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th.n, td.n { text-align: right; }
td.label { max-width: 0; width: 100%; }
td.label div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.label a { color: var(--text); text-decoration: none; }
td.label a:hover { color: var(--accent); text-decoration: underline; }
.rank { color: var(--muted); font-size: 12px; width: 1%; padding-right: 0; }
tfoot td { font-weight: 650; border-top: 1px solid var(--border); border-bottom: 0; }
.ev-bar { height: 6px; border-radius: 999px; background: var(--flat-bg); margin-top: 6px; overflow: hidden; }
.ev-bar span { display: block; height: 100%; background: var(--bar); border-radius: 999px; }
.more { margin-top: 12px; }
.empty { color: var(--muted); padding: 18px 0; text-align: center; }

.footer { color: var(--muted); font-size: 12.5px; margin-top: 28px; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 24px; }
.footer a { color: var(--text-2); }

.loading .stack { opacity: 0.55; transition: opacity 0.15s; }
.error { background: var(--bad-bg); color: var(--bad); border-radius: 12px; padding: 12px 16px; margin-bottom: 16px; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .grid-main { grid-template-columns: minmax(0, 1fr); }
  .vis-list { grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: 24px; }
  .vis-row:nth-last-child(2) { border-bottom: 0; }
}
@media (max-width: 1080px) {
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 900px) {
  .app, .app.collapsed { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(86vw, 300px); z-index: 40;
    transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  }
  .app.drawer-open .sidebar { transform: none; }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(10, 12, 18, 0.45); z-index: 30; }
  .app.collapsed .brand-text, .app.collapsed .nav a span, .app.collapsed .nav-label, .app.collapsed .side-btn span { display: initial; }
  .app.collapsed .nav a, .app.collapsed .side-btn { justify-content: flex-start; padding: 10px 12px; }
  .app.collapsed .brand { justify-content: flex-start; padding: 18px 16px; }
  .collapse-btn { display: none; }
  .menu-btn { display: grid; }
  .main { padding: 16px 16px 32px; }
  h1 { font-size: 22px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .range-note { margin-left: 0; text-align: left; width: 100%; }
}
@media (max-width: 560px) {
  .card { padding: 16px; }
  .kpi { padding: 14px; }
  .kpi-top { margin-bottom: 10px; }
  .chip { width: 34px; height: 34px; }
  .chip svg.i { width: 16px; height: 16px; }
  .kpi-value { font-size: 22px; }
  .kpi-label { font-size: 13px; }
  .kpi-prev { font-size: 12px; }
  .filters { padding: 12px; gap: 10px; }
  .filters > .field { flex: 1 1 140px; }
  .field select, .field input { width: 100%; }
  .custom { flex: 1 1 100%; }
  .custom .field { flex: 1; }
  .hide-sm { display: none; }
  .vis-list { grid-template-columns: minmax(0, 1fr); }
  .vis-row:nth-last-child(2) { border-bottom: 1px solid var(--border); }
  .card-head { flex-wrap: wrap; }
  table { font-size: 13.5px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
@media print {
  .sidebar, .filters .field, .icon-btn, .toggle, .more { display: none !important; }
  .app { grid-template-columns: 1fr; }
  .card { break-inside: avoid; box-shadow: none; }
}

/* Tables adapt to their card's width, not the viewport */
.table-scroll { container-type: inline-size; }
@container (max-width: 620px) { .hide-md { display: none; } }
@container (max-width: 440px) { .hide-sm { display: none; } }
@container (max-width: 440px) {
  td.label div { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; line-height: 1.35; }
  .rank { display: none; }
  th, td { padding-left: 6px; padding-right: 6px; }
}
.kpi-prev .short { display: none; }
@media (max-width: 560px) { .kpi-prev .long { display: none; } .kpi-prev .short { display: inline; } }

/* Marketing report page tabs: hidden views must not keep their grid space */
:root[data-report="marketing"] .stack [hidden] { display: none !important; }
