/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);

  /* categorical (validated reference palette, fixed order) */
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s4: #eda100;
  --s5: #e87ba4;
  --s6: #008300;
  --s7: #6250d6;
  --s8: #e34948;

  /* sequential blue */
  --q1: #cde2fb; --q2: #9ec5f4; --q3: #6da7ec; --q4: #3987e5; --q5: #256abf; --q6: #184f95; --q7: #0d366b;

  /* status (fixed) */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --good-text: #006300;
  --bad-text: #b42323;

  /* data sources (deck colors) */
  --src-a: #2f6fb0;
  --src-b: #2e7d6b;
  --src-c: #7a4fa8;

  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --good-text: #0ca30c;
    --bad-text: #e66767;
    --src-a: #5b95d1; --src-b: #4fa48f; --src-c: #a07fcb;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --good-text: #0ca30c;
  --bad-text: #e66767;
  --src-a: #5b95d1; --src-b: #4fa48f; --src-c: #a07fcb;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }

.app { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 20px 14px;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 20px; }
.brand-mark { width: 30px; height: 30px; border-radius: 50% 50% 46% 54%; background: linear-gradient(135deg, var(--src-a), var(--src-b)); flex: none; }
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--muted); }
#nav { display: flex; flex-direction: column; gap: 2px; }
.nav-group { font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding: 14px 10px 6px; }
.nav-link {
  display: flex; align-items: center; gap: 8px; justify-content: space-between;
  padding: 7px 10px; border-radius: 8px; color: var(--ink-2); text-decoration: none; font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); color: var(--ink); }
.nav-link[aria-current="page"] { background: var(--accent-wash); color: var(--ink); font-weight: 600; }
.nav-pri { font-size: 11px; color: var(--muted); font-weight: 600; }
.sidebar-foot { margin-top: auto; padding: 16px 8px 0; border-top: 1px solid var(--border); }
.legend-sources { display: grid; grid-template-columns: auto 1fr; gap: 6px 8px; align-items: center; font-size: 12px; color: var(--ink-2); }

/* ---------- Topbar & filters ---------- */
.main { min-width: 0; padding: 0 28px 48px; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 22px 0 14px; }
.page-heading { flex: 1; min-width: 0; }
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); }
.page-heading h1 { font-size: 24px; font-weight: 700; line-height: 1.2; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.mock-badge { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); border: 1px dashed var(--axis); }
.icon-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; width: 34px; height: 34px; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.menu-btn { display: none; }

.filters {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  padding: 10px 0 18px; position: sticky; top: 0; z-index: 5; background: var(--page);
}
.filter { display: flex; flex-direction: column; gap: 2px; }
.filter label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.filter select {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: 6px 30px 6px 10px; font: inherit; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 52%, calc(100% - 10px) 52%; background-size: 5px 5px; background-repeat: no-repeat;
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 6px 12px; cursor: pointer; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--accent-wash); color: var(--ink); font-weight: 600; }
.filter-note { font-size: 12px; color: var(--muted); margin-left: auto; align-self: flex-end; padding-bottom: 6px; }

/* ---------- Layout ---------- */
.content { display: flex; flex-direction: column; gap: 18px; transition: opacity .15s; }
.content.refetch { opacity: .55; }
.grid { display: grid; gap: 18px; grid-template-columns: repeat(12, minmax(0, 1fr)); }
.col-3 { grid-column: span 3; } .col-4 { grid-column: span 4; } .col-5 { grid-column: span 5; }
.col-6 { grid-column: span 6; } .col-7 { grid-column: span 7; } .col-8 { grid-column: span 8; } .col-12 { grid-column: span 12; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; min-width: 0; }
.card-head { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; margin-bottom: 14px; }
.card-title { font-size: 15px; font-weight: 650; }
.card-sub { font-size: 12.5px; color: var(--ink-2); margin-top: 2px; max-width: 70ch; }
.card-meta { display: flex; gap: 6px; align-items: center; flex: none; }
.card-foot { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.ghost-btn { border: 1px solid var(--border); background: transparent; border-radius: 6px; padding: 3px 8px; font-size: 12px; cursor: pointer; color: var(--ink-2); }
.ghost-btn:hover { background: var(--surface-2); }

.src { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 5px; font-size: 11px; font-weight: 700; color: #fff; }
.src-a { background: var(--src-a); } .src-b { background: var(--src-b); } .src-c { background: var(--src-c); }

/* ---------- Stat tiles ---------- */
.tiles { display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.tile-label { font-size: 12.5px; color: var(--ink-2); display: flex; justify-content: space-between; gap: 6px; }
.tile-value { font-size: 24px; font-weight: 650; line-height: 1.2; white-space: nowrap; }
.tile-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; }
.tile-row svg { flex-shrink: 1; min-width: 0; }
.tile-label > span:last-child { display: inline-flex; gap: 3px; flex: none; }
.delta { font-size: 12px; font-weight: 600; }
.delta.good { color: var(--good-text); } .delta.bad { color: var(--bad-text); } .delta.flat { color: var(--muted); }
.tile-note { font-size: 11.5px; color: var(--muted); }

.hero { display: flex; flex-direction: column; gap: 6px; }
.hero-value { font-size: 52px; font-weight: 700; line-height: 1.05; letter-spacing: -0.01em; }
.hero-label { font-size: 13px; color: var(--ink-2); }

/* ---------- Status ---------- */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.status i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; font-style: normal; }
.status.good i { background: var(--good); } .status.warning i { background: var(--warning); }
.status.serious i { background: var(--serious); } .status.critical i { background: var(--critical); }
.status.neutral i { background: var(--axis); }

.callout { border: 1px solid var(--border); border-left: 4px solid var(--warning); background: var(--surface); border-radius: 10px; padding: 10px 14px; font-size: 13px; color: var(--ink-2); }
.callout b { color: var(--ink); }

/* ---------- Charts ---------- */
.chart { width: 100%; position: relative; }
.chart svg { display: block; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-line { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .end-label { fill: var(--ink-2); font-size: 11.5px; font-weight: 600; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; }
.chart .ref-label { fill: var(--muted); font-size: 11px; }
.chart .ref-line { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .mark:hover, .chart .mark.hot { filter: brightness(1.12); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12.5px; color: var(--ink-2); margin-bottom: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend .ln { width: 14px; height: 2px; border-radius: 2px; display: inline-block; }

/* HTML bar rows */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: var(--label-w, 170px) minmax(0, 1fr) var(--value-w, 92px); gap: 10px; align-items: center; border-radius: 6px; padding: 1px 0; }
.bar-row:hover { background: var(--surface-2); }
.bar-label { font-size: 12.5px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-label small { color: var(--muted); }
.bar-track { position: relative; height: 18px; }
.bar-fill { position: absolute; left: 0; top: 0; height: 18px; display: flex; gap: 2px; }
.bar-seg { height: 100%; }
.bar-seg:last-child { border-radius: 0 4px 4px 0; }
.bar-value { font-size: 12.5px; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.bar-ref { position: absolute; top: -3px; bottom: -3px; width: 0; border-left: 1px dashed var(--muted); }
.bar-ref-label { position: absolute; top: -18px; transform: translateX(-50%); font-size: 10.5px; color: var(--muted); white-space: nowrap; }

/* Funnel */
.funnel { display: flex; flex-direction: column; gap: 4px; }
.funnel-step { display: grid; grid-template-columns: 190px minmax(0, 1fr) 90px; gap: 12px; align-items: center; }
.funnel-name { font-size: 13px; color: var(--ink-2); }
.funnel-name small { display: block; font-size: 11px; color: var(--muted); }
.funnel-bar { height: 26px; background: var(--s1); border-radius: 0 4px 4px 0; }
.funnel-val { font-weight: 650; text-align: right; font-variant-numeric: tabular-nums; }
.funnel-conv { grid-column: 2 / 3; font-size: 11.5px; color: var(--muted); padding: 0 0 2px 4px; }
.funnel-conv b { color: var(--ink-2); }

/* Tables */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th { text-align: left; font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--axis); white-space: nowrap; }
table.data td { padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data tr.child td:first-child { padding-left: 30px; color: var(--ink-2); }
table.data tr.parent { cursor: pointer; }
table.data tr.parent:hover td { background: var(--surface-2); }
table.data tr.total td { font-weight: 650; border-top: 1px solid var(--axis); }
.caret { display: inline-block; width: 14px; color: var(--muted); }
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--border); }
.null { color: var(--muted); font-style: italic; }

/* Heatmap cells */
.heat td.cell { text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; border: 2px solid var(--surface); border-radius: 4px; }

/* Meter */
.meter { height: 10px; border-radius: 999px; background: var(--q1); overflow: hidden; }
.meter > div { height: 100%; border-radius: 999px; background: var(--s1); }
.meter.warn > div { background: var(--warning); }
.meter.over > div { background: var(--critical); }

/* Tooltip */
.tooltip {
  position: fixed; z-index: 50; pointer-events: none; opacity: 0; transition: opacity .08s;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .14); padding: 8px 10px; font-size: 12.5px; min-width: 140px; max-width: 280px;
}
.tt-title { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.tt-row { display: flex; align-items: center; gap: 8px; }
.tt-row strong { font-variant-numeric: tabular-nums; }
.tt-row .tt-label { color: var(--ink-2); }
.tt-key { width: 12px; height: 2px; border-radius: 2px; flex: none; }

.empty-note { font-size: 12.5px; color: var(--muted); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .col-3, .col-4, .col-5 { grid-column: span 6; }
  .col-7, .col-8 { grid-column: span 12; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; left: 0; top: 0; z-index: 30; width: 260px; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .25); }
  .menu-btn { display: inline-block; }
  .main { padding: 0 16px 40px; }
  .col-3, .col-4, .col-5, .col-6 { grid-column: span 12; }
  .bar-row { grid-template-columns: 110px minmax(0, 1fr) 70px; }
  .funnel-step { grid-template-columns: 120px minmax(0, 1fr) 64px; }
  .hero-value { font-size: 44px; }
  .filter-note { margin-left: 0; }
}
