/* ==========================================================================
   MOBILE-FIRST & IPAD-FIRST RESPONSIVE DESIGN SYSTEM v3.0
   Breakpoints:
   - Base: Mobile-First (0 - 639px)
   - sm: Small Tablets (640px - 767px)
   - md: iPad / Tablets (768px - 1023px)
   - lg: Laptops & Desktop (1024px - 1439px)
   - xl: Large Widescreen (1440px+)
   ========================================================================== */

/* ==========================================================================
   1. MOBILE PHONES (< 640px) — BASE STYLES (Mobile-First)
   ========================================================================== */
@media (max-width: 639px) {
  html {
    font-size: 13.5px;
  }

  .app-container {
    padding: 0.5rem 0.6rem 3rem;
  }

  /* ── Sticky Compact Mobile Header ── */
  .top-header {
    padding: 0.55rem 0.75rem;
    margin-bottom: 0.75rem;
    border-radius: var(--radius-md);
    gap: 0.4rem;
    top: 0.35rem;
    flex-wrap: nowrap;
  }

  .mobile-nav-toggle {
    display: inline-flex;
    width: 36px;
    height: 36px;
  }

  .brand-title {
    font-size: 0.82rem;
    line-height: 1.15;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
  }

  .brand-subtitle {
    display: none;
  }

  .live-badge span {
    display: none;
  }

  .live-badge {
    padding: 0.3rem;
    min-width: 28px;
    min-height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  #syncBtn span {
    display: none;
  }

  #syncBtn {
    padding: 0.35rem;
    width: 32px;
    height: 32px;
    min-height: 32px;
  }

  .btn-icon {
    width: 32px;
    height: 32px;
  }

  .header-actions {
    gap: 0.35rem;
    flex-shrink: 0;
  }

  /* ── Sidebar Mobile Off-Canvas Drawer ── */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 82vw;
    transform: translateX(-100%);
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.6);
    z-index: 300;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay.active {
    display: block;
  }

  .sidebar-collapse-btn {
    display: none;
  }

  /* ── KPI Bento Grid: 2 Columns Compact ── */
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.55rem;
    margin-bottom: 0.75rem;
  }

  .kpi-card {
    padding: 0.75rem 0.8rem;
    border-radius: var(--radius-md);
  }

  .kpi-header {
    margin-bottom: 0.3rem;
  }

  .kpi-title {
    font-size: 0.68rem;
    line-height: 1.2;
  }

  .kpi-icon-wrapper {
    width: 24px;
    height: 24px;
  }

  .kpi-icon-wrapper i,
  .kpi-icon-wrapper svg {
    width: 13px !important;
    height: 13px !important;
  }

  .kpi-value {
    font-size: 1.1rem;
  }

  .kpi-unit {
    font-size: 0.65rem;
  }

  .kpi-footer {
    font-size: 0.62rem;
    padding-top: 0.3rem;
    gap: 0.15rem;
  }

  .kpi-footer strong {
    font-size: 0.65rem;
  }

  /* ── Stacked Single-Column Charts ── */
  .charts-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
    margin-bottom: 0.85rem;
  }

  .chart-card {
    padding: 0.85rem;
    border-radius: var(--radius-md);
  }

  .chart-title {
    font-size: 0.82rem;
  }

  .chart-subtitle {
    font-size: 0.68rem;
  }

  .chart-container {
    height: 200px;
  }

  /* ── Toolbar: Vertical Stack ── */
  .toolbar-section {
    flex-direction: column;
    align-items: stretch;
    gap: 0.55rem;
    margin-bottom: 0.75rem;
  }

  .search-box {
    max-width: 100%;
    min-width: 100%;
  }

  .search-box input {
    font-size: 0.8rem;
    padding: 0.5rem 0.85rem 0.5rem 2.2rem;
  }

  .filter-chips {
    width: 100%;
    padding-bottom: 4px;
    flex-wrap: nowrap;
  }

  .filter-chip {
    font-size: 0.72rem;
    padding: 0.35rem 0.65rem;
  }

  .toolbar-actions {
    width: 100%;
    display: flex;
    justify-content: stretch;
  }

  .view-tabs {
    width: 100%;
    display: flex;
  }

  .view-tab {
    flex: 1;
    justify-content: center;
    font-size: 0.72rem;
    padding: 0.35rem 0.5rem;
    gap: 0.3rem;
  }

  /* ── KANBAN: Horizontal Scroll on Mobile ── */
  .kanban-board {
    grid-template-columns: repeat(4, 72vw);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    padding-bottom: 0.5rem;
  }

  .kanban-board::-webkit-scrollbar {
    display: none;
  }

  .kanban-column {
    scroll-snap-align: start;
    min-width: 72vw;
    padding: 0.85rem;
  }

  .kanban-col-title {
    font-size: 0.75rem;
  }

  .kanban-card {
    padding: 0.75rem;
  }

  .kanban-card-title {
    font-size: 0.82rem;
    line-height: 1.25;
  }

  .kanban-card-meta {
    font-size: 0.7rem;
    flex-wrap: wrap;
    gap: 0.25rem;
  }

  /* ── Table: Horizontal Scroll ── */
  .table-container {
    border-radius: var(--radius-md);
  }

  .data-table {
    font-size: 0.78rem;
  }

  .data-table th {
    padding: 0.55rem 0.75rem;
    font-size: 0.68rem;
  }

  .data-table td {
    padding: 0.65rem 0.75rem;
  }

  /* ── Mobile Bottom Sheet Modal ── */
  .modal-overlay {
    align-items: flex-end;
    padding: 0;
  }

  .modal-content {
    max-height: 92vh;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    border-top-left-radius: var(--radius-xl);
    border-top-right-radius: var(--radius-xl);
  }

  .modal-header {
    padding: 0.85rem 1rem;
  }

  .modal-header h2 {
    font-size: 1.1rem;
  }

  .modal-body {
    padding: 1rem;
    gap: 1rem;
  }

  .modal-footer {
    padding: 0.75rem 1rem;
  }

  .detail-grid {
    grid-template-columns: 1fr;
    gap: 0.45rem;
  }

  /* ── Alert Banner ── */
  .alert-banner {
    padding: 0.65rem 0.85rem;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .alert-banner-content {
    font-size: 0.78rem;
  }

  .alert-banner .btn {
    width: 100%;
  }

  /* ── Toast ── */
  .toast-container {
    bottom: 0.75rem;
    right: 0.75rem;
    left: 0.75rem;
  }

  .toast {
    font-size: 0.78rem;
    padding: 0.6rem 0.85rem;
  }

  /* ── Sub/Customer Database Card Grids ── */
  #pageContainer .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.55rem;
  }
}

/* ==========================================================================
   2. SMALL TABLETS (640px to 767px)
   ========================================================================== */
@media (min-width: 640px) and (max-width: 767px) {
  html {
    font-size: 14px;
  }

  .app-container {
    padding: 0.75rem 1rem 3rem;
  }

  .mobile-nav-toggle {
    display: inline-flex;
  }

  .sidebar-collapse-btn {
    display: none;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 290px;
    transform: translateX(-100%);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.6);
    z-index: 300;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay.active {
    display: block;
  }

  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.7rem;
  }

  .charts-grid {
    grid-template-columns: 1fr;
    gap: 0.85rem;
  }

  .kanban-board {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
  }

  .toolbar-section {
    flex-wrap: wrap;
    gap: 0.65rem;
  }

  .search-box {
    flex: 1 1 280px;
    max-width: 100%;
  }
}

/* ==========================================================================
   3. IPAD & TABLET (768px to 1023px)
   ========================================================================== */
@media (min-width: 768px) and (max-width: 1023px) {
  html {
    font-size: 14.5px;
  }

  .app-container {
    padding: 1rem 1.25rem 3.5rem;
  }

  .mobile-nav-toggle {
    display: inline-flex;
  }

  .sidebar-collapse-btn {
    display: none;
  }

  /* Sidebar Drawer on iPad */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 300px;
    transform: translateX(-100%);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    z-index: 300;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay.active {
    display: block;
  }

  /* iPad KPI Grid: Balanced 3 Columns */
  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.85rem;
    margin-bottom: 1.15rem;
  }

  .kpi-card {
    padding: 1rem 1.15rem;
  }

  .kpi-value {
    font-size: 1.35rem;
  }

  /* iPad Charts: Stacked */
  .charts-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-bottom: 1.25rem;
  }

  .chart-container {
    height: 240px;
  }

  /* Kanban: 2 Columns */
  .kanban-board {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.85rem;
  }

  /* Toolbar */
  .toolbar-section {
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  .search-box {
    max-width: 100%;
    flex: 1 1 300px;
  }

  /* Modals */
  .detail-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==========================================================================
   4. DESKTOP & LAPTOPS (>= 1024px)
   ========================================================================== */
@media (min-width: 1024px) {
  .mobile-nav-toggle {
    display: none;
  }

  .sidebar-overlay {
    display: none !important;
  }

  .sidebar-collapse-btn {
    display: flex;
  }

  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .charts-grid {
    grid-template-columns: 1.6fr 1fr;
  }

  .kanban-board {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ==========================================================================
   5. LARGE WIDESCREEN (> 1440px)
   ========================================================================== */
@media (min-width: 1440px) {
  .app-container {
    padding: 1.5rem 2.5rem 4rem;
  }

  .kpi-grid {
    grid-template-columns: repeat(6, 1fr);
  }
}

/* ==========================================================================
   6. ULTRA-WIDE SCREEN (> 1920px)
   ========================================================================== */
@media (min-width: 1920px) {
  .app-container {
    max-width: 1600px;
  }
}

/* ==========================================================================
   7. LANDSCAPE PHONE SPECIFIC
   ========================================================================== */
@media (max-height: 500px) and (orientation: landscape) {
  .top-header {
    top: 0.2rem;
    padding: 0.4rem 0.75rem;
  }

  .kpi-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
  }

  .chart-container {
    height: 180px;
  }
}

/* ==========================================================================
   8. TOUCH & INTERACTION ENHANCEMENTS
   ========================================================================== */
@media (hover: none) and (pointer: coarse) {
  /* Larger touch targets for mobile */
  .btn {
    min-height: 42px;
  }

  .btn-sm {
    min-height: 36px;
    padding: 0.45rem 0.8rem;
  }

  .btn-icon {
    width: 40px;
    height: 40px;
  }

  .nav-item {
    padding: 0.7rem 0.85rem;
    min-height: 44px;
  }

  .filter-chip {
    min-height: 36px;
    padding: 0.4rem 0.75rem;
  }

  .view-tab {
    min-height: 38px;
  }

  /* Disable hover transforms on touch */
  .glass-card:hover {
    transform: none;
  }

  .kanban-card:hover {
    transform: none;
  }
}

/* ==========================================================================
   9. REDUCED MOTION
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   10. PRINT
   ========================================================================== */
@media print {
  .sidebar,
  .sidebar-overlay,
  .mobile-nav-toggle,
  .header-actions,
  .toast-container,
  .modal-overlay {
    display: none !important;
  }

  .app-shell {
    display: block;
  }

  .main-wrapper {
    margin: 0;
    padding: 0;
  }

  .app-container {
    padding: 0;
    max-width: 100%;
  }

  .glass-card {
    border: 1px solid #ddd;
    box-shadow: none;
    backdrop-filter: none;
  }

  body {
    background: white;
    color: black;
  }
}
