*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: #ececec;
    color: #222;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.4;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

button {
    font: inherit;
    touch-action: manipulation;
}

.app {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    padding:
        env(safe-area-inset-top, 0)
        env(safe-area-inset-right, 0)
        env(safe-area-inset-bottom, 0)
        env(safe-area-inset-left, 0);
}

@supports not (height: 100dvh) {
    .app {
        height: 100vh;
        max-height: 100vh;
    }
}

.app-header {
    flex-shrink: 0;
    padding: 0.5rem 0.75rem 0.55rem;
    background: #f7f7f7;
    border-bottom: 1px solid #ccc;
}

.app-name {
    margin: 0 0 0.35rem;
    font-size: 0.75rem;
    color: #666;
}

.hud {
    margin: 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.25rem 1rem;
}

.hud__row {
    display: flex;
    gap: 0.35rem;
    align-items: baseline;
    min-width: 0;
}

.hud__row--wide {
    grid-column: 1 / -1;
}

.hud dt {
    margin: 0;
    font-size: 0.72rem;
    color: #777;
    flex-shrink: 0;
}

.hud dd {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#money-label {
    font-variant-numeric: tabular-nums;
}

.game {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 0.5rem 0.65rem 0.65rem;
    gap: 0.5rem;
    background: #ececec;
}

.viewport {
    position: relative;
    flex: 1 1 auto;
    min-height: 9rem;
    border: 1px solid #bbb;
    background: #ddd;
    overflow: hidden;
}

.scene {
    position: absolute;
    inset: 0;
}

.scene[hidden] {
    display: none !important;
}

.scene-bg {
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
}

.scene-bg--stop {
    background:
        linear-gradient(180deg, #d8e2ea 0%, #d8e2ea 55%, #b8c4ce 55%, #9aa8b4 100%);
}

.scene-bg--stop::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 28%;
    background: #8a959e;
    border-top: 2px solid #6d7882;
}

.scene-bg--stop::before {
    content: "";
    position: absolute;
    left: 8%;
    right: 8%;
    bottom: 26%;
    height: 12px;
    background: #f5f5f5;
    border: 1px solid #999;
    box-shadow: 0 8px 0 -4px rgba(0, 0, 0, 0.08);
}

.scene-bg--drive {
    background:
        linear-gradient(180deg, #c9d6e3 0%, #c9d6e3 42%, #7a8794 42%, #5c6773 100%);
}

.scene-bg--drive::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: 38%;
    height: 3px;
    background: #e8e8e8;
    opacity: 0.5;
}

.scene-bg--drive::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 45%;
    background: repeating-linear-gradient(
        90deg,
        #4a5560 0,
        #4a5560 40px,
        #3d464f 40px,
        #3d464f 80px
    );
    border-top: 3px solid #2f363d;
}

.scene-canvas {
    position: absolute;
    inset: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    display: block;
}

.stop-caption {
    position: absolute;
    z-index: 2;
    top: 0.35rem;
    left: 0.5rem;
    margin: 0;
    padding: 0.15rem 0.4rem;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #444;
    background: rgba(255, 255, 255, 0.65);
    border: 1px solid rgba(0, 0, 0, 0.12);
}

.stop-caption--drive {
    color: #eee;
    background: rgba(0, 0, 0, 0.35);
    border-color: rgba(255, 255, 255, 0.15);
}

.crowd-panel {
    position: absolute;
    z-index: 2;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0.4rem;
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    padding: 0.3rem 0.55rem;
    background: #fff;
    border: 1px solid #aaa;
    font-size: 0.85rem;
}

.crowd-panel__label {
    color: #666;
}

.crowd-panel__count {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.message {
    margin: 0;
    font-size: 0.85rem;
    color: #444;
    min-height: 2.4em;
    text-align: left;
    padding: 0 0.1rem;
}

.timer-wrap {
    flex-shrink: 0;
}

.timer-wrap.is-hidden {
    visibility: hidden;
    height: 6px;
}

.timer-bar {
    height: 6px;
    background: #ccc;
    border: 1px solid #aaa;
}

.timer-fill {
    height: 100%;
    width: 100%;
    background: #333;
    transform-origin: left center;
    transform: scaleX(1);
}

.timer-fill.is-urgent {
    background: #a00;
}

.lane-controls {
    flex-shrink: 0;
    display: flex;
    gap: 0.5rem;
}

.lane-controls[hidden] {
    display: none !important;
}

.controls {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.btn {
    width: 100%;
    min-height: 2.75rem;
    padding: 0.55rem 0.75rem;
    border: 1px solid #888;
    border-radius: 2px;
    background: #fff;
    color: #222;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

.btn:active:not(:disabled) {
    background: #e4e4e4;
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn--primary {
    background: #222;
    border-color: #111;
    color: #fff;
}

.btn--primary:active:not(:disabled) {
    background: #444;
}

.btn--plain {
    background: #f7f7f7;
    font-weight: 500;
    font-size: 0.9rem;
    min-height: 2.4rem;
}

.btn--lane {
    flex: 1;
    min-height: 2.5rem;
    font-size: 0.95rem;
}

@media (prefers-color-scheme: dark) {
    body {
        background: #1a1a1a;
        color: #e8e8e8;
    }

    .app-header {
        background: #242424;
        border-bottom-color: #444;
    }

    .app-name {
        color: #999;
    }

    .hud dt {
        color: #888;
    }

    .game {
        background: #1a1a1a;
    }

    .viewport {
        border-color: #555;
        background: #2a2a2a;
    }

    .scene-bg--stop {
        background: linear-gradient(180deg, #3a4a58 0%, #3a4a58 55%, #2a343c 55%, #1e262c 100%);
    }

    .scene-bg--stop::after {
        background: #151a1f;
        border-top-color: #0d1014;
    }

    .scene-bg--stop::before {
        background: #3a3a3a;
        border-color: #555;
    }

    .scene-bg--drive {
        background: linear-gradient(180deg, #2a3540 0%, #2a3540 42%, #1a2228 42%, #12181d 100%);
    }

    .scene-bg--drive::after {
        background: repeating-linear-gradient(
            90deg,
            #2a3238 0,
            #2a3238 40px,
            #222930 40px,
            #222930 80px
        );
        border-top-color: #111;
    }

    .stop-caption {
        color: #ddd;
        background: rgba(0, 0, 0, 0.45);
        border-color: #555;
    }

    .crowd-panel {
        background: #2a2a2a;
        border-color: #555;
    }

    .crowd-panel__label {
        color: #aaa;
    }

    .message {
        color: #bbb;
    }

    .timer-bar {
        background: #333;
        border-color: #555;
    }

    .timer-fill {
        background: #ccc;
    }

    .timer-fill.is-urgent {
        background: #e55;
    }

    .btn {
        background: #333;
        border-color: #555;
        color: #eee;
    }

    .btn--primary {
        background: #eee;
        color: #111;
        border-color: #ccc;
    }

    .btn--primary:active:not(:disabled) {
        background: #ccc;
    }

    .btn--plain {
        background: #2a2a2a;
    }
}
