/**
 * YourStall Platform - Responsive Views & Multi-Device Adapters
 * Styles for:
 * 1. PC Desktop High-Resolution View
 * 2. Mobile Chrome / Mobile Browser Web View
 * 3. Native PWA / App Standalone View
 */

/* ==========================================================================
   Universal Layout Containers & Overflow Protection (Phase 1)
   ========================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  overflow-x: hidden !important;
  max-width: 100vw;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Desktop Centered Content Wrapper (Max 1440px) */
.desktop-content-container,
.page-responsive-wrap,
.responsive-container {
  width: 100%;
  max-width: var(--desktop-max-width, 1440px);
  margin-left: auto;
  margin-right: auto;
  padding-left: 0.75rem;
  padding-right: 0.75rem;
  box-sizing: border-box;
}

@media (min-width: 640px) {
  .desktop-content-container,
  .page-responsive-wrap,
  .responsive-container {
    padding-left: 1.25rem;
    padding-right: 1.25rem;
  }
}

@media (min-width: 1024px) {
  .desktop-content-container,
  .page-responsive-wrap,
  .responsive-container {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }
}

/* Safe Area Inset Helpers */
.safe-top {
  padding-top: env(safe-area-inset-top, 0px) !important;
}
.safe-bottom {
  padding-bottom: env(safe-area-inset-bottom, 0px) !important;
}
.safe-left {
  padding-left: env(safe-area-inset-left, 0px) !important;
}
.safe-right {
  padding-right: env(safe-area-inset-right, 0px) !important;
}

/* Bottom Navigation Spacing Guard (Prevents content hiding behind bottom bar) */
.has-bottom-nav,
.tab-content-with-nav {
  padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)) !important;
}
@media (min-width: 768px) {
  .has-bottom-nav,
  .tab-content-with-nav {
    padding-bottom: 1.5rem !important;
  }
}

/* Safe Text Wrapping */
.text-truncate-safe {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.break-words-safe {
  word-break: break-word;
  overflow-wrap: break-word;
}

/* ==========================================================================
   1. PC Desktop & High-Resolution Displays (>= 1024px)
   ========================================================================== */
@media (min-width: 1024px) {
  .sidebar-width {
    width: 170px !important;
    min-width: 170px !important;
    max-width: 170px !important;
  }
  
  .header-brand-logo {
    width: 170px !important;
  }

  /* Desktop Multi-Column Grid Layouts */
  .desktop-grid-orders {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1.25rem;
  }

  .desktop-split-view {
    display: flex;
    flex-direction: row;
    gap: 1.5rem;
  }
}

/* ==========================================================================
   2. Mobile Web & Android WebView / APK View (< 768px)
   ========================================================================== */
@media (max-width: 767px) {
  /* 🛡️ Google Play Store Web App Compliance: Zero horizontal overflow */
  html, body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
    -webkit-overflow-scrolling: touch;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }

  /* Compact, clean mobile header bar */
  header {
    height: 48px !important;
    padding-left: env(safe-area-inset-left, 0.5rem) !important;
    padding-right: env(safe-area-inset-right, 0.5rem) !important;
    border-bottom: 1px solid rgba(51, 65, 85, 0.6) !important;
  }

  /* 🚫 Eliminate mobile header laser GPU shader tearing, artifact stripes & horizontal scanline glitch */
  .header-laser-border,
  .header-ambient-halo {
    display: none !important;
  }

  /* 🔔 Mobile Header Bell Icon: ONLY Bell on Mobile, ZERO Outer Box (Pure Bare Icon) */
  #header-bell-btn,
  #site-bell-btn,
  .mobile-header-bell {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0.25rem !important;
    min-height: 38px !important;
    min-width: 38px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    -webkit-tap-highlight-color: transparent !important;
  }

  #header-bell-btn i,
  #header-bell-btn svg,
  #site-bell-btn i,
  #site-bell-btn svg {
    width: 22px !important;
    height: 22px !important;
    stroke-width: 2.2 !important;
  }

  /* Compact Badge on Bell Icon (Positioned snugly on the bell icon shoulder, shifted down + left) */
  #header-bell-badge,
  .mobile-bell-badge {
    min-width: 16px !important;
    height: 16px !important;
    font-size: 8.5px !important;
    top: -2px !important;
    right: -4px !important;
  }

  /* Compact touch targets & padding */
  .mobile-touch-btn {
    min-height: 44px;
    padding: 0.5rem 0.85rem;
    font-size: 0.825rem;
  }

  /* 📱 INNER SIDE STRUCTURAL SYSTEM (Google Play APK Responsive Standard) */
  /* Main scroll container on mobile */
  #main-content-viewport,
  #main-content-scroll,
  .mobile-inner-pane {
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
    padding-top: 0.35rem !important;
    padding-bottom: calc(4.75rem + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Full-width responsive cards on mobile */
  .glass-card {
    border-radius: 0.85rem !important;
    padding: 0.75rem !important;
  }

  /* Clean 2-Column KPI Grid on Mobile (Stops gigantic 4-stack vertical sprawl) */
  .mobile-kpi-grid {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.5rem !important;
  }

  .mobile-kpi-grid > div {
    padding: 0.65rem !important;
  }

  /* Smooth Horizontal Scrollable Action Ribbon */
  .mobile-scroll-ribbon {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    gap: 0.4rem !important;
    padding-bottom: 0.25rem !important;
  }
  .mobile-scroll-ribbon::-webkit-scrollbar {
    display: none;
  }

  /* Clean Order Cards on Mobile */
  .order-card-mobile {
    padding: 0.75rem !important;
    gap: 0.5rem !important;
  }

  /* 📦 Smart Orders Desk - Mobile App View Enhancements */
  #tab-orders {
    width: 100% !important;
    max-width: 100vw !important;
    margin-top: 0 !important;
    padding-top: 0 !important;
  }

  /* 📱 Orders Mobile 3-Col Grid: Stretch rows to fill full height level with Carts button */
  #orders-mobile-scroll-container {
    display: flex !important;
    flex-direction: column !important;
    padding-bottom: 0 !important;
  }

  #orders-mobile-3col-grid {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    grid-template-rows: repeat(5, minmax(80px, 1fr)) !important;
    gap: 0.375rem !important;
    width: 100% !important;
    height: 100% !important;
    min-height: 100% !important;
    flex: 1 1 0% !important;
  }

  .order-card-tile {
    height: 100% !important;
    min-height: 80px !important;
  }

  .order-card-ticket {
    width: 100% !important;
    border-radius: 1rem !important;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35) !important;
    background: #0f172a !important;
    border: 1px solid rgba(51, 65, 85, 0.5) !important;
    transition: transform 0.15s ease, border-color 0.2s ease !important;
  }

  .order-card-ticket.ticket-pending {
    border-top: 3px solid #fbbf24 !important;
  }
  .order-card-ticket.ticket-prep {
    border-top: 3px solid #6366f1 !important;
  }
  .order-card-ticket.ticket-out {
    border-top: 3px solid #38bdf8 !important;
  }
  .order-card-ticket.ticket-delivered {
    border-top: 3px solid #10b981 !important;
  }
  .order-card-ticket.ticket-cancelled {
    border-top: 3px solid #64748b !important;
  }

  .order-card-ticket:active {
    transform: scale(0.99);
  }

  /* Safe Area & Mobile Bottom Navigation clearance for Orders list */
  #orders-list-container {
    padding-bottom: calc(5.5rem + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Touch-friendly full-width action bar in order cards */
  .order-action-bar-mobile {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 0.35rem !important;
    width: 100% !important;
  }

  .order-action-bar-mobile > button {
    flex: 1 1 calc(50% - 0.35rem) !important;
    min-height: 38px !important;
    font-size: 11.5px !important;
  }

  /* Ensure toast notifications don't obstruct headers on small screens */
  #toast-container {
    top: 54px !important;
    right: 12px !important;
    left: 12px !important;
    max-width: calc(100vw - 24px) !important;
  }

  /* Mobile Tables into Stacked Cards */
  .mobile-card-table {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }
}

/* ==========================================================================
   3. PWA / App Standalone View (When Installed as Native App)
   ========================================================================== */
@media (display-mode: standalone) {
  /* Prevent browser pull-to-refresh rubber banding in app mode */
  body {
    overscroll-behavior-y: none;
    user-select: none;
    -webkit-user-select: none;
  }

  /* Safe Area Insets for Mobile Notch & Home Indicator */
  header {
    padding-top: env(safe-area-inset-top, 0px);
    height: calc(52px + env(safe-area-inset-top, 0px)) !important;
  }

  .app-safe-bottom {
    padding-bottom: env(safe-area-inset-bottom, 12px);
  }

  /* Native look: no tap highlights */
  * {
    -webkit-tap-highlight-color: transparent;
  }
}

/* ==========================================================================
   4. Registration Onboarding Bot - Responsive Mobile Viewport Adapter
   ========================================================================== */
@media (max-width: 1023px) {
  /* Dynamic Viewport Height: Prevents Android browser URL bar pushing bottom nav over inputs */
  #dashboard-body-wrapper {
    height: calc(100vh - 48px);
    height: calc(100dvh - 48px) !important;
    max-height: calc(100dvh - 48px) !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  /* Prevent double scrollbar/clipping when setup tab is displayed on mobile */
  html.merchant-pending-setup #main-content-viewport,
  body.merchant-setup-active #main-content-viewport,
  body:has(#tab-setup:not(.hidden)) #main-content-viewport {
    height: 100% !important;
    max-height: 100% !important;
    padding: 0.5rem 0.5rem 0 0.5rem !important;
    padding-bottom: 0 !important;
    overflow: hidden !important;
  }

  /* Hide the top Chat AI | Blueprint toggle on mobile completely per user instruction */
  #mobile-setup-view-toggle {
    display: none !important;
  }

  /* Mobile Tab Setup Container: elevated strictly above fixed 56px bottom navigation */
  #tab-setup.hidden {
    display: none !important;
    height: 0 !important;
    min-height: 0 !important;
    max-height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
  }

  #tab-setup:not(.hidden) {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    min-height: 0 !important;
    flex: 1 1 0% !important;
    overflow: hidden !important;
    padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px) + 0.65rem) !important;
    box-sizing: border-box !important;
  }

  #view-setup-ai {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    flex: 1 1 0% !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: 100% !important;
    gap: 0 !important;
    overflow: hidden !important;
  }

  /* On mobile viewports, strictly hide the blueprint column so chat takes 100% full screen height */
  #col-setup-blueprint {
    display: none !important;
  }

  #col-setup-chat {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    flex: 1 1 0% !important;
    height: 100% !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  /* Strict column toggle on mobile */
  .mobile-col-hidden {
    display: none !important;
  }

  /* Mobile Chat Container adjustments */
  #col-setup-chat .glass-card {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    flex: 1 1 0% !important;
    height: 100% !important;
    min-height: 0 !important;
    border-radius: 1rem !important;
    border-color: rgba(6, 182, 212, 0.25) !important;
    overflow: hidden !important;
  }

  /* Smooth momentum scrolling for mobile chat stream */
  #ai-onboard-chat-stream {
    flex: 1 1 0% !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    padding: 0.75rem 0.5rem !important;
    gap: 0.6rem !important;
  }

  /* Chat message bubbles: Increased width for comfortable phone reading */
  #ai-onboard-chat-stream > div {
    width: 100% !important;
    max-width: 100% !important;
  }

  #ai-onboard-chat-stream .ai-chat-bubble > div:last-child {
    max-width: calc(100% - 2.25rem) !important;
    width: 100% !important;
  }

  #ai-onboard-chat-stream .user-chat-bubble > div:first-child {
    max-width: calc(100% - 2.25rem) !important;
  }

  /* Quick Suggestion Chips: Mobile-Only Vertical Layout (Left-Aligned, Bottom-to-Top) */
  #ai-quick-chips-wrapper {
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-end !important;
    justify-content: space-between !important;
    padding: 0.4rem 0.5rem !important;
    background: rgba(2, 6, 23, 0.98) !important;
  }

  #ai-quick-chips {
    display: flex !important;
    flex-direction: column-reverse !important;
    align-items: flex-start !important;
    justify-content: flex-start !important;
    max-height: 165px !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    white-space: normal !important;
    gap: 0.35rem !important;
    padding: 0.15rem 0 !important;
    flex: 1 1 auto !important;
    min-width: 0 !important;
    -webkit-overflow-scrolling: touch !important;
    touch-action: pan-y !important;
  }
  #ai-quick-chips::-webkit-scrollbar {
    width: 3px !important;
  }
  #ai-quick-chips::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.25) !important;
    border-radius: 9999px !important;
  }

  /* Full touch-friendly chips on mobile, stacked bottom to top */
  #ai-quick-chips button {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: flex-start !important;
    width: auto !important;
    max-width: 96% !important;
    min-height: 32px !important;
    height: auto !important;
    white-space: normal !important;
    text-align: right !important;
    padding: 0.35rem 0.65rem !important;
    font-size: 11.5px !important;
    line-height: 1.35 !important;
    border-radius: 0.5rem !important;
    flex-shrink: 0 !important;
  }

  #ai-step-back-holder {
    display: flex !important;
    align-self: flex-end !important;
    padding-bottom: 0.2rem !important;
    margin-left: 0.35rem !important;
    flex-shrink: 0 !important;
  }

  /* Chat Input Bar on Mobile */
  #form-ai-setup-chat {
    gap: 0.35rem !important;
  }
  #form-ai-setup-chat input {
    font-size: 13.5px !important;
    height: 42px !important;
    padding-left: 0.75rem !important;
    padding-right: 0.75rem !important;
  }
  #btn-ai-onboard-mic,
  #btn-ai-onboard-send {
    min-height: 42px !important;
    height: 42px !important;
  }

}

@media (max-width: 767px) {
  /* 📱 MOBILE CHROME FIRST: Catalog Live Desk & Rail View */
  #tab-catalog {
    width: 100% !important;
    max-width: 100vw !important;
    margin-top: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
  }

  #catalog-mobile-scroll-container {
    padding-bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px)) !important;
    -webkit-overflow-scrolling: touch;
  }

  #mobile-catalog-rail {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    max-height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    scrollbar-width: none;
    gap: 3px !important;
  }
  #mobile-catalog-rail::-webkit-scrollbar {
    display: none;
  }

  .catalog-rail-tab {
    flex: 1 1 0% !important;
    min-height: 46px !important;
    max-height: 56px !important;
    padding: 3px 2px !important;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    overflow: visible !important;
  }

  .catalog-rail-tab i,
  .catalog-rail-tab svg {
    width: 22px !important;
    height: 22px !important;
    stroke-width: 2.2 !important;
  }

  .catalog-rail-tab .catalog-rail-label {
    display: block !important;
    font-size: 8.5px !important;
    font-weight: 800 !important;
    line-height: 1.1 !important;
    text-align: center !important;
    white-space: normal !important;
    word-break: break-word !important;
    max-width: 100% !important;
    overflow: visible !important;
    text-overflow: clip !important;
    margin-top: 1.5px !important;
    letter-spacing: -0.02em !important;
  }

  #catalog-products-grid-mobile:not(.hidden),
  #catalog-deals-grid-mobile:not(.hidden) {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.375rem !important;
    width: 100% !important;
  }

  #catalog-products-grid-mobile.hidden,
  #catalog-deals-grid-mobile.hidden {
    display: none !important;
  }

  #catalog-products-grid-mobile .catalog-product-card-mb,
  #catalog-deals-grid-mobile .catalog-deal-card-mb {
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between !important;
    padding: 0.5rem !important;
    border-radius: 0.75rem !important;
    background: rgba(15, 23, 42, 0.85) !important;
    border: 1px solid rgba(51, 65, 85, 0.7) !important;
    transition: transform 0.1s ease, border-color 0.15s ease;
    min-width: 0 !important;
    width: 100% !important;
  }

  #catalog-products-grid-mobile .catalog-product-card-mb:active,
  #catalog-deals-grid-mobile .catalog-deal-card-mb:active {
    transform: scale(0.97);
  }

  #catalog-products-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.5rem !important;
  }

  .catalog-product-card {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    padding: 0.5rem 0.65rem !important;
    border-radius: 1rem !important;
    gap: 0.75rem !important;
    background: rgba(15, 23, 42, 0.75) !important;
    border: 1px solid rgba(51, 65, 85, 0.6) !important;
  }

  .catalog-product-visual-box {
    width: 68px !important;
    height: 68px !important;
    min-width: 68px !important;
    min-height: 68px !important;
    border-radius: 0.75rem !important;
    border-bottom: none !important;
    border: 1px solid rgba(51, 65, 85, 0.7) !important;
    background: #020617 !important;
  }

  .catalog-product-body {
    padding: 0 !important;
    flex: 1 1 0% !important;
    min-width: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
  }

  /* Catalog category pill scrollbar hide & touch ergonomics */
  #catalog-category-pills-bar,
  #menu-scanner-cat-pills {
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    scrollbar-width: none;
  }
  #catalog-category-pills-bar::-webkit-scrollbar,
  #menu-scanner-cat-pills::-webkit-scrollbar {
    display: none;
  }

  /* Deals Grid on Mobile: Clean 1-column cards */
  #catalog-deals-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.75rem !important;
  }
}

@media (min-width: 768px) {
  .catalog-product-card {
    display: flex !important;
    flex-direction: column !important;
  }
}

