/* =========================================== */
/* CẤU TRÚC CHUNG VÀ FONT */
/* =========================================== */

body {
    font-family: 'Arial', sans-serif;
    margin: 0;
    padding: 0;
    background-color: #2e2e2e;
    color: #f0f0f0;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
}

header {
    width: 90%;
    max-width: 1200px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 0;
    border-bottom: 2px solid #444;
}

h1 {
    font-size: 2.5em;
    color: #4CAF50;
    margin: 0;
}

.main-container {
    width: 90%;
    max-width: 1200px;
    padding: 20px 0;
    flex-grow: 1;
}

/* =========================================== */
/* BỐ CỤC MÀN HÌNH (SCREEN MANAGEMENT) */
/* =========================================== */

.screen {
    display: none;
    padding: 20px;
    border-radius: 8px;
    background-color: #383838;
    margin-top: 20px;
}

.screen.active {
    display: block;
}

#home-screen, #rules-screen {
    text-align: center;
}

#play-screen {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.game-area {
    display: flex;
    width: 100%;
    gap: 20px;
    justify-content: center;
    align-items: flex-start;
}

/* =========================================== */
/* BÀN CỜ VUA (CHESSBOARD) */
/* =========================================== */

.board-wrapper {
    flex-shrink: 0;
    width: 100%;
    max-width: 600px; 
    aspect-ratio: 1 / 1; 
}

.chessboard {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 100%;
    height: 100%;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
    border: 5px solid #444;
}

.square {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 3em; 
    cursor: pointer;
    user-select: none;
}

.light {
    background-color: #b3c2d4; 
}

.dark {
    background-color: #7792b0; 
}

/* Hiệu ứng quân cờ và nước đi */
.piece-white {
    color: #fff;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
}

.piece-black {
    color: #000;
    text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.7);
}

.square.selected {
    box-shadow: inset 0 0 0 4px #FFD700; 
}

.square.highlight-move {
    background-color: rgba(255, 255, 0, 0.5);
    box-shadow: inset 0 0 0 3px #1E90FF;
}

.square.king-in-check {
    background-color: #f44336 !important; 
}

/* =========================================== */
/* CỘT THÔNG TIN BÊN (SIDEBAR) */
/* =========================================== */

.info-sidebar {
    width: 350px; 
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.match-info-box {
    background-color: #444;
    padding: 10px;
    border-radius: 6px;
    text-align: center;
    display: flex;
    flex-direction: column;
    border: 1px solid #555;
}

.player-info {
    font-weight: bold;
    font-size: 1.1em;
    padding: 5px 0;
}

.clock-display {
    padding: 5px;
    font-size: 1.2em;
    font-weight: bold;
    background-color: #555;
    margin: 5px 0;
    border-radius: 4px;
    transition: background-color 0.3s;
}

.clock-display.active {
    background-color: #66bb6a;
    color: #1a1a1a;
}

.turn-indicator {
    padding: 10px 0;
    font-size: 1.1em;
    margin: 10px 0;
    border-top: 1px solid #555;
    border-bottom: 1px solid #555;
}

.turn-color.white-turn { color: #fff; }
.turn-color.black-turn { color: #000; }

.clock-display.low-time {
    background-color: #ff9800 !important;
    animation: blink 1s step-start infinite;
}

@keyframes blink {
    50% { opacity: 0.5; }
}

/* =========================================== */
/* LỊCH SỬ NƯỚC ĐI (MOVE HISTORY) */
/* =========================================== */

.history-box {
    background-color: #444;
    padding: 10px;
    border-radius: 6px;
    height: 250px;
    overflow-y: hidden;
    display: flex;
    flex-direction: column;
}

.move-history-list {
    flex-grow: 1;
    overflow-y: auto;
    padding-right: 5px; /* Khoảng cách cho scrollbar */
}

.move-item-row {
    display: grid;
    grid-template-columns: 1fr 3fr 3fr;
    gap: 10px;
    padding: 5px 0;
    border-bottom: 1px dashed #555;
    font-size: 0.9em;
}

.move-number-col {
    font-weight: bold;
    color: #aaa;
}

.move-detail-col {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.move-white, .move-black {
    padding: 2px 5px;
    border-radius: 3px;
    text-align: center;
}

.move-white { background-color: #666; color: #fff; }
.move-black { background-color: #333; color: #ccc; }

.move-description {
    font-size: 0.75em;
    color: #ccc;
    margin-top: 2px;
}

/* =========================================== */
/* CHAT BOX */
/* =========================================== */

.chat-box {
    background-color: #444;
    padding: 10px;
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    min-height: 200px;
}

.chat-room {
    flex-grow: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column-reverse; /* Tin nhắn mới nhất lên trên */
    padding: 5px;
    border: 1px solid #555;
    margin-bottom: 10px;
}

.chat-room p {
    margin: 2px 0;
    font-size: 0.9em;
    word-wrap: break-word;
}

.chat-room p span {
    font-weight: bold;
    color: #66bb6a;
}

.chat-room .bot-message {
    color: #ff9800;
}

.chat-input-area {
    display: flex;
    gap: 5px;
}

#chat-input {
    flex-grow: 1;
    padding: 8px;
    border: none;
    border-radius: 4px;
    background-color: #555;
    color: #fff;
}

.send-btn {
    padding: 8px 15px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.send-btn:hover {
    background-color: #45a049;
}

/* =========================================== */
/* MODAL & BUTTONS */
/* =========================================== */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    visibility: hidden;
    opacity: 0;
    transition: visibility 0s, opacity 0.3s;
}

.modal-overlay.visible {
    visibility: visible;
    opacity: 1;
}

.modal-content {
    background-color: #383838;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.8);
    width: 90%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.modal-content label {
    font-weight: bold;
    margin-top: 10px;
    color: #aaa;
}

.modal-content input[type="text"], .modal-content select {
    padding: 10px;
    border: 1px solid #555;
    border-radius: 5px;
    background-color: #444;
    color: #f0f0f0;
    font-size: 1em;
}

.level-selection {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
}

.level-btn {
    flex-grow: 1;
    padding: 10px;
    border: 1px solid #555;
    background-color: #555;
    color: white;
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.level-btn.active {
    background-color: #4CAF50;
    border-color: #4CAF50;
}

.level-btn:hover:not(.active) {
    background-color: #666;
}

.modal-actions {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
}

#start-match-btn {
    background-color: #4CAF50;
}

.close-modal-btn {
    background-color: #f44336;
}

.modal-actions button {
    padding: 10px 20px;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s;
}

/* =========================================== */
/* CLOCK WIDGET (KÉO THẢ) */
/* =========================================== */

.time-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 200px;
    background-color: #1a1a1a;
    border: 1px solid #4CAF50;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.6);
    z-index: 999;
    cursor: grab;
    transition: transform 0.1s;
}

.draggable-header {
    background-color: #4CAF50;
    color: white;
    padding: 5px 10px;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
    display: flex;
    justify-content: flex-end;
}

.close-widget-btn {
    font-size: 1.2em;
    cursor: pointer;
    font-weight: bold;
}

.time-content {
    padding: 10px;
    font-size: 0.9em;
}

/* =========================================== */
/* LANGUAGE SELECTOR */
/* =========================================== */

.language-selector .lang-btn {
    background: #333;
    color: #ccc;
    border: 1px solid #555;
    padding: 5px 8px;
    cursor: pointer;
    margin-left: 5px;
    border-radius: 4px;
}

.language-selector .lang-btn.active {
    background: #4CAF50;
    color: white;
    border-color: #4CAF50;
}

/* =========================================== */
/* RESPONSIVENESS */
/* =========================================== */

@media (max-width: 992px) {
    #play-screen {
        flex-direction: column;
        align-items: center;
    }
    
    .game-area {
        flex-direction: column;
        align-items: center;
    }
    
    .info-sidebar {
        width: 100%;
        max-width: 600px; /* Giới hạn chiều rộng Sidebar trên màn hình nhỏ */
    }

    .board-wrapper {
        width: 90%;
        max-width: 450px; 
    }
}

@media (max-width: 480px) {
    h1 {
        font-size: 2em;
    }

    .square {
        font-size: 2em;
    }
    
    .modal-content {
        padding: 20px;
    }
    
    .time-widget {
        width: 150px;
        font-size: 0.8em;
    }
}
