/* Lightbox & Image Tagging Styles */

#lightbox {
    position: fixed;
    z-index: 2000;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.3);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 0.3s ease;
}

#lightbox-content {
    display: flex;
    flex-direction: row;
    width: 95%;
    height: 90%;
    max-width: 1300px;
    max-height: 90vh;
    background-color: rgba(255, 255, 255, 0.4);
    -webkit-backdrop-filter: blur(25px);
    backdrop-filter: blur(25px);
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
}

#lightbox-viewer {
    flex: 1;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 0;
    min-height: 0;
    overflow: visible; /* Permette ai tasti di uscire */
    background: rgba(255, 255, 255, 0.05);
    padding: 16px 40px; /* Mantiene spazio per le frecce senza sprecare troppa larghezza */
}

#lightbox-image-area {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 0;
}

#lightbox-img {
    display: block;
    object-fit: contain;
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    cursor: zoom-in;
    -webkit-user-select: none;
    user-select: none;
}

/* Specifico per immagini: mantengono proporzioni naturali */
img#lightbox-img {
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    max-height: none !important;
    object-fit: contain;
}

/* Specifico per modelli 3D: devono occupare tutto lo spazio */
model-viewer#lightbox-img {
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    max-height: none !important;
}

#lightbox-canvas {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
}

/* Desktop only: contain zoomed image within canvas, prevent leak behind sidebar */
@media (min-width: 901px) {
    #lightbox-canvas.is-zoomed {
        overflow: hidden;
    }
    #lightbox-canvas.is-zoomed .image-tag-wrapper,
    #lightbox-canvas.is-zoomed img#lightbox-img {
        cursor: grab;
    }
    #lightbox-canvas.is-zoomed.dragging .image-tag-wrapper,
    #lightbox-canvas.is-zoomed.dragging img#lightbox-img {
        cursor: grabbing;
    }
}

model-viewer {
    width: 100%;
    height: 100%;
    background-color: transparent;
    --poster-color: transparent;
    display: block;
}

/* --- LIGHTBOX CONTROLS --- */
#lightbox-close {
    position: fixed;
    top: 25px;
    right: 25px;
    width: 44px;
    height: 44px;
    background: rgba(15, 23, 42, 0.6);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2100;
    transition: all 0.2s ease, opacity 0.3s ease;
}

#lightbox-close:hover {
    background: rgba(239, 68, 68, 0.8);
    transform: scale(1.1);
}

#lightbox-prev, #lightbox-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 44px;
    background: rgba(0, 0, 0, 0.5); /* Più sobrio */
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2050;
    transition: all 0.2s ease;
    font-size: 20px;
    -webkit-user-select: none;
    user-select: none;
}

#lightbox-prev { left: 15px; } /* Bordi del viewer, non dell'immagine */
#lightbox-next { right: 15px; }

#lightbox-prev:hover, #lightbox-next:hover {
    background: rgba(0, 0, 0, 0.85);
    transform: translateY(-50%) scale(1.1);
}

/* Matitine dei tag - nascoste di default, visibili solo on hover */
.tag-inline-btn {
    opacity: 0 !important; /* Forza l'invisibilità quando non attiva */
    position: absolute;
    background: rgba(15, 23, 42, 0.8) !important;
    color: white !important;
    border-radius: 50% !important;
    width: 28px !important;
    height: 28px !important;
    min-width: 28px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    cursor: pointer !important;
    z-index: 135 !important;
    transition: opacity 0.2s ease, transform 0.2s ease !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    padding: 0 !important;
}

/* Hide scrollbars globally for lightbox elements while keeping function */
#lightbox-sidebar, 
#desc-text {
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}
#lightbox-sidebar::-webkit-scrollbar, 
#desc-text::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

.tag-bounding-box .tag-inline-btn.tag-edit-btn {
    display: none !important;
}

#lightbox.edit-tags-mode .tag-bounding-box.editing .tag-inline-btn.tag-edit-btn,
#lightbox.edit-tags-mode .tag-bounding-box:hover .tag-inline-btn.tag-edit-btn {
    display: flex !important;
    opacity: 1 !important;
}

.tag-action-container .tag-inline-btn {
    opacity: 1 !important;
}

.tag-edit-btn {
    top: -14px;
    right: -14px;
}

/* Maniglia di ridimensionamento */
.tag-resize-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: #fff;
    border: 1.5px solid #000;
    cursor: nwse-resize;
    z-index: 140;
    border-radius: 2px;
    box-shadow: 0 0 4px rgba(0,0,0,0.3);
}

/* Classi di posizionamento per gli handle */
.tag-resize-handle.br {
    bottom: -6px;
    right: -6px;
}

.tag-resize-handle.hidden {
    display: none !important;
}

/* Contenitore pulsanti azione (Salva/Annulla) */
.tag-action-container {
    position: absolute;
    bottom: -36px;
    left: 50%;
    transform: translateX(-50%);
    display: flex !important;
    gap: 8px !important;
    z-index: 150 !important;
    white-space: nowrap;
    padding: 2px 4px;
    background: rgba(15, 23, 42, 0.4);
    border-radius: 20px;
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    pointer-events: auto !important;
}

.tag-action-container.hidden {
    display: none !important;
}

.tag-save-btn {
    position: static !important;
    background: #10b981 !important; /* Verde smeraldo */
    color: white !important;
}

.tag-remove-btn {
    position: static !important;
    background: #ef4444 !important; /* Rosso */
    color: white !important;
}

.tag-inline-btn span {
    font-size: 18px !important;
    line-height: 1 !important;
    -webkit-user-select: none;
    user-select: none;
}

#lightbox-prev.hidden, #lightbox-next.hidden {
    display: none;
}

/* Image Tag Wrapper (Shrink-to-fit) */
.image-tag-wrapper {
    position: relative;
    display: inline-block;
    max-width: 100%;
    max-height: 100%;
}

.lightbox-desktop-media-frame {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: auto;
}

.image-tag-wrapper img {
    display: block;
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
}

/* --- TAGGING SYSTEM --- */
.tag-bounding-box {
    position: absolute;
    border: 1px solid transparent; /* Change to solid for consistency with staging */
    border-radius: 4px;
    z-index: 100;
    transition: background 0.2s, border-color 0.2s, box-shadow 0.2s;
    box-sizing: border-box;
}

.tag-bounding-box.active, .tag-bounding-box:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.85); /* Slightly brighter */
    box-shadow: 0 0 12px rgba(0, 0, 0, 0.4);
    z-index: 101;
}

/* PC: In non-edit mode, prioritize the zoom cursor even when hovering over tags */
@media (hover: hover) and (pointer: fine) {
    #lightbox:not(.edit-tags-mode) .tag-bounding-box {
        cursor: zoom-in !important;
    }
}

/* Mobile: when zoomed, tags must not block pan/zoom touch gestures */
@media (max-width: 900px) {
    #lightbox-canvas.zoomed .tag-bounding-box,
    #lightbox-canvas.is-zoomed .tag-bounding-box {
        pointer-events: none !important;
    }
}

/* Desktop: when zoomed, tags stay interactive for hover but don't block drag */
@media (min-width: 901px) {
    #lightbox-canvas.is-zoomed .tag-bounding-box {
        cursor: grab;
    }
    #lightbox-canvas.is-zoomed.dragging .tag-bounding-box {
        pointer-events: none !important;
    }
}

.tag-bounding-box.dragging, .tag-bounding-box.editing {
    border-color: rgba(255, 255, 255, 0.9);
    background: rgba(255, 255, 255, 0.1);
    z-index: 102;
    transition: none !important;
}

/* Nascondi il nome durante l'editing per evitare sovrapposizioni coi pulsanti salva/annulla (che prendono il suo posto) */
.tag-bounding-box.editing .photo-tag-label {
    opacity: 0 !important;
    display: none !important;
}

/* Design Premium per Tag e Rettangoli (Glassmorphism) */
.tag-rect-staging {
    border: 1px solid rgba(255, 255, 255, 0.8) !important;
    box-shadow: 0 0 12px rgba(0, 0, 0, 0.5) !important;
    background: rgba(255, 255, 255, 0.05) !important;
    border-radius: 4px;
    animation: rectPulse 2s infinite ease-in-out;
}

@keyframes rectPulse {
    0% { box-shadow: 0 0 8px rgba(255, 255, 255, 0.3); }
    50% { box-shadow: 0 0 18px rgba(255, 255, 255, 0.7); }
    100% { box-shadow: 0 0 8px rgba(255, 255, 255, 0.3); }
}

.photo-tag-label {
    position: absolute;
    bottom: -28px;
    left: 50%;
    transform: translateX(-50%) scale(var(--lightbox-label-counter-scale, 1));
    transform-origin: center top;
    background: rgba(15, 23, 42, 0.6) !important; /* Scuro e semi-trasparente */
    -webkit-backdrop-filter: blur(8px) !important;
    backdrop-filter: blur(8px) !important; /* Sfocatura sfondo Premium */
    color: #ffffff !important;
    padding: 6px 14px !important;
    border-radius: 20px !important; /* Pillola completa */
    font-size: 0.85rem !important;
    font-weight: 500 !important;
    white-space: nowrap;
    opacity: 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.25) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    transition: transform 0.2s ease, opacity 0.2s ease !important;
    pointer-events: none;
    -webkit-user-select: none !important;
    user-select: none !important;
}

.photo-tag-label:hover {
    transform: translateX(-50%) scale(calc(var(--lightbox-label-counter-scale, 1) * 1.05));
}

.tag-bounding-box.active .photo-tag-label,
.tag-bounding-box:hover .photo-tag-label {
    opacity: 1;
}

/* Lightbox Toolbar */
.lightbox-toolbar {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    background: rgba(255, 255, 255, 0.1);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 40px;
    z-index: 1001;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.lightbox-nav {
    width: 44px;
    height: 44px;
    border-radius: 50% !important;
}

.admin-delete-btn,
.admin-delete-tag-btn,
.lightbox-nav-tags {
    position: relative;
    width: 44px;
    height: 44px;
    border-radius: 50% !important;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid transparent;
    background: transparent;
    padding: 0;
}

.lightbox-nav-tags {
    background-color: rgba(59, 130, 246, 0.15);
    color: #60a5fa;
    border-color: rgba(59, 130, 246, 0.3);
}

.admin-delete-tag-btn {
    background-color: rgba(148, 163, 184, 0.15); /* Slate 400 */
    color: #cbd5e1;
    border-color: rgba(148, 163, 184, 0.3);
}

.admin-delete-btn {
    background-color: rgba(244, 63, 94, 0.1); /* Rose 500 */
    color: #fb7185;
    border-color: rgba(244, 63, 94, 0.2);
}

.lightbox-nav-tags:hover {
    background-color: #4f46e5 !important;
    color: white !important;
    transform: scale(1.1);
}

.admin-delete-tag-btn:hover {
    background-color: #94a3b8 !important;
    color: white !important;
    transform: scale(1.05); /* Softer scale */
}

.admin-delete-btn:hover {
    background-color: #f43f5e !important;
    color: white !important;
    transform: scale(1.05); /* Softer scale */
}

/* Sidebar Specific action buttons overrides */
#lightbox-sidebar .admin-action-buttons-row {
    display: flex;
    gap: 12px;
    width: 100%;
}

#lightbox-sidebar .admin-delete-tag-btn,
#lightbox-sidebar .admin-delete-btn {
    flex: 1;
    height: auto;
    padding: 12px 10px;
    border-radius: 8px !important;
    justify-content: center;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    text-align: center;
}

#lightbox-sidebar .admin-delete-tag-btn span,
#lightbox-sidebar .admin-delete-btn span {
    font-size: 18px;
}

.admin-tag-action-btn {
    width: 100%;
    padding: 8px;
    border-radius: 6px;
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
    color: #475569;
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    box-sizing: border-box;
}

.admin-tag-action-btn:hover {
    background: #e2e8f0;
    color: #1e293b;
}

/* Save Tags Button */
.lb-save-tags-container {
    padding: 20px;
    border-top: 1px solid rgba(0,0,0,0.06);
    flex-shrink: 0;
}

.btn-save-tags {
    width: 100%;
    padding: 12px;
    background: #10b981;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background 0.2s;
}

.btn-save-tags:hover {
    background: #059669;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background-color 0.4s ease, opacity 0.3s ease;
}

/* Modalità Immersiva: nasconde controlli e sidebar in modo DISTRUTTIVO */
/* Triplo selettore per garantire la precedenza su ogni altra regola esistente */
#lightbox.immersive-mode #lightbox-close,
#lightbox.immersive-mode #lightbox-sidebar,
#lightbox.immersive-mode #lightbox-prev,
#lightbox.immersive-mode #lightbox-next,
#lightbox.immersive-mode .lightbox-toolbar,
#lightbox.immersive-mode [id="lightbox-close"],
body #lightbox.immersive-mode #lightbox-close { 
    opacity: 0 !important;
    visibility: hidden !important;
    display: none !important;
    pointer-events: none !important;
}

#lightbox-close, 
#lightbox-sidebar,
#lightbox-prev,
#lightbox-next,
.lightbox-toolbar {
    transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
}



/* Sidebar in Lightbox */
#lightbox-sidebar {
    width: 420px;
    background: rgba(241, 245, 249, 0.75);
    -webkit-backdrop-filter: blur(25px);
    backdrop-filter: blur(25px);
    border-left: 1px solid rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    flex-shrink: 0;
    /* transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease !important; */
}

/* Mobile Fixes & Immersive View */
@media (max-width: 900px) {
    #lightbox-content {
        width: 100vw !important;
        height: 100vh !important;
        max-width: none !important;
        max-height: none !important;
        border-radius: 0 !important;
        margin: 0 !important;
        background: black !important;
        padding: 0 !important;
    }

    #lightbox-viewer {
        padding: 0 !important; 
        height: 100dvh !important;
        width: 100dvw !important;
        background: black !important;
        display: block !important;
        overflow: hidden !important; /* Clipper for the scroll container */
        position: relative !important;
    }

    #lightbox-content, #lightbox-viewer {
        background: rgba(0, 0, 0, 0.85) !important;
        -webkit-backdrop-filter: blur(15px);
        backdrop-filter: blur(15px);
    }

    #lightbox-canvas {
        width: 100dvw !important;
        height: 100dvh !important;
        display: flex !important;
        flex-direction: row !important;
        justify-content: flex-start !important;
        overflow-x: auto !important; 
        overflow-y: hidden !important;
        scroll-snap-type: x mandatory !important;
        -webkit-overflow-scrolling: touch !important;
        position: relative !important;
        scroll-behavior: auto !important;
        touch-action: pan-x !important;
    }

    /* Zoomed state: lock the carousel, JS handles all gestures */
    #lightbox-canvas.is-zoomed {
        overflow-x: hidden !important;
        touch-action: none !important;
    }
    
    #lightbox-canvas::-webkit-scrollbar {
        display: none !important;
    }

    #lightbox-image-area {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #lightbox-img {
        max-width: 100vw !important;
        max-height: 100vh !important;
        width: auto !important;
        height: auto !important;
        object-fit: contain;
    }

    /* Nascondi frecce e tasto chiusura su mobile (si usa lo swipe e pull-to-dismiss) */
    #lightbox-prev, #lightbox-next, #lightbox-close {
        display: none !important;
    }

    /* Sidebar come overlay a scomparsa con PEEK infallibile */
    #lightbox-sidebar {
        position: fixed;
        left: 0;
        width: 100%;
        /* Nascondiamo la barra spingendola in basso col transform, invece che col top. 
           L'altezza base è fissa, o la calcoliamo al 70vh */
        height: 70dvh;
        top: auto;
        bottom: 0;
        z-index: 2060;
        border-left: none;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(15, 23, 42, 0.4); 
        -webkit-backdrop-filter: blur(25px);
        backdrop-filter: blur(25px);
        border-radius: 30px 30px 0 0;
        color: white;
        
        box-sizing: border-box; /* IMPEDISCE AL PADDING DI SFASARE L'ALTEZZA */
        transform: translateY(calc(100% - 70px)); /* Mostra solo l'handle di base */
        touch-action: pan-y;
        transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), background 0.4s, opacity 0.3s ease;
        box-shadow: 0 -15px 50px rgba(0,0,0,0.5);
        padding-top: 30px; 
        padding-bottom: calc(env(safe-area-inset-bottom, 20px) + 20px) !important; 
        overflow-y: hidden; /* Nascondi scroll quando è chiusa */
    }

    /* Cuscino invisibile per garantire lo scroll completo fino all'ultimo bottone */
    #lightbox-sidebar::after {
        content: '';
        display: block;
        width: 100%;
        min-height: 60px; /* Spazio per la navigation bar nativa del telefono */
        flex-shrink: 0;
    }

    /* Sidebar Pull Handle */
    #lightbox-sidebar::before {
        content: '';
        position: absolute;
        top: 12px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 2px;
    }

    /* Testo e icone chiari nella sidebar mobile scura */
    #lightbox-sidebar h3,
    #lightbox-sidebar h2,
    #lightbox-sidebar .lb-tag-name,
    #lightbox-sidebar #desc-text {
        color: white !important;
        -webkit-user-select: text;
        user-select: text;
    }
    
    #lightbox-sidebar h3,
    #lightbox-sidebar h2 {
        -webkit-user-select: none !important;
        user-select: none !important;
    }
    
    #lightbox-sidebar .lb-tag-item:hover {
        background: rgba(255,255,255,0.1) !important;
    }

    /* Classe per mostrare la sidebar APERTA */
    #lightbox.mobile-info-active #lightbox-sidebar {
        background: rgba(15, 23, 42, 0.9) !important;
        overflow-y: auto !important;
        transform: translateY(0); /* Rimossa !important per permettere il drag giù */
        opacity: 1 !important;
    }

    /* Toolbar sopra se necessaria, ma tipicamente nascosta */
    .lightbox-toolbar {
        bottom: auto;
        top: 70px;
        background: rgba(15, 23, 42, 0.85);
        opacity: 0.5; /* Più discreta */
    }

    /* Native Slide - Each takes full viewport */
    .carousel-slide-mobile {
        width: 100dvw !important;
        min-width: 100dvw !important;
        max-width: 100dvw !important;
        height: 100dvh !important;
        display: flex !important;
        align-items: center;
        justify-content: center;
        flex: 0 0 100dvw !important;
        scroll-snap-align: center !important;
        scroll-snap-stop: always !important; /* Force single image pagination */
        position: relative !important;
        overflow: hidden !important;
    }

    .image-tag-wrapper {
        max-width: 100vw;
        max-height: 100dvh;
    }

    .image-tag-wrapper img {
        max-width: 100%;
        max-height: 100dvh;
        object-fit: contain;
        transition: none !important;
    }
}

/* Hide AI Chat and scroll-to-top buttons when lightbox is open to avoid overlapping */
body.lightbox-open #ai-chat-fab,
body.lightbox-open .scroll-top-btn,
body.lightbox-open .mobile-index-fab,
body.lightbox-open .mobile-index-overlay {
    display: none !important;
}

/* =========================================
   IMMERSIVE MODE - STATO GLOBALE
   ========================================= */
body.immersive-active #lightbox-close,
body.immersive-active .lightbox-toolbar,
body.immersive-active #lightbox-prev,
body.immersive-active #lightbox-next,
body.immersive-active #lightbox-sidebar {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    transform: translateY(110%) !important;
    transition: all 0.3s ease !important;
}
