/* Preserve the existing gallery geometry; navigation controls stay out of its flex layout. */
.image-modal {
    --gallery-ease-out: cubic-bezier(0.23, 1, 0.32, 1);
    --gallery-arrow-width: clamp(44px, 6vw, 120px);
    --gallery-arrow-gap: clamp(12px, 4vw, 80px);
    position: fixed;
    inset: 0;
    z-index: 999999999;
    box-sizing: border-box;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    padding: 0;
    background: rgb(20, 20, 20);
    color: rgb(150, 150, 150);
    overflow-y: auto;
    overscroll-behavior: contain;
}
.image-modal[hidden] { display: none; }
.image-modal.active {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2vw;
}
.image-modal button {
    box-sizing: border-box;
    appearance: none;
    border: 0;
    border-radius: 0;
    padding: 0;
    background: transparent;
    color: inherit;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.image-modal button:focus-visible {
    outline: 2px solid #f5f5f5;
    outline-offset: -3px;
}
/* Keep the original counter alignment and zero-height desktop menu. */
.image-modal-menu {
    display: flex;
    align-content: end;
    justify-content: end;
    flex-wrap: wrap;
    flex: 0 0 auto;
    position: relative;
    width: 80vw;
    height: 0;
    margin: 0;
    overflow: visible;
}
.gallery-counter { margin: 0; font-size: 16px; line-height: 1; }
.image-modal .image-modal-exit-button {
    position: absolute;
    z-index: 2;
    /* Enlarge the target around the original icon position, keeping it inside the viewport. */
    right: max(0px, calc(4vh - max(0px, (44px - 3vh) / 2)));
    top: max(0px, calc(4vh - max(0px, (44px - 3vh) / 2)));
    width: max(44px, 3vh);
    height: max(44px, 3vh);
    margin: 0;
    display: grid;
    place-items: center;
}
.image-modal-exit-button img { width: 3vh; height: 3vh; }
.nested-modal-image-container {
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: center;
    flex: 0 0 auto;
    width: 80vw;
    height: min(90vh, 45.35vw);
    max-height: 700px;
    margin: 0 auto;
    gap: 2vw;
}
/* The stage occupies exactly the space previously occupied by the main image. */
.gallery-main-stage {
    position: relative;
    flex: 0 0 auto;
    height: 100%;
    aspect-ratio: 1.5 / 1;
    min-width: 0;
    overflow: hidden;
    touch-action: pan-y pinch-zoom;
}
.image-modal.is-zoomed .gallery-main-stage { touch-action: auto; }
.modal-image-main {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    object-fit: scale-down;
    aspect-ratio: 1.5 / 1;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    transition: opacity 140ms var(--gallery-ease-out);
}
.modal-image-main.is-changing { opacity: 0; }
.gallery-image-error { position: absolute; inset: 35% 24px auto; margin: 0; color: #ccc; font-size: 14px; text-align: center; }
.gallery-image-error[hidden] { display: none; }
.nested-modal-small-image-container {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    height: 100%;
    max-height: 700px;
    min-width: 0;
    min-height: 0;
    gap: 1.2vw;
    overflow-x: hidden;
    overflow-y: scroll;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
}
.nested-modal-small-image-container::-webkit-scrollbar { width: 8px; height: 6px; background: transparent; }
.nested-modal-small-image-container::-webkit-scrollbar-thumb { background: rgb(255 255 255 / .2); border-radius: 4px; }
.image-modal .gallery-thumbnail {
    position: relative;
    flex: 0 0 auto;
    width: 100%;
    min-height: 44px;
    aspect-ratio: 1.5 / 1;
    transition: opacity 140ms ease;
}
.modal-image-small {
    box-sizing: border-box;
    display: block;
    width: 100%;
    height: 100%;
    padding: 0 2px;
    object-fit: scale-down;
}
.gallery-thumbnail.selected .modal-image-small { outline: 2px solid rgb(225, 225, 225); outline-offset: -2px; }
/* Absolute side rails add large hit areas without resizing the photo or thumbnail rail. */
.image-modal .gallery-nav {
    position: absolute;
    top: 0;
    bottom: 0;
    display: grid;
    place-items: center;
    width: var(--gallery-arrow-width);
    min-width: 44px;
    min-height: 44px;
    color: #ededed;
    isolation: isolate;
}
.gallery-nav-prev { right: calc(100% + var(--gallery-arrow-gap)); }
.gallery-nav-next { left: calc(100% + var(--gallery-arrow-gap)); }
.gallery-nav::before {
    content: '';
    position: absolute;
    inset: 0;
    background: #fff;
    opacity: 0;
    z-index: -1;
    transition: opacity 140ms ease;
}
.gallery-nav svg {
    width: 20px;
    height: 28px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.2;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: .65;
    transition: opacity 140ms ease, transform 140ms var(--gallery-ease-out);
}
.gallery-nav:active::before { opacity: .1; }
.gallery-nav:active svg { transform: scale(.94); opacity: 1; }
/* Keyboard focus uses a soft fill and brighter arrow, with no border or focus ring. */
.image-modal .gallery-nav:focus-visible { outline: none; }
.image-modal:not([data-navigation-input="arrow"]) .gallery-nav:focus-visible::before { opacity: .08; }
.gallery-nav:focus-visible svg { opacity: 1; }
.image-modal[data-single-image="true"] .gallery-nav { visibility: hidden; }
@media (hover: hover) and (pointer: fine) {
    .gallery-nav:hover::before { opacity: .045; }
    .gallery-nav:hover svg { opacity: 1; }
    .gallery-nav-prev:hover svg { transform: translateX(-2px); }
    .gallery-nav-next:hover svg { transform: translateX(2px); }
    .gallery-thumbnail:hover:not(.selected) { opacity: .8; }
}
@media screen and (min-width: 801px) and (max-height: 500px) {
    /* Keep the short viewport's close target clear without moving gallery content. */
    .image-modal .gallery-nav-next { top: 48px; }
}
@media screen and (max-width: 800px) {
    .image-modal.active {
        justify-content: flex-start;
        padding-top: min(80px, 15vw);
        /* Reserve only the space needed by the controls below the existing thumbnails. */
        padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
    }
    .image-modal-menu { justify-content: center; width: 100%; height: 3vh; margin: 0 !important; }
    .nested-modal-image-container {
        width: 100%;
        height: fit-content;
        max-height: none;
        max-width: 100vw;
        flex-direction: column;
    }
    .gallery-main-stage { width: 100%; height: auto; flex: none; }
    .nested-modal-small-image-container {
        width: 100%;
        height: max(44px, 12vw);
        flex: none;
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-gutter: auto;
        touch-action: pan-x pinch-zoom;
    }
    .image-modal .gallery-thumbnail { width: auto; height: 100%; min-width: 66px; }
    .image-modal .gallery-nav {
        top: calc(100% + 20px);
        bottom: auto;
        width: 44%;
        height: 44px;
    }
    .gallery-nav-prev { left: 0; right: auto; }
    .gallery-nav-next { left: auto; right: 0; }
    .image-modal .image-modal-exit-button {
        /* Match the reference icon inset while retaining a full-size touch target. */
        right: calc(15px + env(safe-area-inset-right, 0px));
        top: calc(20px + env(safe-area-inset-top, 0px));
        width: 44px;
        height: 44px;
    }
    .image-modal .image-modal-exit-button img { width: 24px; height: 24px; }
}
@media (hover: none) { .gallery-nav svg { opacity: .9; } }
@media (prefers-reduced-motion: reduce) {
    .image-modal, .image-modal *, .image-modal *::before { transition: none !important; }
}
