/* =============================================================
   DukaanWale Mobile FIXES v44.1
   Fixes for screenshot issues:
   - Right-side overflow (Tally View widget cutting content)
   - Floating widgets overlap (calc, AI chat)
   - Topbar buttons stacking weirdly
   - Quick access grid cut
   - Owner ka Hisaab horizontal scroll
   ============================================================= */

@media (max-width: 768px) {

  /* ════════════════════════════════════════════
     1. GLOBAL OVERFLOW LOCKDOWN
     ════════════════════════════════════════════ */
  html, body {
    overflow-x: hidden !important;
    width: 100% !important;
    max-width: 100vw !important;
    position: relative !important;
  }

  /* Prevent ANY element from exceeding viewport */
  .topbar,
  .page-wrap,
  .content,
  .main-layout,
  .module-grid,
  .stats-grid,
  .summary-grid {
    max-width: 100vw !important;
    overflow-x: hidden !important;
    box-sizing: border-box !important;
  }

  /* ════════════════════════════════════════════
     2. TALLY VIEW WIDGET - Hide on mobile
     (User can access from "More" menu anyway)
     ════════════════════════════════════════════ */
  .tp-toggle-btn,
  #tpToggleBtn {
    display: none !important;
  }

  /* ════════════════════════════════════════════
     3. CALCULATOR WIDGET - Reposition
     ════════════════════════════════════════════ */
  .dw-calc-icon {
    top: auto !important;
    bottom: 140px !important;  /* Above FAB and bottom nav */
    right: 12px !important;
    width: 42px !important;
    height: 42px !important;
    font-size: 18px !important;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15) !important;
  }

  .dw-calc-panel {
    top: auto !important;
    bottom: 140px !important;
    right: 12px !important;
    width: calc(100vw - 24px) !important;
    max-width: 320px !important;
    max-height: 60vh !important;
  }

  /* ════════════════════════════════════════════
     4. AI CHAT WIDGET - Reposition
     ════════════════════════════════════════════ */
  #dwAiBtn {
    bottom: 78px !important;  /* Above bottom nav (62 + safe margin) */
    right: 12px !important;
    width: 48px !important;
    height: 48px !important;
    z-index: 950 !important;
  }

  #dwAiChat {
    bottom: 138px !important;  /* Above AI button */
    right: 8px !important;
    left: 8px !important;
    width: auto !important;
    max-width: calc(100vw - 16px) !important;
    max-height: 60vh !important;
  }

  /* ════════════════════════════════════════════
     5. TOPBAR - Completely Mobile-Optimized
     ════════════════════════════════════════════ */
  .topbar {
    padding: 12px 14px !important;
    gap: 8px !important;
    align-items: center !important;
    justify-content: space-between !important;
    flex-wrap: nowrap !important;
    overflow: hidden !important;
  }

  .topbar h1 {
    font-size: 1rem !important;
    margin: 0 !important;
    flex: 1 !important;
    min-width: 0 !important;
  }

  /* First div in topbar - shop info */
  .topbar > div:first-child {
    flex: 1 !important;
    min-width: 0 !important;
    overflow: hidden !important;
  }

  /* Second div in topbar - action buttons - HIDE on mobile */
  /* These are accessible via "More" menu now */
  .topbar > div:last-child {
    display: none !important;
  }

  /* But always show user name and Logout button */
  .topbar > div:last-child #userNameTop,
  .topbar > div:last-child .btn-logout {
    display: none !important;
  }

  /* ════════════════════════════════════════════
     6. SHOP INFO CARD - Mobile-friendly
     ════════════════════════════════════════════ */
  .topbar > div:first-child > div:nth-child(2) {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 4px !important;
  }

  /* ════════════════════════════════════════════
     7. MODULE GRID (Quick Access) - 2 columns
     ════════════════════════════════════════════ */
  .module-grid,
  .quick-access-grid {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
    padding: 0 !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .module-card {
    padding: 14px 10px !important;
    margin: 0 !important;
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    text-align: center !important;
  }

  .module-card .icon {
    font-size: 1.8rem !important;
    margin-bottom: 4px !important;
  }

  .module-card .name {
    font-size: 0.85rem !important;
    font-weight: 700 !important;
  }

  .module-card .desc {
    font-size: 0.7rem !important;
    line-height: 1.2 !important;
    margin-top: 2px !important;
  }

  /* ════════════════════════════════════════════
     8. OWNER KA HISAAB - Convert horizontal to grid
     ════════════════════════════════════════════ */
  /* Find the owner card container - usually has multi-column flex */
  [class*="owner"] [style*="display:flex"][style*="flex-wrap"],
  [class*="hisaab"] [style*="display:flex"] {
    flex-wrap: wrap !important;
  }

  /* Inner cards in Owner Ka Hisaab */
  [class*="owner"] [style*="display:flex"] > div,
  [class*="hisaab"] [style*="display:flex"] > div {
    flex: 1 1 calc(50% - 8px) !important;
    min-width: calc(50% - 8px) !important;
    max-width: calc(50% - 8px) !important;
    box-sizing: border-box !important;
  }

  /* ════════════════════════════════════════════
     9. STATS GRID (top stats) - 2 columns
     ════════════════════════════════════════════ */
  .stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }

  /* If has 3 stats (Sale/Stock/Udhari), keep 2-column with last wrap */
  .stats-grid > * {
    min-width: 0 !important;
    max-width: 100% !important;
  }

  /* ════════════════════════════════════════════
     10. PLAN BAR - Mobile layout
     ════════════════════════════════════════════ */
  .planbar,
  #planbar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
    padding: 14px !important;
  }

  .planbar > div,
  #planbar > div {
    width: 100% !important;
  }

  /* Buttons in planbar - stack */
  .planbar [style*="display:flex"],
  #planbar [style*="display:flex"] {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .planbar button,
  #planbar button {
    flex: 1 1 calc(50% - 4px) !important;
    min-width: 0 !important;
    font-size: 0.78rem !important;
    padding: 10px 8px !important;
    white-space: normal !important;
  }

  /* ════════════════════════════════════════════
     11. CURRENT SHOP / ALL SHOPS PILLS - Stack
     ════════════════════════════════════════════ */
  .allshops-banner,
  [class*="shops-banner"] {
    flex-direction: column !important;
    gap: 8px !important;
  }

  .allshops-banner > * {
    width: 100% !important;
  }

  /* ════════════════════════════════════════════
     12. AAJ KE BILLS / RECENT ACTIVITY
     ════════════════════════════════════════════ */
  .bill-row,
  .activity-row {
    flex-wrap: wrap !important;
    padding: 12px 10px !important;
  }

  .bill-row .bill-no,
  .bill-row .bill-amt {
    flex: 1 1 auto !important;
  }

  /* ════════════════════════════════════════════
     13. SECTION HEADERS - Compact
     ════════════════════════════════════════════ */
  .content > h2,
  .content > h3,
  .page-wrap > h2,
  .page-wrap > h3 {
    font-size: 1rem !important;
    margin: 16px 0 10px !important;
    padding: 0 !important;
  }

  /* ════════════════════════════════════════════
     14. CARD CONTAINERS - Tight padding
     ════════════════════════════════════════════ */
  .card {
    padding: 14px !important;
    border-radius: 14px !important;
    margin-bottom: 10px !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  /* ════════════════════════════════════════════
     15. INLINE-STYLED ELEMENTS WITH FIXED WIDTH
     Force them to be responsive
     ════════════════════════════════════════════ */
  [style*="width:1100px"],
  [style*="width: 1100px"],
  [style*="width:1400px"],
  [style*="width: 1400px"],
  [style*="max-width:1100"],
  [style*="max-width:1400"] {
    max-width: 100% !important;
    width: 100% !important;
  }

  /* ════════════════════════════════════════════
     16. PAGE WRAP - Mobile padding
     ════════════════════════════════════════════ */
  .page-wrap {
    padding: 12px !important;
    gap: 10px !important;
    grid-template-columns: 1fr !important;
  }

  /* ════════════════════════════════════════════
     17. RAIL TOGGLE BUTTON - Hide
     ════════════════════════════════════════════ */
  #railToggleBtn,
  .rail-toggle-btn {
    display: none !important;
  }

  /* ════════════════════════════════════════════
     18. RAZORPAY / EXCISE / OTHER WIDGETS
     ════════════════════════════════════════════ */
  iframe.razorpay-checkout-frame,
  .razorpay-container {
    max-width: 100vw !important;
  }

  /* ════════════════════════════════════════════
     19. PERMANENT BUTTONS ROW (Add/Switch/Approvals)
     The big colored buttons in top area - hide
     ════════════════════════════════════════════ */
  #addShopBtn,
  #switchShopBtn,
  #approvalsPageBtn,
  #pendingApprovalsBtn,
  #alertSettingsBtn,
  #dwClearDataBtn {
    display: none !important;
  }

  /* But show pending approvals badge somewhere - we'll put on bottom nav */
  /* Logout - keep accessible via More menu only */
  .btn-logout {
    display: none !important;
  }

  /* ════════════════════════════════════════════
     20. SMART HEADER - Compact shop info
     ════════════════════════════════════════════ */
  /* User name and date - smaller */
  #userNameTop,
  #dateTag {
    font-size: 0.72rem !important;
  }

  /* ════════════════════════════════════════════
     21. CHARTS/RECHARTS - Full width
     ════════════════════════════════════════════ */
  .recharts-wrapper,
  .recharts-surface {
    max-width: 100% !important;
  }

  /* ════════════════════════════════════════════
     22. PRICING/PLAN CARDS
     ════════════════════════════════════════════ */
  .plan-card,
  .pricing-card {
    width: 100% !important;
    max-width: 100% !important;
    margin-bottom: 12px !important;
  }
}

/* ════════════════════════════════════════════
   23. EXTRA-SMALL PHONES (≤400px)
   ════════════════════════════════════════════ */
@media (max-width: 400px) {
  .module-grid,
  .quick-access-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }

  .stats-grid {
    grid-template-columns: 1fr !important;
  }

  .module-card .name {
    font-size: 0.8rem !important;
  }

  .module-card .desc {
    font-size: 0.65rem !important;
  }
}

/* ════════════════════════════════════════════
   24. BOTTOM NAV - Add small badge for approvals
   ════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* When dashboard loads pendingApprovalsCount, our More button can show badge */
  .dw-bottom-nav .nav-item[data-id="more"] {
    position: relative;
  }
}

/* ════════════════════════════════════════════
   25. BOTTOM NAV FORTIFICATION - NEVER HIDE
   Browser scroll, address bar, anything - nav stays
   ════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Maximum priority bottom nav */
  body > .dw-bottom-nav,
  .dw-bottom-nav {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100vw !important;
    z-index: 9990 !important;
    background: #ffffff !important;
    visibility: visible !important;
    opacity: 1 !important;
    display: flex !important;
    transform: translate3d(0, 0, 0) !important;
    -webkit-transform: translate3d(0, 0, 0) !important;
  }

  /* Body padding to prevent content under nav */
  body {
    padding-bottom: 80px !important;
  }

  /* If page has its own scroll container, ensure it doesn't trap nav */
  html {
    height: 100% !important;
    overflow-x: hidden !important;
  }
}

/* ════════════════════════════════════════════
   26. BILLING PAGE - Specific Mobile Fixes
   Long-page issue, tabs spacing, total bar
   ════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Billing mode tabs (Current Shop / All Shops) */
  .mode-tabs {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 6px !important;
    margin-bottom: 12px !important;
    padding: 0 !important;
  }

  .mode-tabs .mt {
    padding: 10px 8px !important;
    font-size: 0.8rem !important;
    text-align: center !important;
    border-radius: 10px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  /* Main billing tabs (Naya Bill / History / Udhari) */
  .tabs {
    display: grid !important;
    grid-template-columns: 1fr 1fr 1fr !important;
    gap: 6px !important;
    margin-bottom: 14px !important;
    padding: 0 !important;
    overflow: visible !important;
  }

  .tabs .tab {
    padding: 10px 6px !important;
    font-size: 0.78rem !important;
    text-align: center !important;
    border-radius: 10px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    min-height: 40px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  /* Bill layout - single column on mobile */
  .bill-layout {
    grid-template-columns: 1fr !important;
    display: block !important;
  }

  /* Cards spacing */
  .bill-layout .card {
    margin-bottom: 12px !important;
  }

  /* tbox (Cash/UPI/Udhari totals) */
  .tbox {
    padding: 12px !important;
    margin-top: 10px !important;
  }

  /* Grand Total - emphasize */
  .tbox > div:last-child,
  .tbox .grand-total {
    font-size: 1.1rem !important;
    font-weight: 800 !important;
  }

  /* Clear + Bill Save Karo buttons */
  .bill-actions,
  [style*="display:flex"][style*="gap"] {
    flex-wrap: wrap !important;
  }

  /* Save Bill button - prominent */
  button[onclick*="saveBill"],
  button[onclick*="newBill"],
  .btn-gr {
    min-height: 48px !important;
    font-size: 0.95rem !important;
    font-weight: 700 !important;
  }
}

/* ════════════════════════════════════════════
   27. PREVENT NAV HIDE DUE TO viewport units
   Mobile browsers (Chrome on Android, Safari iOS)
   collapse address bar on scroll - use dynamic vh
   ════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Use 100% instead of 100vh to avoid browser chrome issues */
  .modal-overlay,
  .dwc-overlay {
    height: 100% !important;
    min-height: 100% !important;
  }
}
