/* ============================================
   IMAGE GALLERY GRID
   ============================================ */

.image-gallery-grid {
    container-type: inline-size;
    width: 100%;
    margin: 2rem 0;
}

/* ============================================
   GRID LAYOUT - Adaptive Based on Image Count
   ============================================ */

.gallery-grid {
    display: grid;
    gap: 8px;
    justify-content: center; /* centre la grille quand elle ne remplit pas toute la largeur */
}

.grid-item {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background: #f0f0f0;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.grid-item:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    /* z-index: 1; */
}

.grid-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top;
    display: block;
}

/* ============================================
   DESKTOP LAYOUTS (> 768px)
   grid-template-rows calculé via cqi (1% de la largeur du conteneur)
   pour que chaque case de base soit un carré parfait.
   Formule : (100cqi - (N-1)*8px) / N  pour N colonnes avec gap 8px.
   ============================================ */

/* 1 image — 1 colonne, 1 rangée */
.gallery-grid[data-count="1"] {
    grid-template-columns: min(100cqi, 400px);
    grid-template-rows: min(100cqi, 400px);
}
.gallery-grid[data-count="1"] .grid-item-1 { grid-area: 1 / 1 / 2 / 2; } /* carré (1col × 1rang) */

/* 2 images — 2 colonnes, 1 rangée */
.gallery-grid[data-count="2"] {
    grid-template-columns: repeat(2, min(calc((100cqi - 8px) / 2), 300px));
    grid-template-rows: min(calc((100cqi - 8px) / 2), 300px);
}
.gallery-grid[data-count="2"] .grid-item-1 { grid-area: 1 / 1 / 2 / 2; } /* carré (1col × 1rang) */
.gallery-grid[data-count="2"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré (1col × 1rang) */

/* 3 images — 2 colonnes, 2 rangées */
.gallery-grid[data-count="3"] {
    grid-template-columns: repeat(2, min(calc((100cqi - 8px) / 2), 240px));
    grid-template-rows: repeat(2, min(calc((100cqi - 8px) / 2), 240px));
}
.gallery-grid[data-count="3"] .grid-item-1 { grid-area: 1 / 1 / 3 / 2; } /* verticale  (1col × 2rangs) */
.gallery-grid[data-count="3"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré      (1col × 1rang)  */
.gallery-grid[data-count="3"] .grid-item-3 { grid-area: 2 / 2 / 3 / 3; } /* carré      (1col × 1rang)  */

/* 4 images — 3 colonnes, 2 rangées */
.gallery-grid[data-count="4"] {
    grid-template-columns: repeat(3, min(calc((100cqi - 16px) / 3), 220px));
    grid-template-rows: repeat(2, min(calc((100cqi - 16px) / 3), 220px));
}
.gallery-grid[data-count="4"] .grid-item-1 { grid-area: 1 / 1 / 3 / 2; } /* verticale   (1col × 2rangs) */
.gallery-grid[data-count="4"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré       (1col × 1rang)  */
.gallery-grid[data-count="4"] .grid-item-3 { grid-area: 1 / 3 / 2 / 4; } /* carré       (1col × 1rang)  */
.gallery-grid[data-count="4"] .grid-item-4 { grid-area: 2 / 2 / 3 / 4; } /* horizontale (2cols × 1rang) */

/* 5 images — 3 colonnes, 2 rangées */
.gallery-grid[data-count="5"] {
    grid-template-columns: repeat(3, min(calc((100cqi - 16px) / 3), 220px));
    grid-template-rows: repeat(2, min(calc((100cqi - 16px) / 3), 220px));
}
.gallery-grid[data-count="5"] .grid-item-1 { grid-area: 1 / 1 / 3 / 2; } /* verticale  (1col × 2rangs) */
.gallery-grid[data-count="5"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré      (1col × 1rang)  */
.gallery-grid[data-count="5"] .grid-item-3 { grid-area: 1 / 3 / 2 / 4; } /* carré      (1col × 1rang)  */
.gallery-grid[data-count="5"] .grid-item-4 { grid-area: 2 / 2 / 3 / 3; } /* carré      (1col × 1rang)  */
.gallery-grid[data-count="5"] .grid-item-5 { grid-area: 2 / 3 / 3 / 4; } /* carré      (1col × 1rang)  */

/* 6 images — 4 colonnes, 2 rangées : [1][2][3][6] / [1][4][5][6] */
.gallery-grid[data-count="6"] {
    grid-template-columns: repeat(4, min(calc((100cqi - 24px) / 4), 240px));
    grid-template-rows: repeat(2, min(calc((100cqi - 24px) / 4), 240px));
}
.gallery-grid[data-count="6"] .grid-item-1 { grid-area: 1 / 1 / 3 / 2; } /* verticale (1col × 2rangs) */
.gallery-grid[data-count="6"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré     (1col × 1rang)  */
.gallery-grid[data-count="6"] .grid-item-3 { grid-area: 1 / 3 / 2 / 4; } /* carré     (1col × 1rang)  */
.gallery-grid[data-count="6"] .grid-item-4 { grid-area: 2 / 2 / 3 / 3; } /* carré     (1col × 1rang)  */
.gallery-grid[data-count="6"] .grid-item-5 { grid-area: 2 / 3 / 3 / 4; } /* carré     (1col × 1rang)  */
.gallery-grid[data-count="6"] .grid-item-6 { grid-area: 1 / 4 / 3 / 5; } /* verticale (1col × 2rangs) */

/* ============================================
   +X PHOTOS OVERLAY
   ============================================ */

.grid-item.has-overlay::before {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    /* z-index: 1; */
    transition: background 0.3s ease;
}

.grid-item.has-overlay:hover::before {
    background: rgba(0, 0, 0, 0.7);
}

.overlay-more {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 2;
    color: white;
    font-weight: 600;
    pointer-events: none;
}

.more-count {
    font-size: 3rem;
    line-height: 1;
    margin-bottom: 0.5rem;
}

.more-text {
    font-size: 1rem;
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* ============================================
   LIGHTBOX
   ============================================ */

.gallery-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 1);
    z-index: 999999;
    display: none;
    align-items: center;
    justify-content: center;
}

.gallery-lightbox.is-open {
    display: flex;
}

.lightbox-content {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.lightbox-image {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    user-select: none;
}

.lightbox-caption {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 2rem 1.5rem 0.75rem;
    background: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.7));
    color: white;
    font-size: 0.85rem;
    text-align: center;
    line-height: 1.5;
}

.lightbox-caption:empty {
    display: none;
}

.lightbox-counter {
    position: absolute;
    top: -28px;
    right: 0;
    color: white;
    font-size: 0.9rem;
    font-weight: 500;
}

/* Lightbox Controls */
button.lightbox-close,
button.lightbox-prev,
button.lightbox-next {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    font-size: 2rem;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease;
    z-index: 10;
}

button.lightbox-close:hover,
button.lightbox-prev:hover,
button.lightbox-next:hover,
button.lightbox-close:focus,
button.lightbox-prev:focus,
button.lightbox-next:focus {
    background: rgba(255, 255, 255, 0.2);
}

.lightbox-close {
    top: 20px;
    right: 20px;
    border-radius: 50%;
    font-size: 2.5rem;
    line-height: 1;
}

.lightbox-prev {
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 4px;
}

.lightbox-next {
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 4px;
}

/* Hide navigation on first/last image */
.gallery-lightbox[data-index="0"] .lightbox-prev {
    opacity: 0.3;
    pointer-events: none;
}

.gallery-lightbox[data-index-is-last="true"] .lightbox-next {
    opacity: 0.3;
    pointer-events: none;
}

/* ============================================
   LIGHTBOX THUMBNAILS CAROUSEL
   ============================================ */

.lightbox-thumbnails {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    max-width: 90vw;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 10px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    backdrop-filter: blur(10px);
}

/* Hide scrollbar but keep functionality */
.lightbox-thumbnails::-webkit-scrollbar {
    height: 4px;
}

.lightbox-thumbnails::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
}

.lightbox-thumbnails::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
}

.lightbox-thumbnails::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5);
}

.thumbnails-track {
    display: flex;
    gap: 8px;
}

.lightbox-thumb {
    width: 80px;
    height: 80px;
    object-fit: cover;
    cursor: pointer;
    border-radius: 4px;
    opacity: 0.5;
    transition: all 0.2s ease;
    border: 2px solid transparent;
    flex-shrink: 0;
}

.lightbox-thumb:hover {
    opacity: 0.8;
    transform: scale(1.05);
}

.lightbox-thumb.is-active {
    opacity: 1;
    border-color: white;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

/* Adjust lightbox content position when thumbnails are visible */
.gallery-lightbox.has-thumbnails .lightbox-content {
    margin-bottom: 100px;
}

/* ============================================
   RESPONSIVE
   ============================================ */

/* Tablet (481px - 768px) */
@media (max-width: 768px) {
    /* 3 images - 1 wide en haut, 2 en bas — 2 colonnes, 2 rangées */
    .gallery-grid[data-count="3"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(2, calc((100cqi - 8px) / 2));
    }
    .gallery-grid[data-count="3"] .grid-item-1 { grid-area: 1 / 1 / 2 / 3; } /* horizontale (2cols × 1rang) */
    .gallery-grid[data-count="3"] .grid-item-2 { grid-area: 2 / 1 / 3 / 2; } /* carré       (1col × 1rang)  */
    .gallery-grid[data-count="3"] .grid-item-3 { grid-area: 2 / 2 / 3 / 3; } /* carré       (1col × 1rang)  */

    /* 4 images - 2x2 — 2 colonnes, 2 rangées */
    .gallery-grid[data-count="4"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(2, calc((100cqi - 8px) / 2));
    }
    .gallery-grid[data-count="4"] .grid-item-1 { grid-area: 1 / 1 / 2 / 2; } /* carré (1col × 1rang) */
    .gallery-grid[data-count="4"] .grid-item-2 { grid-area: 1 / 2 / 2 / 3; } /* carré (1col × 1rang) */
    .gallery-grid[data-count="4"] .grid-item-3 { grid-area: 2 / 1 / 3 / 2; } /* carré (1col × 1rang) */
    .gallery-grid[data-count="4"] .grid-item-4 { grid-area: 2 / 2 / 3 / 3; } /* carré (1col × 1rang) */

    /* 5 images - 1 wide + 2x2 — 2 colonnes, 3 rangées */
    .gallery-grid[data-count="5"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(3, calc((100cqi - 8px) / 2));
    }
    .gallery-grid[data-count="5"] .grid-item-1 { grid-area: 1 / 1 / 2 / 3; } /* horizontale (2cols × 1rang) */
    .gallery-grid[data-count="5"] .grid-item-2 { grid-area: 2 / 1 / 3 / 2; } /* carré       (1col × 1rang)  */
    .gallery-grid[data-count="5"] .grid-item-3 { grid-area: 2 / 2 / 3 / 3; } /* carré       (1col × 1rang)  */
    .gallery-grid[data-count="5"] .grid-item-4 { grid-area: 3 / 1 / 4 / 2; } /* carré       (1col × 1rang)  */
    .gallery-grid[data-count="5"] .grid-item-5 { grid-area: 3 / 2 / 4 / 3; } /* carré       (1col × 1rang)  */

    /* 6 images - 2 colonnes, 4 rangées : [1][6] / [1][6] / [2][3] / [4][5] */
    .gallery-grid[data-count="6"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(4, calc((100cqi - 8px) / 2));
    }
    .gallery-grid[data-count="6"] .grid-item-1 { grid-area: 1 / 1 / 3 / 2; } /* verticale (1col × 2rangs) */
    .gallery-grid[data-count="6"] .grid-item-6 { grid-area: 1 / 2 / 3 / 3; } /* verticale (1col × 2rangs) */
    .gallery-grid[data-count="6"] .grid-item-2 { grid-area: 3 / 1 / 4 / 2; } /* carré     (1col × 1rang)  */
    .gallery-grid[data-count="6"] .grid-item-3 { grid-area: 3 / 2 / 4 / 3; } /* carré     (1col × 1rang)  */
    .gallery-grid[data-count="6"] .grid-item-4 { grid-area: 4 / 1 / 5 / 2; } /* carré     (1col × 1rang)  */
    .gallery-grid[data-count="6"] .grid-item-5 { grid-area: 4 / 2 / 5 / 3; } /* carré     (1col × 1rang)  */

    .more-count {
        font-size: 2rem;
    }
}

/* Mobile (< 480px) - Colonne unique, cases carrées */
@media (max-width: 480px) {
    .gallery-grid[data-count="1"],
    .gallery-grid[data-count="2"],
    .gallery-grid[data-count="3"],
    .gallery-grid[data-count="4"],
    .gallery-grid[data-count="5"],
    .gallery-grid[data-count="6"] {
        grid-template-columns: 1fr;
        grid-template-rows: unset;
        grid-auto-rows: 100cqi;
        gap: 4px;
    }

    /* Reset all grid areas to auto-flow vertically */
    .gallery-grid[data-count="1"] .grid-item-1,
    .gallery-grid[data-count="2"] .grid-item-1,
    .gallery-grid[data-count="2"] .grid-item-2,
    .gallery-grid[data-count="3"] .grid-item-1,
    .gallery-grid[data-count="3"] .grid-item-2,
    .gallery-grid[data-count="3"] .grid-item-3,
    .gallery-grid[data-count="4"] .grid-item-1,
    .gallery-grid[data-count="4"] .grid-item-2,
    .gallery-grid[data-count="4"] .grid-item-3,
    .gallery-grid[data-count="4"] .grid-item-4,
    .gallery-grid[data-count="5"] .grid-item-1,
    .gallery-grid[data-count="5"] .grid-item-2,
    .gallery-grid[data-count="5"] .grid-item-3,
    .gallery-grid[data-count="5"] .grid-item-4,
    .gallery-grid[data-count="5"] .grid-item-5,
    .gallery-grid[data-count="6"] .grid-item-1,
    .gallery-grid[data-count="6"] .grid-item-2,
    .gallery-grid[data-count="6"] .grid-item-3,
    .gallery-grid[data-count="6"] .grid-item-4,
    .gallery-grid[data-count="6"] .grid-item-5,
    .gallery-grid[data-count="6"] .grid-item-6 {
        grid-area: auto;
    }

    .more-count {
        font-size: 2.5rem;
    }

    /* Lightbox adjustments */
    .lightbox-prev,
    .lightbox-next {
        width: 40px;
        height: 40px;
        font-size: 1.5rem;
    }

    .lightbox-close {
        width: 40px;
        height: 40px;
        font-size: 2rem;
    }

    .lightbox-counter {
        top: auto;
        bottom: -2rem;
        right: 50%;
        transform: translateX(50%);
    }

    /* Hide thumbnails on very small screens to save space */
    .lightbox-thumbnails {
        display: none;
    }

    .gallery-lightbox.has-thumbnails .lightbox-content {
        margin-bottom: 0;
    }
}

/* ============================================
   ANIMATIONS
   ============================================ */

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

.gallery-lightbox.is-open {
    animation: fadeIn 0.2s ease;
}

.lightbox-image {
    animation: fadeIn 0.3s ease;
}