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

:root {
  /* Light: sandstone halls by daylight */
  --bg: #efe5d0;
  --bg-warm: #e3d5b8;
  --ink: #33281e;
  --ink-light: #85775f;
  --ink-muted: #b3a385;
  --accent: #b5562f;
  --accent-hover: #984423;
  --filled: #5b7ab8;
  --mark: #d4c6a8;
  --grid-line: #9a8a6e;
  --grid-thick: #33281e;
  --cell-bg: #fbf5e8;
  /* Canvas: dungeon walls, stone floors, shelves */
  --wall: #cbb893;
  --wall-line: #b39f78;
  --alcove: #8a7556;
  --stone: #a8977a;
  --stone-dark: #8f7f64;
  --stone-top: #c4b391;
  --stone-line: #7d6e55;
  --stone-edge: #4a3e2e;
  --wood: #7a4f2c;
  --scroll: #f0e2bd;
  --shadow: rgba(40, 25, 10, 0.55);
  --vignette: 0.22;
  --radius: 6px;
  --max-w: 760px;
}

/* Dark: torchlit dungeon */
html.dark {
  --bg: #12100e;
  --bg-warm: #1d1915;
  --ink: #ecdfc4;
  --ink-light: #8f8169;
  --ink-muted: #574d3f;
  --accent: #e08a3c;
  --accent-hover: #c47230;
  --filled: #3a5a9e;
  --mark: #2e2821;
  --grid-line: #5c5244;
  --grid-thick: #8f826c;
  --cell-bg: #1f1b17;
  --wall: #2a241e;
  --wall-line: #1c1814;
  --alcove: #15110d;
  --stone: #4a4238;
  --stone-dark: #3b342c;
  --stone-top: #5f5547;
  --stone-line: #2c2620;
  --stone-edge: #14110e;
  --wood: #5a3a20;
  --scroll: #cdb98c;
  --shadow: rgba(0, 0, 0, 0.85);
  --vignette: 0.6;
}

html { background: var(--bg); overscroll-behavior-y: none; }

body {
  font-family: 'DM Mono', monospace;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  overscroll-behavior-y: none;
}

/* ── Top bar: title + icons ── */
.top-bar {
  width: 100%;
  max-width: var(--max-w);
  padding: 0.75rem 1rem 0.25rem;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

h1 {
  font-family: 'Cinzel', 'Fraunces', serif;
  font-weight: 800;
  font-size: 1.4rem;
  letter-spacing: 0.02em;
  color: var(--ink);
}

h1 span { color: var(--accent); }

.timer-info {
  font-size: 0.8rem;
  color: var(--ink-light);
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.icon-btn {
  border: none;
  background: none;
  padding: 0.2rem;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  color: var(--ink-light);
}
.icon-btn:hover { border: none; color: var(--ink); }
#pauseBtn { font-size: 0.7rem; letter-spacing: -0.15em; }

/* ── Spacer pushes content toward bottom on mobile ── */
.mobile-spacer { flex: 1 1 auto; }

/* ── Top controls (seed) ── */
.top-controls {
  width: 100%;
  max-width: var(--max-w);
  padding: 0.25rem 1rem 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.controls-row input[type="text"] {
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  padding: 0.45rem 0.65rem;
  border: 1.5px solid var(--grid-line);
  border-radius: var(--radius);
  background: var(--cell-bg);
  color: var(--ink);
  flex: 1;
  min-width: 0;
  outline: none;
  transition: border-color 0.15s;
}
.controls-row input[type="text"]:focus { border-color: var(--accent); }
.controls-row input[type="text"]::placeholder { color: var(--ink-muted); }

button {
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  padding: 0.45rem 0.65rem;
  border: 1.5px solid var(--grid-line);
  border-radius: var(--radius);
  background: var(--cell-bg);
  color: var(--ink);
  cursor: pointer;
  transition: all 0.15s;
}

button:hover { border-color: var(--accent); color: var(--accent); }
button:active { transform: scale(0.97); }
.btn-accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.toggle-btn.active { border-color: var(--accent); color: var(--accent); background: rgba(224, 138, 60, 0.14); }
.btn-accent:hover { background: var(--accent-hover); color: #fff; border-color: var(--accent-hover); }

.seed-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  font-size: 0.7rem;
  color: var(--ink-muted);
}

.seed-display code {
  background: var(--bg-warm);
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  user-select: all;
}

/* ── HUD: hearts + pages/distance ── */
.hud {
  width: 100%;
  max-width: var(--max-w);
  padding: 0.5rem 1rem 0.35rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#heartsDisplay { font-size: 1.35rem; letter-spacing: 0.15em; color: var(--accent); }
.hud-left { display: flex; align-items: center; gap: 0.6rem; }
.hud-items { display: flex; align-items: center; gap: 0.45rem; font-size: 0.85rem; color: var(--ink); }
.potion-slot { display: inline-flex; align-items: center; gap: 0.15rem; font-weight: 500; color: #e05a7a; }
.potion-slot.empty { color: var(--ink-muted); opacity: 0.55; }
.potion-slot.empty svg { filter: grayscale(1); }
.heart-lost { color: var(--ink-muted); }

.hud-stats {
  display: flex;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--ink-light);
}
.hud-stats b { color: var(--ink); font-weight: 500; font-size: 1rem; }

/* ── Game canvas ── */
.game-wrap {
  width: 100%;
  max-width: var(--max-w);
  padding: 0 0.75rem;
}

#game {
  display: block;
  width: 100%;
  border: 1.5px solid var(--grid-thick);
  border-radius: var(--radius);
  background: var(--bg);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}

.hint {
  max-width: var(--max-w);
  padding: 0.6rem 1rem 0;
  text-align: center;
  font-size: 0.7rem;
  line-height: 1.6;
  color: var(--ink-muted);
}
.hint-touch { display: none; }

/* ── Bottom controls: big jump button on touch screens ── */
.bottom-controls {
  width: 100%;
  max-width: var(--max-w);
  padding: 0.6rem 1rem 0.75rem;
  display: none;
  justify-content: center;
  gap: 0.6rem;
}

.jump-btn {
  flex: 1;
  width: 100%;
  max-width: 420px;
  height: 64px;
  font-size: 1.05rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: var(--ink);
  color: var(--bg);
  border: none;
  border-radius: 32px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.jump-btn:hover { color: var(--bg); }
.jump-btn:active, .jump-btn.pressed { transform: scale(0.97); background: var(--accent); }
.throw-btn { background: var(--accent); color: #fff; }
.throw-btn:hover { color: #fff; }

@media (pointer: coarse) {
  .bottom-controls { display: flex; }
  .hint-desktop { display: none; }
  .hint-touch { display: inline; }
}

/* Safe-area padding for phones with gesture bars */
.bottom-pad {
  height: env(safe-area-inset-bottom, 0.5rem);
  min-height: 0.5rem;
}

/* ── Mobile ── */
@media (max-width: 420px) {
  h1 { font-size: 1.2rem; }
  .top-bar { padding: 0.5rem 0.75rem 0.15rem; }
  .game-wrap { padding: 0 0.5rem; }
}

/* ── Desktop: no spacer, content starts at top, bigger viewport ── */
@media (min-width: 700px) {
  :root { --max-w: 1200px; }
  .mobile-spacer { flex: 0 0 0; }
  .top-bar { padding: 1.5rem 1rem 0.5rem; }
  h1 { font-size: 1.6rem; }
}

#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 999;
}

@keyframes pop {
  0% { transform: scale(0.8); opacity: 0; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes game-shake {
  0%   { transform: translateX(0); }
  15%  { transform: translateX(-7px); }
  30%  { transform: translateX(7px); }
  50%  { transform: translateX(-5px); }
  70%  { transform: translateX(5px); }
  85%  { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}
.game-wrap.shake { animation: game-shake 0.5s ease; }

@keyframes heart-pulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); }
  100% { transform: scale(1); }
}
#heartsDisplay.pulse { animation: heart-pulse 0.4s ease; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal-box {
  background: var(--bg);
  border: 1.5px solid var(--grid-thick);
  border-radius: var(--radius);
  padding: 1.75rem 2.25rem;
  text-align: center;
  display: flex; flex-direction: column; gap: 0.75rem;
  min-width: 230px;
  max-width: calc(100vw - 2rem);
  max-height: calc(100dvh - 1rem);
  overflow-y: auto;
  animation: pop 0.35s ease;
}
.modal-title {
  font-family: 'Cinzel', 'Fraunces', serif;
  font-size: 1.5rem; font-weight: 700;
  color: var(--accent);
}
.modal-stats { font-size: 0.9rem; line-height: 1.6; color: var(--ink); }
.modal-sub { font-size: 0.75rem; color: var(--ink-muted); }
.modal-actions { display: flex; gap: 0.5rem; justify-content: center; margin-top: 0.25rem; }

/* ── Quiz modal ── */
.quiz-box { max-width: min(560px, calc(100vw - 2rem)); text-align: left; padding: 1.5rem 1.75rem; }
.quiz-kicker {
  font-family: 'Cinzel', 'Fraunces', serif;
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
  color: var(--accent);
}
.quiz-question { font-family: 'Fraunces', serif; font-size: 1.15rem; line-height: 1.4; color: var(--ink); }
.quiz-stakes { font-size: 0.72rem; color: var(--ink-light); }
.quiz-answers { display: flex; flex-direction: column; gap: 0.45rem; }
.quiz-answers button { text-align: left; line-height: 1.35; padding: 0.6rem 0.75rem; }
.quiz-answers button b { color: var(--ink-light); margin-right: 0.4rem; }
.quiz-answers button:disabled { cursor: default; transform: none; }
.quiz-answers button:disabled:hover { border-color: var(--grid-line); color: var(--ink); }
.quiz-answers button.right, .quiz-answers button.right:hover { border-color: #4f9a55; background: rgba(79, 154, 85, 0.18); }
.quiz-answers button.wrong, .quiz-answers button.wrong:hover { border-color: #d94040; background: rgba(217, 64, 64, 0.16); }
.quiz-feedback { font-size: 0.85rem; line-height: 1.5; color: var(--ink); min-height: 0; }
.quiz-feedback b.right { color: #4f9a55; }
.quiz-feedback b.wrong { color: #d94040; }
.quiz-actions { display: flex; justify-content: flex-end; gap: 0.5rem; }
.quiz-skip { background: none; color: var(--ink-light); }

/* Phones in landscape: compact quiz, answers in two columns, button always reachable */
@media (max-height: 520px) {
  .quiz-box { max-width: min(760px, calc(100vw - 1rem)); padding: 0.75rem 1rem; gap: 0.4rem; }
  .quiz-kicker { font-size: 0.8rem; }
  .quiz-question { font-size: 0.95rem; line-height: 1.3; }
  .quiz-answers { display: grid; grid-template-columns: 1fr 1fr; gap: 0.35rem; }
  .quiz-answers button { padding: 0.4rem 0.55rem; font-size: 0.75rem; }
  .quiz-feedback { font-size: 0.75rem; line-height: 1.35; }
  .quiz-actions { position: sticky; bottom: 0; }
  .quiz-actions button { padding: 0.5rem 1.25rem; }
}

/* ── Fullscreen button + layout ── */
.game-wrap { position: relative; }
.fs-btn {
  position: absolute;
  right: calc(0.75rem + 6px);
  bottom: 6px;
  width: 30px;
  height: 30px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  background: rgba(0, 0, 0, 0.45);
  color: #ecdfc4;
  border: 1px solid rgba(236, 223, 196, 0.35);
}
.fs-btn:hover { color: #fff; border-color: #fff; }

body.fs { height: 100dvh; min-height: 0; overflow: hidden; }
body.fs .top-bar, body.fs .top-controls, body.fs .mobile-spacer, body.fs .hint, body.fs .bottom-pad { display: none; }
body.fs .hud, body.fs .game-wrap, body.fs .bottom-controls { max-width: none; }
body.fs .hud { padding: 0.35rem 1rem 0.25rem; }
body.fs .game-wrap { flex: 1 1 0; min-height: 0; display: flex; align-items: center; padding-bottom: 0.25rem; }
body.fs .bottom-controls { padding: 0.35rem 1rem calc(0.35rem + env(safe-area-inset-bottom, 0px)); }
body.fs .jump-btn { height: 52px; }

/* ── Game over: small inline same-seed restart ── */
.go-seed { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 0.35rem; }
.go-seed code { background: var(--bg-warm); padding: 0.1rem 0.35rem; border-radius: 3px; user-select: all; }
.inline-btn {
  font-size: 0.7rem;
  padding: 0.15rem 0.45rem;
  border-width: 1px;
  color: var(--ink-light);
  background: none;
}

/* Fullscreen: exit button sits in the HUD row, clear of the game */
body.fs .hud { padding-right: 3.25rem; }
body.fs .fs-btn { position: fixed; top: 4px; right: calc(8px + env(safe-area-inset-right, 0px)); z-index: 5; }

/* Phone held sideways in fullscreen: slim HUD, edge-to-edge game,
   round see-through thumb buttons over the bottom corners */
@media (orientation: landscape) and (max-height: 520px) {
  body.fs .hud { padding: 0.15rem 3.25rem 0.15rem calc(0.75rem + env(safe-area-inset-left, 0px)); }
  body.fs #heartsDisplay { font-size: 1rem; }
  body.fs .hud-stats { font-size: 0.7rem; }
  body.fs .hud-stats b { font-size: 0.85rem; }
  body.fs .fs-btn { width: 24px; height: 22px; top: 2px; font-size: 0.8rem; }
  body.fs .game-wrap { padding: 0 env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px); }
  body.fs #game { border: none; border-radius: 0; }
  body.fs .bottom-controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 0 calc(1rem + env(safe-area-inset-right, 0px)) calc(0.75rem + env(safe-area-inset-bottom, 0px)) calc(1rem + env(safe-area-inset-left, 0px));
    justify-content: space-between;
    pointer-events: none;
    z-index: 4;
  }
  body.fs .jump-btn {
    pointer-events: auto;
    flex: 0 0 auto;
    width: 76px;
    height: 76px;
    padding: 0;
    border-radius: 50%;
    font-size: 0.7rem;
    letter-spacing: 0.04em;
    opacity: 0.6;
  }
  body.fs .jump-btn.pressed { opacity: 0.85; }
  body.fs #jumpBtn { order: 2; margin-left: auto; }
  body.fs .throw-btn { order: 1; }
}

/* Seed field keeps a usable width; buttons wrap below it on narrow screens */
.controls-row input[type="text"] { flex: 1 1 160px; }
@media (max-width: 520px) {
  .controls-row input[type="text"] { flex-basis: 100%; }
  .controls-row button { flex: 1 1 auto; }
}

/* ── Game over: highscore entry + board ── */
.hs-form { display: flex; flex-direction: column; gap: 0.4rem; }
.hs-kicker { font-family: 'Cinzel', 'Fraunces', serif; font-weight: 800; font-size: 0.95rem; color: var(--accent); }
.hs-row { display: flex; gap: 0.4rem; }
.hs-row input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.45rem 0.6rem;
  border: 1.5px solid var(--grid-line);
  border-radius: var(--radius);
  background: var(--bg-warm);
  color: var(--ink);
  outline: none;
}
.hs-row input:focus { border-color: var(--accent); }
.hs-error { font-size: 0.75rem; color: #d94040; min-height: 0; }
.hs-error:empty { display: none; }
.hs-board { text-align: left; }
.hs-title { font-family: 'Cinzel', 'Fraunces', serif; font-weight: 700; font-size: 0.85rem; color: var(--ink-light); text-align: center; margin-bottom: 0.3rem; }
.hs-board ol { list-style: none; margin: 0; padding: 0; font-size: 0.8rem; }
.hs-board li {
  display: grid;
  grid-template-columns: 1.6rem 1fr auto;
  gap: 0.5rem;
  align-items: baseline;
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
}
.hs-board li:nth-child(odd) { background: var(--bg-warm); }
.hs-board li.me { outline: 1.5px solid var(--accent); }
.hs-board .hs-rank { color: var(--ink-muted); text-align: right; }
.hs-board .hs-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
.hs-board .hs-stats { color: var(--ink-light); font-size: 0.72rem; white-space: nowrap; }
.hs-board .hs-stats b { color: var(--ink); font-size: 0.8rem; }
.hs-board .hs-hard { color: var(--accent); font-size: 0.65rem; margin-left: 0.3rem; }
.board-box { width: min(420px, calc(100vw - 2rem)); }
.hs-empty { font-size: 0.8rem; color: var(--ink-muted); text-align: center; }
.hs-empty:empty { display: none; }
