/* Centralized report print/export layout. */

.template-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.template-header img {
    width: 56px;
    height: 56px;
    border-radius: 8px;
    object-fit: cover;
}

.template-header div {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.template-header h1 {
    margin: 0 0 4px;
    font-size: 24px;
}

.template-header span {
    color: #667085;
}

.template-footer {
    display: flex;
    margin-top: auto;
    justify-content: space-between;
    gap: 20px;
    padding-top: 18px;
    border-top: 1px solid #d0d5dd;
    color: #667085;
    font-size: 12px;
}

/* Crystal Reports-style report footer: stays at the bottom of the report page. */
.report-footer,
.template-footer {
    margin-top: auto;
}

/* The report summary/footer belongs above the fixed page footer. */
.report-paper .report-page .report-footer,
.report-paper .report-page .template-footer {
    margin-bottom: 28px;
}

.report-page {
    box-sizing: border-box;
    width: 100%;
    height: 297mm;
    min-height: 297mm;
    display: flex;
    flex-direction: column;
    padding: 18mm;
    background: #fff;
    break-after: auto;
    page-break-after: auto;
}

.report-page > .print-template {
    flex: 1 1 auto;
    min-height: 0;
}

.report-page:last-child {
    break-after: auto;
    page-break-after: auto;
}

/* Annual Sales is a flowing table report. Its table owns the document flow;
   do not let the fixed-page footer sit on top of rows after the first sheet. */
.report-page:has([data-report-template]) {
    height: auto;
    min-height: 297mm;
    background: transparent !important;
}

/* html2pdf safety allowance to prevent a rounded A4 page from spilling over. */
.report-page.pdf-export {
    height: 294mm;
    min-height: 294mm;
}

.report-page-footer {
    display: flex;
    margin-top: auto;
    justify-content: space-between;
    gap: 20px;
    padding-top: 8px;
    color: #667085;
    font-size: 11px;
}

.print-template {
    display: flex;
    flex: 1 1 auto;
    min-height: 100%;
    padding: 0;
    flex-direction: column;
    gap: 24px;
    color: #101828;
    font-size: 12px;
}

.template-meta,
.template-total {
    display: flex;
    justify-content: space-between;
    gap: 20px;
}

.template-meta div {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.template-meta {
    padding: 14px 0;
    border-block: 1px solid #d0d5dd;
}

.template-meta span {
    color: #667085;
}

.template-table {
    width: 100%;
    border-collapse: collapse;
}

.template-table th,
.template-table td {
    padding: 10px 8px;
    border-bottom: 1px solid #eaecf0;
    text-align: left;
}

.template-table th {
    background: #f9fafb;
}

.template-table .amount {
    text-align: right;
}

.template-total {
    justify-content: flex-end;
    padding-top: 8px;
    font-size: 16px;
}

.template-notes {
    padding-top: 18px;
    border-top: 1px solid #d0d5dd;
    color: #667085;
}

.template-table thead {
    display: table-header-group;
}

.template-table tfoot {
    display: table-footer-group;
}

.template-table tr,
.template-total,
.template-notes {
    break-inside: avoid;
    page-break-inside: avoid;
}

.report-summary {
    display: flex;
    justify-content: flex-end;
    gap: 24px;
    padding-top: 8px;
    font-size: 16px;
    break-inside: avoid;
    page-break-inside: avoid;
}

@media print {
    body * {
        visibility: hidden !important;
    }

    .report-viewer-page,
    .report-viewer-page * {
        visibility: visible !important;
    }

    .report-viewer-page {
        position: absolute !important;
        inset: 0 !important;
        display: block !important;
        width: 100% !important;
        height: auto !important;
        margin: 0 !important;
        background: #fff !important;
    }

    .report-options,
    .report-viewer-toolbar,
    .report-pagination {
        display: none !important;
    }

    .report-viewer,
    .paper-scroll-area {
        display: block !important;
        height: auto !important;
        overflow: visible !important;
        padding: 0 !important;
    }

    .report-paper {
        display: block !important;
        width: auto !important;
        min-height: 0 !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        margin: 0 !important;
        background: #fff !important;
        box-shadow: none !important;
    }

    .report-page {
        break-after: page;
        page-break-after: always;
    }

    .report-page:last-child {
        break-after: auto;
        page-break-after: auto;
    }

    [data-report-template] {
        display: block;
        min-height: 0;
    }

    /* Annual sales is a flowing detail report. Let the browser paginate the
       table naturally instead of forcing a guessed number of rows per page. */
    .report-page:has([data-report-template]) {
        display: block !important;
        height: auto;
        min-height: 0;
        max-height: none !important;
        overflow: visible !important;
        padding: 18mm;
        background: #fff;
        break-after: auto;
        page-break-after: auto;
    }

    [data-report-template] {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    .annual-sales-table {
        width: 100%;
        break-inside: auto;
        page-break-inside: auto;
    }

    .annual-sales-table thead {
        display: table-header-group;
    }

    .annual-sales-table tr {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    .template-table {
        width: 100%;
        border-collapse: collapse;
    }

    .template-table thead {
        display: table-header-group;
    }

    .template-table tr,
    .template-header,
    .template-footer,
    .report-page-footer {
        break-inside: avoid;
        page-break-inside: avoid;
    }
}

/* html2pdf captures the report paper element, which normally has the gray
   screen-only separator canvas. Keep that canvas out of exported PDFs. */
.report-paper.pdf-export {
    background: #fff !important;
    background-image: none !important;
    box-shadow: none !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
}

.report-paper.pdf-export .report-page:has([data-report-template]) {
    height: 300mm !important;
    min-height: 300mm !important;
    max-height: 300mm !important;
    margin-bottom: 0 !important;
    background: #fff !important;
    break-after: auto !important;
    page-break-after: auto !important;
}

.report-paper.pdf-export .report-page:has([data-report-template]):last-of-type {
    break-after: auto !important;
    page-break-after: auto !important;
}

@page {
    size: A4 portrait;
    margin: 0;
}

/* Real Annual Sales page instances. These override the earlier flowing-table
   preview rules now that Blazor renders one ReportPage per measured page. */
.report-paper .report-page:has([data-report-template]) {
    height: 297mm;
    min-height: 297mm;
    max-height: 297mm;
    overflow: hidden;
    padding: 18mm;
    background: #fff !important;
}

.report-paper .report-page:has([data-report-template]) > .print-template {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 100% !important;
}

.report-paper .report-page:has([data-report-template]):last-child {
    break-after: auto;
    page-break-after: auto;
}

@media screen {
    .report-paper {
        background: #e5e7eb !important;
        background-image: none !important;
    }

    .report-paper .report-page {
        margin-bottom: 16px;
    }

    .report-paper .report-page:last-child {
        margin-bottom: 0;
    }

    .report-paper .report-page:has([data-report-template]) {
        margin-bottom: 16px;
    }

    .report-paper .report-page:has([data-report-template]):last-child {
        margin-bottom: 0;
    }
}

@media print {
    .report-viewer-page {
        position: static !important;
        inset: auto !important;
        width: 100% !important;
        height: auto !important;
    }

    .report-paper .report-page:has([data-report-template]) {
        height: 297mm !important;
        min-height: 297mm !important;
        max-height: 297mm !important;
        overflow: hidden !important;
        padding: 18mm !important;
        background: #fff !important;
        break-after: page !important;
        page-break-after: always !important;
    }

    .report-paper .report-page:has([data-report-template]):last-child {
        break-after: auto !important;
        page-break-after: auto !important;
        margin-bottom: 0 !important;
    }

    .report-paper .report-page:has([data-report-template]) {
        margin-bottom: 0 !important;
    }
}

/* PDF export uses one JavaScript break before each continuation page. */
.report-paper.pdf-export .report-page {
    break-after: auto !important;
    page-break-after: auto !important;
}

/* Keep the 300 mm PDF canvas, but leave a rounding allowance inside each
   captured report page so html2pdf does not create an empty boundary sheet. */
.report-paper.pdf-export .report-page:has([data-report-template]) {
    height: 296mm !important;
    min-height: 296mm !important;
    max-height: 296mm !important;
}

/* Anchor the nested page footer inside the page and reserve its space in the
   template so it remains visible without covering detail rows. */
.report-paper .report-page:has([data-report-template]) {
    position: relative;
}

.report-paper .report-page:has([data-report-template]) > .print-template {
    min-height: 0 !important;
}

.report-paper .report-page:has([data-report-template]) .report-page-footer {
    position: absolute;
    left: 18mm;
    right: 18mm;
    bottom: 18mm;
    margin-top: 0;
}
