/* Freya End-to-End Latency — A Field Engineer's Deep Dive.
   Distinct identity from the voice-settings guide: a "stopwatch / waterfall"
   theme. Cyan-teal accent on near-black ink, timeline rails everywhere.
   No frameworks, no CDN, no build step. Works offline. Light + dark. */

:root {
  /* latency accent: cyan-teal, reads as "clock / signal" */
  --accent: #0bb6c2;
  --accent-2: #6d5cff;        /* secondary, for "LLM" lanes */
  --accent-soft: #0bb6c216;
  --accent-line: #0bb6c238;
  --bg: #f7f9fb;
  --surface: #ffffff;
  --surface-2: #f1f4f7;
  --ink: #0e1726;
  --ink-2: #354253;
  --ink-3: #66738a;
  --hair: #d2dae3;
  --hair-soft: #e6ebf1;
  --ok: #0a8f5b;
  --ok-soft: #0a8f5b14;
  --warn: #b26a00;
  --warn-soft: #b26a0014;
  --dead: #c2253c;
  --dead-soft: #c2253c12;
  /* timeline stage colours (used by lib.js waterfall) */
  --st-vad: #f0a020;
  --st-stt: #2aa0d8;
  --st-llm: #6d5cff;
  --st-tts: #18b58a;
  --st-net: #98a4b5;
  --radius: 14px;
  --radius-sm: 9px;
  --maxw: 1080px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ---------- dark mode (auto + manual override on <html data-theme>) ---------- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent-soft: #0bb6c220;
    --accent-line: #0bb6c24a;
    --bg: #0b1018;
    --surface: #121a24;
    --surface-2: #0e151e;
    --ink: #eaf0f6;
    --ink-2: #b7c2d0;
    --ink-3: #7c8a9c;
    --hair: #243140;
    --hair-soft: #1b2632;
    --ok: #3fcf8e;
    --ok-soft: #3fcf8e1c;
    --warn: #e0a13a;
    --warn-soft: #e0a13a1c;
    --dead: #ff6b81;
    --dead-soft: #ff6b811c;
  }
}
:root[data-theme="dark"] {
  --accent-soft: #0bb6c220;
  --accent-line: #0bb6c24a;
  --bg: #0b1018;
  --surface: #121a24;
  --surface-2: #0e151e;
  --ink: #eaf0f6;
  --ink-2: #b7c2d0;
  --ink-3: #7c8a9c;
  --hair: #243140;
  --hair-soft: #1b2632;
  --ok: #3fcf8e;
  --ok-soft: #3fcf8e1c;
  --warn: #e0a13a;
  --warn-soft: #e0a13a1c;
  --dead: #ff6b81;
  --dead-soft: #ff6b811c;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: 72px; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------- top nav ---------- */
.topnav {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 24px;
  height: 56px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(160%) blur(18px);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  border-bottom: 1px solid var(--hair-soft);
}
.topnav .brand {
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 15px;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.topnav .brand b { color: var(--accent); }
/* little stopwatch glyph before the brand */
.topnav .brand .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  display: inline-block;
}
.topnav .links {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}
.topnav .links::-webkit-scrollbar { display: none; }
.topnav .links a {
  font-size: 13px;
  color: var(--ink-3);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: 8px;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.topnav .links a:hover { color: var(--ink); background: var(--hair-soft); }
.topnav .links a.active { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.topnav .spacer { flex: 1; }
.topnav .theme-toggle {
  flex: none;
  font-family: var(--mono); font-size: 12px;
  color: var(--ink-3); background: transparent;
  border: 1px solid var(--hair); border-radius: 8px;
  padding: 5px 10px; cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.topnav .theme-toggle:hover { color: var(--accent); border-color: var(--accent-line); }

/* ---------- layout ---------- */
.container { max-width: var(--maxw); margin: 0 auto; padding: 0 24px; }
.wide { max-width: 1240px; }

/* ---------- hero ---------- */
.hero {
  position: relative;
  text-align: center;
  padding: 84px 24px 52px;
  overflow: hidden;
  background:
    radial-gradient(1200px 420px at 50% -140px, var(--accent-soft), transparent 70%);
}
/* faint timeline gridlines behind the hero — the "stopwatch" motif */
.hero::before {
  content: "";
  position: absolute; inset: 0;
  background-image: repeating-linear-gradient(90deg, var(--hair-soft) 0 1px, transparent 1px 80px);
  -webkit-mask-image: linear-gradient(180deg, transparent, #000 40%, transparent);
  mask-image: linear-gradient(180deg, transparent, #000 40%, transparent);
  opacity: 0.6;
  pointer-events: none;
}
.hero > * { position: relative; }
.hero .eyebrow {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 14px;
}
.hero h1 {
  font-size: clamp(34px, 6vw, 56px);
  line-height: 1.05;
  letter-spacing: -0.025em;
  margin: 0 0 18px;
  font-weight: 750;
}
.hero p {
  font-size: clamp(17px, 2.4vw, 21px);
  color: var(--ink-2);
  max-width: 720px;
  margin: 0 auto 8px;
}
.hero .sub { font-size: 14px; color: var(--ink-3); margin-top: 18px; }
.hero .sub code { font-family: var(--mono); background: var(--surface); padding: 2px 7px; border-radius: 6px; border: 1px solid var(--hair); }

/* ---------- the latency equation block (hero / index) ---------- */
.equation {
  max-width: 760px;
  margin: 30px auto 0;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--hair);
  border-radius: var(--radius);
  padding: 20px 22px;
  box-shadow: 0 8px 30px rgba(8, 18, 30, 0.06);
}
.equation .eqlbl {
  font-size: 12px; font-weight: 700; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--accent); margin-bottom: 12px;
}
.equation pre {
  margin: 0; background: var(--surface-2); color: var(--ink);
  font-size: 12.5px; line-height: 1.75; padding: 14px 16px;
  border-radius: var(--radius-sm); border: 1px solid var(--hair-soft);
  overflow-x: auto;
}
.equation pre .term { color: var(--accent); font-weight: 600; }
.equation pre .cmt { color: var(--ink-3); }

/* ---------- part header (subpages) ---------- */
.parthead {
  position: relative;
  padding: 60px 24px 38px;
  border-bottom: 1px solid var(--hair-soft);
  background: radial-gradient(900px 320px at 0% -90px, var(--accent-soft), transparent 70%);
  overflow: hidden;
}
.parthead::before {
  content: "";
  position: absolute; inset: 0;
  background-image: repeating-linear-gradient(90deg, var(--hair-soft) 0 1px, transparent 1px 64px);
  -webkit-mask-image: linear-gradient(90deg, #000, transparent 60%);
  mask-image: linear-gradient(90deg, #000, transparent 60%);
  opacity: 0.5; pointer-events: none;
}
.parthead > * { position: relative; }
.parthead .kicker { font-size: 13px; font-weight: 600; color: var(--accent); letter-spacing: 0.04em; text-transform: uppercase; }
.parthead h1 { font-size: clamp(26px, 4.5vw, 40px); letter-spacing: -0.02em; margin: 10px 0 12px; }
.parthead p { font-size: 18px; color: var(--ink-2); max-width: 740px; margin: 0; }

/* ---------- section / step blocks ---------- */
section { padding: 26px 0; }
.step {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--hair-soft);
  border-radius: var(--radius);
  padding: 24px 26px 24px 28px;
  margin: 16px 0;
  box-shadow: 0 1px 2px rgba(8, 18, 30, 0.04);
}
/* a thin "rail" down the left of every step — the timeline spine */
.step::before {
  content: "";
  position: absolute; left: 0; top: 14px; bottom: 14px; width: 3px;
  border-radius: 3px; background: var(--accent-line);
}
.step.s-vad::before { background: var(--st-vad); }
.step.s-stt::before { background: var(--st-stt); }
.step.s-llm::before { background: var(--st-llm); }
.step.s-tts::before { background: var(--st-tts); }
.step.s-net::before { background: var(--st-net); }
.step h2, .step h3 { letter-spacing: -0.015em; }
.step h2 { font-size: 21px; margin: 0 0 6px; }
.step .stepno {
  display: inline-block;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 9px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.step p { color: var(--ink-2); margin: 10px 0; }
.step ul, .step ol { color: var(--ink-2); padding-left: 20px; }
.step li { margin: 5px 0; }
h2, h3, h4 { color: var(--ink); }

/* spec row: key/range/default chips */
.spec { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.spec .chip {
  font-family: var(--mono); font-size: 12px;
  padding: 5px 10px; border-radius: 8px;
  border: 1px solid var(--hair); background: var(--surface); color: var(--ink-2);
}
.spec .chip b { color: var(--ink); font-weight: 600; }
.spec .chip.key { border-color: var(--accent-line); color: var(--accent); }

/* a "budget" pill: shows a millisecond cost inline */
.ms {
  font-family: var(--mono); font-size: 12px; font-weight: 600;
  color: var(--accent); background: var(--accent-soft);
  border: 1px solid var(--accent-line); padding: 1px 7px; border-radius: 6px;
}

/* labels */
.tag {
  display: inline-block;
  font-size: 11px; font-weight: 600; letter-spacing: 0.02em;
  padding: 2px 8px; border-radius: 6px; vertical-align: middle;
}
.tag.dead { color: var(--dead); background: var(--dead-soft); }
.tag.warn { color: var(--warn); background: var(--warn-soft); }
.tag.ok { color: var(--ok); background: var(--ok-soft); }
.tag.vad { color: #9a6a08; background: #f0a02018; }
.tag.stt { color: #1d7bab; background: #2aa0d818; }
.tag.llm { color: #5647d6; background: #6d5cff18; }
.tag.tts { color: #0e8f6c; background: #18b58a18; }

/* callouts */
.callout {
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  padding: 14px 18px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin: 16px 0; font-size: 15px; color: var(--ink-2);
}
.callout.dead { border-left-color: var(--dead); background: var(--dead-soft); }
.callout.warn { border-left-color: var(--warn); background: var(--warn-soft); }
.callout.ok { border-left-color: var(--ok); background: var(--ok-soft); }
.callout .h { display: block; font-weight: 600; color: var(--ink); margin-bottom: 4px; }

.ask {
  border: 1px dashed var(--accent-line);
  border-radius: var(--radius-sm);
  padding: 12px 16px; margin: 16px 0;
  font-size: 14px; color: var(--ink-2); background: var(--surface);
}
.ask .h { font-weight: 600; color: var(--accent); }
.ask code { font-family: var(--mono); font-size: 12.5px; background: var(--accent-soft); padding: 1px 5px; border-radius: 5px; }

/* symptom -> fix line */
.symptom { font-size: 14px; color: var(--ink-3); margin-top: 8px; }
.symptom b { color: var(--ink); }

/* collapsible checkpoint */
details.checkpoint {
  border: 1px solid var(--hair); border-radius: var(--radius-sm);
  padding: 0; margin: 16px 0; background: var(--surface); overflow: hidden;
}
details.checkpoint > summary {
  cursor: pointer; list-style: none; padding: 12px 16px;
  font-size: 14px; font-weight: 600; color: var(--ink);
  display: flex; align-items: center; gap: 8px;
}
details.checkpoint > summary::-webkit-details-marker { display: none; }
details.checkpoint > summary::before {
  content: "?"; font-family: var(--mono); font-weight: 700;
  width: 18px; height: 18px; flex: none; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px;
}
details.checkpoint[open] > summary { border-bottom: 1px solid var(--hair-soft); }
details.checkpoint .body { padding: 14px 16px; font-size: 14px; color: var(--ink-2); }
details.checkpoint .body p { margin: 8px 0; }

/* ---------- demos ---------- */
.demo {
  border: 1px solid var(--hair); border-radius: var(--radius);
  background: var(--surface); padding: 22px; margin: 20px 0;
}
.demo .demo-title {
  font-size: 12px; font-weight: 700; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--accent); margin-bottom: 14px;
}
.demo label { font-size: 13px; color: var(--ink-2); display: block; margin: 10px 0 4px; font-weight: 500; }
.demo .row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.demo input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 4px; border-radius: 2px;
  background: var(--hair); outline: none; margin: 6px 0;
}
.demo input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
  border: 2px solid var(--surface); box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}
.demo input[type=range]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid var(--surface);
}
.demo .readout {
  font-family: var(--mono); font-size: 13px;
  color: var(--accent); font-weight: 600; min-width: 64px; text-align: right;
}
.demo .verdict {
  margin-top: 14px; padding: 12px 16px; border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 500;
}
.demo .verdict.good { background: var(--ok-soft); color: var(--ok); }
.demo .verdict.bad { background: var(--dead-soft); color: var(--dead); }
.demo .verdict.neutral { background: var(--hair-soft); color: var(--ink-2); }
.demo select, .demo input[type=text], .demo textarea {
  font-family: var(--sans); font-size: 14px; padding: 8px 11px;
  border: 1px solid var(--hair); border-radius: 8px;
  background: var(--surface); color: var(--ink); width: 100%;
}
.demo textarea { min-height: 70px; resize: vertical; }
.demo .toggle-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.demo .tg {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 11px; border: 1px solid var(--hair); border-radius: 8px;
  font-size: 13px; cursor: pointer; user-select: none; background: var(--surface);
}
.demo .tg input { accent-color: var(--accent); }
.demo .out {
  font-family: var(--mono); font-size: 13px; line-height: 1.7;
  background: #0c121b; color: #d7e2ee; padding: 14px 16px;
  border-radius: var(--radius-sm); margin-top: 12px; white-space: pre-wrap; word-break: break-word;
}
.demo .out .hl { color: #67e8d4; }
.demo .out .hl2 { color: #b3a7ff; }

/* svg timeline / waterfall */
.demo svg, .tl svg { width: 100%; height: auto; display: block; }
.demo .legend, .tl .legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--ink-3); margin-top: 10px; }
.demo .legend i, .tl .legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: middle; margin-right: 5px; }

/* standalone timeline figure (not in a demo) */
.tl {
  border: 1px solid var(--hair); border-radius: var(--radius);
  background: var(--surface); padding: 20px 22px; margin: 20px 0;
}
.tl .tl-title { font-size: 12px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent); margin-bottom: 12px; }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: 14px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--hair-soft); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--ink-3); font-weight: 600; }
table.sortable thead th { cursor: pointer; user-select: none; white-space: nowrap; transition: color 0.15s; }
table.sortable thead th:hover, table.sortable thead th:focus-visible { color: var(--accent); outline: none; }
table.sortable thead th[aria-sort="ascending"], table.sortable thead th[aria-sort="descending"] { color: var(--accent); }
.sort-ind { font-size: 10px; color: var(--accent); }
td code, p code, li code { font-family: var(--mono); font-size: 12.5px; background: var(--hair-soft); padding: 1px 5px; border-radius: 5px; color: var(--ink); }
/* budget table: healthy / suspect columns */
td.healthy { color: var(--ok); font-family: var(--mono); font-weight: 600; }
td.suspect { color: var(--dead); font-family: var(--mono); font-weight: 600; }
tr.total td { font-weight: 700; border-top: 2px solid var(--hair); }

/* ---------- code block ---------- */
pre {
  background: #0c121b; color: #d7e2ee; padding: 16px 18px;
  border-radius: var(--radius-sm); overflow-x: auto; font-size: 13px; line-height: 1.6;
}
pre code { font-family: var(--mono); background: none; padding: 0; color: inherit; }

/* ---------- part cards (index) ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; margin: 28px 0; }
.card {
  position: relative; display: block; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--hair-soft);
  border-radius: var(--radius); padding: 22px 22px 22px 24px;
  transition: transform 0.18s, box-shadow 0.18s, border-color 0.18s;
  overflow: hidden;
}
.card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--accent); opacity: 0.55; transition: opacity 0.18s;
}
.card.c-vad::before { background: var(--st-vad); }
.card.c-stt::before { background: var(--st-stt); }
.card.c-llm::before { background: var(--st-llm); }
.card.c-tts::before { background: var(--st-tts); }
.card.c-net::before { background: var(--st-net); }
.card:hover { transform: translateY(-3px); box-shadow: 0 14px 30px rgba(8,18,30,0.10); border-color: var(--accent-line); }
.card:hover::before { opacity: 1; }
.card .num { font-family: var(--mono); font-size: 12px; color: var(--accent); font-weight: 600; }
.card h3 { margin: 8px 0 6px; font-size: 17px; letter-spacing: -0.01em; }
.card p { font-size: 14px; color: var(--ink-3); margin: 0; }
.card .meta { margin-top: 12px; font-size: 12px; color: var(--ink-3); }

/* ---------- turn pipeline strip (index) ---------- */
.pipeline { display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch; margin: 22px 0; justify-content: center; }
.pstage {
  flex: 1 1 110px; min-width: 100px; max-width: 150px;
  border: 1px solid var(--hair); border-radius: var(--radius-sm);
  padding: 12px 10px; background: var(--surface); text-align: center;
  cursor: pointer; transition: border-color 0.15s, background 0.15s, transform 0.15s;
  border-top: 3px solid var(--hair);
}
.pstage.g-vad { border-top-color: var(--st-vad); }
.pstage.g-stt { border-top-color: var(--st-stt); }
.pstage.g-llm { border-top-color: var(--st-llm); }
.pstage.g-tts { border-top-color: var(--st-tts); }
.pstage.g-net { border-top-color: var(--st-net); }
.pstage:hover, .pstage.active { background: var(--accent-soft); transform: translateY(-2px); }
.pstage .pname { font-size: 13px; font-weight: 600; }
.pstage .pcost { font-family: var(--mono); font-size: 11px; color: var(--ink-3); margin-top: 3px; }
.pipeline-detail {
  margin-top: 8px; padding: 16px 18px; border-radius: var(--radius-sm);
  background: var(--accent-soft); font-size: 14px; color: var(--ink-2); min-height: 48px;
}
.pipeline-detail a { color: var(--accent); font-weight: 600; text-decoration: none; }

/* ---------- prev/next footer ---------- */
.partfoot {
  display: flex; justify-content: space-between; gap: 12px;
  margin: 40px 0 64px; padding-top: 24px; border-top: 1px solid var(--hair-soft);
}
.partfoot a {
  text-decoration: none; color: var(--ink); font-size: 14px;
  border: 1px solid var(--hair); border-radius: var(--radius-sm);
  padding: 12px 18px; transition: border-color 0.15s, background 0.15s; flex: 1; max-width: 48%;
}
.partfoot a:hover { border-color: var(--accent); background: var(--accent-soft); }
.partfoot a.next { text-align: right; }
.partfoot a .lbl { display: block; font-size: 11px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.03em; }
.partfoot a.disabled { opacity: 0.4; pointer-events: none; }

/* ---------- footer ---------- */
.sitefoot { padding: 40px 24px 64px; text-align: center; color: var(--ink-3); font-size: 13px; border-top: 1px solid var(--hair-soft); }

/* ---------- scroll reveal ---------- */
.reveal { opacity: 0; transform: translateY(16px); transition: opacity 0.5s ease, transform 0.5s ease; }
.reveal.in { opacity: 1; transform: none; }
@media (prefers-reduced-motion: reduce) {
  .reveal { opacity: 1; transform: none; transition: none; }
  html { scroll-behavior: auto; }
}

@media (max-width: 640px) {
  .topnav { gap: 12px; padding: 0 14px; }
  .step { padding: 20px 18px 20px 20px; }
  .partfoot { flex-direction: column; }
  .partfoot a { max-width: 100%; }
}
