/* =========================================
   1. RESET & BASE STYLES
   ========================================= */
* { box-sizing: border-box; }

body { 
    margin: 0; 
    padding: 0; 
    background: #e9ecef; 
    font-family: "Segoe UI", sans-serif; 
    height: 100vh; 
    overflow: hidden; 
}

/* =========================================
   2. DESKTOP LAYOUT (Mặc định)
   ========================================= */

/* Main Wrapper */
#exam-wrapper { 
    display: flex; 
    flex-direction: column; 
    height: 92vh; 
    max-width: 1400px; 
    margin: 4vh auto; 
    background: #fff; 
    box-shadow: 0 0 20px rgba(0,0,0,0.1); 
    border-radius: 8px; 
    overflow: hidden; 
}

/* Header */
.exam-header { 
    flex: 0 0 60px; 
    background: #2c3e50; 
    color: #fff;
    padding: 0 20px; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    z-index: 100; 
    border-bottom: 1px solid #34495e;
}

.header-left { display: flex; align-items: center; overflow: hidden; }
.header-right { display: flex; align-items: center; }

.exam-title { 
    margin: 0; 
    font-size: 1.1rem; 
    font-weight: 600; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis;
    color: #ecf0f1;
}

.btn-finish { 
    background: #e74c3c; 
    color: #fff; 
    border: none; 
    padding: 6px 15px; 
    border-radius: 4px; 
    font-weight: 700; 
    cursor: pointer; 
    font-size: 0.9rem;
    white-space: nowrap;
}
.btn-finish:hover { background: #c0392b; }

#timer-box { 
    font-size: 1.2rem; 
    font-weight: 700; 
    color: #f1c40f; 
    margin-right: 15px; 
    background: rgba(255,255,255,0.1);
    padding: 2px 10px;
    border-radius: 4px;
    white-space: nowrap;
}

/* Audio Bar */
.audio-sticky-bar { 
    flex: 0 0 auto; 
    background: #34495e; 
    color: #fff; 
    padding: 8px; 
    text-align: center; 
    border-top: 1px solid #465b70;
}
.audio-tracks button { 
    background: #566e87; 
    border: 1px solid #666; 
    color: #fff; 
    font-size: 0.8rem; 
    margin: 0 3px; 
    padding: 3px 10px; 
    border-radius: 12px; 
    cursor: pointer; 
}

/* Exam Body */
.exam-body { 
    flex: 1; 
    display: flex; 
    overflow: hidden; 
    position: relative; 
}

/* Layout Panes */
.listening-layout .pane-right { 
    width: 100%; 
    max-width: 1000px; 
    margin: 0 auto; 
    background: #fff; 
}

.reading-layout .pane-left { 
    width: 50%; 
    border-right: 1px solid #ddd; 
    background: #fff; 
    height: 100%; 
    overflow-y: auto; 
    padding: 30px; 
    padding-bottom: 100px;
}

.pane-right { 
    width: 50%; 
    background: #fdfdfd; 
    height: 100%; 
    display: flex; /* Flex row cho menu dọc */
    overflow: hidden; 
}

/* Vertical Tabs (Desktop) */
.section-tabs { 
    display: flex; 
    flex-direction: column; 
    width: 110px; 
    background: #f8f9fa; 
    padding: 10px 5px; 
    border-right: 1px solid #ddd; 
    overflow-y: auto; 
    flex-shrink: 0;
}

.tab-btn { 
    padding: 10px 5px; 
    margin-bottom: 5px; 
    border: 1px solid transparent; 
    background: transparent; 
    color: #555; 
    font-weight: 600; 
    cursor: pointer; 
    border-radius: 4px; 
    text-align: center; 
    font-size: 0.85rem; 
    transition: all 0.2s;
}
.tab-btn:hover { background: #e9ecef; }
.tab-btn.active { background: #3498db; color: white; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }

/* Questions Container */
#questions-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    padding-bottom: 150px;
}

/* =========================================
   3. ELEMENT STYLES (Questions, Boxes)
   ========================================= */
.section-content { display: none; }
.section-content.active { display: block; animation: fadeIn 0.3s; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.instruction { font-style: italic; color: #555; margin-bottom: 15px; padding: 10px; background: #fff; border-left: 3px solid #e67e22; }
.notes-template { font-family: "Courier New", monospace; white-space: pre-wrap; background: #fdfdfd; padding: 15px; border: 1px dashed #ccc; margin-bottom: 20px; font-size: 1rem; line-height: 1.6; }

.question-box { border: 2px solid #444; padding: 15px; margin: 15px 0; background: #fff; }
.question-box h5 { margin-top: 0; text-decoration: underline; font-size: 1rem; color: #000; }
.question-box ul { list-style: none; padding: 0; margin: 0; }
.question-box li { font-weight: 500; margin-bottom: 4px; }

.subsection-container { margin: 20px 0; padding: 15px; background: #f8f9fa; border-left: 4px solid #3498db; border-radius: 4px; }
.question-group-title { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 8px; margin-bottom: 20px; font-size: 1.2rem; font-weight: bold; }

/* Question Items */
.q-item { margin-bottom: 15px; background: #fff; padding: 15px; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); display: flex; align-items: baseline; border: 1px solid #f0f0f0; }
.q-num { background: #34495e; color: #fff; width: 26px; height: 26px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-size: 0.8rem; font-weight: bold; margin-right: 12px; flex-shrink: 0; }
.q-content { flex: 1; }
.q-text { margin-bottom: 10px; font-weight: 500; color: #333; }
.input-text { width: 100%; max-width: 220px; padding: 6px 10px; border: 1px solid #ced4da; border-radius: 4px; margin-top: 5px; }

/* MCQ */
.mcq-group { margin-top: 10px; }
.mcq-option { display: flex; align-items: center; margin: 8px 0; padding: 10px; background: #fff; border: 1px solid #dee2e6; border-radius: 4px; cursor: pointer; transition: all 0.2s; }
.mcq-option:hover { background: #f1f3f5; border-color: #adb5bd; }
.mcq-option input { margin-right: 12px; transform: scale(1.2); }
.mcq-text { flex: 1; font-size: 0.95rem; }

/* =========================================
   4. MOBILE RESPONSIVE FIX (QUAN TRỌNG)
   ========================================= */
@media (max-width: 768px) {
    /* Cho phép cuộn tự nhiên */
    body { height: auto; overflow-y: auto; -webkit-overflow-scrolling: touch; }
    
    #exam-wrapper { 
        height: auto; 
        margin: 0; 
        border-radius: 0; 
        box-shadow: none; 
        max-width: 100%; 
        display: block; /* Bỏ Flex cột */
    }

    /* Header Sticky & Thoáng */
    .exam-header {
        position: sticky;
        top: 0;
        height: 60px; 
        padding: 0 10px;
        background: #2c3e50;
        box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    }

    /* Ẩn Tiêu đề và nút Lịch sử để dành chỗ cho Nộp bài */
    .exam-title, .header-left a { display: none !important; }
    
    /* Căn chỉnh lại Header Right để chiếm toàn bộ */
    .header-right {
        width: 100%;
        display: flex;
        justify-content: space-between; /* Timer trái, Nút phải */
        align-items: center;
    }
    
    #timer-box { 
        margin-right: 0; 
        font-size: 1.1rem;
        background: transparent;
        color: #f1c40f;
    }

    /* Audio Bar Sticky ngay dưới Header */
    .audio-sticky-bar {
        position: sticky;
        top: 60px; /* Dưới header 60px */
        z-index: 99;
    }

    /* Layout Body */
    .exam-body { 
        display: block; 
        height: auto; 
        overflow: visible; 
    }

    /* Reading Layout Mobile */
    .reading-layout .pane-left {
        width: 100%;
        height: 350px; /* Chiều cao cố định cho bài đọc */
        resize: vertical; /* Cho phép user kéo giãn */
        overflow-y: auto;
        border-right: none;
        border-bottom: 5px solid #ccc;
        padding: 15px;
    }

    .pane-right, 
    .reading-layout .pane-right,
    .listening-layout .pane-right {
        width: 100%;
        display: block; /* Bỏ flex row */
        height: auto;
        overflow: visible;
        background: #fff;
        padding: 0;
    }

    /* Menu Tabs chuyển thành Ngang */
    .section-tabs {
        width: 100%;
        flex-direction: row;
        overflow-x: auto;
        white-space: nowrap;
        padding: 8px;
        border-right: none;
        border-bottom: 1px solid #ddd;
        position: sticky;
        top: 60px; /* Dính dưới header (nếu listening thì + chiều cao audio) */
        z-index: 98;
        background: #f8f9fa;
    }
    
    /* Điều chỉnh vị trí dính nếu có Audio */
    .listening-layout .section-tabs {
        top: 100px; /* Header 60 + Audio 40 */
    }

    .tab-btn {
        margin-right: 10px;
        margin-bottom: 0;
        padding: 6px 15px;
        flex: 0 0 auto;
    }

    /* Container câu hỏi */
    #questions-container {
        padding: 15px;
        overflow: visible;
        padding-bottom: 80px; /* Cách đáy để không bị che */
    }
}