* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: #15171c;
  color: #e8eaed;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 16px;
  /* Keep the page itself from scrolling/bouncing while the game handles gestures. */
  overscroll-behavior: none;
}

main {
  /* The game canvas owns all touch gestures (pan/pinch); never let them scroll
     or page-zoom the document. */
  touch-action: none;
}

.topbar {
  width: 100%;
  max-width: 2048px;
  display: flex;
  flex-direction: column;    /* two stable rows: the main bar, then the tool bar */
  gap: 10px;
}

/* Row 1 — asset details (left) with credit + action buttons pushed to the right.
   These stay one grouped unit: when the window narrows the button block wraps as a
   whole beneath the asset panel instead of scattering items into random gaps. */
.topbar-main {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
}

/* Every top-bar button shares one size & shape (uniform height, padding, font),
   so the action buttons and the tool toggles line up cleanly. */
.topbar button {
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  border: 1px solid #3a4150;
  border-radius: 6px;
  background: #2a2f38;
  color: #e8eaed;
  cursor: pointer;
}
.topbar button:hover { background: #353b45; }

/* the action buttons stay together as one block when the bar wraps */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.credit {
  font-size: 20px;
  font-weight: 700;
  color: #5cd17a;
  font-variant-numeric: tabular-nums;
  margin-left: auto;
  margin-right: 8px;
}

/* ── Asset details — left-docked panel, same family as the fleet list ── */
.asset-panel {
  position: absolute;
  top: 10px;
  left: 10px;
  bottom: 10px;
  width: 290px;
  z-index: 6;                /* above the fleet list when both happen to be open */
  display: flex;
  flex-direction: column;
  background: rgba(24, 27, 33, 0.94);
  border: 1px solid #2a2e36;
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  font-size: 12px;
  overflow: hidden;
}
.asset-panel[hidden] { display: none; }
/* When the fleet list is open too, slide over so both panels stay visible. */
.asset-panel.with-fleet { left: 310px; }

/* The panel body (#asset-details) holds the stacked fields renderAsset builds. */
#asset-details {
  display: flex;
  flex-direction: column;
}
#asset-details .atitle { display: none; }   /* the panel header already names it */
#asset-details .muted { color: #8b919c; padding: 8px 2px; }
#asset-details .ai {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 4px;
  border-bottom: 1px solid #262b33;
}
#asset-details .ai i {
  font-style: normal;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #9aa0aa;
}
#asset-details .ai b { font-weight: 600; text-align: right; }
/* broken-down status, flagged red in the details panel */
#asset-details .ai-broken i { color: #ff9a8f; }
#asset-details .ai-broken b { color: #ff6a5a; }
#asset-details select {
  background: #2a2e36;
  color: #e8eaed;
  border: 1px solid #3a3f49;
  border-radius: 5px;
  padding: 3px 6px;
  font-size: 12px;
}
.asset-btn {
  width: 100%;
  margin-top: 10px;
  background: #2563c9;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 9px 10px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.asset-btn:hover { background: #2f74e0; }
.asset-btn.active { background: #f2b81c; color: #14161a; }

h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.tools {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* Compact ore key: tight swatch+label chips that wrap rather than ever clip. */
.legend {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 4px 9px;
  font-size: 11px;
  color: #c7ccd4;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 5px;
}

.sw {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  display: inline-block;
  border: 1px solid rgba(255, 255, 255, 0.12);
  flex-shrink: 0;
}

button {
  background: #2c7be5;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
}
button:hover { background: #1f63c0; }

.modes {
  display: flex;
  gap: 6px;
}

/* tool toggles share the base .topbar button style; only the active one is lit */
.modes .mode.active {
  background: #2c7be5;
  border-color: #2c7be5;
  color: #fff;
  box-shadow: inset 0 0 0 2px #5cd17a;
}

#invert-dir {
  background: #6b5b95;
}
#invert-dir:hover { background: #574a7c; }

#auto-toggle {
  background: #2f8f5b;
}
#auto-toggle:hover { background: #267a4c; }
#auto-toggle.active {
  background: #36c07a;
  box-shadow: inset 0 0 0 2px #ffd83b;
}

#vehicle-menu .row {
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}

#vehicle-menu select {
  width: 100%;
  background: #2a2e36;
  color: #e8eaed;
  border: 1px solid #3a3f49;
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
}

main {
  position: relative;
  width: 100%;
  height: 78vh;
  overflow: hidden;
  background: #0e0f12;
  border: 1px solid #2a2e36;
  border-radius: 6px;
}

#mine,
#roads-layer,
#vehicle-layer {
  position: absolute;
  top: 0;
  left: 0;
}
#mine { z-index: 0; cursor: crosshair; }
/* Roads sit above the grid; the vehicles sit above the roads. */
#roads-layer {
  z-index: 1;
  pointer-events: none; /* toggled to "auto" in road / erase mode */
}
#vehicle-layer {
  z-index: 2;
  pointer-events: none; /* let clicks reach the mine canvas */
}

/* ── Left fleet panel (comparable asset list) ── */
.asset-list {
  position: absolute;
  top: 10px;
  left: 10px;
  bottom: 10px;
  width: 290px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  background: rgba(24, 27, 33, 0.94);
  border: 1px solid #2a2e36;
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  font-size: 12px;
  overflow: hidden;
}
.asset-list[hidden] { display: none; }
.al-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid #2a2e36;
  background: #20242c;
}
.al-title { font-weight: 700; letter-spacing: 0.02em; }
.al-count {
  font-variant-numeric: tabular-nums;
  color: #14161a;
  background: #f2b81c;
  border-radius: 10px;
  padding: 1px 8px;
  font-weight: 700;
}
.al-close {
  margin-left: auto;
  background: none;
  border: none;
  color: #9aa0aa;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.al-close:hover { color: #fff; background: none; }
.al-body { overflow-y: auto; padding: 6px; }

.al-group {
  color: #8b919c;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 10px;
  font-weight: 700;
  margin: 8px 4px 4px;
}
.al-group:first-child { margin-top: 2px; }

.al-row {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  align-items: center;
  gap: 6px;
  padding: 6px 7px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}
.al-row:hover { background: #252a33; }
.al-row.sel { border-color: #f2b81c; background: rgba(242, 184, 28, 0.1); }
/* broken-down assets: red-tinted row + a badge next to the name */
.al-row.broken { background: rgba(224, 88, 74, 0.14); }
.al-row.broken.sel { border-color: #e0584a; }
.al-row.broken .al-name { color: #ff8a7e; }
.al-broken { color: #ff6a5a; font-weight: 700; margin-left: 6px; font-variant-numeric: tabular-nums; }
.al-broken:empty { display: none; }
.al-icon { display: flex; align-items: center; justify-content: center; }
.al-svg { width: 32px; height: 24px; display: block; }
.al-main { min-width: 0; }
.al-name { font-weight: 600; }
.al-name .al-model { color: #8b919c; font-weight: 400; margin-left: 4px; }
.al-stats {
  color: #c7ccd4;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.al-stats .al-bad { color: #e06a5b; }
.al-stats .al-ok { color: #36c07a; }
.al-locate {
  background: #2563c9;
  border: none;
  color: #fff;
  border-radius: 5px;
  padding: 4px 7px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
}
.al-locate:hover { background: #2f74e0; }

/* ── Floating placement hint ── */
.place-hint {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  background: rgba(20, 22, 26, 0.92);
  color: #f2b81c;
  border: 1px solid #f2b81c;
  border-radius: 6px;
  padding: 6px 14px;
  font: 600 13px system-ui;
  pointer-events: none;
  white-space: nowrap;
}
.place-hint[hidden] { display: none; }

/* ── Breakdown alert popup ── */
.breakdown-alert {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 7;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(28, 16, 16, 0.96);
  color: #ffd0d0;
  border: 1px solid #e0584a;
  border-radius: 8px;
  padding: 8px 10px 8px 14px;
  font: 600 13px system-ui;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.5);
  max-width: 92%;
}
.breakdown-alert[hidden] { display: none; }
.breakdown-alert .bd-text { white-space: normal; }
.breakdown-alert .bd-locate {
  flex: none;
  background: #2563c9; color: #fff; border: none; border-radius: 6px;
  padding: 5px 9px; font: 600 12px system-ui; cursor: pointer; white-space: nowrap;
}
.breakdown-alert .bd-locate:hover { background: #2f74e0; }
.breakdown-alert .bd-close {
  flex: none;
  background: none; border: none; color: #d99; font-size: 18px; line-height: 1;
  padding: 0 2px; cursor: pointer;
}
.breakdown-alert .bd-close:hover { color: #fff; }

/* ── Parking resize overlay ── */
.park-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;   /* only the handles catch the pointer */
}
.park-overlay[hidden] { display: none; }
.park-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;  /* centre on the (left,top) anchor point */
  background: #f2b81c;
  border: 2px solid #14161a;
  border-radius: 3px;
  box-shadow: 0 0 6px rgba(242, 184, 28, 0.8);
  pointer-events: auto;
  touch-action: none;
}
.park-left, .park-right { cursor: ew-resize; }
.park-top, .park-bottom { cursor: ns-resize; }
.park-label {
  position: absolute;
  margin: -22px 0 0 -8px;
  padding: 1px 6px;
  background: rgba(20, 22, 26, 0.9);
  color: #f2b81c;
  font: 600 11px system-ui;
  border-radius: 3px;
  white-space: nowrap;
  pointer-events: none;
}

.hint {
  font-size: 12px;
  color: #8b919c;
  margin: 0;
}

/* ── Popup ── */
.popup {
  display: none;
  position: fixed;
  z-index: 10;
  width: 220px;
  background: #20242c;
  border: 1px solid #353b45;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  padding: 10px 12px 12px;
  font-size: 13px;
}

.popup header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  margin-bottom: 8px;
}

.popup .close {
  background: none;
  color: #9aa0aa;
  font-size: 18px;
  line-height: 1;
  padding: 0 4px;
}
.popup .close:hover { color: #fff; background: none; }

.popup .bar {
  display: flex;
  height: 10px;
  width: 100%;
  border-radius: 5px;
  overflow: hidden;
  background: #2a2e36;
  margin-bottom: 10px;
}
.popup .bar .seg { height: 100%; }

.popup .row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
}
.popup .row .label { flex: 1; }
.popup .row .pct { color: #c7ccd4; font-variant-numeric: tabular-nums; }
.popup .row.muted { color: #8b919c; }

.popup .swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

.popup .unknown {
  color: #8b919c;
  padding: 6px 0 12px;
}

.popup .note {
  color: #e86a6a;
  font-size: 12px;
  padding-bottom: 8px;
}

.popup .drill {
  width: 100%;
  background: #d98a2b;
  font-weight: 600;
}
.popup .drill:hover { background: #c2761c; }

.popup .total {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #353b45;
  color: #9aa0aa;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* ── Shop (buy assets) ── */
/* .shop-btn / .about-btn share the uniform `.topbar button` style above. */

.shop-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.shop-modal[hidden] { display: none; }

.shop-card {
  width: min(460px, 92vw);
  background: #20242c;
  border: 1px solid #353b45;
  border-radius: 10px;
  padding: 14px 16px;
  color: #e8eaed;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.shop-head {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 8px;
}
.shop-meta { margin-left: auto; font-size: 12px; font-weight: 500; color: #9aa0aa; }

/* ── Mine statistics modal ── */
.stats-card {
  width: min(740px, 94vw);
  background: #20242c;
  border: 1px solid #353b45;
  border-radius: 10px;
  padding: 14px 16px;
  color: #e8eaed;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.stats-totals {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 6px 0 12px;
}
.stat-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #2a2f38;
  border: 1px solid #3a4150;
  border-radius: 6px;
  padding: 4px 9px;
  font-size: 12px;
}
.stat-chip b { font-variant-numeric: tabular-nums; }
.stat-grand { background: #1d3a2a; border-color: #2f8f5b; color: #9be7b8; }
.stats-chart-wrap { background: #15171c; border: 1px solid #2a2e36; border-radius: 8px; padding: 8px; }
.stats-svg { width: 100%; height: auto; display: block; }
.stats-svg .grid { stroke: #2e333d; stroke-width: 1; }
.stats-svg .ax { fill: #8b919c; font: 10px system-ui; }
.stats-empty { color: #8b919c; text-align: center; padding: 40px 0; font-size: 13px; }
.stats-foot { color: #8b919c; font-size: 11px; margin-top: 8px; text-align: center; }
.shop-close {
  background: none; border: none; color: #9aa0aa;
  font-size: 20px; line-height: 1; padding: 0 4px; cursor: pointer;
}
.shop-close:hover { color: #fff; background: none; }

/* ── About / How-to-play modal (reuses .shop-modal backdrop) ── */
.about-card {
  width: min(560px, 94vw);
  max-height: 86vh;
  overflow-y: auto;
  background: #20242c;
  border: 1px solid #353b45;
  border-radius: 10px;
  padding: 14px 18px 18px;
  color: #e8eaed;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.about-head {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 16px; color: #f2b81c;
  padding-bottom: 8px; margin-bottom: 8px; border-bottom: 1px solid #2a2e36;
  position: sticky; top: -14px; background: #20242c;
}
.about-close {
  margin-left: auto; background: none; border: none; color: #9aa0aa;
  font-size: 22px; line-height: 1; padding: 0 4px; cursor: pointer;
}
.about-close:hover { color: #fff; }
.about-body { font-size: 13.5px; line-height: 1.5; }
.about-body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: #5cd17a; margin: 14px 0 4px; }
.about-body ul { margin: 0; padding-left: 18px; }
.about-body li { margin: 2px 0; }
.about-goal { background: #181b21; border: 1px solid #2a2e36; border-radius: 6px; padding: 8px 10px; margin: 0 0 6px; }
.about-flow { color: #c7ccd4; font-style: italic; margin: 0; }
.about-body kbd {
  background: #11141a; border: 1px solid #353b45; border-bottom-width: 2px;
  border-radius: 4px; padding: 0 5px; font: 600 12px ui-monospace, monospace; color: #f2b81c;
}
.about-gh {
  display: inline-block; margin-top: 16px; padding: 7px 14px;
  background: #2563c9; color: #fff; text-decoration: none; border-radius: 6px; font-weight: 600;
}
.about-gh:hover { background: #2f74e0; }
.about-build { margin-left: 10px; color: #7d8590; font: 12px ui-monospace, monospace; }
/* Always-visible build stamp, bottom-left corner — out of the way of input. */
.build-tag {
  position: fixed; left: 8px; bottom: 6px; z-index: 5; pointer-events: none;
  color: #7d8590; opacity: 0.8; font: 11px ui-monospace, monospace;
}

.shop-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid #2a2e36;
}
.shop-info { display: flex; flex-direction: column; gap: 2px; }
.shop-info b { font-size: 14px; }
.shop-spec { font-size: 12px; color: #9aa0aa; }
.shop-price {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  color: #5cd17a;
  font-weight: 600;
}
.shop-buy {
  background: #2c7be5; color: #fff; border: none; border-radius: 6px;
  padding: 6px 10px; font-weight: 600; min-width: 88px; cursor: pointer;
}
.shop-buy:hover:not(:disabled) { background: #1f63c0; }
.shop-buy:disabled { background: #3a3f49; color: #8b919c; cursor: not-allowed; }

/* Touch devices: drop the blue tap-flash so taps feel native, and enlarge the
   parking resize handles into finger-friendly targets. */
.touch button, .touch .al-row, .touch .al-locate { -webkit-tap-highlight-color: transparent; }
.touch .park-handle { width: 30px; height: 30px; margin: -15px 0 0 -15px; }
/* No long-press text-selection / callout on the game UI; keep it on real inputs. */
.touch { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.touch input, .touch select, .touch textarea { -webkit-user-select: auto; user-select: auto; }
.touch button { touch-action: manipulation; }   /* no 300 ms tap delay */

/* ──────────────────────────────────────────────────────────────────────────────
   Phone / narrow layout (≤ 768px). Placed last so it overrides the desktop rules.
   ────────────────────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  body {
    gap: 0;
    height: 100dvh;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }
  main {
    flex: 1 1 auto;     /* fill whatever the chrome leaves */
    height: auto;
    min-height: 0;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  .hint { display: none; }   /* the desktop "scroll to zoom" footer is irrelevant on touch */

  /* ── Compact top bar: icon-only buttons; legend stays visible but tiny ── */
  .topbar {
    width: 100%;
    flex: 0 0 auto;
    column-gap: 6px;
    row-gap: 6px;
    padding: 6px 8px;
  }
  .topbar button { height: 42px; min-width: 42px; padding: 0 9px; font-size: 18px; }
  .topbar button .lbl { display: none; }       /* show just the emoji (each button has a title) */
  .topbar-actions { gap: 6px; }
  /* Keep the ore key visible (never hidden) — just shrink it to a swatch strip
     that wraps onto its own row below the toggles. */
  .tools { gap: 8px 10px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
  .legend { gap: 4px 8px; font-size: 10px; flex-basis: 100%; justify-content: flex-end; order: 2; }
  .modes { gap: 6px; }
  .credit { font-size: 17px; margin-left: auto; margin-right: 4px; }

  /* Modals, the fleet list and the asset panel all fill the screen. */
  .shop-card, .stats-card, .about-card {
    width: 100%; max-width: none;
    height: 100%; max-height: none;
    border-radius: 0;
    overflow-y: auto;
  }
  .asset-list,
  .asset-panel,
  .asset-panel.with-fleet {
    top: 0; left: 0; right: 0; bottom: 0;
    width: auto;
    border-radius: 0;
    border-left: none; border-right: none;
    background: #181b21;   /* fully opaque: full-screen panels must not bleed through */
  }

  /* Finger-friendly targets in lists & modals. */
  .al-row { padding: 10px 8px; }
  .al-locate { padding: 8px 11px; font-size: 14px; }
  .al-close, .shop-close, .about-close { font-size: 26px; padding: 6px 10px; }
  .shop-row { padding: 12px 0; }
  .shop-buy { padding: 11px 14px; min-width: 100px; font-size: 14px; }
  .asset-btn { padding: 10px 12px; font-size: 14px; }
  #asset-details select { padding: 6px 8px; font-size: 14px; }
  .stats-totals { margin-bottom: 14px; }
}
