/* ===== CSS Variables & Design Tokens ===== */
:root {
    /* Color Palette - Premium Wood Theme */
    --bg-primary: #e0c399;
    --bg-secondary: #d1b287;
    --bg-tertiary: #a47c50;
    --bg-card: rgba(85, 50, 30, 0.9);

    --text-primary: #2d1810;
    --text-secondary: #4a3020;
    --text-muted: #6b4a35;
    --text-light: #f5f5dc;

    --accent-primary: #8b4513;
    --accent-secondary: #a0522d;
    --accent-glow: rgba(139, 69, 19, 0.4);

    --success: #228b22;
    --warning: #daa520;
    --danger: #b22222;

    /* Board Colors */
    --board-light: #f0d9b5;
    --board-dark: #b58863;
    --board-highlight: rgba(255, 255, 80, 0.5);
    --board-check: rgba(255, 0, 0, 0.5);
    --board-last-move: rgba(155, 199, 0, 0.41);

    /* Piece Colors */
    --piece-white-fill: #f8f9fa;
    --piece-white-stroke: #212529;
    --piece-black-fill: #212529;
    --piece-black-stroke: #f8f9fa;

    /* Spacing */
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --spacing-xl: 2rem;
    --spacing-2xl: 3rem;

    /* Border Radius */
    --radius-sm: 0.375rem;
    --radius-md: 0.75rem;
    --radius-lg: 1rem;
    --radius-xl: 1.5rem;
    --radius-full: 50%;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
    --transition-slow: 400ms ease;

    /* Shadows */
    --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.3);
    --shadow-glow: 0 0 20px var(--accent-glow);
    --shadow-inset: inset 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* ===== Reset & Base Styles ===== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

html {
    font-size: 16px;
}

body {
    font-family: 'Outfit', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    /* Wood grain texture effect */
    background-image:
        repeating-linear-gradient(90deg,
            transparent,
            transparent 2px,
            rgba(139, 90, 43, 0.1) 2px,
            rgba(139, 90, 43, 0.1) 4px),
        repeating-linear-gradient(0deg,
            transparent,
            transparent 50px,
            rgba(160, 82, 45, 0.08) 50px,
            rgba(160, 82, 45, 0.08) 100px),
        linear-gradient(180deg, #d4a574 0%, #c49a6c 50%, #b8916a 100%);
}

/* ===== Screen Management ===== */
.screen {
    display: none;
    min-height: 100vh;
    padding: var(--spacing-sm);
}

.screen.active {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===== Menu Screen ===== */
.menu-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-2xl);
    max-width: 400px;
    width: 100%;
}

.logo-container {
    text-align: center;
}

.logo-icon {
    width: 120px;
    height: 120px;
    margin: 0 auto var(--spacing-lg);
    color: var(--text-primary);
    animation: float 3s ease-in-out infinite;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

.logo-svg {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
}

.game-title {
    font-size: 3.5rem;
    font-weight: 700;
    color: var(--text-primary);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
    letter-spacing: 0.05em;
}

.game-subtitle {
    color: var(--text-secondary);
    font-size: 1.1rem;
    margin-top: var(--spacing-sm);
    font-weight: 500;
}

.menu-options {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    width: 100%;
}

.menu-btn {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    padding: var(--spacing-lg) var(--spacing-xl);
    border: none;
    border-radius: var(--radius-lg);
    font-family: inherit;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-normal);
    position: relative;
    overflow: hidden;
    box-shadow: var(--shadow-md);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.menu-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
}

.menu-btn:hover::before {
    left: 100%;
}

.menu-btn.primary {
    background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    color: white;
    box-shadow: var(--shadow-glow), var(--shadow-md);
}

.menu-btn.primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 30px var(--accent-glow), var(--shadow-lg);
}

.menu-btn.tertiary {
    background: rgba(255, 255, 255, 0.3);
    color: var(--text-primary);
    border: 1px solid var(--accent-secondary);
}

.menu-btn.tertiary:hover {
    background: rgba(255, 255, 255, 0.5);
}

.btn-icon {
    font-size: 1.4rem;
}

/* ===== Settings Screen ===== */
.settings-container {
    max-width: 500px;
    width: 100%;
    padding: var(--spacing-lg);
}

.back-btn {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 1rem;
    cursor: pointer;
    padding: var(--spacing-sm) 0;
    margin-bottom: var(--spacing-lg);
    transition: color var(--transition-fast);
    font-weight: 600;
}

.back-btn:hover {
    color: var(--text-primary);
}

.settings-title {
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: var(--spacing-xl);
    text-align: center;
    color: var(--text-primary);
}

.settings-group {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: var(--spacing-lg);
    margin-bottom: var(--spacing-lg);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(139, 69, 19, 0.3);
    box-shadow: var(--shadow-md), var(--shadow-inset);
}

.settings-group h3 {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: var(--spacing-md);
}

.player-inputs {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.player-input {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
}

.player-color {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    flex-shrink: 0;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.player-color.white {
    background: #fff;
}

.player-color.black {
    background: #000;
}

.player-input input {
    flex: 1;
    padding: var(--spacing-sm) var(--spacing-md);
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid transparent;
    border-radius: var(--radius-md);
    font-family: inherit;
    font-size: 1rem;
    color: var(--text-primary);
    transition: all var(--transition-fast);
}

.player-input input:focus {
    outline: none;
    border-color: var(--accent-primary);
}

/* Timer Toggle */
.timer-toggle {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin-bottom: var(--spacing-md);
}

.toggle-switch {
    position: relative;
    width: 52px;
    height: 28px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 28px;
    transition: var(--transition-normal);
}

.slider::before {
    content: '';
    position: absolute;
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background: #f5f5dc;
    border-radius: var(--radius-full);
    transition: var(--transition-normal);
}

.toggle-switch input:checked+.slider {
    background: var(--success);
}

.toggle-switch input:checked+.slider::before {
    transform: translateX(24px);
}

.toggle-label {
    color: var(--text-light);
    font-size: 0.95rem;
}

.timer-options {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    opacity: 0.5;
    pointer-events: none;
    transition: opacity var(--transition-normal);
}

.timer-options.active {
    opacity: 1;
    pointer-events: auto;
}

.timer-hint {
    color: rgba(245, 245, 220, 0.8);
    font-size: 0.85rem;
    margin-bottom: var(--spacing-xs);
}

.timer-buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: var(--spacing-sm);
}

.timer-btn {
    padding: var(--spacing-sm) 0;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: var(--radius-md);
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text-light);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.timer-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.timer-btn.active {
    border-color: var(--warning);
    background: rgba(218, 165, 32, 0.2);
    color: var(--warning);
    font-weight: 600;
}

.increment-toggle {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    padding-top: var(--spacing-sm);
    border-top: 1px solid rgba(255, 255, 255, 0.15);
}

.toggle-switch.small {
    width: 40px;
    height: 22px;
}

.toggle-switch.small .slider::before {
    height: 16px;
    width: 16px;
}

.toggle-switch.small input:checked+.slider::before {
    transform: translateX(18px);
}


/* Start Game Button from Settings */
.start-game-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-md);
    width: 100%;
    padding: var(--spacing-lg);
    background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    border: none;
    border-radius: var(--radius-lg);
    font-family: inherit;
    font-size: 1.2rem;
    font-weight: 600;
    color: white;
    cursor: pointer;
    transition: all var(--transition-normal);
    box-shadow: var(--shadow-glow), var(--shadow-md);
    margin-top: var(--spacing-lg);
}

.start-game-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 30px var(--accent-glow), var(--shadow-lg);
}

/* ===== Game Screen ===== */
#game-screen {
    padding: var(--spacing-sm) var(--spacing-md);
    justify-content: flex-start;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    max-width: 900px;
    margin-bottom: var(--spacing-sm);
}

.icon-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-card);
    border: none;
    border-radius: var(--radius-md);
    font-size: 1.1rem;
    color: var(--text-light);
    cursor: pointer;
    transition: all var(--transition-fast);
    box-shadow: var(--shadow-sm);
}

.icon-btn:hover {
    background: var(--accent-primary);
    color: white;
}

.turn-info {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.4);
    padding: var(--spacing-sm) var(--spacing-lg);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.game-content {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: var(--spacing-md);
    width: 100%;
    max-width: 1100px;
    flex: 1;
    min-height: 0;
    /* Important for flex child to shrink */
}

/* Game Board */
.board-container {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    /* For overlays */
}

.chess-board {
    width: min(75vh, 75vw);
    /* Slightly smaller to fit coords */
    height: min(75vh, 75vw);
    background-color: var(--board-dark);
    display: grid;
    grid-template-columns: 20px repeat(8, 1fr) 20px;
    grid-template-rows: 20px repeat(8, 1fr) 20px;
    border: 8px solid #5d3a24;
    border-radius: 4px;
    box-shadow: var(--shadow-lg);
    user-select: none;
    /* Ensure Fixed Size regardless of content */
    table-layout: fixed;
}

/* Coordinates */
.coord {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
    color: #f5f5dc;
    background: #5d3a24;
}

/* Board Squares */
.square {
    width: 100%;
    height: 100%;
    /* Aspect ratio hack for grid items if needed, but grid usually handles it */
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    font-size: min(7vh, 7vw);
    /* Responsive piece size */
    cursor: pointer;
    overflow: hidden;
    /* Prevent spill */
}

/* Piece Styling */
.piece {
    line-height: 1;
    z-index: 10;
}

.piece.white {
    color: #ffffff;
    text-shadow:
        0 0 2px #000,
        1px 1px 0 #000,
        -1px -1px 0 #000,
        1px -1px 0 #000,
        -1px 1px 0 #000;
}

.piece.black {
    color: #000000;
    text-shadow: 0 0 2px rgba(255, 255, 255, 0.5);
    /* Subtle light glow for dark squares */
}

.square.light {
    background-color: var(--board-light);
}

.square.dark {
    background-color: var(--board-dark);
}

.square.selected {
    background-color: var(--board-highlight) !important;
}

.square.last-move {
    background-color: var(--board-last-move) !important;
}

.square.check {
    background: radial-gradient(circle at center, var(--board-check), transparent 70%);
}

.square.possible-move::after {
    content: '';
    width: 25%;
    height: 25%;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
}

.square.possible-capture::after {
    content: '';
    width: 80%;
    height: 80%;
    border: 4px solid rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    background: transparent;
}


/* Player Panels */
.player-panel {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    padding: var(--spacing-md);
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    width: 200px;
    /* Fixed width */
    backdrop-filter: blur(5px);
    box-shadow: var(--shadow-md);
    transition: all var(--transition-normal);
    border: 2px solid transparent;
}

.player-panel.active {
    border-color: var(--warning);
    box-shadow: 0 0 15px rgba(218, 165, 32, 0.3), var(--shadow-md);
}

.player-info {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-xs);
}

.player-avatar {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    background-color: #eee;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: bold;
}

.player-avatar.white-piece-icon {
    background-color: #fff;
    color: #000;
}

.player-avatar.black-piece-icon {
    background-color: #333;
    color: #fff;
}

.player-name {
    color: var(--text-light);
    font-weight: 600;
    font-size: 1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.material-balance {
    font-size: 0.8rem;
    color: var(--text-light);
    opacity: 0.8;
    height: 1.2em;
    /* Keep layout stable */
}

.captured-pieces {
    background: #f0f0f0;
    /* Very bright gray */
    /* Lighter background */
    border-radius: var(--radius-sm);
    padding: var(--spacing-sm);
    min-height: 80px;
    /* Larger area */
    border: 1px solid rgba(255, 255, 255, 0.5);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.captured-label {
    display: block;
    font-size: 0.8rem;
    text-transform: uppercase;
    color: var(--text-primary);
    /* Darker text for visibility */
    margin-bottom: 4px;
    font-weight: 600;
}

.captured-container {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    min-height: 40px;
}

.captured-piece {
    width: 30px;
    /* Larger pieces */
    height: 30px;
    font-size: 24px;
    line-height: 1.2;
    display: flex;
    align-items: center;
    justify-content: center;
    filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.3));
}

.timer-display {
    background: rgba(0, 0, 0, 0.4);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--radius-md);
    text-align: center;
    margin-top: auto;
}

.timer-text {
    font-family: 'Courier New', monospace;
    /** Monospace for timer to prevent jumping */
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-light);
}

.player-panel.low-time .timer-text {
    color: #ff6b6b;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }

    100% {
        opacity: 1;
    }
}


/* Game Status Bar */
.game-status {
    width: 100%;
    margin-top: var(--spacing-md);
    text-align: center;
}

.status-indicator {
    width: 100%;
    height: 16px;
    /* Thicker bar */
    border-radius: 8px;
    /* Rounded pill shape */
    margin-bottom: var(--spacing-sm);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: background-color var(--transition-normal);
    border: 2px solid rgba(0, 0, 0, 0.1);
    /* Subtle default border */
}

.status-text {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 1.2rem;
}

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-md);
    backdrop-filter: blur(4px);
}

.modal.active {
    display: flex;
    animation: fadeIn 0.3s ease;
}

.modal-content {
    background: var(--bg-primary);
    padding: var(--spacing-xl);
    border-radius: var(--radius-lg);
    max-width: 500px;
    width: 100%;
    position: relative;
    box-shadow: var(--shadow-lg);
    border: 4px solid var(--accent-primary);
}

.close-modal {
    position: absolute;
    top: var(--spacing-md);
    right: var(--spacing-md);
    background: transparent;
    border: none;
    font-size: 1.5rem;
    color: var(--text-secondary);
    cursor: pointer;
}

.modal h2 {
    color: var(--accent-primary);
    margin-bottom: var(--spacing-lg);
    text-align: center;
    font-size: 1.8rem;
}

.rules-content {
    max-height: 80vh;
    overflow-y: auto;
}

.rules-section {
    margin-bottom: var(--spacing-lg);
}

.rules-section h3 {
    font-size: 1.1rem;
    color: var(--accent-secondary);
    margin-bottom: var(--spacing-sm);
}

.rules-section ul {
    list-style-position: inside;
    margin-left: var(--spacing-sm);
}

.modal-btn {
    width: 100%;
    padding: var(--spacing-md);
    border: none;
    border-radius: var(--radius-md);
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: var(--spacing-sm);
    transition: all var(--transition-normal);
}

.modal-btn.primary {
    background: var(--accent-primary);
    color: white;
}

.modal-btn:hover {
    filter: brightness(1.1);
    transform: translateY(-1px);
}

.modal-btn.secondary {
    background: transparent;
    border: 2px solid var(--accent-primary);
    color: var(--accent-primary);
}

.modal-btn.tertiary {
    background: transparent;
    color: var(--text-muted);
    font-size: 1rem;
}

/* Promotion Modal Special */
.promotion-options {
    display: flex;
    justify-content: space-around;
    gap: var(--spacing-md);
    margin-top: var(--spacing-lg);
}

.promotion-piece {
    font-size: 3rem;
    cursor: pointer;
    transition: transform var(--transition-fast);
    padding: var(--spacing-sm);
    background: rgba(255, 255, 255, 0.5);
    border-radius: var(--radius-md);
}

.promotion-piece:hover {
    transform: scale(1.1);
    background: rgba(255, 255, 255, 0.8);
}

/* Winner info */
.winner-display {
    text-align: center;
    margin-bottom: var(--spacing-lg);
}

.winner-icon {
    font-size: 4rem;
    text-align: center;
    margin-bottom: var(--spacing-md);
    animation: bounce 2s infinite;
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translateY(0);
    }

    40% {
        transform: translateY(-20px);
    }

    60% {
        transform: translateY(-10px);
    }
}

/* Game Over Overlay (Not Modal) */
/* Game Over Overlay (Not Modal) */
#gameover-modal {
    position: absolute;
    bottom: 50px;
    /* Position at bottom */
    top: auto;
    left: 50%;
    transform: translateX(-50%);
    width: auto;
    min-width: 300px;
    height: auto;
    background: rgba(255, 255, 255, 0.95);
    border: 4px solid var(--accent-primary);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    padding: var(--spacing-lg);
    z-index: 200;
    /* Above board */
    backdrop-filter: none;
    border-radius: var(--radius-lg);
}

#gameover-modal.active {
    display: flex;
    flex-direction: column;
    /* Ensure vertical layout */
    animation: slideUp 0.5s ease-out;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translate(-50%, 100%);
    }

    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}


/* Responsive */
@media (max-width: 800px) {
    .game-content {
        flex-direction: column;
        align-items: center;
    }

    .player-panel {
        flex-direction: row;
        width: 100%;
        max-width: min(80vh, 80vw);
        padding: var(--spacing-sm);
        height: auto;
    }

    .player-info {
        flex: 1;
        margin-bottom: 0;
    }

    .captured-pieces {
        display: none;
        /* Hide on small screens or make smaller */
    }

    .timer-display {
        margin-top: 0;
        padding: 4px 8px;
    }

    .timer-text {
        font-size: 1.2rem;
    }
}