:root {
  --bg: #05070f;
  --bg2: #0a0f1e;
  --panel: #0d1426;
  --panel2: #121b33;
  --line: #1e2a48;
  --line2: #2b3a60;
  --text: #d9e4ff;
  --muted: #7d8bb0;
  --accent: #4fd1ff;
  --accent2: #ffb347;
  --good: #4ade80;
  --bad: #ff5470;
  --warn: #facc15;
  --war: #fb923c;
  --diplo: #c792ea;
  --radius: 10px;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 'Exo 2', system-ui, sans-serif;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.hidden { display: none !important; }
.mono, .num { font-family: var(--mono); }
.muted { color: var(--muted); }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn { color: var(--warn); }

/* ── Buttons & inputs ── */
.btn, button.btn {
  background: var(--panel2);
  border: 1px solid var(--line2);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); background: #16224a; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: linear-gradient(180deg, #1b6fa0, #11507a); border-color: #3fb8f0; }
.btn.danger { border-color: #7a2436; background: #2a0f18; }
.btn.danger:hover:not(:disabled) { border-color: var(--bad); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.on { border-color: var(--accent); color: var(--accent); }
input[type=text], input[type=number], select {
  background: var(--bg2);
  border: 1px solid var(--line2);
  border-radius: 7px;
  padding: 6px 8px;
  min-width: 0;
}
input[type=number] { width: 90px; font-family: var(--mono); }
input[type=range] { accent-color: var(--accent); width: 100%; }
label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }

/* ── Setup ── */
#setup, #library { height: 100%; overflow-y: auto; background:
  radial-gradient(ellipse at 20% 10%, #12204a 0%, transparent 55%),
  radial-gradient(ellipse at 80% 90%, #2a0f3a 0%, transparent 50%), var(--bg); }
.setup-wrap { max-width: 1100px; margin: 0 auto; padding: 28px 16px 60px; }
.brand { font-family: 'Audiowide', sans-serif; letter-spacing: .12em; margin: 0; font-size: clamp(30px, 6vw, 52px);
  background: linear-gradient(90deg, #8fe3ff, #4fd1ff 40%, #ffb347); -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand span { font-size: .5em; letter-spacing: .3em; }
.tagline { color: var(--muted); margin: 6px 0 24px; }
.setup-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.card {
  background: linear-gradient(180deg, rgba(18, 27, 51, .92), rgba(13, 20, 38, .92));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.card h2 { font-family: 'Audiowide', sans-serif; font-size: 15px; letter-spacing: .08em; margin: 0 0 12px; color: var(--accent); }
.card.wide { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field-row { display: flex; gap: 10px; flex-wrap: wrap; }
.field-row > .field { flex: 1 1 130px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 auto; background: var(--bg2); border: 1px solid var(--line2); border-radius: 7px; padding: 7px 10px; cursor: pointer; }
.seg button.sel { border-color: var(--accent); color: var(--accent); background: #0f2340; }
.race-desc { font-size: 12px; color: var(--muted); min-height: 34px; }
.npc-table { width: 100%; border-collapse: collapse; }
.npc-table th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px 6px; text-transform: uppercase; letter-spacing: .05em; }
.npc-table td { padding: 5px 6px; border-top: 1px solid var(--line); vertical-align: middle; }
.npc-table input[type=text] { width: 100%; }
.npc-swatch { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.aggr-cell { display: flex; align-items: center; gap: 8px; min-width: 190px; }
.aggr-label { font-size: 12px; width: 78px; text-align: right; }
.presets { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.start-row { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
.start-row .btn { padding: 12px 26px; font-size: 16px; font-family: 'Audiowide', sans-serif; letter-spacing: .08em; }
@media (max-width: 720px) {
  .npc-table thead { display: none; }
  .npc-table tr { display: grid; grid-template-columns: 20px 1fr 1fr; gap: 6px; padding: 8px 0; border-top: 1px solid var(--line); }
  .npc-table td { border: 0; padding: 0; }
  .npc-table td.aggr-td { grid-column: 1 / -1; }
  .npc-table td.diff-td { grid-column: 2 / -1; }
}

/* ── Game layout ── */
#game { height: 100%; display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 8px 14px; background: linear-gradient(180deg, #0d1529, #090e1c); border-bottom: 1px solid var(--line);
}
#topbar .brand { font-size: 18px; }
.stat { display: flex; flex-direction: column; line-height: 1.1; }
.stat b { font-family: var(--mono); font-size: 15px; font-weight: 600; }
.stat small { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .08em; }
.time-ctl { display: flex; gap: 6px; margin-left: auto; align-items: center; flex-wrap: wrap; }
.time-ctl select { padding: 4px 6px; }
.planet-switch { display: flex; gap: 6px; align-items: center; }
.planet-switch select { padding: 4px 6px; max-width: 190px; }
#layout { flex: 1; display: grid; grid-template-columns: 1fr 430px; min-height: 0; }
#map-wrap { position: relative; min-height: 0; overflow: hidden; background: #03050b; }
#map { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map.dragging { cursor: grabbing; }
#map.picking { cursor: crosshair; }
#map-controls { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; }
#map-controls button { width: 36px; height: 36px; border-radius: 8px; background: rgba(13, 20, 38, .9); border: 1px solid var(--line2); cursor: pointer; font-size: 18px; }
#map-hud { position: absolute; left: 12px; top: 12px; right: 60px; pointer-events: none; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.hud-pill { pointer-events: auto; background: rgba(13, 20, 38, .9); border: 1px solid var(--line2); border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.hud-pill.pick { border-color: var(--accent2); color: var(--accent2); }
.hud-pill.alert { border-color: var(--bad); color: var(--bad); }

#panel { display: flex; flex-direction: column; min-height: 0; background: var(--panel); border-left: 1px solid var(--line); }
#tabs { display: flex; flex-wrap: wrap; border-bottom: 1px solid var(--line); flex-shrink: 0; }
#tabs button { flex: 1 0 auto; background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 8px; cursor: pointer; color: var(--muted); font-size: 13px; white-space: nowrap; }
#tabs button.sel { color: var(--accent); border-bottom-color: var(--accent); }
#tabs .badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: 10px; padding: 0 5px; margin-left: 3px; }
#panel-body { flex: 1; overflow-y: auto; padding: 14px; }

@media (max-width: 900px) {
  body { overflow: auto; }
  #game { height: auto; min-height: 100%; }
  #layout { grid-template-columns: 1fr; }
  #map-wrap { height: 58vh; }
  #panel { border-left: 0; border-top: 1px solid var(--line); }
  #panel-body { overflow: visible; }
  #topbar { gap: 10px; }
  .time-ctl { margin-left: 0; width: 100%; }
  .planet-switch { width: 100%; }
  .planet-switch select { flex: 1; max-width: none; }
}

/* ── Panel content ── */
.sec { margin-bottom: 16px; }
.sec h3 { font-family: 'Audiowide', sans-serif; font-size: 12px; letter-spacing: .1em; color: var(--accent); margin: 0 0 8px; text-transform: uppercase; }
.ptitle { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.ptitle h2 { margin: 0; font-size: 20px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chip { font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line2); background: var(--bg2); }
.chip.key { border-color: #f5c542; color: #f5c542; }
.chip.danger { border-color: var(--bad); color: var(--bad); }
.kv { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 14px; }
.kv > div { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed var(--line); padding: 3px 0; }
.kv > div span:first-child { color: var(--muted); }
.kv > div span:last-child { font-family: var(--mono); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.bar { height: 6px; background: var(--bg2); border-radius: 3px; overflow: hidden; margin-top: 3px; }
.bar > i { display: block; height: 100%; background: linear-gradient(90deg, #2a8bd0, var(--accent)); }
.list { display: flex; flex-direction: column; gap: 6px; }
.item { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.item.clickable { cursor: pointer; }
.item.clickable:hover { border-color: var(--line2); }
.item.sel { border-color: var(--accent); }
.item-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.item-sub { color: var(--muted); font-size: 12px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { text-align: left; color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; padding: 4px; border-bottom: 1px solid var(--line); }
table.t td { padding: 5px 4px; border-bottom: 1px solid var(--line); }
table.t td.n { text-align: right; font-family: var(--mono); }
table.t tr.clickable { cursor: pointer; }
table.t tr.clickable:hover td { background: var(--panel2); }
.inline { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.ship-grid { display: grid; grid-template-columns: 1fr auto auto; gap: 6px 10px; align-items: center; }
.log-item { font-size: 13px; padding: 6px 0; border-bottom: 1px solid var(--line); display: flex; gap: 8px; }
.log-item time { font-family: var(--mono); font-size: 11px; color: var(--muted); white-space: nowrap; padding-top: 2px; }
.log-item.good { color: var(--good); }
.log-item.bad { color: var(--bad); }
.log-item.warn { color: var(--warn); }
.log-item.war { color: var(--war); }
.log-item.diplo { color: var(--diplo); }
.log-item.system { color: var(--accent); }
.log-item[data-planet] { cursor: pointer; }
.help dt { font-weight: 700; color: var(--accent2); margin-top: 10px; }
.help dd { margin: 3px 0 0; color: #b9c6e8; }
.empty { color: var(--muted); font-style: italic; padding: 8px 0; }
.tech-item { display: flex; justify-content: space-between; gap: 8px; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--line); margin-bottom: 4px; font-size: 13px; }
.tech-item.done { border-color: #1f5f3a; background: #0b2016; }
.tech-item.locked { opacity: .55; }

/* ── Modal & toast ── */
.modal { position: fixed; inset: 0; background: rgba(2, 4, 10, .72); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--panel); border: 1px solid var(--line2); border-radius: 14px; width: min(560px, 100%); max-height: 88vh; overflow-y: auto; padding: 18px; box-shadow: 0 20px 60px rgba(0, 0, 0, .6); }
.modal-card h2 { margin: 0 0 12px; font-family: 'Audiowide', sans-serif; font-size: 17px; color: var(--accent); }
.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: #1a2647; border: 1px solid var(--line2); padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: 90vw; }
.toast.err { border-color: var(--bad); }
.endgame { text-align: center; padding: 10px; }
.endgame .big { font-family: 'Audiowide', sans-serif; font-size: 32px; margin: 10px 0; }
@media (max-width: 480px) {
  .kv { grid-template-columns: 1fr; }
}

/* ── All planets overview ── */
#overview { position: absolute; inset: 0; background: var(--bg); z-index: 5; display: flex; flex-direction: column; padding: 14px 16px 0; min-height: 0; }
.ov-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.ov-head h2 { margin: 0; font-family: 'Audiowide', sans-serif; font-size: 17px; color: var(--accent); }
.ov-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.ov-tabs button { background: var(--panel2); border: 1px solid var(--line2); border-radius: 999px; padding: 5px 12px; cursor: pointer; color: var(--muted); font-size: 13px; }
.ov-tabs button.sel { color: var(--accent); border-color: var(--accent); background: #0f2340; }
.ov-tabs .badge, #topbar .badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: 10px; padding: 0 6px; margin-left: 3px; }
.ov-body { flex: 1; overflow-y: auto; padding-bottom: 16px; min-height: 0; }
.ov-scroll { overflow-x: auto; }
table.ov td { vertical-align: middle; white-space: nowrap; }
table.ov td .bar { min-width: 80px; }
.ov-issue.bad { border-color: #7a2436; }
.ov-issue.warn { border-color: #6b5a1a; }
@media (max-width: 900px) {
  #overview { position: static; min-height: 70vh; }
  #map-wrap:has(#overview:not(.hidden)) { height: auto; }
  #map-wrap:has(#overview:not(.hidden)) #map { display: none; }
}

/* ── Library & cloud status ── */
.lib-card { padding: 12px 14px; }
.cloud-status { font-size: 12px; white-space: nowrap; cursor: pointer; }
