.masonry {
    --masonry-columns: 3;
    --masonry-gap: 6px;
    display: grid !important;
    grid-template-columns: repeat(var(--masonry-columns), minmax(0, 1fr)) !important;
    grid-auto-flow: dense !important;
    grid-auto-rows: 8px;
    gap: var(--masonry-gap) !important;
    align-items: start;
    opacity: 0;
    transition: opacity 320ms ease;
}

.masonry.is-hydrated { opacity: 1; }

.masonry-item {
    appearance: none;
    position: relative;
    display: block !important;
    width: 100%;
    min-width: 0;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    isolation: isolate;
    background: #ededed !important;
    border: 0;
    border-radius: 7px !important;
    cursor: zoom-in;
    touch-action: pan-y;
    contain: layout style paint;
    opacity: 0;
    transform: translateY(8px);
    transition:
        opacity 520ms ease var(--sequence-delay, 0ms),
        transform 520ms ease var(--sequence-delay, 0ms);
}

.masonry.is-hydrated .masonry-item {
    opacity: 1;
    transform: translateY(0);
}

.masonry-item::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 0;
    background: linear-gradient(105deg, #ececec 20%, #f8f8f8 38%, #ececec 56%);
    background-size: 220% 100%;
    opacity: 1;
    animation: gallery-skeleton 1.4s ease-in-out infinite;
    transition: opacity 500ms ease;
}

.masonry-item.is-ready::before {
    opacity: 0;
    pointer-events: none;
}

.gallery-image-stage {
    position: absolute;
    inset: 0;
    z-index: 1;
    overflow: hidden;
    background: #efefef;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.gallery-image-layer,
.modal-image-layer {
    display: block;
    position: absolute;
    inset: 0;
    width: 100% !important;
    height: 100% !important;
    margin: 0;
    opacity: 0;
    object-fit: cover;
    image-rendering: auto;
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    -webkit-touch-callout: none;
}

.gallery-image-layer {
    transform: scale(1.012);
    filter: saturate(0.98);
    will-change: opacity, transform;
    transition:
        opacity 2s cubic-bezier(0.22, 1, 0.36, 1),
        transform 2s cubic-bezier(0.22, 1, 0.36, 1),
        filter 2s ease;
}

.modal-image-layer {
    object-fit: contain;
    transform: none;
    filter: none;
    will-change: auto;
    transition: opacity 180ms ease;
}

.gallery-image-layer.is-active,
.modal-image-layer.is-active {
    opacity: 1;
}

.gallery-image-layer.is-active {
    transform: scale(1);
    filter: saturate(1);
}

.masonry-item:focus-visible {
    outline: 2px solid #111 !important;
    outline-offset: 2px;
}

.carousel-dots {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

.gallery-carousel-dots {
    position: absolute;
    right: 8px;
    bottom: 7px;
    left: 8px;
    z-index: 4;
    min-height: 20px;
    padding: 4px 7px;
    overflow: hidden;
    pointer-events: auto;
}

.carousel-dot {
    appearance: none;
    width: 5px;
    height: 5px;
    flex: 0 0 5px;
    padding: 0;
    background: rgba(255, 255, 255, 0.58);
    border: 0;
    border-radius: 999px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.32);
    cursor: pointer;
    transition: width 320ms ease, background 320ms ease, transform 320ms ease;
}

.carousel-dot[aria-current="true"] {
    width: 15px;
    background: #fff;
}

.carousel-dot:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

.carousel-count {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 4;
    display: grid;
    min-width: 32px;
    height: 23px;
    padding: 0 7px;
    place-items: center;
    color: #fff;
    background: rgba(20, 22, 27, 0.58);
    border: 1px solid rgba(255, 255, 255, 0.34);
    border-radius: 999px;
    font-size: 10px;
    font-variant-numeric: tabular-nums;
    font-weight: 700;
    line-height: 1;
    pointer-events: none;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

#modal-content { position: relative; }

#modal-stage {
    position: relative;
    flex: 0 0 auto;
    max-width: 90vw;
    max-height: calc(100vh - 210px);
    overflow: hidden;
    background: rgba(235, 238, 243, 0.72);
    border-radius: 16px;
    box-shadow: 0 12px 35px rgba(17, 24, 39, 0.15);
    touch-action: pan-y;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.modal-carousel-count { display: none !important; }

.modal-carousel-dots {
    max-width: min(82vw, 720px);
    min-height: 24px;
    padding: 4px 10px;
    overflow-x: auto;
    scrollbar-width: none;
}

.modal-carousel-dots::-webkit-scrollbar { display: none; }

.modal-carousel-dots .carousel-dot {
    background: rgba(38, 43, 52, 0.28);
    box-shadow: none;
}

.modal-carousel-dots .carousel-dot[aria-current="true"] { background: rgba(26, 29, 35, 0.82); }
.modal-carousel-dots .carousel-dot:focus-visible { outline-color: #111; }

.gallery-error {
    grid-column: 1 / -1;
    margin: 40px auto;
    color: #555;
    text-align: center;
}

@keyframes gallery-skeleton {
    0% { background-position: 100% 0; }
    100% { background-position: -120% 0; }
}

@media (max-width: 620px) {
    .gallery-container { padding: 6px 6px 20px !important; }
    .masonry-item {
        grid-column-end: span 1 !important;
        border-radius: 5px !important;
    }
    #modal-stage {
        max-width: 100%;
        max-height: calc(100vh - 190px);
        border-radius: 13px;
    }
    .gallery-carousel-dots {
        right: 4px;
        bottom: 3px;
        left: 4px;
        gap: 4px;
        padding: 3px 4px;
    }
    .carousel-count {
        top: 5px;
        right: 5px;
        min-width: 29px;
        height: 21px;
        padding: 0 6px;
        font-size: 9px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .masonry,
    .masonry-item,
    .masonry-item::before,
    .gallery-image-layer,
    .modal-image-layer,
    .carousel-dot {
        animation: none !important;
        transition: none !important;
    }
}
