/**
 * Mobile Optimizations - BOMAtlas
 * Mobile-first utilities and enhancements for touch devices
 */

/* ==========================================
   Mobile-First Variables
   ========================================== */
:root {
  --mobile-padding: 16px;
  --mobile-gap: 12px;
  --touch-target-min: 48px;
  --bottom-nav-height: 56px;
  --mobile-header-height: 56px;
  --mobile-radius: 12px;
  --mobile-transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ==========================================
   Touch-Friendly Reset
   ========================================== */
@media (hover: none) and (pointer: coarse) {
  * {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
  }

  button,
  a,
  .clickable {
    touch-action: manipulation;
    user-select: none;
  }

  input,
  textarea,
  select {
    font-size: 16px; /* Prevents zoom on iOS */
  }
}

/* ==========================================
   Mobile Utility Classes
   ========================================== */
@media (max-width: 768px) {
  /* Spacing utilities */
  .mobile-p-xs {
    padding: 8px !important;
  }
  .mobile-p-sm {
    padding: 12px !important;
  }
  .mobile-p-md {
    padding: 16px !important;
  }
  .mobile-p-lg {
    padding: 20px !important;
  }
  .mobile-p-xl {
    padding: 24px !important;
  }

  .mobile-m-0 {
    margin: 0 !important;
  }
  .mobile-mt-sm {
    margin-top: 8px !important;
  }
  .mobile-mt-md {
    margin-top: 16px !important;
  }
  .mobile-mb-sm {
    margin-bottom: 8px !important;
  }
  .mobile-mb-md {
    margin-bottom: 16px !important;
  }

  /* Display utilities */
  .mobile-hide {
    display: none !important;
  }
  .mobile-show {
    display: block !important;
  }
  .mobile-flex {
    display: flex !important;
  }
  .mobile-grid {
    display: grid !important;
  }

  /* Text utilities */
  .mobile-text-xs {
    font-size: 12px !important;
  }
  .mobile-text-sm {
    font-size: 14px !important;
  }
  .mobile-text-md {
    font-size: 16px !important;
  }
  .mobile-text-lg {
    font-size: 18px !important;
  }
  .mobile-text-xl {
    font-size: 20px !important;
  }

  /* Layout utilities */
  .mobile-full-width {
    width: 100% !important;
  }
  .mobile-stack {
    flex-direction: column !important;
    gap: var(--mobile-gap) !important;
  }
  .mobile-scroll-x {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
}

/* ==========================================
   Enhanced Touch Targets
   ========================================== */
@media (max-width: 768px) {
  /* Buttons */
  button,
  .btn,
  [role='button'] {
    min-height: var(--touch-target-min);
    min-width: var(--touch-target-min);
    padding: 12px 16px;
  }

  /* Links in navigation */
  nav a,
  .nav-link {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 10px 12px;
  }

  /* Form elements */
  input[type='text'],
  input[type='email'],
  input[type='tel'],
  input[type='number'],
  input[type='search'],
  select,
  textarea {
    min-height: var(--touch-target-min);
    padding: 12px 16px;
    font-size: 16px;
    border-radius: 8px;
  }

  /* Checkboxes and radios */
  input[type='checkbox'],
  input[type='radio'] {
    width: 24px;
    height: 24px;
    cursor: pointer;
  }

  label {
    display: flex;
    align-items: center;
    min-height: 44px;
    cursor: pointer;
  }
}

/* ==========================================
   Mobile Bottom Sheet Pattern
   ========================================== */
.mobile-bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-surface);
  border-top-left-radius: var(--mobile-radius);
  border-top-right-radius: var(--mobile-radius);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
  transform: translateY(100%);
  transition: transform var(--mobile-transition);
  z-index: 2000;
  max-height: 85vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.mobile-bottom-sheet.active {
  transform: translateY(0);
}

.mobile-bottom-sheet-header {
  position: sticky;
  top: 0;
  background: inherit;
  padding: 16px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1;
}

.mobile-bottom-sheet-handle {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 4px;
  background: var(--text-muted);
  border-radius: 2px;
  opacity: 0.3;
}

.mobile-bottom-sheet-content {
  padding: var(--mobile-padding);
  padding-bottom: calc(var(--mobile-padding) + var(--bottom-nav-height));
}

/* ==========================================
   Floating Action Button (FAB)
   ========================================== */
.mobile-fab {
  position: fixed;
  bottom: calc(var(--bottom-nav-height) + 16px);
  right: 16px;
  width: 56px;
  height: 56px;
  background: var(--gold-primary);
  color: var(--text-inverse);
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  cursor: pointer;
  z-index: 1500;
  transition: all var(--mobile-transition);
  border: none;
}

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

.mobile-fab.extended {
  width: auto;
  padding: 0 20px;
  border-radius: 28px;
}

.mobile-fab-label {
  display: none;
  margin-left: 8px;
  font-size: 14px;
  font-weight: 600;
}

.mobile-fab.extended .mobile-fab-label {
  display: inline;
}

/* View Switcher FAB - secondary FAB for map view cycling */
.view-switcher-fab {
  position: fixed;
  bottom: calc(var(--bottom-nav-height) + 80px);
  right: 16px;
  width: 44px;
  height: 44px;
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-default);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  z-index: 1499;
  transition: all var(--mobile-transition);
  opacity: 0;
  transform: scale(0.8);
  pointer-events: none;
}

.view-switcher-fab.visible {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.view-switcher-fab:active {
  transform: scale(0.95);
}

.view-switcher-fab.switching {
  transform: scale(1.1);
}

.view-switcher-fab .view-icon {
  line-height: 1;
}

/* Position view switcher above filter FAB */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .view-switcher-fab {
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 80px);
  }
}

/* ==========================================
   Mobile List Optimizations
   ========================================== */
@media (max-width: 768px) {
  .mobile-list {
    padding: 0;
    margin: 0;
  }

  .mobile-list-item {
    padding: 16px;
    border-bottom: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    min-height: 64px;
    cursor: pointer;
    transition: background var(--mobile-transition);
  }

  .mobile-list-item:active {
    background: var(--bg-elevated);
  }

  .mobile-list-item-content {
    flex: 1;
    min-width: 0;
  }

  .mobile-list-item-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-list-item-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-list-item-action {
    margin-left: 12px;
    color: var(--text-muted);
  }
}

/* ==========================================
   Swipeable Cards
   ========================================== */
.mobile-swipe-container {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  display: flex;
  gap: 12px;
  padding: 0 16px;
  margin: 0 -16px;
}

.mobile-swipe-card {
  flex: 0 0 280px;
  scroll-snap-align: start;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 16px;
}

/* Hide scrollbar but keep functionality */
.mobile-swipe-container::-webkit-scrollbar {
  display: none;
}

.mobile-swipe-container {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

/* ==========================================
   Mobile Loading States
   ========================================== */
.mobile-skeleton {
  background: linear-gradient(
    90deg,
    var(--bg-surface) 0%,
    var(--bg-elevated) 50%,
    var(--bg-surface) 100%
  );
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s infinite;
  border-radius: 4px;
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

.mobile-skeleton-text {
  height: 14px;
  margin-bottom: 8px;
}

.mobile-skeleton-title {
  height: 20px;
  width: 60%;
  margin-bottom: 12px;
}

/* ==========================================
   Pull to Refresh
   ========================================== */
.mobile-pull-refresh {
  position: absolute;
  top: -60px;
  left: 50%;
  transform: translateX(-50%);
  width: auto;
  height: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition:
    opacity 0.3s,
    transform 0.3s;
  opacity: 0;
}

.mobile-pull-refresh.visible {
  opacity: 1;
}

.mobile-pull-refresh .pull-text {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.mobile-pull-refresh.ready .pull-text {
  color: var(--gold-primary);
}

.mobile-pull-refresh-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border-default);
  border-top-color: var(--gold-primary);
  border-radius: 50%;
}

.mobile-pull-refresh.refreshing .mobile-pull-refresh-spinner {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ==========================================
   Touch Target Enforcement - WCAG 2.5.5
   Minimum 44x44px for all interactive elements
   ========================================== */
@media (max-width: 768px) {
  /* Quick filter chips - ensure minimum size */
  .quick-chip,
  .filter-chip {
    min-height: 44px;
    min-width: 44px;
    padding: 10px 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Bottom sheet handle - full-width touch target */
  .sheet-handle {
    min-height: 48px;
    padding: 12px 16px;
    cursor: grab;
    touch-action: pan-y;
  }

  .sheet-handle:active {
    cursor: grabbing;
  }

  /* Handle bar visual (small but touch target is full handle) */
  .sheet-handle-bar {
    width: 36px;
    height: 4px;
    background: var(--text-muted);
    border-radius: 2px;
    opacity: 0.4;
    margin: 0 auto 8px;
  }

  /* View mode buttons */
  .view-btn,
  .view-mode-btn,
  .view-mode-btn-icon {
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Filter section headers - expandable accordions */
  .filter-section-header {
    min-height: 48px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  /* School/entity cards - ensure tappable */
  .school-card,
  .entity-card,
  .list-item {
    min-height: 56px;
    padding: 12px 16px;
  }

  /* Icon-only buttons need explicit sizing */
  .btn-icon,
  .icon-button,
  [class*='-btn']:not(.btn-primary):not(.btn-secondary) {
    min-height: 44px;
    min-width: 44px;
  }

  /* Map controls */
  .leaflet-control-zoom a,
  .leaflet-control a {
    width: 44px !important;
    height: 44px !important;
    line-height: 44px !important;
    font-size: 18px !important;
  }

  /* Sidebar close button */
  .mobile-close-btn {
    min-height: 48px;
    padding: 14px 16px;
  }

  /* Context menu items */
  .mobile-context-menu button {
    min-height: 48px;
    padding: 12px 16px;
  }

  /* Stats badge close/dismiss */
  .stats-badge-close,
  .dismiss-btn {
    min-height: 44px;
    min-width: 44px;
  }

  /* Modal close buttons */
  .modal-close,
  [class*='close-btn'] {
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* ==========================================
   Mobile Modal Adjustments
   ========================================== */
@media (max-width: 768px) {
  .modal {
    padding: 16px;
  }

  .modal-content {
    width: 100%;
    max-width: none;
    margin: 0;
    border-radius: var(--mobile-radius);
    max-height: calc(100vh - 32px);
    overflow-y: auto;
  }

  .modal-header {
    padding: 16px;
    position: sticky;
    top: 0;
    background: inherit;
    z-index: 1;
  }

  .modal-body {
    padding: 16px;
  }

  .modal-footer {
    padding: 16px;
    position: sticky;
    bottom: 0;
    background: inherit;
    border-top: 1px solid var(--border-subtle);
  }
}

/* ==========================================
   Responsive Tables
   ========================================== */
@media (max-width: 768px) {
  .mobile-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .mobile-table table {
    min-width: 100%;
    border-collapse: collapse;
  }

  .mobile-table th,
  .mobile-table td {
    padding: 12px;
    white-space: nowrap;
  }

  /* Card-style table for mobile */
  .mobile-table-cards {
    display: block;
  }

  .mobile-table-cards thead {
    display: none;
  }

  .mobile-table-cards tbody {
    display: block;
  }

  .mobile-table-cards tr {
    display: block;
    margin-bottom: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: 8px;
    padding: 12px;
  }

  .mobile-table-cards td {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border: none;
  }

  .mobile-table-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
}

/* ==========================================
   iOS Safe Areas
   ========================================== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .mobile-bottom-sheet-content,
  .bottom-nav,
  .mobile-fab {
    padding-bottom: calc(var(--mobile-padding) + env(safe-area-inset-bottom));
  }

  .mobile-fab {
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + 16px);
  }
}

/* ==========================================
   Performance Optimizations
   ========================================== */
@media (max-width: 768px) {
  /* Use GPU acceleration for animations */
  .mobile-animated {
    will-change: transform;
    transform: translateZ(0);
    backface-visibility: hidden;
  }

  /* Reduce motion for users who prefer it */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }
}

/* ==========================================
   Dark Mode Adjustments
   ========================================== */
@media (prefers-color-scheme: dark) {
  .mobile-bottom-sheet,
  .modal-content {
    background: var(--bg-surface);
  }

  .mobile-fab {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  }
}
