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

:root {
  --bg: #14160f;
  --panel: #221f18;
  --panel2: #2c2820;
  --ink: #e8e0cc;
  --ink-dim: #a89f88;
  --accent: #c94f38;
  --accent2: #7a8a52;
  --line: #453f32;
  --ocean: #1d2a33;
  --sea-tile: #27394a;
  --radius: 10px;
}

html, body { height: 100%; overflow: hidden; }
body {
  font-family: Georgia, 'Times New Roman', serif;
  background: var(--bg);
  color: var(--ink);
  touch-action: manipulation;
}

.hidden { display: none !important; }
.screen { position: fixed; inset: 0; display: flex; flex-direction: column; overflow: hidden; }

button { font-family: inherit; cursor: pointer; }
input {
  font-family: inherit; font-size: 18px; padding: 12px 14px; width: 100%;
  background: var(--panel2); color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius);
}
input::placeholder { color: var(--ink-dim); }

.btn {
  font-size: 17px; padding: 13px 18px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--panel2); color: var(--ink);
  letter-spacing: .04em;
}
.btn.primary { background: var(--accent); border-color: #8a3626; color: #fff4e4; font-weight: bold; }
.btn.subtle { background: transparent; color: var(--ink-dim); border: none; text-decoration: underline; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; }

/* ---------- HOME ---------- */
#screen-home { background: var(--bg); }
.hero { flex: 0 0 38vh; overflow: hidden; position: relative; }
.hero img { width: 100%; height: 100%; object-fit: cover; display: block; }
.hero::after { content: ''; position: absolute; inset: 0; background: linear-gradient(to bottom, transparent 55%, var(--bg)); }
.home-body { flex: 1; padding: 8px 28px calc(24px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 14px; max-width: 440px; width: 100%; margin: 0 auto; }
h1 { font-size: clamp(30px, 9vw, 44px); letter-spacing: .06em; text-align: center; color: #e8dcbc; text-shadow: 0 2px 0 #000; }
.tagline { text-align: center; color: var(--ink-dim); font-style: italic; margin-top: -8px; }
.join-row { display: flex; gap: 10px; }
.join-row input { width: 40%; text-transform: uppercase; text-align: center; letter-spacing: .3em; }
.join-row .btn { flex: 1; }

/* ---------- BARS / LOBBY ---------- */
.bar {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.room-code { font-size: 24px; letter-spacing: .35em; background: var(--panel2); border: 1px dashed var(--ink-dim); padding: 4px 8px 4px 14px; border-radius: 8px; }
.lobby-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; max-width: 520px; width: 100%; margin: 0 auto; }
.hint { color: var(--ink-dim); font-size: 14px; font-style: italic; }

.qr-box { text-align: center; padding: 6px 0 2px; }
.qr-box img { width: 150px; height: 150px; border-radius: 10px; border: 3px solid var(--line); background: #f2ead2; }
.team-btns { display: flex; gap: 10px; }
.team-btns .btn { flex: 1; }
#btn-fill-ai { border-style: dashed; color: var(--ink-dim); }

.power-row {
  display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--line); font-size: 17px;
}
.power-row .ai-btn {
  width: 42px; height: 38px; font-size: 18px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel2);
}
.power-row .ai-btn.on { background: #2c4a3a; border-color: #4c7a5a; }
.power-row .dot { width: 22px; height: 22px; border-radius: 50%; flex: 0 0 auto; border: 2px solid #0007; }
.power-row .who { margin-left: auto; color: var(--ink-dim); font-size: 14px; }
.power-row.mine { border-color: var(--accent); }
.power-row .team-tag { font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: var(--ink-dim); }
.player-row { padding: 8px 12px; color: var(--ink); }
.player-row .off { color: var(--ink-dim); font-style: italic; }

/* ---------- GAME ---------- */
#screen-game { background: var(--ocean); }
.game-bar { gap: 8px; }
.turn-chip { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: bold; white-space: nowrap; }
.turn-chip .dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid #0007; }
.phase-name { font-size: 13px; color: var(--ink-dim); text-transform: uppercase; letter-spacing: .12em; text-align: center; flex: 1; }
.bar-right { display: flex; align-items: center; gap: 8px; }
.ipc-chip { font-size: 15px; background: var(--panel2); padding: 4px 10px; border-radius: 20px; border: 1px solid var(--line); white-space: nowrap; }
.icon-btn { background: none; border: none; color: var(--ink); font-size: 22px; padding: 4px 8px; }

#map-wrap { flex: 1; position: relative; overflow: hidden; min-height: 0; }
/* absolute positioning (not percentage height) so the svg can never collapse
   to zero height on browsers with flex/percentage quirks */
#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block; background: var(--ocean); touch-action: none; }
#btn-recenter {
  position: absolute; left: 12px; bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 15;
  width: 44px; height: 44px; border-radius: 50%; font-size: 20px;
  background: var(--panel2); color: var(--ink); border: 1px solid var(--line);
  box-shadow: 0 4px 14px #000a;
}

.ocean-tint { fill: #35566b; opacity: .52; pointer-events: none; }
.land-shadow { fill: #3c2f1c; opacity: .28; pointer-events: none; }
.landmass {
  fill: #cdb98b; fill-opacity: .92; stroke: #6d5b3c; stroke-width: 1;
  stroke-linejoin: round; pointer-events: none;
}
.grid-line { stroke: #00000018; stroke-width: 0.7; pointer-events: none; }
.borders {
  fill: none; stroke: #7a684a; stroke-width: 0.6;
  stroke-dasharray: 2.5 1.8; opacity: .75; pointer-events: none;
}
.compass { color: #2e2a20; stroke: currentColor; fill: currentColor; opacity: .45; pointer-events: none; stroke-width: 1; }
.compass-n { font-size: 11px; font-family: Georgia, serif; stroke: none; }

.terr-tile { stroke: #0007; stroke-width: 0.8; }
.sea-hit { fill: #ffffff; fill-opacity: 0.001; }
.sea-name {
  font-size: 8px; font-style: italic; fill: #f0ead6; font-family: Georgia, serif;
  paint-order: stroke; stroke: #2a4051; stroke-width: 2px;
  pointer-events: none; letter-spacing: .05em;
}
.terr-name { font-size: 7.5px; fill: #f2ead2; paint-order: stroke; stroke: #0009; stroke-width: 1.8px; font-weight: bold; pointer-events: none; }
.terr-ipc { font-size: 6px; fill: #ffe9a8; paint-order: stroke; stroke: #0008; stroke-width: 1.6px; pointer-events: none; }
.capital-star { fill: #ffd76a; stroke: #6b5210; stroke-width: .8; pointer-events: none; }
.unit-badge-bg { fill: #10120c; opacity: .82; }
.unit-badge-text { font-size: 7px; fill: #fff; paint-order: stroke; stroke: #000a; stroke-width: 1.4px; pointer-events: none; font-family: Verdana, sans-serif; }
.factory-mark { fill: #cfc5a8; pointer-events: none; }
.tile-hl { fill: none; stroke: #ffd76a; stroke-width: 2.5; rx: 8; pointer-events: none; animation: pulse 1.2s infinite; }
.tile-hl.reach { stroke: #1d6fd1; }
.tile-hl.battle { stroke: #ff5f45; }
@keyframes pulse { 50% { opacity: .45; } }
.adj-line { stroke: #ffd76a55; stroke-width: 1.5; pointer-events: none; }

#battle-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  background: #5c1f14ee; color: #ffe9d9; border: 1px solid #a34; padding: 8px 16px;
  border-radius: 24px; font-size: 14px; white-space: nowrap;
}
#target-hint {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  background: #123c52ee; color: #d8efff; border: 1px solid #4a8ab0; padding: 8px 16px;
  border-radius: 24px; font-size: 14px; white-space: nowrap;
}

/* bottom sheet */
#sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--panel); border-top: 2px solid var(--line);
  border-radius: 18px 18px 0 0; max-height: 52vh; overflow-y: auto;
  padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px #000a;
}
.sheet-grab { width: 44px; height: 4px; background: var(--line); border-radius: 2px; margin: 6px auto 10px; }
.sheet-title { display: flex; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.sheet-title h3 { font-size: 20px; }
.sheet-title .sub { color: var(--ink-dim); font-size: 13px; }
.owner-line { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink-dim); margin-bottom: 10px; }
.owner-line .dot { width: 12px; height: 12px; border-radius: 50%; }

.unit-group { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.unit-group svg { width: 30px; height: 30px; flex: 0 0 auto; }
.unit-group .ug-name { flex: 1; font-size: 15px; }
.unit-group .ug-name small { color: var(--ink-dim); display: block; font-size: 12px; }
.stepper { display: flex; align-items: center; gap: 2px; }
.stepper button {
  width: 38px; height: 38px; font-size: 20px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel2); color: var(--ink);
}
.stepper .count { width: 34px; text-align: center; font-size: 17px; font-family: Verdana, sans-serif; }
.sheet-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1; min-width: 120px; }

.phase-btn {
  position: absolute; right: 12px; bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 15;
  background: var(--accent2); border-color: #4c5a31; color: #f4f8e6; font-weight: bold;
  box-shadow: 0 4px 14px #000a;
}

/* modals */
.modal { position: fixed; inset: 0; background: #000a; z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.modal-card {
  background: var(--panel); width: 100%; max-width: 520px; max-height: 88vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  border: 1px solid var(--line);
}
.modal-card h3 { margin-bottom: 12px; font-size: 20px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

.buy-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
.buy-row svg { width: 32px; height: 32px; }
.buy-row .b-name { flex: 1; font-size: 15px; }
.buy-row .b-name small { display: block; color: var(--ink-dim); font-size: 12px; }
.buy-total { margin-top: 10px; text-align: right; font-size: 17px; color: #ffe9a8; }

/* battle */
.battle-card { max-width: 560px; }
.battle-sub { display: block; font-size: 13px; font-weight: normal; color: var(--ink-dim); margin-top: 2px; }
.help-body p { margin-bottom: 10px; font-size: 15px; }
.help-phases { margin: 0 0 10px 20px; font-size: 15px; }
.help-phases li { margin-bottom: 8px; }
.battle-sides { display: flex; gap: 8px; align-items: flex-start; }
.battle-side { flex: 1; background: var(--panel2); border-radius: 10px; padding: 10px; min-height: 70px; }
.battle-side h4 { font-size: 13px; margin-bottom: 8px; text-transform: uppercase; letter-spacing: .08em; }
.battle-vs { align-self: center; color: var(--accent); font-weight: bold; }
.bunit { display: inline-flex; align-items: center; gap: 3px; margin: 2px 6px 2px 0; font-family: Verdana, sans-serif; font-size: 13px; }
.bunit svg { width: 24px; height: 24px; }
#battle-log { margin-top: 12px; max-height: 30vh; overflow-y: auto; font-size: 14px; }
.round-block { border-top: 1px solid var(--line); padding: 8px 0; }
.dice-row { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; align-items: center; }
.dice-label { font-size: 12px; color: var(--ink-dim); width: 100%; }
.die {
  width: 26px; height: 26px; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center;
  font-family: Verdana, sans-serif; font-size: 14px; font-weight: bold;
  background: #e8e0cc; color: #222;
}
.die.hit { background: var(--accent); color: #fff; }
.losses { font-size: 13px; color: #ff9d8a; }

/* log drawer */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(85vw, 360px); z-index: 45;
  background: var(--panel); border-left: 1px solid var(--line); padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top)); overflow-y: auto;
  box-shadow: -10px 0 30px #000a;
}
.drawer h3 { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
#btn-help, #btn-device { width: 100%; margin-bottom: 10px; }
.device-link { word-break: break-all; font-size: 13px; color: var(--ink-dim); background: var(--panel2); padding: 8px 10px; border-radius: 8px; margin-top: 10px; }
#log-list { font-size: 14px; display: flex; flex-direction: column-reverse; gap: 6px; }
#log-list div { padding: 6px 8px; background: var(--panel2); border-radius: 6px; }
.drawer-players { margin: 14px 0; font-size: 14px; color: var(--ink-dim); }

#winner-overlay {
  position: fixed; inset: 0; z-index: 60; background: #000c; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 16px; text-align: center; padding: 24px;
}
#winner-overlay h1 { font-size: 40px; }
#winner-overlay p { color: var(--ink-dim); }

#toast {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: #402020; color: #ffd9cd; border: 1px solid #7a3a2a; padding: 10px 18px;
  border-radius: 24px; font-size: 15px; z-index: 70; max-width: 88vw; text-align: center;
  animation: toastin .2s;
}
#toast.info { background: #20301c; color: #e2f0d0; border-color: #4c6a3a; }
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 8px); } }

/* landscape phones / tablets */
@media (min-width: 700px) {
  .modal { align-items: center; }
  .modal-card { border-radius: 18px; }
  #sheet { left: auto; width: 400px; border-radius: 18px 0 0 0; }
}
