:root {
    --bg-main: #0f172a;
    --bg-sidebar: #1e293b;
    --bg-card: #334155;
    --border-color: #475569;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --accent-color: #6366f1;
    --accent-hover: #4f46e5;
    
    /* Board colors */
    --square-light: #e2e8f0;
    --square-dark: #64748b;
    --highlight-move: rgba(99, 102, 241, 0.5);
    --highlight-selected: rgba(59, 130, 246, 0.6);
    --highlight-check: rgba(239, 68, 68, 0.7);

    --font-sans: 'Plus Jakarta Sans', sans-serif;
    --font-heading: 'Cinzel', serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-main);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

.app-container {
    display: flex;
    width: 100%;
    max-width: 1300px;
    height: 100vh;
    max-height: 900px;
    background-color: var(--bg-main);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    border-radius: 20px;
    border: 1px solid var(--border-color);
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 340px;
    background-color: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 24px;
    gap: 20px;
    overflow-y: auto;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-icon {
    font-size: 28px;
    background: linear-gradient(135deg, #6366f1, #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 2px 4px rgba(99, 102, 241, 0.3));
}

.brand h1 {
    font-family: var(--font-heading);
    font-size: 22px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.brand .accent {
    color: var(--accent-color);
}

.game-status-card {
    background-color: var(--bg-card);
    padding: 14px 18px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.turn-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 15px;
}

.dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.white-dot {
    background-color: #f8fafc;
    box-shadow: 0 0 8px rgba(248, 250, 252, 0.8);
}

.black-dot {
    background-color: #0f172a;
    border: 2px solid #94a3b8;
}

.status-message {
    font-size: 13px;
    color: var(--text-muted);
}

.controls-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    border-radius: 10px;
    font-family: var(--font-sans);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-color), #8b5cf6);
    color: white;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

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

.btn-secondary {
    background-color: var(--bg-card);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
    background-color: var(--border-color);
}

.btn-secondary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.mode-selector {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.mode-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.mode-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    background-color: var(--bg-card);
    padding: 4px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
}

.mode-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 8px;
    font-size: 13px;
    font-weight: 600;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.mode-btn.active {
    background-color: var(--accent-color);
    color: white;
}

.captured-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background-color: var(--bg-card);
    padding: 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    font-size: 12px;
}

.captured-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.captured-row .label {
    color: var(--text-muted);
    font-weight: 500;
}

.captured-pieces {
    min-height: 24px;
    display: flex;
    align-items: center;
    gap: 2px;
    flex-wrap: wrap;
}

.captured-piece-icon {
    width: 20px;
    height: 20px;
    background-size: 600% 200%;
}

.history-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background-color: var(--bg-card);
    padding: 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    min-height: 120px;
    max-height: 180px;
}

.history-card h3 {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
}

.empty-history {
    color: var(--text-muted);
    text-align: center;
    padding: 20px;
    font-style: italic;
    font-size: 12px;
}

.history-item {
    display: flex;
    justify-content: space-between;
    padding: 4px 8px;
    background-color: rgba(255, 255, 255, 0.03);
    border-radius: 4px;
}

.history-item span {
    font-family: monospace;
}

/* Board Main */
.board-main {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--bg-main);
    padding: 24px;
}

.board-wrapper {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: fit-content;
}

.player-tag {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    background-color: var(--bg-sidebar);
    border-radius: 10px;
    border: 1px solid var(--border-color);
    width: 100%;
}

.player-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
}

.white-avatar {
    background-color: #f8fafc;
    color: #0f172a;
}

.black-avatar {
    background-color: #1e293b;
    color: #f8fafc;
    border: 1px solid #475569;
}

.player-name {
    font-weight: 600;
    font-size: 14px;
    flex: 1;
}

.timer {
    font-family: monospace;
    font-weight: 700;
    font-size: 14px;
    background-color: var(--bg-card);
    padding: 4px 8px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
}

.chess-board-container {
    display: grid;
    grid-template-columns: 24px 1fr;
    grid-template-rows: 24px 1fr 24px;
    grid-template-areas:
        " . files-top"
        "ranks-left board"
        " . files-bottom";
    background-color: var(--bg-sidebar);
    padding: 12px;
    border-radius: 16px;
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.coordinates {
    display: flex;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
}

.files-top, .files-bottom {
    grid-area: files-top; /* adjusted by JS/CSS layout */
    justify-content: space-around;
    align-items: center;
    padding: 0 16px;
}

.files-bottom {
    grid-area: files-bottom;
}

.ranks-left, .ranks-right {
    grid-area: ranks-left;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    padding: 16px 0;
}

.ranks-right {
    grid-area: ranks-right;
}

.board-with-ranks {
    grid-area: board;
    display: flex;
}

.chessboard {
    width: 520px;
    height: 520px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    border: 2px solid var(--border-color);
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
}

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

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

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

.square.highlight::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: rgba(99, 102, 241, 0.6);
}

.square.highlight-capture::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border: 4px solid rgba(239, 68, 68, 0.7);
    border-radius: 4px;
}

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

/* Chess Piece Rendering using Sprite Sheet */
.piece {
    width: 82%;
    height: 82%;
    background-image: url('assets/pieces.png');
    background-size: 600% 200%; /* 6 columns (K, Q, R, B, N, P), 2 rows (White, Black) */
    background-repeat: no-repeat;
    z-index: 2;
    transition: transform 0.15s ease;
    filter: drop-shadow(0 3px 4px rgba(0,0,0,0.3));
}

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

/* Sprite coordinates mapping */
/* White Pieces (Row 0) */
.piece.w-k { background-position: 0% 0%; }
.piece.w-q { background-position: 20% 0%; }
.piece.w-r { background-position: 40% 0%; }
.piece.w-b { background-position: 60% 0%; }
.piece.w-n { background-position: 80% 0%; }
.piece.w-p { background-position: 100% 0%; }

/* Black Pieces (Row 1) */
.piece.b-k { background-position: 0% 100%; }
.piece.b-q { background-position: 20% 100%; }
.piece.b-r { background-position: 40% 100%; }
.piece.b-b { background-position: 60% 100%; }
.piece.b-n { background-position: 80% 100%; }
.piece.b-p { background-position: 100% 100%; }

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background-color: var(--bg-sidebar);
    border: 1px solid var(--border-color);
    padding: 32px;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
    max-width: 400px;
    width: 90%;
    text-align: center;
}

.modal h2 {
    font-family: var(--font-heading);
    font-size: 24px;
}

.promotion-choices {
    display: flex;
    gap: 16px;
}

.promo-choice {
    width: 64px;
    height: 64px;
    background-color: var(--bg-card);
    border: 2px solid var(--border-color);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.promo-choice:hover {
    border-color: var(--accent-color);
    background-color: var(--border-color);
    transform: translateY(-2px);
}

.game-over-modal .trophy-icon {
    font-size: 48px;
    animation: bounce 1s infinite alternate;
}

@keyframes bounce {
    0% { transform: translateY(0); }
    100% { transform: translateY(-10px); }
}

.game-over-modal p {
    color: var(--text-muted);
    font-size: 15px;
}

/* Responsive */
@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
        height: auto;
        max-height: none;
        border-radius: 0;
    }
    .sidebar {
        width: 100%;
        max-height: 300px;
    }
    .board-main {
        padding: 16px;
    }
    .chessboard {
        width: 360px;
        height: 360px;
    }
}
