/* ============================================================================
   Agentic Engineering — online reader
   Visual language extends the book's figure design system (tokens.json):
   warm paper, charcoal ink, ONE desaturated-slate accent used sparingly.
   Signature: Tufte-style margin sidenotes (the book's footnotes are its
   citation system) and figures presented as paper "islands".
   ========================================================================== */

:root {
  /* --- palette: light (warm paper) --- */
  --paper:        #F7F4EF;
  --surface:      #FCFAF6;
  --surface-2:    #F1ECE3;
  --ink:          #2B2B2B;
  --ink-2:        #6B6B6B;
  --ink-3:        #6E695F;   /* AA on paper (4.97:1) */
  --accent:       #4E7191;
  --accent-soft:  #C9D6E0;
  --accent-wash:  #E7EDF2;
  --rule:         #E1DACE;
  --rule-strong:  #CFC6B6;
  --code-bg:      #EFE9DF;
  --fig-paper:    #F7F4EF;
  --fig-ink:      #2B2B2B;
  --shadow:       rgba(43, 43, 43, 0.10);
  --shadow-soft:  rgba(43, 43, 43, 0.06);

  /* --- type --- */
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --sans:  "Source Sans 3", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --mono:  "Source Code Pro", ui-monospace, SFMono-Regular, Menlo, monospace;

  --measure: 39rem;          /* reading column ~ 66–68ch */
  --gutter: 15.5rem;         /* right margin reserved for sidenotes */
  --rail: 17.5rem;           /* left TOC rail on wide screens */
  --topbar-h: 3.5rem;

  --step--1: 0.86rem;
  --step-0:  1.075rem;
  --step-1:  1.32rem;
  --step-2:  1.62rem;
  --step-3:  2.1rem;
  --step-4:  2.9rem;
  --step-5:  clamp(2.8rem, 6vw, 4.6rem);

  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

:root[data-theme="dark"] {
  /* --- palette: "night paper" — warm charcoal, never pure black --- */
  --paper:        #1B1A17;
  --surface:      #23211D;
  --surface-2:    #2A2823;
  --ink:          #E9E3D7;
  --ink-2:        #A8A092;
  --ink-3:        #857D6F;
  --accent:       #8FB4D4;
  --accent-soft:  #34506B;
  --accent-wash:  #23303C;
  --rule:         #35322B;
  --rule-strong:  #46423A;
  --code-bg:      #26241F;
  /* figures stay on warm paper "islands" even at night */
  --fig-paper:    #F4F1EA;
  --fig-ink:      #2B2B2B;
  --shadow:       rgba(0, 0, 0, 0.44);
  --shadow-soft:  rgba(0, 0, 0, 0.28);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --paper:#1B1A17; --surface:#23211D; --surface-2:#2A2823;
    --ink:#E9E3D7; --ink-2:#A8A092; --ink-3:#857D6F;
    --accent:#8FB4D4; --accent-soft:#34506B; --accent-wash:#23303C;
    --rule:#35322B; --rule-strong:#46423A; --code-bg:#26241F;
    --fig-paper:#F4F1EA; --fig-ink:#2B2B2B;
    --shadow:rgba(0,0,0,0.44); --shadow-soft:rgba(0,0,0,0.28);
  }
}

/* ------------------------------------------------------------------ base */
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar-h) + 1rem); }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: var(--step-0);
  line-height: 1.72;
  font-feature-settings: "kern", "liga", "onum", "pnum";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color 0.4s var(--ease), color 0.4s var(--ease);
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
::selection { background: var(--accent-soft); color: var(--ink); }

/* ------------------------------------------------------- reading progress */
.progress {
  position: fixed; top: 0; left: 0; right: 0; height: 2px; z-index: 60;
  background: transparent; pointer-events: none;
}
.progress > span {
  display: block; height: 100%; width: var(--p, 0%);
  background: var(--accent);
  transition: width 0.1s linear;
}

/* ---------------------------------------------------------------- topbar */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h); z-index: 50;
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0 clamp(0.75rem, 2vw, 1.25rem);
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  backdrop-filter: saturate(1.3) blur(10px);
  -webkit-backdrop-filter: saturate(1.3) blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; text-decoration: none; color: var(--ink); margin-right: auto; }
.brand-title { font-family: var(--sans); font-weight: 600; font-size: 0.98rem; letter-spacing: 0.01em; }
.topbar-actions { display: flex; gap: 0.15rem; }

.icon-btn {
  display: grid; place-items: center; width: 2.35rem; height: 2.35rem;
  border: none; border-radius: 8px; background: transparent; color: var(--ink-2);
  cursor: pointer; transition: background-color 0.18s var(--ease), color 0.18s var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] .i-sun { display: none; }
  :root[data-theme="auto"] .i-moon { display: block; }
}

/* --------------------------------------------------------------- sidebar */
.sidebar {
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0; width: var(--rail); z-index: 45;
  padding: 1.5rem 0.5rem 3rem 1.25rem; overflow-y: auto;
  background: var(--paper); border-right: 1px solid var(--rule);
  transform: translateX(-102%);
  transition: transform 0.32s var(--ease);
  overscroll-behavior: contain;
}
.sidebar.open { transform: translateX(0); box-shadow: 12px 0 40px var(--shadow); }
.sidebar-home { display: block; text-decoration: none; color: var(--ink); margin-bottom: 1.25rem; padding-right: 0.75rem; }
.sidebar-book { display: block; font-family: var(--sans); font-weight: 600; font-size: 0.98rem; }
.sidebar-sub { display: block; font-family: var(--sans); font-size: 0.76rem; color: var(--ink-2); margin-top: 0.15rem; line-height: 1.35; }
.sidebar-nav { display: flex; flex-direction: column; }

.sb-link {
  display: grid; grid-template-columns: auto 1fr; gap: 0.1rem 0.6rem; align-items: baseline;
  padding: 0.38rem 0.75rem 0.38rem 0.7rem; text-decoration: none; color: var(--ink-2);
  border-radius: 7px; border-left: 2px solid transparent; margin-left: -2px;
  transition: background-color 0.16s var(--ease), color 0.16s var(--ease);
}
.sb-link:hover { background: var(--surface-2); color: var(--ink); }
.sb-link.is-current { color: var(--ink); background: var(--accent-wash); border-left-color: var(--accent); }
.sb-label { grid-column: 1; font-family: var(--sans); font-size: 0.68rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-3); white-space: nowrap; }
.sb-link.is-current .sb-label { color: #3F6283; } /* accent, darkened for AA on the wash */
.sb-title { grid-column: 2; font-family: var(--sans); font-size: 0.9rem; line-height: 1.3; }
.sb-link.is-locked .sb-title { color: var(--ink-3); }
.lock { font-size: 0.72em; opacity: 0.75; }

.scrim {
  position: fixed; inset: 0; z-index: 44; background: rgba(20, 18, 15, 0.34);
  backdrop-filter: blur(1px); opacity: 0; transition: opacity 0.3s var(--ease);
}
.scrim.show { opacity: 1; }

/* --------------------------------------------------------------- content */
.content {
  padding-top: var(--topbar-h);
  min-height: 100vh;
}

/* ============================================================ COVER PAGE */
.cover { max-width: 62rem; margin: 0 auto; padding: 0 clamp(1.25rem, 4vw, 2.5rem) 4rem; }
.cover-hero {
  padding: clamp(3rem, 9vw, 7rem) 0 clamp(2.5rem, 6vw, 4rem);
  border-bottom: 1px solid var(--rule);
}
.cover-kicker {
  font-family: var(--sans); text-transform: uppercase; letter-spacing: 0.22em;
  font-size: 0.74rem; font-weight: 600; color: var(--accent); margin: 0 0 1.25rem;
}
.cover-title {
  font-family: var(--serif); font-weight: 700; font-size: var(--step-5);
  line-height: 1.03; letter-spacing: -0.015em; margin: 0; max-width: 16ch;
}
.cover-subtitle {
  font-family: var(--serif); font-style: italic; color: var(--ink-2);
  font-size: clamp(1.1rem, 2.4vw, 1.5rem); line-height: 1.4; margin: 1.4rem 0 0; max-width: 44ch;
}
.cover-actions { display: flex; flex-wrap: wrap; gap: 0.8rem; margin-top: 2.4rem; }

.btn {
  font-family: var(--sans); font-weight: 600; font-size: 0.95rem;
  padding: 0.7rem 1.4rem; border-radius: 9px; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 0.4rem;
  border: 1px solid transparent; transition: transform 0.15s var(--ease), background-color 0.18s var(--ease), border-color 0.18s var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; }
:root[data-theme="dark"] .btn-primary { color: #16202a; }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--rule-strong); }
.btn-ghost:hover { background: var(--surface-2); }

/* table of contents */
.toc { margin-top: 3rem; display: grid; gap: 2.75rem; }
.toc-part-title {
  font-family: var(--sans); font-size: 0.78rem; font-weight: 600; letter-spacing: 0.14em;
  text-transform: uppercase; color: var(--accent); margin: 0 0 1rem;
  padding-bottom: 0.5rem; border-bottom: 1px solid var(--rule);
}
.toc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.15rem; }
.toc-item a {
  display: grid; grid-template-columns: 2.6rem 1fr; align-items: baseline; gap: 0.5rem;
  text-decoration: none; color: var(--ink); padding: 0.6rem 0.75rem; border-radius: 9px;
  transition: background-color 0.16s var(--ease);
}
.toc-item a:hover { background: var(--surface-2); }
.toc-num {
  font-family: var(--serif); font-weight: 600; font-size: 1.15rem; color: var(--accent);
  font-variant-numeric: lining-nums; text-align: right;
}
.toc-body { display: flex; flex-direction: column; gap: 0.1rem; }
.toc-title { font-family: var(--serif); font-size: 1.12rem; line-height: 1.3; }
.toc-meta { font-family: var(--sans); font-size: 0.76rem; color: var(--ink-2); }
.toc-item.is-locked .toc-title { color: var(--ink-3); }

.cover-foot { margin-top: 3.5rem; padding-top: 1.5rem; border-top: 1px solid var(--rule);
  font-family: var(--sans); font-size: 0.8rem; color: var(--ink-3); }

/* ========================================================== CHAPTER PAGE */
.chapter {
  margin-left: auto; margin-right: auto;
  padding: clamp(1.5rem, 4vw, 3rem) clamp(1.15rem, 4vw, 2rem) 5rem;
  /* reading column + right gutter for sidenotes, centered */
  max-width: calc(var(--measure) + var(--gutter) + 2rem);
}
.tex-anchor { display: block; height: 0; visibility: hidden; }

.chapter-head { max-width: var(--measure); margin-bottom: 2.5rem; }
.chapter-label {
  font-family: var(--sans); text-transform: uppercase; letter-spacing: 0.18em;
  font-size: 0.76rem; font-weight: 600; color: var(--accent); margin: 0 0 0.8rem;
}
.chapter-title {
  font-family: var(--serif); font-weight: 700; font-size: var(--step-4); line-height: 1.08;
  letter-spacing: -0.014em; margin: 0; text-wrap: balance;
}
.chapter-meta { font-family: var(--sans); font-size: 0.82rem; color: var(--ink-2); margin: 1.1rem 0 0; }
.chapter-head::after {
  content: ""; display: block; width: 3.5rem; height: 2px; margin-top: 1.5rem;
  background: var(--accent);
}

/* on-this-page */
.chapter-onpage {
  max-width: var(--measure); margin: 0 0 2.75rem; padding: 0.9rem 1.15rem;
  background: var(--surface); border: 1px solid var(--rule); border-radius: 11px;
  font-family: var(--sans);
}
.chapter-onpage > summary {
  cursor: pointer; font-size: 0.74rem; font-weight: 600; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--ink-2); list-style: none;
}
.chapter-onpage > summary::-webkit-details-marker { display: none; }
.chapter-onpage > summary::before { content: "§ "; color: var(--accent); }
.chapter-onpage ol { list-style: none; margin: 0.85rem 0 0.2rem; padding: 0; display: grid; gap: 0.35rem; }
.chapter-onpage a { text-decoration: none; color: var(--ink-2); font-size: 0.92rem; display: inline-flex; gap: 0.5rem; }
.chapter-onpage a:hover { color: var(--accent); }
.onpage-num { color: var(--accent); font-variant-numeric: lining-nums; min-width: 2ch; }

/* ------------------------------------------------------------ prose body */
.prose { max-width: var(--measure); }
.prose > * { max-width: var(--measure); }
.prose p { margin: 0 0 1.35rem; hyphens: auto; }
.prose p:first-of-type::first-letter {
  /* drop cap on the first paragraph, quietly */
  initial-letter: 2.6;
  -webkit-initial-letter: 2.6;
  font-weight: 700; color: var(--accent); margin-right: 0.6rem;
}
@supports not (initial-letter: 2) { .prose p:first-of-type::first-letter { font-size: 1em; color: inherit; margin: 0; } }

.prose h2 {
  font-family: var(--serif); font-weight: 600; font-size: var(--step-2); line-height: 1.2;
  letter-spacing: -0.01em; margin: 3rem 0 1rem; scroll-margin-top: calc(var(--topbar-h) + 1rem);
}
.prose h2::before {
  content: ""; display: block; width: 1.6rem; height: 2px; background: var(--rule-strong);
  margin-bottom: 0.9rem;
}
.prose h3 { font-family: var(--serif); font-weight: 600; font-size: var(--step-1); margin: 2.2rem 0 0.8rem; }

.prose a { color: var(--accent); text-decoration: underline; text-decoration-color: var(--accent-soft); }
.prose a:hover { text-decoration-color: var(--accent); }

.prose em { font-style: italic; }
.prose strong { font-weight: 600; }
.smallcaps { font-variant: small-caps; letter-spacing: 0.03em; }

/* cross-references */
.xref {
  font-family: var(--sans); font-size: 0.92em; font-weight: 600; text-decoration: none;
  color: var(--accent); padding: 0 0.12em; border-radius: 3px;
  background: var(--accent-wash); white-space: nowrap;
}
.xref:hover { background: var(--accent-soft); }
.xref-missing { color: var(--ink-3); background: transparent; }
/* reference to a chapter withheld from this build: number kept, no link */
.xref-plain { color: var(--ink); background: transparent; font-weight: 600; }

/* lists */
.prose ul, .prose ol { margin: 0 0 1.35rem; padding-left: 1.5rem; }
.prose li { margin: 0.35rem 0; }
.prose ul { list-style: none; }
.prose ul > li { position: relative; }
.prose ul > li::before {
  content: ""; position: absolute; left: -1.15rem; top: 0.72em; width: 0.36rem; height: 0.36rem;
  background: var(--accent); border-radius: 50%;
}
.prose dl { margin: 0 0 1.35rem; }
.prose dt { font-weight: 600; font-family: var(--sans); font-size: 0.98rem; margin-top: 1rem; }
.prose dd { margin: 0.2rem 0 0; }

/* code */
.prose code {
  font-family: var(--mono); font-size: 0.88em; background: var(--code-bg);
  padding: 0.1em 0.36em; border-radius: 5px; color: var(--ink);
}
.prose pre {
  font-family: var(--mono); background: var(--surface); border: 1px solid var(--rule);
  border-left: 3px solid var(--accent); border-radius: 10px;
  padding: 1.15rem 1.3rem; overflow-x: auto; margin: 0 0 1.6rem;
  font-size: 0.8rem; line-height: 1.6; box-shadow: 0 1px 3px var(--shadow-soft);
  /* code benefits from a little more width than the text measure */
  max-width: calc(var(--measure) + 5rem);
}
.prose pre code { white-space: pre; tab-size: 4; }
.prose pre code { background: none; padding: 0; font-size: 1em; }

/* quotes */
.prose blockquote {
  margin: 0 0 1.5rem; padding: 0.3rem 0 0.3rem 1.4rem;
  border-left: 3px solid var(--accent-soft); color: var(--ink-2); font-style: italic;
}

/* --------------------------------------------------- SIGNATURE: sidenotes */
.fn { }
.fn-marker {
  cursor: pointer; color: var(--accent); font-family: var(--sans);
  font-weight: 600; line-height: 0; padding: 0 0.05em;
}
.fn-marker sup { font-size: 0.72em; }
.fn-marker:hover { color: color-mix(in srgb, var(--accent) 75%, var(--ink)); }
.fn-marker:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

.sidenote {
  float: right; clear: right;
  width: var(--gutter);
  margin-right: calc(-1 * var(--gutter) - 1.5rem);
  margin-bottom: 1.1rem; margin-top: 0.15rem;
  font-family: var(--sans); font-size: 0.8rem; line-height: 1.5; color: var(--ink-2);
  text-align: left; hyphens: auto;
}
.sidenote-num { color: var(--accent); font-weight: 700; margin-right: 0.25em; font-size: 0.82em; }
.sidenote a { color: var(--accent); word-break: break-word; }

/* narrow screens: hide in margin, toggle inline */
@media (max-width: 1100px) {
  .sidenote {
    display: none; float: none; width: auto; margin: 0.6rem 0 1rem;
    padding: 0.85rem 1rem; background: var(--surface); border: 1px solid var(--rule);
    border-left: 3px solid var(--accent); border-radius: 9px;
  }
  .fn-toggle:checked ~ .sidenote { display: block; animation: sidenote-in 0.28s var(--ease); }
  .fn-marker { text-decoration: underline; text-decoration-color: var(--accent-soft); text-underline-offset: 3px; }
}
@keyframes sidenote-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* ---------------------------------------------------- FIGURES as islands */
.book-figure { margin: 2.5rem 0; max-width: 100%; }
.fig-media {
  background: var(--fig-paper);
  border: 1px solid var(--rule); border-radius: 14px;
  padding: clamp(1rem, 3vw, 2rem);
  box-shadow: 0 2px 10px var(--shadow-soft), 0 12px 30px -18px var(--shadow);
  display: flex; justify-content: center; align-items: center;
}
.fig-diagram .tikz-svg, .fig-diagram img { width: 100%; height: auto; max-width: 34rem; }
.fig-illustration img { width: 100%; height: auto; border-radius: 6px; display: block; }
.fig-illustration { padding: 0; overflow: hidden; }
.book-figure img { cursor: zoom-in; }

.fig-placeholder {
  flex-direction: column; gap: 0.6rem; min-height: 8rem; color: var(--ink-2);
  border-style: dashed; text-align: center;
}
.fig-placeholder-mark { font-family: var(--sans); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--accent); }
.fig-placeholder-alt { font-family: var(--serif); font-style: italic; font-size: 0.92rem; max-width: 32ch; }

figcaption {
  font-family: var(--sans); font-size: 0.82rem; line-height: 1.5; color: var(--ink-2);
  margin-top: 0.85rem; max-width: 42rem; text-align: center; margin-inline: auto;
}
.fig-num { font-weight: 600; color: var(--accent); }
figcaption .xref { font-size: 0.9em; }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: color-mix(in srgb, var(--paper) 80%, #000 60%);
  backdrop-filter: blur(6px); padding: 3vw; opacity: 0; transition: opacity 0.25s var(--ease);
}
.lightbox.show { opacity: 1; }
.lightbox img, .lightbox svg { max-width: 94vw; max-height: 90vh; width: auto; height: auto;
  background: var(--fig-paper); border-radius: 12px; padding: 1.5rem; box-shadow: 0 30px 80px rgba(0,0,0,0.5); }
.lightbox-close { position: fixed; top: 1.2rem; right: 1.4rem; font-size: 1.6rem; color: #fff;
  background: rgba(0,0,0,0.3); border: none; border-radius: 50%; width: 2.6rem; height: 2.6rem; cursor: pointer; }

/* ---------------------------------------------------------- chapter nav */
.chapter-nav {
  max-width: var(--measure); margin: 4rem 0 0; padding-top: 2rem; border-top: 1px solid var(--rule);
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;
}
.chnav {
  display: flex; flex-direction: column; gap: 0.3rem; text-decoration: none; color: var(--ink);
  padding: 1rem 1.15rem; border: 1px solid var(--rule); border-radius: 12px;
  background: var(--surface); transition: border-color 0.18s var(--ease), transform 0.18s var(--ease), box-shadow 0.18s var(--ease);
}
.chnav:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 24px -14px var(--shadow); }
.chnav.next { text-align: right; }
.chnav-dir { font-family: var(--sans); font-size: 0.74rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent); }
.chnav-title { font-family: var(--serif); font-size: 0.98rem; line-height: 1.3; color: var(--ink-2); }

/* --------------------------------------------------------- hidden chapter */
.locked-card {
  max-width: var(--measure); margin: 2rem 0; padding: 2.5rem; text-align: center;
  background: var(--surface); border: 1px solid var(--rule); border-radius: 16px;
}
.locked-mark { font-size: 2.4rem; opacity: 0.7; }
.locked-card h2 { font-family: var(--serif); font-size: var(--step-2); margin: 0.6rem 0; }
.locked-card p { color: var(--ink-2); max-width: 40ch; margin: 0 auto 1.6rem; }
.locked-links { display: flex; gap: 0.8rem; justify-content: center; flex-wrap: wrap; }

/* --------------------------------------------------------- preview banner */
.preview-banner {
  position: fixed; top: var(--topbar-h); left: 0; right: 0; z-index: 40;
  font-family: var(--sans); font-size: 0.8rem; text-align: center; padding: 0.4rem 1rem;
  background: var(--accent); color: #fff; letter-spacing: 0.02em;
}
:root[data-theme="dark"] .preview-banner { color: #14202a; }
body:has(.preview-banner) .content { padding-top: calc(var(--topbar-h) + 2rem); }
body:has(.preview-banner) .sidebar { top: calc(var(--topbar-h) + 2rem); }

/* --------------------------------------------------------- search overlay */
.search-overlay {
  position: fixed; inset: 0; z-index: 70; display: grid; place-items: start center;
  padding-top: 12vh; background: color-mix(in srgb, var(--paper) 55%, transparent);
  backdrop-filter: blur(6px); opacity: 0; transition: opacity 0.2s var(--ease);
}
/* the [hidden] attribute must win over the display above, or the invisible
   overlay covers the page and swallows every click */
.search-overlay[hidden] { display: none; }
.search-overlay.show { opacity: 1; }
.search-box {
  width: min(40rem, 92vw); background: var(--surface); border: 1px solid var(--rule-strong);
  border-radius: 16px; box-shadow: 0 30px 80px -30px var(--shadow), 0 10px 30px var(--shadow-soft);
  overflow: hidden; transform: translateY(-8px); transition: transform 0.22s var(--ease);
}
.search-overlay.show .search-box { transform: none; }
.search-input-row { display: flex; align-items: center; gap: 0.6rem; padding: 0.9rem 1.1rem; border-bottom: 1px solid var(--rule); color: var(--ink-2); }
#searchInput {
  flex: 1; border: none; background: none; outline: none; color: var(--ink);
  font-family: var(--sans); font-size: 1.05rem;
}
.search-esc { font-family: var(--sans); font-size: 0.7rem; color: var(--ink-3); border: 1px solid var(--rule-strong); border-radius: 5px; padding: 0.1rem 0.4rem; }
.search-results { max-height: 56vh; overflow-y: auto; overscroll-behavior: contain; }
.search-group-title { font-family: var(--sans); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); padding: 0.9rem 1.2rem 0.35rem; }
.search-hit { display: block; padding: 0.65rem 1.2rem; text-decoration: none; color: var(--ink); border-left: 2px solid transparent; }
.search-hit:hover, .search-hit.active { background: var(--accent-wash); border-left-color: var(--accent); }
.search-hit-title { font-family: var(--sans); font-size: 0.9rem; font-weight: 600; }
.search-hit-context { font-size: 0.85rem; color: var(--ink-2); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.search-hit mark { background: var(--accent-soft); color: var(--ink); border-radius: 2px; padding: 0 1px; }
.search-empty { padding: 1.5rem 1.2rem; color: var(--ink-2); font-family: var(--sans); font-size: 0.9rem; text-align: center; }

/* ------------------------------------------------------------ reveal-in */
.reveal { opacity: 0; transform: translateY(12px); }
.reveal.in { opacity: 1; transform: none; transition: opacity 0.6s var(--ease), transform 0.6s var(--ease); }
@media (prefers-reduced-motion: reduce) { .reveal, .reveal.in { opacity: 1; transform: none; transition: none; } }

/* ------------------------------------------------- wide-screen: fixed rail */
@media (min-width: 1280px) {
  .sidebar { transform: translateX(0); box-shadow: none; }
  .content { margin-left: var(--rail); }
  .nav-toggle { display: none; }
  .scrim { display: none !important; }
  /* re-center the reading column within the space beside the rail */
  .chapter, .cover { margin-left: auto; margin-right: auto; }
}

/* keep the article optically centered accounting for the sidenote gutter */
@media (min-width: 1280px) {
  .chapter { padding-left: clamp(2rem, 5vw, 5rem); }
}

/* ------------------------------------------------------------- mobile */
@media (max-width: 640px) {
  :root { --step-4: 2.1rem; }
  .toc-item a { grid-template-columns: 2rem 1fr; }
  .chapter-nav { grid-template-columns: 1fr; }
  .prose p:first-of-type::first-letter { initial-letter: 2; -webkit-initial-letter: 2; }
}

/* focus visibility everywhere */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
