/**
 * Modern full-screen photo viewer (lightbox)
 * Touch swipe, keyboard nav, smooth controls, responsive.
 */

#photo-viewer-overlay {
    display: none;
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: rgba(0, 0, 0, 0.92);
    z-index: 10000;
    opacity: 0;
    transition: opacity 0.35s ease;
}

#photo-viewer-overlay.open {
    display: block;
    opacity: 1;
}

#photo-viewer-overlay.is-zoomed {
    background: #000;
}

/* Main image stage */
.photo-viewer-stage {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    /* Prevent iOS Safari from fighting the swipe for native scroll/zoom-back. */
    touch-action: none;
}

.photo-viewer-stage img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease, opacity 0.3s ease;
    user-select: none;
    -webkit-user-drag: none;
}

.photo-viewer-stage img.is-sliding {
    transition: transform 0.25s ease-out, opacity 0.2s ease;
}

/* Peek neighbor images (swipe preview, like Instagram/TikTok).
   4 sibling <img> sit behind the active stageImg and slide in
   from the swipe direction with growing opacity. */
.photo-viewer-stage img.photo-viewer-peek {
    position: absolute;
    inset: 0;
    margin: auto;          /* match object-fit: contain centering */
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    opacity: 0;
    pointer-events: none;  /* must not steal touch from stageImg */
    user-select: none;
    -webkit-user-drag: none;
    z-index: 1;
}

/* Active stageImg is the LAST <img> in .photo-viewer-stage; raise it
   above the peek cards so the current photo always wins z-fight. */
.photo-viewer-stage > img:not(.photo-viewer-peek) {
    position: relative;
    z-index: 2;
}

/* Toolbar (top) */
.photo-viewer-toolbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    background: linear-gradient(to bottom, rgba(0,0,0,0.55) 0%, rgba(0,0,0,0) 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 10002;
    pointer-events: none;
}

#photo-viewer-overlay:hover .photo-viewer-toolbar,
#photo-viewer-overlay.show-ui .photo-viewer-toolbar {
    opacity: 1;
    pointer-events: auto;
}

.photo-viewer-title {
    color: #fff;
    font-size: 14px;
    text-shadow: 0 1px 3px rgba(0,0,0,0.6);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60vw;
}

.photo-viewer-counter {
    color: rgba(255,255,255,0.8);
    font-size: 12px;
    margin-left: 8px;
}

/* Controls */
.photo-viewer-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255,255,255,0.15);
    color: #fff;
    border: none;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    transition: background 0.2s, transform 0.2s;
    text-decoration: none;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.photo-viewer-btn:hover {
    background: rgba(255,255,255,0.3);
    transform: scale(1.05);
}

.photo-viewer-btn:active {
    transform: scale(0.95);
}

/* Prev / Next arrows */
.photo-viewer-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10001;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

#photo-viewer-overlay:hover .photo-viewer-nav,
#photo-viewer-overlay.show-ui .photo-viewer-nav {
    opacity: 1;
    pointer-events: auto;
}

.photo-viewer-nav.prev { left: 12px; }
.photo-viewer-nav.next { right: 12px; }

.photo-viewer-nav .photo-viewer-btn {
    width: 48px;
    height: 48px;
    font-size: 24px;
}

/* Bottom bar (thumbnails + actions) */
.photo-viewer-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 10002;
    pointer-events: none;
    padding: 16px;
}

#photo-viewer-overlay:hover .photo-viewer-bottom,
#photo-viewer-overlay.show-ui .photo-viewer-bottom {
    opacity: 1;
    pointer-events: auto;
}

.photo-viewer-thumbs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 6px;
    scrollbar-width: none;
    /* Stage sets touch-action: none; allow horizontal pan on the thumb strip. */
    touch-action: pan-x;
}

.photo-viewer-thumbs::-webkit-scrollbar {
    display: none;
}

.photo-viewer-thumbs img {
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity 0.2s, transform 0.2s;
    border: 2px solid transparent;
    flex-shrink: 0;
}

.photo-viewer-thumbs img.active {
    opacity: 1;
    border-color: #fff;
    transform: scale(1.08);
}

.photo-viewer-thumbs img:hover {
    opacity: 0.85;
}

/* Rating / marks bar */
.photo-viewer-rating {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    flex-wrap: wrap;
}

.photo-viewer-rating .mark-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255,255,255,0.15);
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.2s;
    padding: 0;
}

.photo-viewer-rating .mark-btn:hover,
.photo-viewer-rating .mark-btn.active {
    background: rgba(255,255,255,0.4);
    transform: scale(1.1);
}

.photo-viewer-rating .mark-btn img {
    width: 22px;
    height: 22px;
}

/* Swipe hint for mobile */
.photo-viewer-swipe-hint {
    display: none;
    position: absolute;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255,255,255,0.5);
    font-size: 12px;
    pointer-events: none;
    animation: swipeHintPulse 2s infinite;
}

@media (max-width: 767px) {
    .photo-viewer-swipe-hint {
        display: block;
    }
    .photo-viewer-nav {
        display: none;
    }
    .photo-viewer-toolbar {
        padding: 0 8px;
        height: 48px;
    }
    .photo-viewer-title {
        max-width: 50vw;
        font-size: 12px;
    }
    .photo-viewer-thumbs img {
        width: 40px;
        height: 40px;
    }
    /* GPU-accelerate peek images for smooth TikTok-style swipe on mobile. */
    .photo-viewer-stage img.photo-viewer-peek {
        will-change: transform, opacity;
    }
}

@keyframes swipeHintPulse {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 0.7; }
}

/* Inline photo view (fallback when viewer is closed) */
.photo-inline-view {
    position: relative;
    display: block;
    width: 100%;
    max-width: 100%;
    margin: 0;
}

.photo-inline-stage {
    position: relative;
    display: block;
    width: 100%;
    max-width: 100%;
}

.photo-inline-stage .photo-inline-img {
    display: block;
    width: auto;
    max-width: 100%;
    height: auto;
    max-height: 80vh;
    margin: 0 auto;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
    object-fit: contain;
}

.photo-inline-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.4);
    color: #fff;
    text-decoration: none;
    transition: background 0.2s, transform 0.15s;
    z-index: 2;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.photo-inline-nav svg {
    width: 18px;
    height: 18px;
}

.photo-inline-nav:hover {
    background: rgba(0, 0, 0, 0.65);
    transform: translateY(-50%) scale(1.08);
}
.photo-inline-nav:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
    background: rgba(0, 0, 0, 0.65);
}

.photo-inline-nav.prev { left: 12px; }
.photo-inline-nav.next { right: 12px; }

.photo-viewer-open-wrap {
    margin-top: 16px;
    text-align: center;
}

.photo-viewer-open-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 999px;
    background: var(--c-primary, #4a90d9);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    transition: background 0.2s, transform 0.15s, box-shadow 0.2s;
    box-shadow: 0 2px 8px rgba(74, 144, 217, 0.25);
    cursor: pointer;
}

.photo-viewer-open-btn svg {
    width: 16px;
    height: 16px;
}

.photo-viewer-open-btn:hover {
    background: var(--c-primary-hover, #3a7bc8);
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(74, 144, 217, 0.35);
}
.photo-viewer-open-btn:focus-visible {
    outline: 2px solid #4a90d9;
    outline-offset: 2px;
    background: var(--c-primary-hover, #3a7bc8);
    box-shadow: 0 4px 14px rgba(74, 144, 217, 0.35);
}

.photo-viewer-open-btn:active {
    transform: translateY(0);
}

/* Marks bar */
.photo-marks-bar {
    margin-top: 16px;
    text-align: center;
}

.photo-marks-bar a {
    display: inline-block;
    margin: 2px;
    transition: transform 0.2s;
}

.photo-marks-bar a:hover {
    transform: scale(1.15);
}

@media (max-width: 600px) {
    .photo-inline-nav {
        width: 36px;
        height: 36px;
    }
    .photo-inline-nav svg {
        width: 16px;
        height: 16px;
    }
    .photo-inline-nav.prev { left: 8px; }
    .photo-inline-nav.next { right: 8px; }
    .photo-viewer-open-btn {
        padding: 8px 16px;
        font-size: 13px;
    }
}

/* Loading spinner */
.photo-viewer-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255,255,255,0.2);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    display: none;
}

.photo-viewer-spinner.visible {
    display: block;
}

@keyframes spin {
    to { transform: translate(-50%, -50%) rotate(360deg); }
}
