/**
 * Product Gallery Styles - Updated
 * Shopee-like gallery with thumbnails and lightbox functionality
 * With larger thumbnails (65x65px) and skeleton loading effect
 */

 :root {
    --gallery-primary: #D40000; /* Rosso Corsa Red */
    --gallery-primary-light: #ff3333;
    --gallery-primary-dark: #a20000;
    --gallery-text: #333333;
    --gallery-text-light: #666666;
    --gallery-gray-light: #f5f5f5;
    --gallery-gray: #e0e0e0;
    --gallery-gray-dark: #999999;
    --gallery-white: #ffffff;
    --gallery-black: #000000;
    --gallery-shadow: rgba(0, 0, 0, 0.1);
    --gallery-transition: all 0.3s ease;
}

/* Main container */
.product-gallery-container {
    position: relative;
    width: 100%;
    margin-bottom: 30px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

/* Main image container */
.product-gallery-main {
    position: relative;
    width: 100%;
    margin-bottom: 12px;
    border-radius: 8px;
    overflow: hidden;
    background-color: var(--gallery-white);
    box-shadow: 0 2px 8px var(--gallery-shadow);
}

.product-gallery-main-image {
    position: relative;
    width: 100%;
    padding-bottom: 100%; /* 1:1 Aspect Ratio */
    cursor: pointer;
}

.product-gallery-current-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: var(--gallery-transition);
    opacity: 0; /* Start hidden for fade-in effect */
}

.product-gallery-current-image.loaded {
    opacity: 1;
}

/* Skeleton loading for main image */
.product-gallery-main-skeleton {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--gallery-gray-light);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.product-gallery-main-skeleton.hidden {
    display: none;
}

.product-gallery-skeleton-pulse {
    animation: gallery-skeleton-pulse 1.5s infinite;
}

@keyframes gallery-skeleton-pulse {
    0% {
        opacity: 0.6;
    }
    50% {
        opacity: 1;
    }
    100% {
        opacity: 0.6;
    }
}

.product-gallery-skeleton-icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 3px solid var(--gallery-gray);
    border-top-color: var(--gallery-gray-dark);
    animation: gallery-skeleton-spin 1s linear infinite;
}

@keyframes gallery-skeleton-spin {
    to {
        transform: rotate(360deg);
    }
}

/* Zoom icon */
.product-gallery-zoom-icon {
    position: absolute;
    bottom: 10px;
    right: 10px;
    width: 36px;
    height: 36px;
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--gallery-text);
    opacity: 0.7;
    transition: var(--gallery-transition);
    z-index: 2;
}

.product-gallery-main-image:hover .product-gallery-zoom-icon {
    opacity: 1;
}

/* Thumbnails container */
.product-gallery-thumbs {
    position: relative;
    width: 100%;
    height: 85px; /* Increased height for larger thumbnails */
    margin-top: 8px;
}

.product-gallery-thumbs-list {
    display: flex;
    align-items: center;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* Internet Explorer 10+ */
    gap: 8px;
    padding: 5px 0;
    height: 100%;
}

.product-gallery-thumbs-list::-webkit-scrollbar {
    display: none; /* WebKit */
}

/* Thumbnail item - UPDATED SIZE to 65x65px */
.product-gallery-thumb {
    flex: 0 0 auto;
    width: 65px; /* Updated from 50px to 65px */
    height: 65px; /* Updated from 50px to 65px */
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: var(--gallery-transition);
    position: relative;
}

.product-gallery-thumb.active {
    border-color: var(--gallery-primary);
}

.product-gallery-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    opacity: 0; /* Start hidden for fade-in effect */
    transition: opacity 0.3s ease;
}

.product-gallery-thumb img.loaded {
    opacity: 1;
}

/* Thumbnail skeleton */
.product-gallery-thumb-skeleton {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--gallery-gray-light);
    animation: gallery-skeleton-pulse 1.5s infinite;
}

.product-gallery-thumb-skeleton.hidden {
    display: none;
}

.product-gallery-thumb:hover {
    border-color: var(--gallery-primary-light);
}

/* Thumbnails navigation */
.product-gallery-thumbs-navigation {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.product-gallery-thumbs-prev,
.product-gallery-thumbs-next {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: var(--gallery-white);
    border: 1px solid var(--gallery-gray);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    pointer-events: auto;
    color: var(--gallery-text);
    box-shadow: 0 2px 5px var(--gallery-shadow);
    transition: var(--gallery-transition);
    opacity: 0.9;
}

.product-gallery-thumbs-prev svg,
.product-gallery-thumbs-next svg {
    width: 18px;
    height: 18px;
}

.product-gallery-thumbs-prev:hover,
.product-gallery-thumbs-next:hover {
    background-color: var(--gallery-primary);
    color: var(--gallery-white);
    border-color: var(--gallery-primary);
}

.product-gallery-thumbs-prev.disabled,
.product-gallery-thumbs-next.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* Modal/Lightbox */
.product-gallery-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    z-index: 2147483647; /* ค่า z-index สูงสุดที่เป็นไปได้ */
    display: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: all; /* รับประกันว่าสามารถใช้งาน modal ได้ */
    isolation: isolate; /* สร้าง stacking context ใหม่ */
    transform: translateZ(0); /* เปิดใช้งาน hardware acceleration */
}

.product-gallery-modal.open {
    display: block;
    opacity: 1;
}

.product-gallery-modal-content {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    z-index: 2147483647; /* ค่า z-index สูงสุด */
}

/* รับประกันว่าปุ่มปิดและส่วนควบคุมอื่น ๆ ก็จะอยู่เหนือทุก object */
.product-gallery-modal-header,
.product-gallery-modal-close,
.product-gallery-modal-prev,
.product-gallery-modal-next {
    z-index: 2147483647; /* ค่า z-index สูงสุด */
}

/* เพิ่ม CSS รองรับการทำงานบน iOS Safari ที่บางครั้งมีปัญหากับ fixed position */
@supports (-webkit-overflow-scrolling: touch) {
    .product-gallery-modal {
        position: fixed;
        -webkit-overflow-scrolling: touch;
        overflow-y: hidden;
    }
    
    body.modal-open {
        position: fixed;
        width: 100%;
        height: 100%;
    }
}

/* Modal header */
.product-gallery-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    color: var(--gallery-white);
}

.product-gallery-image-counter {
    font-size: 14px;
}

.product-gallery-modal-close {
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    color: var(--gallery-white);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.8;
    transition: var(--gallery-transition);
}

.product-gallery-modal-close:hover {
    opacity: 1;
    transform: scale(1.1);
}

/* Modal body */
.product-gallery-modal-body {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.product-gallery-modal-main {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.product-gallery-modal-slide {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
}

.product-gallery-modal-slide img {
    max-width: 90%;
    max-height: 70vh;
    object-fit: contain;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.product-gallery-modal-slide img.loaded {
    opacity: 1;
}

/* Modal image skeleton */
.product-gallery-modal-skeleton {
    width: 80%;
    height: 60vh;
    background-color: var(--gallery-gray-dark);
    animation: gallery-skeleton-pulse 1.5s infinite;
    border-radius: 4px;
}

.product-gallery-modal-skeleton.hidden {
    display: none;
}

/* Modal navigation */
.product-gallery-modal-prev,
.product-gallery-modal-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    background-color: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--gallery-white);
    transition: var(--gallery-transition);
}

.product-gallery-modal-prev {
    left: 20px;
}

.product-gallery-modal-next {
    right: 20px;
}

.product-gallery-modal-prev:hover,
.product-gallery-modal-next:hover {
    background-color: rgba(255, 255, 255, 0.4);
}

/* Modal thumbnails */
.product-gallery-modal-thumbs {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    padding: 15px 20px;
    background-color: rgba(0, 0, 0, 0.5);
    overflow-x: auto;
    white-space: nowrap;
    scrollbar-width: thin;
    scrollbar-color: var(--gallery-gray-dark) transparent;
}

.product-gallery-modal-thumbs::-webkit-scrollbar {
    height: 6px;
}

.product-gallery-modal-thumbs::-webkit-scrollbar-track {
    background: transparent;
}

.product-gallery-modal-thumbs::-webkit-scrollbar-thumb {
    background-color: var(--gallery-gray-dark);
    border-radius: 3px;
}

.product-gallery-modal-thumb {
    width: 65px; /* Updated from 50px to 65px to match main thumbnails */
    height: 65px; /* Updated from 50px to 65px to match main thumbnails */
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: var(--gallery-transition);
    opacity: 0.7;
    position: relative;
}

.product-gallery-modal-thumb.active {
    border-color: var(--gallery-white);
    opacity: 1;
}

.product-gallery-modal-thumb:hover {
    opacity: 1;
}

.product-gallery-modal-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.product-gallery-modal-thumb img.loaded {
    opacity: 1;
}

/* Modal thumbnail skeleton */
.product-gallery-modal-thumb-skeleton {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--gallery-gray-dark);
    animation: gallery-skeleton-pulse 1.5s infinite;
}

.product-gallery-modal-thumb-skeleton.hidden {
    display: none;
}

/* Responsive styles */
@media (max-width: 768px) {
    /* แก้ไขส่วนนี้เพื่อจำกัดความสูงของภาพใหญ่ไม่เกิน 400px ในมุมมองมือถือ */
    .product-gallery-main-image {
        padding-bottom: 0; /* ยกเลิก aspect ratio 1:1 */
        height: 400px; /* กำหนดความสูงคงที่ 400px */
        max-height: 400px;
    }
    
    .product-gallery-current-image {
        position: relative; /* ปรับตำแหน่งให้เหมาะสม */
        object-fit: contain; /* รักษาอัตราส่วนภาพ */
        height: 400px;
    }
    
    .product-gallery-main {
        height: auto;
        max-height: 400px;
    }
    
    /* คงค่าเดิมสำหรับส่วนอื่น ๆ */
    .product-gallery-modal-slide img {
        max-width: 100%;
        max-height: 60vh;
    }
    
    .product-gallery-modal-prev,
    .product-gallery-modal-next {
        width: 36px;
        height: 36px;
    }
    
    .product-gallery-modal-prev {
        left: 10px;
    }
    
    .product-gallery-modal-next {
        right: 10px;
    }
    
    .product-gallery-modal-thumbs {
        padding: 10px;
    }
    
    .product-gallery-modal-thumb {
        width: 50px; /* Smaller on mobile */
        height: 50px; /* Smaller on mobile */
    }
    
    .product-gallery-thumb {
        width: 55px; /* Slightly smaller on mobile */
        height: 55px; /* Slightly smaller on mobile */
    }
    
    .product-gallery-thumbs {
        height: 75px; /* Adjusted for smaller thumbnails */
    }
}