: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);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --deemph: #d3d2cb;
  --wash: rgba(42, 120, 214, 0.08);
  --critical: #d03b3b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --div-neg: #c33b3b; --div-mid: #f0efec; --div-pos: #1c5cab;
  --seq-lo: #e7f0fb; --seq-hi: #104281;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --deemph: #4a4945; --wash: rgba(57, 135, 229, 0.12);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --div-neg: #e66767; --div-mid: #383835; --div-pos: #3987e5;
    --seq-lo: #22272e; --seq-hi: #6da7ec;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --deemph: #4a4945; --wash: rgba(57, 135, 229, 0.12);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --div-neg: #e66767; --div-mid: #383835; --div-pos: #3987e5;
  --seq-lo: #22272e; --seq-hi: #6da7ec;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
/* Side padding clears the notch / rounded corners in landscape (viewport-fit=cover). */
main, .top {
  max-width: 960px; margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}
.top {
  padding-top: max(32px, env(safe-area-inset-top)); padding-bottom: 8px;
  display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; column-gap: 12px;
}
.brand { display: contents; }
.top h1 { grid-area: 1 / 1; white-space: nowrap; }
.top .theme-switch { grid-area: 1 / 2; }
.top .status { grid-area: 2 / 1 / 3 / 3; }
.theme-switch, .view-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; background: var(--surface); }
.theme-switch button, .view-switch button {
  border: 0; background: transparent; color: var(--ink-2); padding: 3px 10px; border-radius: 6px; font-size: 13px;
  flex: none; white-space: nowrap;
}
.theme-switch button[aria-checked="true"], .view-switch button[aria-selected="true"] {
  background: var(--wash); color: var(--ink); font-weight: 600;
}
/* Too many chart types for a narrow card: the switch scrolls sideways instead of the page. */
.view-switch { max-width: 100%; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.view-switch::-webkit-scrollbar { display: none; }
/* Hover styles only where there's a real hover (on touch they stick after a tap). */
@media (hover: hover) {
  .theme-switch button:hover, .view-switch button:hover { color: var(--ink); }
  .history-bar button:hover, .act:hover { color: var(--ink); border-color: var(--border); background: var(--page); }
  .followup button:hover { color: var(--ink); }
  .examples button:hover { color: var(--ink); border-color: var(--axis); }
  th:hover { color: var(--ink); }
  tbody tr:hover td { background: var(--wash); }
  tbody tr:hover td:first-child { background: linear-gradient(var(--wash), var(--wash)), var(--surface); }
}

.history-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  margin: 18px 0 0; font-size: 13px; color: var(--muted);
}
.history-bar button, .act {
  background: none; border: 1px solid transparent; color: var(--ink-2); border-radius: 6px;
  padding: 2px 8px; font-size: 12.5px;
}
.card-head { position: relative; }
.card-actions { float: right; display: flex; gap: 2px; margin: -4px -8px 0 8px; }
.act.remove { font-size: 16px; line-height: 1; padding: 2px 8px 4px; }
.act.remove .lbl { display: none; }
.pending .card-actions, .card-actions[hidden] { display: none; }
.question .when { margin-left: 6px; }
h1 { font-size: 22px; font-weight: 650; margin: 0; letter-spacing: -0.01em; }
.status { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.ask { position: sticky; top: 0; z-index: 5; background: var(--page); padding: 12px 0 8px; }
.ask-row {
  display: flex; gap: 8px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 8px 8px 14px; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.ask-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--wash); }
textarea {
  flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--ink);
  font: inherit; font-size: 16px; padding: 6px 0; max-height: 160px;
}
textarea::placeholder { color: var(--muted); }
button { font: inherit; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:active:not(:disabled) { opacity: 0.7; }
#ask-btn {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 8px;
  padding: 8px 18px; font-weight: 600;
}
#ask-btn:disabled { opacity: 0.5; cursor: progress; }
.followup {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  font-size: 13px; color: var(--ink-2); padding: 0 4px 6px;
}
.followup b { font-weight: 550; color: var(--ink); }
.followup button { background: none; border: 0; color: var(--muted); padding: 0; font-size: 13px; white-space: nowrap; }
.warning {
  margin-top: 8px; padding: 10px 14px; border-radius: 8px; font-size: 14px;
  border: 1px solid var(--critical); color: var(--ink);
}
.examples { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.examples button {
  background: var(--surface); border: 1px solid var(--border); color: var(--ink-2);
  border-radius: 999px; padding: 4px 12px; font-size: 13px;
}

.results { display: flex; flex-direction: column; gap: 16px; padding: 8px 0 calc(64px + env(safe-area-inset-bottom)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px 20px 14px; min-width: 0;
}
.question { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.question::before { content: "Q  "; font-weight: 600; }
.title { margin: 0; font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }
.subtitle { margin: 2px 0 0; color: var(--ink-2); font-size: 13px; }
.subtitle:empty { display: none; }
.summary { margin: 12px 0 0; font-size: 15px; color: var(--ink); min-height: 1.5em; }
.summary.loading { color: var(--muted); }
.summary.clarify { color: var(--ink-2); }
.summary.error { color: var(--critical); }

.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 14px 0 8px; flex-wrap: wrap; }
.view-switch:empty { display: none; }
.meta { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.viz { position: relative; min-width: 0; }
.chart-box { position: relative; width: 100%; }

.assumptions { list-style: none; padding: 0; margin: 12px 0 0; display: flex; flex-wrap: wrap; gap: 6px; }
.assumptions:empty { display: none; }
.assumptions li {
  font-size: 12px; color: var(--ink-2); background: var(--page); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 8px;
}
.assumptions li::before { content: "Assumed: "; color: var(--muted); }

.sql { margin-top: 12px; border-top: 1px solid var(--grid); padding-top: 10px; }
.sql summary { cursor: pointer; color: var(--ink-2); font-size: 13px; }
.sql-wrap { position: relative; }
.sql pre {
  margin: 8px 0 0; padding: 12px; background: var(--page); border: 1px solid var(--border); border-radius: 8px;
  overflow-x: auto; font: 12.5px/1.5 var(--mono); color: var(--ink);
}
.copy {
  position: absolute; top: 6px; right: 6px; font-size: 12px; background: var(--surface); color: var(--ink-2);
  border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px;
}

/* stat tiles */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; background: var(--page); }
.stat .label { font-size: 12px; color: var(--ink-2); }
.stat .value { font-size: 30px; font-weight: 650; letter-spacing: -0.02em; line-height: 1.2; margin-top: 2px; }

/* table */
.table-wrap { max-height: min(480px, 70vh); overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 6px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--grid); }
th {
  position: sticky; top: 0; background: var(--surface); font-weight: 600; color: var(--ink-2);
  cursor: pointer; user-select: none; border-bottom: 1px solid var(--axis);
}
th .arrow { color: var(--muted); font-size: 10px; margin-left: 3px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
/* The first column (usually the name) stays put when a wide table scrolls sideways. */
td:first-child, th:first-child { position: sticky; left: 0; background: var(--surface); box-shadow: inset -1px 0 var(--grid); }
th:first-child { z-index: 2; }
tr:last-child td { border-bottom: 0; }
tr.hl td { font-weight: 650; }
.truncated { font-size: 12px; color: var(--muted); margin: 6px 0 0; }

/* heatmap */
.heat { display: grid; gap: 2px; font-size: 13px; overflow-x: auto; }
.heat .hd { color: var(--ink-2); font-size: 12px; padding: 4px; display: flex; align-items: flex-end; justify-content: center; text-align: center; }
.heat .rh { color: var(--ink-2); font-size: 12px; padding: 4px 8px 4px 0; display: flex; align-items: center; justify-content: flex-end; white-space: nowrap; }
.heat .rh, .heat .corner { position: sticky; left: 0; z-index: 1; background: var(--surface); }
.heat .cell {
  border-radius: 4px; min-height: 40px; display: flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums; font-weight: 550; position: relative;
}
.heat .cell.empty { background: transparent; border: 1px dashed var(--grid); }
.heat-legend { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 12px; color: var(--ink-2); }
.heat-legend .ramp { width: 160px; height: 8px; border-radius: 4px; }
.heat-axis { font-size: 12px; color: var(--muted); margin: 0 0 6px; }

.empty-state { color: var(--ink-2); font-size: 14px; padding: 16px 0; }
.pending .viz { min-height: 120px; }
.shimmer {
  height: 220px; border-radius: 8px;
  background: linear-gradient(90deg, var(--page) 0%, var(--grid) 50%, var(--page) 100%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite linear; opacity: 0.6;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .shimmer { animation: none; } }

/* Touch screens (and narrow windows): every control at least 44x44 CSS px
   (WCAG 2.5.5 / Apple HIG; Material asks 48dp, WCAG 2.2 AA's floor is 24). */
@media (pointer: coarse), (max-width: 600px) {
  .theme-switch button, .view-switch button { min-height: 44px; min-width: 44px; padding: 0 14px; }
  .theme-switch button { padding: 0 8px; }
  .history-bar button, .act { min-height: 44px; min-width: 44px; padding: 0 12px; font-size: 13px; }
  .followup { padding: 0 0 4px 4px; }
  .followup button { min-height: 44px; padding: 0 4px 0 12px; }
  textarea { min-height: 44px; padding: 10px 0; }
  .ask-row { padding: 6px 6px 6px 14px; align-items: center; }
  #ask-btn { min-height: 44px; padding: 0 20px; }
  .examples { gap: 8px; }
  .examples button { min-height: 44px; padding: 0 16px; }
  .sql { padding-top: 0; }
  .sql summary { padding: 12px 0; line-height: 20px; }
  .copy { position: static; display: block; min-height: 44px; min-width: 44px; margin: 4px 0 0 auto; padding: 0 14px; font-size: 13px; }
  .sql-wrap { display: flex; flex-direction: column; }
  th { height: 44px; }
}

@media (max-width: 600px) {
  .top { padding-top: max(20px, env(safe-area-inset-top)); }
  .examples {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain;
    margin-right: min(-16px, calc(-1 * env(safe-area-inset-right))); padding-right: 16px;
  }
  .examples::-webkit-scrollbar { display: none; }
  .examples button { white-space: nowrap; flex: none; }
  .card { padding: 16px 14px 4px; display: flex; flex-direction: column; }
  .card.pending { padding-bottom: 14px; }
  /* Card actions move from the corner to a labelled row at the bottom, so the question gets the
     full width and each action is a full-size target. */
  .card-head { display: contents; }
  .card-actions {
    order: 1; float: none; margin: 4px -12px 0; gap: 4px;
    border-top: 1px solid var(--grid); padding-top: 4px;
  }
  .act.remove { margin-left: auto; font-size: 13px; padding: 0 12px; }
  .act.remove .x { display: none; }
  .act.remove .lbl { display: inline; }
  .sql { margin-top: 8px; }
  .toolbar .view-switch { width: 100%; }
  .view-switch button { flex: 1 0 auto; }
  .stats { grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; }
  .stat .value { font-size: 24px; }
  .title { font-size: 17px; }
}
