:root {
  --bg: #efe9dd; --bg2: #e7dfd1; --surface: #faf6ee;
  --ink: #241f18; --muted: #6c6252; --faint: #8f8674;
  --line: rgba(40,30,16,0.14); --line2: rgba(40,30,16,0.08);
  --accent: #c9791b; --accent-ink: #a8620f; --accent-soft: rgba(201,121,27,0.13);
  --glow: rgba(201,121,27,0.20);
  --code-bg: #211a12; --code-ink: #ece3d2; --code-c: #9a8f78; --code-kw: #e8a13c;
  --code-str: #8fb98f; --code-num: #d99a8a; --code-type: #8fb3d9; --code-fn: #e6c38a;
  --eq-bg: #f6efe2; --screen: #0d0b08; --figure-bg: #ffffff;
  --shadow: 0 1px 2px rgba(40,30,16,.06), 0 8px 30px rgba(40,30,16,.08);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #14110d; --bg2: #1a160f; --surface: #211b14;
  --ink: #f2ebdf; --muted: #a99f8e; --faint: #857c6c;
  --line: rgba(242,235,223,0.13); --line2: rgba(242,235,223,0.06);
  --accent: #e8a13c; --accent-ink: #f0b968; --accent-soft: rgba(232,161,60,0.14);
  --glow: rgba(232,161,60,0.28);
  --code-bg: #0d0b08; --code-ink: #ece3d2; --code-c: #7c7362; --code-kw: #e8a13c;
  --code-str: #7cae7c; --code-num: #e0a493; --code-type: #8fb3d9; --code-fn: #e6c38a;
  --eq-bg: #1b1710; --screen: #0d0b08; --figure-bg: #f4efe6;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 12px 40px rgba(0,0,0,.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: 72px; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Hanken Grotesk", system-ui, sans-serif;
  font-size: 18px; line-height: 1.65;
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}
::selection { background: var(--accent-soft); }
.wrap { max-width: 730px; margin: 0 auto; padding: 0 24px; }
.wide { max-width: 1080px; margin: 0 auto; padding: 0 24px; }
p { margin: 0 0 1.1em; }
a { color: var(--accent-ink); text-underline-offset: 3px; }
strong { font-weight: 700; color: var(--ink); }
h1, h2, h3, h4 { font-family: "Fraunces", Georgia, serif; text-wrap: balance; line-height: 1.1; margin: 0; }

/* ── top bar ─────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 50; display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 20px; background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line2);
}
.topbar .left, .topbar .right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand {
  font-family: "IBM Plex Mono", monospace; font-size: 12px; letter-spacing: .14em; text-transform: uppercase;
  color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-decoration: none;
}
.brand b { color: var(--ink); font-weight: 600; }
.pill {
  font: inherit; font-size: 13px; display: inline-flex; align-items: center; gap: 7px; padding: 6px 13px;
  cursor: pointer; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
}
.menu-toggle { padding: 7px 10px; }
.menu-toggle svg { display: block; }
.pill:hover { border-color: var(--accent); }
.pill:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.home-link { display: inline-flex; color: var(--muted); }
.home-link:hover { color: var(--accent-ink); }
.home-link svg { width: 20px; height: 20px; }

.search { position: relative; }
.search-input {
  font: inherit; font-size: 13px; width: 230px; padding: 6px 13px; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; outline: none;
}
.search-input:focus { border-color: var(--accent); }
.search-results {
  position: absolute; right: 0; top: calc(100% + 8px); width: min(420px, 90vw); max-height: 70vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 6px;
}
.search-hit { display: block; padding: 8px 10px; border-radius: 8px; text-decoration: none; color: var(--ink); }
.search-hit:hover { background: var(--accent-soft); }
.search-hit-title { display: block; font-weight: 600; font-size: 14px; }
.search-hit-sub { display: block; font-size: 12px; color: var(--muted); }
.search-empty { padding: 10px; font-size: 13px; color: var(--muted); }

/* ── drawer ──────────────────────────────────────────────────────────── */
.scrim {
  position: fixed; inset: 0; background: rgba(10,8,5,.45); z-index: 90; opacity: 0; pointer-events: none;
  transition: opacity .2s;
}
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: min(380px, 88vw); z-index: 100; background: var(--surface);
  border-right: 1px solid var(--line); box-shadow: var(--shadow); transform: translateX(-102%);
  transition: transform .24s ease; display: flex; flex-direction: column;
}
body.nav-open .drawer { transform: none; }
body.nav-open .scrim { opacity: 1; pointer-events: auto; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 12px;
  border-bottom: 1px solid var(--line2);
}
.drawer-head a { font-family: "Fraunces", serif; font-size: 1.1rem; font-weight: 600; color: var(--ink); text-decoration: none; }
.drawer-close { font: inherit; font-size: 20px; line-height: 1; background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px; }
.drawer nav { overflow-y: auto; padding: 8px 0 30px; }
.drawer .part {
  font-family: "IBM Plex Mono", monospace; font-size: 10.5px; letter-spacing: .16em; text-transform: uppercase;
  color: var(--accent-ink); padding: 16px 20px 6px;
}
.drawer a.ch {
  display: grid; grid-template-columns: 28px 1fr; gap: 6px; padding: 6px 20px; font-size: 15px; line-height: 1.35;
  color: var(--ink); text-decoration: none; border-left: 3px solid transparent;
}
.drawer a.ch:hover { background: var(--accent-soft); }
.drawer a.ch.current { border-left-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.drawer .ch .num { font-family: "IBM Plex Mono", monospace; font-size: 12px; color: var(--faint); padding-top: 2px; }
.drawer .sub { list-style: none; margin: 2px 0 8px; padding: 0 0 0 54px; }
.drawer .sub a { display: block; padding: 3px 16px 3px 0; font-size: 13.5px; line-height: 1.35; color: var(--muted); text-decoration: none; }
.drawer .sub a:hover { color: var(--accent-ink); }

/* ── hero ────────────────────────────────────────────────────────────── */
.hero { padding: 56px 0 30px; position: relative; overflow: hidden; }
.hero::before {
  content: ""; position: absolute; inset: -30% 0 auto 0; height: 560px;
  background: radial-gradient(60% 100% at 72% 0%, var(--glow), transparent 70%); pointer-events: none; z-index: -1;
}
.eyebrow {
  font-family: "IBM Plex Mono", monospace; font-size: 12.5px; letter-spacing: .2em; text-transform: uppercase;
  color: var(--accent-ink); margin: 0 0 18px;
}
h1 { font-size: clamp(2.4rem, 6vw, 4.4rem); font-weight: 600; font-optical-sizing: auto; letter-spacing: -0.02em; margin-bottom: 20px; }
h1 em { font-style: italic; color: var(--accent-ink); font-weight: 500; }
.byline { font-size: clamp(1rem, 2vw, 1.15rem); font-style: italic; color: var(--muted); margin: -8px 0 20px; }
.standfirst { font-size: clamp(1.1rem, 2.3vw, 1.36rem); color: var(--muted); max-width: 46ch; }
.standfirst strong { color: var(--ink); font-weight: 600; }
.hero .book-demo { margin-top: 34px; }

/* ── prose ───────────────────────────────────────────────────────────── */
main { padding-bottom: 20px; }
.section-head { margin: 64px 0 18px; }
.kicker {
  font-family: "IBM Plex Mono", monospace; font-size: 12px; letter-spacing: .18em; text-transform: uppercase;
  color: var(--accent-ink); margin: 0 0 12px; display: block;
}
h2 { font-size: clamp(1.75rem, 3.8vw, 2.5rem); font-weight: 500; letter-spacing: -0.015em; }
h3 { font-size: 1.4rem; font-weight: 600; margin: 40px 0 10px; letter-spacing: -0.01em; }
h4 { font-size: 1.06rem; margin: 28px 0 6px; font-weight: 600; letter-spacing: -0.01em; }
.prose > h2 { margin: 64px 0 18px; }
.hanchor { margin-left: .3em; color: var(--faint); font-weight: 400; opacity: 0; text-decoration: none; font-size: .7em; }
h2:hover .hanchor, h3:hover .hanchor, h4:hover .hanchor { opacity: 1; }
.lede { font-size: 1.2rem; color: var(--muted); margin-top: 12px; }
.prose ol, .prose ul { margin: 0 0 1.1em; padding-left: 1.35em; }
.prose li { margin: 0 0 .5em; }
.prose li > p { margin: 0 0 .5em; }
hr { border: 0; border-top: 1px solid var(--line); margin: 50px 0; }

code {
  font-family: "IBM Plex Mono", monospace; font-size: 0.85em; background: var(--accent-soft); color: var(--accent-ink);
  padding: 1px 6px; border-radius: 5px; overflow-wrap: break-word;
}
a code { color: inherit; }

/* code panels */
.code { background: var(--code-bg); border-radius: 10px; margin: 22px 0; overflow: hidden; box-shadow: var(--shadow); }
/* listings break out of the 730px prose column so real source lines fit */
.wrap > .code { width: min(920px, calc(100vw - 32px)); margin-left: calc(50% - min(460px, 50vw - 16px)); }
.code .filelabel {
  font-family: "IBM Plex Mono", monospace; font-size: 11px; letter-spacing: .06em; color: var(--code-c);
  padding: 9px 16px; border-bottom: 1px solid rgba(255,255,255,.07); display: flex; justify-content: space-between; gap: 12px;
}
.code .filelabel a { color: var(--code-c); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code .filelabel a:hover { color: var(--code-ink); }
.code .filelabel .lang { color: var(--code-kw); white-space: nowrap; }
.code pre { margin: 0; padding: 16px; overflow-x: auto; max-height: 620px; font-size: 13px; line-height: 1.6; }
.code code { background: none; color: var(--code-ink); padding: 0; font-size: 13px; line-height: 1.6; overflow-wrap: normal; }
.hljs-comment, .hljs-quote { color: var(--code-c); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-doctag { color: var(--code-kw); }
.hljs-string, .hljs-regexp, .hljs-template-variable, .hljs-addition { color: var(--code-str); }
.hljs-number, .hljs-literal { color: var(--code-num); }
.hljs-type, .hljs-built_in, .hljs-class .hljs-title, .hljs-title.class_ { color: var(--code-type); }
.hljs-title, .hljs-title.function_, .hljs-section { color: var(--code-fn); }
.hljs-meta, .hljs-attr, .hljs-attribute, .hljs-variable, .hljs-params { color: var(--code-ink); }
.hljs-meta { color: var(--code-str); }
.hljs-deletion { color: #f08a7a; }

/* figures (the book's SVG illustrations) */
figure { margin: 0; }
.figure { margin: 28px 0; }
.figure .frame {
  background: var(--figure-bg); border: 1px solid var(--line); border-radius: 12px; padding: 12px;
  box-shadow: var(--shadow);
}
.figure img { display: block; width: 100%; height: auto; }
figcaption { font-size: 14px; color: var(--faint); margin-top: 12px; font-style: italic; }
figcaption strong { font-style: normal; font-weight: 600; color: var(--muted); }

/* tables */
.table-wrap { overflow-x: auto; margin: 22px 0; }
.table-wrap table { border-collapse: collapse; width: 100%; font-size: .9em; line-height: 1.5; }
.table-wrap th {
  font-family: "IBM Plex Mono", monospace; font-size: 11px; letter-spacing: .1em; text-transform: uppercase;
  color: var(--accent-ink); text-align: left; font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--line);
}
.table-wrap td { padding: 8px 10px; border-bottom: 1px solid var(--line2); vertical-align: top; }

/* blockquotes read as the graphics book's side notes */
blockquote { border-left: 2px solid var(--accent); padding: 2px 0 2px 16px; color: var(--muted); margin: 20px 0; font-size: .98em; }
blockquote p:last-child { margin-bottom: 0; }

/* ── interactive demos (```demo fences) ─────────────────────────────── */
.book-demo {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin: 28px 0;
  box-shadow: var(--shadow);
}
.book-demo .dhead {
  font-family: "IBM Plex Mono", monospace; font-size: 11px; letter-spacing: .14em; text-transform: uppercase;
  color: var(--accent-ink); margin-bottom: 12px; display: flex; align-items: center; gap: 8px;
}
.book-demo .dhead::before { content: "▶"; font-size: 9px; }
.book-demo .dhead a {
  margin-left: auto; font-size: 10.5px; letter-spacing: .12em; text-decoration: none; color: var(--accent-ink);
  border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px;
}
.book-demo .dhead a:hover { border-color: var(--accent); }
.book-demo-frame { position: relative; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: var(--screen); }
.book-demo iframe { display: block; width: 100%; border: 0; aspect-ratio: 16 / 10.5; background: var(--screen); }
.book-demo-placeholder {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #8f8674; font-size: 14px; pointer-events: none;
}
.book-demo.loaded .book-demo-placeholder { display: none; }

/* ── prev / next + footer ────────────────────────────────────────────── */
.chapter-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 64px 0 0; }
.chapter-nav > a {
  display: block; padding: 14px 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
  text-decoration: none; color: var(--ink);
}
.chapter-nav > a:hover { border-color: var(--accent); }
.chapter-nav .dir {
  font-family: "IBM Plex Mono", monospace; font-size: 11px; letter-spacing: .14em; text-transform: uppercase;
  color: var(--accent-ink); display: block; margin-bottom: 4px;
}
.chapter-nav .t { font-family: "Fraunces", serif; font-size: 1.05rem; font-weight: 600; }
.chapter-nav .next { text-align: right; grid-column: 2; }
footer { border-top: 1px solid var(--line); margin-top: 64px; padding: 38px 0 68px; color: var(--faint); font-size: 14px; }
footer p { margin: 0 0 6px; }

@media (max-width: 860px) {
  .search-input { width: 150px; }
}
@media (max-width: 720px) {
  body { font-size: 17px; }
  .topbar { padding: 9px 14px; }
  .brand { display: none; }
  .chapter-nav { grid-template-columns: 1fr; }
  .chapter-nav .next { grid-column: 1; }
  .book-demo { padding: 10px; }
}
