/* 
 * PROVEN SOLUTION: Implementation from swatch.md documentation
 * @version 10.0.0 - PLUGIN OVERRIDE: Target WooCommerce Variation Swatches plugin selectors
 * Uses object-fit: cover for automatic resizing and centering
 */

/* ===== DESKTOP SWATCHES: 40px circular with native resolution + crop ===== */

/* Target plugin's swatch wrapper - consolidated rules */
.swatch-wrapper,
.select-option.swatch-wrapper {
    width: 50px !important;
    height: 50px !important;
    border-radius: 50% !important;
    overflow: hidden !important;
    float: left !important;
    margin: 0 5px 5px 0 !important;
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    border: 2px solid #ddd !important;
    position: relative !important;
    box-sizing: border-box !important;
    padding: 0 !important; /* Remove padding that reduces anchor size */
}

/* Ensure swatch links are properly sized */
.swatch-wrapper a,
.select-option.swatch-wrapper a,
.swatch-anchor {
    width: 100% !important; /* Fill wrapper completely - override inline styles */
    height: 100% !important; /* Fill wrapper completely - override inline styles */
    display: block !important;
    line-height: 0 !important;
    text-decoration: none !important;
    position: relative !important;
    overflow: hidden !important;
    border: none !important; /* Remove any borders */
    padding: 0 !important; /* Remove any padding */
    margin: 0 !important; /* Remove any margins */
    box-sizing: border-box !important;
}

/* Critical: Preserve native resolution and center images - consolidated */
.single-product .swatch-wrapper img,
.single-product .select-option.swatch-wrapper img,
.single-product .swatch-anchor img {
    width: auto !important;
    height: auto !important;
    max-width: none !important;
    max-height: none !important;
    object-fit: cover !important;
    object-position: center !important;
    position: absolute !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    min-width: 100% !important;
    min-height: 100% !important;
}

/* ===== MOBILE SWATCHES: 50px circular with native resolution + crop ===== */

@media (max-width: 768px) {
    /* Override all swatch wrapper properties for mobile */
    body.single-product .swatch-wrapper,
    body.single-product .select-option.swatch-wrapper {
        width: 50px !important;
        height: 50px !important;
        /* Reduce outer margin to allow 3 items per row in narrow columns */
        margin: 0 3px 3px 0 !important;
        border: 2px solid #ddd !important;
    }

    /* Override all swatch link properties for mobile */
    body.single-product .swatch-wrapper a,
    body.single-product .select-option.swatch-wrapper a,
    body.single-product .swatch-anchor,
    .swatch-wrapper a,
    .select-option.swatch-wrapper a,
    .swatch-anchor {
        width: 100% !important; /* Fill wrapper completely */
        height: 100% !important; /* Fill wrapper completely */
        line-height: 0 !important;
        /* Remove inner right/bottom margin to avoid double spacing */
        margin: 0 !important;
    }

    /* Override all swatch image properties for mobile */
    body.single-product .swatch-wrapper img,
    body.single-product .select-option.swatch-wrapper img,
    body.single-product .swatch-anchor img,
    .variations-table .swatch-wrapper img,
    .variations-table .select-option.swatch-wrapper img,
    .variations-table .swatch-anchor img {
        width: auto !important;
        height: auto !important;
        max-width: none !important;
        max-height: none !important;
        object-fit: cover !important;
        object-position: center !important;
        position: absolute !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%) !important;
        min-width: 100% !important;
        min-height: 100% !important;
    }
}

/* ===== HOVER & SELECTED STATES ===== */

.swatch-wrapper:hover,
.select-option.swatch-wrapper:hover {
    border-color: #333 !important;
}

.swatch-wrapper.selected,
.select-option.swatch-wrapper.selected {
    border-color: #000 !important;
}

/* ===== CRITICAL OVERRIDE: Ensure our rules take precedence over Storefront theme ===== */

/* Mobile overrides for variations-table */
@media (max-width: 768px) {
    body.single-product .variations-table .swatch-wrapper,
    body.single-product .variations-table .select-option.swatch-wrapper {
        width: 50px !important;
        height: 50px !important;
        margin: 0 4px 4px 0 !important;
    }

    body.single-product .variations-table .swatch-wrapper a,
    body.single-product .variations-table .select-option.swatch-wrapper a,
    body.single-product .variations-table .swatch-anchor {
        width: 100% !important; /* Fill wrapper completely */
        height: 100% !important; /* Fill wrapper completely */
    }
    
    body.single-product .variations-table .swatch-wrapper img,
    body.single-product .variations-table .select-option.swatch-wrapper img,
    body.single-product .variations-table .swatch-anchor img {
        width: auto !important;
        height: auto !important;
        max-width: none !important;
        max-height: none !important;
        object-fit: cover !important;
        object-position: center !important;
        position: absolute !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%) !important;
        min-width: 100% !important;
        min-height: 100% !important;
    }
}

/* ===== CRITICAL: Restore swatch plugin functionality ===== */

/* Hide original dropdown when swatches are present */
/* Progressive enhancement: Use :has() for modern browsers, fallback for older browsers */
.single-product .variations_form .variations:has(.swatch-wrapper) select,
.single-product .variations_form .variations:has(.select-option.swatch-wrapper) select {
    display: none !important;
}

/* Fallback: Hide select dropdown when swatches are present (for older browsers) */
.single-product .variations_form .variations .swatch-wrapper ~ select,
.single-product .variations_form .variations .select-option.swatch-wrapper ~ select {
    display: none !important;
}

/* Ensure swatches are visible and properly styled */
.single-product .variations_form .swatch-wrapper,
.single-product .variations_form .select-option.swatch-wrapper {
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    width: 40px !important;
    height: 40px !important;
    border-radius: 50% !important;
    overflow: hidden !important;
    float: left !important;
    margin: 0 5px 5px 0 !important;
    border: 2px solid #ddd !important;
    position: relative !important;
    box-sizing: border-box !important;
}

/* Style swatch anchor elements */
.single-product .variations_form .swatch-wrapper a,
.single-product .variations_form .select-option.swatch-wrapper a {
    width: 100% !important; /* Fill wrapper completely */
    height: 100% !important; /* Fill wrapper completely */
    display: block !important;
    line-height: 0 !important;
    text-decoration: none !important;
    position: relative !important;
    overflow: hidden !important;
}

/* Style swatch images */
.single-product .variations_form .swatch-wrapper img,
.single-product .variations_form .select-option.swatch-wrapper img {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
    object-position: center !important;
}

/* Ensure swatch buttons are properly styled as circular elements */
.single-product .variations_form .swatch-wrapper,
.single-product .variations_form .select-option.swatch-wrapper {
    width: 40px !important;
    height: 40px !important;
    border-radius: 50% !important;
    overflow: hidden !important;
    float: left !important;
    margin: 0 5px 5px 0 !important;
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    border: 2px solid #ddd !important;
    position: relative !important;
    box-sizing: border-box !important;
}

.single-product .variations_form .swatch-wrapper a,
.single-product .variations_form .select-option.swatch-wrapper a {
    width: 40px !important;
    height: 40px !important;
    display: block !important;
    line-height: 0 !important;
    text-decoration: none !important;
    position: relative !important;
    overflow: hidden !important;
}

.single-product .variations_form .swatch-wrapper img,
.single-product .variations_form .select-option.swatch-wrapper img {
    width: auto !important;
    height: auto !important;
    max-width: none !important;
    max-height: none !important;
    object-fit: cover !important;
    object-position: center !important;
    position: absolute !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    min-width: 100% !important;
    min-height: 100% !important;
}

/* Mobile swatch sizing */
@media (max-width: 768px) {
    .single-product .variations_form .swatch-wrapper,
    .single-product .variations_form .select-option.swatch-wrapper {
        width: 50px !important;
        height: 50px !important;
        margin: 0 4px 4px 0 !important;
    }
    
    .single-product .variations_form .swatch-wrapper a,
    .single-product .variations_form .select-option.swatch-wrapper a {
        width: 100% !important; /* Fill wrapper completely */
        height: 100% !important; /* Fill wrapper completely */
    }
}

/* ===== FINAL CANONICAL CENTERING (OVERRIDES ABSOLUTE-POSITION VARIANTS) ===== */
/* Ensure perfect centering at native resolution (no scaling), crop via circle */
.single-product .swatch-wrapper img,
.single-product .select-option.swatch-wrapper img,
.single-product .swatch-anchor img,
.single-product .variations_form .swatch-wrapper img,
.single-product .variations_form .select-option.swatch-wrapper img,
.single-product .variations_form .swatch-anchor img,
/* Ensure override inside variations-table contexts too */
.single-product .variations-table .swatch-wrapper img,
.single-product .variations-table .select-option.swatch-wrapper img,
.single-product .variations-table .swatch-anchor img,
/* Fallback for any other context on product pages */
.single-product .product .swatch-wrapper img,
.single-product .product .select-option.swatch-wrapper img,
.single-product .product .swatch-anchor img {
    width: auto !important; /* Ensure native width */
    height: auto !important; /* Ensure native height */
    max-width: none !important; /* Prevent scaling down */
    max-height: none !important; /* Prevent scaling down */
    object-fit: cover !important; /* Scale up to fill container */
    object-position: center !important;
    position: absolute !important;
    top: 50% !important; /* Center vertically */
    left: 50% !important; /* Center horizontally */
    transform: translate(-50%, -50%) !important; /* Perfect centering */
    min-width: 100% !important; /* Ensure image fills container */
    min-height: 100% !important; /* Ensure image fills container */
    margin: 0 !important;
    padding: 0 !important;
}

/* Use a consistent 50px circular crop on all breakpoints */
.single-product .swatch-wrapper,
.single-product .select-option.swatch-wrapper,
.single-product .variations_form .swatch-wrapper,
.single-product .variations_form .select-option.swatch-wrapper {
    width: 50px !important;
    height: 50px !important;
}

/* Make anchors fill the wrapper's content box to avoid border-induced drift */
.single-product .swatch-wrapper a,
.single-product .select-option.swatch-wrapper a,
.single-product .variations_form .swatch-wrapper a,
.single-product .variations_form .select-option.swatch-wrapper a {
    width: 100% !important; /* Override inline styles */
    height: 100% !important; /* Override inline styles */
    box-sizing: border-box !important;
}

/* ===== ACCESSIBILITY FIX: Remove aria-hidden from focusable elements ===== */
/* Fix for WooCommerce Variation Swatches plugin adding aria-hidden to focusable links */
/* This violates WCAG 2.1 - focusable elements must not be hidden from assistive technology */
.swatch-anchor[aria-hidden="true"],
.swatch-wrapper a[aria-hidden="true"],
.select-option.swatch-wrapper a[aria-hidden="true"] {
    /* CSS cannot remove attributes, but we ensure the element remains accessible */
    /* The JavaScript fix below will handle removing the aria-hidden attribute */
}

/* Ensure swatch links are always keyboard accessible */
.swatch-anchor:focus,
.swatch-wrapper a:focus,
.select-option.swatch-wrapper a:focus {
    outline: 2px solid #0073aa !important;
    outline-offset: 2px !important;
    z-index: 10 !important;
}