/* ==========================================
   EDGE FADE
   → POSTS FADE OUT TO TRANSPARENT
   → INSTEAD OF BLENDING INTO A SOLID COLOUR

   Works regardless of what is behind the slider: solid colour, gradient or
   image.

   Implemented with a CSS mask-image directly on the scrolling element: the
   cards themselves become transparent at the edges, nothing opaque lies on
   top of them.
   ========================================== */

.uagb-slider-fade-wrap {
    position: relative;

    /*
     * Width of the fade.
     *
     * Deliberately equal to the side padding of the slider (see
     * .uagb-post-grid further down), so the transition ends exactly at the
     * edge of the first/last card instead of in the middle of the shadow area.
     */
    --uagb-slider-fade-width: 40px;
}


.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {

    /*
     * MASK:
     *
     * From the left 0 → 100% opacity at --uagb-slider-fade-width, mirrored on
     * the right.
     *
     * Default: both sides faded. Lifted again at the respective edge through
     * the JS state classes (is-at-start / is-at-end), see below.
     */
    -webkit-mask-image: linear-gradient(
        to right,
        transparent 0,
        black var(--uagb-slider-fade-width),
        black calc(100% - var(--uagb-slider-fade-width)),
        transparent 100%
    );

    mask-image: linear-gradient(
        to right,
        transparent 0,
        black var(--uagb-slider-fade-width),
        black calc(100% - var(--uagb-slider-fade-width)),
        transparent 100%
    );

    transition: -webkit-mask-image 0.2s ease,
                mask-image 0.2s ease;
}


/*
 * Reached the start edge:
 * no fade on the left any more (full opacity from the beginning).
 */
.uagb-slider-fade-wrap.is-at-start .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {

    -webkit-mask-image: linear-gradient(
        to right,
        black 0,
        black calc(100% - var(--uagb-slider-fade-width)),
        transparent 100%
    );

    mask-image: linear-gradient(
        to right,
        black 0,
        black calc(100% - var(--uagb-slider-fade-width)),
        transparent 100%
    );

}


/*
 * Reached the end edge:
 * no fade on the right any more.
 */
.uagb-slider-fade-wrap.is-at-end .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {

    -webkit-mask-image: linear-gradient(
        to right,
        transparent 0,
        black var(--uagb-slider-fade-width),
        black 100%
    );

    mask-image: linear-gradient(
        to right,
        transparent 0,
        black var(--uagb-slider-fade-width),
        black 100%
    );

}


/*
 * At BOTH edges (e.g. the slider fits into view completely, no scrolling
 * needed): remove the mask entirely.
 */
.uagb-slider-fade-wrap.is-at-start.is-at-end .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {
    -webkit-mask-image: none;
    mask-image: none;
}


/* ==========================================
   UAGB POST GRIDS
   → HORIZONTAL FREE SCROLL
   → VERTICAL PAGE SCROLL AT THE END
   → DO NOT CUT OFF THE CARD SHADOWS
   ========================================== */

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;

    width: 100% !important;
    max-width: none !important;

    gap: 24px !important;

    /*
     * EXTRA PADDING LEFT/RIGHT
     * so the shadow of the first and last card is not cut off at the edge of
     * the scroll area.
     */
    padding: 20px 40px 30px !important;

    margin: 0 !important;

    overflow-x: auto !important;
    overflow-y: hidden !important;

    /*
     * NO SNAPPING
     */
    scroll-snap-type: none !important;

    /*
     * TOUCH:
     * the browser may tell horizontal and vertical gestures apart.
     */
    touch-action: pan-x pan-y;

    /*
     * MOUSE
     */
    cursor: grab;

    /*
     * DO NOT SELECT TEXT WHILE DRAGGING
     */
    user-select: none;
    -webkit-user-select: none;

    /*
     * LIMIT HORIZONTAL OVERSCROLL
     *
     * Vertical page scrolling stays allowed.
     */
    overscroll-behavior-x: contain;
    overscroll-behavior-y: auto;

    /*
     * Neutralise the Spectra grid
     */
    grid-template-columns: none !important;

    /*
     * Needed for the correct width
     */
    box-sizing: border-box !important;

    /*
     * Scroll padding matching the shadow area
     */
    scroll-padding-left: 40px;
    scroll-padding-right: 40px;
}


/* ==========================================
   MOUSE BUTTON PRESSED
   ========================================== */

.uagb-post-grid.is-dragging {
    cursor: grabbing !important;
}


/* ==========================================
   POST CARDS
   ========================================== */

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap {

    /*
     * FIXED CARD WIDTH
     */
    flex: 0 0 340px !important;

    width: 340px !important;
    min-width: 340px !important;
    max-width: 340px !important;

    /*
     * NO AUTOMATIC SPACING
     */
    margin: 0 !important;

    /*
     * Neutralise the Spectra grid
     */
    grid-column: auto !important;
    grid-row: auto !important;

    /*
     * NO SNAP
     */
    scroll-snap-align: none !important;
}


/* ==========================================
   HIDE PAGINATION
   ========================================== */

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post-pagination-wrap {
    display: none !important;
}


/* ==========================================
   UNIFORM IMAGES
   ========================================== */

/*
 * Only in the NORMAL layout (image above/below): image always 16:9.
 * In the background layout the card itself takes care of that
 * (see section "IMAGE AS BACKGROUND").
 */
.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry, .uagb-post__image-position-background) .uagb-post__image img {
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
}


/* ==========================================
   READ-MORE BUTTONS AT THE SAME HEIGHT
   (normal layout AND background layout)

   All cards of a row are stretched to the same height, the card itself is a
   vertical flexbox and the button container is pushed to the bottom edge
   with margin-top:auto.
   ========================================== */

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {
    align-items: stretch !important;
}

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap {
    display: flex !important;
    flex-direction: column !important;
}

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap > * {
    flex-shrink: 0;
}

.uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap > .uagb-post__cta {
    margin-top: auto !important;
}


/* ==========================================
   IMAGE AS BACKGROUND
   → CARD ALWAYS 16:9, ALL CONTENT VISIBLE
   → CARDS GET WIDER, HEIGHT AS SMALL AS POSSIBLE

   The JS looks for the smallest height H at which the whole content fits
   into a card of width H × 16 / 9 and sets:

     --uagb-card-h  = H
     --uagb-card-w  = H × 16 / 9

   Layers, overlay and position of the read-more button are still handled by
   Spectra itself (image z-index 2, text z-index 10) – the z-index values are
   deliberately NOT touched here. Without JS everything stays in the normal
   layout.
   ========================================== */

.uagb-post-grid.uagb-post__image-position-background {

    /*
     * Space below the excerpt for the read-more button
     * (Spectra reserves 100px here by default).
     * Button ≈ 36px high + 20px distance to the edge.
     */
    --uagb-card-btn-space: 70px;
}

.uagb-post-grid.uagb-post__image-position-background .uagb-post__inner-wrap > .uagb-post__text:nth-last-child(3) {
    padding-bottom: var(--uagb-card-btn-space) !important;
}

/*
 * IMAGE EXACTLY AT CARD SIZE
 *
 * In the background layout Spectra sets width:auto / height:auto with
 * min-width/min-height:100% and centres the image through a transform. That
 * makes the NATURAL image width apply (e.g. 800 to 1024px) – a narrower card
 * then only shows an enlarged crop.
 *
 * Here the image is stretched exactly onto the card instead. Card and image
 * are both 16:9, so nothing is cut off. Other image formats: object-fit: cover.
 */
.uagb-post-grid.uagb-post__image-position-background .uagb-post__image img {
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    min-width: 0 !important;
    min-height: 0 !important;
    max-width: 100% !important;
    transform: none !important;
    aspect-ratio: auto !important;
    object-fit: cover;
    object-position: center;
}

/* Width (also while measuring) */
.uagb-post-grid.uagb-post__image-position-background.is-ratio-measure > .uagb-post__inner-wrap,
.uagb-post-grid.uagb-post__image-position-background.is-ratio-sized > .uagb-post__inner-wrap {
    flex: 0 0 var(--uagb-card-w) !important;
    width: var(--uagb-card-w) !important;
    min-width: var(--uagb-card-w) !important;
    max-width: var(--uagb-card-w) !important;
}

/* While measuring: free height, determined by the content */
.uagb-post-grid.uagb-post__image-position-background.is-ratio-measure > .uagb-post__inner-wrap {
    height: auto !important;
    min-height: 0 !important;
}

/* Final: fixed height calculated from 16:9 */
.uagb-post-grid.uagb-post__image-position-background.is-ratio-sized > .uagb-post__inner-wrap {
    height: var(--uagb-card-h) !important;
    min-height: var(--uagb-card-h) !important;
}


/* ==========================================
   LAYOUT SWITCHED BY THE SETTINGS PAGE
   (class uagb-slider-forced, see PHP)

   "Slider quer" / "Slider normal" switch the Spectra layout of a block on the
   website only. The colours Spectra generates from the stored block were
   chosen for the other layout, so the text colour is reset here:

   - switched to the background layout: white text with a soft shadow, on
     top of the featured image and Spectra's overlay
   - switched to image on top: text colour of the theme
   The read-more button keeps its colours.
   ========================================== */

.uagb-post-grid.uagb-slider-forced.uagb-post__image-position-background > .uagb-post__inner-wrap > .uagb-post__text:not(.uagb-post__cta),
.uagb-post-grid.uagb-slider-forced.uagb-post__image-position-background > .uagb-post__inner-wrap > .uagb-post__text:not(.uagb-post__cta) * {
    color: #fff !important;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

.uagb-post-grid.uagb-slider-forced.uagb-post__image-position-top > .uagb-post__inner-wrap,
.uagb-post-grid.uagb-slider-forced.uagb-post__image-position-top > .uagb-post__inner-wrap > .uagb-post__text:not(.uagb-post__cta),
.uagb-post-grid.uagb-slider-forced.uagb-post__image-position-top > .uagb-post__inner-wrap > .uagb-post__text:not(.uagb-post__cta) * {
    color: inherit !important;
    text-shadow: none !important;
}


/* ==========================================
   MASONRY (display "masonry", class uagb-slider-masonry from PHP)

   Spectra's CSS grid with its columns (desktop, tablet, mobile) stays. The
   JS reads Spectra's row gap, then switches to rows of 1 px (is-masonry-on)
   and gives every card "grid-row-end: span <height + gap>" in a fixed column
   (1, 2, 3, 1, 2, 3 …: reading order left to right, then down). "dense" lets
   each card close up under the one above it. Without JS the normal Spectra
   grid stays.
   ========================================== */

.uagb-post-grid.uagb-slider-masonry.is-masonry-on {
    display: grid !important;
    grid-auto-flow: row dense !important;
    grid-auto-rows: 1px !important;
    row-gap: 0 !important;
    align-items: start !important;
}

.uagb-post-grid.uagb-slider-masonry.is-masonry-on > .uagb-post__inner-wrap {
    height: auto !important;
    min-height: 0 !important;
    align-self: start !important;
    margin: 0 !important;
}

/* Images in their own aspect ratio: that is what makes the heights differ. */
.uagb-post-grid.uagb-slider-masonry.is-masonry-on:not(.uagb-post__image-position-background) .uagb-post__image img {
    display: block;
    width: 100%;
    height: auto;
}


/* ==========================================
   READ-MORE BUTTONS: SAME STAR ANIMATION
   AS ALL OTHER BUTTONS OF THE SITE

   The PHP plugin (filter "render_block") wraps the read-more link in a real
   button host:

     div.uagb-post__cta.wp-block-button     ← Spectra container
       └ div.wp-block-button                ← real button host
           └ a.wp-block-button__link        ← link

   The theme's star rules therefore apply to the inner host – where the
   geometry is right, because it is exactly as large as the link. Only the
   essentials remain here:
   ========================================== */

/*
 * The outer container gets no stars of its own any more, otherwise they would
 * appear twice. (:has() → without the PHP conversion the old behaviour stays
 * unchanged.)
 */
.uagb-post__cta.wp-block-button:has(> .wp-block-button)::before,
.uagb-post__cta.wp-block-button:has(> .wp-block-button)::after {
    content: none !important;
    display: none !important;
}

/* Spectra sets overflow:hidden – but the stars stick out */
.uagb-post-grid .uagb-post__inner-wrap > .uagb-post__cta {
    overflow: visible !important;
}

/*
 * In the background layout Spectra puts an invisible link area (z-index 11)
 * over the whole card. The button is lifted above it so it receives :hover.
 * The link points to the same target, the click behaviour does not change.
 */
.uagb-post-grid.uagb-post__image-position-background .uagb-post__inner-wrap > .uagb-post__cta {
    z-index: 12 !important;
}

/*
 * Variation per card (angle, distance, start rotation, time offset). The
 * variables are inherited by the inner button host.
 */
.uagb-post-grid > .uagb-post__inner-wrap:nth-child(4n+1) > .uagb-post__cta.wp-block-button {
    --va: 0deg;  --vd: 0em;     --vr: 0deg;   --vt: 0s;
}
.uagb-post-grid > .uagb-post__inner-wrap:nth-child(4n+2) > .uagb-post__cta.wp-block-button {
    --va: 8deg;  --vd: 0.12em;  --vr: 12deg;  --vt: -0.6s;
}
.uagb-post-grid > .uagb-post__inner-wrap:nth-child(4n+3) > .uagb-post__cta.wp-block-button {
    --va: -7deg; --vd: -0.05em; --vr: -10deg; --vt: -1.1s;
}
.uagb-post-grid > .uagb-post__inner-wrap:nth-child(4n+4) > .uagb-post__cta.wp-block-button {
    --va: 4deg;  --vd: 0.20em;  --vr: 6deg;   --vt: -1.7s;
}


/* ==========================================
   TABLET
   ========================================== */

@media (max-width: 900px) {

    .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {

        gap: 18px !important;

        /*
         * Slightly less edge space on tablets
         */
        padding-left: 30px !important;
        padding-right: 30px !important;

        scroll-padding-left: 30px;
        scroll-padding-right: 30px;
    }


    .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap {

        flex-basis: 310px !important;

        width: 310px !important;
        min-width: 310px !important;
        max-width: 310px !important;

    }

}


/* ==========================================
   MOBILE
   ========================================== */

@media (max-width: 600px) {

    .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) {

        gap: 16px !important;

        /*
         * Enough room for the shadow of the first/last card
         */
        padding-left: 20px !important;
        padding-right: 20px !important;

        /*
         * Hide the scrollbar
         */
        scrollbar-width: none !important;

        /*
         * The browser may tell horizontal and vertical finger movements apart
         */
        touch-action: pan-x pan-y;

        /*
         * Limit horizontal overscroll, allow vertical page scrolling
         */
        overscroll-behavior-x: contain;
        overscroll-behavior-y: auto;

        /*
         * Scroll padding
         */
        scroll-padding-left: 20px;
        scroll-padding-right: 20px;
    }


    /*
     * Chrome / Safari
     */
    .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry)::-webkit-scrollbar {
        display: none;
    }


    /* ======================================
       MOBILE CARDS
       ====================================== */

    .uagb-post-grid:not(.is-masonry, .uagb-slider-masonry) > .uagb-post__inner-wrap {

        /*
         * 82% of the screen width
         */
        flex-basis: 82vw !important;

        width: 82vw !important;
        min-width: 82vw !important;
        max-width: 82vw !important;

    }

}
