/* ==========================================================================
   Visor de libros interactivos
   El tamaño se calcula con el ancho REAL del contenedor (container queries),
   no con el de la ventana, así nunca se sale de su columna.
   ========================================================================== */

.libro-reader-wrapper {
    position: relative;
    display: block;
    width: 100%;
    max-width: 1080px;
    margin: 20px auto;
    padding: 28px 16px 20px;
    box-sizing: border-box;
    background: var(--libro-bg-color, transparent);
    border-radius: 12px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    container-type: inline-size;   /* ancho disponible = ancho del contenedor */
    overflow-x: clip;              /* nada puede sobresalir en horizontal */
}
.libro-reader-wrapper:focus { outline: none; }
.libro-reader-wrapper:focus-visible {
    outline: 2px solid var(--libro-dot-active-color, #1f3a34);
    outline-offset: 2px;
}

/* --- Escenario ---------------------------------------------------------- */
.libro-book-stage {
    /* Fallback para navegadores sin container queries */
    --libro-page-w: clamp(120px, calc((100vw - 200px) / 2), 420px);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
}
@supports (width: 1cqw) {
    .libro-book-stage {
        /* 140px = 2 flechas (46px) + 2 huecos (24px) */
        --libro-page-w: min(420px, calc((100cqw - 140px) / 2));
    }
}

.libro-reader-wrapper .libro-nav-arrow {
    width: 46px !important;
    height: 46px !important;
    min-width: 46px !important;
    padding: 0 !important;
    border-radius: 50% !important;
    background-color: var(--libro-arrow-bg, #ffffff) !important;
    border: 1px solid #e2e2e5 !important;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08) !important;
    color: var(--libro-arrow-icon, #1f3a34) !important;
    font-size: 16px !important;
    line-height: 1 !important;
    cursor: pointer;
    flex-shrink: 0;
    display: flex !important;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, background-color 0.2s, color 0.2s;
    z-index: 30;
}
.libro-reader-wrapper .libro-nav-arrow:hover,
.libro-reader-wrapper .libro-nav-arrow:focus-visible {
    background-color: var(--libro-arrow-hover-bg, #1f3a34) !important;
    color: var(--libro-arrow-hover-icon, #ffffff) !important;
    transform: scale(1.06);
    outline: none;
}

/* --- Libro -------------------------------------------------------------- */
.libro-book-viewport {
    position: relative;
    flex: 0 0 auto;
    width: calc(var(--libro-page-w) * 2);
    height: calc(var(--libro-page-w) * 4 / 3);
}

/* Contenedor interno: la perspectiva vive aquí (justo encima de la hoja que gira)
   y se desplaza para que portada / contraportada queden centradas. */
.libro-book {
    position: absolute;
    inset: 0;
    perspective: 2200px;
    transform: translateX(0);
}
.libro-book.is-ready {
    transition: transform 0.8s cubic-bezier(0.45, 0.05, 0.25, 1);
}
.libro-book[data-state="cover"] { transform: translateX(-25%); }
.libro-book[data-state="back"]  { transform: translateX(25%); }

/* --- Páginas ------------------------------------------------------------ */
.libro-page {
    position: absolute;
    top: 0;
    left: var(--libro-page-w);            /* por defecto: lado derecho */
    width: var(--libro-page-w);
    height: calc(var(--libro-page-w) * 4 / 3);
    display: none;
    box-sizing: border-box;
    overflow: hidden;
    background: #fdfbf7;
    box-shadow: 0 12px 28px rgba(0,0,0,0.14);
}
.libro-page.visible { display: block; }
.libro-page[data-side="left"],
.libro-page[data-side="single"] { left: 0; }

.libro-page[data-side="right"]  { border-radius: 0 6px 6px 0; }
.libro-page[data-side="left"]   { border-radius: 6px 0 0 6px; }
.libro-page[data-side="single"] { border-radius: 6px; }

.libro-cover { background-color: #1f3a34; }
.libro-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    pointer-events: none;
}

.page-left {
    background: linear-gradient(to right, #fdfbf7 90%, #e5dfd3 100%);
    border-right: 1px solid #d1c9ba;
}
.page-right {
    background: linear-gradient(to left, #fdfbf7 90%, #e5dfd3 100%);
}

/* --- Hoja que gira ------------------------------------------------------ */
.libro-turn-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--libro-page-w);
    height: calc(var(--libro-page-w) * 4 / 3);
    z-index: 25;
    transform-style: preserve-3d;
    pointer-events: none;
    will-change: transform;
}
.libro-turn-wrapper[data-kind="next"] { left: var(--libro-page-w); transform-origin: left center; }
.libro-turn-wrapper[data-kind="prev"] { left: 0;                   transform-origin: right center; }
.libro-turn-wrapper[data-kind^="single"] { left: 0;                transform-origin: left center; }

.libro-turn-front,
.libro-turn-back {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.libro-turn-back { transform: rotateY(180deg); }

/* El clon de la página ocupa la cara completa, sin heredar su posición */
.libro-turn-front .libro-page,
.libro-turn-back .libro-page {
    display: block !important;
    position: absolute;
    inset: 0;
    left: 0 !important;
    width: 100%;
    height: 100%;
    box-shadow: 0 10px 24px rgba(0,0,0,0.18);
}

/* Sombra de pliegue (solo en giros automáticos) */
.libro-turn-wrapper.is-auto .libro-turn-front::after,
.libro-turn-wrapper.is-auto .libro-turn-back::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(to right, rgba(0,0,0,0.20), transparent 45%);
    opacity: 0;
    animation: libroFoldShadow var(--libro-turn-ms, 800ms) ease-in-out forwards;
    pointer-events: none;
}
@keyframes libroFoldShadow {
    0%, 100% { opacity: 0; }
    50%      { opacity: 1; }
}

/* --- Controles inferiores ---------------------------------------------- */
.libro-bottom-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    margin-top: 22px;
}
.libro-page-counter {
    font-size: 13px !important;
    font-weight: 700 !important;
    color: var(--libro-numbers-color, #444444) !important;
    min-width: 75px;
    text-align: center;
}
.libro-page-dots {
    display: flex !important;
    flex-direction: row !important;
    gap: 8px !important;
    align-items: center !important;
    justify-content: center !important;
    flex-wrap: wrap;
}
.libro-reader-wrapper .libro-dot {
    display: inline-block !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    box-sizing: border-box !important;
    width: 9px !important;
    height: 9px !important;
    min-width: 9px !important;
    min-height: 9px !important;
    max-height: 9px !important;
    padding: 0 !important;
    margin: 0 !important;
    border-radius: 50% !important;
    border: 1.5px solid var(--libro-dots-color, #b0b8c9) !important;
    background-color: transparent !important;
    cursor: pointer;
    flex-shrink: 0 !important;
    transition: width 0.25s ease, background-color 0.25s ease, border-color 0.25s ease;
}
.libro-reader-wrapper .libro-dot:hover,
.libro-reader-wrapper .libro-dot:focus {
    background-color: transparent !important;
    border-color: var(--libro-dot-active-color, #1f3a34) !important;
}
.libro-reader-wrapper .libro-dot:focus-visible {
    outline: 2px solid var(--libro-dot-active-color, #1f3a34) !important;
    outline-offset: 2px !important;
}
.libro-reader-wrapper .libro-dot.active,
.libro-reader-wrapper .libro-dot.active:hover,
.libro-reader-wrapper .libro-dot.active:focus {
    width: 24px !important;
    min-width: 24px !important;
    border-radius: 99px !important;
    border-color: var(--libro-dot-active-color, #1f3a34) !important;
    background-color: var(--libro-dot-active-color, #1f3a34) !important;
}

/* --- Popup -------------------------------------------------------------- */
.libro-popup-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.3s, visibility 0s linear 0.3s;
    z-index: 999999;
    padding: 20px;
}
.libro-popup-overlay.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transition: opacity 0.3s, visibility 0s linear 0s;
}
.libro-popup-content {
    background: #ffffff;
    padding: 32px;
    border-radius: 12px;
    text-align: center;
    max-width: 420px;
    width: 100%;
    box-shadow: 0 20px 40px rgba(0,0,0,0.25);
}
.libro-popup-content h3 { margin-top: 0; color: #1f3a34; }
.libro-btn-buy {
    display: block;
    background-color: var(--libro-btn-bg, #1f3a34) !important;
    color: var(--libro-btn-text, #ffffff) !important;
    padding: 13px 20px;
    border-radius: 6px;
    text-decoration: none !important;
    font-weight: 700;
    margin: 18px 0 10px;
}
.libro-btn-buy:focus-visible,
.libro-btn-close:focus-visible {
    outline: 2px solid var(--libro-btn-bg, #1f3a34);
    outline-offset: 2px;
}
.libro-btn-close {
    background: transparent !important;
    border: none !important;
    color: #666 !important;
    text-decoration: underline;
    cursor: pointer;
    font-size: 13px;
}

/* --- Vista de una sola página (según el ancho del CONTENEDOR) ------------
   Debe coincidir con CONTAINER_BREAKPOINT en libro.js (720px). */
@container (max-width: 720px) {
    .libro-book-stage {
        --libro-page-w: min(100cqw, 360px);
        gap: 0;
        touch-action: pan-y;
    }
    .libro-reader-wrapper .libro-nav-arrow { display: none !important; }
    .libro-book-viewport { width: var(--libro-page-w); }
    .libro-book,
    .libro-book[data-state] { transform: none; }
    .libro-bottom-controls {
        flex-direction: column;
        gap: 12px;
        margin-top: 16px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .libro-book.is-ready { transition: none; }
}
