:root {
  color-scheme: dark;
  --bg: #090b10;
  --panel: #141820;
  --panel-2: #1d2330;
  --line: #30394a;
  --text: #f2f6ff;
  --muted: #9da8b8;
  --accent: #47d7ff;
  --gold: #ffd166;
  --danger: #ff5c7a;
  --good: #5cff99;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 20% 0%, #182033 0, #090b10 36rem);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

.shell {
  width: min(1220px, calc(100vw - 24px));
  margin: 0 auto;
  padding: 16px 0 22px;
}

.status-toast {
  position: fixed;
  top: 18px;
  left: 50%;
  z-index: 20;
  width: min(560px, calc(100vw - 28px));
  min-height: 44px;
  display: grid;
  place-items: center;
  padding: 10px 16px;
  border: 1px solid rgb(71 215 255 / 70%);
  border-radius: 8px;
  color: var(--text);
  background: rgb(12 18 28 / 94%);
  box-shadow: 0 18px 44px rgb(0 0 0 / 42%);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -12px);
  transition:
    opacity 160ms ease,
    transform 160ms ease;
  text-align: center;
  font-weight: 800;
}

.status-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.hud {
  display: grid;
  grid-template-columns: repeat(5, minmax(92px, 1fr)) minmax(110px, auto) minmax(132px, auto);
  gap: 10px;
  align-items: stretch;
  margin-bottom: 12px;
}

.hud-item,
.primary-button,
.secondary-button,
.action-button,
.panel,
.tower-button {
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 90%, black);
  box-shadow: 0 12px 30px rgb(0 0 0 / 24%);
}

.hud-item {
  min-height: 58px;
  padding: 10px 12px;
  border-radius: 8px;
}

.hud-item span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.1;
  text-transform: uppercase;
}

.hud-item strong {
  display: block;
  margin-top: 4px;
  color: var(--gold);
  font-size: 1.36rem;
  line-height: 1.1;
}

.primary-button {
  min-height: 58px;
  border-radius: 8px;
  color: #061017;
  background: linear-gradient(180deg, #77e8ff, #28bfe8);
  cursor: pointer;
  font-weight: 800;
}

.primary-button:disabled {
  color: #677381;
  background: #202633;
  cursor: default;
}

.secondary-button {
  width: 100%;
  min-height: 42px;
  margin-top: 10px;
  border-radius: 8px;
  color: var(--text);
  background: #172030;
  cursor: pointer;
  font-weight: 800;
}

.hud-button {
  min-height: 58px;
  margin-top: 0;
}

.secondary-button:hover {
  border-color: var(--accent);
  background: #1c2a3e;
}

.secondary-button.is-selected {
  border-color: var(--accent);
  color: #061018;
  background: var(--accent);
}

.secondary-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.panel-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.action-button {
  width: 100%;
  min-height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: var(--text);
  background: #172030;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 900;
  line-height: 1;
}

.action-button:hover,
.action-button.is-selected {
  border-color: var(--accent);
  color: #061018;
  background: var(--accent);
}

.action-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.icon-button {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: #172030;
  cursor: pointer;
  font-weight: 900;
}

.icon-button:hover {
  border-color: var(--accent);
  background: #1c2a3e;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 250px;
  gap: 12px;
  align-items: start;
}

.board-shell {
  position: relative;
  width: 100%;
  max-width: 960px;
}

#gameCanvas {
  width: 100%;
  max-width: 960px;
  aspect-ratio: 3 / 2;
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #090d14;
  touch-action: manipulation;
}

.start-screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  border-radius: 8px;
  background: rgb(6 10 16 / 78%);
  opacity: 1;
  visibility: visible;
  transition:
    opacity 180ms ease,
    visibility 180ms ease;
}

.start-screen.is-hidden {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

.start-panel {
  width: min(420px, 100%);
  padding: 18px;
  border: 1px solid rgb(71 215 255 / 42%);
  border-radius: 8px;
  background: rgb(13 18 28 / 96%);
  box-shadow: 0 24px 70px rgb(0 0 0 / 46%);
}

.start-kicker {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.start-panel h2 {
  margin: 0;
  font-size: clamp(1.4rem, 4vw, 2rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.start-panel .primary-button {
  width: 100%;
  margin-top: 12px;
}

.panel {
  border-radius: 8px;
  padding: 14px;
}

.panel h1 {
  margin: 0 0 10px;
  font-size: clamp(1.1rem, 2.2vw, 1.45rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.status-message {
  min-height: 36px;
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid #273142;
  border-radius: 8px;
  color: var(--accent);
  background: #101621;
  font-size: 0.92rem;
}

.difficulty-control {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  padding: 4px;
  border: 1px solid #273142;
  border-radius: 8px;
  background: #0f141d;
}

.start-difficulty-control {
  margin-top: 14px;
}

.difficulty-button {
  min-height: 34px;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-weight: 800;
}

.difficulty-button:hover,
.difficulty-button.is-selected {
  border-color: var(--accent);
  color: var(--text);
  background: #172233;
}

.difficulty-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.tower-grid {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.tower-button {
  min-height: 52px;
  display: grid;
  grid-template-columns: 32px 1fr auto;
  gap: 9px;
  align-items: center;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.tower-button strong {
  color: var(--gold);
}

.tower-button:hover,
.tower-button.is-selected {
  border-color: var(--accent);
  background: #172233;
}

.tower-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.tower-icon,
.tower-glyph,
.legend-chip {
  display: inline-block;
  justify-self: center;
}

.tower-icon {
  position: relative;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
}

.tower-glyph {
  width: 24px;
  height: 24px;
}

.tower-hotkey {
  position: absolute;
  right: -2px;
  bottom: -1px;
  min-width: 15px;
  height: 15px;
  display: grid;
  place-items: center;
  border: 1px solid rgb(242 246 255 / 72%);
  border-radius: 4px;
  color: #061017;
  background: #f2f6ff;
  font-size: 0.64rem;
  font-weight: 900;
  line-height: 1;
}

.tower-glyph-basic {
  background: #fff;
}

.tower-glyph-poison {
  width: 0;
  height: 0;
  border-left: 13px solid transparent;
  border-right: 13px solid transparent;
  border-bottom: 24px solid #48ff81;
}

.tower-glyph-splash {
  border-radius: 50%;
  background: #ff3d52;
}

.tower-glyph-slow {
  background: #45a3ff;
  clip-path: polygon(50% 0, 100% 38%, 82% 100%, 18% 100%, 0 38%);
}

.tower-glyph-corrosion {
  background: #556b2f;
  clip-path: polygon(50% 0, 93% 25%, 93% 75%, 50% 100%, 7% 75%, 7% 25%);
}

.tower-glyph-sniper {
  background: #173a78;
  clip-path: polygon(50% 0, 88% 100%, 50% 76%, 12% 100%);
  filter: drop-shadow(0 0 7px rgb(127 178 255 / 72%));
  box-shadow: inset 0 0 0 2px #7fb2ff;
}

.legend {
  display: grid;
  gap: 8px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.88rem;
}

.legend div {
  display: flex;
  gap: 8px;
  align-items: center;
}

.legend-chip {
  width: 18px;
  height: 18px;
  border-radius: 4px;
}

.path-chip {
  background: #263044;
}

.wall-chip {
  background: #101620;
  border: 1px solid #354157;
}

.range-chip {
  border: 2px solid rgb(71 215 255 / 70%);
  border-radius: 50%;
}

.bestiary-modal,
.confirm-modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgb(4 7 11 / 72%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.bestiary-modal.is-open,
.confirm-modal.is-open {
  opacity: 1;
  pointer-events: auto;
}

.bestiary-dialog {
  width: min(760px, 100%);
  max-height: min(720px, calc(100vh - 36px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101620;
  box-shadow: 0 28px 80px rgb(0 0 0 / 58%);
}

.confirm-dialog {
  width: min(360px, 100%);
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101620;
  box-shadow: 0 28px 80px rgb(0 0 0 / 58%);
}

.confirm-dialog h2 {
  margin: 0;
  font-size: 1.2rem;
  line-height: 1.15;
}

.confirm-dialog p {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.4;
}

.confirm-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 16px;
}

.confirm-actions .primary-button,
.confirm-actions .secondary-button {
  width: 100%;
  min-height: 40px;
  margin-top: 0;
}

.bestiary-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
  background: #101620;
}

.bestiary-header h2 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.1;
  letter-spacing: 0;
}

.bestiary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 14px;
}

.bestiary-entry {
  min-height: 112px;
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid #273142;
  border-radius: 8px;
  background: #121925;
}

.bestiary-entry h3 {
  margin: 0 0 5px;
  font-size: 1rem;
  line-height: 1.15;
}

.bestiary-entry p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.enemy-icon,
.enemy-carousel {
  width: 42px;
  height: 42px;
  display: inline-block;
  justify-self: center;
}

.enemy-icon {
  position: relative;
}

.enemy-icon-normal {
  width: 30px;
  height: 30px;
  border: 3px solid #5d6673;
  background: #d9dde5;
}

.enemy-icon-normal::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #ffffff;
  transform: translate(-50%, -50%);
}

.enemy-icon-tank {
  width: 36px;
  height: 36px;
  background: #505866;
  clip-path: polygon(50% 0, 93% 25%, 93% 75%, 50% 100%, 7% 75%, 7% 25%);
  filter: drop-shadow(0 0 0 #090909) drop-shadow(0 0 2px #090909);
}

.enemy-icon-runner {
  width: 30px;
  height: 30px;
  background: #ffe45c;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  filter: drop-shadow(0 0 2px #ff9d2e);
}

.enemy-icon-healer {
  width: 32px;
  height: 32px;
  background: #9a62ff;
  clip-path: polygon(50% 0, 85% 15%, 100% 50%, 85% 85%, 50% 100%, 15% 85%, 0 50%, 15% 15%);
  filter: drop-shadow(0 0 2px #e1d3ff);
}

.enemy-icon-healer::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #6effe5;
  transform: translate(-50%, -50%);
}

.enemy-icon-freezer {
  width: 30px;
  height: 40px;
  background: #7ee7ff;
  clip-path: polygon(50% 0, 88% 50%, 50% 100%, 12% 50%);
  filter: drop-shadow(0 0 2px #d9f8ff);
}

.enemy-icon-freezer::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #0b2a6b;
  transform: translate(-50%, -50%);
}

.enemy-carousel {
  position: relative;
  width: 88px;
  height: 88px;
}

.enemy-carousel .enemy-icon {
  position: absolute;
  left: 50%;
  top: 50%;
  opacity: 0;
  animation: enemy-carousel-cycle 5s infinite;
  transform: translate(-50%, -50%) scale(var(--enemy-rank-scale, 1));
  transform-origin: center;
}

.enemy-carousel .enemy-icon:nth-child(1) {
  animation-delay: 0s;
}

.enemy-carousel .enemy-icon:nth-child(2) {
  animation-delay: 1s;
}

.enemy-carousel .enemy-icon:nth-child(3) {
  animation-delay: 2s;
}

.enemy-carousel .enemy-icon:nth-child(4) {
  animation-delay: 3s;
}

.enemy-carousel .enemy-icon:nth-child(5) {
  animation-delay: 4s;
}

.enemy-carousel-elite {
  --enemy-rank-scale: 1.5;
  filter: drop-shadow(0 0 8px rgb(255 209 102 / 58%));
}

.enemy-carousel-boss {
  --enemy-rank-scale: 2;
  filter: drop-shadow(0 0 10px rgb(255 92 122 / 62%));
}

@keyframes enemy-carousel-cycle {
  0%,
  19.99% {
    opacity: 1;
  }

  20%,
  100% {
    opacity: 0;
  }
}

@media (max-width: 900px) {
  .hud {
    grid-template-columns: repeat(2, minmax(110px, 1fr));
  }

  .primary-button {
    grid-column: span 2;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  #gameCanvas {
    max-width: none;
  }

  .board-shell {
    max-width: none;
  }

  .tower-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .bestiary-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .shell {
    width: min(100vw - 14px, 1220px);
    padding-top: 8px;
  }

  .status-toast {
    top: 8px;
    min-height: 40px;
    font-size: 0.88rem;
  }

  .hud {
    gap: 7px;
  }

  .hud-item,
  .primary-button {
    min-height: 50px;
  }

  .tower-grid {
    grid-template-columns: 1fr;
  }
}
