/* ==========================================================================
   DESIGN TOKENS
   Tema scuro Dark Plum e tema chiaro High-Contrast Light. Superfici
   chiaramente separate, tipografia leggibile e controlli essenziali.
   ========================================================================== */
@import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,500;0,600;0,700;1,500;1,600&family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500;700&display=swap');

/* --- PALETTE DELLA SCACCHIERA cm-chessboard ---
   Modifica questi token per cambiare caselle, frecce ed evidenziazioni. */
:root {
    --chess-square-light: #938593;
    --chess-square-dark: #11001c;
    /* Ultima mossa: blu/teal separati dalle annotazioni verdi. */
    --chess-last-move-light: #4f91b5;
    --chess-last-move-dark: #087b86;
    /* Verde smeraldo più saturo, leggibile sia sul malva sia sul prugna. */
    --chess-annotation: #00e676;
    --chess-manual-arrow: #00c875;
    --chess-arrow-outline: #1b3a4b;
    --chess-highlight-light: #00e676;
    --chess-highlight-dark: #00b866;
    /* Le frecce che indicano continuazioni PGN sono lilla, opacità 90% nel CSS. */
    --chess-pgn-arrow: #d3c9d3;
}

/* --- TEMA CHIARO B: superfici chiare, testo scuro e bordi ben leggibili. --- */
:root {
    color-scheme: light;
    --paper: #f2f3f7;
    --paper-panel: #ffffff;
    --paper-panel-alt: #e8eaf0;
    --paper-panel-raised: #ffffff;
    --rule: #c5c9d4;
    --rule-soft: #d9dce5;

    --ink: #17131d;
    --ink-muted: #45404e;
    --ink-faint: #6c6677;

    --accent: #633178;
    --accent-strong: #7d4294;
    --accent-dim: #4d245f;
    --accent-ink: #ffffff;
    --accent-soft-bg: rgba(99, 49, 120, 0.12);

    --brass: #8a6418;
    --brass-strong: #a4771d;
    --brass-ink: #ffffff;
    --brass-soft-bg: rgba(138, 100, 24, 0.13);

    --plum: #633178;
    --plum-strong: #7d4294;
    --plum-ink: #ffffff;

    --success: #08764b;
    --success-ink: #ffffff;
    --success-soft-bg: rgba(8, 118, 75, 0.12);
    --danger: #b4233e;
    --danger-strong: #c82e4b;
    --danger-ink: #ffffff;

    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --radius-pill: 999px;

    --shadow-card: 0 1px 2px rgba(20, 15, 28, .08), 0 10px 24px -14px rgba(20, 15, 28, .28);
    --shadow-inset-line: inset 0 1px 0 rgba(255, 255, 255, .9);

    --font-display: 'Lora', ui-serif, Georgia, serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Consolas', 'Courier New', monospace;

    --ease-bounce: cubic-bezier(.34, 1.56, .64, 1);
    --ease-out: cubic-bezier(.16, 1, .3, 1);
}

/* --- TEMA SCURO A: Dark Plum, prugna profondo e accenti smeraldo. --- */
:root[data-theme="dark"] {
    color-scheme: dark;
    --paper: #130a19;
    --paper-panel: #1d1126;
    --paper-panel-alt: #291632;
    --paper-panel-raised: #321d3c;
    --rule: #54345f;
    --rule-soft: #3b2545;

    --ink: #fbf6fd;
    --ink-muted: #d3c4db;
    --ink-faint: #a58eaf;

    --accent: #c18bd1;
    --accent-strong: #dfafea;
    --accent-dim: #925aa2;
    --accent-ink: #1b1020;
    --accent-soft-bg: rgba(193, 139, 209, 0.18);

    --brass: #e0bb62;
    --brass-strong: #f1d27e;
    --brass-ink: #211805;
    --brass-soft-bg: rgba(224, 187, 98, 0.18);

    --plum: #ad78bb;
    --plum-strong: #c995d6;
    --plum-ink: #1b1020;

    --success: #35d99a;
    --success-ink: #062015;
    --success-soft-bg: rgba(53, 217, 154, .17);
    --danger: #ff7187;
    --danger-strong: #ff91a2;
    --danger-ink: #25080d;

    --shadow-card: 0 1px 2px rgba(0, 0, 0, .42), 0 14px 32px -16px rgba(0, 0, 0, .72);
    --shadow-inset-line: inset 0 1px 0 rgba(255, 255, 255, .055);
}

* {
    box-sizing: border-box;
}

html {
    /* Il bug della pagina che "scende" leggermente quando il commento di una
       mossa è lungo viene da qui: il browser applica lo scroll-anchoring
       ogni volta che il layout sopra il viewport cambia altezza. Il fix
       strutturale è dare al pannello dei tab un'altezza fissa con scroll
       interno (vedi .tab-content), questo è solo una rete di sicurezza. */
    overflow-anchor: none;
    color-scheme: light dark;
    /* Disattiva il doppio-tap-to-zoom di Safari/Chrome mobile: toccando in
       fretta più volte le freccette di navigazione (o facendo mosse veloci
       sulla scacchiera) il browser lo interpretava come un doppio tap e
       zoomava lo schermo. "manipulation" toglie SOLO quel gesto, lasciando
       intatto lo zoom "vero" a due dita per chi ne ha bisogno. */
    touch-action: manipulation;
}

body {
    font-family: var(--font-body);
    text-align: center;
    color: var(--ink);
    margin: 0;
    padding: 0;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-anchor: none;
    background: var(--paper);
    transition: background-color .2s ease, color .2s ease;
}

::selection {
    background: var(--accent-soft-bg);
    color: var(--accent-strong);
}

/* scrollbar sottile e coerente col tema, ovunque */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--rule) transparent;
}
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--rule); border-radius: var(--radius-pill); }
*::-webkit-scrollbar-thumb:hover { background: var(--accent-dim); }

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ==========================================================================
   HEADER — sostituisce i vecchi controlli "flottanti" (mute, streak, banner
   di esplorazione) che su schermi stretti finivano sopra la scacchiera,
   impedendo di muovere i pezzi nelle prime righe. Essendo ora un elemento
   normale nel flusso della pagina, la scacchiera parte sempre SOTTO di lui:
   il bug non può più verificarsi, qualunque sia la modalità o lo schermo.
   ========================================================================== */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    padding: 10px 16px;
    background: var(--paper-panel);
    border-bottom: solid 1px var(--rule);
}

.app-header-slot {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 34px;
}

.app-header-center {
    flex: 1;
    justify-content: center;
}

.icon-btn {
    background: transparent;
    border: solid 1px var(--rule);
    color: var(--ink-muted);
    width: 34px;
    height: 34px;
    padding: 0;
    margin: 0;
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    box-shadow: none;
    transition: color .16s ease, background-color .16s ease, border-color .16s ease;
}

.icon-btn svg {
    display: block;
    width: 19px;
    height: 19px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon-btn .theme-icon,
.icon-btn .audio-icon {
    display: none;
}

.icon-btn.move-dots-toggle.is-enabled {
    color: var(--success);
    background: var(--success-soft-bg);
    border-color: var(--success);
}

.icon-btn .move-dots-icon circle {
    fill: currentColor;
    stroke: none;
}

.icon-btn .move-dots-icon .move-dots-slash {
    opacity: 0;
    stroke-width: 2.2;
    transition: opacity .16s ease;
}

.icon-btn.move-dots-toggle[aria-pressed="false"] .move-dots-icon .move-dots-slash {
    opacity: 1;
}

:root[data-theme="dark"] .icon-btn .theme-icon-sun,
:root[data-theme="light"] .icon-btn .theme-icon-moon,
.icon-btn:not([data-muted="true"]) .audio-icon-on,
.icon-btn[data-muted="true"] .audio-icon-off {
    display: block;
}

.icon-btn:hover {
    color: var(--ink);
    border-color: var(--accent-dim);
    transform: none;
    filter: none;
}

.icon-btn:active {
    transform: scale(.92);
    box-shadow: none;
}

/* --- Selettore di modalità: tre destinazioni esplicite invece di un unico
   pulsante che le cicla — si vede subito dove si è e si arriva ovunque in un
   tocco, invece di dover ripassare per le altre. --- */
.mode-switch {
    display: flex;
    background: var(--paper-panel-alt);
    border: solid 1px var(--rule);
    border-radius: var(--radius-pill);
    padding: 3px;
    gap: 2px;
}

.mode-switch-btn {
    background: transparent;
    color: var(--ink-muted);
    border: none;
    margin: 0;
    padding: 7px 13px;
    font-size: 11.5px;
    font-weight: 700;
    letter-spacing: .02em;
    border-radius: var(--radius-pill);
    box-shadow: none;
    transition: background-color .15s var(--ease-out), color .15s ease;
}

.mode-switch-btn:hover {
    color: var(--ink);
    transform: none;
    filter: none;
}

.mode-switch-btn.active {
    background: var(--accent);
    color: var(--accent-ink);
    box-shadow: var(--shadow-card);
}

.mode-switch-btn.active:hover {
    color: var(--accent-ink);
}

/* --- STREAK: badge discreto, non più medaglia dorata con glow --- */
.drill-streak {
    display: none;
    align-items: center;
    gap: 7px;
    padding: 6px 14px 6px 10px;
    border-radius: var(--radius-pill);
    background: var(--paper-panel-alt);
    border: solid 1px var(--rule);
    font-weight: 700;
    font-family: var(--font-display);
    font-size: 15px;
    color: var(--ink-muted);
    transition: border-color .25s ease, color .25s ease, background-color .25s ease;
}

body.mode-drill .drill-streak {
    display: inline-flex;
}

.drill-streak-flame {
    font-size: 15px;
    opacity: .45;
    display: inline-block;
    transition: opacity .25s ease, transform .25s ease;
}

.drill-streak.lit {
    border-color: var(--brass);
    background: var(--brass-soft-bg);
    color: var(--brass-ink);
}

:root[data-theme="dark"] .drill-streak.lit {
    color: var(--brass-strong);
}

.drill-streak.lit .drill-streak-flame {
    opacity: 1;
}

#drill-streak-count {
    color: inherit;
}

.drill-streak.wobble .drill-streak-flame {
    animation: crownBounce .4s var(--ease-bounce);
}

.drill-streak.wobble {
    animation: streakPulse .4s var(--ease-out);
}

@keyframes crownBounce {
    0% { transform: scale(1) rotate(0deg); }
    35% { transform: scale(1.3) rotate(-8deg); }
    65% { transform: scale(1.08) rotate(6deg); }
    100% { transform: scale(1) rotate(0deg); }
}

@keyframes streakPulse {
    0% { box-shadow: 0 0 0 0 var(--brass-soft-bg); }
    100% { box-shadow: 0 0 0 8px rgba(163, 121, 47, 0); }
}

/* Confetti canvas per i milestone dello streak — invisibile e ignora i click */
#confetti-canvas {
    position: fixed;
    inset: 0;
    z-index: 250;
    pointer-events: none;
}

/* --- Banner "stai esplorando" — ora vive nell'header, in flusso --- */
.exploration-banner {
    display: none;
    align-items: center;
    gap: 10px;
    padding: 6px 8px 6px 14px;
    border-radius: var(--radius-pill);
    background: var(--plum);
    color: var(--plum-ink);
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
}

body.mode-read.exploring .exploration-banner {
    display: inline-flex;
    animation: pop .25s var(--ease-bounce);
}

.exploration-reset-btn {
    background: rgba(255, 255, 255, .8);
    color: var(--plum);
    padding: 5px 12px;
    font-size: 12px;
    border-radius: var(--radius-pill);
    box-shadow: none;
}

.exploration-reset-btn:hover {
    filter: brightness(1.05);
    transform: none;
}

/* --- LAYOUT PRINCIPALE --- */
.board-and-controls {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 24px;
    gap: 24px;
    max-width: 1160px;
    margin: 0 auto;
    width: 100%;
    flex: 1;
}

.game-area {
    display: flex;
    justify-content: center;
    align-items: stretch;
    height: min(70vh, calc(100vw - 60px));
}

#board-container {
    position: relative;
    width: min(70vh, calc(100vw - 60px));
    height: min(70vh, calc(100vw - 60px));
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--chess-square-dark);
    box-shadow: 0 0 0 1px var(--rule), var(--shadow-card);
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

#board-container .cm-chessboard {
    width: 100%;
    height: 100%;
}

/* Le classi .white/.black sono quelle delle caselle SVG di cm-chessboard. */
#board-container .cm-chessboard .board .square.white {
    fill: var(--chess-square-light) !important;
}

#board-container .cm-chessboard .board .square.black {
    fill: var(--chess-square-dark) !important;
}

#board-container .cm-chessboard .coordinates .coordinate {
    font-weight: 700;
    fill: var(--ink-muted);
}

#board-container .pgn-board-highlights,
#board-container .pgn-board-arrows,
#board-container .pgn-board-errors {
    pointer-events: none;
}

#board-container .pgn-square-highlight {
    pointer-events: none;
    shape-rendering: crispEdges;
}

/* Le frecce sono poligoni pieni, con una sola linea di bordo uniforme. */
#board-container .pgn-arrow {
    pointer-events: none;
    stroke-linejoin: round;
    stroke-linecap: round;
}
#board-container .pgn-arrow--pgn {
    opacity: 0.9;
}
#board-container .pgn-arrow--manual {
    opacity: 1;
}

/* NAG in tile arrotondato (stile B) e corona di scacco animata. */
#board-container .pgn-nag-badge,
#board-container .pgn-check-crown {
    pointer-events: none;
    overflow: visible;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, .48));
}
#board-container .pgn-check-crown {
    filter: drop-shadow(0 3px 7px rgba(115, 12, 37, .68));
}

/* Brilliant ibrido: l'overlay è SOPRA la scacchiera. La maschera SVG
   apre soltanto i quadretti del percorso e la finestra mobile del pezzo;
   tutti gli altri pezzi vengono quindi oscurati insieme alle caselle. */
#board-container .pgn-brilliant-board-layer,
#board-container .pgn-brilliant-board-layer *,
#board-container .pgn-brilliant-dim-layer,
#board-container .pgn-brilliant-dim-layer *,
#board-container .pgn-brilliant-layer,
#board-container .pgn-brilliant-layer * {
    pointer-events: none;
}
#board-container .pgn-brilliant-dim {
    fill: #57535d;
    opacity: 0;
    animation: pgn-brilliant-dim-in 440ms cubic-bezier(.42, 0, .58, 1) forwards;
}
#board-container .pgn-brilliant-path-square {
    fill: #00c875;
    fill-opacity: .62;
    stroke: rgba(190, 255, 222, .98);
    stroke-width: 1.25;
    filter: drop-shadow(0 0 7px rgba(0, 200, 117, .9));
    opacity: 0;
    transform-box: fill-box;
    transform-origin: center;
    animation: pgn-brilliant-path-reveal var(--path-reveal-duration, 300ms) cubic-bezier(.42, 0, .58, 1) var(--path-reveal-delay, 0ms) both;
}
#board-container .pgn-brilliant-label {
    opacity: 0;
    transform-box: fill-box;
    transform-origin: center;
    animation: pgn-brilliant-label 1.42s cubic-bezier(.18, .9, .25, 1) both;
}
#board-container .pgn-brilliant-pill {
    fill: #073d32;
    stroke: #a9ffda;
    stroke-width: 1.8;
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, .5)) drop-shadow(0 0 6px rgba(42, 239, 176, .65));
}
#board-container .pgn-brilliant-nag-box {
    fill: #00b976;
    stroke: rgba(226, 255, 241, .9);
    stroke-width: .9;
}
#board-container .pgn-brilliant-nag-text {
    fill: #ffffff;
    font-family: Inter, sans-serif;
    font-weight: 900;
}
#board-container .pgn-brilliant-text {
    fill: #ffffff;
    font-family: Inter, sans-serif;
    font-weight: 800;
    letter-spacing: 1.2px;
}
#board-container.pgn-brilliant-active {
    pointer-events: none;
}
#board-container .pgn-brilliant-pieces-shake {
    transform-box: fill-box;
    transform-origin: center;
    animation: pgn-brilliant-pieces-shake 320ms cubic-bezier(.36, .07, .19, .97) both;
}
@keyframes pgn-brilliant-dim-in {
    from { opacity: 0; }
    to { opacity: .74; }
}
@keyframes pgn-brilliant-path-reveal {
    0% { opacity: 0; transform: scale(.76); }
    48% { opacity: .82; transform: scale(1.035); }
    100% { opacity: 1; transform: scale(1); }
}
@keyframes pgn-brilliant-label {
    0% { opacity: 0; transform: translateY(7px) scale(.88); }
    16% { opacity: 1; transform: translateY(-1px) scale(1.035); }
    28%, 78% { opacity: 1; transform: translateY(0) scale(1); }
    100% { opacity: 0; transform: translateY(-4px) scale(.97); }
}
@keyframes pgn-brilliant-pieces-shake {
    0%, 100% { transform: translate(0, 0) rotate(0); }
    12% { transform: translate(-1.25px, .55px) rotate(-.2deg); }
    25% { transform: translate(1.2px, -.6px) rotate(.2deg); }
    38% { transform: translate(-1px, .45px) rotate(-.16deg); }
    51% { transform: translate(.9px, -.4px) rotate(.14deg); }
    64% { transform: translate(-.65px, .3px) rotate(-.1deg); }
    77% { transform: translate(.4px, -.2px) rotate(.06deg); }
    90% { transform: translate(-.18px, .1px) rotate(-.025deg); }
}
@media (prefers-reduced-motion: reduce) {
    #board-container .pgn-brilliant-dim,
    #board-container .pgn-brilliant-path-square,
    #board-container .pgn-brilliant-label,
    #board-container .pgn-brilliant-pieces-shake {
        animation: none !important;
    }
}

#board-container .cm-promotion-menu {
    position: absolute;
    z-index: 20;
    inset: 50% auto auto 50%;
    transform: translate(-50%, -50%);
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    width: min(82%, 280px);
    padding: 12px;
    border: 1px solid var(--chess-annotation);
    border-radius: 12px;
    background: var(--paper-panel-raised);
    box-shadow: 0 12px 36px rgba(0, 0, 0, .35);
}

#board-container .cm-promotion-choice {
    min-height: 42px;
    padding: 8px 10px;
    border: 1px solid var(--rule);
    border-radius: 8px;
    background: var(--paper-panel);
    color: var(--ink);
    font: 600 13px var(--font-body);
    cursor: pointer;
}

#board-container .cm-promotion-choice:hover,
#board-container .cm-promotion-choice:focus-visible {
    border-color: var(--chess-annotation);
    box-shadow: 0 0 0 2px rgba(0, 100, 102, .24);
}

@keyframes pgn-board-error-flash {
    0% { opacity: 0; }
    20% { opacity: .78; }
    100% { opacity: 0; }
}

#board-container .pgn-error-flash {
    fill: #d94343;
    fill-opacity: .72;
    animation: pgn-board-error-flash .48s ease-out forwards;
    pointer-events: none;
}

.tabs {
    width: min(420px, calc(100vw - 40px));
    display: flex;
    flex-direction: column;
    justify-self: center;
    align-items: center;
}

/* --- SELETTORE TAB: come i separatori a linguetta di un raccoglitore —
   quella selezionata resta "attaccata" al foglio sotto (stesso bordo/sfondo
   del pannello), le altre restano leggermente arretrate. --- */
.tab-switcher {
    width: 100%;
    display: flex;
    justify-content: center;
    gap: 2px;
    margin-bottom: -1px;
    position: relative;
    z-index: 1;
}

.tab {
    padding: 10px 8px 9px;
    background-color: var(--paper-panel-alt);
    color: var(--ink-faint);
    user-select: none;
    box-sizing: border-box;
    width: calc(100% / 3);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: .02em;
    border: solid 1px var(--rule);
    border-bottom: none;
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    transition: background-color .15s var(--ease-out), color .15s ease;
}

.tab.selected {
    background: var(--paper-panel);
    color: var(--accent);
    border-color: var(--rule);
    box-shadow: 0 -2px 0 0 var(--accent) inset;
}

.tab:hover {
    cursor: pointer;
    color: var(--ink);
}

.tab.selected:hover {
    color: var(--accent);
}

.tab-content {
    padding: 20px;
    background-color: var(--paper-panel);
    border: solid 1px var(--rule);
    display: flex;
    flex-direction: column;
    width: 100%;
    box-sizing: border-box;
    position: relative;
    /* Altezza FISSA (min == max) con scroll interno: risolve alla radice il
       bug per cui commenti lunghi facevano crescere la pagina e scattava lo
       scroll-anchoring del browser. Il pannello ora è una "carta" stabile,
       come in un'app da mobile — solo il suo contenuto scorre. */
    height: min(70vh, calc(100vw - 60px));
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
    box-shadow: var(--shadow-card);
    overflow-y: auto;
    overscroll-behavior: contain;
}

/* In un flex-column ad altezza fissa i figli si restringono per default
   (flex-shrink:1) invece di far scorrere il pannello: un commento molto
   lungo comprimeva così il proprio box e "sfondava" sopra i controlli
   sottostanti. Bloccando lo shrink, ogni figlio mantiene la sua altezza
   naturale e l'overflow-y:auto del pannello gestisce lo scroll com'è giusto
   che sia — root cause reale del bug di scroll segnalato. */
.tab-content > * {
    flex-shrink: 0;
}

/* Rete di sicurezza extra: ora che il pannello stesso è scrollabile
   (overflow-y:auto sopra), anche LUI è soggetto allo scroll-anchoring
   nativo del browser quando il contenuto sopra il suo viewport interno
   cambia altezza — stesso fenomeno del bug originale, ma un livello più
   in profondità. */
.tab-content {
    overflow-anchor: none;
}

@keyframes fadeSlideIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Spazio extra in fondo solo qui: le freccette di navigazione sono fisse in
   basso (vedi .nav-buttons) e non devono coprire l'ultima parte del contenuto. */
#analysis {
    padding-bottom: 64px;
}

/* --- EVAL BAR --- */
.eval-bar-wrapper {
    width: 24px;
    /* Colore fisso, non legato al tema chiaro/scuro del sito: qui "nero" deve
       restare sempre visivamente scuro. var(--ink) si inverte apposta tra i
       due temi (è il colore del testo), quindi usarla qui mandava la barra
       in confusione nel tema scuro (lo sfondo "nero" diventava chiaro). */
    background-color: #241a2a;
    border-radius: var(--radius-sm);
    margin-right: 14px;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    transition: transform 0.2s ease;
    box-shadow: 0 0 0 1px var(--rule), var(--shadow-card);
}

.eval-bar-wrapper.flipped {
    transform: scaleY(-1);
}

.eval-bar-fill {
    width: 100%;
    background: linear-gradient(180deg, #fffdf7, #e7dfcc);
    height: 50%;
    transition: height 0.35s var(--ease-out);
}

.eval-bar-text {
    position: absolute;
    width: 100%;
    text-align: center;
    font-size: 10px;
    font-weight: 800;
    font-family: var(--font-mono);
    z-index: 2;
    padding: 5px 0;
    box-sizing: border-box;
}

.eval-bar-text.top {
    top: 0;
    color: #fffdf7;
}

.eval-bar-text.bottom {
    bottom: 0;
    color: #241a2a;
}

.eval-bar-wrapper.flipped .eval-bar-text {
    transform: scaleY(-1);
}

/* --- BOTTONI ---
   Piatti, con bordo netto stile "timbro" invece del rilievo 3D gommoso di
   prima: si abbassano leggermente al click, niente ombre a più strati. */
button {
    padding: 10px 18px;
    margin: 4px;
    cursor: pointer;
    border: solid 1px var(--accent-dim);
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 700;
    font-family: inherit;
    font-size: 13px;
    letter-spacing: .01em;
    box-shadow: var(--shadow-card);
    transition: transform .1s var(--ease-out), box-shadow .12s ease, filter .12s ease, background-color .12s ease;
}

button:hover {
    filter: brightness(1.08);
}

button:active {
    transform: translateY(1px);
    box-shadow: none;
}

button.danger {
    background: var(--danger);
    border-color: var(--danger-strong);
    color: var(--danger-ink);
}

button.secondary {
    background: var(--paper-panel-alt);
    color: var(--ink-muted);
    border: solid 1px var(--rule);
    box-shadow: none;
    align-self: flex-start;
}

button.secondary:hover {
    color: var(--ink);
    border-color: var(--accent-dim);
    filter: none;
}

button:disabled,
.delete-subtree-btn:disabled {
    background: var(--paper-panel-alt);
    border-color: var(--rule);
    color: var(--ink-faint);
    cursor: not-allowed;
    filter: none;
    transform: none;
    box-shadow: none;
}

#engine-info {
    margin-top: 14px;
    padding: 10px 12px;
    background: var(--paper-panel-alt);
    border: solid 1px var(--rule-soft);
    border-radius: var(--radius-sm);
    font-size: 12.5px;
    color: var(--ink-muted);
    text-align: left;
    display: block;
    font-family: var(--font-mono);
    line-height: 1.7;
    min-height: 56px;
}

@media (max-aspect-ratio: 1/1) {
    .board-and-controls {
        flex-direction: column;
        align-items: center;
        padding: 16px;
        padding-bottom: 90px;
    }

    .tabs {
        width: min(520px, calc(100vw - 32px));
    }

    #engine-info {
        font-size: 10.5px;
    }
}

/* --- TAB ANALYSIS: bottoni, commento, move-list --- */

.buttons {
    display: flex;
    justify-content: center;
    gap: 8px;
}

/* Il commento va per primo nel markup: come l'annotazione a margine di un
   quaderno, con un filo di bordeaux a sinistra per distinguerla subito. */
.move-comment {
    text-align: left;
    margin-bottom: 14px;
    min-height: 1.4em;
    animation: fadeIn .25s ease;
}

.cb-comment-empty {
    color: var(--ink-faint);
    font-style: italic;
    font-size: 13px;
    padding: 12px 14px;
    background: var(--paper-panel-alt);
    border: dashed 1px var(--rule);
    border-radius: var(--radius-sm);
}

.cb-comment-opening,
.cb-comment-variation,
.cb-comment-line,
.cb-comment-content {
    padding: 3px 0 3px 14px;
    border-left: solid 3px var(--rule);
    margin-bottom: 6px;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.cb-comment-opening {
    font-family: var(--font-display);
    font-size: 19px;
    font-weight: 700;
    color: var(--ink);
    border-left-color: var(--accent);
}

/* "Unknown opening": nessun dato trovato nel database ECO né scritto a mano.
   Resta leggibile ma visivamente "spento" — non deve sembrare un titolo vero
   quanto piuttosto un placeholder onesto. */
.cb-comment-opening-fallback {
    color: var(--ink-faint);
    font-weight: 500;
    font-style: italic;
    border-left-color: var(--rule);
}

.cb-comment-variation {
    font-family: var(--font-display);
    font-size: 13.5px;
    font-style: italic;
    color: var(--plum);
    border-left-color: var(--plum);
}

.cb-comment-line {
    font-family: var(--font-mono);
    font-size: 12.5px;
    color: var(--ink-muted);
}

.cb-comment-content {
    font-size: 14px;
    color: var(--ink);
    line-height: 1.6;
    white-space: pre-wrap;
    border-left-color: var(--rule);
}

/* Freccette di navigazione — pillola fissa in basso: avanti/indietro sono i
   due pulsanti pieni al centro; inizio/fine linea sono "ghost" ai lati. */
.nav-buttons {
    position: fixed;
    bottom: 14px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    z-index: 90;
    background-color: var(--paper-panel);
    border: solid 1px var(--rule);
    border-radius: var(--radius-pill);
    padding: 6px;
    box-shadow: var(--shadow-card);
}

.nav-btn {
    width: 40px;
    height: 40px;
    min-width: 0;
    padding: 0;
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 50%;
    box-shadow: none;
    transition: transform .1s var(--ease-out), background-color .15s ease, color .15s ease;
}

.nav-btn svg {
    display: block;
}

.nav-btn-primary {
    width: 46px;
    height: 46px;
    background: var(--accent);
    color: var(--accent-ink);
    border: solid 1px var(--accent-dim);
}

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

.nav-btn-primary:active {
    transform: translateY(1px) scale(0.97);
}

.nav-btn-ghost {
    background-color: transparent;
    color: var(--ink-faint);
    border: solid 1px transparent;
}

.nav-btn-ghost:hover {
    background-color: var(--paper-panel-alt);
    color: var(--ink);
    transform: none;
}

.nav-btn-ghost:active {
    transform: scale(.9);
}

.move-list-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.move-list-header .field-label {
    margin: 10px 0 4px;
}

.toggle-arrows-btn {
    font-size: 10.5px;
    padding: 5px 10px;
    background-color: var(--paper-panel-alt);
    color: var(--ink-faint);
    border: solid 1px var(--rule);
    box-shadow: none;
    border-radius: var(--radius-pill);
}

.toggle-arrows-btn:hover {
    transform: none;
    color: var(--ink-muted);
    filter: none;
}

.toggle-arrows-btn.active {
    color: var(--accent-ink);
    background: var(--accent);
    border-color: var(--accent-dim);
}

/* Move-list: le varianti sono blocchi rientrati a sé (non più testo tra
   parentesi in linea, illeggibile con più diramazioni annidate) — basta
   seguire il rientro/colore del bordo per capire subito "dove si è". */
.move-list {
    text-align: left;
    font-family: var(--font-mono);
    font-size: 13px;
    color: var(--ink-muted);
    background-color: var(--paper-panel-alt);
    border: solid 1px var(--rule-soft);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    margin-top: 8px;
    max-height: 280px;
    overflow-y: auto;
}

.move-list-empty {
    color: var(--ink-faint);
    font-style: italic;
}

.move-list-line {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1px 2px;
    line-height: 2.1;
}

.move-list-number {
    color: var(--ink-faint);
    padding: 0 2px 0 5px;
    user-select: none;
}

.move-list-line.depth-0 > .move-list-number:first-child {
    padding-left: 0;
}

.move-list-move {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 2px 7px;
    border-radius: 3px;
    color: var(--ink);
    cursor: pointer;
    transition: background-color .12s ease;
}

.move-list-move:hover {
    background-color: var(--accent-soft-bg);
}

/* La mossa attiva è "passata all'evidenziatore", non un pulsante dorato:
   coerente col resto del quaderno — sottolineatura piena color ottone. */
.move-list-move.active {
    background: var(--brass-soft-bg);
    box-shadow: inset 0 -2px 0 0 var(--brass);
    color: var(--ink);
    font-weight: 700;
}

.move-list-nag-icon {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    vertical-align: -2px;
}

/* --- Varianti: blocco rientrato, bordo colorato per profondità --- */

.move-list-variation {
    flex-basis: 100%;
    align-items: baseline;
    margin: 3px 0 3px 8px;
    padding: 3px 0 3px 10px;
    border-left: solid 2px var(--plum);
    font-size: 12.5px;
}

.move-list-branch-marker {
    color: var(--ink-faint);
    font-size: 11px;
    margin-right: 3px;
    user-select: none;
}

.move-list-variation .move-list-move {
    color: var(--ink-muted);
}

.move-list-variation .move-list-move.active {
    color: var(--ink);
}

.move-list-variation.depth-2 {
    margin-left: 18px;
    border-left-color: var(--plum-strong);
    opacity: .92;
}

.move-list-variation.depth-3 {
    margin-left: 28px;
    border-left-color: var(--brass);
    opacity: .86;
    font-size: 12px;
}

.move-list-variation.depth-4 {
    margin-left: 36px;
    border-left-color: var(--rule);
    opacity: .8;
    font-size: 12px;
}

/* --- TAB NOTES (annotazione mosse + NAG) --- */

.field-label {
    text-align: left;
    font-size: 10.5px;
    color: var(--ink-faint);
    text-transform: uppercase;
    letter-spacing: .08em;
    font-weight: 700;
    margin: 10px 0 6px;
}

.field-label:first-child {
    margin-top: 0;
}

.comment-editor {
    width: 100%;
    box-sizing: border-box;
    resize: vertical;
    min-height: 150px;
    padding: 12px;
    font-family: var(--font-mono);
    font-size: 13px;
    background-color: var(--paper-panel-alt);
    color: var(--ink);
    border: solid 1px var(--rule);
    border-radius: var(--radius-sm);
    transition: border-color .15s ease, box-shadow .15s ease;
}

.comment-editor:focus {
    outline: none;
    border-color: var(--accent-dim);
    box-shadow: 0 0 0 3px var(--accent-soft-bg);
}

.comment-editor:disabled {
    color: var(--ink-faint);
    background-color: var(--paper-panel);
}

.edit-danger-zone {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 20px;
    padding-top: 16px;
    border-top: solid 1px var(--rule-soft);
}

/* --- NAG: pulsanti rettangolari coordinati con i badge quadrati sulla board. --- */
.nag-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.nag-palette.disabled {
    opacity: .4;
    pointer-events: none;
}

.nag-btn {
    width: 48px;
    height: 40px;
    padding: 2px;
    margin: 0;
    border-radius: 9px;
    background-color: var(--paper-panel-alt);
    border: solid 2px var(--rule);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: none;
    transition: transform .12s var(--ease-bounce), border-color .15s ease, box-shadow .15s ease;
}

.nag-btn img {
    width: 100%;
    height: 100%;
    border-radius: 6px;
    object-fit: contain;
}

.nag-btn:hover {
    transform: translateY(-2px);
    border-color: var(--nag-color, var(--accent-dim));
}

.nag-btn:active {
    transform: scale(0.92);
}

.nag-btn.selected {
    border-color: var(--nag-color, var(--accent));
    box-shadow: 0 0 0 2px var(--paper-panel), 0 0 0 4px var(--nag-color, var(--accent));
    animation: pop .22s var(--ease-bounce);
}

.nag-btn-clear {
    background-color: var(--paper-panel-raised);
    color: var(--ink-muted);
    font-weight: bold;
    border-color: var(--rule);
}

/* --- TAB FILE (import/export PGN) --- */

input[type="file"] {
    color: var(--ink-muted);
    font-size: 13px;
}

.pgn-preview-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}

.pgn-clean-toggle-label {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 11.5px;
    color: var(--ink-faint);
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.pgn-clean-toggle-label:hover {
    color: var(--ink-muted);
}

.pgn-clean-toggle-label input[type="checkbox"] {
    accent-color: var(--accent);
    width: 13px;
    height: 13px;
    margin: 0;
    cursor: pointer;
}

.pgn-export-preview {
    width: 100%;
    box-sizing: border-box;
    min-height: 190px;
    padding: 12px;
    font-family: var(--font-mono);
    font-size: 12px;
    background-color: var(--paper-panel-alt);
    color: var(--ink-muted);
    border: solid 1px var(--rule);
    border-radius: var(--radius-sm);
    resize: vertical;
}

.export-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.export-buttons button {
    flex: 1;
    min-width: 140px;
}

.error-text {
    color: var(--danger);
    font-size: 13px;
    text-align: left;
}

/* --- GATE: forza il caricamento di un PGN --- */

.pgn-gate-overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(20, 14, 6, .55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
    box-sizing: border-box;
}

.pgn-gate-overlay.visible {
    display: flex;
    animation: fadeIn .25s ease;
}

.pgn-gate-box {
    background: var(--paper-panel-raised);
    border: solid 1px var(--rule);
    border-radius: var(--radius-lg);
    padding: 30px 26px;
    max-width: 360px;
    text-align: center;
    animation: pop .28s var(--ease-bounce);
    box-shadow: var(--shadow-card);
}

.pgn-gate-title {
    font-family: var(--font-display);
    font-size: 20px;
    font-weight: 700;
    margin: 0 0 8px;
    color: var(--ink);
}

.pgn-gate-hint {
    font-size: 13px;
    color: var(--ink-muted);
    margin: 0 0 18px;
    line-height: 1.5;
}

.pgn-gate-file-label {
    display: inline-block;
    padding: 10px 20px;
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 700;
    border-radius: var(--radius-sm);
    cursor: pointer;
    box-shadow: var(--shadow-card);
}

.pgn-gate-file-label:hover {
    filter: brightness(1.08);
}

.pgn-gate-file {
    display: none;
}

.pgn-gate-error {
    color: var(--danger);
    font-size: 13px;
    margin: 14px 0 0;
}

/* --- MODALITÀ --- */

body.mode-read .tab-switcher {
    display: none;
}

body.mode-drill .tabs,
body.mode-drill .eval-bar-wrapper {
    display: none;
}

body.mode-drill .board-and-controls {
    justify-content: center;
    flex: 1;
}

body.mode-drill .game-area {
    margin: auto;
}

/* Segnala il turno dell'utente in drill con un contorno pulsante attorno
   alla scacchiera — piccolo ma efficace per il "cosa devo fare adesso". */
body.mode-drill #board-container {
    animation: drillReady 2.4s ease-in-out infinite;
}

@keyframes drillReady {
    0%, 100% { box-shadow: 0 0 0 1px var(--rule), var(--shadow-card); }
    50% { box-shadow: 0 0 0 1px var(--plum), var(--shadow-card), 0 0 26px -8px var(--plum); }
}

/* ==========================================================================
   ANIMAZIONI CONDIVISE
   ========================================================================== */

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes pop {
    0% { transform: scale(0.85); }
    60% { transform: scale(1.08); }
    100% { transform: scale(1); }
}

@keyframes wobble {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-4deg); }
    75% { transform: rotate(4deg); }
}

.wobble {
    animation: wobble .3s ease;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}
