:root {
  --bg: #0b1424;
  --panel: #121c2e;
  --panel2: #18243a;
  --line: #24324d;
  --text: #e6ecf5;
  --dim: #8b9ab3;
  --indigo: #4b3cc9;
  --neros: #1fb6d9;
  --sky: #f2c94c;
  --danger: #e5484d;
  --ok: #46c26b;
  --font: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sidebar: 330px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.4 var(--font); }
body { display: flex; flex-direction: column; overflow: hidden; }
[hidden] { display: none !important; }
h2, h3, h4 { margin: 0 0 6px; font-weight: 700; letter-spacing: 0.04em; }
h3 { font-size: 11px; text-transform: uppercase; color: var(--dim); margin-top: 10px; }
h4 { font-size: 11px; text-transform: uppercase; color: var(--dim); margin: 12px 0 6px; }
.dim { color: var(--dim); }

/* ---- top bar ---- */
#topbar {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px;
  background: var(--panel); border-bottom: 2px solid var(--line); flex: none; min-height: 42px;
}
.brand { font-weight: 900; letter-spacing: 0.25em; color: var(--sky); text-shadow: 2px 2px 0 var(--indigo); }
.pill { padding: 3px 8px; background: var(--panel2); border: 1px solid var(--line); border-radius: 3px; white-space: nowrap; }
.pill.warn { background: #5a2a10; border-color: #c96a2a; color: #ffd2a8; animation: blink 1s steps(2) infinite; }
.pill.dim { color: var(--dim); }
@keyframes blink { 50% { opacity: 0.55; } }
.spacer { flex: 1; }
#conn { display: flex; align-items: center; gap: 6px; }
#conn .dot { width: 8px; height: 8px; background: var(--danger); display: inline-block; }
#conn.on .dot { background: var(--ok); }

.btn {
  font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-bottom-width: 3px; padding: 4px 10px; cursor: pointer; border-radius: 3px;
}
.btn:hover { background: #22304b; }
.btn:active { transform: translateY(1px); border-bottom-width: 2px; }
.btn.primary { background: #2b3f8f; border-color: #4b5fc9; }
.btn.danger { background: #5a1f24; border-color: #a8353b; }
.btn.small { padding: 2px 6px; font-size: 11px; }
.btn.active { outline: 2px solid var(--sky); }
.mobile-only { display: none; }

/* ---- layout ---- */
main { flex: 1; display: flex; min-height: 0; }
#stage { flex: 1; position: relative; min-width: 0; overflow: hidden; }
#map { width: 100%; height: 100%; display: block; image-rendering: pixelated; cursor: grab; touch-action: none; }
#map.dragging { cursor: grabbing; }
#map.placing { cursor: crosshair; }
#minimap {
  position: absolute; left: 10px; bottom: 10px; width: 180px; height: 180px;
  border: 2px solid var(--line); background: #000; image-rendering: pixelated; cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}
#zoom { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; gap: 4px; }
#zoom .btn { width: 36px; height: 36px; font-size: 18px; padding: 0; }
.toast {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  background: #5a1f24; border: 1px solid #a8353b; padding: 6px 12px; border-radius: 3px;
}

#sidebar {
  width: var(--sidebar); flex: none; background: var(--panel); border-left: 2px solid var(--line);
  overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column;
}
#sidebar section { margin-bottom: 8px; }
#sidebar section.grow { flex: 1; min-height: 160px; display: flex; flex-direction: column; }

/* race cards */
.race {
  border: 1px solid var(--line); border-left: 4px solid var(--c); background: var(--panel2);
  padding: 6px 8px; margin-bottom: 6px;
}
.race .name { color: var(--c); font-weight: 800; letter-spacing: 0.08em; display: flex; justify-content: space-between; }
.race .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px 8px; margin-top: 3px; }
.race .stats b { color: var(--text); }
.race .stats span { color: var(--dim); }
.race .research { margin-top: 4px; font-size: 12px; }
.bar { height: 5px; background: #0a1120; margin-top: 2px; }
.bar > div { height: 100%; background: var(--c); }
.badge { font-size: 10px; padding: 0 4px; border: 1px solid currentColor; margin-left: 4px; }

/* relations */
#relations table { border-collapse: collapse; width: 100%; }
#relations td, #relations th { border: 1px solid var(--line); padding: 3px; text-align: center; font-size: 12px; }
#relations th { color: var(--dim); font-weight: 600; }
.st-war { background: #5a1f24; }
.st-hostile { background: #4a3214; }
.st-neutral { background: #1f2a3e; }
.st-friendly { background: #1d3d2a; }
.st-alliance { background: #1f5a34; }

.chart { width: 100%; height: 100px; display: block; }
.legend { display: flex; gap: 8px; flex-wrap: wrap; font-size: 10px; color: var(--dim); margin-top: 3px; }
.legend span::before { content: ''; display: inline-block; width: 8px; height: 8px; background: var(--c); margin-right: 3px; }

/* log */
#log { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; font-size: 12px; }
#log li { padding: 3px 4px; border-bottom: 1px solid #1a2438; cursor: default; }
#log li.has-pos { cursor: pointer; }
#log li.has-pos:hover { background: var(--panel2); }
#log .y { color: var(--dim); margin-right: 4px; }
#log li.t1 { color: #b9d7ff; }
#log li.t2 { color: #ff9b9b; }
#log li.t3 { color: #ffd98a; }
#log li.t5 { color: #c8a8ff; }
#log li.t6 { color: #8fe3a0; }
#log li.t7 { color: #ffb48a; }

/* cards, popup, modal */
.card { background: var(--panel); border: 2px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,0.5); padding: 10px; border-radius: 4px; }
#popup { position: absolute; top: 10px; left: 10px; width: 270px; max-height: calc(100% - 220px); overflow-y: auto; }
#popup .title { font-weight: 800; color: var(--c); display: flex; justify-content: space-between; align-items: center; }
#popup .kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin-top: 6px; }
#popup .kv span:nth-child(odd) { color: var(--dim); }
#popup .res { display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px; margin-top: 6px; text-align: center; font-size: 11px; }
#popup .res div { background: var(--panel2); padding: 2px; }
#popup ul { margin: 4px 0 0; padding-left: 16px; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 20; }
.modal form { width: min(320px, 92vw); display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 4px 0; }
.error { color: var(--danger); min-height: 1em; }
input, select {
  font: inherit; color: var(--text); background: #0a1120; border: 1px solid var(--line); padding: 4px 6px; border-radius: 3px;
  min-width: 0;
}
input[type=number] { width: 72px; }
input[type=range] { width: 100%; }
label { display: block; margin: 6px 0; }

/* admin panel */
#admin-panel {
  position: fixed; left: 10px; top: 52px; width: 360px; max-width: calc(100vw - 20px);
  max-height: calc(100vh - 70px); overflow-y: auto; z-index: 10;
}
.panel-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.panel-head strong { color: var(--sky); }
.tabs { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.tab { font: inherit; font-size: 11px; background: none; color: var(--dim); border: 1px solid transparent; padding: 2px 6px; cursor: pointer; }
.tab.active { color: var(--text); border-color: var(--line); background: var(--panel2); }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; }
.form-grid { display: grid; grid-template-columns: 1fr 90px; gap: 4px 8px; align-items: center; }
.form-grid input { width: 100%; }
#save-list { list-style: none; padding: 0; margin: 6px 0 0; }
#save-list li { display: flex; gap: 6px; align-items: center; padding: 3px 0; border-bottom: 1px solid var(--line); }
#save-list li .n { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.status { margin-top: 8px; min-height: 1.2em; color: var(--dim); }
.status.err { color: var(--danger); }
.rel-row { display: grid; grid-template-columns: 120px 1fr 40px; gap: 6px; align-items: center; }

/* ---- mobile ---- */
@media (max-width: 800px) {
  .mobile-only { display: inline-block; }
  .brand { display: none; }
  #viewers, #speed { display: none; }
  #date { min-width: 0; overflow: hidden; text-overflow: ellipsis; flex-shrink: 1; }
  #conn .label { display: none; }
  #topbar { gap: 4px; padding: 4px 6px; font-size: 12px; }
  #sidebar {
    position: fixed; right: 0; top: 42px; bottom: 0; width: min(92vw, 360px); z-index: 5;
    transform: translateX(100%); transition: transform 0.2s;
  }
  #sidebar.open { transform: none; }
  #minimap { width: 120px; height: 120px; }
  #popup { width: calc(100% - 20px); max-height: 45%; }
  #admin-panel { left: 6px; right: 6px; width: auto; }
}
