:root {
  color-scheme: dark;
  --ink: #f3f7e8;
  --muted: #a9beb0;
  --panel: rgba(15, 37, 34, 0.92);
  --panel-light: rgba(31, 63, 52, 0.86);
  --line: rgba(224, 244, 203, 0.16);
  --mint: #8ff0b0;
  --gold: #ffd66b;
  --danger: #ff8c78;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  color: var(--ink);
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, sans-serif;
  background:
    radial-gradient(circle at 12% 5%, rgba(79, 153, 118, 0.24), transparent 34rem),
    radial-gradient(circle at 88% 100%, rgba(88, 75, 145, 0.25), transparent 35rem),
    #0a1719;
}

button { font: inherit; }
button:focus-visible, canvas:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}

.game-shell {
  width: 100%;
  height: 100vh;
  margin: 0 auto;
  padding: 8px 0 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 8px;
}

.topbar {
  padding: 0 12px;
}
.topbar, .help-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
}

h1 {
  margin: 0;
  font-size: clamp(1.65rem, 3vw, 3rem);
  line-height: 0.9;
  letter-spacing: -0.055em;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--mint);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

.status-strip { display: flex; gap: 10px; }
.status-pill {
  display: grid;
  grid-template-columns: auto auto;
  column-gap: 8px;
  min-width: 94px;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 10px 28px rgba(0,0,0,.2);
}
.status-pill span { grid-row: 1 / 3; align-self: center; font-size: 1.35rem; }
.status-pill strong { font-variant-numeric: tabular-nums; line-height: 1; }
.status-pill small { color: var(--muted); font-size: 0.65rem; text-transform: uppercase; }

.control-deck {
  margin: 0 8px;
  padding: 8px 10px 9px;
  border: 1px solid rgba(209, 243, 205, 0.14);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(18,39,35,0.96), rgba(14,30,28,0.92));
  box-shadow: 0 18px 40px rgba(4, 15, 12, 0.25);
  align-items: stretch;
  flex-direction: column;
}
.game-menu {
  position: relative;
  overflow: hidden;
}
.game-menu::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(120deg, rgba(144, 244, 176, 0.06), transparent 40%, rgba(240, 255, 197, 0.04));
  pointer-events: none;
}
.defender-tray {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 6px;
}
.defender-card {
  position: relative;
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 3px 8px;
  min-width: 0;
  flex: 1;
  min-height: 58px;
  padding: 7px 7px 8px;
  color: var(--ink);
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: linear-gradient(145deg, rgba(42, 68, 52, .96), rgba(18, 39, 34, .94));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.05);
  cursor: pointer;
  transition: transform .15s ease, border-color .15s ease, opacity .2s;
}
.defender-card:hover:not(:disabled) { transform: translateY(-2px); border-color: rgba(143,240,176,.6); }
.defender-card.selected { border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold), 0 5px 22px rgba(255,214,107,.12); }
.defender-card:disabled { cursor: not-allowed; opacity: .42; filter: grayscale(.55); }
.defender-icon { grid-row: 1 / 3; align-self: center; text-align: center; }
.plant-icon {
  position: relative;
  display: block;
  width: 27px;
  height: 34px;
  margin: auto;
  border-bottom: 4px solid #416f4c;
  border-radius: 50%;
}
.plant-icon::before,
.plant-icon::after {
  content: "";
  position: absolute;
  display: block;
  background: currentColor;
}
.plant-icon::before {
  left: 11px;
  bottom: 5px;
  width: 5px;
  height: 22px;
  border-radius: 7px;
}
.plant-icon::after {
  left: 3px;
  top: 1px;
  width: 20px;
  height: 14px;
  border-radius: 100% 0 100% 0;
  transform: rotate(-12deg);
  box-shadow: 8px 11px 0 -2px currentColor;
}
.plant-well::after, .plant-tidewood::after { border-radius: 50%; transform: scale(.8); }
.plant-coil::after, .plant-storm::after { border-radius: 100% 0 100% 0; transform: rotate(38deg) scale(.8); }
.plant-bark::before, .plant-tidewood::before { width: 15px; left: 6px; height: 26px; bottom: 2px; }
.plant-bark::after, .plant-tidewood::after { width: 22px; height: 22px; top: 4px; left: 2px; }
.plant-breeze::after, .plant-tempo::after { border-radius: 100% 0; transform: rotate(30deg); }
.plant-mend::after { border-radius: 100% 0 100% 0; transform: rotate(-45deg); }
.plant-ember::after, .plant-firewind::after { border-radius: 60% 40% 60% 40%; transform: scale(.85); }
.defender-name { font-size: .72rem; font-weight: 800; line-height: 1.1; }
.defender-meta { color: var(--muted); font-size: .58rem; line-height: 1.1; }
.defender-cost { position: absolute; right: 8px; top: 7px; color: var(--gold); font-size: .72rem; font-weight: 800; }
.defender-card .key { position: absolute; left: 5px; top: 4px; color: rgba(255,255,255,.45); font-size: .58rem; }
.recharge {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 13px;
  color: white;
  font-size: .78rem;
  font-weight: 900;
  letter-spacing: .05em;
  background: rgba(8, 24, 22, .76);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
}
.defender-card.recharging .recharge { opacity: 1; }

.wave-track {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 6px;
  margin-top: 0;
  order: 1;
  flex: 1;
}
.wave-mark {
  position: relative;
  height: 7px;
  overflow: visible;
  border-radius: 10px;
  background: rgba(208, 235, 203, .14);
}
.wave-mark.done { background: var(--mint); }
.wave-mark.active { background: var(--gold); box-shadow: 0 0 11px rgba(255,214,107,.55); }
.wave-mark.surge::after {
  content: "◆";
  position: absolute;
  top: -12px;
  right: -2px;
  color: #d9a4ff;
  font-size: .72rem;
}
.enemy-guide {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 7px 18px;
  order: 2;
  color: var(--muted);
  font-size: .67rem;
  padding-top: 0;
  display: none;
}
.enemy-guide span { display: inline-flex; align-items: center; gap: 5px; }
.enemy-guide strong { color: var(--ink); }
.resist-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  box-shadow: 0 0 8px currentColor;
}
.resist-dot.blast { color: #ff9366; background: #ff9366; }
.resist-dot.slow { color: #d993f6; background: #d993f6; }
.resist-dot.push { color: #b8efff; background: #b8efff; }

.utility-controls {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(7, minmax(82px, 1fr));
  gap: 8px;
  align-items: stretch;
  justify-content: flex-end;
  order: 2;
  margin-top: 0;
}
.menu-lower { display: flex; align-items: center; gap: 12px; margin-top: 7px; }
.settings-panel {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px 14px;
  align-items: center;
  font-size: .72rem;
  color: var(--muted);
  margin: 8px 0 0;
}
.settings-panel.hidden { display: none; }
.settings-panel label { display: inline-flex; align-items: center; gap: 5px; }
.settings-panel input { accent-color: var(--gold); }
.settings-panel .tool-button { justify-self: end; }
button.primary-button, button.tool-button {
  border-radius: 13px;
  padding: 8px 11px;
  min-height: 36px;
  color: var(--ink);
  border: 1px solid var(--line);
  cursor: pointer;
  white-space: nowrap;
}
button.primary-button {
  color: #13201b;
  border: 0;
  font-weight: 900;
  background: linear-gradient(135deg, var(--gold), #ec9e43);
  box-shadow: 0 6px 20px rgba(236,158,67,.2);
}
button.tool-button { background: var(--panel-light); }
button.tool-button[aria-pressed="true"] { border-color: var(--danger); color: var(--danger); }
button:disabled { opacity: .45; cursor: not-allowed; }

.board-wrap {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(185, 226, 188, .2);
  border-radius: 24px;
  background: #183b31;
  box-shadow: 0 25px 70px rgba(0,0,0,.38);
  min-height: 0;
  height: 100%;
  margin-top: 0;
}
canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: crosshair;
  margin: 0 auto;
}

.message-panel {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(7, 18, 18, .56);
  backdrop-filter: blur(5px);
  transition: opacity .2s ease;
}
.message-panel.hidden { opacity: 0; pointer-events: none; }
.message-card {
  width: min(520px, 100%);
  padding: clamp(22px, 4vw, 42px);
  text-align: center;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: rgba(13, 34, 31, .96);
  box-shadow: 0 22px 70px rgba(0,0,0,.4);
}
.message-card h2 { margin: 0 0 10px; font-size: clamp(1.7rem, 4vw, 3rem); letter-spacing: -.04em; }
.message-card p:not(.eyebrow) { color: var(--muted); line-height: 1.6; }
.message-card button { margin-top: 8px; padding: 13px 22px; }
.level-menu {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin: 18px 0 4px;
}
.level-menu button {
  min-width: 0;
  min-height: 56px;
  margin: 0;
  padding: 8px 5px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-light);
  cursor: pointer;
}
.level-menu button:hover:not(:disabled),
.level-menu button.selected { border-color: var(--gold); color: var(--gold); }
.level-menu button:disabled { opacity: .4; cursor: not-allowed; }
.level-menu strong, .level-menu small { display: block; }
.level-menu small { margin-top: 4px; color: var(--muted); font-size: .62rem; }

.toast {
  position: absolute;
  left: 50%;
  bottom: 16px;
  z-index: 4;
  max-width: 80%;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(7, 22, 20, .9);
  color: var(--ink);
  font-size: .8rem;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition: .2s ease;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

.help-bar { padding: 0 12px; color: var(--muted); font-size: .68rem; min-height: 24px; }
.help-bar p { margin: 0; }
kbd {
  padding: 1px 5px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--panel);
}
.debug-tools { display: flex; align-items: center; gap: 5px; margin-left: auto; }
.debug-tools label { color: var(--muted); white-space: nowrap; }
.debug-tools input {
  width: 150px;
  min-width: 0;
  padding: 5px 7px;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(7, 22, 20, .75);
}
.debug-tools button { padding: 5px 9px; min-height: 0; }
.debug-tools span { color: var(--muted); white-space: nowrap; }

@media (max-width: 1050px) {
  .defender-tray { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .defender-card { min-width: 0; }
  .utility-controls { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .utility-controls button { min-width: 0; padding-inline: 6px; font-size: .78rem; }
  .settings-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 720px) {
  body { overflow: auto; height: auto; }
  .game-shell { height: auto; min-height: 100vh; display: flex; flex-direction: column; }
  .topbar { align-items: flex-start; }
  .status-strip { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; }
  .status-pill { min-width: 0; padding: 7px; }
  .topbar, .help-bar { flex-direction: column; align-items: stretch; }
  .defender-tray { grid-template-columns: 1fr 1fr; width: 100%; }
  .defender-card { min-height: 63px; }
  .menu-lower { align-items: stretch; flex-direction: column; }
  .utility-controls { display: grid; grid-template-columns: 1fr 1fr; }
  .settings-panel { grid-template-columns: 1fr; }
  .help-bar { gap: 4px; }
  .debug-tools { margin-left: 0; flex-wrap: wrap; }
  .debug-tools input { flex: 1; width: auto; }
  .board-wrap { height: min(72vh, 620px); }
  .level-menu { grid-template-columns: 1fr 1fr; }
}

body.high-contrast {
  --ink: #ffffff;
  --muted: #d7e6d6;
  --panel: rgba(3, 15, 15, 0.95);
  --panel-light: rgba(5, 26, 26, 0.92);
  --line: rgba(255, 255, 255, 0.28);
  --mint: #95ffb9;
  --gold: #ffd84f;
  --danger: #ff8a80;
}
