/* ===== The Anti-Fragile Investor — shadcn-inspired Design System ===== */

/* --- Reset --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
img, svg { max-width: 100%; height: auto; display: block; }

/* --- Design Tokens (shadcn/zinc) --- */
:root {
  --radius: 0.5rem;
  --font-sans: 'Plus Jakarta Sans', 'Plus Jakarta Sans Fallback', ui-sans-serif, system-ui, -apple-system, sans-serif;
  --font-serif: 'Newsreader', 'Newsreader Fallback', 'Georgia', 'Times New Roman', serif;

  /* Zinc-tinted neutrals (warm) */
  --background: oklch(98.5% 0.005 80);
  --foreground: oklch(15% 0.01 80);
  --card: oklch(100% 0.003 80);
  --card-foreground: oklch(15% 0.01 80);
  --popover: oklch(100% 0.003 80);
  --popover-foreground: oklch(15% 0.01 80);
  --primary: oklch(40% 0.12 80);
  --primary-foreground: oklch(98% 0.005 80);
  --secondary: oklch(95% 0.008 80);
  --secondary-foreground: oklch(20% 0.01 80);
  --muted: oklch(94% 0.006 80);
  --muted-foreground: oklch(48% 0.015 80);
  --accent: oklch(94% 0.008 80);
  --accent-foreground: oklch(20% 0.01 80);
  --destructive: oklch(55% 0.2 25);
  --destructive-foreground: oklch(98% 0.005 80);
  --border: oklch(90% 0.008 80);
  --input: oklch(90% 0.008 80);
  --ring: oklch(40% 0.12 80);

  /* Book-specific tokens */
  --gold: oklch(65% 0.15 85);
  --gold-muted: oklch(55% 0.1 85);
  --gold-bg: oklch(95% 0.03 85);
  --green: oklch(55% 0.14 145);
  --green-bg: oklch(95% 0.03 145);
  --green-fg: oklch(30% 0.08 145);
  --blue: oklch(55% 0.14 250);
  --blue-bg: oklch(95% 0.03 250);
  --blue-fg: oklch(30% 0.08 250);
  --swan-bg: oklch(16% 0.015 260);
  --swan-fg: oklch(88% 0.01 80);
  --swan-accent: oklch(65% 0.15 85);
  --red: oklch(55% 0.2 25);
  --red-muted: oklch(45% 0.15 25);

  /* Layout */
  --sidebar-width: 240px;
  --content-max: 720px;
  --header-height: 56px;
}

[data-theme="dark"] {
  --background: oklch(12% 0.01 80);
  --foreground: oklch(92% 0.008 80);
  --card: oklch(16% 0.012 80);
  --card-foreground: oklch(92% 0.008 80);
  --popover: oklch(16% 0.012 80);
  --popover-foreground: oklch(92% 0.008 80);
  --primary: oklch(70% 0.12 85);
  --primary-foreground: oklch(12% 0.01 80);
  --secondary: oklch(20% 0.01 80);
  --secondary-foreground: oklch(92% 0.008 80);
  --muted: oklch(20% 0.01 80);
  --muted-foreground: oklch(62% 0.012 80);
  --accent: oklch(20% 0.012 80);
  --accent-foreground: oklch(92% 0.008 80);
  --border: oklch(24% 0.01 80);
  --input: oklch(24% 0.01 80);
  --ring: oklch(70% 0.12 85);

  --gold: oklch(72% 0.13 85);
  --gold-muted: oklch(60% 0.1 85);
  --gold-bg: oklch(20% 0.03 85);
  --green-bg: oklch(18% 0.03 145);
  --green-fg: oklch(75% 0.1 145);
  --blue-bg: oklch(18% 0.03 250);
  --blue-fg: oklch(75% 0.1 250);
  --swan-bg: oklch(10% 0.015 260);
  --swan-fg: oklch(85% 0.01 80);
  --swan-accent: oklch(72% 0.13 85);
}

/* --- Base --- */
html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: break-word;
  word-break: break-word;
}

body {
  font-family: var(--font-serif);
  background: var(--background);
  color: var(--foreground);
  line-height: 1.75;
  min-height: 100vh;
  min-height: 100dvh;
  transition: background-color 0.2s ease, color 0.2s ease;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* Safe areas for notched devices */
.mobile-header {
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
}
.page-wrapper {
  padding-left: max(2rem, env(safe-area-inset-left));
  padding-right: max(2rem, env(safe-area-inset-right));
}

/* --- Focus (accessibility) --- */
:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: 2px;
}

/* Skip link */
.skip-link {
  position: absolute;
  top: -100%;
  left: 1rem;
  z-index: 999;
  padding: 0.75rem 1.5rem;
  background: var(--primary);
  color: var(--primary-foreground);
  font-family: var(--font-sans);
  font-size: 0.875rem;
  font-weight: 600;
  border-radius: var(--radius);
  text-decoration: none;
}
.skip-link:focus {
  top: 1rem;
}

/* --- Typography --- */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-sans);
  font-weight: 700;
  line-height: 1.3;
  color: var(--foreground);
  letter-spacing: -0.02em;
}

h1 { font-size: clamp(1.75rem, 3vw + 0.5rem, 2.25rem); margin-bottom: 1rem; }
h2 { font-size: clamp(1.35rem, 2vw + 0.5rem, 1.65rem); margin: 2.5rem 0 0.75rem; }
h3 { font-size: clamp(1.1rem, 1.5vw + 0.4rem, 1.3rem); margin: 2rem 0 0.5rem; }
h4 { font-size: 1.05rem; margin: 1.5rem 0 0.5rem; font-weight: 600; }

p { margin-bottom: 1.125rem; }

a {
  color: var(--primary);
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 3px;
  transition: text-decoration-color 0.15s ease;
}
a:hover { text-decoration-color: var(--primary); }

strong { font-weight: 700; }
em { font-style: italic; }

/* Long word / URL overflow protection */
p, li, td, blockquote, .alert, .concept-box, .pull-quote {
  overflow-wrap: break-word;
  word-break: break-word;
  hyphens: auto;
}

/* --- Layout Shell --- */
.site-layout {
  display: flex;
  min-height: 100vh;
}

/* --- Sidebar (Desktop) --- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 40;
  overflow: hidden;
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.sidebar-header {
  padding: 1.25rem 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-brand {
  font-family: var(--font-sans);
  font-weight: 800;
  font-size: 0.9rem;
  color: var(--foreground);
  text-decoration: none;
  letter-spacing: -0.02em;
  display: block;
  line-height: 1.3;
}
.sidebar-brand:hover { text-decoration: none; color: var(--primary); }

.sidebar-brand small {
  display: block;
  font-weight: 500;
  font-size: 0.7rem;
  color: var(--muted-foreground);
  letter-spacing: 0;
  margin-top: 2px;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.sidebar-section {
  padding: 0 0.75rem;
  margin-bottom: 0.5rem;
}

.sidebar-section-title {
  font-family: var(--font-sans);
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted-foreground);
  padding: 0.5rem;
  margin-top: 0.25rem;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.625rem;
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 450;
  color: var(--muted-foreground);
  text-decoration: none;
  transition: background-color 0.1s ease, color 0.1s ease;
  line-height: 1.4;
  min-height: 44px; /* WCAG touch target minimum */
}
.sidebar-link:hover {
  background: var(--accent);
  color: var(--accent-foreground);
  text-decoration: none;
}
.sidebar-link.active {
  background: var(--accent);
  color: var(--foreground);
  font-weight: 600;
}
.sidebar-link .ch-num {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--primary);
  min-width: 1.5rem;
  font-variant-numeric: tabular-nums;
}

.sidebar-footer {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.625rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--background);
  color: var(--muted-foreground);
  font-family: var(--font-sans);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.1s ease, border-color 0.15s ease;
  min-height: 40px;
}
.theme-toggle:hover {
  background: var(--accent);
  border-color: var(--ring);
}
.theme-toggle .toggle-icon { font-size: 1rem; }
.theme-toggle .toggle-label { font-weight: 500; }

/* --- Mobile Header --- */
.mobile-header {
  display: none;
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-height);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
  align-items: center;
  justify-content: space-between;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.mobile-brand {
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--foreground);
  text-decoration: none;
}
.mobile-brand:hover { text-decoration: none; }

.menu-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: none;
  color: var(--foreground);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 1.25rem;
  transition: background-color 0.1s ease;
}
.menu-toggle:hover { background: var(--accent); }

.mobile-theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  background: var(--background);
  color: var(--muted-foreground);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 1.1rem;
  transition: background-color 0.1s ease;
}
.mobile-theme-toggle:hover { background: var(--accent); }

/* Overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.5);
  z-index: 35;
}

/* --- Main Content --- */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
}

.page-wrapper {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 2.5rem 2rem 4rem;
}

/* --- Scroll Progress --- */
.scroll-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  background: var(--primary);
  z-index: 100;
  pointer-events: none;
}

/* --- Chapter Header --- */
.chapter-header {
  margin-bottom: 2.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}
.chapter-header .part-label {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted-foreground);
  margin-bottom: 0.75rem;
}
.chapter-header .chapter-number {
  font-family: var(--font-sans);
  font-size: 3rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
  margin-bottom: 0.5rem;
  opacity: 0.35;
}
.chapter-header h1 {
  margin-bottom: 0.5rem;
}
.chapter-header .subtitle {
  font-size: 1rem;
  color: var(--muted-foreground);
  font-style: italic;
  line-height: 1.5;
}

/* --- Blockquote --- */
blockquote {
  border-left: 3px solid var(--primary);
  margin: 1.75rem 0;
  padding: 1rem 1.25rem;
  background: var(--secondary);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-style: italic;
}
blockquote cite {
  display: block;
  margin-top: 0.625rem;
  font-style: normal;
  font-size: 0.8rem;
  color: var(--muted-foreground);
  font-family: var(--font-sans);
  font-weight: 500;
}

/* --- Alert Boxes (shadcn-style) --- */
.alert {
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin: 1.75rem 0;
  font-size: 0.875rem;
  line-height: 1.65;
  border: 1px solid;
}
.alert-title {
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  margin-bottom: 0.375rem;
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.alert-swan {
  background: var(--swan-bg);
  color: var(--swan-fg);
  border-color: oklch(28% 0.015 260);
}
.alert-swan .alert-title { color: var(--swan-accent); }

.alert-tip {
  background: var(--green-bg);
  color: var(--green-fg);
  border-color: var(--green);
}
.alert-tip .alert-title { color: var(--green); }

.alert-update {
  background: var(--blue-bg);
  color: var(--blue-fg);
  border-color: var(--blue);
}
.alert-update .alert-title { color: var(--blue); }

/* --- Tables (shadcn-style) --- */
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 1.5rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* Auto-wrap bare tables in scrollable container via CSS */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  font-family: var(--font-sans);
  font-variant-numeric: tabular-nums;
}

/* Prevent table cells from forcing width overflow */
td, th {
  min-width: 0;
  word-break: normal;
}
thead th {
  background: var(--muted);
  color: var(--foreground);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.625rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
tbody td {
  padding: 0.625rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--accent); }

/* --- Lists --- */
ul, ol { margin: 0.75rem 0 1.25rem 1.25rem; }
li { margin-bottom: 0.375rem; }
li::marker { color: var(--primary); }

/* --- Concept Box (shadcn card) --- */
.concept-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin: 1.75rem 0;
}
.concept-box h4 {
  font-family: var(--font-sans);
  color: var(--primary);
  margin-top: 0;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

/* --- Pull Quote --- */
.pull-quote {
  margin: 2rem 0;
  padding: 1.25rem 1.5rem;
  font-size: 1.1rem;
  font-style: italic;
  color: var(--foreground);
  border-left: 3px solid var(--primary);
  background: var(--gold-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
  line-height: 1.6;
}
.pull-quote cite {
  display: block;
  font-size: 0.8rem;
  font-style: normal;
  color: var(--muted-foreground);
  margin-top: 0.625rem;
  font-family: var(--font-sans);
  font-weight: 500;
}

/* --- Diagrams --- */
.diagram {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin: 1.75rem 0;
  text-align: center;
  overflow-x: auto;
}
.diagram-title {
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.8rem;
  color: var(--muted-foreground);
  margin-bottom: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Barbell */
.barbell-visual {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  margin: 1rem 0;
}
.barbell-weight {
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.85rem;
  text-align: center;
  min-width: 140px;
}
.barbell-weight.safe {
  background: oklch(40% 0.12 145);
  color: oklch(95% 0.02 145);
}
.barbell-weight.speculative {
  background: oklch(45% 0.15 25);
  color: oklch(95% 0.02 25);
}
.barbell-bar {
  height: 4px;
  background: var(--border);
  flex: 1;
  max-width: 160px;
  min-width: 40px;
}
.barbell-weight .pct {
  font-size: 1.5rem;
  font-weight: 800;
  display: block;
  margin-bottom: 0.25rem;
}

/* Distribution chart */
.distribution-chart {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 2px;
  height: 160px;
  margin: 1rem 0;
}
.dist-bar {
  width: 10px;
  border-radius: 2px 2px 0 0;
  position: relative;
}
.dist-bar.normal { background: var(--primary); opacity: 0.25; }
.dist-bar.fat-tail { background: var(--red); }
.dist-bar-label {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.6rem;
  color: var(--muted-foreground);
  white-space: nowrap;
  font-family: var(--font-sans);
}
.dist-legend {
  display: flex;
  gap: 1.5rem;
  justify-content: center;
  margin-top: 1.5rem;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--muted-foreground);
}
.dist-legend span::before {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 0.375rem;
  vertical-align: middle;
}
.dist-legend .leg-normal::before { background: var(--primary); opacity: 0.3; }
.dist-legend .leg-fat::before { background: var(--red); }

/* --- Chapter Navigation --- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.chapter-nav a {
  font-family: var(--font-sans);
  font-size: 0.825rem;
  font-weight: 500;
  color: var(--foreground);
  text-decoration: none;
  padding: 0.75rem 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.15s ease, background-color 0.15s ease;
  max-width: 48%;
  min-width: 0;
}
.chapter-nav a:hover {
  border-color: var(--ring);
  background: var(--accent);
  text-decoration: none;
}
.chapter-nav a .label {
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--muted-foreground);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: block;
  margin-bottom: 0.125rem;
}
.chapter-nav .next { text-align: right; margin-left: auto; }

/* --- Cover Page --- */
.cover {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 3rem 1.5rem;
  background: var(--background);
}
.cover-ornament {
  font-size: 1.5rem;
  color: var(--primary);
  margin-bottom: 2rem;
  opacity: 0.4;
  letter-spacing: 0.5em;
}
.cover h1 {
  font-size: clamp(2rem, 5vw + 0.5rem, 3rem);
  color: var(--foreground);
  margin-bottom: 0.75rem;
  max-width: 600px;
}
.cover .cover-subtitle {
  font-size: clamp(1rem, 2vw + 0.3rem, 1.25rem);
  color: var(--primary);
  font-family: var(--font-serif);
  font-style: italic;
  margin-bottom: 2rem;
  max-width: 480px;
}
.cover .cover-tagline {
  font-size: 0.95rem;
  color: var(--muted-foreground);
  margin-bottom: 2.5rem;
  max-width: 500px;
  line-height: 1.65;
}
.cover .cover-cta {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 2rem;
  background: var(--primary);
  color: var(--primary-foreground);
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.875rem;
  border-radius: var(--radius);
  text-decoration: none;
  transition: opacity 0.15s ease;
}
.cover .cover-cta:hover {
  opacity: 0.9;
  text-decoration: none;
}

/* --- TOC --- */
.toc {
  max-width: 600px;
  margin: 0 auto;
  padding: 3rem 1.5rem;
}
.toc h2 {
  margin-bottom: 1.5rem;
}
.toc-part {
  margin-bottom: 1.5rem;
}
.toc-part-title {
  font-family: var(--font-sans);
  font-weight: 600;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted-foreground);
  margin-bottom: 0.5rem;
  padding-bottom: 0.375rem;
  border-bottom: 1px solid var(--border);
}
.toc-item {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.5rem 0.625rem;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--foreground);
  font-size: 0.9rem;
  transition: background-color 0.1s ease;
}
.toc-item:hover {
  background: var(--accent);
  text-decoration: none;
}
.toc-chapter-num {
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--primary);
  min-width: 1.5rem;
  font-variant-numeric: tabular-nums;
}
.toc-chapter-title { font-weight: 450; }

/* --- Footer --- */
.site-footer {
  text-align: center;
  padding: 1.5rem;
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--muted-foreground);
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}

/* --- Stat Grid --- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.75rem;
  margin: 1.5rem 0;
}
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  text-align: center;
}
.stat-card .stat-value {
  font-family: var(--font-sans);
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}
.stat-card .stat-label {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--muted-foreground);
  margin-top: 0.125rem;
}

/* --- Checklist --- */
.checklist { list-style: none; margin-left: 0; }
.checklist li {
  padding: 0.375rem 0;
  padding-left: 1.75rem;
  position: relative;
  font-size: 0.925rem;
}
.checklist li::before {
  content: '\2610';
  position: absolute;
  left: 0;
  color: var(--primary);
  font-size: 1rem;
}

/* --- Section Divider --- */
.section-divider {
  text-align: center;
  margin: 2.5rem 0;
  color: var(--muted-foreground);
  opacity: 0.4;
  font-size: 1rem;
  letter-spacing: 0.5em;
}

/* --- Nav Links (legacy compat — hidden, replaced by sidebar) --- */
.nav-links { display: none; }
.site-nav { display: none; }

/* --- Font loading resilience --- */
/* Size-adjusted fallbacks prevent layout shift if Google Fonts fail */
@font-face {
  font-family: 'Plus Jakarta Sans Fallback';
  src: local('Avenir Next'), local('Avenir'), local('Helvetica Neue'), local('Arial');
  size-adjust: 102%;
  ascent-override: 95%;
  descent-override: 25%;
}
@font-face {
  font-family: 'Newsreader Fallback';
  src: local('Georgia'), local('Times New Roman');
  size-adjust: 98%;
  ascent-override: 90%;
}

/* --- High Contrast Mode support --- */
@media (forced-colors: active) {
  .alert, .concept-box, .stat-card, blockquote, .pull-quote {
    border: 2px solid CanvasText;
  }
  .sidebar-link.active {
    outline: 2px solid Highlight;
  }
  .barbell-weight.safe,
  .barbell-weight.speculative {
    border: 2px solid CanvasText;
  }
  .scroll-progress {
    background: Highlight;
  }
}

/* --- Noscript fallback: sidebar always visible on desktop --- */
/* When JS fails, the mobile sidebar can't open, but the desktop sidebar is still usable.
   The theme toggle won't work but the default theme is still functional. */

/* --- Text zoom resilience (up to 200%) --- */
@media (min-resolution: 1dppx) {
  .sidebar-section-title,
  .chapter-header .part-label,
  .toc-part-title,
  .chapter-nav a .label {
    white-space: normal;
    overflow-wrap: break-word;
  }
}

/* --- Selection styling --- */
::selection {
  background: var(--primary);
  color: var(--primary-foreground);
}

/* --- Responsive --- */

/* Narrow sidebar for medium screens */
@media (max-width: 900px) {
  :root { --sidebar-width: 200px; }
  .sidebar-section-title { font-size: 0.6rem; }
  .sidebar-link { font-size: 0.75rem; min-height: 40px; }
  .sidebar-brand { font-size: 0.8rem; }
  .page-wrapper { padding: 2rem 1.5rem 3rem; }
}

/* Sidebar collapses to drawer */
@media (max-width: 640px) {
  .sidebar {
    width: 280px;
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px oklch(0% 0 0 / 0.15);
  }
  .sidebar-overlay.open {
    display: block;
  }
  .mobile-header {
    display: flex;
  }
  .main-content {
    margin-left: 0;
  }
  html { font-size: 15px; }
  .page-wrapper {
    padding: 1.5rem 1rem 2.5rem;
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }
  .chapter-header .chapter-number {
    font-size: 2.25rem;
  }
  .chapter-nav {
    flex-direction: column;
  }
  .chapter-nav a {
    max-width: 100%;
  }
  .barbell-visual {
    flex-direction: column;
  }
  .barbell-bar {
    width: 4px;
    height: 32px;
    max-width: none;
    min-width: 0;
  }
  .barbell-weight {
    min-width: 160px;
  }
  .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .alert {
    padding: 0.875rem 1rem;
  }
  /* Tables scroll horizontally on mobile */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  thead, tbody, tr {
    display: table;
    width: 100%;
    table-layout: auto;
  }
  .pull-quote {
    font-size: 1rem;
    padding: 1rem;
  }
}

@media (max-width: 380px) {
  .stat-grid {
    grid-template-columns: 1fr;
  }
}

/* --- Print --- */
@media print {
  .sidebar, .mobile-header, .scroll-progress, .chapter-nav, .sidebar-overlay { display: none !important; }
  .main-content { margin-left: 0; }
  body { font-size: 11pt; background: white; color: black; }
  .page-wrapper { max-width: 100%; padding: 0; }
  .alert-swan { background: #eee; color: #000; border: 1px solid #999; }
  blockquote { border-left-color: #999; background: #f5f5f5; }
  a { color: black; text-decoration: none; }
  h2 { page-break-before: always; }
}

/* --- Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  html { scroll-behavior: auto; }
}

/* --- Entrance Animation --- */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.page-wrapper {
  animation: fadeIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
