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

:root {
  --bg: #071018;
  --panel: #0f1c28;
  --cream: #e8eef8;
  --muted: rgba(232, 238, 248, 0.55);
  --gold: #fbbf24;
  --purple: #a78bfa;
  --cyan: #3df0ff;
  --danger: #f87171;
  --ok: #4ade80;
}

html,
body {
  height: 100%;
  background: var(--bg);
  color: var(--cream);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse 70% 40% at 50% -5%, rgba(124, 58, 237, 0.16), transparent 55%),
    radial-gradient(ellipse 50% 35% at 85% 80%, rgba(61, 240, 255, 0.06), transparent 50%),
    var(--bg);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: max(0.5rem, env(safe-area-inset-top)) 0.9rem 0.35rem;
  flex-shrink: 0;
}

.home-link {
  color: #7dd3fc;
  text-decoration: none;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.06em;
}

.logo {
  font-size: 0.95rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.logo span {
  color: var(--purple);
}

.version {
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  opacity: 0.45;
  color: #86efac;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem 1rem;
  justify-content: center;
  padding: 0.25rem 0.75rem 0.45rem;
  flex-shrink: 0;
}

.stat {
  min-width: 4.5rem;
  padding: 0.35rem 0.7rem;
  border-radius: 0.55rem;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  text-align: center;
}

.stat .label {
  display: block;
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.5;
  margin-bottom: 0.1rem;
}

.stat strong {
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  font-weight: 800;
}

.stat.lives strong {
  color: var(--danger);
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 0.6rem;
  gap: 0.4rem;
}

#game {
  width: min(960px, 100%);
  height: auto;
  max-height: 100%;
  aspect-ratio: 960 / 540;
  border-radius: 0.85rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: #0a1a14;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  cursor: crosshair;
  touch-action: none;
  display: block;
}

/* Mobile portrait map — tall canvas, path runs top→bottom */
#game.is-portrait {
  aspect-ratio: 540 / 960;
  width: auto;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}

/* Upgrade / sell — floats next to selected tower (above canvas) */
.tower-panel {
  position: absolute;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  padding: 0.35rem;
  border-radius: 0.55rem;
  background: rgba(15, 28, 40, 0.96);
  border: 1px solid rgba(251, 191, 36, 0.55);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  pointer-events: auto;
  transform: translateY(-50%);
  touch-action: manipulation;
}

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

.tp-btn {
  min-width: 6.4rem;
  padding: 0.4rem 0.55rem;
  border-radius: 0.4rem;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: var(--cream);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

.tp-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.tp-btn.upgrade {
  border-color: rgba(74, 222, 128, 0.4);
  background: rgba(34, 197, 94, 0.15);
}

.tp-btn.upgrade:hover:not(:disabled) {
  background: rgba(34, 197, 94, 0.28);
}

.tp-btn.sell {
  border-color: rgba(248, 113, 113, 0.4);
  background: rgba(239, 68, 68, 0.12);
}

.tp-btn.sell:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.24);
}

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  min-height: 1.1rem;
}

.dock {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
  justify-content: center;
  padding: 0.55rem 0.75rem max(0.75rem, env(safe-area-inset-bottom));
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.35));
}

.tower-picks {
  display: flex;
  gap: 0.45rem;
}

.pick {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.12rem;
  min-width: 7.2rem;
  padding: 0.55rem 0.7rem;
  border-radius: 0.65rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
  color: inherit;
  font: inherit;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}

.pick:hover {
  border-color: rgba(125, 211, 252, 0.45);
}

.pick[aria-pressed="true"] {
  border-color: rgba(167, 139, 250, 0.75);
  background: rgba(124, 58, 237, 0.16);
  box-shadow: 0 0 0 1px rgba(167, 139, 250, 0.35);
}

.pick[data-type="tri"][aria-pressed="true"] {
  border-color: rgba(61, 240, 255, 0.7);
  background: rgba(61, 240, 255, 0.1);
  box-shadow: 0 0 0 1px rgba(61, 240, 255, 0.3);
}

.pick[data-type="circ"][aria-pressed="true"] {
  border-color: rgba(251, 113, 133, 0.75);
  background: rgba(244, 63, 94, 0.14);
  box-shadow: 0 0 0 1px rgba(251, 113, 133, 0.35);
}

.pick-icon {
  width: 1.35rem;
  height: 1.35rem;
  margin-bottom: 0.15rem;
}

.squipy-ico {
  border-radius: 0.28rem;
  background: linear-gradient(160deg, #c4b5fd, #7c3aed 55%, #5b21b6);
  border: 1.5px solid rgba(255, 255, 255, 0.7);
  box-shadow: 0 0 10px rgba(167, 139, 250, 0.45);
}

.tri-ico {
  width: 0;
  height: 0;
  border-left: 0.65rem solid transparent;
  border-right: 0.65rem solid transparent;
  border-bottom: 1.15rem solid #3df0ff;
  background: none;
  filter: drop-shadow(0 0 6px rgba(61, 240, 255, 0.5));
}

.circ-ico {
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fda4af, #fb7185 50%, #e11d48);
  border: 1.5px solid rgba(255, 255, 255, 0.75);
  box-shadow: 0 0 10px rgba(251, 113, 133, 0.5);
}

.tower-picks .pick {
  min-width: 6.4rem;
}

.pick-name {
  font-weight: 800;
  font-size: 0.92rem;
}

.pick-meta {
  font-size: 0.72rem;
  opacity: 0.6;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
}

.btn {
  min-width: 5.4rem;
  padding: 0.55rem 0.85rem;
  border-radius: 0.55rem;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: inherit;
  font: inherit;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.btn.primary {
  background: linear-gradient(180deg, #7c3aed, #5b21b6);
  border-color: rgba(196, 181, 253, 0.45);
  color: #f5f3ff;
}

.btn.primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn.ghost:hover:not(:disabled) {
  border-color: rgba(125, 211, 252, 0.45);
  background: rgba(56, 189, 248, 0.1);
}

.btn[aria-pressed="true"]#btn-auto {
  border-color: rgba(74, 222, 128, 0.55);
  background: rgba(34, 197, 94, 0.16);
  color: #bbf7d0;
}

.modal {
  position: fixed;
  inset: var(--rg-bar) 0 0 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(6, 16, 24, 0.78);
  backdrop-filter: blur(4px);
  padding: 1rem;
}

.modal[hidden] {
  display: none !important;
}

.modal-card {
  width: min(22rem, 100%);
  padding: 1.35rem 1.25rem 1.2rem;
  border-radius: 1rem;
  background: linear-gradient(180deg, #152433, #0f1c28);
  border: 1px solid rgba(255, 255, 255, 0.12);
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

.modal-card h2 {
  font-size: 1.45rem;
  color: #fde68a;
  margin-bottom: 0.3rem;
}

.modal-sub {
  font-size: 0.9rem;
  opacity: 0.65;
  margin-bottom: 0.85rem;
  line-height: 1.4;
}

.over-score {
  font-size: 1.05rem;
  margin-bottom: 1rem;
}

.over-score strong {
  color: var(--gold);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  justify-content: center;
}

/* Mobile: compact chrome so the map can zoom into remaining space */
@media (max-width: 700px) {
  .top-bar {
    padding: max(0.25rem, env(safe-area-inset-top)) 0.4rem 0.1rem;
    gap: 0.3rem;
  }

  .logo {
    font-size: 0.78rem;
  }

  .home-link {
    font-size: 0.68rem;
  }

  .version {
    font-size: 0.55rem;
  }

  .hud {
    gap: 0.22rem 0.28rem;
    padding: 0.1rem 0.3rem 0.2rem;
  }

  .stat {
    min-width: 2.7rem;
    padding: 0.18rem 0.28rem;
  }

  .stat strong {
    font-size: 0.85rem;
  }

  .stat .label {
    font-size: 0.5rem;
    margin-bottom: 0.04rem;
  }

  .stage {
    flex: 1 1 auto;
    min-height: 0;
    padding: 0 0.2rem;
    gap: 0;
    justify-content: center;
    align-items: center;
  }

  #game {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0.4rem;
    /* Fill the stage box as much as aspect allows */
    flex: 0 1 auto;
  }

  #game.is-portrait {
    height: 100%;
    width: auto;
    max-width: 100%;
    max-height: 100%;
  }

  #game:not(.is-portrait) {
    width: 100%;
    height: auto;
    max-height: 100%;
  }

  .hint {
    display: none;
  }

  .pick {
    min-width: 0;
    flex: 1 1 30%;
    padding: 0.32rem 0.28rem;
  }

  .pick-name {
    font-size: 0.78rem;
  }

  .pick-meta {
    font-size: 0.55rem;
  }

  .btn {
    min-width: 3.8rem;
    min-height: 2.25rem;
    padding: 0.38rem 0.4rem;
    font-size: 0.72rem;
  }

  .dock {
    gap: 0.28rem;
    padding: 0.25rem 0.25rem max(0.4rem, env(safe-area-inset-bottom));
    flex-shrink: 0;
  }

  .tower-picks {
    width: 100%;
    justify-content: center;
    flex-wrap: nowrap;
    gap: 0.25rem;
  }

  .actions {
    width: 100%;
    gap: 0.25rem;
    justify-content: center;
  }

  .tower-panel {
    transform: translateY(-50%) scale(1.05);
  }

  .tp-btn {
    min-height: 2.35rem;
    min-width: 6.2rem;
  }
}

@media (max-width: 420px) {
  .pick-meta {
    font-size: 0.52rem;
  }

  .top-bar {
    padding: max(0.22rem, env(safe-area-inset-top)) 0.35rem 0.08rem;
  }

  .logo {
    font-size: 0.72rem;
  }
}
