/* =============================================================================
   GLOBAL MODAL SYSTEM
   Modern, consistent modal styling with theme support

   Usage:
   - All modals automatically inherit these styles
   - Use modal_wrapper.html include for new modals
   - Standard sizes: modal-sm (400px), modal-lg (600px), modal-xl (900px)
   - Supports dark/light themes via CSS variables

   Browser Support:
   - Chrome/Edge 90+
   - Safari 14+
   - Firefox 88+
   ============================================================================= */

/* -----------------------------------------------------------------------------
   Backdrop Enhancements
   ----------------------------------------------------------------------------- */
.modal-backdrop {
  backdrop-filter: blur(8px);
  background-color: rgba(0, 0, 0, 0.65);
  transition: opacity 0.25s ease-out;
}

[data-theme='light'] .modal-backdrop {
  background-color: rgba(0, 0, 0, 0.5);
}

/* -----------------------------------------------------------------------------
   Modal Dialog Container
   ----------------------------------------------------------------------------- */
.modal-dialog {
  /* Default to modal-lg (900px - 1.5x wider than standard) */
  max-width: 900px;
}

.modal-dialog-centered {
  min-height: calc(100% - 3rem);
}

/* Ensure scrollable modals have proper constraints */
.modal-dialog-scrollable {
  height: calc(100% - 3rem);
}

.modal-dialog-scrollable .modal-content {
  max-height: 100%;
}

.modal-dialog-scrollable .modal-body {
  overflow-y: auto;
  max-height: calc(100vh - 210px);
}

/* -----------------------------------------------------------------------------
   Modal Content - Main Container
   ----------------------------------------------------------------------------- */
.modal-content {
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: 15px;
  color: var(--color-text);
  box-shadow: var(--shadow-modal);
  overflow: hidden;
  transition: background-color 0.3s ease,
              border-color 0.3s ease,
              color 0.3s ease;
}

/* Enhanced animation - slide up from bottom */
.modal.fade .modal-dialog {
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
              opacity 0.3s ease-out;
  transform: translateY(100px);
  opacity: 0;
}

.modal.show .modal-dialog {
  transform: translateY(0);
  opacity: 1;
}

/* -----------------------------------------------------------------------------
   Modal Header
   ----------------------------------------------------------------------------- */
.modal-header {
  padding: 1rem 1.75rem;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  border-radius: 15px 15px 0 0;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.modal-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--color-text);
  letter-spacing: -0.01em;
  line-height: 1.3;
  margin: 0;
}

/* Support for subtitle under title */
.modal-subtitle {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.9rem;
  font-weight: 400;
  color: var(--color-muted);
  line-height: 1.4;
}

/* Close button styling */
.modal-header .btn-close {
  padding: 0.5rem;
  margin: -0.25rem -0.25rem -0.25rem auto;
  opacity: 0.6;
  transition: opacity 0.2s ease, transform 0.2s ease;
  flex-shrink: 0;
}

.modal-header .btn-close:hover {
  opacity: 1;
  transform: scale(1.1);
}

.modal-header .btn-close:focus-visible {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 2px;
  opacity: 1;
}

/* -----------------------------------------------------------------------------
   Modal Body
   ----------------------------------------------------------------------------- */
.modal-body {
  padding: 1.75rem;
  background: var(--color-surface-alt);
  transition: background-color 0.3s ease;
}

/* Section spacing within modal body */
.modal-body .mb-3:last-child {
  margin-bottom: 0 !important;
}

/* -----------------------------------------------------------------------------
   Modal Form Elements & Inputs
   ----------------------------------------------------------------------------- */

/* Form groups within modals */
.modal-body .mb-3,
.modal-body .form-group {
  margin-bottom: 1.25rem;
}

/* Labels - clean, modern styling */
.modal-body .form-label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--color-text-secondary, var(--color-text));
  letter-spacing: 0.01em;
}

/* Input fields - modern styling matching reference image */
.modal-body .form-control,
.modal-body .form-select {
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  color: var(--color-text-muted);
  line-height: 1.5;
  transition: border-color 0.2s ease,
              box-shadow 0.2s ease,
              background-color 0.2s ease;
}

.modal-body .form-control:hover:not(:disabled):not([readonly]):not(:focus),
.modal-body .form-select:hover:not(:disabled):not(:focus) {
  border-color: var(--color-border-strong, var(--color-primary));
}

.modal-body .form-control:focus,
.modal-body .form-select:focus {
  background: var(--color-surface);
  border-color: var(--color-primary);
  border-width: 1px;
  outline: none;
  box-shadow: none;
}

.modal-body .form-control::placeholder {
  color: var(--color-placeholder);
  opacity: 0.7;
}

/* Textarea specific */
.modal-body textarea.form-control {
  resize: vertical;
  min-height: 100px;
  line-height: 1.6;
}

/* Select dropdowns */
.modal-body .form-select {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23666' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
  background-position: right 0.75rem center;
  background-size: 16px 12px;
  background-repeat: no-repeat;
  padding-right: 2.5rem;
  cursor: pointer;
}

/* Disabled state */
.modal-body .form-control:disabled,
.modal-body .form-select:disabled,
.modal-body .form-control[readonly] {
  background: var(--color-surface-hover, var(--color-surface-alt));
  color: var(--color-muted);
  opacity: 0.7;
  cursor: not-allowed;
  border-color: var(--color-border);
}

/* Input groups */
.modal-body .input-group .form-control:first-child {
  border-radius: 12px 0 0 12px;
}

.modal-body .input-group .form-control:last-child {
  border-radius: 0 12px 12px 0;
}

.modal-body .input-group .btn:first-child {
  border-radius: 12px 0 0 12px;
}

.modal-body .input-group .btn:last-child {
  border-radius: 0 12px 12px 0;
}

.modal-body .input-group .form-control:only-child,
.modal-body .input-group .btn:only-child {
  border-radius: 12px;
}

/* Form validation states */
.modal-body .form-control.is-invalid,
.modal-body .form-select.is-invalid {
  border-color: var(--color-danger);
  background-image: none;
}

.modal-body .form-control.is-invalid:focus,
.modal-body .form-select.is-invalid:focus {
  border-color: var(--color-danger);
  outline-color: var(--color-danger);
}

.modal-body .invalid-feedback {
  display: block;
  color: var(--color-danger);
  font-size: 0.875rem;
  margin-top: 0.35rem;
  font-weight: 500;
}

.modal-body .form-control.is-valid,
.modal-body .form-select.is-valid {
  border-color: var(--color-success);
}

.modal-body .valid-feedback {
  display: block;
  color: var(--color-success);
  font-size: 0.875rem;
  margin-top: 0.35rem;
  font-weight: 500;
}

/* Checkboxes and radios */
.modal-body .form-check-input {
  border-radius: 4px;
  border: 1px solid var(--color-border);
  background-color: var(--color-surface);
}

.modal-body .form-check-input:checked {
  background-color: var(--color-primary);
  border-color: var(--color-primary);
}

.modal-body .form-check-input:focus {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 2px;
  box-shadow: none;
}

/* Grid layouts for forms */
.modal-body .row {
  margin-left: -0.5rem;
  margin-right: -0.5rem;
}

.modal-body .row > * {
  padding-left: 0.5rem;
  padding-right: 0.5rem;
}

/* -----------------------------------------------------------------------------
   Modal Footer
   ----------------------------------------------------------------------------- */
.modal-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
  border-radius: 0 0 15px 15px;
  gap: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

/* Button styling in modal footer */
.modal-footer .btn {
  padding: 0.5rem 1rem;
  font-weight: 600;
  font-size: 0.95rem;
  border-radius: 10px;
  transition: all 0.2s ease;
  border-width: 1px;
  border-style: solid;
}

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

/* Primary button */
.modal-footer .btn-primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.25);
}

.modal-footer .btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover, var(--color-primary));
  border-color: var(--color-primary-hover, var(--color-primary));
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.35);
}

.modal-footer .btn-primary:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.25);
}

.modal-footer .btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  box-shadow: none;
}

/* Secondary button */
.modal-footer .btn-secondary,
.modal-footer .btn-outline-secondary {
  background: transparent;
  border-color: var(--color-border);
  color: var(--color-text-muted);
  box-shadow: none;
}

.modal-footer .btn-secondary:hover:not(:disabled),
.modal-footer .btn-outline-secondary:hover:not(:disabled) {
  background: var(--color-surface-hover, var(--color-surface-alt));
  border-color: var(--color-border-strong, var(--color-border));
  color: var(--color-text-muted);
}

.modal-footer .btn-secondary:disabled,
.modal-footer .btn-outline-secondary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Danger button */
.modal-footer .btn-danger {
  background: var(--color-danger);
  border-color: var(--color-danger);
  color: white;
}

.modal-footer .btn-danger:hover:not(:disabled) {
  background: var(--color-danger-hover, var(--color-danger));
  border-color: var(--color-danger-hover, var(--color-danger));
  filter: brightness(0.9);
}

.modal-footer .btn-outline-danger {
  background: transparent;
  border-color: var(--color-danger);
  color: var(--color-danger);
}

.modal-footer .btn-outline-danger:hover:not(:disabled) {
  background: var(--color-danger);
  border-color: var(--color-danger);
  color: white;
}

/* Success button */
.modal-footer .btn-success {
  background: var(--color-success);
  border-color: var(--color-success);
  color: white;
}

.modal-footer .btn-success:hover:not(:disabled) {
  background: var(--color-success-hover, var(--color-success));
  border-color: var(--color-success-hover, var(--color-success));
  filter: brightness(0.9);
}

/* -----------------------------------------------------------------------------
   Size Variants (Standardized)
   ----------------------------------------------------------------------------- */

/* Small modal - for simple confirmations */
.modal-dialog.modal-sm {
  max-width: 400px;
}

/* Large modal - DEFAULT (900px - 1.5x wider) */
.modal-dialog.modal-lg {
  max-width: 900px;
}

/* Extra large - for very complex forms */
.modal-dialog.modal-xl {
  max-width: 1200px;
}

/* -----------------------------------------------------------------------------
   Special Modal Variants
   ----------------------------------------------------------------------------- */

/* Post-clean review modal - custom scrolling behavior */
#postCleanReviewModal .modal-dialog {
  max-height: calc(100vh - 2rem);
}

#postCleanReviewModal .modal-content {
  display: flex;
  flex-direction: column;
  max-height: 100%;
}

#postCleanReviewModal .modal-body {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
  max-height: calc(
    100vh - 2rem - var(--post-clean-modal-header, 4.5rem) - var(--post-clean-modal-footer, 5rem)
  );
  padding-bottom: 3rem;
}

#postCleanReviewModal .modal-footer {
  position: sticky;
  bottom: 0;
  background-color: var(
    --cleanings-surface,
    var(--color-surface-alt, var(--color-surface, #ffffff))
  );
  border-top: 1px solid var(--color-border, rgba(15, 23, 42, 0.08));
  z-index: 1;
  flex-shrink: 0;
}

[data-theme='dark'] #postCleanReviewModal .modal-footer {
  background-color: var(
    --cleanings-surface,
    var(--color-surface-alt, var(--color-surface, #1f242c))
  );
  border-top-color: rgba(255, 255, 255, 0.08);
}

/* Dashboard settings modal */
.dashboard-settings-modal .modal-content {
  /* Inherits all global styles - add custom overrides if needed */
}

/* Sequence builder modal - extra large for complex interface */
#sequenceBuilderModal .modal-dialog {
  max-width: 1200px;
}

/* -----------------------------------------------------------------------------
   Responsive Adjustments
   ----------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .modal-dialog,
  .modal-dialog.modal-xl,
  .modal-dialog.modal-lg,
  .modal-dialog.modal-sm {
    margin: 1rem;
    max-width: calc(100% - 2rem);
  }

  .modal-header {
    padding: 1.25rem;
  }

  .modal-body {
    padding: 1.25rem;
  }

  .modal-footer {
    padding: 1rem 1.25rem;
    flex-direction: column;
  }

  .modal-footer .btn {
    width: 100%;
    margin: 0;
  }

  /* Ensure stacking order is correct on mobile */
  .modal-footer .btn:first-child {
    order: 2;
  }

  .modal-footer .btn-primary {
    order: 1;
  }

  .modal-title {
    font-size: 1.15rem;
  }

  .modal-body .form-control,
  .modal-body .form-select {
    font-size: 16px; /* Prevents iOS zoom on focus */
  }
}

@media (max-width: 576px) {
  .modal-dialog {
    margin: 0.5rem;
    max-width: calc(100% - 1rem);
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 1rem;
  }

  .modal-title {
    font-size: 1.1rem;
  }
}

/* Tablet landscape adjustments */
@media (min-width: 769px) and (max-width: 1024px) {
  .modal-dialog.modal-xl {
    max-width: 90%;
  }
}

/* -----------------------------------------------------------------------------
   Accessibility Enhancements
   ----------------------------------------------------------------------------- */

/* Focus-visible support for better keyboard navigation */
.modal-header .btn-close:focus-visible,
.modal-footer .btn:focus-visible {
  outline: 2px solid var(--color-focus-ring);
  outline-offset: 2px;
}

.modal-body .form-control:focus-visible,
.modal-body .form-select:focus-visible {
  outline: none;
  box-shadow: none;
  border-color: var(--color-primary);
  border-width: 1px;
}

/* Ensure proper focus trap */
.modal.show {
  overflow-x: hidden;
  overflow-y: auto;
}

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

/* Reduced motion support - accessibility */
@media (prefers-reduced-motion: reduce) {
  .modal.fade .modal-dialog {
    transition: opacity 0.15s linear;
    transform: none;
  }

  .modal-header .btn-close,
  .modal-footer .btn,
  .modal-body .form-control,
  .modal-body .form-select {
    transition: none;
  }

  .modal-backdrop {
    transition: none;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .modal-content {
    border-width: 2px;
  }

  .modal-body .form-control,
  .modal-body .form-select {
    border-width: 2px;
  }

  .modal-body .form-control:focus,
  .modal-body .form-select:focus {
    outline-width: 3px;
  }
}

/* -----------------------------------------------------------------------------
   Custom Scrollbar Styling (for better dark theme support)
   ----------------------------------------------------------------------------- */
.modal-body::-webkit-scrollbar {
  width: 8px;
}

.modal-body::-webkit-scrollbar-track {
  background: var(--color-surface);
  border-radius: 4px;
}

.modal-body::-webkit-scrollbar-thumb {
  background: var(--color-border-strong, var(--color-border));
  border-radius: 4px;
}

.modal-body::-webkit-scrollbar-thumb:hover {
  background: var(--color-muted);
}

/* Firefox scrollbar */
.modal-body {
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-strong, var(--color-border)) var(--color-surface);
}

/* -----------------------------------------------------------------------------
   Animation Variants (Optional - add class to modal)
   ----------------------------------------------------------------------------- */

/* Slide-up animation variant - add "modal-slide-up" class to .modal for more pronounced effect */
.modal.modal-slide-up.fade .modal-dialog {
  transform: translateY(150px);
}

.modal.modal-slide-up.show .modal-dialog {
  transform: translateY(0);
}

/* Scale animation variant - add "modal-scale" class to .modal */
.modal.modal-scale.fade .modal-dialog {
  transform: scale(0.8);
}

.modal.modal-scale.show .modal-dialog {
  transform: scale(1);
}

/* -----------------------------------------------------------------------------
   Print Styles
   ----------------------------------------------------------------------------- */
@media print {
  .modal {
    position: static;
  }

  .modal-dialog {
    max-width: 100%;
    margin: 0;
  }

  .modal-backdrop {
    display: none;
  }

  .modal-header,
  .modal-footer {
    border: none;
    background: white;
  }

  .modal-header .btn-close {
    display: none;
  }
}

/* -----------------------------------------------------------------------------
   Legacy Support & Compatibility
   ----------------------------------------------------------------------------- */

/* Ensure Bootstrap 5 compatibility */
.modal.fade:not(.show) {
  opacity: 0;
}

.modal.show {
  opacity: 1;
}

/* Support for older modal implementations during migration */
.modal-content.legacy-modal {
  /* Override with legacy styles if needed during transition */
  /* Currently empty - add overrides only if specific modals break */
}

/* -----------------------------------------------------------------------------
   Light Theme Overrides
   ----------------------------------------------------------------------------- */
.theme-light .modal-header {
  background: var(--color-bg);
}

.theme-light .modal-body {
  background: var(--color-bg);
}

.theme-light .modal-footer {
  background: var(--color-bg);
}

/* Light Theme - Make primary buttons in modals match Add Lead button style */
.theme-light .modal-footer .btn-primary,
.theme-light #addLeadModal .modal-footer .btn-primary {
  background: var(--color-primary-soft) !important;  /* Light purple background like btn-outline-primary */
  border-color: var(--color-primary-border) !important;  /* Purple border */
  color: var(--color-primary) !important;  /* Purple text instead of white */
  box-shadow: none !important;  /* Remove shadow for cleaner look */
}

.theme-light .modal-footer .btn-primary:hover:not(:disabled),
.theme-light #addLeadModal .modal-footer .btn-primary:hover,
.theme-light #addLeadModal .modal-footer .btn-primary:focus {
  background: var(--color-primary) !important;  /* Solid purple on hover */
  border-color: var(--color-primary) !important;
  color: #ffffff !important;  /* White text on hover */
  transform: translateY(-1px);
  box-shadow: 0 4px 12px var(--color-primary-soft) !important;
}

.theme-light .modal-footer .btn-primary:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 8px var(--color-primary-soft) !important;
}

/* =============================================================================
   END OF GLOBAL MODAL SYSTEM
   ============================================================================= */
