/*
 * Modal shell (js/components/modal.js).
 *
 * Structure: .modal (fixed, centres the panel) > .modal__backdrop +
 * .modal__panel > .modal__close. Instances set the custom properties below
 * (and their own panel/backdrop paint) in their component file.
 *
 * Motion: opening adds .is-open (on the next frame for "transition" modals,
 * which then play the CSS transitions here; at once for "animate" modals,
 * whose Web Animations override them). Closing adds .is-closing; the panel
 * and backdrop exits are Web Animations (modal.js), the close button and any
 * embedded frame fade with the transitions below.
 */

html.has-overlay {
  overflow: hidden;
}

.modal {
  --modal-ease-open: cubic-bezier(0.16, 1, 0.3, 1);
  --modal-ease-close: cubic-bezier(0.55, 0.06, 0.68, 0.19);
  --modal-open-duration: 1020ms;     /* panel transform on open */
  --modal-close-duration: 820ms;
  --modal-close-backdrop-delay: 100ms;
  --modal-lift: 40px;                /* closed panel offset… */
  --modal-scale: 0.962;              /* …and scale */
  --modal-backdrop-ease: var(--modal-ease-open);
  --modal-close-open-delay: 0ms;     /* close-button fade-in delay once open */
  /* Close-button exit (instances replace the whole list). */
  --modal-close-exit:
    opacity 533ms ease,
    border-color 533ms ease,
    box-shadow 533ms ease,
    transform 533ms cubic-bezier(0.22, 0.61, 0.36, 1);

  position: fixed;
  inset: 0;
  z-index: 99999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.modal[hidden] {
  display: none;
}

/* ----- Backdrop ----- */

.modal__backdrop {
  position: absolute;
  inset: 0;
  cursor: pointer;
  opacity: 0;
  transition: opacity 680ms var(--modal-backdrop-ease);
}

.modal.is-open .modal__backdrop {
  opacity: 1;
}

.modal.is-closing .modal__backdrop {
  opacity: 0;
  transition: opacity var(--modal-close-duration) var(--modal-ease-close) var(--modal-close-backdrop-delay);
}

/* ----- Panel ----- */

.modal__panel {
  position: relative;
  z-index: 2;
  opacity: 0;
  transform: translateY(var(--modal-lift)) scale(var(--modal-scale));
  transition:
    opacity 520ms ease,
    transform var(--modal-open-duration) var(--modal-ease-open);
}

.modal.is-open .modal__panel {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.modal.is-closing .modal__panel {
  opacity: 0;
  transform: translateY(var(--modal-lift)) scale(var(--modal-scale));
  transition:
    opacity var(--modal-close-duration) var(--modal-ease-close),
    transform var(--modal-close-duration) var(--modal-ease-close);
}

/* An embedded document fades out with the panel. */
.modal__frame {
  display: block;
  width: 100%;
  height: 100%;
  flex: 1 1 auto;
  border: 0;
}

.modal__frame[hidden] {
  display: none;
}

/* ----- Close button (two crossed bars) ----- */

.modal__close {
  position: absolute;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  opacity: 0;
  transition:
    opacity 420ms ease 180ms,
    border-color 0.25s ease,
    box-shadow 0.25s ease,
    transform 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.modal__close span {
  position: absolute;
  width: 16px;
  height: 1.5px;
  border-radius: 999px;
  transition: background 0.25s ease;
}

.modal__close span:nth-child(1) {
  transform: rotate(45deg);
}

.modal__close span:nth-child(2) {
  transform: rotate(-45deg);
}

.modal__close:hover,
.modal__close:focus-visible {
  transform: rotate(90deg);
}

.modal.is-open .modal__close {
  opacity: 1;
  transition-delay: var(--modal-close-open-delay);
}

.modal.is-closing .modal__close {
  opacity: 0;
  transition: var(--modal-close-exit);
}

/* Reduced motion: transitions collapse to 1ms. Instances with their own
   reduced-motion treatment opt out with .modal--own-reduced-motion. */
@media (prefers-reduced-motion: reduce) {
  .modal:not(.modal--own-reduced-motion) :is(.modal__backdrop, .modal__panel, .modal__close, .modal__frame) {
    transition-duration: 1ms !important;
    transition-delay: 0ms !important;
  }
}
