/**
 * Mobile Responsiveness Styles
 * Comprehensive mobile fixes for all pages
 */

/* Mobile Sidebar responsive adjustments */
@media (max-width: 1023px) {
  #mobile-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 300px;
    max-width: 85vw;
    height: 100vh;
    background: white;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
    z-index: 1050;
    overflow-y: auto;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

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

  #mobile-sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1049;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
  }

  #mobile-sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }

  /* Collapsible groups in mobile sidebar */
  #mobile-sidebar ul[id^="mobile-group-"] {
    transition:
      max-height 0.3s ease-out,
      opacity 0.2s ease-out;
    opacity: 1;
  }

  #mobile-sidebar ul[id^="mobile-group-"][style*="max-height: 0"] {
    opacity: 0;
  }

  /* Group button styling */
  #mobile-sidebar button[onclick*="toggleMobileGroup"] {
    text-align: left;
  }

  /* Hide desktop sidebar on mobile */
  #sidebar {
    display: none !important;
  }
}

/* Header Mobile Improvements */
@media (max-width: 640px) {
  header {
    padding-left: 0.75rem !important;
    padding-right: 0.75rem !important;
  }

  header .flex {
    gap: 0.5rem !important;
  }

  /* Stack logos vertically on very small screens */
  @media (max-width: 360px) {
    header .flex.items-center.gap-4 {
      flex-direction: column;
      align-items: flex-start;
    }
  }
}

/* Main Content Mobile Padding */
@media (max-width: 768px) {
  main {
    padding-left: 0.75rem !important;
    padding-right: 0.75rem !important;
  }

  .container {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }
}

/* Table Responsiveness */
@media (max-width: 768px) {
  /* Make tables horizontally scrollable */
  .table-responsive,
  table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Stack table cells on very small screens */
  @media (max-width: 480px) {
    table,
    thead,
    tbody,
    th,
    td,
    tr {
      display: block;
    }

    thead tr {
      position: absolute;
      top: -9999px;
      left: -9999px;
    }

    tr {
      border: 1px solid #e5e7eb;
      margin-bottom: 0.5rem;
      padding: 0.5rem;
      border-radius: 0.375rem;
    }

    td {
      border: none;
      position: relative;
      padding-left: 40% !important;
      padding-top: 0.5rem;
      padding-bottom: 0.5rem;
      white-space: normal;
    }

    td:before {
      content: attr(data-label) ": ";
      position: absolute;
      left: 0.5rem;
      width: 35%;
      font-weight: 600;
      color: #374151;
    }
  }
}

/* Card Layouts - Stack on Mobile */
@media (max-width: 768px) {
  .grid {
    grid-template-columns: 1fr !important;
  }

  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4 {
    grid-template-columns: 1fr !important;
  }

  /* Flex containers stack on mobile */
  .flex-row {
    flex-direction: column !important;
  }

  .flex-wrap {
    flex-wrap: wrap;
  }
}

/* Button Groups - Stack on Mobile */
@media (max-width: 640px) {
  .inline-flex.rounded-lg {
    flex-direction: column;
    width: 100%;
  }

  .inline-flex.rounded-lg label,
  .inline-flex.rounded-lg input[type="radio"] + label {
    border-right: none !important;
    border-bottom: 1px solid #e5e7eb;
    border-radius: 0 !important;
  }

  .inline-flex.rounded-lg label:first-child {
    border-top-left-radius: 0.5rem !important;
    border-top-right-radius: 0.5rem !important;
  }

  .inline-flex.rounded-lg label:last-child {
    border-bottom-left-radius: 0.5rem !important;
    border-bottom-right-radius: 0.5rem !important;
    border-bottom: none !important;
  }
}

/* Form Elements - Full Width on Mobile */
@media (max-width: 640px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  select,
  textarea {
    width: 100% !important;
  }

  .form-group {
    margin-bottom: 1rem;
  }
}

/* Charts - Responsive */
@media (max-width: 768px) {
  .chart-container,
  #chart-container,
  [id*="chart"] {
    width: 100% !important;
    height: auto !important;
    min-height: 300px;
  }
}

/* Debug Console - Mobile Positioning */
@media (max-width: 768px) {
  #db-debug-console {
    position: fixed !important;
    top: 10px !important;
    right: 10px !important;
    left: 10px !important;
    width: auto !important;
    max-width: none !important;
    max-height: 50vh !important;
    font-size: 11px !important;
  }

  #db-debug-console .db-debug-tabs {
    flex-wrap: wrap;
  }

  #db-debug-console .db-debug-tab {
    flex: 1 1 50%;
    min-width: 80px;
    font-size: 10px;
    padding: 6px 4px;
  }

  /* Floating toggle button on mobile */
  #db-debug-toggle {
    bottom: 1rem !important;
    right: 1rem !important;
    padding: 0.75rem !important;
  }
}

/* Dashboard Stats Cards - Stack on Mobile */
@media (max-width: 640px) {
  .stats-grid,
  [class*="grid-cols"] {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .stat-card {
    width: 100% !important;
  }
}

/* Navigation Tabs - Scrollable on Mobile */
@media (max-width: 640px) {
  .border-b.border-gray-200 nav,
  .tab-navigation {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .border-b.border-gray-200 nav::-webkit-scrollbar,
  .tab-navigation::-webkit-scrollbar {
    display: none;
  }

  .tab-button {
    white-space: nowrap;
    min-width: fit-content;
  }
}

/* Modal/Dialog - Full Screen on Mobile */
@media (max-width: 640px) {
  .modal,
  [role="dialog"],
  .dialog {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh !important;
    max-height: 100vh !important;
    margin: 0 !important;
    border-radius: 0 !important;
  }
}

/* Admin Tables - Better Mobile View */
@media (max-width: 768px) {
  .admin-table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .admin-table {
    min-width: 600px;
  }
}

/* Spacing Adjustments */
@media (max-width: 640px) {
  .mb-6 {
    margin-bottom: 1.5rem !important;
  }

  .mt-6 {
    margin-top: 1.5rem !important;
  }

  .py-6 {
    padding-top: 1.5rem !important;
    padding-bottom: 1.5rem !important;
  }

  .px-4 {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }
}

/* Text Sizing on Mobile */
@media (max-width: 640px) {
  h1 {
    font-size: 1.75rem !important;
  }

  h2 {
    font-size: 1.5rem !important;
  }

  h3 {
    font-size: 1.25rem !important;
  }

  .text-3xl {
    font-size: 1.5rem !important;
  }

  .text-2xl {
    font-size: 1.25rem !important;
  }
}

/* Filter Sections - Stack on Mobile */
@media (max-width: 640px) {
  .flex.flex-col.sm\:flex-row {
    flex-direction: column !important;
  }

  .flex.items-center.gap-2 {
    flex-wrap: wrap;
  }
}

/* Breadcrumbs - Mobile Responsive */
@media (max-width: 640px) {
  .breadcrumbs-container {
    padding: 0.5rem 1rem !important;
  }

  .breadcrumbs-container ol {
    flex-wrap: wrap;
    font-size: 0.75rem;
  }

  .breadcrumbs-container li {
    max-width: 100%;
  }

  .breadcrumbs-container a,
  .breadcrumbs-container span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
    display: inline-block;
  }
}
