/**
 * LifeOS Design System - Modal Component
 * 
 * Standardized modal patterns with backdrop, animations, and responsive sizing.
 * Uses semantic tokens from tokens.css.
 * 
 * @see tokens.css for variable definitions
 */

/* ==========================================================================
   Z-INDEX SCALE (for reference across all components)
   ========================================================================== 
   
   Components use a consistent z-index scale:
   
   1000    - Modal overlay (.modal-overlay)
   200     - Mobile navigation (.mobile-nav)
   100     - App header (.app-header)
   99      - Toast container fallback
   10      - Modal header/footer sticky (.modal-header, .modal-footer)
   1       - Modal content (.modal)
   
   Note: Toast notifications and tooltips use 9999 (see feedback.css)
   Skip links use 9999, focus debug uses 10000 (see accessibility.css)
   ========================================================================== */

/* ==========================================================================
   1. MODAL OVERLAY
   ========================================================================== */

/**
 * Modal backdrop - covers the entire viewport
 */
.modal-overlay {
  /* Positioning */
  position: fixed;
  inset: 0;
  z-index: 1000;
  
  /* Layout */
  display: flex;
  align-items: flex-end;
  justify-content: center;
  
  /* Appearance */
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  
  /* Animation */
  animation: modalFadeIn var(--primitive-duration-normal) var(--primitive-ease-default);
}

/* Mobile: slide up from bottom with safe area handling */
.modal-overlay:not(.modal-centered) {
  /* Ensure modal header is always visible with minimum top spacing */
  padding-top: max(var(--space-lg), env(safe-area-inset-top, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Centered modal variant */
.modal-overlay.modal-centered {
  align-items: center;
  padding: var(--space-lg);
}

/* ==========================================================================
   2. MODAL CONTAINER
   ========================================================================== */

.modal {
  /* Layout */
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  
  /* Sizing - Full width on mobile, constrained on larger screens */
  width: 100%;
  max-width: none; /* Full width on mobile by default */
  /* Leave space for header visibility - overlay handles safe areas */
  max-height: calc(100% - var(--space-md));
  
  /* Appearance */
  background-color: var(--surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  
  /* Overflow - enable scrolling within modal */
  overflow: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
  overscroll-behavior: contain; /* Prevent scroll chaining */
  
  /* Animation */
  animation: modalSlideUp var(--primitive-duration-slow) var(--primitive-ease-default);
}

/* Small screens: constrain width slightly for better appearance */
@media (min-width: 480px) {
  .modal {
    max-width: 500px;
  }
}

/* Centered variant */
.modal-centered {
  border-radius: var(--radius-xl);
}

/* Mobile: slide up animation */
@keyframes modalSlideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* Fade in for backdrop */
@keyframes modalFadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* Hidden state */
.modal-overlay.hidden {
  display: none;
}

/* ==========================================================================
   3. MODAL HEADER
   ========================================================================== */

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md);
  border-bottom: 1px solid var(--border-muted);
  background-color: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: var(--primitive-font-weight-semibold);
  color: var(--text);
  margin: 0;
}

.modal-subtitle {
  font-size: var(--text-sm);
  color: var(--text-dim);
  margin: var(--space-xs) 0 0;
}

/* ==========================================================================
   4. MODAL BODY
   ========================================================================== */

.modal-body {
  flex: 1;
  /* min-height: 0 allows flex item to shrink below content size */
  min-height: 0;
  padding: var(--space-md);
  /* Don't set overflow-y here - let .modal handle scrolling
     so that sticky elements inside (like .settings-tabs) work */
}

/* ==========================================================================
   5. MODAL FOOTER
   ========================================================================== */

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-sm);
  padding: var(--space-md);
  border-top: 1px solid var(--border-muted);
  background-color: var(--surface);
  position: sticky;
  bottom: 0;
  z-index: 10;
}

/* ==========================================================================
   6. MODAL CLOSE BUTTON
   ========================================================================== */

.modal-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px; /* WCAG 2.1 minimum touch target */
  min-height: 44px; /* WCAG 2.1 minimum touch target */
  padding: var(--space-xs);
  border: none;
  border-radius: var(--radius-md);
  background-color: var(--surface-hover);
  color: var(--text-dim);
  cursor: pointer;
  transition: background-color var(--primitive-duration-fast) var(--primitive-ease-default),
              color var(--primitive-duration-fast) var(--primitive-ease-default);
}

.modal-close:hover {
  background-color: var(--surface-active);
  color: var(--text);
}

.modal-close:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ==========================================================================
   7. MODAL CONTENT VARIANTS
   ========================================================================== */

/* Settings modal - wider for 2-column layouts */
/* On mobile (<480px), inherits max-width: none from .modal for full width */
.modal-settings .modal-body {
  padding: var(--space-lg);
}

/* Settings modal width progression */
@media (min-width: 480px) {
  .modal-settings {
    max-width: 720px;
  }
}

/* Form modal */
.modal-form .modal-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

/* Confirmation modal */
.modal-confirm .modal-body {
  text-align: center;
  padding: var(--space-xl);
}

.modal-confirm .modal-icon {
  font-size: 3rem;
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.modal-confirm .modal-message {
  font-size: var(--text-base);
  color: var(--text-dim);
  margin-bottom: var(--space-lg);
}

/* ==========================================================================
   8. RESPONSIVE BREAKPOINTS
   ========================================================================== */

/* Tablet and up */
@media (min-width: 768px) {
  .modal {
    max-width: 680px;
  }
  
  .modal-overlay {
    align-items: center;
    padding: var(--space-xl);
  }
  
  .modal {
    border-radius: var(--radius-xl);
    max-height: 85vh;
    max-height: 85dvh;
  }
  
  .modal-header {
    padding: var(--space-lg);
  }
  
  .modal-body {
    padding: var(--space-lg);
  }
  
  .modal-footer {
    padding: var(--space-lg);
  }
  
  /* Settings modal gets extra width */
  .modal-settings {
    max-width: 800px;
  }
  
  /* Centered modal animation for larger screens */
  @keyframes modalCenterIn {
    from {
      opacity: 0;
      transform: scale(0.95);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }
  
  .modal-overlay.modal-centered .modal {
    animation: modalCenterIn var(--primitive-duration-slow) var(--primitive-ease-default);
  }
}

/* Large desktop */
@media (min-width: 1024px) {
  .modal-settings {
    max-width: 900px;
  }
}


/* ==========================================================================
   9. REDUCED MOTION SUPPORT
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  /* Disable modal animations */
  .modal-overlay {
    animation: none;
  }
  
  .modal {
    animation: none;
    transform: none;
  }
  
  .modal-overlay.modal-centered .modal {
    animation: none;
  }
  
  /* Instant transitions */
  .modal-close {
    transition: none;
  }
}
