/* ==========================================================================
   The Blind Tinkerer — Evolution & Natural Selection
   Shared stylesheet. Deep-forest night palette, field-notebook type.
   Colour meanings are fixed across the whole book:
     teal  = environment      amber = mutation / new variation
     leaf  = selection / fit  coral = predator / death / cost
     lilac = chance (drift)   sand  = inheritance / genes / lineages
   ========================================================================== */

:root {
  --bg: #0d1310;
  --bg-2: #111a15;
  --card: #16211b;
  --card-2: #1b2821;
  --canvas: #0a0f0c;
  --border: #263a2e;
  --border-soft: rgba(227, 207, 159, 0.14);

  --text: #e9efe7;
  --text-2: #cdd9cf;
  --muted: #9db0a2;
  --dim: #6d8173;

  --env: #5cc2c9;
  --mut: #f2b54a;
  --sel: #8cc85a;
  --pred: #ef6a57;
  --chance: #b79cf0;
  --gene: #e3cf9f;

  --env-soft: rgba(92, 194, 201, 0.14);
  --mut-soft: rgba(242, 181, 74, 0.14);
  --sel-soft: rgba(140, 200, 90, 0.14);
  --pred-soft: rgba(239, 106, 87, 0.14);
  --chance-soft: rgba(183, 156, 240, 0.14);
  --gene-soft: rgba(227, 207, 159, 0.12);

  --font-display: 'Fraunces', 'Iowan Old Style', Georgia, 'Times New Roman', serif;
  --font-body: 'Source Sans 3', system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Consolas, 'Courier New', monospace;

  --sidebar-w: 286px;
  --prose: 70ch;
  --radius: 14px;
  --radius-sm: 8px;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 70% 45% at 70% -8%, rgba(140, 200, 90, 0.07), transparent 60%),
    radial-gradient(ellipse 55% 40% at 8% 95%, rgba(92, 194, 201, 0.05), transparent 60%),
    var(--bg);
}

a { color: var(--gene); text-decoration: none; border-bottom: 1px solid rgba(227, 207, 159, 0.3); }
a:hover { color: #fff; border-bottom-color: var(--gene); }
:focus-visible { outline: 2px solid var(--mut); outline-offset: 2px; border-radius: 4px; }

/* ---------- Reading progress ---------- */
.reading-progress {
  position: fixed; top: 0; left: 0; height: 3px; width: 0;
  background: linear-gradient(90deg, var(--env), var(--sel), var(--mut));
  z-index: 1000;
}

/* ---------- Layout ---------- */
.book { display: flex; min-height: 100vh; }
.content {
  flex: 1; min-width: 0;
  padding: 2.6rem clamp(1rem, 4vw, 3.6rem) 5rem;
  max-width: calc(var(--prose) + 14rem);
  margin: 0 auto;
}

/* ---------- Mobile top bar ---------- */
.topbar {
  display: none; position: sticky; top: 0; z-index: 90;
  background: rgba(13, 19, 16, 0.94);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1rem; align-items: center; justify-content: space-between; gap: 0.6rem;
}
.topbar .brand-link { font-family: var(--font-display); font-weight: 600; color: var(--text); border: 0; }
.menu-btn {
  font: 600 0.88rem var(--font-body); color: var(--text);
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.42rem 0.8rem; cursor: pointer;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--bg-2); border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  padding: 1.4rem 1rem 2.4rem; z-index: 95;
}
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
.side-brand { display: flex; gap: 0.7rem; align-items: center; color: var(--text); border: 0; padding: 0.2rem 0.3rem 0.9rem; border-bottom: 1px solid var(--border); }
.side-brand:hover { color: var(--text); }
.brand-mark {
  width: 36px; height: 36px; flex-shrink: 0; border-radius: 10px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, rgba(140, 200, 90, 0.25), rgba(92, 194, 201, 0.2));
  border: 1px solid rgba(140, 200, 90, 0.45);
}
.brand-mark svg { width: 22px; height: 22px; }
.brand-title { display: block; font-family: var(--font-display); font-weight: 600; font-size: 1.06rem; line-height: 1.15; }
.brand-sub { display: block; font-size: 0.74rem; color: var(--muted); }
.side-progress { margin: 1rem 0 0.4rem; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0.6rem 0.75rem; }
.side-progress-label { display: flex; justify-content: space-between; font-size: 0.76rem; color: var(--muted); margin-bottom: 0.35rem; }
.track { height: 6px; background: rgba(255, 255, 255, 0.07); border-radius: 99px; overflow: hidden; }
.track-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--sel), var(--mut)); border-radius: 99px; transition: width 0.3s ease; }
.toc-part { font-size: 0.68rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--dim); margin: 1rem 0 0.3rem 0.45rem; }
.toc { list-style: none; margin: 0; padding: 0; }
.toc a {
  display: flex; gap: 0.55rem; align-items: center; border: 0;
  padding: 0.4rem 0.55rem; border-radius: var(--radius-sm);
  font-size: 0.9rem; line-height: 1.3; color: var(--muted);
}
.toc a:hover { background: rgba(140, 200, 90, 0.08); color: var(--text); }
.toc a.active { background: rgba(140, 200, 90, 0.14); color: var(--sel); font-weight: 600; }
.toc-num { font-family: var(--font-mono); font-size: 0.72rem; color: var(--dim); width: 1.4rem; flex-shrink: 0; }
.toc a.active .toc-num { color: var(--sel); }
.toc-check { margin-left: auto; color: var(--sel); font-size: 0.8rem; visibility: hidden; }
.toc a.done .toc-check { visibility: visible; }
.side-home { display: block; margin-top: 1.2rem; font-size: 0.82rem; color: var(--muted); border: 0; padding-left: 0.5rem; }
.backdrop { display: none; }

/* ---------- Chapter header & prose ---------- */
.chapter-head { padding-bottom: 1.6rem; margin-bottom: 2.2rem; border-bottom: 1px solid var(--border); }
.eyebrow { font-size: 0.78rem; font-weight: 700; letter-spacing: 0.13em; text-transform: uppercase; color: var(--sel); margin-bottom: 0.55rem; }
.chapter-head h1, .hero h1 {
  font-family: var(--font-display); font-weight: 600; color: #fff;
  font-size: clamp(2.05rem, 4.6vw, 3.1rem); line-height: 1.1; letter-spacing: -0.015em; margin: 0 0 0.8rem;
}
.subtitle { font-size: 1.16rem; color: var(--muted); max-width: 64ch; margin: 0; line-height: 1.6; }

.content h2 { font-family: var(--font-display); font-weight: 600; font-size: 1.65rem; line-height: 1.25; color: #fff; margin: 2.7rem 0 0.8rem; }
.content h3 { font-size: 1.12rem; color: var(--gene); margin: 1.8rem 0 0.5rem; }
.content p { color: var(--text-2); max-width: var(--prose); margin: 0 0 1.1rem; }
.content ul, .content ol { color: var(--text-2); max-width: var(--prose); padding-left: 1.35rem; margin: 0 0 1.2rem; }
.content li { margin-bottom: 0.4rem; }
.content strong { color: var(--text); }
.content em { color: var(--text); }

.c-env { color: var(--env); font-weight: 600; }
.c-mut { color: var(--mut); font-weight: 600; }
.c-sel { color: var(--sel); font-weight: 600; }
.c-pred { color: var(--pred); font-weight: 600; }
.c-chance { color: var(--chance); font-weight: 600; }
.c-gene { color: var(--gene); font-weight: 600; }
.mono { font-family: var(--font-mono); font-size: 0.92em; }

/* ---------- Callouts ---------- */
.callout {
  max-width: var(--prose); margin: 1.6rem 0;
  background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--sel);
  border-radius: var(--radius-sm); padding: 0.95rem 1.2rem;
}
.callout p:last-child { margin-bottom: 0; }
.callout-label { font-size: 0.74rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--sel); margin-bottom: 0.3rem; }
.callout.principle { background: linear-gradient(90deg, rgba(140, 200, 90, 0.08), var(--card) 45%); }
.callout.principle p { font-size: 1.06rem; color: var(--text); }
.callout.myth { border-left-color: var(--pred); background: linear-gradient(90deg, rgba(239, 106, 87, 0.07), var(--card) 45%); }
.callout.myth .callout-label { color: var(--pred); }
.callout.note { border-left-color: var(--env); }
.callout.note .callout-label { color: var(--env); }
.callout.chance { border-left-color: var(--chance); }
.callout.chance .callout-label { color: var(--chance); }
.callout.history { border-left-color: var(--gene); }
.callout.history .callout-label { color: var(--gene); }
.callout.formula { border-left-color: var(--mut); }
.callout.formula .callout-label { color: var(--mut); }
.formula-line { font-family: var(--font-mono); font-size: 1.05rem; color: #fff; text-align: center; padding: 0.4rem 0; }

/* ---------- Interactive figures ---------- */
.figure {
  margin: 2rem 0 2.3rem; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; box-shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
}
.fig-head {
  display: flex; flex-wrap: wrap; gap: 0.35rem 0.9rem; align-items: baseline; justify-content: space-between;
  padding: 0.75rem 1.05rem; border-bottom: 1px solid var(--border); background: rgba(255, 255, 255, 0.02);
}
.fig-title { font-weight: 700; color: var(--text); font-size: 0.98rem; display: flex; gap: 0.55rem; align-items: baseline; flex-wrap: wrap; }
.fig-badge {
  font: 600 0.68rem var(--font-mono); letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--sel); background: var(--sel-soft); border: 1px solid rgba(140, 200, 90, 0.3);
  border-radius: 99px; padding: 0.1rem 0.5rem; white-space: nowrap;
}
.fig-hint { font-size: 0.84rem; color: var(--muted); }
.fig-stage { background: var(--canvas); line-height: 0; position: relative; }
.fig-stage canvas { display: block; width: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }
.fig-stage canvas.scrollable { touch-action: pan-y; }
.fig-controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.7rem 1.2rem;
  padding: 0.8rem 1.05rem; background: var(--bg-2); border-top: 1px solid var(--border);
}
.ctrl { display: inline-flex; align-items: center; gap: 0.5rem; font-size: 0.88rem; color: var(--muted); flex-wrap: nowrap; }
.ctrl label { white-space: nowrap; }
.ctrl .val { font-family: var(--font-mono); font-size: 0.82rem; color: var(--mut); min-width: 3.4rem; }
.btn-row { display: inline-flex; flex-wrap: wrap; gap: 0.4rem; }
.fig-readout {
  padding: 0.6rem 1.05rem; font-family: var(--font-mono); font-size: 0.84rem; line-height: 1.55; color: var(--text);
  background: rgba(255, 255, 255, 0.025); border-top: 1px solid var(--border);
}
.fig-readout .k { color: var(--muted); }
.fig-caption { padding: 0.7rem 1.05rem 0.85rem; font-size: 0.9rem; color: var(--muted); border-top: 1px solid rgba(255, 255, 255, 0.04); line-height: 1.55; }
.fig-caption strong { color: var(--text); }
.legend { display: flex; flex-wrap: wrap; gap: 0.3rem 0.95rem; padding: 0.5rem 1.05rem; font-size: 0.8rem; color: var(--muted); border-top: 1px solid rgba(255, 255, 255, 0.04); }
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.swatch { width: 11px; height: 11px; border-radius: 3px; display: inline-block; flex-shrink: 0; }

input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 140px; height: 6px; border-radius: 99px;
  background: var(--border); cursor: pointer; margin: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 17px; height: 17px; border-radius: 50%;
  background: var(--gene); border: 2px solid var(--bg); box-shadow: 0 0 0 1px var(--gene);
}
input[type="range"]::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: var(--gene); border: 2px solid var(--bg); }
input[type="range"]:focus-visible { outline: 2px solid var(--mut); outline-offset: 4px; }
input[type="text"] {
  font: 500 0.88rem var(--font-mono); color: var(--text); background: var(--canvas);
  border: 1px solid var(--border); border-radius: 6px; padding: 0.35rem 0.55rem; width: 17rem; max-width: 100%;
  text-transform: uppercase;
}

.btn {
  font: 600 0.86rem var(--font-body); cursor: pointer;
  color: var(--text); background: var(--card-2); border: 1px solid #335040; border-radius: var(--radius-sm);
  padding: 0.42rem 0.85rem; display: inline-flex; align-items: center; gap: 0.35rem; line-height: 1.3;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: #22342a; border-color: var(--sel); }
.btn.primary { background: var(--sel); color: #0c140e; border-color: var(--sel); }
.btn.primary:hover { background: #a3d978; }
.btn[aria-pressed="true"] { background: rgba(140, 200, 90, 0.2); border-color: var(--sel); color: #fff; }
.btn.env[aria-pressed="true"] { background: var(--env-soft); border-color: var(--env); }
.btn.warn { border-color: rgba(239, 106, 87, 0.55); }
.btn.warn:hover { border-color: var(--pred); background: rgba(239, 106, 87, 0.15); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-lg { padding: 0.7rem 1.3rem; font-size: 0.98rem; border-radius: 10px; }

/* ---------- Intuition check ---------- */
.quiz {
  max-width: var(--prose); margin: 2.2rem 0; padding: 1.15rem 1.25rem 1.2rem;
  background: linear-gradient(160deg, rgba(242, 181, 74, 0.07), var(--card) 55%);
  border: 1px solid rgba(242, 181, 74, 0.32); border-radius: var(--radius);
}
.quiz-label { font: 600 0.72rem var(--font-mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--mut); }
.quiz h3 { margin: 0.25rem 0 0.6rem; color: #fff; font-size: 1.08rem; }
.quiz-options { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 0.6rem; }
.quiz-option {
  text-align: left; font: 500 0.97rem var(--font-body); color: var(--text-2); cursor: pointer;
  background: var(--canvas); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0.65rem 0.85rem;
}
.quiz-option:hover { border-color: var(--mut); color: #fff; }
.quiz-option.correct { border-color: var(--sel); background: var(--sel-soft); color: #fff; }
.quiz-option.wrong { border-color: var(--pred); background: var(--pred-soft); }
.quiz-feedback { margin-top: 0.75rem; font-size: 0.95rem; color: var(--text-2); min-height: 1.4em; }
.quiz-feedback.ok { color: #cfe9b8; }
.quiz-feedback.no { color: #f6c1b8; }

/* ---------- Connections & recap ---------- */
.connections {
  max-width: var(--prose); margin: 2.4rem 0 1.4rem; padding: 0.95rem 1.2rem;
  border: 1px dashed rgba(227, 207, 159, 0.35); border-radius: var(--radius-sm); background: var(--gene-soft);
}
.connections h2 { font-size: 1.05rem; margin: 0 0 0.35rem; font-family: var(--font-body); font-weight: 700; color: var(--gene); letter-spacing: 0.02em; }
.connections p, .connections li { font-size: 0.95rem; }
.connections ul { margin-bottom: 0; }
.recap {
  max-width: var(--prose); margin: 2.4rem 0 0; padding: 1.1rem 1.3rem;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
}
.recap h2 { margin-top: 0; font-size: 1.35rem; }
.recap ul { margin-bottom: 0; }

/* ---------- Chapter footer ---------- */
.chapter-footer { margin-top: 3rem; padding-top: 1.8rem; border-top: 1px solid var(--border); }
.complete-bar {
  display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center; justify-content: space-between;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.95rem 1.2rem; margin-bottom: 1.2rem;
}
.complete-bar p { margin: 0; color: var(--muted); font-size: 0.95rem; }
.btn.done { background: rgba(140, 200, 90, 0.2); border-color: var(--sel); color: #fff; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.pager a {
  display: flex; flex-direction: column; gap: 0.15rem; padding: 0.95rem 1.15rem; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--card); color: var(--text);
}
.pager a:hover { border-color: var(--sel); }
.pager .next { text-align: right; align-items: flex-end; }
.pager .dir { font-size: 0.72rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--dim); }
.pager .ttl { font-weight: 600; }

/* ---------- Home page ---------- */
.hero { padding: 0.6rem 0 2.2rem; border-bottom: 1px solid var(--border); margin-bottom: 2rem; }
.hero-grid { display: grid; grid-template-columns: 0.95fr 1.05fr; gap: 2rem; align-items: center; }
.hero h1 { font-size: clamp(2.4rem, 5.4vw, 3.7rem); }
.hero-lead { font-size: 1.14rem; color: var(--text-2); margin: 0 0 1.4rem; line-height: 1.6; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 0.7rem; }
.hero-actions a { border: 0; }
.hero .figure { margin: 0; }
.progress-banner {
  display: flex; flex-wrap: wrap; gap: 0.9rem 1.4rem; align-items: center; justify-content: space-between;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; margin: 0 0 1.8rem;
}
.progress-banner .track { flex: 1 1 180px; height: 8px; }
.progress-banner a.btn { border-bottom: 1px solid var(--sel); }
.legend-box {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem 1.2rem; margin-bottom: 1.6rem;
}
.legend-box h2 { font-size: 1.05rem; margin: 0 0 0.5rem; font-family: var(--font-body); font-weight: 700; color: var(--text); }
.legend-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 0.35rem 1.2rem; font-size: 0.92rem; color: var(--text-2); }
.legend-grid span { display: inline-flex; align-items: center; gap: 0.5rem; }
.part-block { margin: 2.2rem 0 1rem; }
.part-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.7rem; margin-bottom: 0.9rem; }
.part-tag { font: 600 0.74rem var(--font-mono); color: var(--mut); border: 1px solid rgba(242, 181, 74, 0.35); background: var(--mut-soft); border-radius: 99px; padding: 0.12rem 0.55rem; }
.part-head h2 { margin: 0; font-size: 1.45rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(255px, 1fr)); gap: 0.95rem; }
.card {
  display: flex; flex-direction: column; gap: 0.4rem; padding: 1.05rem 1.15rem; color: var(--text);
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.card:hover { border-color: var(--sel); transform: translateY(-2px); color: var(--text); }
.card-top { display: flex; justify-content: space-between; font: 600 0.76rem var(--font-mono); color: var(--sel); }
.card-done { color: var(--sel); }
.card h3 { margin: 0; font-family: var(--font-display); font-weight: 600; font-size: 1.14rem; color: #fff; }
.card p { margin: 0; font-size: 0.92rem; color: var(--muted); line-height: 1.5; }
.card-sim { margin-top: auto; padding-top: 0.35rem; font-size: 0.8rem; color: var(--env); }
.site-foot { margin-top: 3rem; padding-top: 1.2rem; border-top: 1px solid var(--border); font-size: 0.85rem; color: var(--dim); }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .topbar { display: flex; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; transform: translateX(-100%);
    transition: transform 0.25s ease; box-shadow: 12px 0 36px rgba(0, 0, 0, 0.6);
  }
  .sidebar.open { transform: none; }
  .backdrop.open { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 92; }
  .hero-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  html { font-size: 16px; }
  .content { padding-top: 1.6rem; }
  .pager { grid-template-columns: 1fr; }
  .fig-controls { gap: 0.6rem 0.9rem; padding: 0.7rem 0.8rem; }
  .fig-head, .fig-readout, .fig-caption, .legend { padding-left: 0.8rem; padding-right: 0.8rem; }
  .ctrl { flex-wrap: wrap; }
  input[type="range"] { width: 120px; }
  .content h2 { font-size: 1.42rem; }
}
