/* Spaceback Insights. Tokens follow the data-viz reference palette (validated in both
   modes); every colour is a role, so light and dark swap in one place. */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --hover: rgba(11, 11, 11, 0.05);
  --accent: #2a78d6;
  --danger: #d03b3b;
  /* categorical: platforms keep these everywhere */
  --s1: #2a78d6;   /* mac */
  --s2: #eb6834;   /* iphone */
  --s3: #1baf7a;   /* ipad */
  --sother: #b5b4ad;
  --sall: #52514e; /* "all platforms" line: ink, the emphasis series */
  /* ordinal ramp (one hue), lightest -> darkest */
  --o1: #86b6ef; --o2: #6da7ec; --o3: #5598e7; --o4: #2a78d6; --o5: #256abf; --o6: #1c5cab; --o7: #104281;
  --onone: #e1e0d9;
  /* sequential ramp for cohort cells */
  --q1: #cde2fb; --q2: #b7d3f6; --q3: #9ec5f4; --q4: #6da7ec; --q5: #3987e5; --q6: #256abf; --q7: #184f95;
  --q-ink-light: #0b0b0b;
  --q-ink-dark: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --hover: rgba(255, 255, 255, 0.06);
    --accent: #3987e5;
    --danger: #e66767;
    --s1: #3987e5;
    --s2: #d95926;
    --s3: #199e70;
    --sother: #6f6e69;
    --sall: #c3c2b7;
    /* ordinal on dark: never darker than step 600, lightest last */
    --o1: #184f95; --o2: #1c5cab; --o3: #256abf; --o4: #2a78d6; --o5: #3987e5; --o6: #6da7ec; --o7: #9ec5f4;
    --onone: #383835;
    /* sequential flips its anchor on dark: low values sit near the surface */
    --q1: #104281; --q2: #184f95; --q3: #1c5cab; --q4: #256abf; --q5: #3987e5; --q6: #6da7ec; --q7: #b7d3f6;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --hover: rgba(255, 255, 255, 0.06); --accent: #3987e5; --danger: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --sother: #6f6e69; --sall: #c3c2b7;
  --o1: #184f95; --o2: #1c5cab; --o3: #256abf; --o4: #2a78d6; --o5: #3987e5; --o6: #6da7ec; --o7: #9ec5f4;
  --onone: #383835;
  --q1: #104281; --q2: #184f95; --q3: #1c5cab; --q4: #256abf; --q5: #3987e5; --q6: #6da7ec; --q7: #b7d3f6;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); text-underline-offset: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------------ top bar */
.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
}
.brand { font-weight: 600; color: var(--ink); text-decoration: none; }
.top nav { display: flex; flex-wrap: wrap; gap: 4px; flex: 1; }
.top nav a {
  color: var(--ink-2); text-decoration: none; padding: 4px 10px; border-radius: 6px;
}
.top nav a:hover { background: var(--hover); }
.top nav a[aria-current="page"] { color: var(--ink); background: var(--hover); font-weight: 600; }
.top form { margin: 0; }
@media (max-width: 760px) {
  /* Phone: brand and sign-out share the first line; the page links get a line of their
     own that scrolls sideways instead of stacking into a tall column. */
  .top { justify-content: space-between; }
  .top nav { order: 3; flex: 1 0 100%; flex-wrap: nowrap; overflow-x: auto; }
  .top nav a { white-space: nowrap; }
}
.linkish {
  background: none; border: 0; color: var(--ink-2); font: inherit; cursor: pointer;
  padding: 4px 8px; border-radius: 6px;
}
.linkish:hover { background: var(--hover); color: var(--ink); }

main { max-width: 1440px; margin: 0 auto; padding: 16px; }
.heading h1 { font-size: 22px; margin: 4px 0 2px; }
.intro { color: var(--ink-2); margin: 0 0 12px; max-width: 80ch; }

/* ------------------------------------------------------------------ filters */
.filters {
  display: flex; flex-wrap: wrap; align-items: end; gap: 8px 14px;
  padding: 10px 12px; margin-bottom: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.filters label { display: flex; flex-direction: column; gap: 2px; color: var(--ink-2); font-size: 12px; }
.filters input, .filters select, .filters button, .login input, .login button {
  font: inherit; color: var(--ink); background: var(--page);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; min-height: 32px;
}
.filters button, .login button {
  background: var(--accent); color: #fff; border-color: transparent; cursor: pointer;
  padding: 5px 14px; font-weight: 600;
}
.presets { display: flex; gap: 4px; flex-wrap: wrap; margin-right: 6px; }
.presets a {
  text-decoration: none; color: var(--ink-2); padding: 5px 10px; border-radius: 6px;
  border: 1px solid var(--border); min-height: 32px; display: inline-flex; align-items: center;
}
.presets a:hover { background: var(--hover); }
.presets a[aria-current="true"] { color: var(--ink); font-weight: 600; background: var(--hover); }

/* ------------------------------------------------------------------ panels */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px 10px; min-width: 0;
}
.panel.wide { grid-column: 1 / -1; }
@media (max-width: 1000px) { .grid { grid-template-columns: minmax(0, 1fr); } }
.panel-head h2 { font-size: 15px; margin: 0; }
.note { color: var(--muted); margin: 2px 0 10px; font-size: 12.5px; }
.panel-body { min-width: 0; }
.panel-foot {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 14px;
  margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 12.5px;
}
.panel-foot .meta { color: var(--muted); font-variant-numeric: tabular-nums; }
.panel-foot details.sql { flex-basis: 100%; }
details summary { cursor: pointer; color: var(--ink-2); }
details.tableview { margin-top: 8px; font-size: 12.5px; }
pre {
  margin: 6px 0 0; padding: 10px; overflow: auto; max-height: 420px;
  background: var(--page); border: 1px solid var(--border); border-radius: 8px;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--ink-2);
  white-space: pre;
}
.error {
  color: var(--danger); background: var(--hover); padding: 8px 10px; border-radius: 8px;
  overflow-wrap: anywhere;
}
.empty { color: var(--muted); padding: 24px 0; text-align: center; margin: 0; }
.more { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

/* ------------------------------------------------------------------ stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.tile { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.tile-label { color: var(--ink-2); font-size: 12.5px; }
.tile-value { font-size: 24px; font-weight: 600; line-height: 1.25; margin-top: 2px; }
.tile-sub { color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------------ tables */
.table-wrap { overflow: auto; max-height: 560px; }
table.data { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.data th, table.data td {
  padding: 5px 8px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap;
}
table.data th { color: var(--ink-2); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: var(--hover); }
table.heatmap td.heat { color: var(--q-ink-light); min-width: 64px; }
td.q0 { background: transparent; color: var(--muted) !important; }
td.q1 { background: var(--q1); } td.q2 { background: var(--q2); } td.q3 { background: var(--q3); }
td.q4 { background: var(--q4); } td.q5 { background: var(--q5); color: var(--q-ink-dark) !important; }
td.q6 { background: var(--q6); color: var(--q-ink-dark) !important; }
td.q7 { background: var(--q7); color: var(--q-ink-dark) !important; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) table.heatmap td.heat { color: var(--q-ink-dark); }
  :root:not([data-theme="light"]) td.q6, :root:not([data-theme="light"]) td.q7 { color: var(--q-ink-light) !important; }
  :root:not([data-theme="light"]) td.q5 { color: var(--q-ink-dark) !important; }
}

/* ------------------------------------------------------------------ charts */
svg.chart { display: block; width: 100%; height: auto; overflow: visible; }
svg.chart text { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
.tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.cat { fill: var(--ink-2); font-size: 12px; }
.val { fill: var(--ink); font-size: 12px; font-variant-numeric: tabular-nums; }
line.grid { stroke: var(--grid); stroke-width: 1; }
line.axis { stroke: var(--axis); stroke-width: 1; }
.xhair { stroke: var(--axis); stroke-width: 1; opacity: 0; }
.band .hit { fill: transparent; }
.band:hover .hit { fill: var(--hover); }
.band:hover .xhair { opacity: 1; }

path.s1, rect.s1 { fill: var(--s1); }
path.s2, rect.s2 { fill: var(--s2); }
path.s3, rect.s3 { fill: var(--s3); }
path.sother, rect.sother { fill: var(--sother); }
path.sall, rect.sall { fill: var(--sall); }
.ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
polyline.s1, line.s1 { stroke: var(--s1); }
polyline.s2, line.s2 { stroke: var(--s2); }
polyline.s3, line.s3 { stroke: var(--s3); }
polyline.sother, line.sother { stroke: var(--sother); }
polyline.sall, line.sall { stroke: var(--sall); }
circle.dot { stroke: var(--surface); stroke-width: 2; }
circle.s1 { fill: var(--s1); } circle.s2 { fill: var(--s2); } circle.s3 { fill: var(--s3); }
circle.sother { fill: var(--sother); } circle.sall { fill: var(--sall); }
.o1 { fill: var(--o1); } .o2 { fill: var(--o2); } .o3 { fill: var(--o3); } .o4 { fill: var(--o4); }
.o5 { fill: var(--o5); } .o6 { fill: var(--o6); } .o7 { fill: var(--o7); } .onone { fill: var(--onone); }

.legend {
  display: flex; flex-wrap: wrap; gap: 4px 14px; list-style: none; margin: 0 0 6px; padding: 0;
  color: var(--ink-2); font-size: 12px;
}
.legend li { display: inline-flex; align-items: center; gap: 6px; }
svg.key { width: 14px; height: 10px; }

.multiples { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px 20px; }
.multiple h4 { margin: 0 0 4px; font-size: 13px; font-weight: 600; color: var(--ink-2); }

/* ------------------------------------------------------------------ login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card {
  width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px;
}
.login h1 { font-size: 18px; margin: 0; }
.login label { color: var(--ink-2); font-size: 12.5px; margin-top: 6px; }
