:root {
  --paper-bg: #e9e3d5;
  --ink: #1a1816;
  --accent: #b3402e;
  --card: #f6f2e8;
  --shadow: 0 2px 14px rgba(40, 30, 10, 0.25);
}
@media (prefers-color-scheme: dark) {
  :root { --paper-bg: #262320; --ink: #1a1816; --card: #efe9dc; }
  /* --ink stays dark because the cards stay light — so anything set directly
     on the dark page must not inherit it. Body text defaults to a light tone;
     every light-card container pins color: var(--ink) itself below. */
  body { color: color-mix(in srgb, var(--card) 92%, black); }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--paper-bg);
  color: var(--ink);
  font-family: Georgia, 'Times New Roman', serif;
  line-height: 1.5;
}

header {
  max-width: 900px;
  margin: 0 auto;
  padding: 40px 16px 8px;
  color: color-mix(in srgb, var(--card) 92%, black);
}
@media (prefers-color-scheme: light) {
  header { color: var(--ink); }
}
header h1 { font-size: 2.1rem; margin: 10px 0 6px; font-style: italic; }
header p { max-width: 62ch; margin: 6px 0; }
header .age { font-size: 1.05rem; }
header .tagline {
  font-size: 1.35rem;
  font-style: italic;
  line-height: 1.4;
  max-width: 58ch;
  margin: 12px 0;
}

.masthead {
  font-variant: small-caps;
  letter-spacing: 0.14em;
  font-size: 0.78rem;
  border-top: 2px solid currentColor;
  border-bottom: 1px solid currentColor;
  padding: 4px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.stamp {
  font-family: ui-monospace, monospace;
  font-variant: normal;
  letter-spacing: 0.08em;
  font-size: 0.72rem;
  color: var(--accent);
  border: 2px solid var(--accent);
  border-radius: 4px;
  padding: 2px 8px;
  transform: rotate(-4deg);
  opacity: 0.85;
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 900px) 270px;
  gap: 20px;
  justify-content: center;
  padding: 16px;
}

.pages { min-width: 0; }

/* the hero stage: the equations page in its own scrollable panel, so you
   scroll the page itself to reach all four equations — the rest of the
   paper does not pile up underneath */
.stage { margin: 0 0 26px; }
.stage-scroll {
  max-height: 80vh;
  overflow-y: auto;
  overflow-x: hidden;
  box-shadow: var(--shadow);
  border: 1px solid rgba(40, 30, 10, 0.25);
  background: var(--card);
  color: var(--ink);
  overscroll-behavior: contain;
}
.stage .caption { margin-top: 10px; }

/* while the five scans arrive — the stage must not sit empty and silent */
.stage-loading {
  min-height: 40vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  color: var(--accent);
  animation: stage-loading-pulse 1.1s ease-in-out infinite alternate;
}
.stage-loading b { margin: 0 0.4em; font-variant-numeric: tabular-nums; }
@keyframes stage-loading-pulse { from { opacity: 0.45; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .stage-loading { animation: none; } }

.caption {
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  color: var(--accent);
  margin: -18px 0 26px;
  text-align: center;
}
@media (prefers-color-scheme: dark) {
  .caption { color: color-mix(in srgb, var(--accent) 80%, white); }
}

/* guided intro — a narration ribbon at the top while the real page heals */
.intro {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 50;
  display: flex;
  justify-content: center;
  padding: 14px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease;
}
.intro.show { opacity: 1; }
.intro[hidden] { display: none; }
.intro-card {
  pointer-events: auto;
  background: var(--card);
  color: var(--ink);
  max-width: min(92%, 640px);
  padding: 16px 24px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  border-left: 4px solid var(--accent);
  text-align: center;
}
#intro-text {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 1.25rem;
  line-height: 1.4;
  white-space: pre-line;
  margin: 0 0 12px;
  min-height: 2.7em;
  transition: opacity 0.35s ease;
}

/* the stage: all five pages stacked, scrollable, each scribbleable */
.page-slot { position: relative; }
.page-tab {
  position: sticky;
  top: 0;
  z-index: 3;
  font-family: ui-monospace, monospace;
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  color: var(--card);
  background: color-mix(in srgb, var(--ink) 88%, transparent);
  padding: 4px 10px;
}
.page-canvas {
  display: block;
  width: 100%;
  height: auto;
  cursor: crosshair;
  /* let a vertical swipe scroll the page; a sideways swipe scribbles/erases */
  touch-action: pan-y;
  background: var(--card);
  border-bottom: 8px solid var(--paper-bg);
}
.page-canvas.healing { outline: 2px solid var(--accent); outline-offset: -2px; }
.page-note {
  background: color-mix(in srgb, var(--accent) 8%, var(--card));
  border-top: 2px solid var(--accent);
  color: var(--ink);
  padding: 12px 16px;
  font-size: 0.9rem;
  line-height: 1.5;
}
.page-note b { color: var(--accent); font-weight: 700; }
.page-note em { color: color-mix(in srgb, var(--ink) 78%, var(--card)); }
.note-eq {
  display: block;
  text-align: center;
  font-family: 'Times New Roman', Georgia, serif;
  font-style: italic;
  font-size: 1.35rem;
  margin: 10px 0;
}
.page-note strong { display: block; margin-bottom: 3px; }

/* the Fig. 2 bench, once app.js has moved it into the scroll under p. 2557 —
   it should read as part of the paper, not as a card floating over it */
.stage-scroll .bench {
  margin: 0;
  border: 0;
  border-top: 2px solid var(--accent);
  border-bottom: 8px solid var(--paper-bg);
  box-shadow: none;
  background: color-mix(in srgb, var(--accent) 8%, var(--card));
}
.stage-scroll .bench p { margin: 0 0 10px; }

/* the AI reveal — the punchline */
.reveal {
  background: var(--ink);
  color: var(--card);
  padding: 22px 24px;
  margin: 26px 0;
  box-shadow: var(--shadow);
  border-left: 5px solid var(--accent);
}
.reveal h2 {
  margin: 0 0 8px;
  font-size: 1.5rem;
  font-style: italic;
  color: var(--card);
}
.reveal p { margin: 0 0 10px; max-width: 64ch; line-height: 1.5; }
.reveal p:last-child { margin-bottom: 0; }
/* the two lines the equivalence turns on, set as mathematics */
.reveal .reveal-eq {
  font-family: 'Times New Roman', Georgia, serif;
  font-style: italic;
  font-size: 1.3rem;
  text-align: center;
  color: var(--card);
  max-width: none;
  margin: 14px 0;
  overflow-x: auto;
}
/* accent is too dark on the near-black .reveal; lift it like .reveal a does */
.reveal-eq b {
  font-style: normal;
  font-weight: 700;
  color: color-mix(in srgb, var(--accent) 60%, white);
}
.reveal-eq sup, .reveal-eq sub { font-size: 0.7em; }
.reveal a { color: color-mix(in srgb, var(--accent) 65%, white); }
/* the derivation, folded away by default — the newcomer never has to open it */
.reveal-more { margin-top: 14px; }
.reveal-more > summary {
  cursor: pointer;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
  color: color-mix(in srgb, var(--accent) 68%, white);
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.14);
}
.reveal-more[open] > summary { margin-bottom: 6px; }
.reveal-more .reveal-eq { color: var(--card); }
@media (prefers-color-scheme: light) {
  .reveal { background: #241f1a; }
}
/* the recall rule, firing live, in the rail */
.livecode {
  margin: 12px 0 0;
  border: 1px solid rgba(40, 30, 10, 0.25);
  background: color-mix(in srgb, var(--accent) 5%, var(--card));
}
.lc-tag {
  font-family: ui-monospace, monospace;
  font-size: 0.62rem;
  letter-spacing: 0.05em;
  color: var(--accent);
  padding: 5px 9px;
  border-bottom: 1px solid rgba(40, 30, 10, 0.18);
}
#lc-state { opacity: 0.75; }
/* equation [1] itself, with the live numbers standing in it — set in serif,
   because it is the paper's mathematics, not the JavaScript that runs it */
.lc-eq {
  padding: 10px 9px;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 0.92rem;
  line-height: 1.45;
}
.lc-eq i { font-style: italic; }
.lc-eq sub { font-size: 0.72em; }
.lc-row { margin-bottom: 8px; }
.lc-row b, #lc-h, #lc-i {
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
/* plain-English gloss — nobody arrives knowing what i and h are */
.lc-c {
  display: block;
  font-family: ui-monospace, monospace;
  font-size: 0.62rem;
  font-style: normal;
  line-height: 1.35;
  color: #9a9184;
  margin-top: 1px;
}
/* the wiring, T — only one of the two is showing, whichever is wired up */
.lc-t {
  border-left: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  padding-left: 7px;
  margin-left: -1px;
}
.lc-t b { color: var(--accent); font-weight: 700; }
.lc-t sup { font-size: 0.72em; }
.lc-t-1982 { display: none; }
body.rule-1982 .lc-t-1985 { display: none; }
body.rule-1982 .lc-t-1982 { display: block; }

/* the three cases of [1]; the one that just fired lights up */
.lc-b {
  padding: 2px 5px;
  margin: 0 -5px;
  color: color-mix(in srgb, var(--ink) 55%, var(--card));
  /* fade between decisions instead of hard-flipping — the panel should read
     as one decision after another, not as a blink */
  transition: background-color 0.2s ease, color 0.2s ease;
}
.lc-b .lc-c { display: inline; margin-left: 6px; }
.lc-b.on {
  color: var(--card);
  background: var(--accent);
}
.lc-b.on .lc-c { color: color-mix(in srgb, var(--card) 78%, var(--accent)); }
/* the legend — every symbol used above, defined once */
.lc-foot {
  font-family: ui-monospace, monospace;
  font-size: 0.62rem;
  line-height: 1.4;
  color: #9a9184;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(40, 30, 10, 0.18);
}
.lc-foot span { display: block; }
.lc-foot span + span { margin-top: 3px; }
.lc-foot i { font-family: Georgia, serif; font-style: italic; }
.lc-count {
  font-family: ui-monospace, monospace;
  padding: 6px 9px 8px;
  border-top: 1px solid rgba(40, 30, 10, 0.18);
}
#lc-flips {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.lc-unit { font-size: 0.63rem; color: #7d7568; letter-spacing: 0.05em; }
/* the caveat: what you watch is sampled, not the whole run */
.lc-sample {
  display: block;
  font-size: 0.6rem;
  line-height: 1.4;
  color: #9a9184;
  margin-top: 5px;
}

/* the story in four links */
.story {
  background: var(--card);
  color: var(--ink);
  border: 1px solid rgba(40, 30, 10, 0.25);
  box-shadow: var(--shadow);
  padding: 16px 20px 16px 8px;
  margin: 0 0 26px;
}
.story h2 {
  font-family: ui-monospace, monospace;
  font-size: 0.8rem; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--accent); margin: 0 0 10px 12px;
}
.story ul { margin: 0; padding-left: 34px; }
.story li { margin: 8px 0; line-height: 1.45; }
.story a { color: var(--accent); text-decoration: none; }
.story a:hover { text-decoration: underline; }
#intro-skip {
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  background: none;
  border: 1px solid var(--ink);
  color: var(--ink);
  padding: 4px 12px;
  cursor: pointer;
}
#intro-skip:hover { background: rgba(40, 30, 10, 0.08); }

.card .hint {
  font-size: 0.78rem;
  color: #6b6257;
  margin: 0 0 8px;
}
.card .deeper {
  margin-top: 14px;
  border-top: 1px solid rgba(40, 30, 10, 0.18);
  padding-top: 10px;
}
.card .deeper summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
}
.card .deeper .eq { margin: 10px 0 4px; }
.card .sub { font-size: 0.72rem; color: #6b6257; margin-bottom: 4px; }

/* instrument rail */
.rail { min-width: 0; }
.card {
  position: sticky;
  top: 14px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid rgba(40, 30, 10, 0.25);
  box-shadow: var(--shadow);
  padding: 14px;
  font-family: ui-monospace, 'Cascadia Mono', monospace;
  font-size: 0.78rem;
}
/* block labels in the rail — the reader needs to know what each readout is */
.card h2 {
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  margin: 14px 0 6px;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
}
.card h2 + .livecode { margin-top: 0; }
#e-value {
  font-size: 1.5rem;
  letter-spacing: 0.02em;
  margin: 2px 0 4px;
  /* never let a long readout push the rail wider than its column */
  max-width: 100%;
  overflow-wrap: anywhere;
}
#e-trace { width: 100%; height: 96px; display: block; margin-bottom: 2px; }
.trace-cap { font-size: 0.66rem; color: #6b6257; line-height: 1.35; margin-bottom: 8px; }
#status { color: #6b6257; min-height: 2.4em; }
/* a reading, not a paragraph — set like the other instruments */
#verdict {
  color: var(--accent);
  font-family: ui-monospace, monospace;
  font-size: 0.72rem;
  line-height: 1.5;
  min-height: 2.2em;
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
}

.controls { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
button {
  font-family: inherit;
  font-size: 0.75rem;
  background: none;
  border: 1px solid var(--ink);
  color: var(--ink);
  padding: 4px 9px;
  cursor: pointer;
}
button:hover { background: rgba(40, 30, 10, 0.08); }
#btn-merge.on { background: var(--accent); color: var(--card); border-color: var(--accent); }

/* cards inline with the pages */
.note, .bench {
  background: var(--card);
  color: var(--ink);
  border: 1px solid rgba(40, 30, 10, 0.25);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin: -8px 0 26px;
  font-size: 0.92rem;
}

.limits {
  background: var(--card);
  color: var(--ink);
  border: 1px solid rgba(40, 30, 10, 0.25);
  box-shadow: var(--shadow);
  padding: 10px 16px;
  margin: -8px 0 26px;
  font-size: 0.86rem;
}
.limits summary {
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  color: var(--accent);
  cursor: pointer;
}
.limits ul { margin: 10px 0 2px; padding-left: 20px; }
.limits li { margin: 6px 0; }
.bench h2 {
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  color: var(--accent);
  margin: 0 0 6px;
}
#fig2-chart { width: 100%; max-width: 560px; height: auto; display: block; margin: 8px 0; }
#fig2-out { font-family: ui-monospace, monospace; font-size: 0.75rem; color: #6b6257; }
.bench select {
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  padding: 2px 4px;
}

footer {
  max-width: 900px;
  margin: 0 auto;
  padding: 8px 16px 48px;
  font-size: 0.8rem;
  color: #8a8172;
}
/* stay quiet, but be findable — `color: inherit` alone made these invisible */
footer a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-color: color-mix(in srgb, var(--accent) 50%, transparent);
}
footer a:hover { color: var(--accent); text-decoration-color: var(--accent); }
footer p { margin: 0 0 10px; line-height: 1.55; }
footer p:last-child { margin-bottom: 0; }
footer strong { color: #a09684; }

@media (max-width: 1100px) {
  /* Stack pages first. The rail used to be a hint and two buttons, so leading
     with it was fine; it is now the equation panel, and opening a phone on
     `i = —, h = —` before you have seen a page reads as a wall of symbols. */
  .layout { display: flex; flex-direction: column; gap: 14px; }
  .card { position: static; }
  /* no nested scroll box on touch — let the phone scroll the whole document */
  .stage-scroll { max-height: none; overflow: visible; }
}

@media (max-width: 600px) {
  html, body { overflow-x: hidden; max-width: 100%; }
  img, canvas { max-width: 100%; }
  .layout, .pages, .stage, .stage-scroll, .page-slot, .rail, .card { min-width: 0; max-width: 100%; }
  header { padding: 24px 14px 6px; }
  header h1 { font-size: 1.6rem; }
  header .tagline { font-size: 1.1rem; }
  header .age { font-size: 0.98rem; }
  .masthead { flex-wrap: wrap; row-gap: 6px; font-size: 0.64rem; letter-spacing: 0.08em; }
  .stamp { white-space: nowrap; }
  .card { padding: 12px; }
}