/* =========================================================================
   Mobile & Fullscreen Cinema Adaptation (Portrait + Landscape + Safe Area)
   ========================================================================= */

/* Eliminate 300ms tap delay & accidental double-tap zoom on controls */
button,
.choice-btn,
.top-pill,
.seg-btn,
.mini-ctrl,
.lang-pill {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Smooth finger drag during 3D orbit scrubbing */
.stage-frame.orbit-ready {
  touch-action: none;
}

/* Desktop fullscreen support when user clicks ⛶ Fullscreen */
:fullscreen .top-bar,
:fullscreen .showcase-strip,
:fullscreen .right-column,
:fullscreen .dag-panel,
:fullscreen .transport-bar,
:-webkit-full-screen .top-bar,
:-webkit-full-screen .showcase-strip,
:-webkit-full-screen .right-column,
:-webkit-full-screen .dag-panel,
:-webkit-full-screen .transport-bar {
  display: none !important;
}

:fullscreen .workspace-layout,
:-webkit-full-screen .workspace-layout {
  max-width: 100vw !important;
  width: 100vw !important;
  height: 100vh !important;
  padding: 0 !important;
  margin: 0 !important;
}

:fullscreen .stage-frame,
:-webkit-full-screen .stage-frame {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  aspect-ratio: auto !important;
  border: none !important;
  border-radius: 0 !important;
}

/* Floating Mobile Controls (hidden on desktop unless in fullscreen) */
.mobile-fab-bar {
  display: none;
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 60;
  align-items: center;
  gap: 6px;
}

:fullscreen .mobile-fab-bar,
:-webkit-full-screen .mobile-fab-bar {
  display: flex;
}

.mobile-fab-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 34px;
  height: 34px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid rgba(214, 178, 110, 0.45);
  background: rgba(11, 9, 16, 0.72);
  color: var(--brass-bright, #f5dea8);
  font-size: 12px;
  font-weight: 600;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
}

.mobile-fab-btn:active {
  transform: scale(0.95);
}

/* Video fit toggle: default cover (immersive full-bleed), optional contain (full 16:9) */
body.video-fit-contain .stage-video {
  object-fit: contain !important;
  background: #050408;
}

/* Mobile slide-down drawer for top-bar when user taps ☰ */
.mobile-drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 54;
  background: rgba(5, 4, 9, 0.65);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

body.mobile-menu-open .mobile-drawer-backdrop {
  display: block;
}

/* =========================================================================
   Mobile Viewport (Portrait <= 960px OR Landscape phone height <= 540px)
   ========================================================================= */
@media (max-width: 960px), (max-height: 540px) and (pointer: coarse) {
  html, body {
    overscroll-behavior: none;
  }

  /* In default Cinema Mode on mobile: True edge-to-edge 100dvh fullscreen */
  body.cinema-mode {
    overflow: hidden;
    height: 100dvh;
    width: 100vw;
  }

  body.cinema-mode .mobile-fab-bar {
    display: flex;
  }

  body.cinema-mode .top-bar {
    display: none;
    position: fixed;
    top: calc(max(10px, env(safe-area-inset-top)) + 42px);
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    z-index: 58;
    border-radius: 14px;
    border: 1px solid rgba(214, 178, 110, 0.4);
    background: rgba(14, 11, 22, 0.96);
    padding: 14px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.85);
    max-height: calc(100dvh - 80px);
    overflow-y: auto;
  }

  body.cinema-mode.mobile-menu-open .top-bar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  body.cinema-mode .brand-sub {
    display: none;
  }

  body.cinema-mode .top-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  body.cinema-mode .workspace-layout {
    max-width: 100vw !important;
    width: 100vw;
    height: 100dvh;
    padding: 0 !important;
    margin: 0 !important;
  }

  body.cinema-mode .left-column,
  body.cinema-mode .stage-perspective-wrap {
    width: 100vw;
    height: 100dvh;
    gap: 0;
  }

  body.cinema-mode .stage-frame {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    aspect-ratio: auto;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  /* Hide desktop transport bar in mobile cinema mode (replaced by drawer + stage HUD) */
  body.cinema-mode .transport-bar {
    display: none;
  }

  body.cinema-mode.mobile-menu-open .transport-bar {
    display: flex;
    position: fixed;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    z-index: 58;
    background: rgba(14, 11, 22, 0.96);
    border: 1px solid rgba(214, 178, 110, 0.4);
    border-radius: 12px;
    padding: 10px 12px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.8);
  }

  /* Opening Start Curtain on Mobile */
  .start-curtain {
    padding: max(20px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  }

  .curtain-lang-bar {
    top: max(12px, env(safe-area-inset-top));
    left: max(12px, env(safe-area-inset-left));
    right: auto;
  }

  .curtain-title {
    font-size: clamp(26px, 7.5vw, 40px) !important;
  }

  .curtain-subtitle {
    font-size: 13.5px !important;
    line-height: 1.5;
    padding: 0 6px;
  }

  .btn-brass-primary {
    padding: 13px 22px;
    font-size: 15px;
    min-height: 46px;
  }

  /* In-Stage Top HUD: respect notch & leave room for right floating FAB bar */
  .hud-top {
    padding: max(10px, env(safe-area-inset-top)) 122px 8px max(12px, env(safe-area-inset-left));
    gap: 6px;
  }

  .model-tag {
    font-size: 9.5px;
    padding: 2px 7px;
  }

  .objective-card {
    padding: 5px 9px;
    font-size: 11.5px;
  }

  .rpg-hud-bar {
    flex-wrap: wrap;
    gap: 5px;
  }

  .rpg-pill {
    font-size: 11px;
    padding: 4px 8px;
  }

  /* Subtitles: safe-area bottom + readable size */
  .subtitle-box {
    bottom: calc(12px + env(safe-area-inset-bottom));
    width: calc(100% - 24px);
    padding: 6px 10px;
    border-radius: 8px;
    background: linear-gradient(180deg, rgba(7, 6, 11, 0), rgba(7, 6, 11, 0.62));
  }

  .sub-en {
    font-size: 13.5px;
    line-height: 1.35;
  }

  .sub-zh {
    font-size: 12.5px;
    line-height: 1.35;
  }

  /* Choice Overlay: positioned above subtitles, touch-friendly targets */
  .choice-overlay {
    bottom: calc(74px + env(safe-area-inset-bottom));
    width: calc(100% - 24px);
    max-height: 54dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .choice-buttons {
    gap: 8px;
  }

  .choice-btn {
    flex: 1 1 100%;
    max-width: 100%;
    min-height: 46px;
    padding: 10px 14px;
  }

  .choice-label {
    font-size: 13.5px;
  }

  .choice-hint {
    font-size: 11px;
  }

  .choice-timer-wrap {
    width: 92%;
  }

  /* Courtyard Hub / Action Buttons on Mobile */
  .courtyard-hub {
    bottom: calc(20px + env(safe-area-inset-bottom));
    width: calc(100% - 24px);
  }

  /* Gameplay Overlays (Map, Revelio, Astrolabe, Cross-Exam, Spell Duel) */
  .gameplay-overlay {
    padding: calc(max(12px, env(safe-area-inset-top)) + 44px) 12px calc(14px + env(safe-area-inset-bottom));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-end;
  }

  .gameplay-overlay.cinema-strip {
    padding: 0 10px calc(12px + env(safe-area-inset-bottom));
  }

  .gp-card {
    padding: 12px 14px;
    max-height: calc(100dvh - 88px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .gp-header {
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
    padding-bottom: 6px;
  }

  /* Modals / Native <dialog> on Mobile */
  .wiz-dialog {
    width: min(94vw, 640px) !important;
    max-width: 94vw !important;
    max-height: 86dvh !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px !important;
  }

  .satchel-grid,
  .bonds-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Extra compact tweaks for narrow Portrait phones (<= 480px) */
@media (max-width: 480px) and (orientation: portrait) {
  .hud-left .model-tag {
    display: none;
  }

  .hud-top {
    padding-right: 116px;
  }

  .mobile-fab-btn {
    padding: 0 8px;
    font-size: 11.5px;
    height: 32px;
  }
}

/* Landscape Phone Optimization (short screen height <= 500px) */
@media (max-height: 500px) and (orientation: landscape) {
  .hud-top {
    padding: 6px 124px 4px max(12px, env(safe-area-inset-left));
  }

  .hud-left .model-tag,
  .curtain-eyebrow,
  .curtain-hint {
    display: none;
  }

  .curtain-crest svg {
    width: 36px;
    height: 36px;
  }

  .curtain-title {
    font-size: 24px !important;
    margin-bottom: 4px !important;
  }

  .curtain-subtitle {
    margin-bottom: 12px !important;
    font-size: 12px !important;
  }

  .choice-overlay {
    bottom: calc(52px + env(safe-area-inset-bottom));
    gap: 6px;
  }

  .choice-buttons {
    flex-wrap: nowrap;
  }

  .choice-btn {
    flex: 1 1 0;
    padding: 7px 10px;
    min-height: 40px;
  }

  .choice-hint {
    display: none;
  }

  .subtitle-box {
    bottom: calc(6px + env(safe-area-inset-bottom));
    padding: 3px 8px;
  }

  .sub-en { font-size: 12.5px; }
  .sub-zh { font-size: 11.5px; }

  .gameplay-overlay {
    padding: 42px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  }

  .gp-card {
    padding: 10px 12px;
    max-height: calc(100dvh - 54px);
  }
}
