/* ==========================================================================
   MANSATI STORE MANAGER — UNIVERSAL MOBILE & MULTI-DEVICE RESPONSIVE SYSTEM
   Optimized for Smartphones (Android & iOS), Tablets, and High-DPI Screens
   ========================================================================== */

/* Universal Touch & Scroll Optimizations */
html, body {
  overflow-x: hidden !important;
  width: 100% !important;
  max-width: 100vw !important;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* --------------------------------------------------------------------------
   1. TABLETS & COMPACT LAPTOPS (<= 1024px)
   -------------------------------------------------------------------------- */
@media (max-width: 1024px) {
  .sidebar {
    transform: translateX(100%);
    width: 280px;
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.6);
    z-index: 1001;
  }
  
  .sidebar.open {
    transform: translateX(0);
  }
  
  .sidebar-overlay.active {
    display: block;
    opacity: 1;
    z-index: 1000;
  }

  .main-container {
    margin-right: 0 !important;
    width: 100% !important;
  }

  .mobile-bottom-nav {
    display: block;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .content-area {
    padding: 1rem 1rem calc(var(--bottom-nav-height) + 2.5rem);
    max-width: 100vw;
  }

  .header-search {
    width: 200px;
  }

  /* 2-column chart grids stack vertically on tablets */
  div[style*="grid-template-columns: 2fr"],
  div[style*="grid-template-columns: 1.5fr"],
  .charts-grid {
    grid-template-columns: 1fr !important;
  }
}

/* --------------------------------------------------------------------------
   2. TABLETS & PHABLETS (<= 768px)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  /* Generic Responsive Grids */
  .content-area div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
    gap: 0.85rem !important;
  }

  /* Metric KPI Cards (2 Columns on Phablets/Tablets) */
  #reportsKpiContainer,
  .metric-grid,
  .content-area div[style*="grid-template-columns: repeat(auto-fit, minmax(220px"],
  .content-area div[style*="grid-template-columns: repeat(auto-fit, minmax(200px"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.75rem !important;
  }

  .metric-card {
    padding: 0.85rem 1rem !important;
  }

  .metric-value {
    font-size: 1.3rem !important;
  }

  /* Card Headers Wrap Cleanly */
  .card-header {
    flex-wrap: wrap !important;
    gap: 0.75rem !important;
    align-items: flex-start !important;
  }

  .card-header > div:last-child {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* Page Top Header with Title and Action Buttons */
  .content-area > div:first-child.flex.items-center.justify-between,
  .content-area > div:first-child[style*="justify-content: space-between"] {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.85rem !important;
  }

  .content-area > div:first-child .flex {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* Modals */
  .modal-card {
    max-width: 96vw !important;
    margin: 0.5rem;
  }

  .login-modal-container {
    max-width: 100% !important;
    padding: 0.5rem 0 !important;
    gap: 0.85rem !important;
  }

  .login-modal-card {
    max-width: calc(100vw - 1.25rem) !important;
    margin: 0 auto !important;
    border-radius: var(--radius-xl, 20px) !important;
  }

  .login-modal-header {
    padding: 1.4rem 1.25rem 0.5rem !important;
  }

  .login-brand-img-full {
    width: 68px !important;
    max-height: 72px !important;
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
    filter: none !important;
  }

  .login-title {
    font-size: 1.2rem !important;
  }
}

/* --------------------------------------------------------------------------
   3. SMARTPHONES (<= 640px)
   -------------------------------------------------------------------------- */
@media (max-width: 640px) {
  /* Topbar minimalism on mobile */
  .top-header {
    height: 58px !important;
    padding: 0 0.75rem !important;
  }

  .header-search {
    display: none !important;
  }

  #topbarInstallBtn {
    display: none !important;
  }
  
  .user-profile-btn {
    padding: 0.25rem 0.4rem !important;
  }

  .user-profile-btn span.user-name-text {
    display: none !important;
  }

  .user-profile-btn .avatar-circle {
    width: 34px !important;
    height: 34px !important;
    font-size: 0.85rem !important;
  }

  .content-area {
    padding: 0.85rem 0.65rem calc(var(--bottom-nav-height) + 2.5rem) !important;
    gap: 0.85rem !important;
  }

  /* KPI Cards Grid (Compact 2 columns on Phones) */
  #reportsKpiContainer,
  .metric-grid,
  .content-area div[style*="grid-template-columns: repeat(auto-fit, minmax(220px"],
  .content-area div[style*="grid-template-columns: repeat(auto-fit, minmax(200px"],
  .content-area div[style*="grid-template-columns: repeat(auto-fit, minmax(160px"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.6rem !important;
  }

  .metric-card {
    padding: 0.75rem 0.85rem !important;
    border-radius: var(--radius-lg) !important;
  }

  .metric-label {
    font-size: 0.75rem !important;
    line-height: 1.3;
  }

  .metric-icon {
    width: 32px !important;
    height: 32px !important;
    font-size: 1rem !important;
  }

  .metric-value {
    font-size: 1.15rem !important;
    margin: 0.35rem 0 !important;
  }

  /* All Charts & Cards (Full Width 100%) */
  .card {
    padding: 1rem 0.85rem !important;
    border-radius: var(--radius-lg) !important;
  }

  .card-title {
    font-size: 0.95rem !important;
  }

  /* Filter Button Groups */
  .btn-group {
    width: 100% !important;
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .btn-group .btn {
    flex: 1 1 auto !important;
    font-size: 0.75rem !important;
    padding: 0.4rem 0.5rem !important;
    white-space: nowrap !important;
  }

  /* All Form Grids within Modals stack vertically */
  .form-grid,
  .modal-body div[style*="grid-template-columns"],
  .card-body div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
    gap: 0.75rem !important;
  }

  /* Modern Bottom Sheet Modals on Phones */
  .modal-backdrop {
    display: none !important;
    padding: 0 !important;
    align-items: flex-end !important;
    background: rgba(0, 0, 0, 0.7) !important;
    z-index: 1000 !important;
  }

  .modal-backdrop.active {
    display: flex !important;
  }

  .modal-card {
    border-radius: var(--radius-2xl) var(--radius-2xl) 0 0 !important;
    max-height: 88vh !important;
    margin: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    animation: modal-slide-up 0.3s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }

  @keyframes modal-slide-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  .modal-header {
    padding: 1rem 1.15rem !important;
  }

  .modal-body {
    padding: 1rem 1.15rem !important;
    max-height: 70vh !important;
    overflow-y: auto !important;
  }

  .modal-footer {
    padding: 0.85rem 1.15rem !important;
    flex-direction: column-reverse !important;
    gap: 0.5rem !important;
  }

  .modal-footer button {
    width: 100% !important;
    padding: 0.75rem !important;
  }

  /* Table Smooth Horizontal Touch Scrolling */
  .table-responsive {
    border-radius: var(--radius-md) !important;
    -webkit-overflow-scrolling: touch;
    width: 100% !important;
    max-width: 100% !important;
  }

  .data-table, .table {
    min-width: 540px !important;
  }

  .data-table th, .data-table td,
  .table th, .table td {
    padding: 0.55rem 0.65rem !important;
    font-size: 0.8rem !important;
  }

  /* Table action buttons */
  .data-table td .flex.items-center.gap-1 {
    flex-wrap: wrap;
    gap: 0.3rem;
  }

  /* Toast Notifications on Mobile */
  .toast-container {
    top: 0.85rem !important;
    left: 0.75rem !important;
    right: 0.75rem !important;
    max-width: none !important;
  }

  .toast {
    padding: 0.75rem 1rem !important;
    font-size: 0.85rem !important;
  }

  /* Mobile Bottom Navigation */
  .mobile-bottom-nav {
    height: 60px !important;
  }

  .mobile-nav-item {
    font-size: 0.68rem !important;
  }

  .quick-add-btn {
    width: 46px !important;
    height: 46px !important;
    font-size: 1.3rem !important;
  }

  /* PWA Install Banner on Mobile: Clean Top Drawer */
  .pwa-install-banner {
    position: fixed !important;
    top: 66px !important;
    bottom: auto !important;
    left: 0.75rem !important;
    right: 0.75rem !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0.65rem 0.85rem !important;
    border-radius: var(--radius-lg) !important;
    background: rgba(18, 24, 38, 0.95) !important;
    border: 1px solid rgba(99, 102, 241, 0.35) !important;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6) !important;
    z-index: 998 !important;
  }

  .pwa-banner-icon {
    width: 36px !important;
    height: 36px !important;
    font-size: 1.2rem !important;
  }

  .pwa-banner-title {
    font-size: 0.82rem !important;
  }

  .pwa-banner-desc {
    font-size: 0.7rem !important;
  }

  /* Mobile Tabs Navigation (Pills) */
  .scrollable-tabs-bar {
    padding: 0.35rem 0.25rem 0.75rem !important;
    gap: 0.45rem !important;
    margin-top: 0.85rem !important;
  }

  .scrollable-tabs-bar .btn,
  .ren-tab-btn,
  .pause-tab-btn,
  .fin-tab-btn {
    padding: 0.45rem 0.85rem !important;
    font-size: 0.78rem !important;
  }
}

/* --------------------------------------------------------------------------
   4. EXTRA COMPACT SMARTPHONES (<= 380px)
   -------------------------------------------------------------------------- */
@media (max-width: 380px) {
  .metric-value {
    font-size: 1.05rem !important;
  }

  .metric-label {
    font-size: 0.7rem !important;
  }

  .btn {
    font-size: 0.75rem !important;
    padding: 0.45rem 0.75rem !important;
  }

  .card {
    padding: 0.75rem 0.65rem !important;
  }
}
