/* ============================================================
   classexam.css  –  Phiếu trả lời trắc nghiệm 3 phần
   ============================================================ */

/* ---- Root / Fullscreen ---- */
.exam-body {
    background: #e9ecef;
    min-height: 100vh;
    font-family: 'Segoe UI', Arial, sans-serif;
    font-size: 14px;
}

/* ---- Header bar ---- */
.exam-header {
    background: #320159;
    color: white;
    padding: 12px 16px; /* Increased padding for mobile */
    position: sticky;
    top: 0;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0,0,0,.3);
    /* Promote to its own GPU layer so timer/badge changes don't repaint scrolled PDF below */
    will-change: transform;
    transform: translate3d(0,0,0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}
.exam-header > .container-fluid,
.exam-header > .d-flex {
    flex-wrap: nowrap !important; /* Prevent timer from wrapping */
    align-items: center;
    gap: 12px; /* Increased gap for touch */
}
.exam-header .timer-box {
    background: rgba(255,255,255,0.15);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 1px;
    min-width: 90px; /* Reduced from 100px */
    text-align: center;
    /* Make it more flexible */
    flex-shrink: 1;
    flex-grow: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.exam-header .timer-box.warning { background: rgba(255,193,7,0.3); color: #ffe082; }
.exam-header .timer-box.danger  { background: rgba(220,53,69,0.4); color: #ff8a80; }

/* ---- Main layout ---- */
.exam-main {
    display: flex;
    gap: 16px;
    padding: 16px;
    max-width: 1400px;
    margin: 0 auto;
}

/* ---- PDF panel ---- */
.exam-pdf-panel {
    flex: 1 1 55%;
    min-width: 0;
}
.exam-pdf-panel iframe {
    width: 100%;
    /* Use dynamic viewport height for mobile Safari */
    height: calc(100dvh - 120px); /* dvh accounts for mobile browser chrome */
    border: none;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,.2);
}

/* Fallback for browsers without dvh support */
@supports not (height: 100dvh) {
    .exam-pdf-panel iframe {
        height: calc(100vh - 100px);
        min-height: 400px;
    }
}
.no-pdf-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: calc(100vh - 100px);
    background: white;
    border-radius: 8px;
    color: #aaa;
}

/* ---- Answer sheet panel ---- */
.exam-answer-panel {
    flex: 0 0 420px;
    width: 420px;
}

/* ---- Answer sheet card ---- */
.answer-sheet {
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 12px rgba(0,0,0,.15);
    overflow: hidden;
}
.answer-sheet-title {
    background: #d32f2f;
    color: white;
    text-align: center;
    padding: 10px;
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: 1px;
    text-transform: uppercase;
}
.answer-sheet-info {
    background: #ffebee;
    padding: 8px 14px;
    font-size: 0.82rem;
    border-bottom: 1px solid #ffcdd2;
}

/* ---- Part headers ---- */
.part-header {
    background: #fce4ec;
    color: #c62828;
    font-weight: 700;
    font-size: 0.82rem;
    padding: 6px 14px;
    border-top: 2px solid #ef9a9a;
    border-bottom: 1px solid #ef9a9a;
    letter-spacing: 0.5px;
}

/* ============================================================
   PHẦN I – Bảng tô bóng A/B/C/D
   ============================================================ */
.part1-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0;
    padding: 8px 10px;
}
.p1-row {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 3px 6px;
    border-radius: 4px;
}
.p1-row:hover { background: #fff9c4; }
.p1-num {
    font-size: 0.75rem;
    font-weight: 700;
    color: #333;
    min-width: 22px;
    text-align: right;
}
.p1-bubble {
    position: relative;
    width: 26px;
    height: 26px;
    cursor: pointer;
}
.p1-bubble input[type=radio] { display: none; }
.p1-bubble .bubble-label {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid #888;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    font-weight: 700;
    color: #555;
    cursor: pointer;
    transition: all 0.1s;
    background: white;
    user-select: none;
}
.p1-bubble input:checked + .bubble-label {
    background: #320159;
    border-color: #320159;
    color: white;
}
.p1-bubble .bubble-label:hover {
    border-color: #3949ab;
    color: #3949ab;
}
.p1-bubble input:checked + .bubble-label:hover {
    color: white;
}

/* ============================================================
   PHẦN II – Bảng đúng/sai
   ============================================================ */
.part2-table-wrap {
    padding: 8px 10px;
    overflow-x: auto;
}
.part2-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.78rem;
}
.part2-table th {
    text-align: center;
    padding: 4px 6px;
    background: #fce4ec;
    border: 1px solid #f48fb1;
    font-weight: 700;
    color: #c62828;
}
.part2-table td {
    border: 1px solid #f48fb1;
    text-align: center;
    padding: 3px;
}
.part2-table .q-label {
    font-weight: 700;
    background: #fff3e0;
    padding: 3px 8px;
    font-size: 0.8rem;
}
.ds-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 24px;
    border-radius: 4px;
    border: 2px solid #ccc;
    cursor: pointer;
    font-weight: 700;
    font-size: 0.72rem;
    transition: all 0.1s;
    user-select: none;
    margin: 1px;
    background: white;
}
.ds-btn input { display: none; }
.ds-btn.sel-T { border-color: #2e7d32; background: #2e7d32; color: white; }
.ds-btn.sel-F { border-color: #c62828; background: #c62828; color: white; }
.ds-btn:hover:not(.sel-T):not(.sel-F) { border-color: #999; }

/* ============================================================
   PHẦN III – Table layout giống phiếu THPT thực tế
   Hàng = ký tự (-, ,, 0-9) | Cột = vị trí 1-4
   ============================================================ */
.part3-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    padding: 8px 10px;
}
.p3-card {
    border: 1px solid #e57373;
    border-radius: 6px;
    overflow: hidden;
    background: #fff;
}
.p3-card-header {
    background: #fce4ec;
    font-weight: 700;
    font-size: 0.75rem;
    text-align: center;
    padding: 4px;
    border-bottom: 1px solid #f48fb1;
    color: #c62828;
}
/* Thanh hiển thị 4 ô kết quả */
.p3-display {
    display: flex;
    justify-content: center;
    gap: 2px;
    padding: 4px 6px;
    border-bottom: 1px solid #f48fb1;
    background: #fff8f8;
    min-height: 26px;
}
.p3-display-cell {
    width: 18px;
    height: 18px;
    border: 1.5px solid #e57373;
    border-radius: 2px;
    font-size: 0.7rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #320159;
    background: white;
}
.p3-display-cell.filled {
    background: #e3f2fd;
    border-color: #1565c0;
}
/* Bảng bubble */
.p3-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}
.p3-table thead th,
.p3-table tbody td {
    padding: 0;
    text-align: center;
    vertical-align: middle;
    height: 20px;
}
/* Nhãn hàng bên trái (-, ,, 0-9) */
.p3-row-label {
    width: 16px;
    font-size: 0.65rem;
    font-weight: 700;
    color: #333;
    text-align: right;
    padding-right: 2px !important;
    border-right: 1px solid #fce4ec;
}
/* Header cột (1,2,3,4) */
.p3-col-header {
    font-size: 0.62rem;
    font-weight: 700;
    color: #c62828;
    background: #fce4ec;
    border-bottom: 1px solid #f48fb1;
    height: 18px !important;
}
/* Wrapper label cho bubble */
.p3-bubble-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 20px;
    cursor: pointer;
}
.p3-bubble-wrap input { display: none; }
/* Bubble circle */
.p3-bubble {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1.5px solid #e57373;
    display: inline-flex;
    background: white;
    transition: all 0.1s;
    flex-shrink: 0;
}
.p3-bubble-wrap:hover .p3-bubble {
    border-color: #c62828;
    background: #fce4ec;
}
.p3-bubble-wrap input:checked + .p3-bubble {
    background: #c62828;
    border-color: #c62828;
}
/* Ô trống (không có bubble ở hàng đó) */
.p3-bubble-empty {
    display: inline-block;
    width: 14px;
    height: 14px;
}

/* ---- Submit button ---- */
.exam-submit-area {
    padding: 12px 14px;
    text-align: center;
    border-top: 2px solid #ffcdd2;
    background: #fff8f8;
}

/* ---- Result view bubble colors ---- */
.bubble-correct input:checked + .bubble-label { background: #2e7d32 !important; border-color: #2e7d32 !important; }
.bubble-wrong   input:checked + .bubble-label { background: #c62828 !important; border-color: #c62828 !important; }
.bubble-missed  .bubble-label { border-color: #2e7d32 !important; color: #2e7d32 !important; }

/* ---- Responsive ---- */
@media (max-width: 900px) {
    .exam-main { flex-direction: column; }
    .exam-pdf-panel iframe { height: 50vh; }
    .exam-answer-panel { flex: none; width: 100%; }
    .part3-grid { grid-template-columns: repeat(3, 1fr); }
    /* Shorter container when panels are stacked vertically */
    .pdf-viewer-container { height: 55vh; min-height: 280px; }
}
@media (max-width: 768px) {
    /* Timer header adjustments for mobile */
    .exam-header {
        padding: 10px 12px;
    }

    .exam-header .timer-box {
        font-size: 1.1rem;
        padding: 5px 10px;
        min-width: 80px;
    }

    .exam-header small {
        font-size: 0.75rem;
    }

    .exam-header .btn-danger.btn-sm {
        padding: 6px 12px;
        font-size: 0.85rem;
        min-height: 36px;
    }

    /* ---- Bigger touch targets for answer bubbles on mobile ---- */
    .p1-bubble { width: 34px; height: 34px; }
    .p1-bubble .bubble-label { font-size: 0.85rem; }
    .p1-num { font-size: 0.85rem; min-width: 26px; }
    .p1-row { padding: 5px 6px; gap: 6px; }

    .ds-btn { width: 40px; height: 32px; font-size: 0.85rem; }
    .part2-table { font-size: 0.85rem; }
    .part2-table .q-label { font-size: 0.9rem; }

    .p3-bubble { width: 18px; height: 18px; }
    .p3-bubble-empty { width: 18px; height: 18px; }
    .p3-bubble-wrap { height: 26px; }
    .p3-table thead th, .p3-table tbody td { height: 26px; }
    .p3-row-label { font-size: 0.78rem; width: 18px; }
    .p3-col-header { font-size: 0.72rem; }
    .p3-display-cell { width: 22px; height: 22px; font-size: 0.85rem; }

    /* Sticky bottom controls so they stay reachable while reading PDF */
    .pdf-controls {
        position: sticky;
        bottom: 0;
        z-index: 10;
        margin-top: 0;
        border-radius: 0 0 8px 8px;
        box-shadow: 0 -2px 8px rgba(0,0,0,.15);
    }
}
@media (max-width: 500px) {
    .part1-grid { grid-template-columns: 1fr; }
    .part3-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 480px) {
    /* Very small screens - restructure header */
    .exam-header {
        padding: 8px 10px;
    }

    .exam-header .timer-box {
        font-size: 1rem;
        padding: 4px 8px;
        min-width: 70px;
    }

    /* Move timer below header text on extra small screens */
    .exam-header > .d-flex {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .exam-header .timer-box {
        align-self: flex-end;
    }
}

/* ============================================================
   PDF.js Viewer Styles
   ============================================================ */
.pdf-viewer-container {
    position: relative;
    width: 100%;
    /* Fixed height so overflow:auto actually scrolls on mobile */
    height: calc(100dvh - 185px);
    min-height: 300px;
    overflow: auto; /* native scroll for desktop wheel + zoomed-in mobile after re-render */
    background: #525659;
    border-radius: 8px;
    display: flex;
    /* "safe" keyword: center khi nhỏ hơn container, fallback start khi overflow
       → vẫn scroll được sang trái khi zoom lớn hơn viewport */
    justify-content: safe center;
    align-items: safe flex-start;
    -webkit-overflow-scrolling: touch;
    /* JS switches to "none" during active pinch/pan gestures on touch devices */
    touch-action: pan-x pan-y;
    overscroll-behavior: contain;
    contain: layout paint;
}

@supports not (height: 100dvh) {
    .pdf-viewer-container {
        height: calc(100vh - 185px);
    }
}

/* Transform layer for canvas — pinch-zoom & pan applied here on the GPU.
   position: relative so the canvas contributes to the container's scroll area
   when re-rendered at >1× zoom (native scroll then takes over from JS pan). */
.pdf-canvas-wrapper {
    position: relative;
    transform-origin: 0 0;
    will-change: transform;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    transform: translate3d(0,0,0);
}

#pdfCanvas {
    display: block;
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}

/* PDF Controls */
.pdf-controls {
    display: none; /* Hidden by default, shown when PDF.js loads */
    gap: 8px;
    justify-content: center;
    align-items: center;
    padding: 10px;
    background: rgba(255,255,255,0.95);
    border-radius: 8px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.pdf-controls .btn {
    min-width: 90px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
}

#pdfPageInfo {
    font-weight: 600;
    color: #320159;
    min-width: 80px;
    text-align: center;
}

#pdfZoomLevel {
    font-weight: 600;
    color: #320159;
    min-width: 45px;
    text-align: center;
}

/* Mobile responsive for PDF controls */
@media (max-width: 576px) {
    .pdf-controls {
        gap: 6px;
        padding: 8px;
    }

    .pdf-controls .btn {
        flex: 1 1 auto;
        min-width: 70px;
        font-size: 0.8rem;
        padding: 6px 8px;
    }

    #pdfPageInfo {
        order: -1;
        width: 100%;
        margin-bottom: 5px;
        font-size: 0.9rem;
    }

    .pdf-controls .btn-outline-secondary {
        flex: 0 1 auto; /* Zoom buttons can be smaller */
    }

    #pdfZoomLevel {
        font-size: 0.85rem;
    }
}

/* Fallback iframe styles when PDF.js fails */
.pdf-viewer-container iframe {
    width: 100%;
    min-height: 500px;
    border: none;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}

.pdf-fallback-notice {
    margin-top: 8px;
    padding: 10px;
    background: #fff3cd;
    color: #856404;
    border-radius: 6px;
    text-align: center;
    font-size: 0.9rem;
}

/* ============================================================
   Touch-friendly Mobile Improvements
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
    .exam-header .btn,
    .pdf-controls .btn {
        min-height: 44px; /* Minimum touch target */
        min-width: 44px;
    }

    .answer-option label,
    .question-item {
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    /* Add visual feedback for touch */
    .exam-header .btn:active,
    .pdf-controls .btn:active {
        transform: scale(0.96);
        transition: transform 0.1s ease;
    }

    /* Smooth scrolling */
    .exam-answer-panel {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
}
