/**
 * Modal Components CSS
 * Comprehensive modal and overlay styling for both light and dark themes
 * Includes accessibility features and smooth animations
 */

/* ===== Base Modal Styles ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: var(--theme-overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1050;
  backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition: all var(--theme-transition-normal);
}

.modal.show {
  opacity: 1;
  visibility: visible;
}

.modal-dialog {
  width: 100%;
  max-width: 600px;
  margin: 1.75rem auto;
  transform: scale(0.9) translateY(-20px);
  transition: all var(--theme-transition-normal);
}

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

.modal-content {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  background-color: var(--theme-modal-bg);
  border-radius: 12px;
  box-shadow: var(--theme-shadow-xl);
  border: 1px solid var(--theme-modal-border);
  outline: 0;
  max-height: 90vh;
  overflow: hidden;
}

/* ===== Modal Header ===== */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  border-bottom: 1px solid var(--theme-border-primary);
  background-color: var(--theme-modal-bg);
  border-radius: 12px 12px 0 0;
}

.modal-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--theme-text-primary);
  line-height: 1.4;
}

.modal-subtitle {
  margin: 0.25rem 0 0 0;
  font-size: 0.875rem;
  color: var(--theme-text-secondary);
  font-weight: 400;
}

/* ===== Modal Close Button ===== */
.modal-close,
.close {
  background: transparent;
  border: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--theme-text-secondary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 6px;
  transition: all var(--theme-transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  line-height: 1;
}

.modal-close:hover,
.close:hover {
  background-color: var(--theme-hover-bg);
  color: var(--theme-text-primary);
}

.modal-close:focus,
.close:focus {
  outline: none;
  box-shadow: 0 0 0 3px var(--theme-focus-ring);
}

.modal-close:active,
.close:active {
  transform: scale(0.95);
}

/* ===== Modal Body ===== */
.modal-body {
  position: relative;
  flex: 1 1 auto;
  padding: 1.5rem;
  overflow-y: auto;
  color: var(--theme-text-primary);
  max-height: calc(90vh - 140px); /* Account for header and footer */
}

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

.modal-body::-webkit-scrollbar-track {
  background: var(--theme-bg-secondary);
  border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb {
  background: var(--theme-border-secondary);
  border-radius: 3px;
}

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

/* ===== Modal Footer ===== */
.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 1.5rem;
  border-top: 1px solid var(--theme-border-primary);
  background-color: var(--theme-bg-secondary);
  border-radius: 0 0 12px 12px;
  gap: 1rem;
  flex-wrap: wrap;
}

.modal-footer.centered {
  justify-content: center;
}

.modal-footer.start {
  justify-content: flex-start;
}

.modal-footer.between {
  justify-content: space-between;
}

/* ===== Modal Sizes ===== */
.modal-sm .modal-dialog {
  max-width: 400px;
}

.modal-lg .modal-dialog {
  max-width: 800px;
}

.modal-xl .modal-dialog {
  max-width: 1200px;
}

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

.modal-fullscreen .modal-content {
  height: 100%;
  border-radius: 0;
}

/* ===== Overlay Variants ===== */
.modal-overlay-light {
  background-color: rgba(255, 255, 255, 0.8);
}

.modal-overlay-dark {
  background-color: rgba(0, 0, 0, 0.8);
}

.modal-overlay-blur {
  backdrop-filter: blur(8px);
}

/* ===== Modal Animations ===== */
@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes modalFadeOut {
  from {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
  to {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
}

.modal-animate-in .modal-dialog {
  animation: modalFadeIn var(--theme-transition-normal) ease-out;
}

.modal-animate-out .modal-dialog {
  animation: modalFadeOut var(--theme-transition-normal) ease-in;
}

/* ===== Confirmation Modal Styles ===== */
.modal-confirm .modal-header {
  background-color: var(--theme-brand-warning);
  color: white;
  border-bottom-color: rgba(255, 255, 255, 0.2);
}

.modal-confirm .modal-title {
  color: white;
}

.modal-confirm .modal-close {
  color: rgba(255, 255, 255, 0.8);
}

.modal-confirm .modal-close:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

/* ===== Error Modal Styles ===== */
.modal-error .modal-header {
  background-color: var(--theme-brand-error);
  color: white;
  border-bottom-color: rgba(255, 255, 255, 0.2);
}

.modal-error .modal-title {
  color: white;
}

.modal-error .modal-close {
  color: rgba(255, 255, 255, 0.8);
}

.modal-error .modal-close:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

/* ===== Success Modal Styles ===== */
.modal-success .modal-header {
  background-color: var(--theme-brand-success);
  color: white;
  border-bottom-color: rgba(255, 255, 255, 0.2);
}

.modal-success .modal-title {
  color: white;
}

.modal-success .modal-close {
  color: rgba(255, 255, 255, 0.8);
}

.modal-success .modal-close:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

/* ===== Modal Loading State ===== */
.modal-loading {
  pointer-events: none;
}

.modal-loading .modal-content {
  position: relative;
}

.modal-loading .modal-content::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--theme-overlay-bg);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.modal-loading .modal-content::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2rem;
  height: 2rem;
  margin: -1rem 0 0 -1rem;
  border: 2px solid var(--theme-border-muted);
  border-radius: 50%;
  border-top-color: var(--theme-brand-primary);
  animation: spin 1s ease-in-out infinite;
  z-index: 11;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ===== Responsive Design ===== */
@media (max-width: 768px) {
  .modal-dialog {
    margin: 1rem;
    max-width: calc(100% - 2rem);
  }
  
  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 1rem;
  }
  
  .modal-footer {
    flex-direction: column;
    align-items: stretch;
  }
  
  .modal-footer > * {
    width: 100%;
    margin-bottom: 0.5rem;
  }
  
  .modal-footer > *:last-child {
    margin-bottom: 0;
  }
  
  .modal-fullscreen .modal-dialog {
    margin: 0;
    max-width: 100%;
  }
}

@media (max-width: 480px) {
  .modal-dialog {
    margin: 0.5rem;
    max-width: calc(100% - 1rem);
  }
  
  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 0.75rem;
  }
  
  .modal-title {
    font-size: 1.125rem;
  }
}

/* ===== Accessibility Enhancements ===== */
.modal[aria-hidden="true"] {
  display: none;
}

.modal[aria-hidden="false"] {
  display: flex;
}

/* Focus trap for modals */
.modal-content:focus {
  outline: none;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .modal-content {
    border-width: 2px;
  }
  
  .modal-header,
  .modal-footer {
    border-width: 2px;
  }
  
  .modal-close:focus,
  .close:focus {
    outline: 2px solid currentColor;
    outline-offset: 2px;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .modal,
  .modal-dialog,
  .modal-close,
  .close {
    transition: none;
  }
  
  .modal-animate-in .modal-dialog,
  .modal-animate-out .modal-dialog {
    animation: none;
  }
  
  .modal-loading .modal-content::before {
    animation: none;
  }
}

/* ===== Dark Theme Specific Adjustments ===== */
[data-theme="dark"] .modal-overlay-light {
  background-color: rgba(15, 23, 42, 0.8);
}

[data-theme="dark"] .modal-content {
  box-shadow: 
    var(--theme-shadow-xl),
    0 0 0 1px rgba(255, 255, 255, 0.05);
}

/* ===== Print Styles ===== */
@media print {
  .modal {
    display: none !important;
  }
}