/* Zero vertical margins suppress browser date/title/URL furniture. The book
   can restore a bottom margin after its cover; Keys uses the bottom of its
   right margin instead. Neither number overlaps the page's content area.
   Leave paper size/orientation to the dialog; numbering needs margin-box support. */
.print-document { display: none; }

@page {
    size: auto;
    margin: 0 18mm;
}

@page cover {
    counter-reset: page 0;
    @bottom-right { content: none; }
}

/* The marginless cover suppresses Chromium's default browser furniture for
   the whole book. Later pages can therefore give the number a real margin,
   without relying on an overlapping box that subsequent pages can obscure.
   https://developer.chrome.com/blog/print-margins#things_to_note_when_using_page_margins */
@page book {
    margin-bottom: 22mm;
    @bottom-right {
        content: counter(page);
        font: 10pt "Valkyrie OT A", Georgia, serif;
        color: #000;
        vertical-align: middle;
    }
}

/* Keys has no cover to suppress browser furniture. Keep both vertical margins
   at zero and put each number in the final 22 mm of the real right-hand margin.
   This repeats on continuation sheets without the old negative-margin overlap. */
@page keys {
    @right-bottom {
        content: counter(page);
        height: 22mm;
        font: 10pt "Valkyrie OT A", Georgia, serif;
        color: #000;
        text-align: left;
        vertical-align: middle;
    }
}

@media print {
    :root {
        --reading-font-size: 12pt;
        --reading-line-height: 1.45;
        --reading-width: 150mm;
        --paper-color: #fff;
        --ink-color: #000;
    }

    html, body {
        background: #fff !important;
        color: #000 !important;
        visibility: visible !important;
        overflow: visible !important;
    }

    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
        text-shadow: none !important;
        box-shadow: none !important;
        scroll-snap-type: none !important;
        scroll-behavior: auto !important;
    }

    /* The static document remains printable if JavaScript never started. */
    .reading-frame, .reading-surface, .reading-frame main {
        position: static !important;
        display: block !important;
        width: auto !important;
        height: auto !important;
        min-height: 0 !important;
        overflow: visible !important;
        transform: none !important;
        background: #fff !important;
    }

    .reading-header, .reading-contents, .reading-label-measure { display: none !important; }
    .is-printing .reading-frame { display: none !important; }
    .print-document { display: block; max-width: 150mm; margin: 0 auto; }

    .body-section, .chapter, .writing-section, .title-section {
        display: block !important;
        width: auto !important;
        min-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        background: #fff !important;
        color: #000 !important;
    }

    .text, .text a, .text-muted { color: #000 !important; opacity: 1 !important; }
    .text { font-size: 12pt; line-height: 1.45; text-wrap: wrap; }
    p { orphans: 3; widows: 3; }
    a { color: #000 !important; text-decoration: none; }
    h2, h3 { break-after: avoid; }
    [data-section-title] { break-before: page; }
    [data-section-title]::before, [data-section-title]::after { content: none !important; }
    .reading-frame h2 {
        position: static !important; width: auto !important; height: auto !important;
        clip-path: none !important; overflow: visible !important; white-space: normal !important;
        margin: 0 0 10mm !important; color: #000 !important;
    }

    .print-cover {
        page: cover;
        height: calc(100vh - 1px);
        display: flex;
        align-items: center;
        justify-content: center;
        break-after: page;
    }
    .print-cover .title { margin: 0; font-size: 42pt; transform: none; }

    .print-chapter {
        page: auto;
        width: 100%;
        border-collapse: collapse;
        table-layout: fixed;
        break-before: page;
    }
    .print-chapter td { padding: 0; vertical-align: top; }
    .print-chapter thead { display: table-header-group; break-inside: avoid; }
    .print-chapter thead td { padding-top: 22mm; padding-bottom: 12mm; }
    .print-chapter tfoot { display: table-footer-group; break-inside: avoid; }
    .print-chapter tfoot td { height: 22mm; }
    .print-book .print-chapter { page: book; }
    .print-book .print-chapter tfoot { display: none; }
    .print-heading {
        display: flex;
        align-items: center;
        gap: 5mm;
        margin: 0;
        font-size: 11pt;
        font-weight: 400;
        line-height: 1.3;
        font-variant-caps: small-caps;
        font-synthesis: none;
    }
    .print-heading > span:first-child { white-space: nowrap; }
    .print-heading > span:last-child { text-align: right; margin-left: auto; }
    .print-rule { flex: 1; min-width: 5mm; border-top: 0.5pt solid #000; }
    .print-contents {
        list-style: none;
        margin: 0;
        padding: 0;
        font-size: 12pt;
        line-height: 2.2;
        font-variant-caps: small-caps;
        font-synthesis: none;
    }
    /* The heading is outside the centring calculation: the text is centred on
       the sheet itself, rather than in the space left below the heading. */
    .print-centred-page {
        position: relative;
        height: calc(100vh - 1px);
        display: grid;
        place-items: center;
        break-inside: avoid;
    }
    .print-centred-page > .print-heading {
        position: absolute;
        top: 22mm;
        left: 0;
        right: 0;
    }
    .print-book .print-centred-page {
        height: calc(100vh - 22mm - 1px);
        /* Balance the bottom page margin to keep copy centred on the sheet. */
        padding-top: 22mm;
    }
    .print-care, .print-note { width: 100%; text-align: center; }
    .print-care { position: relative; }
    /* Keep the handover itself centred; its address sits just beneath it. */
    .print-keys-address {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        margin: 4mm 0 0;
        font-size: 11pt;
        line-height: 1.3;
    }
    .print-note > .text { max-width: 110mm; margin: 0 auto; }
    .print-note p { margin: 0; text-wrap: balance; }
    .print-boundaries .question { display: block; text-align: left; }
    .print-boundaries .answer { display: block; text-align: right; }
    .print-boundaries .door { text-align: center; }
    .print-note p + p { margin-top: 1em; }
    .print-gratitudes > .chapter { break-inside: avoid; margin-bottom: 6mm !important; }
    .print-keys { page: keys; }
    .print-keys .chapter { break-inside: avoid; margin-bottom: 5mm !important; }
    .print-keys .text { font-size: 11pt; line-height: 1.35; }
    .print-keys .chapter:last-child { margin-bottom: 0 !important; }
}
