/**
 * SP Gallery Field — Frontend Styles
 *
 * Grid pattern (repeating):
 *   Row 1 → 1 image spanning 2 cols
 *   Row 2 → 2 images each spanning 1 col
 *   Row 3 → 1 image spanning 2 cols
 *   Row 4 → 2 images each spanning 1 col
 *   ...and so on
 *
 * Achieved purely with CSS Grid + nth-child — no JS required.
 */

/* ============================================================
   Gallery grid — 2 equal columns
   Gap can be overridden: --sp-gallery-gap: 16px;
   ============================================================ */
.sp-gallery-frontend {
    position: relative;
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--sp-gallery-gap, 12px);
    align-items: start;
}

/* ============================================================
   Pattern:
   Every 3rd item starting from 1  → spans 2 cols (wide)
   Every 3rd item starting from 2  → spans 1 col  (narrow)
   Every 3rd item starting from 3  → spans 1 col  (narrow)
   ============================================================ */

/* Wide item — positions 1, 4, 7, 10 ... */
.sp-gallery-item:nth-child(3n+1) {
    grid-column: span 2;
}

/* Narrow items — positions 2, 3, 5, 6, 8, 9 ... */
.sp-gallery-item:nth-child(3n+2),
.sp-gallery-item:nth-child(3n+3) {
    grid-column: span 1;
}

/* ============================================================
   Individual item
   ============================================================ */
.sp-gallery-item {
    position: relative;
    width: 100%;
    min-width: 0; /* prevent grid blowout */
}

/* ============================================================
   Trigger button — full clickable area
   ============================================================ */
.sp-gallery-trigger {
    position: relative;
    display: block;
    width: 100%;
    padding: 0;
    margin: 0;
    background: none;
    border: none;
    cursor: pointer;
    line-height: 0;
    overflow: hidden;
}

.sp-gallery-trigger img {
    width: 100%;
    height: auto;
    display: block;
    transition: opacity 0.2s ease;
}

.sp-gallery-trigger:hover img {
    opacity: 0.92;
}

/* ============================================================
   Icon overlay — top right of image
   ============================================================ */
.sp-gallery-icon {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 44px;
    height: 44px;
    display: block;
    line-height: 0;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.3));
}

.sp-gallery-trigger:hover .sp-gallery-icon {
    opacity: 1;
    transform: scale(1.05);
}

.sp-gallery-icon svg {
    width: 44px;
    height: 44px;
    display: block;
}

.sp-gallery-icon circle {
    fill-opacity: 0.92;
}

/* ============================================================
   Caption
   ============================================================ */
.sp-gallery-caption {
    margin: 6px 0 0;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
    color: #555;
}

/* ============================================================
   Lightbox
   ============================================================ */
.sp-gallery-lightbox {
    position: fixed;
    inset: 0;
    z-index: 99999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.sp-gallery-lightbox.is-open {
    opacity: 1;
    pointer-events: all;
}

.sp-gallery-lightbox-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
    cursor: pointer;
}

.sp-gallery-lightbox-inner {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 92vw;
    max-height: 92vh;
}

/* ── Close button ── */
.sp-gallery-lightbox-close {
    position: absolute;
    top: -14px;
    right: -14px;
    width: 36px;
    height: 36px;
    background: #fff;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    z-index: 2;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    transition: background 0.15s ease;
    flex-shrink: 0;
}

.sp-gallery-lightbox-close:hover {
    background: #f0f0f0;
}

.sp-gallery-lightbox-close svg {
    width: 16px;
    height: 16px;
}

/* ── Prev / Next arrows ── */
.sp-gallery-lightbox-prev,
.sp-gallery-lightbox-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    z-index: 2;
    transition: background 0.15s ease, border-color 0.15s ease;
    flex-shrink: 0;
}

.sp-gallery-lightbox-prev { left: -64px; }
.sp-gallery-lightbox-next { right: -64px; }

.sp-gallery-lightbox-prev:hover,
.sp-gallery-lightbox-next:hover {
    background: rgba(255, 255, 255, 0.28);
    border-color: rgba(255, 255, 255, 0.6);
}

.sp-gallery-lightbox-prev:disabled,
.sp-gallery-lightbox-next:disabled {
    opacity: 0.3;
    cursor: default;
}

.sp-gallery-lightbox-prev svg,
.sp-gallery-lightbox-next svg {
    width: 20px;
    height: 20px;
    color: #fff;
}

/* ── Lightbox image ── */
.sp-gallery-lightbox-img-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: 88vw;
    max-height: 80vh;
}

.sp-gallery-lightbox-img {
    max-width: 88vw;
    max-height: 80vh;
    width: auto;
    height: auto;
    object-fit: contain;
    display: block;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.sp-gallery-lightbox-img.is-loaded {
    opacity: 1;
}

/* ── Counter ── */
.sp-gallery-lightbox-counter {
    margin: 10px 0 0;
    color: rgba(255, 255, 255, 0.7);
    font-size: 13px;
    text-align: center;
}

/* ── Caption ── */
.sp-gallery-lightbox-caption {
    margin: 6px 0 0;
    padding: 0 12px;
    color: rgba(255, 255, 255, 0.85);
    font-size: 13px;
    text-align: center;
    max-width: 88vw;
    line-height: 1.5;
}

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

/* ── Body scroll lock ── */
.sp-lightbox-open {
    overflow: hidden !important;
}

/* ============================================================
   Responsive — single column on mobile, pattern resets
   ============================================================ */
@media (max-width: 520px) {
    .sp-gallery-frontend {
        grid-template-columns: 1fr;
    }

    /* All items full width on mobile */
    .sp-gallery-item:nth-child(3n+1),
    .sp-gallery-item:nth-child(3n+2),
    .sp-gallery-item:nth-child(3n+3) {
        grid-column: span 1;
    }

    .sp-gallery-icon {
        width: 36px;
        height: 36px;
        top: 8px;
        right: 8px;
        opacity: 1; /* always visible on touch devices */
    }

    .sp-gallery-icon svg {
        width: 36px;
        height: 36px;
    }

    .sp-gallery-lightbox-prev,
    .sp-gallery-lightbox-next {
        top: auto;
        bottom: -56px;
        transform: none;
        width: 40px;
        height: 40px;
    }

    .sp-gallery-lightbox-prev {
        left: calc(50% - 48px);
        right: auto;
    }

    .sp-gallery-lightbox-next {
        right: calc(50% - 48px);
        left: auto;
    }
}
button.sp-gallery-trigger {
    border-radius: 10px;
}