/**
 * Mobile Utilities CSS Library
 * Reusable utility classes for mobile-optimized components
 * Material Design 3 compliant with iOS/Android best practices
 */

/* ========================================
   CSS Custom Properties (Design Tokens)
   ======================================== */

:root {
  /* Touch Target Sizes (WCAG 2.5.5, Material Design 3) */
  --touch-target-min: 48px;
  --touch-target-comfortable: 56px;
  --touch-target-large: 64px;

  /* Safe Area Insets (iOS notch, Dynamic Island, Android gesture bars) */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  /* Spacing Scale (8pt grid system) */
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;
  --spacing-2xl: 48px;

  /* Border Radius (Material Design 3) */
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  /* Animation Timings (Material Design Motion) */
  --duration-instant: 100ms;
  --duration-fast: 200ms;
  --duration-normal: 300ms;
  --duration-slow: 500ms;

  --easing-standard: cubic-bezier(0.4, 0, 0.2, 1);
  --easing-decelerate: cubic-bezier(0, 0, 0.2, 1);
  --easing-accelerate: cubic-bezier(0.4, 0, 1, 1);

  /* Z-index Scale */
  --z-base: 0;
  --z-dropdown: 1000;
  --z-sticky: 1020;
  --z-fixed: 1030;
  --z-modal-backdrop: 1040;
  --z-modal: 1050;
  --z-popover: 1060;
  --z-tooltip: 1070;

  /* Typography Scale */
  --font-size-xs: 12px;
  --font-size-sm: 14px;
  --font-size-base: 16px; /* Prevents iOS zoom on focus */
  --font-size-lg: 18px;
  --font-size-xl: 20px;
  --font-size-2xl: 24px;

  /* Mobile Colors (Light Mode) */
  --mobile-surface: #ffffff;
  --mobile-surface-variant: #f5f5f5;
  --mobile-border: #e0e0e0;
  --mobile-text-primary: #212121;
  --mobile-text-secondary: #757575;
  --mobile-accent: #1976d2;
  --mobile-error: #d32f2f;
  --mobile-success: #388e3c;
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
  :root {
    --mobile-surface: #121212;
    --mobile-surface-variant: #1e1e1e;
    --mobile-border: #2c2c2c;
    --mobile-text-primary: #ffffff;
    --mobile-text-secondary: #b0b0b0;
    --mobile-accent: #64b5f6;
    --mobile-error: #ef5350;
    --mobile-success: #66bb6a;
  }
}

/* ========================================
   Touch Target Utilities
   ======================================== */

.touch-target {
  min-height: var(--touch-target-min);
  min-width: var(--touch-target-min);
}

.touch-target-comfortable {
  min-height: var(--touch-target-comfortable);
  min-width: var(--touch-target-comfortable);
}

.touch-target-large {
  min-height: var(--touch-target-large);
  min-width: var(--touch-target-large);
}

/* Tap highlight removal (use sparingly) */
.no-tap-highlight {
  -webkit-tap-highlight-color: transparent;
}

/* ========================================
   Checkbox Touch Targets (WCAG 2.5.5)
   ======================================== */

/**
 * Mobile-optimized checkbox with 48×48px touch target
 * Meets WCAG 2.5.5 Target Size (Level AAA) requirements
 */
.mobile-checkbox {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  min-height: 48px;
  cursor: pointer;
  position: relative;
}

.mobile-checkbox input[type="checkbox"] {
  width: 24px;
  height: 24px;
  margin: 12px; /* Centers 24px checkbox in 48px target */
  cursor: pointer;
}

.mobile-checkbox label {
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

/* Increase touch target for radio buttons as well */
.mobile-radio {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  min-height: 48px;
  cursor: pointer;
  position: relative;
}

.mobile-radio input[type="radio"] {
  width: 24px;
  height: 24px;
  margin: 12px;
  cursor: pointer;
}

.mobile-radio label {
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

/* ========================================
   Safe Area Utilities
   ======================================== */

.safe-top {
  padding-top: var(--safe-top);
}

.safe-bottom {
  padding-bottom: var(--safe-bottom);
}

.safe-left {
  padding-left: var(--safe-left);
}

.safe-right {
  padding-right: var(--safe-right);
}

.safe-x {
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.safe-y {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.safe-all {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

/* ========================================
   Scrolling Utilities
   ======================================== */

.scroll-smooth {
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.scroll-snap-y {
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
}

.scroll-snap-x {
  scroll-snap-type: x mandatory;
  overflow-x: scroll;
}

.scroll-snap-start {
  scroll-snap-align: start;
}

.scroll-snap-center {
  scroll-snap-align: center;
}

.overscroll-contain {
  overscroll-behavior: contain;
}

.overscroll-none {
  overscroll-behavior: none;
}

/* Hide scrollbar but keep functionality */
.scrollbar-hidden {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.scrollbar-hidden::-webkit-scrollbar {
  display: none;
}

/* ========================================
   Animation Utilities
   ======================================== */

.animate-fade-in {
  animation: fadeIn var(--duration-fast) var(--easing-standard);
}

.animate-slide-up {
  animation: slideUp var(--duration-normal) var(--easing-decelerate);
}

.animate-slide-down {
  animation: slideDown var(--duration-normal) var(--easing-accelerate);
}

.animate-scale-in {
  animation: scaleIn var(--duration-fast) var(--easing-standard);
}

/* GPU Acceleration (use for animations) */
.gpu-accelerated {
  transform: translateZ(0);
  will-change: transform;
}

/* ========================================
   Keyframe Animations
   ======================================== */

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes slideDown {
  from {
    transform: translateY(0);
    opacity: 1;
  }
  to {
    transform: translateY(100%);
    opacity: 0;
  }
}

@keyframes scaleIn {
  from {
    transform: scale(0.9);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes shimmer {
  0% {
    background-position: -1000px 0;
  }
  100% {
    background-position: 1000px 0;
  }
}

/* ========================================
   Loading States
   ======================================== */

.loading-skeleton {
  background: linear-gradient(
    90deg,
    var(--mobile-surface-variant) 0%,
    var(--mobile-border) 50%,
    var(--mobile-surface-variant) 100%
  );
  background-size: 1000px 100%;
  animation: shimmer 2s infinite linear;
}

.loading-spinner {
  border: 3px solid var(--mobile-border);
  border-top-color: var(--mobile-accent);
  border-radius: var(--radius-full);
  width: 40px;
  height: 40px;
  animation: spin 0.8s linear infinite;
}

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

/* ========================================
   Typography Utilities
   ======================================== */

.mobile-text-xs {
  font-size: var(--font-size-xs);
}
.mobile-text-sm {
  font-size: var(--font-size-sm);
}
.mobile-text-base {
  font-size: var(--font-size-base);
}
.mobile-text-lg {
  font-size: var(--font-size-lg);
}
.mobile-text-xl {
  font-size: var(--font-size-xl);
}
.mobile-text-2xl {
  font-size: var(--font-size-2xl);
}

/* Prevent iOS zoom on input focus */
.mobile-input {
  font-size: var(--font-size-base) !important;
}

/* ========================================
   Spacing Utilities
   ======================================== */

.mobile-p-xs {
  padding: var(--spacing-xs);
}
.mobile-p-sm {
  padding: var(--spacing-sm);
}
.mobile-p-md {
  padding: var(--spacing-md);
}
.mobile-p-lg {
  padding: var(--spacing-lg);
}
.mobile-p-xl {
  padding: var(--spacing-xl);
}

.mobile-m-xs {
  margin: var(--spacing-xs);
}
.mobile-m-sm {
  margin: var(--spacing-sm);
}
.mobile-m-md {
  margin: var(--spacing-md);
}
.mobile-m-lg {
  margin: var(--spacing-lg);
}
.mobile-m-xl {
  margin: var(--spacing-xl);
}

/* ========================================
   Card Utilities
   ======================================== */

.mobile-card {
  background: var(--mobile-surface);
  border: 1px solid var(--mobile-border);
  border-radius: var(--radius-md);
  padding: var(--spacing-md);
}

.mobile-card-elevated {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* ========================================
   Button Utilities
   ======================================== */

.mobile-btn {
  min-height: var(--touch-target-min);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-base);
  font-weight: 500;
  transition: transform var(--duration-fast) var(--easing-standard);
}

.mobile-btn:active {
  transform: scale(0.98);
}

/* ========================================
   Gesture Utilities
   ======================================== */

.swipeable {
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

.draggable {
  touch-action: none;
  cursor: grab;
}

.draggable:active {
  cursor: grabbing;
}

/* ========================================
   Accessibility Utilities
   ======================================== */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.focus-visible-outline:focus-visible {
  outline: 2px solid var(--mobile-accent);
  outline-offset: 2px;
}

/* ========================================
   Form Validation (WCAG 4.1.3)
   ======================================== */

/**
 * Validation summary styling for screen reader announcements
 */
.validation-summary {
  margin-top: var(--spacing-md);
  margin-bottom: var(--spacing-md);
}

.validation-summary ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.validation-summary li {
  color: var(--mobile-error);
  padding: var(--spacing-xs) 0;
  font-size: var(--font-size-sm);
}

/* ========================================
   Responsive Utilities
   ======================================== */

/* Mobile-only (≤768px) */
@media (max-width: 768px) {
  .mobile-only {
    display: block;
  }

  .desktop-only {
    display: none !important;
  }
}

/* Desktop-only (>768px) */
@media (min-width: 769px) {
  .mobile-only {
    display: none !important;
  }

  .desktop-only {
    display: block;
  }
}

/* ========================================
   Reduced Motion Support (Accessibility)
   ======================================== */

@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;
  }
}

/* ========================================
   High Contrast Mode Support
   ======================================== */

@media (prefers-contrast: high) {
  .mobile-card {
    border-width: 2px;
  }

  .focus-visible-outline:focus-visible {
    outline-width: 3px;
  }
}

/* ========================================
   Print Styles
   ======================================== */

@media print {
  .mobile-only,
  .mobile-btn,
  .mobile-card-elevated {
    box-shadow: none !important;
    animation: none !important;
  }
}
