:root {
  --bar: #1f2a36;
  --bar-text: #eef2f6;
  --bg: #f4f1ea;
  --card: #ffffff;
  --text: #1d232b;
  --muted: #6b7480;
  --line: #dcd6c9;
  --primary: #2f6fb3;
  --danger: #b03a2e;
  --ok: #2e8b57;
  --sea: #9fc5e0;
  --tab-height: 56px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--text);
  height: 100dvh; overflow: hidden;
  overscroll-behavior: none;
}
button {
  font: inherit; border: 1px solid var(--line); background: #fff; color: var(--text);
  border-radius: 10px; padding: 9px 14px; min-height: 40px; cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.small { padding: 5px 10px; min-height: 32px; font-size: 14px; }
button.link { border: none; background: none; color: var(--primary); padding: 6px 4px; }
input, textarea, select { font: inherit; font-size: 16px; border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; background: #fff; color: var(--text); }
textarea { width: 100%; resize: vertical; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.88em; }
.normal { font-weight: normal; }
h3 { margin: 0 0 8px; font-size: 1rem; }

/* Turn bar */
#turnbar { background: var(--bar); color: var(--bar-text); padding: calc(6px + env(safe-area-inset-top)) 10px 6px; flex: none; }
#turnbar .muted { color: #a9b4c0; }
.turnbar-row { display: flex; align-items: center; gap: 8px; }
.turn-info { flex: 1; min-width: 0; }
#turn-label { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#nation-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.turn-actions { display: flex; gap: 6px; }
.turn-actions button { padding: 7px 10px; font-size: 14px; min-height: 38px; }
#resolve-turn { background: #34495e; color: #fff; border-color: #4f6478; }
#resolve-turn.due { background: #e0a526; color: #1d232b; border-color: #e0a526; font-weight: 600; }
.progress { display: flex; gap: 5px; margin-top: 6px; overflow-x: auto; scrollbar-width: none; }
.progress:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; flex: none;
  border-radius: 999px; padding: 3px 8px 3px 4px; font-size: 12px;
  background: rgba(255,255,255,0.08); color: var(--bar-text); border: 1px solid rgba(255,255,255,0.15); min-height: 0;
}
.chip .dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.7); }
.chip.thinking { background: rgba(224,165,38,0.25); border-color: #e0a526; }
.chip.thinking .dot { animation: pulse 1s infinite alternate; }
.chip.done { background: rgba(46,139,87,0.3); border-color: #3fae6f; }
.chip.failed { background: rgba(176,58,46,0.45); border-color: #e05a4a; }
.chip.skipped { opacity: 0.5; }
@keyframes pulse { from { transform: scale(0.75); } to { transform: scale(1.1); } }

/* Views */
main { flex: 1; position: relative; min-height: 0; }
.view { position: absolute; inset: 0; overflow-y: auto; padding: 10px 10px 16px; display: none; -webkit-overflow-scrolling: touch; }
.view.active { display: block; }
#view-map { padding: 0; overflow: hidden; background: var(--sea); }
#map { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.card { background: var(--card); border-radius: 14px; padding: 12px; margin-bottom: 10px; box-shadow: 0 1px 2px rgba(0,0,0,0.06); }

/* Map */
.hex { stroke: rgba(255,255,255,0.55); stroke-width: 0.03; }
.hex.sea { fill: var(--sea); stroke: none; }
.terrain-mark { fill: none; stroke: rgba(40,30,20,0.45); stroke-width: 0.05; pointer-events: none; }
.terrain-mark.mountain { fill: rgba(60,45,30,0.25); }
.city-mark { fill: #fff; stroke: #222; stroke-width: 0.05; pointer-events: none; }
.city-label { font-size: 0.27px; fill: #1a1a1a; text-anchor: middle; pointer-events: none; paint-order: stroke; stroke: rgba(255,255,255,0.85); stroke-width: 0.07px; font-weight: 600; }
.hex-label { font-size: 0.2px; fill: rgba(0,0,0,0.35); text-anchor: middle; pointer-events: none; }
.army circle { stroke: #fff; stroke-width: 0.06; }
.army.mine circle { stroke: #ffd24a; stroke-width: 0.1; }
.army text { font-size: 0.34px; fill: #fff; text-anchor: middle; dominant-baseline: central; font-weight: 700; pointer-events: none; }
.army .count { font-size: 0.22px; fill: #111; }
.army { pointer-events: none; }
.move-arrow { stroke: #1d232b; stroke-width: 0.09; fill: none; stroke-dasharray: 0.18 0.1; marker-end: url(#arrowhead); pointer-events: none; }
.build-mark { fill: none; stroke: #ffd24a; stroke-width: 0.08; stroke-dasharray: 0.12 0.08; pointer-events: none; }
.selected-outline { fill: none; stroke: #111; stroke-width: 0.1; pointer-events: none; }
.reachable { fill: rgba(255,255,255,0.35); stroke: #fff; stroke-width: 0.08; pointer-events: none; animation: glow 0.9s infinite alternate; }
@keyframes glow { from { fill-opacity: 0.25; } to { fill-opacity: 0.7; } }
.map-controls { position: absolute; right: 10px; bottom: 12px; display: flex; flex-direction: column; gap: 8px; }
.map-controls button { width: 44px; height: 44px; padding: 0; font-size: 22px; border-radius: 50%; box-shadow: 0 1px 4px rgba(0,0,0,0.25); }
.move-banner {
  position: absolute; left: 10px; right: 64px; top: 10px; background: var(--bar); color: #fff;
  border-radius: 12px; padding: 8px 8px 8px 12px; display: flex; align-items: center; gap: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.move-banner span { flex: 1; font-size: 14px; }

/* Bottom sheet */
.sheet {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-height) + env(safe-area-inset-bottom));
  background: var(--card); border-radius: 16px 16px 0 0; box-shadow: 0 -4px 18px rgba(0,0,0,0.2);
  padding: 8px 14px 14px; max-height: 55dvh; overflow-y: auto; z-index: 20;
}
.sheet-handle { width: 40px; height: 4px; background: var(--line); border-radius: 2px; margin: 0 auto 8px; }
.sheet-close { position: absolute; right: 8px; top: 6px; border: none; background: none; font-size: 26px; min-height: 0; padding: 2px 10px; color: var(--muted); }
.sheet h2 { margin: 0 0 4px; font-size: 1.1rem; padding-right: 30px; }
.sheet .row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-top: 1px solid #eee; }
.sheet .row .grow { flex: 1; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 4px; }

/* Messages */
.thread-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; margin-bottom: 8px; padding: 10px 12px; background: var(--card); border-radius: 14px; border: none; box-shadow: 0 1px 2px rgba(0,0,0,0.06); }
.thread-item .grow { flex: 1; min-width: 0; }
.thread-item .snippet { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item .avatar { width: 34px; height: 34px; border-radius: 50%; flex: none; color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 12px; padding: 1px 7px; font-weight: 600; }
#thread { display: flex; flex-direction: column; min-height: 100%; }
.thread-header { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
#thread-messages { flex: 1; display: flex; flex-direction: column; gap: 6px; padding-bottom: 8px; }
.bubble { max-width: 85%; padding: 8px 11px; border-radius: 14px; background: var(--card); white-space: pre-wrap; overflow-wrap: anywhere; box-shadow: 0 1px 1px rgba(0,0,0,0.06); }
.bubble.mine { align-self: flex-end; background: #d8e8f8; }
.bubble.system { align-self: center; background: #fff4d6; font-size: 14px; }
.bubble .meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.compose { display: flex; gap: 8px; align-items: flex-end; position: sticky; bottom: 0; background: var(--bg); padding-top: 6px; }
.give-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.give-row input { width: 100px; }
.give-row span { flex: 1; color: var(--muted); font-size: 14px; }

/* News */
.paper h3 { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; border-bottom: 2px solid var(--text); padding-bottom: 4px; }
.paper ul { margin: 6px 0; padding-left: 18px; }
.paper li { margin: 4px 0; }
.paper blockquote { margin: 6px 0; padding: 4px 10px; border-left: 3px solid var(--line); font-style: italic; }
#publish-form button { margin-top: 8px; }

/* Nation */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.stat { background: var(--bg); border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 1.3rem; }
.list-row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-top: 1px solid #eee; }
.list-row:first-child { border-top: none; }
.list-row .grow { flex: 1; min-width: 0; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: #c9c3b6; border-radius: 15px; transition: 0.2s; }
.switch span::before { content: ""; position: absolute; width: 24px; height: 24px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: 0.2s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(20px); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
td, th { padding: 5px 4px; text-align: right; border-top: 1px solid #eee; }
td:first-child, th:first-child { text-align: left; }
.new-game { display: flex; flex-direction: column; gap: 8px; }
.new-game label { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* Agent log */
.run { background: var(--card); border-radius: 12px; margin-bottom: 8px; overflow: hidden; }
.run summary { padding: 10px 12px; display: flex; gap: 8px; align-items: center; cursor: pointer; list-style: none; }
.run summary::-webkit-details-marker { display: none; }
.run .grow { flex: 1; min-width: 0; }
.run-body { padding: 0 12px 12px; }
.run pre { white-space: pre-wrap; overflow-wrap: anywhere; background: #f6f6f3; border-radius: 8px; padding: 8px; font-size: 12px; max-height: 340px; overflow-y: auto; margin: 4px 0 8px; }
.outcome { font-size: 12px; border-radius: 6px; padding: 2px 7px; font-weight: 600; }
.outcome.ok { background: #dff2e6; color: var(--ok); }
.outcome.failed { background: #f8dcd8; color: var(--danger); }
.command { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.command.error { color: var(--danger); }

/* Toast and tabs */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); top: calc(64px + env(safe-area-inset-top)); width: max-content;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 40; max-width: 90vw; font-size: 14px; box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}
.toast.error { background: var(--danger); }
#tabs {
  flex: none; display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--tab-height) + env(safe-area-inset-bottom)); z-index: 30;
}
#tabs button { flex: 1; border: none; border-radius: 0; background: none; font-size: 13px; color: var(--muted); padding: 4px 2px; min-height: var(--tab-height); }
#tabs button.active { color: var(--primary); font-weight: 700; box-shadow: inset 0 3px 0 var(--primary); }
#tabs .badge { font-size: 11px; padding: 0 6px; }
