:root {
  --bg: #010207;
  --panel: rgba(10, 13, 24, 0.86);
  --line: rgba(140, 160, 220, 0.16);
  --text: #d8def0;
  --muted: #7d88a8;
  --accent: #6cd3ff;
  --feral: #ff5a4a;
  --panel-w: 340px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden;
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
canvas#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; touch-action: none; }
canvas#view.dragging { cursor: grabbing; }

#topbar { position: fixed; top: 10px; left: 10px; right: calc(var(--panel-w) + 20px); display: flex; flex-wrap: wrap;
  gap: 8px; align-items: center; padding: 6px 10px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; backdrop-filter: blur(6px); z-index: 5; }
body.panel-hidden #topbar { right: 10px; }
.brand { font-weight: 650; letter-spacing: 0.02em; color: #fff; }
/* Fixed widths so nothing to the right (e.g. the speed slider being dragged) ever shifts. */
.clock { font-variant-numeric: tabular-nums; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--accent);
  width: 15ch; flex: none; white-space: nowrap; }
.speed { display: flex; align-items: center; gap: 6px; }
.speed input { width: 96px; accent-color: var(--accent); }
#speedLabel { font-variant-numeric: tabular-nums; color: var(--muted); width: 104px; flex: none; font-size: 12px; white-space: nowrap; overflow: hidden; }
#speedLabel.lagging { color: #ffb46a; }
.toggles { display: flex; gap: 10px; color: var(--muted); font-size: 12px; }
.toggles label { display: flex; gap: 4px; align-items: center; cursor: pointer; user-select: none; }
.toggles input { accent-color: var(--accent); margin: 0; }
select, input[type=number] { background: #0c1122; color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; font: inherit; }
.btn { background: #141a30; color: var(--text); border: 1px solid var(--line); border-radius: 7px; padding: 4px 10px;
  font: inherit; cursor: pointer; }
.btn:hover { background: #1c2442; }
.btn.tiny { padding: 0 6px; font-size: 11px; line-height: 18px; }
.btn.primary { background: #103049; border-color: #2a6c93; color: #d8f3ff; }
.btn.primary:hover { background: #164063; }
#play { min-width: 38px; }
#panelBtn { margin-left: auto; }

#panel { position: fixed; top: 10px; right: 10px; bottom: 10px; width: var(--panel-w); overflow-y: auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: 4px 12px 12px; backdrop-filter: blur(6px); z-index: 4;
  scrollbar-width: thin; scrollbar-color: #2a3350 transparent; }
body.panel-hidden #panel { display: none; }
#panel section { border-bottom: 1px solid var(--line); padding: 8px 0 10px; }
#panel section:last-child { border-bottom: 0; }
#panel section[hidden] { display: none; }
#inspectorSec h2 { display: flex; justify-content: space-between; }
#panel h2 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); margin: 4px 0 8px; font-weight: 600; }
.stats { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; font-variant-numeric: tabular-nums; }
.stats .k { color: var(--muted); }
.stats .v { text-align: right; color: #eef2ff; }
.stats .v.feral { color: var(--feral); }
.civ { display: grid; grid-template-columns: 12px 1fr auto; gap: 2px 8px; align-items: center; margin-bottom: 6px; }
.civ .sw { width: 10px; height: 10px; border-radius: 50%; }
.civ .nm { font-weight: 600; }
.civ .n { font-variant-numeric: tabular-nums; }
.civ .tech { grid-column: 2 / 4; color: var(--muted); font-size: 11.5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.chart { width: 100%; display: block; margin: 2px 0 8px; }
.chartlabel { font-size: 11px; color: var(--muted); }
.legend span { margin-left: 6px; }
#inspector .title { font-size: 15px; font-weight: 650; color: #fff; }
#inspector .sub { color: var(--muted); margin-bottom: 6px; }
#inspector .bar { height: 5px; background: #1a2038; border-radius: 3px; overflow: hidden; margin: 2px 0 6px; }
#inspector .bar > div { height: 100%; background: var(--accent); }
#inspector .row { margin-top: 8px; }
#inspector .gene { display: inline-block; padding: 1px 6px; border-radius: 4px; background: #151b33; margin-right: 4px; font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 11.5px; }
#log { max-height: 260px; overflow-y: auto; font-size: 12px; scrollbar-width: thin; }
#log .ev { padding: 3px 4px; border-radius: 5px; cursor: pointer; display: grid; grid-template-columns: 62px 1fr; gap: 6px; }
#log .ev:hover { background: #151b33; }
#log .ev .t { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; padding-top: 1px; }
#log .ev.feral .m { color: #ff8a7e; }
#log .ev.sn .m, #log .ev.snreport .m { color: #ffc28a; }
#log .ev.contain .m { color: #9ce0b0; }
#log .ev.schism .m { color: #b9d7ff; }
#log .ev.war .m { color: #e3a6ff; }
#log .ev.extinct .m { color: #c9b39a; }
.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.settings label { display: flex; flex-direction: column; gap: 2px; font-size: 11.5px; color: var(--muted); }
.settings input { width: 100%; }
.row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

#observerBar { position: fixed; left: 50%; transform: translateX(-50%); bottom: 16px; max-width: min(760px, calc(100% - var(--panel-w) - 40px));
  background: rgba(8, 30, 44, 0.9); border: 1px solid #2a6c93; border-radius: 10px; padding: 8px 12px; display: flex; gap: 12px;
  align-items: center; z-index: 6; color: #cfeeff; }
#observerBar[hidden] { display: none; }
body.panel-hidden #observerBar { max-width: calc(100% - 20px); }

#tooltip { position: fixed; pointer-events: none; background: rgba(8, 11, 22, 0.92); border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 8px; font-size: 12px; z-index: 7; white-space: nowrap; }
#tooltip[hidden] { display: none; }

#help { position: fixed; inset: 0; background: rgba(0, 0, 5, 0.6); z-index: 20; display: flex; align-items: center; justify-content: center; padding: 16px; }
#help[hidden] { display: none; }
.helpcard { position: relative; max-width: 720px; max-height: 100%; overflow-y: auto; background: #0a0e1c; border: 1px solid var(--line);
  border-radius: 12px; padding: 18px 22px; line-height: 1.55; }
.helpcard h1 { margin: 0 0 8px; font-size: 20px; }
.helpcard h3 { margin: 14px 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent); }
.helpcard p { margin: 4px 0; color: #c4cbe0; }
.helpcard .close { position: absolute; top: 12px; right: 12px; }

@media (max-width: 1720px) {
  .brand { display: none; }
}
@media (max-width: 820px) {
  :root { --panel-w: 100%; }
  #topbar { right: 10px; gap: 6px; }
  .brand { display: none; }
  .toggles { font-size: 11px; gap: 6px; }
  .speed input { width: 80px; }
  #panel { top: auto; left: 10px; right: 10px; width: auto; height: 46vh; }
  #observerBar { max-width: calc(100% - 20px); font-size: 12px; bottom: calc(46vh + 20px); }
  body.panel-hidden #observerBar { bottom: 16px; }
}

/* tooltips */
#uitip { position: fixed; z-index: 30; max-width: 320px; pointer-events: none; background: #0b1124; color: #dfe6fa;
  border: 1px solid #2c3b66; border-radius: 8px; padding: 7px 10px; font-size: 12px; line-height: 1.45;
  box-shadow: 0 6px 24px rgba(0,0,0,0.5); }
#uitip[hidden] { display: none; }
#uitip b { color: #fff; }
kbd { font: 11px ui-monospace, SFMono-Regular, Menlo, monospace; background: #1a2240; border: 1px solid #2c3b66;
  border-radius: 4px; padding: 0 4px; color: #cfe; }
[data-tip] { cursor: help; }
button[data-tip], input[data-tip], select[data-tip], label[data-tip] { cursor: pointer; }
.stats .k[data-tip] { text-decoration: underline dotted rgba(125,136,168,0.45); text-underline-offset: 3px; }

/* auto-pause menu */
.menuwrap { position: relative; }
#autoPauseState { color: var(--muted); font-size: 11px; }
#autoPauseBtn.on { border-color: #2a6c93; }
#autoPauseBtn.on #autoPauseState { color: var(--accent); }
.menu { position: absolute; top: calc(100% + 6px); left: 0; z-index: 12; background: #0b1020; border: 1px solid var(--line);
  border-radius: 9px; padding: 8px 12px; min-width: 260px; box-shadow: 0 8px 28px rgba(0,0,0,0.55); }
.menu[hidden] { display: none; }
.menu label { display: flex; gap: 7px; align-items: center; padding: 2px 0; cursor: pointer; }
.menu label.master { font-weight: 600; margin-bottom: 4px; }
.menu #apKinds label { padding-left: 4px; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.menu input { accent-color: var(--accent); margin: 0; }

/* event banner */
#eventBar { position: fixed; top: calc(var(--topbar-bottom, 52px) + 10px); left: 50%; transform: translateX(-50%); z-index: 6;
  max-width: min(640px, calc(100% - var(--panel-w) - 40px)); background: rgba(24, 16, 8, 0.93); border: 1px solid #93652a;
  border-radius: 10px; padding: 9px 12px; display: flex; flex-direction: column; gap: 8px; color: #ffe9cc; }
body.panel-hidden #eventBar { max-width: calc(100% - 20px); }
#eventBar[hidden] { display: none; }
#eventBar .row0 { display: flex; gap: 8px; }
.evkind { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: #ffb46a; margin-right: 4px; }

/* legend */
#legend { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; font-size: 12px; }
#legend .li { display: flex; align-items: center; gap: 7px; color: #c4cbe0; }
#legend svg { flex: none; }
#modeLegend { margin-top: 8px; font-size: 11.5px; color: var(--muted); }
#modeLegend .ramp { height: 7px; border-radius: 4px; margin: 3px 0 1px; }
#modeLegend .ends { display: flex; justify-content: space-between; }

@media (max-width: 820px) {
  .clock { width: 14ch; font-size: 12px; }
  #speedLabel { width: 92px; }
  #eventBar { top: auto; bottom: calc(46vh + 20px); max-width: calc(100% - 20px); }
  body.panel-hidden #eventBar { bottom: 16px; }
}

/* mass composition */
.stats .full { grid-column: 1 / 3; margin-top: 6px; }
.massbar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: #1a2038; }
.massbar > div { height: 100%; }
.masslegend { display: flex; flex-wrap: wrap; gap: 2px 10px; font-size: 11px; color: var(--muted); margin-top: 4px; }
.masslegend i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 4px; vertical-align: 0; }
