:root {
  --cab-h: clamp(84px, 14vh, 112px);
  --wood: #96471c;
  --wood-dark: #2b1d12;
  --brass: #d6a73c;
  --brass-light: #f6d77a;
  --cream: #f3e6c8;
  --red: #c8372b;
  --top-h: 44px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  /* The cab's lowest wood colour: on an iPhone, Safari's toolbar and the home bar sit
     over whatever is behind the page, so that must match the cab, not show black. */
  background: #5e2a10;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--cream);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  overscroll-behavior: none;
}

#scene {
  position: fixed;
  inset: 0;
  display: block;
}

button { font: inherit; color: inherit; }

/* Top bar */
#topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  min-height: var(--top-h);
  padding: 6px max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 12px;
  background: linear-gradient(rgba(94, 42, 16, 0.92), rgba(94, 42, 16, 0.7));
  z-index: 5;
}
.brand { font-family: Georgia, "Times New Roman", serif; font-weight: 700; font-size: 18px; white-space: nowrap; }
.brand span { font-weight: 400; font-size: 13px; opacity: 0.75; margin-left: 6px; }
.info { display: flex; gap: 8px; flex: 1; flex-wrap: wrap; font-size: 13px; font-weight: 600; }
.info span { background: rgba(255, 255, 255, 0.1); padding: 4px 9px; border-radius: 999px; white-space: nowrap; }
.info span.alert { background: rgba(200, 55, 43, 0.85); }
.info span.good { background: rgba(46, 160, 67, 0.9); }
.top-buttons { display: flex; gap: 6px; }
.pill {
  min-width: 44px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 2px solid var(--brass);
  background: rgba(0, 0, 0, 0.35);
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}
.pill[aria-pressed="true"] { background: var(--brass); color: #2b1d12; }
/* Sound has not started yet: a gentle pulse says it can be switched on here. */
#btn-sound.nudge { animation: nudge 1.2s ease-in-out infinite alternate; }
@keyframes nudge { to { box-shadow: 0 0 0 4px rgba(246, 215, 122, 0.55); } }
#btn-xray[aria-pressed="true"] { background: #2f7de1; border-color: #9cc8ff; color: white; }

/* Hint banner */
#hint {
  position: fixed;
  top: calc(var(--top-h) + 10px);
  left: 50%;
  transform: translateX(-50%);
  max-width: min(92vw, 640px);
  padding: 10px 18px;
  border-radius: 14px;
  background: rgba(30, 20, 12, 0.85);
  border: 2px solid var(--brass);
  font-size: clamp(14px, 2.2vw, 18px);
  font-weight: 700;
  text-align: center;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  z-index: 4;
}
#hint.show { opacity: 1; }
#hint.urgent { background: rgba(160, 30, 20, 0.92); border-color: #ffb3a8; }

/* Cab */
#cab {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--cab-h) + env(safe-area-inset-bottom));
  padding: 8px max(10px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: clamp(6px, 1.4vw, 18px);
  background:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.08) 0 2px, transparent 2px 46px),
    linear-gradient(#c0662a, #96471c 30%, #5e2a10);
  border-top: 4px solid var(--brass);
  box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.4);
  z-index: 5;
}

#gauges { flex: 0 1 560px; min-width: 240px; height: 100%; }

.fuel-ghost {
  position: fixed;
  top: 0;
  left: 0;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 50;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
.fuel-ghost.coal { background: radial-gradient(circle at 35% 35%, #666, #111 70%); border-radius: 40% 55% 45% 60%; }
.fuel-ghost.wood { background: radial-gradient(circle, #e2b57a 0 25%, #a06a34 26% 45%, #6b4320 46%); }

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(20, 12, 6, 0.6);
  z-index: 20;
}
.modal.hidden { display: none; }
.card {
  max-width: 620px;
  max-height: 92vh;
  overflow: auto;
  padding: clamp(16px, 3vw, 28px);
  border-radius: 18px;
  background: #f4ead2;
  color: #2b1d12;
  border: 4px solid var(--brass);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  touch-action: pan-y;
}
.card h1 { font-family: Georgia, serif; margin: 0 0 8px; font-size: clamp(26px, 4vw, 38px); }
.card .lead { font-size: 17px; line-height: 1.4; }
.howto { padding-left: 22px; line-height: 1.5; font-size: 16px; }
.keys { font-size: 13px; opacity: 0.7; }
/* No keyboard on a phone or tablet, so no list of keys. */
@media (hover: none) and (pointer: coarse) { .keys { display: none; } }
.big {
  display: block;
  margin: 14px auto 0;
  padding: 14px 34px;
  min-height: 52px;
  border-radius: 999px;
  border: 3px solid #7a5512;
  background: linear-gradient(#f6d77a, #d6a73c);
  color: #2b1d12;
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
}

/* Start screen */
.card.welcome { text-align: center; max-width: 520px; }
.choices { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 18px; }
.choices .big { margin: 0; }
.big.secondary { background: transparent; border-color: #a07a3a; font-size: 18px; }

/* The lesson: locked controls fade out, the one being taught glows. */
.locked {
  opacity: 0.28;
  filter: grayscale(0.8);
  pointer-events: none;
  animation: none !important;
}
.spot {
  position: relative;
  z-index: 6;
  animation: spot 0.9s infinite alternate !important;
}
@keyframes spot {
  from { box-shadow: 0 0 0 3px rgba(255, 236, 120, 0.9), 0 0 10px 4px rgba(255, 220, 90, 0.5); }
  to { box-shadow: 0 0 0 5px rgba(255, 236, 120, 1), 0 0 26px 10px rgba(255, 220, 90, 0.8); }
}
#coach-ring {
  position: fixed;
  z-index: 7;
  border-radius: 999px;
  border: 4px solid #fff07a;
  pointer-events: none;
  animation: ring 0.9s infinite alternate;
}
#coach-ring.hidden { display: none; }
@keyframes ring {
  from { box-shadow: 0 0 0 2px rgba(42, 26, 18, 0.8), 0 0 10px 4px rgba(255, 220, 90, 0.5); transform: scale(1); }
  to { box-shadow: 0 0 0 2px rgba(42, 26, 18, 0.8), 0 0 26px 10px rgba(255, 220, 90, 0.85); transform: scale(1.12); }
}

.coach {
  position: fixed;
  z-index: 8;
  width: min(420px, calc(100vw - 20px));
  padding: 12px 16px 10px;
  border-radius: 18px;
  background: #fff7dc;
  color: #2b1d12;
  border: 4px solid #2b1d12;
  box-shadow: 0 8px 0 rgba(43, 29, 18, 0.35), 0 12px 30px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
}
.coach.hidden { display: none; }
.coach.pop { animation: coach-pop 0.35s cubic-bezier(0.3, 1.6, 0.5, 1); }
@keyframes coach-pop { from { transform: scale(0.7); opacity: 0; } }
.coach::after {
  content: "";
  position: absolute;
  left: var(--arrow-x, 50%);
  bottom: -18px;
  margin-left: -14px;
  border: 14px solid transparent;
  border-bottom: 0;
  border-top: 16px solid #2b1d12;
}
.coach.below::after { bottom: auto; top: -18px; border-top: 0; border-bottom: 16px solid #2b1d12; border-top-color: transparent; }
.coach.no-arrow::after { display: none; }
#coach-text {
  margin: 0;
  font-size: clamp(17px, 2.3vw, 21px);
  font-weight: 800;
  line-height: 1.25;
}
#coach-count {
  margin: 6px 0 0;
  display: inline-block;
  padding: 2px 12px;
  border-radius: 999px;
  background: #2f7de1;
  color: white;
  font-weight: 800;
  font-size: 15px;
}
#coach-count.hidden { display: none; }
.coach-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; gap: 8px; }
/* Lesson progress: a short line of railway with the Rocket running along it. */
.dots { position: relative; flex: 1; height: 34px; margin: 0 12px 0 6px; min-width: 0; }
.dots .rails {
  position: absolute; left: -6px; right: -6px; top: 16px; height: 6px;
  border-top: 2px solid #7a5a3a; border-bottom: 2px solid #7a5a3a;
  background: repeating-linear-gradient(90deg, #b88a5a 0 3px, transparent 3px 8px);
}
.dots .stop { position: absolute; top: 2px; width: 0; }
.dots .stop::before {
  content: ''; position: absolute; left: -1px; top: 4px; width: 2px; height: 12px; background: #7a5a3a;
}
.dots .stop::after {
  content: ''; position: absolute; left: 1px; top: 4px; width: 7px; height: 5px; border-radius: 1px; background: #d8c8a0;
}
.dots .stop.passed::after { background: #3aa655; }
.dots .stop.end::after { left: -6px; top: 1px; width: 12px; height: 8px; border-radius: 2px; background: #6b4a2a; }
.dots .stop b {
  position: absolute; top: 23px; transform: translateX(-50%); font-size: 9px; line-height: 1;
  color: #9a7a5a; font-weight: 800; letter-spacing: 0.02em; text-transform: uppercase; white-space: nowrap;
}
.dots .stop.passed b { color: #2f7a40; }
.dots .mini-loco {
  position: absolute; top: 1px; transform: translateX(-60%); transition: left 0.8s cubic-bezier(0.4, 0, 0.2, 1);
  line-height: 0; filter: drop-shadow(0 1px 0 rgba(43, 29, 18, 0.3));
}
.skip {
  border: 0;
  background: none;
  color: #7a5a3a;
  font-size: 13px;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
  padding: 6px 2px;
  min-height: 32px;
}

/* A piece of fuel showing the way from its bin to the firebox. */
.coach-drag {
  z-index: 7;
  opacity: 0;
  animation: coach-drag 1.8s ease-in-out infinite;
}
#coach-drag.hidden { display: none; }
@keyframes coach-drag {
  0% { transform: translate(var(--fx), var(--fy)) scale(0.8); opacity: 0; }
  15% { transform: translate(var(--fx), var(--fy)) scale(1); opacity: 0.95; }
  75% { transform: translate(var(--tx), var(--ty)) scale(1); opacity: 0.95; }
  100% { transform: translate(var(--tx), var(--ty)) scale(0.5); opacity: 0; }
}

/* The player's own zoom, tucked in the corner above the gauges. */
.zoom {
  position: fixed;
  right: max(10px, env(safe-area-inset-right));
  bottom: calc(var(--cab-h) + env(safe-area-inset-bottom) + 10px);
  display: flex;
  gap: 8px;
  z-index: 4;
}
.zoom .pill { min-width: 44px; min-height: 44px; padding: 0; font-size: 24px; line-height: 1; background: rgba(30, 20, 12, 0.6); }
.zoom .pill:disabled { opacity: 0.35; cursor: default; }

@media (max-width: 560px) {
  .brand { display: none; }
  .info span:nth-child(4) { display: none; }
}
/* A phone held upright: buttons on one row, the readouts in a row underneath. */
@media (orientation: portrait) and (max-width: 600px) {
  #topbar { flex-wrap: wrap; gap: 6px 8px; }
  .top-buttons { margin-left: auto; }
  .info { order: 2; flex-basis: 100%; gap: 6px; font-size: 12px; }
  .info span { padding: 3px 8px; }
  .info span:nth-child(4) { display: inline; } /* the readouts have a row of their own */
  .top-buttons { gap: 5px; }
  .top-buttons .pill { padding: 0 10px; font-size: 14px; min-height: 40px; }
}
/* A phone on its side: one slim row, so the line keeps most of the height. */
@media (max-height: 520px) {
  .brand { display: none; }
  .info { flex-wrap: nowrap; overflow: hidden; }
}
@media (max-height: 520px) {
  :root { --cab-h: 72px; --top-h: 38px; }
  .info { font-size: 11px; }
}
@media (max-height: 520px) {
  .coach { width: min(290px, calc(100vw - 20px)); padding: 8px 12px 6px; }
  #coach-text { font-size: 15px; }
  .coach-foot { margin-top: 4px; }
}
/* Hover (or press and hold) a part of the engine to name it. */
#part-tip {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9;
  max-width: 260px;
  padding: 8px 11px;
  background: #fff8e6;
  color: #2a1d12;
  border: 3px solid #2a1d12;
  border-radius: 10px;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.35);
  font-size: 14px;
  line-height: 1.3;
  pointer-events: none;
}
#part-tip b { display: block; font-size: 15px; margin-bottom: 2px; color: #b8321f; }
#part-tip.hidden { display: none; }
/* Shown whenever the lesson runs time faster than real. */
#warp {
  position: fixed;
  top: calc(var(--top-h) + 10px);
  right: 12px;
  z-index: 6;
  padding: 5px 12px;
  border-radius: 999px;
  background: #2a1d12;
  color: #fff07a;
  border: 2px solid #fff07a;
  font-weight: 800;
  font-size: 15px;
  pointer-events: none;
}
#warp.hidden { display: none; }
