.lightbox {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(1.5rem, env(safe-area-inset-top)) max(1.5rem, env(safe-area-inset-right)) max(1.5rem, env(safe-area-inset-bottom)) max(1.5rem, env(safe-area-inset-left));
    background: rgba(0, 0, 0, 0.94);
    opacity: 0;
    visibility: hidden;
    overscroll-behavior: contain;
    /* visibility flips instantly on open (so the close button can take focus) and after the fade on close */
    transition: opacity 0.25s ease, visibility 0s linear 0.25s;
}

.lightbox.is-open {
    opacity: 1;
    visibility: visible;
    transition: opacity 0.25s ease, visibility 0s;
}

.lightbox__dialog {
    position: relative;
    z-index: 1;
    width: min(1100px, 100%);
    max-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.lightbox__figure {
    margin: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    max-height: calc(100vh - 8rem);
    max-height: calc(100dvh - 8rem);
    /* Vertical pan / pinch stay native; horizontal drags are handled by lightbox.js (swipe). */
    touch-action: pan-y pinch-zoom;
}

.lightbox__image {
    max-width: 100%;
    max-height: calc(100vh - 8rem);
    max-height: calc(100dvh - 8rem);
    width: auto;
    height: auto;
    object-fit: contain;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    will-change: transform;
}

.lightbox__caption {
    color: rgba(255, 255, 255, 0.9);
    text-align: center;
    font-size: 1rem;
    margin: 0;
    padding: 0 3.5rem;
}

.lightbox__counter {
    position: absolute;
    z-index: 3;
    top: max(1rem, env(safe-area-inset-top));
    left: max(1rem, env(safe-area-inset-left));
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0 0.9rem;
    border-radius: var(--radius-pill);
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 0.95rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.lightbox__counter:empty {
    display: none;
}

.lightbox__close,
.lightbox__nav {
    position: absolute;
    z-index: 3;
    border: 0;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.2s, transform 0.2s;
}

.lightbox__close:hover,
.lightbox__nav:hover {
    background: rgba(255, 255, 255, 0.3);
}

.lightbox__close:focus-visible,
.lightbox__nav:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 3px;
}

.lightbox__close {
    top: max(1rem, env(safe-area-inset-top));
    right: max(1rem, env(safe-area-inset-right));
    font-size: 2rem;
    line-height: 1;
}

.lightbox__nav {
    top: 50%;
    transform: translateY(-50%);
    font-size: 2rem;
    line-height: 1;
}

.lightbox__nav:hover {
    transform: translateY(-50%) scale(1.05);
}

.lightbox__nav--prev {
    left: max(1rem, env(safe-area-inset-left));
}

.lightbox__nav--next {
    right: max(1rem, env(safe-area-inset-right));
}

.lightbox__nav[hidden] {
    display: none;
}

@media (max-width: 767px) {
    .lightbox {
        padding: max(0.5rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right)) max(0.75rem, env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left));
    }

    .lightbox__figure,
    .lightbox__image {
        max-height: calc(100vh - 11rem);
        max-height: calc(100dvh - 11rem);
    }

    .lightbox__image {
        border-radius: var(--radius-sm, 8px);
    }

    .lightbox__caption {
        padding: 0 0.5rem;
        font-size: 1rem;
    }

    .lightbox__close {
        top: max(0.5rem, env(safe-area-inset-top));
        right: max(0.5rem, env(safe-area-inset-right));
    }

    .lightbox__counter {
        top: max(0.5rem, env(safe-area-inset-top));
        left: max(0.5rem, env(safe-area-inset-left));
    }

    .lightbox__nav--prev {
        left: max(0.5rem, env(safe-area-inset-left));
    }

    .lightbox__nav--next {
        right: max(0.5rem, env(safe-area-inset-right));
    }
}

@media (prefers-reduced-motion: reduce) {
    .lightbox,
    .lightbox__close,
    .lightbox__nav {
        transition: none;
    }

    .lightbox__nav:hover {
        transform: translateY(-50%);
    }
}
