: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;
}

.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;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
}

.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) {
    .game-header { margin-block-end: 0.5rem; }
    .viewport { height: clamp(9rem, min(42svh, calc(100svh - 24rem)), 30rem); }
    .lever-panel {
        gap: 0.4rem;
        padding: 0.5rem;
    }
    .lever {
        grid-template-rows: auto auto 1fr;
        justify-items: start;
    }

    .lever:last-child { justify-items: end; }
}

@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: max(15rem, calc(100svh - 4.125rem)) auto;
        gap: 0.375rem;
    }

    .viewport {
        grid-area: 1 / 2 / 2 / 4;
        height: auto;
    }

    .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;
        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; }
}
