/* =============================================================
   DukaanWale Mobile FIXES v44.1 (+ v45.8k33 Android WebView opts)
   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
   - v45.8k33: Android WebView text-inflation + fluid base font + safe areas
   ============================================================= */

/* ★ k33: GLOBAL — applies to ALL screen sizes — prevents Android WebView
   from inflating text when user has system text-size > 100% in accessibility,
   AND establishes a fluid base font-size that scales with viewport. */
html {
  -webkit-text-size-adjust: 100% !important;
  -moz-text-size-adjust: 100% !important;
  text-size-adjust: 100% !important;
  /* Fluid base: 13px on tiny phones, 16px on tablets+ */
  font-size: clamp(13px, 3.6vw, 16px) !important;
}

/* ★ k33: Safe-area insets (notch + gesture bar on Android 9+) */
body {
  padding-left: env(safe-area-inset-left, 0) !important;
  padding-right: env(safe-area-inset-right, 0) !important;
}
.topbar {
  padding-top: calc(12px + env(safe-area-inset-top, 0)) !important;
}

/* ★ k33: prevent uncontrolled tap-highlight + smooth scrolling */
* { -webkit-tap-highlight-color: rgba(0,0,0,0.05); }
html, body { -webkit-overflow-scrolling: touch; scroll-behavior: smooth; }

/* ★ k33: tablet sizing — between phone and desktop */
@media (min-width: 769px) and (max-width: 1024px) {
  html { font-size: 15px !important; }
}

@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: 8px 10px !important;
    gap: 6px !important;
    align-items: center !important;
    justify-content: space-between !important;
    flex-wrap: nowrap !important;
    overflow: visible !important;
    min-height: 44px !important;
  }

  .topbar h1 {
    font-size: 0.95rem !important;
    margin: 0 !important;
    flex: 0 1 auto !important;
    min-width: 0 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !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 cluster.
     On mobile, we hide MOST buttons but keep Logout + Date picker visible
     (Logout was buried in "More" menu — users couldn't find it easily) */
  .topbar > div:last-child {
    display: flex !important;
    gap: 6px !important;
    align-items: center !important;
    flex-shrink: 0 !important;
  }

  /* Hide most action buttons on mobile (still accessible via "More" menu) */
  .topbar > div:last-child > button,
  .topbar > div:last-child > span,
  .topbar > div:last-child > a {
    display: none !important;
  }

  /* But ALWAYS show: Logout button + Day End date picker */
  .topbar > div:last-child .btn-logout,
  .topbar > div:last-child [onclick*="logout()"],
  .topbar > div:last-child label:has(#dayEndDatePicker) {
    display: inline-flex !important;
    align-items: center !important;
  }

  /* Backup: show direct date picker label (no :has support) */
  #dayEndDatePicker {
    display: inline-block !important;
  }
  /* Make sure label wrapping #dayEndDatePicker is visible */
  .topbar label[for="dayEndDatePicker"],
  .topbar > div:last-child > label {
    display: inline-flex !important;
    align-items: center !important;
    gap: 4px !important;
    background: rgba(255,255,255,0.18) !important;
    padding: 3px 8px !important;
    border-radius: 16px !important;
    font-size: 0.72rem !important;
    color: #fff !important;
  }
  .topbar > div:last-child > label input[type="date"] {
    background: transparent !important;
    border: none !important;
    color: #fff !important;
    font-weight: 700 !important;
    font-size: 0.72rem !important;
    padding: 2px 0 !important;
    outline: none !important;
    min-width: 100px !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;
  }
}

/* ════════════════════════════════════════════
   ★ v45.8k33: ANDROID APP DENSITY OPTIMIZATIONS
   Compact mobile sizing — less scroll, faster nav
   ════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Tighter content padding */
  .content { padding: 8px !important; }
  .page-wrap { padding: 6px !important; }

  /* Compact module cards (Quick Access) */
  .module-card {
    padding: 10px 6px !important;
    border-radius: 10px !important;
  }
  .module-card .icon { font-size: 1.6rem !important; margin-bottom: 4px !important; }
  .module-card .name { font-size: 0.78rem !important; font-weight: 800 !important; }
  .module-card .desc { font-size: 0.62rem !important; }

  /* Compact stat tiles */
  .stat, .stat-card {
    padding: 8px 10px !important;
    border-radius: 10px !important;
  }
  .stat .num, .stat-card .num { font-size: 1.1rem !important; }
  .stat .label, .stat-card .label { font-size: 0.65rem !important; }

  /* Buttons - smaller touch targets but still 36px+ */
  .btn { padding: 8px 12px !important; font-size: 0.82rem !important; min-height: 36px !important; }
  .btn-sm { padding: 6px 10px !important; font-size: 0.75rem !important; }
  .btn-back { padding: 5px 9px !important; font-size: 0.72rem !important; }

  /* Inputs — prevent iOS auto-zoom but compact */
  input[type=text], input[type=number], input[type=date], input[type=tel], input[type=email],
  select, textarea {
    font-size: 16px !important;  /* required to prevent iOS zoom */
    padding: 8px 10px !important;
    border-radius: 8px !important;
  }

  /* Headings */
  h2 { font-size: 1.1rem !important; margin: 8px 0 6px !important; }
  h3 { font-size: 0.95rem !important; margin: 8px 0 5px !important; }
  h4 { font-size: 0.85rem !important; margin: 6px 0 4px !important; }

  /* Tables - allow horizontal scroll but reduce padding */
  table { font-size: 0.75rem !important; }
  th, td { padding: 6px 5px !important; }

  /* Modals - full-width on phones */
  .modal-content, [class*="modal"][class*="content"] {
    width: 96vw !important;
    max-width: 96vw !important;
    margin: 8px !important;
    padding: 14px !important;
    border-radius: 12px !important;
  }

  /* Forms - tighter row spacing */
  .form-row { margin-bottom: 8px !important; }
  label { font-size: 0.78rem !important; margin-bottom: 3px !important; }
}

/* ════════════════════════════════════════════
   ★ v45.8k33: HIGH-DPI DENSITY FIX
   For phones with pixel ratio > 2 (most modern Android)
   ════════════════════════════════════════════ */
@media (max-width: 420px) {
  html { font-size: 13px !important; }
  .topbar { padding: 6px 8px !important; }
  .topbar h1 { font-size: 0.88rem !important; }
  .module-grid { gap: 6px !important; }
  .module-card { padding: 8px 4px !important; }
}

/* ════════════════════════════════════════════
   ★ v45.8k33: TINY PHONES (≤ 360px width)
   ════════════════════════════════════════════ */
@media (max-width: 360px) {
  html { font-size: 12.5px !important; }
  .module-grid { grid-template-columns: repeat(2, 1fr) !important; }
  .topbar h1 { font-size: 0.82rem !important; }
}
