/* =============================================
   Modal Component — UI-0085, UI-0086, UI-0087, UI-0104
   ============================================= */

/* --- Modal Backdrop --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  background-color: rgba(8, 28, 46, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  overflow-y: auto;

  /* Backdrop enters */
  opacity: 0;
  animation: modal-backdrop-in var(--duration-normal, 300ms) var(--ease-out) forwards;
}

/* Backdrop exit */
.modal-backdrop.is-closing {
  animation: modal-backdrop-out var(--duration-normal, 300ms) var(--ease-in) forwards;
}

/* --- Modal Panel (inside .modal-backdrop) --- */
.modal-backdrop .modal {
  position: relative;
  background-color: var(--color-white);
  border-radius: var(--radius-xl); /* 16px */
  box-shadow: var(--shadow-xl);
  width: 100%;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 64px);

  /* Modal enters */
  opacity: 0;
  transform: scale(0.95);
  animation: modal-panel-in var(--duration-normal, 300ms) var(--ease-out) forwards;
}

/* --- BEM pattern: .modal[hidden] as outer wrapper --- */
.modal[hidden] {
  display: none !important;
}

.modal[role="dialog"] {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal, 1000);
  background-color: rgba(8, 28, 46, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  overflow-y: auto;
  opacity: 0;
  animation: modal-backdrop-in var(--duration-normal, 300ms) var(--ease-out) forwards;
}

.modal[role="dialog"] .modal__backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  cursor: pointer;
}

.modal[role="dialog"] .modal__content {
  position: relative;
  z-index: 1;
  background-color: var(--color-white);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 64px);
  opacity: 0;
  transform: scale(0.95);
  animation: modal-panel-in var(--duration-normal, 300ms) var(--ease-out) forwards;
}

.modal[role="dialog"] .modal__content--sm { max-width: 480px; }
.modal[role="dialog"] .modal__content--lg { max-width: 800px; }

/* Modal exit */
.modal-backdrop.is-closing .modal {
  animation: modal-panel-out var(--duration-normal, 300ms) var(--ease-in) forwards;
}

/* =============================================
   Modal Size Variants
   ============================================= */
.modal--sm  { max-width: 480px; }
.modal--md  { max-width: 640px; }  /* default */
.modal--lg  { max-width: 800px; }
.modal--xl  { max-width: 1024px; }
.modal--full {
  max-width: none;
  width: calc(100vw - 48px);
  height: calc(100vh - 48px);
  max-height: calc(100vh - 48px);
}

/* Default size if no size modifier */
.modal:not([class*="modal--"]) { max-width: 640px; }

/* =============================================
   Modal Structure
   ============================================= */

/* Header */
.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-6);
  border-bottom: 1px solid var(--border-default, #E2E8F0);
  flex-shrink: 0;
}

.modal__title {
  font-family: var(--font-sans);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--text-primary, #111111);
  line-height: var(--leading-tight, 1.25);
}

.modal__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted, #B0BAC5);
  border-radius: var(--radius-md);
  flex-shrink: 0;
  transition: color var(--duration-fast) var(--ease-default),
              background-color var(--duration-fast) var(--ease-default);
}

.modal__close:hover {
  color: var(--text-primary, #111111);
  background-color: var(--bg-subtle, #F7F8FA);
}

.modal__close svg {
  width: var(--icon-md);
  height: var(--icon-md);
  stroke: currentColor;
  fill: none;
}

/* Body */
.modal__body {
  padding: var(--space-6);
  overflow-y: auto;
  flex: 1;
  /* Max height leaves room for header + footer (~200px) */
  max-height: calc(100vh - 200px);
}

/* Footer */
.modal__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end; /* right-aligned buttons */
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-default, #E2E8F0);
  flex-shrink: 0;
  flex-wrap: wrap;
}

/* --- Modal Full Size Body --- */
.modal--full .modal__body {
  max-height: none;
}

/* =============================================
   Mobile: Slide Up from Bottom (< 640px)
   ============================================= */
@media (max-width: 639px) {
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal-backdrop .modal {
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    max-height: 90vh;
    width: 100%;
    animation: modal-slide-up var(--duration-normal, 300ms) var(--ease-out) forwards;
  }

  .modal-backdrop.is-closing .modal {
    animation: modal-slide-down var(--duration-normal, 300ms) var(--ease-in) forwards;
  }

  .modal[role="dialog"] {
    align-items: flex-end;
    padding: 0;
  }

  .modal[role="dialog"] .modal__content {
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    max-height: 90vh;
    max-width: 100%;
    animation: modal-slide-up var(--duration-normal, 300ms) var(--ease-out) forwards;
  }
}

/* =============================================
   Body scroll lock (applied via JS)
   ============================================= */
body.modal-open {
  overflow: hidden;
}

/* =============================================
   Keyframe Animations — UI-0087, UI-0104
   ============================================= */

@keyframes modal-backdrop-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes modal-backdrop-out {
  from { opacity: 1; }
  to   { opacity: 0; }
}

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

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

@keyframes modal-slide-up {
  from {
    opacity: 0;
    transform: translateY(100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes modal-slide-down {
  from {
    opacity: 1;
    transform: translateY(0);
  }
  to {
    opacity: 0;
    transform: translateY(100%);
  }
}

/* =============================================
   Modal — Enhanced Mobile Responsive
   ============================================= */
@media (max-width: 639px) {
  .modal--sm,
  .modal--md,
  .modal--lg,
  .modal--xl,
  .modal:not([class*="modal--"]) {
    max-width: 100%;
    width: 100%;
  }

  .modal__header {
    padding: var(--space-4);
  }

  .modal__body {
    padding: var(--space-4);
    max-height: calc(90vh - 160px);
    -webkit-overflow-scrolling: touch;
  }

  .modal__footer {
    padding: var(--space-4);
    flex-direction: column;
    gap: var(--space-2);
  }

  .modal__footer .btn-primary,
  .modal__footer .btn-secondary,
  .modal__footer .btn-ghost,
  .modal__footer .btn-destructive {
    width: 100%;
    justify-content: center;
  }

  .modal__title {
    font-size: var(--text-base);
  }

  /* Full-screen modal on mobile */
  .modal--full {
    width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
  }
}

@media (max-width: 480px) {
  .modal-backdrop .modal {
    max-height: 95vh;
  }

  .modal__header {
    padding: var(--space-3);
  }

  .modal__body {
    padding: var(--space-3);
    max-height: calc(95vh - 140px);
  }

  .modal__footer {
    padding: var(--space-3);
  }

  .modal__title {
    font-size: 15px;
  }
}
