:root {
  color-scheme: dark;
  --black: #000;
  --deep: #080808;
  --panel: #151515;
  --gray: #555;
  --silver: #aaa;
  --white: #fff;
  --red: #f00000;
  --dark-red: #720000;
  --blue: #0000f0;
  --yellow: #f5e800;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 12% 18%, #aaa 0 1px, transparent 1.5px),
    radial-gradient(circle at 78% 30%, #555 0 1px, transparent 1.5px),
    radial-gradient(circle at 45% 82%, #fff 0 1px, transparent 1.4px),
    radial-gradient(circle at 92% 74%, #555 0 1px, transparent 1.5px),
    #000;
  background-size: 211px 173px, 263px 197px, 307px 239px, 181px 223px;
  color: var(--white);
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }

button:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--white);
  outline-offset: 2px;
}

.star-shell {
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px clamp(10px, 2vw, 28px) 28px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 0 auto 12px;
  max-width: 1020px;
}

.brand {
  letter-spacing: -.07em;
  line-height: .82;
  color: var(--white);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(23px, 4vw, 42px);
  font-weight: 900;
  text-shadow: 2px 2px 0 var(--gray);
}

.brand strong { color: var(--red); }

.brand small {
  margin-left: 10px;
  color: var(--silver);
  font: 700 10px ui-monospace, monospace;
  letter-spacing: .12em;
  text-shadow: none;
}

.toolbar { display: flex; gap: 7px; flex-wrap: wrap; justify-content: flex-end; }

.toolbar button,
.dialog-close {
  border: 1px solid var(--silver);
  border-bottom-color: var(--gray);
  background: linear-gradient(#222, #080808);
  color: var(--white);
  padding: 8px 11px;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px #000, 0 0 0 1px #333;
}

.toolbar button:hover { color: var(--red); border-color: var(--red); }
.toolbar button:disabled { color: var(--gray); border-color: #333; cursor: default; opacity: .62; }

.battle-cabinet {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(320px, 800px) minmax(150px, 1fr);
  align-items: center;
  gap: 12px;
  max-width: 1120px;
  margin: 0 auto;
}

.screen-frame {
  position: relative;
  padding: clamp(8px, 1.5vw, 15px);
  border: 2px solid var(--silver);
  background: #111;
  box-shadow:
    inset 0 0 0 3px #333,
    inset 0 0 0 5px #000,
    0 0 0 2px #000,
    0 16px 42px #000;
}

.screen-frame::before,
.screen-frame::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  border: 1px solid var(--gray);
  background: #000;
  transform: rotate(45deg);
  top: 50%;
}

.screen-frame::before { left: -6px; }
.screen-frame::after { right: -6px; }

.screen-wrap {
  position: relative;
  aspect-ratio: 8 / 5;
  overflow: hidden;
  border: 2px solid var(--blue);
  background: #000;
  box-shadow: 0 0 12px rgba(0, 0, 240, .45);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

.scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(0deg, transparent 0 3px, rgba(0, 0, 0, .11) 3px 4px);
  mix-blend-mode: multiply;
}

.player-card {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 74px;
  padding: 10px;
  border: 1px solid var(--gray);
  background: rgba(0, 0, 0, .86);
  box-shadow: inset 0 0 0 1px #222;
}

.player-card strong { display: block; font-size: 12px; letter-spacing: .06em; }
.player-card span { display: block; margin-top: 5px; color: var(--silver); font-size: 9px; line-height: 1.4; }
.player-two-card strong { color: var(--yellow); }
.player-one-card { text-align: right; }

.ship-icon {
  width: 36px;
  height: 20px;
  flex: 0 0 auto;
  clip-path: polygon(0 50%, 38% 0, 100% 28%, 84% 50%, 100% 72%, 38% 100%);
  filter: drop-shadow(0 0 3px currentColor);
}

.yellow-ship { color: var(--yellow); background: var(--yellow); }
.white-ship { color: var(--white); background: var(--white); transform: scaleX(-1); }

footer {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 11px;
  border: 1px solid #333;
  color: var(--gray);
  background: rgba(0, 0, 0, .9);
  font-size: 9px;
}

#status-text { color: var(--silver); text-align: right; }

.mobile-controls { display: none; grid-column: 1 / -1; gap: 8px; }
.touch-pilot { flex: 1 1 0; padding: 8px; border: 1px solid var(--gray); background: #080808; }
.touch-pilot > strong { display: block; margin-bottom: 6px; text-align: center; font-size: 10px; }
.player-two-touch > strong { color: var(--yellow); }
.touch-row { display: flex; justify-content: center; align-items: center; gap: 9px; }

.dpad {
  display: grid;
  grid-template: repeat(3, 36px) / repeat(3, 36px);
  gap: 2px;
}

.dpad button,
.fire-button {
  border: 1px solid var(--silver);
  border-radius: 2px;
  background: #171717;
  color: var(--white);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.dpad button:active,
.fire-button:active,
.dpad button.is-held { background: var(--dark-red); border-color: var(--red); }
.dpad button[data-control$="up"] { grid-area: 1 / 2; }
.dpad button[data-control$="left"] { grid-area: 2 / 1; }
.dpad button[data-control$="down"] { grid-area: 2 / 2; }
.dpad button[data-control$="right"] { grid-area: 2 / 3; }
.fire-button { width: 54px; height: 54px; color: var(--red); font-weight: 900; }

dialog {
  width: min(620px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  overflow: auto;
  border: 2px solid var(--silver);
  background: #080808;
  color: var(--white);
  box-shadow: 0 0 0 4px #000, 0 0 0 6px var(--blue);
}

dialog::backdrop { background: rgba(0, 0, 0, .82); }
.dialog-close { float: right; padding: 4px 9px; color: var(--red); font-size: 20px; }
dialog h2 { margin: 5px 45px 16px 0; color: var(--red); font-family: Georgia, serif; }
dialog p, dialog li { color: var(--silver); font-size: 13px; line-height: 1.55; }
.help-players { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.help-players section { padding: 8px 12px; border: 1px solid #333; background: #000; }
.help-players h3 { margin: 0 0 5px; color: var(--white); font-size: 13px; }
kbd { display: inline-block; min-width: 22px; padding: 2px 5px; border: 1px solid var(--gray); background: #171717; color: var(--white); text-align: center; }
.pickup { display: inline-block; width: 12px; height: 12px; margin-right: 6px; border-radius: 50%; vertical-align: -1px; }
.pickup.gold { background: var(--yellow); box-shadow: 0 0 5px var(--yellow); }
.pickup.power { background: var(--red); }
.pickup.shield { background: var(--white); border: 2px solid var(--yellow); }
.pickup.pulse { background: #00c000; }
.help-note { border-top: 1px solid #333; padding-top: 10px; }

body.cpu-mode .player-one-card { border-color: #777; }
body.cpu-mode .player-one-card strong::after {
  content: " AI";
  color: var(--red);
  font-size: 8px;
}

body.cpu-mode .player-one-touch { display: none; }
body.cpu-mode .player-two-touch { max-width: 390px; margin-inline: auto; }

@media (max-width: 930px) {
  .battle-cabinet { grid-template-columns: 1fr 1fr; }
  .screen-frame { grid-column: 1 / -1; grid-row: 1; }
  .player-card { grid-row: 2; }
  footer { grid-row: 4; }
}

@media (hover: none), (pointer: coarse), (max-width: 720px) {
  .star-shell { padding: 8px 6px 18px; }
  .topbar { align-items: flex-start; }
  .toolbar button { padding: 7px 8px; font-size: 10px; }
  .toolbar #restart-button { display: none; }
  .battle-cabinet { gap: 7px; }
  .player-card { display: none; }
  .mobile-controls { display: flex; grid-row: 2; }
  body.cpu-mode .mobile-controls { justify-content: center; }
  footer { grid-row: 3; }
  footer span:first-child { display: none; }
  footer { justify-content: center; }
  #status-text { text-align: center; }
}

@media (max-width: 520px) {
  .brand { font-size: 23px; }
  .brand small { display: none; }
  .toolbar { gap: 4px; }
  .toolbar button { padding: 6px; font-size: 9px; }
  .screen-frame { padding: 6px; }
  .dpad { grid-template: repeat(3, 31px) / repeat(3, 31px); }
  .fire-button { width: 45px; height: 45px; font-size: 10px; }
  .touch-row { gap: 5px; }
  .touch-pilot { padding: 6px 3px; }
  .help-players { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
  .scanlines { display: none; }
}
