:root {
  --bg: #0b0f14;
  --panel: #131a22;
  --panel-2: #1a2330;
  --border: #263344;
  --text: #dbe4ee;
  --muted: #8296ac;
  --accent: #e5484d;
  --accent-2: #3b82f6;
  --good: #22c55e;
  --warn: #eab308;
  font-size: 15px;
}

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

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 2px solid #7cb3ff;
  outline-offset: 2px;
}
#canvas:focus-visible { outline-offset: -3px; }

kbd {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  font-family: inherit;
}
.kbd-hints { margin-top: 10px; line-height: 2; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; width: 100%; }

/* ---------- screens ---------- */
.screen { display: none; position: fixed; inset: 0; }
.screen.active { display: flex; align-items: center; justify-content: center; }
#screen-menu.active, #screen-lobby.active { overflow-y: auto; padding: 16px 0; }
#screen-menu .menu-box, #screen-lobby .menu-box { margin: auto; }
#screen-game.active {
  display: block;
  user-select: none;
  -webkit-user-select: none;
}

#screen-menu, #screen-lobby {
  background:
    radial-gradient(1200px 700px at 70% -10%, #16324a55, transparent),
    radial-gradient(900px 600px at 10% 110%, #4a161d55, transparent),
    var(--bg);
}

.menu-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px 36px;
  width: min(440px, 92vw);
  box-shadow: 0 20px 60px #0009;
}

.logo {
  font-size: 44px;
  letter-spacing: 4px;
  font-weight: 800;
  text-align: center;
}
.logo span { color: var(--accent); }
.tagline {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin: 6px 0 24px;
}

h2 { margin-bottom: 16px; }

.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }

input, select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 16px; /* >=16px prevents iOS zoom-on-focus */
  outline: none;
}
input:focus, select:focus { border-color: var(--accent-2); }

.btn {
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: filter .12s, transform .05s;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.25); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: transparent; }
.btn.danger { background: #58151c; border-color: #7f2029; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn:disabled { opacity: .45; cursor: not-allowed; filter: none; }

.menu-actions { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: 3px; font-weight: 700; text-align: center; }
.join-row .btn { flex-shrink: 0; }

.error-msg { color: #ff8589; font-size: 13px; min-height: 18px; margin-top: 10px; text-align: center; }
.hint { color: var(--muted); font-size: 12.5px; margin-top: 10px; text-align: center; }

.howto { margin-top: 20px; color: var(--muted); font-size: 13px; }
.howto summary { cursor: pointer; font-weight: 600; }
.howto ul { margin: 8px 0 0 18px; display: grid; gap: 4px; }

/* ---------- lobby ---------- */
.room-code-row { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.room-code-row span { color: var(--muted); font-size: 13px; }
.room-code-row code {
  background: var(--panel-2);
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 22px;
  letter-spacing: 5px;
  font-weight: 800;
  border: 1px dashed var(--border);
}

.player-list { list-style: none; display: grid; gap: 8px; margin-bottom: 18px; }
.player-list li {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-2);
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.player-list .dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.player-list .host-tag { margin-left: auto; font-size: 11px; color: var(--warn); }
.player-list .off { margin-left: auto; font-size: 11px; color: var(--muted); }

.options { display: flex; gap: 14px; align-items: end; margin-bottom: 18px; flex-wrap: wrap; }
.options label { flex: 1; min-width: 90px; }
.options label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.options .checkbox { display: flex; align-items: center; gap: 8px; flex: 0; white-space: nowrap; }
.options .checkbox input { width: auto; }
.options .checkbox span { margin: 0; color: var(--text); }
.options.locked { pointer-events: none; opacity: .6; }

/* ---------- game HUD ---------- */
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#canvas.panning { cursor: grabbing; }

#hud-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 14px;
  padding: calc(10px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 10px calc(14px + env(safe-area-inset-left));
  background: linear-gradient(#0b0f14ee, #0b0f1400);
  pointer-events: none;
}
#hud-top .btn { pointer-events: auto; }
.hud-spacer { flex: 1; }

#hud-players { display: flex; gap: 10px; }
.hud-player {
  display: flex; align-items: center; gap: 7px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 6px 11px;
  border-radius: 8px;
  font-size: 13px;
}
.hud-player.current { border-color: var(--warn); box-shadow: 0 0 8px #eab30833; }
.hud-player.dead { opacity: .4; text-decoration: line-through; }
.hud-player .dot { width: 10px; height: 10px; border-radius: 50%; }
.hud-player .gold { color: var(--warn); font-weight: 700; }
.hud-player .discon { color: var(--muted); font-size: 11px; }

#hud-turn { font-size: 14px; font-weight: 700; color: var(--muted); }

#hud-bottom {
  position: absolute; left: 0; bottom: 0;
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  pointer-events: none;
  font-size: 13px;
}
#tile-info, #unit-info {
  background: #131a22cc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 11px;
  max-width: 320px;
  backdrop-filter: blur(4px);
}
#tile-info:empty, #unit-info:empty { display: none; }
#unit-info b { color: var(--warn); }

/* ---------- popups ---------- */
.popup {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  z-index: 30;
  box-shadow: 0 12px 40px #000a;
  min-width: 130px;
}
.popup.hidden { display: none; }
.popup h3 { font-size: 13px; color: var(--muted); margin: 4px 6px 8px; }
.popup .btn { display: block; width: 100%; text-align: left; margin-bottom: 5px; border: none; }
.popup .btn:last-child { margin-bottom: 0; }
.popup .btn .sub { float: right; color: var(--warn); font-weight: 700; margin-left: 14px; }
.popup .btn .dmg { float: right; color: #ff8589; font-weight: 700; margin-left: 14px; }
.popup .btn.disabled { opacity: .4; pointer-events: none; }

#build-menu { width: 240px; }
#build-list .btn small { display: block; color: var(--muted); font-weight: 400; font-size: 11px; }

/* ---------- chat ---------- */
#chat {
  position: absolute;
  right: calc(12px + env(safe-area-inset-right));
  bottom: calc(12px + env(safe-area-inset-bottom));
  width: 280px;
  display: flex; flex-direction: column;
  background: #131a22cc;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  backdrop-filter: blur(4px);
}
#chat-log {
  max-height: 160px;
  overflow-y: auto;
  padding: 8px 10px;
  font-size: 12.5px;
  display: flex; flex-direction: column; gap: 3px;
}
#chat-log .sys { color: var(--muted); font-style: italic; }
#chat-log b { font-weight: 700; }
#chat-form { border-top: 1px solid var(--border); }
#chat-input { border: none; border-radius: 0; background: transparent; font-size: 13px; }

/* ---------- toasts & banner ---------- */
#toasts {
  position: absolute; top: 64px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px;
  z-index: 40; pointer-events: none;
}
.toast {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13.5px;
  animation: toast-in .18s ease-out;
  box-shadow: 0 8px 30px #000a;
}
.toast.error { border-color: #7f2029; color: #ff8589; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

#banner {
  position: absolute; top: 38%; left: 0; right: 0;
  text-align: center;
  font-size: 42px;
  font-weight: 800;
  letter-spacing: 6px;
  text-shadow: 0 4px 30px #000;
  z-index: 35;
  pointer-events: none;
  animation: banner-pop .5s ease-out;
}
#banner.hidden { display: none; }
@keyframes banner-pop {
  0% { opacity: 0; transform: scale(.8); }
  60% { opacity: 1; transform: scale(1.06); }
  100% { transform: scale(1); }
}

.overlay {
  position: absolute; inset: 0;
  background: #0b0f14cc;
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  backdrop-filter: blur(3px);
}
.overlay.hidden { display: none; }
#gameover .menu-box { text-align: center; }
#gameover p { color: var(--muted); margin: 10px 0 22px; }

/* ---------- mobile ---------- */
@media (pointer: coarse) {
  .btn.small { padding: 9px 13px; font-size: 13.5px; }
  .popup .btn { padding: 11px 12px; margin-bottom: 6px; }
  #canvas { cursor: default; }
}

@media (max-width: 720px) {
  :root { font-size: 14px; }
  .logo { font-size: 34px; }
  .menu-box { padding: 24px 20px; }

  #hud-top { gap: 8px; flex-wrap: wrap; padding-top: calc(6px + env(safe-area-inset-top)); }
  #hud-players { gap: 6px; flex-wrap: wrap; }
  .hud-player { padding: 4px 8px; font-size: 11.5px; gap: 5px; }
  #hud-turn { font-size: 12px; }

  #tile-info, #unit-info { max-width: 52vw; font-size: 12px; padding: 5px 9px; }

  #chat { width: min(240px, 44vw); }
  #chat-log { max-height: 84px; font-size: 12px; }

  #build-menu { width: min(260px, calc(100vw - 16px)); }
  #banner { font-size: 26px; letter-spacing: 3px; }
  #toasts { top: 96px; max-width: 92vw; }
  .toast { font-size: 12.5px; }
}

/* Short landscape phones: tighten vertical chrome. */
@media (max-height: 450px) {
  #chat-log { max-height: 60px; }
  #toasts { top: 52px; }
  .menu-box { padding: 16px 20px; }
}
