:root {
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface2: #201f1e;
  --border: #2c2c2a;
  --text: #ffffff;
  --text2: #c3c2b7;
  --muted: #898781;
  --gold: #f5c243;
  --blue: #3987e5;
  --orange: #d95926;
  --green: #199e70;
  --amber: #c98500;
  --pink: #d55181;
  --good: #0ca30c;
  --bad: #d03b3b;
}

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

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 12px 28px;
  -webkit-font-smoothing: antialiased;
}

.num { font-variant-numeric: tabular-nums; }

/* ---- Layout ---- */
#app { width: 100%; max-width: 1120px; }

header.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
header.top h1 {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: .01em;
  display: flex;
  align-items: center;
  gap: 9px;
}
header.top h1 .dot {
  width: 12px; height: 12px; border-radius: 3px;
  background: var(--gold);
  box-shadow: 0 0 12px rgba(245,194,67,.6);
}

#hud {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 6px 12px;
  min-width: 78px;
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}
.stat .label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
}
.stat .value {
  font-size: 17px;
  font-weight: 700;
}
.stat.lives .value { color: var(--bad); }
.stat.gold .value { color: var(--gold); }
.stat.wave .value { color: var(--text); }

.btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 13px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
  user-select: none;
  -webkit-user-select: none;
}
.btn:hover { background: var(--surface2); border-color: #3a3a37; }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--gold);
  color: #1a1200;
  border-color: var(--gold);
}
.btn.primary:hover { background: #ffd15c; }
.btn.on { border-color: var(--gold); color: var(--gold); }
.btn:disabled { opacity: .4; cursor: not-allowed; }

/* ---- Main game area ---- */
#game {
  display: grid;
  grid-template-columns: 1fr 268px;
  gap: 14px;
  align-items: start;
}

#stageWrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  overflow: hidden;
}
#stage {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 7px;
  background: #0a0a0a;
  touch-action: none;
}

/* Overlays (menu / gameover / help) */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(8,8,8,.86);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 26px;
  z-index: 5;
  border-radius: 12px;
}
.overlay.hidden { display: none; }
.overlay h2 {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: .01em;
  margin-bottom: 6px;
}
.overlay h2.win { color: var(--gold); }
.overlay h2.lose { color: var(--bad); }
.overlay p.sub { color: var(--text2); max-width: 460px; margin-bottom: 16px; font-size: 14px; line-height: 1.5; }
.overlay .row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 4px; }
.overlay .best { color: var(--gold); font-weight: 700; margin-bottom: 14px; }
.statgrid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0,1fr));
  gap: 10px;
  margin: 4px 0 18px;
  width: 100%;
  max-width: 420px;
}
.statgrid .cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}
.statgrid .cell .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.statgrid .cell .v { font-size: 20px; font-weight: 700; margin-top: 3px; }

.howto {
  text-align: left;
  max-width: 480px;
  color: var(--text2);
  font-size: 13.5px;
  line-height: 1.6;
}
.howto ul { list-style: none; margin: 10px 0; }
.howto li { margin: 5px 0; padding-left: 18px; position: relative; }
.howto li::before { content: '›'; position: absolute; left: 2px; color: var(--gold); font-weight: 700; }
.howto b { color: var(--text); }
kbd {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 12px;
  font-family: inherit;
  color: var(--text);
}

/* ---- Side panel ---- */
#side {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}
.panel h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin-bottom: 10px;
  font-weight: 700;
}

.shop { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tower-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 8px;
  cursor: pointer;
  text-align: left;
  transition: border-color .12s, background .12s, transform .06s;
  position: relative;
}
.tower-btn:hover { border-color: #3a3a37; }
.tower-btn:active { transform: translateY(1px); }
.tower-btn.selected { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
.tower-btn.cant { opacity: .5; }
.tower-btn .tname { font-size: 13px; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.tower-btn .tname .sw { width: 10px; height: 10px; border-radius: 3px; }
.tower-btn .tcost { font-size: 12px; color: var(--gold); margin-top: 3px; font-weight: 600; }
.tower-btn .tkey { position: absolute; top: 6px; right: 7px; font-size: 10px; color: var(--muted); }

.hint { font-size: 11.5px; color: var(--muted); margin-top: 9px; line-height: 1.45; }

/* Selected tower inspector */
#inspector .irow { display: flex; justify-content: space-between; font-size: 12.5px; margin: 3px 0; }
#inspector .irow .k { color: var(--muted); }
#inspector .irow .v { font-weight: 600; }
#inspector .ihead { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
#inspector .ihead .sw { width: 14px; height: 14px; border-radius: 4px; }
#inspector .ihead .nm { font-size: 14px; font-weight: 700; }
#inspector .ihead .tier { font-size: 11px; color: var(--gold); margin-left: auto; }
#inspector .actions { display: flex; gap: 8px; margin-top: 10px; }
#inspector .actions .btn { flex: 1; padding: 8px 6px; font-size: 12.5px; }
#inspector .prio { margin-top: 8px; }
#inspector .prio .btn { width: 100%; font-size: 12px; }
.empty-note { font-size: 12px; color: var(--muted); line-height: 1.5; }

/* Wave preview */
#wavePanel .wpreview { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.wchip {
  display: flex; align-items: center; gap: 5px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 4px 8px;
  font-size: 12px;
}
.wchip .sw { width: 9px; height: 9px; border-radius: 50%; }
.wchip.boss { border-color: var(--orange); }
#wavePanel .btn { width: 100%; }
#wavePanel .between { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
#wavePanel .between b { color: var(--gold); }

/* Toast messages */
#toasts {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  z-index: 4;
  pointer-events: none;
}
.toast {
  background: rgba(26,26,25,.95);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  animation: toastIn .2s ease;
}
.toast.good { border-color: var(--good); color: #7fe07f; }
.toast.bad { border-color: var(--bad); color: #ff8a8a; }
.toast.gold { border-color: var(--gold); color: var(--gold); }
@keyframes toastIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

footer.foot {
  margin-top: 14px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  line-height: 1.6;
}
footer.foot b { color: var(--text2); }

/* ---- Responsive ---- */
@media (max-width: 860px) {
  #game { grid-template-columns: 1fr; }
  #side { flex-direction: column; }
  .shop { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 460px) {
  header.top h1 { font-size: 16px; }
  .stat { min-width: 62px; padding: 5px 9px; }
  .stat .value { font-size: 15px; }
  .overlay h2 { font-size: 24px; }
  .btn { padding: 7px 10px; font-size: 12px; }
}
