:root {
  --bg: rgba(6, 12, 10, 0.8);
  --bg2: rgba(12, 24, 18, 0.94);
  --line: rgba(141, 255, 176, 0.2);
  --line2: rgba(141, 255, 176, 0.55);
  --fg: #dff7e6;
  --dim: #7fa58d;
  --acc: #8dffb0;
  --amber: #ffc35a;
  --red: #ff5470;
  --blue: #6fc3ff;
  --mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  --sans: 'Chakra Petch', system-ui, sans-serif;
  /* Real heights of the top and build bars, kept up to date by ui.js (both wrap on small screens) */
  --top-h: 58px;
  --bar-h: 96px;
  /* Screen edges, clear of phone notches and rounded corners */
  --st: max(10px, env(safe-area-inset-top));
  --sr: max(10px, env(safe-area-inset-right));
  --sb: max(10px, env(safe-area-inset-bottom));
  --sl: max(10px, env(safe-area-inset-left));
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #0b1420; color: var(--fg);
  font-family: var(--sans); user-select: none; -webkit-user-select: none;
  overscroll-behavior: none; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none;
}
/* The game handles every touch on the map itself: no browser scrolling or zooming there */
#app canvas { display: block; touch-action: none; }
button, .card { touch-action: manipulation; }
kbd {
  font-family: var(--mono); font-size: 10px; padding: 1px 4px; margin: 0 1px;
  border: 1px solid var(--line2); border-radius: 3px; color: var(--acc);
}

/* ---------- HUD shell ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
#topbar, #buildbar, #panel { pointer-events: auto; }

#topbar {
  position: absolute; top: var(--st); left: var(--sl); right: var(--sr);
  display: flex; align-items: stretch; gap: 8px;
}
.stat {
  background: var(--bg); border: 1px solid var(--line); border-radius: 4px;
  padding: 6px 12px; display: flex; flex-direction: column; justify-content: center; min-width: 90px;
}
.stat .lbl { font-size: 10px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--dim); }
.stat .val { font-family: var(--mono); font-size: 20px; font-weight: 700; color: var(--acc); line-height: 1.15; }
.stat.base { min-width: 200px; }
.bar { height: 10px; margin-top: 6px; background: rgba(255, 255, 255, 0.07); border-radius: 2px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; background: var(--acc); transition: width 0.25s, background 0.25s; }
.stat.base.hit { animation: hit 0.4s; }
@keyframes hit { 0%, 60% { border-color: var(--red); box-shadow: 0 0 18px rgba(255, 84, 112, 0.6); } }
.stat.intel { flex: 1; min-width: 0; }
#intel { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#intel b { color: var(--red); font-weight: 600; }
#intel .new { color: var(--amber); }

.btn {
  font-family: var(--sans); font-weight: 600; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--fg); background: var(--bg); border: 1px solid var(--line2); border-radius: 4px;
  padding: 6px 14px; cursor: pointer;
}
.btn:hover { background: rgba(141, 255, 176, 0.12); }
.btn.primary { color: #062012; background: var(--acc); border-color: var(--acc); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.primary kbd { color: #062012; border-color: rgba(6, 32, 18, 0.4); }
.btn.big { font-size: 16px; padding: 12px 28px; }
.btn:disabled { opacity: 0.4; cursor: default; filter: none; }
.btn.icon { font-family: var(--mono); font-size: 11px; padding: 6px 10px; }
.btn.icon.off { color: var(--dim); text-decoration: line-through; }

.speed { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
.speed button {
  font-family: var(--mono); font-size: 12px; color: var(--dim); background: none; border: 0;
  border-right: 1px solid var(--line); padding: 0 11px; cursor: pointer;
}
.speed button:last-child { border-right: 0; }
.speed button.on { color: #062012; background: var(--acc); }

/* ---------- Build bar ---------- */
/* Centred boxes use width: max-content, or `left: 50%` would cap them at half the screen */
#buildbar {
  position: absolute; left: 50%; bottom: var(--sb); transform: translateX(-50%);
  display: flex; gap: 6px; padding: 6px; width: max-content; max-width: calc(100vw - 20px);
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px;
  overflow-x: auto; scrollbar-width: none; touch-action: pan-x; overscroll-behavior-x: contain;
}
#buildbar::-webkit-scrollbar { display: none; }
.card {
  flex: 0 0 auto; position: relative; width: 104px; padding: 8px 8px 7px; border: 1px solid var(--line); border-radius: 4px;
  background: rgba(255, 255, 255, 0.02); cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.card:hover { border-color: var(--line2); background: rgba(141, 255, 176, 0.07); }
.card.active { border-color: var(--acc); background: rgba(141, 255, 176, 0.16); box-shadow: 0 0 14px rgba(141, 255, 176, 0.25); }
.card .key { position: absolute; top: 4px; right: 6px; font-family: var(--mono); font-size: 10px; color: var(--dim); }
.card svg { width: 26px; height: 26px; display: block; margin-bottom: 4px; }
.card .name { font-size: 12px; font-weight: 600; line-height: 1.1; height: 27px; }
.card .cost { font-family: var(--mono); font-size: 13px; color: var(--acc); margin-top: 2px; }
.card.poor .cost { color: var(--red); }
.card.poor { opacity: 0.65; }
.card.locked .cost { color: var(--amber); font-size: 11px; }
.card.locked svg { opacity: 0.35; }
.card.sealed { opacity: 0.38; cursor: default; }
.card.sealed .cost { color: var(--dim); }

/* ---------- Selection panel ---------- */
#panel {
  position: absolute; right: var(--sr); top: calc(var(--st) + var(--top-h) + 10px); width: 250px; padding: 12px 14px;
  max-height: calc(100vh - var(--st) - var(--top-h) - var(--bar-h) - var(--sb) - 30px);
  max-height: calc(100dvh - var(--st) - var(--top-h) - var(--bar-h) - var(--sb) - 30px);
  overflow-y: auto; touch-action: pan-y; overscroll-behavior: contain;
  background: var(--bg2); border: 1px solid var(--line2); border-radius: 6px;
}
#panel.hidden, #tooltip.hidden { display: none; }
#panel h3 { margin: 0 0 2px; font-size: 17px; letter-spacing: 0.04em; }
#panel .lvl { font-family: var(--mono); font-size: 11px; color: var(--amber); }
#panel .tag { display: inline-block; margin-top: 6px; font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--blue); border: 1px solid rgba(111, 195, 255, 0.4); border-radius: 3px; padding: 1px 6px; }
#panel .rows { margin: 10px 0; font-family: var(--mono); font-size: 12px; }
#panel .rows div { display: flex; justify-content: space-between; padding: 2px 0; border-bottom: 1px dashed rgba(141, 255, 176, 0.1); }
#panel .rows span:first-child { color: var(--dim); }
#panel .actions { display: flex; gap: 6px; }
#panel .actions .btn { flex: 1; padding: 7px 6px; font-size: 12px; }
#panel .btn.sell { border-color: rgba(255, 84, 112, 0.5); }

/* ---------- Tooltip ---------- */
#tooltip {
  position: absolute; bottom: calc(var(--sb) + var(--bar-h) + 56px); left: 50%; transform: translateX(-50%);
  width: min(340px, calc(100vw - 20px)); padding: 10px 12px;
  background: var(--bg2); border: 1px solid var(--line2); border-radius: 6px; pointer-events: none; font-size: 13px;
}
#tooltip h4 { margin: 0 0 4px; font-size: 15px; }
#tooltip p { margin: 0 0 6px; color: #b9d8c3; line-height: 1.35; }
#tooltip .specs { font-family: var(--mono); font-size: 11px; color: var(--acc); }

/* ---------- Toasts, floaters, hint ---------- */
#toasts { position: absolute; top: calc(var(--st) + var(--top-h) + 8px); left: 50%; transform: translateX(-50%); width: max-content; max-width: calc(100vw - 20px); display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast {
  background: var(--bg2); border: 1px solid var(--line2); border-left: 3px solid var(--acc); border-radius: 4px;
  padding: 8px 14px; font-size: 14px; max-width: 520px; animation: toast-in 0.25s ease-out;
}
.toast.warn { border-left-color: var(--amber); }
.toast.bad { border-left-color: var(--red); }
.toast b { color: var(--amber); }
.toast.out { opacity: 0; transition: opacity 0.4s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

#floaters { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.floater {
  position: absolute; font-family: var(--mono); font-weight: 700; font-size: 13px; color: var(--acc);
  text-shadow: 0 0 6px rgba(0, 0, 0, 0.9); transform: translate(-50%, -50%); animation: float-up 1.1s ease-out forwards;
}
@keyframes float-up { to { opacity: 0; margin-top: -34px; } }

#modebar {
  position: absolute; bottom: calc(var(--sb) + var(--bar-h) + 10px); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px; width: max-content; max-width: calc(100vw - 20px);
}
#hint {
  font-size: 13px; color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 4px;
  padding: 5px 12px; opacity: 0; transition: opacity 0.2s;
}
#hint.on { opacity: 1; }
#btn-cancel { pointer-events: auto; white-space: nowrap; border-color: rgba(255, 84, 112, 0.6); }

/* ---------- Overlay (start / end) ---------- */
#overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: var(--st) var(--sr) var(--sb) var(--sl);
  background: radial-gradient(ellipse at center, rgba(5, 10, 14, 0.55), rgba(5, 10, 14, 0.9));
}
#overlay.hidden { display: none; }
.sheet {
  max-width: 560px; max-height: 100%; overflow-y: auto; touch-action: pan-y; overscroll-behavior: contain;
  padding: 28px 32px; background: var(--bg2); border: 1px solid var(--line2); border-radius: 8px;
  box-shadow: 0 0 60px rgba(141, 255, 176, 0.08);
}
.sheet .eyebrow { font-size: 11px; letter-spacing: 0.22em; text-transform: uppercase; color: var(--amber); }
.sheet h1 { margin: 4px 0 10px; font-size: 52px; letter-spacing: 0.12em; color: var(--acc); text-shadow: 0 0 24px rgba(141, 255, 176, 0.45); }
.sheet .lede { color: #b9d8c3; line-height: 1.5; margin: 0 0 14px; }
.sheet .controls { margin: 0 0 22px; padding-left: 18px; color: var(--dim); font-size: 13px; line-height: 1.9; }
.sheet .controls b { color: var(--fg); }
.sheet .stats { font-family: var(--mono); font-size: 13px; margin: 0 0 20px; }
.sheet .stats div { display: flex; justify-content: space-between; padding: 3px 0; border-bottom: 1px dashed var(--line); }
.sheet .row { display: flex; gap: 10px; }

.hidden { display: none !important; }
.card.busy { opacity: 0.6; cursor: default; }
.card.busy .cost { color: var(--blue); }
.stat .val.rp { color: var(--blue); }

/* ---------- Build bar tabs ---------- */
#buildbar .cards { display: flex; gap: 6px; flex: 0 0 auto; }
#buildbar .tabs {
  display: flex; flex-direction: column; justify-content: center; gap: 4px; flex: 0 0 auto;
  padding-right: 6px; border-right: 1px solid var(--line);
  position: sticky; left: 0; z-index: 1; background: var(--bg2); /* stays put while the cards scroll on a phone */
}
#buildbar .tabs button {
  font-family: var(--sans); font-weight: 600; font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--dim); background: none; border: 1px solid var(--line); border-radius: 3px; padding: 5px 8px; cursor: pointer;
}
#buildbar .tabs button.on { color: #062012; background: var(--acc); border-color: var(--acc); }
#buildbar .tabs kbd { align-self: center; }

/* ---------- Research & contracts ---------- */
#cmdbar { position: absolute; top: calc(var(--st) + var(--top-h) + 8px); left: var(--sl); display: flex; gap: 6px; pointer-events: auto; }
#cmdbar .btn { position: relative; }
#cmdbar .btn[data-badge]:not([data-badge=""])::after {
  content: attr(data-badge); position: absolute; top: -7px; right: -7px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--amber); color: #1a1205; font: 700 11px/18px var(--mono); text-align: center;
}
#command {
  position: absolute; top: calc(var(--st) + var(--top-h) + 52px); left: var(--sl); width: 360px; max-width: calc(100vw - 20px);
  max-height: calc(100vh - var(--st) - var(--top-h) - var(--bar-h) - var(--sb) - 70px);
  max-height: calc(100dvh - var(--st) - var(--top-h) - var(--bar-h) - var(--sb) - 70px);
  overflow-y: auto; touch-action: pan-y; overscroll-behavior: contain;
  pointer-events: auto; background: var(--bg2); border: 1px solid var(--line2); border-radius: 6px;
}
#command .chead { display: flex; position: sticky; top: 0; z-index: 1; background: var(--bg2); border-bottom: 1px solid var(--line); }
#command .ctab {
  flex: 1; font: 600 13px var(--sans); letter-spacing: 0.08em; text-transform: uppercase; color: var(--dim);
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 8px; cursor: pointer;
}
#command .ctab.on { color: var(--fg); border-bottom-color: var(--acc); }
#command .cclose { background: none; border: 0; color: var(--dim); font-size: 14px; padding: 0 12px; cursor: pointer; }
#command .cclose:hover { color: var(--fg); }
#command .cmeta { padding: 8px 12px; font-size: 12px; color: var(--dim); border-bottom: 1px dashed var(--line); }
#command .cmeta b { color: var(--blue); font-family: var(--mono); }
#command .cbody { padding: 10px 12px 12px; }
#command .branches { display: flex; gap: 4px; margin-bottom: 6px; }
#command .branches button {
  flex: 1; font: 600 11px var(--sans); letter-spacing: 0.06em; text-transform: uppercase; color: var(--dim);
  background: rgba(255, 255, 255, 0.03); border: 1px solid var(--line); border-radius: 3px; padding: 6px 4px; cursor: pointer;
}
#command .branches button.on { color: var(--fg); border-color: var(--line2); background: rgba(141, 255, 176, 0.1); }
#command .blurb { font-size: 12px; color: #b9d8c3; margin: 4px 0 10px; line-height: 1.4; }
#command .empty { font-size: 12px; color: var(--dim); margin: 2px 0 10px; }
#command .csec { font-size: 10px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--dim); margin: 6px 0; }
.node, .contract { border: 1px solid var(--line); border-radius: 4px; padding: 8px 10px; margin-bottom: 6px; background: rgba(255, 255, 255, 0.02); }
.node[data-cmd] { cursor: pointer; }
.node[data-cmd]:hover { border-color: var(--line2); background: rgba(141, 255, 176, 0.07); }
.node.done { border-color: rgba(141, 255, 176, 0.35); background: rgba(141, 255, 176, 0.06); }
.node.done .ncost { color: var(--acc); }
.node.blocked { opacity: 0.5; }
.node.poor .ncost { color: var(--red); }
.node.focus { border-color: var(--amber); animation: focus 0.8s ease-in-out 3; }
@keyframes focus { 50% { box-shadow: 0 0 0 2px rgba(255, 195, 90, 0.6); } }
.nhead { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; font-weight: 600; }
.ncost { font-family: var(--mono); font-size: 12px; color: var(--blue); white-space: nowrap; }
.ndesc { font-size: 12px; color: #b9d8c3; margin-top: 3px; line-height: 1.35; }
.nreq { font-size: 11px; color: var(--amber); margin-top: 4px; }
.contract.live { border-color: rgba(111, 195, 255, 0.45); }
.contract .terms { margin: 8px 0; font-size: 12px; }
.contract .terms div { display: flex; justify-content: space-between; gap: 10px; padding: 2px 0; border-bottom: 1px dashed rgba(141, 255, 176, 0.1); }
.contract .terms span { color: var(--dim); }
.contract .terms b { font-weight: 600; text-align: right; }
.contract .btn { width: 100%; }

/* ---------- Small screens and touch ---------- */
@media (max-width: 900px) {
  #topbar { flex-wrap: wrap; gap: 6px; }
  .stat.intel { display: none; }
  .card { width: 82px; }
  .card .name { font-size: 10.5px; }
}

/* Phones held upright: full-width bars, the selection panel as a sheet above the build bar */
@media (max-width: 640px) {
  .stat { min-width: 0; padding: 4px 8px; }
  .stat .lbl { font-size: 9px; letter-spacing: 0.1em; }
  .stat .val { font-size: 16px; }
  .stat.base { flex: 1 1 110px; min-width: 0; }
  #btn-wave { flex: 1 1 auto; padding: 8px 10px; }
  #btn-mute { padding: 6px 8px; }
  .speed button { padding: 0 10px; }
  #buildbar { left: var(--sl); right: var(--sr); transform: none; width: auto; max-width: none; }
  .card { width: 76px; padding: 6px 6px 5px; }
  .card svg { width: 22px; height: 22px; }
  #panel { left: var(--sl); right: var(--sr); width: auto; top: auto; bottom: calc(var(--sb) + var(--bar-h) + 10px); max-height: 46vh; }
  #command { left: var(--sl); right: var(--sr); width: auto; max-width: none; }
  #toasts { top: calc(var(--st) + var(--top-h) + 52px); left: var(--sl); right: var(--sr); transform: none; width: auto; max-width: none; align-items: stretch; }
  #tooltip { bottom: calc(var(--sb) + var(--bar-h) + 76px); }
  .toast { font-size: 12.5px; padding: 6px 10px; max-width: none; }
  #toasts .toast:nth-last-child(n + 3) { display: none; } /* just the latest two */
  #hint { font-size: 12px; }
  .sheet { padding: 18px 18px 20px; }
  .sheet h1 { font-size: 38px; }
  .sheet .lede { font-size: 14px; }
  .sheet .controls { font-size: 12.5px; line-height: 1.55; }
  .sheet .controls li { margin-bottom: 5px; }
}

/* Phones held sideways: very little height, so everything gets slimmer */
@media (max-height: 520px) {
  .stat { padding: 3px 8px; min-width: 64px; }
  .stat.base { min-width: 120px; }
  .stat .val { font-size: 15px; }
  .stat .lbl { font-size: 8.5px; }
  .bar { margin-top: 3px; height: 8px; }
  .btn { padding: 5px 10px; font-size: 12px; }
  #buildbar { padding: 4px; gap: 4px; }
  .card { width: 70px; padding: 4px 5px; }
  .card svg { width: 18px; height: 18px; margin-bottom: 2px; }
  .card .name { height: 24px; font-size: 10px; }
  .card .cost { font-size: 11px; margin-top: 0; }
  #buildbar .tabs button { padding: 3px 6px; font-size: 10px; }
  #toasts { max-width: 40vw; } /* fits between the Research buttons and the selection panel */
  #toasts .toast:nth-last-child(n + 2) { display: none; } /* just the latest one */
  .toast { font-size: 12.5px; padding: 6px 10px; }
  .sheet h1 { font-size: 30px; margin: 2px 0 6px; }
  .sheet .controls { line-height: 1.5; }
}

/* Touch screens: no keyboard hints, and the touch version of the instructions */
@media (pointer: coarse) {
  kbd, .card .key { display: none; }
  .controls.desk { display: none; }
}
@media (pointer: fine) {
  .controls.touch { display: none; }
}
@media (orientation: landscape) {
  .portrait-tip { display: none; }
}
