:root {
  color-scheme: light;
  --ink: #17324d;
  --sky: #72cfff;
  --cream: #fff8dc;
  --yellow: #ffd84d;
  --red: #ef5b5b;
  --green: #52bd73;
  --blue: #2789d8;
  font-family: "Trebuchet MS", "Arial Rounded MT Bold", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: #163d64; }
button { font: inherit; color: inherit; touch-action: manipulation; user-select: none; cursor: pointer; }
button:focus-visible {
  outline: 4px solid #17324d;
  outline-offset: 3px;
  box-shadow: 0 0 0 7px rgba(255,255,255,.95);
}
kbd {
  display: none;
  min-width: 22px;
  padding: 2px 5px;
  border: 1px solid #9db1bf;
  border-bottom-width: 3px;
  border-radius: 5px;
  background: #fff;
  color: #29465c;
  font: 800 11px/1 system-ui, sans-serif;
  text-align: center;
}

#game-shell {
  width: 100%;
  height: 100%;
  position: relative;
  background: #8ed8ff;
  color: var(--ink);
  overscroll-behavior: none;
}

.topbar {
  position: absolute;
  inset: 0 0 auto;
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(8px, env(safe-area-inset-top)) 18px 7px;
  background: linear-gradient(rgba(255,255,255,.96), rgba(255,255,255,.72), transparent);
  box-shadow: none;
  z-index: 8;
  pointer-events: none;
}
.topbar button { pointer-events: auto; }

.brand, .status { display: flex; align-items: center; gap: 10px; }
.camera-buttons { display: flex; gap: 5px; pointer-events: auto; }
.camera-buttons .round-button { width: 41px; height: 41px; font-size: 24px; line-height: 1; }
.brand-train { font-size: 36px; filter: drop-shadow(0 2px 0 #fff); }
.brand strong { display: block; font-size: clamp(17px, 2.2vw, 25px); line-height: 1; }
.brand small { display: block; color: #64809a; margin-top: 4px; }
.star-box { display: flex; gap: 5px; align-items: center; padding: 7px 11px; background: #fff4b7; border: 3px solid #efc63b; border-radius: 18px; font-size: 19px; }
.round-button { width: 48px; height: 48px; border-radius: 50%; border: 3px solid #cbdbe8; background: white; font-size: 21px; font-weight: 900; box-shadow: 0 3px 0 #a6bdce; }
.round-button:active { transform: translateY(2px); box-shadow: 0 1px 0 #a6bdce; }

.game-stage { position: absolute; inset: 0; overflow: hidden; background: var(--sky); }
#game-canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.mission-card {
  position: absolute;
  top: 68px;
  left: 50%;
  transform: translateX(-50%);
  min-width: min(390px, 72vw);
  display: grid;
  grid-template-columns: 42px 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 13px;
  border: 3px solid rgba(255,255,255,.95);
  border-radius: 18px;
  background: rgba(23, 50, 77, .9);
  color: white;
  box-shadow: 0 5px 14px rgba(20, 51, 76, .25);
  pointer-events: none;
}
.mission-card > span { grid-row: span 2; font-size: 30px; }
.mission-card small { display: block; color: #ffd84d; font-weight: 900; font-size: 10px; letter-spacing: 1px; }
.mission-card strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 17px; }
.mission-progress { height: 5px; background: #45647e; border-radius: 8px; overflow: hidden; }
.mission-progress i { display: block; height: 100%; width: 0; background: #ffd84d; transition: width .3s; }
.game-stage.incident-active .mission-card,
.game-stage.help-active .mission-card,
.game-stage.new-world-active .mission-card { opacity: 0; transform: translate(-50%, -12px); }
.game-stage.welcome-active .mission-card,
.game-stage.welcome-active .incident-banner { opacity: 0; }
.game-stage.welcome-active .station-panel,
.game-stage.welcome-active .junction-choices { display: none; }
#toast {
  position: absolute;
  left: 50%;
  bottom: 220px;
  transform: translate(-50%, 20px) scale(.9);
  opacity: 0;
  padding: 10px 18px;
  border-radius: 22px;
  background: #fff;
  color: var(--ink);
  font-size: 19px;
  font-weight: 900;
  box-shadow: 0 5px 16px rgba(23,50,77,.3);
  pointer-events: none;
  transition: .25s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.incident-banner {
  position: absolute;
  z-index: 5;
  left: 50%;
  top: 82px;
  transform: translate(-50%, -18px) scale(.9);
  opacity: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  border-radius: 22px;
  background: rgba(255,255,255,.96);
  color: var(--ink);
  box-shadow: 0 7px 25px rgba(18,45,68,.35);
  transition: .25s ease;
}
.incident-banner.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.incident-banner { border: 5px solid #e3504e; font-size: 26px; }
.incident-banner strong { font-size: 30px; }
.incident-banner #incident-text { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: #e3504e; color: #fff; font-weight: 900; }
.welcome-card {
  position: absolute;
  z-index: 12;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -45%) scale(.94);
  opacity: 0;
  pointer-events: none;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  width: min(520px, calc(100vw - 36px));
  padding: 16px;
  border: 4px solid #fff;
  border-radius: 26px;
  background: rgba(23,50,77,.95);
  color: #fff;
  box-shadow: 0 12px 38px rgba(15,35,52,.42);
  transition: .22s ease;
}
.welcome-card.show { opacity: 1; pointer-events: auto; transform: translate(-50%, -50%) scale(1); }
.welcome-card .helper-face { font-size: 48px; }
.welcome-card strong { display: block; font-size: 24px; }
.welcome-card p { margin: 4px 0 0; color: #d8ebf7; font-size: 17px; }
.welcome-card button {
  min-width: 94px;
  min-height: 58px;
  border: 3px solid #278e47;
  border-radius: 18px;
  background: #52bd73;
  color: #102c1b;
  font-size: 18px;
  font-weight: 900;
  box-shadow: 0 5px 0 #278e47;
}

.help-panel {
  position: absolute;
  z-index: 13;
  top: max(68px, env(safe-area-inset-top));
  right: 14px;
  width: min(390px, calc(100vw - 28px));
  padding: 14px;
  border: 3px solid rgba(255,255,255,.95);
  border-radius: 24px;
  background: rgba(242,249,253,.97);
  color: var(--ink);
  box-shadow: 0 10px 36px rgba(15,38,57,.4);
  transform: translateX(calc(100% + 28px));
  opacity: 0;
  pointer-events: none;
  transition: .22s ease;
}
.help-panel.show { transform: translateX(0); opacity: 1; pointer-events: auto; }
.help-panel header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.help-panel header div { display: flex; align-items: center; gap: 8px; font-size: 22px; }
.help-panel header span { font-size: 32px; }
.help-panel .round-button { width: 48px; height: 48px; box-shadow: none; }
.help-mode {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px;
  margin-top: 8px;
  border-radius: 17px;
  background: #fff;
  border: 2px solid #d2e0e9;
}
.help-mode > span { font-size: 33px; text-align: center; }
.help-mode strong { font-size: 18px; }
.help-mode p { margin: 2px 0 0; font-size: 16px; line-height: 1.25; color: #49677d; }
.help-mode kbd, .desktop-help kbd { display: inline-block; }
.desktop-help { margin: 12px 4px 2px; font-size: 13px; line-height: 1.9; color: #49677d; }
.new-world-panel {
  position: absolute;
  z-index: 14;
  top: max(68px, env(safe-area-inset-top));
  right: 14px;
  width: min(360px, calc(100vw - 28px));
  padding: 14px;
  border: 3px solid #fff;
  border-radius: 24px;
  background: rgba(242,249,253,.98);
  box-shadow: 0 10px 36px rgba(15,38,57,.4);
  transform: translateX(calc(100% + 28px));
  opacity: 0;
  pointer-events: none;
  transition: .22s ease;
}
.new-world-panel.show { transform: translateX(0); opacity: 1; pointer-events: auto; }
.new-world-panel header { display: flex; align-items: center; justify-content: space-between; }
.new-world-panel header div { display: flex; align-items: center; gap: 8px; font-size: 22px; }
.new-world-panel header span { font-size: 32px; }
.new-world-panel p { margin: 10px 2px 14px; color: #49677d; font-size: 16px; line-height: 1.3; }
.new-world-confirm {
  position: relative;
  width: 100%;
  min-height: 82px;
  overflow: hidden;
  border: 3px solid #278e47;
  border-radius: 19px;
  background: #dff5e5;
  box-shadow: 0 5px 0 #278e47;
  font-weight: 900;
}
.new-world-confirm > span, .new-world-confirm > b { position: relative; z-index: 2; display: block; }
.new-world-confirm > span { font-size: 29px; }
.new-world-confirm > i { position: absolute; inset: auto 9px 8px; height: 8px; overflow: hidden; border-radius: 8px; background: #b7d8c0; }
.new-world-confirm > i > em { display: block; width: 0; height: 100%; background: #34b65c; transition: width 1.2s linear; }
.new-world-confirm.holding { background: #bfeccc; transform: translateY(3px); box-shadow: 0 2px 0 #278e47; }
.junction-choices {
  position: absolute;
  inset: 0;
  z-index: 9;
  pointer-events: none;
}
.junction-choices.hidden { display: none; }
.junction-choice {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: auto;
  width: 58px;
  height: 54px;
  border: 3px solid #c8dbea;
  border-radius: 15px;
  background: #eef7fc;
  font-size: 27px;
  box-shadow: 0 4px 0 #9eb8ca;
}
.junction-choice span { display: inline-block; transform: rotate(var(--track-angle)); }
.junction-choice.recommended {
  border-color: #ffd84d;
  box-shadow: 0 0 0 5px rgba(255,216,77,.48), 0 5px 12px rgba(19,50,77,.3);
}
.junction-choice b {
  position: absolute;
  top: -16px;
  right: -13px;
  font-size: 20px;
  filter: drop-shadow(0 2px 0 #17324d);
}
.junction-choice.selected { border-color: #ffd84d; background: #fff3ad; box-shadow: 0 2px 0 #aa8d25, 0 0 0 6px rgba(255,216,77,.35); }

.station-panel {
  position: absolute;
  z-index: 8;
  left: 50%;
  bottom: 112px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 9px;
  max-width: calc(100vw - 120px);
  padding: 8px 10px;
  border: 3px solid rgba(255,255,255,.95);
  border-radius: 22px;
  background: rgba(23,50,77,.92);
  color: white;
  box-shadow: 0 7px 24px rgba(18,42,62,.35);
  backdrop-filter: blur(9px);
}
.station-panel.hidden { display: none; }
.station-heading { display: flex; align-items: center; gap: 8px; min-width: 170px; }
.station-heading > span { font-size: 29px; }
.station-heading strong, .station-heading small { display: block; }
.station-heading strong { max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.station-heading small { color: #cce2f0; font-weight: 800; margin-top: 3px; }
.station-action {
  min-width: 92px;
  height: 58px;
  border: 3px solid #c6d8e5;
  border-radius: 17px;
  background: #eef8fd;
  color: #17324d;
  box-shadow: 0 4px 0 #9eb8ca;
  font-weight: 900;
}
.station-action.hidden { display: none; }
.station-action span, .station-action b { display: block; }
.station-action span { font-size: 22px; }
.station-action.cargo { background: #fff0bd; border-color: #e2bd58; box-shadow: 0 4px 0 #b78e35; }
.station-action.active { transform: translateY(2px); box-shadow: 0 2px 0 #7d98ab; }

.mode-tabs {
  position: absolute;
  z-index: 7;
  left: 12px;
  top: 72px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 7px;
  border-radius: 22px;
  background: rgba(241,248,252,.78);
  border: 2px solid rgba(255,255,255,.82);
  box-shadow: 0 5px 18px rgba(20,45,65,.22);
  backdrop-filter: blur(8px);
}
.mode-button {
  width: 76px;
  min-height: 64px;
  border: 2px solid rgba(255,255,255,.8);
  border-radius: 17px;
  background: #dce9f1;
  color: #506c83;
  font-weight: 900;
  font-size: 15px;
  line-height: 1;
  position: relative;
}
.mode-button span, .mode-button b { display: block; }
.mode-button span { font-size: 27px; margin-bottom: 3px; }
.mode-button.selected { color: white; background: var(--blue); border-color: #fff; box-shadow: inset 0 -4px 0 rgba(0,0,0,.14), 0 0 0 3px rgba(39,137,216,.25); }
.mode-button.selected::after { content: "✓"; position: absolute; right: 5px; top: 4px; font-size: 12px; }

.control-panel {
  position: absolute;
  z-index: 7;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 10px;
  max-width: calc(100vw - 96px);
  padding: 8px 10px;
  border: 2px solid rgba(255,255,255,.85);
  border-radius: 25px;
  background: rgba(248,252,255,.8);
  box-shadow: 0 7px 26px rgba(18,42,62,.28);
  backdrop-filter: blur(10px);
}
#build-controls { justify-content: center; width: min(650px, calc(100vw - 112px)); }
#train-controls { width: min(900px, calc(100vw - 96px)); }
#car-controls { width: min(870px, calc(100vw - 96px)); }
.control-panel.hidden { display: none; }
.action-button, .lever-button, .car-pedal {
  min-height: 64px;
  border: 3px solid #c6d8e5;
  border-radius: 20px;
  background: #eff6fa;
  font-weight: 900;
  box-shadow: 0 5px 0 #afc4d2;
}
.action-button { min-width: 92px; padding: 6px 10px; font-size: 16px; line-height: 1.05; }
.action-button span { display: block; font-size: 24px; }
.road-picker { min-width: 106px; max-width: 135px; }
.road-picker b, .train-picker b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.action-button.selected { border-color: var(--blue); background: #dceeff; color: #136db4; box-shadow: 0 5px 0 #88b7d8, 0 0 0 3px rgba(39,137,216,.24); }
.action-button:active, .lever-button:active, .car-pedal:active, .steering-wheel button:active { transform: translateY(4px); box-shadow: 0 1px 0 #afc4d2; }
.speed-readout { width: 66px; text-align: center; }
.speed-readout small { display: block; font-size: 10px; font-weight: 900; color: #7790a3; }
.speed-readout strong { font-size: 35px; line-height: .9; }
.big-go { background: #62cf7f; border-color: #3ca75b; box-shadow: 0 5px 0 #278e47; }
.train-quadrant { display: grid; grid-template-columns: 70px 35px 70px; gap: 4px; align-items: center; padding: 5px; border-radius: 18px; background: #293943; box-shadow: inset 0 3px 8px #101a20; }
.lever-button { min-height: 58px; border-radius: 13px; border: 3px solid #171f24; color: #fff; font-size: 15px; font-weight: 900; }
.lever-button span { display: block; font-size: 23px; }
.lever-button.brake { background: linear-gradient(#e96a62, #a82e2e); }
.lever-button.power { background: linear-gradient(#66c97b, #268c45); }
.lever-slot { height: 65px; position: relative; display: grid; place-items: end center; color: #cbd6dc; font-size: 8px; }
.lever-slot i { position: absolute; top: 5px; width: 10px; height: 45px; border-radius: 8px; background: #10171b; box-shadow: inset 0 0 4px #000; }
.lever-slot i::after { content: ""; position: absolute; width: 30px; height: 13px; left: -10px; top: 14px; border-radius: 6px; background: #dfbd70; border: 3px solid #796233; }
.cord span { transform: rotate(-10deg); }
.steering-wheel { --steering-angle: 0deg; width: 170px; height: 80px; display: grid; grid-template-columns: 48px 34px 48px; justify-content: center; align-items: center; padding: 5px; border: 10px solid #293942; border-radius: 50%; background: #576973; box-shadow: inset 0 0 0 6px #1c282f, 0 5px 0 #9fb3c0; transform: rotate(var(--steering-angle)); transition: transform .08s linear; }
.steering-wheel:has(button.active) { box-shadow: inset 0 0 0 7px #1c282f, 0 2px 0 #9fb3c0, 0 0 0 5px #65c6ff; }
.steering-wheel button { min-width: 48px; height: 54px; border: 0; background: transparent; color: #fff; font-size: 25px; font-weight: 900; }
.wheel-hub { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; color: #d9b94e; background: #172229; border: 5px solid #a9bac3; }
.car-pedals { display: flex; gap: 14px; align-items: flex-end; }
.car-pedal { width: 92px; color: #18242a; }
.car-pedal span { display: block; font-size: 33px; transform: perspective(60px) rotateX(18deg); }
.brake-pedal { min-height: 68px; background: linear-gradient(#d5dbe0, #87969e); }
.gas-pedal { min-height: 86px; background: linear-gradient(#bfcbd1, #667780); }
.dashboard { position: relative; width: 90px; height: 78px; display: grid; place-items: center; align-content: center; border-radius: 45px 45px 15px 15px; color: #fff; background: #14232c; border: 5px solid #82949e; box-shadow: inset 0 0 0 2px #050a0d; }
.dashboard small { color: #a8d7ee; font-size: 9px; }
.dashboard strong { font-size: 31px; line-height: 1; }
.dashboard i { position: absolute; width: 3px; height: 27px; bottom: 12px; transform-origin: bottom; transform: rotate(35deg); background: #ef4f45; }

.utility-panel {
  position: absolute;
  z-index: 7;
  right: 14px;
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 7px;
  border: 2px solid rgba(255,255,255,.88);
  border-radius: 19px;
  background: rgba(248,252,255,.84);
  box-shadow: 0 6px 22px rgba(18,42,62,.25);
  backdrop-filter: blur(9px);
}
.utility-panel.hidden { display: none; }
.utility-button {
  width: 72px;
  min-height: 58px;
  border: 3px solid #c6d8e5;
  border-radius: 15px;
  background: #eff6fa;
  box-shadow: 0 4px 0 #afc4d2;
  font-weight: 900;
}
.utility-button span { display: block; font-size: 23px; }
.utility-button b { display: block; font-size: 14px; line-height: 1.05; }
.utility-button.undo { background: #fff2b9; border-color: #e1bc4e; box-shadow: 0 4px 0 #b99031; }

@media (hover: hover) and (pointer: fine) {
  .control-panel kbd, .mode-button kbd, .utility-button kbd {
    display: block;
    position: absolute;
    right: -4px;
    top: -7px;
  }
  .action-button, .lever-button, .car-pedal, .mode-button, .utility-button { position: relative; }
  .mode-button { width: 88px; }
  .control-panel { padding-block: 7px; }
}
@media (max-height: 650px) {
  .action-button, .lever-button, .car-pedal { min-height: 62px; }
  .topbar { padding-top: 5px; }
}

@media (max-width: 650px) {
  .brand small, .speed-readout { display: none; }
  .brand strong { font-size: 17px; }
  .brand-train { font-size: 30px; }
  .status { gap: 6px; }
  .star-box { padding: 6px 9px; }
  .action-button { min-width: 88px; padding: 6px; }
  .steering-wheel { width: 160px; }
  .mode-tabs { left: 7px; top: 70px; padding: 5px; }
  .mode-button { width: 64px; min-height: 60px; font-size: 14px; }
  .mode-button span { font-size: 22px; }
  .mission-card { top: 6px; left: calc(50% + 10px); min-width: 0; width: min(360px, calc(100vw - 245px)); }
  .mission-card strong { font-size: 15px; }
  .station-panel { max-width: calc(100vw - 24px); bottom: 104px; }
  .station-heading { min-width: 118px; }
  .station-heading strong { max-width: 135px; font-size: 13px; }
  .station-heading small { font-size: 11px; }
  .station-action { min-width: 76px; }
  .welcome-card { grid-template-columns: auto 1fr; }
  .welcome-card button { grid-column: 1 / -1; width: 100%; }
  .help-panel { top: 62px; right: 8px; max-height: calc(100vh - 72px); overflow-y: auto; }
  .new-world-panel { top: 62px; right: 8px; }
}

@media (orientation: portrait) {
  .control-panel { overflow-x: auto; justify-content: flex-start; bottom: max(10px, env(safe-area-inset-bottom)); }
  .control-panel > * { flex: 0 0 auto; }
  .mission-card { max-width: calc(100vw - 165px); min-width: 0; }
  #train-controls, #car-controls { width: calc(100vw - 48px); max-width: none; }
  #build-controls { width: calc(100vw - 116px); max-width: none; }
  .utility-panel { right: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }
}
