/* Page turning is handled by StPageFlip (vendor/page-flip). Pages are laid
   out at a fixed 450x600 design size and scaled with --s, except on phones,
   where they reflow at full text size and scroll. */

:root {
    --navy: #1D4F91;
    --bg: oklch(0.94 0.006 85);           /* warm desk grey, darker than the paper */
    --paper: oklch(0.99 0.006 85);
    --ink: oklch(0.38 0.01 255);          /* body text, ~9:1 on paper */
    --heading: var(--navy);
    --muted: oklch(0.48 0.01 255);        /* labels and page numbers, ~6:1 */
    --line: rgb(29 79 145 / 0.16);
    --tint: rgb(29 79 145 / 0.06);
    --tint-strong: rgb(29 79 145 / 0.13);
    --endpaper: var(--navy);
    --cta-bg: var(--navy);
    --cta-fg: #fff;
    --spine: rgb(0 0 0 / 0.16);
    --font: 'Inter', system-ui, -apple-system, sans-serif;
}

html.dark {
    --bg: oklch(0.17 0.005 255);
    --paper: oklch(0.25 0.006 255);       /* lighter surface carries the depth */
    --ink: oklch(0.88 0.005 255);
    --heading: #AECBFA;
    --muted: oklch(0.72 0.01 255);
    --line: rgb(174 203 250 / 0.2);
    --tint: rgb(174 203 250 / 0.08);
    --tint-strong: rgb(174 203 250 / 0.16);
    --endpaper: #1A3358;
    --cta-bg: #AECBFA;
    --cta-fg: #0E2546;
    --spine: rgb(0 0 0 / 0.4);
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; }

body {
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.4s ease;
}

h1, h2, h3, h4 { text-wrap: balance; }
p { text-wrap: pretty; }

/* ---------- Stage ---------- */

.stage {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 22px;
    padding: 64px 16px calc(24px + env(safe-area-inset-bottom));
    overflow: hidden;
}

.book-frame {
    width: var(--frame-w, 900px);
    transition: transform 0.8s cubic-bezier(0.2, 0.7, 0.2, 1);
}

/* Closed book: the cover sits on the right half of a landscape spread,
   so slide the frame to center it (and the back cover, which sits left). */
.book-frame.is-landscape.at-cover { transform: translateX(-25%); }
.book-frame.is-landscape.at-back { transform: translateX(25%); }

/* ---------- Pages ---------- */

.pg {
    background-color: var(--paper);
    overflow: hidden;
}

/* Gutter shading toward the spine */
.pg.\-\-left { border-radius: 6px 0 0 6px; box-shadow: inset -22px 0 26px -24px var(--spine); }
.pg.\-\-right { border-radius: 0 6px 6px 0; box-shadow: inset 22px 0 26px -24px var(--spine); }

.pc,
.cover-content {
    position: relative;
    width: 450px;
    height: 600px;
    transform: scale(var(--s, 1));
    transform-origin: 0 0;
}

.pc {
    display: flex;
    flex-direction: column;
    padding: 36px 40px 52px;
    overflow: hidden;
}

.pc-centered { justify-content: center; }

/* ---------- Covers ---------- */

.pg-cover {
    background: var(--navy) url('cover_bg.jpg') center / cover no-repeat;
    color: #fff;
}

.pg-cover::before {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(in oklch to bottom, rgb(29 79 145 / 0.42), rgb(0 40 110 / 0.86));
}

.pg-cover.\-\-right { border-radius: 4px 10px 10px 4px; }

.cover-content {
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    text-align: center;
}

.cover-content h1 {
    text-wrap: wrap;
    margin: 0 0 16px;
    font-size: 38px;
    font-weight: 700;
    line-height: 1.12;
    letter-spacing: -0.02em;
}

.nowrap { white-space: nowrap; }

.author { margin: 0; font-size: 18px; font-weight: 600; color: #fff; }

.university-title { margin: 4px 0 36px; font-size: 16px; color: rgb(255 255 255 / 0.85); }

.open-book-btn {
    font: 600 16px/1 var(--font);
    color: var(--navy);
    background: #fff;
    border: 0;
    border-radius: 999px;
    padding: 14px 28px;
    cursor: pointer;
    box-shadow: 0 6px 18px rgb(0 0 0 / 0.22);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.pg-endpaper,
.pg-back {
    background-color: var(--endpaper);
}

.pg-back.\-\-left { border-radius: 10px 4px 4px 10px; }

.cover-credit {
    position: absolute;
    left: calc(40px * var(--s, 1));
    right: calc(40px * var(--s, 1));
    bottom: calc(28px * var(--s, 1));
    margin: 0;
    font-size: calc(12px * var(--s, 1));
    line-height: 1.45;
    color: rgb(255 255 255 / 0.8);
}

/* ---------- Type ---------- */

.chapter-title {
    margin: 0 0 20px;
    font-size: 28px;
    font-weight: 700;
    line-height: 1.15;
    letter-spacing: -0.015em;
    color: var(--heading);
}

h3 {
    margin: 0 0 12px;
    font-size: 19px;
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: -0.01em;
    color: var(--heading);
}

.chapter-title + h3 { margin-top: 0; }

h4 {
    margin: 0 0 4px;
    font-size: 14.5px;
    font-weight: 700;
    color: var(--heading);
}

.pc p {
    margin: 0 0 10px;
    font-size: 14.4px;
    line-height: 1.5;
    color: var(--ink);
}

.pc .subtitle {
    margin: -10px 0 14px;
    font-size: 16px;
    font-weight: 600;
    color: var(--muted);
}

.chapter-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
}

.chapter-row .chapter-title { margin: 0; }

.cta {
    flex: none;
    font-size: 13px;
    font-weight: 600;
    color: var(--cta-fg);
    background: var(--cta-bg);
    border-radius: 999px;
    padding: 8px 14px;
    text-decoration: none;
    white-space: nowrap;
}

/* ---------- Blocks ---------- */

.callout {
    margin-top: 14px;
    padding: 14px 16px;
    border-radius: 10px;
    background: var(--tint);
}

.callout h4 { font-size: 12px; font-weight: 600; color: var(--muted); }

.pc .callout p { margin: 0; font-size: 13.6px; line-height: 1.45; }

.quotes { display: flex; flex-direction: column; gap: 18px; margin-top: 6px; }

.quote { margin: 0; }

.quote figcaption {
    margin-bottom: 4px;
    font-size: 12px;
    line-height: 1.4;
    color: var(--muted);
}

.quote blockquote {
    margin: 0;
    font-size: 14.4px;
    line-height: 1.5;
    color: var(--heading);
}

/* Routine care / grey zone / ER as one segmented bar (inner radius 8 = outer 12 - padding 4) */
.spectrum {
    display: grid;
    grid-template-columns: 1fr 1.55fr 0.6fr;
    gap: 4px;
    padding: 4px;
    margin: 2px 0 18px;
    border-radius: 12px;
    background: var(--tint);
}

.spectrum span {
    padding: 10px 6px;
    border-radius: 8px;
    font-size: 13px;
    text-align: center;
    color: var(--muted);
}

.spectrum .focus {
    background: var(--tint-strong);
    color: var(--heading);
    font-weight: 700;
}

.features section { margin-bottom: 14px; }
.pc .features p { margin: 0; }

.grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px 20px;
}

.grid .full { grid-column: 1 / -1; }

.pc .grid p { margin: 0; font-size: 13.8px; }

/* ---------- Figures ---------- */

.zoom-btn {
    display: block;
    width: 100%;
    margin: 0 0 14px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: none;
    cursor: zoom-in;
}

.zoom-btn img {
    pointer-events: none;   /* clicks land on the button, which the page flipper lets through */
    display: block;
    width: 100%;
    height: auto;
    max-height: 250px;
    object-fit: contain;
    border-radius: 8px;
}

.zoom-btn img.figure-tall { width: auto; max-width: 100%; height: 250px; margin: 0 auto; }
.zoom-btn img.figure-hero { max-height: 410px; }
.zoom-btn img.figure-short { max-height: 170px; }

.zoom-btn:focus-visible,
.nav-btn:focus-visible,
.toc-link:focus-visible,
.open-book-btn:focus-visible,
.theme-toggle:focus-visible,
.home-link:focus-visible,
.pc a:focus-visible {
    outline: 2px solid var(--heading);
    outline-offset: 3px;
}

html.dark .zoom-btn img { background: #fff; }

/* ---------- Readings ---------- */

.readings { margin-top: 16px; }

.readings h4 { font-size: 12px; font-weight: 600; color: var(--muted); }

.readings ul {
    display: flex;
    flex-direction: column;
    gap: 3px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.pc a:not(.cta) {
    font-size: 13.8px;
    color: var(--heading);
    text-decoration: underline;
    text-decoration-color: var(--line);
    text-decoration-thickness: 1px;
    text-underline-offset: 3px;
}

/* ---------- Contents ---------- */

.toc-list { margin: 4px 0 0; padding: 0; list-style: none; }

.toc-link {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    width: 100%;
    padding: 11px 0;
    border: 0;
    background: none;
    font: 500 17px/1.3 var(--font);
    color: var(--heading);
    text-align: left;
    cursor: pointer;
}

.toc-link span { pointer-events: none; }

.toc-page {
    font-size: 14px;
    font-variant-numeric: tabular-nums;
    color: var(--muted);
}

/* ---------- Page numbers ---------- */

.page-number {
    position: absolute;
    right: 40px;
    bottom: 20px;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: var(--muted);
}

.pg.\-\-left .page-number { right: auto; left: 40px; }

/* ---------- Navigation ---------- */

.nav-controls {
    display: flex;
    gap: 10px;
    transition: opacity 0.3s ease;
}

.nav-controls.is-hidden { opacity: 0; pointer-events: none; }

.nav-btn {
    padding: 9px 18px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--paper);
    font: 500 14.5px/1.2 var(--font);
    color: var(--heading);
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.nav-btn:disabled { opacity: 0.45; cursor: default; }

.home-link {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 50;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 40px;
    padding: 0 16px 0 13px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--paper);
    color: var(--heading);
    font-size: 15px;
    font-weight: 500;
    text-decoration: none;
}

.home-link svg { width: 18px; height: 18px; }

.theme-toggle {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 50;
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--paper);
    color: var(--heading);
    cursor: pointer;
}

.theme-toggle svg { width: 20px; height: 20px; }
.theme-toggle .moon-icon { fill: currentColor; }
html.dark .moon-icon,
html:not(.dark) .sun-icon { display: none; }

@media (hover: hover) {
    .nav-btn:hover:not(:disabled),
    .theme-toggle:hover,
    .home-link:hover { background: var(--heading); color: var(--paper); }
    .toc-link:hover span:first-child { text-decoration: underline; text-underline-offset: 4px; }
    .pc a:not(.cta):hover { text-decoration-color: currentColor; }
    .open-book-btn:hover { transform: translateY(-1px); box-shadow: 0 8px 22px rgb(0 0 0 / 0.26); }
}

/* ---------- Image zoom ---------- */

.zoom-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgb(10 14 22 / 0.86);
    cursor: zoom-out;
}

.zoom-overlay.open { display: flex; }

.zoom-overlay img {
    max-width: min(1200px, 100%);
    max-height: calc(100dvh - 96px);
    object-fit: contain;
    border-radius: 8px;
    background: #fff;
}

.zoom-close {
    position: absolute;
    top: 20px;
    right: 20px;
    padding: 8px 14px;
    border: 1px solid rgb(255 255 255 / 0.24);
    border-radius: 8px;
    background: rgb(255 255 255 / 0.12);
    font: 500 14px var(--font);
    color: #fff;
    cursor: pointer;
}

/* ---------- Phones: real-size text that reflows, pages scroll ---------- */

html.phone .stage {
    justify-content: flex-start;
    gap: 14px;
    padding: 64px 12px calc(16px + env(safe-area-inset-bottom));
}

html.phone .pc,
html.phone .cover-content {
    width: 100%;
    height: 100%;
    transform: none;
}

html.phone .pc {
    padding: 26px 22px 20px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

html.phone .pc p,
html.phone .quote blockquote { font-size: 15.5px; }

html.phone .page-number {
    position: static;
    align-self: flex-end;
    margin-top: auto;
    padding-top: 14px;
}

html.phone .pg.\-\-left .page-number { align-self: flex-start; }

html.phone .cover-credit { left: 24px; right: 24px; bottom: 20px; font-size: 12px; }

html.phone .nav-btn { padding: 9px 14px; }

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