/* ECEM — Interactive Companion
   Design system tokens + layout */

:root {
  /* Palette — warm parchment manuscript */
  --bg: #FBF8F2;
  --bg-tint: #F5EFE3;
  --panel: #FFFFFF;
  --rule: #E5DDCD;
  --rule-soft: #EFE8D8;

  --ink: #1A1612;
  --ink-mid: #4A4036;
  --ink-soft: #7A6E60;
  --ink-faint: #A89D8C;

  /* Accents — match published EPS palette */
  --ekpyrotic: oklch(0.55 0.16 30);    /* vermillion red */
  --ekpyrotic-soft: oklch(0.92 0.04 30);
  --bounce: oklch(0.35 0.13 260);       /* midnight blue */
  --bounce-soft: oklch(0.93 0.04 260);
  --matter: oklch(0.50 0.08 140);       /* sage neutral */
  --matter-soft: oklch(0.93 0.03 140);
  --scalar: oklch(0.55 0.10 70);        /* warm gold */
  --shear: oklch(0.45 0.08 320);        /* muted plum */

  /* Type */
  --font-serif: "Newsreader", "Charter", "Iowan Old Style", Georgia, serif;
  --font-mono: "IBM Plex Mono", "JetBrains Mono", ui-monospace, monospace;

  /* Geometry */
  --col: 760px;
  --col-wide: 1080px;
  --col-figure: 1320px;
  --pad: clamp(16px, 3vw, 36px);
  --radius: 4px;
  --radius-lg: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-serif);
  font-optical-sizing: auto;
  font-size: 18px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  min-height: 100vh;
}

/* ============== Layout ============== */
.page {
  max-width: var(--col-figure);
  margin: 0 auto;
  padding: clamp(28px, 4vw, 56px) var(--pad) 120px;
  display: grid;
  grid-template-columns: 1fr min(var(--col), 100%) 1fr;
  column-gap: 24px;
}
.page > * {
  grid-column: 2;
}
.page > .wide {
  grid-column: 1 / -1;
  max-width: var(--col-figure);
  margin: 0 auto;
  width: 100%;
}
.page > .col-wide {
  grid-column: 1 / -1;
  max-width: var(--col-wide);
  margin: 0 auto;
  width: 100%;
}

/* ============== Typography ============== */
h1, h2, h3, h4 {
  font-family: var(--font-serif);
  font-weight: 500;
  letter-spacing: -0.01em;
  color: var(--ink);
  margin: 0;
}

h1.title {
  font-size: clamp(34px, 5vw, 58px);
  line-height: 1.08;
  letter-spacing: -0.022em;
  font-weight: 450;
  text-wrap: balance;
}

h2 {
  font-size: 30px;
  line-height: 1.2;
  font-weight: 450;
  letter-spacing: -0.015em;
  margin-top: 84px;
  margin-bottom: 14px;
  text-wrap: balance;
}

h3 {
  font-size: 21px;
  font-weight: 500;
  line-height: 1.3;
  margin-top: 40px;
  margin-bottom: 12px;
}

h4 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-family: var(--font-mono);
  margin-bottom: 8px;
}

p { margin: 0 0 1em 0; text-wrap: pretty; }

a {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-color: var(--ink-faint);
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
}
a:hover { text-decoration-color: var(--ekpyrotic); }

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

code, .mono {
  font-family: var(--font-mono);
  font-size: 0.88em;
}

/* ============== Eyebrow / meta ============== */
.eyebrow {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-weight: 500;
}

.lede {
  font-size: clamp(18px, 2.1vw, 22px);
  line-height: 1.45;
  color: var(--ink-mid);
  text-wrap: pretty;
  font-weight: 350;
  font-style: italic;
}

.byline {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}

/* ============== Hero ============== */
.hero {
  grid-column: 1 / -1;
  width: 100%;
  max-width: min(1120px, 100%);
  margin-left: auto;
  margin-right: auto;
  padding-bottom: 32px;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 8px;
}
.hero .eyebrow {
  margin-bottom: 18px;
  display: block;
  text-align: center;
}
.hero h1.title {
  margin-bottom: 22px;
  max-width: 100%;
  text-align: center;
  font-size: clamp(40px, 4.45vw, 64px);
}
.hero .lede {
  max-width: 100%;
}
.hero .meta-grid {
  margin-top: 30px;
  display: grid;
  grid-template-columns:
    minmax(190px, 0.9fr)
    minmax(250px, 1.05fr)
    minmax(235px, 1fr)
    minmax(285px, 1.15fr);
  gap: 20px 28px;
}
.hero .meta-grid > div { font-family: var(--font-mono); font-size: 12px; color: var(--ink-mid); min-width: 0; }
.hero .meta-journal,
.hero .meta-doi,
.hero .meta-dates {
  white-space: nowrap;
}
.hero .meta-doi a {
  white-space: nowrap;
  overflow-wrap: normal;
  word-break: keep-all;
}
.hero .meta-keywords {
  grid-column: 2 / -1;
}
.hero .keyword-line {
  display: block;
  white-space: nowrap;
}
.hero .meta-grid label {
  display: block;
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 6px;
}

/* ============== Abstract block ============== */
.abstract {
  grid-column: 1 / -1;
  width: 100%;
  max-width: min(1120px, 100%);
  margin-left: auto;
  margin-right: auto;
  background: var(--bg-tint);
  border-left: 3px solid var(--ekpyrotic);
  padding: 24px 28px;
  margin-top: 36px;
  margin-bottom: 36px;
  border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
}
.abstract h4 { margin-top: 0; color: var(--ekpyrotic); }
.abstract p { font-size: 17px; line-height: 1.56; margin: 0 0 14px 0; }
.abstract p:last-child { margin-bottom: 0; }

@media (max-width: 1100px) {
  .hero .meta-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .hero .meta-keywords {
    grid-column: 1 / -1;
  }
  .hero .keyword-line {
    white-space: normal;
  }
}

@media (max-width: 640px) {
  .page {
    column-gap: 0;
    padding-bottom: 80px;
  }
  h1.title {
    line-height: 1.12;
  }
  .hero h1.title {
    text-align: left;
    font-size: clamp(34px, 10vw, 44px);
  }
  h2 {
    font-size: 25px;
    margin-top: 58px;
  }
  .hero {
    padding-bottom: 24px;
  }
  .hero .meta-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  .hero .meta-keywords {
    grid-column: auto;
  }
  .hero .meta-journal,
  .hero .meta-doi,
  .hero .meta-dates {
    white-space: normal;
  }
  .abstract {
    padding: 20px;
  }
}

/* ============== Pull-quote / margin note ============== */
.note {
  font-size: 14px;
  color: var(--ink-mid);
  line-height: 1.5;
  font-style: italic;
  border-left: 2px solid var(--rule);
  padding: 4px 0 4px 18px;
  margin: 24px 0;
}

/* ============== Figure container ============== */
.figure {
  margin: 48px 0;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.figure-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px 14px;
  border-bottom: 1px solid var(--rule-soft);
}
.figure-head .label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ekpyrotic);
  font-weight: 600;
}
.figure-head .title {
  font-family: var(--font-serif);
  font-size: 17px;
  font-weight: 500;
  color: var(--ink);
  line-height: 1.3;
}
.figure-head .meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
}
.figure-body {
  padding: 24px;
}
.figure-caption {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-soft);
  line-height: 1.55;
  padding: 0 24px 20px;
  border-top: 1px solid var(--rule-soft);
  padding-top: 16px;
}
.journal-figure-img {
  display: block;
  width: 100%;
  height: auto;
  background: #fff;
  border: 1px solid var(--rule-soft);
}
.figure-body > .journal-figure-img {
  width: auto;
  max-width: min(820px, 100%);
  max-height: min(680px, 62vh);
  margin: 0 auto;
  object-fit: contain;
}
#fig1 .journal-figure-img,
#lyapunov .journal-figure-img {
  max-width: min(860px, 100%);
}
#basin > .figure-body:first-of-type .journal-figure-img {
  max-width: min(700px, 100%);
  margin: 0 auto;
}
.journal-pair {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}
.journal-pair figure {
  margin: 0;
  min-width: 0;
}
.journal-pair figcaption {
  margin-top: 8px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-soft);
  line-height: 1.45;
}
@media (max-width: 760px) {
  .journal-pair { grid-template-columns: 1fr; }
}

.cycle-layout,
.phase-companion-grid,
.lyapunov-companion-grid {
  display: grid;
  gap: 22px;
  align-items: start;
}
.cycle-layout {
  grid-template-columns: minmax(0, 1fr) minmax(280px, 0.9fr);
  align-items: center;
}
.phase-companion-grid {
  grid-template-columns: minmax(0, 1fr) minmax(220px, 240px);
}
.lyapunov-companion-grid {
  grid-template-columns: minmax(0, 1fr) minmax(240px, 280px);
}

@media (max-width: 960px) {
  .page > .col-wide,
  .page > .wide {
    max-width: 100%;
  }
  .journal-pair {
    grid-template-columns: 1fr;
  }
  .journal-pair .journal-figure-img {
    width: auto;
    max-width: min(760px, 100%);
    max-height: min(560px, 58vh);
    margin: 0 auto;
    object-fit: contain;
  }
  .cycle-layout,
  .phase-companion-grid,
  .lyapunov-companion-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .figure {
    margin: 32px 0;
    border-radius: var(--radius);
  }
  .figure-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
    padding: 16px 18px 12px;
  }
  .figure-body {
    padding: 16px;
  }
  .figure-caption {
    padding-left: 18px;
    padding-right: 18px;
  }
  .figure-body > .journal-figure-img,
  .journal-pair .journal-figure-img {
    max-height: none;
  }
}

/* ============== Controls ============== */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 16px 28px;
  padding: 16px 24px;
  background: var(--bg-tint);
  border-top: 1px solid var(--rule-soft);
  align-items: center;
}
.control { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.control label {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
}
.control label .val {
  color: var(--ink);
  letter-spacing: 0;
  text-transform: none;
  font-size: 12px;
  font-feature-settings: "tnum";
}
.control input[type="range"] {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  min-width: 140px;
  height: 4px;
  background: var(--rule);
  border-radius: 2px;
  outline: none;
  margin: 8px 0;
}
.control input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--ink);
  cursor: pointer;
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 1px var(--ink);
}
.control input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--ink);
  cursor: pointer;
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 1px var(--ink);
}

button.btn, .btn {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 7px 12px;
  background: transparent;
  border: 1px solid var(--ink);
  color: var(--ink);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 120ms, color 120ms;
}
button.btn:hover { background: var(--ink); color: var(--bg); }
button.btn.active { background: var(--ink); color: var(--bg); }
button.btn.ghost { border-color: var(--rule); color: var(--ink-mid); }
button.btn.ghost:hover { border-color: var(--ink); }
button.btn.lyapunov-case-btn.active {
  background: var(--bounce-soft);
  border-color: var(--bounce);
  color: var(--bounce);
}
button.btn.lyapunov-case-btn.active:hover {
  background: var(--bounce-soft);
  color: var(--bounce);
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

@media (max-width: 640px) {
  .controls {
    align-items: stretch;
    gap: 14px;
    padding: 14px 16px;
  }
  .control {
    flex-basis: 100% !important;
  }
  .preset-row {
    width: 100%;
    margin-left: 0;
  }
  button.btn,
  .btn {
    min-height: 34px;
  }
}

/* ============== Legend ============== */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-mid);
  padding: 0 24px 16px;
}
.legend .swatch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.legend .dot {
  width: 14px;
  height: 3px;
  background: currentColor;
  border-radius: 1px;
}

/* ============== Section nav ============== */
.toc {
  position: fixed;
  top: 50%;
  right: 24px;
  transform: translateY(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}
.toc a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
  text-decoration: none;
  color: var(--ink-soft);
  opacity: 0.7;
  transition: opacity 120ms, color 120ms;
}
.toc a:hover, .toc a.active { color: var(--ink); opacity: 1; }
.toc a::before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 1px;
  background: currentColor;
  transition: width 160ms;
}
.toc a.active::before { width: 28px; background: var(--ekpyrotic); }

@media (max-width: 1280px) {
  .toc { display: none; }
}

/* ============== Section spacing ============== */
section {
  scroll-margin-top: 60px;
}

/* ============== Equation block ============== */
.eqn {
  background: var(--bg);
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  padding: 18px 24px;
  margin: 24px 0;
  text-align: center;
  font-size: 17px;
  overflow-x: auto;
}
.eqn .tag {
  float: right;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
}

/* ============== Inline math (KaTeX) ============== */
.katex { font-size: 1em !important; }
.katex,
.katex * {
  text-transform: none;
  letter-spacing: 0;
}

/* ============== Pills / tags ============== */
.tag-pill {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--rule);
  color: var(--ink-mid);
}
.tag-pill.bounce { background: var(--bounce-soft); border-color: transparent; color: var(--bounce); }
.tag-pill.ekpyrotic { background: var(--ekpyrotic-soft); border-color: transparent; color: var(--ekpyrotic); }
.tag-pill.matter { background: var(--matter-soft); border-color: transparent; color: var(--matter); }

/* ============== Cycle diagram ============== */
.cycle-svg { width: 100%; height: auto; display: block; }
.cycle-node {
  cursor: pointer;
  transition: filter 160ms;
}
.cycle-node:hover { filter: brightness(0.96); }
.cycle-node text { font-family: var(--font-serif); font-size: 14px; fill: var(--ink); }
.cycle-node rect { stroke: var(--ink); stroke-width: 1.2; }
.cycle-node.active rect { fill: var(--bounce-soft); stroke: var(--bounce); stroke-width: 2; }

.cycle-detail {
  margin-top: 16px;
  padding: 18px 22px;
  background: var(--bg-tint);
  border-radius: var(--radius);
  min-height: 110px;
  font-size: 15px;
  color: var(--ink-mid);
  line-height: 1.5;
}
.cycle-detail h4 { color: var(--bounce); margin-bottom: 6px; }
.cycle-detail .eq { font-family: var(--font-mono); font-size: 13px; color: var(--ink); }

.cycle-node-box {
  width: 100%;
  height: 100%;
  background: var(--bg);
  border: 1.2px solid var(--ink);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px 6px;
  cursor: pointer;
  text-align: center;
  box-sizing: border-box;
  transition: background 120ms, border-color 120ms;
}
.cycle-node-box.active {
  background: var(--bounce-soft);
  border-color: var(--bounce);
  border-width: 2px;
}
.cycle-node-box:hover { filter: brightness(0.97); }
.cycle-node-title {
  font-family: var(--font-serif);
  font-size: 14px;
  line-height: 1.1;
  color: var(--ink);
}
.cycle-node-cond {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-soft);
  margin-top: 3px;
  line-height: 1;
}
.cycle-node-cond .katex { font-size: 11px; color: var(--ink-soft); }

/* ============== Plot SVG ============== */
.plot-svg { width: 100%; height: auto; display: block; font-family: var(--font-mono); }
.plot-svg .axis text { font-size: 10px; fill: var(--ink-soft); font-family: var(--font-mono); }
.plot-svg .axis path, .plot-svg .axis line { stroke: var(--ink-faint); stroke-width: 0.8; fill: none; }
.plot-svg .grid line { stroke: var(--rule); stroke-width: 0.5; stroke-dasharray: 2 3; }
.plot-svg .label { font-size: 11px; fill: var(--ink-mid); }
.plot-svg .crosshair { stroke: var(--ekpyrotic); stroke-width: 1; stroke-dasharray: 3 2; }
.plot-svg .anno { font-size: 11px; fill: var(--ink-mid); font-family: var(--font-mono); }

/* ============== Bounce timeline panels ============== */
.timeline-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.timeline-grid > .panel { background: var(--bg); border: 1px solid var(--rule-soft); border-radius: var(--radius); padding: 12px; }
.timeline-grid > .panel h4 { margin: 2px 0 8px; color: var(--ink-mid); font-size: 12px; text-transform: none; letter-spacing: 0; font-family: var(--font-mono); }
.timeline-grid > .panel h4 .katex { font-size: 1.05em; color: var(--ink); margin-right: 2px; }
@media (max-width: 760px) {
  .timeline-grid { grid-template-columns: 1fr; }
}

@media (min-width: 1200px) {
  .timeline-grid {
    max-width: 980px;
    margin: 0 auto;
  }
}

.scrubber {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 24px;
  background: var(--bg-tint);
  border-top: 1px solid var(--rule-soft);
}
.scrubber .play {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--ink); color: var(--bg);
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
}
.scrubber input[type="range"] { flex: 1; height: 4px; }
.scrubber .t-readout {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-mid);
  min-width: 90px;
  text-align: right;
  font-feature-settings: "tnum";
}

/* ============== Basin map ============== */
.basin-wrap {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 20px;
  align-items: start;
}
@media (max-width: 880px) {
  .basin-wrap { grid-template-columns: 1fr; }
}
.basin-plot-row {
  min-width: 0;
  display: flex;
  gap: 4px;
  align-items: stretch;
}
.basin-heatmap {
  flex: 1;
  min-width: 0;
}
.basin-colorbar {
  width: 92px;
  flex-shrink: 0;
}
.basin-sidebar {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-mid);
  background: var(--bg);
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  padding: 16px;
  min-height: 200px;
}
.basin-sidebar .row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dotted var(--rule); gap: 12px; white-space: nowrap; font-size: 12px; }
.basin-sidebar .row > span:first-child { color: var(--ink-soft); flex-shrink: 0; }
.basin-sidebar .row:last-child { border-bottom: none; }
.basin-sidebar .row .v { color: var(--ink); font-feature-settings: "tnum"; }
.basin-sidebar .verdict {
  margin-top: 14px;
  padding: 10px;
  border-radius: var(--radius);
  text-align: center;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.basin-sidebar .verdict.bounce { background: var(--bounce-soft); color: var(--bounce); }
.basin-sidebar .verdict.crunch { background: var(--ekpyrotic-soft); color: var(--ekpyrotic); }
.basin-sidebar .verdict.undecided { background: var(--rule-soft); color: var(--ink-soft); }

.metric-grid {
  border-top: 1px solid var(--rule-soft);
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  padding-top: 14px;
}
.metric-grid > div {
  min-width: 0;
}
.metric-grid .eyebrow {
  letter-spacing: 0.12em;
  white-space: nowrap;
}

@media (max-width: 640px) {
  .scrubber {
    gap: 12px;
    padding: 12px 16px;
  }
  .scrubber .t-readout {
    min-width: 76px;
  }
  .basin-plot-row {
    flex-direction: column;
  }
  .basin-colorbar {
    display: none;
  }
  .basin-sidebar {
    padding: 14px;
  }
  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

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

/* ============== Footer / closing ============== */
.closing {
  margin-top: 120px;
  padding-top: 32px;
  border-top: 1px solid var(--rule);
  font-size: 14px;
  color: var(--ink-mid);
  font-family: var(--font-mono);
  line-height: 1.6;
}
.closing a { color: var(--ink); }

/* Selection */
::selection { background: var(--ekpyrotic-soft); }
