/* Cave Escape — styles. Scoped to #app: a game must never style `body` or bare
   elements, because the hub's chrome may live in an adjacent document and
   leaking selectors is the classic way games break their host. */

:root {
  --ink: #ece5d8;
  --dim: #97a0af;
  --faint: #5d6673;
  --flame: #ffab4a;
  --flame-hot: #ffd9a0;
  --ember: #ff5a22;
  --glow: #6fe0ee;
  --bg: #040507;
  --stone: #101318;
  --line: #2a2e36;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  color: var(--ink);
  font: 500 16px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  cursor: default;
}

#app.dragging {
  cursor: grabbing;
}

#scene {
  display: block;
  width: 100%;
  height: 100%;
}

/* --------------------------- cinematic overlays --------------------------- */

#vignette,
#darkveil,
#flash,
#fader,
#grain {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#vignette {
  background: radial-gradient(ellipse at 50% 46%, transparent 34%, rgba(0, 0, 0, 0.62) 100%);
  z-index: 3;
}

/* Closes in as the torch dies. Opacity driven from JS. */
#darkveil {
  background: radial-gradient(ellipse at 50% 48%, transparent 18%, rgba(2, 1, 0, 0.985) 92%);
  opacity: 0;
  z-index: 4;
}

/* Whole-screen color hits: warm for pickups, red-black for falls, white for daylight. */
#flash {
  opacity: 0;
  z-index: 5;
}

#fader {
  background: #000;
  opacity: 1;
  transition: opacity 0.9s ease;
  z-index: 6;
}

#grain {
  opacity: 0.05;
  z-index: 7;
  background-repeat: repeat;
  background-size: 180px 180px;
  animation: grain-shift 0.9s steps(4) infinite;
}

@keyframes grain-shift {
  0% { background-position: 0 0; }
  25% { background-position: -60px 40px; }
  50% { background-position: 40px -70px; }
  75% { background-position: -30px -30px; }
  100% { background-position: 0 0; }
}

/* ---------------------------------- HUD ---------------------------------- */

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 8;
}

#hud.hidden {
  display: none;
}

#torch-hud {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  left: max(18px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 10px;
}

#flame-icon {
  width: 26px;
  height: 26px;
  filter: drop-shadow(0 0 6px rgba(255, 171, 74, 0.75));
  transition: filter 0.5s, opacity 0.5s;
}

#flame-icon .flame-outer {
  fill: var(--flame);
}

#flame-icon .flame-inner {
  fill: var(--flame-hot);
}

#flame-icon.critical {
  animation: flame-panic 0.55s ease-in-out infinite;
}

@keyframes flame-panic {
  0%, 100% { transform: scale(1); opacity: 0.95; }
  50% { transform: scale(0.82) rotate(-4deg); opacity: 0.55; }
}

#flame-icon.dead {
  opacity: 0.25;
  filter: none;
}

#wick {
  position: relative;
  width: 148px;
  height: 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.09);
  box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.8);
  overflow: hidden;
  transform: skewX(-14deg);
}

#wick-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 100%;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--ember), var(--flame) 55%, var(--flame-hot));
  box-shadow: 0 0 10px rgba(255, 150, 60, 0.55);
  transition: width 0.3s linear;
}

#wick-fill.low {
  background: linear-gradient(90deg, #7e1d05, var(--ember));
}

#fuel-pct {
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--flame);
  min-width: 3ch;
  text-shadow: 0 0 8px rgba(255, 150, 60, 0.5);
}

#run-stats {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--dim);
  letter-spacing: 0.06em;
}

#stat-time {
  color: var(--ink);
  font-weight: 700;
}

#run-stats .sep {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--faint);
}

#toast {
  position: absolute;
  left: 50%;
  top: 16%;
  transform: translateX(-50%);
  padding: 10px 22px;
  font-size: 15px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--flame-hot);
  text-shadow: 0 0 14px rgba(255, 150, 60, 0.6);
  background: rgba(6, 5, 3, 0.55);
  border: 1px solid rgba(255, 171, 74, 0.22);
  border-radius: 3px;
  opacity: 0;
  transition: opacity 0.4s, transform 0.4s;
  text-align: center;
  max-width: 86vw;
}

#toast.on {
  opacity: 1;
  transform: translateX(-50%) translateY(6px);
}

#toast.cold {
  color: #aebdd6;
  text-shadow: 0 0 12px rgba(120, 150, 200, 0.5);
  border-color: rgba(140, 160, 200, 0.25);
}

#prompt {
  position: absolute;
  left: 50%;
  bottom: 12%;
  transform: translateX(-50%);
  font-size: 14px;
  color: var(--dim);
  letter-spacing: 0.08em;
  opacity: 0;
  transition: opacity 0.3s;
  text-align: center;
  max-width: 80vw;
}

#prompt.on {
  opacity: 1;
}

#prompt kbd {
  margin: 0 2px;
}

/* -------------------------------- screens -------------------------------- */

.screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 24px;
  z-index: 10;
  background: linear-gradient(100deg, rgba(3, 3, 4, 0.93) 0%, rgba(3, 3, 4, 0.82) 46%, rgba(3, 3, 4, 0.30) 100%);
}

.screen.hidden {
  display: none;
}

.inner {
  max-width: 480px;
  width: 100%;
}

.title-inner {
  text-align: left;
}

.mark {
  width: 44px;
  height: 20px;
  color: #7f8b99;
  margin-bottom: 4px;
  filter: drop-shadow(0 0 6px rgba(140, 170, 210, 0.25));
}

.kicker {
  margin: 0 0 6px;
  font-size: 11px;
  letter-spacing: 0.5em;
  color: var(--dim);
}

.kicker.warm {
  color: var(--flame);
}

.game-title {
  margin: 0;
  font-size: clamp(54px, 11vw, 96px);
  line-height: 0.92;
  font-weight: 900;
  letter-spacing: 0.02em;
  /* cracked-stone treatment, like the key art */
  background: linear-gradient(180deg, #f2ede2 0%, #c9c2b2 55%, #918a7a 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.65)) drop-shadow(0 0 26px rgba(255, 150, 60, 0.18));
}

.game-title .t-accent {
  background: linear-gradient(180deg, #ffcf7e 0%, #f59a35 55%, #c05d12 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.65)) drop-shadow(0 0 30px rgba(255, 140, 50, 0.5));
}

.game-title.win-title {
  background: linear-gradient(180deg, #fff7e2 0%, #ffe3a6 60%, #f5c877 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 44px rgba(255, 220, 150, 0.6));
}

.beats {
  margin: 2px 0 20px;
  font-size: 11.5px;
  letter-spacing: 0.16em;
  color: var(--faint);
  text-transform: uppercase;
}

.beats b {
  display: inline-block;
  min-width: 15px;
  height: 15px;
  line-height: 15px;
  text-align: center;
  margin-right: 4px;
  border-radius: 50%;
  background: rgba(255, 171, 74, 0.16);
  border: 1px solid rgba(255, 171, 74, 0.5);
  color: var(--flame);
  font-size: 9.5px;
}

.cour {
  margin: 2px 0 0;
  font-style: italic;
  color: var(--faint);
  font-size: 14px;
  letter-spacing: 0.06em;
}

h2 {
  margin: 0 0 8px;
  font-size: clamp(26px, 5vw, 34px);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h2.cold {
  color: #9fb0c8;
  text-shadow: 0 0 24px rgba(90, 120, 170, 0.35);
}

.tagline {
  color: var(--dim);
  margin: 10px 0 4px;
  font-size: 13px;
  letter-spacing: 0.34em;
}

.tagline.dim {
  letter-spacing: 0.06em;
  font-size: 15px;
  margin-bottom: 22px;
}

.pitch {
  margin: 18px 0 20px;
  color: #c9c2b4;
  font-size: 16px;
  line-height: 1.55;
}

.howto {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px 18px;
  color: var(--dim);
  font-size: 13px;
  margin: 0 0 26px;
}

.howto .dim {
  color: var(--faint);
}

/* Touch twin of the howto grid; hidden with the grid on coarse pointers. */
.touch-howto {
  display: none;
  color: var(--dim);
  font-size: 13px;
  margin: 0 0 26px;
}

.touch-howto b {
  color: var(--ink);
}

.stat-row {
  display: flex;
  gap: 28px;
  justify-content: center;
  margin: 22px 0 26px;
}

.stat-row > div {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stat-row .k {
  font-size: 10px;
  letter-spacing: 0.18em;
  color: var(--faint);
}

.stat-row .v {
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat-row .v.warm {
  color: var(--flame);
}

.best {
  color: var(--dim);
  font-size: 13px;
  margin: 18px 0 0;
  letter-spacing: 0.04em;
}

.menu-links {
  margin: 14px 0 0;
}

.link-btn {
  appearance: none;
  background: none;
  border: none;
  color: var(--faint);
  font: inherit;
  font-size: 12.5px;
  letter-spacing: 0.08em;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.link-btn:hover {
  color: var(--dim);
}

.row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.title-inner .row {
  justify-content: flex-start;
}

kbd {
  display: inline-block;
  padding: 1px 7px;
  margin: 0 3px 0 0;
  border: 1px solid #3a3d44;
  border-bottom-width: 2px;
  border-radius: 5px;
  background: #16181d;
  font: inherit;
  font-size: 11.5px;
  color: var(--ink);
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #15171c;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.14em;
  padding: 13px 28px;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.btn:hover {
  background: #1d2026;
  border-color: #4a4e58;
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: linear-gradient(180deg, #e88a2a, #c05a10);
  border-color: #ffab4a;
  color: #1a0e02;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
  box-shadow: 0 0 24px rgba(255, 140, 50, 0.25);
}

.btn.primary:hover {
  background: linear-gradient(180deg, #ff9d38, #d66818);
  box-shadow: 0 0 34px rgba(255, 150, 60, 0.45);
}

.whisper {
  position: absolute;
  right: max(26px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  margin: 0;
  color: var(--faint);
  font-size: 13px;
  font-style: italic;
  line-height: 1.6;
  text-align: right;
  opacity: 0.85;
}

.whisper .whisper-2 {
  color: #6b7787;
}

.help-inner {
  max-width: 560px;
  text-align: left;
}

.help-list p {
  color: #b9b2a4;
  font-size: 14.5px;
  line-height: 1.6;
  margin: 0 0 14px;
}

.help-list strong {
  color: var(--flame-hot);
  font-weight: 700;
}

.help-list em {
  color: var(--flame);
  font-style: normal;
  font-weight: 700;
}

/* ------------------------------ corner buttons ---------------------------- */

.icon-btn {
  position: absolute;
  z-index: 11;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(12, 13, 16, 0.7);
  color: var(--dim);
  font: 700 15px/1 ui-sans-serif, system-ui, sans-serif;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.icon-btn:hover {
  color: var(--ink);
  border-color: #4a4e58;
}

#btn-help {
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
}

#btn-mute {
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
}

#btn-mute svg {
  width: 18px;
  height: 18px;
}

#btn-mute .spk {
  fill: currentColor;
}

#btn-mute .waves {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
}

#btn-mute.muted .waves {
  display: none;
}

#btn-mute.muted .spk {
  opacity: 0.4;
}

/* ----------------------------- touch controls ----------------------------- */

#touch-ui {
  position: absolute;
  inset: 0;
  z-index: 9;
  pointer-events: none;
}

#touch-ui.hidden {
  display: none;
}

/* Two zones tile the whole screen — no dead column mid-screen. One thumb
   moves, the other acts and looks. */
#move-zone {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 50%;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

/* A drag anywhere on the right half peeks; the button cluster rides inside
   it — a tap hits the button, a drag elsewhere looks around. */
#peek-zone {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 50%;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

/* A thumb rarely lands on a small fixed circle. The pad catches the touch and
   the stick is placed under it, so anywhere in the left half works — the whole
   point is that the player never has to find the stick first. */
#stick {
  position: absolute;
  left: 26px;
  bottom: 34px;
  width: 118px;
  height: 118px;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.04);
  pointer-events: none;
  touch-action: none;
}

#stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(255, 171, 74, 0.28);
  border: 1.5px solid rgba(255, 171, 74, 0.55);
  transform: translate(-50%, -50%);
}

#touch-buttons {
  position: absolute;
  right: max(22px, env(safe-area-inset-right));
  bottom: max(30px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: flex-end;
  pointer-events: auto;
}

.tbtn {
  appearance: none;
  -webkit-appearance: none;
  width: 88px;
  height: 56px;
  border-radius: 10px;
  border: 1.5px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  font: 700 13px/1 ui-sans-serif, system-ui, sans-serif;
  letter-spacing: 0.12em;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.tbtn.active {
  background: rgba(255, 171, 74, 0.3);
  border-color: rgba(255, 171, 74, 0.6);
}

/* small pause icon, top-right below the run-stats (52px clears the hub shell's
   docked bar on phones). Sits above the zones it overlaps. */
#tbtn-pause {
  position: absolute;
  top: 52px;
  right: max(18px, env(safe-area-inset-right));
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(12, 13, 16, 0.7);
  color: var(--ink);
  font: 700 13px/1 ui-sans-serif, system-ui, sans-serif;
  padding: 0;
  z-index: 1;
  pointer-events: auto; /* #touch-ui is pointer-events: none — the zones opt back in, so must the button */
  appearance: none;
  -webkit-appearance: none;
  touch-action: none;
  cursor: pointer;
}

/* first-run coach marks — what each thumb does; each fades on first use */
#coach {
  position: absolute;
  inset: 0;
  z-index: 9;
  pointer-events: none;
}

#coach.hidden {
  display: none;
}

#coach-move,
#coach-peek,
#coach-act {
  position: absolute;
  top: 44%;
  font: 800 13px/1.5 ui-sans-serif, system-ui, sans-serif;
  letter-spacing: 0.14em;
  color: var(--ink);
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.9);
  white-space: nowrap;
  animation: coachPulse 1.6s ease-in-out infinite;
}

#coach-move {
  left: 9%;
}

#coach-peek {
  right: 24%;
}

#coach-act {
  right: max(18px, env(safe-area-inset-right));
  width: 88px;
  white-space: normal;
  text-align: center;
}

#coach .used {
  opacity: 0;
  animation: none;
  transition: opacity 0.3s;
}

@keyframes coachPulse {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}

/* Coarse pointers get the touch layer: the keyboard howto is meaningless on a
   phone, the in-run `?` gives way to the pad (⏸ pauses instead), and mute
   moves up beside pause — out of the CRAWL cluster that shares its corner. */
@media (pointer: coarse) {
  .howto {
    display: none;
  }
  .touch-howto {
    display: block;
  }
  #app.touch-on #btn-help {
    display: none;
  }
  #app.touch-on #btn-mute {
    top: 52px;
    right: 60px;
    bottom: auto;
  }
}

/* ------------------------------- small widths ----------------------------- */

@media (max-width: 640px) {
  .howto {
    grid-template-columns: 1fr;
  }
  .stat-row {
    gap: 18px;
  }
  .stat-row .v {
    font-size: 18px;
  }
  .whisper {
    display: none;
  }
  #wick {
    width: 104px;
  }
}

/* ------------------------------------------------ mobile pass
   Phones play in landscape. In portrait, a small non-blocking nudge — it never
   eats a touch, and it disappears the moment the device is turned. */
@media (orientation: portrait) and (hover: none) and (pointer: coarse) {
  #app::after {
    content: "↻  Best played in landscape";
    position: fixed;
    left: 50%;
    bottom: max(10px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 9999;
    padding: 0.42rem 0.9rem;
    border: 1px solid rgba(243, 237, 225, 0.18);
    border-radius: 999px;
    background: rgba(8, 10, 16, 0.82);
    color: #f3ede1;
    font: 600 0.72rem/1.2 system-ui, -apple-system, sans-serif;
    letter-spacing: 0.04em;
    white-space: nowrap;
    pointer-events: none;
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
  }
}

/* Phone landscape leaves ~300px of height once the browser's own UI is showing
   — far less than the menu is tall. A launch button you cannot reach is a game
   you cannot start, so the menu scales to whatever height it is given, with
   scrolling as the backstop for anything extreme. */
@media (max-height: 520px) {
  .screen {
    padding: 8px;
    overflow-y: auto;
    align-content: center;
  }
  .screen > .inner {
    transform: scale(0.72);
  }
}
@media (max-height: 460px) { .screen > .inner { transform: scale(0.62); } }
@media (max-height: 400px) { .screen > .inner { transform: scale(0.54); } }
@media (max-height: 360px) { .screen > .inner { transform: scale(0.48); } }
@media (max-height: 320px) { .screen > .inner { transform: scale(0.42); } }
@media (max-height: 280px) { .screen > .inner { transform: scale(0.36); } }

/* ── promo capture: narrative captions (?cine=1) ──────────────────────────
   Fades on GAME time (ui.storyUpdate), so fixed-stepped video capture keeps
   each line on screen for its full scripted duration. */
#story-line {
  position: absolute;
  left: 50%;
  top: 66%;
  transform: translateX(-50%);
  width: min(82vw, 760px);
  text-align: center;
  font-family: Georgia, 'Times New Roman', serif;
  font-style: italic;
  font-size: clamp(19px, 3.2vw, 28px);
  line-height: 1.45;
  color: #f2e4c8;
  background: rgba(5, 7, 10, 0.48);
  padding: 10px 28px;
  border-radius: 8px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.95), 0 0 22px rgba(0, 0, 0, 0.6);
  opacity: 0;
  transition: opacity 0.8s ease;
  pointer-events: none;
  z-index: 30;
}
#story-line.on { opacity: 1; }
