/* CSS Variables and Reset */
:root {
    --board-size: min(80vw, 400px);
    --cell-size: calc(var(--board-size) / 8);
    --piece-size: calc(var(--cell-size) * 0.8);
    --piece-inner-size: calc(var(--piece-size) * 0.8);
    --primary-red: #ef4444;
    --primary-black: #1f2937;
    --primary-green: #84cc16;
    --board-dark: #769656;
    --board-light: #eeeed2;
    --text-primary: #1a1a1a;
    --text-secondary: #4a5568;
    --background-primary: #f0f2f5;
    --background-secondary: #ffffff;
    --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
    /* Added for Dark Mode */
    --move-history-bg: #f8fafc;
    --move-history-item-black-bg: #f8fafc;
    --move-history-item-red-bg: #fef2f2;
}

/* Dark Theme Variables */
[data-theme="dark"] {
    --background-primary: #1a1a1a;
    --background-secondary: #2d2d2d;
    --text-primary: #ffffff;
    --text-secondary: #e0e0e0;
    --board-light: #b8b8b8;
    --board-dark: #666666;
    --move-history-bg: #363636;
    --move-history-item-black-bg: #404040;
    --move-history-item-red-bg: #3d2929; /* Darker, less saturated red background */
}

/* Reset and Box Sizing */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: var(--background-primary);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    color: var(--text-primary);
    line-height: 1.5;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Game Container */
.game-container {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: clamp(15px, 3vw, 40px);
    background: var(--background-secondary);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
}

.game-header {
    display: flex;
    flex-direction: column;
    align-items: center; /* Center header content */
    margin-bottom: clamp(15px, 4vw, 30px);
    position: relative;
}

/* Score Board */
.score-board {
    display: flex;
    justify-content: center;
    gap: clamp(10px, 3vw, 24px);
    margin-bottom: clamp(15px, 3vw, 24px);
    flex-wrap: wrap;
}

.score {
    padding: clamp(8px, 2vw, 12px) clamp(16px, 3vw, 24px);
    border-radius: 100px;
    font-weight: 600;
    font-size: clamp(14px, 2.5vw, 18px);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: var(--shadow-sm);
    white-space: nowrap;
}

.score:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.red-score {
    background: var(--primary-red);
    color: white;
}

.black-score {
    background: var(--primary-black);
    color: white;
}

/* Status Display */
.status {
    text-align: center;
    font-size: clamp(18px, 4vw, 28px);
    margin: clamp(12px, 3vw, 24px) 0;
    font-weight: 600;
    color: var(--text-secondary);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Theme Toggle Button */
.theme-toggle {
    position: absolute;
    top: 0;
    right: 0;
    background: none;
    border: none;
    cursor: pointer;
    font-size: clamp(20px, 2vw, 24px);
    transition: transform 0.2s ease;
    padding: 8px;
    margin: 8px;
}

.theme-toggle:hover {
    transform: rotate(20deg);
}

/* Layout Container */
.game-layout {
    display: flex;
    flex-direction: row;
    gap: clamp(20px, 4vw, 40px);
    justify-content: center;
    align-items: flex-start;
    width: 100%;
    padding: 0 clamp(10px, 2vw, 20px);
    margin-top: -20px;
}

/* Board Container and Labels */
.board-container {
    flex-shrink: 0;
    position: relative;
    margin: 0;
    padding-left: 25px; /* Space for vertical labels */
    padding-bottom: 25px; /* Space for horizontal labels */
}

.board-with-labels {
    display: flex;
    position: relative;
}

/* Vertical Labels */
.board-labels.vertical {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    position: absolute;
    left: -20px;
    top: 0;
    height: var(--board-size);
    width: 25px; /* Fixed width for vertical labels */
    z-index: 2; /* Ensure labels are on top */
    /* Temporary background for debugging */
    /* background-color: rgba(255, 0, 0, 0.1); */
}

.board-labels.vertical span {
    height: var(--cell-size);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: clamp(10px, 1.5vw, 14px);
    font-weight: 500;
}

/* Horizontal Labels */
.board-labels.horizontal {
    display: flex;
    justify-content: space-around;
    position: absolute;
    bottom: 0;
    left: 25px; /* Align with board after vertical labels */
    width: var(--board-size);
    height: 25px; /* Fixed height for horizontal labels */
    z-index: 2; /* Ensure labels are on top */
    /* Temporary background for debugging */
    /* background-color: rgba(0, 255, 0, 0.1); */
}

.board-labels.horizontal span {
    width: var(--cell-size);
    height: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: clamp(10px, 1.5vw, 14px);
    font-weight: 500;
}

/* Move History */
.move-history {
    width: 100%;
    background: var(--background-secondary);
    border-radius: 16px;
    padding: clamp(12px, 3vw, 24px);
    box-shadow: var(--shadow-md);
}

.move-history h3 {
    color: var(--text-primary);
    font-size: clamp(16px, 3vw, 24px);
    font-weight: 700;
    margin-bottom: clamp(10px, 2vw, 16px);
}

.move-history-list {
    height: clamp(180px, 40vh, 300px);
    overflow-y: auto;
    padding: 8px;
    background: var(--move-history-bg);
    border-radius: 12px;
}

/* Move History Items */
.move-history-item {
    padding: clamp(8px, 1.5vw, 12px) clamp(12px, 2vw, 16px);
    margin-bottom: 4px;
    border-radius: 8px;
    font-weight: 500;
    font-size: clamp(12px, 1.5vw, 14px);
}

.red-move {
    color: var(--primary-red);
    border-left: 3px solid var(--primary-red);
    background: var(--move-history-item-red-bg);
}

.black-move {
    color: var(--primary-black);
    border-left: 3px solid var(--primary-black);
    background: var(--move-history-item-black-bg);
}

/* Scrollbar Styling */
.move-history-list::-webkit-scrollbar {
    width: 6px;
}

.move-history-list::-webkit-scrollbar-track {
    background: #f1f5f9;
    border-radius: 100px;
}

.move-history-list::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 100px;
}

.move-history-list::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* Game Controls */
.game-controls {
    width: 100%;
    display: flex;
    justify-content: center;
}

.game-controls button {
    max-width: 200px;
    width: 100%;
    padding: clamp(12px, 2vw, 14px) clamp(20px, 3vw, 24px);
    border: none;
    border-radius: 100px;
    background: var(--primary-green);
    color: white;
    font-size: clamp(14px, 2vw, 16px);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px rgba(132, 204, 22, 0.2);
    margin-top: 20px;
}

.game-controls button:hover {
    background: #65a30d;
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(132, 204, 22, 0.3);
}

.game-controls button:active {
    transform: translateY(0);
}

/* Dark Theme Specific Styles */
[data-theme="dark"] .move-history-list {
    background: var(--move-history-bg);
}

[data-theme="dark"] .move-history-item.black-move {
    background: var(--move-history-item-black-bg);
    color: #e0e0e0;
}

[data-theme="dark"] .move-history-item.red-move {
    background: var(--move-history-item-red-bg);
    color: #ff9999; /* Lighter, more visible red text */
    border-left-color: #ff9999; /* Match the text color for consistency */
}

[data-theme="dark"] .black-score {
    color: #ffffff;
}

[data-theme="dark"] .red-score {
    color: #ffffff;
}

[data-theme="dark"] .winner-announcement {
    background: var(--background-secondary);
    color: var(--text-primary);
}

/* Game Board */
.board {
    width: var(--board-size);
    height: var(--board-size);
    border: 2px solid var(--text-secondary);
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    /* Removed margin-left: 15px; */
    border-radius: 4px;
    overflow: hidden;
    touch-action: manipulation;
    z-index: 1; /* Ensure board is below labels */
}

/* Cells */
.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: relative;
    transition: all 0.2s ease;
    touch-action: manipulation;
}

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

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

/* Pieces */
.piece {
    width: var(--piece-size);
    height: var(--piece-size);
    border-radius: 50%;
    position: relative;
    transition: transform 0.2s ease;
    will-change: transform;
    touch-action: manipulation;
}

.piece::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: var(--piece-inner-size);
    height: var(--piece-inner-size);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
}

.piece:hover {
    transform: scale(1.05);
}

.red-piece {
    background: var(--primary-red);
    box-shadow: 
        0 2px 4px rgba(239, 68, 68, 0.2),
        inset 0 2px 4px rgba(255, 255, 255, 0.3);
}

.black-piece {
    background: var(--primary-black);
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.2),
        inset 0 2px 4px rgba(255, 255, 255, 0.1);
}

/* King Styling */
.king::after {
    content: "♔";
    color: #fcd34d;
    font-size: clamp(16px, calc(var(--cell-size) * 0.4), 24px);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* Move Highlights */
.selected {
    background-color: #baca44 !important;
}

.valid-move {
    position: relative;
}

.valid-move::after {
    content: "";
    position: absolute;
    width: calc(var(--cell-size) * 0.3);
    height: calc(var(--cell-size) * 0.3);
    background-color: rgba(132, 204, 22, 0.5);
    border-radius: 50%;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0% { transform: scale(0.8); opacity: 0.5; }
    50% { transform: scale(1.2); opacity: 0.8; }
    100% { transform: scale(0.8); opacity: 0.5; }
}

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

    .game-info {
        max-width: var(--board-size);
        width: 100%;
    }
}

@media (max-width: 767px) {
    .game-layout {
        padding: 0;
    }

    .game-info {
        max-width: var(--board-size);
        width: 100%;
    }

    .move-history {
        width: 100%;
    }
}

/* Small Phone Adjustments */
@media (max-width: 375px) {
    .game-layout {
        gap: 15px;
    }

    .game-info {
        max-width: 100%;
    }
}

/* Landscape Mode */
@media (max-height: 600px) and (orientation: landscape) {
    .game-layout {
        flex-direction: row;
        gap: 20px;
        align-items: flex-start;
    }

    .game-info {
        max-width: 250px;
    }
}

/* Mobile Specific Centering and Full-Width Adjustments */
@media (max-width: 480px) {
    :root {
        --board-size: min(80vw, 300px);
    }

    .game-container {
        margin: 8px;
        padding: 12px;
    }

    .score-board {
        gap: 8px;
        justify-content: center; /* Center score-board items */
    }

    .score {
        padding: 6px 12px;
        font-size: 13px;
        text-align: center;
        width: 100%;
    }

    .status {
        text-align: center;
    }

    .theme-toggle {
        margin: 0 auto; /* Center the toggle button */
        display: block;
    }

    .game-info {
        align-items: center;
    }

    /* Make Move History Full Width */
    .move-history {
        width: 100%;
    }

    .move-history-list {
        width: 100%;
    }

    .game-controls button {
        width: 100%;
        margin-top: 20px;
    }

    /* Adjust Game Layout for Full-Width on Mobile */
    .game-layout {
        flex-direction: column;
        align-items: center;
        gap: 20px;
    }

    /* Responsive Adjustments for Labels on Mobile */
    .board-container {
        padding-left: 20px; /* Slightly smaller padding on mobile */
        padding-bottom: 20px;
    }

    .board-labels.vertical {
        width: 20px;
    }

    .board-labels.horizontal {
        left: 20px;
        height: 20px;
    }

    .board-labels.vertical span,
    .board-labels.horizontal span {
        font-size: 10px; /* Smaller font size on mobile */
    }
}

/* iPhone Notch Handling */
@supports (padding-top: env(safe-area-inset-top)) {
    body {
        padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }
    
    .game-container {
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
}

/* Touch Device Optimizations */
@media (hover: none) {
    .piece:hover {
        transform: none;
    }

    .piece:active {
        transform: scale(0.95);
    }

    .score:hover {
        transform: none;
        box-shadow: var(--shadow-sm);
    }

    .game-controls button:hover {
        transform: none;
        box-shadow: var(--shadow-sm);
    }

    .cell:active {
        opacity: 0.8;
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    .piece,
    .score,
    .game-controls button {
        transition: none;
    }

    .valid-move::after {
        animation: none;
    }

    .winner-announcement {
        animation: none;
    }
}

/* Print Styles */
@media print {
    body {
        background: none;
    }

    .game-container {
        box-shadow: none;
        margin: 0;
        padding: 20px;
    }

    .game-controls {
        display: none;
    }

    .move-history-list {
        height: auto;
        overflow: visible;
    }

    .piece {
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }
}

/* Winner Announcement */
.winner-announcement {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--background-secondary);
    padding: clamp(20px, 4vw, 30px) clamp(30px, 5vw, 50px);
    border-radius: 16px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.2);
    text-align: center;
    z-index: 1000;
    animation: announceWinner 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
    width: clamp(280px, 80vw, 400px);
}

@keyframes announceWinner {
    from { 
        transform: translate(-50%, -40%);
        opacity: 0;
    }
    to { 
        transform: translate(-50%, -50%);
        opacity: 1;
    }
}
