/* ==========================================================================
   TẬP LỆNH ĐỊNH DẠNG RIÊNG CHO THIẾT BỊ DI ĐỘNG (MOBILE & TABLET <= 768PX)
   Hệ Thống Thi Trắc Nghiệm Nội Bộ - Sư Đoàn 302
   Đảm bảo 100% không làm ảnh hưởng đến giao diện máy tính để bàn (Desktop > 768px)
   ========================================================================== */

/* Mặc định trên màn hình lớn: Ẩn tuyệt đối các thành phần giao diện di động */
.mobile-bottom-bar,
.mobile-drawer-overlay,
.mobile-timer-strip,
.mobile-only-btn {
  display: none !important;
}

/* ==========================================================================
   QUY TẮC HIỂN THỊ DÀNH RIÊNG CHO MÀN HÌNH <= 768PX
   ========================================================================== */
@media (max-width: 768px) {

  /* 1. Thiết lập chung & Tránh phóng to màn hình không mong muốn */
  html, body {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
  }

  .container {
    padding: 0.75rem 0.65rem !important;
    max-width: 100% !important;
  }

  /* 2. Sticky Header mỏng nhẹ trên Mobile */
  .military-nav {
    position: sticky !important;
    top: 0 !important;
    z-index: 90 !important;
    padding: 0.5rem 0.75rem !important;
    min-height: 52px !important;
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15) !important;
  }

  .military-nav .logo-brand {
    gap: 0.5rem !important;
    max-width: 65% !important;
  }

  .military-nav .emblem-img {
    width: 32px !important;
    height: 32px !important;
    min-width: 32px !important;
  }

  .military-nav .brand-title {
    font-size: 0.92rem !important;
    line-height: 1.25 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  .military-nav .brand-subtitle {
    font-size: 0.72rem !important;
    line-height: 1.2 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    opacity: 0.9;
  }

  /* Ẩn bớt text tự động lưu trên header mobile để nhường chỗ */
  #autoSaveBadge {
    display: none !important;
  }

  .military-nav .btn-danger {
    padding: 0.4rem 0.75rem !important;
    font-size: 0.8rem !important;
    font-weight: 700 !important;
  }

  /* 3. Dải đồng hồ đếm ngược & Tiến độ mỏng ghim sát dưới Header */
  .mobile-timer-strip {
    display: flex !important;
    position: sticky;
    top: 52px;
    z-index: 89;
    background: #ffffff;
    border-bottom: 1px solid var(--border-color);
    padding: 0.45rem 0.85rem;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  }

  .mobile-timer-strip .timer-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.95rem;
    font-weight: 800;
    color: var(--military-navy);
    letter-spacing: 0.5px;
  }

  .mobile-timer-strip .timer-chip.timer-warning {
    color: var(--danger) !important;
    animation: none !important; /* Không nhấp nháy làm mỏi mắt */
  }

  .mobile-timer-strip .progress-chip {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
  }

  /* 4. Khung nội dung làm bài thi (Exam Layout) */
  .exam-layout {
    display: block !important;
    margin-bottom: 76px !important; /* Dành khoảng trống cho Bottom Bar */
  }

  /* Ẩn cột bảng câu hỏi tĩnh ở giao diện Desktop để tránh cuộn dài */
  .exam-layout > div:last-child {
    display: none !important;
  }

  .question-box {
    padding: 1.1rem 0.95rem !important;
    border-radius: 12px !important;
    margin-bottom: 1rem !important;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04) !important;
  }

  .question-header {
    display: flex !important;
    flex-wrap: wrap !important;
    justify-content: space-between !important;
    align-items: center !important;
    gap: 0.5rem !important;
    margin-bottom: 1rem !important;
    padding-bottom: 0.75rem !important;
  }

  .question-num-tag {
    font-size: 0.82rem !important;
    padding: 4px 10px !important;
  }

  .question-text {
    font-size: 1.06rem !important;
    line-height: 1.65 !important;
    font-weight: 600 !important;
    margin-bottom: 1.25rem !important;
    color: var(--text-primary) !important;
  }

  /* 5. Vùng chạm chọn đáp án thân thiện cảm ứng (Touch-Friendly) */
  .options-grid {
    gap: 0.75rem !important;
  }

  .option-item {
    min-height: 52px !important;
    padding: 0.85rem 1rem !important;
    border-radius: 10px !important;
    gap: 0.85rem !important;
    align-items: center !important;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease !important;
  }

  .option-item:active {
    transform: scale(0.985);
    background: var(--accent-blue-light) !important;
  }

  .option-key {
    width: 34px !important;
    height: 34px !important;
    min-width: 34px !important;
    font-size: 0.95rem !important;
    font-weight: 700 !important;
  }

  .option-label {
    font-size: 1rem !important; /* Chuẩn 16px để chống Safari tự động zoom */
    line-height: 1.5 !important;
    margin-top: 0 !important;
    flex: 1;
  }

  /* Ẩn các nút Next/Prev cũ nằm trong question-box vì đã có Bottom Bar */
  .question-box > div:last-child {
    display: none !important;
  }

  /* 6. Thanh điều khiển cố định dưới đáy màn hình (Bottom Navigation Bar) */
  .mobile-bottom-bar {
    display: flex !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    height: 62px !important;
    background: #ffffff !important;
    border-top: 1px solid var(--border-color) !important;
    box-shadow: 0 -3px 12px rgba(0, 0, 0, 0.08) !important;
    z-index: 100 !important;
    align-items: center !important;
    justify-content: space-around !important;
    padding: 0 0.5rem calc(env(safe-area-inset-bottom, 0px)) 0.5rem !important;
  }

  .mobile-nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-secondary);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 8px;
    min-width: 60px;
    height: 52px;
    transition: background 0.15s, color 0.15s;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-nav-btn:active {
    background: #f1f5f9;
  }

  .mobile-nav-btn svg {
    width: 20px;
    height: 20px;
    margin-bottom: 2px;
  }

  .mobile-nav-btn.btn-flag.active {
    color: #b45309 !important;
  }

  .mobile-nav-btn.btn-flag.active svg {
    fill: #d97706;
  }

  .mobile-nav-btn.btn-palette-trigger {
    background: var(--accent-blue-light);
    color: var(--accent-blue);
    font-weight: 700;
  }

  .mobile-nav-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
  }

  /* 7. Ngăn kéo Bảng câu hỏi dạng Bottom Sheet (Question Drawer) */
  .mobile-drawer-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.55);
    z-index: 200;
    backdrop-filter: blur(2px);
    transition: opacity 0.25s ease;
    opacity: 0;
    pointer-events: none;
  }

  .mobile-drawer-overlay.open {
    display: block !important;
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-drawer-content {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 82vh;
    background: #ffffff;
    border-radius: 20px 20px 0 0;
    z-index: 201;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  }

  .mobile-drawer-overlay.open .mobile-drawer-content {
    transform: translateY(0);
  }

  .mobile-drawer-handle {
    width: 44px;
    height: 5px;
    background: #cbd5e1;
    border-radius: 3px;
    margin: 10px auto 4px auto;
  }

  .mobile-drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.6rem 1.1rem;
    border-bottom: 1px solid var(--border-color);
  }

  .mobile-drawer-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
  }

  .mobile-drawer-close {
    background: #f1f5f9;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    color: var(--text-secondary);
    cursor: pointer;
  }

  .mobile-drawer-body {
    overflow-y: auto;
    padding: 1rem 1.1rem 1.5rem 1.1rem;
    -webkit-overflow-scrolling: touch;
    flex: 1;
  }

  .mobile-drawer-body .palette-grid {
    display: grid !important;
    grid-template-columns: repeat(5, 1fr) !important;
    gap: 8px !important;
    margin-top: 0.75rem !important;
  }

  .mobile-drawer-body .palette-btn {
    min-width: 44px !important;
    min-height: 44px !important;
    font-size: 1rem !important;
    font-weight: 700 !important;
    border-radius: 10px !important;
  }

  /* 8. Tối ưu Trang Cổng Thí Sinh (/home) trên Mobile */
  .container > div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  /* Thẻ thông tin ca thi full-width */
  .military-card {
    padding: 1.1rem 1rem !important;
    border-radius: 12px !important;
  }

  /* Nút bấm to rõ ràng, dễ nhấn */
  .military-card .btn {
    min-height: 46px !important;
    font-size: 0.95rem !important;
    font-weight: 700 !important;
  }

  /* Bảng cảnh báo vi phạm & Modal xác nhận nộp bài trên Mobile */
  .violation-content {
    margin: 1rem !important;
    padding: 1.5rem 1.1rem !important;
    max-width: calc(100% - 2rem) !important;
    border-radius: 14px !important;
  }

  /* Chân trang chữ mờ trên Mobile */
  .app-global-footer {
    padding: 0.75rem 0.5rem !important;
    font-size: 0.75rem !important;
    opacity: 0.5 !important;
    margin-bottom: 0 !important;
  }

  /* Khi ở trong phòng thi: chừa khoảng trống cho thanh Bottom Navigation Bar */
  .app-global-footer.exam-footer {
    margin-bottom: 68px !important;
  }
}
