:root {
  --ink: #17212e;
  --paper: #fffdf6;
  --shadow: rgba(12, 28, 46, 0.28);
  --gold: #f7c948;
  --grass: #4aa14a;
  --danger: #e4453a;
  --ui-font: 'Trebuchet MS', 'Avenir Next', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #8ed0ef;
  font-family: var(--ui-font);
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

#game { position: fixed; inset: 0; display: block; width: 100%; height: 100%; }

#ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
           max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}
#ui [data-ui] { pointer-events: auto; }

/* ---------- HUD ---------- */
.hud { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }

.score {
  font-size: clamp(40px, 9vmin, 76px);
  font-weight: 900;
  line-height: 0.95;
  color: #fff;
  letter-spacing: -0.02em;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.28), 0 8px 22px rgba(0, 0, 0, 0.22);
  font-variant-numeric: tabular-nums;
  transition: transform 90ms ease-out;
}
.score.pop { transform: scale(1.16); }
.score small {
  display: block;
  font-size: 0.26em;
  font-weight: 800;
  letter-spacing: 0.16em;
  opacity: 0.85;
  text-transform: uppercase;
}

.hud-right { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border-radius: 999px;
  background: rgba(255, 253, 246, 0.92);
  box-shadow: 0 3px 0 var(--shadow);
  font-weight: 800;
  font-size: clamp(14px, 3.2vmin, 19px);
  font-variant-numeric: tabular-nums;
}
.coin-dot {
  width: 16px; height: 16px; border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #ffe792, var(--gold) 62%, #d29d16);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.16);
}

.icon-row { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px;
  border: 0; border-radius: 12px;
  background: rgba(255, 253, 246, 0.92);
  box-shadow: 0 3px 0 var(--shadow);
  font-size: 18px; cursor: pointer;
  display: grid; place-items: center;
  transition: transform 80ms ease;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 0 0 var(--shadow); }
.icon-btn.off { opacity: 0.5; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  padding: 20px;
  background: rgba(10, 32, 52, 0.42);
  backdrop-filter: blur(3px);
  opacity: 0; visibility: hidden;
  transition: opacity 180ms ease, visibility 180ms;
  z-index: 5;
}
.overlay.show { opacity: 1; visibility: visible; }

.card {
  width: min(430px, 94vw);
  max-height: 92vh;
  overflow-y: auto;
  padding: 26px 24px 22px;
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 12px 0 rgba(12, 28, 46, 0.22), 0 26px 60px rgba(0, 0, 0, 0.32);
  text-align: center;
  transform: translateY(14px) scale(0.97);
  transition: transform 220ms cubic-bezier(0.2, 1.3, 0.5, 1);
}
.overlay.show .card { transform: none; }

.logo {
  font-size: clamp(34px, 8vmin, 52px);
  font-weight: 900;
  line-height: 0.92;
  letter-spacing: -0.03em;
  margin: 0 0 4px;
}
.logo span { display: block; }
.logo .a { color: var(--grass); }
.logo .b { color: #3d6ec9; }
.tagline { margin: 0 0 18px; font-weight: 700; opacity: 0.62; font-size: 15px; }

.big-btn {
  width: 100%;
  padding: 15px 18px;
  margin-top: 10px;
  border: 0; border-radius: 16px;
  background: var(--grass);
  color: #fff;
  font-family: inherit; font-size: 19px; font-weight: 900;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 5px 0 #33763a;
  transition: transform 70ms ease, box-shadow 70ms ease;
}
.big-btn:active { transform: translateY(5px); box-shadow: 0 0 0 #33763a; }
.big-btn.ghost { background: #e8e2d4; color: var(--ink); box-shadow: 0 5px 0 #c8c2b4; }
.big-btn.ghost:active { box-shadow: 0 0 0 #c8c2b4; }
.big-btn[disabled] { opacity: 0.45; cursor: default; }

.stat-row { display: flex; gap: 12px; margin: 16px 0 4px; }
.stat {
  flex: 1;
  padding: 12px 8px;
  border-radius: 16px;
  background: #f1ece0;
}
.stat b {
  display: block;
  font-size: clamp(24px, 6vmin, 34px);
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}
.stat span { font-size: 11px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; opacity: 0.55; }
.new-best { color: var(--gold); font-weight: 900; letter-spacing: 0.08em; text-transform: uppercase; font-size: 13px; height: 16px; }

.keys { margin-top: 16px; font-size: 13px; font-weight: 700; opacity: 0.6; line-height: 1.7; }
kbd {
  display: inline-block; min-width: 22px; padding: 1px 6px;
  border-radius: 6px; background: #e8e2d4; box-shadow: 0 2px 0 #c8c2b4;
  font-family: inherit; font-weight: 800; font-size: 12px;
}

/* ---------- character shop ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 16px 0 6px;
}
.char {
  position: relative;
  padding: 10px 6px 8px;
  border: 3px solid transparent;
  border-radius: 16px;
  background: #f1ece0;
  cursor: pointer;
  font-family: inherit;
  transition: transform 80ms ease;
}
.char:active { transform: scale(0.96); }
.char.selected { border-color: var(--grass); background: #e6f4e2; }
.char.locked { opacity: 0.92; }
.char-name { display: block; margin-top: 6px; font-size: 12px; font-weight: 800; }
.char-cost {
  display: block; margin-top: 2px; font-size: 11px; font-weight: 800;
  color: #8a6b12;
}
.char.affordable .char-cost { color: var(--grass); }

.portrait {
  position: relative;
  width: 46px; height: 46px;
  margin: 0 auto;
  border-radius: 10px;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.14);
}
.portrait i {
  position: absolute;
  display: block;
  border-radius: 2px;
}
.portrait .eye { width: 7px; height: 7px; top: 15px; }
.portrait .eye.l { left: 9px; }
.portrait .eye.r { right: 9px; }
.portrait .snout { width: 12px; height: 8px; left: 17px; top: 27px; border-radius: 3px; }
.portrait .crest { width: 8px; height: 9px; left: 19px; top: -6px; border-radius: 3px 3px 0 0; }
.lock-badge {
  position: absolute; inset: auto 6px 34px auto;
  font-size: 13px;
}

/* ---------- touch controls ---------- */
.dpad {
  display: none;
  justify-self: center;
  align-self: end;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 52px);
  gap: 7px;
  opacity: 0.9;
}
body.touch .dpad { display: grid; }
.dpad button {
  border: 0; border-radius: 14px;
  background: rgba(255, 253, 246, 0.86);
  box-shadow: 0 4px 0 var(--shadow);
  font-size: 21px; font-weight: 900; color: var(--ink);
  cursor: pointer;
}
.dpad button:active { transform: translateY(4px); box-shadow: 0 0 0 var(--shadow); }
.dpad .up { grid-column: 2; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }

/* ---------- misc ---------- */
.hint {
  position: fixed;
  left: 50%; bottom: 12%;
  transform: translateX(-50%);
  padding: 9px 18px;
  border-radius: 999px;
  background: rgba(255, 253, 246, 0.92);
  box-shadow: 0 3px 0 var(--shadow);
  font-weight: 800; font-size: 15px;
  animation: bob 1.5s ease-in-out infinite;
  z-index: 3;
}
.hint[hidden] { display: none; }
@keyframes bob {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-7px); }
}

.flash {
  position: fixed; inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  z-index: 4;
}
.flash.hit { animation: flash 260ms ease-out; }
@keyframes flash { from { opacity: 0.55; } to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .hint { animation: none; }
  .card, .overlay, .score { transition: none; }
}
