/* ==========================================================================
   Seeing Machines — A Visual, Jargon-Free Book on Classical Machine Learning
   3Blue1Brown-inspired dark geometric design system
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,500;0,6..72,600;1,6..72,400&display=swap');

:root {
  /* Core 3Blue1Brown-inspired palette */
  --bg: #0b0e17;
  --bg-elevated: #111625;
  --bg-card: #151c2f;
  --bg-canvas: #090c14;
  --border: rgba(148, 163, 184, 0.16);
  --border-strong: rgba(148, 163, 184, 0.3);

  /* Text */
  --text: #e8edf5;
  --text-soft: #b2bfd4;
  --text-muted: #7888a3;

  /* Signature geometric accents */
  --blue: #58c4dd;
  --blue-soft: rgba(88, 196, 221, 0.15);
  --gold: #f5c542;
  --gold-soft: rgba(245, 197, 66, 0.15);
  --red: #fc6255;
  --red-soft: rgba(252, 98, 85, 0.15);
  --green: #83c167;
  --green-soft: rgba(131, 193, 103, 0.15);
  --purple: #b189d4;
  --purple-soft: rgba(177, 137, 212, 0.15);

  /* Typography */
  --font-serif: 'Newsreader', Georgia, 'Times New Roman', serif;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  /* Layout */
  --max-width: 820px;
  --nav-height: 62px;
  --radius: 14px;
  --radius-sm: 8px;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(88, 196, 221, 0.08), transparent 60%),
    radial-gradient(ellipse 60% 40% at 90% 20%, rgba(245, 197, 66, 0.04), transparent 55%),
    var(--bg);
  color: var(--text);
  line-height: 1.75;
}

a {
  color: var(--blue);
  text-decoration: none;
  transition: color 0.2s ease, opacity 0.2s ease;
}

a:hover {
  color: var(--gold);
}

/* ---------- Top Navigation Bar ---------- */
.book-nav {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--nav-height);
  background: rgba(11, 14, 23, 0.86);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

.book-nav-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.toc-toggle-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  font-size: 0.86rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: border-color 0.2s, background 0.2s;
}

.toc-toggle-btn:hover {
  border-color: var(--blue);
  background: rgba(88, 196, 221, 0.1);
}

.book-brand {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.book-brand span {
  color: var(--blue);
}

.book-nav-center {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: none;
}

@media (min-width: 760px) {
  .book-nav-center {
    display: block;
  }
}

.book-nav-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-pill {
  padding: 6px 13px;
  border-radius: 999px;
  font-size: 0.83rem;
  font-weight: 500;
  border: 1px solid var(--border);
  color: var(--text-soft);
  background: transparent;
  transition: all 0.2s ease;
}

.nav-pill:hover {
  border-color: var(--blue);
  color: var(--text);
  background: var(--blue-soft);
}

.nav-pill.disabled {
  opacity: 0.35;
  pointer-events: none;
}

.reading-progress-track {
  position: fixed;
  top: var(--nav-height);
  left: 0;
  right: 0;
  height: 2px;
  background: transparent;
  z-index: 101;
}

.reading-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--blue), var(--gold));
  transition: width 0.1s linear;
}

/* ---------- Slide-out Table of Contents Drawer ---------- */
.toc-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 13, 0.65);
  backdrop-filter: blur(4px);
  z-index: 199;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.toc-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.toc-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(360px, 88vw);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  z-index: 200;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.toc-drawer.open {
  transform: translateX(0);
}

.toc-drawer-header {
  padding: 20px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.toc-drawer-header h3 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.2rem;
  color: var(--text);
}

.toc-close-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-soft);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  display: grid;
  place-items: center;
}

.toc-close-btn:hover {
  border-color: var(--red);
  color: var(--red);
}

.toc-drawer-list {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 30px;
}

.toc-part-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--gold);
  margin: 18px 8px 6px;
  font-weight: 600;
}

.toc-part-label:first-child {
  margin-top: 4px;
}

.toc-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 10px;
  border-radius: 9px;
  color: var(--text-soft);
  font-size: 0.9rem;
  transition: background 0.18s, color 0.18s;
}

.toc-item:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}

.toc-item.active {
  background: var(--blue-soft);
  color: var(--blue);
  font-weight: 600;
}

.toc-item-num {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  display: grid;
  place-items: center;
  font-size: 0.76rem;
  font-weight: 600;
  flex-shrink: 0;
}

.toc-item.visited .toc-item-num {
  background: var(--green-soft);
  color: var(--green);
}

.toc-item.active .toc-item-num {
  background: var(--blue);
  color: #090c14;
}

/* ---------- Main Chapter Container ---------- */
.chapter-container {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 52px 24px 96px;
}

.chapter-header {
  margin-bottom: 42px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 30px;
}

.chapter-kicker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--blue);
  margin-bottom: 12px;
}

.chapter-title {
  font-family: var(--font-serif);
  font-size: clamp(2.1rem, 4.5vw, 3.1rem);
  line-height: 1.16;
  font-weight: 600;
  margin: 0 0 14px;
  letter-spacing: -0.02em;
  color: var(--text);
}

.chapter-subtitle {
  font-family: var(--font-serif);
  font-size: 1.24rem;
  line-height: 1.55;
  color: var(--text-soft);
  margin: 0;
  font-style: italic;
}

/* ---------- Narrative Prose ---------- */
.prose p {
  font-family: var(--font-serif);
  font-size: 1.18rem;
  line-height: 1.82;
  color: #dce4f2;
  margin: 0 0 22px;
}

.prose h2 {
  font-family: var(--font-serif);
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--text);
  margin: 48px 0 16px;
  letter-spacing: -0.01em;
}

.prose h3 {
  font-family: var(--font-sans);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--gold);
  margin: 30px 0 10px;
  letter-spacing: 0.01em;
}

.prose strong {
  color: #ffffff;
  font-weight: 600;
}

.prose em {
  color: var(--gold);
  font-style: italic;
}

.prose ul, .prose ol {
  font-family: var(--font-serif);
  font-size: 1.14rem;
  color: #dce4f2;
  padding-left: 24px;
  margin: 0 0 24px;
}

.prose li {
  margin-bottom: 10px;
}

/* Colored inline highlights matching canvas elements */
.hl-blue { color: var(--blue); font-weight: 600; }
.hl-gold { color: var(--gold); font-weight: 600; }
.hl-red { color: var(--red); font-weight: 600; }
.hl-green { color: var(--green); font-weight: 600; }
.hl-purple { color: var(--purple); font-weight: 600; }

/* ---------- Callout Boxes ---------- */
.intuition-box {
  background: linear-gradient(135deg, rgba(88, 196, 221, 0.09), rgba(21, 28, 47, 0.75));
  border: 1px solid rgba(88, 196, 221, 0.28);
  border-left: 4px solid var(--blue);
  border-radius: var(--radius);
  padding: 20px 24px;
  margin: 28px 0;
}

.intuition-box .box-label {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 700;
  color: var(--blue);
  margin-bottom: 8px;
  display: block;
}

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

.key-takeaway {
  background: linear-gradient(135deg, rgba(245, 197, 66, 0.09), rgba(21, 28, 47, 0.75));
  border: 1px solid rgba(245, 197, 66, 0.28);
  border-left: 4px solid var(--gold);
  border-radius: var(--radius);
  padding: 22px 26px;
  margin: 36px 0;
}

.key-takeaway .box-label {
  font-family: var(--font-sans);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 700;
  color: var(--gold);
  margin-bottom: 8px;
  display: block;
}

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

/* ---------- Interactive Sandbox Component ---------- */
.interactive-box {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  margin: 34px 0;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

.interactive-header {
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(255, 255, 255, 0.02);
}

.interactive-title {
  font-family: var(--font-sans);
  font-size: 0.96rem;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.interactive-badge {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue);
}

.interactive-hint {
  font-size: 0.84rem;
  color: var(--text-muted);
}

.canvas-wrap {
  position: relative;
  background: var(--bg-canvas);
  width: 100%;
  overflow: hidden;
}

.canvas-wrap canvas {
  display: block;
  width: 100%;
  cursor: crosshair;
  touch-action: none;
}

.controls-bar {
  padding: 16px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px 22px;
  background: rgba(11, 14, 23, 0.55);
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 160px;
  flex: 1 1 180px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.82rem;
  color: var(--text-soft);
  font-weight: 500;
}

.control-value {
  color: var(--gold);
  font-weight: 600;
}

/* Custom Range Slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.25);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--blue);
  border: 2px solid #090c14;
  box-shadow: 0 0 12px rgba(88, 196, 221, 0.6);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
}

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

/* Interactive Buttons */
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.btn {
  font-family: var(--font-sans);
  font-size: 0.84rem;
  font-weight: 600;
  padding: 8px 15px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  cursor: pointer;
  transition: all 0.18s ease;
}

.btn:hover {
  border-color: var(--blue);
  background: var(--blue-soft);
}

.btn.primary {
  background: var(--blue);
  color: #090c14;
  border-color: var(--blue);
}

.btn.primary:hover {
  background: #77d4e8;
}

.btn.gold {
  background: var(--gold);
  color: #090c14;
  border-color: var(--gold);
}

.btn.gold:hover {
  background: #f9d466;
}

.btn.active {
  background: var(--blue-soft);
  border-color: var(--blue);
  color: var(--blue);
}

/* Live relational readout strip */
.meter-strip {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  background: rgba(9, 12, 20, 0.65);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  font-size: 0.88rem;
  color: var(--text-soft);
}

.meter-bar-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 220px;
}

.meter-bar-track {
  flex: 1;
  height: 10px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.15);
  overflow: hidden;
}

.meter-bar-fill {
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--green), var(--gold), var(--red));
  transition: width 0.15s ease;
}

/* ---------- "In the Wild" Real-World Term Toggle ---------- */
.wild-section {
  margin: 48px 0 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
  overflow: hidden;
}

.wild-bar {
  padding: 18px 22px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.wild-bar-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.wild-bar-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.wild-bar-desc {
  font-size: 0.83rem;
  color: var(--text-muted);
}

/* Switch styling */
.wild-switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--blue);
}

.wild-switch input {
  display: none;
}

.wild-slider {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.25);
  position: relative;
  transition: background 0.22s ease;
}

.wild-slider::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #e8edf5;
  transition: transform 0.22s ease, background 0.22s ease;
}

.wild-switch input:checked + .wild-slider {
  background: var(--purple);
}

.wild-switch input:checked + .wild-slider::after {
  transform: translateX(20px);
  background: #ffffff;
}

.wild-content {
  display: none;
  padding: 0 22px 22px;
  border-top: 1px solid var(--border);
  background: rgba(177, 137, 212, 0.05);
}

.wild-content.revealed {
  display: block;
  animation: fadeSlideIn 0.25s ease;
}

@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.wild-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 14px;
  margin-top: 18px;
}

.wild-card {
  background: var(--bg-card);
  border: 1px solid rgba(177, 137, 212, 0.25);
  border-radius: 10px;
  padding: 14px 16px;
}

.wild-plain {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.wild-term {
  font-size: 1rem;
  font-weight: 700;
  color: var(--purple);
  margin-bottom: 6px;
}

.wild-note {
  font-size: 0.85rem;
  color: var(--text-soft);
  line-height: 1.5;
}

/* ---------- Chapter Footer Navigation ---------- */
.chapter-footer {
  margin-top: 56px;
  padding-top: 28px;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

@media (max-width: 600px) {
  .chapter-footer {
    grid-template-columns: 1fr;
  }
}

.footer-nav-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 18px 20px;
  border-radius: var(--radius);
  background: var(--bg-card);
  border: 1px solid var(--border);
  transition: border-color 0.2s, transform 0.2s, background 0.2s;
}

.footer-nav-card:hover {
  border-color: var(--blue);
  background: rgba(88, 196, 221, 0.07);
  transform: translateY(-2px);
}

.footer-nav-card.next {
  text-align: right;
  align-items: flex-end;
}

.footer-nav-dir {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  font-weight: 600;
}

.footer-nav-title {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
}

/* ---------- Home Page Specifics ---------- */
.hero {
  max-width: 1020px;
  margin: 0 auto;
  padding: 58px 24px 40px;
  text-align: center;
}

.hero-badge {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid rgba(88, 196, 221, 0.35);
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 18px;
}

.hero h1 {
  font-family: var(--font-serif);
  font-size: clamp(2.6rem, 6vw, 4.2rem);
  line-height: 1.08;
  margin: 0 0 18px;
  letter-spacing: -0.025em;
}

.hero h1 span {
  background: linear-gradient(95deg, var(--blue), var(--gold));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.hero-lead {
  font-family: var(--font-serif);
  font-size: 1.3rem;
  color: var(--text-soft);
  max-width: 680px;
  margin: 0 auto 28px;
  line-height: 1.6;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px;
  margin-bottom: 40px;
}

.hero-btn {
  padding: 13px 26px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.96rem;
  transition: transform 0.18s, box-shadow 0.18s;
}

.hero-btn.primary {
  background: var(--blue);
  color: #090c14;
  box-shadow: 0 10px 28px rgba(88, 196, 221, 0.25);
}

.hero-btn.primary:hover {
  transform: translateY(-2px);
  color: #090c14;
  background: #77d4e8;
}

.hero-btn.secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border-strong);
}

.hero-btn.secondary:hover {
  border-color: var(--gold);
  color: var(--gold);
}

.principles-strip {
  max-width: 1020px;
  margin: 0 auto 50px;
  padding: 0 24px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.principle-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.principle-icon {
  font-size: 1.4rem;
  margin-bottom: 8px;
}

.principle-card h3 {
  margin: 0 0 6px;
  font-size: 1rem;
  color: var(--text);
}

.principle-card p {
  margin: 0;
  font-size: 0.88rem;
  color: var(--text-soft);
  line-height: 1.55;
}

.toc-section {
  max-width: 1020px;
  margin: 0 auto 90px;
  padding: 0 24px;
}

.part-block {
  margin-bottom: 42px;
}

.part-header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
}

.part-num {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 700;
  color: var(--gold);
}

.part-title {
  font-family: var(--font-serif);
  font-size: 1.55rem;
  margin: 0;
  color: var(--text);
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 16px;
}

.chapter-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  transition: border-color 0.2s, transform 0.2s, box-shadow 0.2s;
  position: relative;
}

.chapter-card:hover {
  border-color: var(--blue);
  transform: translateY(-3px);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.35);
}

.chapter-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.chapter-card-num {
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--blue);
}

.chapter-card-status {
  font-size: 0.74rem;
  color: var(--green);
  font-weight: 600;
}

.chapter-card h3 {
  font-family: var(--font-serif);
  font-size: 1.32rem;
  margin: 0 0 8px;
  color: var(--text);
}

.chapter-card p {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-soft);
  line-height: 1.55;
}

.chapter-card-wild {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  font-size: 0.78rem;
  color: var(--purple);
  display: none;
}

body.show-wild-home .chapter-card-wild {
  display: block;
}
