/* ============================================================
   PI Stories — design system: "THE CASE FILE"
   ------------------------------------------------------------
   A dossier the writing agent works to solve the case (= to
   produce a story that honors the genre). Light = a typed report
   on manila paper (the desk by day). Dark (DEFAULT) = the night
   desk under a warm lamp, wet street outside. Not an inversion:
   the same room, re-lit. See specs/design.md for the full record.
   House rules that survive any redesign:
   - tokens on :root, dark variant re-declares them under html.dark
   - reading surface never pure white, ink never pure black
   - one strong accent; the semantic accents carry meaning:
       --hardboiled / --noir  → the hardboiled≠noir distinction only
       --agent                → the "Directive to agent" callout only
   ============================================================ */

:root {
  /* Daylight — the typed report on paper */
  --c-bg: #efe8d8;          /* warm typed-report paper (never pure white) */
  --c-surface: #e7ddc4;     /* manila folder */
  --c-surface-2: #f6f1e5;   /* a fresh sheet */
  --c-ink: #211c16;         /* warm near-black (never pure black) */
  --c-muted: #5f5647;       /* faded typewriter grey */
  --c-faint: #8b806c;       /* pencil */
  --c-rule: #cabd9f;        /* hairline */
  /* accent — STAMP OXBLOOD: the crime, the seal, the primary mark */
  --c-accent: #a02c1f;
  --c-accent-soft: #83221a; /* legible oxblood for hover / text on paper */
  /* semantic accents — each meaning-locked (see header) */
  --hardboiled: #2f5d74;    /* STEEL — the code, the high ground */
  --noir: #4c4353;          /* SMOKE — doom, the losers */
  --agent: #2f6d5f;         /* VERDIGRIS — the directive to the agent */
  /* type — The Case File trio */
  --font-display: "Bodoni Moda", "Newsreader", Georgia, serif;
  --font-body: "Newsreader", Georgia, serif;
  --font-mono: "Courier Prime", ui-monospace, "Courier New", monospace;
  --font-ui: "Courier Prime", ui-monospace, monospace;
  /* texture */
  --grain-blend: multiply;
  --grain-opacity: 0.05;
  /* layout */
  --measure: 34rem;
  --page: 72rem;
}

html.dark {
  /* Night desk / wet street — the same world, re-lit */
  --c-bg: #141017;          /* wet asphalt at night, a touch of aubergine */
  --c-surface: #1e1922;     /* desk shadow */
  --c-surface-2: #241d29;
  --c-ink: #e9dfce;         /* warm lamplight on paper (never pure white) */
  --c-muted: #a1957f;
  --c-faint: #736855;
  --c-rule: #39313f;
  --c-accent: #e0553f;      /* wet-neon red in the puddle */
  --c-accent-soft: #ea8672;
  --hardboiled: #63a6c6;    /* lit steel */
  --noir: #8b7c95;          /* smoke under a streetlight */
  --agent: #57a08f;         /* terminal glow / lit verdigris */
  --grain-blend: overlay;
  --grain-opacity: 0.06;
}

/* ---------- base ---------- */

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--c-bg);
  color: var(--c-ink);
  font-family: var(--font-body);
  font-size: 1.0625rem;
  line-height: 1.65;
  font-optical-sizing: auto;
  transition: background 0.4s ease, color 0.4s ease;
}

/* Paper grain — feTurbulence data-URI overlay, the shared identity texture */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 9999;
  pointer-events: none;
  opacity: var(--grain-opacity);
  mix-blend-mode: var(--grain-blend);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
}
@media (prefers-reduced-motion: reduce) { body { transition: none; } }

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.2;
  text-wrap: balance;
}

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

code, pre, .data { font-family: var(--font-mono); font-size: 0.9em; }

.skip-link {
  position: absolute; left: -999px; top: 0;
  background: var(--c-accent); color: var(--c-bg); padding: 0.5rem 1rem;
}
.skip-link:focus { left: 0; z-index: 10; }

/* ---------- masthead ---------- */

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

.masthead__inner {
  max-width: var(--page); margin: 0 auto; padding: 0.9rem 1.25rem;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}

.brand { text-decoration: none; color: var(--c-ink); }
.brand__title { font-family: var(--font-display); font-size: 1.15rem; font-weight: 600; }

.masthead__links { display: flex; align-items: center; gap: 1.1rem; }

.navlink {
  font-family: var(--font-ui); font-size: 0.95rem;
  color: var(--c-ink); text-decoration: none;
}
.navlink:hover { color: var(--c-accent); }

.masthead__burger { display: none; }

.theme-toggle {
  background: none; border: 1px solid var(--c-rule); border-radius: 999px;
  width: 2.1rem; height: 2.1rem; padding: 0.35rem; cursor: pointer; color: var(--c-ink);
}
.theme-toggle svg { width: 100%; height: 100%; }
.theme-toggle__moon { display: none; }
html.dark .theme-toggle__sun { display: none; }
html.dark .theme-toggle__moon { display: block; }

@media (max-width: 40rem) {
  .masthead__burger {
    display: flex; flex-direction: column; gap: 4px;
    background: none; border: none; padding: 0.4rem; cursor: pointer;
  }
  .masthead__burger span { width: 20px; height: 2px; background: var(--c-ink); }
  .masthead__links {
    display: none; position: absolute; right: 1rem; top: 3.4rem;
    flex-direction: column; align-items: flex-end; gap: 0.8rem;
    background: var(--c-surface); border: 1px solid var(--c-rule);
    border-radius: 6px; padding: 1rem 1.25rem; z-index: 20;
  }
  .masthead__links.is-open { display: flex; }
}

/* ---------- content ---------- */

.hero { max-width: var(--measure); margin: 3.5rem auto 1rem; padding: 0 1.25rem; }
.hero__title { font-size: clamp(2rem, 6vw, 3.2rem); margin: 0 0 0.5rem; }
.hero__dek { color: var(--c-muted); font-size: 1.15rem; margin: 0; }

.prose { max-width: var(--measure); margin: 2rem auto 4rem; padding: 0 1.25rem; }
.prose h1 { font-size: clamp(1.7rem, 5vw, 2.4rem); }
.prose img { max-width: 100%; height: auto; }
.prose blockquote {
  margin: 1.5rem 0; padding: 0.25rem 0 0.25rem 1.1rem;
  border-left: 3px solid var(--c-accent); color: var(--c-muted); font-style: italic;
}
.prose table { border-collapse: collapse; width: 100%; }
.prose th, .prose td { border-bottom: 1px solid var(--c-rule); padding: 0.4rem 0.6rem; text-align: left; }
.prose th { font-family: var(--font-ui); font-size: 0.85rem; }

/* ---------- colophon ---------- */

.colophon { border-top: 1px solid var(--c-rule); margin-top: 2rem; }
.colophon__inner {
  max-width: var(--page); margin: 0 auto; padding: 2rem 1.25rem 2.5rem;
  font-family: var(--font-ui); font-size: 0.9rem; color: var(--c-muted);
}
.colophon__title { font-family: var(--font-display); color: var(--c-ink); margin: 0 0 0.5rem; }
.colophon__links { display: flex; flex-wrap: wrap; gap: 1rem; }
.colophon__links a { color: var(--c-muted); }
.colophon__foot { margin-top: 1.25rem; }

/* ---------- motion & print floor (non-negotiable) ---------- */

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media print {
  .masthead, .colophon, .theme-toggle, .skip-link { display: none; }
  body { background: #fff; color: #000; }
}

/* ============================================================
   THE CASE FILE — components (home, chapters, schools, agents)
   Ported from the approved design.html. Tokens only.
   ============================================================ */

:root { --rail: 15.5rem; }

/* ---------- shared marks ---------- */
.kicker { font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.22em; text-transform: uppercase; color: var(--c-faint); display: block; }
.stamp {
  display: inline-block; font-family: var(--font-mono); font-weight: 700; font-size: 0.74rem;
  letter-spacing: 0.16em; text-transform: uppercase; color: var(--c-accent);
  border: 2px solid var(--c-accent); border-radius: 3px; padding: 0.28em 0.6em;
  transform: rotate(-5deg); opacity: 0.85;
}
.cite { font-family: var(--font-mono); font-size: 0.76rem; color: var(--c-muted); font-style: normal; display: block; }
.cite::before { content: "— "; }

/* ============ HOME ============ */
.casehero { max-width: var(--page); margin: 0 auto; padding: 0 1.25rem; }
.casehero__wrap { position: relative; padding: clamp(1.8rem,5vw,4rem) 0 1rem; overflow: hidden; }
.casehero__wrap::before {
  content: ""; position: absolute; inset: -20% -10% -20% 42%; z-index: 0; pointer-events: none;
  background: repeating-linear-gradient(9deg,
     transparent 0 20px, color-mix(in srgb, var(--c-ink) 8%, transparent) 20px 23px);
  -webkit-mask-image: linear-gradient(105deg, transparent 30%, #000 92%);
  mask-image: linear-gradient(105deg, transparent 30%, #000 92%); opacity: 0.5;
}
.casehero__wrap > * { position: relative; z-index: 1; }
.casehero h1 { font-family: var(--font-display); font-weight: 900; font-size: clamp(2.6rem, 1.8rem + 4.4vw, 5.2rem); line-height: 1.04; margin: 0.2em 0 0.35em; letter-spacing: -0.015em; }
.casehero h1 em { font-style: italic; color: var(--c-accent); }
.casehero__dek { font-family: var(--font-body); font-size: clamp(1.12rem, 1rem + 0.6vw, 1.36rem); max-width: 37rem; line-height: 1.5; color: var(--c-ink); }
.casehero__dek b { font-weight: 600; }

.pillars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; background: var(--c-rule); border: 1px solid var(--c-rule); margin: 2.6rem auto 0; max-width: var(--page); }
.pillar { background: var(--c-surface); padding: 1.3rem 1.2rem; }
.pillar .n { font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.14em; color: var(--c-accent); text-transform: uppercase; }
.pillar h2 { font-family: var(--font-display); font-size: 1.3rem; margin: 0.35rem 0 0.4rem; font-weight: 700; }
.pillar p { margin: 0; font-size: 0.95rem; color: var(--c-muted); line-height: 1.5; }
@media (max-width: 46rem) { .pillars { grid-template-columns: 1fr; } }

.homerow { max-width: var(--page); margin: 2.6rem auto 0; padding: 0 1.25rem; }
.homerow h2 { font-family: var(--font-display); font-size: 1.7rem; margin: 0 0 0.3rem; }
.homerow p.lead { color: var(--c-muted); max-width: 42rem; margin: 0 0 1.2rem; }

.cta { display: flex; gap: 0.9rem; margin: 2.2rem 0 0; flex-wrap: wrap; }
.btn { font-family: var(--font-mono); font-size: 0.8rem; letter-spacing: 0.1em; text-transform: uppercase; text-decoration: none; padding: 0.75em 1.2em; border-radius: 3px; border: 1px solid var(--c-accent); color: var(--c-accent); transition: transform 0.12s ease; }
.btn.solid { background: var(--c-accent); color: var(--c-bg); }
.btn:hover { transform: translateY(-1px); }

/* index of chapters (home) */
.chapindex { list-style: none; padding: 0; margin: 1.2rem 0 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); gap: 1px; background: var(--c-rule); border: 1px solid var(--c-rule); }
.chapindex li { background: var(--c-surface); }
.chapindex a { display: block; padding: 1rem 1.15rem; text-decoration: none; color: var(--c-ink); }
.chapindex a:hover { background: var(--c-surface-2); }
.chapindex .n { font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.12em; color: var(--c-accent); }
.chapindex .t { font-family: var(--font-display); font-size: 1.15rem; display: block; margin-top: 0.15rem; }
.chapindex .d { font-size: 0.86rem; color: var(--c-muted); display: block; margin-top: 0.25rem; line-height: 1.4; }

/* ============ CHAPTER ============ */
.casetab { max-width: calc(var(--rail) + var(--measure) + 3.4rem); margin: 2rem auto 1.4rem; padding: 0 1.25rem; }
.tabflap { display: inline-flex; align-items: baseline; gap: 0.8rem; background: var(--c-surface); border: 1px solid var(--c-rule); border-bottom: none; border-radius: 7px 7px 0 0; padding: 0.55em 1.1em; box-shadow: inset 0 -2px 0 var(--c-accent); }
.tabflap .no { font-family: var(--font-mono); font-weight: 700; font-size: 0.78rem; letter-spacing: 0.14em; color: var(--c-accent); }
.tabflap .ttl { font-family: var(--font-mono); font-size: 0.78rem; letter-spacing: 0.08em; color: var(--c-muted); text-transform: uppercase; }
.casetab .bar { height: 1px; background: var(--c-rule); }

.chap { display: grid; grid-template-columns: var(--rail) minmax(0, var(--measure)); gap: clamp(1.5rem, 4vw, 3.4rem); justify-content: center; padding: 0 1.25rem; margin-bottom: 2rem; }
@media (max-width: 52rem) {
  .chap { grid-template-columns: 1fr; }
  .rail { border-bottom: 1px solid var(--c-rule); padding-bottom: 1rem; }
  .rail .toc { position: static; }
  .rail .toc summary { cursor: pointer; }
  .rail .toc summary::after { content: " ▸"; color: var(--c-accent); }
  .rail .toc[open] summary::after { content: " ▾"; }
  .fieldnote { margin-top: 1rem; }
}

.rail { font-size: 0.86rem; }
.rail .toc { position: sticky; top: 1.4rem; }
/* The TOC is JS-built; with JS off the shell stays empty — hide it. */
.rail .toc:has(> nav:empty) { display: none; }
.rail .toc summary { list-style: none; }
.rail .toc summary::-webkit-details-marker { display: none; }
@media (min-width: 52.01rem) { .rail .toc summary { pointer-events: none; } }
.rail .lab { font-family: var(--font-mono); font-size: 0.68rem; letter-spacing: 0.16em; text-transform: uppercase; color: var(--c-faint); margin-bottom: 0.7rem; }
.rail .toc a { display: block; color: var(--c-muted); text-decoration: none; padding: 0.28em 0 0.28em 0.7em; border-left: 2px solid transparent; font-family: var(--font-mono); font-size: 0.78rem; line-height: 1.35; }
.rail .toc a:hover { color: var(--c-accent); }
.rail .toc a.on { color: var(--c-accent); border-color: var(--c-accent); }
.rail .toc a.lvl3 { padding-left: 1.5rem; font-size: 0.74rem; }
.fieldnote { margin-top: 1.8rem; border-top: 1px solid var(--c-rule); padding-top: 1rem; }
.fieldnote .lab { color: var(--noir); }
.fieldnote p { margin: 0.5rem 0 0; font-size: 0.86rem; color: var(--c-muted); font-style: italic; line-height: 1.5; }

.chapter-body { font-size: 1.09rem; }
.chapter-body h1 { font-family: var(--font-display); font-size: clamp(2rem, 1.5rem + 2vw, 2.6rem); font-weight: 700; margin: 0.1em 0 0.15em; }
.chapter-body .subhead { font-family: var(--font-mono); font-size: 0.78rem; letter-spacing: 0.13em; text-transform: uppercase; color: var(--c-faint); margin-bottom: 1.4rem; }
.chapter-body .sechead { font-family: var(--font-display); font-size: 1.5rem; font-weight: 600; margin: 2.2rem 0 0.5rem; scroll-margin-top: 1.5rem; }
.chapter-body .secno { display: block; font-family: var(--font-mono); font-size: 0.74rem; letter-spacing: 0.1em; color: var(--c-accent); font-weight: 400; margin-bottom: 0.15rem; }
.chapter-body h3 { font-family: var(--font-display); font-size: 1.22rem; font-weight: 600; margin: 1.5rem 0 0.4rem; }
.chapter-body p { margin: 0 0 1.05rem; }
.chapter-body > p:first-of-type::first-letter { font-family: var(--font-display); font-weight: 900; float: left; font-size: 3.5em; line-height: 0.82; padding: 0.05em 0.12em 0 0; color: var(--c-accent); }
.chapter-body ul, .chapter-body ol { margin: 0 0 1.05rem; padding-left: 1.2rem; }
.chapter-body li { margin: 0.3rem 0; }
.chapter-body blockquote { margin: 1.4rem 0; padding: 0.1rem 0 0.1rem 1.1rem; border-left: 2px solid var(--c-accent); font-style: italic; color: var(--c-ink); }
.chapter-body blockquote .cite { margin-top: 0.5rem; font-style: normal; }
.chapter-body strong { font-weight: 700; }

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin: 3rem 0 0; border-top: 1px solid var(--c-rule); padding-top: 1.2rem; }
.pager a { text-decoration: none; display: block; }
.pager__next { text-align: right; }
.pager__lab { display: block; font-family: var(--font-mono); font-size: 0.74rem; letter-spacing: 0.08em; color: var(--c-accent); }
.pager__ttl { display: block; font-family: var(--font-display); font-size: 1.05rem; color: var(--c-ink); margin-top: 0.15rem; }

/* ---------- directive (agent hook) ---------- */
.directive { font-family: var(--font-mono); font-size: 0.86rem; line-height: 1.55; border-left: 3px solid var(--agent); border-radius: 0 5px 5px 0; background: color-mix(in srgb, var(--agent) 9%, var(--c-surface)); padding: 1rem 1.15rem; margin: 1.5rem 0; }
.directive .tag { display: block; color: var(--agent); font-weight: 700; font-size: 0.7rem; letter-spacing: 0.16em; text-transform: uppercase; margin-bottom: 0.5rem; }
.directive .tag::before { content: "\25B8  "; }
.directive p { margin: 0.4rem 0 0; }
.directive code { background: color-mix(in srgb, var(--agent) 16%, transparent); padding: 0.05em 0.3em; border-radius: 2px; font-size: 0.95em; }

/* ---------- distinction badge ---------- */
.distinct { display: grid; grid-template-columns: 1fr auto 1fr; align-items: stretch; border: 1px solid var(--c-rule); border-radius: 6px; overflow: hidden; margin: 1.6rem 0; }
.distinct .side { padding: 1.1rem 1.15rem; background: var(--c-surface); }
.distinct .side .h { font-family: var(--font-mono); font-weight: 700; font-size: 0.8rem; letter-spacing: 0.14em; text-transform: uppercase; margin-bottom: 0.5rem; display: block; }
.distinct .hb .h { color: var(--hardboiled); }
.distinct .nr .h { color: var(--noir); }
.distinct .hb { box-shadow: inset 4px 0 0 var(--hardboiled); }
.distinct .nr { box-shadow: inset -4px 0 0 var(--noir); }
.distinct .side p { margin: 0; font-size: 0.9rem; color: var(--c-muted); line-height: 1.45; }
.distinct .vs { display: flex; align-items: center; justify-content: center; padding: 0 0.9rem; background: var(--c-surface-2); font-family: var(--font-display); font-size: 1.6rem; color: var(--c-faint); }
@media (max-width: 34rem) { .distinct { grid-template-columns: 1fr; } .distinct .vs { padding: 0.5rem; } }

/* ============ SCHOOLS ============ */
.spectrum { margin: 1.4rem 0 2.2rem; }
.spectrum .track { height: 12px; border-radius: 6px; background: linear-gradient(90deg, #d8a24a, #c06a35 38%, var(--c-accent) 62%, var(--noir) 100%); }
.spectrum .ends { display: flex; justify-content: space-between; font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--c-muted); margin-top: 0.5rem; }
table.schools { width: 100%; border-collapse: collapse; font-size: 0.9rem; margin: 1rem 0 2rem; }
table.schools th, table.schools td { text-align: left; padding: 0.7em 0.8em; border-bottom: 1px solid var(--c-rule); vertical-align: top; }
table.schools th { font-family: var(--font-mono); font-size: 0.7rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--c-faint); border-bottom: 2px solid var(--c-rule); }
table.schools td.name { font-family: var(--font-display); font-weight: 600; font-size: 1rem; white-space: nowrap; }
table.schools td .ex { font-family: var(--font-mono); font-size: 0.78rem; color: var(--c-muted); }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: 1rem; margin: 1.6rem 0; }
.card { background: var(--c-surface); border: 1px solid var(--c-rule); border-top: 3px solid var(--c-accent); border-radius: 0 0 5px 5px; padding: 1.1rem 1.15rem; }
.card.hb { border-top-color: var(--hardboiled); }
.card.nr { border-top-color: var(--noir); }
.card h3 { font-family: var(--font-display); font-size: 1.22rem; margin: 0 0 0.2rem; }
.card .tone { font-family: var(--font-mono); font-size: 0.7rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--c-faint); }
.card p { font-size: 0.88rem; color: var(--c-muted); margin: 0.6rem 0 0.3rem; line-height: 1.45; }
.card .ex { font-family: var(--font-mono); font-size: 0.76rem; color: var(--c-muted); }

/* ============ prose pages (schools / agents / about) ============ */
.doc { max-width: 46rem; margin: 0 auto 3rem; padding: 0 1.25rem; }
.doc.wide { max-width: 60rem; }
.doc h1 { font-family: var(--font-display); font-size: clamp(2rem, 1.4rem + 2.6vw, 3.1rem); margin: 0.3em 0 0.1em; }
.doc h2 { font-family: var(--font-display); font-size: 1.6rem; margin: 2rem 0 0.5rem; }
.doc h3 { font-family: var(--font-display); font-size: 1.2rem; margin: 1.4rem 0 0.4rem; }
.doc p, .doc li { line-height: 1.62; }
.doc .lead { font-size: 1.15rem; color: var(--c-ink); max-width: 42rem; }
.doc .cite { margin: 0.4rem 0 1rem; }

/* checklist (agents page) */
.checklist { list-style: none; padding: 0; margin: 1rem 0 1.6rem; counter-reset: step; }
.checklist li { position: relative; padding: 0.6rem 0 0.6rem 2.4rem; border-bottom: 1px solid var(--c-rule); }
.checklist li::before { counter-increment: step; content: counter(step, decimal-leading-zero); position: absolute; left: 0; top: 0.6rem; font-family: var(--font-mono); font-size: 0.78rem; color: var(--c-accent); font-weight: 700; }
.checklist strong { font-family: var(--font-display); font-weight: 600; }

/* light divider */
.rule { border: none; border-top: 1px solid var(--c-rule); margin: 2.4rem 0; }

/* home chapter-index coda cell (the "put it to work" pointer) */
.chapindex .coda { background: color-mix(in srgb, var(--c-accent) 10%, var(--c-surface)); }
.chapindex .coda .n { color: var(--c-accent); }

/* schools table: isolate any overflow so the page never pans on mobile */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
@media (max-width: 40rem) { table.schools td.name { white-space: normal; } }
