/**
 * Mobile Booking Sheet - Airbnb-Style Bottom Sheet Modal
 *
 * Full-viewport modal that owns the screen on mobile devices.
 * Hides site header, locks scroll, respects safe areas, and provides
 * classic modal affordances (backdrop, handle, close button).
 *
 * Desktop (≥992px): Sheet is hidden, existing sidebar widget shows
 * Mobile (<992px): Sheet provides full-screen booking experience
 *
 * @package TravelerChildTheme
 * @since 1.0.0
 */

:root {
  --mbs-radius: 20px;
  --mbs-z-index: 9999;  /* Very high to ensure above all site chrome */
  --mbs-backdrop-color: rgba(0, 0, 0, 0.44);
  --mbs-ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --mbs-transition-duration: 280ms;
}

/* ==========================================================================
   Container & Backdrop
   ========================================================================== */

.mbs {
  position: fixed;
  inset: 0;
  z-index: var(--mbs-z-index);
  display: none;
  isolation: isolate;
}

.mbs.is-open {
  display: block;
}

.mbs__backdrop {
  position: absolute;
  inset: 0;
  background: var(--mbs-backdrop-color);
  opacity: 0;
  transition: opacity var(--mbs-transition-duration) var(--mbs-ease);
  cursor: pointer;
}

/* Ensure invisible backdrop never traps scroll/clicks when sheet is closed */
.mbs:not(.is-open) .mbs__backdrop {
  pointer-events: none !important;
}

.mbs.is-open .mbs__backdrop {
  opacity: 1;
}

/* ==========================================================================
   Sheet Container
   ========================================================================== */

.mbs__sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: #ffffff;
  border-top-left-radius: var(--mbs-radius);
  border-top-right-radius: var(--mbs-radius);
  box-shadow: 0 -16px 40px rgba(0, 0, 0, 0.2);
  transform: translateY(100%);
  transition: transform var(--mbs-transition-duration) var(--mbs-ease);
  height: calc(100dvh - 8px);  /* Set actual height (not just max-height) */
  max-height: calc(100dvh - 8px);
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
  overscroll-behavior: contain;
}

/* Fallback for browsers without dvh support */
@supports not (height: 100dvh) {
  .mbs__sheet {
    height: calc(100vh - 8px);
    max-height: calc(100vh - 8px);
  }
}

.mbs.is-open .mbs__sheet {
  transform: translateY(0);
}

/* ==========================================================================
   Sheet Header (Drag Handle + Title + Close Button)
   ========================================================================== */

.mbs__header {
  position: relative;  /* Changed from sticky - header is part of flow, not overlapping */
  background: #ffffff;
  padding: 12px 16px 16px;
  border-top-left-radius: var(--mbs-radius);
  border-top-right-radius: var(--mbs-radius);
  flex-shrink: 0;
  border-bottom: 1px solid #f3f4f6;
  /* Add safe area for notched devices */
  padding-top: calc(12px + env(safe-area-inset-top));
}

/* Drag Handle */
.mbs__handle {
  width: 44px;
  height: 4px;
  border-radius: 2px;
  background: #e5e7eb;
  margin: 6px auto 12px;
  cursor: grab;
  touch-action: none;
}

.mbs__handle:active {
  cursor: grabbing;
}

/* Title */
.mbs__title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  text-align: center;
  color: #111827;
  line-height: 1.5;
  padding: 0 40px; /* Space for close button */
}

/* Close Button */
.mbs__close {
  position: absolute;
  right: 12px;
  top: calc(12px + env(safe-area-inset-top));
  background: transparent;
  border: 0;
  font-size: 32px;
  line-height: 1;
  cursor: pointer;
  color: #6b7280;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.15s ease, color 0.15s ease;
  padding: 0;
}

.mbs__close:hover,
.mbs__close:focus {
  background-color: #f3f4f6;
  color: #111827;
}

.mbs__close:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* ==========================================================================
   Sheet Body (Scrollable Content Area)
   ========================================================================== */

.mbs__body {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px 16px;
  flex: 1 1 auto;  /* Allow growth and shrinking */
  min-height: 200px;  /* Ensure minimum height to prevent collapse */
}

/* Ensure widget inside sheet takes full width */
.mbs__body .booking-widget-instant {
  max-width: 100%;
  margin: 0;
}

/* Prevent widget card from having negative margins that might overlap header */
.mbs__body .widget-card {
  margin-top: 0 !important;
}

.mbs__body .card-body {
  margin-top: 0 !important;
}

/* ==========================================================================
   Body Scroll Lock (When Sheet is Open)
   ========================================================================== */

html.mbs-open,
body.mbs-open {
  overflow: hidden !important;
  height: 100%;
  width: 100%;
}

/* Keep fixed positioning only on body to reduce iOS quirks */
body.mbs-open { position: fixed; }

/* Hide site header and mobile sticky bars when sheet is open */
body.mbs-open .st-header,
body.mbs-open .site-header,
body.mbs-open .header-wrapper,
body.mbs-open header.header-shell,
body.mbs-open nav.navbar,
body.mbs-open .sticky-mobile-booking,
body.mbs-open #sticky-mobile-button,
body.mbs-open .hotel-target-book-mobile {
  display: none !important;
}

/* Prevent legacy overlays from appearing when modern sheet is enabled */
body.mbs-enabled #booking-sheet-backdrop,
body.mbs-enabled #booking-sheet-portal {
  display: none !important;
}

/* ==========================================================================
   Mobile-Specific Adjustments
   ========================================================================== */

@media (max-width: 991px) {
  /* (Optional earlier off-screen rules removed to ensure final suppression wins) */

  /* Only show widget when it's inside the mobile sheet */
  .mbs__body .booking-widget-instant {
    display: block !important;
    visibility: visible !important;
    position: static !important;
    left: auto !important;
    top: auto !important;
    z-index: auto !important;
  }

  /* FINAL: Hide legacy sidebar booking container on mobile; must be last */
  body.mbs-enabled .fixed-on-mobile.st-fixed-form-booking,
  body.mbs-open .fixed-on-mobile.st-fixed-form-booking {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }

  /* Ensure sheet respects keyboard (visual viewport) */
  .mbs__sheet {
    max-height: calc(100dvh - 8px);
  }

  @supports (max-height: 100svh) {
    .mbs__sheet {
      max-height: calc(100svh - 8px);
    }
  }

  /* Adjust sheet header on very small devices */
  @media (max-height: 600px) {
    .mbs__header {
      padding-top: calc(8px + env(safe-area-inset-top));
      padding-bottom: 4px;
    }

    .mbs__title {
      font-size: 14px;
    }

    .mbs__handle {
      margin-bottom: 8px;
    }
  }
}

@media (max-width: 782px) {
  body.mbs-open #wpadminbar { display: none !important; }
}

/* Hide admin bar over sheet on tablets as well */
@media (max-width: 991px) {
  body.mbs-open #wpadminbar { display: none !important; }
}

/* Optional: click-thru fallback for browsers without inert */
@supports not (inert: auto) {
  body.mbs-open > *:not(#mobileBookingSheet) { pointer-events: none; }
}

/* ==========================================================================
   Desktop (≥992px): Hide Sheet Completely
   ========================================================================== */

@media (min-width: 992px) {
  #mobileBookingSheet {
    display: none !important;
  }
}

/* ==========================================================================
   Animation & Transitions
   ========================================================================== */

.mbs__sheet {
  will-change: transform;
}

/* Smooth entry */
.mbs.is-opening .mbs__sheet {
  animation: slideUp var(--mbs-transition-duration) var(--mbs-ease);
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* ==========================================================================
   Accessibility Enhancements
   ========================================================================== */

/* Focus trap visual indicator */
.mbs.is-open {
  /* Ensure high contrast for accessibility */
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
  .mbs__sheet,
  .mbs__backdrop {
    transition-duration: 0.01ms !important;
  }
}

/* ==========================================================================
   Touch & Gesture Enhancements
   ========================================================================== */

/* Prevent pull-to-refresh while sheet is open */
.mbs-open {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* Prevent text selection during drag */
.mbs__header {
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ==========================================================================
   Safe Area & Notch Support
   ========================================================================== */

/* iPhone X+ notch support */
@supports (padding: env(safe-area-inset-top)) {
  .mbs__sheet {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .mbs__header {
    padding-top: calc(12px + env(safe-area-inset-top));
  }
}

/* ==========================================================================
   Z-Index Stacking Context
   ========================================================================== */

/*
 * Stacking order (highest to lowest):
 * 9999 - Mobile booking sheet (must be above site header)
 * 10000 - Flatpickr calendar (above sheet content)
 * 1080 - Other modals
 * 1000+ - Site header
 */

.mbs {
  z-index: 9999;
}

/* Ensure Flatpickr calendar appears above sheet body content */
.mbs-open .flatpickr-calendar {
  z-index: 10000 !important;
}

/* Ensure Package modal (Magnific Popup) renders above the sheet */
body.mbs-open .mfp-bg { z-index: 10040 !important; }
body.mbs-open .mfp-wrap { z-index: 10050 !important; }
body.mbs-open #st-package-popup { position: relative; z-index: 10060 !important; }

/* ==========================================================================
   Widget Adjustments When Inside Sheet
   ========================================================================== */

/* Remove sticky behavior when widget is inside sheet */
.mbs__body .sticky-top {
  position: static !important;
}

.mbs__body .sticky-offset-md {
  position: static !important;
}

/* Reset z-index from widget wrapper (prevents covering sheet header) */
.mbs__body .fixed-on-mobile,
.mbs__body .st-fixed-form-booking {
  position: static !important;
  z-index: auto !important;
}

/* Ensure card takes full width and renders properly */
.mbs__body .widget-card {
  width: 100%;
  border: none;
  box-shadow: none;
  display: block !important;
  min-height: 200px;  /* Prevent collapse */
  height: auto !important;  /* Force height calculation */
}

.mbs__body .card-body {
  padding: 0;
  display: flex !important;  /* Restore flex for proper layout */
  flex-direction: column !important;
  min-height: 100px;  /* Prevent collapse */
  height: auto !important;  /* Force height calculation */
}

/* Force booking view to have height */
.mbs__body .booking-widget-instant .view-pane {
  min-height: 300px !important;
  height: auto !important;
}

/* Ensure widget sections render */
.mbs__body .widget-section {
  min-height: 40px !important;
}
