:root {
    --bg-color: #2c3e50;
    --board-border: #34495e;
    --light-square: #ecf0f1;
    --dark-square: #27ae60;
    /* Greenish for a colorful look */
    --highlight-color: #f1c40f;
    /* Yellow for selection */
    --move-hint-color: rgba(241, 196, 15, 0.5);
    --text-color: #ecf0f1;
    --btn-bg: #e74c3c;
    --btn-hover: #c0392b;
}

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

body {
    font-family: 'Roboto', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 10px;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 900px;
    padding: 10px;
}

header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding: 0 10px;
    flex-wrap: wrap;
    /* Allow wrapping on small screens */
}

h1 {
    font-size: 1.5rem;
}

.controls {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

#mode-select {
    padding: 8px;
    border-radius: 4px;
    border: none;
    background-color: #ecf0f1;
    color: #2c3e50;
    font-family: inherit;
    cursor: pointer;
}

.status-bar {
    display: flex;
    gap: 10px;
    align-items: center;
}

#turn-indicator {
    font-weight: bold;
}

.connection-status {
    font-size: 0.85rem;
    padding: 4px 8px;
    border-radius: 4px;
    font-weight: bold;
}

.connection-status.online {
    background-color: rgba(46, 204, 113, 0.2);
    color: #2ecc71;
}

.connection-status.offline {
    background-color: rgba(231, 76, 60, 0.2);
    color: #e74c3c;
}

button {
    background-color: var(--btn-bg);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background-color 0.2s;
}

button:hover {
    background-color: var(--btn-hover);
}

.secondary-btn {
    background-color: #3498db;
    margin-top: 10px;
}

.secondary-btn:hover {
    background-color: #2980b9;
}

.danger-btn {
    background-color: #e74c3c;
    margin-top: 10px;
}

.danger-btn:hover {
    background-color: #c0392b;
}

.player-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    max-width: 700px;
    margin-bottom: 15px;
    padding: 15px;
    background-color: rgba(52, 73, 94, 0.3);
    border-radius: 8px;
    gap: 15px;
    flex-wrap: wrap;
}

.player-section {
    display: flex;
    flex-direction: column;
    gap: 5px;
    flex: 1;
    min-width: 150px;
}

.player-section input {
    padding: 8px;
    border-radius: 4px;
    border: 2px solid #34495e;
    background-color: #ecf0f1;
    color: #2c3e50;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: bold;
}

.player-section .score {
    font-size: 0.85rem;
    color: #f39c12;
    font-weight: bold;
}

.timer-section {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
}

#game-timer {
    font-size: 1.5rem;
    font-weight: bold;
    color: #3498db;
    background-color: rgba(52, 152, 219, 0.1);
    padding: 8px 16px;
    border-radius: 4px;
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.7);
}

.modal-content {
    background-color: #2c3e50;
    margin: 5% auto;
    padding: 20px;
    border: 1px solid #34495e;
    border-radius: 8px;
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    overflow-y: auto;
}

.close {
    color: #ecf0f1;
    float: right;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close:hover,
.close:focus {
    color: #e74c3c;
}

#history-list {
    margin: 20px 0;
    max-height: 400px;
    overflow-y: auto;
}

.history-item {
    background-color: rgba(52, 73, 94, 0.5);
    padding: 12px;
    margin-bottom: 10px;
    border-radius: 4px;
    border-left: 4px solid #3498db;
}

.history-item.winner-white {
    border-left-color: #ecf0f1;
}

.history-item.winner-black {
    border-left-color: #34495e;
}

.history-item h4 {
    margin: 0 0 8px 0;
    color: #f39c12;
}

.history-item p {
    margin: 4px 0;
    font-size: 0.9rem;
}

.game-area {
    display: flex;
    gap: 15px;
    align-items: flex-start;
    justify-content: center;
    width: 100%;
    flex-wrap: nowrap;
}

@media (max-width: 768px) {
    .game-area {
        flex-wrap: wrap;
    }
}

.captured-pieces {
    background-color: rgba(52, 73, 94, 0.5);
    border-radius: 8px;
    padding: 10px;
    min-width: 80px;
    max-width: 120px;
    flex: 0 0 auto;
}

.captured-pieces h3 {
    font-size: 0.75rem;
    margin-bottom: 8px;
    text-align: center;
    color: var(--text-color);
    writing-mode: horizontal-tb;
}

.pieces-container {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
    min-height: 200px;
    max-height: 500px;
}

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

.chessboard {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 80vmin;
    /* Responsive size based on viewport */
    height: 80vmin;
    max-width: 500px;
    max-height: 500px;
    border: 5px solid var(--board-border);
    user-select: none;
    /* Prevent text selection */
}

.square {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 7vmin;
    /* Responsive font size for pieces */
    cursor: pointer;
    position: relative;
}

@media (min-width: 600px) {
    .square {
        font-size: 3rem;
    }
}

.square.light {
    background-color: var(--light-square);
    color: black;
    /* Piece color on light squares */
}

.square.dark {
    background-color: var(--dark-square);
    color: black;
    /* Piece color on dark squares */
}

/* Piece coloring */
.piece {
    z-index: 2;
    transition: transform 0.1s;
}

.piece.white {
    color: #fff;
    text-shadow: 0 0 2px #000;
    /* Outline for visibility */
}

.piece.black {
    color: #000;
    text-shadow: 0 0 1px #fff;
    /* Slight outline */
}

/* Selection and Highlights */
.square.selected {
    background-color: var(--highlight-color) !important;
}

.square.valid-move::after {
    content: '';
    position: absolute;
    width: 30%;
    height: 30%;
    background-color: var(--move-hint-color);
    border-radius: 50%;
    z-index: 1;
}

.square.capture-move {
    background-color: #e74c3c !important;
    /* Red for capture */
}

#message-area {
    margin-top: 10px;
    height: 20px;
    color: #f39c12;
    font-weight: bold;
}

/* Leaderboard Styles */
#global-stats {
    background-color: rgba(52, 152, 219, 0.2);
    padding: 12px;
    border-radius: 4px;
    margin-bottom: 15px;
    text-align: center;
}

#global-stats p {
    margin: 0;
    font-size: 1rem;
    color: #3498db;
    font-weight: bold;
}

#leaderboard-list {
    margin: 20px 0;
    max-height: 400px;
    overflow-y: auto;
}

.leaderboard-item {
    background-color: rgba(52, 73, 94, 0.5);
    padding: 12px;
    margin-bottom: 10px;
    border-radius: 4px;
    border-left: 4px solid #f39c12;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.leaderboard-item.rank-1 {
    border-left-color: #f1c40f;
    background-color: rgba(241, 196, 15, 0.1);
}

.leaderboard-item.rank-2 {
    border-left-color: #95a5a6;
    background-color: rgba(149, 165, 166, 0.1);
}

.leaderboard-item.rank-3 {
    border-left-color: #cd7f32;
    background-color: rgba(205, 127, 50, 0.1);
}

.leaderboard-item .player-name {
    font-weight: bold;
    font-size: 1rem;
    color: #ecf0f1;
}

.leaderboard-item .player-stats {
    display: flex;
    gap: 15px;
    font-size: 0.9rem;
}

.leaderboard-item .stat {
    color: #95a5a6;
}

.leaderboard-item .stat span {
    color: #3498db;
    font-weight: bold;
}