/*
 * Adapt stored report HTML (designed for paged PDFs) to the scrollable web viewer.
 * Keep sheet-like sections, restore print-positioned content to normal flow, and reflow
 * layouts to fit each section. All document overrides are scoped to the viewer.
 * PDF layouts do not load this stylesheet.
 *
 * Rules are grouped as viewer controls, sheets, document flow, then responsive layouts.
 */

/* Viewer controls */
.report-viewer__document {
    scroll-behavior: smooth;
}

/* Turbo manages busy state on success, failure, and cancellation; no extra controller is needed. */
#assessment-report-viewer[busy] .report-viewer__document-toggle {
    pointer-events: none;
}

#assessment-report-viewer[busy] .report-viewer__document-toggle > span {
    visibility: hidden;
}

#assessment-report-viewer[busy] .report-viewer__document-toggle > .report-viewer__toggle-spinner {
    display: block;
    visibility: visible;
}

.report-viewer__outline-link[aria-current="true"] {
    border-left-color: #271373;
    background-color: #f4f5fb;
}

@media (prefers-reduced-motion: reduce) {
    .report-viewer__document {
        scroll-behavior: auto;
    }

    .report-viewer__toggle-spinner {
        animation: none;
    }
}

/* Sheets: use the reading pane's width for page proportions and spacing. */
.report-viewer__body {
    --report-page-inline-padding: clamp(1.5rem, 6cqw, 3rem);
    --report-page-block-padding: 3rem;

    max-width: 46rem;
    margin: 0 auto;
    container-type: inline-size;
}

/* Anchor print-positioned graphics to their section. */
.report-viewer__body > div {
    position: relative;
}

.report-viewer__body .report-viewer__page {
    break-before: auto;
    min-height: calc(100cqw * 11 / 8.5);
    scroll-margin-top: 1rem;
    padding: var(--report-page-block-padding) var(--report-page-inline-padding);
    margin-bottom: 1.5rem;
    background-color: #fff;
    border: 1px solid #d9d9d9;
}

/* These pages get their background from paged.js's @page rules in print. */
.report-viewer__body--report #cover-page,
.report-viewer__body .report-viewer__separator {
    background-color: #271373;
    color: #fff;
    border-color: #271373;
}

/* Center short separators, but let long copy grow the page with padding at both ends. */
.report-viewer__body .report-viewer__separator {
    display: flex;
    flex-direction: column;
    justify-content: center;
    aspect-ratio: 8.5 / 11;
}

.report-viewer__body .report-viewer__separator > :first-child {
    padding-top: 0;
    width: 100%;
    height: auto;
}

.report-viewer__body .report-viewer__separator .w-3\/5 {
    width: 100%;
    font-size: clamp(2rem, 5cqw, 3rem);
}

/* Keep the cover content and graphic inside the web sheet. */
.report-viewer__body #cover-page {
    display: flex;
    flex-direction: column;
    overflow: clip;
}

.report-viewer__body #cover-page > div:first-child {
    margin-top: 0;
}

/* Printed covers offset the graphic into the page margin while web sheets have no print margin. */
.report-viewer__body #cover-page > .absolute.-right-12 {
    right: 0.5rem;
}

.report-viewer__body--report #cover-page > h1 {
    margin-top: clamp(3rem, 16cqw, 9rem);
}

/* Referral QR codes and their note are for the downloaded document only. */
.report-viewer__body #cover-page > .absolute.left-0 {
    display: none;
}

.report-viewer__body--report #cover-page a,
.report-viewer__body .report-viewer__separator a,
.report-viewer__body .report-viewer__separator .text-metrixPurple {
    color: #fff;
}

/* Document flow: allow content to grow beyond the fixed heights used in print. */
.report-viewer__body #important-considerations-page > .absolute {
    position: static;
    margin: 2rem 0 0 auto;
}

/* Keep footnotes below their section's content when it exceeds a sheet. */
.report-viewer__body .break > .absolute.text-sm,
.report-viewer__body .break > .absolute.text-base,
.report-viewer__body [id^="finding-"] .absolute.bottom-0.text-sm,
.report-viewer__body #summary-page .absolute.text-sm,
.report-viewer__body #summary-page .absolute.text-base {
    position: static;
    margin-top: 2rem;
}

/* Retest charts place the previous-score caption below the bar. Reserve its space in flow. */
.report-viewer__body [id^="finding-"] .relative.border:has(.-bottom-6) {
    margin-bottom: 2rem;
}

/* Page references are populated only during PDF generation. */
.report-viewer__body .page-reference {
    display: none;
}

.report-viewer .suicidality-assessment-table {
    table-layout: fixed;
    width: 100%;
}

/* Responsive layouts: wrap and stack print columns to fit the reading pane. */
@media screen {
    /* Print repeats the condition name above clinical considerations on a new page.
       Hide that duplicate heading in the viewer, where the finding is one continuous section. */
    .report-viewer__body [id^="finding-"] .break > h3:first-child {
        display: none;
    }

    .report-viewer__body {
        overflow-wrap: anywhere;
    }

    .report-viewer__body .grid > *,
    .report-viewer__body .flex > * {
        min-width: 0;
    }

    .report-viewer__body .text-nowrap,
    .report-viewer__body .whitespace-nowrap {
        white-space: normal;
    }

    /* ViewerDocument marks warnings in both new and stored reports. Keep them beside the condition's last line. */
    .report-viewer__body .report-viewer__malingering-link {
        display: inline;
    }

    .report-viewer__body .report-viewer__malingering-icon {
        display: inline-block;
        vertical-align: middle;
    }

    /* This caption is anchored to a zero-width score marker so it needs its own intrinsic width. */
    .report-viewer__body [id^="finding-"] .-bottom-6 {
        white-space: nowrap;
        width: max-content;
        right: 0;
    }

    @container (width > 40rem) {
        /* Give result labels enough room without crowding the test name. */
        .report-viewer__body #possible-explanation-page .grid-cols-12 {
            grid-template-columns: repeat(2, minmax(0, 1fr)) max-content repeat(9, minmax(0, 1fr));
        }

        .report-viewer__body #possible-explanation-page .grid-cols-12 > .col-span-1 {
            white-space: nowrap;
        }
    }

    @container (max-width: 40rem) {
        .report-viewer__body #summary-page h1 {
            font-size: clamp(1.5rem, 5cqw, 2rem);
        }

        .report-viewer__body #possible-explanation-page .grid-cols-2 {
            font-size: 0.75rem;
        }

        /* Likelihoods now sit below the condition, so their print column heading no longer applies. */
        .report-viewer__body #possible-explanation-page .grid-cols-2 > .text-right {
            display: none;
        }

        /* Stack condition names above their likelihood instead of squeezing two print columns. */
        .report-viewer__body #possible-explanation-page .grid-cols-5 {
            grid-template-columns: minmax(0, 1fr);
            padding: 0.75rem 0;
        }

        .report-viewer__body #possible-explanation-page .grid-cols-5 > * {
            grid-column: auto;
            padding: 0.25rem 0;
        }

        .report-viewer__body #possible-explanation-page .grid-cols-5 .text-right {
            text-align: left;
        }

        /* Read the scale name first, then its chart and interpretation together. */
        .report-viewer__body #possible-explanation-page .grid-cols-12 {
            grid-template-columns: 4.5rem minmax(0, 1fr);
            gap: 0.5rem 0.75rem;
            margin-bottom: 1rem;
        }

        .report-viewer__body #possible-explanation-page .grid-cols-12 > * {
            grid-column: auto;
            margin: 0;
        }

        .report-viewer__body #possible-explanation-page .grid-cols-12 > .col-span-9 {
            grid-column: 1 / -1;
            grid-row: 1;
        }

        /* Patient information precedes symptoms, and alert cards each get a full row. */
        .report-viewer__body #clinical-orientation-page .flex.gap-6 {
            flex-direction: column;
        }

        .report-viewer__body #clinical-orientation-page .flex.gap-6 > div {
            width: 100%;
        }

        .report-viewer__body #clinical-orientation-page .grid.gap-4 {
            grid-template-columns: minmax(0, 1fr);
        }

        .report-viewer__body #diagnostic-considerations-page .grid-cols-2.gap-4 {
            grid-template-columns: minmax(0, 1fr);
        }

        .report-viewer__body :is(#background-page, #adolescent-background-page) .grid {
            grid-template-columns: minmax(0, 1fr);
            gap: 1rem;
        }

        .report-viewer__body :is(#background-page, #adolescent-background-page) > .flex-row {
            flex-direction: column;
            gap: 1rem;
        }

        /* Scale separator typography and gaps with the sheet, rather than retaining print-size text. */
        .report-viewer__body .report-viewer__separator {
            font-size: clamp(0.625rem, 3cqw, 1rem);
            padding-block: clamp(1rem, 6cqw, 3rem);
        }

        .report-viewer__body .report-viewer__separator h1,
        .report-viewer__body .report-viewer__separator .w-3\/5 {
            font-size: clamp(1.125rem, 5cqw, 2rem);
        }

        .report-viewer__body .report-viewer__separator .mb-8 {
            margin-bottom: clamp(0.5rem, 2cqw, 2rem);
        }

        /* Detailed test rows keep score and interpretation below the test description. */
        .report-viewer__body [id^="finding-"] .col-span-5.grid-cols-5 {
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }

        .report-viewer__body [id^="finding-"] .col-span-5.grid-cols-5 > .col-span-3 {
            grid-column: 1 / -1;
        }

        .report-viewer__body [id^="finding-"] .grid-cols-5 > .break-inside-avoid + .break-inside-avoid {
            margin-top: 1rem;
        }

        .report-viewer__body [data-test-id="finding.specifiers"] .text-sm + .text-sm {
            margin-top: 0.5rem;
        }

        .report-viewer__body table {
            table-layout: fixed;
            width: 100%;
        }

        .report-viewer__body #summary-page .columns-2 {
            columns: 1;
        }
    }
}

@media screen and (max-width: 767px) {
    .report-viewer__body {
        --report-page-inline-padding: 1.5rem;
        --report-page-block-padding: 2rem;
    }

    .report-viewer__body .report-viewer__page {
        margin-bottom: 1rem;
    }
}
