/* Layout for interactive lectures (story.js). Text panel on the left, live figure on the right,
   chapter track along the bottom. Sized for a projector first, then laptops and phones.
   Colours come from tokens.css. */

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

html, body { height: 100%; }
body.story-app {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background-color: var(--bg);
  background-image:
    radial-gradient(55% 50% at 70% 30%, var(--glow-b), transparent 70%),
    radial-gradient(45% 45% at 15% 85%, var(--glow-a), transparent 70%);
  font: 400 var(--fs) / 1.5 var(--f-ui);
  -webkit-font-smoothing: antialiased;
  --fs: clamp(16px, 0.9vw + 6px, 23px);
  --gap: clamp(10px, 1.2vw, 20px);
}

#app {
  display: grid;
  grid-template-areas: "head head" "story stage" "bar bar";
  grid-template-columns: minmax(330px, 34%) 1fr;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--gap) calc(var(--gap) * 1.6);
  height: 100vh;
  height: 100dvh;
  padding: var(--gap) calc(var(--gap) * 1.6) calc(var(--gap) * 0.6);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 18px;
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

/* ---------- title, as on a slide ---------- */
#head { grid-area: head; min-width: 0; }
#head .kicker {
  margin: 0 0 0.3em;
  font: 500 0.62em var(--f-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}
#head h1 {
  margin: 0;
  font-size: clamp(1.6em, 0.8em + 2.2vw, 3.2em);
  line-height: 1.1;
  font-weight: 800;
  letter-spacing: -0.025em;
  text-wrap: balance;
}

/* ---------- text ---------- */
#story {
  grid-area: story;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  scrollbar-width: thin;
}
#txt { flex: 1 0 auto; font-family: var(--f-serif); line-height: 1.55; }
#txt > :first-child { margin-top: 0; }
#txt p, #txt ul, #txt ol { margin: 0 0 0.7em; }
#txt ul, #txt ol { padding-left: 1.2em; }
#txt li { margin-bottom: 0.3em; }
#txt h3 { margin: 0.6em 0 0.3em; font: 700 1em var(--f-ui); color: var(--accent); }
#txt a { color: var(--accent); }
#txt .small, #txt small { font-size: 0.8em; color: var(--ink-soft); }
#txt .key {
  margin: 0.6em 0 0.8em;
  padding: 0.55em 0.8em;
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  background: var(--accent-soft);
}
#txt .quote {
  margin: 0 0 0.7em;
  padding: 0.5em 0.9em;
  border-left: 3px solid var(--violet);
  font-style: italic;
}
#txt table { width: 100%; border-collapse: collapse; margin: 0.3em 0 0.8em; font-size: 0.92em; }
#txt th, #txt td { padding: 0.32em 0.6em 0.32em 0; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
#txt th { font: 600 0.85em var(--f-ui); color: var(--ink-soft); }
#txt .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(16em, 1fr)); gap: 0.4em 2em; }
#txt .katex-display { margin: 0.6em 0; overflow-x: auto; overflow-y: hidden; }
#txt .step { transition: opacity 0.25s ease; }
#txt .step.hidden { opacity: 0; visibility: hidden; }

#readout { display: grid; grid-template-columns: auto 1fr; gap: 0.2em 0.9em; margin: 0.4em 0 0; font-size: 0.86em; }
#readout:empty { display: none; }
#readout dt { color: var(--ink-soft); }
#readout dd { margin: 0; font: 500 0.95em var(--f-mono); color: var(--ink); font-variant-numeric: tabular-nums; }
#readout .wide { grid-column: 1 / -1; font: 400 0.95em var(--f-ui); color: var(--ink); }

.btn {
  padding: 0.35em 1em;
  font: 500 0.8em var(--f-ui);
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--edge);
  border-radius: 999px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { color: var(--bg); background: var(--accent); border-color: var(--accent); }
.btn:disabled { opacity: 0.35; cursor: default; }
.foot { margin: 0.8em 0 0; font-size: 0.6em; color: var(--ink-soft); }
.foot a { color: var(--ink-soft); }
.foot kbd { padding: 0 0.3em; font: inherit; font-family: var(--f-mono); border: 1px solid var(--edge); border-radius: 3px; }

/* ---------- figure side ---------- */
#stage {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  gap: calc(var(--gap) * 0.7);
  min-height: 0;
  min-width: 0;
}
#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5em 1.6em;
  padding: 0.65em 1.1em;
  font-size: 0.78em;
}
#controls[hidden] { display: none; }
.ctl { display: flex; flex-direction: column; gap: 0.15em; min-width: 12em; flex: 1 1 12em; max-width: 26em; }
.ctl .row { display: flex; justify-content: space-between; gap: 1em; }
.ctl .lab { color: var(--ink-soft); }
.ctl output { font: 500 0.95em var(--f-mono); color: var(--accent); font-variant-numeric: tabular-nums; }
.ctl input[type="range"] { width: 100%; accent-color: var(--accent); margin: 0.2em 0 0; }
.ctl select {
  font: inherit;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 0.2em 0.4em;
}
.ctl-toggle { flex-direction: row; align-items: center; gap: 0.5em; min-width: 0; flex: 0 1 auto; cursor: pointer; }
.ctl-toggle input { width: 1.1em; height: 1.1em; accent-color: var(--accent); margin: 0; }
.ctl-choice .opts { display: flex; flex-wrap: wrap; gap: 0.3em; }
.ctl-choice button {
  padding: 0.15em 0.75em;
  font: inherit;
  color: var(--ink-soft);
  background: transparent;
  border: 1px solid var(--edge);
  border-radius: 999px;
  cursor: pointer;
}
.ctl-choice button[aria-pressed="true"] { color: var(--bg); background: var(--accent); border-color: var(--accent); }
.ctl-button { flex: 0 1 auto; min-width: 0; justify-content: flex-end; }

#fig {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  padding: 0.6em 0.8em;
}
#fig > * { display: block; }
#fig svg { overflow: visible; }
#fig .error { color: var(--hot); font: 0.8em var(--f-mono); white-space: pre-wrap; }
#note { margin: -0.2em 0.4em 0; font-size: 0.6em; color: var(--ink-soft); }
#note:empty { display: none; }

/* ---------- bottom bar: links, chapter track, page number and Back/Next ---------- */
#bar {
  grid-area: bar;
  display: flex;
  align-items: center;
  gap: 1.2em;
  min-width: 0;
  font-size: 0.75em;
}
#bar .links { flex: none; color: var(--ink-soft); font-size: 0.85em; white-space: nowrap; }
#bar .links a { color: var(--ink-soft); }
#bar .nav { flex: none; display: flex; align-items: center; gap: 0.5em; }
#bar .nav .btn { font-size: 1em; }
#count { margin-right: 0.4em; font: 500 0.9em var(--f-mono); color: var(--ink-soft); font-variant-numeric: tabular-nums; }
#track {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
#track .part {
  margin: 0 4px 0 12px;
  font: 500 10px var(--f-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  white-space: nowrap;
}
#track .part:first-child { margin-left: 0; }
#track button {
  flex: none;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  display: grid;
  place-items: center;
}
#track button::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--edge);
  transition: transform 0.15s ease, background-color 0.15s ease;
}
#track button:hover::before { background: var(--ink-soft); transform: scale(1.25); }
#track button.seen::before { background: var(--ink-soft); }
#track button[aria-current="true"]::before { background: var(--accent); transform: scale(1.5); }

#themeToggle {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 8px;
  cursor: pointer;
}
#themeToggle:hover { border-color: var(--accent); }
#themeToggle svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
:root[data-theme="light"] #themeToggle .sun, :root:not([data-theme="light"]) #themeToggle .moon { display: none; }

/* ---------- text-only chapters: the text gets the whole slide ---------- */
#app.layout-text { grid-template-areas: "head" "story" "bar"; grid-template-columns: minmax(0, 1fr); }
#app.layout-text #stage { display: none; }

/* ---------- cover ---------- */
#app.layout-cover {
  grid-template-areas: "." "head" "story" "." "bar";
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: 1fr auto auto 1fr auto;
}
#app.layout-cover #stage {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}
/* fade the figure behind the text so the title and buttons stay readable */
#app.layout-cover::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background: linear-gradient(90deg, var(--bg) 0%, var(--bg) 30%, transparent 75%);
  opacity: 0.85;
}
#app.layout-cover #controls, #app.layout-cover #note, #app.layout-cover #bar .nav, #app.layout-cover #bar .links { display: none; }
#app.layout-cover #fig { padding: 0; background: transparent; border: 0; -webkit-backdrop-filter: none; backdrop-filter: none; }
#app.layout-cover #head, #app.layout-cover #story { width: min(860px, 100%); margin-left: 5vw; overflow: visible; }
/* a soft-edged patch of background behind the text, so the figure never runs through it;
   layered: figure (-2), fade (-2), patches (-1), text */
#app.layout-cover #stage, #app.layout-cover::after { z-index: -2; }
#app.layout-cover #head, #app.layout-cover #story { position: relative; }
#app.layout-cover #head::before, #app.layout-cover #story::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--bg);
  box-shadow: 0 0 40px 36px var(--bg);
}
#app.layout-cover #txt .jump, #app.layout-cover #txt .foot, #app.layout-cover #txt .meta { font-family: var(--f-ui); }
#app.layout-cover #head h1 { font-size: clamp(40px, 5vw, 84px); line-height: 1.02; }
#app.layout-cover #head .kicker { font-size: 0.75em; }
#app.layout-cover .sub { font-size: 1.1em; color: var(--ink-soft); max-width: 34em; }
#app.layout-cover .start { margin-top: 0.4em; font-size: 0.95em; padding: 0.5em 1.6em; }
#app.layout-cover .jump { display: flex; flex-wrap: wrap; gap: 0.4em; margin-top: 0.9em; font-size: 0.85em; }
#app.layout-cover .jump span { color: var(--ink-soft); align-self: center; margin-right: 0.3em; }

/* H hides the text so the figure gets the whole width */
#app.hide-story.layout-figure { grid-template-columns: 0 1fr; column-gap: 0; }
#app.hide-story.layout-figure #story { visibility: hidden; }

/* ---------- narrow screens: one scrolling column ---------- */
@media (max-width: 860px), (max-height: 520px) and (max-width: 1100px) {
  body.story-app { overflow: auto; --fs: 16px; }
  #app {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: 100vh;
    padding: 14px 12px 16px;
  }
  #head h1 { font-size: 1.5em; }
  #story { overflow: visible; flex: none; }
  #fig { flex: none; height: min(78vw, 70vh); }
  #track, #bar .links { display: none; }
  #bar { margin-top: auto; padding-top: 8px; font-size: 0.9em; }
  #bar .nav { flex: 1; }
  #bar .nav .btn { flex: 1; padding: 0.6em 1em; }
  #app.layout-cover #stage { position: absolute; height: 100vh; }
  #app.layout-cover #head { margin: 12vh 0 0; }
  #app.layout-cover #story { margin: 0; }
}

@media (prefers-reduced-motion: reduce) {
  #txt .step, #track button::before { transition: none; }
}

@media print {
  body.story-app { overflow: visible; }
}
