:root {
  color-scheme: dark;
  font: 16px system-ui, sans-serif;
  color: #edf5f6;
  background: var(--deep-water);
  --deep-water: #10212a;
  --sea-near: #425e68;
  --sea-middle: #58757b;
  --sea-far: #465f69;
  --panel: #203741;
  --panel-hover: #2a4651;
  --line: #45616c;
  --muted: #b9cbd1;
  --sand: #faeac2;
  --gold: #ffdf8f;
  --sky: #87e3ff;
  --sky-hover: #b0eeff;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; }
p { margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
button:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }

main {
  min-height: 100dvh;
  background: radial-gradient(ellipse at 50% 47%, var(--panel), var(--deep-water) 75%);
}

#lobby {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 18px;
  width: min(100%, 64rem);
  min-height: 100dvh;
  margin-inline: auto;
  padding: 24px;
  text-align: center;
}

#lobby-title { color: var(--sand); font-size: clamp(2.4rem, 6vw, 4.5rem); line-height: 1.05; }

.lobby-sprites {
  display: flex;
  gap: clamp(0px, 2vw, 24px);
  opacity: 0.36;
}

.lobby-sprites img { width: clamp(54px, 8vw, 88px); height: auto; }

#lobby .guidance {
  max-width: 54rem;
  gap: 6px;
  font-size: 1rem;
  line-height: 1.55;
  word-break: keep-all;
}

.lobby-actions {
  display: grid;
  justify-items: center;
  gap: 12px;
  margin-top: 8px;
}

.lobby-actions button, .lobby-actions select {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: inherit;
  background: var(--panel);
}

.lobby-actions select { padding: 6px 12px; font: inherit; font-size: 0.85rem; }
#play-ai { width: min(18rem, 80vw); padding: 14px 24px; color: var(--deep-water); background: var(--sky); font-size: 1.3rem; font-weight: 750; }
#play-ai:hover { background: var(--sky-hover); }
#play-friend { padding: 8px 16px; font-size: 0.9rem; }
#play-friend:hover { background: var(--panel-hover); }

#language-switch {
  display: flex;
  justify-self: end;
  gap: 4px;
}

#language-switch button {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--panel);
  font-size: 0.75rem;
}

#language-switch [aria-pressed="true"] { border-color: var(--sky); color: var(--sky); }
#desktop-notice {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--sand);
  background: var(--panel);
  font-size: 0.9rem;
}

.privacy { color: var(--muted); font-size: 0.8rem; line-height: 1.5; }
.privacy details { margin-top: 4px; }
.privacy summary, .feedback summary { cursor: pointer; }

#game {
  display: grid;
  grid-template:
    "header" var(--header-height)
    "field" minmax(0, 1fr)
    "footer" calc(var(--build-height) + var(--hints-height)) / minmax(0, 1fr);
  height: 100dvh;
}

header {
  grid-area: header;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, min(46%, 510px)) minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding-inline: clamp(6px, 1.2vw, 16px);
}

h1 {
  margin: 0;
  font-size: 1.23rem;
  letter-spacing: -0.04em;
}

.room-heading { display: flex; align-items: baseline; gap: 7px; }
#title, #opponent, #status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#title { color: var(--sand); font-size: 0.88rem; line-height: 1.1; }
#opponent, #status {
  line-height: 1.15;
  color: var(--muted);
  font-size: 0.68rem;
}
#status { min-height: 1.15em; }

.match-hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  font-variant-numeric: tabular-nums;
}

[data-side="south"] { --team: var(--sky); }
[data-side="north"] { --team: #ff9aad; }

.health {
  min-width: 0;
  padding: 3px 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--team);
}

.health label {
  display: flex;
  justify-content: space-between;
  gap: 5px;
  font-size: 0.69rem;
  font-weight: 650;
  white-space: nowrap;
}
.health label span:first-child { overflow: hidden; text-overflow: ellipsis; }

.health progress {
  display: block;
  appearance: none;
  width: 100%;
  height: 5px;
  margin-top: 2px;
  border: 0;
  border-radius: 4px;
  background: #0d1b23;
  overflow: hidden;
}

.health progress::-webkit-progress-bar { background: #0d1b23; }
.health progress::-webkit-progress-value { background: var(--team); border-radius: 4px; }
.health progress::-moz-progress-bar { background: var(--team); border-radius: 4px; }

#timer {
  min-width: 4ch;
  color: #fff;
  font-size: 1.2rem;
  font-weight: 750;
  text-align: center;
}

.room-actions {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: end;
  gap: 8px;
  min-width: 0;
}

#invite-tools { display: flex; gap: 4px; min-width: 0; max-width: 100%; }
#invite {
  min-width: 0;
  width: 27ch;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 4px 6px;
  color: var(--muted);
  background: #132631;
  font: 0.72rem ui-monospace, monospace;
}

.room-actions button, #result button {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel);
  font-size: 0.78rem;
  white-space: nowrap;
}

.room-actions button:hover, #result button:enabled:hover { background: var(--panel-hover); }
#copy { min-width: 4.4em; }
#copy-note {
  position: absolute;
  top: calc(100% + 3px);
  right: 0;
  color: #ffe0a6;
  background: var(--deep-water);
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 0.72rem;
  text-align: right;
}

.guidance {
  display: flex;
  flex-direction: column;
  gap: 2px;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

#lobby-objective { color: #e4eef0; }

#field {
  grid-area: field;
  position: relative;
  min-width: 0;
  min-height: 0;
}

#arena { display: block; position: absolute; inset: 0; width: 100%; height: 100%; }

.sea-overlays {
  position: absolute;
  top: var(--field-top, 0px);
  left: 50%;
  width: var(--sea-width, 0px);
  height: var(--field-height, 0px);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  overflow: hidden;
  padding: 6px min(6px, 1vw);
  pointer-events: none;
  text-align: center;
}

#onboarding-hint, #placement {
  max-width: 100%;
  padding: 4px 7px;
  border-radius: 5px;
  font-size: 0.75rem;
  line-height: 1.2;
}

#onboarding-hint {
  /* Keep the base boundaries and missile entries clear. */
  max-width: min(64%, 29rem);
  background: #182c37e8;
  border: 1px solid #789ba6;
  overflow-wrap: anywhere;
}

#onboarding-hint.scrap { color: #ffe39b; background: #4b3e29; font-weight: 700; }
#placement { margin-top: auto; color: #ffcd9e; background: #182c37e8; }
#onboarding-hint:empty, #placement:empty { display: none; }

#result {
  position: absolute;
  top: 50%;
  left: 50%;
  display: flex;
  flex-direction: column;
  width: min(92%, 430px);
  max-height: min(100%, var(--field-height, 100%));
  transform: translate(-50%, -50%);
  overflow: hidden;
  border: 1px solid #6f8b9a;
  border-radius: 11px;
  background: #182c37;
  box-shadow: 0 8px 30px #070d15ad;
  text-align: center;
}

.result-content { min-height: 0; overflow: auto; padding: 14px 18px 10px; }
.result-footer { flex: none; padding: 8px 10px 12px; border-top: 1px solid var(--line); }
#result-title { margin: 0; font-size: 1.4rem; }
#result-opponent { margin-top: 3px; color: var(--muted); font-size: 0.8rem; }
#result-detail { margin: 5px 0 10px; color: #d6e4e8; font-size: 0.88rem; line-height: 1.35; }
#result-stats {
  display: grid;
  grid-template-columns: 1fr minmax(min-content, auto);
  gap: 4px 12px;
  margin: 0;
  padding: 10px 0;
  border-top: 1px solid var(--line);
  font-size: 0.8rem;
  text-align: left;
}
#result-stats dt { color: var(--muted); }
#result-stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.feedback { border-top: 1px solid var(--line); padding-top: 8px; text-align: left; font-size: 0.8rem; }
.feedback summary, #feedback-thanks { color: var(--muted); }
#feedback-form { display: grid; gap: 5px; margin-top: 8px; }
#feedback-form textarea {
  resize: vertical;
  width: 100%;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--deep-water);
  color: inherit;
  font: inherit;
}
#feedback-form fieldset { margin: 2px 0; padding: 3px 0; border: 0; }
#feedback-form fieldset label { display: inline-flex; align-items: center; margin-right: 8px; }
#feedback-form button { justify-self: start; }
#feedback-error { color: var(--sand); }
#feedback-error:empty, #feedback-thanks:empty { display: none; }

#rematch-status { margin-bottom: 10px; color: var(--muted); font-size: 0.85rem; }
#rematch-status:empty { display: none; }
#rematch-countdown { margin-bottom: 6px; color: var(--muted); font-size: 0.72rem; font-variant-numeric: tabular-nums; }
#result button { padding: 7px 14px; font-size: 0.86rem; }
#result button:disabled { opacity: 0.62; }
#rematch { border-color: var(--sky); }

.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; }

footer {
  grid-area: footer;
  z-index: 1;
  display: grid;
  grid-template-rows: var(--build-height) var(--hints-height);
  min-width: 0;
  padding-inline: clamp(6px, 1.2vw, 16px);
}

.build-hud {
  display: grid;
  grid-template-columns: max-content minmax(0, 1060px);
  justify-content: center;
  align-items: center;
  gap: 7px;
}

.economy {
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  font-size: 0.74rem;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.gold-group { position: relative; min-width: 5.7ch; }
#gold { color: var(--gold); font-size: 0.98rem; font-weight: 750; }
#gold-pickup {
  position: absolute;
  top: 2px;
  left: calc(100% + 12px);
  z-index: 1;
  color: #ffe79a;
  background: #4b3e29;
  border-radius: 3px;
  padding: 2px 4px;
  font-weight: 750;
  font-size: 0.66rem;
  line-height: 1;
  text-shadow: 0 1px 3px var(--deep-water);
  white-space: nowrap;
}
#gold-pickup:empty { display: none; }
#income { color: #afe8c6; }
#dash { min-width: 8ch; color: #d5e5ec; }

#cards {
  display: grid;
  grid-template-columns: repeat(5, minmax(154px, 1fr));
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: thin;
}

@media (min-width: 1280px) {
  #cards {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    overflow-x: visible;
  }
}

.card {
  display: grid;
  min-width: 0;
  grid-template:
    "icon title" 16px
    "icon role" 14px
    "icon count" 12px / 36px minmax(0, 1fr);
  align-items: center;
  gap: 2px 5px;
  padding: 5px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(var(--panel), var(--deep-water));
  color: #ecf4f5;
  font-size: 0.75rem;
  text-align: left;
}

.card-icon { grid-area: icon; width: 100%; aspect-ratio: 1; object-fit: contain; }
.card-title { grid-area: title; display: flex; align-items: center; gap: 4px; font-weight: 700; }
.card-price { color: var(--gold); font-size: 0.74rem; font-weight: 800; }
.card-title, .card-role, .card-count { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-role { grid-area: role; font-size: 0.68rem; color: #dbe6e9; }
.card-count { grid-area: count; font-size: 0.64rem; color: var(--muted); }
.card:enabled:hover { background: var(--panel-hover); }
.card:focus-visible { outline-offset: -2px; }
.card[aria-pressed="true"] { border-color: #9cefb7; box-shadow: inset 0 0 0 1px #9cefb7; background: #244a40; }
.card:disabled { color: #bdced4; background: #192c35; }
.card:disabled .card-icon { opacity: 0.58; }
.card[data-state="not_enough_gold"] .card-price { color: #ffaf9e; }
.card[data-state="limit_exceeded"] .card-count { color: #ffaf9e; font-weight: 700; }

.card kbd {
  display: inline-grid;
  place-items: center;
  min-width: 0.9rem;
  padding: 0 2px;
  border: 1px solid var(--sea-middle);
  border-radius: 3px;
  color: var(--sky-hover);
  background: var(--deep-water);
  font-size: 0.68rem;
  font-weight: 750;
  line-height: 1.2;
}

.hints { position: relative; text-align: center; }
.controls { color: var(--muted); font-size: 0.69rem; line-height: 16px; white-space: nowrap; }
#sound-hint { margin-inline-start: 0.7em; }
#controls-toggle { display: none; }
.telemetry {
  position: absolute;
  right: 0;
  bottom: 2px;
  display: flex;
  gap: 8px;
  color: #9bb5c3;
  font: 0.60rem / 1.2 ui-monospace, monospace;
  white-space: nowrap;
}
#cursor-readout { position: absolute; right: 0; bottom: calc(100% + 2px); background: var(--deep-water); }
#build-info:empty { display: none; }

@media (max-width: 1000px) {
  .hints { text-align: left; }
  #controls-toggle {
    display: block;
    height: var(--hints-height);
    padding: 0 6px;
    border: 1px solid var(--line);
    border-radius: 3px;
    background: var(--panel);
    font-size: 0.68rem;
  }
  .controls {
    display: none;
    position: absolute;
    bottom: calc(var(--build-height) + var(--hints-height) + 2px);
    left: 0;
    max-width: 100%;
    padding: 5px 8px;
    border: 1px solid var(--line);
    border-radius: 5px;
    background: var(--deep-water);
    white-space: normal;
  }
  #controls-toggle[aria-expanded="true"] + .controls { display: block; }
}

@media (max-width: 620px) {
  header { grid-template-columns: minmax(0, 1fr) minmax(0, 54%) minmax(0, 1fr); gap: 4px; }
  #title { font-size: 0.76rem; }
  #opponent, #status, .health label { font-size: 0.62rem; }
  .room-heading, .match-hud { gap: 3px; }
  .health { padding-inline: 3px; }
  #timer { font-size: 1rem; }
  .room-actions button { padding-inline: 4px; font-size: 0.65rem; }
  #copy { min-width: 3em; }
}
