/* ─── Responsive: ≤ 768px (Mobile) ─── */
@media (max-width: 768px) {
  :root {
    --font-size-base: 13px;
    --spacing-lg: 20px;
    --spacing-xl: 24px;
  }

  /* Sidebar off-screen by default on mobile, above Leaflet map and the GPS
     bottom sheet. Stacking order (bottom → top):
       Leaflet controls        = 1000
       GPS bottom sheet        = 1040
       Sidebar scrim overlay   = 1080   (covers both)
       Sidebar drawer itself   = 1100   (above its scrim) */
  .sidebar {
    width: var(--sidebar-width);
    transform: translateX(calc(-1 * var(--sidebar-width)));
    transition: transform var(--transition-base), width var(--transition-base);
    z-index: 1100;
  }

  .sidebar-overlay {
    z-index: 1080;
  }

  .sidebar .sidebar-brand,
  .sidebar .nav-item-label,
  .sidebar .nav-section-label,
  .sidebar .collapse-label {
    opacity: 1;
    width: auto;
    overflow: visible;
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  /* Hide sidebar collapse button on mobile */
  .sidebar-footer {
    display: none;
  }

  /* Main area full width on mobile */
  .main-area {
    margin-left: 0;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .topbar {
    left: 0;
    padding: 0 var(--spacing-sm);
    height: 48px;
    z-index: 1010;
  }

  .topbar-hamburger {
    display: flex;
  }

  /* Compact main content */
  .main-content {
    margin-top: 48px;
    padding: var(--spacing-sm);
  }

  /* Page header stacks vertically */
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-sm);
  }

  .page-title {
    font-size: var(--font-size-lg);
  }

  .page-actions {
    width: 100%;
    flex-direction: column;
  }

  /* Make page action buttons fill on mobile */
  .page-actions .btn {
    width: 100%;
    justify-content: center;
  }

  /* KPI overview: compact padding */
  .kpi-overview-body {
    padding: var(--spacing-sm) !important;
  }

  /* Stats grid: 2 columns on mobile */
  .stats-grid,
  .stat-card-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-sm);
  }

  /* Stat cards: compact horizontal layout with icon left */
  .stat-card {
    display: grid;
    grid-template-columns: 32px 1fr;
    grid-template-rows: auto auto;
    gap: 0 var(--spacing-sm);
    padding: var(--spacing-sm) var(--spacing-md);
    border-radius: var(--radius-md);
  }

  .stat-card-icon {
    grid-row: 1 / 3;
    width: 32px;
    height: 32px;
    margin-bottom: 0;
    align-self: center;
  }

  .stat-card-icon .material-icons {
    font-size: 16px;
  }

  .stat-card-value {
    font-size: var(--font-size-base);
    grid-column: 2;
    align-self: end;
  }

  .stat-card-label {
    font-size: 10px;
    grid-column: 2;
    align-self: start;
    line-height: 1.2;
  }

  /* Hide trend badge on mobile */
  .stat-card-change {
    display: none;
  }

  /* Quick actions: 3-column icon grid */
  .quick-actions-grid {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: var(--spacing-sm) !important;
    padding: var(--spacing-sm) !important;
  }

  .quick-action-item {
    flex-direction: column;
    text-align: center;
    padding: var(--spacing-sm);
    gap: 4px;
    justify-content: center;
    border-color: var(--color-border-light);
  }

  .quick-action-icon {
    font-size: 22px;
  }

  .quick-action-label {
    font-size: 10px;
    line-height: 1.2;
    word-break: break-word;
  }

  /* Activity section: reduce max-height */
  #activity-section .card-body {
    max-height: 240px !important;
  }

  /* Content grids: single column */
  .content-grid,
  .content-grid-2,
  .content-grid-3 {
    grid-template-columns: 1fr;
  }

  /* Reset grid-column spans on single-column layout */
  .content-grid-2 > [style*="grid-column"] {
    grid-column: auto !important;
  }

  /* Tables */
  .table-toolbar {
    flex-direction: column !important;
    align-items: stretch !important;
  }

  .table-toolbar-left {
    width: 100%;
  }

  .table-toolbar-right {
    width: 100%;
    justify-content: flex-start;
  }

  .search-input {
    width: 100%;
  }

  .search-input:focus {
    width: 100%;
  }

  /* Table actions always visible on touch devices */
  .table-actions {
    opacity: 1;
  }

  .table-pagination {
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-sm);
  }

  .pagination-info {
    text-align: center;
    width: 100%;
  }

  .pagination-controls {
    flex-wrap: wrap;
    justify-content: center;
  }

  /* Tables: reduce padding */
  .data-table tbody td,
  .data-table thead th {
    padding: 8px var(--spacing-sm);
    font-size: var(--font-size-xs);
  }

  /* Pagination: compact */
  .pagination-btn {
    width: 28px;
    height: 28px;
    font-size: var(--font-size-xs);
  }

  .pagination-size {
    font-size: var(--font-size-xs);
  }

  /* Topbar right: hide labels */
  .user-menu-name,
  .user-menu-role {
    display: none;
  }

  /* Breadcrumb: hide on mobile */
  .breadcrumb {
    display: none;
  }

  /* Charts */
  .chart-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .chart-header-right {
    width: 100%;
    overflow-x: auto;
  }

  .chart-stats-row {
    flex-direction: column;
  }

  .chart-stat {
    border-right: none;
    border-bottom: 1px solid var(--color-border-light);
  }

  .chart-stat:last-child {
    border-bottom: none;
  }

  .chart-stat-value {
    font-size: var(--font-size-lg);
  }

  .chart-body {
    padding: var(--spacing-md);
    min-height: 200px;
  }

  /* Card padding reduction */
  .card-body {
    padding: var(--spacing-md);
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-xs);
  }

  /* Date range picker inside a filter-bar: stack the two date inputs */
  .date-range-picker {
    flex-direction: column;
    width: 100%;
  }

  .date-range-inputs {
    width: 100%;
    flex-wrap: wrap;
  }

  .date-range-inputs input[type="date"] {
    flex: 1;
    min-width: 0;
  }

  /* Preset chip row: horizontal scroll when too many for the viewport */
  .filter-presets {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }
  .filter-presets::-webkit-scrollbar {
    display: none;
  }

  /* Modal adjustments: bottom sheet */
  .modal-backdrop {
    padding: 0;
    align-items: flex-end;
  }

  .modal {
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    max-height: 92vh;
    max-width: 100vw;
    animation: modalSlideUp var(--transition-base) ease;
  }

  @keyframes modalSlideUp {
    from {
      opacity: 0;
      transform: translateY(100%);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .modal-sm,
  .modal-lg,
  .modal-xl {
    max-width: 100vw;
  }

  .modal-body {
    padding: var(--spacing-md);
  }

  .modal-header {
    padding: var(--spacing-md);
  }

  .modal-footer {
    padding: var(--spacing-sm) var(--spacing-md);
    flex-wrap: wrap;
  }

  .modal-footer .btn {
    flex: 1;
    min-width: 0;
    justify-content: center;
  }

  /* Form grids: single column */
  .form-grid-2,
  .form-grid-3,
  .form-grid-4 {
    grid-template-columns: 1fr;
  }

  /* Form groups in inline layouts */
  .form-group {
    min-width: 0 !important;
    width: 100%;
  }

  /* Forms: prevent iOS zoom on focus */
  .form-input,
  .form-select,
  .form-textarea {
    font-size: 16px;
  }

  /* Alert cards */
  .alert-card .card-body > div {
    flex-direction: column;
    gap: var(--spacing-sm);
  }

  /* Legend: wrap */
  .chart-legend {
    gap: var(--spacing-sm);
  }

  /* Dropdown on mobile: full width */
  .dropdown-menu {
    position: fixed;
    left: var(--spacing-md);
    right: var(--spacing-md);
    top: auto;
    bottom: var(--spacing-md);
    min-width: auto;
    max-height: 60vh;
    overflow-y: auto;
    border-radius: var(--radius-xl);
    z-index: 301;
  }

  /* Tabs: scrollable */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-shrink: 0;
  }

  .tabs::-webkit-scrollbar {
    display: none;
  }

  /* Touch-friendly sizes: minimum 44px tap targets */
  .btn {
    min-height: 40px;
    padding: 10px var(--spacing-md);
  }

  .btn-sm {
    min-height: 36px;
    padding: 8px 12px;
  }

  .btn-icon {
    min-height: 40px;
    min-width: 40px;
  }

  .btn-icon.btn-sm {
    min-height: 36px;
    min-width: 36px;
  }

  .nav-item {
    padding: 12px var(--spacing-md);
  }

  .tab-item {
    padding: var(--spacing-sm) var(--spacing-md);
    min-height: 40px;
    display: flex;
    align-items: center;
  }

  /* Filter chips */
  .filter-chips {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }

  .filter-chip {
    flex-shrink: 0;
  }

  /* Bulk actions bar */
  .bulk-actions-bar {
    flex-wrap: wrap;
    padding: var(--spacing-sm) var(--spacing-md);
    font-size: var(--font-size-xs);
  }

  /* Empty state compact */
  .empty-state {
    padding: var(--spacing-lg);
  }

  .empty-state .material-icons {
    font-size: 40px;
  }

  /* Login page */
  .login-page {
    padding: var(--spacing-md);
  }

  .login-card {
    padding: var(--spacing-lg);
    border-radius: var(--radius-lg);
  }

  /* Badge */
  .badge {
    font-size: 10px;
    padding: 2px 6px;
  }

  .status-badge {
    font-size: 10px;
    padding: 3px 8px;
  }
}

/* ─── Touch Device Optimizations ─── */
@media (hover: none) and (pointer: coarse) {
  /* Always show table row actions on touch devices */
  .table-actions {
    opacity: 1;
  }

  /* Larger touch targets */
  .dropdown-item {
    padding: 12px var(--spacing-md);
  }

  .nav-item {
    padding: 12px var(--spacing-md);
  }

  /* Remove hover-dependent effects */
  .data-table tbody tr:hover {
    background-color: transparent;
  }

  .data-table tbody tr:nth-child(even) {
    background-color: rgba(245, 246, 250, 0.5);
  }

  /* Scrollable table hint shadow */
  .table-wrapper {
    position: relative;
  }

  .table-wrapper::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 20px;
    background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.04));
    pointer-events: none;
    opacity: 1;
    transition: opacity var(--transition-fast);
  }
}

/* ─── Landscape Phone ─── */
@media (max-height: 500px) and (orientation: landscape) {
  .modal-backdrop {
    padding: var(--spacing-xs);
    align-items: center;
  }

  .modal {
    max-height: 95vh;
    border-radius: var(--radius-lg);
  }

  .sidebar {
    overflow-y: auto;
  }
}

/* ─── Print ─── */
@media print {
  .sidebar,
  .topbar,
  .table-toolbar,
  .table-pagination,
  .page-actions,
  .sidebar-overlay {
    display: none !important;
  }

  .main-area {
    margin-left: 0;
  }

  .main-content {
    margin-top: 0;
    padding: 0;
  }

  .data-table {
    border: none;
  }

  .data-table thead th {
    background-color: #f0f0f0;
    color: #000;
  }
}
