/**
 * Breakdance Loop Skeleton Loader
 * Animated placeholders for lazy-loaded content
 * 
 * VERSION: 3.1
 * UPDATED: 2026-04-28
 * 
 * KEY FEATURES:
 * - Absolute positioned skeleton overlay (doesn't affect height)
 * - Real content in flow but invisible (opacity + visibility + content-visibility)
 * - Works with JS v2.2.0 data-src image blocking for true lazy loading
 * - Zero layout shift on load for all loop types
 * 
 * BREAKING CHANGES FROM v2.x:
 * - ALL LOOPS use absolute positioned skeleton overlay (not just term)
 * - Real content ALWAYS in flow but invisible until loaded
 * - Guarantees correct height calculation from start for Swiper/layout
 * - Skeleton doesn't affect parent height - pure visual overlay
 * 
 * KEY PRINCIPLE: Real content dictates height, skeleton is just visual overlay
 */

/* Skeleton Container */
.bde-lazy-container {
    position: relative;
}

.bde-lazy-item {
    position: relative;
    height: auto; /* Fully automatic height */
    min-height: 0;
    display: flex;
    flex-direction: column;
    transition: opacity 0.3s ease;
}

/* Keep flex layout when loaded */
.bde-lazy-item.loaded {
    height: auto;
    min-height: 0;
}

/* Skeleton and content positioning for smooth crossfade */
.bde-skeleton-wrapper,
.bde-lazy-content {
    width: 100%;
}

/* UNIVERSAL FIX: Skeleton as absolute overlay, real content always in flow */
/* Real content takes space (for proper height) but invisible until loaded */
.bde-lazy-item:not(.loaded) .bde-lazy-content {
    opacity: 0 !important; /* Hidden but takes space */
    pointer-events: none;
    position: relative;
    z-index: 1;
    visibility: hidden; /* Prevents image loading in some browsers */
    content-visibility: hidden; /* Modern CSS - blocks rendering & resource loading */
}

/* CRITICAL: Block images from loading until intersection observer triggers */
.bde-lazy-item:not(.loaded) .bde-lazy-content img {
    visibility: hidden !important;
    content-visibility: hidden !important;
}

/* Skeleton overlays real content (doesn't affect height calculation) */
.bde-lazy-item:not(.loaded) .bde-skeleton-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0; /* Match parent height */
    z-index: 10;
    background: #ffffff;
    display: flex;
    align-items: flex-start; /* Top align content */
    padding: 1rem;
    box-sizing: border-box;
}

/* When loaded, remove skeleton and show content */
.bde-lazy-item.loaded .bde-skeleton-wrapper {
    display: none !important; /* IMMEDIATE removal */
}

.bde-lazy-item.loaded .bde-lazy-content {
    opacity: 1 !important;
    transition: opacity 0.3s ease;
}

/* Skeleton Base - White background with light gray blocks */
.bde-skeleton {
    background: #e0e0e0;
    border-radius: 5px;
    position: relative;
    overflow: hidden;
}

/* Skeleton Wrapper - White card background with automatic height */
/* Note: Positioned absolutely when not loaded (see above) */
.bde-skeleton-wrapper {
    background: #ffffff;
    border: 0px solid #e0e0e0;
    border-radius: 5px;
    width: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    /* Padding moved to :not(.loaded) state above for absolute positioning */
}

/* Shimmer Animation - Lighter shimmer effect */
.bde-skeleton::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.8) 50%,
        transparent 100%
    );
    animation: skeleton-shimmer 2s infinite;
}

@keyframes skeleton-shimmer {
    0% { left: -100%; }
    100% { left: 100%; }
}

/* Skeleton Components */
.bde-skeleton-image {
    width: 100%;
    aspect-ratio: 1/1; /* Match WooCommerce square thumbnails */
    margin-bottom: 1rem;
    flex-shrink: 0; /* Prevent image from shrinking */
    /* Reserve exact space to prevent CLS */
    min-height: 200px; /* Adjust based on your grid */
}

/* Product-specific skeleton with exact dimensions */
.bde-skeleton-product .bde-skeleton-image {
    aspect-ratio: 1/1;
    min-height: auto; /* Let aspect-ratio handle it */
}

.bde-skeleton-title {
    width: 80%;
    height: 1.5rem;
    margin-bottom: 0.75rem;
    flex-shrink: 0;
}

.bde-skeleton-price {
    width: 40%;
    height: 1.25rem;
    margin-bottom: 0.5rem;
    flex-shrink: 0;
}

.bde-skeleton-meta {
    width: 60%;
    height: 1rem;
    margin-bottom: 0.5rem;
    flex-shrink: 0;
}

.bde-skeleton-text {
    width: 100%;
    height: 0.875rem;
    margin-bottom: 0.5rem;
    flex-shrink: 0;
}

.bde-skeleton-text:nth-child(2) {
    width: 90%;
}

.bde-skeleton-text:last-child {
    width: 70%;
}

.bde-skeleton-button {
    width: 120px;
    height: 2.5rem;
    margin-top: auto; /* Push button to bottom */
    flex-shrink: 0;
}

/* Flexible spacer - grows to fill available space */
.bde-skeleton-spacer {
    flex-grow: 1;
    min-height: 1rem;
}

/* Grid Layout Support - Automatic height */
.products .bde-lazy-item,
.bde-posts-grid .bde-lazy-item {
    margin-bottom: 2rem;
    height: auto; /* Automatic height */
}

/* Ensure product grid items use flexbox but allow natural height */
.products .product,
.woocommerce ul.products li.product {
    height: auto;
    display: flex;
    flex-direction: column;
}

/* Slider Support - Automatic height in swiper */
.swiper-slide .bde-lazy-item {
    height: auto; /* Automatic height */
}

.swiper-slide {
    height: auto !important; /* Allow slide to grow with content */
}

/* Fade In Animation */
.bde-lazy-item.loaded .bde-lazy-content {
    animation: fadeInUp 0.4s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Loading Error State */
.bde-lazy-item.error .bde-skeleton {
    background: #ffebee;
    border: 1px dashed #ef5350;
}

.bde-lazy-item.error .bde-skeleton::after {
    display: none;
}

/* Dark Mode - Keep light skeleton even in dark mode */
@media (prefers-color-scheme: dark) {
    .bde-skeleton-wrapper {
        background: #ffffff;
        border-color: #e0e0e0;
    }
    
    .bde-skeleton {
        background: #e0e0e0;
    }

    .bde-skeleton::after {
        background: linear-gradient(
            90deg,
            transparent 0%,
            rgba(255, 255, 255, 0.8) 50%,
            transparent 100%
        );
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    .bde-skeleton::after {
        animation: none;
    }
    
    .bde-lazy-item.loaded .bde-lazy-content {
        animation: none;
    }
}

/* Product Loop Skeleton - Automatic height based on content */
.bde-skeleton-product {
    display: flex;
    flex-direction: column;
    /* NO min-height - fully automatic based on skeleton components */
}

/* Enhanced Product Skeleton v2.1 - Matches EXACT jbskin.lt product structure */

/* Link Wrapper - Mimics <a> tag (position: relative for absolute children) */
.bde-skeleton-link-wrapper {
    position: relative;
    display: block;
    width: 100%;
}

/* Badge Wrapper - Absolute positioned (top-left, like real .product-badges-wrapper) */
.bde-skeleton-badges-wrapper {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 998;
    width: 75%;
}

.bde-skeleton-badge {
    width: 50px;
    height: 24px;
    border-radius: 3px;
    margin-bottom: 6px;
    display: inline-block;
    margin-right: 6px;
}

/* Image Wrapper - Mimics .bde-woo-product-image */
.bde-skeleton-image-wrapper {
    position: relative;
    width: 100%;
    margin-bottom: 1rem;
}

/* Product Image - Aspect ratio from real images */
.bde-skeleton-image-wrapper .bde-skeleton-image {
    width: 100%;
    aspect-ratio: 522 / 701; /* Real image ratio from jbskin.lt */
    margin-bottom: 0;
}

/* Icons Wrapper - Absolute positioned (top-right, like real .product-icons-wrapper) */
.bde-skeleton-icons-wrapper {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 999;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.bde-skeleton-icon {
    width: 36px;
    height: 36px;
    border-radius: 50%; /* Circular icons */
}

/* Brand Name - .product-brand */
.bde-skeleton-brand {
    width: 60%;
    height: 0.875rem; /* 14px */
    margin-bottom: 0.5rem;
}

/* Product Title - h2.woocommerce-loop-product__title */
.bde-skeleton-title {
    width: 100%;
    height: 1.2rem; /* Line 1 */
    margin-bottom: 0.5rem;
}

/* Second line of title */
.bde-skeleton-title::after {
    content: '';
    display: block;
    width: 80%;
    height: 1.2rem;
    margin-top: 0.3rem;
    background: inherit;
    border-radius: inherit;
}

/* Price - span.price */
.bde-skeleton-price {
    width: 35%;
    height: 1.25rem;
    margin-top: 0.5rem;
    margin-bottom: 1rem;
}

/* Footer - Mimics .bde-woo-product-footer (empty but present) */
.bde-skeleton-footer {
    min-height: 0.5rem;
}

/* Responsive Adjustments for Enhanced Skeleton */
@media (max-width: 768px) {
    .bde-skeleton-badges-wrapper {
        top: 15px;
        left: 15px;
    }
    
    .bde-skeleton-badge {
        width: 45px;
        height: 22px;
    }
    
    .bde-skeleton-icons-wrapper {
        top: 8px;
        right: 8px;
        gap: 6px;
    }
    
    .bde-skeleton-icon {
        width: 32px;
        height: 32px;
    }
}

/* Post Loop Skeleton - Automatic height based on content */
.bde-skeleton-post {
    display: flex;
    flex-direction: column;
    /* NO min-height - fully automatic based on skeleton components */
}

/* Term Loop Skeleton */
.bde-skeleton-term {
    text-align: center;
}

.bde-skeleton-term .bde-skeleton-image {
    aspect-ratio: 1/1; /* Match real term images (square) */
}

/* Responsive Adjustments */
@media (max-width: 768px) {
    /* NO min-height restrictions - fully automatic on mobile */
    
    .bde-skeleton-wrapper {
        padding: 0.75rem; /* Just reduce padding on mobile */
    }
}

/* Ensure skeleton wrapper fills space in Breakdance elements */
.breakdance .bde-lazy-item,
[data-type="loop_builder"] .bde-lazy-item {
    height: auto; /* Automatic height */
}

/* Fix for WooCommerce product grids */
.woocommerce ul.products li.product .bde-lazy-item,
.woocommerce-page ul.products li.product .bde-lazy-item {
    height: auto; /* Automatic height */
    display: flex;
    flex-direction: column;
}

/* CRITICAL FIX: Swiper slider height normalization override */
/* Let Swiper manage heights, but prevent extreme stretching for term loops */
.bde-term-loop .swiper-slide.bde-lazy-item {
    /* Don't use !important - let Swiper update naturally */
    align-self: flex-start; /* Prevent flex stretch */
}

/* Term loop specific - skeleton should be centered */
.bde-term-loop .bde-lazy-item:not(.loaded) .bde-skeleton-wrapper {
    align-items: center; /* Center skeleton content */
    justify-content: center;
}

/* Ensure term loop images maintain aspect ratio in slider */
.bde-term-loop .swiper-slide img {
    width: 100%;
    height: auto;
    object-fit: contain;
}
