:root {
  --bg: #070b16;
  --bg2: #0c1326;
  --panel: #111a30;
  --panel2: #0e1528;
  --border: #233152;
  --text: #e8edf7;
  --muted: #93a2c4;
  --accent: #4cc6ff;
  --accent2: #b98cff;
  --good: #46e0a0;
  --warn: #ffd166;
  --bad: #ff6b7d;
  --radius: 14px;
  --shadow: 0 10px 34px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background:
    radial-gradient(1200px 700px at 80% -10%, #16213f 0%, rgba(22, 33, 63, 0) 60%),
    radial-gradient(900px 600px at -10% 110%, #1a1340 0%, rgba(26, 19, 64, 0) 55%),
    var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.game {
  max-width: 1180px;
  margin: 0 auto;
  padding: 22px 18px 64px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  overflow: hidden;
  white-space: nowrap;
}

.station-title {
  margin: 0;
  font-family: inherit;
  font-size: clamp(22px, 3.4vw, 32px);
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--accent);
  background: transparent;
  border: none;
  border-bottom: 1px dashed transparent;
  padding: 2px 4px 2px 0;
  max-width: min(60vw, 420px);
  border-radius: 4px;
}
.station-title:hover { border-bottom-color: var(--border); }
.station-title:focus { outline: none; color: var(--text); border-bottom-color: var(--accent); }

.resources {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  min-width: 74px;
}

.stat-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--muted);
  font-weight: 600;
}

.stat-value {
  font-size: 16px;
  font-weight: 700;
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
}
.stat-value.is-bad { color: var(--bad); }
.stat-value.is-good { color: var(--good); }

.stat-happy {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin-top: 1px;
}
.stat-happy.is-good { color: var(--good); }
.stat-happy.is-bad { color: var(--bad); }

/* ---------- Stage / canvas ---------- */
.stage {
  position: relative;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: #05070f;
}

#view {
  display: block;
  width: 100%;
  height: min(62vh, 660px);
  touch-action: none;
  cursor: crosshair;
}
#view.idle { cursor: default; }

.inspect {
  position: absolute;
  top: 12px;
  left: 12px;
  width: min(260px, 70%);
  max-height: calc(100% - 24px);
  overflow-y: auto;
  background: rgba(10, 16, 30, 0.94);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  font-size: 12px;
  line-height: 1.45;
}
.inspect-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.inspect-title { font-weight: 800; font-size: 14px; color: var(--accent); }
.inspect-close {
  border: none; background: transparent; color: var(--muted);
  font-size: 18px; line-height: 1; cursor: pointer; padding: 0 2px;
}
.inspect-close:hover { color: var(--text); }
.inspect-line { color: var(--text); }
.inspect-sub { color: var(--muted); margin-top: 6px; font-weight: 700; }
.inspect-factor { color: var(--muted); padding-left: 8px; }

.toast {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%) translateY(8px);
  background: rgba(7, 11, 22, 0.92);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: 90%;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Panels ---------- */
.panels {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
}

@media (max-width: 880px) {
  .panels { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  font-weight: 700;
}

.muted { color: var(--muted); font-weight: 600; }

.hint { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 12px 0 0; }

/* ---------- Build tools (rotate / demolish) ---------- */
.build-tools { display: flex; gap: 8px; margin-bottom: 10px; }
.build-tools button {
  flex: 1 1 auto;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 12px;
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}
.build-tools button:hover { border-color: var(--accent); }
.build-tools button:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.build-tools #rotate-state { color: var(--muted); font-weight: 700; }
.build-tools #rotate-btn.active { border-color: var(--accent); color: var(--accent); background: #112236; }
.build-tools #demolish-btn.active { border-color: var(--bad); color: var(--bad); background: #2a121a; }

/* ---------- Build list ---------- */
.build-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
@media (max-width: 520px) { .build-list { grid-template-columns: 1fr; } }

.build-item {
  text-align: left;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 11px;
  cursor: pointer;
  font: inherit;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: border-color 0.12s ease, transform 0.12s ease, background 0.12s ease;
}
.build-item:hover { border-color: var(--accent); transform: translateY(-1px); }
.build-item:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.build-item.selected { border-color: var(--accent); background: #15223f; box-shadow: 0 0 0 1px var(--accent) inset; }
.build-item:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.bi-top { display: flex; align-items: center; gap: 7px; }
.bi-swatch { width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; }
.bi-name { font-weight: 700; font-size: 13px; }
.bi-cost {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 11px;
  color: var(--muted);
}
.bi-cost .ok { color: var(--good); }
.bi-cost .no { color: var(--bad); }
.bi-work { font-size: 11px; font-weight: 600; color: var(--accent); }
.bi-desc { font-size: 11px; color: var(--muted); line-height: 1.35; }

/* ---------- Ships ---------- */
.ships-list { display: flex; flex-direction: column; gap: 10px; }

.ship-card {
  border: 1px solid var(--border);
  background: var(--panel2);
  border-radius: 10px;
  padding: 10px 11px;
}
.ship-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.ship-name { font-weight: 700; font-size: 13px; }
.ship-timer { font-size: 11px; color: var(--muted); font-family: ui-monospace, Menlo, Consolas, monospace; }
.ship-sub { font-size: 11px; color: var(--accent); font-weight: 600; margin-bottom: 6px; }
.ship-pax { font-size: 12px; color: var(--good); }
.ship-card.cargo { border-left: 3px solid #9fb2d6; }
.ship-card.passenger { border-left: 3px solid #7fe0c8; }
.ship-card.tourist { border-left: 3px solid #ffe066; }
.ship-card.departed { opacity: 0.45; pointer-events: none; filter: grayscale(0.6); }
.ship-goods { display: flex; flex-direction: column; gap: 6px; }

.goods-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.goods-label { font-size: 12px; }
.goods-buy {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  border-radius: 7px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.goods-buy:hover { border-color: var(--accent); }
.goods-buy:disabled { opacity: 0.45; cursor: not-allowed; }

.ships-empty { color: var(--muted); font-size: 12px; font-style: italic; }

/* ---------- Save panel ---------- */
.save-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }

.save-buttons button {
  flex: 1 1 auto;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease;
}
.save-buttons button:hover { border-color: var(--accent); }
.save-buttons button:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.save-buttons button.danger:hover { border-color: var(--bad); color: var(--bad); }

.io-label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 6px; }

#io-area {
  width: 100%;
  min-height: 84px;
  resize: vertical;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.4;
  word-break: break-all;
}
#io-area:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Footer ---------- */
.page-foot {
  margin-top: 40px;
  text-align: center;
  font-size: 13px;
  line-height: 1.7;
  color: var(--muted);
}
.page-foot a { color: var(--muted); text-decoration: none; }
.page-foot a:hover { color: var(--text); text-decoration: underline; }

/* ============================================================================
   New layout: left build rail, centre stage, right ships rail, save popout
   ========================================================================== */
.game { max-width: 1500px; position: relative; --stage-h: min(78vh, 840px); }

.title-wrap { display: flex; align-items: center; gap: 6px; }
.rename-btn {
  border: 1px solid var(--border); background: var(--panel2); color: var(--accent);
  width: 28px; height: 28px; border-radius: 8px; cursor: pointer; font-size: 14px; line-height: 1;
  flex: 0 0 auto;
}
.rename-btn:hover { border-color: var(--accent); }
.station-title { max-width: min(46vw, 360px); }

.save-toggle {
  flex: 0 0 auto;
  border: 1px solid var(--border); background: var(--panel2); color: var(--text);
  font: inherit; font-weight: 700; font-size: 13px; border-radius: 8px; padding: 8px 14px; cursor: pointer;
}
.save-toggle:hover { border-color: var(--accent); }
.save-toggle.active { border-color: var(--accent); color: var(--accent); }

.save-popout {
  position: absolute;
  top: 60px; right: 18px; z-index: 60;
  width: min(360px, 92vw);
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.save-popout h2 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.save-local-note {
  background: #2a1410; border: 1px solid #5a2c1f; color: #ffcdb6;
  border-radius: 8px; padding: 8px 10px; font-size: 12px; line-height: 1.5; margin: 0 0 12px;
}

/* ---------- Workspace ---------- */
.workspace { display: flex; gap: 12px; align-items: stretch; }

.build-rail {
  flex: 0 0 204px; width: 204px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px; overflow: hidden;
  display: flex; flex-direction: column; gap: 6px;
  max-height: var(--stage-h); /* never taller than the canvas, so the stage box stays fixed */
}
.rail-head { font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; white-space: nowrap; }
.build-rail .build-list { display: flex; flex-direction: column; gap: 4px; grid-template-columns: none; overflow-y: auto; flex: 1 1 auto; min-height: 0; }
.build-tools { flex-direction: column; }
.build-tools button { font-size: 13px; text-align: left; }

/* accordion category header (click to expand its modules) */
.build-cat {
  display: block; width: 100%; text-align: left;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--accent2); font-weight: 700;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 9px; cursor: pointer; white-space: nowrap; margin-top: 4px;
}
.build-cat:first-of-type { margin-top: 0; }
.build-cat:hover { border-color: var(--accent); }
.build-cat.open { color: var(--accent); }

/* compact build item: swatch + name + cost */
.build-item {
  display: flex; flex-direction: row; align-items: center; gap: 8px;
  padding: 6px 8px; white-space: nowrap; margin-left: 6px;
}
.bi-swatch { width: 16px; height: 16px; border-radius: 4px; flex: 0 0 auto; }
.bi-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; overflow: hidden; }
.bi-name { font-weight: 700; font-size: 13px; }
.bi-cost { font-size: 10px; }
.bi-cost .ok { color: var(--good); }
.bi-cost .no { color: var(--bad); }

.stage { flex: 1 1 auto; margin-top: 0; min-width: 0; align-self: flex-start; }
#view { height: var(--stage-h); }

.ships-rail {
  flex: 0 0 290px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; overflow-y: auto; max-height: var(--stage-h);
}
.ships-rail h2 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }

/* mythic offer row in the ships panel */
.mythic-row .goods-label { color: #d9a6ff; font-weight: 700; }
.mythic-row .goods-buy { border-color: #8b5cf6; color: #d9a6ff; }
.build-item.mythic-row { border: 1px solid #6b46c1; }
.build-item.mythic-row .bi-name { color: #d9a6ff; }

/* banked-prefabs header in the build menu (not a toggle) */
.build-cat.prefab-head { color: #ffd86b; border-color: #6a5a2a; cursor: default; }

@media (max-width: 900px) {
  .workspace { flex-direction: column; }
  .build-rail { flex: 1 1 auto; width: 100%; }
  .build-rail .build-list { max-height: 40vh; }
  .build-tools { flex-direction: row; }
  .ships-rail { flex: 1 1 auto; max-height: none; }
  #view { height: 54vh; }
  .save-popout { right: 8px; left: 8px; width: auto; }
}
