* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f5f5f5;
    height: 100vh;
    overflow: hidden;
}

#viewer-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: relative;
}

#viewer-header {
    background: white;
    padding: 1rem;
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    z-index: 10;
    /* Altezza ridotta a 50px */
    min-height: 50px;
}

#document-title {
    font-size: 1.5rem;
    color: #333;
    /* Assicura che il titolo non vada a capo in modo incontrollato */
    line-height: 1.3;
    margin: 0;
}

#viewer-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#viewer-controls button {
    padding: 0.5rem 1rem;
    border: 1px solid #ddd;
    background: white;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

#viewer-controls button:hover {
    background: #f0f0f0;
    transform: translateY(-1px);
}

#viewer-controls button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Navigation arrows inside viewer */
.nav-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0,0,0,0.7);
    color: white;
    border: none;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    font-size: 1.5rem;
    cursor: pointer;
    z-index: 5;
    opacity: 0;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.nav-arrow:hover {
    background: rgba(0,0,0,0.9);
    transform: translateY(-50%) scale(1.1);
}

.nav-arrow:disabled {
    opacity: 0 !important;
    cursor: not-allowed;
}

.nav-arrow.prev {
    left: 20px;
}

.nav-arrow.next {
    right: 20px;
}

.nav-arrow:not(:disabled) {
    opacity: 0.8;
}

/* Fullscreen button inside viewer */
.fullscreen-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(0,0,0,0.8);
    color: white;
    border: none;
    padding: 0.75rem;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    z-index: 10;
    opacity: 0.8;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
}

.fullscreen-btn:hover {
    background: rgba(0,0,0,0.9);
    transform: scale(1.1);
}

/* Bottom controls */
.bottom-controls {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    background: rgba(0,0,0,0.9);
    padding: 0.4rem 0.8rem;
    border-radius: 12px 12px 0 0;
    z-index: 10;
    opacity: 1;
    transition: opacity 0.3s;
    min-width: 250px;
}

.bottom-controls button {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.3);
    color: white;
    padding: 0.25rem;
    border-radius: 4px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.75rem;
}

.bottom-controls button:hover {
    background: rgba(255,255,255,0.2);
    border-color: rgba(255,255,255,0.5);
}

.bottom-controls .page-info {
    color: white;
    font-weight: 500;
    margin: 0 0.4rem;
    font-size: 0.75rem;
}

/* Share button bottom right */
.share-button {
    position: absolute;
    bottom: 20px;
    right: 20px;
    background: rgba(0,0,0,0.8);
    color: white;
    border: none;
    padding: 0.75rem;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    z-index: 10;
    opacity: 0;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.share-button {
    opacity: 0.8;
}

.share-button:hover {
    background: rgba(0,0,0,0.9);
    transform: scale(1.1);
}

#viewer-main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

#page-container {
    flex: 1;
    display: flex;
    background: #fff;
    position: relative;
    overflow: hidden;
    min-height: 0;
    min-width: 0;
}

#zoom-stage {
    position: absolute;
    inset: 0;
    overflow: hidden;
    z-index: 1;
    cursor: default;
}

#zoom-layer.page-loading {
    visibility: hidden;
}

#zoom-layer {
    display: inline-block;
    line-height: 0;
    touch-action: none;
}

#current-page {
    display: block;
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    pointer-events: none;
}

#zoom-layer.zoomed {
    cursor: grab;
}

#zoom-layer.zoomed:active {
    cursor: grabbing;
}

/* Touch feedback per mobile */
@media (hover: none) and (pointer: coarse) {
    /* Styles per dispositivi touch */
    .nav-arrow {
        opacity: 0.9;
        transform: translateY(-50%) scale(1.1);
        background: rgba(0,0,0,0.8);
    }

    .nav-arrow:active {
        transform: translateY(-50%) scale(0.95);
        background: rgba(0,0,0,0.9);
    }

    .bottom-controls button {
        min-height: 44px; /* Touch target size consigliato */
        min-width: 44px;
        font-size: 1.1rem;
    }

    .bottom-controls button:active {
        transform: scale(0.95);
        background: rgba(255,255,255,0.3);
    }

    /* Il controllo di touch-action è gestito da Panzoom tramite #zoom-layer */
}

/* Mobile responsive */
@media (max-width: 768px) {
    /* CSS Custom Properties per altezza dinamica mobile */
    :root {
        --vh: 1vh;
        --mobile-header-height: 50px; /* Ridotto a 50px come desktop */
    }

    html, body {
        height: 100%;
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        overflow: hidden;
        position: fixed;
        width: 100%;
    }

    #viewer-container {
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        display: flex;
        flex-direction: column;
        position: relative;
        overflow: hidden;
    }

    #viewer-header {
        flex-direction: column;
        gap: 0.5rem;
        height: auto; /* Lascia che si adatti al contenuto */
        min-height: 50px; /* Ridotto a 50px come desktop */
        flex-shrink: 0;
        padding: 0.75rem; /* Padding ridotto per mobile */
    }

    #document-title {
        font-size: 1.1rem; /* Leggermente ridotto per adattarsi all'header più piccolo */
        text-align: center;
        line-height: 1.2;
        margin: 0;
    }

    #viewer-main {
        flex: 1;
        display: flex;
        flex-direction: column;
        height: 100%;
        min-height: 0;
        overflow: hidden;
        position: relative;
    }
    
    #page-container {
        width: 100%;
        height: 100%;
        min-height: 0;
    }

    #thumbnails-container {
        display: none !important; /* Hide on mobile by default */
    }
    
    /* Touch-friendly navigation arrows */
    .nav-arrow {
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
        font-size: 1.2rem;
        opacity: 0.9 !important; /* Always visible on mobile */
    }
    
    .nav-arrow.prev {
        left: 10px;
    }
    
    .nav-arrow.next {
        right: 10px;
    }
    
    /* Touch-friendly bottom controls */
    .bottom-controls {
        bottom: 0;
        padding: 0.5rem 0.8rem;
    }
    
    .bottom-controls button {
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
        font-size: 0.8rem;
        margin: 0 2px;
    }
    
    .bottom-controls .page-info {
        font-size: 0.8rem;
        margin: 0 0.5rem;
        padding: 0.5rem;
    }
    
    /* Touch-friendly share button */
    .share-button {
        bottom: 10px;
        right: 10px;
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
    }
    
    /* Touch-friendly fullscreen button */
    .fullscreen-btn {
        top: 10px;
        right: 10px;
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
        font-size: 1rem;
    }
    
    /* Touch feedback */
    button:active {
        transform: scale(0.95);
        opacity: 0.8;
    }
    
    /* Disable text selection and callouts on mobile */
    #zoom-stage,
    #zoom-layer {
        -webkit-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
        -webkit-tap-highlight-color: transparent;
        touch-action: none;
    }

    /* Fix specifici per diversi browser mobile */

    /* iOS Safari */
    @supports (-webkit-touch-callout: none) {
        #viewer-container {
            height: -webkit-fill-available;
        }

        #zoom-stage {
            height: 100%;
        }
    }

    /* Fix per viewport dinamico su alcuni Android */
    @media (orientation: portrait) {
        #viewer-container {
            height: 100vh;
            height: calc(var(--vh, 1vh) * 100);
        }
    }

    @media (orientation: landscape) {
        #viewer-container {
            height: 100vh;
            height: calc(var(--vh, 1vh) * 100);
        }
    }
}

#thumbnails-container {
    width: 200px;
    background: #f8f9fa;
    border-left: 1px solid #ddd;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #ccc transparent;
}

#thumbnails-container::-webkit-scrollbar {
    width: 8px;
}

#thumbnails-container::-webkit-scrollbar-track {
    background: transparent;
}

#thumbnails-container::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 4px;
}

#thumbnails-container::-webkit-scrollbar-thumb:hover {
    background: #999;
}

#thumbnails {
    padding: 1rem 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.thumbnail {
    border: 2px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s ease;
    background: white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.thumbnail:hover {
    border-color: #007cba;
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}

.thumbnail.active {
    border-color: #007cba;
    box-shadow: 0 2px 8px rgba(0,124,186,0.3);
}

.thumbnail img {
    width: 100%;
    height: auto;
    display: block;
    transition: transform 0.2s ease;
}

.thumbnail:hover img {
    transform: scale(1.05);
}

/* Loading states */
#loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255,255,255,0.9);
    padding: 1rem 2rem;
    border-radius: 8px;
    font-weight: 500;
    z-index: 100;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
