:root {
  color-scheme: dark;
  --ink: #101918;
  --panel: rgba(20, 31, 29, .94);
  --paper: #f7f2e8;
  --muted: #b8c5bf;
  --line: rgba(247, 242, 232, .18);
  --red: #e94b48;
  --teal: #178c78;
  --gold: #e8b23b;
  --green: #5e815f;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  min-width: 320px;
  margin: 0;
  overflow: hidden;
  background: var(--ink);
  overscroll-behavior: none;
}

body,
button,
canvas,
a {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button,
input {
  font: inherit;
  letter-spacing: 0;
}

button,
a {
  color: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.game-app {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 480px;
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr) 70px;
  background: #152523;
}

.topbar {
  z-index: 20;
  min-width: 0;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  align-items: center;
  gap: 11px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: #121b1a;
}

.back-link,
.icon-button {
  width: 38px;
  height: 38px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  text-decoration: none;
  font-size: 1.1rem;
  font-weight: 850;
}

.brand {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand > span:last-child {
  min-width: 0;
  display: grid;
}

.brand strong {
  overflow: hidden;
  font-family: Georgia, serif;
  font-size: clamp(.82rem, 2.4vw, 1.04rem);
  line-height: 1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand small {
  margin-top: 3px;
  color: var(--gold);
  font-size: .58rem;
  font-weight: 900;
}

.flag-mark {
  position: relative;
  flex: 0 0 auto;
  width: 30px;
  height: 22px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 2px;
  background: #f6f3eb;
}

.flag-mark::before,
.flag-mark::after,
.flag-mark i::before,
.flag-mark i::after {
  content: "";
  position: absolute;
}

.flag-mark::before {
  inset: 0 auto 0 9px;
  width: 7px;
  background: #19558a;
}

.flag-mark::after {
  inset: 7px 0 auto;
  height: 7px;
  background: #19558a;
}

.flag-mark i::before {
  z-index: 1;
  inset: 0 auto 0 11px;
  width: 3px;
  background: #d73543;
}

.flag-mark i::after {
  z-index: 1;
  inset: 9px 0 auto;
  height: 3px;
  background: #d73543;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 9px;
}

.connection-state {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: .56rem;
}

.connection-state i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8c9892;
}

.connection-state[data-state="online"] i {
  background: #55d392;
  box-shadow: 0 0 12px rgba(85, 211, 146, .62);
}

.connection-state[data-state="connecting"] i {
  background: var(--gold);
  animation: pulse 1s infinite;
}

.battle-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  isolation: isolate;
  touch-action: none;
  background:
    linear-gradient(rgba(11, 24, 25, .16), rgba(6, 14, 15, .52)),
    url("assets/faroese-sea.png") center / cover;
}

#battleCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.map-vignette {
  pointer-events: none;
  position: absolute;
  inset: 0;
  z-index: 2;
  box-shadow: inset 0 0 110px rgba(1, 8, 9, .62);
}

.score-panel {
  position: absolute;
  z-index: 5;
  top: 12px;
  left: 12px;
  width: min(310px, calc(100% - 24px));
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(14, 24, 23, .88);
  box-shadow: 0 12px 32px rgba(0,0,0,.28);
  backdrop-filter: blur(10px);
}

.battle-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: .62rem;
  font-weight: 850;
}

.battle-meta time {
  color: var(--paper);
  font-variant-numeric: tabular-nums;
}

.faction-rows {
  display: grid;
  gap: 6px;
}

.faction-row {
  display: grid;
  grid-template-columns: 9px minmax(74px, auto) 1fr auto;
  align-items: center;
  gap: 7px;
  min-width: 0;
  font-size: .65rem;
}

.faction-row > i {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--faction);
}

.faction-name {
  overflow: hidden;
  font-weight: 820;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.faction-name small {
  margin-left: 4px;
  padding: 2px 4px;
  border: 1px solid var(--line);
  border-radius: 3px;
  color: var(--muted);
  font-size: .47rem;
}

.faction-meter {
  height: 7px;
  overflow: hidden;
  border-radius: 2px;
  background: rgba(255,255,255,.1);
}

.faction-meter i {
  display: block;
  width: var(--share);
  height: 100%;
  background: var(--faction);
  transition: width .25s ease;
}

.faction-value {
  min-width: 34px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.room-chip {
  position: absolute;
  z-index: 5;
  top: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 7px 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(14, 24, 23, .88);
  color: var(--muted);
  font-size: .56rem;
  font-weight: 900;
}

.room-chip button {
  padding: 4px 6px;
  border-radius: 3px;
  background: rgba(255,255,255,.09);
  font-size: .68rem;
  font-weight: 900;
}

.drag-hint {
  pointer-events: none;
  position: absolute;
  z-index: 6;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  padding: 7px 10px;
  border: 1px solid rgba(255,255,255,.24);
  border-radius: 4px;
  background: rgba(10, 18, 17, .88);
  color: var(--gold);
  font-size: .58rem;
  font-weight: 900;
}

.toast {
  pointer-events: none;
  position: absolute;
  z-index: 12;
  left: 50%;
  bottom: 22px;
  max-width: min(360px, calc(100% - 24px));
  transform: translate(-50%, 14px);
  padding: 9px 12px;
  border-radius: 5px;
  background: rgba(12, 20, 19, .94);
  color: var(--paper);
  font-size: .68rem;
  opacity: 0;
  transition: opacity .18s ease, transform .18s ease;
}

.toast.visible {
  transform: translate(-50%, 0);
  opacity: 1;
}

.command-dock {
  z-index: 20;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(45px, 1fr) auto minmax(70px, 1fr);
  align-items: center;
  gap: 12px;
  padding: 8px max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: #121b1a;
}

.command-dock > span {
  color: var(--muted);
  font-size: .58rem;
  font-weight: 900;
}

.command-dock > span:last-child {
  text-align: right;
}

.ratio-control {
  height: 42px;
  display: grid;
  grid-template-columns: repeat(3, 70px);
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255,255,255,.04);
}

.ratio-button {
  border-radius: 3px;
  background: transparent;
  color: var(--muted);
  font-size: .7rem;
  font-weight: 850;
}

.ratio-button.selected {
  background: var(--paper);
  color: var(--ink);
}

dialog {
  max-width: none;
  max-height: none;
  padding: 0;
  border: 0;
  color: var(--paper);
  background: transparent;
}

dialog::backdrop {
  background: rgba(5, 11, 11, .8);
  backdrop-filter: blur(8px);
}

.setup-dialog::backdrop {
  background: rgba(5, 11, 11, .52);
  backdrop-filter: blur(2px);
}

.setup-dialog,
.lobby-dialog,
.result-dialog {
  width: min(520px, calc(100% - 24px));
}

.setup-dialog form,
.lobby-dialog > section,
.result-dialog > section {
  max-height: calc(100svh - 24px);
  overflow-y: auto;
  padding: 24px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 8px;
  background: #172321;
  box-shadow: 0 24px 80px rgba(0,0,0,.5);
}

dialog header h1,
dialog header h2 {
  margin: 5px 0 9px;
  font-family: Georgia, serif;
  font-weight: 600;
  line-height: 1.04;
}

dialog header h1 {
  font-size: 2rem;
}

dialog header h2,
.result-dialog h2 {
  font-size: 1.8rem;
}

dialog header p,
.result-dialog p {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.5;
}

.eyebrow,
.field-label {
  color: var(--gold);
  font-size: .58rem;
  font-weight: 900;
}

.field-label {
  display: block;
  margin: 13px 0 6px;
}

input {
  width: 100%;
  height: 46px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 5px;
  outline: none;
  background: rgba(255,255,255,.06);
  color: var(--paper);
}

input:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(232, 178, 59, .13);
}

.primary-actions {
  display: grid;
  gap: 9px;
  margin-top: 14px;
}

.primary-button,
.secondary-button,
.join-button {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  text-decoration: none;
  font-size: .68rem;
  font-weight: 900;
}

.primary-button {
  flex-direction: column;
  background: var(--red);
  color: white;
}

.primary-button small,
.secondary-button small {
  margin-top: 3px;
  color: rgba(255,255,255,.74);
  font-size: .55rem;
  font-weight: 650;
}

.secondary-button {
  flex-direction: column;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.05);
}

.join-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 0;
  color: var(--muted);
  font-size: .54rem;
}

.join-divider::before,
.join-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

.join-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  gap: 8px;
}

.join-row .field-label {
  grid-column: 1 / -1;
}

.join-row input {
  text-transform: uppercase;
  font-weight: 900;
  letter-spacing: .12em;
}

.join-button {
  background: var(--gold);
  color: #231a08;
}

.form-error {
  min-height: 18px;
  margin: 8px 0 0;
  color: #ff8f8c;
  font-size: .68rem;
}

.short-rule {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: .66rem;
  line-height: 1.45;
}

.large-room-code {
  width: 100%;
  min-height: 72px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255,255,255,.05);
  color: var(--paper);
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: .16em;
}

.share-actions,
.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.lobby-players {
  display: grid;
  gap: 7px;
  margin: 16px 0 12px;
}

.lobby-player {
  min-height: 44px;
  display: grid;
  grid-template-columns: 10px 1fr auto;
  align-items: center;
  gap: 9px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: rgba(255,255,255,.035);
  font-size: .7rem;
}

.lobby-player > i {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  background: var(--faction);
}

.lobby-player small {
  color: var(--muted);
  font-size: .54rem;
  font-weight: 850;
}

.lobby-player.empty {
  color: var(--muted);
}

.wait-meter {
  height: 5px;
  overflow: hidden;
  margin-bottom: 12px;
  border-radius: 2px;
  background: rgba(255,255,255,.08);
}

.wait-meter i {
  display: block;
  width: 0;
  height: 100%;
  background: var(--gold);
  transition: width .25s linear;
}

#startCpuButton {
  width: 100%;
}

.text-button {
  width: 100%;
  margin-top: 12px;
  padding: 8px;
  background: transparent;
  color: var(--muted);
  font-size: .62rem;
  font-weight: 850;
}

.result-standings {
  display: grid;
  gap: 7px;
  margin: 14px 0;
}

.result-standing {
  display: grid;
  grid-template-columns: 28px 10px 1fr auto;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 9px;
  border-bottom: 1px solid var(--line);
  font-size: .7rem;
}

.result-standing > i {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  background: var(--faction);
}

.result-standing b:last-child {
  font-variant-numeric: tabular-nums;
}

button:disabled {
  cursor: wait;
  opacity: .55;
}

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

@keyframes pulse {
  50% { opacity: .35; }
}

@media (hover: hover) {
  button:hover,
  .back-link:hover,
  .secondary-button:hover {
    border-color: rgba(255,255,255,.38);
    filter: brightness(1.08);
  }
}

@media (max-width: 720px) {
  .game-app {
    grid-template-rows: max(58px, calc(50px + env(safe-area-inset-top))) minmax(0, 1fr) max(68px, calc(60px + env(safe-area-inset-bottom)));
  }

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

  .brand .flag-mark,
  .connection-state b {
    display: none;
  }

  .score-panel {
    top: 8px;
    left: 8px;
    width: min(270px, calc(100% - 70px));
    padding: 8px 9px;
  }

  .faction-row {
    grid-template-columns: 8px minmax(62px, 84px) 1fr 30px;
    gap: 5px;
    font-size: .59rem;
  }

  .room-chip {
    top: 8px;
    right: 8px;
    padding-left: 7px;
  }

  .room-chip > span {
    display: none;
  }

  .command-dock {
    grid-template-columns: 1fr;
    place-items: center;
    padding-top: 6px;
  }

  .command-dock > span {
    display: none;
  }

  .ratio-control {
    width: min(290px, calc(100vw - 28px));
    grid-template-columns: repeat(3, 1fr);
  }

  .setup-dialog form,
  .lobby-dialog > section,
  .result-dialog > section {
    padding: 19px;
  }

  dialog header h1 {
    font-size: 1.65rem;
  }
}

@media (max-height: 620px) and (orientation: landscape) {
  .game-app {
    grid-template-rows: 48px minmax(0, 1fr) 52px;
  }

  .topbar {
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .back-link,
  .icon-button {
    width: 34px;
    height: 34px;
  }

  .score-panel {
    width: 260px;
  }

  .ratio-control {
    height: 38px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}
