:root {
  --navy: #1f3a5f;
  --navy-deep: #16294a;
  --ink: #2b3a55;
  --cream: #fff6e0;
  --sun: #ffd23f;
  --orange: #ff8a3d;
  --green: #3ccf7a;
  --red: #ff5d5d;
  --blue: #4d96ff;
  font-family: 'Fredoka', 'Nunito', 'Trebuchet MS', 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body { background: var(--navy-deep); color: var(--ink); overflow: hidden; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button { font: inherit; cursor: pointer; }

#app { position: relative; height: 100%; height: 100dvh; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); display: flex; flex-direction: column; }

/* Top bar */
.topbar { flex: 0 0 auto; display: flex; align-items: center; gap: 18px; padding: 8px 18px; background: var(--navy); border-bottom: 4px solid var(--sun); color: #fff; }
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.brand-badge { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; background: var(--sun); color: var(--navy); font-weight: 700; font-size: 24px; box-shadow: 0 3px 0 #c99a00; transform: rotate(-4deg); }
.brand strong { display: block; font-size: 20px; letter-spacing: .3px; }
.brand small { display: block; margin-top: -2px; color: #bcd1ec; font-size: 13px; }
.clock { padding: 4px 16px; border-radius: 12px; background: #0e1b33; border: 2px solid #3b557f; font: 600 28px/1.1 'Fredoka', monospace; letter-spacing: 2px; color: var(--sun); box-shadow: inset 0 2px 6px #0008; }
.stars-total { font-size: 20px; font-weight: 600; }
.star-icon { color: var(--sun); text-shadow: 0 2px 0 #c99a00; }
.controls { display: flex; align-items: center; gap: 8px; }
.round, .pill { height: 38px; border: 0; border-radius: 19px; background: #fff; color: var(--navy); font-weight: 700; box-shadow: 0 3px 0 #9fb6d6; transition: transform .08s; }
.round { width: 38px; font-size: 17px; }
.pill { padding: 0 16px; }
.round:hover, .pill:hover { transform: translateY(-1px); }
.round:active, .pill:active { transform: translateY(2px); box-shadow: 0 1px 0 #9fb6d6; }
.round.on { background: var(--sun); box-shadow: 0 3px 0 #c99a00; }
.speed { display: flex; padding: 3px; border-radius: 19px; background: #16294a; gap: 2px; }
.speed button { height: 32px; min-width: 40px; border: 0; border-radius: 16px; background: transparent; color: #bcd1ec; font-weight: 600; }
.speed button.selected { background: var(--sun); color: var(--navy); }

/* Stage */
.stage { flex: 1 1 auto; min-height: 0; display: flex; }
.canvas-wrap { position: relative; flex: 1 1 auto; min-width: 0; background: #0f2a45; }
.scroller { position: absolute; inset: 0; display: flex; overflow: auto; -webkit-overflow-scrolling: touch; }
.scroller #yard { margin: auto; flex: none; touch-action: pan-x pan-y; }
.zoom-controls { position: absolute; left: 10px; bottom: 10px; z-index: 5; display: flex; flex-direction: column; gap: 6px; }
.zoom-controls button { width: 42px; height: 42px; border-radius: 12px; border: 3px solid var(--sun); background: var(--navy); color: #fff; font-size: 22px; line-height: 1; padding: 0; }
@media (hover: hover) and (pointer: fine) { .zoom-controls { display: none; } }
#yard { display: block; border-radius: 18px; box-shadow: 0 10px 30px #0008; cursor: default; }
.hint { position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); max-width: 80%; padding: 8px 18px; border-radius: 20px; background: #fff; border: 3px solid var(--ink); color: var(--ink); font-weight: 600; font-size: 16px; text-align: center; box-shadow: 0 4px 0 #0005; pointer-events: none; }
.hint:empty { display: none; }
.toasts { position: absolute; top: 22px; left: 26px; display: flex; flex-direction: column; align-items: flex-start; gap: 8px; pointer-events: none; }
.toast { padding: 8px 18px; border-radius: 18px; background: var(--sun); border: 3px solid var(--ink); font-weight: 600; box-shadow: 0 4px 0 #0005; animation: pop .35s ease-out, fade .5s ease-in 3.2s forwards; }
.toast.good { background: #b8f2cf; }
.toast.bad { background: #ffc9c9; }
@keyframes pop { from { transform: scale(.6) translateY(-14px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { to { opacity: 0; transform: translateY(-10px); } }

/* Departures board */
.board { flex: 0 0 340px; overflow-x: hidden; overflow-y: auto; padding: 14px; background: var(--cream); border-left: 4px solid var(--sun); }
.board h2 { display: flex; align-items: center; gap: 8px; margin: 0 0 4px; font-size: 20px; }
.board-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--orange); box-shadow: 0 0 0 3px #ffd6b0; }
.board-dot.arrive { background: var(--blue); box-shadow: 0 0 0 3px #c5dcff; }
.board-note { margin: 0 0 10px; color: #7a6a4a; font-size: 13px; }
.board section + section { margin-top: 20px; }
.cards { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.card { padding: 10px 12px; border-radius: 16px; background: #fff; border: 3px solid #e4d6b4; box-shadow: 0 3px 0 #e4d6b4; }
.card .top { display: flex; align-items: center; gap: 10px; }
.card .time { padding: 2px 8px; border-radius: 8px; background: var(--navy); color: var(--sun); font-weight: 600; font-size: 17px; letter-spacing: 1px; }
.card .name { flex: 1; min-width: 0; line-height: 1.15; }
.card .name b { display: block; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .name small { color: #7a6a4a; font-size: 12px; }
.chip { padding: 2px 9px; border-radius: 12px; background: #eee3c6; font-size: 12px; font-weight: 600; white-space: nowrap; }
.consist { display: flex; align-items: center; gap: 2px; margin-top: 8px; padding: 4px 6px; border-radius: 10px; background: #f3ebd3; }
.consist img { flex: 1 1 0; min-width: 0; max-width: 40px; height: auto; aspect-ratio: 84 / 60; }
.consist .arrow { flex: 0 0 auto; margin-left: auto; padding-left: 6px; color: #b09a63; font-weight: 700; }
.card.dep.active { border-color: var(--orange); box-shadow: 0 3px 0 #e6a56a; }
.card.status-boarding .chip { background: #ffe29a; }
.card.status-ready .chip { background: #b8f2cf; }
.card.status-done { border-color: #a9e3bf; background: #f1fff6; box-shadow: 0 3px 0 #a9e3bf; }
.card.status-done .chip { background: #b8f2cf; }
.card.status-cancelled { border-color: #f0b0b0; background: #fff3f3; box-shadow: 0 3px 0 #f0b0b0; opacity: .8; }
.card.status-cancelled .chip { background: #ffc9c9; }
.card.status-arrived { opacity: .6; }
.card.status-arriving { border-color: var(--blue); }

/* Overlays */
.overlay { position: absolute; inset: 0; z-index: 10; display: flex; padding: 18px; background: radial-gradient(circle at 50% 30%, #3b7fc4cc, #16294af0); overflow-y: auto; }
.overlay.hidden { display: none; }
.sheet { margin: auto; width: min(640px, 100%); padding: 26px 30px; border-radius: 28px; background: var(--cream); border: 5px solid var(--sun); box-shadow: 0 10px 0 #0006; text-align: center; animation: pop .35s ease-out; }
.sheet h1 { margin: 6px 0; font-size: 44px; color: var(--navy); }
.sheet h2 { margin: 0 0 10px; font-size: 32px; color: var(--navy); }
.lead { margin: 0 0 18px; font-size: 18px; color: #5b5a4a; }
.logo-train { display: flex; justify-content: center; gap: 6px; margin-bottom: 4px; }
.logo-train span { width: 54px; height: 34px; border-radius: 10px; background: var(--blue); border: 3px solid var(--ink); position: relative; animation: bob 1.4s ease-in-out infinite; }
.logo-train span:nth-child(1) { background: var(--green); animation-delay: -.3s; }
.logo-train span:nth-child(2) { background: var(--sun); animation-delay: -.6s; }
.logo-train span:nth-child(3) { background: var(--red); animation-delay: -.9s; }
.logo-train span:nth-child(4) { background: var(--orange); width: 62px; }
.logo-train span::after { content: ''; position: absolute; left: 8px; right: 8px; top: 6px; height: 10px; border-radius: 4px; background: #d9f1ff; border: 2px solid var(--ink); }
@keyframes bob { 50% { transform: translateY(-6px); } }
.sheet.menu-sheet { width: min(980px, 100%); }
.levels { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
@media (max-width: 700px) { .levels { grid-template-columns: 1fr; } }
.level-card { display: flex; align-items: center; gap: 14px; padding: 12px 16px; border: 4px solid var(--ink); border-radius: 20px; background: #fff; text-align: left; color: var(--ink); box-shadow: 0 5px 0 #0003; transition: transform .1s; }
.level-card:hover { transform: translateY(-2px) rotate(-.4deg); }
.level-card .num { width: 46px; height: 46px; flex: 0 0 auto; display: grid; place-items: center; border-radius: 14px; background: var(--orange); color: #fff; font-size: 26px; font-weight: 700; }
.level-card .txt { flex: 1; }
.level-card b { display: block; font-size: 20px; }
.level-card small { color: #6f6a58; font-size: 14px; }
.level-card .earned { font-size: 22px; color: var(--sun); text-shadow: 0 2px 0 #c99a00; white-space: nowrap; }
.level-card .earned i { color: #d8cfae; text-shadow: none; font-style: normal; }
.big { padding: 12px 28px; border: 0; border-radius: 26px; background: var(--green); color: #fff; font-size: 22px; font-weight: 700; box-shadow: 0 5px 0 #26995a; text-shadow: 0 2px 0 #26995a66; }
.big.secondary { background: var(--blue); box-shadow: 0 5px 0 #2f6fd0; text-shadow: 0 2px 0 #2f6fd066; }
.big:active { transform: translateY(3px); box-shadow: 0 2px 0 #26995a; }
.link { margin-top: 14px; border: 0; background: transparent; color: #6f6a58; font-size: 16px; text-decoration: underline; }
.steps { margin: 0 0 14px; padding-left: 24px; text-align: left; font-size: 18px; line-height: 1.45; }
.steps li { margin-bottom: 8px; }
.tip { margin: 0 0 18px; padding: 8px 14px; border-radius: 14px; background: #fff0b8; font-size: 16px; }
.row { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; }
.result-stars { display: flex; justify-content: center; gap: 6px; margin: 4px 0 8px; font-size: 54px; color: #e0d6b8; }
.result-stars .on { color: var(--sun); text-shadow: 0 4px 0 #c99a00; animation: pop .5s ease-out both; }

@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; }
  .stage { flex-direction: column; }
  .board { flex: 0 0 200px; border-left: 0; border-top: 4px solid var(--sun); }
  .stars-total { display: none; }
}

.credit { margin: 18px 0 0; color: #6f6a58; font-size: 14px; }

/* Touch devices: bigger tap targets */
@media (pointer: coarse) {
  .round, .pill, .speed button { min-width: 44px; min-height: 44px; }
}

/* Phones and small tablets in landscape: short screens */
@media (max-height: 520px) {
  .topbar { padding: 4px 10px; gap: 8px; border-bottom-width: 3px; }
  .brand small { display: none; }
  .board { flex-basis: 250px; padding: 8px; }
  .board-note { display: none; }
  .overlay .sheet { max-height: 94dvh; overflow-y: auto; padding: 14px 18px; }
}

/* Phones in portrait */
@media (max-width: 600px) {
  .topbar { padding: 6px 10px; }
  .brand small { display: none; }
  .board { flex-basis: 170px; padding: 10px; }
  .board-note { display: none; }
  .overlay .sheet { max-height: 94dvh; overflow-y: auto; }
}

.rotate-hint { display: none; }
@media (orientation: portrait) and (max-width: 700px) {
  html.is-touch .rotate-hint { display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--sun); color: var(--navy-deep); font-size: 14px; }
  .rotate-hint span { flex: 1; }
  .rotate-hint button { border: 0; background: transparent; font-size: 26px; line-height: 1; color: inherit; min-width: 36px; min-height: 36px; }
  html.is-touch .rotate-hint.dismissed { display: none; }
}
.round:disabled { opacity: .45; cursor: default; transform: none; }
.card.dep { border-left: 8px solid var(--orange); }
.card.arr { border-left: 8px solid var(--blue); }
.card.next { box-shadow: 0 0 0 3px #ffd23f, 0 3px 0 #e6a56a; }
.board-note .board-dot { display: inline-block; width: 10px; height: 10px; margin-right: 4px; vertical-align: middle; box-shadow: none; }

/* Sky chip next to the clock */
.clock { display: flex; align-items: center; gap: 10px; }
.sky-chip { font-size: 24px; line-height: 1; }

/* Special trains on the timetable */
.card.dep.special { border-color: #f2c94c; box-shadow: 0 3px 0 #f2c94c; background: #fffbea; }
.card.dep.special.status-done { background: #f1fff6; border-color: #a9e3bf; box-shadow: 0 3px 0 #a9e3bf; }

/* Results and stats */
.stat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 14px; margin: 0 0 16px; text-align: left; }
.stat-grid:empty { display: none; }
.stat { display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 14px; background: #fff; border: 3px solid #e4d6b4; font-size: 15px; }
.stat .si { font-size: 20px; }
.stat .sn { flex: 1; color: #6f6a58; }
.stat .sv { color: var(--navy); font-size: 17px; white-space: nowrap; }
@media (max-width: 600px) { .stat-grid { grid-template-columns: 1fr; } }

/* Upgrades */
.sheet.upgrades-sheet { width: min(760px, 100%); }
.upgrade-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 0 0 18px; text-align: left; }
@media (max-width: 700px) { .upgrade-list { grid-template-columns: 1fr; } }
.upgrade { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border: 4px solid var(--ink); border-radius: 18px; background: #fff; box-shadow: 0 4px 0 #0003; }
.upgrade.owned { background: #f1fff6; border-color: #3a9b66; }
.upgrade .ui { font-size: 30px; flex: 0 0 auto; }
.upgrade .ut { flex: 1; min-width: 0; }
.upgrade b { display: block; font-size: 17px; }
.upgrade small { color: #6f6a58; font-size: 13px; line-height: 1.2; display: block; }
.upgrade .buy { flex: 0 0 auto; padding: 6px 14px; border: 0; border-radius: 16px; background: var(--sun); color: var(--navy); font-weight: 700; font-size: 16px; box-shadow: 0 3px 0 #c99a00; }
.upgrade .buy:disabled { background: #e8e1c8; color: #9b9378; box-shadow: none; cursor: default; }
.upgrade.owned .buy:disabled { background: #b8f2cf; color: #1f7a48; }
#upgrades-button.glow { animation: glow 1.6s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 5px 0 #2f6fd0, 0 0 0 6px #ffd23f; } }

/* Tutorial */
.tutorial { position: absolute; left: 14px; bottom: 14px; z-index: 6; width: min(380px, calc(100% - 28px)); padding: 12px 16px; border-radius: 20px; background: #fff; border: 4px solid var(--orange); box-shadow: 0 6px 0 #0004; animation: pop .35s ease-out; }
.tutorial.hidden { display: none; }
.tutorial p { margin: 6px 0 8px; font-size: 15px; line-height: 1.35; }
.tut-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.tut-head b { font-size: 18px; color: var(--navy); }
.tut-head span { color: #9b9378; font-size: 13px; font-weight: 600; }
.tut-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tut-row .link { margin: 0; font-size: 13px; }
.tut-next { padding: 6px 18px; border: 0; border-radius: 16px; background: var(--green); color: #fff; font-weight: 700; font-size: 16px; box-shadow: 0 3px 0 #26995a; }
html.is-touch .tutorial { left: 64px; width: min(340px, calc(100% - 78px)); }
.board.tut-pulse { animation: boardpulse 1.2s ease-in-out infinite; }
@keyframes boardpulse { 50% { box-shadow: inset 0 0 0 5px var(--orange); } }

/* Timetable: arrivals sit to the left in red, departures are offset to the right in green. */
.cards .card.arr { margin-right: 28px; border-color: #e8a3a3; border-left: 8px solid #d94f4f; background: #fff1f1; box-shadow: 0 3px 0 #e8a3a3; }
.cards .card.dep { margin-left: 28px; border-color: #9fd9b4; border-left: 8px solid #2f9e6b; background: #f0fbf4; box-shadow: 0 3px 0 #9fd9b4; }
.cards .card.dep.active { border-color: #2f9e6b; box-shadow: 0 3px 0 #1f7a50; }
.cards .card.dep.special { border-color: #f2c94c; border-left-color: #2f9e6b; box-shadow: 0 3px 0 #f2c94c; background: #fffbea; }
.cards .card.arr.status-arriving { border-color: #d94f4f; }
.cards .card.arr.status-derailed { border-color: #d94f4f; background: #ffd9d9; }
.cards .card.next { box-shadow: 0 0 0 3px #ffd23f, 0 3px 0 #e6a56a; }
.cards .card.status-done { border-color: #a9e3bf; background: #f1fff6; box-shadow: 0 3px 0 #a9e3bf; }
.cards .card.status-cancelled { border-color: #f0b0b0; background: #fff3f3; box-shadow: 0 3px 0 #f0b0b0; }

.fast-forward { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 12px 28px; border-radius: 22px; background: var(--sun); border: 3px solid var(--ink); font-size: 26px; font-weight: 700; box-shadow: 0 5px 0 #0005; pointer-events: none; animation: ff-pulse 1s ease-in-out infinite; }
.fast-forward[hidden] { display: none; }
@keyframes ff-pulse { 50% { opacity: .6; } }

.board-note .board-dot { background: #2f9e6b; }
.board-note .board-dot.arrive { background: #d94f4f; }
