:root {
  --sky-top: #aee6ff;
  --sky-bottom: #ffe9c7;
  --stone-light: #d9c7a8;
  --stone-dark: #8a6f4e;
  --wall-color: #5b4630;
  --floor-a: #f3e6c9;
  --floor-b: #ecdab5;
  --accent-pink: #ff8fc7;
  --accent-purple: #b98cff;
  --accent-teal: #6fe3d1;
  --accent-green: #4caf7d;
  --text-dark: #4a3826;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100%;
  font-family: "Baloo 2", "Comic Sans MS", "Segoe UI Rounded", system-ui, sans-serif;
  color: var(--text-dark);
  overflow-x: hidden;
  background: linear-gradient(180deg, var(--sky-top) 0%, var(--sky-bottom) 100%);
  overscroll-behavior-y: contain;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

button {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

/* ---------- Background sky decoration ---------- */
.sky {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
}
.sun {
  position: absolute;
  top: 40px; right: 60px;
  width: 90px; height: 90px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff9d6, #ffd76b 60%, #ffbf3f 100%);
  box-shadow: 0 0 60px 20px rgba(255, 210, 100, .55);
}
.cloud {
  position: absolute;
  background: #fff;
  border-radius: 50px;
  opacity: .9;
  filter: drop-shadow(0 6px 0 rgba(0,0,0,0.03));
  animation: drift 60s linear infinite;
}
.cloud::before, .cloud::after {
  content: "";
  position: absolute;
  background: #fff;
  border-radius: 50%;
}
.c1 { width: 120px; height: 40px; top: 12%; left: -20%; }
.c1::before { width: 60px; height: 60px; top: -30px; left: 15px; }
.c1::after { width: 50px; height: 50px; top: -20px; left: 60px; }
.c2 { width: 90px; height: 32px; top: 28%; left: -30%; animation-duration: 80s; animation-delay: -20s; }
.c2::before { width: 46px; height: 46px; top: -22px; left: 10px; }
.c2::after { width: 40px; height: 40px; top: -16px; left: 45px; }
.c3 { width: 140px; height: 44px; top: 6%; left: -40%; animation-duration: 100s; animation-delay: -50s; }
.c3::before { width: 70px; height: 70px; top: -34px; left: 20px; }
.c3::after { width: 55px; height: 55px; top: -22px; left: 75px; }
@keyframes drift {
  from { transform: translateX(0); }
  to { transform: translateX(160vw); }
}

/* ---------- Utility buttons ---------- */
.icon-btn {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  right: calc(14px + env(safe-area-inset-right, 0px));
  z-index: 50;
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 3px solid #fff;
  background: rgba(255,255,255,.7);
  font-size: 20px;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0,0,0,.1);
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,.1); }

.btn {
  font-family: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 14px 28px;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  box-shadow: 0 5px 0 rgba(0,0,0,.15);
  transition: transform .1s ease, filter .15s ease, opacity .15s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.15); }
.btn-primary {
  background: linear-gradient(180deg, #ffd166, #ff9f43);
  color: #4a2e00;
}
.btn-primary:disabled {
  background: #d9d0c2;
  color: #8a8177;
  cursor: not-allowed;
  box-shadow: 0 5px 0 rgba(0,0,0,.08);
}
.btn-ghost {
  background: #fff;
  color: var(--text-dark);
  border: 2px solid #e0d3b8;
}
.btn.small { font-size: .85rem; padding: 8px 16px; box-shadow: 0 3px 0 rgba(0,0,0,.12); }

/* ---------- Screens ---------- */
.screen {
  display: none;
  min-height: 100svh;
  flex-direction: column;
  align-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom, 0px));
}
.screen.active { display: flex; }

.title {
  font-size: clamp(1.8rem, 5vw, 3rem);
  text-align: center;
  margin: 10px 0 6px;
  color: #7a4a2b;
  text-shadow: 2px 2px 0 #fff, 4px 4px 0 rgba(0,0,0,.08);
}
.title span {
  display: block;
  font-size: 0.42em;
  color: #a9784a;
  font-weight: 600;
  margin-top: 4px;
  text-shadow: none;
}
.subtitle {
  text-align: center;
  max-width: 560px;
  color: #6b5439;
  margin-bottom: 24px;
  line-height: 1.5;
}

/* ---------- Character picker ---------- */
.picker {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 28px;
}
.hero-card {
  width: 220px;
  max-width: 80vw;
  background: rgba(255,255,255,.75);
  border: 4px solid transparent;
  border-radius: 28px;
  padding: 18px 12px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  box-shadow: 0 8px 0 rgba(0,0,0,.08);
  transition: transform .15s ease, border-color .15s ease, box-shadow .15s ease;
  font-family: inherit;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  user-select: none;
}
.hero-card:hover { transform: translateY(-4px); }
.hero-card.selected {
  border-color: #ffb347;
  box-shadow: 0 8px 0 rgba(0,0,0,.08), 0 0 0 6px rgba(255,179,71,.25);
  background: rgba(255,255,255,.95);
}
.hero-art {
  width: 140px;
  height: 140px;
  margin-bottom: 8px;
  filter: drop-shadow(0 6px 6px rgba(0,0,0,.15));
}
.hero-art svg { width: 100%; height: 100%; }
.hero-name { font-weight: 800; font-size: 1.1rem; color: var(--text-dark); }
.hero-tag { font-size: .85rem; color: #8a7457; margin-top: 2px; }

.vs {
  font-weight: 900;
  font-size: 1.3rem;
  color: #c98a4b;
  text-align: center;
  line-height: 1.1;
}
.vs span { display:block; font-size:.6rem; font-weight:600; color:#a9784a; }

/* idle bob animation applied to character art via JS toggle */
.bob { animation: bob 2.4s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-1deg); }
  50% { transform: translateY(-8px) rotate(1deg); }
}

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  background: rgba(255,255,255,.8);
  border-radius: 999px;
  padding: 8px 18px;
  margin: 0 auto 16px;
  /* keep clear of the fixed mute button (44px + offset) in the corner */
  max-width: calc(100% - 66px);
  box-shadow: 0 4px 0 rgba(0,0,0,.08);
}
.hud-item { font-weight: 700; font-size: .95rem; white-space: nowrap; }

/* ---------- Maze ---------- */
.stage {
  display: flex;
  justify-content: center;
  width: 100%;
}
.maze-wrap {
  background: linear-gradient(180deg, var(--stone-light), var(--stone-dark));
  padding: 14px;
  border-radius: 18px;
  box-shadow:
    0 10px 0 rgba(0,0,0,.15),
    inset 0 0 0 4px rgba(255,255,255,.25);
  position: relative;
  touch-action: none;
  -webkit-touch-callout: none;
  user-select: none;
}
.maze-wrap::before {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: 24px;
  background-image:
    repeating-linear-gradient(90deg, rgba(0,0,0,.15) 0 6px, transparent 6px 26px);
  z-index: -1;
  opacity: .5;
}
.maze {
  position: relative;
  display: grid;
  background: var(--floor-a);
  border-radius: 8px;
  overflow: hidden;
}
.cell {
  position: relative;
  background-color: var(--floor-a);
}
.cell.floor-alt { background-color: var(--floor-b); }
.cell .w {
  position: absolute;
  background: var(--wall-color);
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.25), inset 0 2px 0 rgba(255,255,255,.15);
  border-radius: 3px;
}
.cell .w-top { top: -3px; left: -3px; right: -3px; height: 8px; }
.cell .w-bottom { bottom: -3px; left: -3px; right: -3px; height: 8px; }
.cell .w-left { top: -3px; bottom: -3px; left: -3px; width: 8px; }
.cell .w-right { top: -3px; bottom: -3px; right: -3px; width: 8px; }

.token {
  position: absolute;
  z-index: 5;
  transition: transform .14s ease-out;
  pointer-events: none;
  filter: drop-shadow(0 4px 4px rgba(0,0,0,.3));
}
.token svg { width: 100%; height: 100%; display:block; }
.token.player { z-index: 6; }
.token.goal {
  animation: goalPulse 1.6s ease-in-out infinite;
}
@keyframes goalPulse {
  0%, 100% { transform: translate(var(--tx), var(--ty)) scale(1); }
  50% { transform: translate(var(--tx), var(--ty)) scale(1.12); }
}
.token.player.walk-up { transform: translate(var(--tx), var(--ty)) rotate(0deg); }
.token.step {
  animation: step .14s ease-out;
}
@keyframes step {
  0% { transform: translate(var(--tx), var(--ty)) scale(1, 1); }
  40% { transform: translate(var(--tx), var(--ty)) scale(1.12, .88); }
  100% { transform: translate(var(--tx), var(--ty)) scale(1, 1); }
}

.sparkle {
  position: absolute;
  pointer-events: none;
  z-index: 4;
  font-size: 14px;
  animation: sparkle 1.4s ease-in-out infinite;
  opacity: .9;
}
@keyframes sparkle {
  0%, 100% { transform: translateY(0) scale(.8); opacity: .4; }
  50% { transform: translateY(-6px) scale(1.15); opacity: 1; }
}

/* ---------- D-Pad (touch controls) ---------- */
.dpad {
  margin-top: 18px;
  margin-bottom: env(safe-area-inset-bottom, 0px);
  display: grid;
  grid-template-columns: 58px 58px 58px;
  grid-template-rows: 58px 58px;
  gap: 8px;
  justify-content: center;
  flex-shrink: 0;
}
.dbtn {
  border: none;
  border-radius: 16px;
  background: rgba(255,255,255,.85);
  box-shadow: 0 5px 0 rgba(0,0,0,.15);
  font-size: 1.4rem;
  color: #7a5a3a;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  user-select: none;
}
.dbtn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.15); }
.dup { grid-column: 2; grid-row: 1; }
.dleft { grid-column: 1; grid-row: 2; }
.dright { grid-column: 3; grid-row: 2; }
.ddown { grid-column: 2; grid-row: 2; }

/* Hide the on-screen D-pad only for mouse-driven setups — arrow keys
   already cover them. Touch devices (any width/orientation) keep it,
   since a width breakpoint alone misses wide/landscape phones & tablets. */
@media (hover: hover) and (pointer: fine) {
  .dpad { display: none; }
}

/* ---------- Win modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(60, 40, 20, .45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay.show { display: flex; }
.modal {
  background: #fff8ea;
  border-radius: 28px;
  padding: 28px 32px 32px;
  text-align: center;
  max-width: 360px;
  box-shadow: 0 16px 0 rgba(0,0,0,.15);
  animation: popIn .35s cubic-bezier(.34,1.56,.64,1);
}
@keyframes popIn {
  from { transform: scale(.6); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.modal-hug {
  width: 160px;
  height: 100px;
  margin: 0 auto 8px;
}
.modal-hug svg { width: 100%; height: 100%; }
.modal h2 { margin: 4px 0; color: #7a4a2b; }
.modal p { color: #6b5439; margin-bottom: 18px; }

/* ---------- Mobile: stack the character picker cleanly ----------
   Two 220px cards + the VS badge don't fit one row below ~560px, and
   letting flex-wrap break them naturally produces a lopsided
   card / (vs+card) split. Force a tidy single column instead. */
@media (max-width: 560px) {
  .picker { flex-direction: column; gap: 10px; }
  .hero-card { width: min(82vw, 300px); }
  .vs { margin: -2px 0; }
}

/* ---------- Small phone tweaks ---------- */
@media (max-width: 420px) {
  .title { font-size: clamp(1.5rem, 8vw, 2.2rem); }
  .subtitle { font-size: .9rem; padding: 0 4px; }
  .hero-art { width: 100px; height: 100px; }
  .hero-name { font-size: .95rem; text-align: center; }
  .hero-tag { font-size: .75rem; text-align: center; }
  .hud { padding: 6px 10px; gap: 6px; }
  .hud-item { font-size: .8rem; }
  .btn.small { padding: 6px 10px; font-size: .75rem; }
}

@media (max-height: 560px) and (orientation: landscape) {
  .hud { margin-bottom: 8px; }
  .dpad { margin-top: 10px; grid-template-columns: 46px 46px 46px; grid-template-rows: 46px 46px; }
}

/* ---------- Confetti ---------- */
.confetti-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 90;
  overflow: hidden;
}
.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  opacity: .95;
  animation-name: fall;
  animation-timing-function: cubic-bezier(.35,0,.65,1);
  animation-iteration-count: 1;
}
@keyframes fall {
  from { transform: translateY(0) rotate(0deg); }
  to { transform: translateY(105vh) rotate(600deg); }
}
