/* style.css */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    color-scheme: dark;
    --md-sys-color-primary: #C5A46A;
    --md-sys-color-on-primary: #171512;
    --md-sys-color-primary-hover: #DEC18C;
    --md-sys-color-primary-container: #3C3223;
    --md-sys-color-on-primary-container: #F2E8D5;
    --md-sys-color-secondary-container: #332D25;
    --md-sys-color-secondary-container-hover: #494033;
    --md-sys-color-on-secondary-container: #F2E8D5;
    --md-sys-color-surface: #171512;
    --md-sys-color-surface-container-low: #1D1A16;
    --md-sys-color-surface-container: #24201B;
    --md-sys-color-surface-container-high: #24201B;
    --md-sys-color-surface-container-highest: #332D25;
    --md-sys-color-on-surface: #F2E8D5;
    --md-sys-color-on-surface-variant: #C8BAA3;
    --md-sys-color-outline: #B19A76;
    --md-sys-color-outline-variant: #514536;
    --md-sys-color-error: #ffb4ab;
    --md-sys-color-scrim: rgba(0, 0, 0, 0.68);
    --md-focus-ring: #FFF4D9;
    --md-card-shadow: 0 2px 8px rgba(0, 0, 0, 0.24);
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--md-sys-color-surface);
    color: var(--md-sys-color-on-surface);
    line-height: 1.5;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Keep the regular layout scrollable when its content needs more space. */
html, body {
    overflow: auto;
}

/* Header removed — controls are in sidebar */

/* Controls row */
.controls-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 6px;
    background: var(--md-sys-color-surface-container);
    border-radius: 12px;
    border: 1px solid var(--md-sys-color-outline-variant);
    box-shadow: var(--md-card-shadow);
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Depth value display */
.depth-value {
    display: inline-block;
    min-width: 24px;
    text-align: center;
    font-weight: bold;
    color: var(--md-sys-color-on-surface);
    font-size: 1rem;
}

/* Control focus indicator (keyboard/TV navigation) */
.btn.control-focused {
    outline: 2px solid var(--md-focus-ring);
    outline-offset: 2px;
    transform: scale(1.05);
}

.btn {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    padding: 8px 18px;
    border: none;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: background 0.2s, color 0.2s, transform 0.1s, box-shadow 0.2s;
}

.btn:active {
    transform: scale(0.96);
}

/* Keep every control reachable with a touch remote without making the row bulky. */
.control-btn {
    min-width: 48px;
    min-height: 48px;
    padding: 8px 12px;
    touch-action: manipulation;
}

.btn:not(.control-btn),
#promo-cancel,
.gameover-close {
    min-width: 48px;
    min-height: 48px;
}

.btn:focus-visible,
.promo-btn:focus-visible,
#promo-cancel:focus-visible,
.gameover-close:focus-visible {
    outline: 3px solid var(--md-focus-ring);
    outline-offset: 3px;
}

.btn::after,
.promo-btn::after,
#promo-cancel::after,
.gameover-close::after {
    content: '';
    position: absolute;
    inset: 50% auto auto 50%;
    width: 140%;
    aspect-ratio: 1;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.22);
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, -50%) scale(0);
    z-index: 0;
}

.btn:active::after,
.promo-btn:active::after,
#promo-cancel:active::after,
.gameover-close:active::after {
    animation: md-ripple 0.45s ease-out;
}

@keyframes md-ripple {
    0% { opacity: 0.5; transform: translate(-50%, -50%) scale(0); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(1); }
}

.btn:active {
    transform: scale(0.97);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: var(--md-sys-color-primary);
    color: var(--md-sys-color-on-primary);
}

.btn-primary:hover:not(:disabled) {
    background: var(--md-sys-color-primary-hover);
}

.btn-secondary {
    background: var(--md-sys-color-secondary-container);
    color: var(--md-sys-color-on-secondary-container);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--md-sys-color-secondary-container-hover);
    color: var(--md-sys-color-on-secondary-container);
}

.depth-label {
    display: flex;
    align-items: center;
    font-size: 0.85rem;
    color: var(--md-sys-color-on-surface-variant);
    font-weight: 500;
}

/* Main layout */
main {
    display: flex;
    gap: 24px;
    max-width: 900px;
    width: 100%;
    padding: 20px;
    padding-top: max(16px, env(safe-area-inset-top, 16px));
    padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
    padding-left: max(20px, env(safe-area-inset-left, 20px));
    padding-right: max(20px, env(safe-area-inset-right, 20px));
    align-items: flex-start;
    justify-content: center;
}

/* Board container */
.board-container {
    position: relative;
    display: flex;
    flex-direction: column;
}

#board {
    position: relative;
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(8, minmax(0, 1fr));
    box-sizing: border-box;
    width: 480px;
    height: 480px;
    border: 3px solid var(--md-sys-color-outline);
    border-radius: 4px;
    overflow: hidden;
    /* Keep the frame visible even when focus moves to a control. */
    box-shadow: 0 0 0 2px var(--md-sys-color-outline), 0 0 0 4px var(--md-sys-color-surface-container-high), 0 8px 32px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.05);
}

.move-arrow-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 3;
    transition: opacity 0.2s ease-in-out;
}

.square {
    width: auto;
    height: auto;
    min-width: 0;
    min-height: 0;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    container-type: inline-size;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    transition: background 0.15s;
}

.square.light {
    background: #E8D8B9;
}

.square.dark {
    background: #98704E;
}

.square:hover {
    filter: brightness(1.1);
}

/* Coordinates */
.coord {
    position: absolute;
    font-size: 0.65rem;
    font-weight: 700;
    opacity: 1;
    pointer-events: none;
    user-select: none;
}

.rank-label {
    top: 2px;
    left: 3px;
}

.file-label {
    bottom: 2px;
    right: 3px;
}

.square.light .coord {
    color: #58402C;
}

.square.dark .coord {
    color: #FFF4DF;
}

/* Pieces */
.piece {
    font-size: 2.8rem;
    line-height: 1;
    text-shadow: 1px 1px 3px rgba(0,0,0,0.3);
    pointer-events: none;
    user-select: none;
    z-index: 1;
}

.piece[data-color="white"] {
    color: #fff;
    text-shadow:
        -1px -1px 0 #000,
         1px -1px 0 #000,
        -1px  1px 0 #000,
         1px  1px 0 #000;
}

.piece[data-color="black"] {
    color: #000;
    text-shadow:
        -1px -1px 0 #fff,
         1px -1px 0 #fff,
        -1px  1px 0 #fff,
         1px  1px 0 #fff,
         0 0 2px rgba(255,255,255,0.6);
}

/* Selected square */
.square.selected {
    background: #7fc97f !important;
    box-shadow: inset 0 0 0 3px rgba(58, 134, 255, 0.8);
}

/* Legal move indicators */
.square.legal-move::after {
    content: '';
    position: absolute;
    width: 30cqw;
    height: 30cqw;
    border-radius: 50%;
    background: rgba(0, 128, 0, 0.4);
    z-index: 2;
    pointer-events: none;
}

.square.capture-move::after {
    content: '';
    position: absolute;
    width: 87cqw;
    height: 87cqw;
    border-radius: 50%;
    box-shadow: inset 0 0 0 max(1px, 4cqw) rgba(200, 0, 0, 0.6);
    background: transparent;
    z-index: 2;
    pointer-events: none;
}

/* Training hint highlight */
.square.hint-move {
    box-shadow: inset 0 0 0 4px rgba(255, 200, 0, 0.9) !important;
}

/* Last move highlight */
.square.last-move {
    background: rgba(255, 255, 0, 0.35) !important;
}

.square.last-move.light {
    background: rgba(255, 255, 0, 0.4) !important;
}

.square.last-move.dark {
    background: rgba(200, 180, 0, 0.45) !important;
}

/* In-check highlight */
.square.in-check {
    background: rgba(255, 0, 0, 0.45) !important;
    animation: check-pulse 1s infinite alternate;
}

@keyframes check-pulse {
    from { box-shadow: inset 0 0 10px rgba(255, 0, 0, 0.3); }
    to   { box-shadow: inset 0 0 20px rgba(255, 0, 0, 0.6); }
}

/* Sidebar */
.sidebar {
    flex: 1;
    min-width: 220px;
    max-width: 280px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Status bar */
#status {
    padding: 10px 16px;
    background: var(--md-sys-color-surface-container);
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 500;
    text-align: center;
    border: 1px solid var(--md-sys-color-outline-variant);
    box-shadow: var(--md-card-shadow);
}

.status-check {
    color: var(--md-sys-color-error);
    border-color: var(--md-sys-color-error);
}

.status-checkmate {
    color: #ffd93d;
    border-color: #ffd93d;
    font-weight: 700;
}

.status-draw {
    color: #a0a0a0;
    border-color: #666;
}

/* Thinking indicator */
#thinking-indicator {
    display: none;
    padding: 8px 16px;
    background: var(--md-sys-color-surface-container);
    border-radius: 12px;
    text-align: center;
    font-size: 0.85rem;
    color: var(--md-sys-color-on-surface-variant);
    border: 1px solid var(--md-sys-color-outline-variant);
    box-shadow: var(--md-card-shadow);
}

#thinking-indicator.visible {
    display: none;
}

#thinking-indicator .dots,
#status .dots {
    display: inline-block;
    min-width: 3ch;
    text-align: left;
}

#thinking-indicator .dots::after,
#status .dots::after {
    /* Static ellipsis: older TV engines and reduced motion never run the
     * content-keyframe animation, so the dots must read without it. */
    content: '...';
    animation: dots 1.5s infinite;
}

/* AI thinking lives inside #status: explicit visibility contract so the label
 * never depends on UA-default hidden handling (notably on TV browsers). */
#status-thinking {
    display: inline;
    color: inherit;
}
#status-thinking[hidden] {
    display: none;
}

@keyframes dots {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

/* Move history */
.move-history {
    background: var(--md-sys-color-surface-container-low);
    border-radius: 12px;
    border: 1px solid var(--md-sys-color-outline-variant);
    padding: 10px;
    flex: 1;
    min-height: 132px;
    max-height: 360px;
    overflow-y: auto;
    box-shadow: var(--md-card-shadow);
}

.move-history h3 {
    font-size: 0.85rem;
    color: var(--md-sys-color-on-surface-variant);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

#move-list {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9rem;
}

.move-pair {
    padding: 2px 0;
    line-height: 1.6;
    white-space: nowrap;
}

.move-number {
    color: var(--md-sys-color-on-surface-variant);
    display: inline-block;
    width: 32px;
}

.move-white {
    display: inline-block;
    width: 60px;
}

.move-black {
    display: inline-block;
    width: 60px;
}

/* Move list zebra striping */
.move-pair:nth-child(even) {
    background: rgba(255, 255, 255, 0.03);
    border-radius: 3px;
}
.move-pair:hover {
    background: rgba(255, 255, 255, 0.06);
}

/* Subtle button hover lift */
.btn:hover:not(:disabled) {
    transform: translateY(-1px);
}

/* Captured pieces */
.captured-pieces {
    min-height: 32px;
    padding: 4px 8px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px;
    -webkit-user-select: none;
    user-select: none;
}
.captured-pieces:empty {
    /* Hidden but space-preserving: the board must not jump when
     * the first capture appears. */
    visibility: hidden;
}
.captured-piece {
    font-size: 1.2rem;
    opacity: 0.8;
}
.captured-piece[data-color="white"] {
    color: #fff;
    text-shadow: 0 0 2px #000;
}
.captured-piece[data-color="black"] {
    color: #222;
    text-shadow: 0 0 2px #fff;
}
.material-advantage {
    font-size: 0.75rem;
    color: #aaa;
    margin-left: 6px;
    font-weight: 600;
}

/* AI thinking progress: clean bar with time appended to the header line */
#thinking-indicator {
    white-space: nowrap;
}
#ai-progress {
    width: 100%;
    height: 1.6em;
    accent-color: #C5A46A;
}
#ai-timer {
    font-variant-numeric: tabular-nums;
    color: #FFF4D9;
    white-space: nowrap;
}

/* Game Over Overlay */
#game-over-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--md-sys-color-scrim);
    z-index: 100;
    align-items: center;
    justify-content: center;
}
#game-over-overlay.visible {
    display: flex;
}
.gameover-dialog {
    position: relative;
    background: var(--md-sys-color-surface-container-high);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: 28px;
    padding: 32px 40px;
    text-align: center;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.7);
    max-width: 380px;
    width: 90%;
}
#game-over-overlay .gameover-dialog {
    padding: 20px 24px;
    max-width: 340px;
    max-height: calc(100vh - 32px);
    overflow: auto;
}
/* Game over close button */
.gameover-close {
    position: absolute;
    isolation: isolate;
    overflow: hidden;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    font-size: 2rem;
    color: var(--md-sys-color-on-surface-variant);
    cursor: pointer;
    line-height: 1;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
}
.gameover-close:hover {
    color: var(--md-sys-color-on-surface);
}
.gameover-icon {
    font-size: 2rem;
    margin-bottom: 8px;
}
.gameover-chest[hidden] {
    display: none;
}
.gameover-chest {
    position: relative;
    margin: 8px 0 12px;
}
.chest-button {
    position: relative;
    display: block;
    width: 132px;
    height: 116px;
    margin: 0 auto;
    padding: 0;
    border: 0;
    border-radius: 16px;
    background: radial-gradient(ellipse at 50% 80%, #ffd93d30, transparent 70%);
    color: #ffd93d;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.chest-button:focus {
    outline: 3px solid #fff1a8;
    outline-offset: 5px;
    box-shadow: 0 0 22px #ffd93d80;
}
.chest-button:disabled { cursor: default; }
.chest-base, .chest-lid, .chest-lock {
    position: absolute;
    display: block;
    box-sizing: border-box;
}
.chest-base, .chest-lid {
    left: 15px;
    width: 102px;
    border: 3px solid #e4b749;
    background: linear-gradient(90deg, transparent 17%, #e9bd56 18% 24%, transparent 25% 75%, #e9bd56 76% 82%, transparent 83%),
        repeating-linear-gradient(0deg, #0000 0 17px, #30150980 18px 20px),
        linear-gradient(110deg, #9f5426, #663017 60%, #32190f);
    box-shadow: inset 0 2px 3px #fff3ae90, inset 0 -5px 10px #190b08a0, 0 8px 12px #0005;
}
.chest-base {
    top: 73px;
    height: 37px;
    border-radius: 0 0 12px 12px;
}
.chest-lid {
    top: 46px;
    height: 30px;
    border-radius: 40px 40px 4px 4px;
    transform-origin: 10% 100%;
    transition: transform 550ms cubic-bezier(0.2, 0.7, 0.2, 1);
}
.chest-lock {
    left: 59px;
    top: 68px;
    width: 15px;
    height: 19px;
    border: 2px solid #ffe7a2;
    border-radius: 4px 4px 9px 9px;
    background: radial-gradient(ellipse at 50% 42%, #3b210c 0 3px, transparent 4px), linear-gradient(110deg, #fff0a2, #c78c20);
    box-shadow: 0 2px 4px #0008;
    transition: opacity 150ms;
}
.chest-open .chest-lid { transform: translateY(-32px) rotate(-24deg); }
.chest-open .chest-lock { opacity: 0; }
.gameover-chest-icon {
    position: absolute;
    pointer-events: none;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    font-size: 3.3rem;
    line-height: 1;
}
.gameover-chest-icon[hidden] { display: none; }
.gameover-chest-icon.chest-gold {
    color: #ffd93d;
    text-shadow: 0 -1px 0 #fff5c0, 1px 1px 0 #d39d20, 2px 2px 0 #af7414,
        3px 4px 0 #775010, 0 6px 6px #24100380, 0 0 18px #ffda6090;
    filter: drop-shadow(0 0 9px #ffe8a670);
}
.chest-open .gameover-chest-icon { animation: chest-reveal 750ms ease-out both; }
@keyframes chest-reveal {
    0%, 25% { opacity: 0; transform: translate(-50%, 65px) scale(0.4); }
    75% { opacity: 1; transform: translate(-50%, -6px) scale(1.08); }
    100% { opacity: 1; transform: translate(-50%, 0) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
    .chest-lid, .chest-lock { transition: none; }
    .chest-open .gameover-chest-icon { animation: none; }
}
/* Victory fireworks: repeated DOM volleys of 24-28 class-driven particles.
 * All geometry lives in fw-*/keyframes; no inline styles are used. */
#gameover-fireworks {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
    z-index: 5;
}
#gameover-fireworks[hidden] {
    display: none;
}
.firework-particle {
    position: absolute;
    left: 50%;
    top: 38%;
    display: block;
    margin: 0;
    padding: 0;
    border-radius: 50%;
    opacity: 0;
    pointer-events: none;
}
.fw-0 { width: 6px; height: 6px; background: #ffd93d; animation: fw-burst-0 1.6s ease-out both; }
.fw-1 { width: 5px; height: 5px; background: #fff1a8; animation: fw-burst-1 1.5s ease-out both; }
.fw-2 { width: 7px; height: 7px; background: #e4b749; animation: fw-burst-2 1.7s ease-out both; }
.fw-3 { width: 4px; height: 4px; background: #ffd93d; animation: fw-burst-3 1.4s ease-out both; }
.fw-4 { width: 6px; height: 6px; background: #ff9d5c; animation: fw-burst-4 1.8s ease-out both; }
.fw-5 { width: 5px; height: 5px; background: #fff1a8; animation: fw-burst-5 1.6s ease-out both; }
@keyframes fw-burst-0 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-110px, -80px) scale(0.3); }
}
@keyframes fw-burst-1 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(95px, -70px) scale(0.4); }
}
@keyframes fw-burst-2 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-75px, 60px) scale(0.3); }
}
@keyframes fw-burst-3 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(85px, 55px) scale(0.5); }
}
@keyframes fw-burst-4 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(0, -100px) scale(0.3); }
}
@keyframes fw-burst-5 {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(0, 85px) scale(0.4); }
}
@media (prefers-reduced-motion: reduce) {
    #gameover-fireworks { display: none; }
    .firework-particle { animation: none; }
}
.gameover-chest-label {
    font-size: 1rem;
    margin-bottom: 0;
}
.gameover-dialog h2 {
    font-size: 1.3rem;
    margin-bottom: 6px;
    color: var(--md-sys-color-on-surface);
}
.gameover-dialog p {
    font-size: 1rem;
    color: var(--md-sys-color-on-surface-variant);
    margin-bottom: 12px;
}
.gameover-win .gameover-dialog {
    background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-on-primary-container);
    border-color: #ffd93d;
}
.gameover-loss .gameover-dialog {
    border-color: #ff6b6b;
}
.gameover-draw .gameover-dialog {
    border-color: #888;
}

/* Promotion modal */
#side-modal,
#promotion-modal {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--md-sys-color-scrim);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

#side-modal.visible,
#promotion-modal.visible {
    display: flex;
}

.promo-dialog {
    background: var(--md-sys-color-surface-container-high);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: 28px;
    padding: 20px;
    text-align: center;
    box-shadow: 0 12px 40px rgba(0,0,0,0.6);
}

.promo-dialog h3 {
    margin-bottom: 16px;
    font-size: 1.1rem;
}

.promo-dialog p {
    margin: -8px 0 16px;
    color: var(--md-sys-color-on-surface-variant);
    font-size: 0.9rem;
}

.promo-choices {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-bottom: 14px;
}

.promo-btn {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    width: 60px;
    height: 60px;
    font-size: 2.4rem;
    background: var(--md-sys-color-surface-container-highest);
    border: 1px solid var(--md-sys-color-outline);
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, border-color 0.2s;
}

.promo-btn:hover {
    background: var(--md-sys-color-secondary-container-hover);
    border-color: var(--md-sys-color-primary);
}

#promo-cancel {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    padding: 6px 20px;
    background: transparent;
    color: var(--md-sys-color-primary);
    border: 1px solid var(--md-sys-color-outline);
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
}

#promo-cancel:hover {
    background: rgba(181, 200, 255, 0.12);
    border-color: var(--md-sys-color-primary);
}

/* Scrollbar */
#move-list::-webkit-scrollbar {
    width: 6px;
}

#move-list::-webkit-scrollbar-track {
    background: var(--md-sys-color-surface);
    border-radius: 3px;
}

#move-list::-webkit-scrollbar-thumb {
    background: var(--md-sys-color-outline-variant);
    border-radius: 3px;
}

/* TV remote / D-pad cursor */
.square.cursor {
    outline: 4px solid var(--md-focus-ring);
    outline-offset: -4px;
    z-index: 10;
}

/* Promotion button focused by keyboard */
.promo-btn.promo-focused {
    border-color: var(--md-sys-color-primary) !important;
    box-shadow: 0 0 0 3px rgba(181, 200, 255, 0.6);
}

/* Board focus outline for TV */
#board:focus {
    outline: none;
}

#board:focus-visible {
    outline: 3px solid var(--md-focus-ring);
    outline-offset: 4px;
}

/* Responsive */
@media (max-width: 780px) {
    main {
        flex-direction: column;
        align-items: center;
    }
    .board-container {
        width: 100%;
    }
    #board {
        width: min(90vw, 480px, 100%);
        height: auto;
        aspect-ratio: 1;
    }
    .piece {
        font-size: min(8.5vw, 2.8rem);
    }
    .sidebar {
        width: 100%;
        max-width: 480px;
    }
    /* Captured pieces — mobile */
    .captured-pieces {
        min-height: 24px;
        padding: 2px 4px;
    }
    .captured-piece {
        font-size: 1rem;
    }
    /* Horizontal move strip for mobile */
    .move-history {
        flex: 0 0 auto;
        min-height: 42px;
        max-height: 48px;
        padding: 4px 8px;
        display: flex;
        align-items: center;
        gap: 6px;
        overflow-x: auto;
        overflow-y: hidden;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    .move-history h3 {
        display: none;
    }
    #move-list {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 6px;
        font-size: 0.85rem;
    }
    .move-pair {
        display: inline-flex;
        align-items: center;
        gap: 2px;
        padding: 2px 6px;
        border-radius: 4px;
        background: rgba(255, 255, 255, 0.05);
    }
    .move-number {
        width: auto;
        font-weight: 600;
        margin-right: 2px;
    }
    .move-white, .move-black {
        width: auto;
        min-width: 24px;
    }
}

/* Small phones (320-480px) */
@media (max-width: 480px) {
    main {
        padding: 10px;
        gap: 12px;
    }
    .controls-row {
        gap: 4px;
        padding: 4px;
    }
    .control-btn {
        min-width: 48px;
        min-height: 48px;
        padding: 6px 10px;
        font-size: 0.8rem;
    }
    .btn:not(.control-btn) {
        padding: 6px 12px;
        font-size: 0.8rem;
    }
    .sidebar {
        gap: 8px;
    }
    .move-history {
        max-height: 48px;
        min-height: 42px;
    }
    .promo-btn {
        width: 52px;
        height: 52px;
        font-size: 2rem;
    }
    .gameover-dialog {
        padding: 24px 20px;
        border-radius: 20px;
    }
    .promo-dialog {
        padding: 16px;
        border-radius: 20px;
    }
    .promo-dialog h3 {
        font-size: 1rem;
        margin-bottom: 10px;
    }
    .promo-choices {
        gap: 6px;
        margin-bottom: 10px;
    }
    #board {
        touch-action: manipulation;
    }
}

/* TV mode remote badges */
.tv-mode #btn-history-back::before {
    content: '\1F534  ';
    font-size: 0.8em;
}
.tv-mode #btn-history-fwd::before {
    content: '\1F535  ';
    font-size: 0.8em;
}
.tv-mode #btn-new-game::before {
    content: '\1F7E2  ';
    font-size: 0.8em;
}
.tv-mode .depth-label::before {
    content: '\1F7E1  ';
    font-size: 0.8em;
}

/* Accessibility */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (min-width: 1400px) and (min-height: 721px) {
    .btn {
        padding: 10px 24px;
        font-size: 1rem;
    }
    .depth-label {
        font-size: 1rem;
    }
    /* Controls — TV */
    .control-btn {
        min-width: 60px;
        font-size: 1.2rem;
        padding: 10px 20px;
    }
    .depth-value {
        font-size: 1.3rem;
        min-width: 32px;
    }
    /* TV color keys indicators */
    #btn-history-back::before {
        content: '\1F534  ';
        font-size: 0.8em;
    }
    #btn-history-fwd::before {
        content: '\1F535  ';
        font-size: 0.8em;
    }
    #btn-new-game::before {
        content: '\1F7E2  ';
        font-size: 0.8em;
    }
    .depth-label::before {
        content: '\1F7E1  ';
        font-size: 0.8em;
    }
    .btn.control-focused,
    .btn:focus-visible {
        outline: 4px solid var(--md-focus-ring);
        outline-offset: 3px;
        box-shadow: 0 0 16px rgba(255, 244, 217, 0.45);
    }

    .piece[data-color="white"] {
        color: #fff;
        text-shadow:
            -2px -2px 0 #000,
             2px -2px 0 #000,
            -2px  2px 0 #000,
             2px  2px 0 #000,
             0 0 3px rgba(0,0,0,0.5);
    }

    .piece[data-color="black"] {
        color: #000;
        text-shadow:
            -2px -2px 0 #fff,
             2px -2px 0 #fff,
            -2px  2px 0 #fff,
             2px  2px 0 #fff,
             0 0 4px rgba(255,255,255,0.9);
    }

    /* Coordinates */
    .coord {
        font-size: 0.85rem;
    }

    /* Selected square glow */
    .square.selected {
        box-shadow: inset 0 0 0 4px rgba(255, 215, 0, 0.6);
    }

    /* In-check animation scaled */
    .square.in-check {
        animation: check-flash 0.6s ease-in-out infinite alternate;
    }
    @keyframes check-flash {
        from { box-shadow: inset 0 0 15px rgba(255, 0, 0, 0.5); }
        to { box-shadow: inset 0 0 30px rgba(255, 0, 0, 0.8); }
    }

    /* Sidebar — wider on TV */
    .sidebar {
        flex: 0 1 380px;
        min-width: 300px;
        max-width: 380px;
    }
    #status {
        font-size: 1rem;
        padding: 14px 20px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    #thinking-indicator {
        font-size: 1rem;
        padding: 10px 20px;
    }
    .move-history {
        max-height: 500px;
        padding: 14px;
        overflow-y: auto;
    }
    .move-history h3 {
        font-size: 1rem;
    }
    #move-list {
        font-size: 1.1rem;
    }
    .move-number {
        width: 40px;
    }
    .move-white, .move-black {
        width: 72px;
    }

    /* Promotion modal — larger */
    .promo-dialog {
        padding: 28px;
    }
    .promo-dialog h3 {
        font-size: 1.4rem;
    }
    .promo-dialog p {
        font-size: 1rem;
    }
    .promo-btn {
        width: 80px;
        height: 80px;
        font-size: 3.5rem;
    }
    #promo-cancel {
        padding: 8px 28px;
        font-size: 1rem;
    }

    /* TV remote cursor — thicker outline for TV viewing */
    .square.cursor {
        outline-width: 5px;
    }

    /* Captured pieces — TV */
    .captured-pieces {
        flex: 0 0 auto;
        min-width: 0;
    }
    .captured-piece {
        font-size: 1rem;
    }
    .material-advantage {
        font-size: 0.9rem;
    }

    /* Game over overlay — TV */
    .gameover-dialog {
        padding: 40px 52px;
    }
    .gameover-icon {
        font-size: 5rem;
    }
    .gameover-dialog h2 {
        font-size: 2.2rem;
    }
}

/* Dialogs remain scrollable even when the viewport is exceptionally short. */
@media (max-height: 720px) {
    #game-over-overlay,
    #promotion-modal {
        align-items: flex-start;
        overflow-y: auto;
        padding: 16px;
    }
}

/* Enabled by measured landscape CSS-pixel dimensions, including 781..1399px
 * TVs. No min(), aspect-ratio, dvh, or flex-gap support is needed for fitting.
 * JS sets the explicit board px (the truth); CSS 100vh/100dvh above is a
 * progressive fallback only. The board is vertically centered so the JS
 * overscan reserve leaves equal top/bottom margins instead of touching the
 * physical TV edge. Keep page overflow available rather than clipping
 * impossible tiny viewports.
 */
main.viewport-fit {
    flex: 0 0 auto;
    max-width: 1360px;
    padding: 12px 16px;
    gap: 0;
    align-items: flex-start;
    flex-direction: row;
}
.viewport-fit .board-container {
    flex: 1 1 0;
    min-width: 0;
    max-width: 864px;
    align-items: center;
    justify-content: center;
}
.viewport-fit #board {
    flex: 0 0 auto;
}
.viewport-fit .captured-pieces {
    flex: 0 0 auto;
    width: 100%;
    min-height: 40px;
}
.viewport-fit .sidebar {
    flex: 0 0 300px;
    min-width: 300px;
    max-width: 300px;
    height: 100%;
    min-height: 0;
    margin-left: 16px;
    gap: 0;
    /* Emergency scroll only if non-history content itself exceeds the screen. */
    overflow-y: auto;
}
.viewport-fit .sidebar > * {
    flex: 0 0 auto;
}
.viewport-fit .sidebar > * + * {
    margin-top: 6px;
}
.viewport-fit .move-history {
    flex: 1 1 0;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
}
.viewport-fit .version {
    padding: 4px 0;
}

/* Reduce decorative motion; chess-piece travel always retains its timing. */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        scroll-behavior: auto !important;
    }

    :not(.moving-piece),
    *::before,
    *::after {
        transition: none !important;
    }

    .btn,
    .promo-btn,
    #promo-cancel,
    .gameover-close,
    .square.cursor {
        transform: none !important;
    }
}

.version {
    flex: 0 0 auto;
    text-align: center;
    font-size: 0.75rem;
    color: var(--md-sys-color-outline);
    padding: 8px 0;
    opacity: 0.7;
}

/* Compact sidebar summary between Level and Toolbar: plain text, never interactive. */
#selection-summary {
    margin: 0;
    padding: 2px 4px;
    font-size: 0.8rem;
    color: var(--md-sys-color-on-surface-variant);
    text-align: center;
    white-space: nowrap;
}

/* Toolbar row keeps all five actions on one line. */
.controls-row[aria-label^="Toolbar"] {
    flex-wrap: nowrap;
    gap: 4px;
}
.controls-row[aria-label^="Toolbar"] > .btn {
    min-width: 0;
    padding: 8px 6px;
    font-size: 0.8rem;
    white-space: nowrap;
}
.controls-row[aria-label^="Toolbar"] > #btn-sound {
    flex-grow: 2;
}
#chest-demo-trigger::after {
    content: '.';
}
#ai-details[hidden] { display: none; }
#ai-details { font-size: 0.85rem; }
/* Fixed overlays use measured square rectangles, including flipped boards. */
.moving-piece {
    position: fixed;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 20;
    margin: 0;
}
.capture-arrival {
    position: relative;
    z-index: 21;
    background: rgba(255, 80, 60, 0.8);
    filter: brightness(1.7);
}

/* Etiketych Chess: a compact score desk beside an unadorned playing surface.
 * Sidebar decoration is border-box: it never consumes board fitting space. */
.sidebar {
    padding: 8px 12px 12px;
    border: 1px solid var(--md-sys-color-outline-variant);
    background: var(--md-sys-color-surface-container);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}
.club-heading { padding: 0 4px 4px; }
.club-heading h1 {
    font: normal 2rem/1.15 Georgia, 'Times New Roman', serif;
    letter-spacing: -0.04em;
}
.club-heading h1 span { color: var(--md-sys-color-primary); }
.controls-row {
    padding: 0;
    gap: 8px;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    justify-content: flex-start;
}
#controls-panel { flex-direction: column; align-items: stretch; }
.control-group { gap: 8px; }
.control-group .btn { flex: 1; }
.control-group .depth-label { flex: 1; }
.control-group .depth-label ~ .btn { flex: 0 0 48px; padding: 8px; }
.controls-row > .btn { flex: 1; }
.btn, .promo-btn, #promo-cancel {
    border-radius: 3px;
    border: 1px solid var(--md-sys-color-outline-variant);
    font-family: inherit;
    transition: background 0.15s, color 0.15s;
}
.btn-primary { border-color: var(--md-sys-color-primary); }
.btn[aria-pressed="true"] { border-color: var(--md-sys-color-primary); }
.btn:hover:not(:disabled), .btn:active, .btn.control-focused { transform: none; }
.btn::after, .promo-btn::after, #promo-cancel::after, .gameover-close::after { content: none; }
#status, #thinking-indicator {
    border-radius: 0;
    border: 0;
    border-left: 3px solid var(--md-sys-color-primary);
    background: var(--md-sys-color-surface-container-low);
    box-shadow: none;
    padding: 10px 12px;
    text-align: left;
}
#status { font-family: Georgia, 'Times New Roman', serif; font-size: 0.95rem; line-height: 1.35; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#thinking-indicator { color: var(--md-sys-color-primary); }
#ai-details { margin-top: 8px; color: var(--md-sys-color-on-surface-variant); }
.move-history { border-radius: 0; padding: 12px; box-shadow: none; }
.move-history h3 { font-size: 0.65rem; letter-spacing: 0.15em; border-bottom: 1px solid var(--md-sys-color-outline-variant); padding-bottom: 8px; }
.move-pair { padding: 5px 4px; font-variant-numeric: tabular-nums; }
.version { opacity: 1; text-align: right; font-size: 0.65rem; }
.captured-piece { opacity: 1; }
.captured-piece[data-color="black"] { color: #C8BAA3; text-shadow: 1px 1px 0 #000; }
.material-advantage { color: var(--md-sys-color-primary); }
#board { border-radius: 0; border-color: #68543B; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35); }
.promo-dialog, .gameover-dialog {
    border-radius: 4px;
    border-top: 3px solid var(--md-sys-color-primary);
    max-width: 420px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
}
.promo-dialog h3, .gameover-dialog h2 { font-family: Georgia, 'Times New Roman', serif; font-weight: normal; }
.btn:focus-visible, .btn.control-focused, .promo-btn:focus-visible,
.promo-btn.promo-focused, #promo-cancel:focus-visible, .gameover-close:focus-visible {
    outline: 3px solid #FFF4D9;
    outline-offset: 2px;
    box-shadow: 0 0 0 7px #171512;
}
.square.cursor { outline: 3px solid #FFF4D9; outline-offset: -4px; box-shadow: inset 0 0 0 7px #171512; }
#board:focus-visible { outline: 3px solid #FFF4D9; outline-offset: 2px; box-shadow: 0 0 0 7px #171512; }
@media (min-width: 781px) and (orientation: portrait) {
    main { flex-direction: column; align-items: center; }
    .sidebar { width: 480px; max-width: 100%; }
}

/* Landscape (short viewports) */
@media (max-height: 500px) and (orientation: landscape) {
    main {
        flex-direction: row;
        align-items: center;
        padding: 8px 16px;
        padding-top: max(8px, env(safe-area-inset-top, 8px));
        padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
        padding-left: max(16px, env(safe-area-inset-left, 16px));
        padding-right: max(16px, env(safe-area-inset-right, 16px));
        gap: 12px;
        max-width: 100%;
    }
    .board-container {
        flex: 0 0 auto;
    }
    .sidebar {
        flex: 1 1 280px;
        max-width: 340px;
        max-height: calc(100vh - 16px);
        overflow-y: auto;
        gap: 6px;
        padding: 8px;
    }
    .club-heading {
        padding: 0 0 4px;
    }
    .club-heading h1 {
        font-size: 1.2rem;
    }
    #status {
        padding: 6px 10px;
        font-size: 0.95rem;
    }
}

/* TV legibility (10-foot UI): the unconditional compact theme above must not
 * shrink the status line on TV. This later equal-specificity rule restores the
 * pre-1.18 TV size; the thinking label, dots and timer inherit it. */
@media (min-width: 1400px) and (min-height: 721px) {
    #status {
        font-size: 1.2rem;
    }
}

