/* ---------- root ---------- */
:root {
  --bg: #faf8f1;
  --paper: #fffdf6;
  --ink: #1f2937;
  --ink-soft: #4b5563;
  --ink-faint: #9ca3af;
  --rule: #e5e1d3;
  --blue: #2347d9;
  --blue-soft: #5b7cf5;
  --blue-tint: #eef2ff;
  --orange: #f6802b;
  --green: #2f8f5b;
  --red: #c64545;
  --serif: "Iowan Old Style", "Palatino", "Palatino Linotype", Georgia, serif;
  --sans:  "Inter", "Helvetica Neue", system-ui, sans-serif;
  --mono:  "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- credits strip (above topbar) ---------- */
.linkbar {
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
  padding: 7px 32px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  flex-wrap: wrap;
  gap: 6px 18px;
}
.linkbar .group {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  align-items: center;
  margin: 0; /* override scoped `.group` rules on terminology.html */
}
.linkbar .lbl-tag {
  color: var(--ink-faint);
  letter-spacing: 0.12em;
}
.linkbar a {
  color: var(--ink-soft);
  border-bottom: 1px solid transparent;
  transition: color .15s, border-color .15s;
}
.linkbar a:hover {
  color: var(--blue);
  border-bottom-color: var(--blue);
  text-decoration: none;
}
.linkbar .arrow {
  font-size: 9px;
  margin-left: 2px;
  opacity: 0.7;
}
@media (max-width: 720px) {
  .linkbar {
    padding: 8px 16px;
    font-size: 10px;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
  .linkbar .group { gap: 12px; }
  .linkbar .arrow { display: none; }
}
@media (max-width: 540px) {
  .linkbar { font-size: 9.5px; }
  .linkbar .group { gap: 8px; row-gap: 4px; }
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
  padding: 14px 32px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}
.topbar .brand {
  font-family: var(--serif);
  font-style: italic;
  font-size: 15px;
  color: var(--ink);
  text-transform: none;
  letter-spacing: 0;
}
.topbar .crumbs a { color: var(--ink-soft); margin: 0 6px; }
.topbar .crumbs a.current { color: var(--blue); }

/* ---------- layout ---------- */
.shell {
  display: grid;
  grid-template-columns: 240px 1fr 60px;
  gap: 56px;
  max-width: 1340px;
  margin: 0 auto;
  padding: 60px 32px 120px;
}

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; gap: 24px; padding: 24px 14px 64px; }
  .toc { position: static !important; max-height: none !important; padding-bottom: 12px; border-bottom: 1px solid var(--rule); }
  .margin-tag { display: none; }
  .topbar {
    padding: 10px 14px;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
  .topbar .brand { font-size: 12.5px; line-height: 1.35; }
  .topbar .crumbs {
    font-size: 10px;
    line-height: 1.6;
    display: flex;
    flex-wrap: wrap;
    gap: 0 8px;
  }
  .topbar .crumbs a { margin: 0; }
  .cover { padding: 28px 0 24px; }
  .cover .header-block { grid-template-columns: 1fr; gap: 18px; margin-bottom: 24px; }
  .cover .title { font-size: 36px; line-height: 1.02; }
  .cover .sub { font-size: 14px; }
  .cover .meta { font-size: 10px; line-height: 1.7; }
  .content h1 { font-size: 32px; }
  .content .deck { font-size: 16px; max-width: 100%; }
  .content h2 { font-size: 22px; margin-top: 32px; line-height: 1.2; }
  .content h3 { font-size: 17px; }
  .content { max-width: 100%; font-size: 15px; line-height: 1.6; }
  .chapters { grid-template-columns: 1fr; gap: 12px; }
  .chapters .chapter-card { padding: 16px 18px; }
  .chapters .chapter-card .num { font-size: 10px; }
  .chapters .chapter-card .name { font-size: 18px; margin: 4px 0 6px; }
  .chapters .chapter-card .blurb { font-size: 13px; line-height: 1.45; }
  /* Override the inline grid-column:span-2 on the terminology card — single-column on mobile */
  .chapters .chapter-card[href="terminology.html"] { grid-column: auto !important; }

  /* ---------- DIAGRAM HANDLING ON MOBILE ----------
     The dense SVG diagrams were drawn for an 840-940 px viewBox.
     Squeezing them into a 360 px screen makes the labels illegible.
     Solution: keep the SVG at its intended size and let the user scroll horizontally.
     We also surface a small swipe hint above the figure. */
  figure { margin: 22px -4px; }
  figure svg {
    min-width: 720px;       /* hold native size — readable */
    border-radius: 0;
  }
  /* the figure becomes the horizontal-scroll viewport */
  figure {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    position: relative;
  }
  figure::after {
    content: "↔ swipe";
    position: absolute;
    top: 6px;
    right: 12px;
    font-family: var(--mono);
    font-size: 9px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--ink-faint);
    background: rgba(255,253,246,0.8);
    padding: 2px 6px;
    border-radius: 2px;
    pointer-events: none;
    z-index: 2;
  }
  figcaption {
    font-size: 10.5px;
    padding: 0 4px;
  }
  /* on mobile we can also pump up the SVG text so a slight zoom isn't required */
  .diag .lbl, .diag .lbl-soft { font-size: 10.5px; }
  .diag .lbl-big { font-size: 12px; }
  .diag .body { font-size: 11.5px; }

  .pagenav { flex-direction: column; gap: 18px; align-items: flex-start; padding: 0 4px; }

  /* Cover hero stays readable: it's tall, stays full width via the figure container */
  .cover .heroart { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; }
  .cover .heroart svg { min-width: 720px; border-radius: 0; }

  /* Callouts shrink slightly */
  .callout { padding: 14px 16px; font-size: 13.5px; }
  .compare { grid-template-columns: 1fr; gap: 12px; }

  /* Math equation blocks scroll horizontally if too wide */
  .eq { padding: 10px 12px; font-size: 13px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}
@media (max-width: 540px) {
  .shell { padding: 18px 12px 48px; }
  .cover .title { font-size: 28px; }
  .content h1 { font-size: 26px; }
  .content h1 .num { font-size: 14px; margin-bottom: 10px; }
  .content h2 { font-size: 19px; }
  .content { font-size: 14.5px; }
  .term { grid-template-columns: 1fr; }
  .term .name { font-size: 16px; }
  .term .body { font-size: 13.5px; }
  /* further-shrunk topbar/crumbs on very narrow phones */
  .topbar { padding: 8px 12px; }
  .topbar .crumbs { font-size: 9px; }
  .topbar .brand { font-size: 11.5px; }
  /* keep figures readable but accept the swipe */
  figure svg { min-width: 700px; }
  .cover .heroart svg { min-width: 700px; }
}

/* ---------- TOC sidebar ---------- */
.toc {
  position: sticky;
  top: 80px;
  align-self: start;
  font-family: var(--sans);
  font-size: 12.5px;
  line-height: 1.55;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}
.toc h3 {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 8px;
  font-weight: 600;
}
.toc ol {
  list-style: none;
  padding: 0;
  margin: 0 0 24px;
  counter-reset: chap;
}
.toc ol > li {
  counter-increment: chap;
  margin-bottom: 6px;
}
.toc ol > li > a {
  color: var(--ink);
  font-weight: 600;
}
.toc ol > li > a::before {
  content: counter(chap) ". ";
  color: var(--ink-faint);
  font-family: var(--mono);
  font-weight: 400;
  margin-right: 4px;
}
.toc ul {
  list-style: none;
  padding: 4px 0 8px 14px;
  margin: 0;
  border-left: 1px solid var(--rule);
}
.toc ul li { margin: 2px 0; }
.toc ul li a { color: var(--ink-soft); font-weight: 400; }
.toc a.active { color: var(--blue); }

/* ---------- main content ---------- */
.content {
  max-width: 760px;
  font-feature-settings: "kern", "liga";
}

.content h1 {
  font-family: var(--serif);
  font-size: 44px;
  line-height: 1.08;
  font-weight: 700;
  letter-spacing: -0.012em;
  margin: 0 0 6px;
}
.content h1 .num {
  font-family: var(--mono);
  font-weight: 400;
  font-size: 18px;
  color: var(--blue);
  display: block;
  margin-bottom: 14px;
  letter-spacing: 0.05em;
}
.content .deck {
  font-style: italic;
  color: var(--ink-soft);
  font-size: 19px;
  line-height: 1.5;
  margin: 8px 0 36px;
  max-width: 600px;
}
.content h2 {
  font-family: var(--serif);
  font-size: 28px;
  line-height: 1.18;
  margin: 56px 0 14px;
  font-weight: 700;
  letter-spacing: -0.005em;
}
.content h2 .num {
  font-family: var(--mono);
  font-weight: 400;
  font-size: 13px;
  color: var(--blue);
  margin-right: 12px;
  letter-spacing: 0.05em;
}
.content h3 {
  font-family: var(--serif);
  font-size: 19px;
  margin: 32px 0 6px;
  font-weight: 700;
}
.content p {
  margin: 0 0 14px;
}
.content p .lead {
  font-size: 19px;
  line-height: 1.5;
}
.content em { font-style: italic; }
.content strong { font-weight: 700; }

.content code, .content kbd, .math-inline {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--blue-tint);
  color: var(--blue);
  padding: 1px 6px;
  border-radius: 3px;
}

/* ---------- figures ---------- */
figure {
  margin: 40px 0 36px;
  padding: 0;
}
figure svg {
  display: block;
  width: 100%;
  height: auto;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 2px;
}
figcaption {
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  margin-top: 10px;
  line-height: 1.5;
}
figcaption .figno {
  color: var(--blue);
  margin-right: 8px;
}
figcaption .desc {
  font-family: var(--serif);
  font-size: 13px;
  color: var(--ink-soft);
  text-transform: none;
  letter-spacing: 0;
  display: block;
  margin-top: 4px;
  line-height: 1.55;
}

/* ---------- callouts ---------- */
.callout {
  margin: 28px 0;
  padding: 18px 22px;
  background: var(--blue-tint);
  border-left: 3px solid var(--blue);
  border-radius: 2px;
  font-size: 14.5px;
  line-height: 1.55;
}
.callout .tag {
  display: inline-block;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--blue);
  font-weight: 600;
  margin-bottom: 6px;
}
.callout p { margin: 0 0 8px; }
.callout p:last-child { margin-bottom: 0; }

.callout.warn  { background: #fff5e6; border-color: var(--orange); }
.callout.warn .tag { color: var(--orange); }

/* ---------- math display ---------- */
.eq {
  display: block;
  margin: 16px 0;
  padding: 12px 16px;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 2px;
  font-family: var(--mono);
  font-size: 14px;
  text-align: center;
  overflow-x: auto;
}
.eq .lbl {
  float: right;
  color: var(--ink-faint);
  font-size: 11px;
  margin-top: 2px;
}

/* ---------- right margin annotation ---------- */
.margin-tag {
  position: relative;
  border-left: 1px solid var(--rule);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-faint);
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  white-space: nowrap;
  padding: 0 6px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  min-height: 70vh;
}
.margin-tag .top { margin-top: 12px; }
.margin-tag .mid { color: var(--blue); font-weight: 600; }
.margin-tag .bot { margin-bottom: 12px; }

/* ---------- index page hero ---------- */
.cover {
  display: block;
  padding: 48px 0 40px;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 48px;
}
.cover .header-block {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 56px;
  align-items: end;
  margin-bottom: 40px;
}
.cover .title {
  font-family: var(--serif);
  font-size: 56px;
  line-height: 0.98;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--blue);
  margin: 0 0 20px;
  text-transform: uppercase;
  font-style: normal;
}
.cover .sub {
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink-soft);
  font-style: italic;
  margin-bottom: 18px;
}
.cover .meta {
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
  line-height: 1.8;
}
.cover .heroart {
  width: 100%;
  margin: 0 auto;
}
.cover .heroart svg {
  width: 100%;
  height: auto;
  display: block;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 2px;
}
@media (max-width: 900px) {
  .cover .header-block {
    grid-template-columns: 1fr;
    gap: 24px;
    margin-bottom: 28px;
  }
}

/* ---------- chapter cards on index ---------- */
.chapters {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
  margin-top: 24px;
}
@media (max-width: 700px) { .chapters { grid-template-columns: 1fr; } }
.chapter-card {
  background: var(--paper);
  border: 1px solid var(--rule);
  padding: 22px 24px;
  border-radius: 3px;
  text-decoration: none;
  color: var(--ink);
  display: block;
  transition: border-color .15s, transform .15s;
}
.chapter-card:hover { border-color: var(--blue); transform: translateY(-2px); text-decoration: none; }
.chapter-card .num {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--blue);
  text-transform: uppercase;
}
.chapter-card .name {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 700;
  margin: 6px 0 8px;
  letter-spacing: -0.01em;
}
.chapter-card .blurb {
  font-size: 14px;
  color: var(--ink-soft);
  line-height: 1.5;
}

/* ---------- nav strip at bottom of chapters ---------- */
.pagenav {
  margin-top: 64px;
  padding-top: 24px;
  border-top: 1px solid var(--rule);
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.pagenav a {
  color: var(--ink-soft);
}
.pagenav a .nm {
  display: block;
  margin-top: 4px;
  font-family: var(--serif);
  font-size: 14px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
}

/* ---------- SVG defaults ---------- */
.diag {
  --d-stroke: #2347d9;
  --d-stroke-soft: #93a8f0;
  --d-fill: #eef2ff;
  --d-bg: #fffdf6;
  --d-orange: #f6802b;
  --d-green: #2f8f5b;
  --d-text: #1f2937;
  --d-mute: #9ca3af;
}
.diag .stroke    { stroke: var(--d-stroke); fill: none; stroke-width: 1.2; }
.diag .stroke-2  { stroke: var(--d-stroke); fill: none; stroke-width: 1.8; }
.diag .stroke-soft { stroke: var(--d-stroke-soft); fill: none; stroke-width: 1; }
.diag .fill      { fill: var(--d-stroke); }
.diag .fill-soft { fill: var(--d-fill); }
.diag .fill-orange { fill: var(--d-orange); }
.diag .fill-green  { fill: var(--d-green); }
.diag .lead      { stroke: var(--d-stroke); fill: none; stroke-width: 0.8; stroke-dasharray: 0; }
.diag .dashed    { stroke: var(--d-stroke); fill: none; stroke-width: 0.8; stroke-dasharray: 3 3; }
.diag .grid      { stroke: #d8e0f5; fill: none; stroke-width: 0.5; }
.diag .lbl       { font-family: var(--mono); font-size: 9.5px; fill: var(--d-stroke); letter-spacing: 0.06em; text-transform: uppercase; }
.diag .lbl-soft  { font-family: var(--mono); font-size: 9px; fill: var(--d-mute); letter-spacing: 0.06em; text-transform: uppercase; }
.diag .lbl-big   { font-family: var(--mono); font-size: 11px; fill: var(--d-stroke); letter-spacing: 0.06em; text-transform: uppercase; font-weight: 600; }
.diag .body      { font-family: "Iowan Old Style", Palatino, Georgia, serif; font-size: 11px; fill: var(--d-text); }

/* ---------- bars, comparison ---------- */
.bar-row {
  display: flex;
  align-items: center;
  font-family: var(--mono);
  font-size: 12px;
  margin: 6px 0;
}
.bar-row .label { width: 130px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.06em; font-size: 10.5px; }
.bar-row .bar {
  height: 16px;
  background: var(--blue);
  border-radius: 2px;
  margin-right: 10px;
}
.bar-row .v { color: var(--ink); }

/* ---------- two-col compare table ---------- */
.compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin: 20px 0 30px;
}
.compare > div {
  border: 1px solid var(--rule);
  background: var(--paper);
  padding: 18px 20px;
  border-radius: 3px;
}
.compare h4 {
  margin: 0 0 10px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--blue);
}
.compare ul {
  margin: 0; padding: 0; list-style: none;
}
.compare li {
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--ink-soft);
  padding: 4px 0 4px 18px;
  position: relative;
}
.compare li::before {
  content: "→";
  position: absolute;
  left: 0;
  color: var(--blue);
}
.compare li.bad::before { content: "✗"; color: var(--red); }
.compare li.good::before { content: "✓"; color: var(--green); }

/* Late diagram/mobile overrides: keep SVG figures usable after base rules cascade. */
@media (max-width: 900px) {
  .cover { grid-template-columns: 1fr; gap: 28px; padding: 32px 0 24px; }
  .cover .title { font-size: 36px; }
  .cover .sub { font-size: 14px; }
  .diag .lbl, .diag .lbl-soft { font-size: 11px; }
  .diag .lbl-big { font-size: 12.5px; }
  .diag .body { font-size: 12px; }
}
@media (max-width: 540px) {
  .cover .title { font-size: 28px; }
}
