:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100dvh !important;
    max-height: 100dvh !important;
    width: 100vw !important;
    margin: 0;
    padding: 0;
    background: #060913;
    overflow: hidden !important;
    overscroll-behavior: none !important;
    touch-action: manipulation !important;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

* {
    touch-action: manipulation;
}

/* Khung App Điện Thoại Full Màn Hình */
#mobileAppShell {
    width: 100vw;
    max-width: 500px;
    height: 100dvh;
    margin: 0 auto;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background: radial-gradient(circle at 50% 35%, #131b31 0%, #070b16 75%, #04070f 100%);
}

#screenAuth,
#screenModeSelect {
    padding-top: calc(var(--safe-top) + 10px) !important;
    padding-bottom: calc(var(--safe-bottom) + 12px) !important;
}

.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }

/* ==========================================================================
   CỤM SA BÀN LIỀN MẠCH CHUẨN APP CHESS.COM (KHÔNG KHOẢNG TRỐNG ĐEN)
   ========================================================================== */
main {
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center; /* Gom toàn bộ cụm Bàn cờ + 2 Thẻ kỳ thủ vào tâm màn hình */
    align-items: center;
    gap: 6px;
    padding: 4px 0;
}

/* Thẻ Kỳ Thủ gắn sát ngay trên & dưới bàn cờ */
#topPlayerCard,
#bottomPlayerCard {
    width: calc(100% - 8px);
    margin: 0 auto;
    padding: 7px 10px;
    border-radius: 12px;
    flex-shrink: 0;
}

.board-with-eval-wrapper {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
}

/* Thanh Eval Bar nằm ngang tràn 100% theo chiều rộng bàn cờ */
.eval-bar-container {
    width: min(100vw, calc(100dvh - 195px - var(--safe-top) - var(--safe-bottom)), 500px);
    height: 6px;
    background-color: #991b1b;
    border-top: 1px solid rgba(255, 255, 255, 0.15);
    border-bottom: 1px solid rgba(0, 0, 0, 0.6);
    overflow: hidden;
    position: relative;
    display: flex;
    flex-direction: row;
    flex-shrink: 0;
}
.eval-bar-fill-blue {
    height: 100% !important;
    width: 50%;
    background: linear-gradient(90deg, #0284c7 0%, #38bdf8 100%);
    transition: width 0.4s ease;
}
.eval-bar-score {
    display: none;
}

/* BÀN CỜ TRÀN SÁT MÉP MÀN HÌNH (MỞ RỘNG TỐI ĐA KHI BỎ THANH LỆNH XUẤT KÍCH) */
.chessboard-container {
    aspect-ratio: 1 / 1;
    width: min(100vw, calc(100dvh - 195px - var(--safe-top) - var(--safe-bottom)), 500px);
    touch-action: none;
    box-shadow: 
        0 10px 30px rgba(0, 0, 0, 0.9),
        0 -2px 0 #d97706,
        0 2px 0 #d97706;
    border-radius: 2px;
    overflow: hidden;
    position: relative;
    flex-shrink: 0;
}

.chess-grid {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(8, minmax(0, 1fr));
    width: 100%;
    height: 100%;
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

/* Chủ đề bàn cờ */
.theme-donga .sq-light { background-color: #f3e5ab; color: #78350f; }
.theme-donga .sq-dark { background-color: #92400e; color: #fef3c7; }
.theme-jade .sq-light { background-color: #e2e8f0; color: #334155; }
.theme-jade .sq-dark { background-color: #475569; color: #f8fafc; }
.theme-obsidian .sq-light { background-color: #cbd5e1; color: #0f172a; }
.theme-obsidian .sq-dark { background-color: #1e293b; color: #f8fafc; }

/* Đánh dấu Ô Cờ */
.sq-selected {
    background-color: rgba(250, 204, 21, 0.78) !important;
    box-shadow: inset 0 0 0 3px #eab308;
}
.sq-last-move {
    background-color: rgba(251, 191, 36, 0.45) !important;
}
.sq-advisor-hint {
    background-color: rgba(16, 185, 129, 0.6) !important;
    box-shadow: inset 0 0 0 3px #10b981;
}
.sq-check {
    background: radial-gradient(circle, rgba(239,68,68,0.95) 0%, rgba(185,28,28,0.75) 65%, transparent 100%) !important;
}

/* Chấm gợi ý nước đi */
.valid-move-dot {
    width: 36%;
    height: 36%;
    background: radial-gradient(circle, #34d399 25%, rgba(5, 150, 105, 0.92) 100%);
    border: 2px solid #ffffff;
    border-radius: 50%;
    pointer-events: none;
    z-index: 20;
    box-shadow: 0 2px 6px rgba(0,0,0,0.5);
}

.valid-capture-ring {
    position: absolute;
    inset: 4%;
    border: 3.5px dashed #ef4444;
    background-color: rgba(239, 68, 68, 0.22);
    border-radius: 50%;
    pointer-events: none;
    z-index: 20;
}

.coord-rank {
    position: absolute;
    top: 2px;
    left: 3px;
    font-size: 9.5px;
    font-weight: 800;
    line-height: 1;
    pointer-events: none;
    opacity: 0.85;
    z-index: 5;
}
.coord-file {
    position: absolute;
    bottom: 2px;
    right: 3px;
    font-size: 9.5px;
    font-weight: 800;
    line-height: 1;
    pointer-events: none;
    opacity: 0.85;
    z-index: 5;
}

/* ==========================================================================
   QUÂN CỜ 3D TO RÕ SẮC NÉT TRÊN ĐIỆN THOẠI (94% Ô CỜ)
   ========================================================================== */
.piece-token {
    width: 94%;
    height: 94%;
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    z-index: 10;
}

.token-mystery-blue {
    background: radial-gradient(circle at 35% 25%, #1e3a8a 0%, #090d16 90%);
    border: 2px dashed #38bdf8;
    box-shadow: 0 3px 0 #082f49, 0 5px 10px rgba(0, 0, 0, 0.65);
}
.token-mystery-red {
    background: radial-gradient(circle at 35% 25%, #7f1d1d 0%, #160606 90%);
    border: 2px dashed #f87171;
    box-shadow: 0 3px 0 #450a0a, 0 5px 10px rgba(0, 0, 0, 0.65);
}
.token-real-blue {
    background: radial-gradient(circle at 30% 25%, #ffffff 0%, #e0f2fe 48%, #7dd3fc 100%);
    border: 2.5px solid #0284c7;
    box-shadow: 0 3px 0 #0369a1, 0 5px 10px rgba(0, 0, 0, 0.55);
}
.token-real-red {
    background: radial-gradient(circle at 30% 25%, #ffffff 0%, #fee2e2 48%, #fca5a5 100%);
    border: 2.5px solid #dc2626;
    box-shadow: 0 3px 0 #991b1b, 0 5px 10px rgba(0, 0, 0, 0.55);
}
.token-king-blue {
    background: linear-gradient(145deg, #fef3c7 0%, #38bdf8 45%, #0369a1 100%);
    border: 2.5px solid #facc15;
    box-shadow: 0 3px 0 #854d0e, 0 0 12px rgba(250, 204, 21, 0.6);
}
.token-king-red {
    background: linear-gradient(145deg, #fef3c7 0%, #f87171 45%, #b91c1c 100%);
    border: 2.5px solid #facc15;
    box-shadow: 0 3px 0 #7f1d1d, 0 0 12px rgba(250, 204, 21, 0.6);
}

.cburnett-svg {
    width: 80%;
    height: 80%;
    object-fit: contain;
    pointer-events: none;
    margin-top: -4px;
    filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.55));
}

.piece-token span {
    font-size: 9.5px !important;
    font-weight: 900 !important;
    line-height: 1 !important;
}

.mystery-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    width: 17px;
    height: 17px;
    border-radius: 50%;
    background: linear-gradient(135deg, #fde047 0%, #f59e0b 100%);
    color: #090d16;
    font-size: 11px;
    font-weight: 900;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1.5px solid #ffffff;
}

.pawn-lock-badge {
    position: absolute;
    bottom: -4px;
    left: 50%;
    transform: translateX(-50%);
    background: #eab308;
    color: #000;
    font-size: 8px;
    font-weight: 900;
    padding: 1px 4px;
    border-radius: 4px;
}

/* Kéo thả quân hiển thị cao hơn đầu ngón tay để không bị ngón tay che ô cờ */
.dragging-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    transform: translate(-50%, -75%) scale(1.28);
    filter: drop-shadow(0 18px 24px rgba(0, 0, 0, 0.85));
}
.piece-dragging-source {
    opacity: 0.25 !important;
}
.sq-drag-hover {
    box-shadow: inset 0 0 0 4px #38bdf8 !important;
}

/* Ngăn kéo Bottom Sheet trượt từ đáy */
.mobile-bottom-sheet {
    transform: translateY(105%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}
.mobile-bottom-sheet.sheet-open {
    transform: translateY(0%);
}

/* Hiệu ứng chiến đấu & Premove */
.tactical-svg-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 28;
}
.sq-premove-selected, .sq-premove-source {
    background-color: rgba(168, 85, 247, 0.55) !important;
}
.sq-premove-target {
    background-color: rgba(239, 68, 68, 0.65) !important;
}
.premove-move-dot {
    width: 30%;
    height: 30%;
    background: #e879f9;
    border-radius: 50%;
}
.premove-capture-ring {
    position: absolute;
    inset: 6%;
    border: 3px dashed #e879f9;
    border-radius: 50%;
}

/* ==========================================================================
   CHẾ ĐỘ TẬP TRUNG TOÀN MÀN HÌNH (KHÔNG ĐÈ ĐỒNG HỒ, KHÔNG KHOẢNG TRỐNG ĐEN)
   ========================================================================== */
#exitZenFloatingBtn {
    display: none;
}

body.zen-focus-mode header,
body.zen-focus-mode #onlineRoomBar {
    display: none !important;
}

body.zen-focus-mode #exitZenFloatingBtn {
    display: inline-flex !important;
}

/* Trong chế độ tập trung, bàn cờ mở rộng tối đa 100vw và cụm thẻ kỳ thủ ôm sát bàn cờ */
body.zen-focus-mode .chessboard-container,
body.zen-focus-mode .eval-bar-container {
    width: min(100vw, calc(100dvh - 210px - var(--safe-top) - var(--safe-bottom)), 500px) !important;
}