/* ============================================
   Design Tokens
   ============================================ */
:root {
  --bg: #fbfaf7;
  --bg-elevated: #ffffff;
  --ink: #15212e;
  --ink-soft: #3a4a5c;
  --ink-muted: #6b7785;
  --ink-faint: #a4adb8;
  --rule: #e3e0d8;
  --rule-strong: #c9c4b6;
  --upstream: #1f3a52;
  --upstream-soft: #2a5070;
  --midmarket: #4a7396;
  --downstream: #7aa6cc;
  --downstream-soft: #a4c2dc;
  --feedback: #16a085;
  --feedback-soft: #2bbfa1;
  --accent: #c8392c;
  --accent-soft: #e25646;
  --accent-glow: rgba(200, 57, 44, 0.18);
  --warn: #d18b1f;
  --good: #16a085;
  --serif: 'Newsreader', Georgia, 'Times New Roman', serif;
  --sans: 'Inter', -apple-system, 'Segoe UI', Helvetica, Arial, sans-serif;
  --mono: 'JetBrains Mono', 'Fira Code', 'SF Mono', Consolas, monospace;
  --max-width: 920px;
  --shadow-sm: 0 1px 3px rgba(21,33,46,0.06);
  --shadow-md: 0 4px 12px rgba(21,33,46,0.08);
  --shadow-lg: 0 8px 28px rgba(21,33,46,0.10);
}

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

html { scroll-behavior: smooth; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--bg);
}

/* ============================================
   Typography
   ============================================ */
h1, h2, h3, h4 {
  font-family: var(--serif);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h2 { font-size: 2.6rem; }
h3 { font-size: 1.5rem; }
h4 { font-size: 1.15rem; }

a { color: var(--upstream); text-decoration: none; transition: color 0.2s; }
a:hover { color: var(--accent); }

::selection { background: var(--accent-glow); color: var(--ink); }

/* ============================================
   Navigation Bar
   ============================================ */
.nav-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  height: 56px;
  background: rgba(251,250,247,0.88);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--rule);
}

.nav-inner {
  max-width: 1280px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  height: 100%; padding: 0 32px;
}

.nav-title {
  font-family: var(--serif);
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--ink);
  line-height: 1.25;
}

.nav-title .muted {
  font-family: var(--sans);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-muted);
}

.nav-progress {
  position: absolute; bottom: 0; left: 0; height: 2px;
  background: linear-gradient(90deg, var(--upstream), var(--accent));
  width: 0%; transition: width 0.1s linear;
}

/* ============================================
   Chapter Rail (left sidebar)
   ============================================ */
.chapter-rail {
  position: fixed; left: 28px; top: 50%; transform: translateY(-50%);
  z-index: 90;
  list-style: none;
}

.chapter-rail li {
  position: relative;
  margin-bottom: 18px;
}

.chapter-rail .dot {
  display: block; width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--rule-strong);
  cursor: pointer;
  transition: all 0.25s ease;
}

.chapter-rail .dot.active {
  background: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
  transform: scale(1.35);
}

.chapter-rail .label {
  position: absolute; left: 20px; top: -3px;
  font-family: var(--sans);
  font-size: 0.65rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-muted);
  white-space: nowrap;
  opacity: 0;
  transform: translateX(-8px);
  transition: all 0.2s ease;
  pointer-events: none;
}

.chapter-rail li:hover .label,
.chapter-rail li.active .label {
  opacity: 1; transform: translateX(0);
}

/* ============================================
   Chapter Sections
   ============================================ */
.chapter {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 7rem 32px 6rem;
  border-bottom: 1px solid var(--rule);
}

.chapter-num {
  font-family: var(--sans);
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 1rem;
}

.lede {
  font-size: 1.18rem;
  color: var(--ink-soft);
  max-width: 56ch;
  line-height: 1.55;
  margin-bottom: 2rem;
}

/* ============================================
   Cover Section
   ============================================ */
.cover {
  max-width: none;
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  position: relative;
  padding: 7rem 32px;
  border-bottom: 1px solid var(--rule);
}

.cover-bg {
  position: absolute; inset: 0; pointer-events: none;
  background:
    radial-gradient(ellipse 600px 500px at 15% 30%, rgba(200,57,44,0.06), transparent),
    radial-gradient(ellipse 500px 400px at 85% 60%, rgba(31,58,82,0.08), transparent),
    radial-gradient(ellipse 400px 350px at 50% 80%, rgba(22,160,133,0.05), transparent);
}

.cover-inner {
  max-width: var(--max-width);
  position: relative;
  z-index: 1;
}

.cover-eyebrow {
  font-family: var(--sans);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 1.4rem;
}

.cover-title {
  font-size: clamp(2.6rem, 6vw, 4.6rem);
  font-weight: 600;
  line-height: 1.08;
  margin-bottom: 1.4rem;
  max-width: 18ch;
}

.cover-title em {
  color: var(--accent);
  font-style: italic;
}

.cover-subtitle {
  font-size: 1.4rem;
  font-style: italic;
  color: var(--ink-soft);
  margin-bottom: 1.4rem;
}

.cover-byline {
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--ink-soft);
  line-height: 1.6;
  margin-bottom: 2.5rem;
}

.cover-byline .muted {
  color: var(--ink-muted);
}

/* Cover Abstract */
.cover-abstract {
  background: var(--bg);
  border-left: 4px solid var(--warn);
  padding: 2rem 2.2rem;
  margin-bottom: 3rem;
  border-radius: 4px;
}

.cover-abstract-label {
  font-family: var(--sans);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--warn);
  margin-bottom: 1rem;
}

.cover-abstract p {
  font-size: 1.02rem;
  line-height: 1.65;
  color: var(--ink-soft);
  margin-bottom: 1rem;
}

.cover-abstract p:last-child { margin-bottom: 0; }

/* Cover Pillars */
.cover-pillars {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.pillar {
  background: var(--bg-elevated);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 1.6rem 1.5rem;
  position: relative;
  transition: all 0.25s ease;
  box-shadow: var(--shadow-sm);
}

.pillar:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
}

.pillar-num {
  position: absolute; top: -0.6rem; right: 1rem;
  font-family: var(--serif);
  font-size: 2.4rem;
  font-style: italic;
  font-weight: 600;
  color: var(--rule-strong);
  line-height: 1;
}

.pillar-title {
  font-family: var(--serif);
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: var(--ink);
}

.pillar p {
  font-size: 0.88rem;
  color: var(--ink-muted);
  line-height: 1.5;
}

/* ============================================
   Plain English Card
   ============================================ */
.plain-card {
  background: var(--bg);
  border-left: 4px solid var(--warn);
  padding: 1.8rem 2rem;
  margin: 2rem 0;
  border-radius: 4px;
}

.plain-label {
  font-family: var(--sans);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--warn);
  margin-bottom: 0.8rem;
}

.plain-card p {
  font-size: 0.98rem;
  line-height: 1.65;
  color: var(--ink-soft);
  margin-bottom: 0.8rem;
}

.plain-card p:last-child { margin-bottom: 0; }

/* ============================================
   Equation Block
   ============================================ */
.equation-block {
  background: var(--bg-elevated);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 1.6rem 2rem;
  margin: 2rem 0;
  overflow-x: auto;
}

.equation-block .eq-label {
  font-family: var(--sans);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-bottom: 0.8rem;
}

/* ============================================
   Callout
   ============================================ */
.callout {
  background: rgba(200,57,44,0.04);
  border-left: 4px solid var(--accent);
  padding: 1.6rem 2rem;
  margin: 2.5rem 0;
  border-radius: 0 4px 4px 0;
}

.callout-label {
  font-family: var(--sans);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.6rem;
}

.callout-body {
  font-size: 1rem;
  font-style: italic;
  color: var(--ink-soft);
  line-height: 1.6;
}

/* ============================================
   Prose
   ============================================ */
.prose {
  max-width: 64ch;
  margin-bottom: 2rem;
}

.prose p {
  margin-bottom: 1rem;
  color: var(--ink-soft);
}

.prose p:last-child { margin-bottom: 0; }

/* ============================================
   Interactive Section
   ============================================ */
.interactive-section {
  margin: 3rem 0;
}

.interactive-section h3 {
  margin-bottom: 0.5rem;
}

.interactive-section .hint {
  font-family: var(--sans);
  font-size: 0.72rem;
  color: var(--ink-faint);
  margin-bottom: 1.5rem;
}

.viz-container {
  width: 100%;
  min-height: 360px;
  margin-bottom: 1rem;
}

.viz-container.tall {
  min-height: 480px;
}

/* ============================================
   Controls (sliders, buttons, cards)
   ============================================ */
.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1.2rem;
  margin-bottom: 1.5rem;
  align-items: flex-end;
}

.control-group {
  flex: 1 1 200px;
  min-width: 180px;
}

.control-group label {
  display: block;
  font-family: var(--sans);
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.4rem;
}

.control-group .val {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--ink);
  float: right;
}

input[type="range"] {
  -webkit-appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--rule);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--upstream);
  cursor: pointer;
  transition: background 0.2s;
}

input[type="range"]::-webkit-slider-thumb:hover {
  background: var(--accent);
}

.btn-row {
  display: flex; gap: 0.8rem; flex-wrap: wrap;
  margin-bottom: 1.5rem;
}

.btn {
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  padding: 0.55rem 1.3rem;
  border-radius: 4px;
  border: 1px solid var(--rule-strong);
  background: var(--bg-elevated);
  color: var(--ink);
  cursor: pointer;
  transition: all 0.2s;
}

.btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Strategy Cards */
.strategy-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 1.5rem;
}

.strategy-card {
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 1rem 1.2rem;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg-elevated);
}

.strategy-card:hover {
  border-color: var(--midmarket);
  box-shadow: var(--shadow-sm);
}

.strategy-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.strategy-card .sc-title {
  font-family: var(--serif);
  font-size: 0.92rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
}

.strategy-card .sc-desc {
  font-size: 0.78rem;
  color: var(--ink-muted);
  line-height: 1.4;
}

/* Readout cards */
.readout-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-top: 1rem;
}

.readout-card {
  background: var(--bg-elevated);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 1rem 1.2rem;
  text-align: center;
}

.readout-card .rc-label {
  font-family: var(--sans);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-bottom: 0.4rem;
}

.readout-card .rc-value {
  font-family: var(--mono);
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--ink);
}

.readout-card .rc-value.accent { color: var(--accent); }
.readout-card .rc-value.good { color: var(--good); }

/* ============================================
   Chapter Foot (next link)
   ============================================ */
.chapter-foot {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-top: 3rem;
  transition: all 0.25s ease;
}

.chapter-foot .next-arrow {
  display: inline-block;
  transition: transform 0.25s ease;
}

.chapter-foot:hover {
  color: var(--accent);
}

.chapter-foot:hover .next-arrow {
  transform: translateX(5px);
}

/* ============================================
   Grid helpers
   ============================================ */
.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.chart-grid.single {
  grid-template-columns: 1fr;
}

/* ============================================
   Responsive
   ============================================ */
@media (max-width: 1100px) {
  .chapter-rail { display: none; }
}

@media (max-width: 820px) {
  .cover-abstract {
    /* no grid, single column */
  }
  .cover-pillars {
    grid-template-columns: 1fr;
  }
  .chart-grid {
    grid-template-columns: 1fr;
  }
  h2 { font-size: 2rem; }
}

@media (max-width: 600px) {
  body { font-size: 16px; }
  .chapter { padding: 5rem 20px 4rem; }
  .cover { padding: 5rem 20px; }
  .cover-title { font-size: 2.2rem; }
  .controls-row { flex-direction: column; }
  .control-group { min-width: 100%; }
}

/* ============================================
   Plotly overrides
   ============================================ */
.js-plotly-plot .plotly .modebar { top: -8px !important; right: -4px !important; }
.js-plotly-plot .plotly .main-svg { font-family: var(--sans) !important; }
