/**
 * INFINITY E-commerce - Performance Optimization Styles
 * Lazy loading, skeletons, and image optimization
 *
 * @package Infinity_Ecommerce
 * @version 3.3.0
 */

/* ==========================================================================
   LAZY LOADING STATES
   ========================================================================== */

/* Loading state */
img.lazy-loading,
.lazy-loading img {
    opacity: 0;
    filter: blur(10px);
    transform: scale(1.02);
    transition: opacity 0.4s ease, filter 0.4s ease, transform 0.4s ease;
}

/* Loaded state */
img.lazy-loaded,
.lazy-loaded img {
    opacity: 1;
    filter: blur(0);
    transform: scale(1);
}

/* Error state */
img.lazy-error {
    opacity: 0.5;
    filter: grayscale(1);
}

/* Background lazy loading */
[data-bg].lazy-loading,
[data-background].lazy-loading {
    background-color: var(--infinity-light, #F1F5F9);
}

[data-bg].lazy-loaded,
[data-background].lazy-loaded {
    background-color: transparent;
}

/* ==========================================================================
   IMAGE PLACEHOLDER
   ========================================================================== */

.image-placeholder {
    position: relative;
    background: linear-gradient(135deg, #F1F5F9 0%, #E2E8F0 100%);
    overflow: hidden;
}

.image-placeholder::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.4) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { left: -100%; }
    100% { left: 100%; }
}

/* ==========================================================================
   SKELETON LOADERS
   ========================================================================== */

/* Base skeleton styles */
.skeleton {
    background: linear-gradient(135deg, #F1F5F9 0%, #E2E8F0 100%);
    border-radius: var(--radius-md, 8px);
    position: relative;
    overflow: hidden;
}

.skeleton::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.5) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
}

/* Pulse animation alternative */
.skeleton-pulse {
    animation: skeleton-pulse 1.5s ease-in-out infinite;
}

@keyframes skeleton-pulse {
    0%, 100% {
        opacity: 1;
        background-color: #F1F5F9;
    }
    50% {
        opacity: 0.6;
        background-color: #E2E8F0;
    }
}

/* ==========================================================================
   SKELETON COMPONENTS
   ========================================================================== */

/* Skeleton grid container */
.skeleton-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: var(--spacing-lg, 1.5rem);
}

/* Product card skeleton */
.skeleton-card {
    background: white;
    border-radius: var(--radius-xl, 16px);
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.skeleton-image {
    aspect-ratio: 1;
    background: linear-gradient(135deg, #F1F5F9 0%, #E2E8F0 100%);
    position: relative;
    overflow: hidden;
}

.skeleton-image::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.5) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
}

.skeleton-content {
    padding: var(--spacing-lg, 1.5rem);
}

.skeleton-line {
    height: 16px;
    background: #F1F5F9;
    border-radius: 4px;
    margin-bottom: 12px;
}

.skeleton-line:last-child {
    margin-bottom: 0;
}

/* Different line widths */
.skeleton-line.short { width: 40%; }
.skeleton-line.medium { width: 60%; }
.skeleton-line.long { width: 80%; }
.skeleton-line.full { width: 100%; }

/* Skeleton text block */
.skeleton-text {
    height: 14px;
    background: #F1F5F9;
    border-radius: 4px;
}

/* Skeleton avatar */
.skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #F1F5F9;
}

/* Skeleton button */
.skeleton-button {
    height: 44px;
    background: #F1F5F9;
    border-radius: var(--radius-md, 8px);
}

/* ==========================================================================
   PRODUCT SPECIFIC SKELETONS
   ========================================================================== */

/* Product gallery skeleton */
.skeleton-gallery {
    display: grid;
    grid-template-columns: 80px 1fr;
    gap: var(--spacing-md, 1rem);
}

.skeleton-thumbnails {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm, 0.5rem);
}

.skeleton-thumbnail {
    width: 80px;
    height: 80px;
    background: #F1F5F9;
    border-radius: var(--radius-md, 8px);
}

.skeleton-main-image {
    aspect-ratio: 1;
    background: #F1F5F9;
    border-radius: var(--radius-lg, 12px);
}

/* Product info skeleton */
.skeleton-product-info {
    padding: var(--spacing-xl, 2rem);
}

.skeleton-title {
    height: 32px;
    background: #F1F5F9;
    border-radius: 6px;
    margin-bottom: var(--spacing-md, 1rem);
    width: 70%;
}

.skeleton-price {
    height: 28px;
    background: #F1F5F9;
    border-radius: 6px;
    width: 30%;
    margin-bottom: var(--spacing-lg, 1.5rem);
}

.skeleton-description {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: var(--spacing-xl, 2rem);
}

.skeleton-description .skeleton-line {
    height: 14px;
}

/* ==========================================================================
   LOADING OVERLAY
   ========================================================================== */

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--infinity-z-overlay, 1100);
    opacity: 1;
    transition: opacity 0.3s ease;
}

.loading-overlay.fade-out {
    opacity: 0;
    pointer-events: none;
}

.loading-spinner {
    width: 48px;
    height: 48px;
    border: 3px solid var(--infinity-light, #F1F5F9);
    border-top-color: var(--infinity-primary, #6366F1);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Dots loading animation */
.loading-dots {
    display: flex;
    gap: 6px;
}

.loading-dots span {
    width: 10px;
    height: 10px;
    background: var(--infinity-primary, #6366F1);
    border-radius: 50%;
    animation: bounce 1.4s ease-in-out infinite both;
}

.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }
.loading-dots span:nth-child(3) { animation-delay: 0s; }

@keyframes bounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1);
    }
}

/* ==========================================================================
   PROGRESSIVE IMAGE LOADING
   ========================================================================== */

.progressive-image {
    position: relative;
    overflow: hidden;
}

.progressive-image__placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    filter: blur(20px);
    transform: scale(1.1);
    transition: opacity 0.3s ease;
}

.progressive-image__full {
    position: relative;
    z-index: 1;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.progressive-image.loaded .progressive-image__placeholder {
    opacity: 0;
}

.progressive-image.loaded .progressive-image__full {
    opacity: 1;
}

/* ==========================================================================
   CONTENT FADE IN
   ========================================================================== */

.content-fade-in {
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.5s ease, transform 0.5s ease;
}

.content-fade-in.visible {
    opacity: 1;
    transform: translateY(0);
}

/* Stagger children */
.stagger-children > * {
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.stagger-children.visible > *:nth-child(1) { transition-delay: 0.1s; }
.stagger-children.visible > *:nth-child(2) { transition-delay: 0.2s; }
.stagger-children.visible > *:nth-child(3) { transition-delay: 0.3s; }
.stagger-children.visible > *:nth-child(4) { transition-delay: 0.4s; }
.stagger-children.visible > *:nth-child(5) { transition-delay: 0.5s; }
.stagger-children.visible > *:nth-child(6) { transition-delay: 0.6s; }

.stagger-children.visible > * {
    opacity: 1;
    transform: translateY(0);
}

/* ==========================================================================
   CLS PREVENTION - LAYOUT SHIFT FIXES
   ========================================================================== */

/* Reserve space for images to prevent CLS */
img {
    max-width: 100%;
    height: auto;
    /* Ensure images don't collapse */
    min-height: 1px;
}

/* Product image wrapper with aspect ratio */
.product-card__media,
.product-image-wrapper,
.woocommerce ul.products li.product .product-image-wrapper {
    aspect-ratio: 4 / 5;
    background-color: #f8fafc;
    overflow: hidden;
    position: relative;
}

/* Product images — general CLS sizing */
.woocommerce ul.products li.product img,
.woocommerce-product-gallery__image img {
    aspect-ratio: 4 / 5;
    object-fit: cover;
    width: 100%;
    height: auto;
}

/* Product card images — fill aspect-ratio container
   Must beat: .woocommerce ul.products li.product img (0-3-3)
   and WC default: .woocommerce ul.products li.product a img (0-3-4) */
.product-card__media img,
.woocommerce ul.products li.product .product-card__media img {
    aspect-ratio: 4 / 5;
    object-fit: cover;
    width: 100%;
    height: 100%;
}

/* Font loading - prevent FOIT/FOUT layout shift */
.fonts-loading body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Reserve space for font rendering */
h1, h2, h3, h4, h5, h6 {
    font-synthesis: none;
    -webkit-font-smoothing: antialiased;
}

/* Header fixed height to prevent shift */
.site-header {
    min-height: 70px;
}

@media (max-width: 991px) {
    .site-header {
        min-height: 60px;
    }
}

/* Promo bar fixed height */
.promo-bar {
    min-height: 36px;
}

/* Navigation placeholder */
.main-navigation {
    min-height: 48px;
}

/* Search overlay - prevent body scroll shift */
.ajax-search-overlay.active ~ .site-content {
    overflow: hidden;
}

/* Cart/Wishlist count badges - fixed size */
.cart-count,
.wishlist-count {
    min-width: 18px;
    min-height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Button fixed dimensions */
.btn,
.button,
.woocommerce a.button,
.woocommerce button.button {
    min-height: 44px;
    min-width: 44px;
}

/* Category card dimensions */
.category-card {
    min-height: 200px;
}

.category-card__image {
    aspect-ratio: 16 / 9;
    background-color: #f8fafc;
}

/* Gallery thumbnails */
.woocommerce-product-gallery__trigger,
.woocommerce-product-gallery .flex-viewport {
    aspect-ratio: 1 / 1;
}

/* Sidebar widgets */
.widget {
    min-height: 100px;
}

/* Footer sections */
.site-footer .widget {
    min-height: 80px;
}

/* ==========================================================================
   CONTAIN PROPERTY FOR PERFORMANCE
   ========================================================================== */

/* Use CSS containment for performance */
.category-card,
.widget {
    contain: layout style;
}

/* Strict containment for modals */
.ajax-search-overlay,
.cart-sidebar,
.wishlist-sidebar,
.mobile-navigation {
    contain: strict;
}

/* ==========================================================================
   OPTIMIZED IMAGES
   ========================================================================== */

/* Prevent layout shift with intrinsic sizing */
img[width][height] {
    height: auto;
    max-width: 100%;
}

/* Product card images: override height: auto to fill media container
   Specificity 0-4-3 beats both performance.css (0-3-3) and WC default (0-3-4) */
.product-card__media img[width][height],
.woocommerce ul.products li.product .product-card__media img {
    height: 100%;
    object-fit: cover;
}

/* Lazy loaded images placeholder */
img[loading="lazy"] {
    background-color: #f8fafc;
}

/* WebP picture element */
picture {
    display: block;
}

picture img {
    width: 100%;
    height: auto;
}

/* ==========================================================================
   REDUCED MOTION SUPPORT
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    img.lazy-loading {
        transition: opacity 0.1s ease;
        filter: none;
        transform: none;
    }

    .skeleton::after,
    .skeleton-image::after,
    .image-placeholder::before {
        animation: none;
    }

    .skeleton-pulse {
        animation: none;
    }

    .loading-spinner {
        animation-duration: 1.5s;
    }

    .content-fade-in,
    .stagger-children > * {
        transition: opacity 0.1s ease;
        transform: none;
    }
}

/* ==========================================================================
   PRINT STYLES
   ========================================================================== */

@media print {
    .skeleton,
    .skeleton-card,
    .loading-overlay {
        display: none !important;
    }

    img.lazy-loading {
        opacity: 1 !important;
        filter: none !important;
    }
}
