.chessconsole {
    /* stolen from lichess.org */
    --chess-light: #dee3e6;
    --chess-dark: #8ca2ad;
    --chessconsole-history-width: 12.5em;
    --chessconsole-comments-width: 12.5em;
    --chessconsole-board-width: 28em;
    /* Query the outer shell (always definite width). Do not put
       container-type on a fit-content frame — inline-size containment
       collapses that frame to 0 when there is no comments panel. */
    container-type: inline-size;
    container-name: chessconsole;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6em;
    margin: 1.2em auto;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    min-width: 0;
}

/* Frame wraps meta + board row so they share one width.
   Layout is mobile-first (stacked); row rules live in the min-width query.
   has-comments is owned by JS (.chessconsole-has-comments), not :has(). */
.chessconsole-frame {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.6em;
    box-sizing: border-box;
    min-width: 0;
    width: 100%;
    max-width: 100%;
}

.chessconsole-has-comments .chessconsole-frame {
    max-width: min(56em, 100%);
}

.chessconsole-body {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    gap: 0.6em;
    width: 100%;
    min-width: 0;
}

.chessconsole-main {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.6em;
    flex: 0 1 auto;
    order: 1;
    width: 100%;
    min-width: 0;
    max-width: 100%;
}

.chessconsole-history,
.chessconsole-comments {
    flex: 0 0 auto;
    align-self: stretch;
    width: 100%;
    max-width: none;
    min-width: 0;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-sizing: border-box;
    border: 0.4em ridge;
    border-radius: 0.2em;
    background: GhostWhite;
    scrollbar-width: thin;
}

.chessconsole-history {
    order: 2;
    max-height: 12em;
    padding: 0.4em 0.5em;
    font-family: var(--mono-font, monospace);
    font-size: 0.9em;
    line-height: 1.65;
}

.chessconsole-comments {
    order: 3;
    max-height: 10em;
    padding: 0.55em 0.65em;
    color: Black;
    font-family: var(--sans-font, sans-serif);
    font-size: 0.9em;
    line-height: 1.45;
}

/* Wide console: history | board [| comments]. JS syncs side max-height to the board. */
@container chessconsole (min-width: 36.01em) {
    .chessconsole-frame {
        width: max-content;
        max-width: 100%;
    }

    .chessconsole-has-comments .chessconsole-frame {
        width: 100%;
        max-width: min(56em, 100%);
    }

    .chessconsole-body {
        flex-direction: row;
        align-items: flex-start;
    }

    .chessconsole-main {
        order: 0;
        flex: 0 1 auto;
        width: var(--chessconsole-board-width);
        max-width: 100%;
    }

    .chessconsole-has-comments .chessconsole-main {
        flex: 1 1 var(--chessconsole-board-width);
        width: auto;
        max-width: min(var(--chessconsole-board-width), 100%);
    }

    .chessconsole-history {
        order: 0;
        flex: 0 0 auto;
        align-self: flex-start;
        width: max-content;
        max-height: none;
    }

    .chessconsole-comments {
        order: 0;
        flex: 0 0 var(--chessconsole-comments-width);
        align-self: flex-start;
        width: var(--chessconsole-comments-width);
        max-width: 100%;
        max-height: none;
    }

    .chessconsole-has-comments .chessconsole-comments {
        flex: 1 1 0;
        width: auto;
    }

    .chessconsole-has-comments .chessconsole-history {
        flex: 0 0 auto;
        width: max-content;
        min-width: var(--chessconsole-history-width);
    }
}

.chessconsole-history-table {
    width: max-content;
    min-width: 100%;
    margin: 0;
    border: none;
    border-collapse: collapse;
    border-spacing: 0;
}

.chessconsole-history-table td {
    border: none;
    padding: 0.2em 0.35em;
    white-space: nowrap;
}

.chessconsole-history-table .num {
    width: auto;
    padding-left: 0.15em;
    padding-right: 0.35em;
    color: SlateGray;
    font-variant-numeric: tabular-nums;
    vertical-align: baseline;
    text-align: right;
}

.chessconsole-history-table .ply {
    width: auto;
    padding-left: 0.3em;
    padding-right: 0.4em;
    vertical-align: baseline;
    cursor: pointer;
    border-radius: 0.15em;
    color: Black;
    transition: background-color 150ms ease, font-weight 150ms ease;
}

.chessconsole-history-table .ply:hover {
    background: Gainsboro;
    color: Navy;
}

.chessconsole-history-table .ply.is-active {
    font-weight: 700;
    background: Gainsboro;
    color: Navy;
}

.chessconsole-history-table .ply.is-future {
    color: DarkGray;
}

.chessconsole-history-table .ply-nag {
    display: inline-block;
    min-width: 1.2em;
    height: 1.2em;
    line-height: 1.2em;
    margin-left: 0.35em;
    padding: 0 0.2em;
    border-radius: 999px;
    text-align: center;
    color: #fff;
    font-family: var(--mono-font, monospace);
    font-weight: 700;
    font-style: normal;
    font-size: 0.8em;
}

.chessconsole-history-table .ply-fig {
    font-family: var(--sans-font, sans-serif);
    font-style: normal;
    color: inherit;
    margin-right: 0.1em;
}

.chessconsole-comments-header {
    display: flex;
    align-items: baseline;
    gap: 0.3em;
    margin-bottom: 0.45em;
}

.chessconsole-comments-ply {
    margin: 0;
    color: Navy;
    font-family: var(--mono-font, monospace);
    font-size: 0.95em;
    font-weight: 600;
}

.chessconsole-comments-nag {
    display: inline-block;
    min-width: 1.4em;
    height: 1.4em;
    line-height: 1.4em;
    padding: 0 0.2em;
    border-radius: 999px;
    text-align: center;
    color: #fff;
    font-family: var(--mono-font, monospace);
    font-weight: 700;
    font-style: normal;
    font-size: 0.95em;
}

.chessconsole-comments-list {
    display: flex;
    flex-direction: column;
    gap: 0.55em;
    margin: 0;
    padding: 0;
    list-style: none;
}

.chessconsole-comments-item {
    margin: 0;
    color: Black;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.chessconsole-comments-san {
    display: inline;
    color: Navy;
    font-family: var(--mono-font, monospace);
    font-weight: 600;
    white-space: nowrap;
}

.chessconsole-comments-header .ply-fig {
    font-family: var(--sans-font, sans-serif);
    font-style: normal;
    color: Navy;
    font-weight: 400;
    margin-right: 0.1em;
}

.chessconsole-comments-san .ply-fig {
    font-family: var(--sans-font, sans-serif);
    font-style: normal;
    font-weight: 400;
    margin-right: 0.1em;
}

.chessconsole-comments-empty {
    margin: 0;
    color: DarkGray;
    font-style: italic;
}

.chessconsole-meta {
    display: flex;
    flex-direction: column;
    gap: 0.15em;
    width: 100%;
    box-sizing: border-box;
    padding: 0.55em 0.75em;
    border: 0.3em double LightSlateGray;
    border-radius: 0.2em;
    background: GhostWhite;
    /* box-shadow: 0.04em 0.04em 0.1em LightSlateGray; */
    color: Black;
    font-size: 0.92em;
    line-height: 1.35;
}

.chessconsole-meta-players {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5em;
    font-weight: 600;
    color: Navy;
}

.chessconsole-meta-player {
    display: flex;
    align-items: center;
    gap: 0.35em;
    min-width: 0;
    flex: 1;
}

.chessconsole-meta-player-white {
    justify-content: flex-start;
    text-align: left;
}

.chessconsole-meta-player-black {
    justify-content: flex-end;
    text-align: right;
}

.chessconsole-meta-name {
    min-width: 0;
    overflow-x: auto;
    white-space: nowrap;
    scrollbar-width: thin;
}

.chessconsole-meta-elo {
    flex-shrink: 0;
    font-weight: 500;
    color: SlateGray;
    font-variant-numeric: tabular-nums;
}

.chessconsole-meta-swatch {
    flex-shrink: 0;
    width: 0.85em;
    height: 0.85em;
    border: 1px solid SlateGray;
}

.chessconsole-meta-swatch-white {
    background: White;
}

.chessconsole-meta-swatch-black {
    background: Black;
}

.chessconsole-meta-result {
    flex-shrink: 0;
    font-weight: bold;
    color: Black;
    font-variant-numeric: tabular-nums;
}

.chessconsole-meta-event,
.chessconsole-meta-opening,
.chessconsole-meta-termination,
.chessconsole-meta-link {
    color: SlateGray;
    font-size: 0.95em;
}

.chessconsole-meta-termination {
    text-align: center;
}

.chessconsole-meta-link {
    overflow-wrap: anywhere;
}

.chessconsole-meta-opening-name {
    font-weight: 500;
}

.chessconsole-meta-eco {
    font-weight: normal;
}

.chessconsole-board {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
    border: 0.4em ridge;
    border-radius: 0.2em;
    background: GhostWhite;
}

/* Alice's global `svg { width/height: 1em; fill: none }` is for icons.
   Reset it so cm-chessboard can render at full size with piece fills. */
.chessconsole-board svg.cm-chessboard {
    width: 100%;
    height: auto;
    max-width: 100%;
    aspect-ratio: 1;
    display: block;
    stroke: none;
    stroke-width: 0;
    fill: unset;
    /* An edge-line NAG icon is anchored at the destination square's top-right corner;
       its top/right may overhang the drawing area. Allow it to render unclipped. */
    overflow: visible;
}

.chessconsole-board svg.cm-chessboard use.piece {
    fill: unset;
}

/* Rendered by chessconsole.js into the markers layer (under the pieces): highlight the
   current move "from" and "to" squares. Light green tint + alpha so it reads like a typical chess GUI
   without overpowering the board. */
.chessconsole .cm-chessboard .markers-layer .chessconsole-square-to {
    fill: rgba(154, 205, 50, 0.32);
}

.chessconsole .cm-chessboard .markers-layer .chessconsole-square-from {
    fill: rgba(154, 205, 50, 0.16);
}

/* Quality NAG icon overlaid on the destination square (top-right corner).
   Single cached <image> rendered by chessconsole.js into the markers-top-layer. */
.chessconsole .cm-chessboard .chessconsole-nag-icon {
    pointer-events: none;
    user-select: none;
    filter: drop-shadow(0 0.04em 0.08em rgba(0, 0, 0, 0.4));
}

.chessconsole .cm-chessboard.default .board .square.white {
    fill: var(--chess-light);
}

.chessconsole .cm-chessboard.default .board .square.black {
    fill: var(--chess-dark);
}

.chessconsole .cm-chessboard.default.border-type-none .board .border,
.chessconsole .cm-chessboard.default.border-type-thin .board .border {
    fill: var(--chess-dark);
    stroke: var(--chess-dark);
}

.chessconsole .cm-chessboard.default .coordinates .coordinate.white {
    fill: var(--chess-dark);
}

.chessconsole .cm-chessboard.default .coordinates .coordinate.black {
    fill: var(--chess-light);
}

.chessconsole-controls {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    width: 100%;
    gap: 0.55em;
    margin-block: 0.15em;
    padding-block: 0.25em;
    padding-inline: 0.15em;
    overflow-x: auto;
    scrollbar-width: thin;
}

.chessconsole-btn {
    --icon: none;
    box-sizing: border-box;
    display: inline-flex;
    flex: 0 0 2.75em;
    align-items: center;
    justify-content: center;
    width: 2.75em;
    min-width: 2.75em;
    aspect-ratio: 1;
    height: 2.75em;
    padding: 0;
    border: 0.2em outset Gainsboro;
    border-radius: 0.2em;
    background: Snow;
    color: var(--link-color);
    cursor: pointer;
    transition: color 150ms ease;
}

.chessconsole-btn:active:not(:disabled),
.chessconsole-btn-autoplay.is-playing:not(:disabled) {
    border-style: inset;
}

.chessconsole-btn::before {
    content: "";
    display: block;
    width: 56%;
    height: 56%;
    background-color: currentColor;
    mask: var(--icon) center / contain no-repeat;
    -webkit-mask: var(--icon) center / contain no-repeat;
}

.chessconsole-btn-first {
    --icon: url("../svg/ui-oval/skip-backward.svg");
}

.chessconsole-btn-back {
    --icon: url("../svg/ui-oval/fast-backward.svg");
}

.chessconsole-btn-forward {
    --icon: url("../svg/ui-oval/fast-forward.svg");
}

.chessconsole-btn-last {
    --icon: url("../svg/ui-oval/skip-forward.svg");
}

.chessconsole-btn-autoplay {
    --icon: url("../svg/ui-oval/play.svg");
}

.chessconsole-btn-autoplay.is-playing {
    --icon: url("../svg/ui-oval/stop.svg");
}

.chessconsole-btn-flip {
    --icon: url("../svg/ui-oval/switch-vertical-cw.svg");
}

.chessconsole-btn:hover:not(:disabled) {
    color: var(--link-active-color);
}

.chessconsole-btn:disabled {
    color: Silver;
    cursor: not-allowed;
}

.chessconsole-pgn {
    display: none;
}

.chessconsole-error {
    color: FireBrick;
    border: 1px solid FireBrick;
    border-radius: 0.3em;
    padding: 0.6em 0.8em;
    background: GhostWhite;
}
