/**
 * WP Page Sparkz - Adaptive Layout Engine (v-layout)
 *
 * Self-healing layout rules that natively reflow surviving elements with
 * ZERO Cumulative Layout Shift (CLS) and ZERO DOM-rewriting lag when
 * conditional blocks are pruned server-side by wpps-if.
 *
 * @package    WP_Page_Sparkz
 * @subpackage WP_Page_Sparkz/public/css
 * @author     Sparkz Development <development@sparkz.systems>
 * @version    1.0.0
 */

/* ==========================================================================
   1. Multi-Column & Grid Rebalancing
   ========================================================================== */

/**
 * Auto-Rebalance Columns
 * Surviving columns dynamically expand to fill empty gaps when sibling columns are pruned.
 * Overrides hardcoded Gutenberg flex-basis percentages (e.g. 33.33%, 50%).
 */
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > *,
.wp-block-columns.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > .wp-block-column,
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > .wp-block-column,
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > [class*="column"],
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > [class*="col-"] {
    flex: 1 1 280px !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: 100% !important;
}

.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) {
    display: flex !important;
    flex-wrap: wrap !important;
}

/* When only a single column survives, expand it to full width */
.wp-block-columns.wpps-auto-rebalance:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column,
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column,
.wpps-auto-rebalance:not(.wpps-keep-fixed-widths) > .wp-block-column:only-child {
    flex: 1 1 100% !important;
    max-width: 100% !important;
    width: 100% !important;
}

/**
 * Responsive Card Grid Auto-Fit
 * Converts container into a dynamic CSS Grid that fluidly flows and wraps cards evenly.
 */
.wpps-grid-autofit:not(.wpps-keep-fixed-widths),
.wp-block-columns.wpps-grid-autofit:not(.wpps-keep-fixed-widths),
.wp-block-group.wpps-grid-autofit:not(.wpps-keep-fixed-widths) {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)) !important;
    gap: var(--wp--style--block-gap, 1.5rem) !important;
}

.wpps-grid-autofit:not(.wpps-keep-fixed-widths) > * {
    width: auto !important;
    max-width: 100% !important;
    margin: 0 !important;
}

/**
 * Equalize Heights
 * Stretches all surviving cards to matching equal height and anchors footer actions.
 */
.wpps-equalize-heights,
.wpps-equal-height {
    align-items: stretch !important;
}

.wpps-equalize-heights > *,
.wpps-equal-height > * {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
}

.wpps-equalize-heights > * > :is(.wpps-card-footer, .card-footer, footer, .wp-block-buttons):last-child,
.wpps-equal-height > * > :is(.wpps-card-footer, .card-footer, footer, .wp-block-buttons):last-child {
    margin-top: auto !important;
}

/* ==========================================================================
   2. Hero & Split Section Adaptations
   ========================================================================== */

/**
 * Center Copy if Media Missing (Defensive Editorial Centering)
 * When photo/video is absent, centers headline and copy in an elegant 780px editorial width.
 */
.wpps-center-editorial:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))):not(:has(img)):not(:has(video)):not(:has(figure)) :is(.wpps-hero-copy, .hero-copy),
.wpps-center-editorial:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))):not(:has(img)):not(:has(video)):not(:has(figure)) > .wp-block-column,
:is(.wpps-split-hero, .split-hero):not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))) :is(.wpps-hero-copy, .hero-copy),
.wp-block-columns.wpps-center-editorial:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column {
    margin-inline: auto !important;
    text-align: center !important;
    max-width: 780px !important;
    width: 100% !important;
    float: none !important;
}

.wpps-center-editorial:not(.wpps-keep-fixed-widths).split-container:has(> .split-content:only-child) .split-content,
:is(.wpps-split-hero, .split-hero):not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))) .split-content,
.wpps-center-editorial:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))):not(:has(img)) .split-content {
    flex: 1 1 100% !important;
    max-width: 780px !important;
    margin-inline: auto !important;
    text-align: center !important;
}

.wpps-center-editorial:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-media, .hero-media))):not(:has(img)):not(:has(video)):not(:has(figure)) .wp-block-buttons,
.wp-block-columns.wpps-center-editorial:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) .wp-block-buttons {
    justify-content: center !important;
}

/**
 * Expand Media if Copy Missing
 * When text copy is pruned, expands remaining media to 100% full width.
 */
.wpps-expand-media:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-copy, .hero-copy))):not(:has(p)):not(:has(h1)):not(:has(h2)):not(:has(h3)) :is(.wpps-hero-media, .hero-media),
.wp-block-columns.wpps-expand-media:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column {
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 100% !important;
}

.wpps-expand-media:not(.wpps-keep-fixed-widths):not(:has(:is(.wpps-hero-copy, .hero-copy))):not(:has(p)):not(:has(h1)):not(:has(h2)):not(:has(h3)) img,
.wp-block-columns.wpps-expand-media:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) img {
    width: 100% !important;
    height: auto !important;
    object-fit: cover !important;
}

/**
 * Full-Width if Sidebar Empty
 * Main content expands to full width when sidebar column is omitted.
 */
.wpps-sidebar-adapt:not(.wpps-keep-fixed-widths):not(:has(.wpps-sidebar)):not(:has(aside)) .wpps-main-content,
.wpps-expand-content:not(.wpps-keep-fixed-widths):not(:has(.wpps-sidebar)):not(:has(aside)) .wpps-main-content,
.wpps-main-sidebar-layout:not(.wpps-keep-fixed-widths):not(:has(.wpps-sidebar)) .wpps-main-content,
.wp-block-columns.wpps-sidebar-adapt:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column,
.wp-block-columns.wpps-expand-content:not(.wpps-keep-fixed-widths):has(> .wp-block-column:only-child) > .wp-block-column {
    grid-column: 1 / -1 !important;
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 100% !important;
}

/* ==========================================================================
   3. Spacing & Container Healing
   ========================================================================== */

/**
 * Fluid Responsive Padding
 * Replaces fixed section heights with smooth responsive clamp padding.
 */
.wpps-fluid-padding:not(:has(:is(.wpps-hero-media, .hero-media))):not(:has(img)):not(:has(video)):not(:has(figure)) {
    min-height: auto !important;
    padding-block: clamp(2rem, 5vw, 5rem) !important;
}

/**
 * Collapse Empty Spacers & Dividers
 * Collapses redundant spacers and dividers when adjacent content is empty or when
 * explicitly tagged with wpps-collapse-spacers, without hiding surviving content sections.
 */
.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)).wpps-collapse-spacers,
.wp-block-separator.wpps-collapse-spacers,
.wpps-collapse-spacers.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wpps-collapse-spacers.wp-block-separator,
.wpps-collapse-spacers:empty {
    display: none !important;
    height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
}

/* Redundant spacer preceding or following an empty section */
.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):has(+ .wpps-collapse-spacers:empty),
.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):has(+ .wpps-dissolve-parent:empty),
.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):has(+ .wpps-dissolve-empty:empty),
.wpps-collapse-spacers:empty + .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wpps-dissolve-parent:empty + .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wpps-dissolve-empty:empty + .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wpps-collapse-spacers.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)) + .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):has(+ .wpps-collapse-spacers.wp-block-spacer:not(:where(.wpps-no-collapse-spacers *))),
.wpps-collapse-spacers > .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)) + .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)) {
    display: none !important;
    height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
}

/* Redundant spacer inside a container that only contains a spacer */
.wpps-collapse-spacers:has(> .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):only-child) > .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)),
.wpps-collapse-spacers > .wp-block-spacer:not(:where(.wpps-no-collapse-spacers *)):only-child {
    display: none !important;
    height: 0 !important;
}

/**
 * Dissolve Container if Empty
 * Drops outer wrapper borders and shadows only when container is genuinely empty.
 * Preserves text content and replaced/void elements (img, svg, video, iframe, etc.).
 */
.wpps-dissolve-parent:empty,
.wpps-dissolve-empty:empty {
    display: none !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    box-shadow: none !important;
    background: transparent !important;
}

/* ==========================================================================
   4. Buttons & CTAs
   ========================================================================== */

/**
 * Center Single Button
 * Centers remaining CTA button when secondary action button is pruned.
 */
.wp-block-buttons.wpps-center-single:has(> .wp-block-button:only-of-type),
.wpps-center-single:has(> :only-child) {
    justify-content: center !important;
    text-align: center !important;
}

.wp-block-buttons.wpps-center-single:has(> .wp-block-button:only-of-type) > .wp-block-button {
    margin-inline: auto;
}

/* Generated native hero recipes retain their authored split until a side is absent. */
.wpps-hero:has(> .wpps-hero-copy:only-child),
.wpps-hero:has(> .wpps-hero-media:only-child) {
    display: flex !important;
    justify-content: center !important;
    grid-template-columns: minmax(0, 1fr) !important;
}
.wpps-hero > .wpps-hero-copy:only-child { width: 100% !important; max-width: 780px !important; }
.wpps-hero > .wpps-hero-media:only-child { width: 100% !important; max-width: 100% !important; }
