/**
 * GeoFazendas Modal Base Component
 * 
 * Unified modal system with mobile-first design:
 * - Mobile (≤768px): Bottom sheet with swipe-to-close gesture
 * - Desktop (>768px): Centered dialog with scale animation
 * 
 * Usage: Include this CSS globally via base.html
 * See static/js/modal-base.js for JavaScript controller
 * See templates/components/modal_base.html for HTML structure
 * 
 * @version 1.0.0
 */

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

:root {
  /* Modal sizing */
  --gf-modal-width-sm: 400px;
  --gf-modal-width-md: 520px;
  --gf-modal-width-lg: 720px;
  --gf-modal-width-xl: 900px;
  
  /* Mobile bottom sheet */
  --gf-modal-mobile-max-height: 90dvh;
  --gf-modal-mobile-border-radius: 1.25rem;
  --gf-modal-handle-width: 40px;
  --gf-modal-handle-height: 4px;
  
  /* Animation timing */
  --gf-modal-transition-duration: 0.3s;
  --gf-modal-transition-easing: cubic-bezier(0.32, 0.72, 0, 1);
  
  /* Z-index layers */
  --gf-modal-z-index: 1060;
  --gf-modal-backdrop-z-index: 1055;
  
  /* Colors */
  --gf-modal-backdrop-color: rgba(15, 23, 42, 0.6);
  --gf-modal-background: #ffffff;
  --gf-modal-border-color: rgba(0, 0, 0, 0.1);
  --gf-modal-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  --gf-modal-handle-color: rgba(0, 0, 0, 0.2);
}

/* ==========================================================================
   Body State - Prevents scroll when modal is open
   ========================================================================== */

body.gf-modal-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
}

/* iOS Safari fix for body scroll lock */
body.gf-modal-open {
  -webkit-overflow-scrolling: none;
}

/* ==========================================================================
   Modal Container
   ========================================================================== */

.gf-modal {
  position: fixed;
  inset: 0;
  z-index: var(--gf-modal-z-index);
  display: flex;
  align-items: flex-end; /* Mobile: align to bottom */
  justify-content: center;
  padding: 0;
  margin: 0;
  opacity: 1;
  visibility: visible;
  transition: opacity var(--gf-modal-transition-duration) ease,
              visibility var(--gf-modal-transition-duration) ease;
}

.gf-modal[aria-hidden="true"] {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* Explicit mobile override - ensure full width bottom sheet */
@media (max-width: 767.98px) {
  .gf-modal {
    padding: 0 !important;
    margin: 0 !important;
  }
  
  .gf-modal__dialog {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
  }
}

/* ==========================================================================
   Backdrop
   ========================================================================== */

.gf-modal__backdrop {
  position: absolute;
  inset: 0;
  z-index: -1;
  background-color: var(--gf-modal-backdrop-color);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* ==========================================================================
   Dialog (Mobile-First: Bottom Sheet)
   ========================================================================== */

.gf-modal__dialog {
  position: relative;
  width: 100%;
  max-width: 100%;
  margin: 0; /* Ensure no margin on mobile for full-width bottom sheet */
  max-height: var(--gf-modal-mobile-max-height);
  background-color: var(--gf-modal-background);
  border-radius: var(--gf-modal-mobile-border-radius) var(--gf-modal-mobile-border-radius) 0 0;
  box-shadow: var(--gf-modal-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  
  /* Animation: slide up from bottom */
  transform: translateY(0);
  transition: transform var(--gf-modal-transition-duration) var(--gf-modal-transition-easing);
  
  /* Touch handling - pan-y allows vertical scrolling */
  touch-action: pan-y;
  will-change: transform;
}

.gf-modal[aria-hidden="true"] .gf-modal__dialog {
  transform: translateY(100%);
}

/* Dragging state - disable transition for smooth drag */
.gf-modal__dialog.is-dragging {
  transition: none;
}

/* ==========================================================================
   Mobile Drag Handle
   ========================================================================== */

.gf-modal__handle {
  display: flex;
  justify-content: center;
  padding: 0.75rem 1rem 0.25rem;
  cursor: grab;
  touch-action: none;
  flex-shrink: 0;
}

.gf-modal__handle:active {
  cursor: grabbing;
}

.gf-modal__handle-bar {
  width: var(--gf-modal-handle-width);
  height: var(--gf-modal-handle-height);
  background-color: var(--gf-modal-handle-color);
  border-radius: 2px;
  transition: background-color 0.15s ease, width 0.15s ease;
}

.gf-modal__handle:hover .gf-modal__handle-bar,
.gf-modal__handle:active .gf-modal__handle-bar {
  background-color: rgba(0, 0, 0, 0.35);
  width: 50px;
}

/* ==========================================================================
   Header
   ========================================================================== */

.gf-modal__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  flex-shrink: 0;
}

/* Header with centered content (for confirmation modals) */
.gf-modal__header--centered {
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 1.5rem 1.5rem 1rem;
}

.gf-modal__title {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--gf-color-neutral-900, #1e293b);
  margin: 0;
  line-height: 1.4;
  flex: 1;
  padding-right: 2rem; /* Space for close button */
}

.gf-modal__header--centered .gf-modal__title {
  padding-right: 0;
  font-size: 1.25rem;
}

.gf-modal__subtitle {
  font-size: 0.9375rem;
  color: var(--gf-color-neutral-600, #64748b);
  margin: 0.25rem 0 0;
  line-height: 1.5;
}

/* ==========================================================================
   Close Button
   ========================================================================== */

.gf-modal__close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2.25rem;
  height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background-color: transparent;
  border-radius: 0.5rem;
  color: var(--gf-color-neutral-500, #64748b);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
  z-index: 1;
}

.gf-modal__close:hover,
.gf-modal__close:focus-visible {
  background-color: var(--gf-color-neutral-100, #f1f5f9);
  color: var(--gf-color-neutral-700, #334155);
}

.gf-modal__close:focus-visible {
  outline: 2px solid var(--gf-color-primary, #1c6b2a);
  outline-offset: 2px;
}

.gf-modal__close .material-icons,
.gf-modal__close .material-icons-outlined {
  font-size: 1.25rem;
}

/* ==========================================================================
   Icon (for confirmation/alert modals)
   ========================================================================== */

.gf-modal__icon {
  width: 4rem;
  height: 4rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
}

.gf-modal__icon .material-icons,
.gf-modal__icon .material-icons-outlined {
  font-size: 2rem;
}

/* Icon variants */
.gf-modal__icon--warning {
  background-color: rgba(245, 158, 11, 0.1);
  color: #f59e0b;
}

.gf-modal__icon--error,
.gf-modal__icon--danger {
  background-color: rgba(220, 38, 38, 0.1);
  color: #dc2626;
}

.gf-modal__icon--info {
  background-color: rgba(59, 130, 246, 0.1);
  color: #3b82f6;
}

.gf-modal__icon--success {
  background-color: rgba(34, 197, 94, 0.1);
  color: #22c55e;
}

.gf-modal__icon--help {
  background-color: rgba(var(--gf-color-primary-rgb, 28, 107, 42), 0.1);
  color: var(--gf-color-primary, #1c6b2a);
}

/* ==========================================================================
   Body (Scrollable Content Area)
   ========================================================================== */

.gf-modal__body {
  flex: 1;
  padding: 0 1.25rem 1.25rem;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Body with more padding for centered content */
.gf-modal__body--centered {
  text-align: center;
  padding: 0 1.5rem 1.5rem;
}

/* Message text inside body */
.gf-modal__message {
  font-size: 0.9375rem;
  color: var(--gf-color-neutral-600, #64748b);
  margin: 0;
  line-height: 1.6;
}

/* ==========================================================================
   Footer
   ========================================================================== */

.gf-modal__footer {
  display: flex;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  background-color: var(--gf-color-neutral-50, #f8fafc);
  border-top: 1px solid var(--gf-modal-border-color);
  flex-shrink: 0;
  
  /* Mobile: stack buttons vertically (cancel below confirm for thumb reach) */
  flex-direction: column-reverse;
}

.gf-modal__footer .btn {
  width: 100%;
  justify-content: center;
}

/* Footer centered (for confirmation dialogs) */
.gf-modal__footer--centered {
  justify-content: center;
  background-color: transparent;
  border-top: none;
  padding: 0 1.5rem 1.5rem;
}

/* ==========================================================================
   Size Variants
   ========================================================================== */

.gf-modal--sm .gf-modal__dialog {
  --gf-modal-desktop-width: var(--gf-modal-width-sm);
}

.gf-modal--md .gf-modal__dialog {
  --gf-modal-desktop-width: var(--gf-modal-width-md);
}

.gf-modal--lg .gf-modal__dialog {
  --gf-modal-desktop-width: var(--gf-modal-width-lg);
}

.gf-modal--xl .gf-modal__dialog {
  --gf-modal-desktop-width: var(--gf-modal-width-xl);
}

/* ==========================================================================
   DESKTOP STYLES (>768px)
   Centered dialog with scale animation
   ========================================================================== */

@media (min-width: 768px) {
  .gf-modal {
    align-items: center;
    padding: 1rem;
  }
  
  .gf-modal__dialog {
    max-width: var(--gf-modal-desktop-width, var(--gf-modal-width-md));
    max-height: calc(100vh - 2rem);
    border-radius: 1rem;
    
    /* Animation: scale + fade */
    transform: scale(1);
    transition: transform var(--gf-modal-transition-duration) var(--gf-modal-transition-easing),
                opacity var(--gf-modal-transition-duration) ease;
  }
  
  .gf-modal[aria-hidden="true"] .gf-modal__dialog {
    transform: scale(0.95);
  }
  
  /* Hide drag handle on desktop */
  .gf-modal__handle {
    display: none;
  }
  
  /* Header adjustments */
  .gf-modal__header {
    padding: 1.5rem 1.5rem 1rem;
  }
  
  .gf-modal__header--centered {
    padding: 2rem 2rem 1rem;
  }
  
  /* Body adjustments */
  .gf-modal__body {
    padding: 0 1.5rem 1.5rem;
  }
  
  .gf-modal__body--centered {
    padding: 0 2rem 1.5rem;
  }
  
  /* Footer: horizontal layout on desktop */
  .gf-modal__footer {
    flex-direction: row;
    justify-content: flex-end;
    padding: 1rem 1.5rem;
  }
  
  .gf-modal__footer .btn {
    width: auto;
    min-width: 120px;
  }
  
  .gf-modal__footer--centered {
    justify-content: center;
    padding: 0 2rem 2rem;
  }
  
  /* Close button position */
  .gf-modal__close {
    top: 1rem;
    right: 1rem;
  }
}

/* ==========================================================================
   Fullscreen Variant (for complex content like maps, editors)
   ========================================================================== */

.gf-modal--fullscreen .gf-modal__dialog {
  max-width: 100%;
  max-height: 100%;
  height: 100%;
  border-radius: 0;
}

@media (min-width: 768px) {
  .gf-modal--fullscreen .gf-modal__dialog {
    max-width: calc(100vw - 2rem);
    max-height: calc(100vh - 2rem);
    height: auto;
    border-radius: 1rem;
  }
}

/* ==========================================================================
   Form Inside Modal
   ========================================================================== */

.gf-modal form {
  display: contents;
}

.gf-modal .form-group,
.gf-modal .mb-3 {
  margin-bottom: 1rem;
}

.gf-modal .form-label {
  font-weight: 500;
  margin-bottom: 0.5rem;
}

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

/* No footer variant */
.gf-modal--no-footer .gf-modal__footer {
  display: none;
}

/* No header variant */
.gf-modal--no-header .gf-modal__header {
  display: none;
}

.gf-modal--no-header .gf-modal__close {
  z-index: 10;
}

/* Sticky footer (always visible during scroll) */
.gf-modal--sticky-footer .gf-modal__footer {
  position: sticky;
  bottom: 0;
}

/* Loading state */
.gf-modal.is-loading .gf-modal__body {
  opacity: 0.5;
  pointer-events: none;
}

.gf-modal.is-loading .gf-modal__footer .btn {
  pointer-events: none;
}

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

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  .gf-modal,
  .gf-modal__dialog {
    transition: none;
  }
}

/* Focus visible styles */
.gf-modal__dialog:focus {
  outline: none;
}

/* High contrast mode support */
@media (forced-colors: active) {
  .gf-modal__dialog {
    border: 2px solid CanvasText;
  }
  
  .gf-modal__handle-bar {
    background-color: CanvasText;
  }
}
