:root {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #5f5f5f;
  --border: #cccccc;
  --accent: #2f6fdb;
  --warn: #8a5700;
  --error: #b3261e;
  --total: #1c64d6;
  --grown: #2b8a3e;
  --si: #d62828;
  --grid: #e6e6e6;
  --panel: #f7f7f8;
  --code-bg: #eeeef0;
  --substrate: #b9bec6;
  --go: #1e7a34;
  --go-hover: #19632a;
  --go-border: #17602a;
  color-scheme: light;
}

/* Auto: follow the system unless the user picked Light. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1b1d;
    --fg: #eaeaea;
    --muted: #a8a8a8;
    --border: #444444;
    --accent: #7aa7ff;
    --warn: #e0a94a;
    --error: #ff8a80;
    --go: #2e9e4b;
    --go-hover: #38b357;
    --go-border: #2a8b42;
    --total: #74a8ff;
    --grown: #51cf66;
    --si: #ff6b6b;
    --grid: #333336;
    --panel: #222225;
    --code-bg: #2c2c30;
  --substrate: #5d626b;
    --substrate: #5d626b;
    color-scheme: dark;
  }
}

/* Explicit Dark. */
:root[data-theme="dark"] {
  --bg: #1b1b1d;
  --fg: #eaeaea;
  --muted: #a8a8a8;
  --border: #444444;
  --accent: #7aa7ff;
  --warn: #e0a94a;
  --error: #ff8a80;
  --total: #74a8ff;
  --grown: #51cf66;
  --si: #ff6b6b;
  --grid: #333336;
  --panel: #222225;
  --code-bg: #2c2c30;
  --substrate: #5d626b;
  color-scheme: dark;
}

body {
  margin: 0;
  padding: 24px 16px;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.4 system-ui, sans-serif;
}

h1 { font-size: 1.25rem; margin: 0; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
}
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.theme-select { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }

.secondary {
  padding: 6px 12px;
  font: inherit;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
.secondary:hover { border-color: var(--accent); }

.temp-control { max-width: 560px; margin-bottom: 16px; }
.temp-label { display: block; font-weight: 600; margin-bottom: 6px; }

.temp-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.temp-entry {
  width: 8ch;
  padding: 6px 8px;
  font: inherit;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.temp-entry:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.temp-unit { color: var(--muted); }
.temp-celsius { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 12ch; }

.temp-reset {
  margin-left: auto;
  padding: 6px 12px;
  font: inherit;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
.temp-reset:disabled { opacity: 0.45; cursor: default; }

.temp-slider { width: 100%; accent-color: var(--accent); }

.temp-scale {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--muted);
}

.temp-status { min-height: 1.4em; font-size: 0.85rem; margin: 8px 0 0; }
.temp-status[data-kind="warn"] { color: var(--warn); }
.temp-status[data-kind="error"] { color: var(--error); }

h2 { font-size: 1rem; margin: 0 0 4px; }

.layout {
  display: grid;
  grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 800px) { .layout { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  min-width: 0;
}

.field { margin: 0 0 14px; border: 0; padding: 0; }
.field > label, .field > legend { display: block; font-weight: 600; margin-bottom: 6px; padding: 0; }
.row { display: flex; align-items: center; gap: 8px; }
.unit { color: var(--muted); }

.num, select {
  padding: 6px 8px;
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.num { width: 8ch; font-variant-numeric: tabular-nums; }
.num:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.choice { display: block; margin: 4px 0; }
.choice:has(input:disabled) { color: var(--muted); }

.actions { display: flex; gap: 8px; }
.actions .primary { flex: 1; }
.primary {
  width: 100%;
  padding: 8px 12px;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}
.primary:disabled { opacity: 0.6; cursor: progress; }

.error { color: var(--error); min-height: 1.4em; font-size: 0.85rem; margin: 8px 0 0; }
.note.warn { color: var(--warn); }
.note:empty { display: none; }
.choice.inline { display: inline-block; margin-right: 16px; }
.plot-mode { margin-bottom: 4px; font-size: 0.9rem; }
.note, .model-note { color: var(--muted); font-size: 0.85rem; margin: 6px 0 0; }
.model-note { margin: 0 0 12px; }

.plot svg { width: 100%; height: auto; display: block; }
.plot .grid { stroke: var(--grid); }
.plot .grid.minor { stroke-opacity: 0.45; }
.plot .axis { stroke: var(--muted); }
.plot .tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.plot .axis-label { fill: var(--fg); font-size: 13px; }
.plot .line { fill: none; stroke-width: 2; }
.line.total { stroke: var(--total); }
.line.grown { stroke: var(--grown); }
.line.si { stroke: var(--si); }

.legend { display: flex; flex-wrap: wrap; gap: 16px; font-size: 0.85rem; margin-bottom: 6px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
/* Beat `.plot svg { width: 100% }` so legend icons stay small. */
.plot .legend svg.swatch { width: 24px; height: 6px; flex: none; overflow: visible; display: inline-block; }
.swatch .line { fill: none; stroke-width: 2; }

.summary {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 16px;
  margin: 12px 0 0;
  font-size: 0.9rem;
}
.summary dt { color: var(--muted); }
.summary dd { margin: 0; font-variant-numeric: tabular-nums; }

/* Plot controls */
.plot-controls { display: grid; gap: 4px; margin-bottom: 8px; font-size: 0.9rem; }
.control-group { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 0; }
.control-title { font-weight: 600; min-width: 5.5em; margin-right: 8px; }
.plot .tick { font-size: 11.5px; }

/* Show all calculations */
.toggles { margin-top: 16px; font-weight: 600; }
.calcs { margin-top: 8px; display: grid; gap: 12px; }
.calc-model { border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px; background: var(--bg); }
.calc-model > summary { font-weight: 600; cursor: pointer; padding: 4px 0; }
.calc-step { padding: 10px 0; border-top: 1px solid var(--grid); }
.calc-label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.calc-math { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.calc-math .katex-display { margin: 6px 0; text-align: left; }
.calc-math .katex-display > .katex { text-align: left; }
.calc-note { margin: 2px 0 0; font-size: 0.85rem; color: var(--muted); }
.calc-note strong { color: var(--warn); font-weight: 600; }

/* README dialog */
.readme-dialog {
  width: min(900px, calc(100vw - 32px));
  height: min(88vh, 1000px);
  padding: 0;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.readme-dialog[open] { display: flex; flex-direction: column; }
.readme-dialog::backdrop { background: rgb(0 0 0 / 0.5); }
.readme-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.readme-head h2 { margin: 0; }
.readme-body { flex: 1; overflow-y: auto; padding: 8px 24px 32px; line-height: 1.6; }
.readme-body:focus { outline: none; }
.readme-body h1 { font-size: 1.5rem; margin: 16px 0 8px; }
.readme-body h2 { font-size: 1.2rem; margin: 28px 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.readme-body h3 { font-size: 1.05rem; margin: 20px 0 6px; }
.readme-body code { background: var(--code-bg); padding: 1px 5px; border-radius: 3px; font-size: 0.9em; }
.readme-body pre { background: var(--code-bg); padding: 10px 12px; border-radius: 6px; overflow-x: auto; }
.readme-body pre code { padding: 0; background: none; }
.readme-body table { border-collapse: collapse; margin: 10px 0; display: block; overflow-x: auto; }
.readme-body th, .readme-body td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.readme-body th { background: var(--panel); }
.readme-body blockquote { margin: 10px 0; padding: 4px 14px; border-left: 3px solid var(--accent); color: var(--muted); }
.readme-body a { color: var(--accent); }
.math-display { overflow-x: auto; overflow-y: hidden; }
.plot-title { font-size: 0.95rem; margin: 16px 0 4px; }
.control-group select { margin-right: 12px; }
.key { font-size: 0.85em; }
.si-key { color: var(--si); }
.total-key { color: var(--total); }
.grown-key { color: var(--grown); }
.plot-empty { color: var(--muted); padding: 40px 0; text-align: center; }
.reset-right { margin-left: auto; }
.secondary:disabled { opacity: 0.45; cursor: default; }
.secondary { white-space: nowrap; }
.row { flex-wrap: wrap; }
#x0 { width: 6ch; }

/* Cross section */
.xs { margin-top: 12px; }
.xs-controls { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; font-size: 0.9rem; }
.xs-controls label { display: inline-flex; align-items: center; gap: 6px; }
.xs-controls label[hidden] { display: none; }
.xs-svg { width: 100%; height: auto; display: block; margin-top: 8px; }
.xs-svg text { fill: var(--fg); font-size: 15px; font-variant-numeric: tabular-nums; }
.xs-svg .xs-title { font-weight: 700; font-size: 16px; letter-spacing: 0.08em; }
.xs-svg .xs-caption { fill: var(--muted); font-size: 13px; }
.xs-si { fill: var(--substrate); }
.xs-oxide { fill: var(--total); fill-opacity: 0.45; }
.xs-grown { fill: var(--grown); fill-opacity: 0.45; }
.xs-consumed { fill: url(#xs-hatch); }
.xs-hatch-line { stroke: var(--si); stroke-width: 1.5; }
.xs-frame { fill: none; stroke: var(--muted); stroke-width: 1; }
.xs-original { stroke: var(--fg); stroke-width: 1.2; stroke-dasharray: 5 3; }
.xs-hairline { stroke: var(--total); stroke-width: 1.5; }
.xs-callout { fill: none; stroke: var(--muted); stroke-width: 0.8; stroke-dasharray: 2 2; }
.xs-divider { stroke: var(--border); }
.xs-scale line { stroke: var(--fg); stroke-width: 1.5; }
.xs-dim line, .xs-dim polyline { stroke: var(--muted); stroke-width: 1; fill: none; }
.xs-dim .xs-leader { stroke-width: 0.7; }
.xs-dim.total line { stroke: var(--total); }
.xs-dim.grown line { stroke: var(--grown); }
.xs-dim.si line { stroke: var(--si); }
.xs-dim.total text { fill: var(--total); }
.xs-dim.grown text { fill: var(--grown); }
.xs-dim.si text { fill: var(--si); }
.xs-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 0.85rem; margin: 6px 0; }
.xs-legend span { display: inline-flex; align-items: center; gap: 6px; }
.xs-key { display: inline-block; width: 14px; height: 10px; border: 1px solid var(--muted); }
.xs-key.xs-consumed-key { background: repeating-linear-gradient(45deg, var(--si) 0 1.5px, transparent 1.5px 4px); }
.xs-key.xs-oxide { background: color-mix(in srgb, var(--total) 45%, transparent); }
.xs-key.xs-grown { background: color-mix(in srgb, var(--grown) 45%, transparent); }
.xs-key.xs-si { background: var(--substrate); }
.xs-key.xs-original-key { border: 0; border-top: 1.5px dashed var(--fg); height: 0; }
.xs-table { border-collapse: collapse; margin-top: 8px; font-size: 0.85rem; font-variant-numeric: tabular-nums; width: 100%; }
.xs-table th, .xs-table td { border-bottom: 1px solid var(--grid); padding: 4px 8px; text-align: left; }
.xs-table th { color: var(--muted); font-weight: 600; }
.xs-break { fill: none; stroke: var(--muted); stroke-width: 1.2; }
.xs-svg .xs-note { fill: var(--fg); font-size: 12px; }
.range { width: 100%; accent-color: var(--accent); margin-top: 8px; }
.range-scale { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }

/* Left column: controls + Execute panel, smaller text */
.left-col { display: grid; gap: 20px; align-content: start; min-width: 0; font-size: 13px; }
.left-col .field { margin-bottom: 10px; }
.left-col .num, .left-col select, .left-col button { font-size: 13px; }
.left-col .num, .left-col select { padding: 4px 6px; }
.left-col .secondary { padding: 4px 10px; }
.left-col .primary { padding: 6px 10px; }
.left-col .note, .left-col .error { font-size: 12px; }
.left-col .temp-entry { padding: 4px 6px; font-size: 13px; }
.left-col .temp-reset { padding: 4px 10px; font-size: 13px; }
.left-col .temp-status, .left-col .temp-scale, .left-col .range-scale { font-size: 11.5px; }
.left-col .choice { margin: 2px 0; }
.actions { flex-wrap: wrap; }

/* Execute panel */
.exec-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.exec-head h2 { font-size: 14px; margin: 0; }
.exec-now { font-variant-numeric: tabular-nums; font-size: 15px; font-weight: 600; }
.exec-plan { margin: 6px 0 10px; }
.exec-timer { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.exec-k { display: block; color: var(--muted); font-size: 11.5px; }
.exec-big { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.exec-progress { width: 100%; height: 10px; margin: 8px 0; accent-color: var(--accent); }
.exec-times { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0 0 8px; }
.exec-times dt { color: var(--muted); }
.exec-times dd { margin: 0; font-variant-numeric: tabular-nums; }
.exec-est { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; margin-bottom: 8px; }
.exec-est th, .exec-est td { text-align: left; padding: 3px 4px; border-bottom: 1px solid var(--grid); font-weight: normal; }
.exec-est thead th { color: var(--muted); font-size: 11.5px; }
.exec-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.exec-dot.total { background: var(--total); }
.exec-dot.grown { background: var(--grown); }
.exec-dot.si { background: var(--si); }
.exec-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.exec-actions .primary { flex: 1; width: auto; }
.exec-start { font-size: 15px !important; letter-spacing: 0.04em; }
.exec[data-state="running"] { border-color: var(--grown); }
.exec.exec-alarm { animation: exec-flash 1s steps(2, start) infinite; border-color: var(--si); }
.exec.exec-alarm .exec-remaining { color: var(--si); }
@keyframes exec-flash { 50% { background: color-mix(in srgb, var(--si) 25%, var(--panel)); } }
@media (prefers-reduced-motion: reduce) { .exec.exec-alarm { animation: none; background: color-mix(in srgb, var(--si) 20%, var(--panel)); } }
/* Keep cross-section text readable: scroll sideways in narrow windows rather than shrink. */
#xs-view { overflow-x: auto; }
.xs-svg { min-width: 560px; }
.xs-svg[data-panels="3"] { min-width: 840px; }

/* Real-time "now" marker during a fab run */
.marker-line { stroke: var(--fg); stroke-width: 1.5; stroke-dasharray: 4 3; }
.marker-box { fill: var(--bg); fill-opacity: 0.92; stroke: var(--border); }
.plot .marker text { font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.marker-now { fill: var(--fg); }
.marker-leader { fill: none; stroke-width: 1; }
.marker-point circle { stroke: var(--bg); stroke-width: 1.5; }
.marker-point.total circle, .marker-point.total .marker-leader { fill: var(--total); stroke: var(--total); }
.marker-point.grown circle, .marker-point.grown .marker-leader { fill: var(--grown); stroke: var(--grown); }
.marker-point.si circle, .marker-point.si .marker-leader { fill: var(--si); stroke: var(--si); }
.marker-point .marker-leader { fill: none; }
.marker-point.total text { fill: var(--total); }
.marker-point.grown text { fill: var(--grown); }
.marker-point.si text { fill: var(--si); }
.marker-point.total .marker-box { stroke: var(--total); }
.marker-point.grown .marker-box { stroke: var(--grown); }
.marker-point.si .marker-box { stroke: var(--si); }
.fab-lock { margin: 0 0 8px; padding: 6px 10px; border: 1px solid var(--grown); border-radius: 6px; font-size: 0.85rem; }

/* CURRENT panel during a fab run: slow glow on the live dimension labels */
.xs-dim.total { color: var(--total); }
.xs-dim.grown { color: var(--grown); }
.xs-dim.si { color: var(--si); }
.xs-dim.live text, .xs-dim.live line { animation: xs-glow 3s ease-in-out infinite; }
@keyframes xs-glow {
  0%, 100% { filter: drop-shadow(0 0 0 currentColor); }
  50% { filter: drop-shadow(0 0 3px currentColor) drop-shadow(0 0 6px currentColor); }
}
@media (prefers-reduced-motion: reduce) { .xs-dim.live text, .xs-dim.live line { animation: none; } }

/* --- Execute button, in the run box: green, and the widest thing there. */
.exec-go {
  flex: 1;
  background: var(--go);
  color: #ffffff;
  border: 1px solid var(--go-border);
  border-radius: 6px;
  padding: 8px 14px;
  font: inherit;
  font-weight: 700;
  letter-spacing: 0.03em;
  cursor: pointer;
}
.exec-go:hover { background: var(--go-hover); }
.exec-go:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- Target thickness: two linked boxes that drive the duration. */
.target-row { align-items: center; gap: 6px; }
.target-lab { color: var(--muted); font-size: 11.5px; }
.target-row .num { width: 5.5em; }

/* --- Last 30 seconds: a red box that flashes and counts down, then the
       instruction to pull the boat. */
.exec-final {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: 6px;
  text-align: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  background: var(--si);
  color: #ffffff;
  font-size: 34px;
  letter-spacing: 0.04em;
  animation: exec-flash 1s steps(1, end) infinite;
}
.exec-final.unload {
  font-size: 20px;
  letter-spacing: 0.06em;
  animation: exec-flash 0.6s steps(1, end) infinite;
}
@keyframes exec-flash {
  0%, 49% { background: var(--si); color: #ffffff; }
  50%, 100% { background: transparent; color: var(--si); box-shadow: inset 0 0 0 3px var(--si); }
}
@media (prefers-reduced-motion: reduce) {
  .exec-final { animation: none; box-shadow: inset 0 0 0 3px var(--si); }
}

/* --- Anneal inputs, and the two-stage run display. */
.anneal-row { align-items: center; gap: 6px; margin-top: 6px; }
.anneal-row .num { width: 5.5em; }
#anneal-fields { margin-top: 6px; padding-left: 10px; border-left: 2px solid var(--border); }

.exec-phase {
  margin: 0 0 8px;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: 13px;
}
.exec-phase.anneal { border-color: var(--warn); color: var(--warn); }
.exec-stage-k {
  display: block;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 6px;
}
.exec-anneal { margin-top: 4px; }
.exec-a-progress { width: 100%; height: 10px; margin: 8px 0; accent-color: var(--warn); }
/* The stage that is not currently running is dimmed, so it is obvious which
   clock is the live one. */
.exec-stage-done { opacity: 0.55; }

/* --- Furnace temperature now / next, switching at the anneal. */
.exec-temps { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 4px; }
.exec-mid {
  display: block;
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.exec-temp-now { color: var(--total); }
.exec-temps.anneal .exec-temp-now { color: var(--warn); }
.exec-temp-next { color: var(--muted); font-weight: 600; }
/* A brief flash when the furnace changes setpoint, so the switch is not missed. */
.exec-temp-now.switched { animation: temp-switch 0.55s ease-out 6; border-radius: 4px; }
@keyframes temp-switch {
  0%, 100% { background: transparent; }
  40% { background: color-mix(in srgb, var(--warn) 35%, transparent); }
}
@media (prefers-reduced-motion: reduce) { .exec-temp-now.switched { animation: none; } }

/* --- Oxide colour chart: thickness against the interference colour. */
.oc-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.oc-table td { padding: 3px 4px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
.oc-sw { width: 26px; }
.oc-sw i {
  display: block; width: 22px; height: 16px; border-radius: 3px;
  border: 1px solid var(--border);
}
.oc-nm { white-space: nowrap; font-weight: 600; width: 4.4em; }
.oc-name { font-size: 12px; }
.oc-hex { font-size: 11px; color: var(--muted); white-space: nowrap; text-align: right; }
/* The band this run lands on. */
.oc-table tr.oc-on { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.oc-table tr.oc-on .oc-nm, .oc-table tr.oc-on .oc-name { font-weight: 700; }
.oc-table tr.oc-on td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.oc-note { font-weight: 600; }
