:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --text: #1c1d21;
  --muted: #6b6d75;
  --border: #e2e0d8;
  --accent: #1f6f50;
  --accent-text: #ffffff;
  --focus: #2f6fdf;

  /* Magnitude colours: the heart of the game */
  --unit-M: #1d8a5a;
  --unit-B: #2f62d0;
  --unit-T: #a0359e;
  --unit-M-bg: #dff3e8;
  --unit-B-bg: #e2eafc;
  --unit-T-bg: #f6e0f5;

  --g-bullseye: #1d8a5a;
  --g-great: #5b9a2a;
  --g-ok: #c7861a;
  --g-miss: #c8443a;

  --cat-gdp: #2f62d0;
  --cat-gov: #7a5bd0;
  --cat-mcap: #1d8a8a;
  --cat-rev: #c7861a;
  --cat-fine: #c8443a;

  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131417; --surface: #1c1e22; --surface-2: #25282d; --text: #ecebe6; --muted: #9a9ca3;
    --border: #2f3238; --accent: #3fae80; --accent-text: #0d1512; --focus: #7aa2ff;
    --unit-M: #4cc98e; --unit-B: #7ea3ff; --unit-T: #e07ade;
    --unit-M-bg: #173326; --unit-B-bg: #1b2644; --unit-T-bg: #3a1c39;
    --g-bullseye: #4cc98e; --g-great: #97c95a; --g-ok: #e4ac4a; --g-miss: #ef7a70;
    --cat-gdp: #7ea3ff; --cat-gov: #ad96f0; --cat-mcap: #4cc9c9; --cat-rev: #e4ac4a; --cat-fine: #ef7a70;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #131417; --surface: #1c1e22; --surface-2: #25282d; --text: #ecebe6; --muted: #9a9ca3;
  --border: #2f3238; --accent: #3fae80; --accent-text: #0d1512; --focus: #7aa2ff;
  --unit-M: #4cc98e; --unit-B: #7ea3ff; --unit-T: #e07ade;
  --unit-M-bg: #173326; --unit-B-bg: #1b2644; --unit-T-bg: #3a1c39;
  --g-bullseye: #4cc98e; --g-great: #97c95a; --g-ok: #e4ac4a; --g-miss: #ef7a70;
  --cat-gdp: #7ea3ff; --cat-gov: #ad96f0; --cat-mcap: #4cc9c9; --cat-rev: #e4ac4a; --cat-fine: #ef7a70;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
a { color: var(--focus); }
h1, h2, h3, h4 { line-height: 1.2; margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px max(16px, calc((100vw - 1180px) / 2));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 800;
}
.status { display: flex; align-items: center; gap: 10px; font-size: 0.9rem; }
.budget { width: 120px; height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.budget-fill { display: block; height: 100%; background: var(--accent); transition: width 0.4s; }
.budget-fill.low { background: var(--g-miss); }

main { max-width: 1180px; margin: 0 auto; padding: 24px 16px 64px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow);
}

/* ---------- buttons ---------- */
.primary {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px;
  padding: 10px 20px; font-weight: 600;
}
.primary:disabled { opacity: 0.4; cursor: not-allowed; }
.primary.big { padding: 14px 32px; font-size: 1.05rem; }
.ghost { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; color: var(--muted); font-size: 0.875rem; }
.ghost:hover { color: var(--text); }

/* ---------- setup ---------- */
.setup { max-width: 760px; margin: 0 auto; }
.setup h1, .over h1 { font-size: clamp(1.8rem, 5vw, 2.6rem); margin-top: 16px; }
.lede { color: var(--muted); font-size: 1.1rem; max-width: 60ch; }
fieldset { border: 0; padding: 0; margin: 28px 0 0; }
legend { font-weight: 700; margin-bottom: 10px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.choices.four { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.choice { position: relative; }
.choice input { position: absolute; opacity: 0; }
.choice span {
  display: block; height: 100%; padding: 14px 16px; border-radius: 12px; cursor: pointer;
  border: 1.5px solid var(--border); background: var(--surface);
}
.choice small { display: block; color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.choice input:checked + span { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.choice input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip input { position: absolute; opacity: 0; }
.chip span {
  display: inline-block; padding: 8px 14px; border-radius: 999px; border: 1.5px solid var(--border);
  background: var(--surface); cursor: pointer; color: var(--muted);
}
.chip small { opacity: 0.7; margin-left: 2px; }
.chip input:checked + span { border-color: var(--accent); color: var(--text); background: var(--surface); box-shadow: inset 0 0 0 1px var(--accent); }
.chip input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.setup .primary.big { margin-top: 32px; }

/* ---------- play layout ---------- */
.play { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 20px; align-items: start; }
.play-main { display: grid; gap: 20px; min-width: 0; }
@media (max-width: 900px) {
  .play { grid-template-columns: minmax(0, 1fr); }
  /* On narrow screens the reference list sits right under the question: it's what you reason with. */
  .play-main { display: contents; }
  #question-card { order: 1; }
  .refs { order: 2; }
  .chart-card { order: 3; }
}

.q-meta { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 0.85rem; }
.cat-chip {
  font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--surface-2);
  border-left: 4px solid var(--cat, var(--muted));
}
.dots { color: var(--muted); letter-spacing: 2px; }
.asof { color: var(--muted); margin-left: auto; }
.q-prompt { font-size: clamp(1.35rem, 3.4vw, 1.9rem); margin: 14px 0 22px; text-wrap: balance; }

[data-cat="GDP"] { --cat: var(--cat-gdp); }
[data-cat="Government spending"] { --cat: var(--cat-gov); }
[data-cat="Market cap"] { --cat: var(--cat-mcap); }
[data-cat="Revenue"] { --cat: var(--cat-rev); }
[data-cat="Fines & settlements"] { --cat: var(--cat-fine); }

/* guess input */
.guess-row { display: flex; align-items: stretch; gap: 8px; flex-wrap: wrap; }
.dollar { align-self: center; font-size: 1.6rem; color: var(--muted); font-weight: 600; }
#guess-input {
  flex: 1 1 140px; min-width: 0; font: inherit; font-size: 1.6rem; font-weight: 600;
  padding: 8px 14px; border-radius: 10px; border: 1.5px solid var(--border); background: var(--bg); color: var(--text);
}
#guess-input:focus { border-color: var(--focus); outline: none; }
.units { display: flex; gap: 6px; }
.unit-btn {
  width: 56px; border-radius: 10px; font-weight: 800; font-size: 1.2rem;
  border: 1.5px solid var(--u); color: var(--u); background: var(--surface);
}
.unit-btn[aria-checked="true"] { background: var(--u); color: var(--surface); }
.unit-btn.unit-M, .unit.unit-M { --u: var(--unit-M); --ubg: var(--unit-M-bg); }
.unit-btn.unit-B, .unit.unit-B { --u: var(--unit-B); --ubg: var(--unit-B-bg); }
.unit-btn.unit-T, .unit.unit-T { --u: var(--unit-T); --ubg: var(--unit-T-bg); }
.preview { min-height: 1.6em; margin: 12px 0 16px; font-size: 1.05rem; }
.preview .eq { color: var(--muted); }
.preview .hint { color: var(--muted); }

/* amounts */
.unit {
  display: inline-block; padding: 0 0.45em; border-radius: 6px; font-weight: 700;
  color: var(--u); background: var(--ubg); font-size: 0.85em; line-height: 1.5;
}
.num { font-weight: 700; }
.amount { font-size: 1.4rem; }
.amount.big { font-size: 2.2rem; }
.full { color: var(--muted); font-size: 0.85rem; word-break: break-all; }

/* reveal */
.reveal-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; align-items: end; }
@media (max-width: 520px) { .reveal-grid { grid-template-columns: 1fr; } }
.label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 700; }
.verdict {
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  margin: 18px 0 0; padding: 10px 14px; border-radius: 10px; background: var(--surface-2);
  border-left: 4px solid var(--g);
}
.verdict strong { color: var(--g); }
.verdict span { color: var(--muted); }
.grade-bullseye { --g: var(--g-bullseye); }
.grade-great { --g: var(--g-great); }
.grade-ok { --g: var(--g-ok); }
.grade-miss { --g: var(--g-miss); }
.context { font-size: 1.05rem; margin: 16px 0 6px; }
.source { color: var(--muted); font-size: 0.85rem; margin: 0 0 18px; }

/* chart */
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.chart-head h3 { font-size: 1rem; }
.chart-foot { display: flex; justify-content: flex-end; margin-top: 8px; }
svg { display: block; width: 100%; height: auto; }
svg .grid { stroke: var(--border); }
svg .tick { fill: var(--muted); font-size: 11px; }
svg .bar { fill: var(--g); opacity: 0.85; }
svg .avg { fill: none; stroke: var(--text); stroke-width: 2; stroke-linejoin: round; }
.chart-empty { margin: 8px 0; }

/* reference list */
.refs { position: sticky; top: 76px; max-height: calc(100vh - 96px); overflow: auto; }
.refs h3 { font-size: 1rem; margin-bottom: 6px; }
@media (max-width: 900px) { .refs { position: static; max-height: none; } }
.ref-group { margin-top: 14px; }
.ref-group h4 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.ref-group.current h4 { color: var(--text); }
.cat-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--cat, var(--muted)); }
.ref-group ol { list-style: none; margin: 6px 0 0; padding: 0; }
.ref {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 8px;
  padding: 5px 6px; border-radius: 8px; font-size: 0.9rem;
}
.ref-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ref-name small { display: block; color: var(--muted); font-size: 0.75rem; overflow: hidden; text-overflow: ellipsis; }
.ref-val { white-space: nowrap; font-size: 0.85rem; }
.ref .bar { grid-column: 1 / -1; height: 3px; margin-top: 3px; background: var(--surface-2); border-radius: 2px; }
.ref .bar span { display: block; height: 100%; border-radius: 2px; background: var(--cat, var(--muted)); opacity: 0.7; }
.ref-group[data-cat] .bar span { background: var(--cat); }
.ref.ghost { outline: 1.5px dashed var(--focus); background: var(--surface-2); }
.ref.ghost .bar span { background: var(--focus); }
.ref.fresh { background: var(--surface-2); animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--unit-B-bg); } }

/* game over */
.over { max-width: 860px; margin: 0 auto; display: grid; gap: 20px; }
.over h1 { margin-top: 8px; }
.over .lede { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.stat-val { font-size: 1.3rem; font-weight: 700; }
.stat-val small { display: block; font-size: 0.8rem; font-weight: 500; color: var(--muted); }
.over-actions { display: flex; justify-content: center; }
.over h3 { font-size: 1rem; margin-bottom: 8px; }
.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 12px; align-items: center;
  padding: 8px 0; border-top: 1px solid var(--border); font-size: 0.9rem;
}
.h-verdict { color: var(--g); font-weight: 600; white-space: nowrap; }
.h-vals { white-space: nowrap; }
@media (max-width: 600px) {
  .history li { grid-template-columns: 1fr auto; }
  .h-prompt { grid-column: 1 / -1; }
}

.error { color: var(--g-miss); background: var(--surface); padding: 16px; border-radius: 12px; border: 1px solid var(--border); }

@media (max-width: 520px) {
  .unit-btn { width: 48px; }
  .card { padding: 16px; }
  .budget { width: 72px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
