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

:root {
    --bg-dark: #312e2b;
    --bg-darker: #272522;
    --bg-card: #3d3a37;
    --bg-hover: #4a4744;
    --text-primary: #ffffff;
    --text-secondary: #b0ada9;
    --text-muted: #7c7976;
    --accent: #81b64c;
    --accent-hover: #9bc968;
    --danger: #e84a35;
    --danger-hover: #ff5e47;
    --board-light: #eeeed2;
    --board-dark: #769656;
    --board-selected: #baca44;
    --board-move: #f6f669;
    --board-check: #e84a35;
    --gold: #f0c230;
    --silver: #a8a8a8;
    --bronze: #c47f35;
    --border-radius: 4px;
    --shadow: 0 4px 12px rgba(0,0,0,0.3);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-darker);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.5;
    overflow-x: hidden;
}

/* Auth Screen */
.auth-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--bg-darker) 0%, #1a1816 100%);
}

.auth-container {
    text-align: center;
    padding: 2rem;
}

.auth-logo {
    margin-bottom: 2rem;
}

.logo-icon {
    font-size: 4rem;
    color: var(--accent);
    text-shadow: 0 4px 20px rgba(129, 182, 76, 0.3);
}

.auth-logo h1 {
    font-size: 2rem;
    font-weight: 700;
    margin-top: 0.5rem;
    color: var(--text-primary);
}

.auth-box {
    background: var(--bg-card);
    padding: 2.5rem;
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    max-width: 380px;
    margin: 0 auto;
}

.auth-box h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.auth-box p {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
}

.input-group {
    margin-bottom: 1rem;
}

.input-group input {
    width: 100%;
    padding: 0.875rem 1rem;
    background: var(--bg-darker);
    border: 2px solid transparent;
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    transition: border-color 0.2s, background 0.2s;
}

.input-group input:focus {
    outline: none;
    border-color: var(--accent);
    background: var(--bg-dark);
}

.input-group input::placeholder {
    color: var(--text-muted);
}

.btn-primary {
    width: 100%;
    padding: 0.875rem 1.5rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--border-radius);
    font-size: 1rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

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

.btn-primary:active {
    transform: scale(0.98);
}

.btn-danger {
    width: 100%;
    padding: 0.75rem 1rem;
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
    border-radius: var(--border-radius);
    font-size: 0.875rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-danger:hover {
    background: var(--danger);
    color: #fff;
}

.btn-icon {
    padding: 0.5rem;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.25rem;
    cursor: pointer;
    border-radius: var(--border-radius);
    transition: color 0.2s, background 0.2s;
}

.btn-icon:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.error-message {
    margin-top: 1rem;
    padding: 0.75rem;
    background: rgba(232, 74, 53, 0.1);
    border: 1px solid var(--danger);
    border-radius: var(--border-radius);
    color: var(--danger);
    font-size: 0.875rem;
}

.auth-features {
    display: flex;
    justify-content: center;
    gap: 2rem;
    margin-top: 2rem;
    flex-wrap: wrap;
}

.feature {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.feature-icon {
    color: var(--accent);
}

/* Game Screen Layout */
.game-screen {
    display: grid;
    grid-template-columns: 260px 1fr 280px;
    min-height: 100vh;
    background: var(--bg-dark);
}

/* Sidebars */
.sidebar {
    background: var(--bg-darker);
    display: flex;
    flex-direction: column;
    border-right: 1px solid rgba(255,255,255,0.05);
}

.sidebar-right {
    border-right: none;
    border-left: 1px solid rgba(255,255,255,0.05);
}

.sidebar-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.logo-small {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 1.1rem;
}

.logo-icon-small {
    color: var(--accent);
    font-size: 1.5rem;
}

.sidebar-nav {
    padding: 1rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.nav-btn {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.75rem 1rem;
    background: transparent;
    border: none;
    border-radius: var(--border-radius);
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s;
    text-align: left;
}

.nav-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-btn.active {
    background: var(--accent);
    color: #fff;
}

.nav-icon {
    font-size: 1rem;
    width: 1.25rem;
    text-align: center;
}

.sidebar-section {
    padding: 1rem 1.25rem;
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.sidebar-section h3 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.sidebar-footer {
    padding: 1rem 1.25rem;
    border-top: 1px solid rgba(255,255,255,0.05);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex: 1;
    min-width: 0;
}

.user-avatar {
    width: 36px;
    height: 36px;
    background: var(--bg-card);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    color: var(--text-secondary);
}

.user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.user-name {
    font-weight: 500;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-rating {
    font-size: 0.8rem;
    color: var(--gold);
}

/* Game Info Panel */
.game-info-panel {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 1rem;
}

.player-card {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0;
}

.player-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    flex-shrink: 0;
}

.player-card.white .player-color {
    background: #fff;
    border: 1px solid rgba(0,0,0,0.1);
}

.player-card.black .player-color {
    background: #312e2b;
    border: 1px solid rgba(255,255,255,0.1);
}

.player-details {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.player-name {
    font-weight: 500;
    font-size: 0.9rem;
}

.player-rating {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.vs-divider {
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-muted);
    padding: 0.25rem 0;
}

/* Move List */
.move-list {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-card);
    border-radius: var(--border-radius);
    padding: 0.5rem;
    font-family: 'SF Mono', Monaco, monospace;
    font-size: 0.85rem;
    max-height: 200px;
}

.move-row {
    display: grid;
    grid-template-columns: 28px 1fr 1fr;
    gap: 0.25rem;
    padding: 0.25rem 0.5rem;
    border-radius: 2px;
}

.move-row:hover {
    background: var(--bg-hover);
}

.move-num {
    color: var(--text-muted);
}

.move-white, .move-black {
    color: var(--text-primary);
}

/* Game Actions */
.game-actions {
    padding: 1rem 1.25rem;
}

/* Board Area */
.board-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    gap: 1rem;
    background: var(--bg-dark);
}

.game-status-bar {
    font-size: 0.9rem;
    color: var(--text-secondary);
    min-height: 24px;
}

.game-status-bar.warning {
    color: var(--gold);
    font-weight: 500;
}

.game-status-bar.error {
    color: var(--danger);
    font-weight: 500;
}

.board-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.captured-row {
    display: flex;
    gap: 2px;
    min-height: 28px;
    padding: 0.25rem 0.5rem;
}

.captured-piece {
    font-size: 1.25rem;
    opacity: 0.7;
}

/* 3D Board Container */
.board-3d-container {
    perspective: 1000px;
    perspective-origin: 50% 50%;
}

.board-3d {
    transform: rotateX(20deg);
    transform-style: preserve-3d;
    transition: transform 0.3s ease;
}

.chess-board {
    display: grid;
    grid-template-columns: repeat(8, 70px);
    grid-template-rows: repeat(8, 70px);
    border-radius: 4px;
    overflow: hidden;
    box-shadow:
        0 20px 60px rgba(0,0,0,0.5),
        0 0 0 4px var(--bg-darker),
        0 0 0 8px rgba(0,0,0,0.2);
    transform-style: preserve-3d;
}

.square {
    width: 70px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    cursor: pointer;
    position: relative;
    user-select: none;
    transition: background 0.1s;
    transform-style: preserve-3d;
}

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

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

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

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

.square.valid-move::after {
    content: '';
    position: absolute;
    width: 24px;
    height: 24px;
    background: rgba(0,0,0,0.15);
    border-radius: 50%;
    pointer-events: none;
}

.square.valid-move.has-piece::after {
    width: 100%;
    height: 100%;
    background: transparent;
    border-radius: 0;
    border: 4px solid rgba(0,0,0,0.15);
}

.square.in-check {
    background: radial-gradient(ellipse at center, var(--board-check) 0%, var(--board-check) 25%, transparent 80%) !important;
    animation: check-pulse 1s ease-in-out infinite;
}

@keyframes check-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.8; }
}

/* Piece styling */
.piece {
    font-size: 3.2rem;
    line-height: 1;
    text-shadow:
        2px 4px 6px rgba(0,0,0,0.3),
        0 1px 0 rgba(255,255,255,0.1);
    transition: transform 0.1s;
    cursor: grab;
}

.piece:active {
    cursor: grabbing;
    transform: scale(1.1);
}

.piece.white {
    filter: drop-shadow(1px 2px 2px rgba(0,0,0,0.3));
}

.piece.black {
    filter: drop-shadow(1px 2px 2px rgba(0,0,0,0.4));
}

/* Piece animations */
@keyframes piece-move {
    0% { transform: scale(1); }
    50% { transform: scale(1.15) translateY(-8px); }
    100% { transform: scale(1); }
}

@keyframes piece-capture {
    0% { transform: scale(1) rotate(0deg); opacity: 1; }
    50% { transform: scale(0.7) rotate(180deg); opacity: 0.4; }
    100% { transform: scale(0) rotate(360deg); opacity: 0; }
}

.square.animating .piece {
    animation: piece-move 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.square.capturing .piece {
    animation: piece-capture 0.4s ease-out forwards;
}

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

.square.has-piece:active .piece {
    transform: scale(1.12);
}

/* Chess clocks */
.player-clock-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    min-height: 32px;
}

.chess-clock {
    background: var(--bg-card);
    padding: 0.5rem 1rem;
    border-radius: var(--border-radius);
    border: 2px solid var(--bg-hover);
    transition: all 0.3s;
}

.chess-clock.active {
    border-color: var(--accent);
    background: rgba(129, 182, 76, 0.1);
    box-shadow: 0 0 12px rgba(129, 182, 76, 0.3);
}

.chess-clock.low-time {
    border-color: var(--danger);
    background: rgba(232, 74, 53, 0.1);
    animation: clock-pulse 1s infinite;
}

@keyframes clock-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

.clock-time {
    font-size: 1.25rem;
    font-weight: 700;
    font-family: 'Courier New', monospace;
    color: var(--text-primary);
}

/* Time control buttons */
.time-controls {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
}

.time-btn {
    padding: 0.625rem 0.75rem;
    background: var(--bg-card);
    color: var(--text-secondary);
    border: 2px solid transparent;
    border-radius: var(--border-radius);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.time-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.time-btn.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent-hover);
}

/* User ratings display */
.user-ratings {
    display: flex;
    gap: 0.75rem;
    font-size: 0.8rem;
}

.user-rating {
    padding: 0.25rem 0.5rem;
    background: var(--bg-hover);
    border-radius: 3px;
}

.user-rating.ai-rating {
    background: rgba(129, 182, 76, 0.2);
    color: var(--accent-hover);
}

/* Coordinate labels */
.coord-label {
    position: absolute;
    font-size: 0.65rem;
    font-weight: 600;
    pointer-events: none;
}

.square.light .coord-label {
    color: var(--board-dark);
}

.square.dark .coord-label {
    color: var(--board-light);
}

.file-label {
    bottom: 2px;
    right: 4px;
}

.rank-label {
    top: 2px;
    left: 4px;
}

/* Turn Indicator */
.turn-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.turn-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #fff;
    border: 2px solid rgba(0,0,0,0.1);
    transition: background 0.2s;
}

.turn-indicator.black .turn-dot {
    background: #312e2b;
    border-color: rgba(255,255,255,0.1);
}

/* Leaderboard & Online Lists */
.leaderboard-list, .online-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    max-height: 280px;
}

.leaderboard-item, .online-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 0.75rem;
    background: var(--bg-card);
    border-radius: var(--border-radius);
    transition: background 0.15s;
}

.leaderboard-item:hover, .online-item:hover {
    background: var(--bg-hover);
}

.rank {
    font-weight: 700;
    font-size: 0.8rem;
    color: var(--text-muted);
    min-width: 24px;
}

.leaderboard-item.top-1 .rank { color: var(--gold); }
.leaderboard-item.top-2 .rank { color: var(--silver); }
.leaderboard-item.top-3 .rank { color: var(--bronze); }

.lb-name, .online-name {
    flex: 1;
    font-size: 0.875rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lb-rating {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.online-status {
    width: 8px;
    height: 8px;
    background: var(--accent);
    border-radius: 50%;
    flex-shrink: 0;
}

.online-rating {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.loading-state {
    text-align: center;
    color: var(--text-muted);
    padding: 1.5rem;
    font-size: 0.875rem;
}

/* Modals */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal-content {
    background: var(--bg-card);
    border-radius: 12px;
    padding: 2rem;
    text-align: center;
    box-shadow: var(--shadow-lg);
    max-width: 400px;
    width: 90%;
}

.promotion-content h3 {
    margin-bottom: 1.5rem;
    font-size: 1.1rem;
    font-weight: 600;
}

.promotion-pieces {
    display: flex;
    justify-content: center;
    gap: 0.75rem;
}

.promotion-piece {
    font-size: 2.5rem;
    padding: 0.75rem;
    background: var(--bg-hover);
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}

.promotion-piece:hover {
    background: var(--accent);
    transform: scale(1.05);
}

.result-content {
    padding: 2.5rem;
}

.result-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    color: var(--accent);
}

.result-content h2 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.result-content p {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

.rating-change {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 1.5rem;
}

.rating-change.positive {
    color: var(--accent);
}

.rating-change.negative {
    color: var(--danger);
}

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

/* Responsive */
@media (max-width: 1200px) {
    .game-screen {
        grid-template-columns: 220px 1fr 240px;
    }

    .chess-board {
        grid-template-columns: repeat(8, 60px);
        grid-template-rows: repeat(8, 60px);
    }

    .square {
        width: 60px;
        height: 60px;
        font-size: 2.6rem;
    }

    .piece {
        font-size: 2.8rem;
    }
}

@media (max-width: 1024px) {
    .game-screen {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr auto;
    }

    .sidebar-left {
        order: 2;
        flex-direction: row;
        flex-wrap: wrap;
        padding: 0.75rem;
        gap: 0.75rem;
    }

    .sidebar-left .sidebar-header,
    .sidebar-left .sidebar-section,
    .sidebar-left .game-actions {
        display: none;
    }

    .sidebar-left .sidebar-nav {
        flex-direction: row;
        padding: 0;
        flex: 1;
    }

    .sidebar-left .sidebar-footer {
        border: none;
        padding: 0;
    }

    .sidebar-right {
        order: 3;
        flex-direction: row;
        padding: 0.75rem;
        gap: 0.75rem;
    }

    .sidebar-right .sidebar-section {
        flex: 1;
        padding: 0;
    }

    .leaderboard-list, .online-list {
        max-height: 150px;
    }

    .board-area {
        order: 1;
        padding: 1rem;
    }
}

@media (max-width: 600px) {
    .chess-board {
        grid-template-columns: repeat(8, 44px);
        grid-template-rows: repeat(8, 44px);
    }

    .square {
        width: 44px;
        height: 44px;
        font-size: 2rem;
    }

    .piece {
        font-size: 2.2rem;
    }

    .board-3d {
        transform: rotateX(10deg);
    }

    .auth-box {
        padding: 1.5rem;
    }
}