/* A classic book on the web: the shared look of every book made from
   this template. Book-specific widgets bring their own stylesheets. */

:root {
  --paper: #fbf8f1;
  --paper-deep: #f3eee2;
  --ink: #2a2723;
  --ink-soft: #4a453e;
  --muted: #857e72;
  --rule: #ddd5c4;
  --accent: #a23b2a;
  --curious: #2f6f7a;
  --code-bg: #f3eee2;

  /* The cast: each recurring actor of the book (a validator, a sensor, a
     die...) keeps one colour everywhere: text, figures, washes. These
     are deeper versions of the figure washes (CAST_COLORS in
     tool/art/ink.py), readable as text. Colour means *who*, never *how
     it went*. */
  --cast-a: #2f5f94;
  --cast-b: #3d7a3b;
  --cast-c: #a2611a;
  --cast-d: #7a4a98;
  --cast-e: #2a7f7a;
  --cast-f: #a24d6b;
  --cast-g: #6f6a2a;

  /* Syntax colours for code snippets: quiet, so the code reads as text. */
  --code-keyword: #8a3324;
  --code-string: #4f7a2a;
  --code-comment: #8a8273;
  --code-number: #a2611a;
  --code-builtin: #6a4a8a;
  --code-type: #2f5f94;

  --serif: "Newsreader", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --sans: "Source Sans 3", "Helvetica Neue", Arial, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --hand: "Patrick Hand", "Segoe Print", "Comic Neue", cursive;

  --measure: 37rem;
  --sidebar: 14rem;
  --margin-note: 14rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1b1a17;
    --paper-deep: #24221e;
    --ink: #e9e3d6;
    --ink-soft: #cfc8b9;
    --muted: #958d7f;
    --rule: #3a3731;
    --accent: #e0846f;
    --curious: #7fbcc6;
    --code-bg: #24221e;

    --cast-a: #86aedb;
    --cast-b: #8cc489;
    --cast-c: #e2a95f;
    --cast-d: #bd98d4;
    --cast-e: #72c6bf;
    --cast-f: #dc8eaa;
    --cast-g: #c6bf70;

    --code-keyword: #e39a85;
    --code-string: #a6c98a;
    --code-comment: #8e877a;
    --code-number: #e2a95f;
    --code-builtin: #c4a2dc;
    --code-type: #8fb4de;
  }
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1.3rem;
  line-height: 1.6;
  font-optical-sizing: auto;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--accent);
  text-decoration-line: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.18em;
  text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent);
}
a:hover { text-decoration-color: var(--accent); }

::selection { background: color-mix(in srgb, var(--accent) 22%, transparent); }

/* ---------- Chapter layout ---------- */

.page {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, var(--measure)) var(--margin-note);
  column-gap: 3.5rem;
  max-width: calc(var(--sidebar) + var(--measure) + var(--margin-note) + 7rem + 4rem);
  margin: 0 auto;
  padding: 0 2rem;
}

.sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  max-height: 100vh;
  overflow-y: auto;
  padding: 3.5rem 0 2rem;
  font-family: var(--sans);
  font-size: 0.8rem;
  line-height: 1.45;
}

.sidebar a { color: var(--ink-soft); text-decoration: none; }
.sidebar a:hover { color: var(--accent); }

.sidebar-book {
  display: block;
  font-family: var(--serif);
  font-size: 1.05rem;
  font-style: italic;
  margin-bottom: 2.25rem;
}

.sidebar-number {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.sidebar-title {
  margin: 0.2rem 0 1.25rem;
  font-family: var(--serif);
  font-size: 1.35rem;
  line-height: 1.2;
  font-weight: 600;
}

.sidebar-sections {
  list-style: none;
  margin: 0 0 2rem;
  padding: 0 0 0 0.9rem;
  border-left: 1px solid var(--rule);
}
.sidebar-sections li { margin: 0.45rem 0; }

.sidebar-pager { display: flex; flex-direction: column; gap: 0.4rem; }

.content {
  grid-column: 2;
  padding: 3.5rem 0 5rem;
  min-width: 0;
}

/* ---------- Chapter header ---------- */

.chapter-header {
  margin-bottom: 3rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid var(--rule);
}

.chapter-header .chapter-part {
  margin: 0;
  font-family: var(--sans);
  font-size: 0.75rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.chapter-header .chapter-number {
  margin: 1.5rem 0 0;
  font-size: 4.5rem;
  line-height: 1;
  font-weight: 400;
  color: var(--accent);
  font-variant-numeric: oldstyle-nums;
}

.chapter-title {
  margin: 0.4rem 0 0;
  font-size: 3rem;
  line-height: 1.1;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.status, .chapter-header .status {
  display: inline-block;
  margin: 1rem 0 0;
  padding: 0.1rem 0.55rem;
  border: 1px solid var(--rule);
  border-radius: 999px;
  font-family: var(--sans);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------- Prose ---------- */

.content p { margin: 0 0 1.1em; hyphens: auto; }

.content h2 {
  margin: 3.25rem 0 1rem;
  font-size: 1.75rem;
  line-height: 1.2;
  font-weight: 600;
}

.content h3 {
  margin: 2.25rem 0 0.75rem;
  font-size: 1.3rem;
  font-weight: 600;
  font-style: italic;
}

.content h2 a.anchor, .content h3 a.anchor { color: inherit; text-decoration: none; }
.content h2 a.anchor:hover::after, .content h3 a.anchor:hover::after {
  content: " \00A7";
  color: var(--accent);
  font-weight: 400;
}

.content ul, .content ol { margin: 0 0 1.2em; padding-left: 1.4em; }
.content li { margin: 0.35em 0; padding-left: 0.2em; }
.content ol li::marker { color: var(--muted); font-variant-numeric: oldstyle-nums; }
.content ul li::marker { color: var(--accent); }

.content blockquote {
  margin: 1.75em 0;
  padding: 0.1em 0 0.1em 1.25em;
  border-left: 2px solid var(--accent);
  font-size: 1.15em;
  font-style: italic;
  color: var(--ink-soft);
}
.content blockquote p { margin: 0; }

.content hr {
  border: 0;
  margin: 3rem 0;
  text-align: center;
}
.content hr::before {
  content: "\2042";
  color: var(--muted);
  font-size: 1.2rem;
}

/* ---------- Key concepts ---------- */

.concept {
  margin: 2.25rem 0;
  padding: 1.1rem 1.4rem 1.25rem;
  border: 1px solid var(--rule);
  border-top: 3px solid var(--accent);
  border-radius: 2px 2px 6px 6px;
  background: var(--paper-deep);
  font-size: 1.1rem;
  line-height: 1.55;
}

.content .concept-label {
  margin: 0;
  font-family: var(--sans);
  font-size: 0.68rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
}

.content .concept-term {
  margin: 0.1rem 0 0.6rem;
  font-size: 1.45rem;
  line-height: 1.25;
  font-weight: 600;
}

.content .concept p:last-child,
.content .concept ul:last-child,
.content .concept ol:last-child { margin-bottom: 0; }

/* ---------- Side trips for the curious ---------- */

.curious {
  margin: 2.25rem 0;
  padding: 1rem 1.4rem 1.15rem;
  border: 1.5px dashed color-mix(in srgb, var(--curious) 55%, transparent);
  border-left: 4px solid var(--curious);
  border-radius: 6px;
  background: color-mix(in srgb, var(--curious) 7%, var(--paper));
  font-size: 1.02rem;
  line-height: 1.55;
}

.content .curious-label {
  margin: 0;
  font-family: var(--sans);
  font-size: 0.68rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--curious);
}

.content .curious-term {
  margin: 0.1rem 0 0.6rem;
  font-family: var(--hand);
  font-size: 1.5rem;
  line-height: 1.2;
  color: var(--curious);
}

.content .curious p:last-child,
.content .curious ul:last-child,
.content .curious ol:last-child { margin-bottom: 0; }

/* ---------- Code ---------- */

code {
  font-family: var(--mono);
  font-size: 0.78em;
  background: var(--code-bg);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}

pre {
  margin: 1.5em 0;
  padding: 0.9rem 1rem;
  background: var(--code-bg);
  border-radius: 6px;
  overflow-x: auto;
  /* Sized so that 72 columns of code fit the text column. */
  font-size: 0.78rem;
  line-height: 1.6;
}
pre code {
  padding: 0;
  background: none;
  font-size: inherit;
}

.cast { font-weight: 500; }
.cast-a { color: var(--cast-a); }
.cast-b { color: var(--cast-b); }
.cast-c { color: var(--cast-c); }
.cast-d { color: var(--cast-d); }
.cast-e { color: var(--cast-e); }
.cast-f { color: var(--cast-f); }
.cast-g { color: var(--cast-g); }

/* ---------- Snippets of the program ---------- */

.snippet { margin: 1.6em 0; }
.snippet pre { margin: 0; border-radius: 0 6px 6px 6px; }

.snippet-file {
  display: inline-block;
  padding: 0.2rem 0.7rem;
  background: var(--code-bg);
  border-radius: 6px 6px 0 0;
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--muted);
}

.snippet-change {
  margin-left: 0.9rem;
  font-family: var(--sans);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

/* Lines a snippet replaces: still readable, clearly gone. */
pre code del {
  color: var(--muted);
  opacity: 0.75;
  text-decoration-line: line-through;
  text-decoration-color: color-mix(in srgb, var(--accent) 55%, transparent);
  text-decoration-thickness: 1px;
}

pre code .k { color: var(--code-keyword); }
pre code .s { color: var(--code-string); }
pre code .c { color: var(--code-comment); font-style: italic; }
pre code .n,
pre code .v { color: var(--code-number); }
pre code .b { color: var(--code-builtin); }
pre code .t { color: var(--code-type); }
pre code .f { font-weight: 500; }

/* ---------- Margin notes ---------- */

aside {
  font-family: var(--sans);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--ink-soft);
}
aside p { margin: 0 0 0.6em; hyphens: manual; }
aside p:last-child { margin-bottom: 0; }

@media (min-width: 1200px) {
  aside {
    float: right;
    clear: right;
    width: var(--margin-note);
    margin: 0.35rem calc(-1 * (var(--margin-note) + 3.5rem)) 1rem 0;
    padding-top: 0.6rem;
    border-top: 1px solid var(--rule);
  }
}

@media (max-width: 1199.98px) {
  aside {
    margin: 0 0 1.25rem;
    padding: 0.75rem 1rem;
    border-left: 2px solid var(--rule);
    background: var(--paper-deep);
    border-radius: 0 4px 4px 0;
  }
}

/* ---------- Illustrations ---------- */

figure.illustration { margin: 2.5rem 0; }
figure.illustration svg {
  display: block;
  width: 100%;
  height: auto;
  color: var(--ink);
  overflow: visible;
}
figure.illustration svg text { font-family: var(--hand); fill: currentColor; stroke: none; }

/* ---------- Figure placeholders ---------- */

figure.placeholder { margin: 2.25rem 0; }

.placeholder-art {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 12rem;
  padding: 2rem 1.5rem;
  border: 1.5px dashed var(--rule);
  border-radius: 8px;
  background:
    repeating-linear-gradient(-45deg, transparent 0 14px, color-mix(in srgb, var(--rule) 30%, transparent) 14px 15px);
}

.placeholder-kind {
  font-family: var(--sans);
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
}

.placeholder-name {
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--ink-soft);
}

figure.placeholder figcaption {
  margin-top: 0.75rem;
  font-family: var(--sans);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--muted);
}

/* ---------- Pager ---------- */

.pager {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
  font-size: 0.9rem;
}
.pager a { text-decoration: none; }
.pager-next { margin-left: auto; text-align: right; }

/* ---------- Contents page ---------- */

.cover {
  max-width: 38rem;
  margin: 0 auto;
  padding: 6rem 2rem 5rem;
}

.cover-kicker {
  margin: 0;
  font-family: var(--sans);
  font-size: 0.75rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
}

.cover-title {
  margin: 0.75rem 0 0;
  font-size: clamp(2.75rem, 9vw, 4.5rem);
  line-height: 1.02;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.cover-subtitle {
  margin: 1rem 0 0;
  font-size: 1.35rem;
  font-style: italic;
  color: var(--ink-soft);
}

.contents { margin-top: 4rem; }

.contents-part {
  margin: 2.5rem 0 0.75rem;
  font-family: var(--sans);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.contents-chapters {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--rule);
}
.contents-chapters li {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--rule);
}
.contents-chapters a { color: var(--ink); text-decoration: none; font-size: 1.35rem; }
.contents-chapters a:hover { color: var(--accent); }
.contents-chapters .status { margin: 0 0 0 auto; }

.contents-number {
  display: inline-block;
  min-width: 2rem;
  color: var(--accent);
  font-variant-numeric: oldstyle-nums;
}

p.contents-later { margin: 0; color: var(--muted); font-style: italic; font-size: 1.05rem; }

/* ---------- Narrow screens ---------- */

@media (max-width: 1199.98px) {
  .page {
    grid-template-columns: var(--sidebar) minmax(0, var(--measure));
    max-width: calc(var(--sidebar) + var(--measure) + 3.5rem + 4rem);
  }
}

@media (max-width: 860px) {
  body { font-size: 1.15rem; }
  .page {
    display: block;
    padding: 0 1rem;
  }
  .sidebar {
    position: static;
    max-height: none;
    padding: 1.5rem 0 0;
  }
  .sidebar-book { margin-bottom: 0; }
  .sidebar-number, .sidebar-title, .sidebar-sections, .sidebar-pager { display: none; }
  .content { padding-top: 1.5rem; }
  .chapter-header .chapter-number { font-size: 3.25rem; }
  .chapter-title { font-size: 2.3rem; }
  .cover { padding: 3.5rem 1rem 3rem; }
}

/* ------------------------------------------------------------------
   Widgets: the shared frame of every animated illustration (see
   tool/web/kit.js). A widget's own stylesheet adds only its stage.
   ------------------------------------------------------------------ */

.widget { margin: 1.6rem 0; }

/* Without JavaScript, the text block is all there is. With it, the
   scene replaces the text on screen. */
.widget .wg-box { display: none; }
.widget.widget-live .wg-box { display: block; }
.widget.widget-live > pre { display: none; }

.wg-box {
  padding: 0.8rem 1rem 0.9rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper-deep);
  font-family: var(--sans);
  font-size: 0.85rem;
}

.wg-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.8rem;
}

.wg-title {
  font-family: var(--hand);
  font-size: 1.3rem;
  color: var(--ink);
  margin-right: auto;
}

.wg-title code { font-size: 0.85em; }

.wg-controls { display: flex; gap: 0.35rem; flex-wrap: wrap; }

.wg-controls button {
  font: inherit;
  min-width: 2.2rem;
  padding: 0.2rem 0.5rem;
  border: 1px solid var(--rule);
  border-radius: 4px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}

.wg-controls button:hover { border-color: var(--ink-soft); }

/* The code being stepped through. The line running now is drawn
   heavier, never in a colour. */
.wg-code {
  margin: 0.7rem 0 0.4rem;
  padding: 0.5rem 0.7rem;
  background: var(--paper);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.72rem;
  line-height: 1.6;
  color: var(--muted);
  overflow-x: auto;
  white-space: pre;
}

.wg-code-line { display: block; padding-left: 1.1em; position: relative; }
.wg-code-line.wg-current { color: var(--ink); font-weight: 700; }
.wg-code-line.wg-current::before { content: "\25B8"; position: absolute; left: 0; }

.wg-stage {
  display: block;
  width: 100%;
  margin: 0.8rem auto 0.2rem;
}

/* What just happened, in a sentence. */
.wg-line {
  min-height: 4.4em;
  margin-top: 0.4rem;
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1.45;
  color: var(--ink);
}

.wg-line code { font-size: 0.85em; }

.wg-hand {
  font-family: var(--hand);
  fill: var(--ink);
}

.wg-muted { fill: var(--muted); }

.wg-cast-a { fill: var(--cast-a); }
.wg-cast-b { fill: var(--cast-b); }
.wg-cast-c { fill: var(--cast-c); }
.wg-cast-d { fill: var(--cast-d); }
.wg-cast-e { fill: var(--cast-e); }
.wg-cast-f { fill: var(--cast-f); }
.wg-cast-g { fill: var(--cast-g); }
