/**
 * Product Grid/Carousel Widget - Optimized Styles
 *
 * Performance optimizations:
 * - Low specificity (max 2-3 levels)
 * - CSS custom properties for theming
 * - GPU-accelerated animations
 * - Paint containment
 * - Reduced motion support
 *
 * @package Storedash_Checkout
 * @version 2.8.5
 * @updated 2025-10-13
 */

/* ============================================
   CSS Custom Properties
   ============================================ */

:root {
    --pg-primary: #E65500;
    --pg-card-bg: #ffffff;
    --pg-border: #e5e7eb;
    --pg-text: #1f2937;
    --pg-text-light: #6b7280;
    --pg-radius: 12px;
    --pg-spacing: 16px;
    --pg-transition: 0.2s ease;
}

/* ============================================
   Grid Layout - Low Specificity
   ============================================ */

.storedash-products-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
}

/* ============================================
   Product Card - Performance Optimized
   ============================================ */

.product-card {
    position: relative;
    background: var(--pg-card-bg);
    border: 1px solid var(--pg-border);
    border-radius: var(--pg-radius);
    padding: var(--pg-spacing);
    display: flex;
    flex-direction: column;

    /* Paint containment for better performance */
    contain: layout style;
}

/* Optional hover effect - only when enabled */
.has-card-hover .product-card {
    transition: transform var(--pg-transition), box-shadow var(--pg-transition);
    will-change: auto; /* Don't pre-allocate until hover */
}

.has-card-hover .product-card:hover {
    transform: translateY(-4px); /* GPU accelerated */
    box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
    will-change: transform; /* Hint GPU on hover */
}

/* ============================================
   Product Image
   ============================================ */

.product-image {
    position: relative;
    overflow: hidden;
    margin-bottom: var(--pg-spacing);
    border-radius: 8px;
    width: 100%; /* Ensure container takes full width */
}

/* ============================================
   Product Badges - Positioned relative to card (ignores card padding)
   ============================================ */

.product-badge {
    position: absolute !important;
    z-index: 10;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    display: inline-block;
    margin: 0; /* No margin by default - Elementor controls will override if user sets values */
}

/* Badge Position Classes - Stick to absolute corner of card */
.product-badge.badge-position-top-left {
    top: 0 !important;
    left: 0 !important;
    right: auto !important;
    bottom: auto !important;
}

.product-badge.badge-position-top-right {
    top: 0 !important;
    right: 0 !important;
    left: auto !important;
    bottom: auto !important;
}

.product-image img {
    width: 100%;
    max-width: 100%;
    display: block;
    /* Height and object-fit controlled by Elementor controls */
    /* aspect-ratio from Elementor will calculate height when width is set */
}

/* Optional image zoom - only when enabled */
.has-image-zoom .product-image img {
    transition: transform 0.3s ease;
}

.has-image-zoom .product-card:hover .product-image img {
    transform: scale(1.05); /* GPU accelerated */
}

/* ============================================
   Product Badges - New Badge System
   ============================================ */

.product-badge {
    position: absolute !important;
    z-index: 10;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    display: inline-block;
}

/* Badge Position Classes - Stick to absolute corner */
.product-badge.badge-position-top-left {
    top: 0 !important;
    left: 0 !important;
    right: auto !important;
    bottom: auto !important;
}

.product-badge.badge-position-top-right {
    top: 0 !important;
    right: 0 !important;
    left: auto !important;
    bottom: auto !important;
}

/* Badge Type Specific Styles (overridden by Elementor controls) */
.product-badge.badge-sale {
    /* Styles controlled by Elementor */
}

.product-badge.badge-new {
    /* Styles controlled by Elementor */
}

.product-badge.badge-out-of-stock {
    /* Styles controlled by Elementor */
}

/* Legacy sale-badge class - kept for backwards compatibility */
.sale-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    background: #ef4444;
    color: #ffffff;
    padding: 4px 12px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 600;
    z-index: 2;
}

/* ============================================
   Product Info
   ============================================ */

.product-info {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.product-title {
    margin: 0 0 8px;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.4;
}

.product-title a {
    color: var(--pg-text);
    text-decoration: none;
    transition: color var(--pg-transition);
}

.product-title a:hover {
    color: var(--pg-primary);
}

.product-sku {
    font-size: 13px;
    color: var(--pg-text-light);
    margin-bottom: 8px;
}

.product-rating {
    margin-bottom: 8px;
}

.product-excerpt {
    font-size: 14px;
    color: var(--pg-text-light);
    line-height: 1.5;
    margin-bottom: auto; /* Push subsequent items to bottom */
}

.product-price {
    font-size: 18px;
    font-weight: 700;
    color: var(--pg-primary);
    margin-bottom: 12px;
    margin-top: auto; /* Price always at bottom of .product-info */
}

.product-price del {
    color: #9ca3af;
    font-weight: 400;
    font-size: 16px;
    margin-right: 8px;
}

/* ============================================
   Add to Cart Button
   ============================================ */

.product-actions {
    margin-top: 0; /* Button directly after price */
}

.product-add-to-cart {
    width: 100%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    background: var(--pg-primary);
    color: #ffffff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--pg-transition);
}

.product-add-to-cart svg {
    flex-shrink: 0;
}

.product-add-to-cart:hover {
    background: #cc4d00;
    transform: translateY(-2px);
    box-shadow: none;
}

.product-add-to-cart:active {
    transform: translateY(0);
}

.product-add-to-cart.loading {
    opacity: 0.7;
    pointer-events: none;
}

.product-add-to-cart.added {
    background: #22c55e;
}

.product-add-to-cart:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.product-add-to-cart.out-of-stock {
    background: #f3f4f6;
    color: #6b7280;
    border: 1px solid #e5e7eb;
    opacity: 1;
    cursor: not-allowed;
}

.product-add-to-cart.out-of-stock svg {
    color: #9ca3af;
}

/* ============================================
   Cart Icon - Flexible Positioning
   ============================================ */

/* Price + Cart Icon Wrapper - Inline Layout (Price left, Icon right) */
.product-price-cart-wrapper.layout-inline {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-top: auto; /* Stick to bottom of card */
    margin-bottom: 0;
}

/* When using inline layout, the price inside doesn't need margin-top auto */
.product-price-cart-wrapper.layout-inline .product-price {
    margin-top: 0;
    margin-bottom: 0;
}

/* Cart Icon Base Styles */
.product-cart-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
    background: var(--pg-primary);
    color: #ffffff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all var(--pg-transition);
    text-decoration: none;
    flex-shrink: 0;
}

.product-cart-icon svg {
    width: 20px;
    height: 20px;
    display: block; /* Remove any inline spacing */
}

/* Ensure SVG paths inherit button/link color */
.product-cart-icon svg path {
    fill: currentColor;
    transition: fill var(--pg-transition);
}

.product-cart-icon:hover {
    background: #cc4d00;
    transform: translateY(-2px);
}

.product-cart-icon:active {
    transform: translateY(0);
}

.product-cart-icon.loading {
    opacity: 0.7;
    pointer-events: none;
}

.product-cart-icon.added {
    background: #22c55e;
}

.product-cart-icon:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Centered layout for below price */
.product-cart-icon-centered {
    display: flex;
    justify-content: center;
    margin-top: 8px;
}

/* ============================================
   Section Heading
   ============================================ */

.storedash-widget-header {
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.storedash-section-heading {
    margin: 0;
    padding: 0;
    font-size: 24px;
    font-weight: 700;
    line-height: 1.2;
    color: var(--pg-text);
    flex: 1;
}

/* Heading link styling */
.storedash-heading-link {
    color: inherit;
    text-decoration: none;
    transition: color var(--pg-transition), opacity var(--pg-transition);
    display: inline-block;
}

.storedash-heading-link:hover {
    /* Color set via Elementor control (default: #E65500) */
    opacity: 0.9;
}

.storedash-heading-link:active {
    opacity: 0.8;
}

/* Header controls for integrated navigation */
.storedash-header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.storedash-header-controls .swiper-button-prev,
.storedash-header-controls .swiper-button-next {
    position: static;
    margin: 0;
    width: 44px;
    height: 44px;
    background: #ffffff;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

.storedash-header-controls .swiper-button-prev:after,
.storedash-header-controls .swiper-button-next:after {
    font-size: 18px;
    color: var(--pg-text);
    font-weight: 700;
}

.storedash-header-controls .swiper-button-prev:hover,
.storedash-header-controls .swiper-button-next:hover {
    background: var(--pg-primary);
    transform: scale(1.1);
}

.storedash-header-controls .swiper-button-prev:hover:after,
.storedash-header-controls .swiper-button-next:hover:after {
    color: #ffffff;
}

/* When heading is standalone (not integrated) */
.storedash-widget-header:not(:has(.storedash-header-controls)) {
    display: block;
}

/* ============================================
   Carousel Mode
   ============================================ */

.storedash-products-carousel {
    position: relative;
    display: flex;
    flex-direction: column;
}

/* Prevent stretched slides before Swiper initializes */
.storedash-products-carousel .swiper-wrapper {
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
}

.storedash-products-carousel .swiper-slide {
    flex-shrink: 0;
    /* Width calculation uses CSS custom properties set by PHP for each breakpoint */
    /* This prevents FOUC by showing correct slide count before JS initializes */
    width: calc((100% - (var(--gap-size, 20px) * (var(--slides-per-view, 4) - 1))) / var(--slides-per-view, 4));
    min-width: 200px;
}

/* NOTE: Responsive slide widths are now handled by scoped CSS variables */
/* generated per-widget in ProductGridRenderer::render_responsive_carousel_styles() */
/* This ensures correct slide count at all breakpoints without FOUC */

/* When Swiper is initialized, it controls everything */
.storedash-products-carousel.swiper-initialized .swiper-slide {
    width: auto; /* Let Swiper control width completely */
    min-width: 0;
}

/* Add space for arrows when positioned outside */
.nav-top-left,
.nav-top-right {
    padding-top: 70px;
}

.nav-bottom-left,
.nav-bottom-right {
    padding-bottom: 70px;
}

.nav-outside {
    padding-left: 70px;
    padding-right: 70px;
}

.storedash-products-carousel .swiper-slide {
    height: auto;
}

.storedash-products-carousel .product-card {
    height: 100%;
}

/* Swiper Navigation */
.storedash-products-carousel .swiper-button-prev,
.storedash-products-carousel .swiper-button-next {
    width: 44px;
    height: 44px;
    background: #ffffff;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

.storedash-products-carousel .swiper-button-prev:after,
.storedash-products-carousel .swiper-button-next:after {
    font-size: 18px;
    color: var(--pg-text);
    font-weight: 700;
}

.storedash-products-carousel .swiper-button-prev:hover,
.storedash-products-carousel .swiper-button-next:hover {
    background: var(--pg-primary);
    transform: scale(1.1);
}

.storedash-products-carousel .swiper-button-prev:hover:after,
.storedash-products-carousel .swiper-button-next:hover:after {
    color: #ffffff;
}

/* Navigation Positioning - Outside */
.nav-outside .swiper-button-prev {
    left: -60px;
}

.nav-outside .swiper-button-next {
    right: -60px;
}

/* Navigation Positioning - Top Left */
.nav-top-left .swiper-button-prev,
.nav-top-left .swiper-button-next {
    top: 0;
    left: 0;
    margin-top: 0;
}

.nav-top-left .swiper-button-next {
    left: 60px;
}

/* Navigation Positioning - Top Right */
.nav-top-right .swiper-button-prev,
.nav-top-right .swiper-button-next {
    top: 0;
    right: 0;
    left: auto;
    margin-top: 0;
}

.nav-top-right .swiper-button-prev {
    right: 60px;
}

/* Navigation Positioning - Bottom Left */
.nav-bottom-left .swiper-button-prev,
.nav-bottom-left .swiper-button-next {
    top: auto;
    bottom: 0;
    left: 0;
    margin-top: 0;
}

.nav-bottom-left .swiper-button-next {
    left: 60px;
}

/* Navigation Positioning - Bottom Right */
.nav-bottom-right .swiper-button-prev,
.nav-bottom-right .swiper-button-next {
    top: auto;
    bottom: 0;
    right: 0;
    left: auto;
    margin-top: 0;
}

.nav-bottom-right .swiper-button-prev {
    right: 60px;
}

/* Swiper Pagination */
.storedash-products-carousel .swiper-pagination {
    margin-top: 24px;
    position: relative;
    bottom: auto;
    display: flex;
    justify-content: center;
}

.storedash-products-carousel .swiper-pagination-bullet {
    width: 12px;
    height: 12px;
    background: #d1d5db;
    opacity: 1;
    transition: all 0.3s ease;
}

.storedash-products-carousel .swiper-pagination-bullet-active {
    background: var(--pg-primary);
    width: 32px;
    border-radius: 6px;
}

/* Pagination Positioning - Top Center */
.pagination-top-center .swiper-pagination {
    margin-top: 0;
    margin-bottom: 24px;
    order: -1;
}

/* Pagination Positioning - Top Left */
.pagination-top-left .swiper-pagination {
    margin-top: 0;
    margin-bottom: 24px;
    order: -1;
    justify-content: flex-start;
}

/* Pagination Positioning - Top Right */
.pagination-top-right .swiper-pagination {
    margin-top: 0;
    margin-bottom: 24px;
    order: -1;
    justify-content: flex-end;
}

/* Pagination Positioning - Bottom Left */
.pagination-bottom-left .swiper-pagination {
    justify-content: flex-start;
}

/* Pagination Positioning - Bottom Right */
.pagination-bottom-right .swiper-pagination {
    justify-content: flex-end;
}

/* ============================================
   Carousel Bleed Effect (Products & Categories)
   ============================================ */

/* Enable overflow on right side only when bleed is active */
.carousel-bleed-enabled.storedash-products-carousel,
.carousel-bleed-enabled.storedash-categories-carousel {
    overflow: visible; /* Allow overflow to right */
}

/* Keep the swiper wrapper contained on left, overflow on right */
.carousel-bleed-enabled .swiper-wrapper {
    overflow: visible;
}

/* Ensure slides can overflow */
.carousel-bleed-enabled .swiper-slide {
    flex-shrink: 0;
}

/* Mask the overflow on the left side only */
.carousel-bleed-enabled.storedash-products-carousel::before,
.carousel-bleed-enabled.storedash-categories-carousel::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 1px;
    background: transparent;
    z-index: 10;
    pointer-events: none;
}

/* Ensure navigation arrows stay within container padding */
.carousel-bleed-enabled .swiper-button-prev,
.carousel-bleed-enabled .swiper-button-next {
    z-index: 20; /* Above overflow cards */
}

/* Left alignment for bleed mode - respect parent container padding */
/* Note: Swiper's spaceBetween only adds margin between slides, not on the first slide's left edge */
/* So no negative margin compensation is needed - first card naturally aligns with container padding */
.carousel-bleed-enabled .swiper-wrapper {
    justify-content: flex-start !important; /* Force left alignment, prevent centering */
}

/* ============================================
   Responsive Design
   ============================================ */

@media (max-width: 1024px) {
    .storedash-products-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (max-width: 768px) {
    .storedash-products-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
    }

    .product-card {
        padding: 12px;
    }

    .product-title {
        font-size: 14px;
    }

    .product-price {
        font-size: 16px;
    }

    .product-add-to-cart {
        padding: 10px 16px;
        font-size: 14px;
    }

    /* Keep heading and controls inline on mobile */
    .storedash-widget-header {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        margin-bottom: 16px;
    }

    .storedash-section-heading {
        font-size: 18px;
        flex: 1;
        min-width: 0; /* Allow text to shrink if needed */
    }

    .storedash-header-controls {
        flex-shrink: 0;
    }

    /* Adjust control button sizes on mobile */
    .storedash-header-controls .swiper-button-prev,
    .storedash-header-controls .swiper-button-next {
        width: 36px;
        height: 36px;
    }

    .storedash-header-controls .swiper-button-prev:after,
    .storedash-header-controls .swiper-button-next:after {
        font-size: 14px;
    }
}

@media (max-width: 480px) {
    .storedash-products-grid {
        grid-template-columns: 1fr;
    }
}

/* ============================================
   Accessibility - Reduced Motion
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }

    .product-card {
        transition: none !important;
    }
}

/* ============================================
   Print Styles
   ============================================ */

@media print {
    .storedash-products-wrapper {
        display: none;
    }
}

/* ============================================
   Loading State
   ============================================ */

.storedash-products-wrapper.loading {
    opacity: 0.6;
    pointer-events: none;
}

/* ============================================
   Skeleton Loader - Perceived Performance
   Shows animated placeholders while products load
   Prevents layout shift (CLS) and blocking page render
   ============================================ */

.storedash-products-skeleton {
    display: none; /* Hidden by default - only shown during filter loading */
    grid-template-columns: repeat(var(--columns, 4), 1fr);
    gap: var(--pg-gap, 20px);
}

.skeleton-card {
    background: #f5f5f5;
    border: 1px solid var(--pg-border);
    border-radius: var(--pg-radius);
    padding: var(--pg-spacing);
    display: flex;
    flex-direction: column;
}

.skeleton-image {
    aspect-ratio: 1;
    background: #e0e0e0;
    border-radius: 8px;
    margin-bottom: var(--pg-spacing);
}

.skeleton-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.skeleton-title {
    height: 20px;
    background: #e0e0e0;
    border-radius: 4px;
    width: 85%;
}

.skeleton-price {
    height: 18px;
    width: 50%;
    background: #e0e0e0;
    border-radius: 4px;
    margin-top: auto;
}

.skeleton-button {
    height: 44px;
    background: #e0e0e0;
    border-radius: 8px;
    margin-top: 12px;
}

/* Shimmer animation for skeleton elements */
/* Uses CSS variables for Elementor customization with hardcoded fallbacks */
.skeleton-shimmer {
    --skeleton-base: #e0e0e0;
    --skeleton-highlight: #f0f0f0;
    background: linear-gradient(
        90deg,
        var(--skeleton-base, #e0e0e0) 25%,
        var(--skeleton-highlight, #f0f0f0) 50%,
        var(--skeleton-base, #e0e0e0) 75%
    );
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite ease-in-out;
}

@keyframes shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Show skeleton during AJAX filter loading */
.storedash-products-wrapper.filter-loading .storedash-products-skeleton {
    display: grid;
}

.storedash-products-wrapper.filter-loading .storedash-products-grid,
.storedash-products-wrapper.filter-loading .storedash-products-carousel {
    display: none;
}

/* Responsive skeleton: padding adjustments only (columns via --columns CSS variable) */
@media (max-width: 768px) {
    .storedash-products-skeleton {
        gap: 16px;
    }

    .skeleton-card {
        padding: 12px;
    }
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .skeleton-shimmer {
        animation: none;
        background: #e0e0e0;
    }
}

/* ============================================
   Hide WooCommerce Elements
   ============================================ */

.product-card .added_to_cart,
.product-card + .added_to_cart,
.storedash-products-wrapper .added_to_cart.wc-forward {
    display: none !important;
}
