/**
 * OSAPEX GROUP — Comprehensive Mobile-First Responsive Engine
 * Native mobile UX: touch targets, safe areas, bottom nav, gestures
 * Breakpoints: 480 | 640 | 768 | 992 | 1200 | 1440
 */

/* ═══════════════════════════════════════════════════════════════════════════
   BASE MOBILE UTILITIES
   ═══════════════════════════════════════════════════════════════════════════ */
:root {
  --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);
  --container-px: 20px;
}

/* iOS smooth momentum scrolling */
* { -webkit-tap-highlight-color: transparent; }
body { -webkit-overflow-scrolling: touch; }

/* Universal minimum touch target */
a, button, input[type=submit], input[type=button], label[for] {
  min-height: 44px;
  min-width: 44px;
}

/* Responsive scroll container for all tables */
.dashboard-table-card,
.table-container,
.overflow-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

/* ═══════════════════════════════════════════════════════════════════════════
   CONTAINER PADDING
   ═══════════════════════════════════════════════════════════════════════════ */
.container { padding: 0 var(--container-px); }
.section-padding { padding: 80px var(--container-px); }

/* ═══════════════════════════════════════════════════════════════════════════
   ≤ 1200px — LARGE TABLET / SMALL DESKTOP
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 1200px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr) !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   ≤ 992px — TABLET LANDSCAPE
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 992px) {
  :root { --container-px: 16px; }

  .section-padding { padding: 60px var(--container-px) !important; }

  .hero-section > div {
    grid-template-columns: 1fr !important;
    text-align: center !important;
  }
  .hero-section img {
    max-width: 440px;
    display: block;
    margin: 24px auto 0;
  }

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

  #checkoutForm { grid-template-columns: 1fr !important; }

  /* Agent dashboard — hide sidebar on tablet */
  .agent-sidebar { width: 220px !important; }

  /* Dashboard admin sidebar */
  .dashboard-sidebar { width: 220px !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   ≤ 768px — TABLET PORTRAIT / LARGE MOBILE
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  :root { --container-px: 14px; }

  /* Hide desktop nav */
  .main-nav { display: none !important; }
  .top-bar  { display: none !important; }

  /* Hero */
  .hero-section { padding: 56px 16px 64px !important; }
  .hero-section h1 { font-size: clamp(28px, 8vw, 48px) !important; }
  .hero-section .hero-sub { font-size: 14px !important; }
  .hero-section .hero-cta-group {
    flex-direction: column !important;
    align-items: center !important;
    gap: 10px !important;
  }
  .hero-section .btn       { width: 100%; max-width: 320px; text-align:center; }

  /* Products grid — 2 columns on mobile */
  .products-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  .product-card { padding: 10px !important; }
  .product-card h3 { font-size: 12px !important; }
  .product-card .product-price { font-size: 14px !important; }
  .product-card .btn { font-size: 11px !important; padding: 7px 10px !important; }

  /* KPI cards — 2 col */
  .kpi-grid { grid-template-columns: 1fr 1fr !important; gap: 10px !important; }
  .kpi-card { padding: 16px !important; }
  .kpi-value { font-size: 20px !important; }

  /* Agent sidebar hidden — mobile drawer used instead */
  .agent-sidebar { display: none !important; }
  .agent-main    { min-height: 100vh; }
  .agent-topbar  { padding: 12px 16px !important; }
  .agent-body    { padding: 14px !important; }

  /* Admin dashboard sidebar hidden */
  .dashboard-sidebar  { display: none !important; }
  .dashboard-main     { width: 100% !important; }
  .dashboard-topbar   { padding: 12px 16px !important; }
  .dashboard-body     { padding: 14px !important; }

  /* Checkout */
  .checkout-grid { grid-template-columns: 1fr !important; }
  .payment-option-card { padding: 12px !important; }

  /* Cart drawer — full width on mobile */
  #osapexCartDrawer { width: 100vw !important; border-radius: 20px 20px 0 0 !important; top: auto !important; max-height: 92vh !important; overflow: hidden; }
  #osapexCartDrawer.open { transform: translateY(0) !important; }
  #osapexCartOverlay.open { display: block !important; }

  /* AI chat widget */
  #osa-panel {
    bottom: 0 !important; right: 0 !important; left: 0 !important;
    width: 100% !important; max-width: 100% !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 85vh !important;
  }
  #osa-btn { bottom: 80px !important; right: 16px !important; }

  /* PWA install banner */
  #osapex-pwa-banner { flex-direction: column; gap: 12px; padding: 14px 16px; }
  #osapex-pwa-banner > div { justify-content: center; }

  /* Notifications dropdown */
  #notifDropdown { width: 92vw !important; right: -60px !important; }

  /* WhatsApp float */
  a[href*="wa.me"] { right: 16px !important; bottom: 80px !important; }

  /* Footer */
  footer > div:first-child { gap: 28px !important; }

  /* Section headings */
  h2 { font-size: clamp(22px, 6vw, 34px) !important; }
  h3 { font-size: clamp(16px, 5vw, 22px) !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   ≤ 640px — STANDARD MOBILE
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {
  :root { --container-px: 12px; }

  /* Full-width buttons on mobile */
  .btn-block-mobile { width: 100% !important; text-align: center; }

  /* 2 column products stays */
  .products-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 8px !important; }

  /* Cart items compact */
  #osapexCartBody .cart-item { gap: 8px !important; }

  /* AI widget */
  #osa-btn { width: 50px !important; height: 50px !important; }
  #osa-input-row { padding: 10px !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   ≤ 480px — COMPACT MOBILE
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 480px) {
  :root { --container-px: 10px; }

  /* KPI — single column stacked */
  .kpi-grid { grid-template-columns: 1fr !important; gap: 8px !important; }

  /* Products — keep 2 col but slimmer */
  .products-grid { gap: 8px !important; }
  .product-card img { height: 110px !important; object-fit: cover; }

  /* Hero */
  .hero-section { padding: 44px 12px 52px !important; }
  .hero-section h1 { font-size: clamp(24px, 9vw, 36px) !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE BOTTOM NAVIGATION BAR (floating safe-area aware)
   ═══════════════════════════════════════════════════════════════════════════ */
#osapex-mobile-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9990;
  background: rgba(3,14,32,0.98);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid rgba(0,191,255,0.15);
  padding: 8px 0 calc(8px + var(--safe-bottom));
  display: none; /* shown by JS on mobile */
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 -8px 30px rgba(0,0,0,0.5);
  animation: osapexSlideUp 0.3s ease;
}

.osapex-mobile-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 14px;
  border-radius: 12px;
  text-decoration: none;
  color: #475569;
  font-size: 10px;
  font-weight: 700;
  font-family: 'Inter', sans-serif;
  transition: color 0.15s, background 0.15s;
  min-width: 60px;
  cursor: pointer;
}
.osapex-mobile-nav-item .nav-icon { font-size: 22px; line-height: 1; }
.osapex-mobile-nav-item.active,
.osapex-mobile-nav-item:hover {
  color: #0EA5E9;
  background: rgba(14,165,233,0.1);
}

/* Body padding so content doesn't hide behind mobile nav */
@media (max-width: 768px) {
  body.has-mobile-nav {
    padding-bottom: calc(68px + var(--safe-bottom));
  }
  #osapex-mobile-nav { display: flex; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   AGENT MOBILE DRAWER (slide from bottom on mobile)
   ═══════════════════════════════════════════════════════════════════════════ */
#agentMobileDrawer {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 9995;
  background: rgba(3,14,32,0.99);
  backdrop-filter: blur(24px);
  border-top: 1px solid rgba(0,191,255,0.2);
  border-radius: 20px 20px 0 0;
  padding: 8px 0 calc(16px + var(--safe-bottom));
  box-shadow: 0 -16px 50px rgba(0,0,0,0.7);
  max-height: 80vh;
  overflow-y: auto;
  animation: osapexSlideUp 0.3s cubic-bezier(0.16,1,0.3,1);
}
@media (max-width: 768px) {
  #agentMobileDrawer.open { display: block; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   CARD RESPONSIVE STACKING
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .two-col-grid  { grid-template-columns: 1fr !important; }
  .three-col-grid { grid-template-columns: 1fr 1fr !important; }
  .four-col-grid { grid-template-columns: 1fr 1fr !important; }

  .agent-topbar > div:last-child { display: none !important; }

  /* Admin form rows become single col */
  .form-row { grid-template-columns: 1fr !important; }

  /* Data card tables compact  */
  table.agent-table th { padding: 10px 12px !important; font-size: 9px !important; }
  table.agent-table td { padding: 10px 12px !important; font-size: 12px !important; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE CART POSITIONING (slides up from bottom as sheet)
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  #osapexCartDrawer {
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    max-height: 90vh;
    border-radius: 20px 20px 0 0 !important;
    transform: translateY(100%) !important;
    transition: transform 0.35s cubic-bezier(0.16,1,0.3,1) !important;
  }
  #osapexCartDrawer.open {
    transform: translateY(0) !important;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE HEADER SAFE AREA ADJUSTMENTS
   ═══════════════════════════════════════════════════════════════════════════ */
.header-sticky {
  padding-top: calc(0px + var(--safe-top));
}

/* ═══════════════════════════════════════════════════════════════════════════
   TYPOGRAPHY FLUID SCALE
   ═══════════════════════════════════════════════════════════════════════════ */
.fluid-h1 { font-size: clamp(28px, 6vw, 64px); }
.fluid-h2 { font-size: clamp(22px, 4.5vw, 48px); }
.fluid-h3 { font-size: clamp(16px, 3vw, 28px); }
.fluid-p  { font-size: clamp(13px, 2vw, 17px); }

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE NOTIF DROPDOWN — full-width sheet
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {
  .notif-dropdown {
    position: fixed !important;
    top: auto !important; bottom: 73px !important;
    left: 8px !important; right: 8px !important;
    width: auto !important;
    border-radius: 16px !important;
  }
  .notif-dropdown.open { display: block !important; }
}
