:root {
  --bg: #f6f1e7;
  --surface: #efe7d8;
  --ink: #2a211b;
  --muted: #6f6256;
  --rule: #d9ccb6;
  --accent: #b5401c; /* was #c8461f: 3.93:1 on --surface, now 4.6:1 */

  /* sandpile heights 0..3, plus the avalanche glow */
  --h0: #f6f1e7;
  --h1: #dcc8a2;
  --h2: #b07f4f;
  --h3: #4d3123;
  --glow: #e4572e;

  --serif: "Newsreader", "Iowan Old Style", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, Menlo, monospace;
  --measure: 38rem;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14110e;
    --surface: #1d1915;
    --ink: #ece3d4;
    --muted: #a3968a;
    --rule: #3a322a;
    --accent: #ff7a4d;

    --h0: #14110e;
    --h1: #3d3127;
    --h2: #8f6743;
    --h3: #efd2a4;
    --glow: #ff7a4d;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1.2rem;
  line-height: 1.6;
  font-optical-sizing: auto;
}

a { color: inherit; text-decoration-color: var(--accent); text-underline-offset: 0.18em; }
a:hover { color: var(--accent); }

/* ---------- accessibility ---------- */

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
canvas:focus-visible { outline-offset: 3px; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

.skip {
  position: absolute; left: 16px; top: -3rem; z-index: 10;
  font-family: var(--mono); font-size: 0.8rem; padding: 6px 10px;
  background: var(--ink); color: var(--bg); border-radius: 4px;
}
.skip:focus { top: 8px; }

/* keyboard cursor drawn over focusable canvases (see assets/a11y.js) */
.kbd-cursor {
  position: absolute; width: 18px; height: 18px; pointer-events: none; z-index: 2;
  transform: translate(-50%, -50%); border-radius: 50%;
  border: 2px solid var(--accent); box-shadow: 0 0 0 2px var(--bg);
}
.kbd-cursor[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

.wrap { max-width: var(--measure); margin: 0 auto; padding: 0 16px; }
.wide { max-width: 52rem; margin: 0 auto; padding: 0 16px; }

header.site {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 1.25rem 16px; max-width: 52rem; margin: 0 auto;
  font-family: var(--mono); font-size: 0.8rem; letter-spacing: 0.02em;
}
header.site a { text-decoration: none; }
header.site .muted { color: var(--muted); }

h1, h2, h3 { font-weight: 500; line-height: 1.15; letter-spacing: -0.01em; }
h1 { font-size: clamp(2.4rem, 7vw, 3.6rem); margin: 2.5rem 0 0.5rem; }
h2 { font-size: 1.7rem; margin: 3rem 0 0.75rem; }
p { margin: 0 0 1.1rem; }

.kicker {
  font-family: var(--mono); font-size: 0.75rem; text-transform: uppercase;
  letter-spacing: 0.12em; color: var(--accent);
}
.lede { font-size: 1.35rem; color: var(--muted); }
.muted { color: var(--muted); }

hr { border: 0; border-top: 1px solid var(--rule); margin: 3rem 0; }

blockquote {
  margin: 1.5rem 0; padding-left: 1rem; border-left: 2px solid var(--accent);
  color: var(--muted); font-style: italic;
}

code, .mono { font-family: var(--mono); font-size: 0.85em; }

/* ---------- figures / simulations ---------- */

figure.sim {
  margin: 2rem auto; padding: 0;
  max-width: 40rem;
}
.sim-frame {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 12px;
}
.sim canvas.field {
  display: block; width: 100%; aspect-ratio: 1 / 1;
  image-rendering: pixelated; image-rendering: crisp-edges;
  border-radius: 3px; cursor: default;
  background: var(--h0);
}
/* Only canvases you paint, pour or chase on capture touch; the rest let the page scroll. */
.sim canvas.field.paint { touch-action: none; cursor: crosshair; }
.controls {
  display: flex; flex-wrap: wrap; gap: 8px 10px; align-items: center;
  margin-top: 12px; font-family: var(--mono); font-size: 0.8rem;
}
.controls label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.controls input[type="range"] { width: 120px; min-height: 24px; accent-color: var(--accent); }
/* Text fields and selects need a visible boundary (3:1); --rule alone is too faint. */
.sim .controls select, .sim .controls input[type="text"] { border-color: var(--muted); }
button {
  font: inherit; font-family: var(--mono); font-size: 0.8rem;
  color: var(--ink); background: var(--bg);
  border: 1px solid var(--rule); border-radius: 4px;
  padding: 6px 10px; cursor: pointer;
  min-height: 30px; min-width: 30px;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
button.primary:hover { background: var(--accent); border-color: var(--accent); }

.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
  gap: 8px; margin-top: 12px; font-family: var(--mono); font-size: 0.75rem;
}
.stat { color: var(--muted); }
.stat b { display: block; color: var(--ink); font-size: 1.05rem; font-weight: 500; }

figcaption {
  font-size: 0.95rem; color: var(--muted); margin-top: 10px; padding: 0 4px;
}

.legend { display: flex; gap: 12px; flex-wrap: wrap; font-family: var(--mono); font-size: 0.75rem; color: var(--muted); margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 12px; height: 12px; border-radius: 2px; display: inline-block; border: 1px solid var(--rule); }

canvas.chart { display: block; width: 100%; height: 220px; }

/* ---------- landing ---------- */

.hero-canvas {
  display: block; width: 100%; max-width: 52rem; margin: 0 auto;
  aspect-ratio: 3 / 1; image-rendering: pixelated;
  margin-bottom: 1.5rem;
}
ol.chapters { list-style: none; padding: 0; margin: 2rem 0; counter-reset: ch; }
ol.chapters li {
  counter-increment: ch; display: grid; grid-template-columns: 2.5rem 1fr;
  padding: 1rem 0; border-top: 1px solid var(--rule);
}
ol.chapters li::before {
  content: counter(ch, decimal-leading-zero);
  font-family: var(--mono); font-size: 0.8rem; color: var(--muted); padding-top: 0.35rem;
}
ol.chapters .title { font-size: 1.35rem; }
ol.chapters .desc { color: var(--muted); font-size: 1rem; }
ol.chapters .soon { opacity: 0.55; }
ol.chapters .tag { font-family: var(--mono); font-size: 0.7rem; color: var(--muted); margin-left: 0.5rem; }

footer.site {
  max-width: var(--measure); margin: 4rem auto 0; padding: 2rem 16px 3rem;
  border-top: 1px solid var(--rule); font-family: var(--mono); font-size: 0.75rem; color: var(--muted);
}

.next { margin: 3rem 0; font-family: var(--mono); font-size: 0.85rem; }
