/* ========================================
   MODAL SYSTEM
   ======================================== */

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: var(--z-modal);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 5vh;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--bg-primary);
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow-modal), 0 8px 32px rgba(0,0,0,0.12); /* subtle extra shadow */
  max-width: 98vw;
  max-height: 95vh;
  width: 100%;
  position: relative;
  transform: scale(0.9) translateY(20px);
  transition: transform 0.3s ease, opacity 0.2s ease, visibility 0.2s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0 var(--spacing-md) var(--spacing-sm) var(--spacing-md); /* no top padding */
  opacity: 0;
  visibility: hidden;
}

.modal-overlay.active .modal {
  transform: scale(1) translateY(0);
}




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

/* Modal content padding adjustments - ensure content doesn't touch borders */
.modal-content {
  padding: var(--spacing-md);
}

/* Modal content alerts - prevent them from touching modal edges */
.modal .alert {
  margin: var(--spacing-md) 0;
  z-index: calc(var(--z-alert) + 10);
}

/* First alert in modal content should have top margin */
.modal-content > .alert:first-child {
  margin-top: var(--spacing-md);
}

/* Last alert in modal content should have bottom margin */
.modal-content > .alert:last-child {
  margin-bottom: var(--spacing-md);
}

.modal-header {
  padding: var(--spacing-lg) var(--spacing-lg) var(--spacing-md) var(--spacing-lg);
  border-bottom: 2px solid var(--header-border);
  background-color: var(--header-bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.03);
  margin: 0 0 var(--spacing-md) 0;
}

.modal-title {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--header-text);
  letter-spacing: 0.5px;
}

.modal-actions {
  display: flex;
  gap: var(--spacing-sm);
  align-items: center;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: var(--spacing-xs);
  margin-right: var(--spacing-lg);
  border-radius: var(--border-radius);
  transition: color 0.2s ease, background-color 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
}

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



.modal-body {
  padding: var(--spacing-sm)  ;
  overflow-y: auto;
  flex: 1;
  max-height: calc(90vh - 120px);
}

.modal-footer {
  padding: var(--spacing-lg);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: var(--spacing-sm);
  flex-shrink: 0;
}

/* Modal sizes */
.modal-sm {
  max-width: 400px;
}

.modal-md {
  max-width: 600px;
}

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

.modal-xl {
  max-width: 1120px; /* Custom size for forms with line items */
}

.modal-xxl {
  max-width: 1400px; /* Restored to original size */
}

.modal-mdtallish {
  max-width: 600px;
  max-height: 50vh;
  height: 40vh;
}

.modal-tall {
  max-width: 1400px;
  max-height: 95vh;
  height: 95vh;
}

/* Responsive modal sizing - xl behaves like xxl on larger screens */
@media (min-width: 1400px) {
  .modal-xl {
    max-width: 1400px; /* Use xxl size on large screens */
  }
}

@media (min-width: 1200px) and (max-width: 1399px) {
  .modal-xl {
    max-width: 1200px; /* Slightly larger on medium-large screens */
  }
}

.modal-calendar {
  max-width: 100vw;
  min-width: 800px;
  max-height: 70vh;
  width: auto;
  height: auto;
}

.modal-free {
  max-width: 95vw;
  max-height: 90vh;
  width: auto;
  height: auto;
}

/* Loading state */
.modal-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}

.modal-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-color);
  border-top: 3px solid var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Responsive modal centering adjustments */
@media (max-height: 600px) {
  .modal-overlay {
    align-items: center;
    padding-top: 2vh;
  }
}

@media (min-height: 800px) {
  .modal-overlay {
    padding-top: 8vh;
  }
}

@media (max-width: 768px) {
  .modal-overlay {
    padding-top: 3vh;
  }
}