:root {
  --bg: #12100c;
  --bg-glow: #2a2218;
  --panel: rgba(36, 30, 22, 0.94);
  --panel-solid: #241e16;
  --panel-border: #5a4a32;
  --panel-edge: rgba(212, 175, 85, 0.22);
  --text: #f3e8d4;
  --muted: #a89878;
  --accent: #d4af55;
  --accent-dim: rgba(212, 175, 85, 0.28);
  --good: #8bc34a;
  --warn: #ffb74d;
  --danger: #e57373;
  --gold: #ffd54f;
  --arrow: #9ccc65;
  --cannon: #e8985a;
  --frost: #81d4fa;
  --stone: #6d5c45;
  --stone-light: #8a7660;
  --radius: 12px;
  --font: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --hud-pad-y: 8px;
  --hud-pad-x: 12px;
  --hud-font: 14px;
  --panel-pad: 8px 10px;
  --tb-pad: 6px 4px;
  --tb-min-h: 52px;
  --action-min-h: 40px;
  --app-pad: 6px;
  --app-gap: 6px;
  --shadow-panel: 0 4px 18px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 236, 179, 0.06);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  background:
    radial-gradient(ellipse 80% 45% at 50% -8%, #3a2e1c 0%, transparent 55%),
    radial-gradient(ellipse 55% 40% at 15% 100%, #1a2818 0%, transparent 50%),
    radial-gradient(ellipse 40% 30% at 90% 80%, #2a1818 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  overscroll-behavior: none;
  overflow: hidden;
}
#app {
  width: 100%;
  max-width: 480px;
  container-type: inline-size;
  container-name: game;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: var(--app-pad);
  padding-bottom: max(var(--app-pad), env(safe-area-inset-bottom, 0px));
  gap: var(--app-gap);
  overflow: hidden;
}
#hud {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  max-width: 100%;
  background: linear-gradient(180deg, rgba(48, 40, 28, 0.96) 0%, rgba(28, 24, 18, 0.94) 100%);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: var(--hud-pad-y) var(--hud-pad-x);
  font-weight: 600;
  font-size: var(--hud-font);
  min-height: 0;
  box-shadow: var(--shadow-panel), 0 0 0 1px var(--panel-edge) inset;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.hud-stats {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px 6px;
  align-items: center;
  min-width: 0;
  width: 100%;
}
.hud-item {
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(212, 175, 85, 0.12);
  flex: 0 0 auto;
}
.hud-item[title="生命"] { color: #ef9a9a; }
.hud-item[title="金币"] { color: var(--gold); }
.hud-item[title="波次"] { color: #aed581; }
.hud-icon { font-size: 11px; opacity: 0.95; line-height: 1; }
.hud-status {
  margin-left: auto;
  color: var(--muted);
  font-weight: 500;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: none;
  min-width: 0;
  flex: 1 1 auto;
  padding: 2px 6px;
  background: transparent;
  border: none;
}
.hud-tools {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 5px;
  width: 100%;
  min-width: 0;
}
.hud-audio {
  appearance: none;
  -webkit-appearance: none;
  flex: 0 0 auto;
  border: 1px solid rgba(212, 175, 85, 0.4);
  background: rgba(0, 0, 0, 0.28);
  color: var(--accent);
  font: inherit;
  font-weight: 700;
  font-size: 11px;
  line-height: 1;
  padding: 4px 7px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 26px;
}
.hud-audio[aria-pressed="true"] {
  color: var(--muted);
  border-color: rgba(168, 152, 120, 0.35);
}
.hud-audio:hover { filter: brightness(1.08); }
.hud-audio:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.hud-chip {
  appearance: none;
  -webkit-appearance: none;
  flex: 0 0 auto;
  border: 1px solid rgba(212, 175, 85, 0.4);
  background: rgba(0, 0, 0, 0.28);
  color: var(--accent);
  font: inherit;
  font-weight: 700;
  font-size: 11px;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 26px;
  white-space: nowrap;
}
.hud-chip.ghost {
  color: var(--muted);
  border-color: rgba(168, 152, 120, 0.4);
}
.hud-chip[aria-pressed="true"] {
  color: #1a140c;
  background: linear-gradient(135deg, #c9a227, #e8d48b);
  border-color: #e8d48b;
}
.hud-chip:hover { filter: brightness(1.08); }
.speed-seg {
  display: inline-flex;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 168px;
  align-items: stretch;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(212, 175, 85, 0.28);
  background: rgba(0, 0, 0, 0.35);
}
.speed-btn {
  flex: 1 1 0;
  min-width: 0;
  min-height: 26px;
  padding: 2px 0;
  border: none;
  border-right: 1px solid rgba(212, 175, 85, 0.18);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.12s, color 0.12s;
}
.speed-btn:last-child { border-right: none; }
.speed-btn:hover { color: var(--text); background: rgba(212, 175, 85, 0.12); }
.speed-btn.active {
  color: #1a140c;
  background: linear-gradient(135deg, #c9a227, #e8d48b);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15);
}
.hud-speed, .speed-label, .speed-btns { display: none !important; }

#stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  background:
    radial-gradient(ellipse at 50% 0%, #2a2214 0%, #12100c 70%);
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-panel), 0 0 0 1px var(--panel-edge) inset;
  overflow: hidden;
  line-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}
#game {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  cursor: crosshair;
  touch-action: none;
}
#toast {
  position: absolute;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  background: rgba(18, 14, 10, 0.9);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid rgba(212, 175, 85, 0.4);
  font-size: 12px;
  pointer-events: none;
  z-index: 5;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
}
#toast.hidden { display: none; }
#panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
}
.panel-block {
  background: linear-gradient(180deg, rgba(42, 34, 24, 0.96) 0%, rgba(28, 24, 18, 0.94) 100%);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: var(--panel-pad);
  box-shadow: var(--shadow-panel);
}
.panel-title {
  font-size: 11px;
  color: var(--accent);
  margin-bottom: 6px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 700;
}
.tower-btns {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.tower-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--tb-pad);
  min-height: var(--tb-min-h);
  border-radius: 10px;
  border: 1.5px solid #4a3c28;
  background: linear-gradient(180deg, #3a3022 0%, #221c14 100%);
  color: var(--text);
  cursor: pointer;
  font: inherit;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s, transform 0.12s;
  position: relative;
  overflow: hidden;
}
.tower-btn::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.14;
  pointer-events: none;
  background: radial-gradient(circle at 50% 0%, currentColor, transparent 65%);
}
.tower-btn[data-type="arrow"] { color: var(--arrow); }
.tower-btn[data-type="cannon"] { color: var(--cannon); }
.tower-btn[data-type="frost"] { color: var(--frost); }
.tower-btn .tb-name,
.tower-btn .tb-cost,
.tower-btn .tb-desc { position: relative; z-index: 1; }
.tower-btn .tb-name { color: var(--text); }
.tower-btn .tb-desc { color: var(--muted); }
.tower-btn:hover {
  border-color: currentColor;
  box-shadow: 0 0 12px color-mix(in srgb, currentColor 35%, transparent);
}
.tower-btn.active {
  border-color: currentColor;
  background: linear-gradient(180deg, #4a3c28 0%, #2a2218 100%);
  box-shadow: 0 0 0 1px currentColor, 0 0 14px color-mix(in srgb, currentColor 40%, transparent);
}
.tower-btn.disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }
.tower-btn.disabled:hover { border-color: #4a3c28; box-shadow: none; }
.tb-name { font-weight: 700; font-size: 13px; }
.tb-cost { color: var(--gold) !important; font-size: 12px; font-weight: 700; }
.tb-cost::before { content: "🪙 "; font-size: 10px; }
.tb-desc { font-size: 10px; }
.hint {
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  opacity: 0.9;
}
.sel-stats {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
  margin-bottom: 8px;
}
.sel-actions, .wave-block { display: flex; gap: 6px; flex-wrap: wrap; }
.action-btn {
  flex: 1;
  min-width: 72px;
  min-height: var(--action-min-h);
  padding: 8px 10px;
  border-radius: 9px;
  border: none;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  background: #3a3228;
  color: var(--text);
  transition: filter 0.12s, background 0.12s;
}
.action-btn.primary {
  background: linear-gradient(135deg, #6d4c1e, #c9a227);
  box-shadow: 0 2px 8px rgba(201, 162, 39, 0.35);
}
.action-btn.primary:hover { filter: brightness(1.08); }
.action-btn.danger {
  background: linear-gradient(135deg, #8e2a2a, #c62828);
  box-shadow: 0 2px 8px rgba(198, 40, 40, 0.3);
}
.action-btn.danger:hover { filter: brightness(1.08); }
.action-btn.ghost {
  background: transparent;
  border: 1px solid var(--panel-border);
}
.action-btn:disabled { opacity: 0.4; cursor: not-allowed; filter: none; box-shadow: none; }
.wave-btn {
  background: linear-gradient(135deg, #3e4a28, #689f38);
  width: 100%;
  box-shadow: 0 2px 12px rgba(104, 159, 56, 0.3);
  letter-spacing: 0.02em;
}
.wave-btn:hover { filter: brightness(1.08); }
.wave-btn.waiting {
  background: linear-gradient(135deg, #5d4037, #8d6e63);
  box-shadow: 0 2px 12px rgba(141, 110, 99, 0.35);
}
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 6, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
#overlay.hidden { display: none; }
.overlay-card {
  background: linear-gradient(180deg, #2e261c 0%, #1a1610 100%);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 24px 20px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55), 0 0 0 1px var(--panel-edge) inset;
}
.overlay-card h2 { font-size: 26px; margin-bottom: 8px; letter-spacing: 0.04em; }
.overlay-card h2.win { color: var(--gold); text-shadow: 0 0 20px rgba(212, 175, 85, 0.45); }
.overlay-card h2.lose { color: var(--danger); text-shadow: 0 0 20px rgba(229, 115, 115, 0.35); }
#end-msg { color: var(--muted); margin-bottom: 14px; font-size: 14px; }
#end-stats {
  list-style: none;
  text-align: left;
  margin: 0 auto 16px;
  max-width: 220px;
  font-size: 14px;
  line-height: 1.8;
  color: var(--text);
}
#end-stats li span { color: var(--muted); float: right; }
.overlay-card .action-btn { width: 100%; padding: 12px; font-size: 16px; }
.hidden { display: none !important; }

#btn-restart {
  min-height: 48px;
  font-size: 16px !important;
  background: linear-gradient(135deg, #6d4c1e, #c9a227) !important;
}

@media (min-width: 520px) and (min-height: 820px) {
  :root {
    --app-pad: 12px;
    --app-gap: 8px;
    --hud-pad-y: 10px;
    --hud-pad-x: 14px;
    --hud-font: 15px;
    --panel-pad: 10px 12px;
    --tb-pad: 8px 6px;
    --tb-min-h: 58px;
    --action-min-h: 44px;
  }
}

@media (max-height: 820px) {
  :root {
    --app-pad: 4px;
    --app-gap: 4px;
    --hud-pad-y: 6px;
    --hud-pad-x: 10px;
    --hud-font: 13px;
    --panel-pad: 6px 8px;
    --tb-pad: 4px 3px;
    --tb-min-h: 44px;
    --action-min-h: 36px;
  }
  .panel-title { margin-bottom: 4px; font-size: 10px; }
  .hint { margin-top: 4px; font-size: 10px; }
  .tb-name { font-size: 12px; }
  .tb-cost { font-size: 11px; }
  .tb-desc { font-size: 9px; }
  .action-btn { font-size: 12px; padding: 6px 8px; }
  .hud-status { font-size: 11px; }
  .hud-audio, .hud-chip { font-size: 11px; padding: 3px 7px; min-height: 22px; }
  .speed-btn { min-height: 24px; }
}

@media (max-height: 640px) {
  :root {
    --app-pad: 2px;
    --app-gap: 3px;
    --hud-pad-y: 4px;
    --tb-min-h: 36px;
    --action-min-h: 32px;
  }
  .hint { display: none; }
  .tb-desc { display: none; }
  .panel-title { display: none; }
  .speed-btn { min-height: 22px; font-size: 10px; }
  .hud-chip, .hud-audio { font-size: 10px; padding: 2px 6px; min-height: 22px; }
  .speed-seg { max-width: 140px; }
}

@media (max-width: 460px) {
  #hud { gap: 3px; padding-left: 6px; padding-right: 6px; }
  .hud-item { flex-shrink: 0; padding: 2px 5px; gap: 3px; }
  .hud-status { font-size: 10px; padding: 2px 3px; }
  .speed-seg { max-width: 128px; }
  .speed-btn { min-height: 24px; font-size: 10px; }
  .hud-chip, .hud-audio { font-size: 10px; padding: 2px 6px; min-height: 22px; }
}

@media (max-width: 360px) {
  #btn-hud-menu { display: none; }
  .speed-seg { max-width: 120px; }
}



/* ---- v13 暂停 ---- */
#pause-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 6, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 55;
  padding: 16px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
#pause-overlay.hidden { display: none; }
.pause-card h2 {
  color: var(--gold);
  text-shadow: 0 0 18px rgba(212, 175, 85, 0.35);
}
.pause-msg { color: var(--muted); font-size: 13px; margin-bottom: 16px; }

/* ---- v12 选关 / 难度 ---- */
#start-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 6, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 16px;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}
#start-overlay.hidden { display: none; }
.start-card { max-width: 360px; }
.start-card h2 {
  font-size: 28px;
  color: var(--gold);
  text-shadow: 0 0 18px rgba(212, 175, 85, 0.4);
  margin-bottom: 4px;
}
.start-sub { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.start-section-title {
  text-align: left;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 10px 0 6px;
  text-transform: uppercase;
}
.start-level-cap { letter-spacing: 0; text-transform: none; font-size: 11px; }
.diff-btns {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.diff-btn {
  appearance: none;
  border: 1px solid var(--panel-border);
  background: rgba(40, 34, 24, 0.9);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 4px;
  font-size: 13px;
  cursor: pointer;
  min-height: 40px;
}
.diff-btn.active {
  border-color: var(--gold);
  color: var(--gold);
  box-shadow: 0 0 0 1px rgba(212, 175, 85, 0.35) inset, 0 0 12px rgba(212, 175, 85, 0.2);
  background: linear-gradient(180deg, #3a3020, #241e14);
}
.level-row { display: flex; gap: 8px; }
#start-level {
  flex: 1;
  background: #1a1610;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  color: var(--text);
  font-size: 18px;
  padding: 10px 12px;
  text-align: center;
}
.start-hint {
  margin: 8px 0 14px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}
.end-actions { display: flex; flex-direction: column; gap: 8px; }
.end-actions .action-btn { width: 100%; }
#btn-menu { min-height: 42px; }

@media (max-width: 460px) {
  .diff-btns { grid-template-columns: repeat(2, 1fr); }
  #hud-diff-wrap { display: none; }
}

/* v14: stage frame tint only — HUD, pause, and speed controls stay as shipped */
body[data-decor="fog"] #stage-wrap { border-color: #6a6880; }
body[data-decor="snow"] #stage-wrap { border-color: #7f93a8; }
body[data-decor="lava"] #stage-wrap { border-color: #8a4a32; }
body[data-decor="toxic"] #stage-wrap { border-color: #5e7a48; }
