.dialog-wrapper {
  position: relative;
  display: inline-block;
}

.dialog {
  border: none;
  padding: 0;
  margin: 0;
  margin-top: 0.5rem;
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 20;
  max-width: min(24rem, calc(100vw - 2rem));
  width: max-content;
  background: white;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  outline: 1px solid rgb(229 231 235 / 0.5);
  opacity: 0;
}

.dialog.dialog-animated {
  animation: dialog-pop 220ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
  transform-origin: top left;
}

.dialog[data-align="center"] {
  left: 50%;
  translate: -50% 0;
}

.dialog[data-align="right"] {
  left: auto;
  right: 0;
}

.dialog[data-align="center"].dialog-animated {
  transform-origin: top center;
}

.dialog[data-align="right"].dialog-animated {
  transform-origin: top right;
}

.dialog.dialog-flip.dialog-animated {
  animation-name: dialog-pop-up;
  transform-origin: bottom left;
}

.dialog.dialog-flip[data-align="center"].dialog-animated {
  transform-origin: bottom center;
}

.dialog.dialog-flip[data-align="right"].dialog-animated {
  transform-origin: bottom right;
}

@keyframes dialog-pop {
  0% {
    opacity: 0;
    transform: scale(0.85) translateY(-8px);
  }
  60% {
    transform: scale(1.03) translateY(1px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes dialog-pop-up {
  0% {
    opacity: 0;
    transform: scale(0.85) translateY(8px);
  }
  60% {
    transform: scale(1.03) translateY(-1px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.dialog__body {
  padding: var(--space-lg);
}

.dialog__title {
  font-size: 1rem;
  line-height: 1.35;
  font-weight: 600;
  margin-bottom: var(--space-md);
}

.dialog__description {
  font-size: 0.8125rem;
  color: var(--color-armadillo-600);
  margin-bottom: var(--space-md);
}

/* Full modal variant — centered, with backdrop, on all viewports. */
.dialog--full,
.dialog--full[data-align] {
  position: fixed;
  inset: 0;
  margin: auto;
  width: min(48rem, calc(100vw - 2rem));
  max-width: none;
  max-height: calc(100vh - 4rem);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2xl);
  outline: none;
  background: var(--color-white);
  padding: 0;
  flex-direction: column;
  overflow: hidden;
}

.dialog--full[open] {
  display: flex;
}

/* When the dialog's content is wrapped in a turbo-frame (lazy-loaded modal),
   the frame must participate in the flex layout so the body can scroll
   and the footer stays pinned. */
.dialog--full > turbo-frame {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
}

.dialog--narrow,
.dialog--narrow[data-align] {
  width: min(36rem, calc(100vw - 2rem));
}

.dialog--full::backdrop {
  background: rgb(0 0 0 / 0.35);
}

.dialog--full .dialog__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-spring-wood-300);
  flex-shrink: 0;
}

.dialog--full .dialog__title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-armadillo-900);
  margin: 0;
}

.dialog--full .dialog__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-lg);
}

.dialog--full .dialog__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  border-top: 1px solid var(--color-spring-wood-300);
  flex-shrink: 0;
}

/* Forms inside popover dialogs use the same right-aligned action pattern as .dialog__footer. */
.dialog .form__actions {
  flex-direction: row;
  justify-content: flex-end;
}

.dialog .form__actions > .btn {
  flex: 0 0 auto;
}

@media (max-width: 640px) {
  .dialog,
  .dialog[data-align] {
    position: fixed;
    inset: 0;
    translate: none;
    max-width: calc(100vw - 2rem);
    width: 100%;
    margin: auto;
    border-radius: var(--radius-lg);
  }

  .dialog.dialog-animated,
  .dialog[data-align].dialog-animated {
    animation: dialog-mobile 250ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
  }

  @keyframes dialog-mobile {
    from {
      opacity: 0;
      transform: translateY(0.5rem) scale(0.98);
    }
    to {
      opacity: 1;
      transform: translateY(0) scale(1);
    }
  }

  .dialog::backdrop {
    background: rgb(0 0 0 / 0.25);
    animation: backdrop-fade 250ms ease forwards;
  }

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

  .dialog input:not([type=hidden]):not([type=radio]):not([type=checkbox]),
  .dialog textarea,
  .dialog select {
    font-size: 1rem;
  }
}
