/**
 * WP native lightbox (core/image "Enlarge on click") overrides
 *
 * Why: core sizes the lightbox to the *cropped* thumbnail's aspect ratio
 * (e.g. a 1:1 gallery tile) and covers the image inside it, so a tall image
 * shows only a square slice and cannot be scrolled. These overrides let the
 * image render at full height and make the overlay itself scroll when the
 * image is taller than the viewport.
 *
 * Companion JS: /js/applari-js/wp-lightbox.js (re-enables touch scrolling,
 * which core blocks with preventDefault on touchmove).
 *
 * Core's own sizing vars are NOT used: they derive from the attachment's
 * "scaled" metadata, which for very tall images is only a few hundred px
 * wide (WP caps the long edge at 2560 px), and core also bumps the container
 * back down to the thumbnail size. Instead the container fills the available
 * width, capped by --applari-lightbox-max-width, which the companion JS sets
 * from the largest srcset candidate so small images are not upscaled.
 *
 * Core writes the thumbnail's inline styles (e.g. `aspect-ratio:1` from a
 * cropped gallery) onto the lightbox <img>, so the img sizing rules need
 * !important to beat those inline declarations.
 *
 * Selectors are prefixed with `body` to win the cascade regardless of the
 * order core's block styles are printed.
 */

body .wp-lightbox-overlay {
    display: grid;
    grid-template-columns: 100%;
    box-sizing: border-box;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
    /* Room for close (top) and prev/next buttons (bottom on mobile). */
    padding: calc(env(safe-area-inset-top) + 72px) 16px calc(env(safe-area-inset-bottom) + 72px);
}

/* Fixed chrome: stays in place while the overlay scrolls. */
body .wp-lightbox-overlay .scrim {
    position: fixed;
    inset: 0;
    width: auto;
    height: auto;
}

body .wp-lightbox-overlay .wp-lightbox-close-button,
body .wp-lightbox-overlay .wp-lightbox-navigation-button-prev,
body .wp-lightbox-overlay .wp-lightbox-navigation-button-next {
    position: fixed;
}

/* Keep the close button clear of the overlay's own scrollbar. */
body .wp-lightbox-overlay .wp-lightbox-close-button {
    right: calc(env(safe-area-inset-right) + 16px + var(--wp--lightbox-scrollbar-width, 0px));
}

body .wp-lightbox-overlay .wp-lightbox-navigation-button-next {
    right: calc(env(safe-area-inset-right) + 16px + var(--wp--lightbox-scrollbar-width, 0px));
}

/*
 * Core renders two stacked containers (placeholder + enlarged image). Both
 * sit in the same grid cell so they still overlap without absolute
 * positioning. Auto margins center them when the image fits; when it does
 * not, the row grows and the overlay scrolls.
 */
body .wp-lightbox-overlay .lightbox-image-container {
    grid-area: 1 / 1;
    position: relative;
    top: auto;
    left: auto;
    transform: none;
    width: min(100%, var(--applari-lightbox-max-width, 100%));
    max-width: 100%;
    height: auto;
    margin: auto;
    overflow: visible;
}

body .wp-lightbox-overlay .wp-block-image {
    display: block;
    height: auto;
}

body .wp-lightbox-overlay .wp-block-image img {
    display: block;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    min-width: 0 !important;
    min-height: 0 !important;
    aspect-ratio: auto !important;
    object-fit: contain !important;
}

/*
 * Core's "zoom" animation translates the container from the thumbnail's
 * screen position to translate(-50%,-50%), which no longer matches our flow
 * layout. Swap it for the same fade core uses for the scrim.
 */
@media not (prefers-reduced-motion) {
    body .wp-lightbox-overlay.zoom.active .lightbox-image-container {
        animation: turn-on-visibility 0.35s both;
    }

    body .wp-lightbox-overlay.zoom.show-closing-animation:not(.active) .lightbox-image-container {
        animation: turn-off-visibility 0.25s both;
    }
}

@media (min-width: 960px) {
    body .wp-lightbox-overlay {
        /* Prev/next sit at the sides on desktop, so less vertical room needed. */
        padding: calc(env(safe-area-inset-top) + 56px) 96px calc(env(safe-area-inset-bottom) + 56px);
    }
}
