/*
 * Dedicated phone composition.
 *
 * The desktop experience is authored on a 16:9 stage. Earlier mobile rules
 * only shrank that stage, which made type, controls, and each half of the
 * manuscript spread too small on iPhone. This layer is intentionally loaded
 * last and only changes compact viewports.
 */

.book-mobile-side-toggle {
    display: none;
}

@media screen and (max-width: 768px) {
    :root {
        --phone-safe-top: env(safe-area-inset-top, 0px);
        --phone-safe-right: env(safe-area-inset-right, 0px);
        --phone-safe-bottom: env(safe-area-inset-bottom, 0px);
        --phone-safe-left: env(safe-area-inset-left, 0px);
        --phone-gutter: clamp(16px, 5vw, 24px);
        --phone-viewport-height: var(--favor-stable-viewport-height, 100vh);
        --phone-viewport-width: var(--favor-visual-viewport-width, 100vw);
    }

    html,
    body {
        width: 100%;
        min-width: 0;
        min-height: 100%;
        overflow-x: clip;
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    body {
        touch-action: pan-y;
        overscroll-behavior-x: none;
    }

    .scroll-lock,
    .scroll-lock body {
        width: 100% !important;
        height: var(--phone-viewport-height) !important;
    }

    .viewport-wrapper,
    .gallery-section,
    .transition-master {
        width: 100%;
        height: 100vh;
        height: var(--phone-viewport-height);
        min-height: var(--phone-viewport-height);
    }

    /* Hero: use the phone viewport itself, not the 177.77vh landscape canvas. */
    .scene {
        top: 0;
        left: 0;
        width: 100%;
        height: 100vh;
        height: var(--phone-viewport-height);
        min-width: 0;
        min-height: 0;
        transform: none;
        transform-origin: 50% 50%;
    }

    .scene > .layer {
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .character-wrapper {
        top: -1.5%;
        left: -1.5%;
        width: 103%;
        height: 103%;
    }

    .character-wrapper .opening-couple-video {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: 50% 50%;
    }

    .loader-wordmark {
        inset: 0;
        width: 100%;
        height: 100%;
        padding-inline: var(--phone-gutter);
    }

    .loader-wordmark-the {
        font-size: clamp(1.65rem, 8.5vw, 2.35rem);
    }

    .loader-wordmark-title {
        max-width: 18rem;
        font-size: clamp(0.9rem, 4vw, 1.1rem);
        letter-spacing: 0.14em;
        line-height: 1.18;
    }

    .loader-path-window {
        top: 35%;
        height: var(--favor-loader-window-height, 42vh);
        background-size: 250vw auto;
    }

    .loader-text {
        bottom: calc(var(--phone-safe-bottom) + var(--favor-loader-copy-offset, 9vh));
        width: calc(100% - (2 * var(--phone-gutter)));
        font-size: 0.72rem;
        letter-spacing: 0.2em;
    }

    .threshold-layer {
        --gate-width: max(208vw, var(--favor-gate-width, 118vh));
        width: 100%;
        height: 100%;
    }

    .threshold-copy {
        top: 48%;
        left: 50%;
        width: calc(100% - (2 * var(--phone-gutter)));
        transform: translate(-50%, -50%);
        text-align: center;
    }

    .threshold-title {
        align-items: center;
    }

    .threshold-title-the {
        margin: 0 0 -0.18em;
        font-size: clamp(3.2rem, 16vw, 4.7rem);
    }

    .threshold-title-main {
        max-width: 8.5ch;
        font-size: clamp(3.3rem, 15.5vw, 5rem);
        line-height: 0.8;
    }

    .threshold-enter {
        min-width: 152px;
        min-height: 50px;
        margin-top: 2rem;
        padding: 0.85rem 1.8rem;
        font-size: 0.75rem;
    }

    .opening-end-title {
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .opening-end-title::before {
        width: 100%;
        height: 62%;
        background: linear-gradient(180deg, transparent, rgba(12, 5, 2, 0.38) 32%, rgba(12, 5, 2, 0.82));
    }

    .opening-end-title-lockup {
        top: auto;
        left: var(--phone-gutter);
        bottom: calc(var(--phone-safe-bottom) + 7.8rem);
        width: calc(100% - (2 * var(--phone-gutter)));
    }

    .opening-title-primary {
        font-size: clamp(3.55rem, 18vw, 5rem);
    }

    .opening-title-script {
        margin-left: 1.75rem;
        font-size: clamp(1.65rem, 8vw, 2.35rem);
    }

    .opening-title-decree {
        max-width: 12ch;
        white-space: normal;
        font-size: clamp(1.75rem, 8vw, 2.55rem);
        line-height: 0.92;
    }

    .opening-title-author {
        left: var(--phone-gutter);
        right: var(--phone-gutter);
        bottom: calc(var(--phone-safe-bottom) + 1.15rem);
        gap: 0.55rem;
    }

    .opening-author-name {
        font-size: clamp(2rem, 10vw, 2.8rem);
    }

    .opening-author-surname {
        min-height: 36px;
        display: inline-flex;
        align-items: center;
    }

    .hero-scroll-indicator {
        right: calc(var(--phone-safe-right) + 13px);
        bottom: calc(var(--phone-safe-bottom) + 16px);
    }

    .hero-botanical--spray-back {
        top: -9%;
        left: -45%;
        width: min(60vw, 235px) !important;
    }

    .hero-botanical--spray-front {
        right: -48%;
        bottom: -20%;
        width: min(72vw, 290px) !important;
    }

    /* Gallery: retain the cinematic horizontal passage but make one portrait
       the dominant phone-sized object at a time. */
    .gallery-section {
        overflow: hidden;
    }

    .gallery-scroll-wrapper {
        height: 100%;
        gap: 9vw;
        padding-left: 7vw;
        padding-right: 15vw;
    }

    .g-item-up .g-string { height: var(--favor-gallery-string-short, 8vh); }
    .g-item-down .g-string { height: var(--favor-gallery-string-long, 17vh); }

    .g-top-tape {
        width: 24px;
        height: 20px;
    }

    .g-card {
        width: 82vw;
        height: min(112vw, var(--favor-gallery-card-height, 61vh));
        padding: 12px;
        border-radius: 5px;
    }

    .g-side-tape {
        right: -9px;
        width: 19px;
        height: 42px;
    }

    .g-bottom-label {
        bottom: -28px;
        min-width: 132px;
        min-height: 58px;
        padding: 9px 22px;
    }

    .label-main {
        font-size: 0.78rem;
        letter-spacing: 0.15em;
    }

    .label-sub,
    .label-sub .tilde {
        font-size: 0.72rem;
    }

    .interlude-title {
        font-size: clamp(2.65rem, 13vw, 4rem);
    }

    .interlude-eng {
        max-width: 82vw;
        font-size: 0.78rem;
        line-height: 1.5;
        letter-spacing: 0.24em;
        text-align: center;
    }

    .gallery-botanical--spray {
        left: -68%;
        bottom: -23%;
        width: min(88vw, 360px) !important;
        opacity: 0.4;
    }

    .gallery-botanical--peony {
        right: -28%;
        top: -8%;
        width: min(34vw, 145px) !important;
        opacity: 0.38;
    }

    /* Witness: portrait above, readable conversation surface below. */
    .interactive-showcase,
    .cinematic-ui-wrapper {
        width: 100%;
        height: 100%;
    }

    .showcase-bg img,
    .fg-img,
    .witness-blink-frame {
        object-position: 68% 36%;
    }

    .cinematic-shadow {
        top: auto;
        bottom: 0;
        width: 100%;
        height: 69%;
        background: linear-gradient(to top, rgba(11, 7, 4, 0.99) 0%, rgba(14, 8, 4, 0.94) 58%, rgba(14, 8, 4, 0.46) 82%, transparent 100%);
    }

    .cinematic-ui-wrapper {
        display: block;
    }

    .chat-content-area {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 57%;
        max-height: none;
        padding: 0 var(--phone-gutter) calc(var(--phone-safe-bottom) + 2.9rem);
    }

    .chat-header {
        margin-bottom: 0.65rem;
    }

    .chat-title {
        font-size: clamp(1.2rem, 5.7vw, 1.6rem);
        letter-spacing: 0.12em;
    }

    .chat-subtitle {
        font-size: 0.72rem;
        line-height: 1.35;
    }

    .witness-ai-notice {
        margin-top: 0.55rem;
        font-size: 0.58rem;
        letter-spacing: 0.08em;
    }

    .chat-messages {
        gap: 0.75rem;
        padding: 0 4px 0.65rem 0;
    }

    .msg-name {
        font-size: 0.65rem;
    }

    .msg-bot,
    .msg-user {
        padding-left: 12px;
        font-size: clamp(0.94rem, 4.2vw, 1.08rem);
        line-height: 1.48;
    }

    .witness-prompt-row {
        margin: 0.45rem 0 0.55rem;
    }

    .witness-prompt-chip,
    .ambience-mode,
    .chat-send-btn {
        min-height: 44px;
    }

    .witness-prompt-chip {
        padding: 9px 13px;
        font-size: 0.68rem;
    }

    .ambience-switcher {
        top: calc(var(--phone-safe-top) + 10px);
        left: var(--phone-gutter);
        max-width: calc(100% - (2 * var(--phone-gutter)));
    }

    .ambience-mode {
        padding: 0 12px;
        font-size: 0.62rem;
    }

    .chat-input-area {
        min-height: 52px;
    }

    .chat-input {
        font-size: 16px;
    }

    .chat-send-btn {
        min-width: 82px;
        font-size: 0.68rem;
    }

    .witness-scope,
    .witness-status {
        font-size: 0.58rem;
    }

    .scroll-indicator {
        right: var(--phone-gutter);
        bottom: calc(var(--phone-safe-bottom) + 10px);
        font-size: 0.62rem;
    }

    .outer-text-left {
        left: 12px;
        font-size: 0.78rem;
    }

    .outer-text-right {
        left: 61%;
        right: 12px;
        max-width: none;
        font-size: 0.62rem;
        letter-spacing: 0.14em;
        line-height: 1.5;
        text-align: right;
    }

    .witness-consent-gate {
        top: max(29%, 170px);
        right: 0;
        bottom: 0;
        left: 0;
        place-items: end stretch;
        padding: 12px var(--phone-gutter) calc(var(--phone-safe-bottom) + 3.15rem);
        background: linear-gradient(to top, rgba(8, 4, 2, 0.995) 0%, rgba(10, 5, 3, 0.97) 78%, rgba(10, 5, 3, 0.2) 100%);
    }

    .witness-consent {
        max-height: 100%;
        overflow-y: auto;
        padding: clamp(17px, 5vw, 22px);
        border-radius: 13px;
        overscroll-behavior: contain;
    }

    .witness-consent h2 {
        margin-bottom: 0.7rem;
        font-size: clamp(2rem, 10.5vw, 2.7rem);
    }

    .witness-consent-body {
        margin-bottom: 0.55rem;
        font-size: 0.92rem;
        line-height: 1.4;
    }

    .witness-consent-links {
        margin-bottom: 0.7rem;
        font-size: 0.58rem;
    }

    .witness-consent-age-input {
        width: 100%;
        min-height: 48px;
        font-size: 16px;
    }

    .witness-consent-age-note,
    .witness-consent-error {
        margin: 0.3rem 0 0.55rem;
        font-size: 0.64rem;
        line-height: 1.35;
    }

    .witness-consent-agree {
        width: 100%;
        min-height: 50px;
    }

    /* Book: on an open spread each physical page is 95vw. The active right
       page is centered for chapters, then the epilogue and back cover move to
       the left-page position. The 3D turn itself remains unchanged. */
    .book-journey {
        width: 100%;
        height: 100%;
    }

    .book-journey-header {
        top: calc(var(--phone-safe-top) + 12px);
        left: var(--phone-gutter);
        right: var(--phone-gutter);
        gap: 0.75rem;
    }

    .book-journey-eyebrow,
    .book-journey-chapter {
        font-size: 0.62rem;
        line-height: 1.35;
        letter-spacing: 0.12em;
    }

    .book-journey-chapter-name {
        display: none;
    }

    .book-mobile-side-toggle:not([hidden]) {
        position: absolute;
        z-index: 92;
        top: calc(var(--phone-safe-top) + 40px);
        left: 50%;
        display: flex;
        min-height: 44px;
        padding: 2px;
        transform: translateX(-50%);
        border: 1px solid rgba(220, 174, 93, 0.34);
        border-radius: 999px;
        background: rgba(23, 12, 8, 0.97);
        box-shadow: 0 9px 22px rgba(0, 0, 0, 0.24);
    }

    .book-mobile-side-toggle button {
        min-width: 78px;
        min-height: 44px;
        padding: 0 14px;
        border: 0;
        border-radius: 999px;
        color: rgba(240, 221, 186, 0.62);
        background: transparent;
        font-family: 'Space Grotesk', Arial, sans-serif;
        font-size: 0.61rem;
        letter-spacing: 0.11em;
        text-transform: uppercase;
    }

    .book-mobile-side-toggle button.is-active {
        color: #2a180e;
        background: #e1b55f;
        box-shadow: 0 3px 12px rgba(225, 181, 95, 0.24);
    }

    .book-stage {
        inset: calc(var(--phone-safe-top) + 4.8rem) 0 calc(var(--phone-safe-bottom) + 4.65rem);
        overflow: hidden;
        perspective: 1800px;
        perspective-origin: 50% 46%;
    }

    .book-volume {
        left: 0;
        width: min(190vw, var(--favor-mobile-book-width, calc(128.6vh - 218.62px)));
        max-width: none;
        transition: left 260ms cubic-bezier(0.22, 0.61, 0.36, 1);
    }

    .book-volume[data-book-step='0'] {
        left: -23vw;
    }

    .book-volume[data-book-step='1'] {
        left: -71vw;
    }

    /* The first turned leaf has a different 3D envelope from later leaves.
       Give it its own offset so Chapter 05 does not lose its left margin. */
    .book-volume[data-book-step='2'] {
        left: var(--favor-book-first-reading-left, 10vw);
    }

    .book-volume[data-book-step='3'],
    .book-volume[data-book-step='4'],
    .book-volume[data-book-step='5'],
    .book-volume[data-book-step='6'] {
        left: 10vw;
    }

    .book-volume[data-book-step='2'][data-book-mobile-side='artwork'],
    .book-volume[data-book-step='3'][data-book-mobile-side='artwork'],
    .book-volume[data-book-step='4'][data-book-mobile-side='artwork'],
    .book-volume[data-book-step='5'][data-book-mobile-side='artwork'],
    .book-volume[data-book-step='6'][data-book-mobile-side='artwork'] {
        left: var(--favor-book-artwork-left, -71vw);
    }

    .book-volume[data-book-step='7'] {
        left: 10vw;
    }

    .book-volume[data-book-step='8'] {
        left: -42vw;
    }

    .book-page--invitation,
    .book-page--chapter-copy,
    .book-page--closing-copy,
    .book-page--purchase {
        padding: clamp(20px, 6vw, 27px) clamp(18px, 5.5vw, 25px);
    }

    .book-page-kicker,
    .book-page-folio {
        font-size: clamp(0.58rem, 2.7vw, 0.68rem);
        letter-spacing: 0.12em;
    }

    .book-page--invitation h3,
    .book-page--chapter-copy h3,
    .book-page--closing-copy h3,
    .book-page--purchase h3 {
        font-size: clamp(1.8rem, 8.4vw, 2.25rem);
        line-height: 0.94;
    }

    .book-page-lede,
    .book-page--chapter-copy > p:not(.book-page-kicker),
    .book-page--purchase p:not(.book-page-kicker):not(.book-buy-note) {
        font-size: clamp(0.86rem, 3.75vw, 1rem);
        line-height: 1.4;
    }

    .book-page--chapter-copy blockquote {
        margin-top: 0.85rem;
        padding-left: 0.7rem;
        font-size: clamp(0.76rem, 3.35vw, 0.9rem);
        line-height: 1.36;
    }

    .book-page-note {
        margin-top: 1rem;
        font-size: clamp(0.68rem, 3vw, 0.8rem);
        line-height: 1.48;
    }

    .book-chapter-index {
        margin-top: 1.2rem;
    }

    .book-chapter-index span {
        padding: 0.65rem 0;
        font-size: 0.66rem;
    }

    .book-word-notes {
        margin-top: 0.9rem;
    }

    .book-word-notes div {
        grid-template-columns: 4.7rem 1fr;
        gap: 0.45rem;
        padding: 0.48rem 0;
    }

    .book-word-notes dt,
    .book-word-notes dd {
        font-size: clamp(0.58rem, 2.65vw, 0.68rem);
    }

    .book-art-caption {
        gap: 0.32rem;
        padding: 12px 14px 13px;
        font-size: clamp(0.58rem, 2.65vw, 0.68rem);
        line-height: 1.35;
    }

    .book-art-caption strong {
        font-size: clamp(0.95rem, 4.35vw, 1.15rem);
    }

    .book-art-links {
        gap: 0.2rem 0.65rem;
    }

    .book-art-links a {
        min-height: 36px;
        display: inline-flex;
        align-items: center;
    }

    .book-cover-face--inside {
        padding: clamp(24px, 7vw, 34px);
    }

    .book-final-purchase {
        top: auto;
        right: auto;
        bottom: calc(var(--phone-safe-bottom) + 4.35rem);
        left: 7vw;
        width: 86vw;
        padding: 16px 18px;
        translate: none;
        border-radius: 14px;
        background: rgba(19, 10, 7, 0.985);
    }

    .book-final-purchase .book-page-kicker,
    .book-final-purchase .book-buy-note {
        display: none;
    }

    .book-final-purchase h3 {
        max-width: none;
        font-size: 1.55rem;
    }

    .book-final-purchase > p:not(.book-page-kicker):not(.book-buy-note) {
        margin-top: 0.55rem;
        font-size: 0.84rem;
        line-height: 1.35;
    }

    .book-final-purchase .book-buy-button {
        min-height: 48px;
        margin-top: 0.75rem;
        padding: 0.7rem 0.85rem;
    }

    .book-buy-button span {
        font-size: 0.68rem;
    }

    .book-buy-button small {
        font-size: 0.58rem;
    }

    .book-journey-controls {
        bottom: calc(var(--phone-safe-bottom) + 6px);
        width: calc(100% - (2 * var(--phone-gutter)));
        justify-content: center;
        gap: 2px;
    }

    .book-nav-arrow {
        min-width: 54px;
        min-height: 44px;
        font-size: 0.55rem;
    }

    .book-progress-dots button {
        width: clamp(18px, 5vw, 22px);
        height: 44px;
    }

    .book-journey-instruction {
        display: none;
    }

    /* Footer: stop using desktop vw type, and make every link finger-sized. */
    .site-footer {
        padding: clamp(38px, 12vw, 58px) var(--phone-gutter) calc(var(--phone-safe-bottom) + 24px);
        background-size: 28px 28px;
    }

    .f-top-grid {
        gap: 1.5rem;
        margin-bottom: 2.5rem;
    }

    .f-col h4 {
        margin-bottom: 0.85rem;
        font-size: 0.72rem;
    }

    .f-links {
        gap: 0.15rem;
    }

    .f-links a {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        font-size: 1rem;
    }

    .back-to-top {
        min-width: 58px;
        min-height: 58px;
    }

    .btt-text {
        margin-bottom: 0.5rem;
        font-size: 0.58rem;
        letter-spacing: 0.12em;
    }

    .btt-arrow {
        width: 52px;
        height: 52px;
    }

    .btt-arrow-inner {
        font-size: 1.15rem;
    }

    .f-copyright {
        flex-direction: column;
        gap: 0.55rem;
        margin-top: 0;
        font-size: 0.66rem;
        line-height: 1.5;
        letter-spacing: 0.08em;
    }
}

/* iOS Safari has a much smaller decoded-image/GPU budget than desktop
   browsers. JavaScript swaps inactive scene imagery for an 8px transparent
   placeholder; keep that replacement out of paint without changing any box
   measurements used by ScrollTrigger. */
@media screen and (max-width: 768px) {
    .is-memory-suspended img {
        visibility: hidden !important;
    }

    .gallery-scroll-wrapper,
    .g-card,
    .showcase-bg img,
    .fg-img,
    .witness-blink-frame,
    .book-volume,
    .book-page-block,
    .book-leaf,
    .book-front-cover,
    .book-back-cover {
        will-change: auto !important;
    }

    .book-shadow {
        filter: blur(14px);
    }
}

/* Safari with expanded browser chrome has much less usable height than an
   Instagram webview. This class is measured from the visual viewport, so the
   same document remains legible in either shell without UA-specific layouts. */
@media screen and (max-width: 768px) {
    .phone-compact-height .loader-wordmark-the {
        font-size: clamp(1.45rem, 7.4vw, 2rem);
    }

    .phone-compact-height .loader-path-window {
        top: 32%;
        height: min(var(--favor-loader-window-height, 42vh), 38%);
    }

    .phone-compact-height .loader-text {
        bottom: calc(var(--phone-safe-bottom) + 1.35rem);
    }

    .phone-compact-height .threshold-copy {
        top: 46%;
    }

    .phone-compact-height .threshold-title-the {
        font-size: clamp(2.6rem, 14vw, 3.8rem);
    }

    .phone-compact-height .threshold-title-main {
        font-size: clamp(2.75rem, 13.5vw, 4rem);
    }

    .phone-compact-height .threshold-enter {
        min-height: 46px;
        margin-top: 1.15rem;
        padding-block: 0.7rem;
    }

    .phone-compact-height .opening-end-title-lockup {
        bottom: calc(var(--phone-safe-bottom) + 5.6rem);
    }

    .phone-compact-height .opening-title-primary {
        font-size: clamp(3rem, 15.5vw, 4.25rem);
    }

    .phone-compact-height .opening-title-script {
        font-size: clamp(1.35rem, 7vw, 2rem);
    }

    .phone-compact-height .opening-title-decree {
        font-size: clamp(1.5rem, 7vw, 2.15rem);
    }

    .phone-compact-height .g-card {
        width: 78vw;
        height: min(104vw, var(--favor-gallery-card-height, 61vh));
        max-height: calc(var(--phone-viewport-height) - 9.5rem);
    }

    .phone-compact-height .chat-content-area {
        height: 64%;
        padding-bottom: calc(var(--phone-safe-bottom) + 2.45rem);
    }

    .phone-compact-height .chat-header {
        margin-bottom: 0.35rem;
    }

    .phone-compact-height .chat-subtitle,
    .phone-compact-height .witness-ready-seal {
        display: none;
    }

    .phone-compact-height .witness-ai-notice {
        margin-top: 0.25rem;
    }

    .phone-compact-height .witness-consent-gate {
        top: max(19%, 112px);
        padding-top: 8px;
    }

    .phone-compact-height .witness-consent {
        padding: 16px;
    }

    .phone-compact-height .witness-consent h2 {
        margin-bottom: 0.45rem;
        font-size: clamp(1.75rem, 8.8vw, 2.3rem);
    }

    .phone-compact-height .book-journey-header {
        top: calc(var(--phone-safe-top) + 8px);
    }

    .phone-compact-height .book-mobile-side-toggle:not([hidden]) {
        top: calc(var(--phone-safe-top) + 34px);
    }

    .phone-compact-height .book-stage {
        inset: calc(var(--phone-safe-top) + 4.35rem) 0 calc(var(--phone-safe-bottom) + 4.2rem);
    }

    .phone-compact-height .book-final-purchase {
        bottom: calc(var(--phone-safe-bottom) + 4rem);
    }
}

@media screen and (max-width: 380px) {
    .chat-content-area {
        height: 60%;
        padding-bottom: calc(var(--phone-safe-bottom) + 2.45rem);
    }

    .chat-header {
        margin-bottom: 0.4rem;
    }

    .witness-ready-seal,
    .chat-subtitle {
        display: none;
    }

    .book-stage {
        top: calc(var(--phone-safe-top) + 4.6rem);
    }

    .book-volume {
        width: min(190vw, var(--favor-mobile-book-width, calc(128.6vh - 192.9px)));
    }
}

@media screen and (max-width: 768px) and (prefers-reduced-motion: reduce) {
    .book-volume {
        transition: none;
    }
}
