:root {
  --ink: #43351f;
  --ink-soft: #7d6237;
  --cream: #fff5cf;
  --cream-deep: #ead18a;
  --wood: #806033;
  --leaf: #639b3e;
  --leaf-dark: #3e702d;
  --gold: #f4bd37;
  --coral: #e76e48;
  --coral-dark: #a9432d;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  display: grid;
  place-items: center;
  padding: 12px;
  overflow: auto;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 18%, #f8e7b5 0 2px, transparent 3px),
    radial-gradient(circle at 75% 70%, #d8bd7d 0 2px, transparent 3px),
    #e8d3a2;
  background-size: 27px 27px, 31px 31px, auto;
  font-family: "Trebuchet MS", "Microsoft YaHei", sans-serif;
}

button { font: inherit; letter-spacing: 0; }
.game-shell {
  position: relative;
  width: min(960px, calc((100vh - 24px) * 1.5), calc(100vw - 24px));
  aspect-ratio: 3 / 2;
  display: grid;
  grid-template-rows: 70fr 500fr 70fr;
  overflow: hidden;
  background: var(--cream);
  border: 4px solid #594425;
  border-radius: 14px;
  box-shadow: 0 18px 50px #4f392b38, inset 0 0 0 2px #fff7d7;
}

.hud {
  min-height: 0;
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 7px 14px;
  background: linear-gradient(#fff4cb, #f6dda0);
  border-bottom: 3px solid #b88943;
  box-shadow: inset 0 -2px #fff5ca;
  z-index: 4;
}
.level-badge { display: flex; align-items: center; gap: 8px; margin-right: auto; min-width: 148px; }
.level-badge strong { display: block; font-size: 17px; line-height: 1; }
.level-badge small { display: block; margin-top: 4px; color: var(--ink-soft); font-size: 11px; }
.leaf-mark {
  position: relative;
  width: 38px;
  height: 38px;
  border: 3px solid #3f6f2f;
  border-radius: 50% 12px 50% 12px;
  background: linear-gradient(135deg, #a9d85d, #5d9e3e);
  transform: rotate(-11deg);
  box-shadow: inset 5px 5px #d7ed8e88, 0 2px #ad783b;
}
.leaf-mark::after { content: ""; position: absolute; width: 24px; height: 3px; left: 5px; top: 17px; background: #3f6f2f; transform: rotate(-42deg); border-radius: 2px; }
.resource {
  height: 40px;
  min-width: 82px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 11px;
  color: #513b1f;
  background: #fff8dc;
  border: 2px solid #c59b54;
  border-radius: 10px;
  box-shadow: inset 0 -3px #ead293, 0 2px #8b67333a;
}
.resource b { font-size: 16px; }
.coin-glyph { width: 20px; height: 20px; border: 2px solid #aa6a1c; border-radius: 50%; background: #ffd25a; box-shadow: inset 0 0 0 4px #efa62a; }
.wave-glyph { width: 21px; height: 17px; border: 3px solid #58823e; border-left-color: transparent; border-radius: 50%; transform: rotate(-15deg); }
.wave-glyph::after { content: ""; display: block; width: 7px; height: 7px; margin: 8px 0 0 -2px; border-left: 3px solid #58823e; border-bottom: 3px solid #58823e; transform: rotate(20deg); }
.heart-glyph { width: 21px; height: 19px; background: #e95d4f; clip-path: polygon(50% 98%, 4% 48%, 5% 25%, 20% 8%, 39% 10%, 50% 24%, 61% 10%, 80% 8%, 95% 25%, 96% 48%); filter: drop-shadow(0 2px #a83d36); }
.hud-actions { display: flex; gap: 6px; }
.icon-button {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  color: #594224;
  background: #fff8dc;
  border: 2px solid #9c7540;
  border-radius: 9px;
  box-shadow: inset 0 -3px #e8cf91;
  cursor: pointer;
  font-weight: 900;
}
.icon-button:active, .tower-card:active, .start-button:active, .tower-actions button:active { transform: translateY(2px); box-shadow: none; }
.speed-button { font-size: 13px; }
.sound-glyph { position: relative; width: 12px; height: 14px; border-radius: 3px 0 0 3px; background: #5c482b; }
.sound-glyph::before { content: ""; position: absolute; left: -5px; top: 4px; border: 5px solid transparent; border-right-color: #5c482b; }
.sound-glyph::after { content: "))"; position: absolute; left: 9px; top: -5px; font-size: 13px; }
.sound-button.muted .sound-glyph::after { content: "×"; left: 10px; top: -2px; color: #b04738; }

.stage-wrap { position: relative; min-height: 0; overflow: hidden; background: #8fc657; }
.stage-wrap canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.status-pill {
  position: absolute;
  left: 14px;
  top: 12px;
  max-width: 42%;
  padding: 7px 12px;
  color: #5d4727;
  background: #fff8dcdd;
  border: 2px solid #947242;
  border-radius: 9px;
  box-shadow: 0 3px 8px #3e562733;
  font-size: 12px;
  pointer-events: none;
}
.countdown {
  position: absolute;
  left: 50%;
  top: 44%;
  width: 90px;
  height: 90px;
  display: grid;
  place-items: center;
  transform: translate(-50%, -50%);
  color: #fffbea;
  background: #e16a47e8;
  border: 5px solid #fff2bd;
  border-radius: 50%;
  box-shadow: 0 6px 18px #4d312f55;
  font-size: 42px;
  font-weight: 900;
  z-index: 5;
}
.tower-actions {
  position: absolute;
  right: 14px;
  top: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px;
  background: #fff8dfee;
  border: 2px solid #8d6d3c;
  border-radius: 10px;
  box-shadow: 0 4px 12px #4f362f44;
  z-index: 5;
}
.tower-actions > div { padding: 0 5px; }
.tower-actions strong, .tower-actions small { display: block; white-space: nowrap; }
.tower-actions strong { font-size: 12px; }
.tower-actions small { margin-top: 2px; color: #876b40; font-size: 9px; }
.tower-actions button {
  min-width: 78px;
  padding: 7px 9px;
  color: #fffceb;
  background: #df6845;
  border: 2px solid #9e402b;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
  font-size: 11px;
}
.tower-actions .sell-button { color: #5a472b; background: #f1d58c; border-color: #a87a32; }
.tower-actions button:disabled { opacity: .45; cursor: not-allowed; }

.control-bar {
  min-height: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 10px;
  background: linear-gradient(#fff1c3, #efd493);
  border-top: 3px solid #a97938;
  box-shadow: inset 0 2px #fff8da;
  z-index: 4;
}
.tower-tray { display: flex; align-self: stretch; gap: 6px; }
.tower-card {
  position: relative;
  width: 104px;
  min-width: 0;
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: center;
  padding: 3px 5px;
  color: var(--ink);
  background: #fff9df;
  border: 2px solid #b98b46;
  border-radius: 10px;
  box-shadow: inset 0 -3px #e7cb86, 0 2px #7b552e33;
  cursor: pointer;
}
.tower-card.selected { border-color: #d84f37; background: #fff0b9; box-shadow: inset 0 -3px #efc06c, 0 0 0 2px #f0784f55; }
.tower-card.unaffordable { filter: saturate(.35); opacity: .58; }
.tower-card canvas { width: 42px; height: 48px; }
.tower-card strong, .tower-card small { display: block; text-align: left; white-space: nowrap; }
.tower-card strong { font-size: 11px; }
.tower-card small { margin-top: 3px; color: #8a6735; font-size: 9px; }
.tower-card .hotkey { position: absolute; right: 4px; top: 2px; color: #b3925e; font-size: 8px; }
.wave-panel { min-width: 0; display: grid; grid-template-columns: 112px minmax(80px, 1fr) 132px; align-items: center; gap: 9px; flex: 1; }
.wave-copy strong, .wave-copy small { display: block; }
.wave-copy strong { font-size: 12px; }
.wave-copy small { margin-top: 3px; color: #83683d; font-size: 9px; }
.wave-progress { height: 11px; overflow: hidden; background: #d1b36b; border: 2px solid #99723b; border-radius: 8px; box-shadow: inset 0 2px #8a653c44; }
.wave-progress span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, #78ad48, #b5dc66); transition: width .2s; }
.start-button, .primary-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 12px;
  color: #fffbea;
  background: linear-gradient(#ed7b50, #d9543c);
  border: 2px solid #9e3d2c;
  border-radius: 10px;
  box-shadow: inset 0 2px #ffb077, 0 3px #783f2e38;
  cursor: pointer;
  font-weight: 900;
  font-size: 12px;
}
.start-button:disabled { opacity: .65; cursor: default; }
.play-glyph { width: 0; height: 0; border-top: 6px solid transparent; border-bottom: 6px solid transparent; border-left: 10px solid #fff7da; }

.result-panel {
  position: absolute;
  inset: 0;
  width: 360px;
  height: 286px;
  margin: auto;
  padding: 24px 26px 18px;
  text-align: center;
  background: #fff4c8;
  border: 5px solid #76532b;
  border-radius: 18px;
  box-shadow: 0 18px 40px #382c256e, inset 0 0 0 3px #f3c96c;
  z-index: 10;
}
.medal { position: absolute; left: 50%; top: -36px; width: 76px; height: 76px; transform: translateX(-50%); border: 5px solid #a76f1d; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #fff18a, #f0ad28 56%, #ca781d); box-shadow: 0 5px #7c5122; }
.medal span { position: absolute; inset: 16px; background: #fff1a4; clip-path: polygon(50% 0, 62% 34%, 98% 36%, 69% 57%, 79% 94%, 50% 72%, 21% 94%, 31% 57%, 2% 36%, 38% 34%); }
.result-kicker { margin: 20px 0 2px; color: #95713b; font-size: 12px; font-weight: 800; }
.result-panel h1 { margin: 0 0 14px; color: #744319; font-size: 34px; }
.result-stats { display: flex; justify-content: center; gap: 20px; margin-bottom: 20px; }
.result-stats span { padding: 7px 9px; background: #f2d78e; border-radius: 8px; font-size: 11px; }
.result-stats b { display: block; margin-top: 3px; font-size: 15px; }
.primary-button { width: 150px; margin: 0 auto; }
.toast { position: absolute; left: 50%; bottom: 77px; transform: translate(-50%, 12px); padding: 8px 13px; color: #fff8dc; background: #413426e8; border-radius: 8px; font-size: 11px; opacity: 0; pointer-events: none; transition: .18s; z-index: 20; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.hidden { display: none !important; }

@media (max-width: 680px) {
  body { display: block; padding: 0; background: #fff5cf; }
  .game-shell { width: 100vw; aspect-ratio: auto; grid-template-rows: 58px auto 104px; border: 0; border-radius: 0; box-shadow: none; }
  .stage-wrap { aspect-ratio: 960 / 500; }
  .hud { gap: 5px; padding: 5px 7px; border-bottom-width: 2px; }
  .level-badge { min-width: 98px; gap: 5px; }
  .leaf-mark { width: 31px; height: 31px; }
  .leaf-mark::after { width: 19px; top: 14px; }
  .level-badge strong { font-size: 12px; }
  .level-badge small { font-size: 8px; }
  .resource { min-width: 48px; height: 32px; gap: 3px; padding: 0 4px; border-width: 1px; }
  .resource b { font-size: 11px; }
  .coin-glyph { width: 15px; height: 15px; border-width: 1px; box-shadow: inset 0 0 0 3px #efa62a; }
  .wave-glyph { width: 16px; height: 13px; border-width: 2px; }
  .heart-glyph { width: 16px; height: 14px; }
  .hud-actions { gap: 3px; }
  .icon-button { width: 30px; height: 30px; border-width: 1px; font-size: 10px; }
  .sound-button { display: none; }
  .status-pill { left: 8px; top: 7px; max-width: 63%; padding: 5px 8px; font-size: 9px; border-width: 1px; }
  .countdown { width: 56px; height: 56px; border-width: 3px; font-size: 27px; }
  .tower-actions { top: 6px; right: 6px; gap: 3px; padding: 4px; border-width: 1px; }
  .tower-actions > div { display: none; }
  .tower-actions button { min-width: 57px; padding: 5px; border-width: 1px; font-size: 9px; }
  .control-bar { display: block; padding: 5px 7px; border-top-width: 2px; }
  .tower-tray { height: 51px; justify-content: center; margin-bottom: 4px; }
  .tower-card { width: 31.8%; max-width: 124px; grid-template-columns: 38px 1fr; padding: 2px 4px; border-width: 1px; }
  .tower-card canvas { width: 36px; height: 43px; }
  .tower-card strong { font-size: 9px; }
  .tower-card small { font-size: 8px; }
  .wave-panel { grid-template-columns: 76px 1fr 103px; gap: 5px; }
  .wave-copy strong { font-size: 9px; }
  .wave-copy small { font-size: 7px; }
  .wave-progress { height: 8px; border-width: 1px; }
  .start-button { padding: 7px 5px; border-width: 1px; font-size: 9px; }
  .play-glyph { border-top-width: 4px; border-bottom-width: 4px; border-left-width: 7px; }
  .toast { bottom: 111px; font-size: 9px; }
  .result-panel { width: min(310px, 86%); height: 215px; padding: 14px; }
  .medal { width: 56px; height: 56px; top: -27px; }
  .result-kicker { margin-top: 13px; }
  .result-panel h1 { font-size: 26px; margin-bottom: 9px; }
  .result-stats { margin-bottom: 10px; }
  .primary-button { padding: 7px; }
}
