:root {
  --cave: #1a1f2b;
  --stone: #262d3b;
  --stone-edge: #343c4d;
  --lamp: #f4b860;
  --ink: #e9e3d3;
  --muted: #8a93a6;
  --moss: #86b37e;
  --blood: #c24e5a;
  --mono: "Martian Mono", ui-monospace, monospace;
  --serif: "Spectral", Georgia, serif;
  --cell: clamp(11px, 1.55vw, 22px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--cave);
  color: var(--ink);
  font: 400 17px/1.55 var(--serif);
}

a { color: var(--lamp); }
:focus-visible { outline: 2px solid var(--lamp); outline-offset: 2px; }

/* ── header ───────────────────────────────────────── */
.bar {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 20px;
  align-items: baseline;
  padding: 20px clamp(16px, 3vw, 40px) 16px;
}
.wordmark {
  margin: 0;
  font: italic 600 2.1rem/1 var(--serif);
  color: var(--lamp);
  text-shadow: 0 0 18px rgb(244 184 96 / 0.45);
}
.tagline { margin: 0; color: var(--muted); }
.controls {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: end;
  margin-top: 14px;
}
.field { display: grid; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.field select, .field input {
  font: 400 0.95rem var(--serif);
  color: var(--ink);
  background: var(--stone);
  border: 1px solid var(--stone-edge);
  border-radius: 6px;
  padding: 7px 10px;
  min-width: 200px;
}
.field-seed input { min-width: 0; width: 7.5em; }
.toggle { display: flex; gap: 8px; align-items: center; padding-bottom: 8px; color: var(--muted); }
.toggle input { accent-color: var(--lamp); width: 16px; height: 16px; }

button { font: 600 0.95rem var(--serif); cursor: pointer; border-radius: 6px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.primary {
  background: var(--lamp);
  color: #2a1d08;
  border: 0;
  padding: 9px 18px;
  box-shadow: 0 0 0 0 rgb(244 184 96 / 0.4);
}
.primary:not(:disabled):hover { box-shadow: 0 0 22px 2px rgb(244 184 96 / 0.35); }
.secondary { background: transparent; color: var(--ink); border: 1px solid var(--stone-edge); padding: 8px 16px; }
.notice {
  margin: 0;
  align-self: center;
  padding: 7px 12px;
  border-left: 3px solid var(--lamp);
  background: var(--stone);
  border-radius: 0 6px 6px 0;
  font-size: 0.95rem;
}
.notice[hidden] { display: none; }
.runs-left { align-self: center; font-size: 0.9rem; color: var(--muted); }
.sound { margin-left: auto; }
.sound[aria-pressed="true"] { border-color: var(--lamp); color: var(--lamp); }
.link { background: none; border: 0; color: var(--lamp); padding: 0; text-decoration: underline; }

/* ── stage: map + mind + timeline ─────────────────── */
.stage {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr);
  grid-template-areas: "map mind" "time time";
  gap: 0;
  margin: 0 clamp(16px, 3vw, 40px);
  border: 1px solid var(--stone-edge);
  border-radius: 10px;
  overflow: hidden;
  background: #151923;
}

.map-wrap { grid-area: map; padding: 18px 20px 14px; display: flex; flex-direction: column; min-height: 420px; }
.goal { margin: 0 0 12px; color: var(--muted); max-width: 70ch; }
.rules { margin: -6px 0 12px; color: var(--muted); font-size: 0.85rem; max-width: 70ch; }
.rules summary { cursor: pointer; width: fit-content; }
.rules ul { margin: 6px 0 0; padding-left: 1.2em; }
.rules li { margin: 2px 0; }

.viewport { flex: 1; position: relative; display: flex; min-height: 440px; }
.view3d { display: none; }
.has-3d .view3d {
  display: block;
  position: absolute;
  inset: 0;
  border-radius: 8px;
  overflow: hidden;
}
.view3d canvas { width: 100%; height: 100%; display: block; }
/* Shown over the dungeon when the selected call is the last one of a finished run. */
.ending {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  min-width: min(340px, 90%);
  padding: 14px 20px 16px;
  text-align: center;
  background: rgb(21 25 35 / 0.9);
  border: 1px solid var(--stone-edge);
  border-radius: 10px;
  backdrop-filter: blur(4px);
}
.ending[hidden] { display: none; }
.ending-close {
  position: absolute;
  top: 6px;
  right: 8px;
  width: 28px;
  height: 28px;
  padding: 0;
  background: none;
  border: 0;
  color: var(--muted);
  font: 400 1.4rem/1 var(--serif);
}
.ending-close:hover { color: var(--ink); }
.ending-title { margin: 0; font: italic 600 1.5rem var(--serif); }
.ending-title.win { color: var(--moss); }
.ending-title.loss { color: var(--blood); }
.ending-detail { margin: 2px 0 12px; color: var(--muted); }
.ending-actions { display: flex; gap: 10px; justify-content: center; }

.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
}
.empty[hidden] { display: none; }

/* Without 3D the text map is the main view; with 3D it becomes a corner inset. */
.beam-view { flex: 1; display: flex; flex-direction: column; }
.beam-view summary { display: none; }
.has-3d .beam-view {
  flex: none;
  position: absolute;
  right: 12px;
  bottom: 12px;
  max-width: min(46%, 420px);
  background: rgb(21 25 35 / 0.86);
  border: 1px solid var(--stone-edge);
  border-radius: 8px;
  padding: 8px 10px 10px;
  backdrop-filter: blur(3px);
}
.has-3d .beam-view summary { display: list-item; cursor: pointer; font-size: 0.85rem; color: var(--muted); }
.has-3d .beam-view[open] summary { margin-bottom: 6px; }
.has-3d .beam-view .map { --cell: 8px; overflow: hidden; place-content: start; }

.map {
  flex: 1;
  display: grid;
  place-content: center;
  position: relative;
  font: 400 var(--cell)/1.2 var(--mono);
  white-space: pre;
  user-select: none;
}
/* The lantern: a warm pool of light centred on Beam, positioned by app.js. */
.map::before {
  content: "";
  position: absolute;
  left: var(--hx, 50%);
  top: var(--hy, 50%);
  width: calc(var(--cell) * 16);
  height: calc(var(--cell) * 16);
  transform: translate(-50%, -50%);
  background: radial-gradient(closest-side, rgb(244 184 96 / 0.22), rgb(244 184 96 / 0.07) 60%, transparent);
  pointer-events: none;
  transition: left 0.25s ease, top 0.25s ease;
}
.map .row { position: relative; height: calc(var(--cell) * 1.2); }
.map span { display: inline-block; width: 1ch; text-align: center; }
.map .hero { color: var(--lamp); font-weight: 600; text-shadow: 0 0 10px var(--lamp); }
/* Where Beam has walked: floor glyphs take on a faint lamplight tint. */
.map .trail { color: #b5874a; }

.status { margin: 12px 0 0; font-size: 0.95rem; color: var(--muted); }
.status strong { color: var(--ink); font-weight: 600; }
.status .win { color: var(--moss); }
.status .loss { color: var(--blood); }

.mind {
  grid-area: mind;
  border-left: 1px solid var(--stone-edge);
  background: var(--stone);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: 640px;
}
.mind-title { margin: 0 0 8px; font: italic 600 1.25rem var(--serif); }
.reasoning {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 120px;
  padding-right: 6px;
  font-style: italic;
  line-height: 1.65;
  white-space: pre-wrap;
}
.reasoning p { margin: 0 0 0.6em; }
.sub { margin: 14px 0 6px; font: 600 0.9rem var(--serif); color: var(--muted); }

.plan { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.plan li {
  font: 400 0.8rem var(--mono);
  padding: 3px 8px;
  border-radius: 4px;
  border: 1px solid var(--lamp);
  color: var(--lamp);
}
.plan li.skipped { border-style: dashed; border-color: var(--muted); color: var(--muted); text-decoration: line-through; }
.plan .why { border: 0; padding: 3px 0; color: var(--muted); font-family: var(--serif); font-style: italic; }

.coach {
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px solid var(--stone-edge);
  border-radius: 8px;
  background: var(--stone);
}
.coach .sub { margin-top: 0; }
.coach-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.coach-row .coach-form { flex: 1 1 260px; margin-top: 0; }
.reasoning .reply { font-style: normal; color: var(--ink); border-left: 2px solid var(--lamp); padding-left: 8px; }
.coach-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.coach-buttons button, .coach-form button {
  background: var(--cave);
  color: var(--ink);
  border: 1px solid var(--stone-edge);
  padding: 4px 10px;
  font-weight: 400;
}
.coach-buttons button:hover { border-color: var(--lamp); color: var(--lamp); }
.coach-form { display: flex; gap: 6px; margin-top: 8px; }
.coach-form input {
  flex: 1;
  min-width: 0;
  font: 400 0.9rem var(--serif);
  color: var(--ink);
  background: var(--cave);
  border: 1px solid var(--stone-edge);
  border-radius: 6px;
  padding: 5px 8px;
}
.coach-log { margin: 6px 0 0; font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }
.coach-log.refused { color: var(--lamp); font-size: 0.95rem; border-left: 3px solid var(--lamp); padding: 4px 0 4px 10px; }
.reasoning .advice { font-style: normal; color: var(--lamp); }
/* Calls where you coached Beam get a lamp-coloured cap on the timeline. */
.tick.coached { box-shadow: inset 0 3px 0 var(--lamp); }

.notes { margin: 0; font-size: 0.95rem; white-space: pre-wrap; max-height: 9em; overflow-y: auto; }

.prompt { margin-top: 14px; font-size: 0.85rem; color: var(--muted); }
.prompt summary { cursor: pointer; }
.prompt pre { font: 400 0.72rem/1.35 var(--mono); max-height: 260px; overflow: auto; background: var(--cave); padding: 10px; border-radius: 6px; color: var(--ink); }

.timeline-wrap { grid-area: time; border-top: 1px solid var(--stone-edge); padding: 10px 20px 14px; }
.timeline-head { display: flex; justify-content: space-between; align-items: baseline; }
.timeline-head .sub { margin: 0 0 8px; }
.timeline {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 46px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.tick {
  flex: 0 0 9px;
  min-height: 6px;
  border: 0;
  border-radius: 2px 2px 0 0;
  background: #4a5367;
  padding: 0;
}
.tick.act { background: #9a7a48; }
.tick.interrupted { background: var(--lamp); }
.tick.win { background: var(--moss); }
.tick.loss { background: var(--blood); }
.tick[aria-selected="true"] { outline: 2px solid var(--ink); outline-offset: 1px; }

/* ── journal + replays ───────────────────────────── */
.lower {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr);
  gap: 28px;
  margin: 28px clamp(16px, 3vw, 40px);
}
.lower h2 { margin: 0 0 6px; font: italic 600 1.25rem var(--serif); }
.journal-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.lessons { margin: 0; padding-left: 1.1em; max-width: 60ch; }
.lessons li { margin-bottom: 0.5em; }
.lessons li.new::marker { color: var(--lamp); }
.episodes { list-style: none; margin: 0; padding: 0; }
.episodes button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  background: none;
  border: 0;
  border-bottom: 1px solid var(--stone-edge);
  border-radius: 0;
  color: var(--ink);
  padding: 8px 2px;
  font-weight: 400;
}
.episodes button:hover { color: var(--lamp); }
.episodes .win { color: var(--moss); }
.episodes .loss { color: var(--blood); }
.quiet { color: var(--muted); }

.foot { margin: 0 clamp(16px, 3vw, 40px) 28px; font-size: 0.85rem; color: var(--muted); max-width: 90ch; }

@media (max-width: 860px) {
  .bar { grid-template-columns: 1fr; }
  .stage { grid-template-columns: 1fr; grid-template-areas: "map" "mind" "time"; }
  .mind { border-left: 0; border-top: 1px solid var(--stone-edge); max-height: none; }
  .map { overflow-x: auto; place-content: start; }
  .viewport { flex-direction: column; min-height: 0; }
  .has-3d .view3d { position: relative; inset: auto; height: 340px; }
  .has-3d .beam-view { position: static; max-width: none; margin-top: 12px; }
  .lower, .journal-cols { grid-template-columns: 1fr; }
  .field select { min-width: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .map::before { transition: none; }
}
