/* ==========================================================================
   Jarvis Ecosystem Walkthrough - Design System
   ========================================================================== */

:root {
  color-scheme: light;
  /* Color Palette - Historical Light + Amber OKLCH */
  --bg-base: oklch(96.5% 0.012 92);
  --bg-surface: oklch(98.8% 0.004 92);
  --bg-card: oklch(100% 0 0);
  --bg-elevated: oklch(97.5% 0.007 92);
  --text-primary: oklch(24.5% 0.03 282);
  --text-secondary: oklch(43% 0.02 282);
  --text-muted: oklch(57% 0.015 282);
  --accent-primary: oklch(56% 0.13 58);
  --accent-secondary: oklch(63% 0.14 66);
  --accent-primary-light: oklch(48% 0.11 58);
  --border-subtle: oklch(89% 0.012 92);
  --border-strong: oklch(80% 0.018 92);
  --card-shadow: oklch(0% 0 0 / 0.08);
  --card-glow: oklch(63% 0.14 66 / 0.14);
  --grid-line: oklch(24.5% 0.02 282 / 0.06);
  --accent-soft: oklch(63% 0.14 66 / 0.1);
  --accent-soft-strong: oklch(63% 0.14 66 / 0.16);
  --accent-soft-stronger: oklch(63% 0.14 66 / 0.22);
  --rail-bg: oklch(98% 0.005 92 / 0.92);
  --inset-shadow: oklch(24.5% 0.03 282 / 0.05);
  --noise-opacity: 0.03;
  
  /* Typography */
  --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --font-display: 'Space Grotesk', var(--font-sans);
  
  /* Spacing */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-8: 48px;
  --space-10: 64px;
  --space-12: 96px;
  
  /* Layout */
  --nav-height: 60px;
  --rail-left-width: 240px;
  --rail-right-width: 200px;
  --content-max-width: 800px;
  
  /* Radii */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;
  
  /* Transitions */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg-base: oklch(17% 0.015 270);
  --bg-surface: oklch(21% 0.015 270);
  --bg-card: oklch(24% 0.016 270);
  --bg-elevated: oklch(28% 0.016 270);
  --text-primary: oklch(95% 0.01 92);
  --text-secondary: oklch(80% 0.012 92);
  --text-muted: oklch(68% 0.012 92);
  --accent-primary: oklch(74% 0.14 66);
  --accent-secondary: oklch(80% 0.15 72);
  --accent-primary-light: oklch(88% 0.08 92);
  --border-subtle: oklch(34% 0.012 270);
  --border-strong: oklch(45% 0.014 270);
  --card-shadow: oklch(0% 0 0 / 0.45);
  --card-glow: oklch(74% 0.14 66 / 0.18);
  --grid-line: oklch(95% 0.008 92 / 0.07);
  --accent-soft: oklch(74% 0.14 66 / 0.16);
  --accent-soft-strong: oklch(74% 0.14 66 / 0.24);
  --accent-soft-stronger: oklch(74% 0.14 66 / 0.32);
  --rail-bg: oklch(21% 0.015 270 / 0.9);
  --inset-shadow: oklch(0% 0 0 / 0.2);
  --noise-opacity: 0.02;
}

/* ==========================================================================
   Reset & Base
   ========================================================================== */

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

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--nav-height) + var(--space-6));
}

body {
  font-family: var(--font-sans);
  color: var(--text-primary);
  background-color: var(--bg-base);
  background-image: 
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 32px 32px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  color: var(--text-primary);
  line-height: 1.2;
  margin-bottom: var(--space-4);
  font-weight: 600;
}

h1 { font-size: 2.5rem; letter-spacing: -0.02em; }
h2 { font-size: 1.75rem; margin-top: var(--space-8); letter-spacing: -0.01em; }
h3 { font-size: 1.25rem; margin-top: var(--space-6); }
h4 { font-size: 1.125rem; margin-top: var(--space-4); }

p {
  margin-bottom: var(--space-4);
  color: var(--text-secondary);
}

a {
  color: var(--accent-primary-light);
  text-decoration: none;
  transition: color var(--transition-fast);
}

a:hover {
  color: var(--accent-primary);
  text-decoration: underline;
}

a:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--accent-soft);
  padding: 0.2em 0.4em;
  border-radius: var(--radius-sm);
  color: var(--accent-primary);
}

pre {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  overflow-x: auto;
  margin-bottom: var(--space-4);
}

pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

ul, ol {
  margin-bottom: var(--space-4);
  padding-left: var(--space-5);
  color: var(--text-secondary);
}

li {
  margin-bottom: var(--space-2);
}

/* ==========================================================================
   Layout
   ========================================================================== */

.page-layout {
  display: grid;
  grid-template-columns: var(--rail-left-width) 1fr var(--rail-right-width);
  gap: 0;
  min-height: 100vh;
  padding-top: var(--nav-height);
}

.content-main {
  padding: var(--space-8) var(--space-6);
  max-width: var(--content-max-width);
  margin: 0 auto;
  width: 100%;
}

/* Rails */
.rail--chapters,
.rail--toc {
  position: sticky;
  top: var(--nav-height);
  height: calc(100vh - var(--nav-height));
  overflow-y: auto;
  padding: var(--space-6) var(--space-4);
  border-right: 1px solid var(--border-subtle);
  background: var(--rail-bg);
  backdrop-filter: blur(8px);
}

.rail--toc {
  border-right: none;
  border-left: 1px solid var(--border-subtle);
}

.rail-title {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: var(--space-4);
  font-weight: 500;
}

/* ==========================================================================
   Components
   ========================================================================== */

/* Global Navigation */
.global-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--nav-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 100;
  display: flex;
  align-items: center;
  padding: 0 var(--space-4);
  box-shadow: 0 1px 2px var(--card-shadow);
}

.global-nav-brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--accent-primary);
  margin-right: var(--space-6);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.global-nav-brand::before {
  content: '';
  display: block;
  width: 12px;
  height: 12px;
  background: var(--accent-secondary);
  border-radius: 2px;
}

.global-nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: var(--space-4);
  overflow-x: auto;
  scrollbar-width: none; /* Firefox */
}

.global-nav-list::-webkit-scrollbar {
  display: none; /* Chrome/Safari */
}

.global-nav-list a {
  color: var(--text-secondary);
  font-size: 0.875rem;
  font-weight: 500;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  white-space: nowrap;
  position: relative;
}

.global-nav-list a:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
  text-decoration: none;
}

.global-nav-list a.active {
  background: var(--accent-soft);
  color: var(--accent-primary);
}

.theme-toggle {
  margin-left: auto;
  height: 34px;
  min-width: 74px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-full);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.theme-toggle:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
  background: var(--bg-elevated);
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

/* Scroll Progress */
.scroll-progress {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
  z-index: 1000;
  transform-origin: 0 50%;
  transform: scaleX(0);
  transition: transform 0.1s ease-out;
}

/* Hero Section */
.hero-section {
  margin-bottom: var(--space-10);
  padding-bottom: var(--space-8);
  border-bottom: 1px solid var(--border-subtle);
}

.hero-section h1 {
  font-size: 3.5rem;
  line-height: 1.1;
  margin-bottom: var(--space-4);
  color: var(--accent-primary);
}

.hero-section .lead {
  font-size: 1.25rem;
  color: var(--text-secondary);
  max-width: 600px;
}

/* Section Card */
.section-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-6);
  margin-bottom: var(--space-6);
  box-shadow: var(--card-shadow);
  transition: transform 400ms cubic-bezier(0.23, 1, 0.32, 1),
              box-shadow 400ms cubic-bezier(0.23, 1, 0.32, 1);
}

.section-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 30px var(--card-glow), 0 2px 8px var(--card-glow);
}

/* Metric Card */
.metric-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.metric-value {
  font-family: var(--font-display);
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent-primary);
  line-height: 1;
  margin-bottom: var(--space-2);
  font-variant-numeric: tabular-nums;
}

.metric-label {
  font-size: 0.875rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Source Pill / Badge */
.source-pill, .source-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding: 2px 8px;
  background: var(--accent-soft);
  color: var(--accent-primary);
  border: 1px solid var(--accent-soft-strong);
  border-radius: var(--radius-full);
  text-decoration: none;
  transition: all var(--transition-fast);
}

.source-pill:hover, .source-badge:hover {
  background: var(--accent-soft-stronger);
  text-decoration: none;
}

.source-pill::before {
  content: '📄';
  font-size: 0.8em;
}

/* Glossary Term Button */
.term-button {
  background: none;
  border: none;
  border-bottom: 1px dashed var(--accent-secondary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: inherit;
  cursor: help;
  padding: 0;
  transition: color var(--transition-fast);
}

.term-button:hover {
  color: var(--accent-secondary);
}

/* Glossary Popover */
.term-popover {
  position: absolute;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  width: max-content;
  max-width: 300px;
  box-shadow: 0 10px 25px var(--card-shadow);
  z-index: 1000;
  font-size: 0.875rem;
  color: var(--text-secondary);
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: all var(--transition-fast);
  pointer-events: none;
}

.term-popover.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
}

.term-popover-title {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent-primary);
  margin-bottom: var(--space-2);
  display: block;
}

/* Taxonomy Grid */
.taxonomy-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

/* Timeline Step */
.timeline-step {
  position: relative;
  padding-left: var(--space-6);
  margin-bottom: var(--space-6);
}

.timeline-step::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent-secondary);
  border: 2px solid var(--bg-base);
  z-index: 2;
}

.timeline-step::after {
  content: '';
  position: absolute;
  left: 5px;
  top: 18px;
  bottom: calc(var(--space-6) * -1);
  width: 2px;
  background: var(--border-subtle);
  z-index: 1;
}

.timeline-step:last-child::after {
  display: none;
}

.timeline-step h3 {
  margin-top: 0;
  margin-bottom: var(--space-2);
}

/* Diagram Panel */
.diagram-panel {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  overflow-x: auto;
  margin-bottom: var(--space-6);
  box-shadow: inset 0 0 20px var(--inset-shadow);
}

.diagram-panel pre.mermaid {
  background: transparent;
  border: none;
  padding: 0;
  margin: 0;
  display: flex;
  justify-content: center;
}

/* Chapter Rail Links */
.chapter-link {
  display: block;
  padding: var(--space-2) var(--space-3);
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-1);
  font-size: 0.875rem;
  position: relative;
  transition: all 200ms cubic-bezier(0.23, 1, 0.32, 1);
}

.chapter-link:hover {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.chapter-link::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--accent-secondary);
  border-radius: 0 2px 2px 0;
  transform: scaleY(0);
  transition: transform 250ms cubic-bezier(0.23, 1, 0.32, 1);
}

.chapter-link.active::before { transform: scaleY(1); }

.chapter-link.active {
  background: var(--accent-soft-stronger);
  color: var(--accent-primary);
  font-weight: 600;
}

.chapter-subtitle {
  display: block;
  font-size: 0.75rem;
  opacity: 0.7;
  margin-top: 2px;
}

/* TOC Links */
.toc-link {
  display: block;
  padding: var(--space-1) 0;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.875rem;
  padding-left: var(--space-3);
  margin-bottom: var(--space-1);
  transition: all var(--transition-fast);
}

.toc-link:hover {
  color: var(--accent-primary);
}

.toc-link.active {
  color: var(--accent-primary);
  font-weight: 500;
}

.toc-link.level-3 {
  padding-left: var(--space-5);
  font-size: 0.8125rem;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

/* Tablet: 2-column */
@media (max-width: 1024px) {
  .page-layout {
    grid-template-columns: var(--rail-left-width) 1fr;
  }
  .rail--toc {
    display: none;
  }
  .taxonomy-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Mobile: 1-column */
@media (max-width: 768px) {
  body {
    overflow-x: hidden;
  }
  .page-layout {
    grid-template-columns: 1fr;
    overflow-x: hidden;
  }
  .rail--chapters {
    display: none;
  }
  .content-main {
    padding: var(--space-6) var(--space-4);
    max-width: 100%;
    overflow-x: hidden;
  }
  .diagram-panel svg {
    max-width: 100%;
    height: auto;
  }
  .taxonomy-grid {
    grid-template-columns: 1fr;
  }
  .hero-section h1 {
    font-size: 2.5rem;
  }
}

/* === IMPROVEMENT 1: Scroll Reveal === */
.reveal {
  opacity: 0;
  transform: translateY(24px);
  transition: opacity 600ms cubic-bezier(0.23, 1, 0.32, 1),
              transform 600ms cubic-bezier(0.23, 1, 0.32, 1);
}
.reveal.is-visible { opacity: 1; transform: translateY(0); }

.reveal-left {
  opacity: 0;
  transform: translateX(-16px);
  transition: opacity 500ms cubic-bezier(0.23, 1, 0.32, 1),
              transform 500ms cubic-bezier(0.23, 1, 0.32, 1);
}
.reveal-left.is-visible { opacity: 1; transform: translateX(0); }

.reveal-stagger > .reveal:nth-child(1) { transition-delay: 0ms; }
.reveal-stagger > .reveal:nth-child(2) { transition-delay: 80ms; }
.reveal-stagger > .reveal:nth-child(3) { transition-delay: 160ms; }
.reveal-stagger > .reveal:nth-child(4) { transition-delay: 240ms; }

@media (prefers-reduced-motion: reduce) {
  .reveal, .reveal-left { opacity: 1; transform: none; transition: none; }
}

/* === IMPROVEMENT 2: Hero Elevation === */
.hero-section {
  position: relative;
  overflow: hidden;
}
.hero-section h1 {
  background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-primary) 40%, var(--accent-secondary) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.hero-section::before {
  content: attr(data-chapter);
  position: absolute;
  top: -20px;
  right: -10px;
  font-family: var(--font-display);
  font-size: clamp(8rem, 15vw, 12rem);
  font-weight: 700;
  color: var(--accent-primary);
  opacity: 0.04;
  line-height: 1;
  pointer-events: none;
  user-select: none;
  z-index: 0;
}
.hero-section > * {
  opacity: 0;
  transform: translateY(20px);
  animation: heroEntrance 700ms cubic-bezier(0.23, 1, 0.32, 1) forwards;
}
.hero-section > *:nth-child(1) { animation-delay: 100ms; }
.hero-section > *:nth-child(2) { animation-delay: 250ms; }
.hero-section > *:nth-child(3) { animation-delay: 400ms; }
.hero-section > *:nth-child(4) { animation-delay: 550ms; }
@keyframes heroEntrance {
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .hero-section > * { opacity: 1; transform: none; animation: none; }
  .hero-section::before { display: none; }
}

/* === IMPROVEMENT 4: Atmospheric Depth === */
body::after {
  content: '';
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  z-index: 9000;
  opacity: var(--noise-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  background-repeat: repeat;
  background-size: 256px 256px;
}
.term-button:active { transform: scale(0.97); }
.source-pill:active, .source-badge:active { transform: scale(0.96); }
.chapter-link:active { transform: scale(0.98); }

@media (prefers-reduced-motion: reduce) {
  .section-card { transition: none !important; }
  .section-card:hover { transform: none !important; }
  body::after { display: none; }
}
@media (max-width: 768px) {
  body::after { display: none; }
}

/* === IMPROVEMENT 5: Nav Indicators === */
.toc-link {
  position: relative;
  transition: all 200ms cubic-bezier(0.23, 1, 0.32, 1);
}
.toc-link.active::before {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--accent-secondary);
}
.global-nav-list a::after {
  content: '';
  position: absolute;
  bottom: 2px;
  left: var(--space-3, 12px);
  right: var(--space-3, 12px);
  height: 2px;
  background: var(--accent-secondary);
  border-radius: 1px;
  transform: scaleX(0);
  transition: transform 200ms cubic-bezier(0.23, 1, 0.32, 1);
}
.global-nav-list a.active::after,
.global-nav-list a:hover::after { transform: scaleX(1); }
@media (prefers-reduced-motion: reduce) {
  .chapter-link, .toc-link { transition: none; }
  .chapter-link::before { transition: none; }
  .global-nav-list a::after { transition: none; }
}

/* ==========================================================================
   svg-pan-zoom Controls — Dark Background Visibility
   ========================================================================== */

.diagram-panel svg #svg-pan-zoom-controls > g > g {
  cursor: pointer;
}

.diagram-panel svg #svg-pan-zoom-controls > g > g > rect {
  fill: var(--bg-elevated) !important;
  stroke: var(--border-subtle) !important;
  fill-opacity: 0.9 !important;
}

.diagram-panel svg #svg-pan-zoom-controls > g > g > path,
.diagram-panel svg #svg-pan-zoom-controls > g > g > polygon {
  fill: var(--text-secondary) !important;
}

.diagram-panel svg #svg-pan-zoom-controls > g > g:hover > rect {
  fill: var(--bg-surface) !important;
  stroke: var(--border-strong) !important;
}

.diagram-panel svg #svg-pan-zoom-controls > g > g:hover > path,
.diagram-panel svg #svg-pan-zoom-controls > g > g:hover > polygon {
  fill: var(--text-primary) !important;
}
