/* ===== CHESS BOARD STYLES ===== */

/* Game Header */
.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(10px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 10px calc(16px + env(safe-area-inset-left));
    background: rgba(0, 0, 0, 0.4);
    border-bottom: 1px solid var(--card-border);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 10;
    flex-shrink: 0;
}

.game-title {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 2px;
    color: var(--accent);
}

.header-actions {
    display: flex;
    gap: 8px;
}

/* Game Layout */
.game-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
    gap: 0;
}

/* Board Area */
.board-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 12px;
    gap: 8px;
    overflow: hidden;
}

/* Player Info */
.player-info {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    max-width: 480px;
    padding: 8px 12px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    position: relative;
}

.player-info.active-player {
    border-color: var(--accent);
    background: rgba(201, 168, 76, 0.08);
}

.player-avatar {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.05);
    flex-shrink: 0;
    overflow: hidden;
}

#avatar-black {
    background: rgba(255, 255, 255, 0.14);
}

#avatar-white {
    background: rgba(255, 255, 255, 0.16);
}

.player-details {
    flex: 1;
}

.player-name {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
}

.player-captured {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
    min-height: 16px;
}

.player-timer {
    font-size: 18px;
    font-weight: 800;
    font-family: monospace;
    color: var(--accent);
    min-width: 60px;
    text-align: right;
}

.player-timer.low-time {
    color: var(--danger);
    animation: timerPulse 1s ease infinite;
}

.turn-indicator {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    opacity: 0;
}

.active-player .turn-indicator {
    opacity: 1;
}

/* Board Container */
.board-container {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Chess Board */
.chess-board {
    --board-frame-size: 16px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: min(92vw, calc(58 * var(--app-vh)), 520px);
    height: min(92vw, calc(58 * var(--app-vh)), 520px);
    aspect-ratio: 1;
    /* Keep the decorative frame outside the 8×8 grid. A border shares the
       board's fixed height and can overlap the first and last ranks. */
    padding: var(--board-frame-size);
    border-radius: 6px;
    background:
        linear-gradient(var(--frame-fill), var(--frame-fill)) content-box,
        var(--frame-grain),
        var(--frame-decor) padding-box;
    box-shadow: var(--frame-shadow);
    /* overflow: hidden; */
    position: relative;
    flex-shrink: 0;
    transform: perspective(1200px) rotateX(6deg);
    transform-origin: center center;
}

.chess-board::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 5;
    border: 2px solid var(--frame-ring);
    box-shadow: var(--frame-ring-shadow);
    pointer-events: none;
}

/* Chess Square */
.sq {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease;
    overflow: hidden;
    z-index: 1;
}

.sq.light {
    background-color: var(--light-sq);
    box-shadow:
        inset 1px 1px 0 rgba(255, 247, 212, 0.52),
        inset -1px -1px 0 rgba(98, 52, 21, 0.16);
}

.sq.dark {
    background-color: var(--dark-sq);
    box-shadow:
        inset 1px 1px 0 rgba(255, 213, 137, 0.17),
        inset -1px -1px 0 rgba(20, 9, 4, 0.3);
}

[data-theme="wood"] .sq.light {
    background-image:
        linear-gradient(145deg, rgba(255, 247, 207, 0.34), transparent 40%, rgba(113, 62, 21, 0.12)),
        radial-gradient(ellipse at 18% 28%, rgba(255, 241, 186, 0.18), transparent 48%),
        repeating-linear-gradient(2deg, transparent 0 15px, rgba(255, 245, 202, 0.075) 16px, transparent 19px),
        repeating-linear-gradient(178deg, rgba(117, 71, 30, 0.04) 0 1px, transparent 2px 13px),
        repeating-linear-gradient(88deg, transparent 0 31px, rgba(255, 238, 181, 0.05) 32px, transparent 35px);
}

[data-theme="wood"] .sq.dark {
    background-image:
        linear-gradient(145deg, rgba(255, 210, 133, 0.12), transparent 40%, rgba(22, 9, 3, 0.26)),
        radial-gradient(ellipse at 75% 72%, rgba(207, 135, 66, 0.12), transparent 46%),
        repeating-linear-gradient(-2deg, transparent 0 17px, rgba(220, 153, 78, 0.1) 18px, transparent 21px),
        repeating-linear-gradient(179deg, rgba(23, 10, 4, 0.12) 0 1px, transparent 2px 15px),
        repeating-linear-gradient(91deg, transparent 0 28px, rgba(244, 186, 111, 0.06) 29px, transparent 33px);
}

.sq.selected,
.sq.last-move,
.sq.in-check {
    background-color: transparent !important;
}

.sq.selected::before,
.sq.last-move::before,
.sq.in-check::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 2;
}

.sq.selected::before {
    background: var(--selected-sq);
}

.sq.last-move::before {
    background: var(--last-move);
}

.sq.in-check::before {
    background: var(--check-sq);
}

.sq.legal-move::after {
    content: '';
    position: absolute;
    width: 30%;
    height: 30%;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 50%;
    pointer-events: none;
    z-index: 3;
}

.sq.legal-capture::after {
    content: '';
    position: absolute;
    inset: 0;
    border: 4px solid rgba(0, 0, 0, 0.3);
    border-radius: 50%;
    pointer-events: none;
    z-index: 3;
}

.sq:hover:not(.selected) {
    filter: brightness(1.1);
}

/* Coordinate labels */
.sq .coord-rank {
    position: absolute;
    bottom: 2px;
    left: 3px;
    font-size: 10px;
    font-weight: 700;
    opacity: 0.7;
    pointer-events: none;
    line-height: 1;
    z-index: 4;
}

.sq .coord-file {
    position: absolute;
    top: 2px;
    right: 3px;
    font-size: 10px;
    font-weight: 700;
    opacity: 0.7;
    pointer-events: none;
    line-height: 1;
    z-index: 4;
}

.sq.light .coord-rank,
.sq.light .coord-file {
    color: var(--dark-sq);
}

.sq.dark .coord-rank,
.sq.dark .coord-file {
    color: var(--light-sq);
}

/* Chess Piece — always SVG, never Unicode glyphs (iOS turns those into
   colour emoji and ignores fill, making both sides look identical). */
.piece {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    pointer-events: none;
    z-index: 1;
}

.piece-svg {
    width: 91%;
    height: 91%;
    display: block;
    filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.58)) drop-shadow(0 1px 1px rgba(0, 0, 0, 0.4));
}

.three-board-canvas {
    position: absolute;
    /* Match the grid's content box, not the ornamental frame. */
    top: var(--board-frame-size);
    left: var(--board-frame-size);
    width: calc(100% - (var(--board-frame-size) * 2));
    height: calc(100% - (var(--board-frame-size) * 2));
    z-index: 2;
    pointer-events: none;
}

.three-board-error {
    position: absolute;
    inset: 25% 8%;
    z-index: 5;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgba(18, 20, 24, 0.94);
    color: #fff;
    text-align: center;
    font-size: 14px;
    line-height: 1.5;
}

/* Captured-piece tray */
.pc {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 0;
}

.pc-mini {
    width: 15px;
    height: 15px;
    margin-right: 1px;
    opacity: 0.9;
}

.pc-avatar {
    width: 74%;
    height: 74%;
}

.pc-hero {
    width: 100%;
    height: 100%;
}

.pc-result {
    width: 64px;
    height: 64px;
}

.pc-inline {
    width: 1.1em;
    height: 1.1em;
    vertical-align: -0.22em;
    margin-right: 6px;
}

/* Piece move animation */
@keyframes pieceMove {
    0% {
        transform: scale(1.15);
    }

    100% {
        transform: scale(1);
    }
}

.piece-moved {
    animation: pieceMove 0.2s ease;
}

/* Capture animation */
@keyframes capturePop {
    0% {
        transform: scale(1.3);
        opacity: 1;
    }

    100% {
        transform: scale(0);
        opacity: 0;
    }
}

/* Check pulse */
@keyframes checkPulse {

    0%,
    100% {
        background-color: rgba(255, 0, 0, 0.4);
    }

    50% {
        background-color: rgba(255, 0, 0, 0.7);
    }
}

.sq.in-check::before {
    animation: checkPulse 0.8s ease infinite;
}

/* Timer pulse */
@keyframes timerPulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* Promotion choices */
.promotion-choices {
    display: flex;
    gap: 12px;
    justify-content: center;
    margin-top: 16px;
}

.promo-piece {
    width: 64px;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background: var(--card-bg);
    border: 2px solid var(--card-border);
    border-radius: var(--radius-sm);
    transition: all 0.2s ease;
}

.promo-piece .piece-svg {
    width: 84%;
    height: 84%;
}

.promo-piece:hover {
    background: rgba(201, 168, 76, 0.2);
    border-color: var(--accent);
    transform: scale(1.1);
}

/* Reliable touch input on mobile/tablet */
.chess-board {
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.sq {
    touch-action: none;
}
