/* =========================================
   1. CẤU HÌNH MÀU SẮC & BIẾN (VARIABLES)
   ========================================= */
:root {
    --primary: #4FACFE;             /* Xanh dương tươi */
    --primary-dark: #00F2FE;        /* Xanh cyan */
    --accent: #ff9f43;              /* Cam nổi bật */
    --bg-grad: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); /* Nền gradient */
    --white: #ffffff;
    --text: #2d3436;
    --font: 'Segoe UI', 'Roboto', Helvetica, Arial, sans-serif;
    --radius: 24px;                 /* Độ bo tròn */
    --shadow-card: 0 15px 40px rgba(0,0,0,0.12);
}

#nak-chess-wrapper * { box-sizing: border-box; }

/* Wrapper chính: Giả lập điện thoại trên Desktop */
#nak-chess-wrapper {
    position: relative;
    width: 100%; max-width: 480px;
    height: 90vh; min-height: 700px;
    margin: 20px auto;
    background: var(--bg-grad);
    border-radius: 35px;
    overflow: hidden;
    font-family: var(--font-family);
    box-shadow: 0 20px 50px rgba(0,0,0,0.2);
    border: 8px solid #333; /* Viền đen giả lập điện thoại */
}

/* =========================================
   2. TRANG TRÍ NỀN (BACKGROUND DECOR)
   ========================================= */
.bg-decoration {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: radial-gradient(circle at 10% 20%, rgba(255, 255, 255, 0.6) 0%, transparent 20%),
                radial-gradient(circle at 90% 80%, rgba(255, 255, 255, 0.6) 0%, transparent 20%);
    z-index: 0;
}

.floating-icon {
    position: absolute; font-size: 32px;
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.1));
    animation: floatIcon 6s ease-in-out infinite;
    z-index: 1; opacity: 0.8;
}
.icon-1 { top: 15%; left: 10%; animation-delay: 0s; }
.icon-2 { bottom: 20%; right: 10%; animation-delay: 2s; font-size: 40px; }
.icon-3 { top: 50%; right: 5%; animation-delay: 4s; font-size: 25px; }

@keyframes floatIcon {
    0%, 100% { transform: translateY(0) rotate(0deg); }
    50% { transform: translateY(-20px) rotate(10deg); }
}

/* =========================================
   3. MÀN HÌNH CHÀO MỪNG (START SCREEN)
   ========================================= */
#nak-start-screen {
    position: absolute; inset: 0;
    display: flex; justify-content: center; align-items: center;
    background: rgba(255,255,255,0.3);
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    z-index: 100; padding: 20px;
}

/* Thẻ Card nổi */
.start-card {
    background: rgba(255, 255, 255, 0.95);
    padding: 30px 20px;
    border-radius: 30px;
    width: 100%;
    box-shadow: var(--shadow-card);
    text-align: center;
    position: relative;
    animation: popUp 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes popUp { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.card-header h2 { margin: 0 0 5px 0; color: #6c5ce7; font-size: 26px; font-weight: 800; }
.subtitle { font-size: 13px; color: #a29bfe; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; }

/* Avatar Container */
.avatar-container { margin: 25px auto; position: relative; width: 130px; height: 130px; }

.avatar-ring {
    position: absolute; inset: -6px; border-radius: 50%;
    background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%);
    animation: spinRing 4s linear infinite; z-index: 1;
}
@keyframes spinRing { 100% { transform: rotate(360deg); } }

/* Avatar Image: object-fit cover để ảnh chữ nhật vẫn tròn */
.avatar-img {
    width: 100%; height: 100%; 
    border-radius: 50%; 
    object-fit: cover; 
    position: relative; z-index: 2; 
    border: 4px solid #fff;
    background: #fff;
}

.name-tag {
    position: absolute; bottom: -12px; left: 50%; transform: translateX(-50%);
    background: linear-gradient(to right, #ff9f43, #ffca28);
    color: #fff; padding: 5px 15px; border-radius: 20px;
    font-size: 13px; font-weight: bold; z-index: 3;
    box-shadow: 0 4px 10px rgba(0,0,0,0.15); white-space: nowrap;
}

/* Form Styles */
.form-group input {
    width: 100%; padding: 15px; border: 2px solid #f1f2f6;
    border-radius: 20px; text-align: center; font-size: 16px;
    margin-bottom: 20px; outline: none; transition: 0.3s;
    background: #f8f9fa; color: #2d3436; font-weight: 600;
}
.form-group input:focus { border-color: #74b9ff; background: #fff; box-shadow: 0 0 0 4px rgba(116, 185, 255, 0.1); }
.form-group input::placeholder { color: #b2bec3; font-weight: normal; }

/* Difficulty Buttons */
.difficulty-selector { display: flex; gap: 8px; margin-bottom: 25px; }
.difficulty-selector label { flex: 1; cursor: pointer; }
.difficulty-selector input { display: none; }
.diff-btn {
    display: block; padding: 12px 5px; border-radius: 18px;
    font-size: 13px; font-weight: bold; color: #b2bec3;
    background: #f1f2f6; transition: 0.2s; border: 2px solid transparent;
}
.difficulty-selector input:checked + .diff-btn.easy { background: #e0f9b5; color: #6ab04c; border-color: #badc58; transform: translateY(-3px); }
.difficulty-selector input:checked + .diff-btn.medium { background: #dff9fb; color: #22a6b3; border-color: #7ed6df; transform: translateY(-3px); }
.difficulty-selector input:checked + .diff-btn.hard { background: #ffcccc; color: #eb4d4b; border-color: #ff7979; transform: translateY(-3px); }

/* Start Button */
.btn-start {
    width: 100%; padding: 18px; border: none; border-radius: 25px;
    background: linear-gradient(45deg, #6c5ce7, #a29bfe);
    color: white; font-weight: 800; font-size: 18px; cursor: pointer;
    box-shadow: 0 8px 20px rgba(108, 92, 231, 0.3);
    transition: transform 0.2s, box-shadow 0.2s;
}
.btn-start:active { transform: scale(0.96); box-shadow: 0 4px 10px rgba(108, 92, 231, 0.2); }

/* Resume Area (Nút tiếp tục) */
#resume-area { margin-bottom: 20px; animation: fadeIn 0.5s; }
.resume-box {
    background: #e3f2fd; padding: 15px; border-radius: 20px;
    border: 2px solid #2196f3; animation: pulse 2s infinite;
}
@keyframes pulse { 
    0% { box-shadow: 0 0 0 0 rgba(33, 150, 243, 0.4); } 
    70% { box-shadow: 0 0 0 10px rgba(33, 150, 243, 0); } 
    100% { box-shadow: 0 0 0 0 rgba(33, 150, 243, 0); } 
}

.btn-resume {
    background: #2196f3; color: white; border: none; padding: 12px 25px;
    border-radius: 25px; font-weight: bold; cursor: pointer;
    display: inline-flex; align-items: center; gap: 8px; font-size: 15px; width: 100%; justify-content: center;
}
.divider { margin-top: 15px; font-size: 11px; color: #b2bec3; font-weight: bold; letter-spacing: 1px; }

/* =========================================
   4. GIAO DIỆN CHƠI (GAME UI)
   ========================================= */
#nak-game-ui { display: flex; flex-direction: column; height: 100%; }

/* Header */
.nak-header {
    flex-shrink: 0; display: flex; justify-content: space-between; align-items: center;
    padding: 8px 15px; margin: 10px; background: rgba(255,255,255,0.95);
    border-radius: 20px; box-shadow: 0 5px 15px rgba(0,0,0,0.05); z-index: 20;
}
.player-pill { display: flex; align-items: center; gap: 10px; }
.avatar-sm img { 
    width: 42px; height: 42px; border-radius: 50%; 
    border: 2px solid #fff; box-shadow: 0 2px 5px rgba(0,0,0,0.1); 
    object-fit: cover; 
}
.status-badge { font-size: 12px; color: #e17055; font-weight: bold; }
.icon-btn {
    width: 38px; height: 38px; border-radius: 50%; border: none;
    background: #f1f2f6; color: #636e72; cursor: pointer;
    transition: 0.2s; display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: #dfe6e9; }

/* Bàn cờ */
#board-area {
    flex: 1; display: flex; justify-content: center; align-items: center;
    perspective: 1000px; padding: 0 10px; z-index: 10;
}
#board-frame {
    width: 100%; max-width: 400px; padding: 8px; background: #fff;
    border-radius: 12px; box-shadow: 0 15px 40px rgba(0,0,0,0.1);
    transform: rotateX(5deg); transition: 0.3s;
}
.board-b72b1 { border: none !important; }
.square-55d63 img { width: 100%; }

/* =========================================
   5. CHAT BOX & FIX INPUT
   ========================================= */
#nak-chat-box {
    position: relative;
    height: 42%; background: #fff;
    border-radius: 30px 30px 0 0;
    box-shadow: 0 -10px 40px rgba(0,0,0,0.08);
    display: flex; flex-direction: column;
    z-index: 50;
    transition: height 0.3s ease;
}
#nak-chat-box.hidden { height: 65px; }

.chat-header-handle { padding: 12px; text-align: center; cursor: pointer; flex-shrink: 0; }
.handle-bar { display: inline-block; width: 45px; height: 5px; background: #dfe6e9; border-radius: 5px; }

#chat-history {
    flex: 1; overflow-y: auto; padding: 10px 15px;
    background: #f8f9fa;
    display: flex; flex-direction: column; gap: 10px;
    scroll-behavior: smooth;
}

.msg { padding: 10px 14px; border-radius: 18px; font-size: 14px; max-width: 85%; line-height: 1.4; word-wrap: break-word; }
.msg-khang { background: #fff; border: 1px solid #eee; align-self: flex-start; border-bottom-left-radius: 4px; box-shadow: 0 2px 5px rgba(0,0,0,0.03); }
.msg-user { background: #6c5ce7; color: white; align-self: flex-end; border-bottom-right-radius: 4px; box-shadow: 0 2px 5px rgba(108, 92, 231, 0.3); }

/* WRAPPER Ô NHẬP LIỆU (QUAN TRỌNG: FIX LỖI MOBILE) */
.chat-input-wrapper {
    background: #fff;
    padding: 10px 15px;
    border-top: 1px solid #f1f2f6;
    /* Mặc định cho Desktop */
    padding-bottom: 15px; 
}

.chat-input-area { display: flex; gap: 10px; align-items: center; }

#chat-input {
    flex: 1; padding: 12px 18px; border-radius: 25px;
    border: 1px solid #dfe6e9; background: #f1f2f6; outline: none; font-size: 15px;
}
#chat-input:focus { background: #fff; border-color: #a29bfe; }

#send-chat {
    width: 45px; height: 45px; border-radius: 50%; border: none;
    background: #6c5ce7; color: white; font-size: 16px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: 0.2s;
}
#send-chat:hover { transform: scale(1.05); }

/* Typing */
#typing-indicator { padding-left: 20px; margin-bottom: 5px; height: 15px; color: #b2bec3; line-height: 5px; display: flex; gap: 2px;}
#typing-indicator span { animation: blink 1.4s infinite both; }
#typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
#typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0% { opacity: 0.2; } 20% { opacity: 1; } 100% { opacity: 0.2; } }

/* =================================================================
   6. MOBILE SPECIFIC (FIX HOÀN TOÀN CÁC LỖI HIỂN THỊ)
   ================================================================= */
@media (max-width: 480px) {
    #nak-chess-wrapper {
        /* Sử dụng dvh để fix chiều cao thay đổi do thanh địa chỉ */
        height: 100dvh; 
        width: 100vw;
        max-width: none; margin: 0; border: none; border-radius: 0;
        position: fixed; top: 0; left: 0; z-index: 99999;
    }

    /* Avatar & Start Screen Mobile */
    .avatar-container { width: 110px; height: 110px; margin: 15px auto; }
    .card-header h2 { font-size: 22px; }
    .input-group input { padding: 12px; }

    /* 1. ĐẨY BÀN CỜ LÊN SÁT TRÊN */
    .nak-header {
        margin: 0; border-radius: 0; padding: 5px 10px;
        border-bottom: 1px solid #f0f0f0; flex-shrink: 0;
    }
    .avatar-sm img { width: 36px; height: 36px; }
    
    #board-area {
        /* Align start để đẩy lên trên */
        align-items: flex-start; 
        padding-top: 5px; /* Cách header 5px */
        perspective: none; /* Bỏ 3D để tiết kiệm không gian */
    }
    #board-frame {
        transform: none; /* Phẳng */
        box-shadow: none;
        padding: 0; /* Bỏ padding khung */
        max-width: 98vw; /* Rộng tối đa */
    }

    /* 2. KHUNG CHAT TO HƠN */
    #nak-chat-box {
        height: 50%; /* Chiếm 50% màn hình */
        border-radius: 20px 20px 0 0;
    }

    /* 3. FIX LỖI Ô NHẬP LIỆU BỊ LẤP (QUAN TRỌNG NHẤT) */
    .chat-input-wrapper {
        /* Đẩy lên rất cao (80px) để chắc chắn không bị bàn phím che */
        padding-bottom: 80px; 
        /* Thêm an toàn cho iPhone X+ */
        padding-bottom: calc(80px + env(safe-area-inset-bottom));
        padding-bottom: calc(80px + constant(safe-area-inset-bottom));
    }
}