/* ==========================================================================
   Image viewer (.iv-modal)

   The modal is a shared framework component: one overlay for every
   `.iv-thumb` / `.iv-imgs` pair in the project. It is built from the base
   palette only (`rgb(var(--...))` variables), so it follows the theme of the
   project it is vendored into.
   ========================================================================== */

body.iv-scroll-locked {
    overflow: hidden;
}

.iv-modal.hidden {
    display: none;
}

.iv-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    inset: 0;
    z-index: 10000000000000;

    display: flex;
    justify-content: center;
    align-items: center;

    padding: 1.5em;
}

.iv-modal:focus {
    outline: none;
}

.iv-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    inset: 0;

    background: rgba(0, 0, 0, 0.82);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
}

.iv-container {
    position: relative;
    z-index: 1;

    display: flex;
    flex-direction: column;

    width: min(1200px, 100%);
    height: min(90dvh, 900px);
    max-height: 100%;

    background: rgb(var(--body-background));
    border-radius: 1em;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
    overflow: hidden;
}

/* Toolbar: counter, actions (top right), zoom controls, close */

.iv-toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.45em;

    padding: 0.5em 0.75em;

    background: rgb(var(--light-gray));
    border-bottom: 1px solid rgb(var(--neutral-gray));
}

.iv-toolbar-spacer {
    flex: 1 1 auto;
    min-width: 0;
}

.iv-counter {
    color: rgb(var(--text-color));
    font-weight: var(--bold);
    font-variant-numeric: tabular-nums;
    font-size: 0.95em;
    white-space: nowrap;
}

.iv-counter-separator {
    margin: 0 0.2em;
    opacity: 0.45;
}

/* Per-slide actions (rendered by the shared image viewer runtime) */

.iv-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.35em;

    margin: 0;
    max-width: 100%;
}

.iv-actions .button {
    margin: 0;
    padding: 0.5em 0.75em;
    font-size: 0.9em;
    white-space: nowrap;
}

.iv-zoom-controls {
    display: flex;
    align-items: center;
    gap: 0.25em;
}

.iv-zoom-controls button {
    display: inline-flex;
    align-items: center;
    justify-content: center;

    min-width: 2em;
    height: 2em;
    margin: 0;
    padding: 0 0.35em;

    border: 1px solid rgb(var(--neutral-gray));
    border-radius: 0.5em;

    background: rgb(var(--body-background));
    color: rgb(var(--text-color));

    font-size: 0.95em;
    font-weight: var(--bold);
    line-height: 1;

    outline: none;
    cursor: pointer;

    transition: background-color 0.2s ease, transform 0.2s ease;
}

.iv-zoom-controls button:hover {
    background: rgb(var(--bluey-gray));
    transform: translateY(-1px);
}

.iv-zoom-controls button:focus-visible {
    outline: 2px solid rgb(var(--primary-color));
    outline-offset: 2px;
}

.iv-zoom-reset {
    min-width: 3.2em;
    font-size: 0.75em;
}

.iv-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;

    width: 2.1em;
    height: 2.1em;
    margin: 0;
    padding: 0;

    border: none;
    border-radius: 999px;

    background-color: rgba(var(--primary-red), 0.85);
    color: rgb(var(--text-color-inv));

    font-size: 0.95rem;
    font-weight: var(--bold);
    line-height: 1;

    outline: none;
    cursor: pointer;

    transition: background-color 0.2s ease, transform 0.2s ease;
}

.iv-close:hover,
.iv-close:focus-visible {
    background-color: rgba(var(--primary-red), 1);
    transform: scale(1.05);
}

.iv-close:focus-visible {
    outline: 2px solid rgb(var(--primary-color));
    outline-offset: 2px;
}

/* Image area */

.iv-image-wrapper {
    position: relative;
    flex: 1 1 auto;

    width: 100%;
    min-height: 0;

    overflow: hidden;
    cursor: grab;

    /* The viewer owns every gesture on this surface (pan, pinch, swipe). */
    touch-action: none;

    background: rgb(var(--light-gray));
}

.iv-image-wrapper.grabbing {
    cursor: grabbing;
}

.iv-main-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    object-fit: contain;
    transform-origin: center center;
    will-change: transform;

    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;

    -webkit-user-drag: none;
    -moz-user-drag: none;
    -ms-user-drag: none;
    user-drag: none;

    touch-action: none;
}

.iv-image-wrapper.iv-image-error .iv-main-img {
    opacity: 0.15;
}

/* Description */

.iv-image-desc {
    position: absolute;
    bottom: 0.75em;
    left: 50%;
    transform: translateX(-50%);

    display: flex;
    flex-direction: column;
    align-items: flex-end;

    max-width: min(800px, 92%);
    padding: 0.5em 0.85em;
    border-radius: 0.6em;

    background-color: rgba(0, 0, 0, 0.55);

    font-size: 0.85em;
}

.iv-image-desc .readmore-text {
    width: unset;
    margin: 0 auto;
    color: rgb(var(--text-color-inv));
}

/* Thumbnails */

.iv-thumbnails {
    display: flex;
    gap: 0.4em;

    overflow-x: auto;
    padding: 0.6em 0.85em;

    background: rgb(var(--light-gray));
    border-top: 1px solid rgb(var(--neutral-gray));

    scrollbar-width: thin;
}

.iv-thumbnail {
    flex: 0 0 auto;

    padding: 0;
    margin: 0;
    border: 2px solid transparent;
    border-radius: 0.6em;
    overflow: hidden;

    background: none;
    line-height: 0;

    outline: none;
    cursor: pointer;

    transition: border-color 0.2s ease, transform 0.2s ease;
}

.iv-thumbnail img {
    display: block;
    height: 54px;
    width: auto;

    opacity: 0.6;
    transition: opacity 0.2s ease;
}

.iv-thumbnail:hover img {
    opacity: 1;
}

.iv-thumbnail.is-active {
    border-color: rgb(var(--primary-color));
}

.iv-thumbnail.is-active img {
    opacity: 1;
}

.iv-thumbnail:focus-visible {
    outline: 2px solid rgb(var(--primary-color));
    outline-offset: 2px;
}

/* Previous / next */

.iv-prev,
.iv-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 3;

    display: flex;
    align-items: center;
    justify-content: center;

    width: 2.1em;
    height: 2.1em;
    margin: 0;
    padding: 0;

    border: none;
    border-radius: 999px;

    background: rgba(255, 255, 255, 0.85);
    color: rgb(var(--text-color));

    font-size: 1.2rem;
    line-height: 1;

    outline: none;
    cursor: pointer;

    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.22);

    transition: background-color 0.2s ease, transform 0.2s ease;
}

.iv-prev:hover,
.iv-next:hover {
    background: rgb(var(--body-background));
    transform: translateY(-50%) scale(1.05);
}

.iv-prev:focus-visible,
.iv-next:focus-visible {
    outline: 2px solid rgb(var(--primary-color));
    outline-offset: 2px;
}

.iv-prev {
    left: 0.6em;
}

.iv-next {
    right: 0.6em;
}

/* The thumbnail itself (source of the viewer) */

.iv-thumb {
    position: relative;
    cursor: pointer;
}

.iv-thumb::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

    background-color: rgba(0, 0, 0, 0);
    transition: background-color 0.2s ease-in-out;
}

.iv-thumb:hover::after {
    background-color: rgba(0, 0, 0, 0.1);
}

/* The per-item action container: never visible in the source markup, the
   viewer clones it into its own action bar. */
.iv-item-actions {
    display: none !important;
}

/* Mobile */

@media only screen and (max-width: 800px) {
    /* Keep a margin around the modal: the visible backdrop is the tap
       target that closes it. */
    .iv-modal {
        padding: 2rem 1rem;
    }

    .iv-container {
        width: 100%;
        height: calc(100vh - 1rem);
        height: calc(100dvh - 1rem);
        max-height: 100%;
        border-radius: 0.9em;
    }

    .iv-toolbar {
        gap: 0.35rem;
        padding: 0.4rem 0.55rem;
    }

    .iv-counter {
        font-size: 0.85em;
    }

    /* Compact actions: the label is dropped when the button has an icon, so
       several buttons fit in the toolbar. */
    .iv-actions {
        gap: 0.25rem;
    }

    .iv-actions .button {
        padding: 0.4rem 0.5rem;
        font-size: 0.8em;
    }

    .iv-actions .button.with-icon {
        padding: 0.4rem 0.45rem;
    }

    .iv-actions .button.with-icon .button-label {
        display: none;
    }

    .iv-zoom-controls {
        gap: 0.2rem;
    }

    .iv-zoom-controls button {
        min-width: 1.85rem;
        height: 1.85rem;
        padding: 0 0.3rem;
        font-size: 0.85em;
    }

    .iv-zoom-reset {
        min-width: 2.8rem;
        font-size: 0.68em;
    }

    .iv-close {
        width: 1.9rem;
        height: 1.9rem;
        font-size: 0.85rem;
    }

    .iv-thumbnails {
        padding: 0.45rem 0.55rem;
    }

    .iv-thumbnail img {
        height: 40px;
    }

    .iv-prev,
    .iv-next {
        width: 1.9rem;
        height: 1.9rem;
        font-size: 1rem;
    }

    .iv-prev {
        left: 0.35rem;
    }

    .iv-next {
        right: 0.35rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    .iv-container,
    .iv-close,
    .iv-zoom-controls button,
    .iv-prev,
    .iv-next,
    .iv-thumbnail,
    .iv-thumbnail img,
    .iv-thumb::after {
        transition: none !important;
    }
}
