/* Diamond Mine — a warm, tactile mine. Page colours live on [data-theme];
   the canvas side of each look (soil, stone, gems, light) lives in js/themes.js. */

:root {
  --bg-a: #f5ecdd;
  --bg-m: #ecdfca;
  --bg-b: #dfcdb1;
  --blob1: rgba(255, 245, 222, 0.95);
  --blob2: rgba(214, 160, 82, 0.24);
  --blob3: rgba(150, 110, 70, 0.18);
  --ink: #3a2a1c;
  --ink-soft: #86705a;
  --paper: #fbf6ee;
  --paper-2: #f1e6d5;
  --line: rgba(80, 55, 25, 0.14);
  --accent: #a96e28;
  --accent-2: #d29a48;
  --accent-ink: #fffaf0;
  --gem: #4f9ccf;
  --gem-open: #2f8a57;
  --stat-bg: rgba(255, 255, 255, 0.42);
  --stat-border: rgba(255, 255, 255, 0.7);
  --btn-bg: rgba(255, 255, 255, 0.55);
  --btn-hover: rgba(255, 255, 255, 0.85);
  --btn-border: rgba(80, 55, 25, 0.15);
  --backdrop: rgba(50, 32, 14, 0.34);
  --sheet-shadow: rgba(60, 38, 16, 0.26);
  --frame-a: #7a5434;
  --frame-b: #4a301c;
  --frame-c: #2c1b0f;
  --frame-edge: rgba(30, 16, 6, 0.6);
  --frame-shadow: rgba(70, 42, 16, 0.55);
  --plaque: rgba(251, 246, 238, 0.86);
  --plaque-ink: #3a2a1c;
  --radius: 16px;
  --ease: cubic-bezier(0.4, 0.1, 0.2, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --head: var(--serif);
  --num: var(--serif);
  color-scheme: light;
}
:root[data-theme="crystal"] {
  --bg-a: #1a1f38;
  --bg-m: #12162a;
  --bg-b: #0a0c18;
  --blob1: rgba(110, 140, 255, 0.16);
  --blob2: rgba(140, 90, 240, 0.16);
  --blob3: rgba(80, 220, 255, 0.1);
  --ink: #e8eaf7;
  --ink-soft: #a1a7cb;
  --paper: #1d2240;
  --paper-2: #262c50;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #7b86f2;
  --accent-2: #a5adff;
  --accent-ink: #0d1024;
  --gem: #8fe0ff;
  --gem-open: #7ff2c0;
  --stat-bg: rgba(255, 255, 255, 0.05);
  --stat-border: rgba(255, 255, 255, 0.09);
  --btn-bg: rgba(255, 255, 255, 0.06);
  --btn-hover: rgba(255, 255, 255, 0.12);
  --btn-border: rgba(255, 255, 255, 0.12);
  --backdrop: rgba(4, 5, 14, 0.58);
  --sheet-shadow: rgba(0, 0, 0, 0.55);
  --frame-a: #4c5378;
  --frame-b: #2a2f4c;
  --frame-c: #14172a;
  --frame-edge: rgba(150, 200, 255, 0.3);
  --frame-shadow: rgba(0, 0, 0, 0.7);
  --plaque: rgba(24, 28, 54, 0.86);
  --plaque-ink: #e8eaf7;
  color-scheme: dark;
}
:root[data-theme="retro"] {
  --bg-a: #1e1b3c;
  --bg-m: #17152f;
  --bg-b: #100e22;
  --blob1: rgba(59, 85, 201, 0.18);
  --blob2: rgba(200, 116, 44, 0.1);
  --blob3: rgba(108, 208, 224, 0.08);
  --ink: #f1ecd8;
  --ink-soft: #aaa3cc;
  --paper: #23204a;
  --paper-2: #2e2a5c;
  --line: rgba(255, 255, 255, 0.12);
  --accent: #c8742c;
  --accent-2: #f0a050;
  --accent-ink: #160f06;
  --gem: #6cd0e0;
  --gem-open: #f0d050;
  --stat-bg: rgba(0, 0, 0, 0.25);
  --stat-border: rgba(108, 208, 224, 0.25);
  --btn-bg: rgba(0, 0, 0, 0.25);
  --btn-hover: rgba(108, 208, 224, 0.14);
  --btn-border: rgba(108, 208, 224, 0.3);
  --backdrop: rgba(5, 4, 14, 0.6);
  --sheet-shadow: rgba(0, 0, 0, 0.6);
  --frame-a: #6cd0e0;
  --frame-b: #3b55c9;
  --frame-c: #1b2470;
  --frame-edge: rgba(0, 0, 0, 0.8);
  --frame-shadow: rgba(0, 0, 0, 0.7);
  --plaque: rgba(18, 16, 40, 0.92);
  --plaque-ink: #f1ecd8;
  --radius: 4px;
  --head: "SF Mono", ui-monospace, Menlo, Consolas, "Liberation Mono", monospace;
  --num: "SF Mono", ui-monospace, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
button { touch-action: manipulation; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: var(--sans);
  color: var(--ink);
  background: linear-gradient(170deg, var(--bg-a), var(--bg-m) 55%, var(--bg-b));
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  -webkit-touch-callout: none;
}

/* ---------------------------------------------------------- background */
.bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; pointer-events: none; }
.bg .blob { position: absolute; border-radius: 50%; filter: blur(64px); will-change: transform; }
.bg .b1 { width: 60vmax; height: 60vmax; left: -22vmax; top: -26vmax; background: var(--blob1); animation: drift 52s ease-in-out infinite alternate; }
.bg .b2 { width: 48vmax; height: 48vmax; right: -18vmax; bottom: -20vmax; background: var(--blob2); animation: drift 64s ease-in-out infinite alternate-reverse; }
.bg .b3 { width: 40vmax; height: 40vmax; left: 30vw; bottom: -26vmax; background: var(--blob3); animation: drift 76s ease-in-out infinite alternate; }
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to { transform: translate(7vmax, 5vmax) scale(1.1); }
}

/* -------------------------------------------------------------- layout */
.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "stats" "stage" "actions";
  max-width: 1240px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.side { display: contents; }
.top { grid-area: top; }
.stats { grid-area: stats; }
.stage { grid-area: stage; }
.actions { grid-area: actions; }
.top, .stats, .actions { width: 100%; max-width: 760px; justify-self: center; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 4px 16px;
  min-width: 0;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 40px; height: 40px; flex: 0 0 auto; filter: drop-shadow(0 2px 3px rgba(40, 20, 5, 0.3)); }
.brand span { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.brand b { font-family: var(--head); font-weight: 600; font-size: clamp(18px, 5.4vw, 23px); letter-spacing: 0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { font-size: 11px; color: var(--ink-soft); letter-spacing: 0.1em; text-transform: uppercase; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
:root[data-theme="retro"] .brand b { font-size: clamp(15px, 4.6vw, 19px); font-weight: 700; letter-spacing: 0.02em; }
.menu { display: flex; gap: 2px; flex: 0 0 auto; }

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
:root[data-theme="retro"] .icon-btn { border-radius: 6px; }
.icon-btn:hover { background: var(--btn-hover); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.stats {
  display: grid;
  grid-template-columns: 0.95fr 1.2fr 1fr 0.82fr 1.05fr;
  gap: 6px;
  padding: 4px 14px 6px;
}
.stat {
  background: var(--stat-bg);
  border: 1px solid var(--stat-border);
  box-shadow: 0 1px 2px rgba(60, 40, 15, 0.06);
  border-radius: 12px;
  padding: 5px 4px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.15;
  min-width: 0;
  transition: background 0.4s, border-color 0.4s;
}
:root[data-theme="retro"] .stat { border-radius: 3px; border-width: 2px; }
.stat > span { font-size: clamp(9px, 2.45vw, 10.5px); letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-soft); white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.stat b { font-family: var(--num); font-size: clamp(16px, 4.6vw, 20px); font-weight: 600; font-variant-numeric: tabular-nums; display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; }
.stat b i { font-style: normal; }
.stat b small { font-size: 0.68em; color: var(--ink-soft); font-weight: 500; }
.stat b svg { width: 0.85em; height: 0.85em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; opacity: 0.75; }
.stat.gems b svg { color: var(--gem); opacity: 1; }
.stat.gems.open { border-color: color-mix(in oklab, var(--gem-open) 55%, transparent); background: color-mix(in oklab, var(--gem-open) 14%, var(--stat-bg)); }
.stat.gems.open b svg { color: var(--gem-open); }
.stat.low b { color: #c4492b; animation: breathe 1s ease-in-out infinite; }
:root[data-theme="crystal"] .stat.low b, :root[data-theme="retro"] .stat.low b { color: #ff8a6a; }
.bump { animation: bump 0.32s var(--spring); }
@keyframes bump { 40% { transform: scale(1.12); } }
@keyframes breathe { 50% { opacity: 0.55; } }

/* --------------------------------------------------------------- stage */
.stage {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 8px 12px;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.field {
  position: relative;
  z-index: 0;
  width: 100px;
  height: 100px;
  border-radius: 12px;
  container-type: size;
  touch-action: none;
}
/* a timber frame (iron in the crystal cave, chunky pixels in retro) */
.field::before {
  content: "";
  position: absolute;
  inset: -6px;
  z-index: -1;
  border-radius: 16px;
  background: linear-gradient(155deg, var(--frame-a), var(--frame-b) 45%, var(--frame-c));
  box-shadow:
    0 0 0 1px var(--frame-edge),
    inset 0 1px 0 rgba(255, 255, 255, 0.28),
    0 26px 50px -22px var(--frame-shadow),
    0 8px 18px -8px var(--frame-shadow);
}
:root[data-theme="retro"] .field, :root[data-theme="retro"] .field canvas, :root[data-theme="retro"] .overlay, :root[data-theme="retro"] .paused { border-radius: 0; }
:root[data-theme="retro"] .field::before { inset: -8px; border-radius: 0; background: var(--frame-b); box-shadow: 0 0 0 4px var(--frame-c), 0 0 0 8px var(--frame-a), 0 20px 40px -18px var(--frame-shadow); }
.field::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 12px;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35), inset 0 3px 10px rgba(0, 0, 0, 0.35);
}
.field canvas { display: block; width: 100%; height: 100%; border-radius: 12px; touch-action: none; }
.stage.live { cursor: none; }
.stage.live .field { cursor: none; }

/* ------------------------------------------------------------ overlay */
.overlay { position: absolute; inset: 0; pointer-events: none; overflow: hidden; border-radius: 12px; }
.banner {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 2px;
  padding: 14px 24px 15px;
  border-radius: 20px;
  background: var(--plaque);
  color: var(--plaque-ink);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 14px 34px -14px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.35), inset 0 0 0 1px var(--line);
  opacity: 0;
  width: max-content;
  max-width: min(92%, 420px);
}
:root[data-theme="retro"] .banner { border-radius: 2px; box-shadow: 0 0 0 3px var(--frame-b), 0 0 0 5px var(--frame-a), 0 14px 34px -14px #000; }
.banner.show { animation: banner 2.6s var(--ease) both; }
.banner.hold { animation: banner-in 0.5s var(--ease) both; }
.banner.out { animation: banner-out 0.35s var(--ease) both; }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -40%) scale(0.96); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  78% { opacity: 1; transform: translate(-50%, -50%); }
  100% { opacity: 0; transform: translate(-50%, -58%); }
}
@keyframes banner-in { from { opacity: 0; transform: translate(-50%, -40%) scale(0.96); } to { opacity: 1; transform: translate(-50%, -50%); } }
@keyframes banner-out { from { opacity: 1; transform: translate(-50%, -50%); } to { opacity: 0; transform: translate(-50%, -60%) scale(0.98); } }
.banner small { font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; color: color-mix(in oklab, var(--plaque-ink) 60%, transparent); }
.banner b { font-family: var(--head); font-weight: 600; font-size: clamp(21px, 6.4vw, 30px); line-height: 1.15; text-wrap: balance; }
.banner span { font-size: 14.5px; color: color-mix(in oklab, var(--plaque-ink) 78%, transparent); }
.banner em { font-style: normal; font-size: 12.5px; margin-top: 8px; padding: 5px 12px; border-radius: 999px; background: color-mix(in oklab, var(--accent) 18%, transparent); color: var(--plaque-ink); }
.banner span:empty, .banner em:empty, .banner small:empty { display: none; }

.hint {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  margin: 0 auto;
  width: fit-content;
  transform: translateY(8px);
  font-size: 13px;
  line-height: 1.3;
  text-align: center;
  color: var(--plaque-ink);
  background: var(--plaque);
  padding: 7px 14px;
  border-radius: 16px;
  opacity: 0;
  transition: opacity 0.4s, transform 0.4s var(--ease);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: inset 0 0 0 1px var(--line), 0 6px 18px -8px rgba(0, 0, 0, 0.5);
  text-wrap: balance;
}
.touch .hint { right: 84px; margin-right: 0; }
.hint.show { opacity: 0.95; transform: translateY(0); }

.paused {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: rgba(10, 6, 2, 0.32);
  backdrop-filter: blur(3px) saturate(0.85);
  -webkit-backdrop-filter: blur(3px) saturate(0.85);
  animation: fade 0.35s var(--ease);
  cursor: pointer;
}
.paused-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 26px 16px;
  border-radius: 22px;
  background: var(--plaque);
  color: var(--plaque-ink);
  box-shadow: 0 16px 40px -16px rgba(0, 0, 0, 0.6), inset 0 0 0 1px var(--line);
  text-align: center;
  max-width: 86%;
}
:root[data-theme="retro"] .paused-card { border-radius: 2px; }
.paused-card svg {
  width: 52px;
  height: 52px;
  padding: 14px 12px 14px 16px;
  border-radius: 50%;
  fill: var(--accent-ink);
  stroke: none;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  box-shadow: 0 6px 16px -6px color-mix(in oklab, var(--accent) 70%, black), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  margin-bottom: 6px;
}
.paused-card b { font-family: var(--head); font-size: 24px; font-weight: 600; }
.paused-card span { font-size: 14px; opacity: 0.75; line-height: 1.35; }

/* ------------------------------------------------- the stick & grab */
.stick {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  z-index: 3;
  pointer-events: none;
  transition: opacity 0.25s;
}
.stick.fade { opacity: 0; }
.stick .ring {
  position: absolute;
  left: -46px;
  top: -46px;
  width: 92px;
  height: 92px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.08) 0 55%, rgba(255, 255, 255, 0.16) 56% 100%);
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.45), 0 4px 18px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.stick .a {
  position: absolute;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  opacity: 0.55;
  transition: opacity 0.12s, transform 0.12s;
}
.stick .a.l { left: 7px; top: 40px; border-right-color: #fff; border-left-width: 0; }
.stick .a.r { right: 7px; top: 40px; border-left-color: #fff; border-right-width: 0; }
.stick .a.u { top: 7px; left: 40px; border-bottom-color: #fff; border-top-width: 0; }
.stick .a.d { bottom: 7px; left: 40px; border-top-color: #fff; border-bottom-width: 0; }
.stick[data-dir="l"] .a.l, .stick[data-dir="r"] .a.r, .stick[data-dir="u"] .a.u, .stick[data-dir="d"] .a.d { opacity: 1; filter: drop-shadow(0 0 4px var(--accent-2)); }
.stick .knob {
  position: absolute;
  left: -22px;
  top: -22px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 32%, rgba(255, 255, 255, 0.95), rgba(255, 244, 220, 0.75) 45%, rgba(220, 180, 120, 0.6));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35), inset 0 -2px 4px rgba(0, 0, 0, 0.18);
  will-change: transform;
}
:root[data-theme="crystal"] .stick .knob { background: radial-gradient(circle at 38% 32%, #fff, rgba(200, 225, 255, 0.8) 45%, rgba(130, 150, 255, 0.6)); }
:root[data-theme="retro"] .stick .ring { border-radius: 0; }
:root[data-theme="retro"] .stick .knob { border-radius: 2px; background: var(--accent-2); box-shadow: 0 0 0 3px var(--accent-ink); }

.grab {
  position: absolute;
  right: max(18px, env(safe-area-inset-right));
  bottom: 18px;
  z-index: 3;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: 0;
  display: none;
  place-items: center;
  color: #fff;
  background: radial-gradient(circle at 40% 30%, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0.08) 60%), rgba(30, 20, 10, 0.42);
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.4), 0 6px 18px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  touch-action: none;
  cursor: pointer;
  transition: transform 0.15s var(--ease), background 0.2s, box-shadow 0.2s;
}
.touch .grab { display: grid; }
.grab svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.grab.held { transform: scale(0.92); background: color-mix(in oklab, var(--accent) 75%, transparent); }
.grab.armed { box-shadow: inset 0 0 0 2px var(--accent-2), 0 0 0 4px color-mix(in oklab, var(--accent-2) 45%, transparent), 0 6px 18px rgba(0, 0, 0, 0.3); background: color-mix(in oklab, var(--accent) 45%, rgba(30, 20, 10, 0.45)); }
:root[data-theme="retro"] .grab { border-radius: 4px; }

/* -------------------------------------------------------------- actions */
.actions { display: flex; gap: 10px; padding: 6px 14px 14px; }
.btn {
  appearance: none;
  flex: 1;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--ink);
  font: 500 15px var(--sans);
  padding: 11px 12px;
  min-height: 46px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(60, 40, 15, 0.08);
  transition: transform 0.15s, background 0.2s, opacity 0.2s, box-shadow 0.2s;
}
:root[data-theme="retro"] .btn { border-radius: 4px; border-width: 2px; font-family: var(--num); font-size: 14px; }
.btn svg { width: 20px; height: 20px; flex: 0 0 auto; }
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.42; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  border-color: transparent;
  color: var(--accent-ink);
  box-shadow: 0 5px 14px -5px color-mix(in oklab, var(--accent) 75%, black), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
}
:root[data-theme="crystal"] .btn.primary, :root[data-theme="retro"] .btn.primary { text-shadow: none; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { flex: 0 0 auto; background: transparent; box-shadow: none; border-color: transparent; color: var(--ink-soft); font-size: 13.5px; margin: 18px auto 0; display: flex; min-height: 40px; }

/* -------------------------------------------------------- sheets, dialogs */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--backdrop);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade 0.25s;
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  z-index: 21;
  max-width: 560px;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -10px 40px var(--sheet-shadow);
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise 0.32s var(--ease);
}
.sheet.wide { max-width: 720px; }
:root[data-theme="retro"] .sheet { border-radius: 4px 4px 0 0; box-shadow: 0 0 0 3px var(--frame-b), 0 -10px 40px var(--sheet-shadow); }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 22px; flex: 0 0 auto; }
.sheet h2, .dialog h2 { font-family: var(--head); font-weight: 600; font-size: 24px; margin: 0; letter-spacing: 0.01em; }
.sheet-body {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 2px 22px 26px;
  font-size: 15px;
  line-height: 1.55;
  -webkit-user-select: text;
  user-select: text;
}
.sheet-body h3 { font-family: var(--head); font-weight: 600; font-size: 17px; margin: 20px 0 8px; }
.prose p { margin: 0 0 12px; }
.prose ul { margin: 0; padding-left: 20px; }
.prose li { margin: 5px 0; }
.muted { color: var(--ink-soft); }
.note { font-size: 13px; margin: 8px 0 0; }
.about { font-size: 13px; margin-top: 24px; text-align: center; }
.empty { margin-top: 16px; }

@media (min-width: 700px) and (min-height: 560px) {
  .sheet {
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 24px;
    max-height: 82dvh;
    animation: pop 0.28s var(--ease);
  }
  :root[data-theme="retro"] .sheet { border-radius: 4px; }
}
@keyframes pop { from { opacity: 0; transform: translateY(-50%) scale(0.96); } }

.dialog {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 22;
  width: min(92vw, 410px);
  background: var(--paper);
  border-radius: 24px;
  padding: 24px 22px 20px;
  box-shadow: 0 20px 60px var(--sheet-shadow);
  text-align: center;
  animation: popc 0.32s var(--ease);
  overflow: hidden;
}
:root[data-theme="retro"] .dialog { border-radius: 4px; box-shadow: 0 0 0 3px var(--frame-b), 0 0 0 6px var(--frame-a), 0 20px 60px var(--sheet-shadow); }
@keyframes popc { from { opacity: 0; transform: translate(-50%, -50%) scale(0.94); } }
.dialog p { position: relative; margin: 6px 0 16px; color: var(--ink-soft); font-size: 15px; line-height: 1.5; }
.dialog .row { position: relative; display: flex; gap: 10px; }
.result .kicker { position: relative; display: block; font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--ink-soft); margin-bottom: 4px; }
.result h2 { position: relative; font-size: 30px; }
.result .glow {
  position: absolute;
  inset: -40% -20% auto;
  height: 70%;
  background: radial-gradient(ellipse at 50% 42%, color-mix(in oklab, var(--accent-2) 42%, transparent), transparent 65%);
  pointer-events: none;
}
.result[data-outcome="over"] .glow { opacity: 0.3; filter: grayscale(0.6); }
.gems-fall { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.gems-fall i {
  position: absolute;
  top: -20px;
  width: 12px;
  height: 14px;
  background: linear-gradient(160deg, #fff, var(--gem) 55%, color-mix(in oklab, var(--gem) 55%, #102040));
  clip-path: polygon(20% 0, 80% 0, 100% 32%, 50% 100%, 0 32%);
  opacity: 0;
}
.result[data-outcome="cleared"] .gems-fall i, .result[data-outcome="done"] .gems-fall i { animation: gemfall 2.4s var(--ease) both; }
.gems-fall i:nth-child(1) { left: 12%; animation-delay: 0.1s !important; }
.gems-fall i:nth-child(2) { left: 30%; animation-delay: 0.45s !important; transform: scale(0.8); }
.gems-fall i:nth-child(3) { left: 52%; animation-delay: 0.2s !important; }
.gems-fall i:nth-child(4) { left: 71%; animation-delay: 0.6s !important; transform: scale(0.7); }
.gems-fall i:nth-child(5) { left: 88%; animation-delay: 0.3s !important; }
@keyframes gemfall {
  0% { opacity: 0; transform: translateY(0) rotate(0); }
  15% { opacity: 0.9; }
  100% { opacity: 0; transform: translateY(150px) rotate(40deg); }
}
.result-stats { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 12px; }
.result-stats div, .summary div { display: flex; flex-direction: column; align-items: center; background: var(--paper-2); border-radius: 12px; padding: 8px 4px; line-height: 1.2; min-width: 0; }
:root[data-theme="retro"] .result-stats div, :root[data-theme="retro"] .summary div { border-radius: 2px; }
.result-stats span, .summary span { font-size: 10px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--ink-soft); white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.result-stats b, .summary b { font-family: var(--num); font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.final-score { position: relative; display: flex; flex-direction: column; align-items: center; margin: 2px 0 16px; }
.final-score span { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-soft); }
.final-score b { font-family: var(--num); font-size: 44px; font-weight: 600; line-height: 1.05; font-variant-numeric: tabular-nums; }
.final-score small { font-size: 13px; color: var(--ink-soft); margin-top: 2px; }
.final-score small.new { color: var(--accent); font-weight: 600; }
.final-score b:empty, .final-score span:empty, .final-score small:empty { display: none; }
.result[data-outcome="over"] .final-score { margin: 0; }

/* --------------------------------------------------------------- caves */
.caves { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding-top: 6px; }
.cave-card {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  border-radius: 14px;
  padding: 7px 7px 9px;
  text-align: left;
  font: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 7px;
  transition: transform 0.15s, border-color 0.2s, box-shadow 0.2s;
  min-width: 0;
}
:root[data-theme="retro"] .cave-card { border-radius: 3px; }
.cave-card:hover:not(:disabled) { border-color: var(--accent-2); box-shadow: 0 6px 16px -10px rgba(0, 0, 0, 0.5); }
.cave-card:active:not(:disabled) { transform: scale(0.97); }
.cave-card:disabled { cursor: default; opacity: 0.55; }
.cave-card.current { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent-2) 30%, transparent); }
.cave-card .pic { position: relative; border-radius: 8px; overflow: hidden; aspect-ratio: 40 / 22; background: #120b06; display: grid; place-items: center; }
.cave-card .pic canvas { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; display: block; }
.cave-card .pic .lock { position: absolute; width: 28px; height: 28px; fill: none; stroke: #fff; stroke-width: 1.8; padding: 6px; box-sizing: content-box; border-radius: 50%; background: rgba(0, 0, 0, 0.55); }
.cave-card:disabled .pic canvas { filter: grayscale(0.85) brightness(0.55) blur(1px); }
.cave-card .meta { display: flex; flex-direction: column; line-height: 1.25; padding: 0 3px; min-width: 0; }
.cave-card .meta small { font-size: 10.5px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.cave-card .meta b { font-family: var(--head); font-weight: 600; font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cave-card .meta span { font-size: 12.5px; color: var(--ink-soft); }
.cave-card.cleared .meta small::after { content: " ✓"; color: var(--gem-open); font-weight: 700; }
.cave-card.inter .meta small { color: var(--accent); }

/* ------------------------------------------------------------- results */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th { text-align: left; font-weight: 500; color: var(--ink-soft); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; padding: 4px; border-bottom: 1px solid var(--line); }
td { padding: 7px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th.score, td.score { text-align: right; }
td.score { font-family: var(--num); font-weight: 600; font-size: 15px; }
tr.done td.caves { color: var(--accent); font-weight: 600; }
.bests { display: grid; grid-template-columns: 1fr; gap: 0; }
.bests div { display: flex; justify-content: space-between; gap: 10px; padding: 6px 2px; border-bottom: 1px solid var(--line); font-size: 14px; }
.bests b { font-family: var(--num); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- settings */
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt {
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--paper-2);
  color: var(--ink);
  font: 500 14px var(--sans);
  padding: 8px 14px 8px 10px;
  min-height: 44px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.2s, background 0.2s, transform 0.15s;
}
:root[data-theme="retro"] .opt { border-radius: 3px; }
.opt:active { transform: scale(0.97); }
.opt span { display: flex; flex-direction: column; line-height: 1.2; }
.opt span b { font-weight: 500; }
.opt small { font-size: 12px; color: var(--ink-soft); }
.opt[aria-checked="true"] { border-color: var(--accent); background: var(--paper); box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent-2) 28%, transparent); }
.opts.lang .opt { padding-left: 14px; min-width: 96px; justify-content: center; }
.opts.pace .opt { border-radius: 14px; padding: 9px 14px; flex: 1 1 150px; }
.swatch { display: inline-block; width: 26px; height: 26px; border-radius: 50%; flex: 0 0 auto; position: relative; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.25); overflow: hidden; }
.swatch::after { content: ""; position: absolute; left: 50%; top: 50%; width: 11px; height: 12px; transform: translate(-30%, -40%); background: var(--gem, #fff); clip-path: polygon(20% 0, 80% 0, 100% 32%, 50% 100%, 0 32%); }
.swatch.sw-auto { background: conic-gradient(#c08a4a 0 33%, #414c7c 0 66%, #3b55c9 0); }
.swatch.sw-auto::after { display: none; }
.swatch.sw-retro { border-radius: 3px; }
.toggle { display: flex; align-items: center; gap: 12px; margin-top: 20px; cursor: pointer; font-size: 15px; line-height: 1.35; }
.toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle .track { flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: color-mix(in oklab, var(--ink-soft) 45%, transparent); position: relative; transition: background 0.25s; }
.toggle .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.25s var(--ease); }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(18px); }
.toggle input:focus-visible + .track { outline: 2px solid var(--accent-2); outline-offset: 2px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  top: calc(118px + env(safe-area-inset-top));
  transform: translateX(-50%) translateY(-8px);
  z-index: 15;
  background: rgba(36, 24, 14, 0.9);
  color: #fff6e6;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 14px;
  max-width: min(88vw, 420px);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.3s var(--ease);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
:root[data-theme="crystal"] .toast { background: rgba(200, 215, 255, 0.92); color: #10142a; }
:root[data-theme="retro"] .toast { border-radius: 2px; background: #f0d050; color: #1a1408; font-family: var(--num); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ------------------------------------------------------- small screens */
@media (max-width: 400px) {
  .top { padding: 8px 6px 2px 12px; }
  .brand { gap: 8px; }
  .brand img { width: 34px; height: 34px; }
  .menu .icon-btn { width: 40px; height: 44px; }
  .stats { gap: 4px; padding: 3px 10px 5px; }
  .stat { border-radius: 10px; padding: 4px 2px 5px; }
  .stage { padding: 6px 8px; }
  .actions { gap: 8px; padding: 5px 10px 12px; }
}
@media (max-height: 640px) and (orientation: portrait) {
  .brand small { display: none; }
  .actions { padding-bottom: 10px; }
  .btn { min-height: 42px; padding: 8px 10px; }
}
@media (max-width: 520px) {
  .btn { flex-direction: column; gap: 2px; padding: 7px 6px 6px; font-size: 12.5px; min-height: 52px; }
  .btn span { white-space: nowrap; }
  .btn svg { width: 21px; height: 21px; }
}

/* landscape phones: the cave takes the height, the controls a slim column */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    grid-template-columns: 210px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "side stage";
    max-width: none;
  }
  .side { display: flex; flex-direction: column; grid-area: side; min-height: 0; padding-left: max(4px, env(safe-area-inset-left)); }
  .top { padding: 8px 4px 2px 10px; }
  .brand img { width: 32px; height: 32px; }
  .brand span { display: none; }
  .menu .icon-btn { width: 38px; height: 40px; }
  .stats { grid-template-columns: 1fr 1fr; padding: 4px 8px; }
  .stat:nth-child(2) { grid-column: span 2; order: -1; }
  .actions { margin-top: auto; flex-direction: column; gap: 6px; padding: 6px 8px 10px; }
  .btn { flex-direction: row; min-height: 40px; padding: 6px 10px; font-size: 13.5px; }
  .stage { padding: 8px 10px 8px 6px; }
  .toast { top: auto; bottom: calc(18px + env(safe-area-inset-bottom)); }
}

@media (min-width: 900px) and (min-height: 700px) {
  .brand b { font-size: 26px; }
  .stage { padding: 12px 18px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg .blob { animation: none; }
  .sheet, .dialog, .backdrop, .paused, .gems-fall i, .stat.low b { animation: none !important; transition-duration: 0.01s; }
  .banner.show, .banner.hold, .banner.out { animation-duration: 0.2s; }
}
