:root {
  color-scheme: light;
  --paper: #fbf9f5;
  --paper-2: #f3efe7;
  --ink: #23201c;
  --muted: #625b52;
  --faint: #8e867b;
  --rule: #e2dbcf;
  --accent: #a3263a;
  --accent-soft: #f6e6e6;
  --code-bg: #f4f0e8;
  --mark: #fff1c9;

  /* Chunk palette for the hex explorer. */
  --c0: #f2d7d5; --c1: #d9e6f5; --c2: #e4efd9; --c3: #f5e6cc;
  --c4: #e6dcf2; --c5: #d5ecea; --c6: #f2dcea; --c7: #e9e6d6;
  --c8: #dbe3e9; --c9: #f0e2d2; --c10: #dfe9e0; --c11: #ece0e0;

  --serif: "Crimson Pro", "Iowan Old Style", Georgia, serif;
  --sans: "Libre Franklin", "Helvetica Neue", Arial, sans-serif;
  --mono: "Source Code Pro", ui-monospace, "SF Mono", Menlo, monospace;
  --measure: 40rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --paper: #1b1a18;
    --paper-2: #23211e;
    --ink: #ece6dc;
    --muted: #b4ab9e;
    --faint: #8a8276;
    --rule: #38342e;
    --accent: #e77d8c;
    --accent-soft: #3a2427;
    --code-bg: #26241f;
    --mark: #4a3f1c;
    --c0: #4a2c2a; --c1: #263a52; --c2: #2f4426; --c3: #4a3b20;
    --c4: #3a2f4d; --c5: #20403d; --c6: #4a2a3f; --c7: #3d3a28;
    --c8: #2a3640; --c9: #46362a; --c10: #2c3d2f; --c11: #43302f;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 1.22rem/1.62 var(--serif);
  font-kerning: normal;
  font-variant-numeric: oldstyle-nums;
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }

::selection { background: var(--mark); }

/* ---------- Masthead ---------- */

.masthead {
  border-bottom: 1px solid var(--rule);
  background: var(--paper-2);
}

.masthead-inner {
  max-width: 64rem;
  margin: 0 auto;
  padding: 5.5rem 1.5rem 3.5rem;
}

.kicker {
  margin: 0 0 1rem;
  font: 600 0.72rem/1 var(--sans);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
}

.masthead h1 {
  margin: 0;
  font: 300 clamp(2.8rem, 8vw, 5.2rem)/1 var(--serif);
  letter-spacing: -0.02em;
}

.masthead h1 em { font-style: italic; color: var(--accent); }

.dek {
  max-width: 36rem;
  margin: 1.4rem 0 0;
  font-size: 1.4rem;
  line-height: 1.45;
  font-style: italic;
  color: var(--muted);
}

.byline {
  margin-top: 2rem;
  font: 400 0.8rem/1.5 var(--sans);
  color: var(--faint);
}

/* ---------- Layout ---------- */

.page {
  max-width: 64rem;
  margin: 0 auto;
  padding: 0 1.5rem 6rem;
}

.contents {
  max-width: var(--measure);
  margin: 3.5rem 0 1rem;
  font-family: var(--sans);
}

.contents h2 {
  margin: 0 0 1rem;
  font: 600 0.72rem/1 var(--sans);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--faint);
}

.contents ol {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: part;
}

.contents > ol > li {
  counter-increment: part;
  padding: 0.85rem 0;
  border-top: 1px solid var(--rule);
}

.contents > ol > li > a {
  font-size: 1rem;
  font-weight: 600;
  color: var(--ink);
  text-decoration: none;
}

.contents > ol > li > a::before {
  content: counter(part, upper-roman) ".";
  display: inline-block;
  width: 2.4rem;
  color: var(--accent);
  font-family: var(--serif);
  font-weight: 400;
}

.contents ol ol {
  margin: 0.35rem 0 0 2.4rem;
  font-size: 0.86rem;
  line-height: 1.7;
  color: var(--muted);
}

.contents ol ol a { color: var(--muted); text-decoration: none; }
.contents ol ol a:hover { color: var(--accent); }

article { position: relative; }

.part-head {
  max-width: var(--measure);
  margin: 6rem 0 2rem;
  padding-top: 2rem;
  border-top: 1px solid var(--rule);
}

.part-head .num {
  display: block;
  font: 300 3.4rem/1 var(--serif);
  color: var(--accent);
}

.part-head h2 {
  margin: 0.6rem 0 0;
  font: 600 2rem/1.15 var(--sans);
  letter-spacing: -0.015em;
}

.part-head p {
  margin: 0.6rem 0 0;
  font-style: italic;
  color: var(--muted);
}

section > h3 {
  max-width: var(--measure);
  margin: 3.2rem 0 0.8rem;
  font: 600 1.3rem/1.3 var(--sans);
  letter-spacing: -0.01em;
}

section > h4 {
  max-width: var(--measure);
  margin: 2.2rem 0 0.4rem;
  font: 600 0.95rem/1.3 var(--sans);
  color: var(--ink);
}

h3 a.anchor, h4 a.anchor { color: inherit; text-decoration: none; }

p, ul, ol.prose, dl, blockquote { max-width: var(--measure); }

p { margin: 0 0 1.05rem; }

ul, ol.prose { padding-left: 1.3rem; margin: 0 0 1.2rem; }
li { margin-bottom: 0.35rem; }
li::marker { color: var(--faint); }

strong { font-weight: 600; }

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

blockquote cite {
  display: block;
  margin-top: 0.4rem;
  font: normal 0.78rem/1.4 var(--sans);
  color: var(--faint);
}

.lead::first-letter {
  float: left;
  margin: 0.08em 0.1em 0 0;
  font-size: 3.9em;
  line-height: 0.8;
  color: var(--accent);
}

/* Margin notes: in the right gutter on wide screens, inline otherwise. */
.aside {
  max-width: var(--measure);
  margin: 0 0 1.2rem;
  padding: 0.8rem 1rem;
  border-left: 2px solid var(--rule);
  font: 400 0.82rem/1.55 var(--sans);
  color: var(--muted);
}

.aside strong { color: var(--ink); }

@media (min-width: 1080px) {
  .aside {
    float: right;
    clear: right;
    width: 15.5rem;
    margin: 0.3rem -1rem 1rem 0;
    padding: 0 0 0 1rem;
  }
  .page { padding-right: 18rem; max-width: 82rem; }
  .masthead-inner { max-width: 82rem; padding-right: 18rem; }
}

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

code, kbd, pre { font-family: var(--mono); font-variant-numeric: normal; }

:not(pre) > code {
  padding: 0.05em 0.3em;
  font-size: 0.8em;
  background: var(--code-bg);
  border-radius: 3px;
}

pre {
  max-width: var(--measure);
  margin: 1.2rem 0 1.6rem;
  padding: 1rem 1.2rem;
  overflow-x: auto;
  font-size: 0.8rem;
  line-height: 1.55;
  background: var(--code-bg);
  border-radius: 4px;
}

pre.wide { max-width: var(--measure); }

pre .c { color: var(--faint); font-style: italic; }
pre .k { color: var(--accent); }
pre .hl { background: var(--mark); }

/* ---------- Tables ---------- */

.table-wrap {
  max-width: var(--measure);
  margin: 1.2rem 0 1.8rem;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font: 400 0.86rem/1.45 var(--sans);
  font-variant-numeric: lining-nums tabular-nums;
}

th, td {
  padding: 0.55rem 0.8rem 0.55rem 0;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--rule);
}

th {
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
}

td code { white-space: nowrap; }
td.why { color: var(--muted); }

/* Byte layout diagrams. */
.layout {
  display: flex;
  flex-wrap: wrap;
  max-width: var(--measure);
  margin: 1.2rem 0 1.6rem;
  font: 400 0.74rem/1.3 var(--mono);
  border: 1px solid var(--rule);
  border-radius: 4px;
  overflow: hidden;
}

.layout > div {
  flex: var(--w, 1) 1 0;
  min-width: 5.5rem;
  padding: 0.55rem 0.6rem;
  border-right: 1px solid var(--rule);
  background: var(--paper-2);
}

.layout > div:last-child { border-right: 0; }
.layout > div.var { background: transparent; }

.layout b {
  display: block;
  margin-bottom: 0.15rem;
  font: 600 0.68rem/1.2 var(--sans);
  color: var(--ink);
}

.layout span { color: var(--faint); }

/* Bit diagrams for the compact term encoding. */
.bits {
  display: grid;
  grid-template-columns: repeat(8, 2.2rem);
  gap: 2px;
  margin: 0.5rem 0;
  font: 500 0.82rem/2.2rem var(--mono);
  text-align: center;
}

.bits span { background: var(--paper-2); border-radius: 2px; }
.bits .v { background: var(--c1); }
.bits .f { background: var(--c3); }
.bits .t { background: var(--c0); }

.forms {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr));
  gap: 1.2rem 2rem;
  max-width: var(--measure);
  margin: 1.4rem 0 1.8rem;
}

.form h5 {
  margin: 0;
  font: 600 0.82rem/1.3 var(--sans);
}

.form p {
  margin: 0.3rem 0 0;
  font: 400 0.8rem/1.5 var(--sans);
  color: var(--muted);
}

/* ---------- Comparison cards ---------- */

.alts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr));
  gap: 1px;
  max-width: var(--measure);
  margin: 1.6rem 0 2rem;
  background: var(--rule);
  border: 1px solid var(--rule);
}

.alt {
  padding: 1rem 1.1rem 1.1rem;
  background: var(--paper);
  font: 400 0.84rem/1.5 var(--sans);
  color: var(--muted);
}

.alt h5 {
  margin: 0 0 0.4rem;
  font: 600 0.9rem/1.3 var(--sans);
  color: var(--ink);
}

.alt .verdict {
  display: block;
  margin-top: 0.6rem;
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}

/* ---------- Hex explorer ---------- */

.explorer {
  max-width: var(--measure);
  margin: 1.6rem 0 2rem;
  border: 1px solid var(--rule);
  border-radius: 6px;
  overflow: hidden;
  background: var(--paper);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.8rem;
  border-bottom: 1px solid var(--rule);
  background: var(--paper-2);
}

.legend button {
  padding: 0.25rem 0.55rem;
  font: 500 0.74rem/1.3 var(--mono);
  color: var(--ink);
  background: var(--chunk);
  border: 1px solid transparent;
  border-radius: 3px;
  cursor: pointer;
}

.legend button[aria-pressed="true"] { border-color: var(--ink); }

.hex {
  margin: 0;
  padding: 0.9rem 0.8rem;
  max-width: none;
  border-radius: 0;
  background: transparent;
  font-size: 0.74rem;
  line-height: 1.6;
  overflow-x: auto;
}

.hex .off { color: var(--faint); user-select: none; }
.hex .b { padding: 0 1px; border-radius: 2px; cursor: pointer; }
.hex .b.hdr { font-weight: 700; }
.hex .b.pad { color: var(--faint); }
.hex .b.dim { opacity: 0.28; }
.hex .asc { color: var(--muted); }

.note {
  min-height: 7.5rem;
  padding: 0.9rem 1.1rem 1rem;
  border-top: 1px solid var(--rule);
  font: 400 0.86rem/1.55 var(--sans);
  color: var(--muted);
}

.note h5 { margin: 0 0 0.3rem; font: 600 0.9rem/1.3 var(--mono); color: var(--ink); }
.note p { margin: 0 0 0.4rem; max-width: none; }

/* ---------- Playground ---------- */

.play {
  display: grid;
  gap: 1rem;
  max-width: var(--measure);
  margin: 1.4rem 0 2rem;
  padding: 1.1rem 1.2rem 1.2rem;
  border: 1px solid var(--rule);
  border-radius: 6px;
  font: 400 0.84rem/1.4 var(--sans);
}

.play-row {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.8rem;
}

.play label {
  display: grid;
  gap: 0.3rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
}

.play input, .play select {
  min-width: 0;
  padding: 0.45rem 0.6rem;
  font: 400 0.9rem/1.2 var(--mono);
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 4px;
}

.play input:focus, .play select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.play .grow { flex: 1 1 14rem; }
.play .grow input { width: 100%; }

.out {
  padding: 0.7rem 0.8rem;
  font: 400 0.84rem/1.55 var(--mono);
  background: var(--code-bg);
  border-radius: 4px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.out .err { color: var(--accent); }

/* ---------- Footer ---------- */

footer {
  max-width: var(--measure);
  margin-top: 6rem;
  padding-top: 1.6rem;
  border-top: 1px solid var(--rule);
  font: 400 0.8rem/1.6 var(--sans);
  color: var(--faint);
}

footer a { color: var(--muted); }

.clear { clear: both; }

@media (max-width: 640px) {
  body { font-size: 1.1rem; }
  .masthead-inner { padding: 3.2rem 1rem 2.4rem; }
  .page { padding: 0 1rem 4rem; }
  .part-head { margin-top: 4rem; }
  .part-head h2 { font-size: 1.6rem; }
  .hex { font-size: 0.62rem; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

/* ---------- Site chrome shared by all deep-dives ---------- */

.kicker a { color: inherit; text-decoration: none; }
.kicker a:hover { text-decoration: underline; }
.kicker .chapter-ref { color: var(--faint); }

/* ---------- Landing page ---------- */

.intro { margin-top: 3.5rem; }

.topics {
  max-width: var(--measure);
  margin: 3rem 0 0;
  padding: 0;
  list-style: none;
  border-top: 1px solid var(--rule);
}

.topic {
  display: grid;
  grid-template-columns: 3.4rem 1fr;
  gap: 0 1rem;
  margin: 0;
  padding: 1.8rem 0;
  border-bottom: 1px solid var(--rule);
}

.topic .num {
  font: 300 2.6rem/1 var(--serif);
  color: var(--accent);
}

.topic.planned .num { color: var(--faint); }

.topic h3 {
  margin: 0;
  font: 600 1.3rem/1.3 var(--sans);
  letter-spacing: -0.01em;
}

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

.topic .meta {
  margin: 0.35rem 0 0.7rem;
  font: 500 0.7rem/1.4 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
}

.topic .meta .status { color: var(--accent); }
.topic.planned .meta .status { color: var(--faint); }

.topic p { margin: 0; color: var(--muted); }

.topic .sources {
  margin-top: 0.7rem;
  font: 400 0.78rem/1.6 var(--sans);
  color: var(--faint);
}

@media (max-width: 640px) {
  .topic { grid-template-columns: 2.4rem 1fr; }
  .topic .num { font-size: 2rem; }
}

/* Thanks to the book's author (landing page). */
.tribute {
  max-width: var(--measure);
  margin: 3rem 0 0;
  padding: 1.6rem 1.6rem 1.4rem;
  background: var(--paper-2);
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
}

.tribute p { margin-bottom: 0.9rem; }
.tribute .kicker { margin-bottom: 0.8rem; }

.tribute-links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1.4rem;
  margin: 1.1rem 0 0 !important;
  font: 500 0.82rem/1.4 var(--sans);
}

.tribute-links a:first-child { font-weight: 600; }
