:root {
  color-scheme: dark;
  --panel: rgba(7, 12, 22, 0.72);
  --line: rgba(255, 255, 255, 0.18);
  --text: #f4f7fb;
  --muted: #aeb8c8;
  --accent: #7ce7ff;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #050915;
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: none;
  user-select: none;
}

canvas {
  display: block;
  width: 100vw;
  height: 100vh;
}

.hud {
  position: fixed;
  inset: env(safe-area-inset-top) 0 auto 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  padding: 12px max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  pointer-events: none;
}

.controls {
  margin-left: auto;
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.stat,
.button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(12px);
}

.stat {
  display: grid;
  grid-template-columns: auto auto;
  column-gap: 8px;
  align-items: center;
  padding: 7px 11px;
  font-weight: 700;
}

.stat span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.stat strong {
  min-width: 54px;
  text-align: right;
  font-size: 18px;
  line-height: 1;
}

.lives-stat {
  grid-template-columns: auto minmax(78px, auto);
}

.deploy-stat strong {
  min-width: 0;
  max-width: 148px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lives {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  min-width: 78px;
}

.life-icon {
  position: relative;
  width: 18px;
  height: 14px;
  flex: 0 0 auto;
  filter: drop-shadow(0 0 6px rgba(124, 231, 255, 0.42));
}

.life-icon::before {
  position: absolute;
  inset: 0;
  content: "";
  clip-path: var(--life-clip, polygon(100% 50%, 12% 0, 34% 50%, 12% 100%));
  background: var(--life-fill, #eaf7ff);
  border: 1px solid var(--life-stroke, #74d9f1);
}

.life-count {
  min-width: 24px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 900;
  line-height: 1;
  text-align: right;
}

.button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  padding: 0;
  color: var(--text);
  pointer-events: auto;
  cursor: pointer;
}

.button svg {
  width: 21px;
  height: 21px;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  padding: 18px;
  background: linear-gradient(rgba(5, 9, 21, 0.34), rgba(5, 9, 21, 0.54));
  pointer-events: auto;
}

.overlay[hidden] {
  display: none;
}

.dialog {
  width: min(420px, 100%);
  max-height: min(720px, calc(100vh - 36px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 22px;
  background: rgba(7, 12, 22, 0.86);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.38);
  backdrop-filter: blur(18px);
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(30px, 10vw, 46px);
  line-height: 0.95;
  letter-spacing: 0;
}

p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.45;
}

.player-panel,
.ship-panel,
.controls-panel,
.leaderboard {
  margin: 0 0 14px;
}

.panel-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
}

.panel-heading span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.panel-heading strong {
  color: var(--text);
  font-size: 13px;
  text-align: right;
}

.player-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 40px;
  margin: 0 0 10px;
}

.player-list:empty::before {
  content: "Add your name to begin";
  display: grid;
  min-height: 40px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.player-choice,
.name-form button,
.key-row button,
.link-button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.player-choice {
  max-width: 100%;
  padding: 0 11px;
}

.player-choice.active {
  border-color: var(--accent);
  background: rgba(124, 231, 255, 0.16);
  color: #ffffff;
}

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

.ship-choice {
  display: grid;
  gap: 4px;
  min-height: 76px;
  min-width: 0;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 5px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}

.ship-choice svg {
  width: min(100%, 76px);
  height: 38px;
}

.ship-choice span {
  overflow: hidden;
  max-width: 100%;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ship-choice.active {
  border-color: var(--accent);
  background: rgba(124, 231, 255, 0.16);
  color: #ffffff;
}

.name-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.name-form input {
  min-height: 40px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 11px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  font: inherit;
}

.name-form input::placeholder {
  color: rgba(174, 184, 200, 0.72);
}

.name-form button {
  padding: 0 14px;
}

.link-button {
  min-height: 0;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--accent);
  font-size: 12px;
}

.key-bindings {
  display: grid;
  gap: 7px;
}

.key-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(96px, auto);
  gap: 8px;
  align-items: center;
  min-height: 36px;
  color: var(--muted);
  font-size: 13px;
}

.key-row button {
  min-height: 34px;
  padding: 0 10px;
  color: var(--text);
}

.key-row.listening button {
  border-color: var(--accent);
  background: rgba(124, 231, 255, 0.18);
  color: #ffffff;
}

.leaderboard ol {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard li {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  min-height: 32px;
  padding: 5px 8px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  font-size: 13px;
}

.leaderboard .name {
  overflow: hidden;
  color: var(--text);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard .score {
  font-weight: 800;
  color: var(--accent);
}

.empty-board {
  color: var(--muted);
  font-size: 13px;
}

.primary {
  width: 100%;
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #041018;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.primary:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

@media (max-width: 520px) {
  .hud {
    padding-top: 10px;
    gap: 8px;
  }

  .controls {
    position: fixed;
    inset: auto auto 0 0;
    width: 50vw;
    height: 50vh;
    min-width: 124px;
    margin-left: 0;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-end;
    gap: 10px;
    padding: 12px 12px max(24px, env(safe-area-inset-bottom));
    border: 1px solid rgba(124, 231, 255, 0.26);
    border-left: 0;
    border-radius: 0 8px 8px 0;
    background: rgba(5, 9, 21, 0.22);
  }

  .stat {
    min-height: 38px;
    padding-inline: 9px;
  }

  .stat strong {
    min-width: 48px;
    font-size: 17px;
  }

  .deploy-stat strong {
    max-width: 88px;
  }

  .lives-stat {
    grid-template-columns: auto minmax(58px, auto);
  }

  .lives {
    min-width: 58px;
    gap: 3px;
  }

  .life-icon {
    width: 15px;
    height: 12px;
  }

  .button {
    width: min(112px, 100%);
    min-height: clamp(68px, 12vh, 94px);
    border-color: rgba(124, 231, 255, 0.28);
    background: rgba(7, 12, 22, 0.72);
  }

  .button svg {
    width: clamp(34px, 7vh, 44px);
    height: clamp(34px, 7vh, 44px);
  }

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

@media (max-width: 380px) {
  .hud {
    align-items: flex-start;
    flex-direction: column;
  }
}
