/* Matted covers, shared by the home page's grid (.home-cover-grid, pages/home.css) and the {{CoverGrid}} markup
   extension's grid in articles (.cover-grid, pages/article.css).

   Each card (.cover-item) is a mat (a fixed-shape block of warm grey) with the picture mounted inside its margin
   (.cover-frame) and a shadow under it, like a print or a magazine laid on board. Covers are shown whole
   (object-fit: contain), whatever their shape; the shadow is a drop-shadow filter, which follows the painted
   picture rather than the letterboxed box, so a square or landscape cover gets a shadow of its own shape.
   The mat's margins are fractions of a single column's width W, so every card has the same margins.
   The mat is painted as the card's ::before, beneath the picture and the inscription, so that the home page's
   sliding label (pages/home.css) can sit beneath the mat too. Each card is its own stacking context. */
:is(.home-cover-grid, .cover-grid) {
    --mat-ratio: 0.73;
    --mat-pad-block: 0.16;
    --mat-pad-inline: 0.13;
    --mat-color: var(--color-paper-well);
    /* The shadows: a hairline edge (so a pale cover still parts from the pale mat), then one soft shadow cast down
       and to the left, as if lit from the upper right. The shadow is a solid darker shade of the mat, not
       translucent black, so it reads as shade on the board rather than a grey smudge (after Playboy's archive,
       which uses rgb(127 126 113) -4px 5px 10px under a 136px cover). --shadow-unit is 1% of the frame's width,
       resolved at the picture, so shadows scale with the mat. */
    --shadow-unit: 1cqi;
    --mat-shade: color-mix(in oklab, var(--mat-color) 62%, var(--color-black));
    --mat-shadow:
        drop-shadow(0 0 0.6px color-mix(in oklab, var(--mat-shade) 70%, transparent))
        drop-shadow(calc(-2.9 * var(--shadow-unit)) calc(3.7 * var(--shadow-unit)) calc(7.4 * var(--shadow-unit)) var(--mat-shade));
    --mat-shadow-lifted:
        drop-shadow(0 0 0.6px color-mix(in oklab, var(--mat-shade) 70%, transparent))
        drop-shadow(calc(-3.8 * var(--shadow-unit)) calc(5.5 * var(--shadow-unit)) calc(10 * var(--shadow-unit)) var(--mat-shade));
}

.cover-item {
    container-type: inline-size;
    box-sizing: border-box;
    position: relative;
    display: grid;
    grid-template: minmax(0, 1fr) / minmax(0, 1fr);
    align-self: start;
    aspect-ratio: var(--mat-ratio);
    z-index: 0;
    padding: calc(var(--mat-pad-block) * 100%) calc(var(--mat-pad-inline) * 100%);
    text-decoration: none;
    color: inherit;

    &::before {
        content: "";
        position: absolute;
        inset: 0;
        z-index: 1;
        background: var(--mat-color);
    }

    &:focus-visible {
        outline: 2px solid var(--color-primary);
        outline-offset: 2px;
    }
}

/* The frame stays unpositioned, so the card (the mat) is the containing block of what is laid against it. */
.cover-frame {
    display: block;
    min-block-size: 0;

    & > img {
        position: relative;
        z-index: 2;
        display: block;
        inline-size: 100%;
        block-size: 100%;
        object-fit: contain;
        filter: var(--mat-shadow);
    }
}

/* Hover lifts the picture off its mat; the mat stays put. */
@media (hover: hover) and (pointer: fine) {
    .cover-item:hover .cover-frame > img {
        filter: var(--mat-shadow-lifted);
    }
}

@media (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) {
    .cover-frame > img {
        transition: translate 0.2s ease-in-out, filter 0.2s ease-in-out;
    }

    .cover-item:hover .cover-frame > img {
        translate: 0 -4px;
    }
}

/* The name and issue written small on the mat's bottom margin, like an inscription on a mounted print. The home
   grid shows it only on touch screens, which have no hover label (pages/home.css); article grids always show it.
   The margin's height is padBlock * W, and the inscription's 100cqi is the card's inside width,
   W * (1 - 2 * padInline). Two lines at most, so a long title keeps its issue and date. Issue parts are each kept
   on one line (nowrap spans), so a wrapped line breaks between parts or inside the publication's name. */
.cover-item .cover-inscription {
    position: absolute;
    z-index: 2;
    inset-inline: 0;
    inset-block-end: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    block-size: calc(var(--mat-pad-block) * 100cqi / (1 - 2 * var(--mat-pad-inline)));
    margin: 0;
    padding-inline: calc(var(--mat-pad-inline) * 100cqi / (1 - 2 * var(--mat-pad-inline)));
    color: var(--color-paper-ink-soft);
    font-family: var(--font-family-san-serif);
    font-size: clamp(0.625rem, 4cqi, 0.8rem);
    font-weight: 600;
    letter-spacing: 0.06em;
    line-height: 1.25;
    text-align: center;
    text-wrap: balance;
    text-transform: uppercase;

    & > span {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        overflow: hidden;

        & > span {
            white-space: nowrap;
        }
    }
}
