:root {
  --bg: #05070d;
  --panel: rgba(12, 18, 30, 0.94);
  --line: #24324a;
  --text: #d8e2f0;
  --muted: #7f8ea6;
  --accent: #4fd1ff;
  --good: #5be38a;
  --bad: #ff6b6b;
  --warn: #ffc857;
  font-family: "Segoe UI", system-ui, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
canvas#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; }
.hidden { display: none !important; }
button {
  font: inherit; font-size: 13px; color: var(--text); background: #18243a; border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 10px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: #0f5d7a; border-color: #1b88b0; }
input, select {
  font: inherit; font-size: 13px; color: var(--text); background: #0b1220; border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 8px;
}

#hud {
  position: fixed; top: 12px; left: 12px; width: 250px; padding: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; font-size: 13px;
}
#hud .who { display: flex; justify-content: space-between; font-weight: 600; }
#hud .credits { font-size: 22px; font-weight: 700; color: var(--warn); margin: 4px 0 8px; }
.zone { font-size: 11px; padding: 1px 7px; border-radius: 10px; }
.zone.safe { background: #123d27; color: var(--good); }
.zone.lawless { background: #4a1616; color: var(--bad); }
.bar { display: grid; grid-template-columns: 42px 1fr 64px; align-items: center; gap: 6px; margin: 4px 0; }
.bar label { color: var(--muted); }
.bar div { height: 8px; background: #0b1220; border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
#bar-hp { background: var(--good); }
#bar-fuel { background: var(--warn); }
.bar span { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
#hud-hint { margin-top: 8px; color: var(--accent); min-height: 16px; }

#mini { position: fixed; top: 12px; right: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
#keys { position: fixed; bottom: 12px; left: 50%; transform: translateX(-50%); font-size: 12px; color: var(--muted); background: var(--panel); padding: 6px 12px; border-radius: 8px; white-space: nowrap; }
#keys b { color: var(--text); }

#toasts { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; z-index: 30; }
.toast { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 14px; font-size: 13px; animation: fade 4s forwards; }
.toast.good { border-color: var(--good); color: var(--good); }
.toast.error { border-color: var(--bad); color: var(--bad); }
@keyframes fade { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }

#chat { position: fixed; bottom: 48px; left: 12px; width: 340px; font-size: 13px; }
#chatlog { max-height: 160px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; text-shadow: 0 1px 2px #000; }
#chatlog .sys { color: var(--muted); font-style: italic; }
#chatlog b { color: var(--accent); }
#chatin { width: 100%; background: rgba(11, 18, 32, 0.85); }

.panel {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(820px, calc(100vw - 32px));
  max-height: calc(100vh - 120px); display: flex; flex-direction: column; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; z-index: 20;
}
.panel header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.panel header small { color: var(--muted); font-size: 11px; letter-spacing: 1px; }
.panel h2 { margin: 2px 0 0; font-size: 20px; }
#tabs { display: flex; gap: 4px; padding: 10px 18px 0; flex-wrap: wrap; }
#tabs button { border-radius: 6px 6px 0 0; border-bottom: none; }
#tabs button.on { background: #0f5d7a; }
#tab-body, #econ-body { padding: 14px 18px 18px; overflow-y: auto; font-size: 13px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
td { padding: 5px 6px; border-bottom: 1px solid rgba(36, 50, 74, 0.5); vertical-align: middle; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { margin-left: 3px; padding: 3px 8px; }
.best-sell { color: var(--good); font-weight: 600; }
.best-buy { color: var(--accent); font-weight: 600; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
h3 { font-size: 14px; margin: 14px 0 6px; }
.good { color: var(--good); }
.bad { color: var(--bad); }
.card-mod { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.mods { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }

.modal { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(2, 4, 8, 0.7); z-index: 40; }
.modal .card { width: min(420px, calc(100vw - 32px)); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 26px; text-align: center; }
.modal h1 { letter-spacing: 6px; color: var(--accent); margin: 0 0 8px; }
.modal p { color: var(--muted); font-size: 14px; line-height: 1.5; }
.modal input { width: 100%; margin: 10px 0; padding: 10px; font-size: 15px; text-align: center; }
.modal button { width: 100%; padding: 10px; font-size: 15px; }
#login-err { color: var(--bad); margin-top: 10px; min-height: 18px; font-size: 13px; }

/* ---------- touch controls ---------- */
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
canvas#view, #touch * { touch-action: none; user-select: none; -webkit-user-select: none; }
.panel, .modal { touch-action: pan-y; }
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#stick-zone { position: absolute; left: 0; bottom: 0; width: 50%; height: 70%; pointer-events: auto; }
#stick-help { position: absolute; left: 24px; bottom: 20px; font-size: 12px; color: var(--muted); opacity: 0.7; }
#stick-base {
  position: absolute; display: none; width: 132px; height: 132px; margin: -66px 0 0 -66px; border-radius: 50%;
  border: 2px solid rgba(79, 209, 255, 0.4); background: rgba(79, 209, 255, 0.08);
}
#stick-knob {
  position: absolute; left: 50%; top: 50%; width: 58px; height: 58px; margin: -29px 0 0 -29px; border-radius: 50%;
  background: rgba(79, 209, 255, 0.5); box-shadow: 0 0 16px rgba(79, 209, 255, 0.5);
}
#touch-btns {
  position: absolute; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: flex-end; gap: 12px; pointer-events: auto;
}
.small-row, .big-row { display: flex; gap: 10px; align-items: center; }
.tbtn { width: 80px; height: 80px; border-radius: 50%; font-weight: 700; font-size: 14px; letter-spacing: 1px; background: rgba(24, 36, 58, 0.8); }
.tbtn.fire { width: 92px; height: 92px; background: rgba(122, 31, 44, 0.85); border: 2px solid var(--bad); }
.tbtn.mine { background: rgba(15, 93, 122, 0.85); border: 2px solid var(--accent); }
.tbtn.small { width: 62px; height: 40px; border-radius: 10px; font-size: 12px; }
.tbtn.on { filter: brightness(1.6); transform: scale(0.95); }
.tbtn.ready { border-color: var(--good); color: var(--good); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { box-shadow: 0 0 14px var(--good); } }

/* In touch mode the chat sits under the HUD, out of the joystick's way, and only opens on demand. */
.touch #keys { display: none !important; }
.touch #chat { bottom: auto; top: 200px; width: 260px; pointer-events: none; }
.touch .kbd { display: none; }
.touch #chatlog { max-height: 72px; font-size: 12px; }
.touch #chatin { display: none; pointer-events: auto; }
.touch #chat.typing #chatin { display: block; }

@media (max-width: 700px), (max-height: 500px) {
  #keys { display: none !important; }
  #hud { width: 180px; padding: 8px 10px; font-size: 11px; }
  #hud .credits { font-size: 17px; margin: 2px 0 4px; }
  .bar { grid-template-columns: 34px 1fr 50px; margin: 2px 0; }
  #mini { width: 110px; height: 110px; }
  .touch #chat { top: 158px; width: 220px; }
  .grid2 { grid-template-columns: 1fr; }
  .panel { width: calc(100vw - 16px); max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); }
  .panel header { padding: 10px 14px; }
  .panel h2 { font-size: 17px; }
  #tabs { padding: 8px 14px 0; }
  #tab-body, #econ-body { padding: 10px 14px 14px; overflow-x: auto; }
  .hide-sm { display: none; }
  td.actions { white-space: normal; }
  td.actions button { margin: 2px 0 2px 3px; }
  .tbtn { width: 70px; height: 70px; }
  .tbtn.fire { width: 80px; height: 80px; }
}
@media (max-width: 400px) {
  #mini { display: none; }
}
