/* Experience layer: fixes scrolling, accessibility, responsiveness, and interaction ergonomics. */

:root {
  color-scheme: dark;
  --header-height: 68px;
  --focus-ring: 0 0 0 3px rgba(79, 156, 247, 0.38);
  --surface-glass: rgba(8, 11, 18, 0.82);
}

html {
  scroll-padding-top: var(--header-height);
}

body {
  min-height: 100%;
  overflow-x: clip;
}

::selection {
  background: rgba(79, 156, 247, 0.32);
  color: var(--text-primary);
}

.skip-link {
  position: fixed;
  top: 10px;
  left: 12px;
  z-index: 2000;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--text-primary);
  color: var(--bg-deep);
  font-weight: 700;
  transform: translateY(-160%);
  transition: transform var(--duration-fast) var(--ease-out);
}

.skip-link:focus {
  transform: translateY(0);
}

.site-header {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 1000;
  min-height: var(--header-height);
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(180px, auto) minmax(150px, 1fr);
  align-items: center;
  gap: 18px;
  padding: 10px clamp(16px, 3vw, 36px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  background: linear-gradient(180deg, rgba(8, 11, 18, 0.94), rgba(8, 11, 18, 0.72));
  -webkit-backdrop-filter: blur(18px) saturate(130%);
  backdrop-filter: blur(18px) saturate(130%);
}

.site-brand {
  width: fit-content;
  color: var(--text-primary);
  font-weight: 700;
  letter-spacing: -0.02em;
  text-decoration: none;
}

.chapter-status {
  min-width: 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 12px;
  color: var(--text-secondary);
  font-size: 0.78rem;
}

#chapter-label {
  max-width: 32ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#chapter-count {
  flex: none;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 0.72rem;
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.chapter-menu {
  position: relative;
}

.chapter-menu summary {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  padding: 8px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(15, 21, 32, 0.76);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 600;
  list-style: none;
  transition: border-color var(--duration-fast), color var(--duration-fast), background var(--duration-fast);
}

.chapter-menu summary::-webkit-details-marker {
  display: none;
}

.chapter-menu summary::after {
  content: '⌄';
  margin-left: 8px;
  transform: translateY(-1px);
}

.chapter-menu[open] summary,
.chapter-menu summary:hover {
  border-color: var(--accent-dim);
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.chapter-menu nav {
  position: absolute;
  top: calc(100% + 12px);
  right: 0;
  width: min(390px, calc(100vw - 24px));
  max-height: min(72vh, 620px);
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: rgba(15, 21, 32, 0.97);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
}

.chapter-list {
  display: grid;
  gap: 2px;
  list-style: none;
}

.chapter-list a {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 10px;
  align-items: baseline;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  text-decoration: none;
}

.chapter-list a:hover,
.chapter-list a.is-current {
  background: var(--accent-glow);
  color: var(--text-primary);
}

.chapter-list-number {
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 0.7rem;
}

.chapter-list-title {
  font-size: 0.84rem;
  line-height: 1.35;
}

.reading-progress {
  position: absolute;
  inset: auto 0 0;
  height: 2px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.045);
}

.reading-progress span {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--accent-dim), #8b5cf6);
  transform: scaleX(0);
  transform-origin: left center;
  will-change: transform;
}

.scroll-container {
  height: auto;
  overflow: visible;
  scroll-snap-type: none;
}

.hero {
  min-height: 100svh;
  padding: calc(var(--header-height) + 72px) clamp(20px, 4vw, 56px) 96px;
  isolation: isolate;
}

.hero::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 45%, rgba(79, 156, 247, 0.12), transparent 34%),
    linear-gradient(180deg, transparent 55%, var(--bg-deep) 100%);
}

.hero canvas {
  opacity: 0.55;
}

.hero-eyebrow,
.hero-title,
.hero-subtitle,
.hero-actions {
  position: relative;
  z-index: 2;
}

.hero-eyebrow {
  margin-bottom: 14px;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.hero-title {
  max-width: 13ch;
  font-size: clamp(3rem, 9vw, 7.4rem);
  line-height: 0.94;
  letter-spacing: -0.065em;
  text-wrap: balance;
  background: linear-gradient(180deg, #ffffff 15%, #b9c8df 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.hero-subtitle {
  max-width: 680px;
  margin-bottom: 32px;
  font-size: clamp(1rem, 1.7vw, 1.25rem);
  text-wrap: balance;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
}

.hero-primary-action,
.hero-secondary-action {
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 18px;
  border-radius: 999px;
  font: 600 0.88rem var(--font-body);
  cursor: pointer;
  text-decoration: none;
  transition: transform var(--duration-fast), border-color var(--duration-fast), background var(--duration-fast);
}

.hero-primary-action {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #06111f;
  box-shadow: 0 12px 36px rgba(79, 156, 247, 0.24);
}

.hero-secondary-action {
  border: 1px solid var(--border);
  background: var(--bg-glass);
  color: var(--text-primary);
}

.hero-primary-action:hover,
.hero-secondary-action:hover {
  transform: translateY(-2px);
}

.section {
  height: auto;
  min-height: 100svh;
  grid-template-columns: minmax(0, 1.08fr) minmax(360px, 0.92fr);
  align-items: start;
  scroll-snap-align: none;
}

.viz-pane {
  position: sticky;
  top: 0;
  align-self: start;
  width: 100%;
  height: 100svh;
  min-height: 620px;
  background:
    radial-gradient(circle at 50% 45%, rgba(79, 156, 247, 0.055), transparent 48%),
    var(--bg-deep);
}

.viz-pane::after {
  content: '';
  position: absolute;
  inset: var(--header-height) 0 0;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(8, 11, 18, 0.4), transparent 16%, transparent 78%, rgba(8, 11, 18, 0.68));
}

.viz-pane canvas {
  position: relative;
  z-index: 1;
  touch-action: pan-y pinch-zoom;
}

.text-pane {
  min-height: 100svh;
  overflow: visible;
  padding: calc(var(--header-height) + clamp(50px, 8vh, 100px)) clamp(30px, 5vw, 74px) clamp(70px, 10vh, 120px);
  justify-content: center;
}

.text-content {
  width: 100%;
  max-width: 620px;
  margin: auto 0;
}

.section-title {
  font-size: clamp(2rem, 4.2vw, 3.8rem);
  line-height: 1.05;
  text-wrap: balance;
}

.section-subtitle {
  max-width: 46ch;
  margin-bottom: clamp(26px, 4vw, 42px);
  font-size: clamp(1rem, 1.4vw, 1.16rem);
}

.text-body {
  max-width: 66ch;
  font-size: clamp(0.96rem, 1.1vw, 1.04rem);
}

.insight {
  margin-top: 30px;
  border-left-width: 2px;
  background: linear-gradient(90deg, var(--accent-glow), rgba(22, 29, 45, 0.62));
}

.key-terms {
  margin-top: 24px;
  color: var(--text-muted);
  font-size: 0.82rem;
}

.key-terms strong {
  color: var(--text-secondary);
}

.viz-controls {
  bottom: clamp(18px, 3vw, 30px);
  left: clamp(16px, 3vw, 30px);
  right: clamp(16px, 3vw, 30px);
  z-index: 20;
  align-items: center;
  gap: 10px;
}

.viz-btn,
.slider-container,
.switch-container {
  min-height: 44px;
  border-color: rgba(164, 171, 188, 0.22);
  background: rgba(15, 21, 32, 0.88);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.18);
}

.viz-btn {
  padding: 10px 15px;
}

.viz-btn:disabled,
.viz-btn.disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
}

.viz-btn--outline {
  color: var(--text-secondary);
  background: rgba(8, 11, 18, 0.76);
}

.slider-container {
  min-width: min(310px, 100%);
}

input[type='range'] {
  min-width: 110px;
  min-height: 28px;
  background: transparent;
}

input[type='range']::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 999px;
  background: var(--border);
}

input[type='range']::-webkit-slider-thumb {
  width: 18px;
  height: 18px;
  margin-top: -6.5px;
}

input[type='range']::-moz-range-track {
  height: 5px;
  border-radius: 999px;
  background: var(--border);
}

input[type='range']::-moz-range-thumb {
  width: 18px;
  height: 18px;
}

.switch-input {
  display: block;
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.switch-input:focus-visible + .switch-track {
  box-shadow: var(--focus-ring);
}

.viz-hint {
  top: calc(var(--header-height) + 16px);
  max-width: min(48%, 420px);
  padding: 7px 11px;
  line-height: 1.45;
  opacity: 0.82;
}

.viz-stats {
  top: calc(var(--header-height) + 16px);
}

.viz-error canvas {
  opacity: 0.12;
}

.viz-error .viz-stats {
  display: block;
  max-width: 320px;
  color: var(--warning);
  border-color: rgba(245, 158, 11, 0.28);
}

.footer {
  position: relative;
  z-index: 2;
  scroll-snap-align: none;
  padding: 42px 24px;
}

.audio-controls {
  position: static;
}

.audio-btn {
  min-height: 42px;
  padding: 8px 13px;
  border-radius: 999px;
  transition: border-color var(--duration-fast), color var(--duration-fast), background var(--duration-fast);
}

.audio-btn:hover,
.audio-btn.playing {
  border-color: var(--accent-dim);
  color: var(--accent);
  background: var(--accent-glow);
  transform: none;
}

.audio-btn.playing {
  box-shadow: 0 0 0 1px rgba(79, 156, 247, 0.12), 0 0 24px rgba(79, 156, 247, 0.18);
}

:where(a, button, input, summary):focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

@media (max-width: 1080px) {
  .site-header {
    grid-template-columns: 1fr auto;
  }

  .chapter-status {
    display: none;
  }

  .section {
    grid-template-columns: minmax(0, 1fr) minmax(330px, 0.9fr);
  }

  .text-pane {
    padding-inline: clamp(26px, 4vw, 48px);
  }
}

@media (max-width: 900px) {
  :root {
    --header-height: 62px;
  }

  .site-header {
    padding-inline: 14px;
  }

  .site-brand {
    font-size: 0.92rem;
  }

  .chapter-menu summary,
  .audio-btn {
    min-height: 40px;
    padding: 7px 11px;
  }

  .section {
    display: block;
    min-height: 0;
  }

  .viz-pane {
    position: relative;
    top: auto;
    height: clamp(430px, 62svh, 600px);
    min-height: 430px;
  }

  .text-pane {
    min-height: auto;
    padding: 56px clamp(20px, 6vw, 48px) 78px;
  }

  .text-content {
    max-width: 680px;
    margin-inline: auto;
  }

  .section-title {
    font-size: clamp(2rem, 8vw, 3.1rem);
  }

  .viz-controls {
    max-height: 36%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 2px;
  }
}

@media (max-width: 620px) {
  .site-brand {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .audio-label {
    display: none;
  }

  .chapter-menu summary {
    font-size: 0;
  }

  .chapter-menu summary::before {
    content: 'Chapters';
    font-size: 0.76rem;
  }

  .hero {
    padding-inline: 18px;
  }

  .hero-title {
    font-size: clamp(3.15rem, 17vw, 5rem);
  }

  .hero-subtitle {
    font-size: 1rem;
  }

  .hero-actions {
    width: min(100%, 340px);
  }

  .hero-primary-action,
  .hero-secondary-action {
    width: 100%;
  }

  .scroll-indicator {
    display: none;
  }

  .viz-pane {
    height: clamp(420px, 58svh, 520px);
  }

  .viz-hint {
    display: none;
  }

  .viz-stats {
    right: 16px;
    max-width: calc(100% - 32px);
  }

  .viz-controls {
    left: 12px;
    right: 12px;
    bottom: 12px;
    gap: 8px;
  }

  .viz-btn,
  .switch-container,
  .slider-container {
    flex: 1 1 auto;
  }

  .viz-btn {
    white-space: normal;
    line-height: 1.25;
  }

  .slider-container {
    flex: 1 1 100%;
    min-width: 0;
    gap: 10px;
  }

  .slider-label {
    max-width: 34%;
    white-space: normal;
    line-height: 1.25;
  }

  input[type='range'] {
    min-width: 0;
    flex: 1;
  }

  .text-pane {
    padding-inline: 18px;
  }
}

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

  .hero canvas {
    opacity: 0.32;
  }
}

@media (prefers-contrast: more) {
  :root {
    --text-secondary: #c8cfde;
    --text-muted: #aab2c2;
    --border: #48546a;
  }

  .site-header,
  .viz-btn,
  .slider-container,
  .switch-container,
  .audio-btn,
  .chapter-menu summary {
    border-width: 2px;
  }
}
