.game-page {
    --game-surface: var(--bg-elevated);
    --game-surface-2: var(--bg-sunken);
    --game-border: var(--border);
    --game-grid-line: var(--border);
    --game-text: var(--text);
    --game-text-dim: var(--text-muted);
    --game-accent: var(--accent);
    --game-accent-contrast: var(--accent-contrast);
    --game-danger: var(--danger);
    --game-info: var(--accent);
    --game-warn: var(--warning);
    --game-win: var(--success);

    display: grid;
    grid-template-columns: minmax(220px, 300px) minmax(0, 1fr) minmax(220px, 320px);
    gap: 16px;
    align-items: start;
    padding: 16px;
    color: var(--game-text);
}

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

.game-page__left,
.game-page__center,
.game-page__right {
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-width: 0;
}

@media (max-width: 900px) {
    .game-page {
        grid-template-columns: 1fr;
    }

    .game-page__right {
        order: 3;
    }

    /*
        The board never spans several screens: its width is derived from the viewport height left over by
        the chrome above/below it (top bar, HUD, actions), so a portrait 3×10 board stays one screen tall
        while a landscape 10×3 board stays width-capped at 100%.
    */
    .game-board {
        width: min(100%, calc((100vh - 15rem) * var(--cols) / var(--rows)));
        width: min(100%, calc((100dvh - 15rem) * var(--cols) / var(--rows)));
    }
}

/*
    Desktop fits the whole game screen into the viewport below the top bar: the page itself does not
    scroll, the side columns scroll internally, and the board is sized to the smaller of the centre
    column's width and the height left for it, so any board shape (3×10, 10×3, …) fits. The frame is a
    size container; `100cqh` is the height the centre column left for the board.
*/
@media (min-width: 901px) {
    .game-page {
        height: calc(100vh - var(--topbar-height));
        height: calc(100dvh - var(--topbar-height));
        align-items: stretch;
        overflow: hidden;
    }

    .game-page__left,
    .game-page__center,
    .game-page__right {
        min-height: 0;
        overflow-y: auto;
    }

    .game-page__right .game-chat {
        flex: 1 1 auto;
        min-height: 0;
        max-height: none;
    }

    .game-hud,
    .game-actions,
    .game-over {
        flex: none;
    }

    .game-board-frame {
        flex: 1 1 auto;
        min-height: 12rem;
        align-items: center;
        container-type: size;
    }

    .game-board {
        width: min(100%, calc(100cqh * var(--cols) / var(--rows)));
    }
}

.game-card {
    background: var(--game-surface);
    border: 1px solid var(--game-border);
    border-radius: var(--radius);
    padding: 12px 14px;
}

.game-title {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
}

.game-page__heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}

.game-page__number,
.game-panel__title {
    color: var(--game-text-dim);
}

.game-panel__title {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.game-empty,
.game-hint {
    margin: 0;
    color: var(--game-text-dim);
    font-size: 0.9rem;
}

.game-hint--info {
    color: var(--game-info);
}

.game-hint--error {
    color: var(--game-danger);
    font-weight: 600;
}

/* ---------------------------------------------------------------- HUD */

.game-hud {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.game-hud__turn {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 48px;
}

.game-hud__turn-text {
    font-size: 1.1rem;
    font-weight: 600;
}

.game-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex: 0 0 auto;
}

.game-hud__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 18px;
    color: var(--game-text-dim);
    font-size: 0.9rem;
}

/* ---------------------------------------------------------------- Board */

.game-board-frame {
    display: flex;
    justify-content: center;
}

.game-board {
    position: relative;
    width: 100%;
    max-width: min(92vw, 720px);
    aspect-ratio: var(--cols) / var(--rows);
    display: grid;
    grid-template-columns: repeat(var(--cols), 1fr);
    grid-template-rows: repeat(var(--rows), 1fr);
    container-type: inline-size;
    background: var(--game-surface-2);
    border: 1px solid var(--game-border);
    border-radius: var(--radius);
    overflow: hidden;

    --winline-thickness: 6px;
}

.game-board--inactive {
    opacity: 0.94;
}

.game-board__empty {
    color: var(--game-text-dim);
    text-align: center;
    padding: 40px 0;
}

.game-cell {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 0;
    margin: 0;
    padding: 0;
    background: transparent;
    border: 0;
    border-right: 1px solid var(--game-grid-line);
    border-bottom: 1px solid var(--game-grid-line);
    color: inherit;
    font: inherit;
    cursor: default;
}

.game-cell--legal {
    cursor: pointer;
}

.game-cell--legal:hover {
    background: color-mix(in srgb, var(--game-accent) 18%, transparent);
}

.game-cell--legal:focus-visible {
    outline: 2px solid var(--game-accent);
    outline-offset: -3px;
}

.game-mark {
    /* 58% of one cell, where a cell is 100cqw / columns wide (cells are square); the shared FigureMark
       SVG scales its 24×24 stroked outline to that square. */
    width: var(--mark);
    height: var(--mark);
    display: block;
    user-select: none;
}

.game-board__lines {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.game-board__line {
    position: absolute;
    height: var(--winline-thickness);
    background: var(--winline-color, var(--game-win));
    border-radius: 999px;
    transform-origin: 0 50%;
    opacity: 0.9;
}

/* ---------------------------------------------------------------- Players */

.game-players {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.game-players__list,
.game-spectators,
.game-standings {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.game-player {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--game-surface);
    border: 1px solid var(--game-border);
    border-radius: var(--radius-sm);
}

.game-player--turn {
    border-color: var(--game-accent);
    background: color-mix(in srgb, var(--game-accent) 14%, var(--game-surface));
}

.game-player__body {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.game-player__head {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.game-player__name,
.game-spectator__name,
.game-standing__name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.game-player__meta {
    display: flex;
    gap: 12px;
    color: var(--game-text-dim);
    font-size: 0.82rem;
}

.game-badges {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 4px;
}

.game-badge {
    padding: 1px 7px;
    border-radius: 8px;
    font-size: 0.68rem;
    font-weight: 700;
    line-height: 1.6;
    white-space: nowrap;
}

.game-badge--you {
    background: var(--game-surface-2);
    border: 1px solid var(--game-border);
    color: var(--game-text);
}

.game-badge--leader {
    background: var(--game-accent);
    color: var(--game-accent-contrast);
}

.game-badge--warn {
    background: transparent;
    border: 1px solid var(--game-warn);
    color: var(--game-warn);
}

.game-badge--seat {
    background: transparent;
    border: 1px solid var(--badge-color, var(--game-border));
    color: var(--badge-color, var(--game-text));
}

.game-figure {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: var(--radius-sm);
    background: var(--game-surface-2);
    border: 1px solid var(--game-border);
}

.game-figure__mark {
    display: block;
    width: 20px;
    height: 20px;
}

.game-figure--sm {
    width: 24px;
    height: 24px;
}

.game-figure--sm .game-figure__mark {
    width: 14px;
    height: 14px;
}

.game-avatar {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}

.game-avatar--sm {
    width: 26px;
    height: 26px;
    font-size: 0.68rem;
}

.game-spectator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 6px;
    color: var(--game-text-dim);
    font-size: 0.9rem;
}

/* ---------------------------------------------------------------- Actions */

.game-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.game-actions__buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.game-actions__confirm-title,
.game-actions__draw-title {
    margin: 0 0 6px;
    font-weight: 700;
}

.game-actions__confirm-text {
    margin: 0 0 8px;
}

.game-actions__draw p {
    margin: 0 0 4px;
}

.game-actions__expiry {
    color: var(--game-warn);
    font-weight: 600;
}

.game-actions__draw .game-actions__buttons {
    margin-top: 8px;
}

.game-actions__notice {
    margin: 0;
    color: var(--game-info);
    font-size: 0.9rem;
}

.game-actions__error {
    margin: 0;
    color: var(--game-danger);
    font-size: 0.9rem;
    font-weight: 600;
}

.game-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--game-border);
    background: var(--game-surface);
    color: var(--game-text);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.game-btn:hover {
    filter: brightness(1.08);
}

.game-btn--primary {
    background: var(--game-accent);
    border-color: var(--game-accent);
    color: var(--game-accent-contrast);
}

.game-btn--danger {
    background: var(--game-danger);
    border-color: var(--game-danger);
    color: #fff;
}

.game-btn--ghost {
    background: transparent;
}

/* ---------------------------------------------------------------- Game over */

.game-over {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.game-over__head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
}

.game-over__summary {
    min-width: 0;
}

.game-over__caption {
    margin: 0;
    color: var(--game-text-dim);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.game-over__outcome {
    margin: 2px 0 0;
    font-size: 1.15rem;
    font-weight: 700;
}

.game-over__reason,
.game-over__surrenders {
    margin: 4px 0 0;
    color: var(--game-text-dim);
    font-size: 0.9rem;
}

.game-over__standings summary {
    cursor: pointer;
    font-weight: 700;
    padding: 4px 0;
}

.game-standings {
    margin-top: 8px;
}

.game-standing {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border: 1px solid var(--game-border);
    border-radius: var(--radius-sm);
    background: var(--game-surface-2);
}

.game-standing--winner {
    border-color: var(--game-win);
    background: color-mix(in srgb, var(--game-win) 14%, var(--game-surface-2));
}

.game-standing__place,
.game-standing__points,
.game-standing__session {
    color: var(--game-text-dim);
    font-size: 0.85rem;
}

.game-standing__name {
    flex: 1 1 auto;
}

/* ---------------------------------------------------------------- Chat */

.game-chat {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 76vh;
}

.game-chat__viewport {
    flex: 1 1 auto;
    min-height: 180px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
    background: var(--game-surface-2);
    border-radius: var(--radius-sm);
}

.game-chat__bubble {
    align-self: flex-start;
    max-width: 88%;
    padding: 7px 10px;
    border-radius: var(--radius-sm);
    background: var(--game-surface);
    border: 1px solid var(--game-border);
}

.game-chat__bubble--self {
    align-self: flex-end;
    background: color-mix(in srgb, var(--game-accent) 16%, var(--game-surface));
    border-color: color-mix(in srgb, var(--game-accent) 40%, var(--game-border));
}

.game-chat__meta {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-bottom: 2px;
}

.game-chat__sender {
    font-size: 0.78rem;
    font-weight: 700;
}

.game-chat__time {
    color: var(--game-text-dim);
    font-size: 0.7rem;
}

.game-chat__text {
    display: block;
    overflow-wrap: anywhere;
}

.game-chat__system {
    margin: 0;
    align-self: center;
    color: var(--game-text-dim);
    font-size: 0.8rem;
    text-align: center;
}

.game-chat__new {
    align-self: center;
    padding: 4px 12px;
    border-radius: 999px;
    border: 1px solid var(--game-accent);
    background: var(--game-accent);
    color: var(--game-accent-contrast);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
}

.game-chat__error {
    margin: 0;
    color: var(--game-danger);
    font-size: 0.85rem;
    font-weight: 600;
}

.game-chat__input {
    display: flex;
    gap: 8px;
}

.game-chat__field {
    flex: 1 1 auto;
    min-width: 0;
    padding: 7px 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--bg);
    color: var(--game-text);
    font: inherit;
}

.game-chat__field:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 0;
}

/* ---------------------------------------------------------------- Motion */

/*
    Purely decorative state transitions: a placed mark pops in, an evicted/dropped mark (which the board
    keeps rendered for a moment, see GameBoard.razor) fades out, a completed-line strike-through draws
    itself, and the game-over panel slides in. The same states render identically without any of it,
    which is exactly what reduced-motion users get.
*/
@media (prefers-reduced-motion: no-preference) {
    .game-mark {
        transform-box: view-box;
        transform-origin: center;
        animation: game-mark-in 0.16s ease-out;
    }

    .game-mark--leaving {
        animation: game-mark-out 0.18s ease-in forwards;
    }

    .game-board__line {
        animation: game-line-in 0.28s ease-out;
    }

    .game-cell--legal {
        transition: background-color 0.12s ease;
    }

    .game-cell--legal:active {
        background: color-mix(in srgb, var(--game-accent) 32%, transparent);
    }

    .game-over {
        animation: game-over-in 0.22s ease-out;
    }
}

@keyframes game-mark-in {
    from {
        opacity: 0;
        transform: scale(0.4);
    }

    70% {
        transform: scale(1.08);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

@keyframes game-mark-out {
    from {
        opacity: 1;
        transform: none;
    }

    to {
        opacity: 0;
        transform: scale(0.5);
    }
}

@keyframes game-line-in {
    from {
        clip-path: inset(0 100% 0 0);
    }

    to {
        clip-path: inset(0 0 0 0);
    }
}

@keyframes game-over-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .game-mark,
    .game-mark--leaving,
    .game-board__line,
    .game-over {
        animation: none;
    }
}
