:root {
  --bg: #0d0f14; --panel: rgba(16, 19, 27, .82); --panel-solid: #151923; --line: rgba(255, 255, 255, .12);
  --ink: #f4f1ea; --muted: #a4abb8; --orange: #ff7a1a; --amber: #ffb347; --mint: #6ee7a8; --red: #ff5a4d; --cyan: #34d0f0;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --head: "Arial Black", Impact, system-ui, sans-serif;
  --body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font: 14px/1.45 var(--body); overflow: hidden; -webkit-tap-highlight-color: transparent; }
canvas#c { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
kbd { font: 700 11px var(--mono); padding: 2px 6px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; background: rgba(255, 255, 255, .06); margin-right: 3px; }
.k { font: 700 9px var(--mono); letter-spacing: .14em; color: var(--muted); }
.eyebrow { font: 800 11px var(--mono); letter-spacing: .2em; color: var(--orange); margin: 0 0 10px; }
.fine { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); box-shadow: 0 10px 40px rgba(0, 0, 0, .35); }

/* ------------------------------------------------------------ screens */
.screen { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px; overflow: auto; }
.screen.overlay { background: rgba(6, 8, 12, .62); z-index: 30; }
#menu { background: linear-gradient(180deg, rgba(8, 10, 14, .55), rgba(8, 10, 14, .2) 45%, rgba(8, 10, 14, .7)); }
.menu-card { width: min(760px, 100%); text-align: center; margin: auto; padding: 8px; }
.menu-card h1 { margin: 0; font: 900 clamp(48px, 10vw, 104px)/.9 var(--head); letter-spacing: -.05em; text-transform: uppercase; text-shadow: 0 6px 40px rgba(0, 0, 0, .55); }
.menu-card h1 span { color: var(--orange); }
.tag { color: #d8dbe3; max-width: 560px; margin: 16px auto 22px; text-shadow: 0 2px 12px rgba(0, 0, 0, .7); }
.tag b { color: var(--amber); }
.menu-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.big { display: grid; gap: 3px; text-align: left; padding: 15px 18px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); backdrop-filter: blur(10px); transition: transform .12s, border-color .12s, background .12s; }
.big b { font: 800 17px var(--body); }
.big small { color: var(--muted); font-size: 12px; }
.big:hover { transform: translateY(-2px); border-color: rgba(255, 122, 26, .6); }
.big.primary { background: linear-gradient(135deg, #ff7a1a, #e0560b); border-color: transparent; }
.big.primary small { color: rgba(255, 255, 255, .85); }
.big.slim { text-align: center; padding: 12px 22px; }
.ghost { padding: 9px 14px; border-radius: 9px; border: 1px solid var(--line); background: rgba(16, 19, 27, .6); font-weight: 600; }
.ghost:hover { border-color: rgba(255, 122, 26, .6); }
.ghost.sm { padding: 5px 10px; font-size: 12px; }
.join-row, .menu-row { display: flex; gap: 8px; margin-top: 10px; justify-content: center; flex-wrap: wrap; align-items: center; }
input { font: inherit; color: var(--ink); background: rgba(10, 12, 18, .75); border: 1px solid var(--line); border-radius: 9px; padding: 9px 12px; min-width: 0; }
input:focus { outline: 2px solid rgba(255, 122, 26, .6); outline-offset: 1px; }
#codeIn { width: 150px; text-align: center; letter-spacing: .25em; font: 700 14px var(--mono); text-transform: uppercase; }
.namebox { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.namebox input { width: 150px; }
.msg { min-height: 20px; margin: 14px 0 0; color: var(--amber); font-weight: 600; }
.msg.err { color: var(--red); }
#menuStats { text-shadow: 0 1px 6px #000; }
#shardCount { color: var(--amber); font: 700 12px var(--mono); }


.lobby-card, .depot-card, .over-card, .help-card { width: min(720px, 100%); padding: 24px; margin: auto; }
.lobby-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
h2 { margin: 0; font: 900 clamp(24px, 4vw, 34px)/1.05 var(--head); letter-spacing: -.03em; text-transform: uppercase; }
h3 { margin: 0 0 8px; font: 800 12px var(--mono); letter-spacing: .14em; color: var(--amber); text-transform: uppercase; }
.code-box { display: grid; gap: 4px; justify-items: end; }
.code-box b { font: 800 30px var(--mono); letter-spacing: .18em; color: var(--amber); }
.plist { list-style: none; margin: 18px 0; padding: 0; display: grid; gap: 8px; }
.plist li { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: rgba(255, 255, 255, .03); }
.plist .dot { width: 14px; height: 14px; border-radius: 4px; flex: 0 0 14px; }
.plist .nm { font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plist .tagx { font: 700 10px var(--mono); letter-spacing: .1em; color: var(--muted); }
.plist .rdy { font: 800 10px var(--mono); letter-spacing: .12em; padding: 3px 8px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); }
.plist .rdy.on { color: #062; background: var(--mint); border-color: transparent; }
.plist li.empty { opacity: .35; border-style: dashed; }
.lobby-opts { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 7px 13px; font-weight: 600; }
.seg button.on { background: var(--orange); color: #111; }
.count { font: 800 18px var(--mono); color: var(--amber); min-height: 26px; margin: 12px 0 0; text-align: center; }
.lobby-btns { display: flex; gap: 10px; justify-content: center; margin-top: 12px; flex-wrap: wrap; align-items: center; }

/* ----------------------------------------------------------------- HUD */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#hud button { pointer-events: auto; }
.hud-tl { position: absolute; top: 12px; left: 12px; padding: 10px 14px; display: flex; gap: 16px; }
.stat { display: grid; gap: 2px; }
.stat b { font: 800 18px var(--mono); }
.hud-tc { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); padding: 10px 14px; display: grid; gap: 6px; width: min(340px, calc(100vw - 460px)); min-width: 250px; }
.meter { display: grid; grid-template-columns: 82px 1fr 34px; gap: 8px; align-items: center; }
.meter label { font: 700 9px var(--mono); letter-spacing: .1em; color: var(--muted); }
.meter em { font: 700 12px var(--mono); font-style: normal; text-align: right; }
.bar { height: 9px; background: rgba(255, 255, 255, .1); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 50%; background: var(--mint); border-radius: 99px; transition: width .2s, background .2s; }
.bar.fuel i { background: var(--orange); }
.bar.track i { background: var(--cyan); }
.bar.low i { background: var(--red) !important; }
.hud-tr { position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.crew { padding: 8px 12px; display: grid; gap: 5px; min-width: 170px; }
.crew .row { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.crew .dot { width: 10px; height: 10px; border-radius: 3px; }
.crew .row.off { opacity: .4; }
.crew .h { margin-left: auto; font: 700 11px var(--mono); color: var(--muted); }
.hud-btns { display: flex; gap: 6px; }
.ico { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel); font-weight: 800; display: grid; place-items: center; }
.ico:hover { border-color: var(--orange); }
.alert { position: absolute; top: 118px; left: 50%; transform: translateX(-50%); padding: 10px 22px; border-radius: 10px; background: rgba(160, 20, 12, .9); border: 1px solid #ff8a7d; font: 800 15px var(--mono); letter-spacing: .06em; animation: pulse .7s infinite alternate; text-align: center; }
.alert.info { background: rgba(20, 90, 60, .9); border-color: #6ee7a8; animation: none; }
@keyframes pulse { from { box-shadow: 0 0 0 0 rgba(255, 90, 77, .6); } to { box-shadow: 0 0 30px 6px rgba(255, 90, 77, .45); } }
.objective { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); max-width: min(560px, calc(100vw - 340px)); text-align: center; padding: 8px 16px; border-radius: 99px; background: rgba(10, 12, 18, .72); border: 1px solid var(--line); font-weight: 600; font-size: 13px; color: #e8ebf2; }
.hud-bl { position: absolute; bottom: 12px; left: 12px; padding: 10px 14px; min-width: 190px; max-width: 250px; }
.hand { font: 800 15px var(--mono); margin-top: 3px; display: flex; align-items: center; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 4px; min-height: 17px; }
.chip { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 4px; border: 1px solid rgba(0, 0, 0, .35); }
.chip.wood { background: #b07a3e; } .chip.iron { background: #aab6c6; } .chip.coal { background: #1b1b20; border-color: #555; } .chip.track { background: #8b7d6b; box-shadow: inset 0 0 0 2px #b8c0cc; }
#toasts { position: absolute; bottom: 64px; left: 50%; transform: translateX(-50%); display: grid; gap: 6px; justify-items: center; }
.toast { padding: 7px 16px; border-radius: 99px; background: rgba(10, 12, 18, .9); border: 1px solid var(--line); font-weight: 600; animation: rise 2.6s forwards; white-space: nowrap; }
.toast.good { border-color: var(--mint); } .toast.bad { border-color: var(--red); }
@keyframes rise { 0% { opacity: 0; transform: translateY(8px); } 8% { opacity: 1; transform: none; } 80% { opacity: 1; } 100% { opacity: 0; transform: translateY(-6px); } }

/* -------------------------------------------------- world-space labels */
#labels { position: fixed; inset: 0; z-index: 5; pointer-events: none; overflow: hidden; }
.lbl { position: absolute; left: 0; top: 0; will-change: transform; white-space: nowrap; font: 700 11px var(--mono); text-shadow: 0 1px 3px #000, 0 0 6px #000; transform-origin: 0 0; }
.lbl.tag { padding: 1px 6px; border-radius: 4px; background: rgba(8, 10, 14, .55); }
.lbl.car { padding: 3px 8px; border-radius: 6px; background: rgba(8, 10, 14, .7); border: 1px solid rgba(255, 255, 255, .18); }
.lbl .hp { display: block; width: 34px; height: 4px; background: rgba(0, 0, 0, .6); border-radius: 2px; overflow: hidden; margin: 0 auto; }
.lbl .hp i { display: block; height: 100%; background: var(--red); }
.lbl.ping { padding: 4px 10px; border-radius: 99px; color: #111; font-weight: 800; animation: bob 1s infinite alternate; }
@keyframes bob { from { margin-top: 0; } to { margin-top: -8px; } }
#workRing { position: fixed; z-index: 6; width: 44px; height: 44px; margin: -22px 0 0 -22px; pointer-events: none; }
#workRing svg { width: 100%; height: 100%; transform: rotate(-90deg); }
#workRing circle { fill: none; stroke-width: 5; }
#workRing .bg { stroke: rgba(0, 0, 0, .5); } #workRing .fg { stroke: var(--amber); stroke-dasharray: 100; stroke-dashoffset: 100; stroke-linecap: round; }

/* --------------------------------------------------------------- cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-top: 16px; }
.card { text-align: left; padding: 16px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255, 255, 255, .04); display: grid; gap: 6px; align-content: start; transition: transform .12s, border-color .12s; }
.card:hover:not(:disabled) { transform: translateY(-3px); border-color: var(--orange); }
.card.voted { border-color: var(--mint); box-shadow: 0 0 0 2px rgba(110, 231, 168, .4); }
.card .t { font: 900 16px var(--head); text-transform: uppercase; letter-spacing: -.02em; }
.card .d { color: var(--muted); font-size: 13px; min-height: 52px; }
.card .lv { font: 700 10px var(--mono); letter-spacing: .12em; color: var(--amber); }
.card .votes { font: 800 12px var(--mono); color: var(--mint); min-height: 16px; }
.card .sw { display: flex; gap: 4px; }
.card .sw i { width: 38px; height: 14px; border-radius: 3px; border: 1px solid rgba(0, 0, 0, .3); }
.card:disabled { opacity: .55; cursor: default; }
.depot-timer { height: 6px; background: rgba(255, 255, 255, .1); border-radius: 99px; margin-top: 18px; overflow: hidden; }
.depot-timer i { display: block; height: 100%; width: 100%; background: var(--orange); transition: width .25s linear; }

/* ------------------------------------------------------------- results */
.over-card { text-align: center; }
#overDist { font-size: clamp(44px, 9vw, 84px); color: var(--orange); margin: 4px 0 6px; text-transform: none; }
.flags { min-height: 20px; color: var(--amber); font-weight: 700; margin: 0 0 12px; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 8px; margin: 14px 0; }
.stats-grid div { padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: rgba(255, 255, 255, .03); }
.stats-grid b { display: block; font: 800 20px var(--mono); }
.crew-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 6px; }
.crew-table th { font: 700 9px var(--mono); letter-spacing: .12em; color: var(--muted); text-align: right; padding: 4px 8px; }
.crew-table td { padding: 6px 8px; border-top: 1px solid var(--line); text-align: right; font-family: var(--mono); }
.crew-table th:first-child, .crew-table td:first-child { text-align: left; font-family: var(--body); font-weight: 600; }
.reward { color: var(--amber); font-weight: 700; min-height: 20px; margin: 10px 0 0; }

/* ---------------------------------------------------------------- help */
.help-card { width: min(860px, 100%); }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; margin-top: 16px; }
.help-grid ol { padding-left: 20px; margin: 0; display: grid; gap: 6px; }
.keys { border-collapse: collapse; width: 100%; font-size: 13px; }
.keys td { padding: 5px 6px 5px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.keys td:first-child { white-space: nowrap; }

.conn { position: fixed; top: 0; left: 50%; transform: translateX(-50%); z-index: 60; padding: 6px 16px; border-radius: 0 0 10px 10px; background: var(--red); font-weight: 700; font-size: 13px; }

/* --------------------------------------------------------------- touch */
.touch { position: absolute; inset: auto 0 0 0; height: 220px; pointer-events: none; }
.touch > * { pointer-events: auto; }
#stick { position: absolute; left: 22px; bottom: 26px; width: 130px; height: 130px; border-radius: 50%; background: rgba(255, 255, 255, .08); border: 2px solid rgba(255, 255, 255, .25); touch-action: none; }
#stickKnob { position: absolute; left: 41px; top: 41px; width: 48px; height: 48px; border-radius: 50%; background: rgba(255, 122, 26, .8); }
.tbtns { position: absolute; right: 18px; bottom: 26px; display: grid; grid-template-columns: repeat(2, auto); gap: 10px; align-items: end; }
.tbtns button { width: 84px; height: 84px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .3); background: rgba(16, 19, 27, .75); font: 800 13px var(--mono); touch-action: none; }
#btnAct { background: rgba(255, 122, 26, .85); grid-column: 2; } #btnShoot { grid-column: 1; } #btnDrop { width: 60px; height: 60px; grid-column: 1; font-size: 11px; }

@media (max-width: 900px) {
  .hud-tc { top: 100px; left: 12px; right: 12px; transform: none; width: auto; min-width: 0; }
  .hud-tl { gap: 10px; padding: 8px 10px; } .stat b { font-size: 15px; }
  .hud-tr { top: 12px; } .crew { min-width: 130px; }
  .alert { top: 226px; max-width: calc(100vw - 24px); font-size: 13px; }
  .objective { top: 184px; bottom: auto; max-width: calc(100vw - 24px); font-size: 12px; padding: 6px 12px; }
  .hud-bl { bottom: 236px; max-width: 200px; }
  #toasts { bottom: 300px; }
  .help-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .menu-grid { grid-template-columns: 1fr; }
  .code-box { justify-items: start; }
  .hud-tl .stat:nth-child(4) { display: none; }
}
@media (prefers-reduced-motion: reduce) { .alert, .lbl.ping, .toast { animation: none; } }

/* Depot voting is a sheet, not a blocking modal: the crew keeps laying track while it votes. */
#depot.screen.overlay { background: none; place-items: end center; pointer-events: none; padding: 0 16px 62px; }
#depot .depot-card { pointer-events: auto; width: min(640px, 100%); padding: 16px 18px; margin: 0; }
#depot .cards { margin-top: 10px; }
#depot .card { padding: 12px 14px; } #depot .card .d { min-height: 0; font-size: 12px; }
#depot h2 { font-size: 20px; }

/* Wide screens: menu hugs the left so the rolling train stays visible on the right. */
@media (min-width: 1000px) {
  #menu { place-items: center start; background: linear-gradient(90deg, rgba(8, 10, 14, .82), rgba(8, 10, 14, .45) 48%, rgba(8, 10, 14, 0) 72%); }
  .menu-card { margin: auto 0 auto max(24px, 5vw); text-align: left; width: min(600px, 100%); }
  .tag { margin-left: 0; }
  .join-row, .menu-row { justify-content: flex-start; }
}

body.depot-open #objective { display: none; }
