/* Lightbox extension for Sphinx — Pure CSS checkbox-toggle lightbox
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Architecture:
 *   <input class="lightbox-toggle">   (hidden checkbox)
 *   .lightbox-toggle:checked ~ .lightbox-overlay   (shows the overlay)
 *
 * Accessibility (WCAG 2.1 AA):
 *   - Visible focus indicators on trigger and close controls.
 *   - prefers-reduced-motion: disables transitions.
 *   - prefers-contrast: more: high-contrast borders and outlines.
 *   - role="dialog", aria-modal="true", and accessible control text in HTML.
 */

/* -----------------------------------------------------------------------
 * Utility — visually hidden text for label controls
 * ----------------------------------------------------------------------- */
.lightbox-visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* -----------------------------------------------------------------------
 * Hidden checkbox — drives the toggle state
 * ----------------------------------------------------------------------- */
.lightbox-toggle {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    pointer-events: none;
}

/* -----------------------------------------------------------------------
 * Container — wraps trigger + checkbox + overlay as siblings
 * ----------------------------------------------------------------------- */
.lightbox-container {
    display: block;
    position: relative;
    margin: 1em 0;
    overflow: hidden;
}

.lightbox-container.align-center {
    text-align: center;
}

.lightbox-container.align-left {
    text-align: left;
}

.lightbox-container.align-right {
    text-align: right;
}

/* -----------------------------------------------------------------------
 * Trigger — the thumbnail label the user clicks
 * ----------------------------------------------------------------------- */
.lightbox-trigger-label {
    display: inline-block;
    vertical-align: top;
    cursor: pointer;
}

.lightbox-trigger-label img.lightbox-trigger {
    max-width: 100%;
    height: auto;
    cursor: pointer;
    transition: opacity 0.15s ease;
}

.lightbox-trigger-label:hover img.lightbox-trigger {
    opacity: 0.85;
}

/* Focus indicator — visible outline on keyboard focus */
.lightbox-trigger-label:focus {
    outline: 3px solid #4A90D9;
    outline-offset: 3px;
    border-radius: 2px;
}

.lightbox-trigger-label:focus:not(:focus-visible) {
    outline: none;
}

.lightbox-trigger-label:focus-visible {
    outline: 3px solid #4A90D9;
    outline-offset: 3px;
    border-radius: 2px;
}

/* -----------------------------------------------------------------------
 * Overlay — hidden by default, shown when checkbox is :checked
 * ----------------------------------------------------------------------- */
.lightbox-overlay {
    display: none;
    position: fixed;
    z-index: 9999;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.85);
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    overflow-y: auto;
    padding: 2rem;
}

/* Toggle: show overlay when checkbox is checked */
.lightbox-toggle:checked ~ .lightbox-overlay {
    display: flex;
}

/* -----------------------------------------------------------------------
 * Overlay content — centres the image and caption
 * ----------------------------------------------------------------------- */
.lightbox-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    max-width: min(100%, 1200px);
    min-height: min-content;
    position: relative;
    z-index: 2;
    pointer-events: none; /* Let clicks pass through to backdrop */
}

.lightbox-content img {
    display: block;
    max-width: 100%;
    max-height: 72vh;
    object-fit: contain;
    height: auto;
    background-color: white;
    pointer-events: auto; /* Restore interaction for the image */
}

/* -----------------------------------------------------------------------
 * Caption
 * ----------------------------------------------------------------------- */
.lightbox-text {
    background: rgba(0, 0, 0, 0.92);
    border-radius: 4px;
    box-sizing: border-box;
    margin-top: 0.75em;
    max-width: min(80vw, 56rem);
    overflow-wrap: anywhere;
    padding: 0.5rem 0.875rem;
    pointer-events: auto; /* Restore interaction for the overlay text */
    text-align: center;
}

.lightbox-caption {
    color: #fff;
    font-weight: bold;
    margin: 0;
}

.lightbox-legend {
    color: #f4f4f4;
    margin: 0;
}

.lightbox-caption + .lightbox-legend {
    margin-top: 0.35em;
}

/* -----------------------------------------------------------------------
 * Gallery controls
 * ----------------------------------------------------------------------- */
.lightbox-gallery-control {
    position: fixed;
    top: 50%;
    z-index: 3;
    transform: translateY(-50%);
    width: 3rem;
    height: 3rem;
    border: 1px solid rgba(255, 255, 255, 0.7);
    border-radius: 4px;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    font-size: 2.25rem;
    line-height: 1;
    cursor: pointer;
}

.lightbox-gallery-prev {
    left: 0.75rem;
}

.lightbox-gallery-next {
    right: 0.75rem;
}

.lightbox-gallery-control:hover {
    background: rgba(0, 0, 0, 0.9);
}

.lightbox-gallery-control:focus {
    outline: 3px solid #4A90D9;
    outline-offset: 2px;
}

.lightbox-gallery-control:focus:not(:focus-visible) {
    outline: none;
}

.lightbox-gallery-control:focus-visible {
    outline: 3px solid #4A90D9;
    outline-offset: 2px;
}

/* -----------------------------------------------------------------------
 * Close button — positioned top-right over the overlay
 * ----------------------------------------------------------------------- */
.lightbox-close {
    position: fixed;
    top: 0.5em;
    right: 0.75em;
    z-index: 3;
    font-size: 2.5em;
    line-height: 1;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    border: none;
    border-radius: 4px;
    padding: 0.1em 0.35em;
    cursor: pointer;
    text-decoration: none;
}

.lightbox-close:hover {
    background: rgba(0, 0, 0, 0.9);
}

/* Focus indicator on close button */
.lightbox-close:focus {
    outline: 3px solid #4A90D9;
    outline-offset: 2px;
}

.lightbox-close:focus:not(:focus-visible) {
    outline: none;
}

.lightbox-close:focus-visible {
    outline: 3px solid #4A90D9;
    outline-offset: 2px;
}

/* -----------------------------------------------------------------------
 * Backdrop close — invisible label covering the backdrop area
 * ----------------------------------------------------------------------- */
.lightbox-backdrop-close {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 1;
    cursor: default;
}

/* -----------------------------------------------------------------------
 * Utility classes — optional styling for thumbnails and overlay images
 * ----------------------------------------------------------------------- */
.with-shadow {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
}

.with-border {
    border: 1px solid rgba(0, 0, 0, 0.25);
    border-radius: 4px;
}

/* -----------------------------------------------------------------------
 * Accessibility: prefers-reduced-motion
 * ----------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
    .lightbox-trigger-label img.lightbox-trigger {
        transition: none;
    }

    .lightbox-overlay {
        transition: none;
    }
}

/* -----------------------------------------------------------------------
 * Accessibility: prefers-contrast (high contrast mode)
 * ----------------------------------------------------------------------- */
@media (prefers-contrast: more) {
    .lightbox-trigger-label:focus,
    .lightbox-trigger-label:focus-visible {
        outline: 3px solid #000;
        outline-offset: 3px;
    }

    .lightbox-close:focus,
    .lightbox-gallery-control:focus,
    .lightbox-gallery-control:focus-visible,
    .lightbox-close:focus-visible {
        outline: 3px solid #fff;
        outline-offset: 2px;
    }

    .lightbox-close,
    .lightbox-gallery-control {
        background: #000;
        border: 2px solid #fff;
    }

    .lightbox-overlay {
        background-color: rgba(0, 0, 0, 0.95);
    }

    .lightbox-content img {
        border: 2px solid #fff;
    }

    .lightbox-text {
        background: #000;
        border: 2px solid #fff;
        text-shadow: none;
    }
}

@media (max-width: 44rem), (max-height: 34rem) {
    .lightbox-overlay {
        align-items: flex-start;
        padding: 3.75rem 1rem 1rem;
    }

    .lightbox-content {
        justify-content: flex-start;
        width: 100%;
    }

    .lightbox-content img {
        max-height: 58vh;
    }

    .lightbox-text {
        max-width: 100%;
        padding: 0.5rem 0.75rem;
    }

    .lightbox-gallery-control {
        top: auto;
        bottom: 0.75rem;
        transform: none;
        width: 2.75rem;
        height: 2.75rem;
        font-size: 2rem;
    }
}

/* -----------------------------------------------------------------------
 * Hidden helper image — used only to trigger Sphinx's ImageCollector.
 * Must not be visible in the rendered page.
 * ----------------------------------------------------------------------- */
img.lightbox-hidden {
    display: none !important;
    width: 0;
    height: 0;
    position: absolute;
    overflow: hidden;
}
