/* =============================================
   UniMate Modal System — um-modal.css
   
   Single source of truth for all modal styles.
   Three HTML patterns all render identically:
   
   A) .modal > .modal-content
   B) .modal-overlay > .modal (inner .modal = content)
   C) .modal-overlay > .modal-card
   
   Optional wrapper: .modal-dialog around .modal-content
   ============================================= */

/* --- 1. Overlay (full-screen backdrop) --- */
.modal,
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.58);
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity .28s cubic-bezier(0.4, 0, 0.2, 1),
              visibility .28s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 24px;
  isolation: isolate;
}

.modal.hidden,
.modal-overlay.hidden {
  display: none !important;
}

.modal.active,
.modal[aria-hidden="false"],
.modal-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* --- 2. Content panel (the card) --- */
.modal-content,
.modal-overlay > .modal,
.modal-overlay > .modal-card {
  width: 100%;
  max-width: 560px;
  background: var(--surface, #ffffff);
  border-radius: 22px;
  border: 1px solid rgba(226, 232, 240, 0.72);
  box-shadow:
    0 36px 70px -16px rgba(2, 6, 23, 0.36),
    0 0 0 1px rgba(255, 255, 255, 0.18),
    0 12px 32px rgba(15, 23, 42, 0.12);
  overflow: hidden;
  position: relative;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  max-height: min(calc(100vh - 48px), 680px);
  transform: translateY(16px) scale(0.97);
  opacity: 0;
  transition: transform .32s cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity .24s ease;
}

/* When overlay is active → card slides into place */
.modal.active .modal-content,
.modal.active > .modal-dialog > .modal-content,
.modal[aria-hidden="false"] .modal-content,
.modal-overlay.active > .modal,
.modal-overlay.active > .modal-card {
  transform: translateY(0) scale(1);
  opacity: 1;
}

/* .modal-dialog — transparent wrapper */
.modal-dialog {
  width: 100%;
  max-width: 520px;
  position: relative;
  z-index: 1;
}

/* --- 3. Header --- */
.modal-header {
  padding: 18px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border, #e2e8f0);
  background: linear-gradient(180deg,
    rgba(255, 255, 255, 0.98) 0%,
    rgba(248, 250, 252, 0.86) 100%);
  gap: 12px;
  flex-shrink: 0;
}

.modal-header h2,
.modal-header h3,
.modal-header .modal-title {
  margin: 0;
  font-size: 1.12rem;
  font-weight: 800;
  color: var(--text, #0f172a);
  line-height: 1.35;
  letter-spacing: -0.01em;
}

/* --- 4. Body --- */
.modal-body {
  padding: 22px 24px;
  overflow-y: auto;
  flex: 1 1 auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Subtle custom scrollbar inside modal body */
.modal-body::-webkit-scrollbar { width: 6px; }
.modal-body::-webkit-scrollbar-track { background: transparent; }
.modal-body::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.25);
  border-radius: 3px;
}
.modal-body::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.4);
}

/* --- 5. Footer / form-actions --- */
.modal-footer,
.modal .form-actions,
.modal-overlay .form-actions {
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  background: linear-gradient(0deg,
    rgba(248, 250, 252, 0.95) 0%,
    rgba(250, 252, 255, 0.72) 100%);
  border-top: 1px solid var(--border, #e2e8f0);
  flex-shrink: 0;
}

/* --- 6. Close button (all variants) --- */
.modal-close,
.modal-close-btn,
.close-modal,
.close-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid rgba(226, 232, 240, 0.7);
  background: rgba(0, 0, 0, 0.03);
  color: #94a3b8;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 16px;
  line-height: 1;
  padding: 0;
  text-decoration: none;
  transition: background .2s ease,
              color .2s ease,
              transform .25s cubic-bezier(0.34, 1.56, 0.64, 1),
              border-color .2s ease;
}
.close-modal { user-select: none; }

.modal-close:hover,
.modal-close-btn:hover,
.close-modal:hover,
.close-btn:hover {
  background: rgba(239, 68, 68, 0.08);
  color: #ef4444;
  border-color: rgba(239, 68, 68, 0.25);
  transform: rotate(90deg);
}

/* Also style btn-icon variants inside modal headers */
.modal .modal-header .btn.btn-icon,
.um-modal .um-modal__header .btn.btn-icon,
.um-modal .um-modal__header .btn-icon {
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid var(--border, #e2e8f0);
  font-size: 18px;
  color: var(--muted, #64748b);
  cursor: pointer;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  transition: background 0.2s, color 0.2s, transform 0.2s, border-color 0.2s;
  flex: 0 0 auto;
}
.modal .modal-header .btn.btn-icon:hover,
.um-modal .um-modal__header .btn.btn-icon:hover,
.um-modal .um-modal__header .btn-icon:hover {
  background: rgba(239, 68, 68, 0.08);
  color: #ef4444;
  transform: rotate(90deg);
}

/* --- 7. Invisible backdrop for click-outside --- */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: transparent;
  z-index: 0;
}
.modal-dialog { z-index: 1; }

/* --- 8. Size variants --- */
.modal-wide .modal-content,
.modal-wide.modal-content,
.modal-dialog.modal-wide,
.modal-dialog.modal-lg,
.modal-overlay > .modal.modal-wide,
.modal-overlay > .modal-card.modal-wide {
  max-width: 680px;
}

.modal-sm .modal-content,
.modal-sm.modal-content,
.modal-dialog.modal-sm {
  max-width: 400px;
}

/* --- 9. Focus trap: disable interaction with background UI when modal is open --- */
body.um-modal-open .sidebar,
body.um-modal-open .admin-sidebar,
body.um-modal-open .header,
body.um-modal-open .mobile-nav,
body.um-modal-open .compact-footer {
  pointer-events: none !important;
  user-select: none !important;
  opacity: 0.55;
  filter: saturate(0.8) brightness(0.92);
  transition: opacity .18s ease, filter .18s ease;
}

body:has(.modal.active) .sidebar,
body:has(.modal[aria-hidden="false"]) .sidebar,
body:has(.modal-overlay.active) .sidebar,
body:has(.modal.active) .admin-sidebar,
body:has(.modal[aria-hidden="false"]) .admin-sidebar,
body:has(.modal-overlay.active) .admin-sidebar,
body:has(.modal.active) .header,
body:has(.modal[aria-hidden="false"]) .header,
body:has(.modal-overlay.active) .header,
body:has(.modal.active) .mobile-nav,
body:has(.modal[aria-hidden="false"]) .mobile-nav,
body:has(.modal-overlay.active) .mobile-nav,
body:has(.modal.active) .compact-footer,
body:has(.modal[aria-hidden="false"]) .compact-footer,
body:has(.modal-overlay.active) .compact-footer {
  pointer-events: none !important;
  user-select: none !important;
  opacity: 0.55;
  filter: saturate(0.8) brightness(0.92);
}

body.um-modal-open .sidebar *,
body.um-modal-open .admin-sidebar *,
body.um-modal-open .header *,
body.um-modal-open .mobile-nav *,
body.um-modal-open .compact-footer * {
  pointer-events: none !important;
}

body:has(.modal.active) .sidebar *,
body:has(.modal[aria-hidden="false"]) .sidebar *,
body:has(.modal-overlay.active) .sidebar *,
body:has(.modal.active) .admin-sidebar *,
body:has(.modal[aria-hidden="false"]) .admin-sidebar *,
body:has(.modal-overlay.active) .admin-sidebar *,
body:has(.modal.active) .header *,
body:has(.modal[aria-hidden="false"]) .header *,
body:has(.modal-overlay.active) .header *,
body:has(.modal.active) .mobile-nav *,
body:has(.modal[aria-hidden="false"]) .mobile-nav *,
body:has(.modal-overlay.active) .mobile-nav *,
body:has(.modal.active) .compact-footer *,
body:has(.modal[aria-hidden="false"]) .compact-footer *,
body:has(.modal-overlay.active) .compact-footer * {
  pointer-events: none !important;
}

/* Ensure inert elements can't receive focus (browser support for [inert]) */
[inert] {
  pointer-events: none !important;
  user-select: none !important;
}
[inert] * {
  pointer-events: none !important;
}

/* --- 10. Mobile: bottom-sheet --- */
/* --- 11. Dark theme --- */
.theme-dark .modal,
.theme-dark .modal-overlay {
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.theme-dark .modal-content,
.theme-dark .modal-overlay > .modal,
.theme-dark .modal-overlay > .modal-card {
  background: var(--surface-elevated, var(--surface, #111827));
  border-color: rgba(148, 163, 184, 0.15);
  color: var(--text, #f1f5f9);
  box-shadow:
    0 24px 64px -12px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(148, 163, 184, 0.08),
    inset 0 1px 0 rgba(148, 163, 184, 0.06);
}

.theme-dark .modal-header {
  border-bottom-color: var(--border, rgba(148,163,184,0.12));
  background: linear-gradient(180deg,
    rgba(148, 163, 184, 0.04) 0%,
    transparent 100%);
}

.theme-dark .modal-header h2,
.theme-dark .modal-header h3,
.theme-dark .modal-header .modal-title {
  color: var(--text, #e5e7eb);
}

.theme-dark .modal-body {
  color: var(--text, #e5e7eb);
}

.theme-dark .modal-body::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.15);
}
.theme-dark .modal-body::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.3);
}

.theme-dark .modal-footer,
.theme-dark .modal .form-actions,
.theme-dark .modal-overlay .form-actions {
  background: rgba(10, 14, 26, 0.5);
  border-top-color: var(--border, rgba(148,163,184,0.12));
}

.theme-dark .close-btn,
.theme-dark .modal-close,
.theme-dark .modal-close-btn,
.theme-dark .close-modal {
  background: rgba(255, 255, 255, 0.05);
  border-color: rgba(148, 163, 184, 0.2);
  color: #94a3b8;
}
.theme-dark .close-btn:hover,
.theme-dark .modal-close:hover,
.theme-dark .modal-close-btn:hover,
.theme-dark .close-modal:hover {
  background: rgba(239, 68, 68, 0.12);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.3);
}

.theme-dark .modal .modal-header .btn.btn-icon:hover,
.theme-dark .um-modal .um-modal__header .btn.btn-icon:hover,
.theme-dark .um-modal .um-modal__header .btn-icon:hover {
  background: rgba(239, 68, 68, 0.12);
  color: #f87171;
}

.theme-dark .modal .modal-dialog {
  background: transparent;
}

/* --- 12. Form elements inside modals --- */
.modal .form-row,
.modal-overlay .form-row {
  margin-bottom: 14px;
}

.modal .form-row label,
.modal-overlay .form-row label {
  display: block;
  font-weight: 600;
  color: var(--text, #0f172a);
  margin-bottom: 6px;
  font-size: 13.5px;
}

.modal .form-group,
.modal-overlay .form-group {
  margin-bottom: 14px;
}

.modal .form-group label,
.modal-overlay .form-group label {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text, #0f172a);
}

/* --- 14. Modal actions row (used in intl, places, etc.) --- */
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 16px;
  border-top: 1px solid var(--border, #e2e8f0);
  margin-top: 16px;
}
