* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    font-family: "Microsoft YaHei", "微软雅黑", "PingFang SC", Arial, sans-serif;
    background: #a9def5;
}

button { font: inherit; }

.cargo-game {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #a9def5;
}

.scene-background {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    pointer-events: none;
}

.cargo-game::after {
    content: "";
    position: absolute;
    z-index: 1;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(180deg, rgba(221,247,255,.34), transparent 29%, transparent 66%, rgba(73,72,36,.12));
}

.game-header {
    position: absolute;
    z-index: 40;
    top: max(9px, env(safe-area-inset-top));
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    display: grid;
    grid-template-columns: 54px minmax(0,1fr) 54px;
    align-items: start;
    pointer-events: none;
}

.round-btn {
    width: 52px;
    height: 52px;
    border: 3px solid rgba(255,255,255,.9);
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: rgba(255,255,255,.8);
    color: #32546b;
    text-decoration: none;
    font-size: 29px;
    font-weight: 900;
    line-height: 1;
    box-shadow: 0 6px 0 rgba(47,76,94,.18), 0 9px 24px rgba(40,68,84,.18);
    backdrop-filter: blur(8px);
    cursor: pointer;
    pointer-events: auto;
}

.round-btn:disabled { opacity: .5; cursor: default; }
.round-btn:not(:disabled):active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(47,76,94,.18); }

.title-wrap {
    justify-self: center;
    max-width: min(680px, calc(100vw - 150px));
    padding: 3px 20px 8px;
    border-radius: 20px;
    text-align: center;
    background: rgba(245,253,255,.78);
    box-shadow: 0 7px 24px rgba(43,71,86,.14);
    backdrop-filter: blur(8px);
}

.title-wrap h1 {
    margin: 0;
    color: #29475b;
    font-size: clamp(27px,4vw,43px);
    line-height: 1.08;
    text-shadow: 0 3px 0 rgba(255,255,255,.85);
}

.title-wrap p {
    margin: 3px 0 0;
    color: #526b78;
    font-size: clamp(14px,2vw,18px);
    font-weight: 900;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status-row {
    position: absolute;
    z-index: 18;
    top: max(81px, calc(env(safe-area-inset-top) + 72px));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 8px;
}

.status-row span {
    padding: 7px 13px;
    border: 2px solid rgba(255,255,255,.86);
    border-radius: 999px;
    background: rgba(245,253,255,.78);
    color: #3c6075;
    font-size: clamp(13px,1.65vw,17px);
    font-weight: 900;
    white-space: nowrap;
    box-shadow: 0 5px 15px rgba(43,72,88,.13);
    backdrop-filter: blur(7px);
}

#collection-badge.complete { color: #9a5b0f; background: rgba(255,239,137,.94); }
.play-area { position: absolute; z-index: 5; inset: 0; }

.train-stage {
    position: absolute;
    z-index: 8;
    left: 50%;
    top: 49%;
    width: min(1180px, 96vw);
    height: clamp(190px, 25vw, 285px);
    transform: translate(-50%, -50%);
    will-change: transform;
}

.train {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.locomotive {
    position: relative;
    flex: 0 0 27%;
    height: 100%;
    z-index: 3;
}

.locomotive img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    object-position: right bottom;
    filter: drop-shadow(0 13px 8px rgba(43,45,40,.26));
    pointer-events: none;
}

.coupler {
    position: relative;
    z-index: 1;
    flex: 0 0 clamp(9px,1.35vw,18px);
    height: 56px;
    align-self: flex-end;
    /* Keep the link on the same centerline as the engine hook and wagon chassis. */
    transform: translateY(clamp(-56px, -4.4vw, -28px));
}

.coupler::before,
.coupler::after {
    content: "";
    position: absolute;
    top: 14px;
    width: 9px;
    height: 22px;
    border: 4px solid #2f3540;
    border-radius: 50%;
    background: #69727c;
}

.coupler::before { left: -4px; }
.coupler::after { right: -4px; }
.coupler i { position: absolute; left: 2px; right: 2px; top: 23px; height: 7px; border-radius: 7px; background: #343a43; }

/* The locomotive artwork has transparent padding on its right edge. Extend
   only the first link into that padding so its hook visibly meets the train. */
.locomotive + .coupler::before {
    left: clamp(-16px, -1.2vw, -8px);
}

.locomotive + .coupler i {
    left: clamp(-20px, -1.45vw, -10px);
}

.wagon {
    position: relative;
    flex: 0 0 21.3%;
    height: 68%;
    min-width: 0;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
}

.wagon-body {
    position: absolute;
    z-index: 2;
    left: 1%;
    right: 1%;
    bottom: 34px;
    height: 68%;
    border: clamp(3px,.42vw,6px) solid #2a313b;
    border-radius: 20px 20px 13px 13px;
    overflow: hidden;
    background: linear-gradient(180deg, var(--wagon-light) 0 11%, var(--wagon-color) 12% 72%, var(--wagon-dark) 73% 100%);
    box-shadow: inset 0 8px 0 rgba(255,255,255,.23), inset 0 -8px 0 rgba(0,0,0,.1), 0 11px 8px rgba(44,45,37,.2);
}

.wagon-body::before {
    content: "";
    position: absolute;
    z-index: 3;
    left: -2%;
    right: -2%;
    top: 22%;
    height: 10px;
    border: 4px solid #2a313b;
    border-radius: 9px;
    background: var(--wagon-light);
}

.wagon-body::after {
    content: "";
    position: absolute;
    left: 11%;
    right: 11%;
    bottom: 16%;
    height: 10px;
    border-radius: 9px;
    background: rgba(255,255,255,.22);
}

.shine { position: absolute; z-index: 4; left: 8%; top: 43%; width: 7%; height: 27%; border-radius: 99px; background: rgba(255,255,255,.32); transform: rotate(8deg); }

.cargo-bay {
    position: absolute;
    z-index: 1;
    left: 10%;
    right: 10%;
    top: -16%;
    height: 68%;
    display: grid;
    place-items: center;
    pointer-events: none;
}

.cargo-bay img {
    width: 90%;
    height: 100%;
    object-fit: contain;
    opacity: 0;
    transform: translateY(-28px) scale(.62);
    filter: drop-shadow(0 7px 5px rgba(42,42,35,.22));
    transition: opacity .2s ease, transform .55s cubic-bezier(.2,.9,.3,1.25);
}

.wagon.loaded .cargo-bay img { opacity: 1; transform: translateY(-34px) scale(.78); }

.wagon-wheel {
    position: absolute;
    z-index: 6;
    bottom: 8px;
    width: clamp(31px,4.2vw,52px);
    aspect-ratio: 1;
    border: clamp(4px,.48vw,6px) solid #28303a;
    border-radius: 50%;
    background: radial-gradient(circle, #ffd45c 0 18%, #3e4855 19% 45%, #92a0ad 46% 58%, #27313c 59% 100%);
    box-shadow: 0 5px 4px rgba(41,42,37,.25);
}

.wheel-left { left: 13%; }
.wheel-right { right: 13%; }

.wagon-label {
    position: absolute;
    z-index: 8;
    left: 50%;
    bottom: -8px;
    transform: translateX(-50%);
    padding: 4px 9px;
    border: 2px solid rgba(255,255,255,.9);
    border-radius: 999px;
    background: rgba(250,252,245,.92);
    color: #4c5661;
    font-size: clamp(11px,1.45vw,16px);
    font-weight: 900;
    white-space: nowrap;
    box-shadow: 0 4px 0 rgba(43,49,55,.15);
}

.wagon-label i { display: inline-block; width: .8em; height: .8em; margin-right: 4px; border: 2px solid rgba(38,44,50,.55); border-radius: 50%; vertical-align: -1px; }

.wagon.ready .wagon-body,
.wagon:focus-visible .wagon-body { outline: 6px dashed #fff378; outline-offset: 3px; filter: brightness(1.08); }
.wagon.loaded .wagon-body { box-shadow: inset 0 8px 0 rgba(255,255,255,.23), inset 0 -8px 0 rgba(0,0,0,.1), 0 0 0 5px rgba(255,236,98,.8), 0 11px 8px rgba(44,45,37,.2); }
.wagon.wrong { animation: wagonNo .48s ease; }

.toy-dock {
    position: absolute;
    z-index: 18;
    left: 50%;
    bottom: max(14px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    width: min(720px,92vw);
    min-height: 130px;
    padding: 23px 18px 9px;
    border: 4px solid rgba(255,255,255,.88);
    border-radius: 30px;
    background: linear-gradient(180deg, rgba(245,253,255,.9), rgba(213,240,250,.9));
    box-shadow: 0 10px 0 rgba(49,99,123,.14), 0 15px 32px rgba(47,70,80,.2);
    backdrop-filter: blur(9px);
    transition: transform .35s ease, opacity .35s ease;
}

.toy-dock.hidden { opacity: 0; transform: translate(-50%, 140%); pointer-events: none; }

.dock-title {
    position: absolute;
    top: -18px;
    left: 50%;
    transform: translateX(-50%);
    padding: 5px 15px;
    border: 3px solid rgba(255,255,255,.92);
    border-radius: 999px;
    background: #53c3e8;
    color: #1f5065;
    font-size: 16px;
    font-weight: 900;
    white-space: nowrap;
    box-shadow: 0 4px 0 #238caf;
}

.toy-row { display: flex; justify-content: space-evenly; align-items: center; gap: 12px; }

.toy-card {
    position: relative;
    width: clamp(108px,13vw,138px);
    height: 101px;
    padding: 0 5px 5px;
    border: 3px solid transparent;
    border-radius: 22px;
    display: grid;
    grid-template-rows: 1fr auto;
    place-items: center;
    background: rgba(255,255,255,.38);
    color: #405462;
    font-weight: 900;
    cursor: grab;
    transition: transform .16s ease, background .16s ease, border-color .16s ease, opacity .2s ease;
}

.toy-card > img { width: 78px; height: 78px; object-fit: contain; pointer-events: none; filter: drop-shadow(0 7px 5px rgba(50,60,57,.2)); }
.toy-card > span { margin-top: -9px; font-size: 14px; white-space: nowrap; }
.toy-card > span i { display: inline-block; width: 10px; height: 10px; margin-right: 4px; border: 1px solid rgba(38,44,50,.5); border-radius: 50%; }
.toy-card.selected { transform: translateY(-10px) scale(1.08); border-color: #ffe35c; background: rgba(255,250,189,.9); box-shadow: 0 0 0 4px rgba(255,255,255,.6); }
.toy-card.used { opacity: .14; transform: scale(.72); pointer-events: none; }
.toy-card.wrong { animation: toyNo .48s ease; background: rgba(255,194,181,.82); }

.drag-ghost {
    position: fixed;
    z-index: 100;
    width: 96px;
    height: 96px;
    object-fit: contain;
    transform: translate(-50%,-50%) scale(1.1);
    filter: drop-shadow(0 12px 8px rgba(48,52,45,.3));
    pointer-events: none;
}

.travel-message {
    position: absolute;
    z-index: 35;
    left: 50%;
    top: 28%;
    transform: translate(-50%,-50%);
    padding: 10px 18px;
    border: 4px solid rgba(255,255,255,.9);
    border-radius: 999px;
    background: rgba(255,236,126,.94);
    color: #6a4318;
    font-size: clamp(19px,3vw,28px);
    box-shadow: 0 7px 0 #d39831, 0 12px 25px rgba(72,63,25,.2);
    animation: messagePop .45s cubic-bezier(.2,.9,.3,1.3);
}
.travel-message[hidden] { display: none; }

.steam-cloud {
    position: absolute;
    z-index: 12;
    left: 7.6%;
    top: -7%;
    width: 118px;
    height: 92px;
    pointer-events: none;
    opacity: 0;
}

.steam-cloud i {
    position: absolute;
    left: 0;
    bottom: 8px;
    width: clamp(22px, 2.7vw, 34px);
    height: clamp(22px, 2.7vw, 34px);
    border: 2px solid rgba(255,255,255,.32);
    border-radius: 50%;
    background: radial-gradient(circle at 32% 28%, rgba(123,132,137,.88), rgba(65,72,77,.86) 64%, rgba(43,49,54,.78));
    box-shadow: inset 0 3px 6px rgba(255,255,255,.2), 0 4px 7px rgba(42,48,52,.16);
    opacity: 0;
    transform-origin: center;
}

.steam-cloud i:nth-child(2) { animation-delay: .36s !important; }
.steam-cloud i:nth-child(3) { animation-delay: .72s !important; }

.speed-lines { position: absolute; z-index: -1; left: 94%; top: 42%; width: 30vw; height: 110px; opacity: 0; background: repeating-linear-gradient(180deg, transparent 0 13px, rgba(255,255,255,.74) 14px 19px, transparent 20px 31px); mask-image: linear-gradient(90deg, #000, transparent); }

.train-stage.departing { animation: depart 2.75s cubic-bezier(.42,0,1,1) forwards; }
.train-stage.departing .steam-cloud { opacity: 1; }
.train-stage.departing .steam-cloud i { animation: smokePuff 1.08s ease-out infinite; }
.train-stage.departing .speed-lines { animation: linesIn 1.8s .65s ease forwards; }
.train-stage.departing .wagon-wheel { animation: wheelDepart 2.75s cubic-bezier(.42,0,1,1) forwards; }
.train-stage.entering { animation: enter 1.85s cubic-bezier(.12,.76,.24,1) both; }
.train-stage.entering .wagon-wheel { animation: wheelEnter 1.85s cubic-bezier(.12,.76,.24,1) both; }

@keyframes depart { from { transform: translate(-50%,-50%); } to { transform: translate(-190vw,-50%); } }
@keyframes enter { from { transform: translate(135vw,-50%); } to { transform: translate(-50%,-50%); } }
@keyframes wheelDepart { from { transform: rotate(0); } to { transform: rotate(-2520deg); } }
@keyframes wheelEnter { from { transform: rotate(0); } to { transform: rotate(-1080deg); } }
@keyframes smokePuff {
    0% { opacity: 0; transform: translate(0, 10px) scale(.28); }
    14% { opacity: .78; }
    64% { opacity: .52; }
    100% { opacity: 0; transform: translate(-78px, -76px) scale(1.32); }
}
@keyframes linesIn { from { opacity: 0; transform: translateX(0); } to { opacity: .8; transform: translateX(30px); } }
@keyframes wagonNo { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-9px); } 75% { transform: translateX(9px); } }
@keyframes toyNo { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-10px) rotate(-5deg); } 75% { transform: translateX(10px) rotate(5deg); } }
@keyframes messagePop { from { opacity: 0; transform: translate(-50%,-30%) scale(.6); } to { opacity: 1; transform: translate(-50%,-50%) scale(1); } }

@media (max-height: 650px) and (min-width: 700px) {
    .status-row { top: 69px; }
    .train-stage { top: 47%; height: min(25vw,220px); }
    .toy-dock { min-height: 112px; padding-top: 18px; }
    .toy-card { height: 86px; }
    .toy-card > img { width: 64px; height: 64px; }
}

@media (max-width: 600px) {
    .game-header { grid-template-columns: 46px minmax(0,1fr) 46px; top: max(7px, env(safe-area-inset-top)); left: 8px; right: 8px; }
    .round-btn { width: 44px; height: 44px; border-width: 2px; font-size: 25px; }
    .title-wrap { max-width: calc(100vw - 105px); padding: 3px 9px 6px; border-radius: 16px; }
    .title-wrap h1 { font-size: 25px; }
    .title-wrap p { font-size: 12px; }
    .status-row { top: max(61px, calc(env(safe-area-inset-top) + 54px)); gap: 5px; }
    .status-row span { padding: 5px 8px; font-size: 12px; }
    .train-stage { top: 47%; width: 99vw; height: 43vw; min-height: 165px; max-height: 185px; }
    .locomotive { flex-basis: 28%; }
    .wagon { flex-basis: 21.1%; height: 70%; }
    .coupler { flex-basis: 7px; height: 42px; }
    .coupler::before, .coupler::after { width: 6px; height: 15px; border-width: 2px; }
    .coupler i { top: 19px; height: 4px; }
    .wagon-body { bottom: 25px; border-width: 3px; border-radius: 10px 10px 7px 7px; }
    .wagon-body::before { height: 5px; border-width: 2px; }
    .wagon-body::after { height: 5px; }
    .wagon-wheel { bottom: 6px; width: 25px; border-width: 3px; }
    .wagon-label { bottom: -7px; padding: 2px 4px; border-width: 1px; font-size: 9px; box-shadow: 0 2px 0 rgba(43,49,55,.15); }
    .cargo-bay { top: -17%; }
    .wagon.loaded .cargo-bay img { transform: translateY(-23px) scale(.68); }
    .steam-cloud { left: 5%; top: -12%; width: 82px; height: 72px; }
    .toy-dock { width: 96vw; bottom: max(9px, env(safe-area-inset-bottom)); min-height: 113px; padding: 21px 5px 7px; border-width: 3px; border-radius: 23px; }
    .dock-title { top: -15px; padding: 3px 10px; border-width: 2px; font-size: 13px; box-shadow: 0 3px 0 #238caf; }
    .toy-row { gap: 0; }
    .toy-card { width: 31%; height: 85px; border-radius: 16px; }
    .toy-card > img { width: 65px; height: 65px; }
    .toy-card > span { font-size: 11px; margin-top: -8px; }
    .drag-ghost { width: 80px; height: 80px; }
    .travel-message { top: 27%; white-space: nowrap; }
}

@media (prefers-reduced-motion: reduce) {
    .train-stage.departing { animation-duration: .5s !important; }
    .train-stage.entering { animation-duration: .5s !important; }
    .train-stage.departing .wagon-wheel,
    .train-stage.entering .wagon-wheel,
    .train-stage.departing .steam-cloud i { animation-duration: .01ms !important; }
}
