/*
 * book.css — styles for the WFBook viewer (assets/book.js).
 *
 * Two layouts driven by a class on the host:
 *   .wfbook-narrow  — phones (<768 px). One page visible at a time, full width.
 *                     Page turn = horizontal slide. Swipe + Back/Next buttons.
 *   .wfbook-wide    — tablet/desktop. Two pages visible (spread). Page turn
 *                     advances by 2 and slides the whole spread.
 *
 * The 3D rotateY page-flip animation from the marketing comps is intentionally
 * NOT here yet — getting layout + paging + sentence highlight wired first.
 * When you want the polish, hook it on .wfbook-page transitions; the data
 * attributes already track which page is incoming vs outgoing.
 */

.wfbook-host {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
}
.wfbook {
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    perspective: 1500px;
}

/* ---- Spread (the visible page area) ---- */
.wfbook-spread {
    position: relative;
    width: 100%;
    max-width: 920px;
    background: #fefbf3;            /* warm paper */
    border: 4px solid #1e293b;
    border-radius: 14px;
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.35);
    display: flex;
    overflow: hidden;
    min-height: 360px;
    /* preserve-3d on the spread so the flip overlay's rotateY shows in 3D
       relative to the page perspective set on .wfbook. */
    transform-style: preserve-3d;
}

/* Spine line — only visible in wide mode where two pages sit side by side. */
.wfbook-wide .wfbook-spread::after {
    content: '';
    position: absolute;
    top: 6%;
    bottom: 6%;
    left: 50%;
    width: 2px;
    background: linear-gradient(180deg, transparent, rgba(15, 23, 42, 0.25), transparent);
    transform: translateX(-50%);
    pointer-events: none;
}

/* ---- Individual pages ---- */
.wfbook-page {
    box-sizing: border-box;
    position: relative;
    padding: 1.5rem 1.75rem 2.25rem;
    font-family: 'Nunito', system-ui, sans-serif;
    color: #1e293b;
    display: flex;
    flex-direction: column;
    transition: transform 0.32s ease, opacity 0.25s ease;
}

/* Narrow: one full-width page. Hide the others entirely. */
.wfbook-narrow .wfbook-page {
    flex: 0 0 100%;
    width: 100%;
}
.wfbook-narrow .wfbook-page.wfbook-page-hidden { display: none; }

/* Wide: two half-width pages sit side-by-side. */
.wfbook-wide .wfbook-page {
    flex: 0 0 50%;
    width: 50%;
}
.wfbook-wide .wfbook-page.wfbook-page-hidden { display: none; }

.wfbook-page-image {
    margin: 0 auto 1rem;
    width: 100%;
    max-width: 360px;
    aspect-ratio: 16 / 10;
    background: #fef3c7;
    border-radius: 12px;
    overflow: hidden;
    border: 3px solid #1e293b;
    box-shadow: 4px 4px 0 #1e293b;
    display: flex;
    align-items: center;
    justify-content: center;
}
.wfbook-page-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.wfbook-page-body {
    /* Kid-readable font (Group 37). Andika is SIL's early-literacy
       font — single-story "a", clear letterforms, distinguishable
       lowercase "l" / capital "I". Host page is responsible for
       @import-ing the font; if absent, falls back through Lexend Mega
       (branding font) and Nunito (default). */
    font-family: 'Andika', 'Lexend Mega', 'Nunito', sans-serif;
    font-size: clamp(1rem, 1.6vw, 1.25rem);
    line-height: 1.55;
    font-weight: 700;
    flex: 1;
}
.wfbook-page-body:empty { display: none; }

.wfbook-page-num {
    position: absolute;
    bottom: 0.6rem;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.7rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    color: #94a3b8;
    text-transform: uppercase;
}
.wfbook-wide .wfbook-side-left  .wfbook-page-num { left: 25%; }
.wfbook-wide .wfbook-side-right .wfbook-page-num { left: 75%; }

/* ---- Controls ---- */
.wfbook-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    width: 100%;
    max-width: 920px;
    margin-top: 0.9rem;
    padding: 0 0.5rem;
}
.wfbook-counter {
    font-family: 'Nunito', sans-serif;
    font-weight: 900;
    font-size: 0.85rem;
    color: #475569;
    letter-spacing: 0.05em;
}
.wfbook-btn {
    font-family: 'Lexend Mega', 'Nunito', sans-serif;
    font-weight: 900;
    text-transform: uppercase;
    font-size: 0.85rem;
    padding: 0.55rem 1.1rem;
    border: 3px solid #1e293b;
    border-radius: 9999px;
    background: white;
    color: #1e293b;
    cursor: pointer;
    box-shadow: 0 4px 0 #1e293b;
    transition: transform 0.1s, box-shadow 0.1s, opacity 0.2s;
}
.wfbook-btn:active {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #1e293b;
}
.wfbook-btn[disabled] {
    opacity: 0.4;
    cursor: not-allowed;
    box-shadow: 0 2px 0 #1e293b;
}

/* ---- Narrow-mode 3D flip (mobile) -------------------------------
   Phones used to do a cheap horizontal slide. They now get the same
   3D rotateY flip the desktop spread uses, scaled to full page width
   so a single page can pivot around its spine. Animation budget is
   ~0.55s — a touch slower than desktop so the flip reads at phone
   distance. The .wfbook-page-enter-* fallback animations below
   remain as a safety net for the long-jump path (which can't use the
   overlay) but are tightened from the old 0.32s slide. */
.wfbook-narrow .wfbook-page-enter-forward  { animation: wfbook-slide-in-right 0.22s ease both; }
.wfbook-narrow .wfbook-page-enter-backward { animation: wfbook-slide-in-left  0.22s ease both; }
@keyframes wfbook-slide-in-right {
    from { transform: translateX(12%);  opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}
@keyframes wfbook-slide-in-left {
    from { transform: translateX(-12%); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

/* Narrow-mode flip overlay. Same .wfbook-flip skeleton the wide
   spread uses — `position: absolute` on .wfbook-flip already covers
   inset 0 in narrow mode because the spread itself is full-width.
   We override the width: 50% rule that the wide spread sets so the
   overlay covers the entire page. */
.wfbook-narrow .wfbook-flip { width: 100%; }
.wfbook-narrow .wfbook-flip-narrow-forward {
    right: 0;
    transform-origin: left center;
    transform: rotateY(0deg);
}
.wfbook-narrow .wfbook-flip-narrow-forward.wfbook-flip-go {
    transform: rotateY(-180deg);
}
.wfbook-narrow .wfbook-flip-narrow-backward {
    left: 0;
    transform-origin: right center;
    transform: rotateY(0deg);
}
.wfbook-narrow .wfbook-flip-narrow-backward.wfbook-flip-go {
    transform: rotateY(180deg);
}

/* Page-curl shadow — a moving gradient over the flipping page that
   mimics the light a real paper edge catches as it lifts. Pseudo-
   element + keyframe so we don't pay JS time for it. The shadow is
   strongest at 50% (when the page is edge-on) and fades at the
   endpoints; same shape in both directions thanks to symmetric
   keyframes. */
.wfbook-flip .wfbook-face::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    border-radius: inherit;
    background: linear-gradient(110deg,
        rgba(15, 23, 42, 0.00) 0%,
        rgba(15, 23, 42, 0.18) 45%,
        rgba(15, 23, 42, 0.32) 55%,
        rgba(15, 23, 42, 0.00) 100%);
    opacity: 0;
    animation: wfbook-curl 0.7s ease-in-out forwards;
}
@keyframes wfbook-curl {
    0%   { opacity: 0; }
    50%  { opacity: 1; }
    100% { opacity: 0; }
}
.wfbook-narrow .wfbook-flip .wfbook-face::after {
    animation-duration: 0.55s; /* match the narrow-mode flip duration */
}

/* ---- Wide-mode 3D page flip --------------------------------------
   A two-face overlay is appended to the spread for the duration of a
   page turn. Its front face matches the page being turned away from;
   its back face is the page about to be revealed. The overlay rotates
   around the spine (left edge for forward, right edge for backward);
   backface-visibility on each face means the user sees the front
   during the first 90deg of rotation and the back during the second.
   Underlying pages swap during the flip — covered while obscured —
   so the visible state stays consistent throughout. */
.wfbook-flip {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 50%;
    z-index: 5;
    transform-style: preserve-3d;
    backface-visibility: hidden;
    /* Slight terminal overshoot (y2=1.04) lets the page "settle" at
       the end of the flip instead of stopping flat — gives the turn
       a satisfying weight without going full rubber-band. */
    transition: transform 0.7s cubic-bezier(0.4, 0, 0.2, 1.04);
    /* Subtle drop shadow on the lifting page so it looks like it's
       coming off the paper rather than sliding flat. */
    box-shadow: 0 14px 36px rgba(15, 23, 42, 0.28);
}
/* Narrow-mode flips run a touch faster — full-screen rotation reads
   slower at phone distance otherwise. */
.wfbook-narrow .wfbook-flip {
    transition-duration: 0.55s;
}
.wfbook-flip-forward {
    right: 0;
    transform-origin: left center;
    transform: rotateY(0deg);
}
.wfbook-flip-forward.wfbook-flip-go {
    transform: rotateY(-180deg);
}
.wfbook-flip-backward {
    left: 0;
    transform-origin: right center;
    transform: rotateY(0deg);
}
.wfbook-flip-backward.wfbook-flip-go {
    transform: rotateY(180deg);
}
/* Faces stack at the same spot, distinguished by their rotateY. The
   front face is at 0deg, the back at 180deg so it shows once the
   parent has rotated past 90deg. */
.wfbook-flip .wfbook-face {
    position: absolute !important;
    inset: 0;
    backface-visibility: hidden;
    background: #fefbf3;
    overflow: hidden;
}
.wfbook-flip .wfbook-face-front { transform: rotateY(0deg); }
.wfbook-flip .wfbook-face-back  { transform: rotateY(180deg); }
/* Hide page-num inside cloned faces — already shown on the underlying
   pages. Keeps the flip clean without a stray number floating mid-air. */
.wfbook-flip .wfbook-page-num { display: none; }

/* Wide mode replaces the slide entirely with the 3D flip — no slide-in
   on the destination pages or you'd see a double animation. */
.wfbook-wide .wfbook-page-enter-forward,
.wfbook-wide .wfbook-page-enter-backward { animation: none !important; }

/* ============================================================
   Closed-book cover overlay (Group 35).
   Shown when WFBook.render({cover: {...}}) is provided. Kid sees the
   story art with the title on top + an "Open Book" button. Tapping
   anywhere on the cover dismisses it and reveals the page spread.
   ============================================================ */
.wfbook-closed .wfbook-controls { visibility: hidden; }
.wfbook-cover {
    position: absolute;
    inset: 0;
    z-index: 10;
    border: 4px solid #1e293b;
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 14px 32px rgba(15, 23, 42, 0.45);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #fefbf3;          /* warm paper, overridden by inline style if a bgHex is passed */
    transform-origin: left center;
    transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1),
                opacity   0.5s ease-out;
}
.wfbook-cover-opening {
    /* The book "opens" — cover swings to the left like a real cover flap,
       then fades out. transform-origin: left center pivots it from the
       spine side, giving the unmistakable book-cover-flip feel. */
    transform: rotateY(-110deg);
    opacity: 0;
    pointer-events: none;
}
.wfbook-cover-art {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: inherit;
}
.wfbook-cover-art img {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
}
.wfbook-cover-emoji {
    /* Fallback when no PNG exists yet — show the emoji huge. Keeps the
       book feeling like it has SOMETHING on the cover even when image
       pre-warm hasn't run for this story. */
    font-size: clamp(6rem, 22vw, 14rem);
    line-height: 1;
    margin-bottom: 1rem;
    user-select: none;
}
.wfbook-cover-title-wrap {
    position: absolute;
    top: 1.4rem;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 2rem);
    max-width: 760px;
    display: flex;
    justify-content: center;
    pointer-events: none;
}
.wfbook-cover-title {
    font-family: 'Lexend Mega', 'Nunito', sans-serif;
    font-weight: 900;
    font-size: clamp(1.6rem, 4.4vw, 2.6rem);
    text-align: center;
    color: #1e293b;
    background: rgba(255, 253, 240, 0.92);
    padding: 0.55rem 1.1rem;
    border-radius: 14px;
    border: 3px solid #1e293b;
    box-shadow: 4px 4px 0 #1e293b;
    text-transform: uppercase;
    letter-spacing: -0.01em;
    line-height: 1.15;
}
.wfbook-cover-open {
    position: absolute;
    bottom: 1.4rem;
    left: 50%;
    transform: translateX(-50%);
    font-family: 'Lexend Mega', 'Nunito', sans-serif;
    font-weight: 900;
    font-size: 1rem;
    padding: 0.75rem 1.6rem;
    border: 3px solid #1e293b;
    border-radius: 12px;
    background: #fbbf24;
    color: #1e293b;
    box-shadow: 4px 4px 0 #1e293b;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.wfbook-cover-open:hover {
    transform: translateX(-50%) translateY(-2px);
    box-shadow: 4px 6px 0 #1e293b;
}
.wfbook-cover-open:active {
    transform: translateX(-50%) translateY(2px);
    box-shadow: 2px 2px 0 #1e293b;
}
