/* ---------- Tokens ---------- */
:root {
  color-scheme: light dark;
  --paper: #f7f4ec;
  --paper-2: #efeadd;
  --ink: #1c1b18;
  --ink-2: #57534a;
  --rule: #d6cfbd;
  --accent: #a3321f;
  --control-bg: #fffdf8;
  --control-line: #a59d8b;

  /* The six symbols. Clock family: cool. Counter family: warm. Eraser: neutral. */
  --sym-I: #1f4e8c;
  --sym-D: #19727a;
  --sym-z: #5d5a9c;
  --sym-a: #8a6a00;
  --sym-b: #b8540d;
  --sym-e: #7a756a;

  --lane-0: #2f7a4b;
  --lane-1: #9a3b78;

  --serif: "Iowan Old Style", "Charter", "Source Serif Pro", "Source Serif 4", Georgia, serif;
  --sans: ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #171614;
    --paper-2: #211f1c;
    --ink: #e9e4d8;
    --ink-2: #a8a193;
    --rule: #3a3731;
    --accent: #e8735b;
    --control-bg: #24221f;
    --control-line: #6b665c;
    --sym-I: #7fa8e6;
    --sym-D: #5fc2c4;
    --sym-z: #a9a3e8;
    --sym-a: #d9b44a;
    --sym-b: #f0935a;
    --sym-e: #9c968a;
    --lane-0: #6cc08e;
    --lane-1: #e081bd;
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { font: 112.5%/1.6 var(--serif); }
body { margin: 0; background: var(--paper); color: var(--ink); }
main { max-width: 74rem; margin: auto; padding: clamp(1rem, 4vw, 3.5rem) clamp(1rem, 4vw, 2.5rem) 5rem; }
a { color: var(--accent); text-underline-offset: .15em; }
h1, h2, h3 { font-weight: 600; letter-spacing: -.005em; }
h1 { font-size: clamp(2rem, 5vw, 2.9rem); line-height: 1.1; margin: 0 0 .4rem; }
h2 { font-size: 1.35rem; line-height: 1.25; margin: 0 0 .75rem; }
h3 { font-size: 1.05rem; margin: 1.4rem 0 .35rem; }
p { margin: .7rem 0; }
code, pre, kbd, samp { font-family: var(--mono); font-size: .86em; }
pre { margin: 0; padding: .6rem .8rem; background: var(--paper-2); font-size: .85rem; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }
dd { margin: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.muted, dt { color: var(--ink-2); }
.muted { font-size: .88rem; }
.input-error { color: var(--accent); font-family: var(--sans); font-size: .9rem; }

/* ---------- Header ---------- */
header { margin-bottom: 2.25rem; }
.subtitle { color: var(--ink-2); margin: 0; font-style: italic; }
.header-links { display: flex; gap: 1.1rem; margin-top: .6rem; font: .85rem var(--sans); }
.tabs { display: flex; gap: 1.75rem; margin-top: 1.75rem; border-bottom: 1px solid var(--rule); }
.tab {
  box-shadow: none;
  font: 600 .9rem var(--sans); color: var(--ink-2); background: none; border: 0;
  border-bottom: 2px solid transparent; border-radius: 0; padding: .5rem 0; margin-bottom: -1px; cursor: pointer;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

/* ---------- Controls (sans, square) ---------- */
label, button, select, textarea, .metrics, #status, .legend, .lane-key, summary, th { font-family: var(--sans); }
label { display: block; font-size: .8rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-2); margin: .9rem 0 .3rem; }
button, select, textarea { font-size: .88rem; color: var(--ink); background: var(--control-bg); border: 1px solid var(--control-line); border-radius: 2px; }
button, select { box-shadow: 0 1px 0 color-mix(in srgb, var(--control-line) 45%, transparent); font-weight: 500; }
button, select { padding: .38rem .7rem; }
button { cursor: pointer; }
button:hover:enabled { border-color: var(--ink); }
button:disabled { opacity: .45; cursor: default; }
#step, #next-output { background: var(--ink); color: var(--paper); border-color: var(--ink); font-weight: 600; }
select { max-width: 100%; }
textarea { display: block; width: 100%; padding: .5rem .6rem; resize: vertical; font-family: var(--mono); font-size: .85rem; line-height: 1.5; }
.actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.view-toggle { display: flex; width: fit-content; align-items: center; gap: .4rem; margin-top: .9rem; text-transform: none; letter-spacing: 0; font-size: .88rem; }
.view-toggle input { accent-color: var(--accent); }

/* ---------- Playground layout: flat, ruled ---------- */
.workspace { display: grid; grid-template-columns: 14rem minmax(0, 1fr); gap: 0 2.5rem; align-items: start; }
.workspace > aside { position: sticky; top: 1rem; padding-right: 1.5rem; border-right: 1px solid var(--rule); }
.workbench { display: grid; gap: 0; min-width: 0; }
.workbench > section { padding: 1.6rem 0; border-top: 1px solid var(--rule); }
.workbench > section:first-child { padding-top: 0; border-top: 0; }
.workspace h2 { font-size: 1.15rem; }
.program-input { margin-top: 1.1rem; padding-top: .3rem; }
.program-input .muted { margin: .45rem 0; }
.help { margin: .4rem 0 .6rem; }
.help summary { font-weight: 500; color: var(--ink-2); font-size: .82rem; }
.help p { margin: .3rem 0 0; }

/* ---------- Symbols ---------- */
.sym { font-family: var(--mono); font-weight: 600; }
.sym-I { --c: var(--sym-I); } .sym-D { --c: var(--sym-D); } .sym-z { --c: var(--sym-z); }
.sym-a { --c: var(--sym-a); } .sym-b { --c: var(--sym-b); } .sym-e { --c: var(--sym-e); }
.sym { color: var(--c); }

/* Rules: each symbol as a tile, appendant as a row of tiles */
#rules { display: grid; grid-template-columns: auto 1fr; gap: .45rem .6rem; align-items: center; margin: 0; font-family: var(--mono); }
#rules dt.sym, .legend .sym {
  display: inline-grid; place-items: center; width: 1.6rem; height: 1.6rem;
  background: color-mix(in srgb, var(--c) 14%, transparent); border: 1px solid color-mix(in srgb, var(--c) 45%, transparent);
}
#rules dt.active { background: var(--c); color: var(--paper); border-color: var(--c); }
#rules dd { color: var(--ink-2); font-size: .9rem; }
#rules dd .sym { letter-spacing: .08em; }
.legend { display: grid; gap: .35rem; margin: 1.2rem 0 .8rem; font-size: .82rem; }
.legend > div { display: flex; align-items: center; gap: .5rem; }
.legend dt { display: flex; gap: 2px; }
.legend .sym { width: 1.3rem; height: 1.3rem; font-size: .8rem; }

/* Literal queue: a tape of cells with a read head */
.tape {
  display: flex; flex-wrap: wrap; gap: 1px; padding: .5rem; max-height: 18rem; overflow: auto;
  background: var(--paper-2); border: 1px solid var(--rule);
}
.tape .sym {
  display: inline-grid; place-items: center; width: 1.25rem; height: 1.6rem; font-size: .82rem;
  background: color-mix(in srgb, var(--c) 12%, var(--paper));
  border-bottom: 2px solid color-mix(in srgb, var(--c) 55%, transparent);
}
.tape .head { background: var(--c); color: var(--paper); outline: 2px solid var(--ink); outline-offset: 1px; position: relative; z-index: 1; }
.tape .head-pair { box-shadow: inset 0 0 0 1px var(--ink-2); opacity: .7; }
.tape-gap { flex-basis: 100%; padding: .35rem 0; font: italic .8rem var(--serif); color: var(--ink-2); text-align: center; }

/* ---------- Run section ---------- */
.metrics { display: flex; gap: .5rem 2rem; flex-wrap: wrap; margin: 1.1rem 0 .4rem; }
.metrics dt { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; }
.metrics dd { font: 1.25rem var(--mono); }
#status, #fizz-values { min-height: 1.5em; font-size: .9rem; }
#diagram h3, #literal h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; font-family: var(--sans); color: var(--ink-2); }
#diagram-command { overflow-wrap: anywhere; }

/* Lanes */
.lane-0 { --entry-lane: var(--lane-0); --exit-lane: var(--lane-1); }
.lane-1 { --entry-lane: var(--lane-1); --exit-lane: var(--lane-0); }
.lane-chip, .macro-word { color: var(--paper); background: var(--entry-lane); border-radius: 2px; }
.lane-chip { display: inline-block; min-width: 1.15rem; padding: 0 .2rem; text-align: center; font: 600 .78rem/1.35 var(--mono); vertical-align: .08em; }
.lane-key { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; }
.lane-key .lane-chip:not(:first-child) { margin-left: .6rem; }
.lanes { font-family: var(--mono); white-space: nowrap; }

#macro-queue { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px; padding: .5rem; background: var(--paper-2); border: 1px solid var(--rule); }
.macro-word { padding: .15rem .45rem; white-space: nowrap; font-family: var(--mono); font-size: .9rem; }
.macro-word.lane-flip { background: linear-gradient(90deg, var(--entry-lane) 50%, var(--exit-lane) 50%); }
.block-note { display: block; color: var(--ink-2); font: .8rem/1.4 var(--sans); }

/* Tables */
table { width: 100%; border-collapse: collapse; text-align: left; font-size: .9rem; }
th, td { padding: .35rem .8rem .35rem 0; border-bottom: 1px solid var(--rule); vertical-align: top; }
thead th { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-2); font-weight: 600; }
.layout-scroll, #checkpoint-table { max-width: 100%; overflow: auto; }
.layout-scroll { max-height: 25rem; }
#diagram thead { position: sticky; top: 0; background: var(--paper); }
th.control { color: var(--sym-I); }
th.counter { color: var(--sym-b); }
.control { color: var(--sym-I); }
.counter, .value { color: var(--sym-b); }
.register-checkpoints tr > :first-child { position: sticky; left: 0; max-width: min(16rem, 45vw); overflow-wrap: anywhere; background: var(--paper); }

details { margin-top: 1rem; }
summary { cursor: pointer; width: fit-content; font-size: .88rem; font-weight: 600; }
details[open] > summary { margin-bottom: .5rem; }
.notation { font-size: .92rem; }

#outputs { display: flex; flex-wrap: wrap; gap: 2px; padding: 0; list-style: none; }
#outputs li { padding: .15rem .5rem; font: .85rem var(--mono); background: var(--paper-2); border-bottom: 2px solid var(--rule); }

@media (max-width: 52rem) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .workspace > aside { position: static; order: 1; padding: 1.5rem 0 0; border-right: 0; border-top: 1px solid var(--rule); }
}

/* ---------- Walkthrough: a single reading column ---------- */
.walkthrough { max-width: 41rem; margin: 0 auto; }
.walkthrough section { padding: 2.25rem 0 0; }
.walkthrough section + section { margin-top: 1.5rem; border-top: 1px solid var(--rule); }
.walkthrough section:first-child { padding-top: 0; }
.walkthrough h2 { font-size: 1.55rem; }
.walkthrough h3 { font-size: 1.12rem; margin-top: 1.9rem; }
.walkthrough { line-height: 1.65; }
.walkthrough code { font-size: .84em; }
.walkthrough code { white-space: nowrap; }
.walkthrough pre code { white-space: pre-wrap; }

.wt-list { margin: .6rem 0 1rem; padding-left: 1.4rem; }
.wt-list li { margin: .4rem 0; }

/* Worked examples: an indented aside with a hairline, not a box */
.wt-example { display: grid; gap: .55rem; margin: 1.1rem 0; padding: .15rem 0 .15rem 1.1rem; border-left: 2px solid var(--rule); font-size: .94rem; }

/* Three phases as a numbered list */
.phases { list-style: none; counter-reset: phase; padding: 0; margin: 1.2rem 0; }
.phases > li { counter-increment: phase; display: grid; grid-template-columns: 2.25rem 1fr; padding: .9rem 0; border-top: 1px solid var(--rule); }
.phases > li:last-child { border-bottom: 1px solid var(--rule); }
.phases > li::before { content: counter(phase); grid-row: span 9; font-size: 1.6rem; line-height: 1.1; color: var(--accent); font-variant-numeric: oldstyle-nums; }
.phases h3 { margin: 0 0 .2rem; font-style: normal; font-weight: 600; }
.phases p { margin: .35rem 0; }
.phases pre { margin: .5rem 0; }

.wt-deep-dive { margin-top: 1.4rem; font-size: .93rem; }
.wt-deep-dive summary { font-family: var(--sans); color: var(--ink-2); }
.wt-deep-dive[open] { padding-bottom: .4rem; border-bottom: 1px solid var(--rule); }

/* Guard trace: full-bleed-ish figure */
.wt-interactive { margin: 1.4rem 0; padding: 1rem 0; border-top: 2px solid var(--ink); border-bottom: 1px solid var(--rule); }
.wt-controls { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .6rem; }
.wt-toggle-group { display: flex; }
.wt-toggle-group .wt-choice + .wt-choice { margin-left: -1px; }
.wt-choice[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.wt-stage-pills { display: flex; flex-wrap: wrap; gap: .3rem 1.1rem; margin-top: .9rem; }
.wt-pill { box-shadow: none; font-size: .82rem; padding: .2rem 0; background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0; color: var(--ink-2); }
.wt-pill:hover:enabled { color: var(--ink); }
.wt-pill[aria-current="step"] { color: var(--ink); border-bottom-color: var(--accent); }
.wt-summary { display: grid; margin: .9rem 0 .6rem; font-size: .95rem; color: var(--ink-2); }
.wt-summary > span { grid-area: 1 / 1; }
.wt-summary-spacer { visibility: hidden; }
.wt-interactive .layout-scroll { max-height: none; }
.wt-table { font-size: .85rem; font-variant-numeric: tabular-nums; }
.wt-table td, .wt-table th { white-space: nowrap; padding: .3rem 1rem .3rem .5rem; }
.wt-table thead th { text-transform: none; letter-spacing: 0; font-size: .8rem; }
.wt-table thead code { font-size: .95em; }
.wt-table tbody th { font-family: var(--mono); font-weight: 400; color: var(--ink-2); width: 2.5rem; }
.wt-table tbody tr { border-left: 3px solid transparent; }
.wt-table tr.wt-stage-start > * { border-top: 2px solid var(--rule); }
.wt-table tr.wt-row-active { background: color-mix(in srgb, var(--accent) 7%, transparent); border-left-color: var(--accent); }
.wt-table tr:not(.wt-row-active) td { opacity: .55; }
.wt-table tr.wt-row-active td { opacity: 1; }
.wt-sign { display: inline-block; width: 1.5rem; text-align: center; font-family: var(--mono); margin-right: .5rem; font-weight: 600; border-radius: 2px; }
.wt-sign-flip { color: var(--paper); background: var(--lane-1); }
.wt-key { margin: .7rem 0 0; }
.wt-key .wt-sign { margin-right: .3rem; }
.wt-flag { margin-left: .5rem; padding: 0 .35rem; font: 600 .7rem/1.5 var(--sans); letter-spacing: .03em; color: var(--sym-b); border: 1px solid color-mix(in srgb, var(--sym-b) 50%, transparent); border-radius: 2px; }
.wt-exp { font-family: var(--mono); color: var(--ink-2); }
.wt-try { margin-top: 1.2rem; }
.wt-try button { box-shadow: none; background: none; border: 0; padding: 0; color: var(--accent); font-size: .88rem; text-decoration: underline; text-underline-offset: .15em; }
.wt-try { gap: .4rem 1.5rem; }

/* ---------- Walkthrough figures (diagrams.js) ---------- */
.wt-figure { margin: 1.4rem 0 .4rem; overflow-x: auto; }
.wt-figure figcaption { margin-top: .5rem; font-size: .88rem; color: var(--ink-2); }
.wt-steps { margin-top: .4rem; }
.wt-steps summary { font-family: var(--sans); font-size: .82rem; font-weight: 500; color: var(--ink-2); }
.wt-steps .wt-example { margin-top: .5rem; }
.diagram { display: block; margin: 0 auto; width: 100%; min-width: 32rem; height: auto; overflow: visible; font-family: var(--sans); }
.diagram text { fill: var(--ink); }
.diagram .d-sub { font-size: .75em; }
.diagram .d-row-title { font-size: 12px; font-weight: 600; }
.diagram .d-note { font-size: 11px; fill: var(--ink-2); }
.diagram .d-sep { stroke: var(--ink-2); stroke-dasharray: 2 3; }
.diagram .d-cell { fill: var(--paper); stroke: var(--ink-2); }
.diagram .d-cell-read { fill: var(--ink); stroke: var(--ink); }
.diagram .d-cell-out { fill: var(--paper-2); stroke: var(--rule); }
.diagram .d-cell-eaten { fill: var(--paper-2); stroke: var(--lane-1); stroke-dasharray: 3 2; }
.diagram .d-cell-text { font: 12px var(--mono); text-anchor: middle; }
.diagram .d-cell-text-read { fill: var(--paper); font-weight: 600; }
.diagram .d-strike { stroke: var(--lane-1); stroke-width: 1.5; }
.diagram .d-bracket { fill: none; stroke: var(--ink-2); }
.diagram .d-arrow { stroke-width: 1.5; fill: none; }
.diagram .d-lane-fill-0 { fill: var(--lane-0); }
.diagram .d-lane-fill-1 { fill: var(--lane-1); }
.diagram .d-lane-stroke-0 { stroke: var(--lane-0); }
.diagram .d-lane-stroke-1 { stroke: var(--lane-1); }
.diagram .d-chip-text { font: 600 11px var(--mono); text-anchor: middle; fill: var(--paper); }
.diagram .d-band { stroke: none; }
.diagram .d-band-clock { fill: color-mix(in srgb, var(--sym-I) 7%, transparent); }
.diagram .d-band-counter { fill: color-mix(in srgb, var(--sym-b) 7%, transparent); }
.diagram .d-col { font: 600 13px var(--mono); text-anchor: middle; }
.diagram .d-col-clock { fill: var(--sym-I); }
.diagram .d-col-counter { fill: var(--sym-b); }
.diagram .d-row-rule { stroke: var(--rule); }
.diagram .d-guide { stroke: var(--rule); stroke-dasharray: 1 3; }
.diagram .d-guide-label { font: 10px var(--mono); fill: var(--ink-2); text-anchor: end; }
.diagram .d-wire { fill: none; stroke-width: 2.5; stroke-linecap: round; }
.diagram .d-wire-flip { stroke: var(--ink-2); stroke-width: 1.5; stroke-dasharray: 3 2; }
.diagram .d-sign { fill: var(--paper); stroke-width: 1.5; }
.diagram .d-sign-inverted { fill: var(--lane-1); stroke: var(--lane-1); }
.diagram .d-sign-text { font: 700 14px var(--mono); text-anchor: middle; }
.diagram .d-sign-text-inverted { fill: var(--paper); }
.diagram .d-step-note { font-size: 10px; fill: var(--ink-2); }
.diagram .d-flag { fill: none; stroke: color-mix(in srgb, var(--sym-b) 55%, transparent); }
.diagram .d-flag-text { font: 600 8.5px var(--sans); letter-spacing: .03em; text-anchor: middle; fill: var(--sym-b); }
/* Draw figures at about 1.3× their viewBox so small ones don't balloon to the column width. */
.diagram { max-width: calc(var(--natural-width) * 1.3); min-width: min(32rem, calc(var(--natural-width) * 1.3)); }
