:root {
  --bg: #0e1a15;
  --panel: #16281f;
  --line: #243a2e;
  --text: #e8f0ea;
  --muted: #8fa89a;
  --accent: #d8672a;
  --accent-press: #b5541f;
  --coin: #f2c14e;
  --heart: #e2504a;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; touch-action: manipulation;
}
#app {
  display: flex; flex-direction: column; height: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* HUD */
#hud {
  display: flex; gap: 20px; align-items: center;
  padding: 12px 18px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.stat { display: flex; align-items: center; gap: 7px; font-size: 18px; font-weight: 600; }
.hud-diff { font-size: 11px; font-weight: 800; color: #fff; border-radius: 999px; padding: 3px 10px; letter-spacing: 0.02em; white-space: nowrap; cursor: pointer; }
.hud-diff:active { transform: translateY(1px); }
.stat .lbl { color: var(--muted); font-weight: 500; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; }
.ico { width: 16px; height: 16px; border-radius: 50%; display: inline-block; }
.ico.coin { background: var(--coin); box-shadow: inset 0 0 0 2px #c99a2e; }
.ico.heart {
  width: 15px; height: 15px; border-radius: 0; background: var(--heart);
  clip-path: path('M7.5 14 L1 7 A3.6 3.6 0 0 1 7.5 3 A3.6 3.6 0 0 1 14 7 Z');
}

/* Stage */
#stage {
  position: relative; flex: 1 1 auto;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(120% 90% at 50% 0%, #14261d 0%, var(--bg) 70%);
  min-height: 0;
}
#game {
  display: block; image-rendering: auto;
  border-radius: 6px; box-shadow: 0 8px 30px rgba(0,0,0,0.45);
  touch-action: none;
}
#loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 15px;
}

/* Bottom bar */
#bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line); flex: 0 0 auto;
  position: relative;
  min-height: calc(76px + env(safe-area-inset-bottom)); /* fits the labeled quick row → no resize when it opens/closes */
}
.build-tip {
  position: absolute; bottom: calc(100% + 18px); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 3px; white-space: nowrap; z-index: 6;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 8px 15px; box-shadow: 0 6px 20px rgba(0,0,0,0.45);
}
.build-tip .bt-row { display: flex; align-items: center; gap: 10px; }
.build-tip .bt-name { font-weight: 700; font-size: 14px; }
.build-tip .bt-price { display: flex; align-items: center; gap: 4px; font-weight: 800; font-size: 14px; color: #ffcf3f; }
.build-tip .bt-price.poor { color: var(--heart); }
.build-tip .bt-tag { font-size: 11px; font-weight: 600; color: var(--muted); }
.ttag { font-size: 10px; font-weight: 600; color: var(--muted); text-align: center; line-height: 1.15; margin-top: 2px; white-space: normal; }
.ticon { display: inline-flex; width: 14px; height: 14px; }
.ticon svg { width: 100%; height: 100%; display: block; }
.seltargets { display: flex; align-items: center; gap: 5px; margin-top: 4px; }
.seltargets .ticon { width: 16px; height: 16px; }
.seltargets .st-text { font-size: 12px; color: var(--muted); font-weight: 600; }
.build-tip .bt-tagline { display: flex; align-items: center; gap: 5px; }
.hint { color: var(--muted); font-size: 13px; }
#startBtn {
  border: 0; border-radius: 10px; padding: 12px 20px;
  background: var(--accent); color: #fff; font-size: 16px; font-weight: 600;
  cursor: pointer; transition: background 0.15s, transform 0.05s; white-space: nowrap;
}
#startBtn:active { transform: translateY(1px); background: var(--accent-press); }
#startBtn:disabled { background: #33463c; color: var(--muted); cursor: default; }

.actions { display: flex; gap: 8px; align-items: center; }
#upgradeBtn {
  border: 0; border-radius: 10px; padding: 12px 16px;
  background: var(--coin); color: #3a2c05; font-size: 15px; font-weight: 700;
  cursor: pointer; transition: transform 0.05s, background 0.15s;
}
#upgradeBtn:active { transform: translateY(1px); }
#upgradeBtn:disabled { background: #33463c; color: var(--muted); cursor: default; }
#sellBtn:disabled { background: #3a2530; color: var(--muted); cursor: default; }

/* Build tray (tower picker) */
#tray {
  display: flex; gap: 8px; padding: 8px 12px; overflow-x: auto;
  background: var(--panel); border-top: 1px solid var(--line);
  flex: 0 0 auto; -webkit-overflow-scrolling: touch;
}
#tray::-webkit-scrollbar { height: 0; }
.tcard {
  flex: 0 0 auto; width: 74px; padding: 6px 4px 5px;
  border: 2px solid transparent; border-radius: 12px;
  background: #10201a; color: var(--text); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  transition: border-color 0.12s, background 0.12s;
  position: relative;
}
.tcard .thumb { width: 46px; height: 46px; }
.tcard .tname { font-size: 10px; color: var(--muted); white-space: nowrap; }
.tcard .tcost { font-size: 13px; font-weight: 700; color: var(--coin); line-height: 1.1; }
.tcat { display: flex; flex-direction: column; gap: 5px; flex: 0 0 auto; }
.tcat-label { font-size: 10px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; padding-left: 3px; }
.tcat-row { display: flex; gap: 6px; }
.tcat + .tcat { border-left: 1px solid var(--line); padding-left: 9px; margin-left: 1px; }
.tcard.armed { border-color: var(--coin); background: #16281f; }
.tcard.poor { opacity: 0.45; }
.tcard:active { transform: translateY(1px); }

/* Selection panel (tower stats + sell/upgrade) */
#selpanel {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 12px; background: var(--panel);
  border-top: 1px solid var(--line); flex: 0 0 auto;
}
.selrow { display: flex; align-items: center; gap: 10px; }
#selthumb { width: 40px; height: 40px; flex: 0 0 auto; }
.selname { font-size: 15px; font-weight: 600; }
.sellvl { font-size: 12px; color: var(--muted); }
.selstats { display: flex; gap: 6px; }
.statchip { flex: 1; background: #10201a; border-radius: 8px; padding: 5px 4px; text-align: center; }
.statchip .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.statchip .v { font-size: 14px; font-weight: 600; margin-top: 1px; }
.selactions { display: flex; gap: 8px; }
.selactions button {
  flex: 1; border: 0; border-radius: 10px; padding: 12px;
  font-size: 15px; font-weight: 700; cursor: pointer; transition: transform 0.05s;
}
.selactions button:active { transform: translateY(1px); }
#sellBtn { background: #7a3b33; color: #ffdcd4; }
#upgradeBtn { background: var(--coin); color: #3a2c05; }
#upgradeBtn:disabled { background: #33463c; color: var(--muted); }
#setTargetBtn { background: #6b2f2a; color: #ffd0c8; }

/* Fixed-height dock: tray + detail stats (prevents canvas resize on select) */
#dock {
  flex: 0 0 auto; height: 172px; display: flex; flex-direction: column;
  background: var(--panel); border-top: 1px solid var(--line);
}
#tray { background: transparent; border-top: none; border-bottom: 1px solid var(--line); }
#detail {
  flex: 1 1 auto; min-height: 0; padding: 8px 12px;
  display: flex; flex-direction: column; gap: 6px; justify-content: center;
}
.detail-empty { color: var(--muted); font-size: 13px; text-align: center; }
.detail-head { display: flex; align-items: center; gap: 10px; }
.detail-back { flex: 0 0 auto; align-self: flex-start; display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 9px; padding: 8px 12px; font-size: 13px; font-weight: 700; cursor: pointer; }
.detail-back:active { background: var(--accent-press); border-color: var(--accent-press); }
#selthumb { width: 36px; height: 36px; flex: 0 0 auto; }
.detail-meta { flex: 1 1 auto; min-width: 0; }
.detail-meta .selname { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-meta .sellvl { font-size: 12px; color: var(--muted); }
#selstats { display: flex; gap: 6px; }
#selstats .statchip { flex: 1; background: #10201a; border-radius: 8px; padding: 5px 4px; text-align: center; }
#selstats .statchip .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
#selstats .statchip .v { font-size: 14px; font-weight: 600; margin-top: 1px; }
#selactions { display: flex; gap: 8px; flex: 0 0 auto; }
#selactions button { border: 0; border-radius: 9px; padding: 9px 12px; font-size: 14px; font-weight: 700; cursor: pointer; transition: transform 0.05s; }
#selactions button:active { transform: translateY(1px); }
#sellBtn { background: #7a3b33; color: #ffdcd4; }
#upgradeBtn { background: var(--coin); color: #3a2c05; }
#upgradeBtn:disabled { background: #33463c; color: var(--muted); }

/* Fix: size the dock to its content and give the detail area a fixed height
   tall enough for the selected-tower state (thumb + buttons + stats), so the
   stat row can't be clipped by the bottom bar. */
#dock { height: auto; }
#detail { height: 116px; flex: 0 0 auto; box-sizing: border-box; overflow: hidden; justify-content: center; }

/* --- Build sheet overlay + build button (v2 UX) --- */
#stage { overflow: hidden; }
#sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0; box-shadow: 0 -10px 30px rgba(0,0,0,0.4);
  overflow: hidden;
  transform: translateY(112%); transition: transform 0.22s ease;
}
#sheet.open { transform: translateY(0); }
/* sheet sizes to content (overlay), so no fixed dock height needed */
#dock { display: contents; }

#bar { justify-content: flex-start; }
#hint { flex: 0 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Placing footer: hint / quick-swap row / pill handle sit together after the tower icon */
.build-strip { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 10px; }
.tower-switch { flex: 0 0 auto; display: none; align-items: center; background: none; border: 0; padding: 6px 7px; cursor: pointer; }
.tower-switch .ts-line { display: block; width: 3px; height: 22px; border-radius: 999px; background: var(--muted); transition: background .15s, height .15s; }
.tower-switch.open .ts-line, .tower-switch:active .ts-line { height: 26px; }
.quick-towers { display: none; align-items: center; gap: 8px; }
.quick-towers.open { display: flex; flex: 1 1 auto; min-width: 0; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; padding: 2px 0; }
.quick-towers.open::-webkit-scrollbar { display: none; }
.quick-tower { flex: 0 0 auto; width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 10px; background: #12241d; display: flex; align-items: center; justify-content: center; padding: 0; cursor: pointer; transition: transform .05s; }
.quick-tower:active { transform: translateY(1px); }
.quick-tower.poor { opacity: 0.45; }
.quick-tower.armed { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.quick-tower canvas { width: 34px; height: 34px; }
.qcat { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; flex: 0 0 auto; }
.qcat-label { font-size: 8px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; white-space: nowrap; line-height: 1; padding-left: 1px; }
.qcat-row { display: flex; gap: 6px; }
.qcat + .qcat { border-left: 1px solid var(--line); padding-left: 8px; }
#buildBtn {
  position: relative; flex: 0 0 auto; width: 46px; height: 46px;
  border: 0; border-radius: 12px; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
#buildBtn:active { transform: translateY(1px); }
#buildBtn .icon-box, #buildBtn .icon-x { display: flex; }
#buildBtn .icon-x { display: none; }
#buildBtn.is-open { background: #33463c; }
#buildBtn.is-open .icon-box { display: none; }
#buildBtn.is-open .icon-x { display: flex; }
#buildBtn.glow { animation: buildglow 1.4s ease-in-out infinite; }
@keyframes buildglow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(216,103,42,0.0); background: var(--accent); }
  50% { box-shadow: 0 0 0 7px rgba(216,103,42,0.30); background: #e2762f; }
}

/* Place button (build mode primary action) */
#placeBtn { background: #2f7d4f; color: #eafff0; }
#placeBtn:disabled { background: #33463c; color: var(--muted); }

/* Build button — blue */
#buildBtn { background: #2f80d8; }
#buildBtn.is-open { background: #33463c; }
@keyframes buildglow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(47,128,216,0.0); background: #2f80d8; }
  50% { box-shadow: 0 0 0 7px rgba(47,128,216,0.32); background: #3a8ee6; }
}

/* End screen (win / lose) */
#app { position: relative; }
.end-screen {
  position: absolute; inset: 0; z-index: 20;
  background: rgba(8,14,11,0.92);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.end-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px 22px; width: 100%; max-width: 360px; text-align: center;
  box-shadow: 0 14px 44px rgba(0,0,0,0.55);
}
.end-title { font-size: 22px; font-weight: 600; margin-bottom: 16px; }
.end-stars { display: flex; justify-content: center; gap: 12px; margin-bottom: 20px; min-height: 44px; align-items: center; }
.end-stars svg { width: 46px; height: 46px; }
.end-defeat { color: var(--heart); font-size: 15px; }
.end-card { position: relative; z-index: 1; }
.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.confetti-piece { position: absolute; top: -16px; border-radius: 2px; opacity: 0.95; animation: confetti-fall linear infinite; }
@keyframes confetti-fall { 0% { transform: translateY(-20px) rotate(0deg); } 100% { transform: translateY(110vh) rotate(760deg); } }
.end-decor { width: 0; height: 0; margin: 0 auto; overflow: hidden; transition: width 0.2s, height 0.2s; }
.end-decor svg { width: 100%; height: 100%; display: block; color: #f2c14e; }
.celebrate .end-decor { width: 54px; height: 54px; margin: 2px auto 12px; }
.celebrate .end-decor.trophy { width: 66px; height: 66px; }
.celebrate .end-decor svg { filter: drop-shadow(0 0 10px rgba(242,193,78,0.55)); animation: decor-pop 0.5s ease-out; }
@keyframes decor-pop { 0% { transform: scale(0.3) rotate(-20deg); opacity: 0; } 70% { transform: scale(1.15) rotate(6deg); } 100% { transform: scale(1) rotate(0deg); opacity: 1; } }
.end-card.celebrate { border: 1px solid rgba(242,193,78,0.4); box-shadow: 0 0 34px rgba(242,193,78,0.16); }
.end-card.grand .end-title { color: #f2c14e; text-shadow: 0 0 18px rgba(242,193,78,0.55); font-size: 26px; }
.end-card.nightmare-win { border-color: rgba(242,193,78,0.7); box-shadow: 0 0 48px rgba(242,193,78,0.3), inset 0 0 24px rgba(184,59,59,0.18); }
.end-card.nightmare-win .end-decor svg { filter: drop-shadow(0 0 16px rgba(242,193,78,0.85)); }
.prestige-icon { width: 84px; height: 84px; }
.prestige-title { color: #f2c14e; text-shadow: 0 0 18px rgba(242,193,78,0.6); font-size: 26px; }
.end-congrats { color: var(--muted); font-size: 14px; font-weight: 600; margin-bottom: 12px; line-height: 1.4; }
.end-stats { display: flex; flex-direction: column; gap: 9px; margin-bottom: 22px; }
.end-row { display: flex; justify-content: space-between; font-size: 14px; }
.end-row .k { color: var(--muted); }
.end-row .v { font-weight: 600; }
.end-actions { display: flex; flex-direction: column; gap: 10px; }
.end-actions button { width: 100%; border: 0; border-radius: 10px; padding: 13px; font-size: 15px; font-weight: 700; cursor: pointer; transition: transform 0.05s; }
.end-actions button:active { transform: translateY(1px); }
#nextBtn { background: var(--accent); color: #fff; }
#retryBtn { background: #33463c; color: var(--text); }
#retryBtn.primary { background: var(--accent); color: #fff; }
#menuBtn { background: #33463c; color: var(--text); }

/* ===== Screens & main menu ===== */
#app { display: block; }
.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: flex; flex-direction: column; }

/* Title screen */
#menuScreen.active { align-items: center; justify-content: center; }
.menu-wrap { text-align: center; width: 100%; max-width: 340px; padding: 24px; }
.menu-logo { font-size: clamp(84px, 26vw, 128px); font-weight: 900; letter-spacing: 8px; line-height: 1; color: var(--accent); text-align: center; text-shadow: 0 6px 30px rgba(216,103,42,0.5), 0 3px 0 rgba(0,0,0,0.35); }
.menu-logo span { display: block; color: var(--accent); }
.menu-sub { color: var(--muted); margin: 10px 0 30px; font-size: 14px; }
.menu-btns { display: flex; flex-direction: column; gap: 12px; }
.menu-btn { border: 0; border-radius: 12px; padding: 15px; font-size: 16px; font-weight: 700; cursor: pointer; background: var(--panel); color: var(--text); transition: transform 0.05s; }
.menu-btn.primary { background: var(--accent); color: #fff; }
.menu-btn.resume { background: #2f80d8; color: #fff; }
.menu-btn:active { transform: translateY(1px); }

/* Sub-pages (slots, level, lore, settings) */
.page { flex: 1; width: 100%; max-width: 480px; margin: 0 auto; display: flex; flex-direction: column; min-height: 0; }
.page-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.page-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.diff-pill { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 999px; padding: 5px 11px; background: var(--panel); color: var(--text); font-size: 13px; font-weight: 700; cursor: pointer; transition: transform 0.05s, background 0.15s; white-space: nowrap; }
.diff-pill:active { transform: translateY(1px); }
.diff-pill:hover { background: #16241d; }
.diff-pill-dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }
.diff-pill-edit { color: var(--muted); margin-left: 1px; }
.back { border: 0; background: transparent; color: var(--muted); font-size: 15px; cursor: pointer; padding: 4px 6px; }

.slots-list, .level-list { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.slot-card, .level-card { text-align: left; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 16px; cursor: pointer; }
.slot-card { padding: 0; display: flex; align-items: stretch; overflow: hidden; }
.slot-main { flex: 1 1 auto; min-width: 0; text-align: left; background: transparent; border: 0; color: inherit; padding: 16px; cursor: pointer; font: inherit; }
.slot-main:active { background: rgba(255,255,255,0.03); }
.slot-delete { flex: 0 0 auto; background: transparent; border: 0; border-left: 1px solid var(--line); color: var(--muted); padding: 0 15px; cursor: pointer; display: flex; align-items: center; }
.slot-progress { flex: 0 0 auto; background: transparent; border: 0; border-left: 1px solid var(--line); color: var(--muted); padding: 0 15px; cursor: pointer; display: flex; align-items: center; position: relative; }
.slot-progress.has-notif { color: var(--accent); }
.slot-progress.has-notif::after { content: ''; position: absolute; top: 11px; right: 11px; width: 9px; height: 9px; border-radius: 50%; background: var(--heart); box-shadow: 0 0 7px rgba(226,80,74,0.95); animation: notifpulse 1.4s ease-in-out infinite; }
@keyframes notifpulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.25); opacity: 0.75; } }
.trophy-pop { text-align: center; }
.trophy-pop-icon { width: 76px; height: 76px; margin: 0 auto 14px; color: #2b4034; }
.trophy-pop-icon.earned { color: #f2c14e; filter: drop-shadow(0 0 14px rgba(242,193,78,0.55)); }
.trophy-pop-icon svg { width: 100%; height: 100%; display: block; }
.trophy-pop-msg { color: var(--muted); font-size: 14px; margin-bottom: 18px; }
.tp-diff { display: inline-block; color: #fff; font-weight: 800; font-size: 12px; border-radius: 999px; padding: 3px 10px; margin-right: 4px; }
.slot-progress:active { color: var(--accent); background: rgba(216,103,42,0.08); }
.progress-body { flex: 1; overflow-y: auto; min-height: 0; -webkit-overflow-scrolling: touch; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.prog-card { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); padding: 16px; box-shadow: inset 4px 0 0 var(--accent); }
.prog-name { font-size: 20px; font-weight: 800; }
.prog-badges { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.diff-badge { display: inline-block; vertical-align: middle; font-size: 12px; font-weight: 800; color: #fff; border-radius: 999px; padding: 4px 12px; letter-spacing: 0.02em; }
.slot-diff { margin-right: 4px; }
.level-card.locked { opacity: 0.4; cursor: default; }
.go-next-diff { width: 100%; margin-top: 14px; color: #fff; }
#levelAdvance { margin: 0 16px; } /* match Begin Mission's side insets so the Go button lines up */
.level-locked { color: var(--muted); font-size: 13px; font-weight: 600; }
.prog-date { color: var(--muted); font-size: 12px; }
.prog-next { width: 100%; }
.prog-trophies { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.trophy { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); padding: 12px 6px; text-align: center; }
.trophy-icon { width: 34px; height: 34px; margin: 0 auto; }
.trophy-icon svg { width: 100%; height: 100%; display: block; }
.trophy.locked .trophy-icon { color: #2b4034; } /* silhouette placeholder */
.trophy.earned { box-shadow: inset 0 0 0 1px rgba(255,255,255,0.08); }
.trophy-name { font-size: 11px; font-weight: 800; color: var(--muted); margin-top: 6px; line-height: 1.25; }
.trophy-cond { display: block; font-size: 8px; font-weight: 600; opacity: 0.75; margin-bottom: 2px; letter-spacing: 0.02em; }
.trophy.earned .trophy-name { color: var(--text); }
.prestige-btn { width: 100%; margin-top: 4px; color: #1a1206; font-weight: 800; background: linear-gradient(180deg, #f2c14e, #d89a2a); }
.prestige-divider { display: flex; align-items: center; gap: 12px; margin: 6px 2px; color: #f2c14e; font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em; }
.prestige-divider::before, .prestige-divider::after { content: ''; flex: 1; height: 1px; background: linear-gradient(90deg, transparent, rgba(242,193,78,0.5), transparent); }
.slot-card.prestige { border-color: rgba(242,193,78,0.45); box-shadow: inset 4px 0 0 #f2c14e; background: linear-gradient(90deg, rgba(242,193,78,0.10), var(--panel) 42%); }
.prestige-badge { color: #f2c14e; }
.showcase-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.showcase-title { font-size: 22px; font-weight: 800; text-align: center; }
.showcase-empty { flex: 1 1 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 14px; padding: 30px 24px; }
.showcase-empty-icon { width: 72px; height: 72px; color: var(--muted); opacity: 0.3; }
.showcase-empty-icon svg { width: 100%; height: 100%; display: block; }
.showcase-empty-head { font-size: 20px; font-weight: 800; color: var(--text); }
.showcase-empty-text { color: var(--muted); font-size: 14px; line-height: 1.6; max-width: 300px; }
.hero-list { display: flex; flex-direction: column; gap: 10px; }
.hero { display: flex; align-items: center; gap: 14px; border: 1px solid rgba(242,193,78,0.4); border-radius: 12px; background: linear-gradient(90deg, rgba(242,193,78,0.10), var(--panel) 50%); padding: 14px; cursor: pointer; transition: transform .05s, border-color .12s; }
.hero:active { transform: translateY(1px); }
.hero:hover { border-color: rgba(242,193,78,0.7); }
.hero-info { flex: 1 1 auto; min-width: 0; }
.hero-chev { flex: 0 0 auto; color: var(--muted); display: flex; }
.hero-trophy { width: 34px; height: 34px; color: #f2c14e; flex: 0 0 auto; }
.hero-trophy svg { width: 100%; height: 100%; display: block; }
.hero-name { font-size: 17px; font-weight: 800; }
.hero-meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.prog-summary { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.pstat { flex: 1 1 72px; background: rgba(255,255,255,0.04); border: 1px solid var(--line); border-radius: 10px; padding: 10px; text-align: center; }
.ps-v { font-size: 18px; font-weight: 800; color: var(--text); }
.ps-k { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin-top: 3px; }
.prog-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 4px 2px -4px; }
.prog-missions { display: flex; flex-direction: column; gap: 8px; }
.prog-mission { display: flex; align-items: center; justify-content: space-between; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); padding: 12px 14px; }
.pm-name { font-weight: 700; }
.pm-status { color: var(--muted); font-size: 13px; }
.slot-delete:active { color: var(--heart); background: rgba(226,80,74,0.08); }
.slot-title { font-size: 16px; font-weight: 700; }
.slot-sub { color: var(--muted); font-size: 13px; margin-top: 4px; }
/* Saved slots stand out; empty slots read as "add new" placeholders. */
.slot-card.filled { border-color: rgba(216,103,42,0.5); box-shadow: inset 4px 0 0 var(--accent); background: linear-gradient(90deg, rgba(216,103,42,0.08), var(--panel) 42%); }
.slot-card.filled .slot-title { color: var(--text); }
.slot-stars { color: var(--coin); font-weight: 700; }
.slot-card.empty { border: 1px dashed var(--line); background: transparent; }
.slot-card.empty .slot-main { display: flex; align-items: center; gap: 14px; }
.slot-plus { flex: 0 0 auto; width: 26px; text-align: center; font-size: 26px; font-weight: 300; line-height: 1; color: var(--muted); }
.slot-card.empty .slot-title { color: var(--muted); font-weight: 600; }
.slot-card.empty .slot-sub { color: var(--muted); }
.level-card { display: flex; flex-direction: row; align-items: center; gap: 14px; }
/* Selected mission: same accent treatment as a filled save slot. */
.level-card.selected { border-color: rgba(216,103,42,0.5); box-shadow: inset 4px 0 0 var(--accent); background: linear-gradient(90deg, rgba(216,103,42,0.08), var(--panel) 42%); }
.level-thumb { width: 60px; height: 60px; flex: 0 0 auto; border-radius: 10px; object-fit: cover; background: rgba(0,0,0,0.18); border: 1px solid var(--line); }
.level-info { flex: 1; display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.level-name { font-size: 17px; font-weight: 700; }
.level-diff { align-self: flex-start; font-size: 12px; font-weight: 700; color: #9ed36f; background: rgba(99,153,34,0.18); padding: 2px 9px; border-radius: 6px; }
.level-card.current { border-color: rgba(242,193,78,0.45); box-shadow: inset 4px 0 0 #f2c14e; background: linear-gradient(90deg, rgba(242,193,78,0.08), var(--panel) 42%); }
.level-status { flex: 0 0 auto; font-size: 12px; font-weight: 800; padding: 4px 11px; border-radius: 999px; white-space: nowrap; }
.level-status.completed { color: #9ed36f; background: rgba(99,153,34,0.18); }
.level-status.inprogress { color: #f2c14e; background: rgba(242,193,78,0.16); }
.level-status.locked-status { color: var(--muted); background: rgba(255,255,255,0.05); }
.mstar { color: #33463c; font-size: 18px; }
.mstar.on { color: #f2c14e; }
.level-detail { margin: 0 16px 18px; border-top: 1px solid var(--line); padding-top: 16px; display: flex; flex-direction: column; align-items: center; gap: 8px; flex: 0 0 auto; }
.ld-name { font-size: 20px; font-weight: 700; }
.ld-meta { color: var(--muted); font-size: 13px; }
.ld-stars { font-size: 26px; margin: 2px 0 6px; }
.level-detail .menu-btn { width: 100%; }

/* Lore */
.lore-tabs { display: flex; gap: 8px; padding: 12px 16px; flex: 0 0 auto; }
.lore-tab { flex: 1; border: 0; border-radius: 10px; padding: 10px; font-size: 14px; font-weight: 700; cursor: pointer; background: var(--panel); color: var(--muted); }
.lore-tab.active { background: var(--accent); color: #fff; }
.lore-body { flex: 1; overflow-y: auto; padding: 0 16px 20px; }
.lore-entry { position: relative; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); padding: 16px; margin-bottom: 12px; }
.lore-credit { position: absolute; top: 10px; right: 12px; font-size: 10px; font-weight: 700; letter-spacing: 0.09em; color: var(--muted); opacity: 0.65; }
.lore-name { font-size: 16px; font-weight: 700; margin-bottom: 6px; }
.lore-name .tag { font-size: 11px; color: #85b7eb; background: rgba(55,138,221,0.18); padding: 2px 7px; border-radius: 6px; font-weight: 700; margin-left: 6px; }
.lore-text { color: var(--muted); font-size: 14px; line-height: 1.6; margin: 0; }
.lore-text.clamped { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.lore-desc { margin-bottom: 8px; }
.lore-expand { margin-top: 4px; background: none; border: 0; color: var(--accent); font-size: 12px; font-weight: 700; cursor: pointer; padding: 2px 0; }
.stat-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.stat-chip { background: rgba(255,255,255,0.05); border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; text-align: center; min-width: 40px; }
.sc-val { font-size: 14px; font-weight: 800; color: var(--text); line-height: 1.1; }
.sc-lbl { font-size: 9px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 1px; }
.evo { display: flex; align-items: center; gap: 12px; padding: 7px 0; border-top: 1px solid var(--line); }
.evo-lvl { font-size: 12px; font-weight: 700; color: var(--coin); flex: 0 0 auto; width: 36px; }
.evo-stats { font-size: 13px; color: var(--text); }

/* Settings */
.settings-body { padding: 8px 16px; }
.setting-row { display: flex; justify-content: space-between; padding: 15px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.setting-row .muted { color: var(--muted); }

/* ===== Roomier build/selection sheet ===== */
#sheet { padding-top: 10px; }
#sheet::before {
  content: ''; display: block; width: 40px; height: 4px; border-radius: 2px;
  background: var(--border-strong); margin: 0 auto 6px;
}
#tray { padding: 10px 16px; gap: 10px; }
#detail { height: 150px; padding: 16px 18px 18px; gap: 14px; }
.detail-head { gap: 14px; }
#selthumb { width: 42px; height: 42px; }
.detail-meta .selname { font-size: 16px; }
.detail-meta .sellvl { font-size: 12.5px; margin-top: 2px; }
#selactions { gap: 10px; }
#selactions button { padding: 12px 16px; border-radius: 10px; }
#selstats { gap: 9px; }
#selstats .statchip { padding: 11px 6px; border-radius: 10px; }
#selstats .statchip .k { font-size: 10px; }
#selstats .statchip .v { font-size: 16px; margin-top: 4px; }

/* ===== v1.0.0 additions: title, version footer, lore pictures ===== */
.menu-logo .owner { display: block; font-size: 15px; letter-spacing: 3px; color: var(--muted); font-weight: 700; margin-bottom: 2px; }

/* #menuScreen is already position:absolute (from .screen), which anchors the footer. */
.menu-foot { position: absolute; bottom: 0; left: 0; right: 0; text-align: center; padding: 16px 16px 30px; }
.menu-foot .version { color: var(--muted); font-size: 12px; }
.update-link { border: 0; background: transparent; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; margin-top: 2px; padding: 4px 8px; }
.update-link:disabled { color: var(--muted); }

/* lore entries now carry a picture */
.evo { align-items: center; gap: 14px; }
.evo-img { width: 58px; height: 58px; flex: 0 0 auto; }
.evo-info { flex: 1; min-width: 0; }
.evo-lvl { width: auto; flex: none; font-size: 13px; font-weight: 700; color: var(--coin); margin-bottom: 2px; }
.enemy-row { display: flex; gap: 14px; align-items: center; }
.enemy-viewbox { display: flex; flex-direction: column; gap: 8px; align-items: center; flex: 0 0 auto; }
.enemy-views { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; flex: 0 0 auto; }
.ev { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.enemy-view { width: 54px; height: 54px; background: rgba(0,0,0,0.2); border: 1px solid var(--line); border-radius: 10px; }
.ev span { font-size: 9px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.view-toggle { display: flex; align-items: center; gap: 8px; }
.vt-arrow { width: 26px; height: 26px; border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 8px; font-size: 15px; font-weight: 700; line-height: 1; padding: 0; cursor: pointer; }
.vt-arrow:active { background: var(--accent-press); border-color: var(--accent-press); }
.vt-label { font-size: 10px; font-weight: 700; color: #85b7eb; text-transform: uppercase; letter-spacing: 0.05em; min-width: 58px; text-align: center; }
.enemy-info { flex: 1; min-width: 0; }
.enemy-info .lore-desc { margin-bottom: 6px; }

/* ===== iOS safe areas (installed PWA / notch + home indicator) ===== */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
/* The absolute screens fill #app's padding box, so padding on #app can't inset
   them. Apply the insets to the real top/bottom bars instead. */
#app { padding: 0; }
#hud {
  padding: calc(12px + var(--safe-top)) calc(18px + var(--safe-right)) 12px calc(18px + var(--safe-left));
}
#bar {
  padding: 12px calc(18px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(18px + var(--safe-left));
}
.menu-foot { padding: 16px 16px calc(30px + var(--safe-bottom)); }
.page-head { padding-top: calc(14px + var(--safe-top)); }
.slots-list, .level-list, .lore-body { padding-bottom: calc(20px + var(--safe-bottom)); }

/* ===== Bottom layout fix (v1.0.5) ===== */
/* Pin to the REAL visible viewport: JS sets --app-h = window.innerHeight (ground
   truth in standalone); 100dvh is the CSS fallback. height:100%/inset:0 both
   mis-resolve on iOS home-screen apps and left a dark strip at the bottom. */
#app { position: fixed; top: 0; left: 0; width: 100%; height: var(--app-h, 100dvh); }

/* Build icon alone opens the menu; Start wave fills the rest of the bar. */
#hint { display: none; }
#startBtn { flex: 1 1 auto; }
#bar {
  gap: 10px;
  padding: 10px calc(14px + var(--safe-right)) max(10px, min(var(--safe-bottom), 20px)) calc(14px + var(--safe-left));
}
.menu-foot { padding: 16px 16px max(16px, min(var(--safe-bottom), 24px)); }
.slots-list, .level-list, .lore-body { padding-bottom: max(16px, min(var(--safe-bottom), 24px)); }

/* ===== v1.0.7: bar layout, pause menu, sheet tweaks ===== */
/* Full-screen app: inset:0 with viewport-fit=cover covers the whole screen
   (including under the home indicator), so the bar's own bg fills the bottom. */
#app { inset: 0; height: auto; }

/* Build icon stays left; Start wave sits on the right (not stretched). */
#bar { justify-content: space-between; gap: 12px; }
#startBtn { flex: 0 0 auto; padding: 13px 26px; }

/* HUD menu/pause button, pinned to the right. */
#pauseBtn { margin-left: auto; }
.hud-btn {
  flex: 0 0 auto; width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: rgba(255,255,255,0.08); color: var(--text);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.hud-btn:active { transform: translateY(1px); }

/* Detail: Place / Sell / Upgrade sit under the stats, centered. */
#selactions { display: flex; justify-content: center; gap: 10px; margin-top: 4px; }
#selactions button { flex: 0 0 auto; min-width: 104px; text-align: center; }

/* Build sheet: tighter, consistent vertical padding. */
#sheet { padding-top: 8px; }
#detail { height: auto; padding: 12px 16px 14px; gap: 10px; }
#tray { padding: 8px 16px; }

/* Pause overlay. */
.name-input { width: 100%; box-sizing: border-box; padding: 13px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 16px; margin-bottom: 16px; text-align: center; }
.name-input:focus { outline: none; border-color: var(--accent); }
.modal-btns { display: flex; gap: 10px; }
.modal-btns .menu-btn { flex: 1; }
.pause-btns { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.pause-confirm-text { color: var(--muted); font-size: 14px; margin-bottom: 4px; line-height: 1.5; }
.menu-btn.danger { background: #7a2e2e; color: #fff; }
.difficulty-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; text-align: left; }
.difficulty-opt { position: relative; border: 1px solid var(--line); border-left: 4px solid var(--line); border-radius: 12px; padding: 13px 16px; background: var(--panel); color: var(--text); cursor: pointer; text-align: left; transition: transform 0.05s, box-shadow 0.15s, background 0.15s; }
.difficulty-opt:not(.locked):active { transform: translateY(1px); }
.difficulty-opt:not(.locked):hover { background: #16241d; }
.difficulty-opt.locked { opacity: 0.55; cursor: default; }
.diff-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.diff-name { font-size: 17px; font-weight: 800; letter-spacing: 0.01em; }
.diff-lives { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 700; white-space: nowrap; }
.diff-lives svg { width: 14px; height: 14px; }
.diff-lives b { font-size: 15px; font-weight: 800; }
.diff-lock-icon { display: inline-flex; color: var(--muted); }
.diff-lock-icon svg { width: 16px; height: 16px; }
.diff-tag { font-size: 12px; font-weight: 600; color: var(--text); margin-top: 5px; }
.diff-unlock { font-size: 12px; font-weight: 700; margin-top: 5px; }
.diff-desc { font-size: 12px; line-height: 1.5; color: var(--muted); margin-top: 6px; }

/* ===== v1.0.8: professional detail actions ===== */
#detail { gap: 14px; }
/* Left-align Place / Sell / Upgrade and separate them from the stats with a divider. */
#selactions {
  justify-content: flex-start;
  margin-top: 0;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
#selactions button {
  min-width: 108px;
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 700;
}

/* ===== v1.0.9: detail spacing polish ===== */
/* Details toggle: compact secondary button, leftmost in the actions row. */
#selactions #detailsBtn { min-width: 0; padding: 12px 16px; background: var(--panel); border: 1px solid var(--line); color: var(--text); }
#selactions #detailsBtn.on { border-color: var(--accent); color: var(--accent); }
/* Breathing room between the name/level header and the stats row. */
.detail-head { margin-bottom: 8px; }
/* Trim the padding above the selected tower card in the sheet. */
#sheet { padding-top: 6px; }
#sheet::before { margin: 0 auto 4px; }
#tray { padding: 4px 16px 8px; }

/* ===== v1.1.0: Place button (ground blue + pin icon) ===== */
#placeBtn {
  background: #12bcff; color: #06202a;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
#placeBtn:disabled { background: #33463c; color: var(--muted); }

/* ===== v1.1.2: placing indicator on the build button ===== */
#buildIcon { display: none; width: 40px; height: 40px; }
#buildBtn.placing { background: #12241d; }
#buildBtn.placing.poor #buildIcon { opacity: 0.4; }
#buildBtn.placing .icon-box, #buildBtn.placing .icon-x { display: none; }
#buildBtn.placing #buildIcon { display: block; }

/* Keep the build button (+ Place hint) on the left, Start wave on the right. */
#bar { justify-content: flex-start; }
#startBtn { margin-left: auto; }
#hint { color: var(--muted); font-size: 13px; align-self: center; flex: 0 1 auto; }
