:root {
  --bg: #18241d;
  --panel: #26392e;
  --panel-2: #314a39;
  --ink: #f1f6ee;
  --muted: #bcd3c2;
  --void: transparent;
  --ember: #ffb98a;   /* pastel peach accent */
  --ember-2: #ffe4b0; /* pastel cream */
  --danger: #f0968c;  /* pastel red */
  --gold: #f5daa0;    /* pastel gold */
  --exit: #cbb0f2;    /* pastel lavender */
  --divider: #7cae8c; /* strong section divider */
  --cell-gap: 4px;
  --radius: 12px;

  /* themed (overridden per level by JS; defaults = Wildwood, pastel) */
  --board-bg1: #1e2f25;
  --board-bg2: #18261e;
  --open: #91a488;
  --open-2: #849778;
  --open-edge: #6c7d63;
  --open-top: #b8c6ac;
  --tree1: #86b184;
  --tree2: #6f9d6e;
  --tree-edge: #5a8259;
  --water1: #a7c3d4;
  --water2: #90b0c2;
  --water-edge: #7593a5;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(120% 80% at 50% 0%, #223528 0%, var(--bg) 70%) fixed;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 10px calc(env(safe-area-inset-bottom) + 6px);
}

/* ---------- HUD ---------- */
#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  margin: 4px 0 0;
  background: var(--panel);
  border: 2px solid var(--divider);
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-icon { width: 30px; height: 30px; border-radius: 8px; box-shadow: 0 1px 4px #0009; }
.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 16px;
  line-height: 1;
  letter-spacing: .5px;
  background: linear-gradient(180deg, var(--ember-2), var(--ember));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand-text span { font-size: 11px; color: #bfe0c6; }

.hud-right { display: flex; align-items: center; gap: 6px; }
.stats { display: flex; gap: 6px; }
.stat {
  background: var(--panel-2);
  border: 1px solid #4a6a55;
  padding: 4px 8px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 13px;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 3px;
}
.stat-sub { color: var(--muted); font-weight: 700; }
.stat-icon { font-size: 12px; }
.icon-btn {
  width: 30px; height: 30px;
  border-radius: 9px;
  border: 1px solid #4a6a55;
  background: var(--panel-2);
  color: var(--ink);
  font-size: 16px; font-weight: 800; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn:active { transform: translateY(1px); }

/* ---------- Board ---------- */
main { flex: 1; display: flex; align-items: center; justify-content: center; }
#board-wrap {
  width: 100%;
  margin: 8px 0;
  background: linear-gradient(180deg, var(--board-bg1), var(--board-bg2));
  border: 3px solid var(--divider);
  border-radius: 16px;
  padding: 10px;
  box-shadow: inset 0 0 30px #0004;
  transition: background .4s ease;
}
#board {
  display: grid;
  gap: var(--cell-gap);
  width: 100%;
}
.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(14px, 4.4vw, 26px);
  user-select: none;
  transition: transform .08s ease, box-shadow .12s ease;
}
.cell.void { background: var(--void); }
.cell.open, .cell.monster, .cell.treasure, .cell.heal, .cell.end, .cell.start {
  background: var(--open);
  box-shadow: inset 0 -2px 0 var(--open-edge);
}
.cell.tree { background: var(--tree1); box-shadow: inset 0 -2px 0 var(--tree-edge); }
.cell.water { background: var(--water1); box-shadow: inset 0 -2px 0 var(--water-edge); }
.cell.collapsed {
  /* flat, soft "ash" tile — clean and clearly faded */
  background: #d5cdc0;
  box-shadow: inset 0 -2px 0 #b9afa0;
  opacity: .85;
}

/* the goal cell stands out with a bright purple ring */
.cell.end {
  box-shadow: 0 0 0 3px var(--exit), 0 0 16px 4px #a855f777, inset 0 -3px 0 var(--open-edge);
  z-index: 1;
  animation: endpulse 1.5s ease-in-out infinite;
}
@keyframes endpulse {
  0%,100% { box-shadow: 0 0 0 3px var(--exit), 0 0 8px 2px #a855f766, inset 0 -3px 0 var(--open-edge); }
  50%     { box-shadow: 0 0 0 3px #d8b4fe, 0 0 22px 8px #a855f7cc, inset 0 -3px 0 var(--open-edge); }
}
/* when the exit is a valid move, show the target highlight, not the purple ring */
.cell.end.target {
  animation: pulse 1s infinite ease-in-out;
  outline: 3px solid var(--ember-2);
  outline-offset: -3px;
  box-shadow: 0 0 14px 2px #ffb14baa, inset 0 -3px 0 var(--open-edge);
}

/* target highlight */
.cell.target {
  cursor: pointer;
  outline: 3px solid var(--ember-2);
  outline-offset: -3px;
  box-shadow: 0 0 14px 2px #ffb14baa, inset 0 -3px 0 var(--open-edge);
  animation: pulse 1s infinite ease-in-out;
}
.cell.target:active { transform: scale(.94); }
@keyframes pulse { 0%,100% { outline-color: var(--ember-2);} 50% { outline-color: var(--ember);} }

.cell .marker { pointer-events: none; }
.cell.player-here::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: 10px;
  box-shadow: 0 0 0 3px #fff, 0 0 18px 4px var(--ember);
  animation: glow 1.2s infinite ease-in-out;
}
@keyframes glow { 0%,100% { box-shadow: 0 0 0 3px #fff, 0 0 12px 2px var(--ember);} 50% { box-shadow: 0 0 0 3px #fff, 0 0 22px 8px var(--ember);} }

.badge {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: 10px;
  font-weight: 800;
  color: #fff;
  background: #0009;
  border-radius: 6px;
  padding: 0 3px;
  line-height: 14px;
}

/* ---------- Controls ---------- */
#controls {
  padding: 12px 4px 4px;
  margin-top: 8px;
  border-top: 3px solid var(--divider);
}
#message {
  text-align: center;
  min-height: 20px;
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 8px;
}
.control-row { display: flex; align-items: center; justify-content: center; gap: 14px; }

.die {
  width: 52px; height: 52px;
  box-sizing: border-box;
  padding: 8px;
  border-radius: 12px;
  background: #fbfbf8;
  box-shadow: 0 2px 0 #dcdcd6, 0 4px 10px rgba(0, 0, 0, .3);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  transition: transform .25s ease;
}
.die .pip {
  align-self: center;
  justify-self: center;
  width: 0; height: 0;           /* inactive pips take space but are invisible */
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #4a4a4a, #111);
}
.die .pip.on { width: 8px; height: 8px; }
.die.big { width: 80px; height: 80px; padding: 14px; margin: 8px auto; }
.die.big .pip.on { width: 12px; height: 12px; }

.die.rolling { animation: tumble .5s ease; }
@keyframes tumble {
  0%   { transform: rotate(0deg) scale(1); }
  25%  { transform: rotate(120deg) scale(1.12); }
  50%  { transform: rotate(220deg) scale(.94); }
  75%  { transform: rotate(320deg) scale(1.08); }
  100% { transform: rotate(360deg) scale(1); }
}

/* balanced on a corner = "ready to roll again" */
.die.on-corner { animation: tiltbob 1.5s ease-in-out infinite; }
@keyframes tiltbob {
  0%,100% { transform: rotate(45deg) translateY(0) scale(.88); }
  50%     { transform: rotate(45deg) translateY(-4px) scale(.88); }
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 22px;
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  background: var(--panel-2);
  box-shadow: 0 2px 0 rgba(0, 0, 0, .22);
  cursor: pointer;
}
.btn:active { transform: translateY(1px); box-shadow: 0 1px 0 rgba(0, 0, 0, .22); }
.btn.primary { background: var(--ember); color: #5a3410; box-shadow: 0 2px 0 rgba(0, 0, 0, .18); }
.btn.primary:active { box-shadow: 0 1px 0 rgba(0, 0, 0, .18); }
.btn.ghost { background: transparent; border: 1px solid #4a6a55; box-shadow: none; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0;
  background: #04120bcc;
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  z-index: 20;
}
.overlay.hidden { display: none; }
.panel {
  width: 100%; max-width: 360px;
  background: var(--panel);
  border: 2px solid var(--divider);
  border-radius: 18px;
  padding: 22px;
  text-align: center;
  box-shadow: 0 20px 60px #000a;
}
.panel h2 { margin: 0 0 10px; font-size: 24px; }
.panel.scroll { max-height: 82vh; overflow: auto; text-align: left; }
.rules { margin: 0 0 14px; padding-left: 20px; }
.rules li { margin: 8px 0; font-size: 14px; line-height: 1.4; color: var(--ink); }
.menu-best { color: var(--gold); font-weight: 800; margin: 2px 0 16px; }
.combat-monster { margin: 6px 0; }
.combat-emoji { font-size: 48px; display: block; }
.hint { color: var(--muted); font-size: 13px; margin-top: 4px; }
.combat-msg { min-height: 22px; margin: 8px 0 14px; font-weight: 700; }
.combat-actions { display: flex; flex-direction: column; gap: 8px; }

/* ---------- new: die "?" ready face, move chip, inventory ---------- */
.die-wrap { position: relative; display: flex; align-items: center; justify-content: center; }
.die .die-q {
  grid-column: 1 / 4; grid-row: 1 / 4;
  place-self: center;
  font-size: 26px; font-weight: 800; color: #9a9a9a;
}
.die.big .die-q { font-size: 40px; }
.move-chip {
  position: absolute; top: -12px; left: 50%; transform: translateX(-50%);
  background: linear-gradient(180deg, var(--ember-2), var(--ember));
  color: #3a1c00; font-weight: 800; font-size: 12px;
  padding: 2px 9px; border-radius: 999px; white-space: nowrap;
  box-shadow: 0 2px 6px #0007;
}
.move-chip.hidden { display: none; }
.inv { text-align: center; font-size: 14px; color: var(--ink); min-height: 18px; margin-bottom: 6px; letter-spacing: 1px; }

/* ---------- new: danger + bridge move targets ---------- */
.cell.target.danger {
  outline-color: var(--danger);
  box-shadow: 0 0 14px 3px #e0483abb, inset 0 -3px 0 var(--open-edge);
}
.cell.target.bridge-target {
  outline-color: #8fdcff;
  box-shadow: 0 0 14px 3px #3fa9e6bb, inset 0 -3px 0 #2b7fb4;
}

/* ---------- new: landing bounce + ash puff + footprints ---------- */
.cell.land { animation: land .35s ease; }
@keyframes land {
  0% { transform: scale(.4); }
  60% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
.cell.puff::before {
  content: "";
  position: absolute; inset: 0; border-radius: 8px;
  background: radial-gradient(circle, #efe4d2ee 0%, #cbbfae88 42%, transparent 72%);
  animation: puff .55s ease forwards;
  pointer-events: none;
}
@keyframes puff {
  0% { transform: scale(.6); opacity: .9; }
  100% { transform: scale(1.7); opacity: 0; }
}
.cell.collapsed::after {
  content: "👣";
  font-size: 62%;
  opacity: .3;
  filter: grayscale(1);
}

/* ---------- new: shop ---------- */
.shop { text-align: left; margin: 0 0 14px; }
.shop-head { display: flex; justify-content: space-between; align-items: center; font-weight: 800; margin-bottom: 8px; }
.shop-gold { font-size: 14px; color: var(--gold); }
.shop-item {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  background: #14311f; border: 1px solid #3a6b48; border-radius: 12px;
  padding: 10px; margin-bottom: 8px; color: var(--ink); cursor: pointer;
}
.shop-item:active { transform: translateY(1px); }
.shop-item:disabled { opacity: .45; cursor: not-allowed; }
.shop-emoji { font-size: 26px; }
.shop-info { display: flex; flex-direction: column; }
.shop-info small { color: var(--muted); font-size: 12px; }
.own { color: var(--gold); font-size: 12px; margin-left: 4px; }

/* ---------- new: simple player token ---------- */
.player-token {
  width: 66%; height: 66%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe0a0, var(--ember));
  border: 3px solid #fff;
  box-shadow: 0 2px 6px #0007;
}

/* ---------- new: toasts ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: 84px; transform: translateX(-50%);
  background: #14311f; color: #fff;
  border: 1px solid #3f7350;
  padding: 10px 16px; border-radius: 999px;
  font-weight: 700; font-size: 14px;
  box-shadow: 0 6px 20px #0009; z-index: 45;
  max-width: 90%;
}
.toast.hidden { display: none; }
.toast.update {
  bottom: auto; top: 10px;
  display: flex; align-items: center; gap: 10px;
}
.toast-btn {
  background: linear-gradient(180deg, var(--ember-2), var(--ember));
  color: #3a1c00; border: none; border-radius: 999px;
  padding: 4px 12px; font-weight: 800; cursor: pointer;
}

/* ---------- new: tutorial ---------- */
.tut-text {
  font-size: 15px; line-height: 1.45; color: var(--ink);
  min-height: 100px; text-align: left;
}
.tut-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; }
.tut-progress { color: var(--muted); font-size: 13px; }
.tut-btns { display: flex; gap: 8px; }
.tut-btns .btn { padding: 10px 16px; }

/* ---------- new: leaderboard ---------- */
.lb-list { list-style: none; padding: 0; margin: 0 0 14px; text-align: left; }
.lb-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 4px; border-bottom: 1px solid #2c5a3b;
}
.lb-rank { width: 22px; font-weight: 800; color: var(--gold); }
.lb-score { font-weight: 800; }
.lb-meta { margin-left: auto; color: var(--muted); font-size: 12px; }
.lb-empty { color: var(--muted); text-align: center; padding: 16px; }
