/* Tokens: dataviz reference palette (light, then dark selected step-for-step). */
:root {
  color-scheme: light;
  --page: #f4f3ef;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #4a4945;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: #e1e0d9;
  --series: #2a78d6;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c4372f;
  --marker: #9a7b16;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #121211;
    --surface: #1a1a19;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: #2c2c2a;
    --series: #3987e5;
    --accent: #3987e5;
    --accent-ink: #0b0b0b;
    --danger: #e5655c;
    --marker: #c9a43a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { background: var(--page); }
body {
  margin: 0;
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--page);
}
[hidden] { display: none !important; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
h1, h2, h3 { line-height: 1.25; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 16px 0 4px; }

.topbar { padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--surface); }
.topbar h1 { font-size: 18px; margin: 0; }
.topbar .sub { font-weight: 400; color: var(--muted); font-size: 14px; margin-left: 6px; }
main { max-width: 1400px; margin: 0 auto; padding: 20px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
}
.card-title { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 8px; }
.hint { color: var(--text-2); font-size: 13px; margin: 6px 0; }
.error { color: var(--danger); font-weight: 500; margin: 8px 0 0; }

/* ---------- landing ---------- */
.prose { max-width: 72ch; }
.lede { font-size: 16px; }
.landing-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; margin: 20px 0; }
table.ref { width: 100%; border-collapse: collapse; font-size: 13px; }
table.ref th { text-align: left; color: var(--muted); font-weight: 500; border-bottom: 1px solid var(--border); padding: 4px 6px; }
table.ref td { border-bottom: 1px solid var(--border); padding: 5px 6px; vertical-align: top; }
table.ref .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.ref.cmds td:first-child { font-family: ui-monospace, Menlo, monospace; font-size: 12px; white-space: nowrap; }
.landing-grid .card { overflow-x: auto; }
ul.plain { padding-left: 18px; margin: 0; }
ul.plain li { margin: 4px 0; }
.start { margin: 18px 0 4px; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.start label { color: var(--text-2); }
.start input { flex: 0 1 280px; min-width: 0; }
.start .error { flex-basis: 100%; }

/* ---------- controls ---------- */
input[type="text"], input[type="number"], select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--axis); border-radius: 6px; padding: 6px 8px;
}
button {
  font: inherit; cursor: pointer; border-radius: 6px; padding: 6px 14px;
  border: 1px solid var(--axis); background: var(--surface); color: var(--text);
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 6px 4px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.controls { display: grid; grid-template-columns: minmax(240px, 1fr) minmax(240px, 1fr) minmax(320px, 1.6fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .controls { grid-template-columns: 1fr; } }
.buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }

.status-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.quarter { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
.quarter .of { font-size: 13px; font-weight: 400; color: var(--muted); }
.badge { font-size: 12px; font-weight: 600; border-radius: 999px; padding: 2px 10px; border: 1px solid var(--axis); color: var(--text-2); }
.badge::before { content: "\25CF  "; }
.badge[data-state="running"] { color: var(--accent); border-color: var(--accent); }
.badge[data-state="paused"]::before { content: "II  "; font-weight: 800; }
.badge[data-state="finished"]::before { content: "\2713  "; }
.badge[data-state="stopped"]::before { content: "\25A0  "; }
.badge[data-state="error"] { color: var(--danger); border-color: var(--danger); }
.badge[data-state="error"]::before { content: "!  "; }
.progress { height: 4px; background: var(--grid); border-radius: 2px; margin-top: 8px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }

input[type="range"] { width: 100%; accent-color: var(--accent); }
.range-ends { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; }
output { font-weight: 600; font-variant-numeric: tabular-nums; }

.shock-row { display: grid; grid-template-columns: minmax(0, 1fr) 96px 28px; gap: 6px; margin-bottom: 6px; }
.shock-row select, .shock-row input { width: 100%; min-width: 0; }
.shock-row .remove { padding: 0; border: none; background: none; color: var(--muted); font-size: 18px; }
.log { list-style: none; margin: 10px 0 0; padding: 0; max-height: 140px; overflow-y: auto; font-size: 13px; }
.log li { padding: 3px 0; border-top: 1px solid var(--border); color: var(--text-2); font-variant-numeric: tabular-nums; }
.log li.bad { color: var(--danger); }
.log .q { color: var(--muted); display: inline-block; min-width: 3.2em; }

/* ---------- charts ---------- */
.charts-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 20px 0 4px; flex-wrap: wrap; }
.group-title { font-size: 13px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.04em; margin: 18px 0 8px; }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
figure.chart {
  margin: 0; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 10px 4px; min-width: 0; overflow: hidden;
}
figure.chart:focus-visible { outline: 2px solid var(--accent); }
figure.chart figcaption { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 13px; }
.chart-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-name code { color: var(--muted); font-size: 11px; margin-left: 2px; }
.chart-value { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
figure.chart svg { display: block; width: 100%; overflow: visible; }

.grid line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.grid line.axis { stroke: var(--axis); }
.grid text { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.grid .ylabel { text-anchor: end; }
.grid .xlabel { text-anchor: middle; }
line.zero { stroke: var(--axis); stroke-width: 1.5; stroke-dasharray: 4 3; }
line.zero.start { stroke-dasharray: none; }
line.longrun { stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 3; opacity: 0.8; }
.markers line { stroke: var(--marker); stroke-width: 1.5; stroke-dasharray: 2 2.5; }
path.line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
circle.dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
line.crosshair { stroke: var(--muted); stroke-width: 1; }
rect.overlay { fill: transparent; cursor: crosshair; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none; max-width: 280px;
  background: var(--surface); color: var(--text); border: 1px solid var(--axis);
  border-radius: 8px; padding: 6px 10px; font-size: 12px; box-shadow: 0 4px 14px rgb(0 0 0 / 14%);
  font-variant-numeric: tabular-nums;
}
.tt-head { color: var(--muted); }
.tt-value { font-weight: 600; font-size: 13px; }
.tt-muted { color: var(--text-2); }

.table-wrap { overflow-x: auto; margin-top: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); max-height: 70vh; }
.table-wrap table { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
.table-wrap th, .table-wrap td { padding: 4px 8px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
.table-wrap thead th { position: sticky; top: 0; background: var(--surface); color: var(--muted); font-weight: 500; }
.table-wrap th:first-child, .table-wrap td:first-child { position: sticky; left: 0; background: var(--surface); text-align: left; }

/* ---------- system diagram ---------- */
.intro { display: grid; grid-template-columns: minmax(0, 1fr); gap: 20px; align-items: start; }
@media (min-width: 1100px) { .intro { grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.2fr); } }
.diagram-card { margin: 0; }
.diagram-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.diagram-tabs button { padding: 3px 10px; font-size: 13px; border-radius: 999px; }
.diagram-tabs button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.diagram-wrap { overflow-x: auto; }
svg.diagram { display: block; width: 100%; min-width: 520px; height: auto; font-family: inherit; }

.sectors rect { fill: var(--page); stroke: var(--border); }
.sector-labels text { paint-order: stroke; stroke: var(--page); stroke-width: 4px; stroke-linejoin: round; fill: var(--muted); font-size: 10px; font-weight: 600; letter-spacing: 0.05em; }

.edge .stroke { fill: none; stroke: var(--axis); stroke-width: 1.5; stroke-linejoin: round; }
.edge .head { fill: var(--axis); }
.edge.lag .stroke { stroke-dasharray: 5 4; }
.edge { transition: opacity 0.15s; }
svg.focus .edge { opacity: 0.18; }
svg.focus .edge.on, svg.focus .edge.in, svg.focus .edge.out { opacity: 1; }
.edge.on .stroke, .edge.out .stroke { stroke: var(--accent); stroke-width: 2; }
.edge.on .head, .edge.out .head { fill: var(--accent); }
.edge.in .stroke { stroke: var(--text-2); stroke-width: 2; }
.edge.in .head { fill: var(--text-2); }

.node { cursor: pointer; outline: none; transition: opacity 0.15s; }
.node rect { fill: var(--surface); stroke: var(--axis); stroke-width: 1; }
.node .label { fill: var(--text); font-size: 12.5px; font-weight: 500; text-anchor: middle; }
.node .key { fill: var(--muted); font-size: 10.5px; text-anchor: middle; font-family: ui-monospace, Menlo, monospace; }
svg.focus .node { opacity: 0.45; }
svg.focus .node.on, svg.focus .node.near { opacity: 1; }
.node.on > rect { stroke: var(--accent); stroke-width: 2; }
.node:focus-visible > rect { stroke: var(--accent); stroke-width: 2.5; }
.node .badge-e { fill: var(--surface); stroke: var(--text-2); stroke-width: 1; }
.node .badge-e-text { fill: var(--text-2); font-size: 9px; font-weight: 700; text-anchor: middle; }
.node .badge-s { fill: var(--marker); }
.node .badge-s-text { fill: var(--surface); font-size: 10px; font-weight: 700; text-anchor: middle; font-family: ui-monospace, Menlo, monospace; }

.diagram-legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 12px; color: var(--text-2); margin-top: 6px; }
.diagram-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.lg-solid, .lg-dash { stroke: var(--text-2); stroke-width: 1.5; }
.lg-dash { stroke-dasharray: 5 4; }
.lg-e { display: inline-grid; place-items: center; width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--text-2); font-size: 9px; font-weight: 700; }
.lg-s { display: inline-grid; place-items: center; padding: 0 5px; height: 14px; border-radius: 7px; background: var(--marker); color: var(--surface); font: 700 10px ui-monospace, Menlo, monospace; }
.diagram-panel { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); font-size: 13px; min-height: 7.5em; }
.diagram-panel p { margin: 4px 0; color: var(--text-2); }
.dp-title { font-weight: 600; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.dp-title code { color: var(--muted); font-size: 12px; font-weight: 400; }
.dp-sector { margin-left: auto; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
.dp-chain { font-size: 12px; }
.diagram-panel strong { color: var(--text); font-weight: 500; }

.prose a, .diagram-panel a, table.ref a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
.node[aria-pressed="true"] > rect { stroke: var(--accent); stroke-width: 2.5; }

/* ---------- society and policy ---------- */
.society-card { margin: 20px 0; }
.society-card > h2 { margin-bottom: 2px; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 4px; }
.presets button { padding: 4px 12px; font-size: 13px; border-radius: 999px; }
.presets button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.preset-about { min-height: 1.5em; margin: 2px 0 10px; }
.society-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 20px; align-items: start; }
@media (max-width: 900px) { .society-grid { grid-template-columns: minmax(0, 1fr); } }
.sliders { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px 18px; }
.slider label { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; }
.slider .sl-name { color: var(--text); font-weight: 500; }
.slider output { font-size: 13px; }
.slider .sl-unit { display: block; color: var(--muted); font-size: 11px; min-height: 1.2em; }
.slider input[type="range"] { margin: 2px 0 0; }
.summary { background: var(--page); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; font-size: 13px; transition: opacity 0.15s; }
.summary.stale { opacity: 0.55; }
.summary h3 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.summary dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 12px; margin: 0; }
.summary dt { color: var(--text-2); }
.summary dd { margin: 0; font-weight: 500; font-variant-numeric: tabular-nums; }
.summary .error { margin: 0; }
.society-card .start { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 16px; }
.society-line { margin: 8px 0 0; font-size: 12px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.slider.inactive { opacity: 0.45; }

/* ---------- run view: policy changes, totals, view toggle ---------- */
.policy-card { grid-column: span 2; }
.policy-card .sliders { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px 18px; }
@media (max-width: 900px) { .policy-card { grid-column: auto; } }
.hint.inline { display: inline; margin: 0; font-weight: 400; }
table.totals { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
table.totals th, table.totals td { padding: 3px 4px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
table.totals thead th { color: var(--muted); font-weight: 500; font-size: 12px; }
table.totals tbody th { text-align: left; font-weight: 400; color: var(--text-2); white-space: normal; }
.view-toggle { display: flex; gap: 4px; border: 1px solid var(--border); border-radius: 999px; padding: 2px; margin: 0; }
.view-toggle label { padding: 3px 12px; border-radius: 999px; font-size: 13px; cursor: pointer; }
.view-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.view-toggle label:has(input:checked) { background: var(--accent); color: var(--accent-ink); }
.view-toggle label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.charts-head .hint { flex: 1 1 300px; }
.society-side { display: grid; gap: 12px; }
fieldset.units { border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px 12px; margin: 0; }
fieldset.units legend { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); padding: 0 4px; }
fieldset.units .hint { margin-top: 0; }
.units-row { display: flex; flex-wrap: wrap; gap: 8px 14px; }
.units-row label { display: grid; gap: 2px; font-size: 12px; color: var(--text-2); }
.units-row input[type="number"] { width: 9em; }
