:root {
  --bg: #0d1726;
  --panel: rgba(14, 22, 36, 0.88);
  --panel-solid: #142033;
  --border: rgba(255, 255, 255, 0.12);
  --text: #e8eef7;
  --muted: #8ea0b8;
  --accent: #f4b942;
  --danger: #e63946;
  --good: #52b788;
  --font: 'Pixelify Sans', ui-monospace, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 15px;
  -webkit-user-select: none; user-select: none;
}

canvas#game { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; cursor: crosshair; }
canvas#game.grabbing { cursor: grabbing; }

[hidden] { display: none !important; }

button {
  font: inherit; color: var(--text); background: #22324a; border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 12px; cursor: pointer;
}
button:hover:not(:disabled) { background: #2d4262; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: #1b1300; border-color: transparent; font-weight: 700; }
button.primary:hover { background: #ffcb5c; }
button.danger { background: #8f1d27; }
button.danger:hover:not(:disabled) { background: #b0242f; }
button.good { background: #1f6b4a; }
button.good:hover:not(:disabled) { background: #26845b; }
button.active { background: var(--accent); color: #1b1300; }
button.small { padding: 2px 8px; }

input, select {
  font: inherit; color: var(--text); background: #0f1a2b; border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px;
}
select:disabled { opacity: 0.4; }
input[type=range] { padding: 0; accent-color: var(--accent); }
input[type=color] { padding: 0 2px; height: 34px; width: 60px; }

kbd { background: #22324a; border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 0.85em; }

.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 3px; border: 1px solid rgba(0,0,0,0.5); flex: none; }
.traitor { background: var(--danger); color: #fff; font-size: 11px; padding: 1px 6px; border-radius: 4px; letter-spacing: 1px; }

/* ------------------------------------------------ overlays */
.overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, rgba(13, 23, 38, 0.55), rgba(5, 10, 18, 0.9));
  z-index: 20; padding: 16px; overflow-y: auto;
}
.menu-card {
  background: var(--panel-solid); border: 2px solid var(--border); border-radius: 12px;
  padding: 24px 28px; width: min(480px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  max-height: 100%; overflow-y: auto;
}
.menu-card h1 { margin: 0; font-size: 40px; text-align: center; color: var(--accent); letter-spacing: 1px; text-shadow: 3px 3px 0 #7a2b10; }
.menu-card h1 span { color: var(--text); font-size: 0.6em; text-shadow: none; }
.tagline { text-align: center; color: var(--muted); margin: 4px 0 18px; }
.form { display: grid; gap: 10px; margin-bottom: 18px; }
.form label { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.form input:not([type=range]):not([type=color]), .form select { width: 200px; }
.range-row { display: flex; align-items: center; gap: 8px; width: 200px; }
.range-row input { flex: 1; min-width: 0; }
.range-row b { width: 28px; text-align: right; }
#btn-start { width: 100%; font-size: 20px; padding: 10px; }
.howto { margin-top: 14px; color: var(--muted); }
.howto summary { cursor: pointer; color: var(--text); }
.howto ul { padding-left: 18px; margin: 8px 0 0; line-height: 1.5; }
#go-text { text-align: center; color: var(--muted); line-height: 1.5; }

/* ------------------------------------------------ HUD */
#hud > * { position: fixed; z-index: 5; }
#topbar, #leaderboard, #nation-panel, #bottombar, #toasts > div {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  backdrop-filter: blur(6px);
}

#topbar {
  top: 10px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 18px; padding: 6px 14px; white-space: nowrap;
  max-width: calc(100vw - 20px); overflow-x: auto;
}
.nation-tag { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.stat small { color: var(--muted); }
.speed { display: flex; gap: 3px; }
.speed button { padding: 2px 8px; }

#leaderboard { top: 108px; right: 10px; width: 260px; padding: 8px 10px; max-height: 46vh; overflow-y: auto; }
#leaderboard h3 { margin: 0 0 6px; font-size: 15px; }
#leaderboard h3 small { color: var(--muted); font-weight: 400; }
#lb-list { list-style: none; margin: 0; padding: 0; }
#lb-list li {
  display: grid; grid-template-columns: 22px 14px 1fr auto; gap: 6px; align-items: center;
  padding: 3px 4px; border-radius: 4px; cursor: pointer; font-size: 13px;
}
#lb-list li:hover { background: rgba(255,255,255,0.07); }
#lb-list li.me { background: rgba(244, 185, 66, 0.15); }
#lb-list li.tribe .nm { color: var(--muted); font-style: italic; }
#lb-list .rank { color: var(--muted); text-align: right; }
#lb-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lb-list .pct { color: var(--muted); font-variant-numeric: tabular-nums; }
#lb-list .ally { color: var(--good); }
#lb-list .tr { color: var(--danger); }

#nation-panel { top: 108px; left: 10px; width: 260px; padding: 12px; }
#nation-panel .close { position: absolute; top: 6px; right: 6px; padding: 0 7px; background: transparent; border: none; }
.np-head { display: flex; align-items: center; gap: 8px; padding-right: 20px; }
.np-head h3 { margin: 0; font-size: 17px; }
.np-relation { margin: 6px 0; font-size: 13px; }
.np-relation.war { color: var(--danger); }
.np-relation.peace { color: var(--muted); }
.np-relation.ally { color: var(--good); }
.np-stats { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0; font-size: 14px; }
.np-stats dt { color: var(--muted); }
.np-stats dd { margin: 0; text-align: right; }
.np-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.np-actions button { flex: 1; padding: 5px 8px; font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

#bottombar {
  bottom: 10px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px; padding: 8px 12px; max-width: calc(100vw - 20px);
}
.ratio { display: flex; flex-direction: column; gap: 4px; }
.ratio .lbl { font-size: 12px; color: var(--muted); }
.ratio small { color: var(--muted); font-size: 12px; }
.ratio-btns { display: flex; gap: 3px; }
.ratio-btns button { padding: 3px 7px; font-size: 13px; }
.build { display: grid; grid-template-columns: repeat(10, minmax(54px, 1fr)); gap: 4px; }
.build button { display: flex; flex-direction: column; align-items: center; padding: 3px 4px; font-size: 11px; line-height: 1.15; position: relative; }
.build button .em { font-size: 18px; line-height: 1.2; }
.build button small { color: var(--accent); font-size: 11px; }
.build button .hk { position: absolute; top: 1px; left: 4px; font-size: 9px; color: var(--muted); }
.build button.nuke { background: #3a1f28; }
.build button.active { outline: 2px solid var(--accent); background: #2d4262; color: var(--text); }
.build button.poor small { color: var(--danger); }

#attack-list { bottom: 92px; left: 50%; transform: translateX(-50%); display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; max-width: 720px; }
#attack-list:empty { display: none; }
.atk { display: flex; align-items: center; gap: 5px; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 3px 4px 3px 8px; font-size: 13px; }
.atk.in { border-color: var(--danger); color: #ffb3b8; padding-right: 8px; animation: pulse 1.2s infinite; }
.atk button { padding: 0 6px; background: transparent; border: none; color: var(--muted); }
.atk button:hover { color: var(--danger); background: transparent; }

#log { left: 10px; bottom: 10px; width: 320px; display: flex; flex-direction: column; gap: 3px; pointer-events: none; }
#log div {
  background: rgba(14, 22, 36, 0.78); border-left: 3px solid var(--muted); padding: 3px 8px; font-size: 13px; border-radius: 3px;
  animation: fadein 0.3s; transition: opacity 1s;
}
#log div.war { border-color: var(--danger); }
#log div.good { border-color: var(--good); }
#log div.bad { border-color: var(--danger); color: #ffb3b8; }
#log div.old { opacity: 0.5; }

#toasts { top: 108px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; width: min(360px, 90vw); }
#toasts > div { padding: 10px 12px; animation: fadein 0.3s; }
#toasts .t-actions { display: flex; gap: 8px; margin-top: 8px; }
#toasts .t-actions button { flex: 1; }

#banner {
  top: 108px; left: 50%; transform: translateX(-50%); background: var(--panel); border: 2px solid var(--accent);
  border-radius: 10px; padding: 10px 20px; font-size: 18px; text-align: center; pointer-events: none;
}
#bigbanner {
  top: 38%; left: 50%; transform: translate(-50%, -50%); font-size: clamp(32px, 7vw, 72px); font-weight: 700;
  color: var(--accent); text-shadow: 4px 4px 0 #7a2b10, 0 0 30px rgba(0,0,0,0.8); pointer-events: none; white-space: nowrap;
  animation: zoomin 0.5s;
}

#tooltip {
  pointer-events: none; background: rgba(8, 14, 24, 0.92); border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 8px; font-size: 13px; white-space: nowrap; z-index: 6;
}
#tooltip .tt-sub { color: var(--muted); font-size: 12px; }

@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@keyframes zoomin { from { opacity: 0; transform: translate(-50%, -50%) scale(1.6); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
@keyframes pulse { 50% { background: rgba(230, 57, 70, 0.25); } }

@media (max-width: 1100px) {
  #bottombar { flex-direction: column; gap: 6px; }
  #attack-list { bottom: 150px; }
  #log { display: none; }
}
@media (max-width: 700px) {
  #leaderboard { display: none; }
  #topbar { gap: 10px; font-size: 13px; }
  .build { grid-template-columns: repeat(5, 1fr); }
  #attack-list { bottom: 210px; }
  #nation-panel { width: calc(100vw - 20px); }
}

/* ------------------------------------------------ main menu: maps, preview, modes */
.menu-card.wide { width: min(1000px, 100%); }
.menu-card.small-card { width: min(360px, 100%); text-align: center; }
.menu-card h2 { font-size: 15px; color: var(--muted); margin: 0 0 8px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; }
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
#map-gallery { display: flex; flex-direction: column; gap: 10px; }
.map-group-title { font-size: 12px; color: var(--muted); margin-top: 2px; }
.map-group { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }
.map-card {
  display: flex; flex-direction: column; align-items: stretch; gap: 4px; padding: 4px; font-size: 12px;
  background: #0f1a2b; border: 2px solid transparent;
}
.map-card canvas { width: 100%; aspect-ratio: 16 / 10; object-fit: contain; image-rendering: pixelated; background: #142f52; border-radius: 4px; }
.map-card.active { border-color: var(--accent); background: #1c2c44; color: var(--text); }
.map-card .tag { font-size: 10px; color: var(--muted); }
.preview-wrap { position: relative; background: #142f52; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
#map-preview { display: block; width: 100%; aspect-ratio: 16 / 10; object-fit: contain; image-rendering: pixelated; }
#map-preview.pickable { cursor: pointer; }
.preview-hint { position: absolute; left: 8px; bottom: 6px; font-size: 12px; color: #fff; text-shadow: 0 1px 3px #000; pointer-events: none; }
.tabs { display: flex; gap: 4px; margin: 12px 0; }
.tabs button { flex: 1; }
.tabs button:disabled { text-decoration: line-through; }
#menu:not(.mode-classic) .classic-only, #menu:not(.mode-reality) .reality-only, #menu:not(.group-random) .random-only { display: none; }
#opt-country { width: 200px; }
.stack { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }

/* ------------------------------------------------ victory progress bar */
#winbar {
  top: 58px; left: 50%; transform: translateX(-50%); width: min(480px, calc(100vw - 20px));
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 5px 10px 4px;
}
.wb-track { position: relative; height: 10px; background: #0b1422; border-radius: 5px; overflow: visible; }
.wb-fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 5px; transition: width 0.3s; }
.wb-leader { position: absolute; top: -4px; width: 4px; height: 18px; background: #fff; border-radius: 2px; box-shadow: 0 0 4px #000; transition: left 0.3s; }
.wb-text { font-size: 12px; color: var(--muted); text-align: center; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wb-text b { color: var(--text); }

/* ------------------------------------------------ radial menu */
#radial { position: fixed; z-index: 15; width: 0; height: 0; }
#radial .r-center {
  position: absolute; left: -38px; top: -38px; width: 76px; height: 76px; border-radius: 50%;
  background: rgba(10, 16, 28, 0.92); border: 2px solid var(--border); display: flex; align-items: center; justify-content: center;
  text-align: center; font-size: 11px; padding: 6px; line-height: 1.1; overflow: hidden;
}
#radial .r-item {
  position: absolute; width: 64px; height: 64px; margin: -32px 0 0 -32px; border-radius: 50%; padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  background: rgba(20, 32, 51, 0.95); border: 2px solid var(--border); font-size: 10px; line-height: 1.1;
  animation: radialIn 0.15s ease-out both;
}
#radial .r-item .em { font-size: 20px; }
#radial .r-item small { color: var(--accent); font-size: 9px; }
#radial .r-item.hot { background: var(--accent); color: #1b1300; border-color: #fff; transform: scale(1.1); }
#radial .r-item.hot small { color: #1b1300; }
#radial .r-item.dim { opacity: 0.5; }
@keyframes radialIn { from { opacity: 0; transform: scale(0.4); } to { opacity: 1; transform: none; } }

@media (max-width: 800px) {
  .menu-grid { grid-template-columns: 1fr; }
  #winbar { top: 54px; }
}

/* ------------------------------------------------ cheats */
#hud:not(.cheats) .cheat-only { display: none; }
#cheat-panel {
  top: 108px; left: 280px; width: 270px; padding: 12px; background: var(--panel); border: 1px solid #b07cff;
  border-radius: 10px; backdrop-filter: blur(6px); display: flex; flex-direction: column; gap: 6px; font-size: 13px;
}
#cheat-panel h3 { margin: 0 0 4px; font-size: 16px; color: #c9a6ff; }
#cheat-panel label { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; }
#cheat-panel .close { position: absolute; top: 6px; right: 6px; padding: 0 7px; background: transparent; border: none; }
.cp-actions { display: flex; gap: 6px; margin-top: 4px; }
.cp-actions button { flex: 1; font-size: 13px; padding: 4px; }
#btn-cheats { background: #4b2a7a; }
.muted { color: var(--muted); }
input[type=checkbox] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
