/* ---------- UNIFIED VIEWPORT + PERSISTENT UI FOUNDATION ---------- */
let unifiedViewportRaf=0;

function syncUnifiedViewport(){
  unifiedViewportRaf=0;

  const viewport=window.visualViewport;
  const viewportH=Math.max(1,Math.round(viewport?.height||window.innerHeight||document.documentElement.clientHeight));
  document.documentElement.style.setProperty('--viewport-h',viewportH+'px');

  const nav=document.getElementById('bottomNav');
  if(nav){
    const rect=nav.getBoundingClientRect();
    const obstruction=Math.max(0,Math.ceil(viewportH-rect.top));
    document.documentElement.style.setProperty('--bottom-ui-clearance',(obstruction+16)+'px');
  }
}

function scheduleUnifiedViewportSync(){
  if(unifiedViewportRaf) return;
  unifiedViewportRaf=requestAnimationFrame(syncUnifiedViewport);
}

scheduleUnifiedViewportSync();
window.addEventListener('resize',scheduleUnifiedViewportSync,{passive:true});
window.addEventListener('orientationchange',()=>setTimeout(scheduleUnifiedViewportSync,80),{passive:true});

if(window.visualViewport){
  visualViewport.addEventListener('resize',scheduleUnifiedViewportSync,{passive:true});
  visualViewport.addEventListener('scroll',scheduleUnifiedViewportSync,{passive:true});
}

if('ResizeObserver' in window){
  const nav=document.getElementById('bottomNav');
  if(nav) new ResizeObserver(scheduleUnifiedViewportSync).observe(nav);
}

/* Ensure interactive maps do not initiate browser text selection / odd gestures. */
['googleMap1','googleMap2'].forEach(id=>{
  const el=document.getElementById(id);
  if(el) el.style.touchAction='pan-x pan-y';
});

/* Re-sync after fonts and late media settle. */
if(document.fonts?.ready){
  document.fonts.ready.then(scheduleUnifiedViewportSync).catch(()=>{});
}
window.addEventListener('load',scheduleUnifiedViewportSync,{once:true});


/* ---------- FULLSCREEN MEDIA VIEWER ---------- */
  .media-viewer {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: #000;
    display: flex;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 220ms cubic-bezier(.22,.8,.24,1), visibility 0s linear 220ms;
  }

  .media-viewer.open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transition-delay: 0s;
  }

  .media-viewer-top {
    position: absolute;
    inset: 0 0 auto 0;
    z-index: 6;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: max(14px, env(safe-area-inset-top)) 16px 14px;
    background: linear-gradient(180deg, rgba(0,0,0,.55), rgba(0,0,0,0));
    pointer-events: none;
  }

  .media-viewer-close,
  .media-viewer-count {
    pointer-events: auto;
  }

  .media-viewer-close {
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 999px;
    background: rgba(255,255,255,.12);
    color: #FFFDF7;
    font-size: 22px;
    display: grid;
    place-items: center;
    cursor: pointer;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  .media-viewer-count {
    color: rgba(255,255,255,.82);
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: .05em;
    padding: 7px 10px;
    border-radius: 999px;
    background: rgba(0,0,0,.26);
  }

  .media-viewer-stage {
    position: relative;
    flex: 1;
    overflow: hidden;
    touch-action: pan-y;
  }

  .media-viewer-track {
    height: 100%;
    display: flex;
    transform: translate3d(0,0,0);
    transition: transform 320ms cubic-bezier(.22,.8,.24,1);
    will-change: transform;
  }

  .media-viewer-slide {
    min-width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    position: relative;
  }

  .media-viewer-slide img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    user-select: none;
    -webkit-user-drag: none;
  }

  .media-viewer-slide video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    background: #000;
  }

  .media-viewer-hint {
    position: absolute;
    left: 50%;
    bottom: max(18px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 5;
    color: rgba(255,255,255,.6);
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    white-space: nowrap;
    pointer-events: none;
    transition: opacity 240ms ease;
  }

  .media-viewer.open .media-viewer-hint {
    animation: hintFade 2.2s ease forwards;
  }

  @keyframes hintFade {
    0%,60% { opacity: 1; }
    100% { opacity: 0; }
  }

  .s3-hero img,
  .s3-hero video {
    cursor: zoom-in;
  }


  /* ---------- HOME CARD POSITIONING FIX ---------- */
  .discover-sheet {
    left: 50% !important;
    right: auto !important;
    width: min(390px, calc(100vw - 24px)) !important;
    transform: translateX(-50%) !important;
    text-align: center !important;
    bottom: calc(var(--bottom-nav-height, 78px) + 12px + env(safe-area-inset-bottom)) !important;
  }
  .discover-sheet h1,
  .discover-sheet p,
  .discover-kicker {
    text-align: center !important;
    margin-left: auto !important;
    margin-right: auto !important;
  }

  @media (min-width: 700px) {
    .discover-sheet {
      width: 390px !important;
    }
  }

  /* ---------- FULLSCREEN VIEWER RELIABILITY FIX ---------- */
  .media-viewer {
    touch-action: none;
  }
  .media-viewer-stage {
    touch-action: none !important;
    overscroll-behavior: contain;
  }
  .media-viewer-close {
    z-index: 20;
    pointer-events: auto !important;
    touch-action: manipulation;
  }
  .media-viewer-top {
    z-index: 20;
  }


  /* ---------- HOME CARD + NAV RELATION ---------- */
  .discover-sheet {
    bottom: calc(var(--bottom-nav-height, 78px) + 8px + env(safe-area-inset-bottom)) !important;
  }


  /* ---------- HOME CARD CENTERED ---------- */
  .discover-sheet {
    top: 50% !important;
    bottom: auto !important;
    left: 50% !important;
    right: auto !important;
    width: min(390px, calc(100vw - 24px)) !important;
    transform: translate(-50%, -50%) !important;
    text-align: center !important;
  }
  .discover-sheet h1,
  .discover-sheet p,
  .discover-kicker {
    text-align: center !important;
    margin-left: auto !important;
    margin-right: auto !important;
  }

  .creator-tiktok-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 12px;
    padding: 11px 14px;
    border-radius: 999px;
    background: var(--ink);
    color: var(--paper);
    text-decoration: none;
    font-size: 12px;
    font-weight: 600;
    transition:
      transform 160ms cubic-bezier(.22,.8,.24,1),
      opacity 180ms cubic-bezier(.22,.8,.24,1),
      background-color 240ms cubic-bezier(.22,.8,.24,1);
  }
  .creator-tiktok-btn:active {
    transform: scale(.975);
    opacity: .9;
  }


  /* ---------- SEARCH SHEET HANDLE + DESKTOP CENTERING ---------- */
  .drawer {
    will-change: transform;
  }

  .drawer-handle-btn {
    width: 44px;
    height: 28px;
    margin: 0 auto 4px;
    border: 0;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    touch-action: none;
  }

  .drawer-handle-btn:active {
    cursor: grabbing;
  }

  .drawer-handle-btn::before {
    content: '';
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: rgba(18,18,18,.18);
    transition: transform 160ms cubic-bezier(.22,.8,.24,1),
                background-color 160ms cubic-bezier(.22,.8,.24,1);
  }

  .drawer-handle-btn:active::before {
    transform: scaleX(.9);
    background: rgba(18,18,18,.28);
  }

  @media (min-width: 700px) {
    .drawer {
      left: 50% !important;
      right: auto !important;
      top: 50% !important;
      bottom: auto !important;
      width: min(520px, calc(100vw - 48px)) !important;
      height: min(680px, calc(100vh - 64px)) !important;
      transform: translate(-50%, calc(-50% + 24px));
      border-radius: 24px !important;
      opacity: 0;
    }

    .drawer-overlay.open .drawer {
      transform: translate(-50%, -50%);
      opacity: 1;
    }
  }


  /* ---------- FULLSCREEN MEDIA GESTURE FIX ---------- */
  .media-viewer {
    z-index: 2147483000 !important;
    touch-action: none !important;
  }

  .media-viewer-stage {
    position: relative;
    touch-action: none !important;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
  }

  .media-viewer-track,
  .media-viewer-slide {
    touch-action: none !important;
  }

  .media-viewer-slide img {
    pointer-events: none;
  }

  .media-viewer-slide video {
    position: relative;
    z-index: 1;
  }

  .media-viewer-top {
    z-index: 100 !important;
    pointer-events: none !important;
  }

  .media-viewer-close {
    position: relative;
    z-index: 101 !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    min-width: 44px;
    min-height: 44px;
  }

  .media-viewer-count {
    pointer-events: none !important;
  }

  .media-swipe-layer {
    position: absolute;
    inset: 0;
    z-index: 20;
    background: transparent;
    touch-action: none;
  }

  .media-viewer-slide video + .media-video-control-zone {
    position: absolute;
    inset: 18% 10% 18%;
    z-index: 25;
    pointer-events: none;
  }


  /* ---------- FULLSCREEN CLOSE / SWIPE STRUCTURAL FIX ---------- */
  .media-viewer {
    position: fixed !important;
    inset: 0 !important;
    z-index: 2147483640 !important;
    background: #000 !important;
    isolation: isolate;
  }

  .media-viewer-stage {
    position: absolute !important;
    inset: 0 !important;
    z-index: 1 !important;
    overflow: hidden;
    touch-action: none !important;
  }

  .media-viewer-close {
    position: absolute !important;
    top: max(14px, env(safe-area-inset-top)) !important;
    left: 14px !important;
    width: 48px !important;
    height: 48px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 999px !important;
    z-index: 10000 !important;
    display: grid !important;
    place-items: center !important;
    background: rgba(20,20,20,.72) !important;
    color: #FFFDF7 !important;
    font-size: 28px !important;
    line-height: 1 !important;
    cursor: pointer !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    -webkit-tap-highlight-color: transparent;
  }

  .media-viewer-count {
    position: absolute !important;
    top: max(20px, calc(env(safe-area-inset-top) + 6px)) !important;
    right: 16px !important;
    z-index: 9999 !important;
    pointer-events: none !important;
  }

  .media-swipe-layer {
    position: absolute !important;
    /* Keep the entire top control strip out of the gesture surface */
    top: calc(max(14px, env(safe-area-inset-top)) + 62px) !important;
    right: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    z-index: 50 !important;
    background: transparent !important;
    touch-action: none !important;
    pointer-events: auto !important;
  }

  .media-viewer-track {
    position: relative;
    z-index: 1 !important;
  }

  .media-viewer-hint {
    z-index: 60 !important;
    pointer-events: none !important;
  }


  /* ---------- FULLSCREEN SWIPE + CLOSE FINAL FIX ---------- */
  .media-viewer {
    position: fixed !important;
    inset: 0 !important;
    z-index: 2147483640 !important;
    background: #000 !important;
    isolation: isolate;
    touch-action: none !important;
  }

  .media-viewer-stage {
    position: absolute !important;
    inset: 0 !important;
    z-index: 1 !important;
    overflow: hidden !important;
    touch-action: none !important;
    overscroll-behavior: none !important;
    -webkit-user-select: none !important;
    user-select: none !important;
  }

  .media-viewer-track {
    height: 100% !important;
    display: flex !important;
    transition: transform 320ms cubic-bezier(.22,.8,.24,1) !important;
    will-change: transform !important;
    touch-action: none !important;
  }

  .media-viewer-slide {
    min-width: 100% !important;
    height: 100% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    position: relative !important;
    background: #000 !important;
    touch-action: none !important;
  }

  .media-viewer-slide img,
  .media-viewer-slide video {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    display: block !important;
    background: #000 !important;
  }

  .media-viewer-slide img {
    pointer-events: none !important;
    -webkit-user-drag: none !important;
    user-select: none !important;
  }

  .media-viewer-slide video {
    pointer-events: auto !important;
  }

  .media-viewer-close {
    position: absolute !important;
    top: max(14px, env(safe-area-inset-top)) !important;
    left: 14px !important;
    width: 48px !important;
    height: 48px !important;
    z-index: 10000 !important;
    display: grid !important;
    place-items: center !important;
    border: 0 !important;
    border-radius: 999px !important;
    background: rgba(20,20,20,.72) !important;
    color: #FFFDF7 !important;
    font-size: 28px !important;
    line-height: 1 !important;
    cursor: pointer !important;
    pointer-events: auto !important;
    touch-action: manipulation !important;
    -webkit-tap-highlight-color: transparent !important;
  }

  .media-viewer-count {
    position: absolute !important;
    top: max(20px, calc(env(safe-area-inset-top) + 6px)) !important;
    right: 16px !important;
    z-index: 9999 !important;
    pointer-events: none !important;
  }

  .media-viewer-hint {
    z-index: 20 !important;
    pointer-events: none !important;
  }


/* ===== CLEAN FULLSCREEN MEDIA VIEWER ===== */
.media-viewer{
  position:fixed!important;
  inset:0!important;
  z-index:2147483640!important;
  background:#000!important;
  opacity:0;
  visibility:hidden;
  pointer-events:none;
  transition:opacity .2s ease,visibility 0s linear .2s;
}
.media-viewer.open{
  opacity:1;
  visibility:visible;
  pointer-events:auto;
  transition-delay:0s;
}
.media-viewer-stage{
  position:absolute!important;
  inset:0!important;
  overflow:hidden!important;
  touch-action:none!important;
  overscroll-behavior:none!important;
  z-index:1!important;
}
.media-viewer-track{
  height:100%!important;
  display:flex!important;
  transform:translate3d(0,0,0);
  transition:transform .28s cubic-bezier(.22,.8,.24,1);
  will-change:transform;
}
.media-viewer-slide{
  flex:0 0 100%!important;
  width:100%!important;
  height:100%!important;
  display:flex!important;
  align-items:center!important;
  justify-content:center!important;
  background:#000!important;
}
.media-viewer-slide img,
.media-viewer-slide video{
  width:100%!important;
  height:100%!important;
  object-fit:contain!important;
  display:block!important;
  background:#000!important;
}
.media-viewer-slide img{
  pointer-events:none!important;
  user-select:none!important;
  -webkit-user-drag:none!important;
}
.media-viewer-close{
  position:absolute!important;
  top:max(14px,env(safe-area-inset-top))!important;
  left:14px!important;
  z-index:1000!important;
  width:48px!important;
  height:48px!important;
  border:0!important;
  border-radius:999px!important;
  background:rgba(20,20,20,.74)!important;
  color:#FFFDF7!important;
  display:grid!important;
  place-items:center!important;
  font-size:28px!important;
  line-height:1!important;
  cursor:pointer!important;
  pointer-events:auto!important;
  touch-action:manipulation!important;
  -webkit-tap-highlight-color:transparent!important;
}
.media-viewer-count{
  position:absolute!important;
  top:max(22px,calc(env(safe-area-inset-top) + 8px))!important;
  right:16px!important;
  z-index:999!important;
  color:#FFFDF7!important;
  background:rgba(20,20,20,.45)!important;
  border-radius:999px!important;
  padding:6px 10px!important;
  pointer-events:none!important;
}


/* ===== CROPPED MEDIA FRAMING + SAFE NAV CONTROLS ===== */
.s3-hero {
  position: relative !important;
  overflow: hidden !important;
}

.s3-hero img,
.s3-hero video {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover !important;
  object-position: 50% 50% !important;
  display: block !important;
}

/* keep media navigation inside a reliable safe zone */
.s3-nav-btn {
  top: max(12px, env(safe-area-inset-top)) !important;
  width: 40px !important;
  height: 40px !important;
  border-radius: 999px !important;
  z-index: 30 !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  background: rgba(0,0,0,.42) !important;
  color: #FFFDF7 !important;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  pointer-events: auto !important;
  touch-action: manipulation !important;
}

.s3-nav-prev {
  left: max(12px, env(safe-area-inset-left)) !important;
}

.s3-nav-next {
  right: max(12px, env(safe-area-inset-right)) !important;
}

.s3-tap-left,
.s3-tap-right {
  top: 0 !important;
  bottom: 0 !important;
  width: 32% !important;
  z-index: 10 !important;
}

.s3-dots {
  bottom: max(12px, env(safe-area-inset-bottom)) !important;
  z-index: 25 !important;
  padding: 0 52px !important;
  pointer-events: none !important;
}

/* smaller screens: slightly tighter but still finger-safe */
@media (max-width: 420px) {
  .s3-nav-btn {
    width: 38px !important;
    height: 38px !important;
  }
  .s3-nav-prev {
    left: max(10px, env(safe-area-inset-left)) !important;
  }
  .s3-nav-next {
    right: max(10px, env(safe-area-inset-right)) !important;
  }
}


/* ===== FULLSCREEN BUTTON NAVIGATION ===== */
.media-viewer-arrow{
  position:absolute!important;
  top:50%!important;
  transform:translateY(-50%)!important;
  z-index:1000!important;
  width:48px!important;
  height:48px!important;
  border:0!important;
  border-radius:999px!important;
  background:rgba(20,20,20,.7)!important;
  color:#FFFDF7!important;
  display:grid!important;
  place-items:center!important;
  font-size:34px!important;
  line-height:1!important;
  cursor:pointer!important;
  pointer-events:auto!important;
  touch-action:manipulation!important;
  -webkit-tap-highlight-color:transparent!important;
}
.media-viewer-prev{
  left:max(12px,env(safe-area-inset-left))!important;
}
.media-viewer-next{
  right:max(12px,env(safe-area-inset-right))!important;
}
.media-viewer-arrow:disabled{
  opacity:.25!important;
  pointer-events:none!important;
}
@media (max-width:420px){
  .media-viewer-arrow{
    width:44px!important;
    height:44px!important;
    font-size:30px!important;
  }
}


/* Fullscreen controls must always receive touch */
.media-viewer-close,
.media-viewer-arrow {
  z-index: 2147483646 !important;
  pointer-events: auto !important;
  touch-action: manipulation !important;
}
.media-viewer-stage {
  z-index: 1 !important;
}


  .location-btn.is-disabled {
    opacity: .48;
    cursor: default;
  }


