/* ── Reset & base ──────────────────────────────────────────── */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

/*
 * Runbook — Field Manual for Troubleshooting
 * Tokens follow PROJECTS.md §5. Header and footer chrome come from
 * the Neorgon kits and are never restyled here.
 */

:root {
  --bg: #040714;
  --surface-1: rgba(255,255,255,.03);
  --surface-2: rgba(255,255,255,.06);
  --border-subtle: rgba(255,255,255,.07);
  --border: rgba(255,255,255,.1);
  --border-strong: rgba(255,255,255,.22);
  --text-primary: #f9f9f9;
  --text-secondary: #cacaca;
  --text-muted: rgba(255,255,255,.55);
  --accent: #b8935f;
  --accent-bright: #d8b280;
  --accent-dim: rgba(184,147,95,.15);
  --danger: #e11d48;
  --danger-bright: #f43f5e;
  --font: 'Avenir Next', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --text-xs: 0.75rem; --text-sm: 0.875rem; --text-base: 1rem;
  --text-lg: 1.125rem; --text-xl: 1.25rem; --text-2xl: 1.5rem;
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-6: 24px; --space-8: 32px;
  --radius-sm: 6px; --radius: 10px; --radius-lg: 15px;
  --ease-snap: cubic-bezier(0.34, 1.56, 0.64, 1);
  --dur-fast: 120ms; --dur: 200ms;

  /* Site-local: no canonical mono token exists in the fleet yet */
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --toc-w: 304px;
  --measure: 72ch;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-secondary);
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-bright); text-underline-offset: 3px; }
a:hover { color: var(--text-primary); }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* ── Shell ──────────────────────────────────────────────────── */

.shell {
  display: grid;
  grid-template-columns: var(--toc-w) minmax(0, 1fr);
  align-items: start;
  flex: 1;
}

/* ── Table of contents ──────────────────────────────────────── */

.toc {
  position: sticky;
  top: var(--header-h, 68px);
  height: calc(100dvh - var(--header-h, 68px));
  overflow-y: auto;
  overscroll-behavior: contain;
  border-right: 1px solid var(--border-subtle);
  padding-bottom: var(--space-8);
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.toc::-webkit-scrollbar { width: var(--space-2); }
.toc::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--radius-sm); }

.toc-tools {
  display: flex;
  gap: var(--space-2);
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg);
  padding: var(--space-4) var(--space-6) var(--space-3);
}

.toc-search { flex: 1; min-width: 0; }

.toc-search input {
  width: 100%;
  height: var(--space-8);
  padding: 0 var(--space-3);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: var(--text-sm);
}
.toc-search input::placeholder { color: var(--text-muted); }
.toc-search input:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.toc-clear {
  height: var(--space-8);
  padding: 0 var(--space-3);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: .04em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color var(--dur-fast), border-color var(--dur-fast);
}
.toc-clear:hover { color: var(--text-primary); border-color: var(--border-strong); }

.toc-count {
  padding: 0 var(--space-6);
  min-height: var(--space-4);
  font-family: var(--mono);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.toc-list, .toc-secs { list-style: none; }
.toc-list { margin-top: var(--space-2); }

.toc-ch-link {
  display: flex;
  gap: var(--space-3);
  align-items: baseline;
  padding: var(--space-2) var(--space-6);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: var(--text-sm);
  font-weight: 600;
}
.toc-ch-link:hover { color: var(--text-primary); background: var(--surface-1); }

.toc-ch-num {
  font-family: var(--mono);
  font-size: var(--text-xs);
  color: var(--text-muted);
  min-width: var(--space-3);
}

.toc-ch.is-current > .toc-ch-link { color: var(--text-primary); }
.toc-ch.is-current > .toc-ch-link .toc-ch-num { color: var(--accent); }

/* Sections stay collapsed until their chapter is the one being read,
   which keeps a 66-section contents list navigable. */
.toc-secs {
  display: none;
  margin: 0 0 var(--space-3) var(--space-8);
  border-left: 1px solid var(--border-subtle);
}
.toc-ch.is-current .toc-secs,
body.is-filtering .toc-secs { display: block; }

.toc-sec-link {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-6) var(--space-1) var(--space-3);
  color: var(--text-muted);
  text-decoration: none;
  font-size: var(--text-xs);
  line-height: 1.5;
}
.toc-sec-link:hover { color: var(--text-primary); }

.toc-sec-num { font-family: var(--mono); font-size: var(--text-xs); flex: none; }

.toc-sec.is-active { margin-left: -1px; border-left: 1px solid var(--accent); }
.toc-sec.is-active .toc-sec-link { color: var(--text-primary); }
.toc-sec.is-active .toc-sec-num { color: var(--accent); }

.toc-scrim { display: none; }

/* ── Document ───────────────────────────────────────────────── */

.doc {
  padding: var(--space-8) clamp(var(--space-6), 5vw, 64px) calc(var(--space-8) * 4);
  max-width: calc(var(--measure) + 128px);
}

.ch { margin-bottom: calc(var(--space-8) * 3); }
.ch:last-child { margin-bottom: 0; }

.ch-head {
  padding-bottom: var(--space-6);
  margin-bottom: var(--space-8);
  border-bottom: 1px solid var(--border);
}

.ch-eyebrow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.ch-label {
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.ch-copy {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: var(--text-xs);
  padding: var(--space-1) var(--space-3);
  cursor: pointer;
  transition: color var(--dur-fast), border-color var(--dur-fast);
}
.ch-copy:hover { color: var(--accent-bright); border-color: var(--accent); }

.ch-title {
  display: flex;
  align-items: baseline;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
  font-size: var(--text-2xl);
  line-height: 1.25;
  font-weight: 600;
  letter-spacing: -.02em;
  color: var(--text-primary);
}

.ch-num {
  font-family: var(--mono);
  font-size: var(--text-base);
  font-weight: 400;
  color: var(--accent);
  letter-spacing: 0;
  flex: none;
}

.ch-summary {
  max-width: var(--measure);
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* ── Sections ───────────────────────────────────────────────── */

.sec {
  scroll-margin-top: calc(var(--header-h, 68px) + var(--space-6));
  margin-bottom: calc(var(--space-8) + var(--space-3));
}

.sec-head {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
  font-size: var(--text-lg);
  line-height: 1.4;
  font-weight: 600;
  color: var(--text-primary);
}

.sec-num {
  font-family: var(--mono);
  font-size: var(--text-sm);
  font-weight: 400;
  color: var(--accent);
  flex: none;
}

.sec-anchor {
  font-family: var(--mono);
  font-size: var(--text-sm);
  color: var(--text-muted);
  text-decoration: none;
  opacity: 0;
  transition: opacity var(--dur-fast);
}
.sec-head:hover .sec-anchor,
.sec-anchor:focus-visible { opacity: 1; }

.sec-body { max-width: var(--measure); }
.sec-body p { margin-bottom: var(--space-4); }
.sec-body > *:last-child { margin-bottom: 0; }
.sec-body strong { color: var(--text-primary); font-weight: 600; }

code {
  font-family: var(--mono);
  font-size: .875em;
  background: var(--surface-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 0 var(--space-1);
  color: var(--text-primary);
}

/* ── Lists ──────────────────────────────────────────────────── */

.doc-list { margin-bottom: var(--space-4); padding-left: var(--space-4); }
.doc-list li { margin-bottom: var(--space-2); padding-left: var(--space-1); }
.doc-list li::marker { color: var(--text-muted); }
.doc-list--ordered li::marker { font-family: var(--mono); font-size: var(--text-sm); }

/* ── Definition blocks ──────────────────────────────────────── */

.defs { margin-bottom: var(--space-4); }
.def { padding: var(--space-3) 0; border-top: 1px solid var(--border-subtle); }
.def:last-child { border-bottom: 1px solid var(--border-subtle); }
.def dt { color: var(--text-primary); font-weight: 600; margin-bottom: var(--space-1); }

/* ── Callouts ───────────────────────────────────────────────── */

.note {
  margin-bottom: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-left: 2px solid var(--border-strong);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--surface-1);
}

.note-label {
  margin-bottom: var(--space-1);
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.note-text { font-size: var(--text-sm); }

.note[data-label="Rule"] { border-left-color: var(--accent); }
.note[data-label="Rule"] .note-label { color: var(--accent); }
.note[data-label="Trap"],
.note[data-label="Misdiagnosis"] { border-left-color: var(--danger); }
.note[data-label="Trap"] .note-label,
.note[data-label="Misdiagnosis"] .note-label { color: var(--danger-bright); }

/* ── Checklists ─────────────────────────────────────────────── */

.checks { list-style: none; margin-bottom: var(--space-4); }

.check {
  display: grid;
  grid-template-columns: var(--space-4) 1fr;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-top: 1px solid var(--border-subtle);
}
.check:last-child { border-bottom: 1px solid var(--border-subtle); }

.check-mark {
  width: var(--space-4);
  height: var(--space-4);
  margin-top: var(--space-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}

.check-item { margin-bottom: var(--space-1); color: var(--text-primary); font-weight: 600; }
.check-how { font-size: var(--text-sm); }

/* ── Code figures ───────────────────────────────────────────── */

.code { margin-bottom: var(--space-4); }

.code figcaption {
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding-bottom: var(--space-2);
}

.code pre {
  /* Set explicitly: a bare `pre` inherits the UA monospace quirk (13px). */
  font-family: var(--mono);
  font-size: var(--text-sm);
  padding: var(--space-4);
  background: rgba(0,0,0,.34);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  overflow-x: auto;
}

.code code {
  background: none;
  border: 0;
  padding: 0;
  font-size: var(--text-sm);
  line-height: 1.65;
  color: var(--text-secondary);
  white-space: pre;
}

/* ── Tables ─────────────────────────────────────────────────── */

.table-wrap {
  margin-bottom: var(--space-4);
  overflow-x: auto;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
}

table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }

th, td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border-subtle);
}
tbody tr:last-child td { border-bottom: 0; }

th {
  font-family: var(--mono);
  font-size: var(--text-xs);
  font-weight: 400;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface-1);
  white-space: nowrap;
}

td:first-child { color: var(--text-primary); }

/* ── Worked examples ────────────────────────────────────────── */

.example {
  margin-bottom: var(--space-4);
  padding: var(--space-6);
  background: var(--surface-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
}

.example-title {
  margin-bottom: var(--space-3);
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--text-primary);
}

.example-scenario {
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  font-size: var(--text-sm);
}

.walk { list-style: none; counter-reset: walk; margin-bottom: var(--space-4); }

.walk-step {
  counter-increment: walk;
  position: relative;
  padding-left: var(--space-8);
  margin-bottom: var(--space-4);
}
.walk-step:last-child { margin-bottom: 0; }

.walk-step::before {
  content: counter(walk, decimal-leading-zero);
  position: absolute;
  left: 0;
  font-family: var(--mono);
  font-size: var(--text-xs);
  color: var(--accent);
}

.walk-label { color: var(--text-primary); font-weight: 600; font-size: var(--text-sm); }
.walk-text { font-size: var(--text-sm); }

.example-lesson {
  padding-top: var(--space-4);
  border-top: 1px solid var(--border-subtle);
  font-size: var(--text-sm);
}

.example-lesson-label {
  display: block;
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: var(--space-1);
}

/* ── Shortcuts panel ────────────────────────────────────────── */

.shortcuts {
  position: fixed;
  right: var(--space-6);
  bottom: var(--space-6);
  z-index: var(--z-modal, 400);
  width: 256px;
  padding: var(--space-4);
  background: var(--surface-toast, #1a1730);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 24px 48px rgba(2, 6, 23, .58);
}

.shortcuts-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.shortcuts-head h2 {
  font-family: var(--mono);
  font-size: var(--text-xs);
  font-weight: 400;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.shortcuts-close {
  background: none;
  border: 0;
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: var(--text-xs);
  cursor: pointer;
  transition: color var(--dur-fast);
}
.shortcuts-close:hover { color: var(--text-primary); }

.shortcuts-list > div {
  display: flex;
  gap: var(--space-3);
  align-items: baseline;
  margin-bottom: var(--space-2);
}
.shortcuts-list > div:last-child { margin-bottom: 0; }
.shortcuts-list dt { flex: none; width: var(--space-8); }
.shortcuts-list dd { font-size: var(--text-sm); }

kbd {
  display: inline-block;
  min-width: var(--space-6);
  padding: 0 var(--space-2);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: var(--text-xs);
  color: var(--text-primary);
  text-align: center;
}

/* ── Toast ──────────────────────────────────────────────────── */

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--space-6);
  transform: translate(-50%, var(--space-3));
  z-index: var(--z-toast, 500);
  padding: var(--space-2) var(--space-4);
  background: var(--surface-toast, #1a1730);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: var(--text-sm);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur), transform var(--dur) var(--ease-snap);
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

/* ── Responsive ─────────────────────────────────────────────── */

.toc-toggle { display: none; }

@media (max-width: 1000px) {
  .shell { grid-template-columns: minmax(0, 1fr); }

  .toc-toggle { display: inline-flex; }

  .toc {
    position: fixed;
    top: var(--header-h, 68px);
    left: 0;
    z-index: var(--z-sticky-header, 200);
    width: min(340px, 86vw);
    background: var(--bg);
    border-right: 1px solid var(--border);
    transform: translateX(-102%);
    transition: transform var(--dur) ease;
  }
  body.drawer-open .toc { transform: translateX(0); }

  .toc-scrim {
    display: block;
    position: fixed;
    inset: var(--header-h, 68px) 0 0;
    z-index: calc(var(--z-sticky-header, 200) - 1);
    background: rgba(0,0,0,.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur);
  }
  body.drawer-open .toc-scrim { opacity: 1; pointer-events: auto; }

  .doc { padding: var(--space-6) clamp(var(--space-4), 5vw, var(--space-8)) calc(var(--space-8) * 3); }
}

@media (prefers-reduced-motion: reduce) {
  .toc, .toc-scrim, .toast { transition: none; }
}

@media (max-width: 640px) {
  .ch { margin-bottom: calc(var(--space-8) * 2); }
  .example { padding: var(--space-4); }
  .shortcuts { right: var(--space-3); left: var(--space-3); width: auto; bottom: var(--space-3); }
}

/* ── Print ──────────────────────────────────────────────────── */

@media print {
  .toc, .toc-scrim, .shortcuts, .toast, .header-bar, .ch-copy, .sec-anchor { display: none !important; }

  body { background: #fff; color: #000; font-size: 11pt; }
  .shell { display: block; }
  .doc { max-width: none; padding: 0; }

  .ch { break-before: page; margin-bottom: var(--space-6); }
  .ch:first-child { break-before: auto; }
  .sec, .example, .note, .check, .code { break-inside: avoid; }

  .ch-title, .sec-head, .check-item, .def dt, strong { color: #000; }
  .ch-num, .sec-num, .walk-step::before, .example-lesson-label { color: #7a5a28; }
  .ch-summary, .note-label, .ch-label { color: #444; }

  .note, .example, .code pre, .table-wrap { background: none; border: 1px solid #bbb; }
  a { color: #000; }
}
