:root {
  --bg: #15110e;
  --panel: #231c16;
  --panel-2: #2e251d;
  --line: #43372b;
  --text: #f3eadf;
  --muted: #b5a797;
  --accent: #ffb547;
  --good: #5fd35f;
  --bad: #e8483a;
  --fuel: #f0a830;
  --hull: #4fb3e8;
  --radius: 10px;
  --header-h: 48px;
  --hud-h: 44px;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}


.page {
  padding: 8px 8px 40px;
}

.game-root {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 1100px;
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- HUD ---------- */

.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--hud-h);
  padding: 0 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
  /* A swipe on the HUD mid-run must not scroll the page. */
  touch-action: none;
}

.hud-cell {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  font-weight: 700;
  font-size: 15px;
  line-height: 1.1;
}

.hud-cell span {
  white-space: nowrap;
}

.hud-cell .lbl {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.hud-cell.cash span:last-child {
  color: var(--accent);
}

.hud-cell.meter {
  width: 110px;
}

.bar {
  height: 10px;
  background: #0d0a08;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  margin-top: 3px;
}

.bar-fill {
  height: 100%;
  width: 0;
  transition: width 0.15s linear;
}

.bar-fill.fuel {
  background: linear-gradient(90deg, #d97b1c, var(--fuel));
}

.bar-fill.hull {
  background: linear-gradient(90deg, #2a78b8, var(--hull));
}

.bar-fill.low {
  background: var(--bad);
  animation: blink 0.6s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0.35;
  }
}

.hud-spacer {
  flex: 1;
}

.btn {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 700;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  color: #1b1108;
  border-color: var(--accent);
}

.btn-icon {
  width: 34px;
  height: 34px;
  padding: 0;
  display: grid;
  place-items: center;
}

/* ---------- Stage ---------- */

.stage {
  position: relative;
  width: 100%;
  height: clamp(360px, calc(100vh - var(--header-h) - var(--hud-h) - 28px), 820px);
  height: clamp(360px, calc(100dvh - var(--header-h) - var(--hud-h) - 28px), 820px);
  background: #17110d;
  touch-action: none;
}

.stage #game {
  display: block;
  width: 100%;
  height: 100%;
}

.contract-chip {
  position: absolute;
  top: 8px;
  right: 8px;
  max-width: 45%;
  padding: 6px 8px;
  background: rgba(20, 15, 11, 0.78);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
  text-align: right;
}

.contract-chip b {
  color: #8ef58e;
}

/* Placed over the building the rig is at by placeEnter() in main.js. */
.enter-btn {
  position: absolute;
  left: 8px;
  top: 8px;
  white-space: nowrap;
  min-height: 44px;
  padding: 10px 18px;
  font-weight: 800;
  font-size: 16px;
  background: var(--accent);
  color: #1b1108;
  border: 0;
  border-radius: 999px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  animation: glow 1.2s ease-in-out infinite;
}

@keyframes glow {
  50% {
    box-shadow: 0 0 0 6px rgba(255, 181, 71, 0.35), 0 4px 16px rgba(0, 0, 0, 0.5);
  }
}

.items {
  position: absolute;
  left: 8px;
  top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.item-btn {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(35, 28, 22, 0.85);
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.item-btn:disabled {
  opacity: 0.35;
}

.item-btn .count {
  position: absolute;
  right: 2px;
  bottom: 1px;
  font-size: 11px;
  font-weight: 800;
}

.item-btn .key {
  position: absolute;
  left: 3px;
  top: 1px;
  font-size: 9px;
  color: var(--muted);
}

/* ---------- Touch controls ---------- */

.touch {
  position: absolute;
  inset: auto 0 0 0;
  display: flex;
  justify-content: space-between;
  padding: 10px;
  pointer-events: none;
}

.touch-left,
.touch-right {
  display: flex;
  gap: 10px;
  pointer-events: auto;
}

.tbtn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.35);
  background: rgba(20, 15, 11, 0.45);
  font-size: 24px;
  color: #fff;
  touch-action: none;
}

.tbtn.active {
  background: rgba(255, 181, 71, 0.55);
}

.tbtn-up {
  width: 76px;
  height: 76px;
}

/* ---------- Toasts ---------- */

.toasts {
  position: absolute;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  width: min(90%, 420px);
}

.toast {
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(20, 15, 11, 0.9);
  border: 1px solid var(--line);
  font-weight: 700;
  font-size: 14px;
  text-align: center;
  animation: toast-in 0.25s ease-out;
}

.toast.good {
  border-color: var(--good);
}

.toast.bad {
  border-color: var(--bad);
}

.toast.gold {
  border-color: var(--accent);
  color: var(--accent);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

/* ---------- Overlays and shops ---------- */

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(8, 6, 4, 0.72);
  z-index: 5;
}

.overlay.show {
  display: flex;
}

.panel {
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}

/* The panel takes focus only so keys don't land on a Buy button. */
.panel:focus {
  outline: none;
}

.panel h2 {
  margin: 0 0 4px;
  font-size: 22px;
}

.panel .sub {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 14px;
}

/* What the counter is: one line of trade detail under the building's name. */
.panel .trade {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
}

/* The Service Center's counters, one tab each. They wrap on narrow screens. */
.panel .tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0 12px;
}

.panel .tab {
  padding: 6px 11px;
  font-size: 14px;
  color: var(--muted);
}

.panel .tab[aria-selected='true'] {
  color: var(--text);
  border-color: var(--accent);
  box-shadow: inset 0 -3px 0 var(--accent);
}

.panel .row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.panel .row canvas {
  flex: none;
  width: 28px;
  height: 28px;
}

.panel .row .grow {
  flex: 1;
  min-width: 0;
}

.panel .row .name {
  font-weight: 700;
}

.panel .row .desc {
  font-size: 12px;
  color: var(--muted);
}

.panel .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 12px;
}

/* A shop's buttons (Sell all, Fill up, Leave) stay in view at the bottom of
   the panel however long its list, rather than below the fold. */
.panel > .actions:last-child {
  position: sticky;
  bottom: -16px;
  margin: 12px -16px -16px;
  padding: 10px 16px 16px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.tiers {
  display: flex;
  gap: 3px;
  margin-top: 4px;
}

.tiers i {
  width: 14px;
  height: 5px;
  border-radius: 2px;
  background: #3b3128;
}

.tiers i.on {
  background: var(--accent);
}

.price {
  font-weight: 800;
  color: var(--accent);
  white-space: nowrap;
}

.hot {
  color: #ff9a5a;
  font-weight: 800;
  font-size: 11px;
  margin-left: 6px;
}

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.shelf div {
  font-size: 12px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
}

.shelf div.found {
  color: #ffd76a;
  border-color: #6d5a26;
}

.title-screen {
  text-align: center;
}

.logo {
  font-size: clamp(40px, 9vw, 72px);
  font-weight: 900;
  letter-spacing: 4px;
  margin: 0;
  background: linear-gradient(180deg, #ffe08a, #d9822b 60%, #8c3b16);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.title-screen .tag {
  color: var(--muted);
  margin: 4px 0 16px;
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 280px;
  margin: 0 auto;
}

.menu-buttons .btn {
  padding: 12px;
  font-size: 16px;
}

.stats {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  font-size: 14px;
  margin: 10px 0;
}

.stats span:nth-child(even) {
  font-weight: 700;
  text-align: right;
}

.ach {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-top: 1px solid var(--line);
  font-size: 14px;
}

.ach.locked {
  color: var(--muted);
}

.ad-below {
  max-width: 1100px;
  margin: 12px auto 0;
}

/* Keep the ad well clear of the on-screen touch pad, which sits at the
   bottom of the stage on touch screens. */
@media (pointer: coarse) {
  .ad-below {
    margin-top: 72px;
  }
}

.about {
  max-width: 820px;
  margin: 24px auto 0;
  padding: 0 8px;
  line-height: 1.55;
  color: #ddd2c4;
}

.about h1,
.about h2 {
  color: var(--text);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--panel);
  font-size: 12px;
}

@media (max-width: 640px) {
  .hud {
    gap: 6px;
    padding: 0 6px;
  }
  .hud-cell {
    font-size: 13px;
  }
  /* Phone meters are as wide as their labels (BATTERY, FRAME), at least this wide. */
  .hud-cell.meter {
    width: auto;
    min-width: 44px;
  }
  .hud-cell .lbl {
    font-size: 10px;
    letter-spacing: 0;
  }
  .page {
    padding: 4px 0 30px;
  }
  .game-root {
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }
  .contract-chip {
    font-size: 11px;
    max-width: 40%;
  }
}

@media (max-width: 380px) {
  .hud {
    gap: 4px;
  }
  .hud-cell {
    font-size: 12px;
  }
  .hud-cell.meter {
    min-width: 36px;
  }
  .btn-icon {
    width: 30px;
  }
}

/* Landscape phones: the HUD and the stage fill the screen together once the
   game is scrolled to the top (the game does that when a run starts). */
@media (orientation: landscape) and (max-height: 500px) {
  .stage {
    height: calc(100vh - var(--hud-h) - 2px);
    height: calc(100dvh - var(--hud-h) - 2px);
    min-height: 220px;
  }
  .page {
    padding-top: 0;
  }
  .tbtn {
    width: 56px;
    height: 56px;
  }
  .tbtn-up {
    width: 64px;
    height: 64px;
  }
  .toasts {
    bottom: 76px;
  }
}

/* Short screens (landscape phones): dialogs fit without scrolling, so no
   button hides below the fold. Menu buttons sit three to a row and the stats
   two pairs to a row. */
@media (max-height: 500px) {
  .overlay {
    padding: 6px;
  }
  .panel {
    padding: 10px 14px;
  }
  .panel h2 {
    font-size: 18px;
  }
  .panel .sub {
    margin-bottom: 6px;
    font-size: 13px;
  }
  .panel .trade {
    display: none;
  }
  .panel .actions {
    margin-top: 8px;
  }
  .panel > .actions:last-child {
    bottom: -10px;
    margin: 8px -14px -10px;
    padding: 8px 14px 10px;
  }
  .logo {
    font-size: 32px;
  }
  .title-screen .tag {
    margin: 0 0 8px;
    font-size: 13px;
  }
  .stats {
    grid-template-columns: 1fr auto 1fr auto;
    gap: 2px 14px;
    margin: 4px 0 6px;
    font-size: 13px;
  }
  .menu-buttons {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    max-width: none;
  }
  .menu-buttons .btn {
    min-height: 44px;
    padding: 6px 8px;
    font-size: 14px;
  }
}

/* Touch screens: every button is at least 44 px tall (Apple's minimum
   target), and the sound and menu buttons 44x44 in a HUD tall enough to hold
   them. Last, so it wins over the narrow-phone sizes above. */
@media (pointer: coarse) {
  :root {
    --hud-h: 48px;
  }
  .btn {
    min-height: 44px;
  }
  .btn-icon {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
  }
  /* Text at least 12 px, and no key hints on the item buttons. */
  .hud-cell .lbl,
  .item-btn .count,
  .contract-chip {
    font-size: 12px;
  }
  .item-btn .key {
    display: none;
  }
}

/* Phones up to 420 px: tighter HUD spacing, so the widest values (cash in
   the millions, depth near the core) stay on one line with 12 px labels. */
@media (pointer: coarse) and (max-width: 420px) {
  .hud {
    gap: 3px;
    padding: 0 4px;
  }
}

/* 320 px phones: narrower icons and meters as well. */
@media (pointer: coarse) and (max-width: 340px) {
  .btn-icon {
    width: 34px;
  }
  .hud-cell.meter {
    min-width: 32px;
  }
  /* The narrow meters' labels at 11 px, so BATTERY takes little room. */
  .hud-cell.meter .lbl {
    font-size: 11px;
  }
}

/* ---------- Voice message transcripts and the Titan bar ---------- */
.top-stack {
  position: absolute;
  top: 8px;
  left: 64px;
  right: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  z-index: 3;
}
.stage.voice-on .contract-chip,
.stage.arena-on .contract-chip {
  visibility: hidden;
}
.voice-msg {
  pointer-events: auto;
  cursor: pointer;
  display: flex;
  gap: 10px;
  width: min(100%, 580px);
  padding: 8px;
  background: linear-gradient(180deg, rgba(30, 25, 19, 0.95), rgba(14, 11, 9, 0.95));
  border: 1px solid #5a4a36;
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.55);
  animation: toast-in 0.25s ease-out;
}
.voice-msg[hidden] {
  display: none;
}
.voice-msg.urgent {
  border-left-color: var(--bad);
}
.voice-msg.titan {
  border-color: #3a120c;
  border-left-color: #ff5a2a;
  background: linear-gradient(180deg, rgba(26, 8, 5, 0.96), rgba(8, 3, 2, 0.96));
}
.voice-face {
  flex: none;
  width: 72px;
  height: 72px;
  border: 1px solid #000;
  border-radius: 3px;
}
.voice-body {
  flex: 1;
  min-width: 0;
}
.voice-who {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 8px;
  font-size: 13px;
}
.voice-who span {
  color: #b9a58a;
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.voice-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  color: #8f7d66;
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.voice-wave {
  flex: none;
  width: 17px;
  height: 10px;
  background:
    linear-gradient(currentColor, currentColor) 0 50% / 2px 40% no-repeat,
    linear-gradient(currentColor, currentColor) 3px 50% / 2px 80% no-repeat,
    linear-gradient(currentColor, currentColor) 6px 50% / 2px 100% no-repeat,
    linear-gradient(currentColor, currentColor) 9px 50% / 2px 60% no-repeat,
    linear-gradient(currentColor, currentColor) 12px 50% / 2px 90% no-repeat,
    linear-gradient(currentColor, currentColor) 15px 50% / 2px 30% no-repeat;
}
.voice-text {
  margin: 4px 0 0;
  min-height: 3.9em;
  font: 13px/1.35 ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
  color: #eadfcb;
}
.titan-bar {
  width: min(100%, 460px);
  font-size: 12px;
  text-align: center;
  text-shadow: 0 1px 3px #000;
}
.titan-bar[hidden] {
  display: none;
}
.titan-name b {
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #ffd9c2;
}
.titan-name span {
  color: #c9a89a;
}
.titan-track {
  height: 10px;
  margin-top: 3px;
  background: rgba(10, 4, 3, 0.85);
  border: 1px solid #5a1f14;
  border-radius: 5px;
  overflow: hidden;
}
.titan-fill {
  height: 100%;
  background: linear-gradient(90deg, #7a1010, #ff5a2a 70%, #ffc27a);
  transition: width 0.15s linear;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
@media (max-width: 640px) {
  .top-stack {
    left: 58px;
    right: 6px;
    top: 6px;
  }
  .voice-msg {
    gap: 7px;
    padding: 6px;
  }
  .voice-face {
    width: 52px;
    height: 52px;
  }
  .voice-text {
    min-height: 0;
    font-size: 12px;
  }
}
@media (orientation: landscape) and (max-height: 500px) {
  .voice-face {
    width: 44px;
    height: 44px;
  }
  .voice-text {
    min-height: 0;
    font-size: 11.5px;
  }
}
