/*
 * Sensei Card Stack — scroll-driven card stack.
 * Cards are the direct child modules of .ds-card-stack__cards; no per-card wrapper, so a
 * looped child yields one card per loop instance. Every card has the same resting slot — the
 * single grid cell of the box, later cards above earlier ones in DOM order — so the box is exactly
 * as tall as the tallest card plus its padding; the pushed-up stack and the waiting queue overflow
 * it. Custom properties are scoped to the module wrapper, never :root.
 * Settings feed them through the style system (ModuleStylesTrait.php / module-styles.tsx):
 *   --ds-card-stack-offset  Top Offset (cards.advanced.offset), 10vh default
 *   --ds-card-stack-pinned  Pinned Stack (cards.advanced.pinnedStack): off (default) / on
 *   --ds-card-stack-offset-target  Offset Target (cards.advanced.offsetTarget): card (default) / stack
 *   --ds-card-stack-detect-menu  Detect Menu (cards.advanced.detectMenu): on (default) / off; the admin bar is always detected
 *   --ds-card-stack-scroll-speed  Scroll Speed (cards.advanced.scrollSpeed): animation px per scrolled px, or with Auto Scroll the glide speed (column too), 100% default
 *   --ds-card-stack-auto-scroll  Auto Scroll (cards.advanced.autoScroll): off (default) / on, one card per scroll gesture
 *   --ds-card-stack-align   Equalize Card Height (cards.advanced.equalHeight): stretch (default) / start
 *   .ds-card-stack--column  Column Layout (cards.advanced.column): cards in flow, sticky at the pin line
 *   Entry Animation: --ds-card-stack-first-stacked / -overlap / -entry-direction / -entry-size / -entry-spacing /
 *     -entry-tilt-angle / -entry-tilt-origin / -entry-scale / -entry-fade / -entry-blur / -entry-fade-length
 *   Exit Animation:  --ds-card-stack-exit-direction / -size / -step / -exit-length / -tilt-angle /
 *     -tilt-origin / -exit-scale / -exit-fade / -exit-blur / -exit-fade-length
 * public/js/CardStack.js (frontend + Visual Builder) drives:
 *   --ds-card-stack-header  viewport pixels covered by fixed/sticky bars (admin bar, and menus with Detect Menu)
 *   --ds-card-stack-inset   the box's top border + padding: Top Offset applies to the box edge,
 *                             the cards rest that much lower
 *   --ds-card-stack-room / -room-bottom  room above / below the resting slot for the full stack:
 *                             around the pinned card, or above it with Offset Target = stack, else 0
 *   --ds-card-stack-stick   how far below the pin line the column cards stick: the top room, 0 when pinned
 *   --ds-card-stick         per column card of a pinned stack: how far below the pin line it sticks (its place)
 *   --ds-card-room / -room-above  per column card of a pinned stack: bottom margin that ends every card's
 *                             margin box on the stack's end; the previous card's, taken off the top margin
 *   --ds-card-x/-y/-rotate/-scale/-opacity/-visibility/-z  per card: its pose in the entry queue
 *                             until it lands, then in the exit stack (depth = cards on top)
 *   --ds-card-stack-z       on the ancestors of every stack: earlier stacks above later ones, so a
 *                             stack's waiting cards hang over the rows that follow it
 * .ds-card-stack--active marks the stack that is animating, so its ancestors win the z-order.
 * While a deck animates the runtime holds the page still and consumes the scroll input itself.
 * Without JS (or with reduced motion) the cards render as a plain pile in the resting slot.
 */
.ds_card_stack {
    --ds-card-stack-offset: 10vh;
    --ds-card-stack-pinned: off;
    --ds-card-stack-offset-target: card;
    --ds-card-stack-detect-menu: on;
    --ds-card-stack-scroll-speed: 100%;
    --ds-card-stack-auto-scroll: off;
    --ds-card-stack-first-stacked: on;
    --ds-card-stack-overlap: on;
    --ds-card-stack-entry-direction: 180deg;
    --ds-card-stack-entry-size: 3;
    --ds-card-stack-entry-spacing: 5vh;
    --ds-card-stack-entry-tilt-angle: 0deg;
    --ds-card-stack-entry-tilt-origin: 50% 50%;
    --ds-card-stack-entry-scale: 2%;
    --ds-card-stack-entry-fade: on;
    --ds-card-stack-entry-fade-length: 100px;
    --ds-card-stack-entry-blur: 0px;
    --ds-card-stack-exit-direction: 0deg;
    --ds-card-stack-size: 3;
    --ds-card-stack-step: 3vh;
    --ds-card-stack-exit-length: 100px;
    --ds-card-stack-tilt-angle: 0deg;
    --ds-card-stack-tilt-origin: 50% 50%;
    --ds-card-stack-exit-scale: -5%;
    --ds-card-stack-exit-fade: on;
    --ds-card-stack-exit-fade-length: 100px;
    --ds-card-stack-exit-blur: 0px;
    position: relative;
}

/*
 * Hidden probes: the JS reads their resolved `top` / `left` to get the length settings in px
 * whatever unit was used (% of `top` is the box height, of `left` the box width).
 */
.ds_card_stack::before,
.ds_card_stack::after,
.ds-card-stack__cards::before,
.ds-card-stack__cards::after {
    content: "";
    position: absolute;
    left: 0;
    width: 0;
    height: 0;
    visibility: hidden;
    pointer-events: none;
}

/* Pin line. */
.ds_card_stack::before {
    top: calc(var(--ds-card-stack-header, 0px) + var(--ds-card-stack-offset));
}

/* Exit: card distance / fade length. */
.ds_card_stack::after {
    top: var(--ds-card-stack-step);
    left: var(--ds-card-stack-exit-fade-length);
}

/* Entry: card spacing / fade length. */
.ds-card-stack__cards::before {
    top: var(--ds-card-stack-entry-spacing);
    left: var(--ds-card-stack-entry-fade-length);
}

/* Exit: animation length. */
.ds-card-stack__cards::after {
    top: var(--ds-card-stack-exit-length);
}

/*
 * The box. module.json routes background, border, box shadow and spacing here, so the visible
 * box is what stays pinned. Rows 1 and 3 are the room for the stack beyond the resting slot (0 unless
 * pinned or Offset Target is the stack), row 2 holds every card (same cell, tallest wins).
 */
.ds-card-stack__cards {
    position: relative;
    /* Above the page content the flying cards cross; fixed headers sit far higher. */
    z-index: 10;
    display: grid;
    grid-template-rows: var(--ds-card-stack-room, 0px) auto var(--ds-card-stack-room-bottom, 0px);
    align-items: var(--ds-card-stack-align, stretch);
}

/* Individual transform properties, so a Transform set on the card module itself still composes. */
.ds-card-stack__cards > .et_pb_module {
    grid-row: 2;
    grid-column: 1;
    position: relative;
    z-index: var(--ds-card-z, auto);
    translate: var(--ds-card-x, 0px) var(--ds-card-y, 0px);
    rotate: var(--ds-card-rotate, 0deg);
    scale: var(--ds-card-scale, 1);
    opacity: var(--ds-card-opacity, 1);
    visibility: var(--ds-card-visibility, visible);
}

/*
 * Column layout: the cards sit in flow with Card Spacing between them, so the module is as tall
 * as all of them, and each card sticks at the pin line while the next one scrolls in; the
 * pushed-up stack rises above the pin line — or, with the offset applied to the top of the stack, into
 * the room the first card leaves above itself. The box is the sticky containing block, so the whole stack
 * scrolls away once the last card has landed.
 */
.ds-card-stack--column .ds-card-stack__cards {
    display: flex;
    flex-direction: column;
    align-items: stretch;
}

.ds-card-stack--column .ds-card-stack__cards > .et_pb_module {
    position: sticky;
    top: calc(var(--ds-card-stack-header, 0px) + var(--ds-card-stack-offset) + var(--ds-card-stack-inset, 0px) + var(--ds-card-stack-stick, 0px) + var(--ds-card-stick, 0px));
}

.ds-card-stack--column .ds-card-stack__cards > .et_pb_module:first-child {
    margin-top: var(--ds-card-stack-room, 0px);
}

/*
 * Card Spacing is the whole gap: the card's own bottom margin is replaced by the pinned stack's
 * per-card room (all margin boxes end on one line, so the cards let go of the pin line together),
 * which the next card's top margin takes back out of the flow.
 */
.ds-card-stack--column .ds-card-stack__cards > .et_pb_module {
    margin-bottom: var(--ds-card-room, 0px);
}

.ds-card-stack--column .ds-card-stack__cards > .et_pb_module + .et_pb_module {
    margin-top: calc(var(--ds-card-stack-entry-spacing) - var(--ds-card-room-above, 0px));
}

/*
 * The box and the cards waiting in line below it float over the rows and sections that follow.
 * Divi gives every column its own stacking context (z-index: 2), so the stack's ancestors are
 * lifted above their later siblings — and above the ancestors of later stacks, whose own waiting
 * cards start further down (JS writes the descending --ds-card-stack-z; 10 without it).
 */
.et_pb_section:has(.ds_card_stack),
.et_pb_row:has(.ds_card_stack),
.et_pb_row_inner:has(.ds_card_stack),
.et_pb_column:has(.ds_card_stack),
.et_pb_column_inner:has(.ds_card_stack) {
    position: relative;
    z-index: var(--ds-card-stack-z, 10);
}

/*
 * A scaled-up card can be wider than the page; clipped at the section edge it can not widen the
 * page into a horizontal scroll. `clip` (not hidden) keeps the section out of scroll-container
 * duty, so the column layout's sticky cards and the vertical overhang of the waiting cards stay.
 */
.et_pb_section:has(.ds_card_stack) {
    overflow-x: clip;
}

/*
 * Not around the column layout: WebKit (Safari, every iOS browser) only moves a sticky box with the
 * scroll itself — on the scrolling thread — while no ancestor clips its overflow. Under any clipping
 * ancestor, `clip` included, the stuck cards are placed by the main thread a frame or two behind the
 * finger and shake by a few px with every change of speed. The page is clipped sideways at the
 * viewport instead (the body rule below), which is no ancestor in that sense.
 */
.et_pb_section:has(.ds-card-stack--column) {
    overflow-x: visible;
}

/*
 * Divi's animation script sets `overflow-y: hidden` inline on #page-container (and `overflow-x: hidden`
 * on body) as soon as any module on the page has an entrance animation, and never takes it back. That
 * turns #page-container into a scroll container that never scrolls, so the column cards stop sticking.
 * Divi's own stylesheet lays the same trap on #et-boc below 980px (`overflow-x: hidden`, which makes
 * the other axis compute to `auto`) and on #et-main-area, so the cards stop sticking on phones and in
 * the builder's small previews while desktop looks fine — all three get the same treatment.
 * `visible`, not `clip`: a clipping ancestor of any kind takes the sticky cards off WebKit's scrolling
 * thread (see the section rule above). Divi's point — no sideways scrollbar from slide-in animations —
 * is kept by the body rule below, which clips at the viewport. !important because the overflow is an
 * inline style.
 *
 * Never in the Visual Builder's own window (`et-vb-root-ancestor` on its body; the preview frame is
 * `et-vb-app-ancestor` and still gets this): there the builder locks #page-container with
 * `overflow: hidden` while a settings modal is open, and an author !important would outrank that
 * inline lock and leave the modal unscrollable. Layouts whose stacks render in that window would
 * otherwise match it.
 */
body:not(.et-vb-root-ancestor):has(.ds-card-stack--column) #page-container,
body:not(.et-vb-root-ancestor):has(.ds-card-stack--column) #et-boc,
body:not(.et-vb-root-ancestor):has(.ds-card-stack--column) #et-main-area {
    overflow-x: visible !important;
    overflow-y: visible !important;
}

/*
 * Same trap one level up in the Visual Builder: at tablet width and below Divi's builder CSS puts
 * `overflow-x: hidden` on <html>, and the theme script's inline `overflow-x: hidden` on <body> then
 * no longer propagates to the viewport — body becomes a scroll container that never scrolls and
 * the column cards stop sticking in the tablet, phone wide and phone previews. `clip` keeps the
 * sideways clipping without the scroll container, in the builder and on the frontend alike. On the
 * frontend it propagates to the viewport, so the body is no clipping ancestor of the sticky cards and
 * this is where the whole page — the wrappers above included — is clipped sideways.
 *
 * Only the horizontal axis: `clip` on one axis makes the other compute to `clip` as well, so this
 * clips without a scroll container on its own. Forcing `overflow-y` here would outrank the inline
 * `overflow: hidden` the builder locks the page with while a settings panel is open (author
 * !important beats inline), and the panel's own list would stop scrolling.
 */
body:not(.et-vb-root-ancestor):has(.ds-card-stack--column) {
    overflow-x: clip !important;
    /*
     * iOS does not hold a finger to that clip: the waiting cards, scaled up past the page edge (their
     * section must not clip, see above), make the document a few px wider than the screen, and every
     * iOS browser then pans it sideways under a touch — most visibly during pull-to-refresh. With no
     * clipping ancestor allowed, the pan itself is ruled out instead. Scroll containers inside the page
     * (carousels, tables) keep their own sideways pan: touch-action stops at the nearest one.
     */
    touch-action: pan-y pinch-zoom;
}

/* The stack that is animating right now goes above every other stack's ancestors. */
.et_pb_section:has(.ds-card-stack--active),
.et_pb_row:has(.ds-card-stack--active),
.et_pb_row_inner:has(.ds-card-stack--active),
.et_pb_column:has(.ds-card-stack--active),
.et_pb_column_inner:has(.ds-card-stack--active) {
    z-index: 100;
}

@media (prefers-reduced-motion: reduce) {
    .ds-card-stack__cards > .et_pb_module {
        z-index: auto;
        visibility: visible;
        opacity: 1;
        translate: 0 0;
        rotate: 0deg;
        scale: 1;
    }
}

