/* =========================================
   1. THIẾT LẬP CƠ BẢN
   ========================================= */
body {
    font-family: Arial, sans-serif;
    background-color: #f0f4f8;
    color: #333;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    padding: 10px;
    margin: 0;
}

#app-container {
    width: 100%;
    max-width: 800px;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    /* QUAN TRỌNG: Để tính năng sticky hoạt động */
    overflow: visible; 
}

.hidden {
    display: none !important;
}

/* =========================================
   2. HEADER & MENU (TRANG CHỦ)
   ========================================= */
#mode-selection {
    padding: 20px 25px;
}

/* Header: Logo Trái - Text Phải */
.home-header {
    display: flex;
    flex-direction: row; /* Xếp ngang */
    align-items: center; /* Căn giữa dọc */
    justify-content: flex-start; /* Căn trái */
    padding-bottom: 15px;
    margin-bottom: 15px;
    border-bottom: 2px solid #0d47a1;
    text-align: left;
}

.app-logo {
    width: 80px;
    height: 80px;
    margin-right: 20px; /* Khoảng cách logo và chữ */
    margin-bottom: 0;
    border-radius: 50%;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    transition: transform 0.3s ease;
    flex-shrink: 0; /* Không bị co lại khi màn hình nhỏ */
}

.app-logo:hover {
    transform: scale(1.05);
}

.header-text {
    display: flex;
    flex-direction: column;
}

.home-header h1 {
    color: #0d47a1;
    margin: 0 0 5px 0;
    font-size: 1.4em;
    text-transform: uppercase;
    line-height: 1.2;
}

.home-header p {
    font-size: 0.95em;
    color: #555;
    margin: 0;
}

/* Các nút chọn chế độ */
.mode-options {
    margin-top: 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.mode-row {
    display: flex;
    gap: 10px;
    width: 100%;
}

.mode-row button {
    flex: 1;
}

.mode-options button {
    padding: 15px;
    font-size: 1.05em;
    border: 2px solid #0d47a1;
    background-color: #e3f2fd;
    color: #0d47a1;
    border-radius: 8px;
    cursor: pointer;
    text-align: center; /* Căn giữa chữ trong nút */
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: background-color 0.2s;
}

.mode-options button:hover {
    background-color: #bbdefb;
}

.mode-options button strong {
    display: block;
    margin-bottom: 4px;
    text-transform: uppercase;
}

.mode-options button span {
    font-size: 0.85em;
    color: #1976d2;
}

.toggle-topics-btn {
    background-color: #e8eaf6 !important;
    border-style: dashed !important;
    color: #3f51b5 !important;
}

.toggle-topics-btn:hover {
    background-color: #c5cae9 !important;
}

/* Tiêu đề nhóm */
.mode-group-title {
    font-size: 1.1em;
    font-weight: bold;
    color: #555;
    margin-top: 15px;
    margin-bottom: 0;
    text-align: left;
    border-bottom: 1px solid #eee;
    padding-bottom: 3px;
}

.mode-options .mode-group-title:first-of-type {
    margin-top: 0;
}

/* Menu Chủ đề (Grid 3 Cột) */
.topic-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr); /* Cố định 3 cột */
    gap: 6px;
    margin-top: 5px;
    padding: 8px;
    background-color: #fafafa;
    border: 1px solid #ddd;
    border-radius: 8px;
    animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

.topic-btn {
    padding: 8px 10px;
    font-size: 0.85em;
    border: 1px solid #90caf9;
    background-color: white;
    color: #1565c0;
    border-radius: 5px;
    cursor: pointer;
    text-align: left;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.topic-btn:hover {
    background-color: #1976d2;
    color: white;
    border-color: #1976d2;
}

.topic-header {
    display: flex;
    align-items: flex-start;
    gap: 4px;
    font-weight: bold;
    line-height: 1.1;
}

.topic-number {
    background-color: #e3f2fd;
    color: #1565c0;
    padding: 1px 3px;
    border-radius: 3px;
    font-size: 0.8em;
    flex-shrink: 0;
}

.topic-info {
    font-size: 0.8em;
    color: #666;
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-top: 2px;
}

.topic-btn:hover .topic-number,
.topic-btn:hover .topic-info {
    color: #e3f2fd;
    background-color: transparent;
}

/* Bảng quy định điểm (Thu gọn) */
.exam-rules {
    margin-top: 15px;
    padding: 10px;
    background-color: #f9f9f9;
    border-radius: 8px;
    text-align: center;
    font-size: 0.85em;
    color: #444;
}

.exam-rules p {
    font-weight: bold;
    margin-bottom: 8px;
    margin-top: 0;
}

.rules-table {
    width: auto; /* Tự co lại theo nội dung */
    min-width: 300px;
    margin: 0 auto; /* Căn giữa bảng */
    border-collapse: collapse;
    background-color: white;
    font-size: 1em;
}

.rules-table th,
.rules-table td {
    border: 1px solid #ddd;
    padding: 5px 10px;
    text-align: center;
}

.rules-table th {
    background-color: #e3f2fd;
    color: #0d47a1;
    font-weight: bold;
}

.rules-table tr:nth-child(even) {
    background-color: #f2f2f2;
}

.rules-table .failed-row {
    color: #c62828;
    font-weight: bold;
}

/* =========================================
   3. FOOTER & DONATE (CĂN CHỈNH TUYỆT ĐỐI)
   ========================================= */
footer {
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid #eee;
    font-size: 0.85em;
    position: relative; /* Mốc tọa độ */
    display: flex;
    justify-content: flex-end; /* Căn nội dung con (Donate) sang phải */
    align-items: center;
    min-height: 100px; /* Đảm bảo đủ chiều cao cho layout */
}

/* Khối bao quanh Slogan và Email để căn giữa cả cụm */
.footer-branding {
    position: absolute; /* Tách khỏi dòng chảy */
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%); /* Căn giữa dọc tuyệt đối */
    text-align: center;
    width: 100%;
    z-index: 0;
    
    display: flex;
    flex-direction: column;
    gap: 5px;
    
    /* Tránh đè lên phần donate bên phải nếu màn hình hẹp */
    padding-right: 160px; 
    padding-left: 20px;
    box-sizing: border-box;
}

.footer-slogan {
    font-style: italic;
    font-weight: normal;
    color: #1565c0; /* Màu xanh */
    margin: 0;
}

.footer-email {
    font-size: 0.9em;
    color: #757575; /* Màu xám nhạt */
    font-style: normal;
    margin-top: 15px;
}

.donation-container {
    display: flex;
    flex-direction: column; /* Text trên, QR dưới */
    align-items: flex-end;  /* Căn lề phải */
    text-align: right;
    gap: 5px;
    
    position: relative;
    z-index: 1; /* Nổi lên trên */
    background-color: rgba(255, 255, 255, 0.9); /* Nền mờ che text nếu bị chồng */
    padding-left: 10px;
}

.donation-text {
    font-size: 0.95em;
    color: #0d47a1;
    font-weight: bold;
    white-space: nowrap;
}

.qr-code-img {
    width: 80px;
    height: auto;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 2px;
    background: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

/* =========================================
   4. GIAO DIỆN THI (QUIZ)
   ========================================= */
#quiz-container {
    padding: 20px;
}

#quiz-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
    font-size: 1em;
    gap: 10px;
}

#back-to-menu-btn {
    background-color: #f5f5f5;
    color: #555;
    border: 1px solid #ddd;
    padding: 6px 10px;
    font-size: 0.9em;
    font-weight: bold;
    border-radius: 5px;
    cursor: pointer;
}

#quiz-mode-title {
    font-weight: bold;
    color: #0d47a1;
    flex-grow: 1;
    text-align: center;
    text-transform: uppercase;
}

#timer-container {
    color: #d32f2f;
    font-weight: bold;
    white-space: nowrap;
}

#header-submit-btn {
    background-color: #2e7d32;
    color: white;
    border: none;
    padding: 6px 12px;
    font-size: 0.9em;
    font-weight: bold;
    border-radius: 5px;
    cursor: pointer;
}

/* Ma trận câu hỏi (Khi thi) */
#question-matrix {
    display: grid;
    grid-template-columns: repeat(10, 35px);
    gap: 4px;
    justify-content: center;
    margin-top: 20px;
    padding-top: 15px;
    border-top: 1px solid #eee;
}

.matrix-box {
    width: 35px;
    height: 26px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.85em;
    font-weight: bold;
    border: 2px solid #ccc;
    border-radius: 4px;
    background-color: #f5f5f5;
    color: #555;
    cursor: pointer;
    transition: all 0.2s;
}

.matrix-box:hover { background-color: #e0e0e0; }
.matrix-box.answered { background-color: #e3f2fd; border-color: #1976d2; color: #0d47a1; }
.matrix-box.current { border-color: #d32f2f; color: #d32f2f; transform: scale(1.1); box-shadow: 0 0 5px rgba(211, 47, 47, 0.5); }

#question-text {
    font-size: 1.05em;
    font-weight: bold;
    margin-bottom: 20px;
    line-height: 1.4;
}

#options-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.option-btn {
    width: 100%;
    padding: 8px 10px;
    font-size: 0.95em;
    text-align: left;
    background-color: #f5f5f5;
    border: 2px solid #ddd;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    height: 100%;
}

.option-btn:hover { background-color: #e0e0e0; border-color: #1976d2; }
.option-radio { width: 14px; height: 14px; border: 1.5px solid #555; border-radius: 50%; background-color: #fff; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.option-label { line-height: 1.3; }
.option-label strong { color: #0d47a1; }

.option-btn.selected { background-color: #bbdefb; border-color: #0d47a1; }
.option-btn.selected .option-radio { border-color: #0d47a1; }
.option-btn.selected .option-radio::after { content: ''; width: 7px; height: 7px; background-color: #0d47a1; border-radius: 50%; }

.option-btn.correct { background-color: #c8e6c9; border-color: #2e7d32; color: #2e7d32; }
.option-btn.correct .option-radio { border-color: #2e7d32; background-color: #2e7d32; }
.option-btn.correct .option-radio::after { content: '✔'; color: white; font-size: 9px; font-weight: bold; }

.option-btn.incorrect { background-color: #ffcdd2; border-color: #c62828; color: #c62828; }
.option-btn.incorrect .option-radio { border-color: #c62828; background-color: #c62828; }
.option-btn.incorrect .option-radio::after { content: '×'; color: white; font-size: 11px; font-weight: bold; }

#quiz-settings { margin-top: 20px; padding-top: 15px; border-top: 1px solid #eee; text-align: center; font-size: 0.9em; color: #333; }
#quiz-settings label { cursor: pointer; margin-left: 5px; }

#feedback-container { margin-top: 20px; padding: 15px; background-color: #fffde7; border: 1px solid #fbc02d; border-radius: 8px; }
#feedback-text, .review-item .feedback-container { margin-top: 5px; font-size: 0.9em; line-height: 1.5; white-space: pre-wrap; }

#navigation-container { margin-top: 15px; display: flex; }
#navigation-container button { padding: 10px 20px; font-size: 0.95em; font-weight: bold; border: none; border-radius: 5px; cursor: pointer; }
#prev-btn { background-color: #eee; color: #555; }
#next-btn { background-color: #1976d2; color: white; margin-left: auto; }

/* =========================================
   5. KẾT QUẢ & REVIEW
   ========================================= */
#results-container { padding: 25px; }
.results-layout { display: flex; justify-content: space-between; background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 8px; padding: 20px; margin-top: 15px; }
.results-details { flex-basis: 60%; display: flex; flex-direction: column; gap: 10px; }
.detail-item { display: flex; justify-content: space-between; font-size: 1em; }
.results-score { flex-basis: 35%; text-align: center; border-left: 2px solid #eee; padding-left: 15px; }
.score-value { font-size: 3.5em; font-weight: bold; color: #0d47a1; }

.results-navigation, .review-footer-nav { margin-top: 20px; text-align: center; }
.results-navigation button, .review-footer-nav button { padding: 10px 20px; margin: 5px; border: none; border-radius: 5px; font-weight: bold; cursor: pointer; }
#restart-btn, #restart-btn-bottom { background-color: #1976d2; color: white; }
#review-btn, #scroll-to-top-btn { background-color: #fbc02d; color: #333; }
#back-to-menu-btn-results, #back-to-menu-btn-bottom { background-color: #757575; color: white; }

/* Review Items (Gọn gàng) */
#review-section { margin-top: 25px; text-align: left; }
.review-item { 
    padding: 12px; 
    margin-bottom: 10px; 
    border-radius: 8px; 
    border: 1px solid #ddd; 
    scroll-margin-top: 160px; /* Tránh bị sticky header che */
}
.review-item p { margin-bottom: 6px; margin-top: 0; font-size: 0.95em; }
.review-item.review-correct { background-color: #f1f8e9; }
.review-item.review-incorrect { background-color: #fff3f3; }
.review-item.review-unanswered { background-color: #fff3f3; border-color: #c62828; }

.review-options-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 6px; }
.review-answer { padding: 5px 8px; border-radius: 5px; border: 1px solid #ddd; background-color: #fff; display: flex; align-items: center; gap: 6px; font-size: 0.9em; }
.review-answer.correct { background-color: #c8e6c9; border-color: #2e7d32; color: #2e7d32; }
.review-answer.user-wrong { background-color: #ffcdd2; border-color: #c62828; color: #c62828; text-decoration: line-through; }
.review-explanation-hr { border: none; border-top: 1px dashed #ccc; margin-top: 8px; margin-bottom: 8px; }

/* Sticky Header (Ma trận & Bộ lọc) */
#review-sticky-header {
    position: sticky;
    top: 0;
    z-index: 1000;
    background-color: #ffffff;
    border-bottom: 2px solid #0d47a1;
    padding: 8px 0;
    margin-bottom: 15px;
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);
}
.review-header-title { font-weight: bold; text-align: center; margin-bottom: 5px; color: #0d47a1; font-size: 0.85em; }
#review-filters { display: flex; justify-content: center; gap: 8px; margin-bottom: 8px; }
.filter-btn { padding: 4px 10px; border: 1px solid #ddd; background-color: #fff; border-radius: 15px; cursor: pointer; font-size: 0.8em; font-weight: bold; }
.filter-btn.active { background-color: #e3f2fd; border-color: #1976d2; color: #0d47a1; }

#review-matrix {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(35px, 1fr)); 
    gap: 4px;
    justify-content: center;
    max-height: 25vh;
    overflow-y: auto;
    padding: 0 5px;
}
.review-box { height: 25px; display: flex; justify-content: center; align-items: center; font-size: 0.85em; font-weight: bold; border: 1px solid #ccc; border-radius: 3px; cursor: pointer; }
.review-box.correct { background-color: #c8e6c9; border-color: #2e7d32; color: #2e7d32; }
.review-box.incorrect { background-color: #ffcdd2; border-color: #c62828; color: #c62828; }
.review-box.unanswered { background-color: #f5f5f5; border-color: #c62828; color: #c62828; }

.toggle-explanation-btn { background-color: #e3f2fd; color: #1565c0; border: 1px solid #90caf9; padding: 5px 10px; border-radius: 5px; cursor: pointer; font-size: 0.85em; font-weight: bold; margin-top: 5px; width: 100%; text-align: left; }
.toggle-explanation-btn:hover { background-color: #bbdefb; }

/* =========================================
   6. RESPONSIVE MOBILE
   ========================================= */
@media (max-width: 700px) {
    body { padding: 10px; }
    #mode-selection, #quiz-container, #results-container { padding: 10px; }
    
    /* Header Mobile: Logo lên trên */
    .home-header { flex-direction: column; text-align: center; }
    .app-logo { margin-right: 0; margin-bottom: 10px; }
    
    /* Footer Mobile: Xếp dọc */
    footer { flex-direction: column; align-items: center; justify-content: center; gap: 15px; padding-bottom: 20px; }
    .footer-branding {
        position: static; /* Hủy căn tuyệt đối */
        transform: none;
        width: auto;
        padding: 0;
    }
    .donation-container {
        align-items: center; /* Căn giữa */
        text-align: center;
        background-color: transparent;
        padding-left: 0;
    }
    
    .mode-row { flex-direction: column; gap: 10px; }
    .mode-row button { width: 100%; }
    
    .topic-grid { grid-template-columns: repeat(2, 1fr); }
    
    #quiz-header { flex-wrap: wrap; gap: 10px; }
    #quiz-mode-title { order: -1; flex-basis: 100%; }
    
    #question-matrix { grid-template-columns: repeat(8, 1fr); width: 100%; }
    #review-matrix { grid-template-columns: repeat(8, 1fr); }
    
    .matrix-box, .review-box { height: 30px; }
    #options-container, .review-options-grid { gap: 8px; }
    .results-layout { flex-direction: column; gap: 20px; }
    .results-score { border-left: none; border-top: 2px solid #eee; padding-top: 20px; }
    .score-value { font-size: 3.5em; }
    .results-navigation button, .review-footer-nav button { width: 100%; margin: 5px 0; }
    
    .rules-table { font-size: 0.85em; }
    .rules-table th, .rules-table td { padding: 5px; }

    @media (max-width: 400px) { 
        #options-container, .review-options-grid { grid-template-columns: 1fr; } 
        .topic-grid { grid-template-columns: 1fr; }
    }
}