* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
:root {
    color-scheme: light dark;
    --bg-primary: #667eea; /* primary gradient color (light theme) */
    --bg-secondary: #764ba2; /* secondary gradient color (light theme) */
    --container-bg: #ffffff;
    --text-color: #333333;
    --footer-fg: rgba(255, 255, 255, 0.8);
    /* Overlay opacity controls - change these to adjust highlight transparency
       without affecting the board or container backgrounds. Values are 0.0-1.0 */
    --overlay-alpha: 0.85; /* default alpha for generic highlights */
    --last-from-alpha: 0.85;
    --last-to-alpha: 0.88;
    --selected-alpha: 0.39;
    --valid-move-alpha: 0.55;
    /* RGB color tokens for highlights (use with rgba(var(--...), alpha)) */
    --last-rgb: 255, 244, 153; /* pale yellow -- last move */
    --last-from-rgb: 255, 230, 128; /* slightly warmer for from-square */
    --last-to-rgb: 255, 244, 153; /* to-square pale yellow */
    --selected-rgb: 59, 130, 246; /* clear blue for selection */
    --valid-rgb: 76, 175, 80; /* green for valid moves */
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-primary: #0b1220; /* deep navy */
        --bg-secondary: #192432; /* slate blue/ink */
        --container-bg: rgba(18, 22, 30, 0.75);
        --text-color: #e6eef8;
        --footer-fg: rgba(230, 238, 248, 0.85);
        --overlay-alpha: 0.5;
        --last-from-alpha: 0.5;
        --last-to-alpha: 0.55;
        --selected-alpha: 0.28;
        --valid-move-alpha: 0.45;
        --last-rgb: 255, 214, 77;
        --last-from-rgb: 255, 190, 60;
        --last-to-rgb: 255, 214, 77;
        --selected-rgb: 37, 99, 235;
        --valid-rgb: 102, 187, 106;
    }
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(
        135deg,
        var(--bg-primary) 0%,
        var(--bg-secondary) 100%
    );
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.container {
    background: var(--container-bg);
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 800px;
}

h1 {
    text-align: center;
    color: var(--text-color);
    margin-bottom: 20px;
    font-size: 2.5em;
}

.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    gap: 10px;
    flex-wrap: wrap;
}

.turn-indicator {
    font-size: 1.2em;
    font-weight: bold;
    color: #555;
}

#current-turn {
    color: #667eea;
}

.seed-info {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9em;
}

.seed-info label {
    font-weight: bold;
    color: var(--text-color);
}

.seed-info input {
    padding: 5px 10px;
    border: 2px solid #ddd;
    border-radius: 5px;
    width: 120px;
    font-size: 0.95em;
}

.seed-info input:focus {
    outline: none;
    border-color: var(--bg-primary);
}

.seed-info button {
    padding: 5px 15px;
    background: var(--bg-primary);
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-weight: bold;
    transition: background 0.3s;
}

.seed-info button:hover {
    background: #5568d3;
}

#current-seed {
    color: var(--bg-secondary);
    font-weight: bold;
    font-size: 0.95em;
}

button {
    padding: 10px 20px;
    background: var(--bg-primary);
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1em;
    transition: background 0.3s;
}

button:hover {
    background: var(--bg-secondary);
}

.ai-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: #f0f0f0;
    border-radius: 5px;
}

.ai-controls label {
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    font-size: 0.9em;
}

.ai-controls input[type='checkbox'] {
    cursor: pointer;
}

.ai-controls select {
    padding: 5px 10px;
    border: 1px solid #ccc;
    border-radius: 3px;
    background: white;
    cursor: pointer;
}

.mode-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 20px;
}

.multiplayer-controls {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    justify-content: center;
}

.multiplayer-controls button {
    padding: 12px 24px;
    font-size: 1.1em;
}

.multiplayer-controls button:disabled {
    background: #ccc;
    cursor: not-allowed;
}

footer {
    text-align: center;
    padding: 20px;
    color: var(--footer-fg);
    font-size: 0.9em;
    margin-top: auto;
}

footer a {
    color: var(--footer-fg);
    text-decoration: none;
}

footer a:hover {
    text-decoration: underline;
}

#board-container {
    position: relative;
    width: 600px;
    height: 600px;
    margin: 0 auto;
    margin-bottom: 30px;
    overflow: visible;
}

/* Opponent status badge shown above the board to indicate human/AI/search */
.opponent-status,
#opponent-status {
    /* Use normal document flow so HUD displays above the board when moved in the DOM */
    position: relative;
    margin: 0 auto 12px auto; /* sit above the board and create spacing */
    background: rgba(255, 255, 255, 0.95);
    color: #333;
    padding: 6px 10px;
    border-radius: 18px;
    font-size: 1em;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
    z-index: 5;
    min-width: 320px;
    display: flex;
    align-items: center;
    gap: 12px;
    justify-content: center;
}

/* HUD bubble base */
.hud-bubble {
    background: rgba(255, 255, 255, 0.98);
    padding: 8px 12px;
    border-radius: 14px;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.12);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: #222;
}

.hud-center {
    width: 68px;
    height: 68px;
    border-radius: 50%;
    padding: 0;
}

.opponent-icon {
    font-size: 1.9em;
}

.hud-connection {
    min-width: 140px;
    padding: 8px 14px;
    font-weight: 600;
}

.hud-thinking {
    min-width: 110px;
    flex-direction: column;
    padding: 8px 10px;
}

#thinking-bubble {
    font-size: 1.1em;
}

.clock {
    font-size: 0.9em;
    color: #666;
}

.clock-pair {
    display: flex;
    gap: 8px;
}
.clock-pair .clock {
    min-width: 64px;
    text-align: center;
}

.hud-toggle {
    background: transparent;
    border: none;
    padding: 6px 8px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1.2em;
}
.hud-toggle:hover {
    background: rgba(0, 0, 0, 0.04);
}

/* Toggle button color states */
.hud-toggle.search {
    background: linear-gradient(180deg, #3b82f6 0%, #2563eb 100%); /* blue */
    color: white;
    box-shadow: 0 6px 12px rgba(59, 130, 246, 0.18);
}
.hud-toggle.search:hover {
    filter: brightness(1.03);
}

.hud-toggle.cancel {
    background: linear-gradient(180deg, #ef4444 0%, #dc2626 100%); /* red */
    color: white;
    box-shadow: 0 6px 12px rgba(239, 68, 68, 0.18);
}
.hud-toggle.cancel:hover {
    filter: brightness(0.98);
}

/* Movement overlay removed per user request. */

#board {
    position: relative;
    z-index: 2;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 0;
    width: 600px;
    height: 600px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    margin: 0 auto;
}

.square {
    width: 75px;
    height: 75px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2em;
    cursor: pointer;
    position: relative;
    transition: background 0.2s;
}

.square.light {
    background: #a6a6a6;
}

.square.dark {
    background: #595959;
}

.square.selected {
    background: rgba(var(--selected-rgb), var(--selected-alpha)) !important;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
}

.square.valid-move {
    background: rgba(var(--valid-rgb), var(--valid-move-alpha)) !important;
    box-shadow: inset 0 0 15px rgba(0, 100, 0, 0.3);
}

/* Threat highlight when hovering: translucent orange overlay inside the square */
.square.threat::after {
    content: '';
    position: absolute;
    inset: 0px;
    border-radius: 0px;
    background: rgba(255, 0, 0, 0);
    pointer-events: none;
    z-index: 6;
}

.move-dot {
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 0%;
    background: rgba(100, 100, 100, 0.6);
    pointer-events: none;
    z-index: 5;
}

/* Color-coded move dots */
.move-dot.move-both {
    background: rgba(100, 100, 100, 0.6); /* Grey for both move & capture */
}

.move-dot.move-capture-only {
    background: rgba(255, 50, 50, 0.7); /* Red for capture only */
}

.move-dot.move-move-only {
    background: rgba(50, 255, 50, 0.7); /* Green for move only */
}

.piece {
    user-select: none;
    pointer-events: none;
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.mode-settings {
    text-align: center;
    margin-top: 20px;
}

/* Advanced controls: hidden by default and revealed by Advanced tab */
.advanced-controls {
    display: none;
}

/* When body has .advanced-open, reveal advanced controls. Keep layout sensible. */
body.advanced-open .advanced-controls {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 12px;
    flex-wrap: wrap;
}

.advanced-bar {
    display: flex;
    justify-content: flex-end;
    margin-top: 8px;
}

.advanced-toggle {
    padding: 8px 12px;
    background: transparent;
    border: 1px solid rgba(129, 129, 129, 0.08);
    border-radius: 8px;
    cursor: pointer;
    color: rgba(121, 121, 121);
}

.advanced-toggle:hover {
    background: rgba(117, 117, 117, 0.04);
}

#message {
    text-align: center;
    margin-top: 20px;
    font-size: 1.3em;
    font-weight: bold;
    color: #667eea;
    min-height: 30px;
}

/* Status row (message + search button) */
.status-row {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    margin-top: 12px;
}

/* Promotion Dialog */
.promotion-dialog {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.promotion-dialog.hidden {
    display: none;
}

/* Generic hidden helper */
.hidden {
    display: none !important;
}

/* End-of-match controls */
.endmatch-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 12px;
}

.endmatch-btn {
    padding: 10px 14px;
    font-size: 0.95em;
    background: #444;
}

.endmatch-btn.selected {
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.25) inset,
        0 6px 12px rgba(0, 0, 0, 0.2);
    background: #667eea;
}

.endmatch-btn.opponent-selected {
    outline: 2px dashed rgba(118, 75, 162, 0.6);
}

/* Draw offer visuals for mode buttons */
.mode-controls .mode-btn.draw-requested {
    box-shadow: inset 0 0 0 3px rgba(34, 197, 94, 0.12);
    background: linear-gradient(180deg, rgba(34, 197, 94, 0.12), transparent);
}
.mode-controls .mode-btn.draw-opponent-requested {
    box-shadow: 0 0 8px rgba(34, 197, 94, 0.6);
}

.promotion-content {
    background: #808080; /* Neutral gray background for promotion icons */
    padding: 24px;
    border-radius: 12px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.35);
    text-align: center;
}

.promotion-content h3 {
    margin-bottom: 12px;
    color: #fff;
    font-size: 1.2em;
}

/* Result overlay shown on checkmate / stalemate to make outcome obvious */
.result-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    z-index: 2000;
    pointer-events: none; /* let buttons below remain if needed; we can change when showing */
}

.result-overlay .result-box {
    pointer-events: auto;
    position: relative;
    background: rgba(255, 255, 255, 0.98);
    color: #111;
    padding: 28px 34px;
    border-radius: 14px;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
    min-width: 320px;
}

.result-close {
    position: absolute;
    top: 8px;
    right: 8px;
    background: transparent;
    border: none;
    color: #444;
    font-size: 1rem;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 6px;
}
.result-close:hover {
    background: rgba(0, 0, 0, 0.05);
}

.result-emoji {
    font-size: 3.4rem;
    margin-bottom: 8px;
}

.result-title {
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: 6px;
}

.result-subtitle {
    font-size: 1.05rem;
    color: #555;
}

.result-overlay.hidden {
    display: none !important;
}

#promotion-choices {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
}

.promotion-choice {
    width: 100px;
    height: 100px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.12s ease-in-out, box-shadow 0.12s ease-in-out;
    background: transparent;
    padding: 4px;
}

.promotion-piece-name {
    display: none; /* Hide textual names; only show icons */
}

.promotion-choice:hover {
    transform: scale(1.03);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

@media (max-width: 700px) {
    #board-container {
        width: 400px;
        height: 400px;
    }

    .opponent-status,
    #opponent-status {
        top: -36px;
        font-size: 1.1em;
    }
    .opponent-icon {
        font-size: 1.3em;
    }
    .hud-toggle {
        font-size: 1em;
    }
    .clock {
        font-size: 0.85em;
    }
    /* Slightly reduce vertical offset on small screens */
    #opponent-status {
        bottom: calc(100% + 8px);
    }

    #board {
        width: 400px;
        height: 400px;
    }

    .square {
        width: 50px;
        height: 50px;
        font-size: 1.5em;
    }

    .piece {
        font-size: 1.8em;
    }

    /* Movement overlay styles removed */
}

/* Last-move highlighting: sky blue for both squares involved */
.square.last-move {
    /* Fallback single-color highlight if specific from/to classes are not present */
    background: rgba(var(--last-rgb), var(--overlay-alpha)) !important;
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.25);
    transition: background 200ms ease, box-shadow 200ms ease;
}

/* Distinct colors for moved-from and moved-to squares (use alpha vars)
   so changing the alpha variables only affects these overlays. */
.square.last-move-from {
    background: rgba(var(--last-from-rgb), var(--last-from-alpha)) !important;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.28);
}

.square.last-move-to {
    background: rgba(var(--last-to-rgb), var(--last-to-alpha)) !important;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.28);
}

@media (prefers-color-scheme: dark) {
    .square.last-move {
        background: rgba(var(--last-rgb), var(--overlay-alpha)) !important;
    }
    .square.last-move-from {
        background: rgba(
            var(--last-from-rgb),
            var(--last-from-alpha)
        ) !important;
    }
    .square.last-move-to {
        background: rgba(var(--last-to-rgb), var(--last-to-alpha)) !important;
    }
}

/* When it's black's turn, make the main container background black for strong contrast */
/* Softer, palette-consistent container backgrounds for turn states */
body.turn-black .container {
    background: linear-gradient(
        0deg,
        #000000 0%,
        #ffffff 100%
    ); /* deep blue-gray */
    color: #8a8a8a;
    box-shadow: 0 20px 60px rgba(10, 20, 30, 0.6);
}

body.turn-white .container {
    background: linear-gradient(
        0deg,
        #ffffff 0%,
        #000000 100%
    ); /* soft off-white */
    color: #818181;
    box-shadow: 0 20px 60px rgba(100, 120, 180, 0.12);
}

/* Ensure current-turn label remains readable on dark container */
body.turn-black #current-turn {
    color: #ffd873; /* softer warm highlight */
}

/* Ensure move-selection highlights override last-move highlight when both classes are present */
.square.valid-move.last-move,
.square.last-move.valid-move {
    background: rgba(
        var(--valid-rgb),
        var(--valid-move-alpha)
    ) !important; /* green for valid move */
    box-shadow: inset 0 0 15px rgba(0, 100, 0, 0.3);
}

.square.selected.last-move,
.square.last-move.selected {
    background: rgba(
        var(--selected-rgb),
        var(--selected-alpha)
    ) !important; /* blue for selected */
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
}
