/* --- Variables --- */
:root {
    --toolbar-h: 48px;
    --page-gap: 16px;
    --page-shadow: 0 4px 12px rgba(0,0,0,0.15);
    --overlay-hover: rgba(0, 100, 200, 0.15);
    --modal-bg: rgba(0, 0, 0, 0.6);
    --modal-radius: 8px;
}

/* --- Reset / base --- */
*, *::before, *::after { box-sizing: border-box; }
html, body {
    height: 100%;
    overflow: hidden;
}
body {
    margin: 0;
    font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
    background: #2c2c2c;
    background-image:url('../IGEDD_rapport_FOND.jpg');
    color: #eee;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    min-height: 100vh;
    background-size: 100% 100%;
    box-shadow: -2px -4px 9px 3px #00bab7;
}

/* Adaptation du fond selon le ratio de l'écran
   - Écran large (ex: 16/9) : on garde 100% 100% comme actuellement
   - Écran vertical / plus haut que large : on cale l'image sur la hauteur,
     on garde la partie gauche et on coupe la droite si besoin */
@media (max-aspect-ratio: 1/1) {
    body {
        background-size: auto 100%;
        background-position: left center;
    }
}

/* iPad & mobile : fond en cover (naturel), toolbar compacte, indicateur page en texte */
@media (max-width: 1024px) {
    html, body {
        height: auto;
        min-height: 100dvh; /* utilise la hauteur visible réelle sur mobile */
    }
    body {
        background-size: cover;
        background-position: top;
    }
    .reader {
        height: auto;
        min-height: 100dvh;
        /* Pour que le livre soit centré : hauteur fixe = viewport, le viewport prend le reste */
        height: 100dvh;
        display: flex;
        flex-direction: column;
    }
    .reader-viewport {
        flex: 1;
        min-height: 0;
    }
    .reader-toolbar {
        gap: 12px;
        padding: 6px 10px;
        min-height: 40px;
    }
    .toolbar-group {
        gap: 6px;
    }
    .toolbar-group button {
        width: 26px;
        height: 26px;
        font-size: 0.95rem;
    }
    .page-indicator .page-num-input,
    .page-indicator #page-sep,
    .page-indicator #page-total {
        display: none !important;
    }
    .page-indicator .page-num-text {
        display: inline;
        font-size: 0.9rem;
        min-width: 3ch;
    }
    #zoom-label {
        font-size: 0.85rem;
        min-width: 40px;
    }
}

/* Format encore plus étroit (ex: ~915x412) : toolbar encore plus compacte */
@media (max-width: 916px) {
    .reader-toolbar {
        gap: 8px;
        padding: 4px 8px;
        min-height: 36px;
    }
    .toolbar-group {
        gap: 4px;
    }
    .toolbar-group button {
        width: 24px;
        height: 24px;
        font-size: 0.85rem;
    }
    .page-indicator .page-num-text {
        font-size: 0.8rem;
    }
    #zoom-label {
        font-size: 0.8rem;
        min-width: 36px;
    }
}

/* --- Reader layout --- */
.reader {
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-height: 0;
    overflow: hidden;
}

.reader-toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    flex-wrap: wrap;
    flex-shrink: 0;
    min-height: var(--toolbar-h);
    padding: 8px 16px;
    /* background: #1a1a1a; */
    /* border-bottom: 1px solid #444; */
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

.toolbar-group button {
    width: 36px;
    height: 36px;
    border: none;
    /* border-radius: 6px; */
    background: #175177;
    color: #eee;
    font-size: 1.25rem;
    cursor: pointer;
    line-height: 1;
}
.toolbar-group button:hover {
    background: #444;
    border-color: #666;
}

.page-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    color: white;
    font-weight: bold;
}
.page-num-text {
    display: none; /* affiché uniquement sur mobile/iPad */
}
#page-num {
    height: 36px;
    width: 56px;
    padding: 6px 8px;
    border: none;
    background: #175177;
    color: #eee;
    font-size: 0.95rem;
    text-align: center;
}
#page-num:focus {
    outline: none;
    border-color: #0a84ff;
}
#zoom-label {
    min-width: 48px;
    text-align: center;
    font-size: 0.9rem;
    color: white;
    font-weight: bold;
}

.toolbar-group label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: #aaa;
    cursor: pointer;
}
.toolbar-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* --- Viewport & pages --- */
.reader-viewport {
    flex: 1;
    overflow: auto;
    padding: 12px;
    display: block;
    position: relative;
    min-height: 0;
    width: 100%;
}

/* Scrollbars personnalisés pour la zone de lecture */
.reader-viewport {
    scrollbar-color: #00bbb7 transparent; /* Firefox */
    scrollbar-width: thin;
}
.reader-viewport::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}
.reader-viewport::-webkit-scrollbar-track {
    background: transparent;
}
.reader-viewport::-webkit-scrollbar-thumb {
    background-color: #00bbb7;
    border-radius: 4px;
}

/* Conteneur interne pour centrer le livre tout en gardant le scroll quand il dépasse */
.viewport-inner {
    min-height: 100%;
    min-width: fit-content; /* Indispensable : permet le scroll gauche quand le livre est zoomé */
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Empêche le flipbook de rétrécir dans le flex, pour autoriser le scroll horizontal quand il est plus large que le viewport */
.viewport-inner > * {
    flex-shrink: 0;
}
.preload-status {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 16px 24px;
    background: rgba(0, 0, 0, 0.75);
    border-radius: 8px;
    color: #eee;
    font-size: 0.95rem;
    z-index: 5;
}
.preload-status[hidden] {
    display: none;
}

.pages-container {
    display: flex;
    flex-wrap: wrap;
    gap: var(--page-gap);
    justify-content: center;
    align-items: center;
    perspective: 1200px;
    min-height: 0;
}

/* Flipbook Turn.js */
#flipbook.flipbook {
    margin: 0 auto;
    position: relative;
}
#flipbook .turn-page {
    overflow: hidden;
    background: transparent;
    box-shadow: var(--page-shadow);
}
#flipbook .turn-page-inner {
    position: relative;
    width: 100%;
    height: 100%;
}
#flipbook .turn-page-inner img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
}
#flipbook .turn-page .page-overlay {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}
#flipbook .turn-page .page-overlay > * {
    pointer-events: auto;
}

.page-fit-container {
    position: relative;
    flex-shrink: 0;
}

.page-scale-wrap {
    position: relative;
    transform-origin: 0 0;
}

.pages-container.spread {
    flex-wrap: nowrap;
}

.page-wrapper {
    position: relative;
    flex-shrink: 0;
    box-shadow: var(--page-shadow);
    border-radius: 2px;
    overflow: hidden;
    background: transparent;
}

/* Effet coin type Turn.js : un seul rabat avec gradient, curl 3D */
.flip-zone {
    position: absolute;
    z-index: 10;
    cursor: grab;
    pointer-events: auto;
    width: 100px;
    height: 100px;
    transform-origin: 0 0;
    transform-style: preserve-3d;
    transition: transform 0.2s ease-out;
    box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.2);
    perspective: 800px;
}
.flip-zone:active {
    cursor: grabbing;
}
/* Première page : coin plus grand, style Turn.js */
.flip-zone-next {
    right: 0;
    bottom: 0;
    clip-path: polygon(100% 0, 100% 100%, 0 100%);
    background: linear-gradient(
        225deg,
        rgba(255, 255, 255, 0.98) 0%,
        rgba(248, 248, 248, 0.95) 30%,
        rgba(240, 240, 240, 0.9) 55%,
        rgba(200, 200, 200, 0.6) 80%,
        rgba(0, 0, 0, 0.08) 100%
    );
    transform-origin: 0 0;
    backface-visibility: hidden;
}
.flip-zone-prev {
    left: 0;
    bottom: 0;
    clip-path: polygon(0 0, 0 100%, 100% 100%);
    background: linear-gradient(
        315deg,
        rgba(255, 255, 255, 0.98) 0%,
        rgba(248, 248, 248, 0.95) 30%,
        rgba(240, 240, 240, 0.9) 55%,
        rgba(200, 200, 200, 0.6) 80%,
        rgba(0, 0, 0, 0.08) 100%
    );
    transform-origin: 100% 0;
}

/* Désactiver l’effet coin : masquer les zones */
.reader.corner-effect-off .flip-zone {
    display: none !important;
}

.toolbar-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: #aaa;
    cursor: pointer;
}
.toolbar-toggle input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.flip-zone.curling {
    transition: none;
}

.flip-zone:hover::after {
    opacity: 1;
}
.flip-zone::after {
    position: absolute;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.4);
    font-weight: 600;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
}
.flip-zone-next::after {
    content: '›';
    right: 12px;
    bottom: 12px;
}
.flip-zone-prev::after {
    content: '‹';
    left: 12px;
    bottom: 12px;
}

/* Animations flip (tourner la page) */
.page-wrapper {
    transform-style: preserve-3d;
    backface-visibility: hidden;
}
.page-wrapper.flip-out-next {
    transform-origin: left center;
    animation: flip-out-next 0.45s ease-in forwards;
}
.page-wrapper.flip-in-next {
    transform-origin: left center;
    animation: flip-in-next 0.45s ease-out forwards;
}
.page-wrapper.flip-out-prev {
    transform-origin: right center;
    animation: flip-out-prev 0.45s ease-in forwards;
}
.page-wrapper.flip-in-prev {
    transform-origin: right center;
    animation: flip-in-prev 0.45s ease-out forwards;
}

@keyframes flip-out-next {
    from { transform: rotateY(0deg); }
    to   { transform: rotateY(-180deg); }
}
@keyframes flip-in-next {
    from { transform: rotateY(180deg); }
    to   { transform: rotateY(0deg); }
}
@keyframes flip-out-prev {
    from { transform: rotateY(0deg); }
    to   { transform: rotateY(180deg); }
}
@keyframes flip-in-prev {
    from { transform: rotateY(-180deg); }
    to   { transform: rotateY(0deg); }
}

.page-overlay {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.page-overlay > * {
    pointer-events: auto;
}

/* --- Overlay elements (links & videos) --- */
.overlay-link {
    position: absolute;
    cursor: pointer;
    border: none;
    background: transparent;
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
    border-radius: 2px;
}
.overlay-link:hover {
    background: var(--overlay-hover);
}

.overlay-gotopage {
    position: absolute;
    cursor: pointer;
    border: none;
    background: transparent;
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
    border-radius: 2px;
}
.overlay-gotopage:hover {
    background: var(--overlay-hover);
}

.overlay-video {
    position: absolute;
    border-radius: 4px;
    overflow: hidden;
    background: #000;
}
.overlay-video video {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.overlay-iframe {
    position: absolute;
    border-radius: 4px;
    overflow: hidden;
    background: #000;
}
.overlay-iframe iframe {
    display: block;
    width: 100%;
    height: 100%;
    border: 0;
}

/* --- Modal (popup) --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: var(--modal-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 24px;
}

.modal-overlay[hidden] {
    display: none;
}

.modal {
    background: #1e1e1e;
    border-radius: var(--modal-radius);
    box-shadow: 0 16px 48px rgba(0,0,0,0.5);
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid #444;
    flex-shrink: 0;
}

.modal-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
}

.modal-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.modal-open-link {
    width: 32px;
    height: 32px;
    /* border-radius: 6px; */
    border: none;
    background: transparent;
    color: #ddd;
    font-size: 1rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
.modal-open-link:hover:not([disabled]) {
    background: #333;
    border-color: #777;
    color: #fff;
}
.modal-open-link[disabled] {
    opacity: 0.4;
    cursor: default;
}

.modal-close {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: #aaa;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
}
.modal-close:hover {
    background: #333;
    color: #eee;
}

.modal-body {
    flex: 1;
    overflow: auto;
    padding: 0;
}
.modal-body iframe {
    display: block;
    width: 100%;
    height: 70vh;
    min-height: 400px;
    border: none;
}

.modal-body .modal-html {
    padding: 16px;
}

/* Message d'erreur chargement PDF */
.error {
    color: #f88;
    padding: 24px;
    text-align: center;
}
.error code {
    background: #333;
    padding: 2px 6px;
    border-radius: 4px;
}

/* Overrides mobiles supplémentaires pour une toolbar encore plus compacte
   (appliqués APRÈS les règles de base pour être sûrs de gagner la cascade) */
@media (max-width: 1024px) {
    html, body {
        height: auto;
        min-height: 100dvh;
        overflow: auto;
    }
    .reader {
        height: 100dvh;
        min-height: 100dvh;
        overflow: hidden;
    }
    .reader-toolbar {
        gap: 12px;
        padding: 6px 10px;
        min-height: 40px;
    }
    .toolbar-group {
        gap: 6px;
    }
    .toolbar-group button {
        width: 26px;
        height: 26px;
        font-size: 0.95rem;
    }
    .page-indicator .page-num-input,
    .page-indicator #page-sep,
    .page-indicator #page-total {
        display: none !important;
    }
    .page-indicator .page-num-text {
        display: inline;
        font-size: 0.9rem;
        min-width: 3ch;
    }
    #zoom-label {
        font-size: 0.85rem;
        min-width: 40px;
    }
    .reader-viewport {
        padding-bottom: 16px; /* petite marge visuelle en bas sur mobile/tablette */
    }
    #flipbook.flipbook {
        margin-bottom: 16px; /* espace visible entre le bas du document et le bord de l'écran */
    }
}

@media (max-width: 916px) {
    .reader-toolbar {
        gap: 8px;
        padding: 4px 8px;
        min-height: 34px;
    }
    .toolbar-group {
        gap: 4px;
    }
    .toolbar-group button {
        width: 22px;
        height: 22px;
        font-size: 0.8rem;
    }
    .page-indicator .page-num-text {
        font-size: 0.78rem;
    }
    #zoom-label {
        font-size: 0.78rem;
        min-width: 34px;
    }
}
