:root {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #111827;
  color: #fff;
  font-family: Arial, Helvetica, sans-serif;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  overscroll-behavior: none;
}

main {
  width: min(960px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.back {
  align-self: flex-start;
  color: #d1d5db;
  font-weight: 700;
  text-decoration: none;
}

.back:hover {
  color: #93c5fd;
}

h1 {
  margin: 0;
  font-size: clamp(1.7rem, 5vw, 2.4rem);
  text-align: center;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 12px 22px;
  font-size: 1.125rem;
}

#restart,
#deploy,
.fire,
.move-btn,
.weapon-btn {
  font-family: inherit;
  color: #fff;
  font-weight: 700;
  border: 0;
  cursor: pointer;
}

#restart {
  background: #9333ea;
  border-radius: 8px;
  padding: 8px 16px;
}

#restart:hover,
#restart:focus-visible {
  background: #7e22ce;
}

.reload {
  margin: 0;
  color: #facc15;
  font-size: 1.5rem;
  font-weight: 700;
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.45; }
}

.stage {
  width: min(800px, 100%);
  border: 4px solid #374151;
  border-radius: 4px;
  background: #1f2937;
  touch-action: none;
  line-height: 0;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  cursor: crosshair;
  background: #1f2937;
}

.pad,
.help {
  width: min(960px, 100%);
  background: #1f2937;
  border: 2px solid #4b5563;
  border-radius: 8px;
  padding: 16px;
}

.pad h2,
.help h2 {
  margin: 0 0 12px;
  text-align: center;
  font-size: 1.125rem;
}

.pad-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

h3 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 0.875rem;
}

.move {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

#up { grid-column: 2; }
#left { grid-column: 1; }
#down { grid-column: 2; }
#right { grid-column: 3; }

.move-btn {
  min-height: 56px;
  border-radius: 8px;
  background: #2563eb;
  font-size: 1.25rem;
  touch-action: none;
  user-select: none;
}

.move-btn.is-held,
.move-btn:active {
  background: #1d4ed8;
}

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

.weapon-btn {
  min-height: 52px;
  border-radius: 8px;
  background: #4b5563;
  font-size: 0.8rem;
}

.weapon-btn:hover {
  background: #374151;
}

.weapon-btn.is-on {
  background: #16a34a;
  box-shadow: 0 0 0 2px #4ade80;
}

.fire {
  width: 100%;
  min-height: 112px;
  border-radius: 8px;
  background: #dc2626;
  font-size: 1.35rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.25);
  touch-action: none;
  user-select: none;
}

.fire svg {
  width: 32px;
  height: 32px;
}

.fire.is-held,
.fire:active {
  background: #991b1b;
}

.help ul {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 16px;
  margin: 0;
  padding-left: 18px;
  font-size: 0.95rem;
}

.tip {
  margin: 12px 0 0;
  color: #facc15;
  font-size: 0.8rem;
}

.gameover {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.9);
}

.gameover[hidden] {
  display: none;
}

.gameover-card {
  width: min(420px, 100%);
  background: #1f2937;
  border: 4px solid #dc2626;
  border-radius: 8px;
  padding: 32px 24px;
  text-align: center;
}

.alert {
  width: 80px;
  height: 80px;
  color: #ef4444;
}

.gameover-card h2 {
  margin: 8px 0 16px;
  font-size: clamp(1.8rem, 6vw, 2.4rem);
}

#final-score {
  margin: 0 0 24px;
  font-size: 1.5rem;
}

#deploy {
  background: #2563eb;
  border-radius: 8px;
  padding: 12px 28px;
  font-size: 1.25rem;
}

#deploy:hover,
#deploy:focus-visible {
  background: #1d4ed8;
}

:focus-visible {
  outline: 3px solid #facc15;
  outline-offset: 3px;
}

@media (max-width: 720px) {
  .pad-grid,
  .help ul {
    grid-template-columns: 1fr;
  }

  .move-btn,
  .weapon-btn {
    min-height: 64px;
  }
}
