/* ==========================================================================
   css/mobile.css — owner: M-Nav.
   Mobile shell: safe-area & layout tokens, bottom tab bar (#m-tabbar),
   Home app bar (#m-appbar), player dock (#stage) + now-playing bar (#m-nowbar),
   playing-card equalizer indicator, phone landscape watch mode, tablet dock.
   Loaded LAST. Desktop (>= 900 px, not phone landscape) is untouched.
   See docs/MOBILE_SPEC.md §3, §4, §5, §13, §14.
   ========================================================================== */

:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --page-x: 16px;
  --hit: 44px;
  --tabbar-h: 56px;
  --tabbar-total: 0px;
  --appbar-h: 52px;
  --nowbar-h: 56px;
  --dock-h: 0px;
}

html.debug-safe {
  --safe-t: 47px;
  --safe-r: 0px;
  --safe-b: 34px;
  --safe-l: 0px;
}

@media (orientation: landscape) {
  html.debug-safe {
    --safe-t: 0px;
    --safe-r: 47px;
    --safe-b: 21px;
    --safe-l: 47px;
  }
}

/* --------------------------------------------------------------------------
   Desktop default: all mobile shell static elements are hidden
   -------------------------------------------------------------------------- */
#m-tabbar,
#m-appbar,
#m-nowbar,
#m-focus-proxy,
.m-hero-skeleton {
  display: none;
}

/* --------------------------------------------------------------------------
   Shared Mobile Media Query (CONFIG.MQ_MOBILE)
   -------------------------------------------------------------------------- */
@media (max-width: 899px), (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
  :root {
    --header-h: 0px;
    --tabbar-h: 56px;
    --tabbar-total: calc(var(--tabbar-h) + var(--safe-b));
    --appbar-h: 52px;
    --nowbar-h: 56px;
  }

  html,
  body {
    overflow-x: clip;
    max-width: 100%;
    /* mobile_shell.js keeps the page steady when the dock opens/closes. Chrome's scroll anchoring
       would correct a second time and Safari has none, so turn it off: every engine behaves alike. */
    overflow-anchor: none;
  }

  /* The desktop placeholder (contain-intrinsic-size 280px) is ~65 px taller than a phone card, so
     never-rendered rows shifted the page after programmatic scrolls. Phone grids render in small
     batches, so real layout is cheap and exact. */
  .card {
    content-visibility: visible;
  }

  /* Horizontal rows are swiped on phones; the peeking next card is the affordance. WebKit also
     painted the thin scrollbar thumb above sticky bars (a grey line over the condensed person bar). */
  .strip,
  .fp-cast,
  .fp-videos {
    scrollbar-width: none;
  }

  .strip::-webkit-scrollbar,
  .fp-cast::-webkit-scrollbar,
  .fp-videos::-webkit-scrollbar {
    display: none;
  }

  /* Ensure skip-link never fails mobile target size check */
  .skip-link {
    min-width: var(--hit);
    min-height: var(--hit);
    display: inline-flex;
    align-items: center;
  }

  /* Focus proxy for iOS virtual keyboard on Search tab tap */
  #m-focus-proxy {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: 0;
    border: 0;
    opacity: 0;
    pointer-events: none;
    font-size: var(--fs-16);
    z-index: -1;
  }

  /* Hide desktop header, theater bar, stage info and legacy mobile bar */
  #site-header,
  #theater-bar,
  #stage-info,
  #stage-mobile-bar {
    display: none !important;
  }

  /* Mobile hero skeleton inside initial loading skeleton */
  .m-hero-skeleton {
    display: block;
    width: 100%;
    aspect-ratio: 16 / 10;
    max-height: 300px;
    border-radius: 0;
  }

  body.has-dock .m-hero,
  body.has-dock .m-hero-skeleton {
    display: none !important;
  }

  /* Main pane & view host spacing for tab bar and dock */
  .shell,
  body.layout-stacked .shell,
  body.is-theater .shell {
    display: block;
    min-height: 100dvh;
  }

  .main-pane {
    min-height: 100dvh;
    padding-top: var(--dock-h);
    padding-bottom: calc(var(--tabbar-total) + var(--space-6));
    /* Reduce Motion (base.css) turns every change into a 0.01ms transition, which defers this padding
       to the next frame; the dock's scroll compensation measures it synchronously. It never animates. */
    transition: none !important;
  }

  .view {
    min-height: calc(100dvh - var(--tabbar-total));
  }

  /* Sensible sticky offsets on mobile when dock is open/closed */
  .view__header,
  body.layout-stacked .view__header {
    top: var(--dock-h);
  }

  /* Touch polish (§13) */
  button,
  a,
  [role="button"],
  [role="tab"],
  .chip,
  .card {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .card {
    -webkit-touch-callout: none;
    user-select: none;
  }

  /* On mobile, an idle "selected" card or row shows NO ring (§5) */
  .card.is-selected .card__poster::after {
    box-shadow: inset 0 0 0 1px var(--hairline);
  }

  .row.is-selected:not(.is-playing) {
    background: transparent;
    box-shadow: none;
  }

  .row + .row.is-selected:not(.is-playing) {
    box-shadow: 0 -1px 0 var(--hairline);
  }

  /* Keep toasts above the tab bar */
  .toast-host,
  .toast {
    bottom: calc(var(--tabbar-total) + var(--space-3));
  }

  /* Defensive target sizing for legacy filterbar before M-Browse swaps to .m-chips */
  .filterbar .segmented--icons {
    display: none !important;
  }

  .filterbar .filter-btn {
    min-height: var(--hit);
    min-width: var(--hit);
  }

  .filterbar .filterbar__mobile {
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
  }

  /* ========================================================================
     1. Bottom Tab Bar (#m-tabbar, §4)
     ======================================================================== */
  #m-tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-header);
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    align-items: stretch;
    height: calc(var(--tabbar-h) + var(--safe-b));
    padding: 0 max(var(--space-1), var(--safe-r)) var(--safe-b) max(var(--space-1), var(--safe-l));
    background: var(--surface-1);
    background-clip: border-box;
    border-top: 1px solid var(--hairline);
    user-select: none;
  }

  .m-tabbar__tab {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-width: var(--hit);
    min-height: var(--hit);
    padding: var(--space-1) 2px;
    border: 0;
    background: transparent;
    color: var(--text-2);
    font-family: var(--font-sans);
    font-size: var(--fs-12);
    line-height: var(--lh-12);
    font-weight: var(--fw-medium);
    cursor: pointer;
    transition: color var(--dur-hover) var(--ease), transform var(--dur-hover) var(--ease);
  }

  .m-tabbar__tab:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
    border-radius: var(--radius-sm);
  }

  html.touch .m-tabbar__tab:active {
    transform: scale(0.97);
  }

  .m-tabbar__tab[aria-current="page"],
  .m-tabbar__tab.is-active {
    color: var(--accent);
  }

  .m-tabbar__icon {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 24px;
    flex: none;
  }

  .m-tabbar__icon svg {
    width: 22px;
    height: 22px;
  }

  .m-tabbar__ring {
    width: 22px;
    height: 22px;
  }

  .m-tabbar__ring-arc {
    transition: stroke-dashoffset var(--dur-slow) var(--ease), opacity var(--dur-fast) var(--ease);
  }

  .m-tabbar__ring-dot {
    transition: opacity var(--dur-fast) var(--ease);
  }

  .m-tabbar__avatar {
    width: 24px;
    height: 24px;
    border-radius: var(--radius-full);
    object-fit: cover;
    box-shadow: 0 0 0 1.5px currentColor;
  }

  .m-tabbar__label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
    font-variant-numeric: tabular-nums;
  }

  /* ========================================================================
     2. App Bar (#m-appbar, Home only, §4) & Safe-Top Status Scrim (S3a)
     ======================================================================== */
  body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: calc(var(--z-header) - 1);
    height: var(--safe-t);
    background: var(--bg);
    pointer-events: none;
    opacity: 0;
    transition: opacity var(--dur-base) var(--ease);
  }

  body.appbar-hidden:not(.has-dock)::before,
  body.appbar-hidden.dock-suspended::before,
  body.is-scrolled:not(.has-dock):not(.appbar-solid)::before,
  body.is-scrolled.dock-suspended:not(.appbar-solid)::before,
  body:not(.has-dock)[data-m-view]:not([data-m-view="browse"]):not([data-m-view="film"])::before,
  body.dock-suspended[data-m-view]:not([data-m-view="browse"]):not([data-m-view="film"])::before {
    opacity: 1;
  }

  #m-appbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: calc(var(--z-header) - 1);
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    height: calc(var(--appbar-h) + var(--safe-t));
    padding: var(--safe-t) max(var(--page-x), var(--safe-r)) 0 max(var(--page-x), var(--safe-l));
    background: linear-gradient(180deg, var(--scrim) 0%, transparent calc(100% - 2px)) no-repeat border-box;
    border-bottom: 0;
    transition: transform var(--dur-base) var(--ease), background-color var(--dur-base) var(--ease),
      border-color var(--dur-base) var(--ease);
  }

  /* Visible only on Home browse root when dock is closed */
  body.tab-home[data-m-view="browse"]:not(.has-dock) #m-appbar,
  body:not([data-m-view]):not(.has-dock) #m-appbar {
    display: flex;
  }

  body.appbar-solid #m-appbar {
    background: var(--surface-1);
    background-clip: border-box;
    border-bottom: 1px solid var(--hairline);
  }

  body.appbar-hidden #m-appbar {
    transform: translateY(-105%);
    pointer-events: none;
  }

  .m-appbar__brand {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--hit);
    min-width: var(--hit);
    padding: 0 var(--space-1);
    color: var(--text-1);
    text-decoration: none;
    border-radius: var(--radius-sm);
  }

  .m-appbar__brand:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }

  .m-appbar__brand .brand__mark {
    color: var(--accent);
    flex: none;
  }

  .m-appbar__title {
    font-size: var(--fs-16);
    line-height: var(--lh-16);
    font-weight: var(--fw-semibold);
    letter-spacing: var(--tracking-tight);
    white-space: nowrap;
  }

  .m-appbar__actions {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  .m-appbar__btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--hit);
    height: var(--hit);
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    background: var(--surface-2);
    background: color-mix(in srgb, var(--surface-2) 94%, transparent);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    backdrop-filter: blur(20px) saturate(180%);
    color: var(--text-1);
    cursor: pointer;
    transition: background-color var(--dur-hover) var(--ease), transform var(--dur-hover) var(--ease),
      border-color var(--dur-hover) var(--ease);
  }

  .m-appbar__btn[hidden] {
    display: none !important;
  }

  .m-appbar__btn:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }

  html.touch .m-appbar__btn:active {
    transform: scale(0.97);
  }

  .m-appbar__badge {
    position: absolute;
    top: 4px;
    right: 4px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: var(--radius-full);
    background: var(--accent);
    color: var(--on-accent);
    font-size: var(--fs-12);
    line-height: 16px;
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
    text-align: center;
  }

  .m-appbar__badge[hidden] {
    display: none !important;
  }

  /* ========================================================================
     3. Player Dock (#stage) & Now-Playing Bar (#m-nowbar, §5)
     ======================================================================== */
  /* Closed state (default on mobile): fixed, height 0, visibility hidden, NEVER display:none */
  #stage,
  body.layout-stacked #stage,
  body.is-theater #stage,
  body.has-tabbar #stage {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: var(--z-header);
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 0;
    min-height: 0;
    overflow: hidden;
    visibility: hidden;
    pointer-events: none;
    border: 0;
    background: var(--black, #000);
    transition: none;
  }

  /* Keep YouTube iframe slot >= 200x200 even when dock is collapsed */
  #stage .stage__screen-slot {
    width: 100%;
    min-width: 200px;
    min-height: 200px;
    aspect-ratio: 16 / 9;
    background: var(--black, #000);
    flex: none;
  }

  #stage .screen__play,
  #stage .screen__overlay .btn {
    min-width: var(--hit);
    min-height: var(--hit);
  }

  /* Open state: fixed top, 16:9 video + #m-nowbar */
  body.has-dock:not(.dock-suspended) #stage {
    height: auto;
    padding-top: var(--safe-t);
    overflow: visible;
    visibility: visible;
    pointer-events: auto;
    background: var(--black, #000);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-hover);
  }

  #m-nowbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    width: 100%;
    height: var(--nowbar-h);
    min-height: var(--nowbar-h);
    padding: 0 max(var(--space-2), var(--safe-r)) 0 max(var(--space-3), var(--safe-l));
    background: var(--surface-1);
    background-clip: border-box;
    border-top: 1px solid var(--hairline);
    user-select: none;
    touch-action: pan-y;
  }

  .m-nowbar__main {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    flex: 1 1 auto;
    min-width: 0;
    min-height: var(--hit);
    padding: 2px var(--space-1) 2px 0;
    border: 0;
    background: transparent;
    color: var(--text-1);
    text-align: left;
    font-family: var(--font-sans);
    cursor: pointer;
    border-radius: var(--radius-sm);
  }

  .m-nowbar__main:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }

  .m-nowbar__poster {
    width: 32px;
    height: 48px;
    border-radius: 4px;
    object-fit: cover;
    background: var(--surface-2);
    flex: none;
  }

  .m-nowbar__text {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 1px;
    min-width: 0;
    flex: 1 1 auto;
    transition: transform var(--dur-hover) var(--ease), opacity var(--dur-hover) var(--ease);
  }

  .m-nowbar__text.is-sliding-left {
    transform: translateX(-14px);
    opacity: 0.35;
  }

  .m-nowbar__text.is-sliding-right {
    transform: translateX(14px);
    opacity: 0.35;
  }

  .m-nowbar__title {
    font-size: var(--fs-14);
    line-height: var(--lh-14);
    font-weight: var(--fw-semibold);
    color: var(--text-1);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .m-nowbar__sub {
    font-size: var(--fs-12);
    line-height: var(--lh-12);
    color: var(--text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-variant-numeric: tabular-nums;
  }

  .m-nowbar__sub.is-gesture {
    color: var(--accent);
    animation: m-nowbar-pulse 1.6s var(--ease) infinite;
  }

  .m-nowbar__sub.is-upnext {
    color: var(--text-1);
  }

  .m-nowbar__sub.is-error {
    color: var(--danger);
  }

  @keyframes m-nowbar-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.55; }
  }

  .m-nowbar__actions {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    flex: none;
  }

  .m-nowbar__btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--hit);
    height: var(--hit);
    min-width: var(--hit);
    min-height: var(--hit);
    padding: 0;
    border: 0;
    border-radius: var(--radius-full);
    background: transparent;
    color: var(--text-2);
    font-family: var(--font-sans);
    font-size: var(--fs-12);
    font-weight: var(--fw-semibold);
    cursor: pointer;
    transition: background-color var(--dur-hover) var(--ease), color var(--dur-hover) var(--ease),
      transform var(--dur-hover) var(--ease);
  }

  .m-nowbar__btn:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }

  html.touch .m-nowbar__btn:active {
    transform: scale(0.97);
    background: var(--surface-2);
  }

  .m-nowbar__btn.is-seen,
  .m-nowbar__btn[aria-pressed="true"] {
    color: var(--seen);
  }

  .m-nowbar__btn.is-cancel {
    width: auto;
    padding: 0 var(--space-3);
    background: var(--surface-2);
    color: var(--text-1);
    border: 1px solid var(--border);
  }

  .m-nowbar__next-ring {
    position: absolute;
    inset: 4px;
    width: 36px;
    height: 36px;
    pointer-events: none;
    opacity: 0;
  }

  .m-nowbar__btn.is-upnext .m-nowbar__next-ring {
    opacity: 1;
  }

  .m-nowbar__btn.is-upnext .m-nowbar__next-arc {
    stroke: var(--accent);
    stroke-dasharray: 100.53;
    stroke-dashoffset: 100.53;
    animation: m-upnext-ring var(--upnext-ms, 3000ms) linear forwards;
  }

  @keyframes m-upnext-ring {
    from { stroke-dashoffset: 100.53; }
    to { stroke-dashoffset: 0; }
  }

  /* ========================================================================
     4. Playing-card Equalizer Badge (§5)
     ======================================================================== */
  .m-eq-badge {
    position: absolute;
    left: 6px;
    bottom: 6px;
    z-index: 3;
    display: inline-flex;
    align-items: flex-end;
    justify-content: center;
    gap: 2px;
    width: 20px;
    height: 20px;
    padding: 4px;
    border-radius: 5px;
    background: var(--scrim-strong);
    pointer-events: none;
  }

  .m-eq-badge i {
    display: block;
    width: 2.5px;
    height: 10px;
    border-radius: 1px;
    background: var(--accent);
    transform-origin: bottom center;
    animation: m-eq-bar 0.75s var(--ease) infinite alternate;
  }

  .m-eq-badge i:nth-child(1) {
    height: 7px;
    animation-delay: 0ms;
  }

  .m-eq-badge i:nth-child(2) {
    height: 11px;
    animation-delay: 180ms;
  }

  .m-eq-badge i:nth-child(3) {
    height: 8px;
    animation-delay: 320ms;
  }

  @keyframes m-eq-bar {
    0% { transform: scaleY(0.45); }
    100% { transform: scaleY(1); }
  }

  /* ========================================================================
     5. Tablet Portrait (600–899 px, §3, §5)
     ======================================================================== */
  @media (min-width: 600px) and (max-width: 899px) {
    body.has-dock:not(.dock-suspended):not(.dock-landscape) #stage .stage__screen-slot {
      max-height: 45dvh;
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--black, #000);
    }

    body.has-dock:not(.dock-suspended):not(.dock-landscape) #stage .stage__screen-slot .screen {
      max-height: 45dvh;
      width: auto;
      max-width: 100%;
      aspect-ratio: 16 / 9;
      margin: 0 auto;
    }
  }

  /* ========================================================================
     6. Phone Landscape (height <= 500 px and landscape, §3, §4, §5)
     ======================================================================== */
  @media (max-height: 500px) and (orientation: landscape) {
    :root {
      --tabbar-h: 44px;
      --nowbar-h: 48px;
    }

    .main-pane,
    .view__body,
    .browse-body {
      padding-inline: max(var(--page-x), var(--safe-l)) max(var(--page-x), var(--safe-r));
    }

    /* Compact tab bar: icon + label inline, 44 px */
    .m-tabbar__tab {
      flex-direction: row;
      gap: 6px;
      padding: 0 var(--space-2);
    }

    .m-tabbar__icon {
      width: 20px;
      height: 20px;
    }

    .m-tabbar__icon svg,
    .m-tabbar__ring {
      width: 18px;
      height: 18px;
    }

    .m-tabbar__avatar {
      width: 20px;
      height: 20px;
    }

    /* Landscape watch mode when dock is open */
    body.has-dock:not(.dock-suspended) {
      --tabbar-total: 0px;
    }

    body.has-dock:not(.dock-suspended) #m-tabbar,
    body.has-dock:not(.dock-suspended) #m-appbar {
      display: none !important;
    }

    body.has-dock:not(.dock-suspended) #stage {
      position: relative;
      height: 100dvh;
      padding-top: 0;
      box-shadow: none;
    }

    body.has-dock:not(.dock-suspended) #stage .stage__screen-slot {
      height: calc(100dvh - var(--nowbar-h));
      max-height: calc(100dvh - var(--nowbar-h));
      width: 100%;
      aspect-ratio: auto;
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--black, #000);
    }

    body.has-dock:not(.dock-suspended) #stage .stage__screen-slot .screen {
      height: 100%;
      max-height: 100%;
      width: auto;
      max-width: 100%;
      aspect-ratio: 16 / 9;
      margin: 0 auto;
    }

    body.has-dock:not(.dock-suspended) .main-pane {
      padding-top: 0 !important;
    }
  }

  /* Reduced transparency: solid surfaces without backdrop blur (S1) */
  @media (prefers-reduced-transparency: reduce) {
    #m-tabbar,
    body.appbar-solid #m-appbar,
    #m-nowbar {
      background: var(--surface-1);
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
    }

    .m-appbar__btn {
      background: var(--surface-2);
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
    }

    body::before {
      background: var(--bg);
      -webkit-backdrop-filter: none;
      backdrop-filter: none;
    }
  }
}

/* Reduced motion: disable equalizer and pulse animations */
@media (prefers-reduced-motion: reduce) {
  .m-eq-badge i,
  .m-nowbar__sub.is-gesture,
  .m-nowbar__btn.is-upnext .m-nowbar__next-arc {
    animation: none !important;
  }
}
