* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: #08090d;
  color: #f3f5f8;
  font-family: system-ui, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
}

#app {
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background: #10131a;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  user-select: none;
  -webkit-user-select: none;
}

.tower {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 6px 10px 8px;
}

.base {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #171b26;
}

.base.enemy {
  border-radius: 18px 18px 0 0;
  box-shadow: inset 0 0 0 2px #ff4d5e;
  padding-right: 68px;
}

.base.player {
  border-radius: 0 0 18px 18px;
  box-shadow: inset 0 0 0 2px #4c84ff;
}

.base.hit {
  filter: brightness(1.35);
}

.base.firing .core {
  filter: brightness(1.7);
  box-shadow: 0 0 0 8px rgba(255, 236, 170, 0.95);
}

.core {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex: none;
}

.enemy .core {
  background: #ff4d5e;
  box-shadow: 0 0 0 6px rgba(255, 77, 94, 0.22);
}

.player .core {
  background: #4c84ff;
  box-shadow: 0 0 0 6px rgba(76, 132, 255, 0.22);
}

.base-copy {
  flex: 1;
  min-width: 0;
}

.base-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 8px;
}

.base-label span {
  font-size: 13px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #c9d0de;
}

.base-label strong {
  font-size: 24px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.hp-track {
  height: 10px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.38);
  overflow: hidden;
}

.hp-fill {
  width: 100%;
  height: 100%;
  border-radius: inherit;
}

.enemy .hp-fill {
  background: #ff4d5e;
}

.player .hp-fill {
  background: #4c84ff;
}

.hint {
  margin: 8px 0 0;
  color: #aeb6c8;
  font-size: 12px;
  line-height: 1.35;
}

.creep-readout {
  background: #221418;
  box-shadow: inset 2px 0 0 #ff4d5e, inset -2px 0 0 #ff4d5e;
}

.player-fold {
  margin-top: 6px;
}

.fold-toggle {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 0;
  background: transparent;
  color: #f3f5f8;
  font: inherit;
  text-align: left;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.fold-toggle.yours {
  border-radius: 16px 16px 0 0;
  background: #171b26;
}

.player-fold:has(.fold-panel[hidden]) .fold-toggle.yours {
  border-radius: 16px;
}

.fold-toggle span {
  font-size: 16px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #c9d0de;
}

.fold-label {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  min-width: 0;
}

.live-track {
  display: block;
  width: min(100%, 148px);
  height: 6px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.45);
  overflow: hidden;
  letter-spacing: 0;
  text-transform: none;
}

.live-track[hidden] {
  display: none !important;
}

.live-fill {
  display: block;
  width: 0%;
  height: 100%;
  background: #ffd56a;
}

#enemy-upgrade {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.live-track.enemy-live {
  width: 42px;
  flex: none;
}

.fold-toggle strong {
  margin-left: auto;
  padding: 4px 10px;
  border-radius: 999px;
  background: #3a2428;
  font-size: 16px;
  line-height: 1.2;
  letter-spacing: 0;
  text-transform: none;
  color: #ffd0d4;
  font-weight: 700;
  text-align: right;
}

.fold-toggle.yours strong {
  background: #243044;
  color: #d6e4ff;
}

#player-choice.waiting {
  color: #ffffff;
  background: #3d8bff;
  animation: pick-pulse 1.05s ease-in-out infinite;
}

@keyframes pick-pulse {
  0%,
  100% {
    color: #ffffff;
    background: #2f7dff;
    transform: scale(1);
    box-shadow:
      0 0 0 3px rgba(170, 214, 255, 0.95),
      0 0 18px rgba(80, 160, 255, 0.85);
    filter: brightness(1.05);
  }
  50% {
    color: #071426;
    background: #d7ecff;
    transform: scale(1.14);
    box-shadow:
      0 0 0 8px rgba(190, 225, 255, 1),
      0 0 36px rgba(120, 190, 255, 1);
    filter: brightness(1.25);
  }
}

@media (prefers-reduced-motion: reduce) {
  #player-choice.waiting {
    animation: none;
    color: #071426;
    background: #d7ecff;
    transform: none;
    box-shadow:
      0 0 0 4px rgba(190, 225, 255, 1),
      0 0 22px rgba(120, 190, 255, 0.95);
    filter: none;
  }
}

.fold-toggle::after {
  content: "";
  flex: none;
  width: 8px;
  height: 8px;
  margin-top: -4px;
  border-right: 2px solid #c9d0de;
  border-bottom: 2px solid #c9d0de;
  transform: rotate(45deg);
}

.fold-toggle[aria-expanded="true"]::after {
  margin-top: 4px;
  transform: rotate(225deg);
}

.fold-panel {
  padding: 0 8px 8px;
}

.fold-panel[hidden] {
  display: none !important;
}

.creep-readout[data-active="hp"] .fold-toggle strong,
.creep-readout[data-active="attackSpeed"] .fold-toggle strong,
.creep-readout[data-active="damage"] .fold-toggle strong,
.creep-readout[data-active="waveSize"] .fold-toggle strong,
.creep-readout[data-active="improve"] .fold-toggle strong,
.creep-readout[data-active="spawn"] .fold-toggle strong {
  background: #ff4d5e;
  color: #2a0c10;
}

.creep-readout.switching .fold-toggle strong {
  animation: bot-switch 0.75s ease;
}

@keyframes bot-switch {
  0% {
    transform: scale(1.12);
    filter: brightness(1.85);
    box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.85);
  }
  100% {
    transform: scale(1);
    filter: none;
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .creep-readout.switching .fold-toggle strong {
    animation: none;
    box-shadow: 0 0 0 2px #fff;
  }

  #player-choice.waiting {
    animation: none;
  }
}

.readout-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px 8px;
}

.readout-stats p {
  flex: 1;
  min-width: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 2px 5px;
  border-radius: 10px;
}

.creep-readout[data-active="hp"] [data-stat="hp"],
.creep-readout[data-active="attackSpeed"] [data-stat="attackSpeed"],
.creep-readout[data-active="damage"] [data-stat="damage"],
.creep-readout[data-active="waveSize"] [data-stat="waveSize"],
.creep-readout[data-active="improve"] [data-stat="improve"],
.creep-readout[data-active="spawn"] [data-stat="spawn"] {
  background: rgba(255, 77, 94, 0.28);
  box-shadow: inset 0 0 0 1px rgba(255, 154, 163, 0.7);
}

.readout-stats span {
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.15;
  color: #d7bcc0;
}

.readout-stats strong {
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  color: #fff;
}

.upgrades {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.upgrade {
  flex: 1;
  min-width: 0;
  min-height: 58px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 6px 4px;
  border-radius: 16px;
  border: 2px solid #2c3344;
  background: #171b26;
  color: #f3f5f8;
  font: inherit;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.upgrade-name {
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.15;
  color: #c9d0de;
}

.upgrade-value {
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}

.upgrade-state {
  min-height: 16px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #8b95a8;
}

.upgrade-track {
  display: block;
  width: 100%;
  height: 6px;
  margin-top: 4px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

.upgrade-track[hidden] {
  display: none !important;
}

.upgrade-fill {
  display: block;
  width: 0%;
  height: 100%;
  background: #ffd56a;
}

.upgrade[aria-pressed="true"] {
  border-color: #d6e4ff;
  background: #3d7dff;
  box-shadow: 0 0 0 3px rgba(61, 125, 255, 0.35);
}

.upgrade[data-max="true"] .upgrade-state {
  color: #ffd56a;
}

.upgrade[aria-pressed="true"] .upgrade-name,
.upgrade[aria-pressed="true"] .upgrade-value,
.upgrade[aria-pressed="true"] .upgrade-state {
  color: #fff;
}

.upgrade:active {
  transform: scale(0.98);
}

.unlock-launch {
  width: 100%;
  min-height: 48px;
  margin-top: 6px;
  border: 0;
  border-radius: 16px;
  background: #8a6a22;
  color: #fff;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.unlock-launch:active {
  transform: scale(0.98);
}

.unlocks {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.unlock,
.gate-choice {
  width: 100%;
  min-width: 0;
  min-height: 48px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 2px 10px;
  padding: 8px 10px;
  border-radius: 16px;
  border: 2px solid #3a3424;
  background: #1c1914;
  color: #f3f5f8;
  font: inherit;
  text-align: left;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.unlock-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.unlock-name {
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #e6d7b0;
}

.unlock-note,
.unlock-blurb {
  margin: 0;
  color: #d9d1c3;
  font-size: 16px;
  line-height: 1.25;
}

.unlock-blurb {
  margin: 0 0 10px;
}

.unlock-level {
  font-size: 16px;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
  justify-self: end;
}

.unlock-track {
  grid-column: 1 / -1;
  width: 100%;
  height: 6px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

.unlock-fill {
  display: block;
  width: 0%;
  height: 100%;
  background: #ffd56a;
}

.unlock[aria-pressed="true"] {
  border-color: #ffe7a3;
  background: #8a6a22;
  box-shadow: 0 0 0 3px rgba(255, 213, 106, 0.35);
}

.unlock[aria-pressed="true"] .unlock-name,
.unlock[aria-pressed="true"] .unlock-level {
  color: #fff;
}

.unlock[data-max="true"] .unlock-level {
  color: #ffd56a;
}

.unlock[aria-pressed="true"][data-max="true"] .unlock-level {
  color: #fff;
}

.unlock:active,
.gate-choice:active {
  transform: scale(0.98);
}

#unlock-window {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  background: rgba(6, 8, 12, 0.72);
  touch-action: manipulation;
}

#unlock-window:not([hidden]) {
  display: flex;
}

.unlock-card {
  width: min(100%, 420px);
  max-height: calc(100dvh - 24px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 14px 12px;
  text-align: left;
  overflow: auto;
}

.unlock-card h2 {
  margin: 0;
  font-size: 32px;
  line-height: 1.1;
  color: #ffe7a3;
}

#unlock-list[hidden],
#unlock-gate[hidden] {
  display: none !important;
}

#close-unlocks {
  width: 100%;
  min-height: 48px;
  flex: none;
  border: 0;
  border-radius: 16px;
  background: #2c3344;
  color: #fff;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#close-unlocks:active {
  transform: scale(0.98);
}

.upgrade-status {
  margin: 4px 2px 0;
  color: #c5cad6;
  font-size: 13px;
  line-height: 1.3;
}

.lane-wrap {
  position: relative;
  flex: 1;
  min-height: 120px;
  background: #171b26;
  box-shadow: inset 2px 0 0 #2c3344, inset -2px 0 0 #2c3344;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
}

#wave {
  font-variant-numeric: tabular-nums;
  color: #d5dbe8;
}

#menu {
  position: fixed;
  inset: 0;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  background: rgba(6, 8, 12, 0.92);
  touch-action: manipulation;
}

#menu[hidden] {
  display: none;
}

.menu-card h1 {
  margin: 0 0 8px;
  font-size: 40px;
  line-height: 1.1;
  font-weight: 700;
}

.menu-card p {
  margin: 0 0 22px;
  color: #c5cad6;
  font-size: 16px;
  line-height: 1.4;
}

#play {
  width: 100%;
  min-height: 64px;
  border: 0;
  border-radius: 16px;
  background: #3d7dff;
  color: #fff;
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#play:disabled {
  background: #2a3142;
  color: #9aa3b5;
}

#play:active:not(:disabled) {
  background: #2c68d8;
  transform: scale(0.98);
}

#load-error {
  position: fixed;
  inset: 0;
  z-index: 7;
  display: none;
  align-items: center;
  justify-content: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  background: rgba(6, 8, 12, 0.92);
  touch-action: manipulation;
}

#load-error:not([hidden]) {
  display: flex;
}

#retry {
  width: 100%;
  min-height: 64px;
  border: 0;
  border-radius: 16px;
  background: #3d7dff;
  color: #fff;
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#retry:active {
  background: #2c68d8;
  transform: scale(0.98);
}

#overlay {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: none;
  align-items: center;
  justify-content: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  background: rgba(6, 8, 12, 0.82);
  touch-action: manipulation;
}

#overlay:not([hidden]) {
  display: flex;
}

.card {
  width: min(100%, 360px);
  background: #171b24;
  border-radius: 20px;
  padding: 28px 22px 22px;
  text-align: center;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.card h2 {
  margin: 0 0 8px;
  font-size: 40px;
  line-height: 1.1;
}

.unlock-card h2 {
  margin: 0;
  font-size: 28px;
  color: #ffe7a3;
}

.unlock-card p {
  margin: 0;
}

.card h2[data-winner="player"] {
  color: #9ec0ff;
}

.card h2[data-winner="enemy"] {
  color: #ff9aa3;
}

.card h2[data-winner="draw"] {
  color: #f3f5f8;
}

.card p {
  margin: 0 0 22px;
  color: #c5cad6;
  font-size: 16px;
  line-height: 1.4;
}

#restart {
  width: 100%;
  min-height: 64px;
  border: 0;
  border-radius: 16px;
  background: #3d7dff;
  color: #fff;
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#restart:active {
  background: #2c68d8;
  transform: scale(0.98);
}

@media (max-height: 760px) {
  .tower {
    margin: 2px 10px 2px;
  }

  .hint {
    display: none;
  }

  #wave {
    display: block;
  }

  .base {
    gap: 8px;
    padding: 4px 10px;
  }

  .base-label {
    margin-bottom: 4px;
  }

  .core {
    width: 22px;
    height: 22px;
  }

  .base-label strong {
    font-size: 20px;
  }

  .hp-track {
    height: 8px;
  }

  .fold-toggle {
    min-height: 48px;
    padding: 4px 10px;
  }

  .fold-toggle strong {
    font-size: 16px;
    padding: 2px 8px;
  }

  .fold-panel {
    padding: 0 6px 4px;
  }

  .readout-stats {
    gap: 2px 6px;
  }

  .readout-stats p {
    padding: 1px 2px 2px;
    gap: 0;
  }

  .readout-stats strong {
    font-size: 16px;
  }

  .player-fold {
    margin-top: 4px;
  }

  .upgrades {
    gap: 6px;
  }

  .upgrade {
    min-height: 48px;
    padding: 2px 4px;
    border-radius: 14px;
    gap: 0;
  }

  .upgrade-value {
    font-size: 18px;
  }

  .upgrade-state {
    min-height: 14px;
    font-size: 11px;
  }

  .unlock-launch {
    min-height: 48px;
    margin-top: 4px;
    font-size: 16px;
  }

  .unlocks {
    gap: 6px;
  }

  .unlock,
  .gate-choice {
    min-height: 48px;
    padding: 6px 8px;
    border-radius: 14px;
  }

  .unlock-level {
    font-size: 16px;
  }

  .unlock-card {
    gap: 6px;
    padding: 12px 12px 10px;
  }

  .upgrade-status {
    margin-top: 2px;
    font-size: 16px;
    line-height: 1.25;
  }

  .lane-wrap {
    min-height: 120px;
  }
}

#sound-dock {
  position: fixed;
  z-index: 8;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, calc(50vw - 230px));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  touch-action: manipulation;
}

#sound-mute {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 2px solid #8eb4ff;
  border-radius: 16px;
  background: #243044;
  color: #f3f5f8;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.35);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#sound-mute svg {
  width: 26px;
  height: 26px;
  display: block;
}

#sound-mute.is-muted {
  position: relative;
  background: #3a2428;
  border-color: #ff8b96;
  color: #ffd0d4;
}

#sound-mute.is-muted::after {
  content: "";
  position: absolute;
  width: 28px;
  height: 2px;
  border-radius: 99px;
  background: currentColor;
  transform: rotate(-40deg);
}

#sound-mute.is-muted .sound-wave {
  display: none;
}

#sound-mute:active {
  transform: scale(0.98);
}

#sound-menu {
  position: absolute;
  z-index: 3;
  top: 8px;
  left: 8px;
  width: 188px;
  padding: 6px;
  border-radius: 16px;
  background: #171b24;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.08),
    0 10px 24px rgba(0, 0, 0, 0.4);
}

#sound-menu[hidden] {
  display: none !important;
}

.sound-spawn {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 48px;
  margin: 0;
  padding: 0 10px;
  white-space: nowrap;
  border-radius: 12px;
  background: #243044;
  color: #f3f5f8;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.2;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.sound-spawn input {
  width: 22px;
  height: 22px;
  margin: 0;
  flex: none;
  accent-color: #8eb4ff;
}

.sound-hint {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#sound-nudge {
  position: absolute;
  z-index: 2;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(16, 19, 26, 0.9);
  color: #d6e4ff;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.01em;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(142, 180, 255, 0.5);
}
