:root {
  --bg: #0e1420;
  --panel: #172133;
  --panel-2: #1e2b42;
  --line: #2b3a55;
  --text: #e8eef8;
  --muted: #8fa3c0;
  --accent: #4cc9f0;
  --gold: #ffd166;
  --good: #43d17a;
  --bad: #ff6b6b;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at 20% 0%, #16233a, var(--bg) 60%);
  color: var(--text);
  overflow: hidden;              /* the page itself never scrolls */
}

.hidden { display: none !important; }

/* ---------- generic screens (join / voting / finished) ---------- */
.screen {
  height: 100vh;
  overflow-y: auto;
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

h1 { font-size: 30px; margin: 8px 0; }
h1 span { color: var(--accent); }
h2 { font-size: 20px; color: var(--muted); }
h3 { margin: 0 0 8px; font-size: 15px; }
h3 small { color: var(--muted); font-weight: normal; font-size: 11px; }

.tagline { color: var(--muted); margin-top: 0; }

.field { display: block; color: var(--muted); margin: 18px 0; }
input {
  display: block; width: 100%; max-width: 360px; margin-top: 6px;
  padding: 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font-size: 15px;
}

/* ---- country pick ---- */
.country-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; max-width: 720px; }
.country-card {
  background: var(--panel); border: 2px solid var(--line); border-radius: 12px;
  padding: 16px; text-align: center; cursor: pointer; transition: transform .1s, border-color .1s;
}
.country-card:hover { transform: translateY(-3px); border-color: var(--accent); }
.country-card .flag { font-size: 40px; }
.country-card .cname { font-weight: bold; margin-top: 6px; }
.country-card.taken { opacity: .4; cursor: not-allowed; }

/* ---- voting ---- */
.vote-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; max-width: 800px; margin: 20px 0; }
.vote-btn {
  padding: 18px; font-size: 20px; font-weight: bold; cursor: pointer;
  background: var(--panel); color: var(--text); border: 2px solid var(--line); border-radius: 12px;
}
.vote-btn:hover { border-color: var(--accent); }
.vote-btn.chosen { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(255,209,102,.25); }
.vote-btn small { display: block; font-size: 12px; color: var(--muted); font-weight: normal; }

.big-btn {
  padding: 14px 26px; font-size: 18px; font-weight: bold; cursor: pointer;
  background: var(--accent); color: #04121c; border: none; border-radius: 12px;
}
.big-btn.secondary { background: #2b3a55; color: var(--text); margin-top: 18px; }
.big-btn.secondary:hover { background: #35486a; }

/* ============================================================
   THE GAME SCREEN  (fills the window, no page scrolling)
   ============================================================ */
#screen-game {
  max-width: 1700px;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 10px 14px;
}

/* ---- top bar ---- */
.topbar {
  flex: none;
  display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; justify-content: space-between;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 14px; margin-bottom: 10px;
}
.phase-title { font-size: 17px; font-weight: bold; color: var(--gold); }
.timer { font-size: 22px; font-weight: bold; font-variant-numeric: tabular-nums; line-height: 1; }
.century { color: var(--muted); font-size: 12px; }
.pause-btn {
  margin-top: 5px; padding: 4px 14px; font-size: 13px; font-weight: bold;
  cursor: pointer; border: none; border-radius: 8px;
  background: var(--gold); color: #04121c;
}
.resources { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  padding: 5px 9px; font-size: 12.5px; white-space: nowrap;
}
.chip b { color: var(--gold); }

/* ---- main grid: cities top-left, merged tall-left, map + players double-height, chat 2 cols ---- */
.grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1.3fr 1fr;
  grid-template-rows: 1.25fr 1fr 0.75fr;
  grid-template-areas:
    "cities map players"
    "merged map players"
    "merged chat chat";
  gap: 10px;
}
.cities-panel  { grid-area: cities; }
.map-panel     { grid-area: map; }
.players-panel { grid-area: players; }
.merged-panel  { grid-area: merged; }
.chat-box      { grid-area: chat; }

.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px;
  display: flex; flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.panel-scroll { flex: 1; min-height: 0; overflow-y: auto; }
/* thin scrollbars so panels look tidy */
.panel-scroll::-webkit-scrollbar, .chat-log::-webkit-scrollbar { width: 8px; }
.panel-scroll::-webkit-scrollbar-thumb, .chat-log::-webkit-scrollbar-thumb { background: #33456a; border-radius: 8px; }

/* ---- my cities ---- */
.cities-panel #my-cities { flex: 1; }
.city {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  padding: 7px 9px; margin-bottom: 6px;
}
.city-head { display: flex; justify-content: space-between; align-items: baseline; }
.city-name { font-weight: bold; font-size: 13.5px; }
.city-stats { color: var(--muted); font-size: 11.5px; margin: 3px 0 5px; }
.build-row { display: flex; flex-wrap: wrap; gap: 4px; }
.mini {
  background: #24344f; border: 1px solid var(--line); color: var(--text);
  border-radius: 7px; padding: 3px 6px; font-size: 11px; line-height: 1.3; cursor: pointer;
}
.mini small { color: var(--muted); font-size: 10px; }
.mini:hover { border-color: var(--accent); }

/* the war note now lives at the bottom of the cities card */
.war-note { flex: none; padding-top: 8px; margin-top: 4px; border-top: 1px solid var(--line); font-size: 12px; }
.war-note p { margin: 0; }

/* ---- world map (countries pinned at their real places) ---- */
.map-panel #map { flex: 1; }
.map {
  position: relative;
  min-height: 0;
  overflow: hidden;
  container-type: size;
  display: grid;
  place-items: center;
}
.map-canvas { position: relative; width: 100%; height: 100%; }
/* the fade real Earth map, behind the pins */
.map-canvas::before {
  content: "";
  position: absolute; inset: 0;
  background: url("/world-map.svg") center / contain no-repeat;
  opacity: .5;
  filter: brightness(1.05) sepia(1) hue-rotate(165deg) saturate(3);
  pointer-events: none;
}
/* when the browser supports container queries, size the canvas exactly to the image */
@supports (width: 1cqw) {
  .map-canvas {
    width: min(100cqw, calc(100cqh * 950 / 620));
    height: min(100cqh, calc(100cqw * 620 / 950));
  }
  .map-canvas::before { background-size: 100% 100%; }
}

.map-lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.pin-dot {
  position: absolute; width: 9px; height: 9px; border-radius: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, .55);
  z-index: 2;
}
.pin-label {
  position: absolute; transform: translate(-50%, -50%);
  background: rgba(18, 26, 41, .94);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 3px 6px; z-index: 3; cursor: default;
}
.pl-title { font-size: 10px; font-weight: bold; white-space: nowrap; }
.pl-title .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 4px; }
.pl-cities { display: none; flex-direction: column; gap: 1px; margin-top: 3px; }
.pin-label:hover { z-index: 10; }
.pin-label:hover .pl-cities { display: flex; }
.ci { font-size: 9.5px; border-left: 3px solid var(--line); padding-left: 4px; white-space: nowrap; }
.ci small { color: var(--muted); }
.ci.mine { color: var(--good); }

/* ---- the war screen overlay ---- */
.grid { position: relative; }
.war-overlay {
  position: absolute; inset: 0; z-index: 20;
  background: linear-gradient(180deg, #2a0f14, #12060a 65%);
  border: 2px solid #7f1d1d; border-radius: 14px;
  padding: 14px 18px;
  display: flex; flex-direction: column; gap: 8px;
  box-shadow: inset 0 0 70px rgba(220, 40, 40, .18);
}
.war-header { display: flex; justify-content: space-between; align-items: baseline; }
.war-title { font-size: 22px; font-weight: bold; color: #ffb4a2; letter-spacing: .5px; }
.war-timer { font-size: 26px; font-weight: bold; font-variant-numeric: tabular-nums; color: #ffd166; }
.war-subtitle { margin: 0; color: #e3b7ae; font-size: 13px; }
.war-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1.25fr; gap: 14px; }
.war-col { display: flex; flex-direction: column; min-height: 0; }
.war-col h3 { margin: 0 0 8px; }
.war-targets {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px; align-content: start;
}
.war-battles { flex: 1; min-height: 0; overflow-y: auto; }

.war-target {
  display: flex; flex-direction: column; gap: 2px; text-align: left;
  background: #2a1418; border: 2px solid var(--line); border-radius: 10px;
  padding: 8px 10px; cursor: pointer; color: var(--text); font-size: 13px;
}
.war-target:hover { background: #3a1a20; }
.war-target.chosen { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(255, 209, 102, .3); }
.war-target .wt-name { font-weight: bold; }
.war-target .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.war-target small { color: var(--muted); }

.battle-card {
  background: #1e1114; border: 1px solid #5a2530; border-radius: 10px;
  padding: 9px 11px; margin-bottom: 8px;
}
.battle-card.mine { border-color: var(--gold); }
.battle-card.captured { animation: battleFlash .5s ease-in-out 4; }
@keyframes battleFlash { 0%, 100% { background: #1e1114; } 50% { background: #5a1010; } }
.battle-title { font-weight: bold; font-size: 13px; margin-bottom: 6px; }
.battle-title small { color: var(--muted); font-weight: normal; }
.battle-title .arrow { color: var(--gold); }
.bar-row { display: flex; align-items: center; gap: 8px; font-size: 12px; margin: 3px 0; }
.bar-row > span { width: 58px; }
.bar { flex: 1; height: 10px; background: #2a1a1e; border-radius: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; }
.bar i.atk { background: linear-gradient(90deg, #f97316, #ef4444); }
.bar i.def { background: linear-gradient(90deg, #38bdf8, #2563eb); }
.battle-result { margin-top: 6px; font-weight: bold; font-size: 12.5px; }
.battle-result.win { color: #ffb703; }
.battle-result.lose { color: #7fb2d9; }
.muted { color: var(--muted); }

/* ---- players / news ---- */
.players-panel #players { flex: 1.1 1 0; }
.players-panel #events { flex: 1 1 0; }
.player-row {
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
  padding: 3px 0; border-bottom: 1px solid var(--line); font-size: 12.5px;
}
.player-row.dead { opacity: .45; text-decoration: line-through; }
.player-row .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.events { font-size: 12px; color: var(--muted); }
.events div { padding: 3px 0; border-bottom: 1px dashed var(--line); }

/* ---- merged card: knowledge + troops + materials (tall, left) ---- */
.merged-panel { padding: 10px 12px; }
.merged-scroll {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; justify-content: space-evenly; gap: 8px;
}
.merged-section { }
.merged-section h3 { margin: 0 0 8px; }
.merged-items { display: flex; flex-wrap: wrap; gap: 5px; }

/* ---- chat (2 columns, bottom) ---- */
.chat-box {
  display: flex; gap: 10px;
  min-height: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
}
.chat-log { flex: 1; min-height: 0; overflow-y: auto; font-size: 12.5px; }
.chat-log .who { font-weight: bold; }
.chat-input { flex: 0 0 320px; display: flex; gap: 6px; align-items: flex-end; }
.chat-input input { margin: 0; max-width: none; height: 40px; }
.chat-input button { background: var(--accent); border: none; color: #04121c; font-weight: bold; border-radius: 8px; padding: 0 16px; height: 40px; cursor: pointer; }

.error { color: var(--bad); font-weight: bold; }

/* ---- toast ---- */
.toast {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%);
  background: var(--bad); color: #fff; padding: 10px 18px; border-radius: 10px;
  z-index: 50; font-weight: bold;
}
.toast.ok { background: var(--good); color: #04121c; }

/* ============================================================
   SMALL SCREENS: let the page scroll again and stack the cards
   ============================================================ */
@media (max-width: 900px) {
  body { overflow: auto; }
  #screen-game { height: auto; overflow: visible; }
  .grid { grid-template-columns: 1fr; grid-template-rows: none; grid-template-areas: none; }
  .cities-panel, .map-panel, .players-panel, .merged-panel, .chat-box { grid-area: auto; }
  .panel { max-height: 60vh; }
  .chat-box { flex-direction: column; min-height: 140px; }
  .chat-input { flex: none; }
}
