:root {
  --bg: #1e170f; --panel: #2c3e50; --panel2: #34495e; --accent: #2980b9; --accent2: #e67e22;
  --text: #ecf0f1; --muted: #95a5a6; --danger: #c0392b; --ok: #27ae60; --radius: 8px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-size: 14px; }
a { color: #5dade2; text-decoration: none; }
a:hover { text-decoration: underline; }
button { background: var(--accent); color: #fff; border: 0; padding: 8px 12px; border-radius: 6px; cursor: pointer; font: inherit; }
button:hover { filter: brightness(1.12); }
button:disabled { opacity: .5; cursor: default; }
button.secondary { background: var(--panel2); }
button.danger { background: var(--danger); }
button.small { padding: 4px 8px; font-size: 12px; }
button.active { outline: 2px solid #f1c40f; }
input, select, textarea { width: 100%; padding: 7px 8px; margin: 4px 0 10px; border: 1px solid #46617a; border-radius: 6px;
  background: #1f2d3a; color: var(--text); font: inherit; }
input[type=checkbox] { width: auto; margin: 0 6px 0 0; }
input[type=color] { padding: 2px; height: 34px; }
input[type=file] { padding: 4px; }
label { display: block; color: #bdc3c7; font-size: 12px; margin-top: 6px; }
label.inline { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text); margin: 8px 0; }
h1, h2, h3 { margin: 0 0 10px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.error { color: #ff8a80; min-height: 1.2em; margin: 6px 0; font-size: 13px; }
.card { background: var(--panel); border-radius: var(--radius); padding: 18px; box-shadow: 0 6px 24px rgba(0,0,0,.4); }
.center-page { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.center-page .card { width: 100%; max-width: 420px; }
.page { max-width: 900px; margin: 0 auto; padding: 24px 16px; }
.page header { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.page header h1 { flex: 1; font-size: 22px; margin: 0; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 720px) { .cols { grid-template-columns: 1fr; } }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tabs button { flex: 1; background: var(--panel2); }
.tabs button.active { background: var(--accent); outline: none; }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }
.row > button { flex: 0 0 auto; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 8px; padding: 6px 4px; border-bottom: 1px solid #3d566e; }
.list li.clickable { cursor: pointer; }
.list li:hover { background: rgba(255,255,255,.04); }
.list li.selected { background: rgba(41,128,185,.25); }
.list li .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { font-size: 11px; padding: 2px 6px; border-radius: 10px; background: var(--panel2); color: #dfe6e9; white-space: nowrap; }
.badge.gm { background: var(--accent2); color: #fff; }
.badge.online { background: var(--ok); color: #fff; }
.badge.admin { background: #8e44ad; color: #fff; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; border: 1px solid rgba(255,255,255,.4); }
.section { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.code { font-family: Consolas, monospace; font-size: 18px; letter-spacing: .15em; background: #1f2d3a; padding: 6px 10px; border-radius: 6px; display: inline-block; }
.hint { font-size: 12px; color: var(--muted); }

/* ---- Map app layout ---- */
#app { position: fixed; inset: 0; display: grid; grid-template-rows: 48px 1fr; grid-template-columns: 320px 1fr; }
#app.nosidebar { grid-template-columns: 0 1fr; }
#topbar { grid-column: 1 / 3; display: flex; align-items: center; gap: 8px; padding: 0 10px; background: #1a2530; border-bottom: 1px solid #0f1720; z-index: 5; }
#topbar h1 { font-size: 16px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar a.back { font-size: 18px; padding: 0 4px; }
#presence { display: flex; gap: 4px; margin-right: 6px; }
#presence .avatar { width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; background: var(--panel2); border: 2px solid transparent; }
#presence .avatar.gm { border-color: var(--accent2); }
#sidebar { grid-column: 1; grid-row: 2; background: var(--panel); overflow: hidden; display: flex; flex-direction: column; border-right: 1px solid #1a2530; }
#app.nosidebar #sidebar { display: none; }
#sidebar .tabs { padding: 8px 8px 0; }
#sidebar .panel { display: none; overflow: auto; padding: 8px 12px 20px; flex: 1; }
#sidebar .panel.active { display: block; }
#stage { grid-column: 2; grid-row: 2; position: relative; overflow: hidden; background: var(--bg); min-width: 0; min-height: 0; }
#stage canvas { display: block; touch-action: none; }
#hud { position: absolute; left: 10px; bottom: 10px; display: flex; gap: 6px; align-items: center; z-index: 3; }
#hud button { background: rgba(26,37,48,.92); }
#modeHud { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); background: rgba(26,37,48,.95);
  padding: 8px 12px; border-radius: 6px; display: none; z-index: 3; white-space: nowrap; max-width: calc(100% - 20px); overflow: hidden; text-overflow: ellipsis; }
#modeHud.show { display: block; }
.toolgroup { display: flex; gap: 4px; }
.toolgroup button { font-size: 14px; padding: 4px 7px; }
#topbar .tabs button, #sidebar .tabs button { padding: 6px 4px; font-size: 12px; }
.log .ev.roll { color: #f1c40f; }
.log .ev.combat { color: #ff9f80; }
.log .ev.turn { color: #f9c74f; }
.log .ev.history { display: flex; align-items: flex-start; gap: 6px; }
.log .ev.history .grow { flex: 1; }
.log .ev.history.reverted { opacity: .55; text-decoration: line-through; }
.log .ev.history .badge { flex: none; }
.sprite { display: inline-block; vertical-align: middle; }
.spritegrid { display: flex; flex-wrap: wrap; gap: 6px; max-height: 60vh; overflow: auto; background: #1f2d3a; padding: 8px; border-radius: 6px; }
.spritegrid .cell { width: 78px; height: 78px; display: flex; align-items: center; justify-content: center; border: 2px solid transparent; border-radius: 5px; cursor: pointer; background: rgba(0,0,0,.28); }
.spritegrid .cell:hover { border-color: #5dade2; }
.spritegrid .cell.selected { border-color: #f1c40f; }
.coloricon { grid-template-columns: 84px 1fr; }
#addForm .coloricon { grid-template-columns: 1fr; }   /* the sidebar is too narrow for two columns */
.field { display: block; color: #bdc3c7; font-size: 12px; margin-top: 6px; }
.iconrow { display: flex; align-items: center; gap: 5px; min-height: 40px; margin: 4px 0 6px; flex-wrap: wrap; }
.iconrow .sprite { background-color: rgba(0,0,0,.25); border-radius: 4px; }
label.filebtn { display: inline-block; margin: 0; padding: 4px 8px; font-size: 12px; border-radius: 6px; cursor: pointer; background: var(--panel2); color: var(--text); }
label.filebtn:hover { filter: brightness(1.15); }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 8px; }
details.sub { margin: 6px 0; }
details.sub > summary { cursor: pointer; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 0; user-select: none; list-style-position: inside; }
details.sub > summary .hint { text-transform: none; letter-spacing: 0; }
/* Game layer */
#turnLabel.turn { background: rgba(241,196,15,.18); color: #f9c74f; border: 1px solid rgba(241,196,15,.5); }
.nationhead { display: flex; align-items: center; gap: 6px; margin: 4px 0 8px; font-size: 15px; flex-wrap: wrap; }
.pools { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.pools div { background: var(--panel2); border-radius: 6px; padding: 6px 8px; display: flex; flex-direction: column; }
.pools b { font-size: 16px; }
.delta { font-size: 11px; } .delta.up, .num.up { color: #2ecc71; } .delta.down, .num.down { color: #e74c3c; }
.treasury { width: 100%; border-collapse: collapse; font-size: 13px; }
.treasury td, .treasury th { padding: 3px 4px; border-bottom: 1px solid #3d566e; text-align: left; }
.treasury th { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.treasury .num { text-align: right; font-variant-numeric: tabular-nums; }
.resicon { width: 18px; height: 18px; vertical-align: -4px; margin-right: 6px; image-rendering: auto; }
.reskey { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; border: 1px solid rgba(255,255,255,.25); }
.hintlist { margin: 4px 0 0 16px; padding: 0; font-size: 12px; color: var(--muted); }
.rel { display: inline-block; padding: 1px 6px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.rel.war { background: rgba(231,76,60,.25); color: #ff9f80; }
.rel.peace { background: rgba(255,255,255,.08); color: var(--muted); }
.rel.alliance { background: rgba(46,204,113,.22); color: #7ee2a8; }
/* Heroes: a 2:3 portrait in an oval golden ring whose light runs round it */
@property --hero-a { syntax: '<angle>'; inherits: false; initial-value: 0deg; }
@keyframes hero-spin { to { --hero-a: 360deg; } }
.herocard { display: flex; gap: 10px; align-items: flex-start; margin: 4px 0; }
.hero-portrait { flex: 0 0 auto; width: 84px; aspect-ratio: 2 / 3; border-radius: 50%; padding: 4px;
  background: conic-gradient(from var(--hero-a), #6e4f0e, #d4af37 20%, #fff6c8 28%, #d4af37 36%, #8a6a1a 60%, #f5d76e 80%, #6e4f0e);
  animation: hero-spin 3.2s linear infinite; box-shadow: 0 0 12px rgba(245, 215, 110, .35); }
.hero-oval { width: 100%; height: 100%; border-radius: 50%; overflow: hidden; background: #18212b; display: flex; align-items: center; justify-content: center; }
.hero-oval img, .hero-oval canvas { width: 100%; height: 100%; object-fit: cover; }
.hero-initials { font: 700 22px Georgia, serif; color: #f5e6b8; display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.hero-initials small { font-size: 14px; color: #d4af37; }
.hero-info { flex: 1; min-width: 0; }
.hero-role { font-weight: 600; }
.hero-stars { color: #f5d76e; letter-spacing: 1px; font-size: 15px; margin: 1px 0 4px; }
.herocard.travelling .hero-portrait, .herocard.captured .hero-portrait { filter: grayscale(.6) brightness(.8); }
.herosend { display: inline-flex; gap: 4px; align-items: center; }
.herosend select { width: auto; margin: 0; padding: 3px 6px; }
.herolist { display: grid; gap: 4px; }
.heroitem { display: flex; gap: 8px; align-items: center; text-align: left; background: var(--panel2); padding: 4px 8px; width: 100%; }
.hero-mini { width: 26px; height: 39px; border-radius: 50%; overflow: hidden; flex: 0 0 auto; display: flex; align-items: center; justify-content: center;
  background: #18212b; box-shadow: 0 0 0 2px #d4af37; color: #f5d76e; }
.hero-mini img { width: 100%; height: 100%; object-fit: cover; }
@media (prefers-reduced-motion: reduce) { .hero-portrait { animation: none; } }

/* Phase 5: turn reports, deadline countdown, captured places */
.badge.turn.overdue { background: #7a2e2e; color: #ffd9d9; }
.badge.province { background: rgba(20,30,25,.85); color: #e8f0ea; font-style: italic; }
.deadline-quick { flex-wrap: wrap; gap: 4px; margin: 2px 0 8px; }
.deadline-quick .hint { flex: 0 0 auto; }
.deadline-quick button.small { padding: 2px 8px; font-size: 11px; }
.captured { font-size: 12px; margin: 4px 0; }
.captured.up { color: #2ecc71; } .captured.down { color: #e74c3c; }
.treasury tr.me td { background: rgba(255,255,255,.05); }
.report-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
@media (max-width: 640px) { .report-cols { grid-template-columns: 1fr; } }
.report .tabs.small { flex-wrap: wrap; }
.report .tabs.small button { flex: 0 1 auto; padding: 4px 8px; font-size: 12px; }
.hintlist li.up { color: #2ecc71; } .hintlist li.down { color: #e74c3c; }
.diplo { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding: 4px 0; border-bottom: 1px solid #3d566e; font-size: 12px; line-height: 1.2; }
.diplo button.small { padding: 2px 6px; font-size: 11px; }
.diplo .grow { flex: 1; min-width: 120px; }
.site { margin-bottom: 6px; }
.buildlist { max-height: 60vh; overflow: auto; }
.techtree { max-height: 66vh; overflow: auto; margin-top: 8px; }
.techtree .branch { margin: 10px 0 4px; }
.techgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 6px; }
.tech { background: var(--panel2); border-radius: 6px; padding: 6px 8px; font-size: 12px; border: 1px solid transparent; display: flex; flex-direction: column; gap: 3px; }
.tech .t { font-weight: 600; font-size: 13px; display: flex; justify-content: space-between; gap: 6px; }
.tech .t .yr { color: var(--muted); font-weight: 400; }
.tech.done { border-color: rgba(46,204,113,.5); }
.tech.active { border-color: #f1c40f; }
.tech.queued { border-color: rgba(241,196,15,.45); border-style: dashed; }
.tech.locked { opacity: .6; }
.tech .bar { height: 5px; background: rgba(255,255,255,.08); border-radius: 3px; overflow: hidden; }
.tech .bar i { display: block; height: 100%; background: #f1c40f; }
.tech .actions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 2px; }
.progress { height: 8px; background: rgba(255,255,255,.08); border-radius: 4px; overflow: hidden; margin: 4px 0; }
.progress i { display: block; height: 100%; background: #f1c40f; }
.qalist { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 6px; }
.qalist .section { grid-column: 1 / -1; margin: 10px 0 2px; }
.qa { display: flex; align-items: center; gap: 6px; padding: 3px 6px; background: var(--panel2); text-align: left; font-size: 12px; line-height: 1.15; border: 1px solid transparent; min-height: 36px; }
.qa img { width: 40px; height: 27px; flex: none; }
.qa.active { border-color: #f1c40f; background: rgba(241,196,15,.18); }
#popup .sprite { display: block; margin: 0 auto 6px; }
.flag { display: inline-block; height: 14px; width: auto; max-width: 28px; vertical-align: middle; border: 1px solid rgba(0,0,0,.6); border-radius: 2px; background: #000; object-fit: cover; }
.flag.big { height: 22px; max-width: 44px; }
.flagpreview { width: 120px; height: 80px; background: #1f2d3a; border-radius: 6px; display: flex; align-items: center; justify-content: center; overflow: hidden; flex: none; }
.flagpreview img { max-width: 100%; max-height: 100%; }
.flaggrid { display: flex; flex-wrap: wrap; gap: 6px; max-height: 40vh; overflow: auto; background: #1f2d3a; padding: 8px; border-radius: 6px; }
.flaggrid .cell { width: 92px; cursor: pointer; border: 2px solid transparent; border-radius: 5px; padding: 4px; text-align: center; font-size: 11px; color: var(--muted); }
.flaggrid .cell img { width: 80px; height: 53px; object-fit: cover; display: block; margin: 0 auto 3px; border-radius: 3px; }
.flaggrid .cell:hover { border-color: #5dade2; }
.flaggrid .cell.selected { border-color: #f1c40f; color: var(--text); }
.keygrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px 10px; }
.keygrid .k { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.keygrid .k img { width: 66px; height: 45px; image-rendering: auto; flex: none; }
.vis { font-size: 10px; padding: 1px 5px; border-radius: 8px; background: #1f5f8b; color: #dff3ff; white-space: nowrap; }
#popup .row { margin-top: 6px; }
@media (max-width: 600px) { .toolgroup button:nth-child(4), .toolgroup button:nth-child(5) { display: none; } }
#popup { position: absolute; min-width: 210px; max-width: 290px; background: rgba(26,37,48,.97); border: 1px solid #46617a;
  border-radius: 8px; padding: 10px; z-index: 4; display: none; box-shadow: 0 8px 24px rgba(0,0,0,.5); }
#popup h3 { font-size: 14px; margin-bottom: 6px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
#popup .stat { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; padding: 1px 0; }
#popup .actions { display: flex; gap: 4px; margin-top: 8px; flex-wrap: wrap; }
#popup img.icon { max-width: 100%; max-height: 140px; border-radius: 6px; display: block; margin-bottom: 6px; }
#popup .notes { font-size: 12px; white-space: pre-wrap; margin: 6px 0 0; color: #dfe6e9; }
#emptyMap { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; pointer-events: none; z-index: 2; }
#emptyMap.show { display: flex; }
#emptyMap .card { pointer-events: auto; max-width: 360px; text-align: center; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: none; align-items: center; justify-content: center; z-index: 20; padding: 12px; }
.modal.open { display: flex; }
.modal .card { width: 100%; max-width: 460px; max-height: 92vh; overflow: auto; position: relative; }
.modal .close { position: absolute; top: 8px; right: 10px; background: transparent; font-size: 20px; padding: 2px 8px; }
.modal .card.wide { max-width: 820px; }
.statswrap { overflow: auto; max-height: 62vh; margin-top: 8px; border: 1px solid #3d566e; border-radius: 6px; }
.stats { width: 100%; border-collapse: collapse; font-size: 13px; }
.stats th, .stats td { padding: 3px 4px; text-align: center; border-bottom: 1px solid #3d566e; white-space: nowrap; }
.stats th { position: sticky; top: 0; background: #1f2d3a; font-size: 12px; color: #bdc3c7; z-index: 1; }
.stats th.name, .stats td.name { text-align: left; }
.stats tr.cat td { background: rgba(255,255,255,.05); font-weight: 600; text-align: left; padding: 8px 6px 4px; }
.stats tr.changed td.name { color: #f9c74f; }
.stats .pm { display: inline-flex; align-items: center; gap: 2px; }
.stats .pm button { padding: 0 7px; font-size: 14px; line-height: 22px; min-width: 22px; }
.stats .pm input { width: 46px; padding: 2px 4px; margin: 0; text-align: center; font-size: 13px; -moz-appearance: textfield; }
.stats .pm input::-webkit-outer-spin-button, .stats .pm input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stats .pm.chg input { border-color: #f39c12; color: #f9c74f; }
.toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); background: #1a2530; border: 1px solid #46617a;
  padding: 10px 14px; border-radius: 6px; z-index: 30; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }
.log { font-size: 13px; }
.log .ev { padding: 4px 0; border-bottom: 1px solid #3d566e; }
.log .ev.system { color: var(--muted); font-style: italic; }
.log .who { font-weight: 700; color: #5dade2; }
.log .when { font-size: 11px; color: var(--muted); margin-left: 4px; }
@media (max-width: 800px) {
  #app { grid-template-columns: 0 1fr; }
  #sidebar { position: absolute; top: 48px; bottom: 0; left: 0; width: min(320px, 90vw); z-index: 6; }
  #app.nosidebar #sidebar { display: none; }
  #app:not(.nosidebar) #sidebar { display: flex; }
}

/* Railways: cut sections listed in the railway popup */
.railcuts { display: grid; gap: 4px; margin: 6px 0 2px; }
.railcut { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.railcut > span { flex: 1; min-width: 0; }
