:root {
  --panel: rgba(252, 248, 238, 0.93);
  --panel-edge: rgba(79, 66, 49, 0.18);
  --ink: #302a22;
  --ink-soft: #6d6255;
  --accent: #c9762c;
  --accent-2: #2f8f83;
  --good: #4f9b5b;
  --bad: #c0503c;
  --shadow: 0 10px 30px rgba(40, 30, 18, 0.18);
  --sky-a: #bfe6f2;
  --sky-b: #f6e7c8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --panel: rgba(24, 31, 40, 0.90);
    --panel-edge: rgba(180, 210, 230, 0.16);
    --ink: #eef3f5;
    --ink-soft: #aebdc6;
    --accent: #f0ae57;
    --accent-2: #6fd0c1;
    --good: #83cf8e;
    --bad: #f08b74;
    --shadow: 0 12px 34px rgba(0, 0, 0, 0.4);
    --sky-a: #22384d;
    --sky-b: #5b6f66;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Trebuchet MS', 'Avenir Next', system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky-a) 0%, var(--sky-b) 100%);
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
#scene.dragging { cursor: grabbing; }

.hidden { display: none !important; }

kbd {
  display: inline-block;
  min-width: 1.5em;
  padding: 1px 6px;
  border: 1px solid var(--panel-edge);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.35);
  font: inherit;
  font-size: 0.82em;
  text-align: center;
  color: var(--ink);
}

button {
  font: inherit;
  color: var(--ink);
  cursor: pointer;
}
button:focus-visible, .tile-focus:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------------- HUD ---------------- */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
}
#hud button, #hud .panel, #hud aside { pointer-events: auto; }

#topbar {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--panel-edge);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  white-space: nowrap;
  max-width: calc(100vw - 24px);
}

.chip {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 12px;
  border-radius: 10px;
  background: rgba(127, 108, 80, 0.09);
}
.chip .glyph { font-size: 17px; line-height: 1; }
.chip.gold .glyph { color: #d99a2b; }
.chip.lives .glyph { color: var(--bad); }
.chip-label { font-size: 10px; letter-spacing: 0.12em; color: var(--ink-soft); font-weight: bold; }
.chip strong { font-size: 19px; font-variant-numeric: tabular-nums; }
.chip .dim { font-size: 13px; color: var(--ink-soft); font-weight: normal; }

.top-actions { display: flex; gap: 6px; margin-left: 6px; }
.pill {
  min-width: 40px;
  padding: 8px 12px;
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.45);
  font-weight: bold;
  font-size: 13px;
}
.pill:hover { background: rgba(255, 255, 255, 0.8); }
.pill.active { background: var(--accent-2); color: #fff; border-color: transparent; }

#buildPanel, #selectionPanel {
  position: absolute;
  bottom: 16px;
  border: 1px solid var(--panel-edge);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  padding: 14px;
}
#buildPanel { left: 16px; width: 330px; max-width: calc(100vw - 32px); }
#selectionPanel { right: 16px; width: 274px; max-width: calc(100vw - 32px); }

.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  font-weight: bold;
  letter-spacing: 0.14em;
  color: var(--ink-soft);
  margin-bottom: 10px;
}
.panel-title .hint { font-size: 10px; letter-spacing: 0; font-weight: normal; }

#towerCards { display: flex; flex-direction: column; gap: 7px; }
.tower-card {
  display: grid;
  grid-template-columns: 30px 1fr auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: 1px solid var(--panel-edge);
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.35);
  transition: transform 0.12s ease, background 0.12s ease;
}
.tower-card:hover { background: rgba(255, 255, 255, 0.7); transform: translateY(-1px); }
.tower-card.selected { border-color: var(--accent); background: rgba(201, 118, 44, 0.14); box-shadow: inset 0 0 0 1px var(--accent); }
.tower-card.unaffordable { opacity: 0.55; }
.tower-icon {
  width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  font-size: 16px; font-weight: bold; color: #fff;
}
.tower-icon.arrow { background: #b9813c; }
.tower-icon.cannon { background: #596472; }
.tower-icon.frost { background: #4b9fb5; }
.tower-card .tname { font-weight: bold; font-size: 14px; }
.tower-card .trole { font-size: 10.5px; color: var(--ink-soft); line-height: 1.25; }
.tower-card .tcost { font-weight: bold; font-size: 14px; color: var(--accent); white-space: nowrap; }

.preview {
  margin-top: 11px;
  padding: 9px 10px;
  border-radius: 10px;
  background: rgba(47, 143, 131, 0.12);
  border: 1px solid rgba(47, 143, 131, 0.28);
  font-size: 12px;
  line-height: 1.35;
}
.preview .pv-title { font-weight: bold; color: var(--accent-2); font-size: 11px; letter-spacing: 0.08em; }
.preview .pv-boss { color: var(--bad); font-weight: bold; }

.primary {
  display: block;
  width: 100%;
  margin-top: 11px;
  padding: 11px 14px;
  border: none;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-weight: bold;
  font-size: 15px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.16);
}
.primary:hover { filter: brightness(1.08); }
.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.16); }
.primary:disabled { opacity: 0.5; cursor: not-allowed; }
.primary.big { width: auto; padding: 13px 34px; font-size: 17px; margin: 18px auto 0; }
.primary kbd { background: rgba(255, 255, 255, 0.2); border-color: rgba(255, 255, 255, 0.4); color: #fff; }

.tiny-help { margin-top: 9px; font-size: 10px; line-height: 1.4; color: var(--ink-soft); }

#selectionPanel h2 { margin: 0 0 2px; font-size: 16px; }
#selectionPanel .sub { font-size: 11px; color: var(--ink-soft); margin-bottom: 10px; }
.stat-row { display: flex; justify-content: space-between; font-size: 12px; padding: 3px 0; border-bottom: 1px dotted var(--panel-edge); }
.stat-row .up { color: var(--good); font-weight: bold; }
.sel-actions { display: flex; gap: 7px; margin-top: 12px; }
.sel-actions button { flex: 1; padding: 9px 6px; border-radius: 9px; border: 1px solid var(--panel-edge); background: rgba(255, 255, 255, 0.5); font-size: 12px; font-weight: bold; }
.sel-actions button.buy { background: var(--accent); color: #fff; border-color: transparent; }
.sel-actions button.sell { background: var(--bad); color: #fff; border-color: transparent; }
.sel-actions button:disabled { opacity: 0.45; cursor: not-allowed; }
.tier-pips { display: flex; gap: 4px; margin: 6px 0 10px; }
.tier-pips i { width: 22px; height: 5px; border-radius: 3px; background: rgba(127, 108, 80, 0.25); }
.tier-pips i.on { background: var(--accent); }

#toast {
  position: absolute;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%) translateY(14px);
  padding: 10px 18px;
  border-radius: 12px;
  background: rgba(36, 30, 24, 0.9);
  color: #fff;
  font-size: 13px;
  font-weight: bold;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--bad); }

#pauseOverlay, #helpOverlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(20, 24, 30, 0.35);
  z-index: 5;
  pointer-events: auto;
}
#pauseOverlay div { font-size: 38px; font-weight: bold; color: #fff; text-shadow: 0 3px 14px rgba(0,0,0,0.5); text-align: center; }
#pauseOverlay small { display: block; font-size: 14px; font-weight: normal; margin-top: 6px; }
.help-card {
  width: min(460px, calc(100vw - 32px));
  padding: 24px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  box-shadow: var(--shadow);
}
.help-card h2 { margin: 0 0 12px; }
.help-card ul { margin: 0; padding-left: 18px; line-height: 1.9; font-size: 13px; }

/* ---------------- screens ---------------- */
.screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 10;
  background: radial-gradient(ellipse at 50% 35%, rgba(255,255,255,0.25), rgba(26, 34, 41, 0.55));
  padding: 16px;
}
.card {
  width: min(520px, calc(100vw - 32px));
  text-align: center;
  padding: 30px 34px 34px;
  border-radius: 22px;
  border: 1px solid var(--panel-edge);
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(20, 26, 34, 0.35);
  backdrop-filter: blur(10px);
}
.eyebrow { font-size: 11px; letter-spacing: 0.22em; font-weight: bold; color: var(--accent-2); }
.card h1 { margin: 8px 0 6px; font-size: 44px; line-height: 1.05; letter-spacing: -0.02em; }
.card .goal { margin: 8px 0; font-size: 20px; font-weight: bold; }
.card .blurb { margin: 0 auto 16px; max-width: 400px; font-size: 13px; line-height: 1.55; color: var(--ink-soft); }
.key-list { list-style: none; margin: 0 auto; padding: 14px; max-width: 400px; text-align: left; border-radius: 12px; background: rgba(127,108,80,0.09); font-size: 12.5px; line-height: 2.1; }
.best-line { margin: 16px 0 0; font-size: 13px; color: var(--ink-soft); }
.best-line strong { color: var(--accent); font-size: 17px; }
.stats { list-style: none; padding: 0; margin: 18px auto 0; max-width: 300px; text-align: left; }
.stats li { display: flex; justify-content: space-between; padding: 8px 4px; border-bottom: 1px solid var(--panel-edge); font-size: 14px; }
.stats li strong { color: var(--accent); }

/* ---------------- focus pads & popups ---------------- */
#focus-layer { position: fixed; inset: 0; pointer-events: none; z-index: 2; }
.tile-focus {
  position: absolute;
  width: 34px; height: 34px;
  margin: -17px 0 0 -17px;
  border-radius: 50%;
  border: 2px solid transparent;
  background: transparent;
  pointer-events: auto;
  padding: 0;
}
.tile-focus:focus-visible {
  border-color: var(--accent);
  background: rgba(201, 118, 44, 0.18);
  box-shadow: 0 0 0 3px rgba(201, 118, 44, 0.25);
}
#popups { position: fixed; inset: 0; pointer-events: none; z-index: 4; overflow: hidden; }
.popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: bold;
  font-size: 16px;
  color: #ffd76a;
  text-shadow: 0 2px 6px rgba(0,0,0,0.55);
  animation: popup-rise 0.95s ease-out forwards;
}
.popup.leak { color: #ff8a72; }
@keyframes popup-rise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.7); }
  20% { opacity: 1; transform: translate(-50%, -70%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -190%) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
  .tower-card:hover { transform: none; }
}

@media (max-width: 760px) {
  #topbar { flex-wrap: wrap; justify-content: center; top: 8px; gap: 4px; padding: 6px; }
  .chip { padding: 3px 8px; }
  .chip strong { font-size: 15px; }
  #buildPanel { width: calc(100vw - 24px); left: 12px; bottom: 12px; padding: 10px; }
  #selectionPanel { bottom: auto; top: 96px; right: 12px; width: 230px; }
  .tower-card .trole { display: none; }
}
