/*
 * print.css
 * Print / "Save as PDF" output for the generated question paper and answer key.
 *
 * Ctrl+P previously printed the whole application — header, quota bar, config
 * panel, action buttons, footer and every modal — with browser default margins
 * and no page-break control. Everything below narrows the printed page down to
 * the generated paper itself.
 *
 * Targets both `.question` (which the model is instructed to emit, see the
 * system instruction in api_generation_services.php) and bare `li`/`p`, because
 * the paper body is model-generated HTML and the markup is not guaranteed.
 */

@media print {

    /* ---- Page geometry ------------------------------------------------- */

    @page {
        size: A4;
        /* Room for a teacher to write in the margin, and for staples. */
        margin: 18mm 16mm 20mm 16mm;
    }

    html,
    body {
        background: #fff !important;
        margin: 0 !important;
        padding: 0 !important;
        /* Let the content define the page count instead of any app viewport
           sizing, which otherwise emits trailing blank pages. */
        height: auto !important;
        min-height: 0 !important;
        overflow: visible !important;
        font-size: 12pt;
        color: #000 !important;
    }

    /* ---- Hide application chrome --------------------------------------- */

    #api-overlay,
    .header-wrapper,
    #quota-section,
    .footer-text,
    .card-badge,
    #infrastructure-notice,
    .content-grid,
    #login-modal,
    #feedback-modal,
    #save-template-modal,
    #save-config-modal,
    #subscription-modal,
    #payment-checking-modal,
    #error-message,
    .no-print {
        display: none !important;
    }

    /* Every action button on the page: Edit, Answer Key, Drive, Word, PDF. */
    button,
    .btn-generate,
    .btn-add,
    .edit-btn {
        display: none !important;
    }

    /* ---- Unwrap the app shell around the paper -------------------------- */

    .main-wrapper,
    .card-container,
    .teacher-card,
    #output-section,
    #question-paper-output-wrapper,
    #question-paper-output,
    #answer-key-output-wrapper,
    #answer-key-output {
        display: block !important;
        position: static !important;
        margin: 0 !important;
        padding: 0 !important;
        border: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        background: #fff !important;
        max-width: none !important;
        width: auto !important;
        float: none !important;
    }

    /* `hidden` is Tailwind's display:none. The output section carries it until
       a paper is generated, so it must not be force-shown when still empty —
       only the wrapper chain above is unhidden, and an ungenerated paper simply
       prints nothing. */
    #output-section.hidden,
    #answer-key-output-wrapper.hidden {
        display: none !important;
    }

    /* ---- Paper body ----------------------------------------------------- */

    #question-paper-output,
    #answer-key-output {
        font-family: 'Times New Roman', Times, serif;
        font-size: 12pt;
        line-height: 1.45;
        color: #000 !important;
    }

    #question-paper-output h1,
    #question-paper-output h2,
    #question-paper-output h3,
    #answer-key-output h1,
    #answer-key-output h2,
    #answer-key-output h3 {
        color: #000 !important;
        /* A heading stranded at the foot of a page is the classic print defect. */
        break-after: avoid;
        page-break-after: avoid;
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* Keep a question and its options/marks together on one page. */
    #question-paper-output .question,
    #question-paper-output li,
    #question-paper-output tr,
    #answer-key-output .question,
    #answer-key-output li,
    #answer-key-output tr {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* A section header ("Section A — 1 mark questions") should not be the last
       thing on a page. */
    #question-paper-output .section-header,
    #answer-key-output .section-header {
        break-after: avoid;
        page-break-after: avoid;
    }

    /* Figures must not be split, and must fit the text column. */
    #question-paper-output img,
    #answer-key-output img,
    #question-paper-output figure,
    #answer-key-output figure {
        max-width: 100% !important;
        height: auto !important;
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* MathJax renders each expression into its own container; splitting one
       across a page break cuts the glyphs in half. */
    mjx-container {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    /* Display maths is block-level and can be wide; keep it from overflowing
       the print column. */
    mjx-container[display="true"] {
        overflow-x: visible !important;
        max-width: 100% !important;
    }

    /* ---- Answer key starts on its own sheet ----------------------------- */

    #answer-key-output-wrapper {
        break-before: page;
        page-break-before: always;
        border-top: 0 !important;
    }

    /* ---- Tables --------------------------------------------------------- */

    #question-paper-output table,
    #answer-key-output table {
        border-collapse: collapse !important;
        width: 100% !important;
    }

    #question-paper-output th,
    #question-paper-output td,
    #answer-key-output th,
    #answer-key-output td {
        border: 1px solid #000 !important;
        padding: 4pt 6pt !important;
    }

    /* Repeat header rows when a long table spans pages. */
    #question-paper-output thead,
    #answer-key-output thead {
        display: table-header-group;
    }

    /* ---- Links ---------------------------------------------------------- */

    /* Printed paper: a underlined blue link is noise, and the URL is useless. */
    #question-paper-output a,
    #answer-key-output a {
        color: #000 !important;
        text-decoration: none !important;
    }
}
