:root {
  --bg: #05050f;
  --panel: #0c0d1e;
  --panel-2: #14162e;
  --line: #262a52;
  --text: #cfd6f2;
  --dim: #7a82a8;
  --accent: #5ef2ff;
  --good: #7dff9a;
  --bad: #ff6a7a;
  --warn: #ffc23a;
  --metal: #b8c0d8;
  --coal: #8a6a5a;
  --oxygen: #5ec8ff;
}

* { box-sizing: border-box; }
/* `hidden` always hides, whatever display a class gives the element. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'VT323', ui-monospace, monospace;
  font-size: 19px;
  line-height: 1.1;
}

body {
  display: grid;
  grid-template-rows: auto 1fr 112px;
  overflow: hidden;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  padding: 1px 8px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); color: #fff; }
button:disabled, button.off { opacity: 0.45; cursor: not-allowed; }
button.primary { border-color: var(--accent); color: var(--accent); }

#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 2px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-size: 26px; color: var(--accent); letter-spacing: 3px; }
.brand span { font-size: 15px; color: var(--dim); margin-left: 8px; letter-spacing: 0; }
.speed { display: flex; gap: 2px; }
.speed button.on { color: var(--bg); background: var(--accent); border-color: var(--accent); }
#clock { color: var(--dim); min-width: 90px; }
#research-status { color: var(--warn); }
.views { display: flex; gap: 2px; }
.views button.on { color: var(--bg); background: var(--accent); border-color: var(--accent); }
.spacer { flex: 1; }

main {
  display: grid;
  grid-template-columns: 1fr 380px;
  min-height: 0;
}

#mapwrap {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #000;
}
canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }
#map { cursor: crosshair; position: absolute; inset: 0; }
#hint {
  position: absolute;
  top: 8px; left: 50%;
  transform: translateX(-50%);
  background: rgba(12, 13, 30, 0.9);
  border: 1px solid var(--accent);
  padding: 2px 10px;
  color: var(--accent);
  pointer-events: none;
}
#hint:empty { display: none; }
#zoom-controls {
  position: absolute;
  right: 10px; bottom: 10px;
  display: flex; align-items: center; gap: 2px;
  background: rgba(12, 13, 30, 0.85);
  border: 1px solid var(--line);
  padding: 2px;
}
#zoom-controls span { min-width: 36px; text-align: center; color: var(--dim); }

#panel {
  background: var(--panel);
  border-left: 2px solid var(--line);
  overflow-y: auto;
  min-height: 0;
}
/* Empire view: one panel per planet side by side, scrolling sideways; each
   column scrolls on its own. */
main.empire { grid-template-columns: 1fr; }
main.empire #mapwrap, main.empire #panel { display: none; }
#empire {
  display: flex;
  overflow-x: auto;
  min-height: 0;
  scrollbar-color: var(--line) var(--bg);
}
#empire[hidden] { display: none; }
#empire.dragging { cursor: grabbing; user-select: none; }
#empire .col {
  flex: 0 0 380px;
  overflow-y: auto;
  background: var(--panel);
  border-right: 2px solid var(--line);
  scrollbar-color: var(--line) var(--panel);
}
/* Name, picture and resources stay in view while the rest scrolls. */
#empire .col-head {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel);
  border-bottom: 2px solid var(--line);
  padding-bottom: 6px;
}
#empire .col-title { padding: 8px 12px 6px; }
#empire .col-head canvas { width: 100%; height: auto; aspect-ratio: 160 / 112; border-block: 2px solid var(--line); }
#empire .col-res { padding: 0 12px; }
#empire .col-res h3 { margin-top: 6px; }
#empire .col-body { padding: 0 12px 16px; }
/* Fleets dialog list: fixed columns, a heading row per kind of mission. */
.report-table.missions { table-layout: fixed; max-width: 100%; }
.report-table.missions th, .report-table.missions td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.report-table.missions .n { text-align: right; }
.report-table.missions tr.group th { position: static; color: var(--accent); padding-top: 10px; border-bottom: 2px solid var(--line); }
.report-table.missions tr.group small { color: var(--dim); margin-left: 4px; }
/* Recall/Stop: every button the same size, and no taller than a row of text. */
.dialog.wide.mission-list { width: min(1000px, calc(100vw - 32px)); }
/* The Fleets and Reports lists: past about 15 fleets (with their group
   headings) or 20 reports the list scrolls, under its pinned column headers,
   and the dialog stops growing. */
.scroll-list { max-height: min(560px, calc(100vh - 160px)); overflow-y: auto; scrollbar-color: var(--line) var(--panel); }
/* The list reaches into the dialog's right padding so its scrollbar sits in
   that gap, and the rows end in line with the Close button (ui.js sets the
   padding-right that the scrollbar leaves over). The arrival time keeps a
   little more room before the buttons. */
.scroll-list { margin-right: -16px; padding-right: 16px; scrollbar-gutter: stable; }
.report-table.missions .n { padding-right: 15px; }
.scroll-list .report-table th { z-index: 1; }
.report-table.missions tr[data-open] td { height: 24px; }
.report-table.missions td.act { padding: 0; }
.report-table.missions td.act button { width: 100%; height: 21px; padding: 0 4px; line-height: 1; }
/* Mission dialog: the battle report's look, narrower, with label/value tables. */
.dialog.wide.mission { width: min(560px, calc(100vw - 32px)); }
.report-table.mission th { position: static; width: 40%; }
a.goto { cursor: pointer; }
a.goto:hover { text-decoration: underline; text-underline-offset: 4px; }

#detail { width: 100%; height: auto; aspect-ratio: 160 / 112; border-bottom: 2px solid var(--line); }
#info { padding: 8px 12px 16px; }

.info h2 { margin: 0; font-size: 28px; font-weight: normal; }
.info h3 {
  margin: 14px 0 4px;
  font-size: 19px;
  font-weight: normal;
  color: var(--accent);
  border-bottom: 1px dashed var(--line);
  display: flex;
  justify-content: space-between;
}
.info h3 small { color: var(--dim); font-size: 17px; }
.sub { color: var(--dim); }
.row { display: flex; align-items: center; gap: 6px; padding: 2px 0; }
.row .name { flex: 1; min-width: 0; }
.row .n { color: #fff; min-width: 28px; text-align: right; }
.row .desc { color: var(--dim); font-size: 16px; display: block; }
/* A fleet in flight: the whole row opens its mission. */
.row.fleet { cursor: pointer; }
.row.fleet:hover { background: var(--panel-2); }
.rate.pos { color: var(--good); }
.rate.neg { color: var(--bad); }
.cost { font-size: 16px; white-space: nowrap; }
.cost i { font-style: normal; }
.cost .short { color: var(--bad); }
.cost .metal { color: var(--metal); }
.cost .coal { color: var(--coal); }
.cost .oxygen { color: var(--oxygen); }
.res-grid { display: grid; grid-template-columns: auto 1fr auto; gap: 0 10px; }
.res-grid .metal { color: var(--metal); }
.res-grid .coal { color: #c49a82; }
.res-grid .oxygen { color: var(--oxygen); }

.bar { height: 6px; background: #1c1f3c; margin: 2px 0 4px; }
.bar > div { height: 100%; background: var(--accent); }
.bar.warn > div { background: var(--warn); }

.warn-text { color: var(--warn); }
.bad-text { color: var(--bad); }
.good-text { color: var(--good); }
.dim { color: var(--dim); }
.gap { margin-top: 10px; }

.actions { display: flex; gap: 6px; margin-top: 14px; flex-wrap: wrap; }
.actions button { flex: 1; padding: 4px 8px; }
.actions.stacked { flex-direction: column; }

#log {
  background: var(--panel);
  border-top: 2px solid var(--line);
  padding: 4px 12px;
  overflow-y: auto;
  font-size: 17px;
}
#log div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#log .t { color: var(--dim); margin-right: 8px; }
#log .good { color: var(--good); }
#log .bad { color: var(--bad); }

#toast {
  position: fixed;
  bottom: 124px; left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--warn);
  color: var(--warn);
  padding: 4px 14px;
  z-index: 20;
  max-width: min(760px, 90vw);
  text-align: center;
}

#modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 10, 0.7);
  display: grid;
  place-items: center;
  z-index: 10;
}
#modal[hidden] { display: none; }
.dialog {
  background: var(--panel);
  border: 2px solid var(--accent);
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 12px 16px;
}
.dialog h2 { margin: 0 0 6px; font-weight: normal; color: var(--accent); }
.dialog label { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.dialog label span { flex: 1; }
.dialog input, .dialog select {
  font: inherit;
  width: 90px;
  background: #05050f;
  color: #fff;
  border: 1px solid var(--line);
  padding: 0 4px;
}
.dialog select { width: auto; flex: 1; }
.dialog .preview { margin-top: 10px; padding: 6px 8px; background: var(--panel-2); border-left: 3px solid var(--accent); }
.dialog .preview.warn { margin-top: 6px; border-left-color: var(--warn); }
.dialog .err { color: var(--bad); min-height: 1.1em; margin-top: 6px; }

/* ---- battle reports ---- */
#log .report { cursor: pointer; }
#log .report:hover { text-decoration: underline; text-underline-offset: 3px; }
.dialog.wide { width: min(880px, calc(100vw - 32px)); }
.dialog.wide h3 {
  margin: 14px 0 4px;
  font-weight: normal;
  color: var(--accent);
  border-bottom: 1px dashed var(--line);
}
.dialog.wide h3 small { color: var(--dim); }
.report-table { border-collapse: collapse; width: 100%; }
.report-table th { color: var(--dim); font-weight: normal; text-align: left; position: sticky; top: 0; background: var(--panel); }
.report-table th, .report-table td { padding: 1px 10px 1px 0; border-bottom: 1px dashed var(--line); vertical-align: top; }
.report-table a.wiki:hover { color: var(--accent); }
.report-table.list tr[data-open] { cursor: pointer; }
.report-table.list tr[data-open]:hover td { background: var(--panel-2); }
.dialog .rounds { max-height: 340px; overflow: auto; margin-top: 4px; }
.dialog .rounds th, .dialog .rounds td { white-space: nowrap; }
.dialog .scroll-x { overflow-x: auto; }
.dialog .scroll-x th, .dialog .scroll-x td { white-space: nowrap; }
/* Headers stay on one line. On wide screens long lists of ships lost wrap;
   on phones the tables scroll sideways instead. */
@media (min-width: 600px) {
  .dialog .rounds td.lost { white-space: normal; min-width: 8em; }
}
.rounds tr.group th { position: static; }
.dialog .decided { margin: 2px 0 0; padding-left: 18px; }
.dialog .decided li { margin: 1px 0; }

/* ---- manual ---- */
a.wiki { color: inherit; cursor: pointer; text-decoration: none; }
a.wiki:hover { text-decoration: underline; text-underline-offset: 3px; }

#manual {
  position: fixed; inset: 0;
  background: rgba(0, 0, 10, 0.7);
  display: grid;
  place-items: center;
  z-index: 15;
}
#manual[hidden] { display: none; }
.manual {
  position: relative;
  display: grid;
  grid-template-columns: 230px 1fr;
  width: min(1120px, calc(100vw - 32px));
  height: min(760px, calc(100vh - 32px));
  background: var(--panel);
  border: 2px solid var(--accent);
}
.manual nav {
  overflow-y: auto;
  border-right: 2px solid var(--line);
  padding: 10px 0;
}
.manual nav .group { color: var(--accent); padding: 10px 14px 2px; }
.manual nav a { display: block; padding: 1px 14px 1px 24px; color: var(--dim); cursor: pointer; }
.manual nav a:hover { color: #fff; }
.manual nav a.on { color: var(--bg); background: var(--accent); }
.manual nav, .manual article { scrollbar-color: var(--line) var(--panel); }
.manual article { overflow-y: auto; padding: 14px 22px 24px; line-height: 1.25; }
.manual article h2 { margin: 0 0 10px; font-weight: normal; font-size: 30px; color: var(--accent); }
.manual article p { margin: 10px 0; }
.manual article ul { margin: 8px 0; padding-left: 22px; }
.manual article li { margin: 3px 0; }
.manual article a.wiki { color: var(--accent); }
.manual article table { border-collapse: collapse; margin: 12px 0; }
.manual article th { color: var(--dim); font-weight: normal; text-align: left; }
.manual article th, .manual article td { padding: 2px 14px 2px 0; border-bottom: 1px dashed var(--line); vertical-align: top; }
/* The Ships & defences tables: names lined up across all three. */
.manual article .ship-tables th, .manual article .ship-tables td { white-space: nowrap; }
.manual article .ship-tables td:first-child a { display: inline-block; min-width: 6.4em; }
.manual .close { position: absolute; top: 6px; right: 8px; }

@media (max-width: 860px) {
  body { grid-template-rows: auto auto auto 100px; overflow: auto; height: auto; }
  main { grid-template-columns: 1fr; }
  #panel { border-left: 0; border-top: 2px solid var(--line); }
  #mapwrap { min-height: 56vw; }
  #empire { height: 80vh; }
  #empire .col { flex-basis: min(380px, 90vw); }
}

/* ---- main menu ---- */
#menu { position: fixed; inset: 0; z-index: 30; background: #000; overflow: hidden; }
#menu[hidden], #menu [hidden] { display: none; }
#menu-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.menu-ui { position: absolute; display: flex; flex-direction: column; gap: 12px; width: 360px; }
.menu-ui.center { transform: translateX(-50%); }
.menu-ui button {
  font-size: 32px;
  letter-spacing: 2px;
  text-align: left;
  padding: 4px 16px 6px;
  background: rgba(12, 13, 30, 0.88);
  border: 2px solid var(--line);
  border-bottom-width: 4px;
  white-space: nowrap;
}
.menu-ui button::before { content: '> '; color: transparent; }
/* One option is highlighted at a time: the focused one. Hovering focuses it. */
.menu-ui button:hover:not(:focus) { border-color: var(--line); color: var(--text); }
.menu-ui button:focus { border-color: var(--accent); color: var(--accent); outline: none; }
.menu-ui button:focus::before { color: var(--accent); }
/* A note under the label ("coming soon", your name). */
.menu-ui small { display: block; font-size: 17px; letter-spacing: 0; color: var(--dim); padding-left: 26px; }

/* ---- quick game: menu message, lobby panels ---- */
.menu-ui button:disabled { opacity: 0.4; cursor: default; }
.menu-ui button:disabled::before { color: transparent; }
.menu-msg { color: var(--warn); font-size: 19px; max-width: 320px; }
.lobby-panel {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(920px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto;
  background: rgba(12, 13, 30, 0.96); border: 2px solid var(--accent); padding: 14px 18px 16px;
}
.lobby-panel[hidden] { display: none; }
.lobby-panel h2 { margin: 0 0 8px; font-weight: normal; color: var(--accent); }
.lobby-panel h3 { margin: 12px 0 4px; font-weight: normal; color: var(--accent); border-bottom: 1px dashed var(--line); }
.lobby-panel .row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.lobby-panel input:not([type]), .lobby-panel input[type="text"], .lobby-panel select {
  font: inherit; background: #05050f; color: #fff; border: 1px solid var(--line); padding: 1px 6px;
}
.lobby-panel .row input { flex: 1; min-width: 0; }
.lobby-panel .err { color: var(--bad); min-height: 1.1em; margin-top: 6px; }
.lobby-panel .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.lobby-panel .l-list { max-height: 50vh; overflow-y: auto; }
.lobby-panel .l-list td.act button { padding: 0 10px; }
.l-head { display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; }
.l-head h2 { flex: 1; min-width: 0; }
.l-code b { color: #fff; font-weight: normal; letter-spacing: 3px; font-size: 24px; }
.l-body { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 20px; }
@media (max-width: 720px) { .l-body { grid-template-columns: 1fr; } }
.l-slot { display: grid; grid-template-columns: 22px minmax(0, 1fr) auto auto; gap: 10px; align-items: center; padding: 3px 0; border-bottom: 1px dashed var(--line); min-height: 30px; }
.l-slot .l-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.l-slot.locked { opacity: 0.45; }
.l-slot select { font: inherit; background: #05050f; color: var(--text); border: 1px solid var(--line); }
.swatch { display: inline-block; width: 18px; height: 18px; border: 1px solid #000; box-shadow: 0 0 0 1px var(--line); padding: 0; }
.swatch.none { background: transparent; box-shadow: none; }
.swatch.mine { cursor: pointer; box-shadow: 0 0 0 2px #fff; }
.swatch.big { width: 26px; height: 26px; cursor: pointer; }
.swatch.taken { opacity: 0.2; cursor: not-allowed; }
.l-palette { display: flex; gap: 8px; padding: 8px 0; flex-wrap: wrap; }
.l-settings label { display: flex; align-items: center; gap: 10px; padding: 2px 0; }
.l-settings label span { flex: 1; }
.l-chat { height: 160px; overflow-y: auto; background: #05050f; border: 1px solid var(--line); padding: 4px 6px; }
.l-chat b { font-weight: normal; color: var(--accent); }
.l-foot { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.l-foot #l-problem { flex: 1; }
.l-foot button { padding: 4px 14px; }
.dialog.end-screen { text-align: center; }
.dialog.end-screen h2 { font-size: 40px; }
