:root {
    --page-bg: #faf8f0;
    --page-text: #2e3440;
    --page-text-light: #636e7b;
    --page-border: #e8e4da;
    --cover-shadow: rgba(0,0,0,0.3);
    /* Set by flipbook.js from the current page size */
    --fb-font: 14px;
    --fb-pad-t: 36px;
    --fb-pad-x: 32px;
    --fb-pad-b: 44px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: #1a1a2e;
    color: #ccc;
    height: 100vh;
    /* svh = height with the mobile URL bar showing, so the bar sliding in
       and out doesn't resize (and re-paginate) the book mid-read */
    height: 100svh;
    margin: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
}

/* --- Top bar --- */
.fb-topbar {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: rgba(0,0,0,0.25);
    flex-shrink: 0;
}
.fb-topbar a {
    color: #aaa;
    text-decoration: none;
    font-size: 0.85em;
    transition: color 0.15s;
}
.fb-topbar a:hover { color: #fff; }
.fb-topbar .fb-title {
    font-size: 0.95em;
    color: #ddd;
    font-weight: 600;
    text-align: center;
    flex: 1;
}

/* --- Book container --- */
.fb-stage {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    width: 100%;
    min-height: 0;
    overflow: hidden;
}

.fb-book-wrap {
    position: relative;
    /* width/height set by flipbook.js to exactly one page or one spread */
    flex-shrink: 0;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    overflow: hidden;
}

.fb-book {
    margin: 0 auto;
}

/*
 * StPageFlip injects .stf__* styles, but its bundled CSS has a typo
 * (.sft__wrapper instead of .stf__wrapper). Without these rules, pages
 * can fall out of the book during a flip and appear below the spread.
 */
.stf__parent,
#flipbook {
    position: relative;
    overflow: hidden;
}

.stf__wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.stf__block {
    position: absolute;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.stf__item {
    display: none;
    position: absolute !important;
    transform-style: preserve-3d;
    overflow: hidden;
}

/* --- Page base --- */
.fb-page {
    background: var(--page-bg);
    color: var(--page-text);
    font-family: Georgia, 'Times New Roman', serif;
    font-size: var(--fb-font);
    line-height: 1.65;
    padding: var(--fb-pad-t) var(--fb-pad-x) var(--fb-pad-b);
    overflow: hidden;
    /* Do NOT set position here — StPageFlip must control absolute/simple layout.
       A relative position (especially on .picture-page) pulls flipping pages
       into document flow and parks them below the book. */
    border: 1px solid var(--page-border);
}

.fb-page .page-num {
    position: absolute;
    bottom: 14px;
    font-family: 'Segoe UI', system-ui, sans-serif;
    font-size: 11px;
    color: var(--page-text-light);
}
.fb-page .page-num.left { left: var(--fb-pad-x); }
.fb-page .page-num.right { right: var(--fb-pad-x); }
.fb-page .page-num.center { left: 0; right: 0; text-align: center; }

/* --- Cover pages --- */
.fb-cover {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 30px;
    color: #fff;
    border: none;
    font-family: 'Segoe UI', system-ui, sans-serif;
}
.fb-cover .cover-series {
    font-size: 0.7em;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    opacity: 0.7;
    margin-bottom: 16px;
}
.fb-cover .cover-title {
    font-size: 1.6em;
    font-weight: 700;
    line-height: 1.25;
    margin-bottom: 8px;
    text-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.fb-cover .cover-subtitle {
    font-size: 0.95em;
    font-weight: 400;
    opacity: 0.85;
    margin-bottom: 20px;
}
.fb-cover .cover-author {
    font-size: 0.8em;
    opacity: 0.65;
    letter-spacing: 0.08em;
}
.fb-cover .cover-badge {
    display: inline-block;
    margin-top: 18px;
    padding: 4px 16px;
    border-radius: 20px;
    background: rgba(255,255,255,0.2);
    font-size: 0.75em;
    letter-spacing: 0.05em;
}

.fb-cover .cover-inner,
.fb-back-cover .cover-inner {
    /* StPageFlip forces display:block on pages, so the page itself can't be
       a flex container; center the cover content in an inner layer instead. */
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 30px;
}

.cover-book1 { background: linear-gradient(135deg, #f6b93b, #e58e26); }
.cover-book2 { background: linear-gradient(135deg, #38ada9, #1a7a76); }
.cover-book3 { background: linear-gradient(135deg, #6c5ce7, #4834a8); }
.cover-book4 { background: linear-gradient(135deg, #2d3436, #4a6fa5); }

.fb-back-cover {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 30px;
    font-family: 'Segoe UI', system-ui, sans-serif;
    border: none;
}
.fb-back-cover .back-msg {
    font-style: italic;
    font-size: 1em;
    line-height: 1.6;
    max-width: 320px;
    opacity: 0.85;
}
.fb-back-cover .back-author {
    margin-top: 24px;
    font-size: 0.8em;
    opacity: 0.6;
}

/* --- Content styling inside pages --- */
.fb-page h2 {
    font-size: 1.25em;
    margin: 0 0 14px;
    color: #3b4252;
    font-weight: 700;
    border-bottom: 1px solid var(--page-border);
    padding-bottom: 8px;
}
.fb-page h3 {
    font-size: 1.05em;
    margin: 12px 0 8px;
    color: #4a5568;
}
.fb-page p {
    margin: 0 0 10px;
    text-align: justify;
    hyphens: auto;
}
.fb-page img {
    display: block;
    max-width: 90%;
    max-height: 55%;
    margin: 10px auto;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    object-fit: contain;
}
.fb-page blockquote {
    margin: 8px 0;
    padding: 8px 14px;
    border-left: 3px solid #d8dee9;
    background: #f0ede5;
    font-size: 0.92em;
    border-radius: 0 4px 4px 0;
}
.fb-page em { font-style: italic; }
.fb-page strong { font-weight: 700; color: #3b4252; }
.fb-page hr {
    border: none;
    border-top: 1px solid var(--page-border);
    margin: 14px 0;
}
.fb-page ul, .fb-page ol {
    margin: 6px 0 10px 20px;
    font-size: 0.95em;
}
.fb-page table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85em;
    margin: 8px 0;
}
.fb-page th, .fb-page td {
    padding: 5px 8px;
    border: 1px solid var(--page-border);
    text-align: left;
}
.fb-page th { background: #f0ede5; font-weight: 600; }

/* Callout boxes (breathing, silver lining, etc.) */
.fb-page .callout {
    margin: 10px 0;
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 0.92em;
}
.fb-page .callout-breathing { background: #e8f4f8; border-left: 3px solid #5e9ccd; }
.fb-page .callout-silver { background: #fdf3d4; border-left: 3px solid #e8b931; }
.fb-page .callout-buddy { background: #e8f8e8; border-left: 3px solid #38ada9; }
.fb-page .callout-storm { background: #f0e8f8; border-left: 3px solid #6c5ce7; }

/* --- Picture book pages (full-bleed) --- */
.fb-page.picture-page {
    padding: 0;
    overflow: hidden;
    border: none;
    background: #1a1520;
}
.fb-page.picture-page img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    margin: 0;
    max-width: none;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
}
.fb-page.picture-page .page-text {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 40px 24px 20px;
    background: linear-gradient(to top, rgba(15,10,25,0.82) 0%, rgba(15,10,25,0.5) 60%, transparent 100%);
    color: #fff;
    font-family: Georgia, 'Times New Roman', serif;
    font-size: 1.1em;
    line-height: 1.55;
    text-align: center;
    text-shadow:
        0 0 12px rgba(218,165,32,0.8),
        0 0 28px rgba(218,165,32,0.45),
        0 0 4px rgba(255,255,255,0.5);
    z-index: 1;
}
.fb-page.picture-page .page-text strong {
    color: #ffd700;
    text-shadow:
        0 0 14px rgba(218,165,32,0.9),
        0 0 32px rgba(218,165,32,0.5),
        0 0 4px rgba(255,255,255,0.6);
}
/* Text at top variant */
.fb-page.picture-page.text-top .page-text {
    bottom: auto;
    top: 0;
    padding: 20px 24px 40px;
    background: linear-gradient(to bottom, rgba(15,10,25,0.82) 0%, rgba(15,10,25,0.5) 60%, transparent 100%);
}
/* Full-bleed covers for picture book */
.fb-page.fb-front-cover-pic,
.fb-page.fb-back-cover-pic {
    padding: 0;
    border: none;
}
.fb-page.fb-back-cover-pic .page-text {
    font-style: italic;
}

/* --- Controls --- */
.fb-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 14px 20px;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.fb-btn {
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.15);
    color: #ccc;
    padding: 8px 18px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.88em;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}
.fb-btn:hover { background: rgba(255,255,255,0.2); color: #fff; }
.fb-btn:disabled { opacity: 0.35; cursor: default; }

.fb-page-info {
    color: #999;
    font-size: 0.85em;
    min-width: 120px;
    text-align: center;
}

.fb-chapter-select {
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.15);
    color: #ccc;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.85em;
    font-family: inherit;
    cursor: pointer;
    max-width: 220px;
}
.fb-chapter-select option { background: #2d2d44; color: #ccc; }

/* --- Loading --- */
.fb-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60vh;
    color: #888;
    font-size: 1.1em;
}

/* --- Picture pages on single-page layouts ---
   Show the whole illustration (square and tall art both occur) instead of
   cropping it to fit a page shape it wasn't drawn for. */
body.fb-mode-phone .fb-page.picture-page img,
body.fb-mode-square .fb-page.picture-page img {
    object-fit: contain;
}

/* --- Site header/footer (injected by assemble_site.py) on reader pages ---
   The body is a fixed-height, non-scrolling column, so every pixel the
   site chrome takes comes out of the book. Selectors are prefixed with
   "body" to outrank the injected <style> blocks that come later. */
body .ax-nav {
    position: relative;
    margin: 0;
    padding: 6px 20px;
    flex-shrink: 0;
}
body .ax-nav .ax-brand-logo { height: 36px; margin: 0; }
body .ax-footer {
    margin: 0;
    padding: 6px 20px;
    flex-shrink: 0;
}
body .ax-footer .ax-foot-inner { justify-content: center; gap: 10px 20px; }
body .ax-footer .ax-foot-brand { display: none; }
body .ax-footer .ax-foot-links a { font-size: 0.78rem; }
body .ax-footer .ax-foot-legal { display: none; }

/* --- Responsive --- */
@media (max-width: 768px) {
    .fb-stage { padding: 10px; }
    .fb-cover .cover-title { font-size: 1.3em; }
    .fb-controls { gap: 8px; padding: 8px 12px; }
    .fb-btn { padding: 8px 16px; font-size: 0.88em; min-height: 44px; }
    .fb-chapter-select { min-height: 44px; }
}

/* Phones (portrait, or on their side): reading mode.
   Hide the site header and footer; keep only a slim bar with the way back. */
@media (max-width: 640px), (max-height: 500px) {
    body .ax-nav,
    body .ax-footer { display: none; }

    .fb-topbar { padding: 4px 8px; gap: 8px; }
    .fb-topbar a { font-size: 0.8em; white-space: nowrap; }
    .fb-topbar .fb-title {
        font-size: 0.8em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
    }
    .fb-topbar .fb-btn { min-height: 34px; padding: 4px 10px; }

    .fb-stage { padding: 4px; }

    .fb-controls {
        flex-wrap: nowrap;
        gap: 6px;
        padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
        width: 100%;
    }
    .fb-btn { padding: 8px 14px; min-height: 44px; }
    .fb-page-info { min-width: 0; font-size: 0.8em; white-space: nowrap; }
    .fb-chapter-select { max-width: 34vw; min-width: 0; font-size: 0.8em; padding: 6px 8px; }
}

/* Phones on their side: every vertical pixel counts. */
@media (max-height: 500px) and (orientation: landscape) {
    .fb-topbar { padding: 2px 8px; }
    .fb-topbar .fb-btn { min-height: 28px; padding: 2px 8px; }
    .fb-btn { min-height: 36px; padding: 4px 12px; }
    .fb-chapter-select { min-height: 36px; }
}
