/* Storybook renderer.

   Every colour is a variable (project rule). The defaults below are the book's own palette rather
   than the host app's, because this renders inside both manage's dark admin chrome and the Stories
   app's own theme, and a book should look like a book in either.

   The display faces are bundled beside this file rather than fetched from a CDN — the app is
   offline-first. The stacks still name a system serif last, so a host that fails to serve the
   _content/ assets reads correctly rather than falling back to the UI sans. */

/* Bundled display faces. No CDN: the app is offline-first, and §8.1 is explicit that the Google
   Fonts link the prototype used must not come with it.

   Both are SIL Open Font Licence 1.1 — see wwwroot/ASSETS.md for provenance. Only the ranges a
   booklet actually sets are bundled: Arabic and Latin for Amiri, Latin for Inter. Bundling every
   subset Google publishes would add Cyrillic, Greek and Vietnamese to an app that never renders a
   word of them.

   Inter is a variable font, so one file serves every weight — declaring 400 and 600 against the
   same src is correct, not a copy-paste slip. */

@font-face {
    font-family: "Amiri";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("fonts/amiri-400-arabic.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1,
                   U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF,
                   U+FE70-FE74, U+FE76-FEFC;
}

@font-face {
    font-family: "Amiri";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("fonts/amiri-400-latin.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
                   U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
                   U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Amiri";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("fonts/amiri-700-arabic.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0898-08E1,
                   U+08E3-08FF, U+200C-200E, U+2010-2011, U+204F, U+2E41, U+FB50-FDFF,
                   U+FE70-FE74, U+FE76-FEFC;
}

@font-face {
    font-family: "Amiri";
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url("fonts/amiri-700-latin.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
                   U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
                   U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Inter";
    font-style: normal;
    font-weight: 400 700;
    font-display: swap;
    src: url("fonts/inter-400-latin.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
                   U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
                   U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Inter";
    font-style: normal;
    font-weight: 400 700;
    font-display: swap;
    src: url("fonts/inter-400-latin-ext.woff2") format("woff2");
    unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304,
                   U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB,
                   U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

.sbv {
    --sb-paper: #f6efe1;
    --sb-paper-edge: rgba(0, 0, 0, .18);
    --sb-ink-dark: #2c2317;
    --sb-ink-light: #fdfaf3;
    --sb-parchment: rgba(246, 239, 225, .82);
    --sb-glass: rgba(20, 16, 10, .38);
    /* The page-turn buttons sit directly on artwork, so they need more body than the text panel. */
    --sb-nav-bg: rgba(20, 16, 10, .62);
    --sb-fold: rgba(0, 0, 0, .22);
    /* Spine and page-edge modelling, ported from the flip prototype. The crevice/highlight pair is
       what reads as a curved sheet rather than a flat rectangle with a line drawn on it. */
    --sb-spine-deep: rgba(0, 0, 0, .3);
    --sb-spine-mid: rgba(0, 0, 0, .16);
    --sb-spine-hi: rgba(255, 255, 255, .11);
    --sb-edge-drop: rgba(0, 0, 0, .12);
    --sb-stack: #d9d5c8;
    --sb-stack-shadow: rgba(0, 0, 0, .3);
    --sb-stack-soft: rgba(0, 0, 0, .1);
    --sb-spine-crack: rgba(0, 0, 0, .17);
    /* Halos for unpanelled text. Without a panel the words sit straight on the painting, and on a
       busy passage they disappear into it whichever ink is chosen. A halo in the opposite tone is
       what a picture book does with hand-lettering, and it costs no layout. */
    --sb-halo-dark: rgba(20, 16, 10, .88);
    --sb-halo-light: rgba(253, 250, 243, .92);
    --sb-ornament: rgba(120, 92, 48, .22);
    --sb-overflow: #e5484d;

    /* Set by the ResizeObserver in storybook.js — iOS 15 has no container query units, so the
       page width has to arrive as a custom property. */
    --sb-bw: 100%;
    --sb-bh: auto;
    --sb-pw: 400px;

    /* --sb-scale is the book's chosen size; --sb-fit is the per-page shrink the fit pass applies. */
    --sb-scale: .03;
    --sb-fit: 1;
    --sb-line: 1.5;

    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-height: 0;
    user-select: none;
    -webkit-user-select: none;
}

.sbv--empty {
    align-items: center;
    justify-content: center;
    color: var(--sb-ink-dark);
    opacity: .7;
}

.sbv__stage {
    position: relative;
    flex: 1;
    min-height: 0;
    /* A book sits on a surface; flush to the top edge it reads as a background image. 10px was
       measurable and invisible — under 1% of a tablet's height — so this is the value that
       actually shows. observeSize subtracts it, so the book shrinks to fit rather than being
       pushed off the bottom. */
    padding-block: 28px;
    /* The gesture belongs to the book. Without this Android treats a horizontal drag as a scroll:
       it plays its overscroll stretch on the artwork, then cancels the pointer — so the page
       visibly bounced and never turned, because pointerup never arrived. */
    touch-action: none;
    overscroll-behavior: contain;
    display: flex;
    align-items: center;
    justify-content: center;
    perspective: 2400px;
}

.sbv__book {
    position: relative;
    width: var(--sb-bw);
    height: var(--sb-bh);
    transform-style: preserve-3d;
    /* Closed, the book sits over one half of the stage; opening slides it to centre. The prototype's
       fixed 840x560 is gone — the size comes from the stage. */
    transform: translateX(25%);
    transition: transform .6s ease;
}

.sbv--rtl .sbv__book { transform: translateX(-25%); }
.sbv--open .sbv__book { transform: translateX(0); }

/* ── Papers ─────────────────────────────────────────────────────────────── */

.sbv__paper {
    position: absolute;
    top: 0;
    width: 50%;
    height: 100%;
    transform-style: preserve-3d;
    transform-origin: left center;
    transition: transform 1.2s cubic-bezier(.25, 1, .5, 1);
    right: 0;
}

/* RTL mirrors the model rather than the pixels: papers hinge on the other edge and turn the other
   way, but the artwork is never flipped — it is drawn the way it was painted (§8.3). */
.sbv--rtl .sbv__paper {
    right: auto;
    left: 0;
    transform-origin: right center;
}

.sbv__paper--flipped { transform: rotateY(-180deg); }
.sbv--rtl .sbv__paper--flipped { transform: rotateY(180deg); }

.sbv__face {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    overflow: hidden;
    background: var(--sb-paper);
}

.sbv__face--back { transform: rotateY(180deg); }

/* ── Spine, page edges and curve lighting ───────────────────────────────────
   Ported from the flip prototype. Three things together make a page look like paper:

     1. a gradient per face — deep crevice at the spine, a bright highlight just outside it, flat
        across the middle, a slight drop-off at the outer edge;
     2. stacked-page edges, faked with layered box-shadows on the outer side, which is what gives
        the book thickness;
     3. a dark crack behind the centre, grounding the two halves as one object.

   The artwork must not draw any of this — the prompts forbid a painted fold precisely so there is
   only ever one. */
.sbv__face::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    /* Above the artwork, below the words: at the prototype's z-index the crevice fell across the
       start of every line. */
    z-index: 1;
}

.sbv__face--front::after {
    background: linear-gradient(to right,
        var(--sb-spine-deep) 0%,
        var(--sb-spine-mid) 4%,
        var(--sb-spine-hi) 9%,
        transparent 24%,
        transparent 72%,
        var(--sb-edge-drop) 100%);
}

.sbv__face--back::after {
    background: linear-gradient(to left,
        var(--sb-spine-deep) 0%,
        var(--sb-spine-mid) 4%,
        var(--sb-spine-hi) 9%,
        transparent 24%,
        transparent 72%,
        var(--sb-edge-drop) 100%);
}

/* RTL mirrors the model, never the pixels: the spine moves to the other side, so the lighting has
   to follow it. */
.sbv--rtl .sbv__face--front::after {
    background: linear-gradient(to left,
        var(--sb-spine-deep) 0%,
        var(--sb-spine-mid) 4%,
        var(--sb-spine-hi) 9%,
        transparent 24%,
        transparent 72%,
        var(--sb-edge-drop) 100%);
}

.sbv--rtl .sbv__face--back::after {
    background: linear-gradient(to right,
        var(--sb-spine-deep) 0%,
        var(--sb-spine-mid) 4%,
        var(--sb-spine-hi) 9%,
        transparent 24%,
        transparent 72%,
        var(--sb-edge-drop) 100%);
}

/* Rounded outer corners and the stack of pages behind each face. */
.sbv__face--front {
    border-radius: 0 8px 8px 0;
    box-shadow: 3px 5px 15px var(--sb-stack-shadow),
                3px 0 0 var(--sb-stack),
                4px 0 5px var(--sb-stack-soft);
}

.sbv__face--back {
    border-radius: 8px 0 0 8px;
    box-shadow: -3px 5px 15px var(--sb-stack-shadow),
                -3px 0 0 var(--sb-stack),
                -4px 0 5px var(--sb-stack-soft);
}

.sbv--rtl .sbv__face--front {
    border-radius: 8px 0 0 8px;
    box-shadow: -3px 5px 15px var(--sb-stack-shadow),
                -3px 0 0 var(--sb-stack),
                -4px 0 5px var(--sb-stack-soft);
}

.sbv--rtl .sbv__face--back {
    border-radius: 0 8px 8px 0;
    box-shadow: 3px 5px 15px var(--sb-stack-shadow),
                3px 0 0 var(--sb-stack),
                4px 0 5px var(--sb-stack-soft);
}

/* The centre shadow. A soft band either side of the spine rather than the prototype's solid black
   sliver: that was a hard edge sitting behind two pages that already darken toward the fold, and
   the three together read as a black stripe down the middle rather than as a bound book. */
.sbv__book::before {
    content: "";
    position: absolute;
    top: -1%;
    bottom: -1%;
    left: 50%;
    width: 5.6%;
    transform: translateX(-50%);
    background: linear-gradient(to right,
        transparent 0%,
        var(--sb-spine-crack) 42%,
        var(--sb-spine-crack) 58%,
        transparent 100%);
    z-index: -1;
}

/* One leaf, so there is no spine to model. */
.sbv--single .sbv__book::before { display: none; }

.sbv__page {
    position: absolute;
    inset: 0;
    overflow: hidden;
}

/* One <img> at 200% width, shifted by half. Positioned by JS so decode() is available — a
   background-image cannot be awaited, and a page would turn onto a blank. */
.sbv__art {
    /* A long-press drag on an image is the browser's own gesture, and it fights the page turn. */
    -webkit-user-drag: none;
    user-select: none;
    -webkit-touch-callout: none;
    position: absolute;
    top: 0;
    height: 100%;
    max-width: none;
    object-fit: cover;
    pointer-events: none;
}

/* ── Text boxes ─────────────────────────────────────────────────────────── */

.sbv__box {
    position: absolute;
    box-sizing: border-box;
    z-index: 2;
    overflow: hidden;
    /* The panel's padding applies whether or not the panel is drawn, so switching it off never moves
       the text or changes the pagination (§3.4). */
    padding: 2.4% 3%;
    border-radius: 10px;
    font-size: calc(var(--sb-pw) * var(--sb-scale) * var(--sb-fit));
    line-height: var(--sb-line);
    text-align: start;
    font-family: "Amiri", "Noto Naskh Arabic", "Inter", Georgia, "Times New Roman", serif;
}

.sbv__box--center { text-align: center; }

/* Hugs the words, with the panel's padding on every side, up to the width set for it (2026-11-07).
   The text still wraps at the set width, so the pagination and the fit pass are unchanged; only a
   short page's panel shrinks. Right to left, the box keeps its right edge and shrinks leftward. */
.sbv__box--hug { width: fit-content !important; max-width: var(--sb-boxw); }
.sbv--rtl .sbv__box--hug { left: auto !important; right: calc(100% - var(--sb-boxx) - var(--sb-boxw)); }

.sbv__box--none { background: none; }

/* Only when there is no panel: over parchment or glass this would just look smudged.

   An outline, not a drop shadow. A soft glow works when the artwork behind it is consistently
   darker (or lighter) than the text, and a picture book page is not — one line crosses sky, the
   next crosses a figure, and a glow tuned for either fails on the other. A contact outline traced
   around every glyph does not care what is behind it: the letter always has its own edge.

   Built from four 1px offsets plus two soft passes rather than -webkit-text-stroke, because stroke
   thickens the letterforms themselves and Amiri's thin joins close up at reading size. */
.sbv__box--none.sbv__box--ink-dark:not(.sbv__box--halo-off) {
    text-shadow:
        -1px 0 0 var(--sb-halo-light),  1px 0 0 var(--sb-halo-light),
         0 -1px 0 var(--sb-halo-light), 0  1px 0 var(--sb-halo-light),
         0 0 3px var(--sb-halo-light),  0 0 9px var(--sb-halo-light);
}

.sbv__box--none.sbv__box--ink-custom:not(.sbv__box--halo-off) {
    /* --sb-halo-pick is set by storybook.js from the chosen colour's own luminance, so a pale ink
       gets a dark outline and a dark one gets a pale outline without the admin having to think
       about it. Falls back to the dark halo, which suits the light inks most pages use. */
    text-shadow:
        -1px 0 0 var(--sb-halo-pick, var(--sb-halo-dark)),  1px 0 0 var(--sb-halo-pick, var(--sb-halo-dark)),
         0 -1px 0 var(--sb-halo-pick, var(--sb-halo-dark)), 0  1px 0 var(--sb-halo-pick, var(--sb-halo-dark)),
         0 0 3px var(--sb-halo-pick, var(--sb-halo-dark)),  0 0 9px var(--sb-halo-pick, var(--sb-halo-dark));
}

.sbv__box--none.sbv__box--ink-light:not(.sbv__box--halo-off) {
    text-shadow:
        -1px 0 0 var(--sb-halo-dark),  1px 0 0 var(--sb-halo-dark),
         0 -1px 0 var(--sb-halo-dark), 0  1px 0 var(--sb-halo-dark),
         0 0 3px var(--sb-halo-dark),  0 0 9px var(--sb-halo-dark);
}
.sbv__box--parchment { background: var(--sb-parchment); }
.sbv__box--glass {
    background: var(--sb-glass);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.sbv__box--ink-dark  { color: var(--sb-ink-dark); }
.sbv__box--ink-light { color: var(--sb-ink-light); }

/* Past the shrink floor the box scrolls behind a fade rather than clipping: a reader must never
   lose the end of a sentence, however badly a spread was overfilled. */
.sbv__box--overflow {
    overflow-y: auto;
    -webkit-mask-image: linear-gradient(to bottom, #000 82%, transparent 100%);
    mask-image: linear-gradient(to bottom, #000 82%, transparent 100%);
}

.sbv__unit { margin: 0 0 .55em; }

/* A single newline was an authored break, not a paragraph: it gets a smaller gap, so text is laid
   out the way it was written. */
.sbv__unit--line { margin-bottom: .25em; }

/* A safety-net cut is not an authored break at all, so it gets none. */
.sbv__unit--sentence { margin-bottom: 0; }

/* last-of-type, not last-child: the editor appends three drag handles after the units, so the final
   <p> is never the last child there and kept a trailing margin inside the box. */
.sbv__unit:last-of-type { margin-bottom: 0; }

.sbv__box--display {
    font-size: calc(var(--sb-pw) * .075 * var(--sb-fit));
    line-height: 1.25;
    font-weight: 600;
}

/* ── Opening pages ──────────────────────────────────────────────────────── */

/* The opening pages' background.

   Drawn in CSS rather than shipped as front-matter.jpg. The spec called for a generated image, but
   this is a paper tone and a geometric border — gradients do it exactly, at no download, at every
   resolution, and with no asset for someone to have to produce before the feature can ship. An
   image would also have to be halved across the spine like the artwork; this simply is not. */
.sbv__page--bismillah,
.sbv__page--title {
    background:
        repeating-linear-gradient(45deg,
            var(--sb-ornament) 0 2px, transparent 2px 9px) border-box,
        var(--sb-paper);
    /* The ornament is a border only; the middle stays plain because text is printed over it. */
    border: 7% solid transparent;
    box-sizing: border-box;
}

.sbv__frontmatter {
    position: absolute;
    inset: 8% 10%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .6em;
    text-align: center;
    color: var(--sb-ink-dark);
}

.sbv__bismillah {
    font-family: "Amiri", "Noto Naskh Arabic", Georgia, serif;
    font-size: calc(var(--sb-pw) * .085);
    line-height: 1.6;
}

.sbv__bismillah-tr {
    font-family: "Inter", system-ui, sans-serif;
    font-size: calc(var(--sb-pw) * .032);
    opacity: .75;
}

.sbv__title {
    font-family: "Amiri", "Noto Naskh Arabic", Georgia, serif;
    font-size: calc(var(--sb-pw) * .07);
    line-height: 1.3;
}

.sbv__agegroup {
    font-family: "Inter", system-ui, sans-serif;
    font-size: calc(var(--sb-pw) * .03);
    opacity: .7;
}

/* ── Reader controls ────────────────────────────────────────────────────── */

.sbv__nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 50%;
    /* Solid enough to read against any page. --sb-glass at .5 opacity worked over a calm sky and
       vanished over a detailed one, which is most of a picture book. The blur plus a hairline rim
       keeps it legible over both light and dark artwork without a hard-edged button spoiling the
       page. */
    background: var(--sb-nav-bg);
    color: var(--sb-ink-light);
    box-shadow: 0 2px 10px rgba(0, 0, 0, .45), inset 0 0 0 1px rgba(253, 250, 243, .28);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    font-size: 1.2rem;
    cursor: pointer;
    opacity: .85;
    transition: opacity .2s ease;
}

.sbv__nav:hover, .sbv__nav:focus-visible { opacity: 1; }

/* Pinned to the BOOK's edge, not the screen's. The stage centres a book of --sb-bw inside a wider
   viewport, so screen-edge arrows floated out in the letterbox band with nothing under them; over
   the artwork they read as part of the book. Logical properties so RTL puts "previous" on the
   right, which is where the previous page actually is in an RTL book. */
.sbv__nav--prev { inset-inline-start: calc(50% - (var(--sb-bw, 100%) / 2) + 12px); }
.sbv__nav--next { inset-inline-end:   calc(50% - (var(--sb-bw, 100%) / 2) + 12px); }

/* One leaf, so the book is half as wide and the arrows come in to match. */
.sbv--single .sbv__nav--prev { inset-inline-start: calc(50% - (var(--sb-bw, 100%) / 4) + 12px); }
.sbv--single .sbv__nav--next { inset-inline-end:   calc(50% - (var(--sb-bw, 100%) / 4) + 12px); }

/* ── Editor ─────────────────────────────────────────────────────────────── */

/* Flat, one spread at a time: the admin is positioning text, and a 3D turn between every check
   would be in the way. */
.sbv--editor .sbv__book { transform: none; }
.sbv--editor .sbv__paper { transition: none; }

.sbv--editor .sbv__box {
    outline: 1px dashed var(--sb-paper-edge);
    /* The whole box is the move grip (2026-11-07). */
    cursor: move;
    user-select: none;
}

/* The reserved height shows while the height handle is dragged, and the reserved width while the
   width handle is, so each handle visibly does something even when the words are shorter or
   narrower. A plain move keeps the box hugging its words, as it is at rest (2026-11-07). */
.sbv--editor .sbv__box--drag-h { min-height: var(--sb-boxh, auto); }
.sbv--editor .sbv__box--drag-w { min-width: var(--sb-boxw, auto); }

.sbv--editor .sbv__box--dragging { outline-style: solid; }

.sbv__handle {
    position: absolute;
    /* Above the words: a grip a paragraph can cover is a grip nobody finds. */
    z-index: 3;
    background: var(--sb-ink-dark);
    opacity: .55;
    border-radius: 3px;
}

.sbv__handle:hover { opacity: 1; }

/* Inside the box, not outside it. .sbv__box clips with overflow:hidden, so handles hung past its
   edges were painted away entirely — the boxes looked undraggable because their grips were
   invisible, while the bottom one still counted toward scrollHeight and faked an overflow. */
.sbv__handle--move {
    top: 3px;
    left: 50%;
    width: 30px;
    height: 7px;
    margin-left: -15px;
    cursor: move;
}

.sbv__handle--w {
    top: 50%;
    inset-inline-end: 3px;
    width: 7px;
    height: 30px;
    margin-top: -15px;
    cursor: ew-resize;
}

.sbv__handle--h {
    inset-block-end: 3px;
    left: 50%;
    width: 30px;
    height: 7px;
    margin-left: -15px;
    cursor: ns-resize;
}

/* Reduced motion removes the ANIMATION, not the geometry.
   The rotation on .sbv__paper is what places a turned page on the left half of the book, and the
   translate on .sbv__book is what centres an open one — both are static layout that happens to be
   expressed as a transform. Cancelling them with `transform: none !important` and hiding flipped
   papers with `opacity: 0` left every reduced-motion reader looking at a single page on the right
   side of the screen, with no way to reach the other half. Dropping the transitions alone gives the
   same result the setting asks for: pages change without a turn being animated. */
@media (prefers-reduced-motion: reduce) {
    .sbv__book, .sbv__paper { transition: none; }
}

/* ── Portrait: one page at a time ───────────────────────────────────────────
   The same pages in the same reading order, with the book narrowed to a single leaf. Not a separate
   layout — the paper model is untouched, so a rotation mid-story keeps the reader's place. */

.sbv--single .sbv__book {
    width: calc(var(--sb-bw) / 2);
    transform: none;
}

.sbv--single .sbv__paper {
    width: 100%;
}

/* No fold and no stack: there is no facing page for either to run against. */
.sbv--single .sbv__face--front,
.sbv--single .sbv__face--back {
    box-shadow: none;
    border-radius: 8px;
}

.sbv--single .sbv__face::after { background: none; }
