:root {
  --modal-dialog-header-height: 4rem;
  --modal-footer-height: 4rem;
}

dialog.modals--main-modal {
  min-width: 600px;
  width: max-content;
  max-width: var(--modal-max-width);
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 0;
  border-radius: var(--radius-lg);
  border: none;
  box-shadow: var(--shadow-neutral-md);
  backdrop-filter: blur(10px);
  outline: solid 1px var(--border-primary);
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s ease-out;

  &.closing {
    animation: modal-slide-out 0.3s ease-out;
    opacity: 0;
  }

  &[open]:not(.closing) {
    opacity: 1;
    animation: modal-slide-in 0.3s ease-out forwards;
  }

  @media screen and (max-width: 768px) {
    min-width: 300px;
  }

  @media screen and (max-width: 576px) {
    width: 100dvw;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    top: unset;
    bottom: 0;
  }

  &[data-source="child"] {
    &[open]:not(.closing) {
      animation: modal-swipe-left-to-right 0.3s ease-out forwards;
    }
  }

  &[data-source="parent"] {
    &[open]:not(.closing) {
      animation: modal-swipe-right-to-left 0.3s ease-out forwards;
    }
  }
}

@keyframes modal-swipe-left-to-right {
  from {
    transform: translate(-60%, -50%);
  }

  to {
    transform: translate(-50%, -50%);
  }
}

@keyframes modal-swipe-right-to-left {
  from {
    transform: translate(-40%, -50%);
  }

  to {
    transform: translate(-50%, -50%);
  }
}

@keyframes modal-slide-in {
  from {
    transform: translate(-50%, -40%);
  }

  to {
    transform: translate(-50%, -50%);
  }
}

@keyframes modal-slide-out {
  from {
    transform: translate(-50%, -50%);
  }

  to {
    transform: translate(-50%, -40%);
  }
}

@media screen and (max-width: 576px) {
  @keyframes modal-slide-in {
    from {
      transform: translate(-50%, 100%);
    }

    to {
      transform: translate(-50%, 0);
    }
  }

  @keyframes modal-slide-out {
    from {
      transform: translate(-50%, 0);
    }

    to {
      transform: translate(-50%, 100%);
    }
  }
}

.modals--backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100dvh;
  height: 100vh;
  z-index: 999;
  animation: backdrop-fade-in 0.3s ease-out forwards;

  &.closing {
    animation: backdrop-fade-out 0.3s ease-out;
  }
}

@keyframes backdrop-fade-in {
  from {
    background-color: transparent;
  }

  to {
    background-color: var(--modal-backdrop);
  }
}

@keyframes backdrop-fade-out {
  from {
    background-color: var(--modal-backdrop);
  }

  to {
    background-color: transparent;
  }
}

.modals--body {
  padding: 1.5rem;
  max-height: 500px;
  overflow-y: auto;
  overflow-x: clip;
}

.modals--footer {
  padding: 1rem;
  border-top: solid 1px var(--neutral-100);
}

.modals--footer-button-container {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;

  * {
    width: fit-content;
  }
}

.modals--header {
  width: 100%;
  padding: 0;
  border-bottom: solid 1px var(--neutral-100);
  border-radius: var(--table-radius) var(--table-radius) 0 0;
  background-color: white;
  position: relative;
  min-height: 3rem;
}

.modals--close-button {
  width: fit-content;
  height: fit-content;
  background-color: transparent;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  right: 1rem;
  transform: translateY(-50%);
  border: none;
  cursor: pointer;
  padding: 0;

  &:hover {
    opacity: 0.75;
  }
}

.modals--header-container {
  padding: 1.25rem 1.5rem;
  text-align: left;

  span {
    font-size: var(--rem-15);
    font-variation-settings: "wght" var(--text-medium);
    color: var(--neutral-950);
  }
}

.modals--header-container-new {
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 999;
}

.modals--content {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 1.5rem;
  overflow-y: auto;
  overflow-x: clip;
  max-height: calc(100vh - 2 * var(--modal-dialog-header-height) - 2 * var(--modal-footer-height));
}

#create-modal {
  dialog {
    width: clamp(300px, 75vw, 768px);
  }

  @media screen and (max-width: 576px) {
    dialog {
      width: var(--modal-max-width);
    }
  }
}

.modals--form-container {
  width: 100%;
  display: grid;
  /* I want to have 2 columns, but if they don't fit, wrap */
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;

  .patients--fields-row {
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
  }
}

.modals--body:has(#insurance-form) {
  max-height: 40rem;
}

.modals--header-new {
  width: 100%;
  padding: 1.5rem 1.5rem 0;
  border-top-left-radius: inherit;
  border-top-right-radius: inherit;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  
  &[data-is-dialog="true"] {
    padding-bottom: 1.5rem;
    border-bottom: solid 1px var(--border-subtle);
    height: var(--modal-dialog-header-height);
    
    .modals--close-button-new {
      position: relative;
      top: unset;
      right: unset;
    }
  }
  
  &[data-is-stacked="true"] {
    align-items: center;
    
    h5 {
      font-size: var(--rem-15);
      line-height: var(--rem-18);
      font-variation-settings: 'wght' var(--text-semibold);
      letter-spacing: 0;
      width: fit-content;
    }
  }

  h5 {
    font-size: var(--rem-18);
    line-height: var(--rem-26);
    font-variation-settings: 'wght' var(--text-semibold);
    letter-spacing: 0;
  }

  .modals--header-subheading {
    font-size: var(--rem-14);
    line-height: var(--rem-22);
    letter-spacing: 0;
    color: var(--text-subheading);
  }

  .modals--header-text-container {
    width: fit-content;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 0.25rem;
  }

  .modals--close-button-new {
    position: absolute;
    top: 1rem;
    right: 1rem;
  }
}

.modals--footer-new {
  height: var(--modal-footer-height);
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: .5rem;
  padding-block: .75rem;
  padding-inline: 1.5rem;
  background-color: var(--surface-subtle);
  border-top: solid 1px var(--border-secondary);
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit;
}

dialog#document-viewer-modal {
  max-width: 1768px;
  width: 95vw;
  height: 997px;

  @media screen and (max-width: 576px) {
    width: 100vw;
    height: 100vh;
    border-radius: 0;
  }
}

dialog#document-viewer-modal:has(.modals--header-new):has(.modals--footer-new) > .modals--content {
  height: calc(100% - var(--modal-dialog-header-height) - var(--modal-footer-height));
  padding: 0;

  @media screen and (max-width: 576px) {
    height: calc(100% - var(--modal-dialog-header-height) - var(--modal-footer-height) - .5rem);
  }
}