/**
 * Fresh Dhara - Dynamic Screen & Aspect Ratio Stylesheet
 * Responsive auto-adjustment across all devices, aspect ratios, orientations & PWA display modes.
 */

/* ==========================================================================
   1. Root Variables & Safe Area Insets
   ========================================================================== */
:root {
  /* Safe Area Insets (iOS Dynamic Island, Notches, Android Gesture Bars) */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  /* Modern Dynamic Viewport Units */
  --dvh: 100dvh;
  --dvw: 100dvw;
  --svh: 100svh;
  --lvh: 100lvh;

  /* Fluid typography & spacing scale */
  --fluid-h1: clamp(1.75rem, 4vw + 1rem, 3.25rem);
  --fluid-h2: clamp(1.4rem, 3vw + 0.8rem, 2.4rem);
  --fluid-h3: clamp(1.15rem, 2vw + 0.6rem, 1.75rem);
  --fluid-body: clamp(0.875rem, 0.5vw + 0.8rem, 1.05rem);
  --fluid-small: clamp(0.75rem, 0.4vw + 0.7rem, 0.9rem);
  --fluid-gap: clamp(10px, 2vw, 24px);
  --container-max-w: 1440px;
}

/* Fallback for browsers lacking dvh */
@supports not (height: 100dvh) {
  :root {
    --dvh: calc(var(--vh, 1vh) * 100);
    --dvw: calc(var(--vw, 1vw) * 100);
  }
}

/* ==========================================================================
   2. Global Baseline & Screen Adaptation
   ========================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  height: 100%;
}

body {
  min-height: 100%;
  min-height: var(--app-height, 100dvh);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* PWA Standalone Mode Polish */
html[data-pwa-standalone="true"] body {
  overscroll-behavior-y: contain;
  user-select: none;
  -webkit-user-select: none;
}

html[data-pwa-standalone="true"] input,
html[data-pwa-standalone="true"] textarea,
html[data-pwa-standalone="true"] select,
html[data-pwa-standalone="true"] [contenteditable="true"] {
  user-select: text;
  -webkit-user-select: text;
}

/* Touch responsiveness */
button, a, input, select, textarea, [role="button"] {
  touch-action: manipulation;
}

/* Responsive media containment */
img, video, canvas, svg {
  max-width: 100%;
  height: auto;
}

/* ==========================================================================
   3. Header, Navigation & Safe Area Inset Adaptation
   ========================================================================== */
.main-header,
.app-header,
header.site-header {
  padding-top: max(var(--safe-top), 0px) !important;
  transition: padding-top 0.25s ease, height 0.25s ease;
}

/* Mobile Bottom Navigation & Action Bars */
.bottom-nav,
.mobile-bottom-nav,
.mobile-nav-bar,
.mobile-footer-actions,
.checkout-bar-fixed,
.fixed-bottom-cta {
  padding-bottom: max(var(--safe-bottom), 8px) !important;
}

/* Floating Widgets & Toasts Safe Spacing */
#active-order-widget-container,
.active-order-widget-container {
  bottom: calc(75px + var(--safe-bottom)) !important;
}

@media (min-width: 768px) {
  #active-order-widget-container,
  .active-order-widget-container {
    bottom: calc(24px + var(--safe-bottom)) !important;
  }
}

.toast,
.toast-container,
.notification-toast,
#notificationToast {
  bottom: calc(24px + var(--safe-bottom)) !important;
}

/* Floating Action Buttons (WhatsApp, Scroll to top, Cart badge) */
.whatsapp-float,
.floating-cart-btn,
.back-to-top,
.chat-fab {
  bottom: calc(85px + var(--safe-bottom)) !important;
}

@media (min-width: 768px) {
  .whatsapp-float,
  .floating-cart-btn,
  .back-to-top,
  .chat-fab {
    bottom: calc(28px + var(--safe-bottom)) !important;
  }
}

/* ==========================================================================
   4. Aspect Ratio Tiers & Device Auto-Adjustment
   ========================================================================== */

/* --- Tier A: Ultra-Tall Phones & Folded Cover Screens (data-ratio="narrow" / 20:9, 21:9) --- */
html[data-ratio="narrow"] {
  --fluid-gap: 12px;
}

html[data-ratio="narrow"] .hero {
  padding-top: clamp(14px, 3vh, 28px);
  padding-bottom: clamp(16px, 3vh, 32px);
}

html[data-ratio="narrow"] .hero-title {
  font-size: clamp(26px, 7vw, 38px);
  line-height: 1.15;
}

html[data-ratio="narrow"] .products-grid,
html[data-ratio="narrow"] .grid-products {
  grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
  gap: 10px;
}

/* --- Tier B: Foldable Devices & Square Screens (data-ratio="square" / 4:3, 1:1, Galaxy Z Fold unfolded) --- */
html[data-ratio="square"],
html[data-device="foldable"] {
  --container-max-w: 920px;
}

html[data-ratio="square"] .container,
html[data-device="foldable"] .container {
  max-width: 100%;
  padding-left: 20px;
  padding-right: 20px;
}

/* Transform hero into balanced 2-column or wide banner on unfolded screens */
html[data-ratio="square"] .hero-content,
html[data-device="foldable"] .hero-content {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  align-items: center;
  gap: 24px;
  text-align: left;
}

html[data-ratio="square"] .hero-title,
html[data-device="foldable"] .hero-title {
  font-size: clamp(28px, 4vw, 42px);
}

html[data-ratio="square"] .products-grid,
html[data-device="foldable"] .products-grid,
html[data-ratio="square"] .product-grid,
html[data-device="foldable"] .product-grid {
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)) !important;
  gap: 16px;
}

/* Banners maintain proportionate aspect ratio so they don't fill entire vertical screen */
html[data-ratio="square"] .promo-banner,
html[data-device="foldable"] .promo-banner,
html[data-ratio="square"] .hero-banner-img,
html[data-device="foldable"] .hero-banner-img {
  max-height: 260px;
  object-fit: cover;
}

/* Modals on foldable screens become elegant centered dialogs instead of stretched mobile sheets */
html[data-ratio="square"] .modal-content,
html[data-device="foldable"] .modal-content {
  max-width: 620px;
  max-height: 85vh;
  margin: auto;
  border-radius: 20px;
}

/* --- Tier C: Landscape Mobile Devices (Height <= 500px) --- */
@media (orientation: landscape) and (max-height: 520px) {
  .main-header,
  .app-header {
    height: 50px !important;
    padding-top: var(--safe-top) !important;
  }

  .header-content,
  .header-top-row {
    height: 50px !important;
  }

  .logo,
  .header-brand h1 {
    font-size: 1.2rem !important;
  }

  .hero {
    padding: 16px 12px !important;
    min-height: auto !important;
  }

  .hero-title {
    font-size: 1.5rem !important;
  }

  .hero-subtitle {
    display: none;
  }

  /* Modals adapt to horizontal screen */
  .modal,
  .popup-overlay {
    align-items: center;
    padding: 10px var(--safe-right) 10px var(--safe-left);
  }

  .modal-content,
  .popup-content,
  .order-details-modal {
    max-height: calc(100dvh - 20px) !important;
    overflow-y: auto !important;
  }

  /* Bottom nav compact mode */
  .bottom-nav,
  .mobile-bottom-nav {
    height: 48px;
    padding-top: 4px;
  }

  .bottom-nav .nav-label {
    font-size: 10px;
  }
}

/* --- Tier D: Tablets & Medium Displays (600px <= width <= 1024px) --- */
@media (min-width: 600px) and (max-width: 1024px) {
  .container {
    max-width: 100%;
    padding: 0 24px;
  }

  .products-grid,
  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 18px;
  }

  .categories-grid,
  .category-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 14px;
  }

  .app-layout {
    grid-template-columns: 220px 1fr;
    gap: 20px;
    padding: 16px;
  }
}

/* --- Tier E: Desktop & Standard Monitors (1025px <= width <= 1799px) --- */
@media (min-width: 1025px) {
  .container {
    max-width: var(--container-max-w);
    margin-left: auto;
    margin-right: auto;
    padding: 0 32px;
  }

  .products-grid,
  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 24px;
  }

  .app-layout {
    grid-template-columns: 260px 1fr 340px;
    max-width: 1600px;
    margin: 0 auto;
  }
}

/* --- Tier F: Ultra-Wide Screens & High-Resolution Displays (Width >= 1800px or Ratio >= 2.1) --- */
html[data-device="ultrawide"],
html[data-ratio="ultrawide"] {
  --container-max-w: 1560px;
}

html[data-device="ultrawide"] .container,
html[data-ratio="ultrawide"] .container,
html[data-device="ultrawide"] .header-content,
html[data-ratio="ultrawide"] .header-content,
html[data-device="ultrawide"] .app-layout,
html[data-ratio="ultrawide"] .app-layout {
  max-width: var(--container-max-w) !important;
  margin-left: auto !important;
  margin-right: auto !important;
}

html[data-device="ultrawide"] .products-grid,
html[data-ratio="ultrawide"] .products-grid {
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 28px;
}

/* Prevent ultra-stretched banners on 34"+ monitors */
html[data-device="ultrawide"] .hero-banner,
html[data-ratio="ultrawide"] .hero-banner,
html[data-device="ultrawide"] .promo-banner-container {
  max-width: var(--container-max-w);
  margin-left: auto;
  margin-right: auto;
  border-radius: 24px;
  overflow: hidden;
}

/* ==========================================================================
   5. Desktop PWA Window Controls Overlay (WCO)
   ========================================================================== */
html[data-wco="true"] .main-header,
html[data-wco="true"] .app-header {
  padding-top: calc(var(--titlebar-height, 34px) + 6px) !important;
  -webkit-app-region: drag;
  app-region: drag;
}

html[data-wco="true"] button,
html[data-wco="true"] a,
html[data-wco="true"] input,
html[data-wco="true"] select,
html[data-wco="true"] .header-search,
html[data-wco="true"] .nav-links {
  -webkit-app-region: no-drag;
  app-region: no-drag;
}

/* ==========================================================================
   6. Dynamic Modal & Dialog Bounds
   ========================================================================== */
.modal,
.dialog-overlay,
.cart-sidebar,
.drawer {
  max-height: 100dvh;
}

.modal-body,
.dialog-body,
.cart-items-container {
  max-height: calc(100dvh - 160px);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
