:root {
  --bg: #05070c;
  --ink: #e9edf6;
  --muted: #97a2b5;
  --dim: #5d6980;
  --faint: #3a4459;
  --order: #57b8ff;
  --chaos: #ff6b4a;
  --paper: #e7e3da;
  --line: rgba(255, 255, 255, 0.09);
  --line-strong: rgba(255, 255, 255, 0.16);
  --serif: Georgia, "Times New Roman", "Liberation Serif", "DejaVu Serif", serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", "DejaVu Sans", sans-serif;
  --sidew: 76px;
}

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

html { scroll-behavior: smooth; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

::selection { background: rgba(255, 107, 74, 0.35); color: #fff; }
a { color: inherit; }

/* ---------- scrollbar ---------- */
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #1c2434; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #283350; }

/* ---------- chaos progress bar (top edge) ---------- */
.progress {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 3px;
  z-index: 90;
  background: linear-gradient(90deg, var(--order), #57e0bb 30%, #f0d45a 65%, var(--chaos));
  transform-origin: 0 50%;
  transform: scaleX(0);
}

/* ---------- fixed background canvas ---------- */
.bg-stage {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}
#bg-canvas {
  width: 100%;
  height: 100%;
  display: block;
  opacity: 0.26;
}
.bg-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, rgba(5,7,12,0.86) 0%, rgba(5,7,12,0.4) 30%, rgba(5,7,12,0.35) 60%, rgba(5,7,12,0.92) 100%);
}

/* ---------- side nav ---------- */
.side-nav {
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  width: var(--sidew);
  z-index: 40;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 24px 0;
  background: rgba(5,7,12,0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-right: 1px solid var(--line);
}
.side-nav a {
  display: block;
  text-align: center;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--dim);
  padding: 7px 0;
  letter-spacing: 0.06em;
  transition: color 0.2s ease, background 0.2s ease;
  position: relative;
}
.side-nav a:hover { color: var(--ink); background: rgba(255,255,255,0.04); text-decoration: none; }
.side-nav a.active { color: var(--chaos); background: rgba(255,107,74,0.08); }
.side-nav a::after {
  content: attr(data-title);
  position: absolute;
  left: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  font-family: var(--sans);
  font-size: 0.78rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--line);
  padding: 4px 10px;
  border-radius: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 60;
}
.side-nav a:hover::after { opacity: 1; }
.side-nav .brand {
  font-size: 0.9rem;
  color: var(--ink);
  font-weight: 700;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 10px;
}
.side-nav .brand span { color: var(--chaos); }

main { margin-left: var(--sidew); position: relative; z-index: 1; }

/* ---------- hero ---------- */
.hero {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 10vh 8vw;
  max-width: 1200px;
  margin: 0 auto;
}
.hero .topline {
  font-family: var(--mono);
  font-size: 0.82rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--chaos);
  margin-bottom: 4vh;
}
.hero .big {
  font-family: var(--mono);
  font-size: clamp(2.6rem, 10.5vw, 8.5rem);
  font-weight: 700;
  line-height: 1.02;
  letter-spacing: -0.03em;
  background: linear-gradient(115deg, var(--ink) 20%, var(--order) 55%, var(--chaos) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
.hero .sub {
  margin-top: 3vh;
  max-width: 560px;
  font-size: clamp(1.05rem, 2.4vw, 1.35rem);
  color: var(--muted);
  line-height: 1.6;
  font-family: var(--serif);
}
.hero .sub b { color: var(--paper); font-weight: 600; }
.hero .hint {
  margin-top: 7vh;
  font-family: var(--mono);
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--dim);
  display: flex;
  align-items: center;
  gap: 10px;
}
.hero .hint::after {
  content: "";
  width: 60px;
  height: 1px;
  background: var(--dim);
  display: inline-block;
  animation: drip 1.8s ease-in-out infinite;
}
@keyframes drip {
  0%, 100% { transform: scaleX(1); opacity: 0.9; }
  50% { transform: scaleX(0.2); opacity: 0.4; }
}

/* ---------- chapters ---------- */
.chapter {
  position: relative;
  padding: 14vh 8vw 8vh;
  max-width: 900px;
  margin: 0 auto;
}
.chapter.wide { max-width: none; margin: 0; }
.wide-inner { max-width: 1200px; margin: 0 auto; }

.ghost {
  position: absolute;
  pointer-events: none;
  user-select: none;
  font-family: var(--serif);
  font-size: clamp(9rem, 30vw, 22rem);
  line-height: 1;
  font-weight: 700;
  color: rgba(255,255,255,0.035);
  top: 2vh;
  right: 4vw;
  transform: translateY(0);
}
.ghost-left { right: auto; left: -2vw; }

.chap-label {
  font-family: var(--mono);
  font-size: 0.78rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--order);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.chap-label::before {
  content: "";
  width: 26px; height: 1px;
  background: rgba(87,184,255,0.5);
}

.chapter h2 {
  font-family: var(--serif);
  font-weight: 700;
  font-size: clamp(1.8rem, 4.4vw, 3rem);
  line-height: 1.18;
  letter-spacing: -0.01em;
  color: var(--paper);
  margin-bottom: 4vh;
}
.chapter h2 em { color: var(--chaos); font-style: normal; }
.chapter h2 .cool { color: var(--order); }

/* ---------- notes / prose ---------- */
.note {
  background: rgba(6,9,14,0.72);
  border: 1px solid var(--line);
  border-left: 3px solid var(--order);
  padding: 22px 26px;
  margin: 3.2vh 0;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}
.note.warm { border-left-color: var(--chaos); }

.prose { color: var(--muted); font-size: clamp(1.02rem, 1.6vw, 1.12rem); }
.prose p { margin-bottom: 18px; }
.prose b, .prose strong { color: var(--ink); font-weight: 600; }
.prose .mono { color: var(--order); font-family: var(--mono); font-size: 0.95em; }
.prose ul { margin: 14px 0 18px 4px; list-style: none; }
.prose li { padding-left: 28px; position: relative; margin-bottom: 10px; }
.prose li::before {
  content: "→";
  position: absolute;
  left: 0;
  color: var(--chaos);
  font-family: var(--mono);
}

.eq {
  font-family: var(--mono);
  font-size: clamp(1.05rem, 2.2vw, 1.35rem);
  color: var(--ink);
  background: rgba(6,9,14,0.7);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 20px 24px;
  margin: 3.5vh 0;
  overflow-x: auto;
  white-space: nowrap;
  letter-spacing: 0.01em;
}
.eq .hl { color: var(--chaos); }
.eq .c { color: var(--dim); font-size: 0.8em; }

.pull {
  font-family: var(--serif);
  font-style: italic;
  font-size: clamp(1.2rem, 2.6vw, 1.6rem);
  color: var(--paper);
  border-left: 3px solid var(--chaos);
  padding: 8px 0 8px 26px;
  margin: 5vh 0 5vh 6vw;
  max-width: 620px;
}

.margin-note {
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--dim);
  max-width: 340px;
  padding: 12px 0 12px 16px;
border-left: 1px solid var(--line-strong);
  margin: 4vh 0 4vh auto;
  font-style: normal;
}
.margin-note b { color: var(--chaos); font-weight: 600; }

/* ---------- lab: equipment & record cards ---------- */
.lab-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(270px, 1fr));
  gap: 16px;
  margin: 4vh 0;
}
.record {
  background: rgba(6,9,14,0.7);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 22px 22px 20px;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  position: relative;
  overflow: hidden;
}
.record::before {
  content: attr(data-num);
  position: absolute;
  right: -8px;
  top: -14px;
  font-family: var(--serif);
  font-size: 5.2rem;
  font-weight: 700;
  color: rgba(255,255,255,0.04);
  pointer-events: none;
}
.record .stamp {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--chaos);
  display: block;
  margin-bottom: 10px;
}
.record .year {
  font-family: var(--mono);
  color: var(--order);
  font-size: 1.3rem;
  font-weight: 700;
  display: block;
  margin-bottom: 2px;
}
.record h3 { font-family: var(--serif); font-size: 1.08rem; color: var(--paper); margin-bottom: 8px; }
.record p { color: var(--muted); font-size: 0.94rem; }

/* ---------- field notes ---------- */
.field-note {
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 20px 22px;
  background: rgba(6,9,14,0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.field-note .tag {
  font-family: var(--mono);
  font-size: 0.7rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--order);
  display: block;
  margin-bottom: 10px;
}
.field-note h3 { font-family: var(--serif); color: var(--paper); font-size: 1.05rem; margin-bottom: 6px; }
.field-note p { color: var(--muted); font-size: 0.92rem; }

/* ---------- experiments ---------- */
.experiment {
  background: rgba(6,9,14,0.78);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 24px 24px 8px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  margin: 4vh 0;
}
.controls { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; margin-bottom: 6px; }
.controls .rlabel {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--paper);
  min-width: 118px;
  white-space: nowrap;
}
.controls .rlabel b { color: var(--chaos); }
.range-wrap { flex: 1; min-width: 220px; }
input[type="range"] {
  width: 100%;
  accent-color: var(--chaos);
  height: 5px;
  cursor: pointer;
}
.range-labels {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--dim);
  margin-top: 3px;
}
.regime-out {
  font-family: var(--mono);
  font-size: 1.05rem;
  color: var(--order);
  min-width: 200px;
  text-align: right;
  white-space: nowrap;
}
.regime-out._chaos { color: var(--chaos); }
.regime-out._hot { color: #ffb066; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 16px; }
.chip {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 13px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.chip:hover { border-color: var(--order); color: var(--ink); }
.chip.active { border-color: var(--chaos); color: var(--chaos); background: rgba(255,107,74,0.08); }

.act-row { display: flex; gap: 10px; flex-wrap: wrap; margin: 4px 0 16px; }
.act {
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--ink);
  background: rgba(87,184,255,0.08);
  border: 1px solid rgba(87,184,255,0.35);
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.act:hover { background: rgba(87,184,255,0.16); }
.act.on { border-color: var(--chaos); color: var(--chaos); background: rgba(255,107,74,0.12); }

.viz { width: 100%; display: block; background: rgba(4,6,10,0.6); border: 1px solid var(--line); border-radius: 8px; }
#bifurcation { height: min(62vh, 640px); min-height: 320px; cursor: crosshair; }
#orbit, #cobweb { height: 240px; }

.viz-sub { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 16px 0; }
.viz-panel h4 {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--dim);
  margin: 10px 0 6px;
  display: flex;
  justify-content: space-between;
}
.viz-panel h4 .x { color: var(--order); }

.field-label { font-family: var(--mono); font-size: 0.76rem; color: var(--dim); margin: 6px 2px; display: flex; justify-content: space-between; }

.xn-readout { font-family: var(--mono); font-size: 1.3rem; color: var(--chaos); letter-spacing: 0.02em; }

/* ---------- manuscript table ---------- */
.ms-table { margin: 4.5vh 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: rgba(6,9,14,0.6); backdrop-filter: blur(5px); }
.ms-title {
  font-family: var(--mono);
  font-size: 0.74rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--dim);
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,0.02);
}
table, th, td { border-collapse: collapse; }
.table-wrap table { width: 100%; font-size: 0.94rem; min-width: 540px; }
th, td { text-align: left; padding: 11px 20px; border-bottom: 1px solid var(--line); }
th { font-family: var(--mono); font-size: 0.68rem; letter-spacing: 0.16em; text-transform: uppercase; color: var(--dim); font-weight: 600; }
td.num { font-family: var(--mono); color: var(--ink); }
td.hl { font-family: var(--mono); color: var(--chaos); }
tr:hover td { background: rgba(255,255,255,0.025); }
tr.end td { border-bottom: none; }
td.delta { color: var(--order); font-family: var(--mono); }

/* ---------- game ---------- */
.game-wrap { margin: 4vh 0; }
.game-digits {
  font-family: var(--mono);
  font-size: clamp(1rem, 3vw, 1.6rem);
  letter-spacing: 0.06em;
  line-height: 1.9;
  word-break: break-all;
  color: var(--order);
  background: rgba(6,9,14,0.7);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 20px 24px;
  min-height: 96px;
}
.game-digits .cursor { color: var(--chaos); animation: blink 1s step-start infinite; }
@keyframes blink { 50% { opacity: 0; } }
.game-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 18px 0 10px;
}
.game-btns button {
  font-family: var(--mono);
  width: 46px; height: 46px;
  font-size: 1.1rem;
  color: var(--ink);
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.12s ease;
}
.game-btns button:hover { border-color: var(--order); color: var(--order); }
.game-btns button.right { border-color: var(--okc, #54e08a); color: #54e08a; background: rgba(84,224,138,0.1); }
.game-btns button.wrong { border-color: var(--chaos); color: var(--chaos); background: rgba(255,107,74,0.12); }
.game-score {
  font-family: var(--mono);
  font-size: 0.9rem;
  color: var(--muted);
  margin-top: 10px;
}
.game-score b { color: var(--paper); }

/* ---------- digits ---------- */
.digit-box {
  font-family: var(--mono);
  font-size: clamp(0.8rem, 1.6vw, 0.92rem);
  line-height: 2.1;
  color: var(--order);
  background: rgba(6,9,14,0.6);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px 20px;
  word-break: break-all;
  max-height: 230px;
  overflow-y: auto;
  margin: 12px 0 8px;
}
.digit-box .whole { color: var(--paper); font-weight: 700; }
.copy-btn {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 14px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.copy-btn:hover { border-color: var(--order); color: var(--ink); }
.copy-btn.copied { border-color: #54e08a; color: #54e08a; }

/* ---------- references ---------- */
.ref-list { margin: 3vh 0; }
.ref {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 6px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.95rem;
  transition: color 0.15s ease;
}
.ref:hover { color: var(--ink); text-decoration: none; }
.ref .s { font-family: var(--mono); color: var(--dim); font-size: 0.78rem; display: block; margin-top: 2px; }
.ref .go { color: var(--order); font-family: var(--mono); }

/* ---------- final ---------- */
.final-quote {
  font-family: var(--serif);
  font-size: clamp(1.6rem, 4.4vw, 3rem);
  line-height: 1.35;
  color: var(--paper);
  max-width: 780px;
  margin: 0 0 4vh;
}
.final-quote em { color: var(--chaos); }
.final-quote b { color: var(--order); font-weight: 700; font-family: var(--mono); font-size: 0.9em; }

footer {
  border-top: 1px solid var(--line);
  margin-top: 6vh;
  padding: 40px 6vw;
}
.footer-inner {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  justify-content: space-between;
  align-items: center;
  color: var(--dim);
  font-size: 0.82rem;
}
.footer-inner .links { display: flex; gap: 18px; flex-wrap: wrap; }
.footer-inner a { color: var(--muted); }
.footer-inner a:hover { color: var(--ink); text-decoration: underline; }

/* ---------- reveal ---------- */
.reveal { opacity: 0; transform: translateY(18px); transition: opacity 0.7s ease, transform 0.7s ease; }
.reveal.in { opacity: 1; transform: none; }
.reveal-slow { transition-duration: 1s; }

/* ---------- responsive ---------- */
@media (max-width: 980px) {
  .side-nav { display: none; }
  main { margin-left: 0; }
  .viz-sub { grid-template-columns: 1fr; }
  .chapter { padding: 9vh 5vw 6vh; }
  .regime-out { text-align: left; min-width: 0; }
  .controls { gap: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .reveal { opacity: 1; transform: none; transition: none; }
  .hero .hint::after, .game-digits .cursor { animation: none; }
}