/* ========== LARGE TABLETS (1200px and below) ========== */
@media (max-width: 1200px) {
  .page-header .d-flex {
    flex-direction: column;
    align-items: flex-start !important;
  }

  .action-buttons {
    margin-top: 1rem;
    width: 100%;
  }

  .btn-custom {
    width: 100%;
    margin-bottom: 0.5rem;
    margin-right: 0;
  }

  .card-stat h3 {
    font-size: 1.75rem;
  }

  /* Accordion headers */
  .accordion-button {
    font-size: 0.9rem;
    padding: 0.75rem 1rem;
  }

  .accordion-button small {
    font-size: 0.75rem;
  }
}

/* ========== TABLETS (992px and below) ========== */
@media (max-width: 992px) {
  /* Stat cards - 2 columns */
  .row.g-4 > [class*='col-md-3'],
  .row.g-3 > [class*='col-md-3'] {
    flex: 0 0 50%;
    max-width: 50%;
  }

  .row.g-4 > [class*='col-md-4'],
  .row.g-3 > [class*='col-md-4'] {
    flex: 0 0 50%;
    max-width: 50%;
  }

  /* Tables */
  .table thead th {
    font-size: 0.75rem;
    padding: 0.75rem 0.5rem;
  }

  .table tbody td {
    padding: 0.5rem 0.375rem;
    font-size: 0.85rem;
  }

  .table-sm thead th,
  .table-sm tbody td {
    padding: 0.375rem 0.25rem;
    font-size: 0.75rem;
  }

  /* Buttons */
  .btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
  }

  .btn-group-sm > .btn {
    padding: 0.25rem 0.4rem;
    font-size: 0.7rem;
  }

  /* Badges */
  .badge {
    font-size: 0.7rem;
    padding: 0.25rem 0.5rem;
  }

  /* Charts */
  .chart-container {
    padding: 1rem;
  }

  /* Dashboard cards */
  .dashboard-card {
    padding: 1rem;
    margin-bottom: 1rem;
  }

  /* Filter forms */
  .card-body form .row > div {
    margin-bottom: 0.5rem;
  }

  /* Modals */
  .modal-dialog {
    margin: 1rem;
  }
}

/* ========== MOBILE (768px and below) ========== */
@media (max-width: 768px) {
  /* All stat cards full width */
  .row.g-4 > [class*='col-md-'],
  .row.g-3 > [class*='col-md-'],
  .row.g-2 > [class*='col-md-'] {
    flex: 0 0 100%;
    max-width: 100%;
    margin-bottom: 0.75rem;
  }

  /* Keep 2-column for col-6 */
  .row > .col-6 {
    flex: 0 0 50%;
    max-width: 50%;
  }

  /* Sidebar - slide from left */
  .sidebar {
    position: fixed;
    top: 56px;
    left: -100%;
    width: 250px;
    height: calc(100vh - 56px);
    z-index: 1000;
    transition: left 0.3s ease;
    overflow-y: auto;
    box-shadow: none;
  }

  .sidebar.show {
    left: 0;
    box-shadow: 2px 0 15px rgba(0,0,0,0.2);
  }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    top: 56px;
    left: 0;
    width: 100%;
    height: calc(100vh - 56px);
    background: rgba(0,0,0,0.5);
    z-index: 999;
  }

  .sidebar-backdrop.show {
    display: block;
  }

  /* Content area */
  .main-content {
    padding: 12px !important;
    margin-left: 0 !important;
    width: 100% !important;
  }

  /* Typography */
  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.25rem;
  }

  h4, h5 {
    font-size: 1.1rem;
  }

  h6 {
    font-size: 0.95rem;
  }

  /* Navbar */
  .navbar-brand {
    font-size: 0.9rem;
  }

  .navbar-brand i {
    font-size: 1rem;
  }

  .user-info {
    font-size: 0.75rem;
  }

  .role-badge {
    display: none;
  }

  /* Cards */
  .card {
    margin-bottom: 1rem;
  }

  .card-header h5 {
    font-size: 1rem;
  }

  .card-body {
    padding: 1rem;
  }

  .card-stat .card-body {
    padding: 0.75rem;
  }

  .stat-icon {
    font-size: 1.5rem;
  }

  /* Buttons */
  .btn {
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
  }

  .btn-group {
    flex-direction: column;
    width: 100%;
  }

  .btn-group > .btn {
    width: 100%;
    margin-bottom: 0.25rem;
    border-radius: 0.25rem !important;
  }

  /* Tables - horizontal scroll */
  .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .table {
    min-width: 600px;
    font-size: 0.75rem;
  }

  .table thead th {
    white-space: nowrap;
    padding: 0.5rem 0.25rem;
    font-size: 0.7rem;
  }

  .table tbody td {
    padding: 0.4rem 0.25rem;
    font-size: 0.75rem;
  }

  /* Sticky first column */
  .table thead th:first-child,
  .table tbody td:first-child {
    position: sticky;
    left: 0;
    background: white;
    z-index: 10;
  }

  .table thead th:first-child {
    z-index: 11;
  }

  /* Accordion */
  .accordion-button {
    font-size: 0.85rem;
    padding: 0.75rem;
  }

  .accordion-button .d-flex {
    flex-direction: column;
    align-items: flex-start !important;
  }

  .accordion-button small {
    font-size: 0.7rem;
    margin-top: 0.25rem;
  }

  .accordion-body {
    padding: 0.75rem;
  }

  /* Forms */
  .form-control,
  .form-select {
    font-size: 0.875rem;
    padding: 0.5rem 0.75rem;
  }

  .form-label {
    font-size: 0.875rem;
    margin-bottom: 0.25rem;
  }

  .form-control-sm,
  .form-select-sm {
    font-size: 0.75rem;
    padding: 0.375rem 0.5rem;
  }

  /* Input groups */
  .input-group-text {
    font-size: 0.875rem;
  }

  /* Alerts */
  .alert {
    font-size: 0.875rem;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
  }

  /* Modals - full screen */
  .modal-dialog {
    margin: 0;
    max-width: 100%;
    min-height: 100vh;
  }

  .modal-content {
    min-height: 100vh;
    border-radius: 0;
    border: none;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 1rem;
  }

  /* Pagination */
  .pagination {
    font-size: 0.8rem;
  }

  .page-link {
    padding: 0.375rem 0.625rem;
  }

  /* Hide elements on mobile */
  .d-none.d-md-table-cell,
  .d-none.d-lg-table-cell,
  .d-none.d-xl-table-cell {
    display: none !important;
  }

  /* Breadcrumbs */
  .breadcrumb {
    font-size: 0.75rem;
    display: none;
  }

  /* Dispatch item cards */
  .card.mb-2 .card-body {
    padding: 0.5rem;
  }

  /* Filters - stack vertically */
  .card-body form .row > div {
    margin-bottom: 0.75rem;
  }

  /* Action buttons in table */
  .action-cell .btn-sm {
    display: block;
    width: 100%;
    margin-bottom: 0.25rem;
    margin-right: 0 !important;
  }
}

/* ========== SMALL MOBILE (576px and below) ========== */
@media (max-width: 576px) {
  /* Typography */
  h2 {
    font-size: 1.25rem;
  }

  h3 {
    font-size: 1.1rem;
  }

  .card-stat h3 {
    font-size: 1.5rem;
  }

  /* Navbar */
  .navbar-brand {
    font-size: 0.8rem;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Content */
  .main-content {
    padding: 8px !important;
  }

  /* Cards */
  .card-body {
    padding: 0.75rem;
  }

  .card-stat .card-body {
    padding: 0.5rem;
  }

  .stat-icon {
    font-size: 1.25rem;
  }

  /* Tables */
  .table {
    min-width: 500px;
    font-size: 0.7rem;
  }

  .table thead th {
    padding: 0.375rem 0.2rem;
    font-size: 0.65rem;
  }

  .table tbody td {
    padding: 0.3rem 0.2rem;
  }

  /* Buttons */
  .btn-sm {
    font-size: 0.7rem;
    padding: 0.2rem 0.4rem;
  }

  /* Forms */
  .form-control,
  .form-select {
    font-size: 0.8rem;
    padding: 0.4rem 0.6rem;
  }

  /* Accordion */
  .accordion-button {
    font-size: 0.8rem;
    padding: 0.6rem;
  }

  /* Badges */
  .badge {
    font-size: 0.65rem;
    padding: 0.2rem 0.4rem;
  }

  /* Filters - more compact */
  .filter-section .row > div {
    margin-bottom: 0.5rem;
  }

  /* Modal content */
  .modal-body,
  .modal-header,
  .modal-footer {
    padding: 0.75rem;
  }

  /* Toast notifications */
  .alert.position-fixed {
    min-width: 280px !important;
    font-size: 0.8rem;
  }
}

/* ========== LANDSCAPE MOBILE ========== */
@media (max-width: 768px) and (orientation: landscape) {
  .page-header {
    padding: 0.75rem;
  }

  .page-header h1 {
    font-size: 1.1rem;
  }

  .filter-section {
    padding: 0.5rem;
  }

  .sidebar {
    width: 200px;
  }
}

/* ========== TOUCH-FRIENDLY IMPROVEMENTS ========== */
@media (hover: none) and (pointer: coarse) {
  /* Increase touch targets to 44px minimum */
  .btn,
  .form-control,
  .form-select,
  .nav-link,
  a.btn {
    min-height: 44px;
    min-width: 44px;
  }

  .btn-sm {
    min-height: 36px;
  }

  /* Make table rows tappable */
  .table tbody tr {
    cursor: pointer;
  }

  /* Larger checkboxes and radios */
  .form-check-input {
    width: 1.25rem;
    height: 1.25rem;
  }

  /* Easier to tap pagination */
  .page-link {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* ========== MOBILE MENU TOGGLE ========== */
.mobile-menu-toggle {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #163024 0%, #2d5a42 100%);
  color: white;
  border: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  z-index: 1001;
  font-size: 1.5rem;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease;
}

.mobile-menu-toggle:active {
  transform: scale(0.95);
}

@media (max-width: 991px) {
  .mobile-menu-toggle {
    display: flex;
  }
}

/* ========== PRINT STYLES ========== */
@media print {
  .sidebar,
  .navbar,
  .mobile-menu-toggle,
  .sidebar-backdrop,
  .btn,
  .alert {
    display: none !important;
  }

  .main-content {
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .table {
    font-size: 10pt;
  }

  .page-break {
    page-break-after: always;
  }
}

/* ========== UTILITY CLASSES ========== */
@media (max-width: 768px) {
  .mobile-stack {
    flex-direction: column !important;
  }

  .mobile-full-width {
    width: 100% !important;
  }

  .mobile-text-center {
    text-align: center !important;
  }

  .mobile-hide {
    display: none !important;
  }

  .mobile-mb-2 {
    margin-bottom: 0.5rem !important;
  }
}