html, body, canvas {
    margin: 0;
    padding: 0;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    overscroll-behavior: none;
}

html, body {
    height: 100%;
    overflow: hidden;
    background: #17181b;
}

#game {
    position: fixed;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
}

[hidden] {
    display: none !important;
}

.tuner-btn {
    font: 600 14px system-ui, sans-serif;
    color: #eceef1;
    background: #2b2d31;
    border: 1px solid #44474d;
    border-radius: 6px;
    padding: 8px 12px;
    min-height: 40px;
}

.tuner-btn.active {
    background: #5a4a14;
    border-color: #ffd23f;
}

.tune-open {
    position: fixed;
    top: 64px;
    right: calc(8px + env(safe-area-inset-right));
    background: rgba(22, 23, 26, 0.7);
}

.tuner {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 45vw;
    box-sizing: border-box;
    overflow-y: auto;
    padding: 8px calc(10px + env(safe-area-inset-right)) 24px 10px;
    background: rgba(22, 23, 26, 0.94);
    color: #eceef1;
    font: 14px system-ui, sans-serif;
    touch-action: pan-y;
}

.tuner-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.tuner-status {
    min-height: 18px;
    margin: 6px 0;
    color: #ffd23f;
}

.tuner-group summary {
    padding: 10px 0;
    font-weight: 700;
    border-top: 1px solid #3a3d43;
}

.tuner-row {
    display: block;
    margin: 4px 0 10px;
}

.tuner-label {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    color: #a3a7ad;
}

.tuner-label.changed {
    color: #ffd23f;
}

.tuner-value {
    font-variant-numeric: tabular-nums;
    color: #eceef1;
}

.tuner-row input {
    width: 100%;
    height: 32px;
    margin: 0;
    touch-action: none;
}

.tuner-json {
    width: 100%;
    box-sizing: border-box;
    margin-top: 10px;
    font: 12px ui-monospace, monospace;
    color: #eceef1;
    background: #111214;
    border: 1px solid #3a3d43;
    user-select: text;
    -webkit-user-select: text;
}

.results, .track-select {
    position: fixed;
    inset: 0;
    box-sizing: border-box;
    padding: 12px calc(16px + env(safe-area-inset-right)) 12px calc(16px + env(safe-area-inset-left));
    color: #eceef1;
    font: 15px system-ui, sans-serif;
    font-variant-numeric: tabular-nums;
}

.results {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(16, 17, 19, 0.88);
}

.results-title {
    margin: 0 0 4px;
    font-size: 20px;
    letter-spacing: 0.08em;
}

.results-delta {
    margin: 0;
    font-size: 48px;
    font-weight: 800;
    line-height: 1.1;
}

.results-delta.faster { color: #7dff9b; }
.results-delta.slower { color: #ff6b5e; }

.results-banner {
    color: #ffd23f;
    font-weight: 800;
    letter-spacing: 0.06em;
    animation: pulse 0.8s ease-in-out infinite alternate;
}

@keyframes pulse {
    from { opacity: 1; }
    to { opacity: 0.55; }
}

.results-body {
    flex: 1;
    overflow-y: auto;
    text-align: center;
}

.results-table {
    margin: 6px auto;
    border-collapse: collapse;
}

.results-table th, .results-table td {
    padding: 2px 14px;
    text-align: right;
}

.results-table th:first-child {
    text-align: left;
}

.results-total {
    border-top: 1px solid #44474d;
    font-weight: 700;
}

.results-table .faster {
    color: #7dff9b;
}

.results-table .slower {
    color: #ff6b5e;
}

.results-line {
    margin: 4px 0;
    color: #a3a7ad;
}

.results-buttons {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    width: 100%;
}

.results-buttons button {
    font: inherit;
    color: #eceef1;
    border-radius: 8px;
    border: 1px solid #44474d;
    background: #2b2d31;
}

.results-home {
    padding: 10px 18px;
}

.results-buttons .results-retry {
    padding: 16px 64px;
    font-size: 22px;
    font-weight: 800;
    color: #17181b;
    background: #eceef1;
    border-color: #ffffff;
}

/* Track select: a grid of track cards over a slowly drifting backdrop. */

.track-select {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: safe center;
    gap: 14px;
    overflow: hidden;
    background: #131418;
}

.select-backdrop, .select-backdrop::before, .select-backdrop::after {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.select-backdrop {
    background:
        radial-gradient(60% 70% at 15% 0%, rgba(255, 46, 136, 0.16), transparent 70%),
        radial-gradient(55% 65% at 90% 10%, rgba(61, 90, 254, 0.18), transparent 70%),
        radial-gradient(70% 60% at 50% 110%, rgba(255, 165, 60, 0.12), transparent 70%);
}

/* Speed lines streaking right to left. */
.select-backdrop::before {
    content: '';
    background: repeating-linear-gradient(100deg, transparent 0 46px, rgba(255, 255, 255, 0.015) 46px 48px, transparent 48px 130px);
    background-size: 260px 100%;
    animation: streak 2.4s linear infinite;
}

/* A low-poly ridge along the bottom edge. */
.select-backdrop::after {
    content: '';
    top: auto;
    height: 34%;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.01));
    clip-path: polygon(0 70%, 8% 40%, 16% 62%, 27% 18%, 36% 55%, 44% 38%, 55% 72%, 63% 30%, 72% 50%, 81% 12%, 90% 48%, 100% 28%, 100% 100%, 0 100%);
}

@keyframes streak {
    to { background-position: -260px 0; }
}

.track-select-title {
    position: relative;
    display: flex;
    gap: 0.3em;
    margin: 0;
    font-size: 30px;
    font-weight: 900;
    font-style: italic;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    transform: skewX(-8deg);
    animation: title-in 600ms cubic-bezier(0.2, 0.9, 0.3, 1.15) both;
}

.title-word.accent {
    color: transparent;
    background: linear-gradient(100deg, #ffd23f 0 40%, #fff6d0 50%, #ffa53c 60% 100%);
    background-size: 300% 100%;
    background-clip: text;
    -webkit-background-clip: text;
    animation: shimmer 3.2s ease-in-out 600ms infinite;
}

@keyframes title-in {
    from { opacity: 0; transform: skewX(-8deg) translateX(-40px); }
    to { opacity: 1; transform: skewX(-8deg); }
}

@keyframes shimmer {
    0%, 60% { background-position: 100% 0; }
    100% { background-position: 0 0; }
}

.track-grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr));
    gap: 16px;
    width: 100%;
    max-width: 1100px;
    min-height: 0;
    padding: 6px 4px 14px;
    box-sizing: border-box;
    overflow-y: auto;
    touch-action: pan-y;
}

.track-card {
    --glow: color-mix(in srgb, var(--sky-low) 55%, transparent);
    position: relative;
    display: flex;
    flex-direction: column;
    padding: 0;
    overflow: hidden;
    font: inherit;
    color: #eceef1;
    text-align: left;
    cursor: pointer;
    border: 1px solid #34363c;
    border-radius: 14px;
    background: linear-gradient(165deg, color-mix(in srgb, var(--sky-top) 16%, #24262b), #1b1c20 70%);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
    transition: transform 260ms cubic-bezier(0.2, 0.9, 0.3, 1.3), box-shadow 260ms ease, border-color 260ms ease;
    animation: deal 560ms cubic-bezier(0.2, 0.9, 0.3, 1.2) backwards;
    animation-delay: calc(var(--i) * 90ms + 150ms);
}

@keyframes deal {
    from { opacity: 0; transform: translateY(28px) scale(0.92) rotate(-1.5deg); }
    to { opacity: 1; transform: none; }
}

/* A glare that sweeps across the card when it is picked out. */
.track-card::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(110deg, transparent 35%, rgba(255, 255, 255, 0.14) 50%, transparent 65%);
    transform: translateX(-110%);
}

.track-hero {
    position: relative;
    display: block;
    overflow: hidden;
    aspect-ratio: 320 / 120;
}

.track-art {
    position: absolute;
    inset: 0;
    transition: transform 600ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.track-thumb {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
}

.track-num {
    position: absolute;
    left: 10px;
    top: 4px;
    font-size: 28px;
    font-weight: 900;
    font-style: italic;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}

.track-laps {
    position: absolute;
    right: 8px;
    top: 8px;
    padding: 3px 9px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    border-radius: 999px;
    background: rgba(10, 11, 14, 0.55);
    backdrop-filter: blur(4px);
}

/* The call to action: slides in on hover, always shown where there is no hover. */
.track-go {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 6px 14px 6px 12px;
    font-size: 13px;
    font-weight: 900;
    font-style: italic;
    letter-spacing: 0.1em;
    color: #17181b;
    background: #ffffff;
    border-radius: 6px;
    transform: skewX(-10deg);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
    transition: transform 280ms cubic-bezier(0.2, 0.9, 0.3, 1.3), opacity 200ms ease;
}

.track-go::after {
    content: '';
    display: inline-block;
    margin-left: 7px;
    border-left: 8px solid currentColor;
    border-top: 5px solid transparent;
    border-bottom: 5px solid transparent;
}

.track-info {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 10px 14px 12px;
}

.track-name {
    font-size: 19px;
    font-weight: 800;
}

.track-best {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.track-time {
    font-size: 20px;
    font-weight: 700;
}

.track-time.none, .track-ideal, .track-target {
    color: #a3a7ad;
}

.track-ideal, .track-target {
    font-size: 13px;
}

.track-medals {
    display: flex;
    gap: 7px;
}

/* Medals are diamonds: polygons, like everything else here. */
.medal-pip {
    width: 10px;
    height: 10px;
    box-sizing: border-box;
    border: 2px solid #44474d;
    transform: rotate(45deg);
    transition: transform 260ms ease;
}

.medal-pip.won.gold { background: #ffd23f; border-color: #ffd23f; box-shadow: 0 0 10px rgba(255, 210, 63, 0.7); }
.medal-pip.won.silver { background: #c4c8d0; border-color: #c4c8d0; }
.medal-pip.won.bronze { background: #cd7f32; border-color: #cd7f32; }

@media (hover: hover) {
    .track-go {
        opacity: 0;
        transform: skewX(-10deg) translateX(140%);
    }

    .track-card:hover .track-go {
        opacity: 1;
        transform: skewX(-10deg);
    }

    .track-card:hover {
        transform: translateY(-6px);
        border-color: var(--sky-low);
        box-shadow: 0 14px 30px rgba(0, 0, 0, 0.45), 0 0 24px var(--glow);
    }

    .track-card:hover .track-art {
        transform: scale(1.08) translateY(-2%);
    }

    .track-card:hover .medal-pip {
        transform: rotate(225deg);
    }

    .track-card:hover::after {
        transform: translateX(110%);
        transition: transform 700ms ease;
    }
}

.track-card:focus-visible {
    outline: none;
    border-color: #ffffff;
    box-shadow: 0 0 0 2px #ffffff, 0 0 24px var(--glow);
}

.track-card:active {
    transform: scale(0.97);
    transition-duration: 90ms;
}

@media (prefers-reduced-motion: reduce) {
    .track-select *, .track-select *::before, .track-select *::after, .select-backdrop::before {
        animation: none !important;
        transition: none !important;
    }
}

.update-toast {
    position: absolute;
    left: 50%;
    bottom: calc(12px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    padding: 10px 20px;
    font: 600 15px system-ui, sans-serif;
    color: #17181b;
    background: #ffd23f;
    border: none;
    border-radius: 20px;
    box-shadow: 0 6px 20px rgba(255, 210, 63, 0.35);
    animation: toast-in 400ms cubic-bezier(0.2, 0.9, 0.3, 1.3) backwards;
}

@keyframes toast-in {
    from { opacity: 0; transform: translate(-50%, 20px); }
}

.veil {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: #000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 250ms linear;
}

.veil.shown {
    opacity: 1;
    pointer-events: auto;
}

.results {
    animation: fade-in 250ms linear;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.pause {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    padding: 96px calc(16px + env(safe-area-inset-right)) 16px calc(16px + env(safe-area-inset-left));
    background: rgba(16, 17, 19, 0.7);
    color: #eceef1;
    font: 15px system-ui, sans-serif;
}

.pause-title {
    margin: 0;
    font-size: 28px;
    letter-spacing: 0.12em;
}

.pause-buttons {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    width: 100%;
}

.pause-buttons button {
    font: inherit;
    color: #eceef1;
    border-radius: 8px;
    border: 1px solid #44474d;
    background: #2b2d31;
    padding: 10px 18px;
}

.pause-buttons .pause-retry {
    margin-left: auto;
    padding: 14px 28px;
    font-size: 18px;
}

.pause-buttons .pause-resume {
    padding: 16px 64px;
    font-size: 22px;
    font-weight: 800;
    color: #17181b;
    background: #eceef1;
    border-color: #ffffff;
}
