/* mobile-presentation.css — Mobile story player styles.
 * S2: 60/40 focal + carousel split via MobileStageLayout + mobile-carousel.
 * S3 (luminous-mapping-gizmo): Story modal now mounts MobileSheet3Panel.
 *   - .mpl-focal lives inside .msp-top via scaffold's getFocalContent.
 *   - .mpl-carousel-wrap lives inside .msp-middle via getMiddleContent.
 *   - Playback controls render in .msp-bottom via getActions.
 *   - Per-chapter scaffolds are hidden for inactive cards via .pres-card:not(.active).
 * Scoped to the POCKET media queries for desktop zero-regression: portrait
 * mobile OR a phone held landscape (orienting-about-pocket-voice S2, Three
 * Voices) — a landscape iPhone is 852px wide, so the width query alone
 * un-styled the mobile transport on rotation; pointer:coarse keeps desktop out.
 */

@media (max-width: 767px), (orientation: landscape) and (max-height: 480px) and (pointer: coarse) {

  /* ── MobileStageLayout scaffold host ── */

  /* The host is a zero-footprint marker; the scaffold (.msp-root) is
     position:fixed and draws itself over the viewport. */
  .msl-scaffold-host {
    display: contents;
  }

  /* Per-chapter scaffolds stack; only the active card's scaffold is shown. */
  .pres-card:not(.active) .msp-root {
    display: none !important;
  }

  /* When a Story scaffold is mounted, suppress the legacy in-root chrome
     (header, mobile transport, subtitle strip) — the scaffold owns all UI. */
  body:has(.msp-root[data-msp-mode="story"]) .presentation-root .pres-header,
  body:has(.msp-root[data-msp-mode="story"]) .mobile-transport,
  body:has(.msp-root[data-msp-mode="story"]) .mobile-subtitle-strip {
    display: none !important;
  }

  /* ── Focal region styling — applies inside .msp-top ── */

  /* Focal fills the scaffold top panel. No per-region flex math (scaffold handles sizing).
     touch-action: pan-y reserves horizontal gestures for the chapter-swipe
     handler (D3.6, attached in MobileStageLayout._attachChapterSwipe). Without
     it, iOS Safari treats full-bleed horizontal drags as scroll-chrome / edge-
     swipe candidates and never fires a matched touchstart→touchend. Vertical
     scrolling still bubbles to the scaffold. Pattern matches the S2 face-tag
     fix (LESSONS_LEARNED §168). */
  .mpl-focal {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
    touch-action: pan-y;
  }

  .mpl-focal .media-item {
    width: 100%;
    height: 100%;
    max-height: 100%;
  }

  .mpl-focal .media-item img,
  .mpl-focal .media-item video {
    width: 100%;
    height: 100%;
    max-height: 100%;
    object-fit: contain;
    /* Crossfade on cue swap — also applies when Story is maximized. */
    transition: opacity var(--msp-maximize-swap-duration, 180ms) ease;
  }

  /* Carousel wrap (secondary/tertiary/wings) — fills scaffold middle panel */
  .mpl-carousel-wrap {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: stretch;
    gap: 10px;
    padding: 10px 12px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    scroll-padding-inline: 12px;   /* snap to the gutter, not the glass (Greg G-163) */
    -webkit-overflow-scrolling: touch;
    background: transparent;
    /* Hide scrollbar */
    scrollbar-width: none;
  }

  .mpl-carousel-wrap::-webkit-scrollbar {
    display: none;
  }

  /* Carousel cards — each media-item rendered as a scroll-snap tile.
     Sizes scaled down in S3 (nimble-harmonizing-compass) to match the
     shrunken 14vh middle-panel wrap. Aspect stays landscape-ish so
     photos preview cleanly without cropping faces. */
  .mpl-carousel-wrap > .media-item,
  .mpl-carousel-wrap > .mpl-card {
    flex: 0 0 auto;
    width: 30vw;
    max-width: 140px;
    min-width: 96px;
    height: 100%;
    scroll-snap-align: start;
    border-radius: 10px;
    overflow: hidden;
    background: #1a1a1e;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    position: relative;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.18s ease, box-shadow 0.18s ease;
  }

  .mpl-carousel-wrap > .mpl-card:active {
    transform: scale(0.97);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  }

  .mpl-carousel-wrap > .mpl-card img,
  .mpl-carousel-wrap > .mpl-card video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  /* Play badge on video cards */
  .mpl-card-badge {
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: rgba(124, 58, 237, 0.92);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
    pointer-events: none;
  }

  .mpl-card-badge svg {
    margin-left: 2px; /* Visual centering for play triangle */
  }

  /* Document cover badge inside carousel card */
  .mpl-carousel-wrap > .mpl-card .media-doc-badge {
    position: absolute;
    top: 6px;
    left: 6px;
    font-size: 9px;
    padding: 2px 5px;
    background: rgba(15, 15, 18, 0.88);
    color: rgba(255, 255, 255, 0.85);
    border-radius: 3px;
    letter-spacing: 0.5px;
  }

  /* Empty-state placeholder */
  .mpl-carousel-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.35);
    font-size: 13px;
    font-style: italic;
    padding: 12px;
  }

  /* Legacy buffer (no longer used — kept hidden for safety) */
  .msl-buffer {
    display: none !important;
  }

  /* ── Mobile Presentation Root ── */

  .presentation-root {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - var(--mobile-top-bar-height, 56px) - var(--mobile-safe-area-top, 0px) - var(--mobile-tab-bar-height, 56px) - var(--mobile-safe-area-bottom, 0px));
    overflow: hidden;
    background: #000;
  }

  body.story-immersive .presentation-root {
    height: 100dvh;
  }

  /* Hide desktop right-side deck controls on mobile */
  .presentation-root .pres-controls {
    display: none;
  }

  /* Hide desktop nav bar on mobile presentation */
  .presentation-root .pres-nav {
    display: none;
  }

  /* Chapter header: compact for mobile */
  .presentation-root .pres-header {
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    padding: 8px 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    z-index: 10;
  }

  .presentation-root .pres-header-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
  }

  .presentation-root .pres-header-story {
    display: none; /* Hide story name on mobile — chapter title is enough */
  }

  .presentation-root .pres-header-separator {
    display: none;
  }

  .presentation-root .pres-header-name {
    font-size: 14px;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .presentation-root .pres-header-narration {
    color: rgba(255, 255, 255, 0.7);
    background: none;
    border: none;
    padding: 4px;
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .presentation-root .pres-exit-btn {
    color: rgba(255, 255, 255, 0.9);
    min-width: 44px;
    min-height: 44px;
    background: rgba(255, 255, 255, 0.12);
    border-radius: 22px;
  }

  .presentation-root .pres-exit-btn svg {
    width: 20px;
    height: 20px;
  }

  /* Hide desktop scrub bar inside header — mobile has its own transport */
  .presentation-root .pres-header .pres-scrub {
    display: none;
  }

  /* Chapter deck fills remaining space */
  .presentation-root .pres-deck {
    flex: 1;
    min-height: 0;
    position: relative;
    overflow: hidden;
  }

  .presentation-root .pres-card {
    position: absolute;
    inset: 0;
    padding: 44px 12px 0 8px !important;
  }

  .presentation-root .pres-card-inner {
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  /* Visual frames: must fill flex column to avoid 0-height collapse.
     Desktop uses position:absolute on card-inner (not flex), so frames
     fill via inset:0. Mobile card-inner is flex-column, so active
     frames need flex:1 to take the available height. Without this,
     visual types with absolutely-positioned children render invisible. */
  .presentation-root .visual-frame.active {
    flex: 1;
    min-height: 0;
  }

  /* TWW stage takes full width on mobile */
  .presentation-root .tww-stage {
    width: 100%;
    flex: 1;
    min-height: 0;
  }

  /* ── Mobile Subtitle Strip ── */

  .mobile-subtitle-strip {
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    padding: 8px 16px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .mobile-subtitle-strip .mobile-subtitle-text {
    color: #fff;
    font-size: 14px;
    line-height: 1.4;
    text-align: center;
    max-width: 100%;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .mobile-subtitle-strip .mobile-subtitle-text.between-lines {
    opacity: 0.5;
  }

  /* Hide desktop subtitle bar when mobile strip is active.
   * pragma: ffa-bottombar-allowed — reason: mobile-subtitle-strip substitutes
   * the bottombar affordance on mobile viewport per the A3 constitutional
   * rule (plan assembling-foundational-bridges D1.3). The narration-bar
   * principle is preserved by a different selector, not removed.
   */
  .presentation-root .pres-subtitles {
    display: none;
  }

  /* ── Mobile Transport Controls ── */

  .mobile-transport {
    background: rgba(0, 0, 0, 0.9);
    padding: 8px 16px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 10;
  }

  /* Progress scrubber row */
  .mobile-transport-scrub {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .mobile-transport-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.6);
    font-variant-numeric: tabular-nums;
    min-width: 32px;
    text-align: center;
  }

  .mobile-transport-track-wrap {
    flex: 1;
    height: 28px;
    display: flex;
    align-items: center;
    cursor: pointer;
    touch-action: none;
  }

  .mobile-transport-track {
    width: 100%;
    height: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    position: relative;
    overflow: visible;
  }

  .mobile-transport-fill {
    height: 100%;
    background: #7c3aed;
    border-radius: 2px;
    width: 0%;
    transition: width 0.1s linear;
  }

  .mobile-transport-thumb {
    position: absolute;
    top: 50%;
    left: 0%;
    transform: translate(-50%, -50%);
    width: 14px;
    height: 14px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
    pointer-events: none;
  }

  .mobile-transport-track-wrap:active .mobile-transport-thumb {
    width: 18px;
    height: 18px;
  }

  /* Transport buttons row */
  .mobile-transport-buttons {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
  }

  .mobile-transport-btn {
    background: none;
    border: none;
    color: #fff;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    min-height: 48px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-transport-btn svg {
    width: 28px;
    height: 28px;
  }

  .mobile-transport-btn:active {
    opacity: 0.6;
  }

  .mobile-transport-btn.transport-play {
    width: 60px;
    height: 60px;
    background: #7c3aed;
    border-radius: 50%;
  }

  .mobile-transport-btn.transport-play svg {
    width: 28px;
    height: 28px;
  }

  .mobile-transport-btn.transport-play:active {
    background: #6d28d9;
    opacity: 1;
  }

  .mobile-transport-btn.transport-skip {
    opacity: 0.85;
  }

  .mobile-transport-btn.transport-chapter-prev,
  .mobile-transport-btn.transport-chapter-next {
    opacity: 0.7;
  }

  .mobile-transport-btn.transport-chapter-prev svg,
  .mobile-transport-btn.transport-chapter-next svg {
    width: 24px;
    height: 24px;
  }

  .mobile-transport-btn.disabled {
    opacity: 0.25;
    pointer-events: none;
  }

  /* Chapter info row */
  .mobile-transport-chapter {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 4px;
  }

  .mobile-transport-chapter-title {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.5);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 70%;
  }

  .mobile-transport-close {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: rgba(255, 255, 255, 0.85);
    font-size: 13px;
    font-weight: 600;
    padding: 6px 14px;
    border-radius: 16px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    display: flex;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
    font-family: inherit;
  }

  .mobile-transport-close:active {
    background: rgba(255, 255, 255, 0.22);
  }

  .mobile-transport-narration-toggle {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.5);
    font-size: 12px;
    padding: 4px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    min-height: 28px;
  }

  .mobile-transport-narration-toggle.narration-off {
    color: rgba(255, 255, 255, 0.3);
  }

  /* ── Context Pull-Up Sheet ── */

  .mobile-context-sheet .bottom-sheet-content {
    padding: 0;
  }

  .mobile-context-tabs {
    display: flex;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: #1a1a1a;
    position: sticky;
    top: 0;
    z-index: 1;
  }

  .mobile-context-tab {
    flex: 1;
    background: none;
    border: none;
    padding: 10px 8px;
    color: rgba(255, 255, 255, 0.5);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: color 0.2s, border-color 0.2s;
  }

  .mobile-context-tab.active {
    color: #fff;
    border-bottom-color: #7c3aed;
  }

  .mobile-context-panel {
    display: none;
    padding: 12px 16px;
    max-height: 40vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .mobile-context-panel.active {
    display: block;
  }

  /* Context tab: wing media gallery */
  .mobile-context-media {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .mobile-context-media .media-item {
    border-radius: 8px;
    overflow: hidden;
    background: #222;
    aspect-ratio: 4/3;
  }

  .mobile-context-media .media-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .mobile-context-empty {
    color: rgba(255, 255, 255, 0.4);
    text-align: center;
    padding: 24px 16px;
    font-size: 14px;
  }

  /* Transcript tab */
  .mobile-context-transcript {
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    line-height: 1.7;
  }

  .mobile-context-sentence {
    padding: 4px 0;
    transition: color 0.2s, background 0.2s;
    border-radius: 4px;
    cursor: pointer;
  }

  .mobile-context-sentence.active {
    color: #fff;
    background: rgba(124, 58, 237, 0.15);
  }

  /* Chapters tab */
  .mobile-context-chapter-list {
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .mobile-context-chapter-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    cursor: pointer;
    color: rgba(255, 255, 255, 0.7);
    transition: background 0.2s;
    min-height: 44px;
  }

  .mobile-context-chapter-item:active {
    background: rgba(255, 255, 255, 0.05);
  }

  .mobile-context-chapter-item.active {
    color: #7c3aed;
  }

  .mobile-context-chapter-num {
    font-size: 12px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.4);
    min-width: 20px;
  }

  .mobile-context-chapter-item.active .mobile-context-chapter-num {
    color: #7c3aed;
  }

  .mobile-context-chapter-title {
    flex: 1;
    font-size: 14px;
  }

  .mobile-context-chapter-dur {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.4);
    font-variant-numeric: tabular-nums;
  }

  /* ── Context sheet styling via BottomSheet ── */
  .bottom-sheet.mobile-context-sheet {
    background: #1a1a1a;
    color: #fff;
  }

  .bottom-sheet.mobile-context-sheet .bottom-sheet-handle {
    background: rgba(255, 255, 255, 0.3);
  }

  .bottom-sheet.mobile-context-sheet .bottom-sheet-header {
    border-bottom: none;
  }

  .bottom-sheet.mobile-context-sheet .bottom-sheet-title {
    color: #fff;
  }

  .bottom-sheet.mobile-context-sheet .bottom-sheet-close {
    color: rgba(255, 255, 255, 0.6);
  }

  /* ── Safety-net Exit Story control ──
     Rendered on <body> by MobilePlayerController. Must stay above the scaffold
     (msp-z-base=9000, msp-z-over=9500) so the user can always escape even if
     the scaffold fails to mount or is hidden by a mixed-cue visual transition.
     S1 (tranquil-importing-lighthouse): added after vivid-mending-lantern SR
     revealed users could be trapped on a black screen with only audio. */
  .mobile-story-safety-exit {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 12px);
    right: 12px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.72);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.28);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    display: grid;
    place-items: center;
    cursor: pointer;
    z-index: 10000;
    padding: 0;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-story-safety-exit:active {
    transform: scale(0.93);
    background: rgba(0, 0, 0, 0.85);
  }
  .mobile-story-safety-exit:focus-visible {
    outline: 2px solid #5ad1ff;
    outline-offset: 2px;
  }

  /* ── Immersive story mode: full viewport, no header/tabbar ── */
  body.story-immersive .main-content {
    margin-top: 0 !important;
    padding-bottom: 0 !important;
    height: 100vh;
    height: 100dvh;
  }

  body.story-immersive #view-story-presentation {
    height: 100vh;
    height: 100dvh;
  }
}
