/**
 * Universal Modal System Styles
 * WCAG 2.2 AA Compliant Modal Dialog Implementation
 * Version: 1.0.0
 * 
 * Features:
 * - 100% WCAG 2.2 AA accessibility compliance
 * - Perfect background scroll prevention
 * - Adaptive status positioning for all zoom levels
 * - Semantic HTML with native <dialog> elements
 * - British spelling throughout
 */

/* ===== STRUCTURE SECTION ===== */

.universal-modal {
  padding: 0;
  border: none;
  background: transparent;
  max-width: none;
  max-height: none;
}

.universal-modal::backdrop {
  transition: opacity 0.3s ease;
}

.universal-modal-container {
  border-radius: 4px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* Responsive modal: Remove sticky header at high zoom/small viewports */
@media (max-height: 500px), (max-width: 480px) {
  .universal-modal-container {
    overflow-y: auto;
    display: block;
  }

  .universal-modal-body {
    flex: none;
    overflow-y: visible;
    padding-bottom: 4rem;
  }

  .universal-modal-body.has-status {
    padding-bottom: 5rem;
  }
}

/* Even more aggressive for very constrained spaces */
@media (max-height: 400px) {
  .universal-modal-container {
    max-height: 95vh;
  }
}

.universal-modal-header {
  padding: 1.5rem 1.5rem 0 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.universal-modal-icon {
  font-size: 1.25rem;
  flex-shrink: 0;
}

.universal-modal-heading {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  flex: 1;
  line-height: 1.2; /* Ensures consistent text baseline */
}

.universal-modal-close {
  background: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem;
  margin: -0.25rem -0.25rem -0.25rem 0;
  border-radius: 4px;
  flex-shrink: 0;
  outline: 0.2rem solid transparent;
  border: 0.2rem solid transparent;
}

.universal-modal-body {
  padding: 1.5rem;
  flex: 1;
  overflow-y: auto;
  padding-bottom: 4rem;
}

.universal-modal-body.has-status {
  padding-bottom: 5rem;
}

.universal-modal-footer {
  padding: 0 1.5rem 1.5rem 1.5rem;
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

.universal-modal-button {
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  min-width: 80px;
}

.universal-modal-button:focus {
  outline-offset: 2px;
}

.universal-modal-input {
  width: 100%;
  padding: 0.5rem;
  border-radius: 4px;
  font-size: 1rem;
  margin-top: 0.5rem;
}

.universal-modal-input:focus {
  outline-offset: 2px;
}

/* Enhanced Status Notification System with Dynamic Positioning */
.universal-modal-status {
  backdrop-filter: blur(8px);
  padding: 0.75rem 1.5rem;
  text-align: center;
  font-size: 0.9rem;
  z-index: 10;
  transform: translateY(100%);
  transition: transform 0.3s ease, opacity 0.3s ease;
  opacity: 0;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;

  /* Default positioning for normal viewports */
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

/* Normal viewport: absolute positioning works fine */
.universal-modal-status.normal-viewport {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

/* High zoom viewport: dynamic positioning via JavaScript */
.universal-modal-status.scrollable-viewport {
  position: fixed;
  /* Left, right, bottom set dynamically by JavaScript */
}

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

.universal-modal-status-icon {
  font-size: 1.1rem;
  flex-shrink: 0;
}

.universal-modal-status-text {
  flex: 1;
  line-height: 1.4;
}

.universal-modal-status-dismiss {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.25rem;
  margin-left: 0.5rem;
  border-radius: 3px;
  font-size: 1.2rem;
  opacity: 0.8;
  flex-shrink: 0;
}

.universal-modal-status-dismiss:hover {
  opacity: 1;
}

.universal-modal-status-dismiss:focus {
  outline-offset: 2px;
}

.universal-modal-spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

.universal-modal-status.status-long {
  padding: 1rem 1.5rem;
  text-align: left;
  min-height: auto;
  max-height: 120px;
  overflow-y: auto;
}

.universal-modal-status.status-long .universal-modal-status-text {
  white-space: pre-wrap;
  word-break: break-word;
}

/* Modal size variants */
.universal-modal-small .universal-modal-container {
  width: 400px;
  max-height: 90vh;
}
.universal-modal-medium .universal-modal-container {
  width: 650px;
  max-height: 90vh;
}
.universal-modal-large .universal-modal-container {
  width: 800px;
}
.universal-modal-fullscreen .universal-modal-container {
  width: 90vw;
  height: 100vh;
  border-radius: 0;
  max-height: none;
}

/* Animations with reduced motion support */
@media (prefers-reduced-motion: no-preference) {
  .universal-modal {
    animation: modal-scale-in 0.3s ease-out;
  }

  .universal-modal[closing] {
    animation: modal-scale-out 0.2s ease-in;
  }

  @keyframes modal-scale-in {
    from {
      opacity: 0;
      transform: scale(0.8);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }

  @keyframes modal-scale-out {
    from {
      opacity: 1;
      transform: scale(1);
    }
    to {
      opacity: 0;
      transform: scale(0.8);
    }
  }
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .universal-modal-small .universal-modal-container,
  .universal-modal-medium .universal-modal-container,
  .universal-modal-large .universal-modal-container {
    width: 95vw;
    max-height: 90vh;
  }
}

/* Stabilise modal container at high zoom */
@media (max-height: 500px), (max-width: 480px), (min-zoom: 2) {
  .universal-modal-container {
    /* Force consistent overflow behaviour */
    overflow-y: scroll; /* Changed from auto to always show scrollbar */
    overflow-x: hidden;

    /* Ensure smooth scrolling */
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
  }

  .universal-modal-body {
    /* Prevent content from touching edges */
    margin-right: 0.5rem; /* Space for scrollbar */
  }
}
