:root {
  --bg: #0b1320;
  --panel: #111b2c;
  --panel2: #16233a;
  --line: #243653;
  --text: #dfe7f3;
  --dim: #8fa3c0;
  --gold: #e8c46a;
  --red: #e0645c;
  --green: #66c28a;
  --blue: #5aa9e6;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.4 var(--sans); overflow: hidden; }
#app { display: flex; flex-direction: column; height: 100%; }
button { font: inherit; color: inherit; }

/* ---- top bar ---- */
#top { display: flex; align-items: center; gap: 14px; padding: 8px 14px; background: linear-gradient(#142039, #101a2e); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { display: flex; flex-direction: column; line-height: 1; }
.logo { font: 700 22px var(--serif); letter-spacing: 0.28em; color: var(--gold); }
.brand small { color: var(--dim); font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; margin-top: 3px; }
#clock { display: flex; flex-direction: column; min-width: 120px; }
#yearLabel { font: 600 18px var(--serif); }
#eraLabel { color: var(--dim); font-size: 11px; }
.controls { display: flex; align-items: center; gap: 8px; }
.speed { display: flex; align-items: center; gap: 8px; }
.speed input { width: 150px; accent-color: var(--gold); }
#speedLabel { min-width: 46px; font-weight: 600; }
#effLabel { color: var(--dim); font-size: 11px; min-width: 70px; }
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 5px 11px; cursor: pointer; }
.btn:hover { border-color: var(--gold); }
.btn.icon { width: 36px; padding: 5px 0; text-align: center; }
.btn.primary { background: #3b5b2e; border-color: #5c8a49; }
.btn.danger { border-color: #7b3a3a; color: #f1b5b0; }
.btn.small { padding: 2px 8px; font-size: 12px; }
.layers { display: flex; gap: 4px; flex-wrap: wrap; }
.layers label { display: flex; align-items: center; gap: 4px; padding: 3px 8px; border: 1px solid var(--line); border-radius: 12px; cursor: pointer; color: var(--dim); user-select: none; }
.layers label.on { color: var(--text); border-color: #4a6a9a; background: #1a2b47; }
.layers input { display: none; }
.right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.net { width: 9px; height: 9px; border-radius: 50%; background: #666; display: inline-block; }
.net.ws { background: var(--green); } .net.poll { background: var(--gold); } .net.down { background: var(--red); }

/* ---- layout ---- */
main { flex: 1; display: flex; min-height: 0; }
#stage { flex: 1; position: relative; min-width: 0; background: #0a1a2e; touch-action: none; }
#stage canvas { display: block; }
#side { width: 380px; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; background: #0a1a2e; color: var(--dim); font: 16px var(--serif); letter-spacing: 0.08em; z-index: 5; }
#loading.gone { display: none; }
#tooltip { position: absolute; pointer-events: none; background: rgba(8, 14, 26, 0.94); border: 1px solid #36507a; border-radius: 6px; padding: 6px 9px; z-index: 4; max-width: 260px; font-size: 12px; }
#tooltip b { color: var(--gold); }
#legend { position: absolute; left: 10px; bottom: 10px; display: flex; gap: 8px; flex-wrap: wrap; font-size: 11px; color: var(--dim); background: rgba(8, 14, 26, 0.7); padding: 4px 8px; border-radius: 6px; pointer-events: none; }
#legend i { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 3px; vertical-align: -1px; border: 1px solid rgba(255,255,255,.4); }

#toasts { position: absolute; top: 10px; left: 10px; display: flex; flex-direction: column; gap: 6px; z-index: 3; pointer-events: none; max-width: 420px; }
.toast { background: rgba(10, 18, 32, 0.92); border-left: 3px solid var(--gold); padding: 6px 10px; border-radius: 4px; animation: fade 9s forwards; font-size: 12px; }
.toast.war { border-color: var(--red); } .toast.peace { border-color: var(--green); } .toast.conquest, .toast.collapse { border-color: #c06be0; }
.toast small { color: var(--dim); margin-right: 6px; }
@keyframes fade { 0% { opacity: 0; transform: translateX(-10px); } 5% { opacity: 1; transform: none; } 85% { opacity: 1; } 100% { opacity: 0; } }

#settCard { position: absolute; right: 10px; bottom: 10px; width: 250px; background: rgba(10, 18, 32, 0.95); border: 1px solid #36507a; border-radius: 8px; padding: 10px 12px; z-index: 3; }
#settCard h3 { margin: 0 0 2px; font: 600 15px var(--serif); }
#settCard .row { display: flex; justify-content: space-between; color: var(--dim); }
#settCard .row b { color: var(--text); font-weight: 500; }
#settCard .x { position: absolute; top: 6px; right: 8px; cursor: pointer; color: var(--dim); }

/* ---- civ list ---- */
#civlist { max-height: 200px; overflow-y: auto; border-bottom: 1px solid var(--line); }
.civrow { display: grid; grid-template-columns: 12px 1fr auto auto auto; gap: 8px; align-items: center; padding: 4px 12px; cursor: pointer; }
.civrow:hover { background: var(--panel2); }
.civrow.sel { background: #1c2f4d; }
.civrow .sw { width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(255,255,255,.35); }
.civrow .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.civrow .num { color: var(--dim); font-variant-numeric: tabular-nums; text-align: right; min-width: 44px; }
.civrow .war { color: var(--red); }
.fallen { padding: 4px 12px; color: var(--dim); font-size: 11px; }

#tabs { display: flex; border-bottom: 1px solid var(--line); background: #0e1727; }
#tabs button { flex: 1; background: none; border: none; padding: 8px 4px; cursor: pointer; color: var(--dim); border-bottom: 2px solid transparent; }
#tabs button.on { color: var(--gold); border-bottom-color: var(--gold); }
#tabBody { flex: 1; overflow-y: auto; padding: 10px 12px 24px; min-height: 0; }

/* ---- civ detail ---- */
.civhead { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.civhead .big { width: 34px; height: 34px; border-radius: 8px; border: 2px solid rgba(255,255,255,.5); flex: none; }
.civhead h2 { margin: 0; font: 700 19px var(--serif); }
.civhead .sub { color: var(--dim); font-size: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin: 8px 0; }
.stat { display: flex; justify-content: space-between; border-bottom: 1px dotted var(--line); padding: 2px 0; }
.stat span { color: var(--dim); } .stat b { font-weight: 600; font-variant-numeric: tabular-nums; }
h4 { margin: 14px 0 5px; font: 600 11px var(--sans); letter-spacing: 0.1em; text-transform: uppercase; color: var(--gold); }
.bar { display: grid; grid-template-columns: 78px 1fr 30px; gap: 8px; align-items: center; margin: 2px 0; }
.bar > i { display: block; height: 6px; background: #1b2a44; border-radius: 3px; overflow: hidden; }
.bar > i > u { display: block; height: 100%; background: var(--blue); text-decoration: none; }
.bar.red > i > u { background: var(--red); } .bar.green > i > u { background: var(--green); } .bar.gold > i > u { background: var(--gold); }
.bar span { color: var(--dim); } .bar em { font-style: normal; text-align: right; color: var(--dim); font-size: 11px; }
.stock { display: flex; gap: 6px; flex-wrap: wrap; }
.stock div { background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; padding: 2px 7px; }
.stock b { font-variant-numeric: tabular-nums; }
.rel { display: grid; grid-template-columns: 10px 1fr auto auto; gap: 8px; padding: 3px 0; align-items: center; cursor: pointer; }
.rel:hover { background: var(--panel2); }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 8px; text-transform: uppercase; letter-spacing: 0.06em; }
.badge.war { background: #5b2323; color: #ffb4ae; } .badge.peace { background: #1f3a2b; color: #a8e2bd; } .badge.truce { background: #3a3520; color: #e8d38a; }
.sett { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; padding: 2px 0; cursor: pointer; }
.sett:hover { background: var(--panel2); }
.sett .p { color: var(--dim); font-variant-numeric: tabular-nums; }
.muted { color: var(--dim); }
.empty { color: var(--dim); padding: 20px 0; text-align: center; }

/* ---- logs ---- */
.ev { display: grid; grid-template-columns: 52px 1fr; gap: 8px; padding: 4px 0; border-bottom: 1px solid #1a2840; cursor: default; }
.ev.clk { cursor: pointer; } .ev.clk:hover { background: var(--panel2); }
.ev .y { color: var(--dim); font-variant-numeric: tabular-nums; text-align: right; }
.ev.i4 .t, .ev.i5 .t { color: #fff; font-weight: 600; }
.ev.i1 .t { color: var(--dim); }
.ev .k { display: inline-block; width: 6px; height: 6px; border-radius: 50%; margin-right: 5px; background: var(--dim); }
.k.war { background: var(--red); } .k.peace { background: var(--green); } .k.conquest, .k.collapse { background: #c06be0; } .k.battle { background: #e8894f; }
.k.plague, .k.disaster, .k.famine { background: #b0b84a; } .k.succession { background: var(--gold); } .k.trade { background: var(--blue); } .k.secession { background: #e06bb0; }
.century { margin: 14px 0 4px; font: 600 14px var(--serif); color: var(--gold); border-bottom: 1px solid var(--line); }
.filters { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; color: var(--dim); }
.filters select, .filters input[type=range] { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 4px; padding: 2px 4px; }
.note { background: #151f33; border: 1px dashed #314a73; border-radius: 6px; padding: 8px 10px; color: var(--dim); font-size: 12px; margin-bottom: 8px; }
canvas.chart { width: 100%; height: 230px; background: #0d1626; border: 1px solid var(--line); border-radius: 6px; }

/* ---- modal ---- */
#modal { position: fixed; inset: 0; background: rgba(3, 7, 14, 0.7); display: grid; place-items: center; z-index: 20; }
#modal[hidden] { display: none; }
.dialog { width: min(560px, 94vw); max-height: 88vh; overflow: auto; background: var(--panel); border: 1px solid #36507a; border-radius: 10px; padding: 16px 18px; }
.dialog h3 { margin: 0 0 10px; font: 700 18px var(--serif); color: var(--gold); }
.dialog label { display: block; margin: 8px 0 2px; color: var(--dim); }
.dialog input[type=text], .dialog input[type=number], .dialog select { width: 100%; background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 5px; padding: 6px 8px; }
.dialog .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.worldrow { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 5px 0; border-bottom: 1px solid var(--line); }
.worldrow.cur b { color: var(--gold); }

@media (max-width: 900px) {
  main { flex-direction: column; }
  #side { width: 100%; height: 45%; border-left: none; border-top: 1px solid var(--line); }
  .layers { display: none; }
}

.aibadge { font-size: 11px; color: var(--dim); padding: 2px 8px; border: 1px solid var(--line); border-radius: 10px; white-space: nowrap; }
.aibadge.live { color: #9fe8a8; border-color: #3b6b45; } .aibadge.mock { color: #f2d58a; border-color: #6b5a2b; } .aibadge.err { color: #ffb4ae; border-color: #7b3a3a; }
.mind { background: #121c30; border: 1px solid var(--line); border-left: 3px solid var(--gold); border-radius: 6px; padding: 8px 10px; margin: 8px 0; font-size: 12.5px; }
.mind .th { font-style: italic; color: #dfe6f5; margin: 3px 0; } .mind .th small { color: var(--dim); font-style: normal; margin-right: 6px; }
.mind .mem { color: var(--dim); } .mind .fb { color: #ffb4ae; }
.tag { font-size: 10px; padding: 1px 6px; border-radius: 8px; background: #2b3f63; color: #cfe0ff; margin-left: 6px; }
.tag.llm { background: #3b5b2e; color: #d6f5c9; }
.dialog .hint { color: var(--dim); font-size: 11.5px; margin-top: 3px; }
.dialog .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.meter { height: 8px; background: #1b2a44; border-radius: 4px; overflow: hidden; margin: 4px 0 2px; } .meter > u { display: block; height: 100%; background: var(--gold); }

#cutscene { position: fixed; inset: 0; background: rgba(2, 4, 9, 0.88); z-index: 50; display: grid; place-items: center; }
#cutscene[hidden] { display: none; }
.cs-wrap { width: min(960px, 96vw); background: #0b0f1a; border: 1px solid #3a4a6a; border-radius: 10px; overflow: hidden; box-shadow: 0 20px 80px rgba(0,0,0,.7); }
.cs-top { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: #0d1424; border-bottom: 1px solid var(--line); }
#csTitle { font: 700 16px var(--serif); color: var(--gold); flex: 1; } #csYear { color: var(--dim); font-size: 12px; }
#csCanvas { width: 100%; display: block; aspect-ratio: 960 / 440; background: #000; cursor: pointer; }
.cs-box { padding: 12px 18px 10px; min-height: 104px; background: linear-gradient(#0d1424, #080c16); cursor: pointer; border-top: 2px solid #5a4a22; }
#csWho { font: 700 13px var(--serif); letter-spacing: .04em; margin-bottom: 4px; min-height: 16px; }
#csText { font: 17px/1.45 Georgia, serif; color: #f1ead8; min-height: 50px; } #csText.narr { font-style: italic; color: #c9c2ac; }
.cs-hint { color: var(--dim); font-size: 11px; margin-top: 4px; text-align: right; }
.mom { display: grid; grid-template-columns: 52px 1fr auto; gap: 8px; padding: 7px 4px; border-bottom: 1px solid #16213a; cursor: pointer; }
.mom:hover { background: var(--panel2); } .mom .y { color: var(--dim); font-variant-numeric: tabular-nums; } .mom b { font-weight: 600; } .mom small { color: var(--dim); display: block; font-size: 11.5px; }
.mom .play { color: var(--gold); align-self: center; }

#mapStyle { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px; font-size: 12px; margin-right: 4px; }

#stage3d { position: absolute; inset: 0; z-index: 1; background: #9cc0dd; }
#stage3d[hidden] { display: none; }
#toasts, #tooltip, #settCard, #legend { z-index: 5; }
.l3d { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.l3d div { position: absolute; left: 0; top: 0; font: 600 12px Georgia, serif; color: #fff6e0; text-shadow: 0 0 3px #000, 0 1px 2px #000; white-space: nowrap; }
.l3d div.cap { color: #ffe9a8; font-size: 13.5px; }
#btn3d.on { border-color: var(--gold); color: var(--gold); }

/* ---- rise & fall ---- */
.tree { display: flex; align-items: center; gap: 6px; padding: 3px 4px; font-size: 12px; border-left: 2px solid rgba(255,255,255,.08); }
.tree .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.tree small { color: var(--muted, #8a97aa); }
.tree.dead { opacity: .55; }
.k.civilwar, .k.fragmentation { background: #ff7a45; } .k.offshoot, .k.tribe, .k.founding { background: #6fd39b; } .k.laststand { background: #e0476b; }

.k.dragon { background: #e5483a; } .k.monster { background: #6aa84f; } .k.hero { background: #f2c94c; } .k.magic { background: #a98bff; }
.k.invention { background: #5ec8e8; } .k.religion { background: #e8a2f0; } .k.wonder { background: #ffd54a; }
.k.god { background: #ffd54a; } .k.church { background: #c9a7ff; }
