/* {{IssuePages}}: an issue's pages on mats (IssuePages.cs). The front cover and the back cover each have a mat of
   their own; the pages between are mounted as open spreads (2-3, 4-5, ...), two pages butted together with a fold
   shaded over the spine. The mat colour and shadows are the matted covers' (components/matted-covers.css).

   Every page is a window (.issue-page) onto the issue's one sprite sheet, in which every page is --h pixels high and
   rows of pages are stacked. The renderer writes only whole pixel numbers: --sheet-w, --rows and --h on the block,
   --x, --w and --row on each page. Here the sheet is drawn so one row fills the window's height, then moved so the
   page's rectangle fills the window. */
.issue-pages {
    /* Unregistered custom properties keep their tokens, so cqi here resolves where the value is used, against this
       block, the nearest container. Mat margins are fractions of the page height, in the proportions of a
       CoverGrid mat (0.16 and 0.13 of its width, whose picture is 1.05 widths high). */
    --page-height: clamp(6rem, 18cqi, 9rem);
    --pad-block: calc(var(--page-height) * 0.152);
    --pad-inline: calc(var(--page-height) * 0.124);
    /* matted-covers.css measures shadows in 1% of a cover's width; a cover is about 0.7 of its height wide. */
    --shadow-unit: calc(var(--page-height) * 0.007);
    /* One mat's height: a page and its top and bottom margins. */
    --mat-height: calc(var(--page-height) + 2 * var(--pad-block));
    /* As wide as the header's contents (nav.header in layout/header.css, whose width, gutter and feather this
       repeats), not the text column: the block reaches out of the column by the same amount on each side, which
       stays centred because main is. On a narrow screen it is the column's width. */
    --wide-feather: clamp(0px, (100vw - var(--content-max-width)) * 0.5, 6rem);
    --wide-width: calc(min(100vw, var(--content-max-width)) - 2 * (var(--grid-gutter) + var(--wide-feather)));
    container-type: inline-size;
    display: flow-root;
    clear: both;
    inline-size: var(--wide-width);
    margin-inline: calc((100% - var(--wide-width)) / 2);
    margin-block-end: 1.5rem;
}

.issue-pages-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: start;
    gap: 1rem;
}

.issue-page-mat {
    position: relative;
    flex: none;
    margin: 0;
    padding: var(--pad-block) var(--pad-inline);
    background: var(--mat-color);
}

/* The pages keep the sheet's left-to-right order in right-to-left text too: page 2 is the left page of a spread. */
:is(.issue-page-single, .issue-page-spread) {
    position: relative;
    display: flex;
    direction: ltr;
    filter: var(--mat-shadow);
}

.issue-page {
    position: relative;
    flex: none;
    block-size: var(--page-height);
    aspect-ratio: var(--w) / var(--h);
    overflow: hidden;
    /* Blank paper until the sheet has loaded, so the fold never shades an empty mat. */
    background: var(--color-paper);

    /* Physical left, top, width and height on purpose: they are coordinates in the sheet image, whatever the
       writing direction. */
    & > img {
        position: absolute;
        left: calc(var(--x) / var(--w) * -100%);
        top: calc(var(--row) * -100%);
        width: calc(var(--sheet-w) / var(--w) * 100%);
        height: calc(var(--rows) * 100%);
        max-inline-size: none;
        max-block-size: none;
    }
}

/* The fold: the paper darkening into the spine, a soft highlight where each page curves up from it, and a faint
   shade at the outer edges where the pages curl away. --spine is where the left page ends. */
.issue-page-spread {
    --spine: calc(var(--left-w) / (var(--left-w) + var(--right-w)) * 100%);

    &::after {
        content: "";
        position: absolute;
        inset: 0;
        pointer-events: none;
        background:
            linear-gradient(to right,
                transparent calc(var(--spine) - 16%),
                rgb(255 255 255 / 0.08) calc(var(--spine) - 8%),
                rgb(0 0 0 / 0.06) calc(var(--spine) - 4%),
                rgb(0 0 0 / 0.24) calc(var(--spine) - 1%),
                rgb(0 0 0 / 0.42) var(--spine),
                rgb(0 0 0 / 0.24) calc(var(--spine) + 1%),
                rgb(0 0 0 / 0.06) calc(var(--spine) + 4%),
                rgb(255 255 255 / 0.08) calc(var(--spine) + 8%),
                transparent calc(var(--spine) + 16%)),
            linear-gradient(to right,
                rgb(0 0 0 / 0.12),
                transparent 3%,
                transparent 97%,
                rgb(0 0 0 / 0.12));
    }
}

/* The caption, written small on the mat's bottom margin as on a CoverGrid mat: the page label, or the author's
   caption in its place. One line always, so every mat in a row is the same height (the folded row is clipped to
   it); a caption too long for its mat ends in an ellipsis, with its full text in the tooltip. */
.issue-page-mat > figcaption {
    position: absolute;
    inset-inline: 0;
    inset-block-end: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    block-size: var(--pad-block);
    padding-inline: calc(var(--pad-inline) / 2);
    color: var(--color-paper-ink-soft);
    font-family: var(--font-family-san-serif);
    font-size: clamp(0.6rem, calc(var(--page-height) * 0.075), 0.75rem);
    font-weight: 600;
    letter-spacing: 0.06em;
    line-height: 1;
    text-transform: uppercase;
    white-space: nowrap;

    & > span {
        min-inline-size: 0;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

/* Folding, like a long citation list (pages/article.css). An issue on more than ten mats
   (IssuePagesRenderer.FoldAfterMats) ends in an empty <details>, used only for its open state. While it is closed the
   mats are clipped to a row and a half, the half row fading out, and its "Show all" button sits over the fade. Opening
   it removes the clip and hides the disclosure, so the issue stays open. In print every mat shows. */
.issue-pages:has(> details:not([open])) > .issue-pages-row {
    max-block-size: calc(1.5 * var(--mat-height) + 1rem);
    overflow: clip;
    mask-image: linear-gradient(to bottom, black calc(var(--mat-height) + 0.5rem), transparent);
}

.issue-pages > details {
    position: relative;
    margin-block-start: -2.75rem;
    text-align: center;
}

.issue-pages > details[open] {
    display: none;
}

@media print {
    .issue-pages:has(> details:not([open])) > .issue-pages-row {
        max-block-size: none;
        mask-image: none;
    }

    .issue-pages > details {
        display: none;
    }
}
