/* ─────────────────────────────────────────────────────────────────────────────
   chesswick-diagram.css
   Styles for the Chesswick Visual Notation widget (board + sequence types).
   Loaded after chesswick.css so it can reuse the design-system custom props.
   Mobile-first: legible at 320px wide; expands to room available.
   ───────────────────────────────────────────────────────────────────────────── */

/* ── Semantic palette (light theme) ───────────────────────────────────────── */
:root {
    /* Each semantic role gets an accent + a translucent surface variant.
       Hues chosen so good/bad differ in lightness as well as hue (color-blind
       friendly), and focus stands distinctly apart from the other four. */
    --cw-good:           #2f8a4e;
    --cw-good-soft:      rgba(47, 138, 78, 0.28);
    --cw-good-line:      rgba(47, 138, 78, 0.70);

    --cw-interesting:    #c79527;
    --cw-interesting-soft: rgba(199, 149, 39, 0.32);
    --cw-interesting-line: rgba(199, 149, 39, 0.75);

    --cw-bad:            #b62a2a;
    --cw-bad-soft:       rgba(182, 42, 42, 0.28);
    --cw-bad-line:       rgba(182, 42, 42, 0.70);

    --cw-neutral:        #2e6f99;
    --cw-neutral-soft:   rgba(46, 111, 153, 0.26);
    --cw-neutral-line:   rgba(46, 111, 153, 0.70);

    --cw-focus:          #7a4dc4;
    --cw-focus-soft:     rgba(122, 77, 196, 0.30);
    --cw-focus-line:     rgba(122, 77, 196, 0.75);

    --cw-square-light:   #f0d9b5;
    --cw-square-dark:    #b58863;
    --cw-board-border:   var(--color-border, #d6c9b4);
    /* Piece tones tuned like common web clients: warm off-white, deep black */
    --cw-piece-white:    #f4f2eb;
    --cw-piece-white-stroke: #2a2a2a;
    --cw-piece-black:    #121212;
    --cw-piece-black-stroke: #000000;
    /* Cburnett-style black-piece highlight lines (replaces #ececec in source art). */
    --cw-piece-lichess-mid: #b0b0b0;
    /* Last-move tint (sequence steps) — the familiar translucent yellow-green
       every chess client uses; deliberately distinct from the five semantic
       annotation colors so it reads as board state, not coach commentary. */
    --cw-lastmove:       rgba(155, 199, 0, 0.41);
    /* Check glow under the attacked king (accent + soft gradient mid-stop). */
    --cw-check:          rgba(206, 36, 36, 0.85);
    --cw-check-soft:     rgba(206, 36, 36, 0.35);
    /* Engine eval bar (issue #377): White's fill over Black's track. Deliberately
       the piece tones, not a semantic colour — it reads as side-to-side balance. */
    --cw-eval-white:     #f4f2eb;
    --cw-eval-black:     #43484f;
}

[data-theme="dark"] {
    --cw-good:           #4fbf78;
    --cw-good-soft:      rgba(79, 191, 120, 0.30);
    --cw-good-line:      rgba(79, 191, 120, 0.75);

    --cw-interesting:    #e7c062;
    --cw-interesting-soft: rgba(231, 192, 98, 0.32);
    --cw-interesting-line: rgba(231, 192, 98, 0.75);

    --cw-bad:            #e07070;
    --cw-bad-soft:       rgba(224, 112, 112, 0.30);
    --cw-bad-line:       rgba(224, 112, 112, 0.75);

    --cw-neutral:        #6db4dc;
    --cw-neutral-soft:   rgba(109, 180, 220, 0.28);
    --cw-neutral-line:   rgba(109, 180, 220, 0.75);

    --cw-focus:          #b48cff;
    --cw-focus-soft:     rgba(180, 140, 255, 0.32);
    --cw-focus-line:     rgba(180, 140, 255, 0.78);

    --cw-square-light:   #4f4a3e;
    --cw-square-dark:    #2c2a26;
    --cw-board-border:   #3a3a4a;
    --cw-piece-white:    #ece8df;
    --cw-piece-white-stroke: #0e0e12;
    --cw-piece-black:    #0a0a0e;
    --cw-piece-black-stroke: #000000;
    --cw-piece-lichess-mid: #8f8f96;
    --cw-lastmove:       rgba(185, 215, 60, 0.34);
    --cw-check:          rgba(235, 80, 80, 0.85);
    --cw-check-soft:     rgba(235, 80, 80, 0.35);
    --cw-eval-white:     #e8e8ea;
    --cw-eval-black:     #0c0c12;
}

/* ── Semantic helper classes ─────────────────────────────────────────────── */
.chesswick-color-good        { --cw-c: var(--cw-good);        --cw-cs: var(--cw-good-soft);        --cw-cl: var(--cw-good-line); }
.chesswick-color-interesting { --cw-c: var(--cw-interesting); --cw-cs: var(--cw-interesting-soft); --cw-cl: var(--cw-interesting-line); }
.chesswick-color-bad         { --cw-c: var(--cw-bad);         --cw-cs: var(--cw-bad-soft);         --cw-cl: var(--cw-bad-line); }
.chesswick-color-neutral     { --cw-c: var(--cw-neutral);     --cw-cs: var(--cw-neutral-soft);     --cw-cl: var(--cw-neutral-line); }
.chesswick-color-focus       { --cw-c: var(--cw-focus);       --cw-cs: var(--cw-focus-soft);       --cw-cl: var(--cw-focus-line); }

/* ── Diagram container ───────────────────────────────────────────────────── */
.chesswick-diagram {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-sm, 0.5rem);
    margin: var(--space-md, 1rem) 0;
    padding: var(--space-sm, 0.5rem);
    background: var(--color-surface, #fff);
    border: 1px solid var(--cw-board-border);
    border-radius: var(--border-radius, 8px);
    font-family: var(--font-sans, sans-serif);
    box-shadow: 0 2px 10px rgba(44, 36, 22, 0.06);
    transition: box-shadow var(--transition-fast, 120ms ease), border-color var(--transition-fast, 120ms ease);
}

@media (hover: hover) {
    .chesswick-diagram:hover {
        box-shadow: 0 4px 18px rgba(44, 36, 22, 0.1);
        border-color: var(--color-teal, #1b6b77);
    }
}

[data-theme="dark"] .chesswick-diagram {
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.28);
}

@media (hover: hover) {
    [data-theme="dark"] .chesswick-diagram:hover {
        box-shadow: 0 4px 22px rgba(0, 0, 0, 0.4);
        border-color: var(--color-teal, #5ec8d4);
    }
}

.chesswick-diagram__caption {
    font-size: 0.9rem;
    color: var(--color-text-muted, #7A6E5F);
    line-height: 1.45;
    text-align: center;
    padding: 0 var(--space-sm, 0.5rem);
}

.chesswick-diagram__truncated {
    font-size: 0.8rem;
    color: var(--cw-bad);
    margin: 0;
    padding: 0 var(--space-sm, 0.5rem);
    text-align: center;
    font-style: italic;
}

.chesswick-diagram__step-text {
    font-size: 0.95rem;
    line-height: 1.45;
    padding: var(--space-sm, 0.5rem);
    min-height: 2.4em;
    color: var(--color-text, inherit);
}

.chesswick-diagram__step-text code {
    background: var(--color-bg-alt, #f2ede4);
    padding: 1px 4px;
    border-radius: 3px;
    font-family: var(--font-mono, monospace);
    font-size: 0.9em;
}

/* ── Controls (sequence) ──────────────────────────────────────────────────── */
.chesswick-diagram__controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm, 0.5rem);
    padding: 0 var(--space-sm, 0.5rem);
    flex-wrap: wrap;
}

.chesswick-diagram__btn {
    border: 1px solid var(--cw-board-border);
    background: var(--color-bg, transparent);
    color: var(--color-text, inherit);
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background var(--transition-fast, 120ms ease);
    min-width: 44px;
    min-height: 32px;
}

.chesswick-diagram__btn:hover:not(:disabled) {
    background: var(--color-bg-alt, #f2ede4);
}

.chesswick-diagram__btn:focus-visible {
    outline: 2px solid var(--color-teal, #1b6b77);
    outline-offset: 2px;
}

.chesswick-diagram__btn:disabled {
    opacity: 0.4;
    cursor: default;
}

.chesswick-diagram__counter {
    font-size: 0.85rem;
    color: var(--color-text-muted, #7A6E5F);
    font-variant-numeric: tabular-nums;
    min-width: 3.5em;
    text-align: center;
}

/* ── Board ────────────────────────────────────────────────────────────────── */
.chesswick-board {
    position: relative;
    width: 100%;
    max-width: 480px;
    margin: 0 auto;
    aspect-ratio: 1 / 1;
}

.chesswick-board__grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 100%;
    height: 100%;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
    transition: transform 320ms ease;
}

.chesswick-board--flipped .chesswick-board__grid {
    transform: rotate(180deg);
}

.chesswick-board--flipped .chesswick-piece,
.chesswick-board--flipped .chesswick-square__label,
.chesswick-board--flipped .chesswick-region-label,
.chesswick-board--flipped .chesswick-square__coord {
    transform: rotate(180deg);
}

.chesswick-board__overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* SVG arrows do not flip — they're drawn in coordinate space that already
       respects orientation (see indexToOverlayCoord in chesswick-diagram.js). */
}

.chesswick-board__toolbar {
    position: absolute;
    top: -32px;
    right: 0;
    display: flex;
    gap: 4px;
}

.chesswick-board__flip,
.chesswick-board__enlarge,
.chesswick-board__copy {
    background: transparent;
    border: 1px solid var(--cw-board-border);
    color: var(--color-text-muted, #7A6E5F);
    border-radius: 6px;
    padding: 2px 8px;
    font-size: 0.75rem;
    cursor: pointer;
}

.chesswick-board__flip:hover,
.chesswick-board__enlarge:hover,
.chesswick-board__copy:hover { background: var(--color-bg-alt, #f2ede4); }

.chesswick-board__flip:focus-visible,
.chesswick-board__enlarge:focus-visible,
.chesswick-board__copy:focus-visible {
    outline: 2px solid var(--color-teal, #1b6b77);
    outline-offset: 2px;
}

/* Visually-hidden scratch element for the legacy clipboard fallback. */
.chesswick-offscreen {
    position: absolute;
    left: -9999px;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: 0;
}

/* The board grid is tappable to enlarge (and tap-again to return). */
.chesswick-board__grid { cursor: zoom-in; }
.chesswick-diagram--enlarged .chesswick-board__grid { cursor: zoom-out; }

/* ── Hidden coordinates (Settings toggle, spec §13) ──────────────────────── */
/* Coordinate spans are skipped at render time; this rule is a defensive hook
   for any board that still carries the class with coords present. */
.chesswick-board--no-coords .chesswick-square__coord { display: none; }

/* ── Enlarge / fill-viewport mode (spec §10, §13) ────────────────────────── */
html.chesswick-enlarge-active { overflow: hidden; }

.chesswick-diagram--enlarged {
    position: fixed;
    inset: 0;
    z-index: 1000;
    margin: 0;
    border-radius: 0;
    border: 0;
    max-width: none;
    overflow: auto;
    justify-content: center;
    background: var(--color-surface, #fff);
    padding: clamp(1rem, 4vh, 3rem) 1rem;
}

[data-theme="dark"] .chesswick-diagram--enlarged {
    background: var(--color-surface, #1a1a1f);
}

/* Let the board grow to fill the enlarged viewport while staying square. */
.chesswick-diagram--enlarged .chesswick-board {
    max-width: min(92vw, calc(100vh - 8rem));
}

/* The enlarge button doubles as the close affordance while enlarged; lift it
   clear of the toolbar's negative offset so it stays reachable full-screen. */
.chesswick-diagram--enlarged .chesswick-board__toolbar {
    position: fixed;
    top: 0.75rem;
    right: 0.75rem;
}

.chesswick-diagram--board .chesswick-board {
    margin-top: 32px;
}

.chesswick-diagram--sequence .chesswick-board {
    margin-top: 32px;
}

/* ── Player name + material bars (issue #271) ─────────────────────────────────
   Two absolute strips hugging the board's top and bottom edges. Only present
   when a CVN block supplies white/black names, so anonymous diagrams are
   unaffected. The bars live outside the grid, so they stay upright on flip;
   updatePlayerBars() swaps which colour sits at which edge. */
.chesswick-board--has-bottom-player {
    /* Reserve room below for the bottom strip; the top strip shares the 32px
       already reserved above for the flip toolbar. Toggled per-render so a
       top-only name doesn't leave a blank gutter below the board. */
    margin-bottom: 30px;
}

.chesswick-player {
    position: absolute;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    line-height: 1.2;
    color: var(--color-text, inherit);
    pointer-events: none;
}

.chesswick-player--top {
    bottom: 100%;
    margin-bottom: 3px;
    /* Keep the name clear of the toolbar (Enlarge / Flip / Copy FEN) that
       shares this strip. */
    padding-right: 11rem;
}

.chesswick-player--bottom {
    top: 100%;
    margin-top: 3px;
}

.chesswick-player__swatch {
    flex: 0 0 auto;
    width: 0.7rem;
    height: 0.7rem;
    border-radius: 50%;
    border: 1px solid var(--cw-board-border);
}

.chesswick-player--white .chesswick-player__swatch { background: var(--cw-piece-white); }
.chesswick-player--black .chesswick-player__swatch { background: var(--cw-piece-black); }

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

.chesswick-player__rating {
    flex: 0 0 auto;
    color: var(--color-text-muted, #7A6E5F);
    font-variant-numeric: tabular-nums;
}

.chesswick-player__rating::before { content: "("; }
.chesswick-player__rating::after { content: ")"; }
.chesswick-player__rating[hidden] { display: none; }

.chesswick-player__material {
    flex: 0 0 auto;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--cw-good);
}

.chesswick-player__material[hidden] { display: none; }

/* ── Engine evaluation overlay (issue #377) ───────────────────────────────────
   App-fed only — the coach never authors `eval` (the auditor strips a model-
   written one), so when a bar appears the number came from a trusted server
   review. A slim White-POV strip hugs the board's left edge with a compact
   readout floating above it. The gutter is reserved on the figure so an
   eval-bearing widget never shifts when a given step happens to carry no eval;
   diagrams without any eval get neither the class nor the gutter and are
   unchanged. The bar lives inside the board shell, so it survives flip/enlarge. */
.chesswick-diagram--has-eval { padding-left: 1.9rem; }

.chesswick-board__eval {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 0.55rem;
    /* Sit just outside the grid, inside the reserved figure gutter. */
    transform: translateX(-1.15rem);
    border-radius: 3px;
    overflow: hidden;
    background: var(--cw-eval-black);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    pointer-events: none;
}

/* White's share of the bar; fills from the bottom (White at the bottom) by
   default, from the top once the board is flipped (White at the top). */
.chesswick-board__eval-fill {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50%;
    background: var(--cw-eval-white);
    transition: height 320ms ease;
}

.chesswick-board__eval--white-top .chesswick-board__eval-fill {
    bottom: auto;
    top: 0;
}

.chesswick-board__eval-readout {
    position: absolute;
    top: 0;
    left: 0;
    /* Centre the readout over the bar, lifted into the toolbar gutter above the board. */
    transform: translate(-2.05rem, -1.55rem);
    min-width: 2.4rem;
    text-align: center;
    font-size: 0.72rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
    color: var(--color-text, inherit);
    pointer-events: none;
}

/* ── Squares ─────────────────────────────────────────────────────────────── */
.chesswick-square {
    position: relative;
    overflow: visible;
}

.chesswick-square--light { background: var(--cw-square-light); }
.chesswick-square--dark  { background: var(--cw-square-dark); }

/* Last-move tint: the from/to squares of the move that produced the displayed
   position (sequence steps). Sits beneath highlights (z-index 1) and pieces
   (z-index 2) so model annotations always win visually. */
.chesswick-square--lastmove::before {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--cw-lastmove);
    z-index: 0;
    pointer-events: none;
}

/* Check indicator: red radial glow under the king that is under attack. */
.chesswick-square--check::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 50% 50%,
        var(--cw-check) 0%, var(--cw-check-soft) 55%, transparent 78%);
    z-index: 1;
    pointer-events: none;
}

.chesswick-square__coord {
    position: absolute;
    font-size: 0.55rem;
    font-weight: 600;
    line-height: 1;
    color: rgba(0, 0, 0, 0.45);
    pointer-events: none;
    transition: transform 320ms ease;
}

.chesswick-square--dark .chesswick-square__coord { color: rgba(255, 255, 255, 0.65); }

.chesswick-square__coord--rank {
    top: 2px;
    left: 3px;
}

.chesswick-square__coord--file {
    bottom: 2px;
    right: 3px;
}

/* ── Pieces ──────────────────────────────────────────────────────────────── */
.chesswick-piece {
    position: absolute;
    inset: 4%;
    width: 92%;
    height: 92%;
    pointer-events: none;
    transition: transform 320ms ease;
    z-index: 2;
    /* Soft contact shadow like 2D client piece sprites */
    filter: drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22));
}

.chesswick-piece--w .chesswick-piece__fill {
    fill: var(--cw-piece-white);
    stroke: var(--cw-piece-white-stroke);
    stroke-width: 1.65;
    stroke-linecap: round;
    stroke-linejoin: round;
    paint-order: stroke fill;
}

.chesswick-piece--b .chesswick-piece__fill {
    fill: var(--cw-piece-black);
    stroke: var(--cw-piece-black-stroke);
    stroke-width: 1.85;
    stroke-linecap: round;
    stroke-linejoin: round;
    paint-order: stroke fill;
}

/* Lichess default (cburnett) markup uses native 45×45 coordinates inside the same square box. */
.chesswick-board--pieces-lichess .chesswick-piece__lichess {
    pointer-events: none;
}

/* Classic piece set: heavier outline, flatter shadow (matches older diagram look). */
.chesswick-board--pieces-classic .chesswick-piece {
    filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
}

.chesswick-board--pieces-classic .chesswick-piece--w .chesswick-piece__fill {
    stroke-width: 2;
    paint-order: normal;
}

.chesswick-board--pieces-classic .chesswick-piece--b .chesswick-piece__fill {
    stroke-width: 2;
    paint-order: normal;
}

/* ── Highlights ──────────────────────────────────────────────────────────── */
.chesswick-square__fx {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 1;
}

.chesswick-square__fx--fill {
    background: var(--cw-cs);
}

.chesswick-square__fx--ring {
    border: 3px solid var(--cw-c);
    box-sizing: border-box;
    background: transparent;
    border-radius: 2px;
}

.chesswick-square__fx--corners::before,
.chesswick-square__fx--corners::after,
.chesswick-square__fx--corners {
    /* Four 10% sized corner marks via radial gradients. */
}
.chesswick-square__fx--corners {
    background:
        linear-gradient(135deg, var(--cw-c) 12%, transparent 12%) top left/22% 22% no-repeat,
        linear-gradient(225deg, var(--cw-c) 12%, transparent 12%) top right/22% 22% no-repeat,
        linear-gradient(45deg,  var(--cw-c) 12%, transparent 12%) bottom left/22% 22% no-repeat,
        linear-gradient(315deg, var(--cw-c) 12%, transparent 12%) bottom right/22% 22% no-repeat;
    opacity: 0.85;
}

.chesswick-square__fx--dot {
    background: radial-gradient(circle at 50% 50%, var(--cw-c) 0 22%, transparent 23%);
    opacity: 0.85;
}

/* Regions render lighter than highlights so they don't overpower the board.
   When stacked with a fill highlight on the same square, the highlight wins
   visually (it's added after regions in DOM order, so z-index ordering by
   element order gives it the top spot). */
.chesswick-region-cell {
    position: absolute;
    inset: 0;
    background: var(--cw-cs);
    opacity: 0.55;
    z-index: 0;
    pointer-events: none;
}

.chesswick-region-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--cw-c);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 999px;
    white-space: nowrap;
    z-index: 4;
    pointer-events: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    /* Counter-rotate when board is flipped — done at the .chesswick-board
       level rule above. */
}

/* ── Labels ──────────────────────────────────────────────────────────────── */
.chesswick-square__label {
    position: absolute;
    bottom: 2px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--cw-c);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 1px 4px;
    border-radius: 3px;
    line-height: 1;
    z-index: 5;
    pointer-events: none;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

.chesswick-board--flipped .chesswick-square__label {
    transform: translateX(-50%) rotate(180deg);
}

/* ── Piece effects (glow / ring / pulse) ─────────────────────────────────── */
.chesswick-square--piece-fx-glow .chesswick-piece {
    filter:
        drop-shadow(0 0 4px var(--cw-c))
        drop-shadow(0 0 8px var(--cw-c))
        drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22));
}

.chesswick-square--piece-fx-ring::after {
    content: '';
    position: absolute;
    inset: 6%;
    border: 3px solid var(--cw-c);
    border-radius: 50%;
    pointer-events: none;
    z-index: 3;
}

.chesswick-square--piece-fx-pulse .chesswick-piece {
    animation: chesswick-pulse 1.4s ease-in-out infinite;
    filter: drop-shadow(0 0 4px var(--cw-c)) drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22));
}

@keyframes chesswick-pulse {
    0%, 100% { filter: drop-shadow(0 0 3px var(--cw-c))  drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22)); }
    50%      { filter: drop-shadow(0 0 10px var(--cw-c)) drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22)); }
}

/* ── Arrows ──────────────────────────────────────────────────────────────── */
.chesswick-arrow {
    fill: none;
    stroke: var(--cw-cl);
    stroke-width: 14;
    stroke-linecap: round;
    stroke-linejoin: round; /* rounds the elbow of knight (bent) arrows */
    opacity: 0.92;
}

.chesswick-arrow--thick { stroke-width: 22; }
.chesswick-arrow--dashed { stroke-dasharray: 22 14; }

.chesswick-arrow__head { fill: var(--cw-cl); }

/* ── Placeholders ────────────────────────────────────────────────────────── */
.chesswick-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    /* Match the typical rendered-board height so the surrounding text doesn't
       jump when the streaming placeholder swaps in for the real diagram. */
    min-height: 280px;
    padding: var(--space-md, 1rem);
    margin: var(--space-md, 1rem) 0;
    border: 1px dashed var(--cw-board-border);
    border-radius: var(--border-radius, 8px);
    background: var(--color-bg-alt, #f2ede4);
    color: var(--color-text-muted, #7A6E5F);
    font-style: italic;
    font-size: 0.9rem;
}

.chesswick-placeholder--error,
.chesswick-placeholder--incomplete {
    color: var(--cw-bad);
    border-color: var(--cw-bad-line);
    /* Errors shouldn't dominate the message — keep them small. */
    min-height: 48px;
}

.chesswick-placeholder--preparing::after {
    content: '';
    margin-left: 0.5em;
    width: 0.5em;
    height: 0.5em;
    border-radius: 50%;
    background: currentColor;
    animation: chesswick-blink 1.2s ease-in-out infinite;
}

@keyframes chesswick-blink {
    0%, 100% { opacity: 0.2; }
    50%      { opacity: 0.9; }
}

/* ── Reduced motion ──────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .chesswick-square--piece-fx-pulse .chesswick-piece {
        animation: none;
        filter: drop-shadow(0 0 6px var(--cw-c)) drop-shadow(0 1.5px 2px rgba(0, 0, 0, 0.22));
    }
    .chesswick-board__grid,
    .chesswick-piece,
    .chesswick-square__coord,
    .chesswick-square__label,
    .chesswick-region-label,
    .chesswick-board__eval-fill {
        transition: none;
    }
    .chesswick-placeholder--preparing::after { animation: none; opacity: 0.6; }
}

/* ── Clickable SAN move tokens (inline, in chat prose) ───────────────────── */
.san-move {
    display: inline;
    background: transparent;
    border: 0;
    padding: 0;
    margin: 0;
    color: var(--cw-neutral);
    cursor: pointer;
    font: inherit;
    text-decoration: underline dotted;
    text-underline-offset: 2px;
    -webkit-tap-highlight-color: transparent;
}
.san-move:hover {
    background: var(--cw-neutral-soft);
    text-decoration: underline solid;
    border-radius: 3px;
}
.san-move:focus-visible {
    outline: 2px solid var(--cw-focus);
    outline-offset: 2px;
    border-radius: 3px;
}

/* ── Popover: position preview anchored to a clicked SAN token ───────────── */
/* Native <dialog> opened via showModal() — gives a free focus trap, native
   ESC-to-close, and inert-page semantics. The dialog itself fills the
   viewport so click-outside detection works (target === dialog); the actual
   card lives in .san-popover__inner. */
.san-popover[open] {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 1rem;
    border: 0;
    background: transparent;
    color: inherit;
    display: grid;
    place-items: center;
    overflow: hidden;
}
.san-popover::backdrop {
    background: rgba(0, 0, 0, 0.45);
}
.san-popover__inner {
    position: relative;
    background: var(--color-bg, #fffaf2);
    border: 1px solid var(--cw-board-border);
    border-radius: var(--border-radius, 8px);
    padding: 1rem 1rem 0.75rem;
    max-width: min(420px, 95vw);
    max-height: 90vh;
    overflow: auto;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
}
.san-popover__close {
    position: absolute;
    top: 2px;
    right: 6px;
    background: transparent;
    border: 0;
    padding: 4px 8px;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    color: var(--color-text-muted, #7A6E5F);
}
.san-popover__close:hover { color: var(--color-text, #2a2a2a); }
.san-popover__close:focus-visible {
    outline: 2px solid var(--cw-focus);
    outline-offset: 2px;
    border-radius: 3px;
}
.san-popover__inner .chesswick-diagram {
    margin: 0;
}

/* ── Small-screen tightening ─────────────────────────────────────────────── */
@media (max-width: 420px) {
    .chesswick-board__toolbar {
        top: -28px;
    }
    .chesswick-board__flip,
    .chesswick-board__enlarge,
    .chesswick-board__copy {
        font-size: 0.7rem;
        padding: 1px 6px;
    }
    .chesswick-square__coord { font-size: 0.5rem; }
    .chesswick-square__label { font-size: 0.6rem; }
    .chesswick-diagram__step-text { font-size: 0.9rem; }
    .chesswick-player { font-size: 0.72rem; gap: 0.3rem; }
    .chesswick-player--top { padding-right: 9.5rem; }
    .chesswick-diagram--has-eval { padding-left: 1.55rem; }
    .chesswick-board__eval { width: 0.5rem; transform: translateX(-0.95rem); }
    .chesswick-board__eval-readout { font-size: 0.66rem; transform: translate(-1.75rem, -1.5rem); }
    .san-popover__inner { padding: 0.75rem 0.75rem 0.5rem; }
}
