:root {
    color-scheme: dark;
    font-family: system-ui, -apple-system, sans-serif;
    color: #f4f5df;
    background: #14231b;
    font-synthesis: none;
}

* { box-sizing: border-box; }
body { margin: 0; }

main {
    width: min(100%, 96rem);
    margin-inline: auto;
    padding: clamp(0.85rem, 2.4vw, 2.25rem);
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.55rem 1rem;
    margin-block-end: 1rem;
}

.machine-select {
    font-size: clamp(1rem, 3vw, 1.375rem);
    font-weight: 700;
}

.header-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.55rem 1rem;
}

a { color: #dce9ba; }

.game-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(14rem, 18rem);
    gap: 1rem;
    align-items: start;
}

.workspace {
    display: grid;
    gap: 0.75rem;
    min-width: 0;
}

.playfield {
    position: relative;
    min-width: 0;
}

.viewport, .controls, .lever-panel {
    border: 1px solid #405340;
    border-radius: 0.85rem;
}

.viewport {
    position: relative;
    overflow: hidden;
    height: clamp(19rem, calc(100svh - 18rem), 58rem);
    background: #25392d;
}

.viewport > canvas {
    display: block;
    width: 100%;
    height: 100%;
}

.challenge-hud {
    position: absolute;
    inset: 0.5rem auto auto 0.5rem;
    z-index: 1;
    max-width: calc(100% - 1rem);
    font-size: 0.875rem;
    margin: 0;
    padding: 0.45rem 0.6rem;
    border: 1px solid #ae8a44;
    border-radius: 0.45rem;
    background: #18281d;
}

.challenge-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.4rem 0.6rem;
}

.challenge-heading button { flex-shrink: 0; }

#challenge-restart {
    border-color: #ae8a44;
    color: #ffdf8a;
    margin-block-start: 0.35rem;
}

#challenge-status {
    order: -1;
    margin: 0;
    color: #ffdf8a;
    font-weight: 700;
}

#fossil-depth, #fossil-hint { margin: 0.15rem 0 0; }
#fossil-depth { font-variant-numeric: tabular-nums; }
#fossil-hint { color: #dce9ba; }
#fossil-skeleton {
    display: block;
    width: 7.5rem;
    height: auto;
    margin-block-start: 0.2rem;
}
#fossil-skeleton[hidden] { display: none; }

.challenge-hud[data-mode="grading"] { border-color: #7ddbe7; }
[data-mode="grading"] #challenge-status { color: #9ce9f0; }
[data-mode="grading"] #challenge-restart { border-color: #7ddbe7; color: #9ce9f0; }
#grading-progress { font-variant-numeric: tabular-nums; }
#grading-progress p { margin: 0.15rem 0 0; }
#grading-timer { font-weight: 650; }
#grading-timer.low-time { color: #ffb0a2; }
#grading-best { color: #dce9ba; }

.grading-points {
    display: flex;
    flex-wrap: wrap;
    gap: 0.15rem 0.6rem;
    margin: 0.2rem 0 0;
    padding: 0;
    list-style: none;
    font-size: 0.75rem;
}

.grading-points li { color: #ffd181; }
.grading-points li[data-status="fill"] { color: #a9ddff; }
.grading-points li[data-status="match"] { color: #a8edac; }

.error {
    position: absolute;
    inset: auto 1rem 1rem;
    padding: 1rem;
    border-radius: 0.5rem;
    background: #14231b;
}

.controls {
    display: grid;
    gap: 1.15rem;
    padding: 1.1rem;
    background: #223528;
}

.lever-panel {
    display: grid;
    gap: 0.75rem;
    padding: 0.75rem;
    background: #223528;
}

.lever-setting {
    margin: 0;
    font-size: 0.875rem;
}

.lever-bank {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
}

.lever {
    display: grid;
    justify-items: center;
    gap: 0.35rem;
}

.lever-stick { display: contents; }

.drive-controls { display: none; }
.drive-controls[hidden] { display: none; }

.drive-controls h2 { margin: 0; }

.drive-buttons {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.35rem;
}

.drive-buttons button {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: 0.15rem;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    font-size: 0.875rem;
    overflow-wrap: anywhere;
}

.drive-buttons button[data-active] {
    background: #f7ba4c;
    border-color: #f7ba4c;
    color: #18281d;
}

.lever h2 { margin: 0; }

.lever-pad {
    position: relative;
    display: grid;
    place-items: center;
    width: min(100%, 6.5rem);
    aspect-ratio: 1;
    border: 1px solid #56705b;
    border-radius: 50%;
    background:
        linear-gradient(#405340, #405340) center / 1px 100% no-repeat,
        linear-gradient(#405340, #405340) center / 100% 1px no-repeat,
        #18281d;
    cursor: grab;
    touch-action: none;
    user-select: none;
}

.lever-pad[data-active] { cursor: grabbing; }

.lever-pad span {
    width: 40%;
    aspect-ratio: 1;
    border: 2px solid #dce9ba;
    border-radius: 50%;
    background: #56705b;
    pointer-events: none;
    transform: translate(var(--lever-x, 0px), var(--lever-y, 0px));
}

.lever-pad[data-active] span { border-color: #f7ba4c; }

.lever-guide {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.2rem 0.5rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 0.75rem;
    color: #dce9ba;
}

h2 {
    margin: 0 0 0.65rem;
    font-size: 1rem;
}

#keyboard-guide {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 0.35rem 0.6rem;
    margin: 0;
    font-size: 0.875rem;
}

#keyboard-guide dd { margin: 0; }

kbd {
    display: inline-block;
    min-width: 1.55rem;
    padding: 0.1rem 0.3rem;
    border: 1px solid #56705b;
    border-radius: 0.3rem;
    background: #18281d;
    text-align: center;
}

output {
    font-size: 1.25rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

meter {
    display: block;
    width: 100%;
    height: 0.8rem;
    margin-block-start: 0.4rem;
    accent-color: #f7ba4c;
}

.hint {
    margin: 0.75rem 0 0;
    color: #dce9ba;
    font-size: 0.875rem;
}

.bucket-setting {
    margin-block-start: 0.75rem;
}

.bucket-setting,
.graphics {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.55rem;
    font-size: 0.875rem;
}

.graphics h2 {
    grid-column: 1 / -1;
    margin: 0;
}

button, select {
    padding: 0.4rem 0.65rem;
    border: 1px solid #56705b;
    border-radius: 0.3rem;
    background: #18281d;
    color: inherit;
    font: inherit;
}

button { cursor: pointer; }
button:hover { background: #304734; }

@media (max-width: 50rem), (orientation: portrait) {
    .game-layout { grid-template-columns: minmax(0, 1fr); }
    .viewport { height: clamp(16rem, 55svh, 44rem); }
    #keyboard-guide { grid-template-columns: repeat(2, auto minmax(0, 1fr)); }
}

@media (max-width: 50rem), (pointer: coarse), (any-pointer: coarse) {
    .drive-controls { display: grid; gap: 0.35rem; }
    .lever-guide {
        overflow-wrap: anywhere;
    }

    button, select, .header-actions a {
        min-height: 44px;
    }

    select { font-size: 1rem; }
    .header-actions a { display: inline-grid; align-items: center; }
}

@media (max-width: 50rem), (pointer: coarse) {
    .playfield {
        display: grid;
        gap: 0.375rem;
    }

    .challenge-hud {
        position: static;
        max-width: none;
        padding: 0.25rem 0.5rem;
        overflow-wrap: anywhere;
    }

    .challenge-hud button { max-width: 100%; }

    .challenge-hud:not([hidden]) { display: flow-root; }
    .challenge-heading { display: contents; }

    .challenge-heading button {
        float: right;
        margin-inline-start: 0.5rem;
    }

    .grading-points { clear: both; }
}

@media (max-width: 50rem) {
    main { padding: 0.5rem; }
    .game-header { gap: 0.25rem 0.75rem; margin-block-end: 0.375rem; }
    .header-actions { gap: 0.25rem 0.5rem; }
    .header-actions select { max-width: 100%; padding-inline: 0.35rem; }
    .workspace { gap: 0.375rem; }

    /* Leave room for the header, challenge progress, and both touch controls. */
    .viewport { height: clamp(9rem, min(34svh, calc(100svh - 26.5rem)), 24rem); }

    .lever-panel {
        gap: 0.45rem;
        padding: 0.375rem 0.5rem;
    }

    .lever-setting { display: none; }
    .drive-controls { gap: 0.2rem; }
    .drive-controls h2, .lever h2 { font-size: 0.875rem; }

    .lever {
        grid-template-rows: auto auto 1fr;
        gap: 0.25rem;
    }

    .lever-pad { width: min(100%, clamp(4.5rem, 22vw, 6.25rem)); }

    .lever-guide {
        grid-template-columns: minmax(0, 1fr);
        justify-self: start;
        padding-inline: 0.25rem;
    }
}

@media (pointer: coarse) {
    #keyboard-guide { display: none; }
}

@media (orientation: landscape) and (max-height: 32rem) and (pointer: coarse) {
    main { padding: 0.5rem; }
    .game-header { margin-block-end: 0.375rem; }
    .game-layout { grid-template-columns: minmax(0, 1fr); }

    .workspace {
        --lever-column: clamp(4.25rem, 13vw, 8rem);
        grid-template-columns: var(--lever-column) repeat(2, minmax(0, 1fr)) var(--lever-column);
        grid-template-rows: minmax(max(15rem, calc(100svh - 4.125rem)), auto) auto;
        gap: 0.375rem;
    }

    .playfield {
        grid-area: 1 / 2 / 2 / 4;
        grid-template-rows: auto minmax(9rem, 1fr);
    }

    .viewport {
        grid-row: 2;
        height: auto;
    }

    .playfield:has(.challenge-hud[hidden]) { row-gap: 0; }

    .viewport > canvas { position: absolute; inset: 0; }

    .lever-panel {
        grid-area: 1 / 1 / -1 / -1;
        grid-template: subgrid / subgrid;
        gap: inherit;
        padding: 0;
        border: 0;
        background: none;
        pointer-events: none;
    }

    .lever-setting { display: none; }
    .lever-bank { display: contents; }
    .lever:first-child { grid-area: 1 / 1 / -1 / 3; }
    .lever:last-child { grid-area: 1 / 3 / -1 / -1; }

    .lever,
    .lever:last-child {
        grid-template: subgrid / subgrid;
        gap: normal;
    }

    .lever-stick {
        display: grid;
        grid-area: 1 / 1;
        align-self: start;
        justify-self: stretch;
        justify-items: center;
        gap: 0.35rem;
        margin-block-start: clamp(0.75rem, 9svh, 3rem);
    }

    .lever:last-child .lever-stick {
        grid-column: 2;
    }

    .lever h2 {
        font-size: 0.75rem;
        text-align: center;
    }

    .lever-pad {
        width: min(100%, 24svh, 6.5rem);
        pointer-events: auto;
    }

    .lever-guide {
        grid-area: 2 / 1 / 3 / -1;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        justify-self: center;
        font-size: 0.6875rem;
    }

    .drive-controls {
        grid-area: 1 / 1 / 2 / -1;
        grid-template-columns: subgrid;
        align-self: end;
        column-gap: inherit;
    }

    .drive-controls h2 {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    .drive-buttons {
        display: grid;
        grid-column: 1 / -1;
        grid-template-columns: subgrid;
        column-gap: inherit;
        row-gap: 0.25rem;
    }

    .drive-buttons button {
        font-size: 0.75rem;
        pointer-events: auto;
    }

    .drive-buttons [data-drive-value="1"] { grid-column: 1; }
    .drive-buttons [data-drive-value="-1"] { grid-column: 4; }
    .drive-buttons [data-drive-axis="travel"] { grid-row: 1; }
    .drive-buttons [data-drive-axis="turn"] { grid-row: 2; }
}
