/*
 * Chess Diagrams - inline board diagrams inside a post body.
 * Uses the site design tokens from base.html so diagrams follow the theme.
 */

.chess-diagram {
    margin: var(--space-6) auto;
    padding: var(--space-4);
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    border-radius: 16px;
    box-shadow: var(--shadow-md);
}

/* A lone position needs no more room than the board itself. */
.chess-diagram.is-static {
    max-width: 460px;
}

/* A game also carries a scoresheet, so it may run wider. */
.chess-diagram.is-replay {
    max-width: 700px;
}

.chess-diagram:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 3px;
}

.chess-diagram-board {
    /* chessboard.js sizes the squares from this width and then draws a 2px
       border around them, so leave those 4px free or the card scrolls. */
    width: calc(100% - 4px);
    max-width: 400px;
    margin: 0 auto;
}

.chess-diagram-caption {
    margin: var(--space-3) 0 0;
    text-align: center;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-secondary);
    line-height: 1.4;
}

/* Layout: board and scoresheet side by side once there is room. */

.chess-diagram-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-4);
}

.chess-diagram-main {
    min-width: 0;
}

.chess-diagram-side {
    min-width: 0;
    display: flex;
}

@media (min-width: 700px) {
    .chess-diagram.is-replay .chess-diagram-layout {
        grid-template-columns: minmax(0, 1fr) 200px;
        align-items: start;
    }

    /* Match the scoresheet to the board so the pair reads as one block. */
    .chess-diagram.is-replay .chess-diagram-side {
        max-height: 400px;
    }
}

/* Controls */

.chess-diagram-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1);
    margin-top: var(--space-3);
}

/* One row that never wraps: five buttons shrink together rather than
   staircasing when the diagram is narrow. */
.chess-diagram-buttons {
    display: flex;
    justify-content: center;
    gap: var(--space-1);
    flex-wrap: nowrap;
    width: 100%;
}

.chess-diagram-button {
    flex: 0 1 auto;
    min-width: 0;
    padding: var(--space-1) var(--space-2);
    font-size: var(--text-base);
    line-height: 1.2;
    color: #ffffff;
    background: var(--accent-strong);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: all var(--duration-normal) var(--ease-out);
}

.chess-diagram-button:hover:not(:disabled) {
    background: #065f46;
    transform: translateY(-1px);
}

.chess-diagram-button:disabled {
    opacity: 0.4;
    cursor: default;
}

.chess-diagram-counter {
    font-size: var(--text-sm);
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
}

/* Scoresheet */

.chess-diagram-moves {
    flex: 1 1 auto;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    list-style: none;
    font-size: var(--text-sm);
    scrollbar-width: thin;
    scrollbar-color: var(--border-medium) transparent;
}

.chess-diagram-moves::-webkit-scrollbar {
    width: 8px;
}

.chess-diagram-moves::-webkit-scrollbar-track {
    background: transparent;
}

.chess-diagram-moves::-webkit-scrollbar-thumb {
    background: var(--border-medium);
    border-radius: 4px;
}

.chess-diagram-move-row {
    display: grid;
    grid-template-columns: 2.2em 1fr 1fr;
    align-items: baseline;
    gap: var(--space-1);
}

.chess-diagram-move-row:nth-child(odd) {
    background: var(--border-light);
}

.chess-diagram-move-number {
    padding-left: 4px;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.chess-diagram-move {
    padding: 3px 6px;
    font-family: var(--font-body);
    font-size: var(--text-sm);
    text-align: left;
    color: var(--text-primary);
    background: none;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.chess-diagram-move:hover {
    background: var(--accent-strong);
    color: #ffffff;
}

.chess-diagram-move.is-current {
    color: #ffffff;
    background: var(--accent-strong);
    font-weight: 700;
}

/* Parse failures, shown to the author instead of an empty gap */

.chess-diagram-error {
    margin: var(--space-4) auto;
    padding: var(--space-3) var(--space-4);
    max-width: 600px;
    border-left: 4px solid #dc2626;
    border-radius: 8px;
    background: rgba(220, 38, 38, 0.08);
    color: var(--text-primary);
    font-size: var(--text-sm);
    line-height: 1.5;
}

.chess-diagram-error code {
    display: block;
    margin-top: var(--space-2);
    word-break: break-all;
    font-size: var(--text-sm);
    opacity: 0.8;
}

@media (max-width: 768px) {
    /* Almost no side padding, so the board itself gets the room. */
    .chess-diagram {
        padding: var(--space-3) 4px;
    }

    .chess-diagram-caption,
    .chess-diagram-controls {
        padding-left: var(--space-2);
        padding-right: var(--space-2);
    }

    .chess-diagram-button {
        padding: var(--space-1);
        font-size: var(--text-sm);
    }

    /* Stacked under the board, so cap the height instead of matching it. */
    .chess-diagram-side {
        max-height: 11em;
        padding-left: var(--space-2);
        padding-right: var(--space-2);
    }
}
