/* ==========================================================================
   PRINT CORE CSS — ĐỊNH DẠNG CHUẨN KHỔ GIẤY A4 (PAPER-CSS) & BẢN IN VẬT LÝ
   - Lề in chuẩn @page: 10mm bốn phía trên khổ giấy A4 dọc (210mm x 297mm).
   - Chống mất nội dung (Anti-Truncation): KHÔNG dùng overflow:hidden trên .sheet.
   - Hình chìm thông minh (.a4-watermark-layer): position:absolute; z-index:0;
     tuyệt đối không chiếm diện tích dòng chảy hay đẩy rớt câu sang trang trắng.
   ========================================================================== */
@page { size: A4 portrait; margin: 10mm; }

:root {
    --a4-page-w: 210mm; --a4-page-h: 297mm;
    --a4-pad-x: 10mm; --a4-pad-y: 10mm;
    --a4-content-w: calc(var(--a4-page-w) - var(--a4-pad-x) * 2);
    --a4-content-h: calc(var(--a4-page-h) - var(--a4-pad-y) * 2);
}

* { box-sizing: border-box; }

html, body {
    margin: 0; padding: 0; width: 100%; overflow-x: hidden;
    -webkit-text-size-adjust: 100%; font-synthesis: none;
    -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}

body.eco-print-mode .sheet > .a4-body,
body.eco-print-mode .sheet > .a4-footer-bar { filter: grayscale(100%) contrast(128%); }

/* ==========================================================================
   1. BÀN LÀM VIỆC & KHUNG GIẤY A4 (PAPER-CSS LIVE PREVIEW CANVAS)
   ========================================================================== */
.workspace-with-ads {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 18px;
    padding: 8px 4px 28px 4px;
    width: 100%;
    max-width: 100%;
}

#a4-scale-viewport {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: var(--a4-page-w);
    margin: 0;
}

#a4-sheets-wrapper {
    width: var(--a4-page-w);
    transform-origin: top center;
    transition: transform 0.16s ease;
}

body.A4 .sheet,
.A4 .sheet {
    position: relative !important;
    width: var(--a4-page-w) !important;
    min-height: var(--a4-page-h) !important;
    height: auto !important;
    max-height: none !important;
    padding: var(--a4-pad-y) var(--a4-pad-x) !important;
    margin: 0 auto 20px auto !important;
    background: #ffffff !important;
    box-shadow: 0 10px 25px rgba(15, 23, 42, 0.12);
    border-radius: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: visible !important;
    box-sizing: border-box;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
}

.sheet > .a4-body,
.sheet > div:not(.a4-watermark-layer) {
    flex: 1 1 auto;
    min-height: 0;
}

.sheet > .a4-body,
.sheet > .a4-footer-bar {
    position: relative;
    z-index: 1;
}

/* --- Lớp Watermark A4 cô lập hoàn toàn (Zero Pagination Impact) --- */
.a4-watermark-layer {
    position: absolute !important;
    inset: 0 !important;
    flex: none !important;
    width: 100% !important;
    height: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none !important;
    user-select: none !important;
    z-index: 0 !important;
    overflow: hidden !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
}

.a4-watermark-layer.is-wm-off { display: none !important; }

/* --- Auto-fit tỷ lệ co giãn thân đề (Đề 1 Trang) --- */
.sheet > .a4-body { zoom: calc(var(--fit-scale, 1) * var(--sheet-font-scale, 1)); }

@supports not (zoom: 1) {
    .sheet > .a4-body {
        transform: scale(var(--fit-scale, 1)) scale(var(--sheet-font-scale, 1));
        transform-origin: top left;
        width: calc(100% / var(--fit-scale, 1) / var(--sheet-font-scale, 1));
    }
}

/* --- Chế độ xuất file PDF qua html2pdf --- */
body.is-exporting-pdf #a4-sheets-wrapper { width: var(--a4-content-w) !important; }

body.is-exporting-pdf .sheet {
    position: relative !important;
    width: var(--a4-content-w) !important;
    min-height: calc(var(--a4-content-h) - 1mm) !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
    box-shadow: none !important;
}

/* --- Thanh trạng thái chế độ dàn trang (Dồn 1 trang / Nhiều trang) --- */
.fit-mode-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px dashed #cbd5e1;
}

.fit-mode-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    width: 100%;
}

.btn-fit-mode {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 38px;
    padding: 6px 10px;
    border-radius: 8px;
    border: 1.5px solid #cbd5e1;
    background: #f8fafc;
    color: #334155;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
}

.btn-fit-mode.is-active { background: #eff6ff; border-color: #2563eb; color: #1d4ed8; }
.fit-mode-status { font-size: 0.78rem; font-weight: 700; color: #64748b; width: 100%; }
.fit-mode-status.is-ok { color: #059669; }
.fit-mode-status.is-warn { color: #d97706; }

/* --- Bảo vệ vùng giấy A4 & Watermark VIP/Free ngay trên màn hình --- */
.sheet .ad-slot-container, .sheet .adsbygoogle, .sheet .google-auto-placed, .sheet ins.adsbygoogle {
    display: none !important;
    height: 0 !important;
}

body.is-vip-user .a4-free-watermark { display: none !important; }

/* ==========================================================================
   2. THANH TÁC VỤ ĐÁY CỐ ĐỊNH TRÊN MOBILE (< 768px)
   ========================================================================== */
.mobile-bottom-bar { display: none; }

@media (max-width: 767.98px) {
    .mobile-bottom-bar {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 8px;
        position: fixed;
        bottom: 0; left: 0; right: 0;
        z-index: 1030;
        padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px)) 10px;
        background: rgba(255, 255, 255, 0.98);
        border-top: 1px solid #cbd5e1;
        box-shadow: 0 -6px 20px rgba(15, 23, 42, 0.12);
        backdrop-filter: blur(8px);
    }

    .mbb-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        min-height: 46px;
        padding: 8px 10px;
        border: none;
        border-radius: 10px;
        font-size: 0.88rem;
        font-weight: 800;
        color: #ffffff;
        cursor: pointer;
    }

    .mbb-filter { background: #334155; }
    .mbb-primary { background: #10b981; }
    .mbb-print { background: #2563eb; }
    body { padding-bottom: 74px; }
}

/* ==========================================================================
   3. CHẾ ĐỘ IN ẤN VẬT LÝ (@media print) — ĐA TRANG & KHÔNG MẤT NỘI DUNG
   ========================================================================== */
@media print {
    .no-print, .d-print-none, .no-print-toolbar, .no-print-ad, .ad-banner-slot, .adx-wrap,
    .ad-slot-container, .adsbygoogle, .google-auto-placed, ins.adsbygoogle, .adsbox,
    #modal-print-interstitial, #modal-vip-watermark, .smart-print-backdrop, .modal-backdrop,
    iframe[id^="aswift"], iframe[id^="google_ads"], iframe[src*="googlesyndication"], iframe[src*="doubleclick"],
    .edtech-navbar, .edtech-sidebar, .mobile-bottom-bar, .fit-mode-bar, .ta-toolbar-wrap, .ta-coffee-card, .geo-top-wrap, .geo-summary-box, .geo-topic-cluster, .geo-breadcrumbs, .geo-seo-footer,
    header.navbar, nav, aside, .sidebar, .toolbar {
        display: none !important;
        visibility: hidden !important;
        height: 0 !important;
        min-height: 0 !important;
        max-height: 0 !important;
        overflow: hidden !important;
    }

    body:not(.is-vip-user) .sheet .a4-free-watermark { display: inline !important; visibility: visible !important; }
    body.is-vip-user .sheet .a4-free-watermark { display: none !important; visibility: hidden !important; }

    .sheet > .a4-watermark-layer:not(.is-wm-off) {
        display: flex !important;
        visibility: visible !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    html, body, .edtech-split-shell, .edtech-canvas-area, .workspace,
    .preview-container, .workspace-with-ads, #a4-scale-viewport, #a4-sheets-wrapper {
        display: block !important;
        width: auto !important;
        max-width: none !important;
        height: auto !important;
        min-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        background: #ffffff !important;
        overflow: visible !important;
        transform: none !important;
    }

    body.A4 .sheet, .A4 .sheet {
        position: relative !important;
        transform: none !important;
        width: var(--a4-content-w) !important;
        max-width: 100% !important;
        min-height: calc(var(--a4-content-h) - 1mm) !important;
        height: auto !important;
        max-height: none !important;
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
        border-radius: 0 !important;
        overflow: visible !important;
        break-inside: auto;
        page-break-inside: auto;
        break-after: page;
        page-break-after: always;
    }

    /* The answer key is a separate print unit, never a continuation of the exam body. */
    body.A4 #answer-key-sheet,
    .A4 #answer-key-sheet {
        break-before: page !important;
        page-break-before: always !important;
    }

    body.A4 .ak-cont-sheet,
    .A4 .ak-cont-sheet {
        break-before: auto !important;
        page-break-before: auto !important;
    }

    body.A4 .sheet:last-of-type, .A4 .sheet:last-of-type,
    body.A4 .sheet.is-last-visible-sheet, .A4 .sheet.is-last-visible-sheet {
        break-after: auto !important;
        page-break-after: auto !important;
    }

    [contenteditable="true"]:hover { outline: none !important; background: transparent !important; }
}
