/* =============================================
   UniMate Modal System v4.0
   Premium glassmorphism · Spring animations
   Light + Dark theme · Unified styling

   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 --- */
.modal,
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.22, 1, 0.36, 1), visibility 0.3s;
  padding: 16px;
  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 --- */
.modal-content,
.modal-overlay>.modal,
.modal-overlay>.modal-card {
  width: 100%;
  max-width: 520px;
  background: var(--surface, #ffffff);
  border-radius: 20px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  box-shadow:
    0 24px 64px rgba(0, 0, 0, 0.12),
    0 8px 24px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  position: relative;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  max-height: min(calc(100vh - 48px), 85vh);
  transform: scale(0.92) translateY(20px);
  opacity: 0;
  transition:
    transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1),
    opacity 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

.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: scale(1) translateY(0);
  opacity: 1;
}

.modal-dialog {
  width: 100%;
  max-width: 520px;
  position: relative;
  z-index: 1;
}

/* --- 3. Header --- */
.modal-header {
  padding: 20px 24px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
  border-bottom: 1px solid var(--border, rgba(0, 0, 0, 0.06));
  gap: 12px;
  flex-shrink: 0;
}

.modal-header h2,
.modal-header h3,
.modal-header .modal-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  color: var(--text, #0f172a);
  line-height: 1.3;
  letter-spacing: -0.02em;
}

/* --- 4. Body --- */
.modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1 1 auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  font-size: 14px;
  line-height: 1.55;
  color: var(--text, #0f172a);
}

.modal-body::-webkit-scrollbar {
  width: 4px;
}

.modal-body::-webkit-scrollbar-track {
  background: transparent;
}

.modal-body::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.2);
  border-radius: 9999px;
}

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

/* --- 5. Footer --- */
.modal-footer,
.modal .form-actions,
.modal-overlay .form-actions {
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  background: rgba(248, 250, 252, 0.5);
  border-top: 1px solid var(--border, rgba(0, 0, 0, 0.06));
  flex-shrink: 0;
}

/* --- 6. Close button --- */
.modal-close,
.modal-close-btn,
.close-modal,
.close-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border, rgba(0, 0, 0, 0.08));
  background: transparent;
  color: var(--muted, #94a3b8);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 15px;
  line-height: 1;
  padding: 0;
  text-decoration: none;
  transition:
    background 0.15s ease,
    color 0.15s ease,
    border-color 0.15s ease,
    transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.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.2);
  transform: rotate(90deg);
}

.modal .modal-header .btn.btn-icon,
.um-modal .um-modal__header .btn.btn-icon,
.um-modal .um-modal__header .btn-icon {
  background: transparent;
  border: 1px solid var(--border, rgba(0, 0, 0, 0.08));
  font-size: 16px;
  color: var(--muted, #94a3b8);
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s, transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), border-color 0.15s;
  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 --- */
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 .um-footer {
  pointer-events: none !important;
  user-select: 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) .um-footer,
body:has(.modal[aria-hidden="false"]) .um-footer,
body:has(.modal-overlay.active) .um-footer {
  pointer-events: none !important;
  user-select: none !important;
}

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 .um-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) .um-footer *,
body:has(.modal[aria-hidden="false"]) .um-footer *,
body:has(.modal-overlay.active) .um-footer * {
  pointer-events: none !important;
}

[inert] {
  pointer-events: none !important;
  user-select: none !important;
}

[inert] * {
  pointer-events: none !important;
}

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

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

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

.modal input[type="text"],
.modal input[type="email"],
.modal input[type="password"],
.modal input[type="number"],
.modal input[type="url"],
.modal input[type="search"],
.modal textarea,
.modal select {
  width: 100%;
  height: 42px;
  padding: 9px 14px;
  border: 1px solid var(--border, rgba(0, 0, 0, 0.08));
  border-radius: 10px;
  background: var(--surface, #fff);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text, #0f172a);
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  box-sizing: border-box;
  font-family: inherit;
}

.modal textarea {
  height: auto;
  min-height: 80px;
  resize: vertical;
}

.modal select {
  appearance: auto;
  cursor: pointer;
}

.modal input:focus,
.modal textarea:focus,
.modal select:focus {
  border-color: var(--primary, #6366f1);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.modal input::placeholder,
.modal textarea::placeholder {
  color: var(--muted, #94a3b8);
}

/* --- 11. Buttons inside modals --- */
.modal .btn,
.modal-overlay .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 600;
  height: 40px;
  padding: 0 18px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
  border: 1px solid transparent;
  text-decoration: none;
  line-height: 1;
  font-family: inherit;
}

.modal .btn-primary {
  background: var(--primary, #6366f1);
  color: #ffffff;
  border-color: var(--primary, #6366f1);
  box-shadow: 0 1px 3px rgba(99, 102, 241, 0.2);
}

.modal .btn-primary:hover {
  background: var(--primary-hover, #4f46e5);
  border-color: var(--primary-hover, #4f46e5);
  box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3);
  transform: translateY(-1px);
}

.modal .btn-primary:active {
  transform: translateY(0) scale(0.98);
}

.modal .btn-outline,
.modal .btn-secondary {
  background: transparent;
  color: var(--text, #0f172a);
  border-color: var(--border, rgba(0, 0, 0, 0.08));
}

.modal .btn-outline:hover,
.modal .btn-secondary:hover {
  background: rgba(0, 0, 0, 0.03);
  border-color: rgba(0, 0, 0, 0.12);
}

/* --- 12. Modal actions row --- */
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 16px;
  border-top: 1px solid var(--border, rgba(0, 0, 0, 0.06));
  margin-top: 16px;
}

/* ══════════════════════════════════════════════
   DARK THEME
   ══════════════════════════════════════════════ */
.theme-dark .modal,
.theme-dark .modal-overlay {
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
}

.theme-dark .modal-content,
.theme-dark .modal-overlay>.modal,
.theme-dark .modal-overlay>.modal-card {
  background: var(--surface, #1e293b);
  border-color: rgba(255, 255, 255, 0.08);
  color: var(--text, #f1f5f9);
  box-shadow:
    0 24px 64px rgba(0, 0, 0, 0.5),
    0 8px 24px rgba(0, 0, 0, 0.25);
}

.theme-dark .modal-header {
  border-bottom-color: rgba(148, 163, 184, 0.1);
  background: transparent;
}

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

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

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

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

.theme-dark .modal-footer,
.theme-dark .modal .form-actions,
.theme-dark .modal-overlay .form-actions {
  background: rgba(2, 6, 23, 0.3);
  border-top-color: rgba(148, 163, 184, 0.1);
}

.theme-dark .close-btn,
.theme-dark .modal-close,
.theme-dark .modal-close-btn,
.theme-dark .close-modal {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(148, 163, 184, 0.12);
  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.25);
}

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

/* Dark form inputs */
.theme-dark .modal .form-row label,
.theme-dark .modal-overlay .form-row label,
.theme-dark .modal .form-group label,
.theme-dark .modal-overlay .form-group label {
  color: var(--text, #e2e8f0);
}

.theme-dark .modal input[type="text"],
.theme-dark .modal input[type="email"],
.theme-dark .modal input[type="password"],
.theme-dark .modal input[type="number"],
.theme-dark .modal input[type="url"],
.theme-dark .modal input[type="search"],
.theme-dark .modal textarea,
.theme-dark .modal select {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(148, 163, 184, 0.12);
  color: var(--text, #e2e8f0);
}

.theme-dark .modal input:focus,
.theme-dark .modal textarea:focus,
.theme-dark .modal select:focus {
  border-color: var(--primary, #6366f1);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.theme-dark .modal input::placeholder,
.theme-dark .modal textarea::placeholder {
  color: var(--muted, #64748b);
}

/* Dark buttons */
.theme-dark .modal .btn-primary {
  background: var(--primary, #6366f1);
  color: #ffffff;
  border-color: var(--primary, #6366f1);
}

.theme-dark .modal .btn-primary:hover {
  background: var(--primary-hover, #4f46e5);
  border-color: var(--primary-hover, #4f46e5);
}

.theme-dark .modal .btn-outline,
.theme-dark .modal .btn-secondary {
  background: transparent;
  color: var(--text, #e2e8f0);
  border-color: rgba(148, 163, 184, 0.12);
}

.theme-dark .modal .btn-outline:hover,
.theme-dark .modal .btn-secondary:hover {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(148, 163, 184, 0.2);
}

/* Dark modal-actions */
.theme-dark .modal-actions {
  border-top-color: rgba(148, 163, 184, 0.1);
}

/* ══════════════════════════════════════════════
   MOBILE
   ══════════════════════════════════════════════ */
@media (max-width: 640px) {

  .modal,
  .modal-overlay {
    padding: 12px;
    align-items: flex-end;
  }

  .modal-content,
  .modal-overlay>.modal,
  .modal-overlay>.modal-card {
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 85vh;
    transform: translateY(100%);
    opacity: 1;
    transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
  }

  .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);
  }

  .modal-dialog,
  .modal-dialog.modal-wide,
  .modal-dialog.modal-lg,
  .modal-dialog.modal-sm {
    max-width: 100%;
  }

  .modal-header {
    padding: 16px 20px 14px;
  }

  .modal-body {
    padding: 16px 20px;
  }

  .modal-footer,
  .modal .form-actions,
  .modal-overlay .form-actions {
    padding: 14px 20px;
    padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }
}