:root {
  --panel: #0e141b;
  --panel-2: #18212b;
  --edge: #2a3947;
  --accent: #39c06a;
  --amber: #ffb547;
  --red: #ff4d4d;
  --text: #e7eef5;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  background: #87c2e0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

#game { display: block; width: 100vw; height: 100vh; }

#ui { position: fixed; inset: 0; pointer-events: none; z-index: 5; }

/* ---------- Top bar ---------- */
#topbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 16px;
  background: linear-gradient(rgba(6,10,14,.72), rgba(6,10,14,0));
  font-size: 13px; letter-spacing: .5px;
}
#next  { font-weight: 800; color: var(--amber); }
#trainname { color: #cfe0ef; opacity: .9; font-weight: 600; }
#clock { color: #cfe0ef; font-weight: 700; font-variant-numeric: tabular-nums; }
#clock.late { color: #ff7a7a; }
#stat  { color: #a9bccd; }

/* ---------- Speed-limit roundel ---------- */
#limit {
  position: absolute; top: 48px; left: 50%; transform: translateX(-50%);
  width: 60px; height: 60px; border-radius: 50%;
  background: #fff; border: 6px solid #d11; color: #111;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,.35);
}
#limit span { font-size: 22px; font-weight: 800; line-height: 1; }
#limit em { font-style: normal; font-size: 8px; letter-spacing: 1px; color: #555; }
#limit.over { animation: limitflash .5s steps(2, start) infinite; }
@keyframes limitflash { 0% { background: #fff; color: #111; } 100% { background: #ffd4d4; color: #d11; } }

/* ---------- Route progress bar ---------- */
#route { position: fixed; top: 116px; left: 24px; right: 24px; height: 16px; z-index: 6; pointer-events: none; }
#route-line, #route-fill { position: absolute; top: 7px; height: 4px; border-radius: 2px; }
#route-line { left: 0; right: 0; background: rgba(255,255,255,.22); }
#route-fill { left: 0; width: 0; background: var(--accent); box-shadow: 0 0 8px rgba(57,192,106,.6); }
.route-tick { position: absolute; top: 4px; width: 8px; height: 8px; margin-left: -4px; border-radius: 50%; background: #cfe0ef; border: 1px solid #0a0e12; }
#route-marker {
  position: absolute; top: -3px; margin-left: -7px; width: 0; height: 0;
  border-left: 7px solid transparent; border-right: 7px solid transparent; border-top: 10px solid var(--amber);
  filter: drop-shadow(0 0 4px rgba(0,0,0,.5));
}
#route-ghost {
  position: absolute; top: 2px; margin-left: -5px; width: 0; height: 0; display: none;
  border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 8px solid rgba(255,255,255,.6);
}

/* ---------- Cab windscreen frame (cab view only) ---------- */
#cab-frame { position: absolute; inset: 0; display: none; }
body.view-cab #cab-frame { display: block; }

#cab-frame .roofline {
  position: absolute; top: 0; left: 0; right: 0; height: 44px;
  background: linear-gradient(#05080b, #121a22);
}
#cab-frame .pillar {
  position: absolute; top: 40px; bottom: 150px; width: 13vw; min-width: 50px;
  background: linear-gradient(90deg, #060a0e, #1a242e);
}
#cab-frame .pillar.left  { left: 0;  clip-path: polygon(0 0, 100% 0, 55% 100%, 0 100%); }
#cab-frame .pillar.right { right: 0; clip-path: polygon(0 0, 100% 0, 100% 100%, 45% 100%); }
#cab-frame .vignette {
  position: absolute; inset: 0;
  box-shadow: inset 0 0 180px rgba(0,0,0,.55);
  pointer-events: none;
}

/* ---------- Control bar ---------- */
#controls {
  position: absolute; left: 0; right: 0; bottom: 0; height: 150px;
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 12px; padding: 12px 16px 14px;
  background: linear-gradient(rgba(6,10,14,0), rgba(6,10,14,.55) 55%, rgba(6,10,14,.8));
}

.cluster { display: flex; gap: 12px; pointer-events: auto; }

.rbtn {
  width: 66px; height: 66px; border-radius: 50%;
  background: radial-gradient(circle at 50% 35%, #26323f, #0d141b);
  border: 1px solid var(--edge); color: var(--text);
  font-weight: 800; font-size: 12px; letter-spacing: 1px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.rbtn small { color: #7f93a6; font-weight: 600; font-size: 10px; margin-top: 2px; }
.rbtn:active { transform: scale(.95); }

.bigbtn {
  width: 92px; height: 92px; border-radius: 20px;
  border: 1px solid var(--edge); color: #06220f; cursor: pointer;
  font-weight: 800; font-size: 13px; letter-spacing: 1px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 8px 18px rgba(0,0,0,.4);
}
.bigbtn small { font-weight: 700; font-size: 10px; margin-top: 4px; opacity: .8; }
.bigbtn:active { transform: scale(.96); }
.bigbtn.thr { background: linear-gradient(#5fe08a, var(--accent)); }
.bigbtn.brk { background: linear-gradient(#ff7a7a, var(--red)); color: #2a0808; }

/* ---------- Throttle + brake levers ---------- */
.lever { pointer-events: auto; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.lever .ll { font-size: 9px; letter-spacing: 1px; font-weight: 800; }
#lever-thr .ll { color: #54d97f; }
#lever-brk .ll { color: #ff7a7a; }
.lever-track {
  position: relative; width: 46px; height: 120px; border-radius: 14px;
  border: 1px solid var(--edge); box-shadow: inset 0 2px 10px rgba(0,0,0,.6);
  touch-action: none; cursor: grab;
}
.lever-track.thr { background: linear-gradient(#2e8b50, #0b1118); }
.lever-track.brk { background: linear-gradient(#a62f2f, #0b1118); }
.lever-track:active { cursor: grabbing; }
.lever-knob {
  position: absolute; left: 50%; top: 100%; width: 58px; height: 24px;
  transform: translate(-50%, -50%);
  background: linear-gradient(#e9eef3, #aab6c2); border: 1px solid #6b7886; border-radius: 7px;
  box-shadow: 0 3px 8px rgba(0,0,0,.5);
}
.lever-knob::after {
  content: ''; position: absolute; left: 9px; right: 9px; top: 50%; height: 2px;
  background: #55606b; transform: translateY(-50%); box-shadow: 0 -4px 0 #55606b, 0 4px 0 #55606b;
}

/* ---------- Dashboard (centre) ---------- */
.dash {
  flex: 1; max-width: 420px; margin: 0 auto;
  display: flex; align-items: center; gap: 16px;
  background: linear-gradient(#18212b, #0b1118);
  border: 1px solid var(--edge); border-radius: 14px;
  padding: 10px 16px; pointer-events: none;
}
.speedo {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 120px;
}
.speedo span {
  font-size: 50px; font-weight: 800; line-height: .9; color: var(--accent);
  font-variant-numeric: tabular-nums; text-shadow: 0 0 16px rgba(57,192,106,.45);
}
.speedo small { color: #7b8a99; letter-spacing: 3px; font-size: 11px; margin-top: 3px; }

.meters { flex: 1; display: flex; flex-direction: column; gap: 10px; }
.m label { display: block; font-size: 10px; letter-spacing: 2px; color: #8aa0b5; margin-bottom: 5px; }
.bar { height: 14px; border-radius: 7px; background: #05090d; border: 1px solid var(--edge); overflow: hidden; }
#throttle-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #2e8b50, var(--accent)); transition: width .08s linear; }
#brake-fill    { height: 100%; width: 0%; background: linear-gradient(90deg, #a62f2f, var(--red)); transition: width .08s linear; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; top: 20%; left: 50%; transform: translateX(-50%);
  z-index: 8; pointer-events: none;
  font-size: 24px; font-weight: 800; letter-spacing: 1px; color: #fff;
  text-shadow: 0 2px 14px rgba(0,0,0,.7);
  background: rgba(10,14,18,.6); padding: 12px 26px; border-radius: 12px;
  border: 1px solid rgba(255,255,255,.12);
  opacity: 0; transition: opacity .25s ease;
}
#toast.show { opacity: 1; }

/* ---------- Start screen ---------- */
#start, #results {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 30%, #1a2a38, #070b0f);
}
#results { z-index: 21; }
#start.hidden, #results.hidden { display: none; }
.results-grid { display: flex; gap: 20px; justify-content: center; margin: 18px 0 22px; }
.results-grid div { display: flex; flex-direction: column; align-items: center; }
.results-grid span { font-size: 30px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.results-grid small { color: #8aa0b5; letter-spacing: 2px; font-size: 11px; margin-top: 4px; }
.newbest { color: var(--amber); font-weight: 800; margin-bottom: 12px; }
.newbest.hidden { display: none; }
#difficulty { pointer-events: auto; margin: 0 0 16px; display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; }
#difficulty .dlabel { color: #8aa0b5; font-size: 12px; margin-right: 4px; }
#difficulty button { cursor: pointer; font-weight: 700; font-size: 13px; color: #cfe0ef; background: #1c2733; border: 1px solid var(--edge); border-radius: 8px; padding: 6px 14px; }
#difficulty button.sel { background: linear-gradient(#2e8b50, var(--accent)); color: #06220f; border-color: transparent; }
.card {
  width: min(92vw, 470px);
  max-height: 92vh; overflow-y: auto;
  background: linear-gradient(#111922, #0a0e12);
  border: 1px solid var(--edge); border-radius: 18px;
  padding: 28px 30px; text-align: center;
  box-shadow: 0 30px 80px rgba(0,0,0,.6);
}
.card h1 { font-size: 25px; margin-bottom: 6px; }
.route { color: var(--accent); font-weight: 700; letter-spacing: .5px; margin-bottom: 16px; }
.card ul { list-style: none; text-align: left; display: inline-block; margin: 6px auto 14px; line-height: 2; }
.card ul b {
  display: inline-block; min-width: 86px;
  background: #1c2733; border: 1px solid var(--edge); border-radius: 6px;
  padding: 1px 8px; text-align: center; color: var(--amber); font-size: 12px; margin-right: 8px;
}
.goal { color: #a9bccd; font-size: 14px; margin-bottom: 20px; }
#start-btn, #restart-btn {
  pointer-events: auto; cursor: pointer;
  font-size: 18px; font-weight: 800; letter-spacing: 1px; color: #06220f;
  background: linear-gradient(#54d97f, var(--accent)); border: none; border-radius: 12px;
  padding: 14px 40px; box-shadow: 0 8px 22px rgba(57,192,106,.4);
  transition: transform .08s ease;
}
#start-btn:active, #restart-btn:active { transform: scale(.97); }
.tiny { margin-top: 14px; color: #647688; font-size: 12px; }

@media (max-width: 640px) {
  #trainname { display: none; }
  .dash { display: none; }                 /* keep phones uncluttered */
  #controls { padding: 10px 10px 12px; }
  .lever-track { width: 54px; height: 132px; }   /* bigger touch targets */
  .lever-knob { width: 66px; height: 30px; }
  .rbtn { width: 60px; height: 60px; }
  #route { top: 104px; }
}

/* landscape phones: keep the chrome compact so the 3D view stays large */
@media (max-height: 460px) {
  #controls { height: 118px; }
  .lever-track { height: 102px; }
  .rbtn { width: 54px; height: 54px; }
  #topbar { padding: 6px 12px; font-size: 12px; }
  #limit { width: 48px; height: 48px; top: 38px; border-width: 5px; }
  #limit span { font-size: 18px; }
  #route { top: 88px; }
  .speedo span { font-size: 40px; }
}
