:root {
  --bg: #0a0e1a;
  --accent: #ff5722;
  --accent-2: #ffc107;
  --good: #4caf50;
  --bad: #e53935;
  --panel: rgba(14, 20, 36, 0.92);
  --text: #f5f7fa;
  --muted: #93a1bd;
}

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

html, body {
  height: 100%;
  background: #000;
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

#game-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: var(--bg);
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.hidden { display: none !important; }

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  font-weight: 700;
}

.hud-top {
  position: absolute;
  top: env(safe-area-inset-top, 0);
  left: 0; right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
}

.hud-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0, 0, 0, 0.4);
  padding: 6px 14px;
  border-radius: 10px;
  font-size: 18px;
  min-width: 64px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.6);
}
.hud-coins { flex-direction: row; gap: 6px; color: var(--accent-2); font-size: 22px; }
.hud-label { font-size: 11px; color: var(--muted); letter-spacing: 1px; }

.hud-health-wrap {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom, 0) + 16px);
  left: 16px; right: 16px;
  pointer-events: none;
}
.hud-health-bar {
  height: 14px;
  background: rgba(0,0,0,0.5);
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.1);
}
#hud-health-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--bad), var(--good));
  transition: width 0.15s ease-out;
}

.hud-wave-progress {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0) + 62px);
  left: 16px; right: 16px;
  height: 5px;
  background: rgba(0,0,0,0.4);
  border-radius: 3px;
  overflow: hidden;
}
#hud-wave-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s linear;
}

/* Ability buttons */
#abilities {
  position: absolute;
  right: 14px;
  bottom: calc(env(safe-area-inset-bottom, 0) + 46px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  pointer-events: auto;
}
.ability-btn {
  position: relative;
  width: 62px; height: 62px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.25);
  background: rgba(20,28,48,0.85);
  color: #fff;
  font-size: 30px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: transform 0.08s;
}
.ability-btn:active { transform: scale(0.92); }
.ability-btn.ready { border-color: var(--accent-2); box-shadow: 0 0 14px rgba(255,193,7,0.5); }
.ability-key {
  position: absolute;
  bottom: 2px; right: 4px;
  font-size: 10px;
  color: var(--muted);
}
.ability-cd {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.65);
  transform-origin: bottom;
  transform: scaleY(0);
  pointer-events: none;
}

#pause-btn, #mute-btn {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0) + 70px);
  right: 16px;
  width: 40px; height: 40px;
  border-radius: 10px;
  border: none;
  background: rgba(0,0,0,0.4);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  pointer-events: auto;
}
#mute-btn {
  top: calc(env(safe-area-inset-top, 0) + 118px);
}

/* ---------- Overlays / Menus ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(10,14,26,0.7), rgba(0,0,0,0.92));
  backdrop-filter: blur(3px);
  padding: 16px;
  /* Allow the overlay itself to scroll if a panel is taller than the screen. */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.panel {
  background: var(--panel);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 18px;
  padding: 28px 26px;
  text-align: center;
  max-width: 440px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  /* margin:auto keeps the panel centred when it fits but lets the user scroll
     to BOTH ends when it doesn't (a flex child clipped at the top otherwise). */
  margin: auto;
  max-height: 100%;
  display: flex;
  flex-direction: column;
}
.panel.wide { max-width: 560px; }

/* Keep the primary action reachable on short screens: the shop body scrolls
   while the "Next Wave" button stays pinned to the bottom of the panel. */
#shop .panel { overflow: hidden; }
#shop-items { overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1 1 auto; }
#next-wave-btn { flex: 0 0 auto; margin-bottom: 0; }

.title {
  font-size: clamp(40px, 12vw, 64px);
  letter-spacing: 4px;
  background: linear-gradient(180deg, #fff, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 4px 20px rgba(255,87,34,0.3);
}
.subtitle { color: var(--muted); letter-spacing: 3px; margin-bottom: 26px; font-size: 14px; text-transform: uppercase; }
.panel-title { font-size: 30px; letter-spacing: 2px; margin-bottom: 14px; }
.panel-title.danger { color: var(--bad); }
.panel-coins { color: var(--accent-2); font-size: 22px; font-weight: 700; margin-bottom: 18px; }

.big-btn {
  display: block;
  width: 100%;
  margin: 16px 0 6px;
  padding: 16px;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 2px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), #d84315);
  border: none;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 6px 0 #a02d0f, 0 10px 20px rgba(0,0,0,0.4);
  transition: transform 0.08s, box-shadow 0.08s;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #a02d0f; }

.text-btn {
  background: none; border: none; color: var(--muted);
  font-size: 15px; cursor: pointer; margin-top: 12px; text-decoration: underline;
}

.help { margin-top: 22px; color: var(--muted); font-size: 13px; line-height: 1.9; }
.help strong { color: var(--text); }

.result-stats { line-height: 2; font-size: 18px; margin-bottom: 8px; }
.result-stats strong { color: var(--accent-2); }

/* ---------- Shop ---------- */
.shop-eyebrow {
  flex: 0 0 auto;
  font-size: 12px;
  letter-spacing: 2px;
  color: var(--muted);
  font-weight: 700;
}

/* Hero: the coin balance, framed as a spendable wallet. */
.wallet {
  flex: 0 0 auto;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  margin: 6px 0 2px;
}
.wallet-coin { font-size: 26px; }
.wallet-amount {
  font-size: 48px;
  font-weight: 900;
  line-height: 1;
  color: var(--accent-2);
  text-shadow: 0 0 18px rgba(255,193,7,0.35);
}
.wallet-amount.spent { animation: coinSpend 0.4s ease-out; }
.wallet-label {
  font-size: 13px;
  letter-spacing: 1px;
  color: var(--muted);
  text-transform: uppercase;
}
@keyframes coinSpend {
  0% { transform: scale(1.18); color: #fff; }
  100% { transform: scale(1); color: var(--accent-2); }
}
.shop-instruction {
  flex: 0 0 auto;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
}
.shop-instruction strong { color: var(--accent-2); }

.shop-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 0 0 16px;
  padding: 2px;
}
@media (min-width: 480px) {
  .shop-grid { grid-template-columns: repeat(3, 1fr); }
}

/* Each upgrade is a button so the whole card is an obvious tap target. */
.shop-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: center;
  background: rgba(255,255,255,0.04);
  border: 1.5px solid rgba(255,255,255,0.1);
  border-radius: 14px;
  padding: 14px 10px 12px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: transform 0.08s, border-color 0.12s, box-shadow 0.12s, opacity 0.12s;
}
.shop-card:active { transform: scale(0.96); }
.shop-card.affordable {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 1px rgba(255,193,7,0.25), 0 6px 18px rgba(0,0,0,0.35);
}
.shop-card.locked { opacity: 0.5; cursor: not-allowed; }
.shop-card.maxed { cursor: default; border-color: rgba(255,193,7,0.4); background: rgba(255,193,7,0.06); }
.shop-card:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

.card-top { display: flex; align-items: center; justify-content: center; position: relative; }
.card-icon { font-size: 36px; }
.card-badge {
  position: absolute;
  top: -4px; right: -2px;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: var(--bg);
  background: var(--accent);
  padding: 2px 6px;
  border-radius: 6px;
}
.card-name { font-size: 16px; font-weight: 800; margin-top: 6px; }
.card-desc { font-size: 11px; color: var(--muted); margin: 3px 0 8px; min-height: 26px; line-height: 1.3; }
.card-meta { font-size: 12px; color: var(--muted); min-height: 16px; margin-bottom: 10px; font-weight: 700; }

/* Level meter dots */
.card-pips { display: inline-flex; gap: 4px; justify-content: center; }
.pip { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,0.18); }
.pip.on { background: var(--accent-2); box-shadow: 0 0 6px rgba(255,193,7,0.6); }

/* The explicit Buy action — the fix for "how do I spend coins?" */
.card-buy {
  margin-top: auto;
  display: block;
  padding: 9px 8px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.3px;
}
.card-buy.can { background: var(--accent-2); color: #1a1200; box-shadow: 0 3px 0 #c79a00; }
.card-buy.can b { font-weight: 900; }
.card-buy.off { background: transparent; border: 1.5px solid rgba(255,255,255,0.18); color: var(--muted); }
.card-buy.maxed { background: transparent; border: 1.5px solid var(--accent-2); color: var(--accent-2); }

@media (min-width: 760px) {
  #game-shell {
    max-width: 520px;
    border-left: 1px solid rgba(255,255,255,0.06);
    border-right: 1px solid rgba(255,255,255,0.06);
  }
}
