.pgn-viewer-layout {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 1.5rem;
    width: 100%;
}

.pgn-viewer-spacer {
    flex: 1 1 0%;
    max-width: 15rem;
}

.pgn-viewer-board-col {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.pgn-viewer-board-row .board-container {
    display: block;
}

.pgn-viewer-board-row .board-container .board {
    margin-bottom: 0;
}

.pgn-viewer-sidebar {
    flex: 1 1 0%;
    max-width: 36rem;
    min-width: 25.5rem;
    height: var(--board-size-px, 640px);
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    align-self: flex-start;
}

.pgn-viewer-error {
    color: var(--alert-error-text, #9b2f2f);
    background: var(--alert-error-bg, rgba(155, 47, 47, 0.08));
    border: 1px solid var(--alert-error-bd, rgba(155, 47, 47, 0.25));
    border-radius: 0.4rem;
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
}

/* Narrow but not a phone lying down — a short landscape viewport is handled below and must not
   stack. */
@media (max-width: 768px) and (min-height: 621px) {
    .pgn-viewer-layout {
        flex-direction: column;
        align-items: center;
    }

    .pgn-viewer-spacer {
        display: none;
    }

    .pgn-viewer-sidebar {
        flex: 0 0 auto;
        width: 100%;
        max-width: 480px;
        padding-top: 0;
    }
}

/* ── Phone portrait ───────────────────────────────────────────
   Board over moves, full bleed. Landscape is deliberately *not* here: turning the phone gives the
   desktop layout unchanged, and the block further down only resizes the board to fit the height.
   Keyed on the viewport rather than on `pointer: coarse` so a narrow desktop window lands in the
   same layout. */
@media (max-width: 900px) and (orientation: portrait) {
    .pgn-viewer-layout {
        flex-direction: column;
        align-items: stretch;
        gap: 0.4rem;
        --board-size-override: 100vw;
    }

    .pgn-viewer-spacer {
        display: none;
    }

    .pgn-viewer-board-col {
        align-items: center;
    }

    .pgn-viewer-error {
        margin-inline: 0.75rem;
    }

    /* Whatever the full-bleed board leaves over: the board is 100vw tall, and 11rem covers the
       topbar, the collapsed input and the page's own padding. The panel is already a flex column
       in scrollable mode, so header and controls take their natural height and the move tree
       absorbs the rest — no need to guess how tall a two-line game title is. */
    .pgn-viewer-sidebar {
        flex: 0 1 auto;
        align-self: stretch;
        width: auto;
        max-width: none;
        min-width: 0;
        height: calc(100vh - 100vw - 11rem);
        height: calc(100dvh - 100vw - 11rem);
        min-height: 12rem;
        padding-inline: 0.75rem;
        box-sizing: border-box;
    }

    /* Navigation sits directly under the board, where the thumb is, with the game title kept above
       it. NavigatorPanel has its own `pointer: coarse` version of this; both are needed, because
       this layout is keyed on the viewport and also runs on a narrow desktop window. */
    .pgn-viewer-sidebar .navigator-card-header {
        order: -2;
    }

    .pgn-viewer-sidebar .navigator-arrows {
        order: -1;
        border-bottom: 1px solid var(--border);
    }

    /* The desktop floor would push the panel past the sidebar it now has to fit inside. */
    .pgn-viewer-layout .pgn-viewer-sidebar .moves-tree {
        min-height: 0;
    }
}

/* ── Phone landscape ──────────────────────────────────────────
   The desktop layout as-is — spacer, board, panel beside it, input stretched across both by
   pgnReaderLayout.sync. Only what physically cannot fit is touched: the board is sized off the
   viewport height, and the two desktop minimums that assume a 640px board are relaxed so the
   split can compress instead of overflowing. */
@media (max-width: 1024px) and (max-height: 620px) and (orientation: landscape) {
    .pgn-viewer-layout {
        --board-size-override: min(calc(100vw - 21rem), calc(100vh - 8.25rem));
        --board-size-override: min(calc(100vw - 21rem), calc(100dvh - 8.25rem));
    }

    .pgn-viewer-spacer {
        max-width: 6rem;
    }

    .pgn-viewer-sidebar {
        min-width: 0;
    }

    /* Same as portrait: the desktop floor is taller than the panel gets here, and the overflow
       lands under the controls. Desktop type and spacing also read oversized next to a board a
       third of its usual size, so the whole panel scales down with it. */
    .pgn-viewer-layout .pgn-viewer-sidebar .moves-tree {
        min-height: 0;
        padding: 0.3rem 0.45rem;
        font-size: var(--ls-text);
        line-height: var(--ls-line);
    }

    .pgn-viewer-sidebar .navigator-card-header {
        min-height: auto;
        padding: 0.2rem 0.55rem;
    }

    .pgn-viewer-sidebar .navigator-card-title {
        font-size: var(--ls-text);
    }

    .pgn-viewer-sidebar .navigator-arrows {
        min-height: auto;
        padding: 0.25rem 0.45rem 0.35rem;
    }

    .pgn-viewer-layout .pgn-viewer-sidebar .game-control-btn {
        width: var(--ls-btn);
        height: var(--ls-btn);
        min-width: var(--ls-btn);
        min-height: var(--ls-btn);
        font-size: 0.9rem;
    }

    .pgn-viewer-layout .pgn-viewer-sidebar .game-control-btn .oi {
        font-size: var(--ls-glyph);
    }
}
