* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #12161c;
  color: #f1f3f5;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* no double-tap zoom */
  /* no text selection or the iPhone's copy / look-up bubble on a double tap or long press */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

#app {
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 8px env(safe-area-inset-bottom);
}

#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 6px;
  font-size: 18px;
}
.stat b { font-variant-numeric: tabular-nums; }

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* #board is sized from JS to the largest 2:3 box that fits #stage. */
#board {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
#game { display: block; width: 100%; height: 100%; }

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  gap: 10px;
  background: rgba(10, 12, 16, 0.9);
  text-align: center;
  padding: 24px;
  overflow-y: auto;
}
#overlay[hidden] { display: none; }
/* With the unlock card showing, skip the stats so everything fits. */
#overlay.unlocked #summary { display: none; }
#overlay h1 { margin: 0; font-size: 26px; }
#overlay p { margin: 0 0 8px; color: #c9ced6; }

/* The build menu and the controls swap in the same fixed-height bar, so the
   board never jumps when a menu opens. */
#menu, #controls { height: 136px; flex: none; }
#controls {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 0 12px;
}
#controls .row { display: flex; gap: 8px; }
#next { margin: 0; padding: 0 4px; font-size: 13px; line-height: 1.35; color: #c9ced6; min-height: 18px; }
#next .new { display: block; color: #ffd166; }
#next b { color: #f1f3f5; }
#controls button { height: 52px; }

button {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 10px;
  padding: 14px 16px;
  color: #1b1206;
  background: #ffb347;
  cursor: pointer;
}
button:disabled { background: #3a4049; color: #9aa1ab; cursor: default; }
button.urgent:not(:disabled) { background: #ff6b3d; animation: pulse 0.8s ease-in-out infinite; }
#start { flex: 1; }
#speed { min-width: 64px; background: #2b313a; color: #f1f3f5; }
#speed.fast { background: #4a3b2c; color: #ffb347; }

#hint {
  position: absolute;
  left: 0;
  right: 0;
  top: 52px;
  margin: 0 auto;
  width: max-content;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(10, 12, 16, 0.72);
  font-size: 15px;
  pointer-events: none;
  animation: pulse 1.6s ease-in-out infinite;
}
#hint[hidden] { display: none; }
@keyframes pulse { 50% { opacity: 0.55; } }

#menu {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 0 12px;
}
#menu[hidden], #controls[hidden] { display: none; }
#menu-title { font-weight: 600; color: #c9ced6; padding: 0 4px; }
#menu-actions { display: flex; gap: 8px; }
#menu-actions button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 10px 12px; }
#menu-actions button small { font-weight: 500; font-size: 12px; opacity: 0.85; line-height: 1.2; }
#menu-actions button { font-size: 15px; padding: 8px 6px; }
/* Sell takes a third of the row, the same size as when choosing a level-3
   path, so the upgrade button gets the rest. */
#menu-actions button.sell-small { flex: 0 0 calc((100% - 56px - 24px) / 3); min-width: 0; }
#menu-actions button.close { flex: 0 0 56px; justify-content: center; background: #2b313a; color: #f1f3f5; }
button.secondary { background: #3a4049; color: #f1f3f5; }

#mute { padding: 6px 10px; background: transparent; font-size: 18px; color: inherit; }

#stars { font-size: 34px; letter-spacing: 4px; min-height: 40px; }
#stars .off { opacity: 0.25; filter: grayscale(1); }
#summary {
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 18px;
  margin: 4px 0 6px;
  font-size: 15px;
  text-align: left;
}
#summary dt { color: #9aa1ab; }
#summary dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
#best { margin: 0 0 6px; font-size: 14px; color: #ffd166; min-height: 18px; }
#overlay { animation: fadein 0.4s ease-out; }
@keyframes fadein { from { opacity: 0; transform: scale(0.97); } }

/* Build / upgrade buttons with an animated tower picture */
#menu-actions button.with-icon { padding: 4px 4px 6px; gap: 0; justify-content: flex-start; }
#menu-actions button.with-icon > span { font-size: 13px; font-weight: 700; white-space: nowrap; }
#menu-actions button.with-icon small { font-size: 11px; }
.tower-icon { width: 44px; height: 44px; display: block; }
#menu-actions button:disabled .tower-icon { opacity: 0.45; filter: grayscale(0.7); }

/* HUD reactions */
.stat b { display: inline-block; }
#gold.bump { animation: bump 0.35s ease-out; }
@keyframes bump { 40% { transform: scale(1.35); color: #ffd166; } }
.stat.hurt { animation: hurt 0.5s ease-out; }
@keyframes hurt {
  20% { transform: translateX(-4px); color: #ff6b5a; }
  40% { transform: translateX(4px); color: #ff6b5a; }
  60% { transform: translateX(-2px); color: #ff6b5a; }
}
.flying-coin {
  position: fixed;
  z-index: 10;
  font-size: 16px;
  pointer-events: none;
  transform: translate(0, 0);
  transition: transform 0.6s cubic-bezier(0.5, -0.4, 0.7, 1), opacity 0.6s ease-in;
  margin: -8px 0 0 -8px;
}
@media (prefers-reduced-motion: reduce) {
  .flying-coin { display: none; }
  #gold.bump, .stat.hurt { animation: none; }
}

/* Title screen */
#title {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: radial-gradient(circle at 50% 38%, rgba(60, 30, 15, 0.9), rgba(10, 12, 18, 0.94) 60%);
  z-index: 5;
}
#title[hidden] { display: none; }
#title.leaving { animation: title-out 0.45s ease-in forwards; }
@keyframes title-out { to { opacity: 0; transform: scale(1.04); } }
#title-ember { width: 120px; height: 120px; }
.logo { margin: 0; line-height: 1; }
.logo span {
  display: block;
  font-size: 54px;
  font-weight: 800;
  letter-spacing: 1px;
  background: linear-gradient(180deg, #ffe28a, #ff8a2a 60%, #e0442a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 3px 8px rgba(255, 110, 40, 0.45));
}
.logo small { display: block; margin-top: 6px; font-size: 15px; font-weight: 700; letter-spacing: 6px; text-transform: uppercase; color: #c9ced6; }
.tagline { margin: 6px 0 8px; max-width: 280px; color: #c9ced6; font-size: 15px; line-height: 1.4; }
/* Play and Endless side by side; the Daily sits smaller underneath. */
.play-row { display: flex; gap: 10px; justify-content: center; }
#play-endless { min-width: 140px; font-size: 20px; padding: 16px 22px; background: linear-gradient(#7b6cff, #5a48e0); color: #fff; box-shadow: 0 6px 24px rgba(110, 90, 255, 0.35); }
#play { min-width: 140px; font-size: 20px; padding: 16px 28px; box-shadow: 0 6px 24px rgba(255, 140, 50, 0.4); animation: breathe 1.8s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.06); box-shadow: 0 8px 30px rgba(255, 140, 50, 0.6); } }
#title-best { margin: 2px 0 0; min-height: 18px; color: #ffd166; font-size: 14px; }

/* Daily button, links, player line */
.daily-btn { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 240px; padding: 8px 16px; font-size: 14px; background: #2b313a; color: #f1f3f5; border: 1px solid #ff8a2a; }
.daily-btn small { font-weight: 500; font-size: 12px; color: #c9ced6; max-width: 240px; }
.link-btn { background: transparent; color: #ffb347; padding: 6px 10px; font-weight: 600; }
.player-line { margin: 2px 0 0; font-size: 13px; color: #9aa1ab; }
.player-line b { color: #e8ecf1; }
.player-line .rename { padding: 0 4px; font-size: 13px; }

/* End screen score and rank */
#final-score { margin: 0; font-size: 30px; font-weight: 800; color: #ffd166; font-variant-numeric: tabular-nums; }
#final-score small { display: block; font-size: 12px; font-weight: 600; color: #9aa1ab; letter-spacing: 2px; text-transform: uppercase; }
#rank-line { margin: 0; min-height: 18px; font-size: 14px; color: #e8ecf1; }
.end-buttons { display: flex; gap: 8px; }
/* Win screen: the next map, and a card when a map is unlocked. */
#next-map { min-width: 240px; margin: 4px 0 6px; font-size: 18px; box-shadow: 0 6px 24px rgba(255, 140, 50, 0.4); animation: breathe 1.8s ease-in-out infinite; }
#next-map[hidden], .unlock-card[hidden] { display: none; }
.end-buttons.after-next #restart { background: #2b313a; color: #f1f3f5; }
.unlock-card { display: flex; gap: 12px; align-items: center; text-align: left; max-width: 300px; margin: 0 0 14px; padding: 10px 14px 10px 10px; border-radius: 14px; background: linear-gradient(135deg, #3a2a12, #2b313a); border: 2px solid #ffd166; box-shadow: 0 0 0 0 rgba(255, 209, 102, 0.6); animation: unlock-pop 0.6s cubic-bezier(0.2, 1.6, 0.4, 1) both, unlock-glow 1.6s ease-out 0.6s 2; }
.unlock-card canvas { width: 50px; height: 90px; border-radius: 6px; flex: none; }
.unlock-card small { display: block; color: #ffd166; font-weight: 700; font-size: 13px; letter-spacing: 0.02em; }
.unlock-card b { display: block; font-size: 18px; color: #fff; margin: 2px 0; }
.unlock-card span { display: block; font-size: 12px; color: #c9ced6; line-height: 1.35; }
@keyframes unlock-pop { from { transform: scale(0.4) rotate(-6deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes unlock-glow { from { box-shadow: 0 0 0 0 rgba(255, 209, 102, 0.6); } to { box-shadow: 0 0 0 16px rgba(255, 209, 102, 0); } }
.map-card .tag.new { color: #ffd166; }

/* Leaderboard panel */
#lb {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  padding: 16px 18px;
  background: #0e1118;
  animation: fadein 0.25s ease-out;
}
#lb[hidden] { display: none; }
.lb-head { display: flex; align-items: center; justify-content: space-between; }
.lb-head h2 { margin: 0; font-size: 22px; }
.lb-tabs { display: flex; gap: 6px; margin: 12px 0 4px; }
.lb-tabs button { flex: 1; padding: 10px; background: #2b313a; color: #c9ced6; font-size: 14px; }
.lb-tabs button[aria-selected="true"] { background: #ffb347; color: #1b1206; }
.lb-maps { display: flex; gap: 6px; justify-content: center; margin: 6px 0 2px; }
.lb-maps[hidden] { display: none; }
.lb-maps button { padding: 6px 12px; font-size: 13px; background: transparent; color: #c9ced6; border: 1px solid #4a525e; border-radius: 999px; }
.lb-maps button[aria-pressed="true"] { border-color: #ffb347; color: #ffb347; }
#lb-sub { margin: 4px 0 8px; font-size: 13px; color: #9aa1ab; min-height: 17px; }
#lb-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
#lb-list li {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 9px 8px;
  border-bottom: 1px solid #232832;
  font-size: 15px;
}
#lb-list li .rk { font-weight: 800; color: #9aa1ab; text-align: center; }
#lb-list li:nth-child(1) .rk { color: #ffd166; }
#lb-list li:nth-child(2) .rk { color: #d8dee9; }
#lb-list li:nth-child(3) .rk { color: #e0a865; }
#lb-list li .nm small { display: block; font-size: 12px; color: #9aa1ab; }
#lb-list li .sc { font-weight: 700; font-variant-numeric: tabular-nums; }
#lb-list li.me { background: rgba(255, 179, 71, 0.12); border-radius: 8px; }
#lb-list li.gap { display: block; text-align: center; color: #5b616b; border: 0; padding: 2px; }
#lb-list .empty { display: block; text-align: center; color: #9aa1ab; padding: 24px 0; border: 0; }

/* Pause button and menu */
.hud-buttons { display: flex; align-items: center; gap: 2px; }
#pause { padding: 6px 10px; background: transparent; color: #e8ecf1; line-height: 0; }
#pause:disabled { background: transparent; opacity: 0.35; }
#paused {
  position: absolute;
  inset: 0;
  z-index: 7;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(10, 12, 18, 0.88);
  animation: fadein 0.2s ease-out;
}
#paused[hidden] { display: none; }
#paused h1 { margin: 0 0 4px; font-size: 32px; }
#paused-info { margin: 0 0 8px; color: #c9ced6; font-size: 15px; }
#paused button { min-width: 200px; font-size: 18px; }

/* Map picker */
#maps {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  padding: 16px 18px;
  background: #0e1118;
  animation: fadein 0.2s ease-out;
}
#maps[hidden] { display: none; }
/* One row per map: a small picture of the board beside its name and blurb. */
#map-cards { display: grid; grid-template-columns: 1fr; align-content: start; gap: 6px; margin-top: 10px; overflow-y: auto; flex: 1; min-height: 0; }
.map-card {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  background: #1b2029;
  color: #f1f3f5;
  border: 2px solid #2b313a;
  border-radius: 12px;
  text-align: left;
}
.map-card:not(:disabled):active { border-color: #ffb347; }
.map-card canvas { width: 38px; flex: none; aspect-ratio: 400 / 720; border-radius: 6px; display: block; }
.map-card b { font-size: 15px; }
.map-card small { font-size: 12.5px; font-weight: 500; color: #9aa1ab; line-height: 1.3; }
.map-card .tag { color: #ffd166; font-weight: 600; }
.map-card .tag.badge { color: #ffe08a; }
/* Normal / Hard switch above the map cards; Hard cards glow red. */
#map-modes { margin-top: 10px; }
#map-modes[hidden] { display: none; }
#map-cards.hard .map-card:not(:disabled) { box-shadow: 0 0 0 2px #e2553a, 0 0 18px rgba(226, 85, 58, 0.35); }
.map-card:disabled { background: #161a21; color: #9aa1ab; }
.map-card:disabled canvas { filter: grayscale(0.8) brightness(0.45); }
.map-card .lock {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  padding-left: 60px;
  text-align: left;
  font-size: 14px;
  font-weight: 700;
  color: #f1f3f5;
}
.map-card .lock span { margin-right: 6px; font-size: 18px; }
.map-card:disabled > span:not(.lock) { visibility: hidden; }
.lb-tabs button { padding: 10px 4px; font-size: 13px; }

/* Send the next wave early */
#early { flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; padding: 6px 12px; background: #3d6b4a; color: #f1f3f5; line-height: 1.1; }
#early[hidden] { display: none; }
#early small { font-size: 12px; font-weight: 600; color: #ffd166; }
/* Level-3 path choice buttons */
#menu-actions button.path-btn small { font-size: 10.5px; }

/* First-game tips: a bubble on the board, a pulsing ring on what it's about,
   or a glow on the button it means. */
#coach {
  position: absolute;
  left: 12px;
  right: 12px;
  z-index: 4;
  padding: 12px 14px 10px;
  border-radius: 14px;
  background: rgba(20, 24, 31, 0.94);
  border: 2px solid #ffb347;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  color: #f1f3f5;
  animation: coach-in 0.35s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}
#coach[hidden], #coach-ring[hidden] { display: none; }
#coach.top { top: 12px; }
#coach.bottom { bottom: 12px; }
#coach p { margin: 0 0 8px; font-size: 15px; line-height: 1.4; }
.coach-buttons { display: flex; justify-content: space-between; align-items: center; }
#coach-ok { padding: 7px 16px; font-size: 14px; }
#coach-skip { font-size: 13px; color: #9aa3ad; }
#coach-ring {
  position: absolute;
  z-index: 3;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border-radius: 50%;
  border: 3px solid #ffd166;
  pointer-events: none;
  animation: coach-ring 1.2s ease-out infinite;
}
.coach-target { animation: coach-glow 1.2s ease-in-out infinite; }
@keyframes coach-in { from { transform: translateY(8px) scale(0.96); opacity: 0; } }
@keyframes coach-ring { 0% { transform: scale(0.7); opacity: 1; } 100% { transform: scale(1.35); opacity: 0; } }
@keyframes coach-glow { 50% { box-shadow: 0 0 0 4px rgba(255, 209, 102, 0.85), 0 0 22px rgba(255, 179, 71, 0.7); } }

/* Tower aim: a small chip beside the menu title; tap to switch. */
.menu-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#menu-aim { padding: 5px 12px; font-size: 13px; border-radius: 999px; background: #2b313a; color: #ffd166; border: 1px solid #4a525e; }
#menu-aim[hidden] { display: none; }

/* Music and sound-effect switches in the pause menu. */
.audio-toggles { display: flex; gap: 8px; margin-top: 10px; width: 200px; }
#paused .audio-toggles button { flex: 1; min-width: 0; padding: 8px 6px; font-size: 13px; white-space: nowrap; }
.audio-toggles button[aria-pressed="false"] { opacity: 0.6; }

/* The "coming soon" row after the last map. */
.map-card.coming-soon { border-style: dashed; background: transparent; color: #9aa1ab; }
.map-card.coming-soon .soon-icon { width: 38px; flex: none; text-align: center; font-size: 26px; }
