/* ==========================================================================
   assets/css/responsive.css - Comprehensive Mobile, Tablet & Desktop Responsive System
   ========================================================================== */

/* 1. Base Fluid Media, Typography & Global Layout Defaults */
html {
  -webkit-text-size-adjust: 100%;
  box-sizing: border-box;
}

*, *::before, *::after {
  box-sizing: inherit;
}

body {
  overflow-x: hidden;
}

img, svg, video, canvas {
  max-width: 100%;
  height: auto;
}

/* Auth Mode: Strictly hide navigation chrome on all viewports */
.app-shell.auth-mode .side,
.app-shell.auth-mode .topbar,
body.is-login .side,
body.is-login .topbar {
  display: none !important;
}

/* Desktop Collapsible Sidebar System */
@media (min-width: 1025px) {
  .app-shell {
    transition: grid-template-columns 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  }
  body.sidebar-collapsed .app-shell {
    grid-template-columns: 0px 1fr !important;
  }
  body.sidebar-collapsed .side {
    display: none !important;
  }
}

/* Hamburger Menu Toggle Button (Visible & Styled on all viewports) */
[data-nav-toggle] {
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  color: #1e293b !important;
  background-color: #ffffff !important;
  border: 1px solid #cbd5e1 !important;
  border-radius: 8px !important;
  width: 38px !important;
  height: 38px !important;
  min-width: 38px !important;
  min-height: 38px !important;
  padding: 0 !important;
  cursor: pointer !important;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06) !important;
  visibility: visible !important;
  opacity: 1 !important;
  flex-shrink: 0 !important;
}

[data-nav-toggle]:hover {
  background-color: #f1f5f9 !important;
  color: #0f172a !important;
  border-color: #94a3b8 !important;
}

[data-nav-toggle]:active {
  transform: scale(0.95);
}

[data-nav-toggle] svg {
  width: 22px !important;
  height: 22px !important;
  stroke: #1e293b !important;
  stroke-width: 2.2 !important;
  display: block !important;
  visibility: visible !important;
  pointer-events: none !important;
}

/* Global Data Table Responsive System (Active at ALL screen sizes) */
.table-responsive,
.table-container,
.tw,
.overflow-x-auto,
div[class*="overflow-x-auto"] {
  width: 100% !important;
  max-width: 100% !important;
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch !important;
  display: block;
}

/* Custom smooth horizontal scrollbar for data tables */
.table-responsive::-webkit-scrollbar,
.tw::-webkit-scrollbar {
  height: 6px;
}
.table-responsive::-webkit-scrollbar-track,
.tw::-webkit-scrollbar-track {
  background: #f1f5f9;
  border-radius: 4px;
}
.table-responsive::-webkit-scrollbar-thumb,
.tw::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}
.table-responsive::-webkit-scrollbar-thumb:hover,
.tw::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* Guarantee readable minimum width on wide data tables */
.table-responsive > table,
.table-container > table,
.tw > table {
  min-width: 640px;
}

/* Modals base styles */
.modal-dialog {
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-dialog .modal-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* 2. Tablet & Smaller Laptops (<= 1024px) */
@media (max-width: 1024px) {
  /* Collapse shell grid into single column */
  .app-shell {
    display: block !important;
    grid-template-columns: 100% !important;
    width: 100% !important;
    min-height: 100vh !important;
  }

  /* Off-canvas sliding drawer for sidebar */
  .side {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: 280px !important;
    max-width: 85vw !important;
    height: 100vh !important;
    z-index: 1050 !important;
    transform: translateX(-105%) !important;
    transition: transform 0.26s cubic-bezier(0.4, 0, 0.2, 1) !important;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45) !important;
    display: flex !important;
    flex-direction: column !important;
  }

  /* When drawer is opened */
  body.nav-open .side {
    transform: translateX(0) !important;
  }

  /* Smooth dimmed backdrop */
  body.nav-open::after {
    content: '';
    position: fixed !important;
    inset: 0 !important;
    background: rgba(15, 23, 42, 0.6) !important;
    backdrop-filter: blur(3px) !important;
    -webkit-backdrop-filter: blur(3px) !important;
    z-index: 1040 !important;
    animation: crmFadeIn 0.2s ease forwards !important;
  }

  /* Prevent background scroll when navigation drawer is open */
  body.nav-open {
    overflow: hidden !important;
  }

  /* Content area adjustment */
  .content-area {
    padding: 20px 16px !important;
    max-width: 100% !important;
  }

  /* 4-column KPI grids become 2 columns */
  .grid-cols-4,
  .lg\:grid-cols-4,
  .md\:grid-cols-4,
  .kpi-grid,
  .stat-grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 14px !important;
  }
}

/* 3. Tablets & Landscape Phones (<= 768px) */
@media (max-width: 768px) {
  /* Topbar header */
  .topbar {
    padding: 0 12px !important;
    height: 54px !important;
  }

  .topbar .workspace-title {
    display: none !important;
  }

  /* Top bar user badge */
  #userBadgeName {
    font-size: 11.5px !important;
    max-width: 120px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  /* Action bars & Page headers wrap naturally */
  .head,
  .page-header,
  .section-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 12px !important;
  }

  .head .acts,
  .header-actions {
    width: 100% !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .head .acts > *,
  .header-actions > * {
    flex: 1 1 auto !important;
    text-align: center !important;
  }

  /* Filter and search bars */
  .filter-bar,
  .search-row {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }

  .filter-bar > *,
  .search-row > * {
    width: 100% !important;
  }

  /* Table density adjustment */
  table th,
  table td {
    padding: 9px 10px !important;
    font-size: 12.5px !important;
  }

  /* Modals on tablet / mobile */
  .modal-backdrop {
    padding: 12px !important;
    align-items: center !important;
  }

  .modal-dialog {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 auto !important;
    max-height: 88vh !important;
    border-radius: 14px !important;
  }

  /* Cards */
  .card,
  .panel {
    padding: 14px !important;
    border-radius: 12px !important;
  }

  /* Two-column form grids collapse to 1 column */
  .modal-body .grid-cols-2,
  .modal-body .grid-cols-3,
  .modal-body .md\:grid-cols-2,
  .modal-body .md\:grid-cols-3,
  form .grid-cols-2,
  form .grid-cols-3,
  form .md\:grid-cols-2,
  form .md\:grid-cols-3 {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }
}

/* 4. Small Mobile Phones (<= 640px) */
@media (max-width: 640px) {
  /* Line items in invoice modal */
  .line-item-row {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
    background: #f8fafc;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    margin-bottom: 8px;
  }
  .line-item-row > div:nth-child(1) {
    width: 100% !important;
  }
  .line-item-row > div:nth-child(2) {
    flex: 1 1 70px !important;
  }
  .line-item-row > div:nth-child(3) {
    flex: 2 1 110px !important;
  }
  .line-item-row > div:nth-child(4) {
    flex: 0 0 32px !important;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Printable invoice sheet mobile adaptation */
  .printable-card {
    padding: 16px !important;
  }

  /* Ensure inputs are touch-friendly and prevent iOS zoom (< 16px triggers zoom) */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="date"],
  input[type="number"],
  select,
  textarea {
    font-size: 15px !important;
    min-height: 40px !important;
  }
}

/* 5. Extra Small Mobile Phones (<= 480px) */
@media (max-width: 480px) {
  .content-area {
    padding: 12px 10px !important;
  }

  /* Stat / KPI grids become single column cards */
  .grid-cols-4,
  .grid-cols-3,
  .grid-cols-2,
  .sm\:grid-cols-2,
  .sm\:grid-cols-4,
  .md\:grid-cols-4,
  .lg\:grid-cols-4,
  .kpi-grid,
  .stat-grid-4,
  .stat-grid-2 {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  /* Buttons on mobile */
  .btn,
  button[type="submit"] {
    padding: 8px 12px !important;
    font-size: 13px !important;
    min-height: 40px !important;
  }

  /* Bottom sheet style for modals on small phones */
  .modal-backdrop {
    padding: 0 !important;
    align-items: flex-end !important;
  }

  .modal-dialog {
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    max-height: 92vh !important;
  }

  /* Detail tabs */
  .tabs,
  nav.flex {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    white-space: nowrap !important;
    gap: 6px !important;
  }

  .tabs a,
  .tabs button {
    padding: 8px 12px !important;
    font-size: 12.5px !important;
    white-space: nowrap !important;
  }
}

/* Animation */
@keyframes crmFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
