/**
 * CRITICAL-MOBILE.CSS
 * Critical mobile styles for above-the-fold content
 *
 * SIZE TARGET: < 14KB (inline in <head>)
 * LOAD: Synchronous, inline for instant rendering
 * SCOPE: Essential mobile layout, touch targets, typography
 *
 * VERSION: 1.0.0 - Mobile UI Refactoring Phase 1
 * LAST UPDATED: 2025-10-27
 */

/* ===== CSS CUSTOM PROPERTIES (DESIGN TOKENS) ===== */
:root {
  /* Touch Target Standards (Apple/Google Guidelines) */
  --touch-min: 44px;          /* Minimum touch target */
  --touch-optimal: 48px;      /* Optimal touch target */
  --touch-large: 56px;        /* Large touch target (primary actions) */
  --touch-fab: 56px;          /* Floating action button */

  /* Color System */
  --primary-color: #0071e3;
  --primary-dark: #0056b3;
  --primary-light: #66b3ff;
  --success-color: #10b981;
  --warning-color: #f59e0b;
  --danger-color: #ef4444;
  --info-color: #3b82f6;

  /* Grayscale */
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;

  /* Text Colors */
  --text-primary: #1a1a1a;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --text-inverse: #ffffff;

  /* Background Colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #f3f4f6;

  /* Spacing (8px Grid System) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;
  --space-12: 48px;

  /* Typography Scale */
  --text-xs: 0.75rem;    /* 12px */
  --text-sm: 0.875rem;   /* 14px */
  --text-base: 1rem;     /* 16px */
  --text-lg: 1.125rem;   /* 18px */
  --text-xl: 1.25rem;    /* 20px */
  --text-2xl: 1.5rem;    /* 24px */

  /* Border Radius */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1);

  /* Z-Index Layers */
  --z-base: 1;
  --z-dropdown: 1000;
  --z-sticky: 1020;
  --z-fixed: 1030;
  --z-modal-backdrop: 1040;
  --z-modal: 1050;
  --z-toast: 9999;

  /* Transitions */
  --transition-fast: 150ms ease-out;
  --transition-normal: 300ms ease-out;
  --transition-slow: 500ms ease-out;
}

/* ===== RESET & BASE (MOBILE-FIRST) ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  scroll-behavior: smooth;
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'SF Pro Display', 'Segoe UI',
               Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  line-height: 1.6;
  font-size: var(--text-base);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: none;
  min-height: 100vh;
  min-height: -webkit-fill-available;
}

/* ===== GLOBAL TOUCH TARGET ENFORCEMENT ===== */
button,
a[role="button"],
.btn,
.nav-item,
input[type="button"],
input[type="submit"],
input[type="reset"] {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Optimal touch targets for important actions */
.btn-primary,
.btn-success,
.btn-danger,
.fab,
.mobile-action-btn {
  min-height: var(--touch-optimal);
  min-width: var(--touch-optimal);
}

/* Large touch targets for FAB and primary mobile actions */
.fab,
.mobile-fab,
.primary-mobile-action {
  min-height: var(--touch-large);
  min-width: var(--touch-large);
}

/* ===== FORM INPUTS (TOUCH-OPTIMIZED) ===== */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="tel"],
input[type="number"],
input[type="search"],
input[type="url"],
input[type="date"],
input[type="time"],
input[type="datetime-local"],
select,
textarea {
  min-height: var(--touch-optimal);
  padding: 0.75rem 1rem;
  font-size: 16px; /* Prevents iOS zoom */
  border: 2px solid var(--gray-300);
  border-radius: var(--radius-md);
  background: var(--bg-primary);
  color: var(--text-primary);
  transition: all var(--transition-fast);
  -webkit-appearance: none;
  appearance: none;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.1);
}

/* Select dropdowns with custom arrow */
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23374151' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  background-size: 12px;
  padding-right: 2.5rem;
}

/* Textarea specific */
textarea {
  min-height: 100px;
  resize: vertical;
  line-height: 1.5;
}

/* ===== MOBILE LAYOUT SYSTEM ===== */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  overflow-x: hidden;
}

.content-area {
  flex: 1;
  padding: var(--space-4);
  padding-top: calc(60px + var(--space-4)); /* Mobile header height + padding */
}

/* ===== MOBILE HEADER ===== */
.mobile-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--gray-200);
  z-index: var(--z-fixed);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  box-shadow: var(--shadow-sm);
}

.mobile-header-title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text-primary);
}

.mobile-menu-toggle {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  background: none;
  border: none;
  padding: var(--space-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Hamburger Icon */
.mobile-nav-icon {
  display: flex;
  flex-direction: column;
  width: 24px;
  height: 18px;
  justify-content: space-between;
}

.mobile-nav-icon span {
  display: block;
  height: 2px;
  background: var(--gray-700);
  border-radius: 1px;
  transition: all var(--transition-fast);
}

.mobile-nav-icon.active span:nth-child(1) {
  transform: rotate(45deg) translate(6px, 6px);
}

.mobile-nav-icon.active span:nth-child(2) {
  opacity: 0;
}

.mobile-nav-icon.active span:nth-child(3) {
  transform: rotate(-45deg) translate(6px, -6px);
}

/* ===== MOBILE SIDEBAR ===== */
.mobile-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-modal);
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--transition-normal);
}

.mobile-sidebar.active {
  opacity: 1;
  pointer-events: all;
}

.mobile-sidebar-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.mobile-sidebar-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 280px;
  max-width: 85vw;
  height: 100%;
  background: var(--bg-primary);
  transform: translateX(-100%);
  transition: transform var(--transition-normal);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: var(--shadow-xl);
}

.mobile-sidebar.active .mobile-sidebar-content {
  transform: translateX(0);
}

/* ===== BUTTONS (TOUCH-OPTIMIZED) ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch-min);
  padding: 0.75rem 1.25rem;
  font-size: var(--text-base);
  font-weight: 600;
  border-radius: var(--radius-md);
  border: none;
  cursor: pointer;
  transition: all var(--transition-fast);
  text-decoration: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--primary-color);
  color: var(--text-inverse);
  min-height: var(--touch-optimal);
}

.btn-primary:active {
  background: var(--primary-dark);
  transform: scale(0.98);
}

.btn-secondary {
  background: var(--gray-100);
  color: var(--text-primary);
  border: 1px solid var(--gray-300);
}

.btn-secondary:active {
  background: var(--gray-200);
  transform: scale(0.98);
}

.btn-success {
  background: var(--success-color);
  color: var(--text-inverse);
  min-height: var(--touch-optimal);
}

.btn-danger {
  background: var(--danger-color);
  color: var(--text-inverse);
  min-height: var(--touch-optimal);
}

.btn-outline {
  background: transparent;
  border: 2px solid var(--primary-color);
  color: var(--primary-color);
}

/* Icon-only buttons */
.btn-icon {
  padding: var(--space-2);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
}

/* ===== MODAL SYSTEM (BOTTOM SHEET) ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-modal);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.modal.active {
  display: flex;
  align-items: flex-end; /* Bottom sheet on mobile */
}

.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: var(--z-modal-backdrop);
}

.modal-content {
  position: relative;
  z-index: var(--z-modal);
  background: var(--bg-primary);
  border-radius: 20px 20px 0 0;
  max-height: 92vh;
  width: 100%;
  animation: slideUpModal 0.3s ease-out;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

@keyframes slideUpModal {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.modal-header {
  position: sticky;
  top: 0;
  background: var(--bg-primary);
  z-index: 10;
  padding: var(--space-4) var(--space-6);
  border-bottom: 2px solid var(--gray-100);
}

/* Modal drag handle */
.modal-header::before {
  content: '';
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 4px;
  background: var(--gray-300);
  border-radius: 2px;
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: 600;
  padding-top: var(--space-3);
  color: var(--text-primary);
}

.modal-body {
  flex: 1;
  padding: var(--space-6);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-footer {
  position: sticky;
  bottom: 0;
  background: var(--bg-primary);
  border-top: 2px solid var(--gray-100);
  padding: var(--space-4) var(--space-6);
  padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.modal-footer .btn {
  width: 100%;
  min-height: 52px;
  font-size: var(--text-base);
  font-weight: 600;
}

/* ===== SAFE AREA INSETS (iOS NOTCH) ===== */
@supports (padding: max(0px)) {
  .modal-body {
    padding-left: max(var(--space-6), env(safe-area-inset-left));
    padding-right: max(var(--space-6), env(safe-area-inset-right));
  }

  .modal-footer {
    padding-left: max(var(--space-6), env(safe-area-inset-left));
    padding-right: max(var(--space-6), env(safe-area-inset-right));
  }

  .content-area {
    padding-left: max(var(--space-4), env(safe-area-inset-left));
    padding-right: max(var(--space-4), env(safe-area-inset-right));
    padding-bottom: max(var(--space-4), env(safe-area-inset-bottom));
  }
}

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

/* Focus indicators for keyboard navigation */
*:focus-visible {
  outline: 3px solid var(--primary-color);
  outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  button,
  a,
  input,
  select,
  textarea {
    border-width: 2px;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== DESKTOP HIDE (MOBILE-ONLY ELEMENTS) ===== */
@media (min-width: 769px) {
  .mobile-header,
  .mobile-sidebar,
  .mobile-only {
    display: none !important;
  }

  .content-area {
    padding-top: var(--space-4);
  }
}

/* ===== UTILITY CLASSES ===== */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

.fw-normal { font-weight: 400; }
.fw-medium { font-weight: 500; }
.fw-semibold { font-weight: 600; }
.fw-bold { font-weight: 700; }

.text-primary { color: var(--primary-color); }
.text-success { color: var(--success-color); }
.text-danger { color: var(--danger-color); }
.text-muted { color: var(--text-muted); }

.d-none { display: none; }
.d-block { display: block; }
.d-flex { display: flex; }
.d-inline-flex { display: inline-flex; }

@media (max-width: 768px) {
  .d-mobile-none { display: none !important; }
  .d-mobile-block { display: block !important; }
}

/* ===== LOADING SPINNER ===== */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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