:root {
    /* Định nghĩa khoảng cách đồng nhất cho toàn bộ layout */
    --layout-gap: 15px; 
    --board-height: 460px;
    --board-size: 520px; /* độ rộng bàn cờ desktop */

    /* Biến đồng bộ Replay & Biên bản buttons */
    --replay-btn-import-bg: linear-gradient(135deg, #1f618d 0%, #154360 100%);
    --replay-btn-import-hover: linear-gradient(135deg, #2980b9 0%, #1f618d 100%);
    --replay-btn-import-border: #10344d;
    --replay-btn-import-shadow: 0 3px 8px rgba(21, 67, 96, 0.28);
    
    --replay-btn-export-bg: linear-gradient(135deg, #8b1e1e 0%, #6e1515 100%);
    --replay-btn-export-hover: linear-gradient(135deg, #a82626 0%, #8b1e1e 100%);
    --replay-btn-export-border: #5a1010;
    --replay-btn-export-shadow: 0 3px 8px rgba(110, 21, 21, 0.25);
}

/* Responsive cho bàn cờ */
@media (max-width: 576px) {
    :root { --board-size: 340px; } /* Thu nhỏ cho mobile */
    .div-lsd { height: 300px !important; }
}
@media (min-width: 577px) and (max-width: 991px) {
    :root { --board-size: 380px; }
}

.custom-container {
    padding-left: var(--layout-gap) !important;
    padding-right: var(--layout-gap) !important;
    width: 100% !important;
    max-width: 100% !important;
}

/* Ép chiều cao bảng danh sách */
.div-lsd {
    height: 520px; 
    overflow-y: auto;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #fff;
    padding: 5px;
}

/* Căn giữa bàn cờ trên mobile */
.replay-sticky-container {
    display: flex;
    justify-content: center;
    width: 100%;
}

@media (max-width: 768px) {
    .row.tight-gutter > .col-md-6 { margin-bottom: 20px; }
}

#historyTable thead th {
    position: sticky;
    top: 0;
    background-color: #f8f9fa;
    z-index: 10;
}

/* Giảm padding giữa 2 cột theo biến đã định nghĩa */
.row.tight-gutter {
    margin-left: calc(var(--layout-gap) * -0.5);
    margin-right: calc(var(--layout-gap) * -0.5);
}
.row.tight-gutter > [class*='col-'] {
    padding-left: calc(var(--layout-gap) * 0.5);
    padding-right: calc(var(--layout-gap) * 0.5);
}

/* Ép chiều cao bảng danh sách khớp với bàn cờ */
.div-lsd {
    height: var(--board-height); 
    overflow-y: auto;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #fff;
    padding: 5px;
}

#historyTable {
    margin-bottom: 0;
    width: 100%;
}

/* Thanh cuộn hiện đại */
.div-lsd::-webkit-scrollbar,
#move-list-ui::-webkit-scrollbar {
    width: 6px;
}
.div-lsd::-webkit-scrollbar-thumb,
#move-list-ui::-webkit-scrollbar-thumb {
    background-color: #d1bba2;
    border-radius: 10px;
}
.div-lsd::-webkit-scrollbar-thumb:hover,
#move-list-ui::-webkit-scrollbar-thumb:hover {
    background-color: #8b4513;
}

.table-style-1 tr:nth-child(even), tr:nth-child(even), .table-style-2 tr:nth-child(even), .table-style-1 tr:nth-child(odd), tr:nth-child(odd), .table-style-2 tr:nth-child(odd) {
    background: none;
}

/* Bàn cờ & Hiệu ứng */
.xiangqiboard table { border-collapse: collapse; border-spacing: 0; margin: 0 auto; width: 100%; height: 100%; table-layout: fixed; }
.xiangqiboard td { border: none; padding: 0; margin: 0; line-height: 0; text-align: center; vertical-align: middle; }
.piece-moving { position: fixed; z-index: 9999; transition: all 0.3s ease-in-out; pointer-events: none; object-fit: contain; }

/* Layout chính Replay */
.replay-container { 
    display: flex; 
    gap: 16px;
    justify-content: center; 
    align-items: stretch;
    flex-wrap: nowrap; 
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; 
    box-sizing: border-box;
    padding: 6px;
}
.board-section { 
    flex: 0 0 auto; 
    width: var(--board-size);
    border-radius: 8px; 
    overflow: hidden; 
    box-shadow: 0 4px 15px rgba(0,0,0,0.18); 
    border: 1px solid #8b5a2b;
    background: transparent;
}
#xiangqiboard_reply {
    width: 100% !important;
    height: 100% !important;
}
.xiangqiboard img {
    max-width: 96% !important;
    max-height: 96% !important;
    object-fit: contain !important;
    display: block !important;
    margin: 0 auto !important;
}

/* Panel bên phải điều khiển & danh sách nước */
.control-section { 
    flex: 0 0 320px; 
    width: 320px;
    background: #ffffff; 
    border: 1px solid #ebd8be; 
    border-radius: 8px; 
    display: flex; 
    flex-direction: column; 
    height: auto;
    min-height: calc(var(--board-size) * 1.05);
    box-shadow: 0 4px 12px rgba(43, 31, 20, 0.08); 
    box-sizing: border-box;
    overflow: hidden;
}

@media (max-width: 991px) {
    .replay-container {
        flex-wrap: wrap;
    }
    .control-section {
        width: var(--board-size);
        max-width: 100%;
        min-height: 380px;
        display: flex;
        flex-direction: column;
    }
    /* Đảo thứ tự trên mobile: Nút điều khiển lên trên, danh sách ở giữa, nút xuất file xuống dưới */
    .control-section .replay-controls {
        order: 1;
        border-top: none;
        border-bottom: 1px solid #ebd8be;
        border-radius: 8px 8px 0 0;
    }
    .control-section #move-list-ui {
        order: 2;
    }
    .control-section .import-export-section {
        order: 3;
        border-bottom: none;
        border-top: 1px solid #ebd8be;
        border-radius: 0 0 8px 8px;
    }
}

.move-list-header { 
    background: #8b4513; 
    color: #fff; 
    padding: 10px 12px; 
    font-weight: 700; 
    text-align: center; 
    border-radius: 8px 8px 0 0;
    letter-spacing: 0.5px;
    font-size: 13px;
    text-transform: uppercase;
}

/* Nút Xuất File / Nạp File */
.import-export-section { 
    padding: 10px 12px; 
    background: #fdfaf6; 
    border-bottom: 1px solid #ebd8be; 
    display: flex; 
    gap: 10px; 
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    border-radius: 8px 8px 0 0; 
    box-sizing: border-box;
}

.btn-modern {
    border: none !important;
    outline: none !important;
    cursor: pointer !important;
    padding: 7px 16px !important;
    border-radius: 6px;
    font-weight: 700 !important;
    font-size: 12.5px !important;
    transition: all 0.2s ease !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 7px !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    line-height: normal !important;
    box-sizing: border-box !important;
    text-decoration: none !important;
}

.btn-modern:hover {
    transform: translateY(-1px) !important;
}

.btn-modern:active {
    transform: scale(0.96) !important;
}

.btn-modern i,
.btn-replay i,
.replay-controls i {
    font-family: 'Font Awesome 6 Free', 'FontAwesome' !important;
    font-weight: 900 !important;
    font-style: normal !important;
    display: inline-block !important;
    font-size: 13.5px !important;
}

/* Nút Nạp File (Import) */
.btn-import {
    background: var(--replay-btn-import-bg, linear-gradient(135deg, #1f618d 0%, #154360 100%)) !important;
    color: #ffffff !important;
    border: 1px solid var(--replay-btn-import-border, #10344d) !important;
    box-shadow: var(--replay-btn-import-shadow, 0 3px 8px rgba(21, 67, 96, 0.28)) !important;
}
.btn-import:hover {
    background: var(--replay-btn-import-hover, linear-gradient(135deg, #2980b9 0%, #1f618d 100%)) !important;
    box-shadow: 0 5px 12px rgba(21, 67, 96, 0.42) !important;
    color: #ffffff !important;
}

/* Nút Xuất File (Export) */
.btn-export {
    background: var(--replay-btn-export-bg, linear-gradient(135deg, #8b1e1e 0%, #6e1515 100%)) !important;
    color: #ffffff !important;
    border: 1px solid var(--replay-btn-export-border, #5a1010) !important;
    box-shadow: var(--replay-btn-export-shadow, 0 3px 8px rgba(110, 21, 21, 0.25)) !important;
}
.btn-export:hover {
    background: var(--replay-btn-export-hover, linear-gradient(135deg, #a82626 0%, #8b1e1e 100%)) !important;
    box-shadow: 0 5px 12px rgba(110, 21, 21, 0.35) !important;
    color: #ffffff !important;
}

/* Danh sách nước đi dạng bảng mượt mà */
#move-list-ui { 
    flex-grow: 1; 
    overflow-y: auto; 
    list-style: none; 
    padding: 0; 
    margin: 0; 
    background: #ffffff;
    max-height: 400px;
}

#move-list-ui li { 
    display: flex;
    align-items: center;
    padding: 0;
    border-bottom: 1px solid #f2ece4;
    transition: background 0.15s;
    font-size: 13px;
}
#move-list-ui li:hover {
    background: #fdf8f0;
}

/* Cột số thứ tự nước đi */
.step-no { 
    width: 44px; 
    text-align: center; 
    color: #8c735d; 
    font-size: 12px; 
    background: #fcf9f5; 
    padding: 6px 0; 
    border-right: 1px solid #ebd8be; 
    font-weight: 600;
}
.move-item { 
    flex: 1; 
    text-align: center; 
    padding: 6px 4px; 
    cursor: pointer; 
    font-weight: 600;
    color: #2b1f14;
    border-radius: 4px;
    margin: 2px 4px;
    transition: all 0.15s ease;
}
.move-item:hover { 
    background: #f5ebe0; 
    color: #8b4513; 
}

/* Nước đi đang chọn - Tông Nâu Gỗ Hoàng Gia đồng bộ giải đấu */
.active-step { 
    background: #8b4513 !important; 
    color: #ffffff !important; 
    font-weight: 700 !important;
    box-shadow: 0 2px 6px rgba(139, 69, 19, 0.35) !important;
}

/* Controls - Khu vực nút điều khiển phát ván cờ */
.replay-controls { 
    padding: 10px 8px; 
    background: #fdfaf6; 
    border-top: 1px solid #ebd8be; 
    display: flex; 
    justify-content: center; 
    align-items: center;
    gap: 8px; 
    border-radius: 0 0 8px 8px; 
    box-sizing: border-box;
}

.btn-replay { 
    border-radius: 6px; 
    padding: 6px 14px !important; 
    font-size: 13.5px !important; 
    font-weight: 700 !important; 
    border: none !important; 
    cursor: pointer !important; 
    transition: all 0.2s ease !important; 
    display: inline-flex !important; 
    align-items: center !important; 
    justify-content: center !important; 
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08) !important; 
    min-width: 42px;
    height: 34px;
    line-height: normal !important;
}
.btn-replay:hover { 
    transform: translateY(-2px) !important; 
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15) !important; 
}
.btn-replay:active { 
    transform: scale(0.95) !important; 
}

/* Nút Play / Pause (Mặc định Xanh Lá Hoàng Gia #27ae60) */
#btn-play-pause,
.btn-replay.btn-success { 
    background: #27ae60 !important; 
    color: #ffffff !important; 
    border-color: #27ae60 !important;
}
#btn-play-pause:hover,
.btn-replay.btn-success:hover { 
    background: #219653 !important; 
    color: #ffffff !important;
}

/* Trạng thái khi đang tự động chạy (Tạm dừng - Đỏ Ruby #c0392b) */
#btn-play-pause.btn-danger,
.btn-replay.btn-danger { 
    background: #c0392b !important; 
    color: #ffffff !important; 
    border-color: #c0392b !important;
}
#btn-play-pause.btn-danger:hover,
.btn-replay.btn-danger:hover { 
    background: #a93226 !important; 
    color: #ffffff !important;
}

/* Nút Lùi / Tiến - Nâu Gỗ Hoàng Gia #8b4513 */
.btn-replay.btn-prev,
.btn-replay.btn-next,
.btn-replay.btn-primary { 
    background: #8b4513 !important; 
    color: #ffffff !important; 
    border-color: #8b4513 !important;
}
.btn-replay.btn-prev:hover,
.btn-replay.btn-next:hover,
.btn-replay.btn-primary:hover { 
    background: #6f370f !important; 
    color: #ffffff !important;
}

/* Nút Về đầu / Reset - Nâu Đậm Quý Phái #4a2f1b */
.btn-replay.btn-dark { 
    background: #4a2f1b !important; 
    color: #ffffff !important; 
    border-color: #4a2f1b !important;
}
.btn-replay.btn-dark:hover { 
    background: #352112 !important; 
    color: #ffffff !important;
}

/* Khung cấu hình tốc độ tự động */
.auto-play-config {
    margin: 12px 0 0 0;
    padding: 8px 14px;
    background: #fdfaf6;
    border-radius: 6px;
    border: 1px solid #ebd8be;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    font-size: 13px;
    font-weight: 600;
    color: #5c432d;
    box-sizing: border-box;
}

.auto-play-config select {
    padding: 4px 10px;
    border-radius: 4px;
    border: 1px solid #ebd8be;
    background: #ffffff;
    font-size: 12.5px;
    font-weight: 600;
    color: #2b1f14;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s;
}

.auto-play-config select:focus {
    border-color: #8b4513;
}