/*
 * Justified-rows image gallery + lightbox.
 * Markup is produced by ui_gallery() in function/fnc_ui.php, behaviour is in js/components/image-gallery.js.
 * The rows are laid out by CSS alone (flex-grow = aspect ratio), so there is no layout jump and it still
 * works – every tile is a normal link to the full image – when JavaScript is off.
 *
 *   --ui-gallery-row     target row height  (set inline, e.g. 180px)
 *   --ui-gallery-gap     space between tiles (default 6px)
 *   --ui-gallery-radius  tile corner radius  (default 12px)
 */
.ui-gallery {
    --ui-gallery-row: 180px;
    --ui-gallery-gap: 6px;
    --ui-gallery-radius: 12px;
    display: flex;
    flex-wrap: wrap;
    gap: var(--ui-gallery-gap);
}

/* keeps the last (short) row from being stretched to full width */
.ui-gallery::after {
    content: "";
    flex-grow: 1000000;
}

.ui-gallery__item {
    position: relative;
    display: block;
    overflow: hidden;
    /* --r is the image's width / height */
    flex: calc(var(--r, 1.5) * 100) 1 calc(var(--r, 1.5) * var(--ui-gallery-row));
    min-width: 0;
    border-radius: var(--ui-gallery-radius);
    background: #dfe3e8;
    cursor: zoom-in;
}

/* reserves the right amount of height before the image has loaded */
.ui-gallery__item::before {
    content: "";
    display: block;
    padding-bottom: calc(100% / var(--r, 1.5));
}

.ui-gallery__item img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform .35s ease;
}

/* hover veil + magnifier */
.ui-gallery__item::after {
    content: "";
    position: absolute;
    inset: 0;
    background:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='34' height='34' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cpath d='M15.5 15.5 21 21M10.5 7.5v6M7.5 10.5h6'/%3E%3C/svg%3E") center / 34px no-repeat,
        rgba(0, 0, 0, .45);
    opacity: 0;
    transition: opacity .25s ease;
}

.ui-gallery__item:hover::after,
.ui-gallery__item:focus-visible::after { opacity: 1; }

.ui-gallery__item:hover img { transform: scale(1.04); }

.ui-gallery__item:focus-visible {
    outline: 3px solid #1786f1;
    outline-offset: 2px;
}

@media (hover: none) {
    .ui-gallery__item::after { display: none; }
}

/* ------------------------------------------------------------------ lightbox */
html.ui-lightbox-open { overflow: hidden; }

.ui-lightbox {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, .86);
    opacity: 0;
    transition: opacity .2s ease;
}

.ui-lightbox[hidden] { display: none; }
.ui-lightbox.is-open { opacity: 1; }

.ui-lightbox__stage {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 56px 72px;
    touch-action: pan-y;
}

.ui-lightbox__img {
    display: block;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 6px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, .5);
    opacity: 0;
    transition: opacity .2s ease;
    user-select: none;
    -webkit-user-drag: none;
}

.ui-lightbox.is-loaded .ui-lightbox__img { opacity: 1; }

.ui-lightbox__btn {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 46px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, .14);
    color: #fff;
    cursor: pointer;
    transition: background-color .2s ease;
}

.ui-lightbox__btn:hover { background: rgba(255, 255, 255, .28); }

.ui-lightbox__btn:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 2px;
}

.ui-lightbox__close { top: 12px; right: 12px; }
.ui-lightbox__prev,
.ui-lightbox__next { top: 50%; margin-top: -23px; }
.ui-lightbox__prev { left: 12px; }
.ui-lightbox__next { right: 12px; }

.ui-lightbox__close::before,
.ui-lightbox__close::after {
    content: "";
    position: absolute;
    width: 20px;
    height: 2px;
    border-radius: 2px;
    background: currentColor;
    transform: rotate(45deg);
}

.ui-lightbox__close::after { transform: rotate(-45deg); }

.ui-lightbox__prev::before,
.ui-lightbox__next::before {
    content: "";
    width: 11px;
    height: 11px;
    border-top: 3px solid currentColor;
    border-right: 3px solid currentColor;
}

.ui-lightbox__prev::before { transform: translateX(2px) rotate(-135deg); }
.ui-lightbox__next::before { transform: translateX(-2px) rotate(45deg); }

.ui-lightbox__count {
    position: absolute;
    top: 22px;
    left: 20px;
    color: #fff;
    font-size: 15px;
    letter-spacing: .5px;
    text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
}

.ui-lightbox__spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 38px;
    height: 38px;
    margin: -19px 0 0 -19px;
    border: 3px solid rgba(255, 255, 255, .3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: ui-lightbox-spin .8s linear infinite;
}

.ui-lightbox.is-loaded .ui-lightbox__spinner { display: none; }

@keyframes ui-lightbox-spin { to { transform: rotate(360deg); } }

@media (max-width: 767.98px) {
    .ui-lightbox__stage { padding: 56px 8px; }
    .ui-lightbox__prev { left: 6px; }
    .ui-lightbox__next { right: 6px; }
}

@media (prefers-reduced-motion: reduce) {
    .ui-lightbox,
    .ui-lightbox__img,
    .ui-gallery__item img,
    .ui-gallery__item::after { transition: none; }
    .ui-lightbox__spinner { animation-duration: 2s; }
}

/* fixed-column variant: ui_gallery(..., ['columns' => 4]) – equal square-ish tiles, 2 columns on phones */
.ui-gallery--grid {
    display: grid;
    grid-template-columns: repeat(var(--ui-gallery-cols, 4), minmax(0, 1fr));
}
.ui-gallery--grid::after { content: none; }
.ui-gallery--grid .ui-gallery__item { flex: none; aspect-ratio: 3 / 4; }
.ui-gallery--grid .ui-gallery__item::before { content: none; }
@media (max-width: 575.98px) {
    .ui-gallery--grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (min-width: 576px) and (max-width: 767.98px) {
    .ui-gallery--grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
