/* ============================================================
   RoPE Demo — Design System
   Aesthetic: refined editorial minimalism, warm palette
   Two themes: "day" (cream paper) and "night" (charcoal)
   ============================================================ */

:root {
  /* --- Default: DAY theme (cream paper, deep cocoa ink) --- */
  --bg-0: #faf5ea;         /* primary paper */
  --bg-1: #f3ecdc;         /* slightly deeper */
  --bg-2: #ebe2cf;
  --surface: #fefaf0;
  --surface-soft: rgba(60, 42, 22, 0.03);
  --border: rgba(60, 42, 22, 0.10);
  --border-strong: rgba(60, 42, 22, 0.22);

  --text-0: #2a1f12;       /* deep cocoa — primary */
  --text-1: #4e3e2a;       /* secondary warm brown */
  --text-2: #87745a;       /* muted label */
  --text-3: #b8a685;       /* captions */

  /* Accents — query is deep amber, key is deep teal-blue */
  --amber: #b07218;        /* primary accent (query) — deeper for light bg */
  --amber-soft: #d99a3a;
  --amber-deep: #85540e;
  --blue: #2e6a91;         /* deeper teal-blue for light bg */
  --blue-soft: #4f8bb2;
  --rose: #b54f3e;

  /* Glow radial */
  --glow-tint: rgba(176, 114, 24, 0.10);

  /* Grain opacity */
  --grain-opacity: 0.5;
  --grain-blend: multiply;

  /* Typography */
  --font-display: "Fraunces", "Iowan Old Style", "Palatino", Georgia, serif;
  --font-ui: "Manrope", "Inter", -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", Menlo, monospace;

  /* Motion */
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);

  /* Shadow tokens — day uses softer, darker-tinted shadow */
  --shadow-panel: 0 40px 80px -40px rgba(60, 42, 22, 0.25),
                  0 1px 0 0 rgba(255, 255, 255, 0.8) inset;
}

/* --- NIGHT theme --- */
:root[data-theme="night"] {
  --bg-0: #0e0c0a;
  --bg-1: #14110e;
  --bg-2: #1b1713;
  --surface: #1f1a15;
  --surface-soft: rgba(245, 239, 230, 0.03);
  --border: rgba(245, 239, 230, 0.08);
  --border-strong: rgba(245, 239, 230, 0.16);

  --text-0: #f7f1e6;
  --text-1: #d6cdbe;
  --text-2: #8e857a;
  --text-3: #5a5449;

  --amber: #e8a838;
  --amber-soft: #f0c674;
  --amber-deep: #ba7517;
  --blue: #5fa8d3;
  --blue-soft: #8cc3df;
  --rose: #d9756a;

  --glow-tint: rgba(232, 168, 56, 0.12);

  --grain-opacity: 0.35;
  --grain-blend: overlay;

  --shadow-panel: 0 40px 80px -30px rgba(0, 0, 0, 0.8),
                  0 1px 0 0 rgba(245, 239, 230, 0.04) inset;
}

/* ============================================================
   RESET + BASE
   ============================================================ */

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

html {
  scroll-behavior: smooth;
  font-size: 16px;
}

body {
  font-family: var(--font-ui);
  font-weight: 400;
  background: var(--bg-0);
  color: var(--text-0);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
  position: relative;
  min-height: 100vh;
  transition: background 0.5s var(--ease), color 0.5s var(--ease);
}

/* Grain overlay — gives depth and prevents flat look */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  opacity: var(--grain-opacity);
  mix-blend-mode: var(--grain-blend);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.96  0 0 0 0 0.94  0 0 0 0 0.9  0 0 0 0.07 0'/></filter><rect width='160' height='160' filter='url(%23n)'/></svg>");
  transition: opacity 0.5s var(--ease);
}

/* Warm radial glow from upper-left */
.radial-glow {
  position: fixed;
  top: -30vh;
  left: -20vw;
  width: 90vw;
  height: 90vh;
  pointer-events: none;
  z-index: 0;
  background: radial-gradient(
    ellipse at center,
    var(--glow-tint) 0%,
    transparent 60%
  );
  filter: blur(40px);
  transition: background 0.5s var(--ease);
}

/* Section-linking subtle lines */
section {
  position: relative;
  z-index: 2;
}

/* ============================================================
   HERO
   ============================================================ */

.hero {
  min-height: 94vh;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 80px;
  padding: 72px max(48px, 8vw) 96px;
  position: relative;
  z-index: 2;
  border-bottom: 1px solid var(--border);
}

.hero-inner {
  max-width: 640px;
  opacity: 0;
  animation: riseIn 1.4s var(--ease-out) 0.2s forwards;
}

.kicker {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--amber-soft);
  margin-bottom: 32px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.kicker::before {
  content: "";
  display: inline-block;
  width: 24px;
  height: 1px;
  background: var(--amber-soft);
}

.title {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(48px, 7vw, 88px);
  line-height: 0.96;
  letter-spacing: -0.03em;
  margin-bottom: 32px;
  font-variation-settings: "opsz" 144, "SOFT" 50, "WONK" 0;
}

.title-line-1 {
  display: block;
  color: var(--text-0);
}

.title-line-2 {
  display: block;
  font-style: italic;
  color: var(--amber);
  font-weight: 300;
  font-variation-settings: "opsz" 144, "SOFT" 100, "WONK" 1;
  margin-top: 4px;
}

.hero-lede {
  font-size: 19px;
  line-height: 1.55;
  color: var(--text-1);
  max-width: 540px;
  margin-bottom: 48px;
  font-weight: 300;
}

.hero-lede strong {
  color: var(--amber);
  font-weight: 500;
}

.hero-nav {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  color: var(--text-1);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s var(--ease);
}

.chip:hover {
  background: rgba(232, 168, 56, 0.08);
  border-color: var(--amber);
  color: var(--text-0);
  transform: translateY(-1px);
}

.chip-num {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--amber);
  letter-spacing: 0.08em;
}

.hero-orbit {
  flex-shrink: 0;
  opacity: 0;
  animation: riseIn 1.6s var(--ease-out) 0.6s forwards;
}

.hero-orbit canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

@keyframes riseIn {
  from {
    opacity: 0;
    transform: translateY(24px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ============================================================
   PANELS
   ============================================================ */

.panel {
  padding: 120px max(48px, 8vw);
  border-bottom: 1px solid var(--border);
  position: relative;
}

.panel:nth-of-type(even) {
  background: linear-gradient(
    180deg,
    transparent 0%,
    rgba(245, 239, 230, 0.012) 50%,
    transparent 100%
  );
}

.panel-head {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 40px;
  max-width: 1000px;
  margin: 0 auto 72px;
  align-items: start;
}

.panel-num {
  font-family: var(--font-display);
  font-weight: 300;
  font-size: 88px;
  line-height: 0.9;
  color: var(--amber);
  font-variation-settings: "opsz" 144, "SOFT" 100, "WONK" 1;
  font-style: italic;
}

.panel h2 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(32px, 4vw, 48px);
  line-height: 1.05;
  letter-spacing: -0.02em;
  margin-bottom: 20px;
  color: var(--text-0);
}

.panel-sub {
  font-size: 17px;
  line-height: 1.65;
  color: var(--text-1);
  max-width: 720px;
  font-weight: 300;
}

.panel-sub em {
  color: var(--amber-soft);
  font-style: normal;
  font-weight: 500;
}

.panel-body {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 56px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: start;
}

/* Panel 0 has a wide side-by-side canvas — give it most of the space */
#panel-0 .panel-body {
  grid-template-columns: 1.6fr 1fr;
  max-width: 1200px;
}

/* Panels 2 and 3 have wide horizontal canvases — give them more breathing room */
#panel-2 .panel-body,
#panel-3 .panel-body {
  grid-template-columns: 2fr 1fr;
}

/* ============================================================
   CANVAS WRAP
   ============================================================ */

.canvas-wrap {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--shadow-panel);
  transition: background 0.5s var(--ease), border-color 0.5s var(--ease),
    box-shadow 0.5s var(--ease);
}

.canvas-wrap.wide {
  /* canvas-wrap grows to fit its canvas naturally */
}

.canvas-wrap::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(232, 168, 56, 0.4) 50%,
    transparent 100%
  );
}

.canvas-wrap canvas {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
}

/* Panel 0 wants its canvas at natural size, centered (no stretch) */
.canvas-wrap.center-canvas {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 800px;
}
.canvas-wrap.center-canvas canvas {
  width: auto;
  max-width: 100%;
}
.canvas-wrap.center-canvas .canvas-caption {
  align-self: stretch;
}

.canvas-caption {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-2);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  align-items: center;
  letter-spacing: 0.02em;
}

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 4px;
}

.dot-q {
  background: var(--amber);
  box-shadow: 0 0 8px rgba(232, 168, 56, 0.5);
}

.dot-k {
  background: var(--blue);
  box-shadow: 0 0 8px rgba(95, 168, 211, 0.5);
}

.dot-dot {
  background: var(--rose);
  box-shadow: 0 0 8px rgba(217, 117, 106, 0.5);
}

/* ============================================================
   CONTROLS
   ============================================================ */

.controls {
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.control-group.inline {
  gap: 14px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-2);
  font-weight: 500;
}

.control-label em {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 400;
  color: var(--amber-soft);
  font-size: 15px;
  text-transform: none;
  letter-spacing: 0;
}

.control-val {
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--amber);
  font-weight: 500;
  letter-spacing: -0.01em;
}

/* Custom range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 36px;
  background: transparent;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  width: 100%;
  height: 2px;
  background: var(--border-strong);
  border-radius: 1px;
}

input[type="range"]::-moz-range-track {
  width: 100%;
  height: 2px;
  background: var(--border-strong);
  border-radius: 1px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  background: var(--amber);
  margin-top: -9px;
  box-shadow:
    0 0 0 3px var(--bg-0),
    0 0 0 4px var(--amber-deep),
    0 4px 12px rgba(176, 114, 24, 0.4);
  transition: all 0.2s var(--ease);
}

input[type="range"]::-moz-range-thumb {
  height: 20px;
  width: 20px;
  border: none;
  border-radius: 50%;
  background: var(--amber);
  box-shadow:
    0 0 0 3px var(--bg-0),
    0 0 0 4px var(--amber-deep),
    0 4px 12px rgba(176, 114, 24, 0.4);
  transition: all 0.2s var(--ease);
}

input[type="range"]:hover::-webkit-slider-thumb,
input[type="range"]:active::-webkit-slider-thumb {
  transform: scale(1.1);
  box-shadow:
    0 0 0 3px var(--bg-0),
    0 0 0 4px var(--amber),
    0 6px 20px rgba(176, 114, 24, 0.55);
}

.control-hint {
  font-size: 13px;
  color: var(--text-2);
  line-height: 1.5;
  font-weight: 300;
}

/* Custom switch */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
}

.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-slider {
  position: relative;
  width: 40px;
  height: 22px;
  background: var(--border-strong);
  border-radius: 999px;
  transition: background 0.3s var(--ease);
}

.switch-slider::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  background: var(--bg-0);
  border-radius: 50%;
  transition: all 0.3s var(--ease);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.switch input:checked + .switch-slider {
  background: var(--amber-deep);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(18px);
  background: var(--amber);
  box-shadow: 0 0 8px var(--glow-tint), 0 1px 3px rgba(0, 0, 0, 0.2);
}

.switch-text {
  font-size: 14px;
  color: var(--text-1);
  font-weight: 500;
}

/* Button row for Panel 0 actions */
.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.action-btn {
  font-family: var(--font-ui);
  font-weight: 500;
  font-size: 14px;
  padding: 12px 20px;
  border-radius: 999px;
  background: var(--amber);
  color: var(--bg-0);
  border: none;
  cursor: pointer;
  transition: all 0.25s var(--ease);
  letter-spacing: 0.01em;
  box-shadow: 0 4px 14px -4px var(--glow-tint);
}

.action-btn:hover {
  transform: translateY(-1px);
  background: var(--amber-deep);
  box-shadow: 0 6px 20px -4px var(--glow-tint);
}

.action-btn:active {
  transform: translateY(0);
}

.action-btn.secondary {
  background: transparent;
  color: var(--text-1);
  border: 1px solid var(--border-strong);
  box-shadow: none;
}

.action-btn.secondary:hover {
  background: var(--surface-soft);
  color: var(--text-0);
  border-color: var(--amber);
  box-shadow: none;
}

/* Inline monospace, for displaying token strings in prose */
.mono-text {
  font-family: var(--font-mono);
  font-size: 0.92em;
  color: var(--amber-deep);
  font-weight: 500;
  padding: 2px 6px;
  background: var(--surface-soft);
  border-radius: 3px;
}

/* Readout */
.readout {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.readout-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.readout-key {
  font-size: 13px;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.readout-val {
  font-size: 14px;
  color: var(--amber-soft);
  font-weight: 500;
}

.readout-val.mono {
  font-family: var(--font-mono);
}

/* Aha box */
.aha {
  position: relative;
  background: linear-gradient(
    135deg,
    var(--glow-tint) 0%,
    transparent 100%
  );
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 22px 22px 22px 26px;
  overflow: hidden;
  transition: background 0.5s var(--ease), border-color 0.5s var(--ease);
}

.aha::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 2px;
  background: linear-gradient(
    180deg,
    var(--amber) 0%,
    var(--amber-deep) 100%
  );
}

.aha-label {
  font-family: var(--font-display);
  font-style: italic;
  font-size: 13px;
  color: var(--amber);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
  text-transform: lowercase;
  font-weight: 400;
}

.aha p {
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-1);
  font-weight: 300;
}

.aha strong {
  color: var(--text-0);
  font-weight: 500;
}

.aha em {
  color: var(--amber-soft);
  font-style: italic;
}

/* ============================================================
   OUTRO
   ============================================================ */

.outro {
  padding: 120px max(48px, 8vw);
  position: relative;
  z-index: 2;
  background: linear-gradient(
    180deg,
    transparent 0%,
    rgba(232, 168, 56, 0.03) 100%
  );
}

.outro-inner {
  max-width: 760px;
  margin: 0 auto;
}

.outro h3 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(28px, 3vw, 40px);
  margin-bottom: 32px;
  color: var(--text-0);
  letter-spacing: -0.02em;
}

.outro ol {
  list-style: none;
  counter-reset: takeaway;
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-bottom: 56px;
}

.outro ol li {
  counter-increment: takeaway;
  padding-left: 64px;
  position: relative;
  font-size: 17px;
  line-height: 1.6;
  color: var(--text-1);
  font-weight: 300;
}

.outro ol li::before {
  content: counter(takeaway, decimal-leading-zero);
  position: absolute;
  left: 0;
  top: 0;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--amber);
  letter-spacing: 0.04em;
  width: 48px;
  text-align: right;
  font-weight: 500;
}

.outro ol li strong {
  color: var(--text-0);
  font-weight: 500;
}

.credits {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-3);
  letter-spacing: 0.04em;
  padding-top: 32px;
  border-top: 1px solid var(--border);
}

.credits em {
  color: var(--text-2);
  font-style: italic;
}

/* ============================================================
   MATH RENDERING
   ============================================================ */

.katex {
  font-size: 1.05em !important;
  color: var(--text-0);
}

.katex-display {
  margin: 1.2em 0 !important;
  padding: 20px 24px;
  background: var(--surface-soft);
  border-radius: 8px;
  border-left: 2px solid var(--amber-deep);
  overflow-x: auto;
  overflow-y: hidden;
}

.katex-display > .katex {
  color: var(--text-0);
}

/* ============================================================
   THEME TOGGLE
   ============================================================ */

.theme-toggle {
  position: fixed;
  top: 28px;
  right: 32px;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 0;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.5s var(--ease), border-color 0.5s var(--ease),
    transform 0.3s var(--ease), box-shadow 0.3s var(--ease);
  box-shadow: 0 4px 16px rgba(60, 42, 22, 0.08);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

:root[data-theme="night"] .theme-toggle {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.theme-toggle:hover {
  transform: translateY(-1px);
  border-color: var(--amber);
}

.theme-toggle button {
  position: relative;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  color: var(--text-2);
  transition: color 0.3s var(--ease), background 0.3s var(--ease);
  padding: 0;
}

.theme-toggle button:hover {
  color: var(--text-0);
}

.theme-toggle button.active {
  color: var(--amber);
  background: var(--glow-tint);
}

.theme-toggle button svg {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Hide toggle label text on very small screens */
@media (max-width: 600px) {
  .theme-toggle {
    top: 16px;
    right: 16px;
  }
}

/* ============================================================
   RESPONSIVE
   ============================================================ */

@media (max-width: 1100px) {
  .hero {
    grid-template-columns: 1fr;
    gap: 48px;
    padding: 60px 32px;
    min-height: auto;
  }

  .hero-orbit {
    order: -1;
    justify-self: center;
  }

  .hero-orbit canvas {
    width: 280px;
    height: 280px;
  }

  .panel {
    padding: 80px 32px;
  }

  .panel-body {
    grid-template-columns: 1fr;
    gap: 40px;
  }

  .panel-head {
    grid-template-columns: 1fr;
    gap: 16px;
    margin-bottom: 48px;
  }

  .panel-num {
    font-size: 56px;
  }
}

@media (max-width: 600px) {
  .hero {
    padding: 48px 24px;
  }

  .title {
    font-size: 44px;
  }

  .panel {
    padding: 64px 24px;
  }

  .panel h2 {
    font-size: 28px;
  }

  .canvas-wrap {
    padding: 20px;
  }

  .outro {
    padding: 80px 24px;
  }

  .outro ol li {
    padding-left: 48px;
  }
}
