/* Gallery — Responsive grid, lightbox */

@keyframes shimmer {
    0% { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

.markdown-content .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    grid-auto-rows: minmax(0, auto);
    gap: 0.75rem 1rem;
    margin: 1.5rem 0;
    width: 100%;
    box-sizing: border-box;
}
.markdown-content .gallery > h2 {
    grid-column: 1 / -1;
    margin: 1.5rem 0 0.5rem;
}
.markdown-content .gallery > h2:first-child {
    margin-top: 0;
}
.markdown-content .gallery > p {
    margin: 0;
}
.markdown-content .gallery > p img,
.markdown-content .gallery > img {
    margin: 0;
    width: 100%;
    height: auto;
    object-fit: cover;
    aspect-ratio: 4 / 3;
}
.markdown-content .gallery .gallery-item {
    margin: 0;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
    border-radius: var(--radius-md);
    background: linear-gradient(90deg, var(--color-border) 25%, var(--gray-100) 50%, var(--color-border) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}
.markdown-content .gallery .gallery-item:has(img.gallery-img-loaded) {
    animation: none;
    background: var(--color-border);
}
/* Placeholder until image loads, then fade in */
.markdown-content .gallery .gallery-item img {
    display: block;
    margin: 0;
    width: 100%;
    height: auto;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    object-position: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.25s ease-in, filter var(--duration-normal) var(--ease-default), transform var(--duration-normal) var(--ease-default);
}
.markdown-content .gallery .gallery-item img.gallery-img-loaded {
    opacity: 1;
}
.markdown-content .gallery .gallery-item:hover img.gallery-img-loaded {
    filter: brightness(0.93);
    transform: scale(1.02);
}
.markdown-content .gallery .gallery-item figcaption {
    margin-top: 0.35rem;
    font-size: 0.7rem;
    color: var(--color-text-secondary);
    line-height: 1.3;
    text-align: center;
}
@media (min-width: 480px) {
    .markdown-content .gallery {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    }
}
@media (min-width: 768px) {
    .markdown-content .gallery {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: 1rem;
    }
    .markdown-content .gallery .gallery-item figcaption {
        font-size: 0.75rem;
    }
}
@media (min-width: 1024px) {
    .markdown-content .gallery {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    }
}
.markdown-content .gallery > table,
.markdown-content .gallery > hr,
.markdown-content .gallery > p:has(a[href]) {
    grid-column: 1 / -1;
}
.markdown-content .gallery > table {
    max-width: 100%;
}

/* Gallery lightbox: click image to view full size */
.gallery-lightbox {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    box-sizing: border-box;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s var(--ease-default), visibility 0.2s var(--ease-default);
}
.gallery-lightbox.gallery-lightbox-visible {
    opacity: 1;
    visibility: visible;
}
.gallery-lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    cursor: pointer;
}
.gallery-lightbox-content {
    position: relative;
    max-width: 95vw;
    max-height: 95vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 1;
}
.gallery-lightbox-close {
    position: absolute;
    top: -2.5rem;
    right: 0;
    width: 2.25rem;
    height: 2.25rem;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.9);
    color: var(--color-text);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s;
}
.gallery-lightbox-close:hover {
    background: #fff;
    color: var(--gray-900);
}
.gallery-lightbox-img {
    max-width: 95vw;
    max-height: 85vh;
    width: auto;
    height: auto;
    object-fit: contain;
    display: block;
    border-radius: var(--radius-sm);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}
.gallery-lightbox-caption {
    margin: 0.75rem 0 0;
    padding: 0 1rem;
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.95);
    text-align: center;
    max-width: 90vw;
}

.gallery-lightbox-prev,
.gallery-lightbox-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 3rem;
    height: 3rem;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.85);
    color: var(--color-text);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--duration-normal) var(--ease-default),
                transform var(--duration-normal) var(--ease-default);
    z-index: 2;
}

.gallery-lightbox-prev { left: -4rem; }
.gallery-lightbox-next { right: -4rem; }

.gallery-lightbox-prev:hover,
.gallery-lightbox-next:hover {
    background: #fff;
    transform: translateY(-50%) scale(1.1);
}

.gallery-lightbox-counter {
    margin-top: 0.5rem;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.7);
    text-align: center;
}

@media (max-width: 768px) {
    .gallery-lightbox-prev { left: 0.5rem; }
    .gallery-lightbox-next { right: 0.5rem; }
    .gallery-lightbox-prev,
    .gallery-lightbox-next {
        width: 2.5rem;
        height: 2.5rem;
        font-size: 1.5rem;
    }
}
