/* ==========================================================================
   Magazine book-flip reader (hand-rolled, no external library)

   The book is sized by viewport HEIGHT so pages render as large as possible;
   navigation arrows + page indicator float over the booklet, and the title /
   "back to magazines" actions sit under the booklet (revealed on scroll).
   ========================================================================== */

/* Immersive reader: hide the site header / announcement bar so the book can use
   the full viewport height. Scoped to the reader — this stylesheet only loads on
   /magazine/{type}/{id}. "Back to Magazines" lives in the below-bar / scroll.
   !important + body override beat the layout's inline header positioning JS. */
.header-wrapper {
    display: none !important;
}

body {
    padding-top: 0 !important;
}

.mr-main {
    background: #ece8f1;
}

/* --- Full-height book stage ---------------------------------------------- */
.mr-stage {
    position: relative;
    height: 100vh;
    height: 100dvh;
    /* full viewport — the site header is hidden on this page */
    min-height: 420px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    overflow: hidden;
}

.flipbook-viewport {
    --page-ratio: 0.773;
    /* width / height of one page; refined by JS from the first image */
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    perspective: 2800px;
}

.flipbook {
    position: relative;
    display: flex;
    flex: 0 0 auto;
    /* as tall as the stage, but never wider than the viewport */
    height: min(100%, calc(100vw / (var(--page-ratio) * 2)));
    aspect-ratio: calc(var(--page-ratio) * 2);
    transform-style: preserve-3d;
    background: #fff;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
    border-radius: 2px;
}

.flipbook .page {
    position: relative;
    width: 50%;
    height: 100%;
    background: #fff;
    overflow: hidden;
}

.flipbook .page--left {
    box-shadow: inset -16px 0 26px -18px rgba(0, 0, 0, 0.4);
    border-radius: 2px 0 0 2px;
}

.flipbook .page--right {
    box-shadow: inset 16px 0 26px -18px rgba(0, 0, 0, 0.4);
    border-radius: 0 2px 2px 0;
}

.flipbook .page img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    background: #fff;
    cursor: zoom-in;
    /* hint: hover to magnify (desktop) */
}

.flipbook .page.is-blank img {
    visibility: hidden;
}

/* Loading spinner per page slot */
.flipbook .page.is-loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 36px;
    height: 36px;
    margin: -18px 0 0 -18px;
    border: 3px solid #d8cee6;
    border-top-color: #895ea5;
    border-radius: 50%;
    animation: mr-spin 0.7s linear infinite;
}

@keyframes mr-spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- The turning leaf ---------------------------------------------------- */
.flipbook .leaf {
    position: absolute;
    top: 0;
    height: 100%;
    width: 50%;
    transform-style: preserve-3d;
    transition: transform 0.6s ease;
    z-index: 20;
    will-change: transform;
}

.flipbook .leaf--right {
    left: 50%;
    transform-origin: left center;
}

.flipbook .leaf--left {
    left: 0;
    transform-origin: right center;
}

.flipbook .leaf--right.is-turning {
    transform: rotateY(-180deg);
}

.flipbook .leaf--left.is-turning {
    transform: rotateY(180deg);
}

.flipbook .leaf__face {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    overflow: hidden;
    background: #fff;
}

.flipbook .leaf__face img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    background: #fff;
}

.flipbook .leaf__face--back {
    transform: rotateY(180deg);
}

/* Soft shading sweep for depth while turning */
.flipbook .leaf__face::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(to right, rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0));
    opacity: 0;
    transition: opacity 0.6s ease;
    pointer-events: none;
}

.flipbook .leaf.is-turning .leaf__face::after {
    opacity: 1;
}

/* --- Floating navigation arrows ------------------------------------------ */
.mr-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 30;
    width: 54px;
    height: 54px;
    border: none;
    border-radius: 50%;
    background: rgba(137, 94, 165, 0.5);
    color: #fff;
    font-size: 30px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s ease, opacity 0.15s ease;
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
}

.mr-nav--prev {
    left: 18px;
}

.mr-nav--next {
    right: 18px;
}

.mr-nav:hover:not(:disabled) {
    background: rgba(137, 94, 165, 0.9);
}

.mr-nav:disabled {
    opacity: 0.25;
    cursor: default;
}

/* --- Floating page indicator --------------------------------------------- */
.mr-indicator {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 30;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-weight: 600;
    font-size: 0.85rem;
    letter-spacing: 0.03em;
    padding: 5px 16px;
    border-radius: 20px;
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
}

/* --- Desktop hover magnifier lens ---------------------------------------- */
.mr-lens {
    position: fixed;
    z-index: 40;
    border-radius: 12px;
    border: 3px solid rgba(255, 255, 255, 0.95);
    box-shadow: 0 12px 38px rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(0, 0, 0, 0.18);
    background-color: #fff;
    background-repeat: no-repeat;
    pointer-events: none;
    display: none;
}

/* --- Title + actions under the booklet ----------------------------------- */
.mr-belowbar {
    text-align: center;
    padding: 34px 16px 60px;
}

.mr-belowbar .mr-title {
    margin: 0 0 16px;
}

.mr-belowbar__actions {
    display: flex;
    gap: 18px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}

.mr-title {
    color: #4a3360;
    font-size: 1.7rem;
}

.mr-back {
    color: #895ea5;
    font-weight: 600;
    text-decoration: none;
    white-space: nowrap;
}

.mr-back:hover {
    text-decoration: underline;
}

/* --- Fallback (images not ready) ----------------------------------------- */
.mr-topbar {
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    margin-bottom: 20px;
}

.mr-fallback {
    text-align: center;
    background: #fff;
    border: 1px solid #e3dced;
    border-radius: 10px;
    padding: 40px 20px;
    max-width: 640px;
    margin: 20px auto;
}

.mr-fallback p {
    margin-bottom: 20px;
    color: #555;
}

.mr-btn,
.mr-pdf-link {
    display: inline-block;
    background: #895ea5;
    color: #fff;
    padding: 10px 22px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
}

.mr-pdf-link {
    padding: 8px 18px;
    font-size: 0.9rem;
}

.mr-btn:hover,
.mr-pdf-link:hover {
    background: #6f4a89;
    color: #fff;
}

/* --- Mobile single-page mode --------------------------------------------- */
@media (max-width: 768px) {
    .mr-stage {
        height: 100vh;
        height: 100dvh;
        padding: 0;
    }

    .flipbook {
        /* edge-to-edge: the page fills the full viewport width on phones */
        height: min(100%, calc(100vw / var(--page-ratio)));
        aspect-ratio: var(--page-ratio);
    }

    .flipbook .page--left {
        display: none;
    }

    .flipbook .page--right {
        width: 100%;
        box-shadow: none;
        border-radius: 2px;
    }

    .flipbook .page img {
        /* allow vertical scroll (reach the below-bar) but let JS own pinch/zoom */
        touch-action: pan-y;
        cursor: default;
    }

    .flipbook .page.is-zoomed img {
        touch-action: none;
    }

    /* magnifier is desktop-only */
    .mr-lens {
        display: none !important;
    }

    .mr-nav {
        width: 44px;
        height: 44px;
        font-size: 26px;
    }

    .mr-nav--prev {
        left: 6px;
    }

    .mr-nav--next {
        right: 6px;
    }

    .mr-title {
        font-size: 1.3rem;
    }
}
