/* ═══════════════════════════════════════════════════════════════════
   Cave v4 — the staged page shell.

   Colours come from docs/_sass/cave-theme.scss and nowhere else: this file holds
   layout and state, not palette. The stage cards borrow Dig's fieldset
   chrome (rock-deep panel, rock-edge hairline, 8 px radius) so the two pages
   read as the same room; the chips, the train, the wagon table and the wagon
   graph are drawn by filter.css and are only placed here.

   The rail is two controls in one: a vertical sticky stepper from 900 px up,
   thin segments across the top of the column below that (Mobbin's Stripe
   setup / Fresha patterns). From 900 px it is also a mine shaft read
   downwards — a ledge per floor with the lift cage parked at the stage being
   worked on (K22) — while on a phone the cage is hidden and the segments stay.
   A done step is gold, the open one lamp, the rest rock: the same three states
   the cards use.

   Legible at 360 px: nothing here sets a width a phone cannot honour, the
   rail's names are for screen readers only on the narrow layout, and the
   summary line on a folded card clips rather than pushing the head's name
   off the row.
   ═══════════════════════════════════════════════════════════════════ */

#cave-app {
  /* The walls are this box's siblings out in the gutters (see below), so the
     lift is belt and braces: if a gutter ever miscalculated, the cards still
     win the paint order.

     71rem is the whole page — rail, cards, hero — padding included, which
     makes the card column 54rem: 120% of the 45rem it had inside 60rem. That
     is wider than minima's wrapper (1110 px inside), so the box sizes itself
     against the viewport and centres on the wrapper with a margin that goes
     negative once it outgrows it. 100vw - 2rem never reaches the scrollbar. */
  --cave-w: min(71rem, 100vw - 2rem);
  position: relative;
  z-index: 1;
  box-sizing: border-box;
  width: var(--cave-w);
  margin: 0 0 0 calc((100% - var(--cave-w)) / 2);
  padding: 0.5rem 1rem 3rem;
}

/* ── Side walls: wider, out in the real gutters ─────────────────
   `cave-wall-dig` is this page's hook into style.css's .cave-wall: absolute
   against .page-content, cropped from the inner edge by background-position,
   pixelated, pointer-events none. Two classes beat that sheet's own
   `.cave-wall` whatever order the two land in, and no other room carries the
   class, so Stockpile, Haul and Details keep their gutters untouched.

   Width is the gutter left outside the 71rem box with 3rem of slack, capped
   at 320px. It never overlaps #cave-app's box, which is 1136px (71rem at the
   default 16px root) centred on the page:
     at 1240px: wall = (1240 - 1136 - 48) / 2 = 28;  app left = (1240 - 1136) / 2 = 52
       — 24px clear, and both edges move 1px per 2px of viewport, so the 24px
       holds until the wall hits its 320px cap at 1136 + 48 + 640 = 1824px.
     at 1920px: wall = 320; app left = 392 — 72px clear.

   Shown from 1240px up, not the site's 1340: a 28px strip of rock still reads
   as a wall. Below that the box has the whole width and there is no gutter. */
.cave-wall.cave-wall-dig {
  width: clamp(0px, calc((100vw - 71rem - 3rem) / 2), 320px);
}

@media (min-width: 1240px) {
  .cave-wall.cave-wall-dig {
    display: block;
  }
}

/* ── Hero ─────────────────────────────────────────────────────── */
.cave-hero {
  margin: 0 0 1.1rem;
}

.cave-lede {
  font-size: 1.12rem;
  line-height: 1.55;
  color: var(--text);
  margin: 0 0 0.5rem;
  max-width: 46em;
}

/* ── The ride ───────────────────────────────────────────────────
   Only a shared launch link starts one (`cave-launch-ride.mjs`): the lift in
   the rail rides down to Ranking at a walking pace — `--ride-ms`, declared
   below and held to the module's RIDE_MS by a test — and this box parks beside
   the cards with the reason the ranking is not here yet. It replaces a strip of
   three small step markers, which said the same thing at a size nobody read.

   `position: fixed` and `pointer-events: none` together are what make the box
   safe over a phone: it floats above the cards (z-index) but every control
   under it still takes a press, and cave.js takes the box away when the
   ranking lands. Colours are theme tokens, so the dark theme draws it free. */
.cave-ride {
  position: fixed;
  left: 0.75rem;
  right: 0.75rem;
  bottom: 0.75rem;
  z-index: 20;
  box-sizing: border-box;
  margin: 0;
  padding: 1.2rem 1.3rem 1.3rem;
  background: var(--rock-deep);
  border: 1px solid var(--rock-lit);
  border-radius: 8px;
  box-shadow: 0 18px 40px -18px color-mix(in srgb, var(--cave-air) 85%, transparent);
  pointer-events: none;
  animation: cave-ride-in 400ms ease-out;
}

/* The scrim: the cards under the box are waiting too, so they are dimmed and
   cannot be pressed until the ranking lands (cave.js drops the box, and the
   scrim with it, on `ranked` or after 90 s). It lives inside the box so one
   fade takes both away; z-index -1 keeps it behind the box's own words. */
.cave-ride::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: color-mix(in srgb, var(--cave-air) 55%, transparent);
  pointer-events: auto;
  cursor: progress;
}

@keyframes cave-ride-in {
  from {
    opacity: 0;
  }
}

/* From 900 px the lift exists to ride, so the box leaves the bottom edge for
   the gutter beside the cards, roughly over the ranking they are waiting for.
   22rem holds the heading on one or two lines and leaves the cards readable. */
@media (min-width: 900px) {
  .cave-ride {
    left: 50%;
    right: auto;
    top: 16rem;
    bottom: auto;
    transform: translateX(-50%);
    width: 24rem;
    padding: 1.6rem 1.7rem 1.7rem;
  }
}

.cave-ride-head {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.2;
  color: var(--text);
}

.cave-ride-copy {
  margin: 0.55rem 0 0;
  font-size: 1rem;
  line-height: 1.5;
  color: var(--text-dim);
}

.cave-ride-label {
  margin: 0.95rem 0 0;
  font-size: 0.82rem;
  color: var(--text-faint);
}

/* The pace, in CSS's own numbers: a module constant is not readable from here,
   so `tests/js/launch.test.mjs` holds this one to RIDE_MS. It sits on the app
   because the cage is a descendant and inherits it. */
#cave-app.is-riding {
  --ride-ms: 14000ms;
}

/* The descent. The rail's ordinary move is 700ms (below, in the desktop
   block), which is right for a click the reader made; six floors in one go at
   that pace is a jump. */
@media (min-width: 900px) {
  #cave-app.is-riding .cave-lift {
    transition: transform var(--ride-ms) ease-in-out;
  }

  #cave-app.is-riding .cave-lift-cable {
    transition: height var(--ride-ms) ease-in-out;
  }
}

/* Ranking landed: the box has nothing left to say. It fades where it stands and
   cave.js removes the empty element on animationend. Opacity only, so a reader
   who asked for less motion keeps the fade — it is what says the box may go. */
.cave-ride.is-done {
  animation: cave-ride-fade 500ms ease forwards;
}

@keyframes cave-ride-fade {
  to {
    opacity: 0;
  }
}

/* ── The rail ─────────────────────────────────────────────────── */
.cave-rail {
  margin: 0 0 1.2rem;
}

/* The lift is a desktop idea: on a phone the rail is a row of segments and
   there is no shaft to ride, so the cage stays out of the markup's way. */
.cave-lift {
  display: none;
}

.cave-rail-list {
  list-style: none;
  display: flex;
  gap: 0.35rem;
  margin: 0;
  padding: 0;
}

.cave-rail-step {
  flex: 1 1 0;
  min-width: 0;
}

.cave-rail-link,
.cave-rail-link:visited {
  display: block;
  padding: 0.35rem 0 0.1rem;
  border-radius: 3px;
  color: var(--text-dim);
  text-decoration: none;
}

.cave-rail-link:hover {
  text-decoration: none;
}

.cave-rail-link:focus-visible {
  outline: 2px solid var(--lamp);
  outline-offset: 1px;
}

/* One segment: rock when locked, a lamp ring when it is the stage to work on,
   solid gold once it is done. */
.cave-rail-dot {
  display: block;
  height: 3px;
  border-radius: 999px;
  background: var(--rock-edge);
  border: 1px solid transparent;
}

.cave-rail-step.is-done .cave-rail-dot {
  background: var(--gold);
}

.cave-rail-step.is-open .cave-rail-dot {
  background: color-mix(in srgb, var(--lamp) 25%, transparent);
  border-color: var(--lamp);
}

.cave-rail-step.is-locked .cave-rail-link {
  cursor: default;
}

/* Names and summaries are read out, not drawn, on the narrow layout — eight
   labelled segments will not fit 360 px. */
.cave-rail-name,
.cave-rail-summary {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  white-space: nowrap;
  clip-path: inset(50%);
}

/* ── Stage cards ──────────────────────────────────────────────── */
.cave-stage {
  background: var(--rock-deep);
  border: 1px solid var(--rock-edge);
  border-radius: 8px;
  margin-bottom: 0.9rem;
  min-width: 0;
}

.cave-stage-head {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  width: 100%;
  padding: 0.8rem 1rem;
  background: none;
  border: 0;
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.cave-stage-head:hover {
  background: var(--rock);
}

.cave-stage-head:focus-visible {
  outline: 2px solid var(--lamp);
  outline-offset: -2px;
}

.cave-stage-n {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: var(--rock);
  border: 1px solid var(--rock-lit);
  color: var(--lamp);
  font-size: 0.9rem;
  font-weight: 700;
}

.cave-stage-name {
  font-weight: 700;
  font-size: 1.02rem;
  flex: none;
}

/* The one-line summary of a folded stage. Clipped, not wrapped: a wrapped
   summary makes every folded card a different height, which is exactly the
   noise folding is meant to remove. */
.cave-stage-summary {
  flex: 1 1 auto;
  min-width: 0;
  color: var(--text-faint);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: right;
}

.cave-stage-body {
  padding: 0.15rem 1rem 1rem;
}

.cave-stage:not(.is-expanded) .cave-stage-body {
  display: none;
}

/* Done: the number is the gold the page is digging for. */
.cave-stage.is-done .cave-stage-n {
  background: var(--gold);
  border-color: var(--gold);
  color: var(--cave-air);
}

.cave-stage.is-done .cave-stage-name {
  color: var(--text-dim);
}

/* Open: lit by the blue lamp, because the work here is still the dig's, not
   the fire's (stages 1–3 carry no warmth). */
.cave-stage.is-open {
  border-color: var(--rock-lit);
  box-shadow: inset 2px 0 0 var(--lamp);
}

/* Locked: still readable, plainly not yet the thing to do. */
.cave-stage.is-locked {
  opacity: 0.55;
}

.cave-stage.is-locked .cave-stage-head {
  cursor: default;
}

.cave-stage-coming {
  margin: 0;
  color: var(--text-faint);
  font-style: italic;
}

/* ── Dig ──────────────────────────────────────────────────────── */
.cave-field {
  margin-bottom: 0.8rem;
}

.cave-field-label {
  display: block;
  font-size: 0.92rem;
  color: var(--text-dim);
  margin-bottom: 0.4rem;
}

/* The offer to read these categories with their own field's topics. */
.cave-dig-field {
  margin: 0.45rem 0 0;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.cave-field-note {
  display: inline-block;
  margin: 0.6rem 0 0;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--rock-edge);
  border-radius: 6px;
  background: var(--rock-deep);
  font-size: 0.85rem;
  color: var(--text-dim);
}

.cave-field-note[hidden] {
  display: none;
}

.cave-field-note b {
  color: var(--gold);
}

.cave-dig-field-btn,
.cave-field-note-btn {
  padding: 0;
  background: none;
  border: 0;
  color: var(--lamp);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
}

.cave-dig-field-btn:focus-visible,
.cave-field-note-btn:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.cave-dig .role-grid {
  margin-bottom: 0.8rem;
}

.cave-dig-error {
  color: var(--ember);
}

/* Dig and Continue are the one thing to press on their card, so they carry
   the ore colour like the Dig page's haul button does. The two-class
   selector is the specificity that beats filter.css's `.button-bar button`. */
.button-bar .cave-primary {
  background: var(--gold);
  color: var(--cave-air);
  border-color: var(--gold);
  font-weight: 600;
  box-shadow: 0 6px 22px -10px color-mix(in srgb, var(--gold) 55%, transparent);
}

.button-bar .cave-primary:hover:not(:disabled) {
  background: var(--ore-6);
}

.button-bar .cave-primary:disabled {
  background: var(--rock);
  color: var(--text-faint);
  border-color: var(--rock-edge);
  box-shadow: none;
}

/* ── Train ────────────────────────────────────────────────────── */
.cave-train-hint {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: var(--text-faint);
}

/* ── Map: the topic bar ───────────────────────────────────────────
   One segment per topic parent, its width the count of tonight's papers in
   it, so the shape of the night reads before the wagons do. Hover, tap or
   keyboard focus opens the leaf counts underneath. */
.cave-topic-bar {
  display: flex;
  gap: 3px;
  margin-bottom: 0.6rem;
}

.cave-topic-bar:empty {
  display: none;
}

.cave-topic-seg {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  height: 30px;
  padding: 0 0.35rem;
  border: 1px solid var(--rock-edge);
  border-radius: 3px;
  background: var(--lamp-3);
  color: var(--text);
  font: inherit;
  font-size: 0.72rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  overflow: hidden;
  cursor: default;
}

.cave-topic-seg:nth-child(even) {
  background: var(--lamp-2);
}

.cave-topic-seg:focus-visible {
  outline: 2px solid var(--lamp);
  outline-offset: -2px;
}

.cave-topic-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cave-topic-count {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--gold);
}

.cave-topic-leaves {
  display: none;
  position: absolute;
  left: 0;
  top: calc(100% + 4px);
  z-index: 5;
  min-width: 12rem;
  max-width: 18rem;
  padding: 0.45rem 0.55rem;
  background: var(--rock-deep);
  border: 1px solid var(--rock-lit);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 0.78rem;
  line-height: 1.45;
  text-align: left;
  white-space: normal;
  box-shadow: 0 10px 22px -12px color-mix(in srgb, var(--cave-air) 85%, transparent);
}

/* The last segment's card would hang off the right edge. */
.cave-topic-seg:last-child .cave-topic-leaves {
  left: auto;
  right: 0;
}

.cave-topic-seg:hover .cave-topic-leaves,
.cave-topic-seg:focus-visible .cave-topic-leaves,
.cave-topic-seg:focus-within .cave-topic-leaves {
  display: block;
}

.cave-map-empty {
  margin: 0.6rem 0 0;
}

.cave-map-continue {
  margin-top: 0.9rem;
}

/* ── Notice line ──────────────────────────────────────────────── */
.cave-notice {
  margin: 1rem 0 0;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.cave-notice[hidden] {
  display: none;
}

/* ── Desktop: the rail is a sticky vertical stepper ───────────── */
@media (min-width: 900px) {
  #cave-app {
    display: grid;
    grid-template-columns: 13.5rem minmax(0, 1fr);
    column-gap: 1.5rem;
    align-items: start;
    padding-top: 1rem;
  }

  .cave-hero,
  .cave-notice {
    grid-column: 1 / -1;
  }

  /* Hero on row 1, the notice line on row 2; the rail and the cards share
     row 3 so a floor of the lift lines up with the card beside it. */
  .cave-rail {
    grid-column: 1;
    grid-row: 3;
    position: sticky;
    top: 1rem;
    margin: 0;
    /* The lift's geometry, in one place: one floor is one step, the cage is the
       48 px drawing, and the ledge is where inside a floor the cage parks. */
    --cave-lift-pitch: 4.25rem;
    --cave-lift-cage: 3rem;
    --cave-lift-headroom: 3.4rem;  /* room for the cage above the top floor */
    --cave-lift-ledge: 0.62rem;
  }

  /* The shaft: two rails and a sleeper at every floor, drawn as the shaft's own
     background so it sits behind the floors and the cage both. */
  .cave-lift-shaft {
    position: relative;
    padding-top: var(--cave-lift-headroom);
    background:
      linear-gradient(var(--rock-edge), var(--rock-edge)) 1.05rem 0 / 2px 100% no-repeat,
      linear-gradient(var(--rock-edge), var(--rock-edge)) 1.93rem 0 / 2px 100% no-repeat,
      repeating-linear-gradient(to bottom,
        var(--rock) 0 2px, transparent 2px var(--cave-lift-pitch))
        1.05rem var(--cave-lift-ledge) / 0.9rem 100% no-repeat;
  }

  .cave-stages {
    grid-column: 2;
    grid-row: 3;
    min-width: 0;
  }

  .cave-rail-list {
    display: block;
  }

  /* A fixed floor: the cage's translateY is a multiple of this, so floor n's
     ledge and the cage's resting height can never drift apart. */
  .cave-rail-step {
    min-height: var(--cave-lift-pitch);
  }

  /* The last floor has no cage coming below it, so its pitch only buys shaft. */
  .cave-rail-step:last-child {
    min-height: auto;
  }

  .cave-rail-link {
    display: grid;
    grid-template-columns: var(--cave-lift-cage) minmax(0, 1fr);
    column-gap: 0.4rem;
    align-items: start;
    padding: 0.3rem 0;
  }

  /* The dot becomes the floor's ledge: a short horizontal bar rather than a
     bullet, so the column reads as the floors of a shaft. */
  .cave-rail-dot {
    box-sizing: border-box;
    width: 100%;
    height: 3px;
    border-radius: 0;
    margin-top: 0.3rem;
    background: var(--rock-edge);
  }

  .cave-rail-step.is-done .cave-rail-dot {
    background: var(--gold);
  }

  .cave-rail-step.is-open .cave-rail-dot {
    background: var(--lamp);
    border-color: var(--lamp);
  }

  /* The cage. `--lift-floor` is the one thing the shell writes; the transition
     is what makes a finished stage read as the lift going down a floor. */
  .cave-lift {
    --lift-floor: 0;
    display: block;
    position: absolute;
    left: 0;
    top: calc(var(--cave-lift-headroom) + var(--cave-lift-ledge));
    width: var(--cave-lift-cage);
    height: 0;
    pointer-events: none;
    transform: translateY(calc(var(--lift-floor) * var(--cave-lift-pitch)));
    transition: transform 700ms ease-in-out;
  }

  .cave-lift-cage {
    position: absolute;
    left: 0;
    bottom: 0;
    width: var(--cave-lift-cage);
    height: var(--cave-lift-cage);
    image-rendering: pixelated;
  }

  /* The winding cable: shaft top down to the cage's roof, so it shortens as the
     cage descends. */
  .cave-lift-cable {
    position: absolute;
    left: 50%;
    bottom: var(--cave-lift-cage);
    width: 2px;
    margin-left: -1px;
    height: calc(var(--cave-lift-headroom) + var(--cave-lift-ledge)
      + var(--lift-floor) * var(--cave-lift-pitch) - var(--cave-lift-cage));
    background: var(--rock-lit);
    /* Same clock as the cage: a cable that jumped to its final length while the
       cage still travelled would stick out above the shaft. */
    transition: height 700ms ease-in-out;
  }

  .cave-rail-name,
  .cave-rail-summary {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    white-space: normal;
    clip-path: none;
  }

  .cave-rail-name {
    font-size: 0.9rem;
    color: var(--text-dim);
  }

  .cave-rail-step.is-open .cave-rail-name {
    color: var(--text);
    font-weight: 600;
  }

  .cave-rail-step.is-done .cave-rail-name {
    color: var(--text-faint);
  }

  .cave-rail-summary {
    grid-column: 2;
    font-size: 0.76rem;
    line-height: 1.35;
    color: var(--text-faint);
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
}

/* The cage still moves between floors, it just arrives at once. */
@media (prefers-reduced-motion: reduce) {
  .cave-lift,
  .cave-lift-cable {
    transition: none;
  }

  /* A fourteen-second descent is exactly the travel this setting exists to spare a
     reader, so the ride's slow transition goes too: the cage jumps to Ranking
     and the box still says where the ranking is. The box's own fade is opacity
     only and is kept — it is what tells cave.js the element may go. */
  #cave-app.is-riding .cave-lift,
  #cave-app.is-riding .cave-lift-cable {
    transition: none;
  }
}

/* ── Narrow ───────────────────────────────────────────────────── */
@media (max-width: 600px) {
  #cave-app {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  .cave-stage-head {
    padding: 0.7rem 0.75rem;
    gap: 0.45rem;
  }

  .cave-stage-body {
    padding: 0.1rem 0.75rem 0.85rem;
  }

  .cave-topic-seg {
    font-size: 0.66rem;
    padding: 0 0.2rem;
  }
}
