/* ==========================================================================
   Gallery Styles - Layout, Progressive Images, Modal, Back-to-Top
   ========================================================================== */

/* --- Gallery Section --- */

#gallery {
    font-family: 'Poppins', sans-serif;
    opacity: 1;
    transition: opacity 0.3s ease-in-out;
}

#gallery:not(.active) {
    opacity: 0;
    pointer-events: none;
}

#gallery .gallery-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 40px 20px 0 20px;
}

/* Gallery Heading */
#gallery .gallery-container .heading {
    width: 30%;
    padding-bottom: 50px;
}

#gallery .gallery-container .heading h3 {
    font-size: 3em;
    font-weight: bolder;
    border-bottom: 3px solid #222;
    margin-bottom: 30px;
    padding-bottom: 10px;
}

#gallery .gallery-container .heading h3 span {
    font-weight: 50;
}

/* --- Group Sections --- */

#gallery .group-section {
    width: 100%;
    max-width: 1400px;
    margin: 0 auto 40px auto;
    text-align: left;
}

@supports (content-visibility: auto) {
    #gallery .group-section {
        content-visibility: auto;
        contain-intrinsic-size: auto 900px;
    }
}

#gallery .group-header {
    width: 100%;
    margin: 0 0 16px 0;
}

#gallery .group-header h4 {
    display: inline-block;
    font-size: 1.4em;
    font-weight: 700;
    color: #000;
    border-bottom: 3px solid #000;
    padding-bottom: 6px;
    margin: 0 0 4px 0;
}

/* --- Masonry Layout --- */

#gallery .gallery-container .box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    gap: 20px;
}

#gallery .gallery-container .box .dream {
    display: flex;
    flex-direction: column;
    flex: 1;
    width: 32.5%;
}

/* --- Image Containers --- */

#gallery .image-container {
    width: 100%;
    overflow: hidden;
    margin-bottom: 20px;
    position: relative;
    border-radius: 5px;
    opacity: 1;
    transform: none;
}

#gallery .gallery-container .box .dream img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
    cursor: pointer;
    display: block;
    image-orientation: from-image;
}

#gallery .dream img:hover {
    transform: scale(1.1);
}

/* Loading States */
#gallery .image-hidden,
#gallery .image-loading {
    opacity: 1;
    transform: none;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* --- Progressive Image Loading --- */

.progressive-image-container {
    position: relative;
    width: 100%;
    height: auto;
    overflow: hidden;
    border-radius: 5px;
    background: #e8e8e8;
}

.image-placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #f3f3f3, #dedede);
    transition: opacity 0.3s ease;
    z-index: 1;
    pointer-events: none;
}

.progressive-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0;
    z-index: 2;
    position: absolute;
    inset: 0;
    cursor: pointer;
    image-orientation: from-image;
}

.progressive-image.loading {
    opacity: 0;
    transform: none;
}

.progressive-image.loaded {
    opacity: 1;
    transform: none;
}

.progressive-image.error {
    opacity: 0;
}

.progressive-image.loaded:hover {
    transform: scale(1.1);
}

.progressive-image-container::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #f3f3f3;
    border-top: 2px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 3;
    opacity: 0;
    pointer-events: none;
}

.image-container.image-loading .progressive-image-container::before {
    opacity: 1;
}

.image-container.image-error .progressive-image-container::before {
    display: none;
}

.image-container.image-error .progressive-image-container::after {
    content: 'Image unavailable';
    position: absolute;
    inset: 0;
    z-index: 4;
    display: grid;
    place-items: center;
    color: #666;
    background: #eeeeee;
    font-size: 0.85rem;
    pointer-events: none;
}

/* --- Modal --- */

#imageModal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(5px);
    overscroll-behavior: contain;
    transition: opacity 0.3s ease;
}

#imageModal.modal-opening {
    animation: modalFadeIn 0.3s ease;
}

#imageModal.modal-closing {
    animation: modalFadeOut 0.3s ease;
}

@keyframes modalFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes modalFadeOut {
    from { opacity: 1; }
    to { opacity: 0; }
}

#imageModal .modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-width: 90vw;
    max-height: 90vh;
}

#imageModal .modal img,
#imageModal #modalImg {
    max-width: 100%;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    image-orientation: from-image;
}

#imageModal .modal-close {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 18px);
    right: calc(env(safe-area-inset-right, 0px) + 16px);
    width: 52px;
    height: 52px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(10px);
    color: #fff;
    font-size: 30px;
    font-weight: 500;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    z-index: 2002;
    -webkit-appearance: none;
    appearance: none;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.3s ease, color 0.3s ease, transform 0.3s ease;
}

#imageModal .modal-close:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
    transform: scale(1.05);
}

#imageModal .nav-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: none;
    font-size: 24px;
    padding: 15px 20px;
    cursor: pointer;
    border-radius: 5px;
    transition: background-color 0.3s ease;
    z-index: 2001;
}

#imageModal .nav-arrow:hover {
    background: rgba(255, 255, 255, 0.4);
}

#imageModal .prev {
    left: 20px;
}

#imageModal .next {
    right: 20px;
}

#imageModal .image-transitioning {
    opacity: 0.3;
    transform: scale(0.95);
    transition: all 0.15s ease-in-out;
}

/* --- Back to Top Button --- */

#backToTop {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 50px;
    height: 50px;
    background: white;
    color: black;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    opacity: 0;
    visibility: hidden;
    transform: translateY(100px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

#backToTop::before {
    content: '';
    width: 12px;
    height: 2px;
    background: black;
    margin-bottom: 2px;
    border-radius: 1px;
}

#backToTop.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

#backToTop:hover {
    background: #f5f5f5;
    transform: translateY(-3px) scale(1.1);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

#backToTop:active {
    transform: translateY(-1px) scale(1.05);
    background: #eeeeee;
}

/* --- Groups Container --- */

#groupsContainer {
    width: 100%;
    max-width: 1400px;
}

/* --- Gallery Status --- */

.gallery-status {
    text-align: center;
    padding: 56px 24px;
    color: #666;
    background: rgba(255, 255, 255, 0.7);
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 10px;
    margin: 20px auto;
    max-width: 600px;
}

.gallery-status h3 {
    font-size: 1.5em;
    margin: 0 0 12px;
    color: #333;
}

.gallery-status p {
    margin: 0;
    font-size: 1em;
    line-height: 1.5;
}

.gallery-status__spinner {
    display: block;
    width: 28px;
    height: 28px;
    margin: 0 auto 18px;
    border: 3px solid #dddddd;
    border-top-color: #222;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.gallery-retry {
    margin-top: 20px;
    padding: 10px 18px;
    border: 1px solid #222;
    border-radius: 999px;
    background: #222;
    color: #fff;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.gallery-retry:hover,
.gallery-retry:focus-visible {
    background: #fff;
    color: #222;
}

/* --- Gallery Responsive --- */

@media only screen and (max-width: 769px) {
    #gallery .gallery-container .box {
        flex-direction: column;
        gap: 20px;
    }

    #gallery .gallery-container .box .dream {
        width: 100%;
    }

    #imageModal .nav-arrow {
        padding: 10px 15px;
        font-size: 18px;
    }

    #imageModal .modal-close {
        width: 52px;
        height: 52px;
        font-size: 28px;
        right: calc(env(safe-area-inset-right, 0px) + 14px);
        top: calc(env(safe-area-inset-top, 0px) + 18px);
    }

    #backToTop {
        bottom: 20px;
        right: 20px;
        width: 45px;
        height: 45px;
        font-size: 14px;
    }

    #backToTop::before {
        width: 10px;
        height: 2px;
    }

    #gallery .gallery-container .heading {
        width: 90%;
    }
}

@media only screen and (max-width: 643px) {
    #gallery .gallery-container .heading {
        width: 100%;
    }

    #gallery .gallery-container .heading h3 {
        font-size: 2em;
    }

    #gallery .group-header h4 {
        font-size: 1.2em;
    }
}

@media (prefers-reduced-motion: reduce) {
    #gallery .image-container,
    #gallery .progressive-image,
    #imageModal,
    #imageModal .modal-content,
    .gallery-status__spinner {
        animation-duration: 0.01ms;
        animation-iteration-count: 1;
        animation-delay: 0ms;
        transition-duration: 0.01ms;
        transition-delay: 0ms;
    }

}
