/* ==========================================================
   ChartedNurse Modals
   Shared modal / dialog system
   Mobile-first: designed for 390px and up
   Requires: base.css, buttons.css
   ========================================================== */


/* ==========================================================
   OVERLAY
   ========================================================== */

.cn-modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  background: rgba(15, 23, 42, 0.58);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.cn-modal.is-open,
.cn-modal.open,
.cn-modal[aria-hidden="false"] {
  display: flex;
}


/* ==========================================================
   DIALOG
   Mobile defaults to bottom-sheet behavior
   ========================================================== */

.cn-modal-dialog {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: min(90dvh, 760px);
  margin-top: auto;
  border: 1px solid var(--cn-border-soft);
  border-bottom: 0;
  border-radius:
    var(--cn-radius-xl)
    var(--cn-radius-xl)
    0
    0;
  background: var(--cn-bg);
  box-shadow: var(--cn-shadow-lg);
  overflow: hidden;
}

.cn-modal-dialog-sm,
.cn-modal-dialog-md,
.cn-modal-dialog-lg,
.cn-modal-dialog-xl {
  width: 100%;
}


/* ==========================================================
   MOBILE DRAG INDICATOR
   Decorative only
   ========================================================== */

.cn-modal-handle {
  flex: 0 0 auto;
  width: 44px;
  height: 4px;
  margin: var(--cn-space-2) auto 0;
  border-radius: var(--cn-radius-pill);
  background: var(--cn-border);
}


/* ==========================================================
   HEADER
   ========================================================== */

.cn-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex: 0 0 auto;
  gap: var(--cn-space-4);
  padding: var(--cn-space-5) var(--cn-space-4) var(--cn-space-4);
  border-bottom: 1px solid var(--cn-border-soft);
}

.cn-modal-header-content {
  flex: 1;
  min-width: 0;
}

.cn-modal-eyebrow {
  margin: 0 0 var(--cn-space-1);
  color: var(--cn-teal-dark);
  font-size: var(--cn-text-xs);
  font-weight: 900;
  letter-spacing: 0.05em;
  line-height: 1.3;
  text-transform: uppercase;
}

.cn-modal-title {
  margin: 0;
  color: var(--cn-navy);
  font-size: var(--cn-text-xl);
  font-weight: 800;
  line-height: 1.25;
}

.cn-modal-description {
  margin: var(--cn-space-2) 0 0;
  color: var(--cn-text-secondary);
  font-size: var(--cn-text-sm);
  line-height: 1.55;
}


/* ==========================================================
   CLOSE BUTTON
   ========================================================== */

.cn-modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  margin: -0.35rem -0.35rem 0 0;
  padding: 0;
  border: 0;
  border-radius: var(--cn-radius-md);
  color: var(--cn-text-muted);
  background: transparent;
  cursor: pointer;
  transition:
    color var(--cn-transition-fast),
    background-color var(--cn-transition-fast);
}

.cn-modal-close:hover {
  color: var(--cn-navy);
  background: var(--cn-bg-muted);
}

.cn-modal-close:focus-visible {
  outline: 3px solid rgba(28, 167, 163, 0.18);
  outline-offset: 2px;
}

.cn-modal-close i {
  color: inherit;
  font-size: 1rem;
}


/* ==========================================================
   BODY
   ========================================================== */

.cn-modal-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: var(--cn-space-5) var(--cn-space-4);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.cn-modal-body > :first-child {
  margin-top: 0;
}

.cn-modal-body > :last-child {
  margin-bottom: 0;
}


/* ==========================================================
   FOOTER / ACTIONS
   ========================================================== */

.cn-modal-footer {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  gap: var(--cn-space-3);
  padding: var(--cn-space-4);
  border-top: 1px solid var(--cn-border-soft);
  background: var(--cn-bg);
}

.cn-modal-footer .cn-btn {
  width: 100%;
}


/* ==========================================================
   BODY SECTIONS
   ========================================================== */

.cn-modal-section {
  display: grid;
  gap: var(--cn-space-3);
}

.cn-modal-section + .cn-modal-section {
  margin-top: var(--cn-space-6);
}

.cn-modal-section-title {
  margin: 0;
  color: var(--cn-navy);
  font-size: var(--cn-text-lg);
  font-weight: 800;
}

.cn-modal-section-description {
  margin: 0;
  color: var(--cn-text-secondary);
  font-size: var(--cn-text-sm);
  line-height: 1.6;
}


/* ==========================================================
   ICON / STATUS MODAL
   ========================================================== */

.cn-modal-status {
  display: grid;
  justify-items: center;
  gap: var(--cn-space-4);
  padding: var(--cn-space-2) 0;
  text-align: center;
}

.cn-modal-status-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  color: var(--cn-teal);
  background: var(--cn-teal-soft);
}

.cn-modal-status-icon i {
  color: inherit;
  font-size: 1.5rem;
}

.cn-modal-status-icon-success {
  color: var(--cn-success);
  background: var(--cn-success-bg);
}

.cn-modal-status-icon-warning {
  color: var(--cn-warning);
  background: var(--cn-warning-bg);
}

.cn-modal-status-icon-danger,
.cn-modal-status-icon-error {
  color: var(--cn-danger);
  background: var(--cn-danger-bg);
}

.cn-modal-status-icon-info {
  color: var(--cn-info);
  background: var(--cn-info-bg);
}


/* ==========================================================
   CONFIRMATION MODAL
   ========================================================== */

.cn-modal-confirm .cn-modal-body {
  padding-top: var(--cn-space-6);
  padding-bottom: var(--cn-space-6);
}

.cn-modal-confirm-message {
  max-width: 520px;
  margin: 0 auto;
  color: var(--cn-text-secondary);
  font-size: var(--cn-text-base);
  line-height: 1.65;
  text-align: center;
}


/* ==========================================================
   DANGER MODAL
   ========================================================== */

.cn-modal-danger .cn-modal-header {
  border-bottom-color: rgba(185, 28, 28, 0.12);
}

.cn-modal-danger .cn-modal-eyebrow {
  color: var(--cn-danger);
}


/* ==========================================================
   FULL-SCREEN MOBILE MODAL
   Optional modifier
   ========================================================== */

.cn-modal-full-mobile {
  align-items: stretch;
}

.cn-modal-full-mobile .cn-modal-dialog {
  max-height: none;
  height: 100dvh;
  margin: 0;
  border: 0;
  border-radius: 0;
}


/* ==========================================================
   SCROLL LOCK
   Add to <body> while modal is open
   ========================================================== */

body.cn-modal-open {
  overflow: hidden;
}


/* ==========================================================
   LOADING STATE
   ========================================================== */

.cn-modal-loading {
  display: grid;
  place-items: center;
  min-height: 180px;
  gap: var(--cn-space-3);
  color: var(--cn-text-secondary);
  text-align: center;
}

.cn-modal-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--cn-border-soft);
  border-top-color: var(--cn-teal);
  border-radius: 50%;
  animation: cn-modal-spin 0.75s linear infinite;
}

@keyframes cn-modal-spin {
  to {
    transform: rotate(360deg);
  }
}


/* ==========================================================
   TABLET+
   Convert bottom sheet into centered dialog
   ========================================================== */

@media (min-width: 576px) {
  .cn-modal {
    align-items: center;
    padding: var(--cn-space-6);
  }

  .cn-modal-dialog {
    width: min(100%, 640px);
    max-height: min(88dvh, 760px);
    margin: auto;
    border-bottom: 1px solid var(--cn-border-soft);
    border-radius: var(--cn-radius-xl);
  }

  .cn-modal-dialog-sm {
    max-width: 440px;
  }

  .cn-modal-dialog-md {
    max-width: 640px;
  }

  .cn-modal-dialog-lg {
    max-width: 820px;
  }

  .cn-modal-dialog-xl {
    max-width: 1040px;
  }

  .cn-modal-handle {
    display: none;
  }

  .cn-modal-header {
    padding: var(--cn-space-5) var(--cn-space-6);
  }

  .cn-modal-body {
    padding: var(--cn-space-6);
  }

  .cn-modal-footer {
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    padding: var(--cn-space-4) var(--cn-space-6);
  }

  .cn-modal-footer .cn-btn {
    width: auto;
  }

  .cn-modal-footer--split {
    justify-content: space-between;
  }

  .cn-modal-full-mobile {
    align-items: center;
  }

  .cn-modal-full-mobile .cn-modal-dialog {
    width: min(100%, 820px);
    height: auto;
    max-height: min(88dvh, 820px);
    margin: auto;
    border: 1px solid var(--cn-border-soft);
    border-radius: var(--cn-radius-xl);
  }
}


/* ==========================================================
   LARGE DESKTOP
   ========================================================== */

@media (min-width: 1024px) {
  .cn-modal {
    padding: var(--cn-space-8);
  }
}


/* ==========================================================
   REDUCED MOTION
   ========================================================== */

@media (prefers-reduced-motion: reduce) {
  .cn-modal-close {
    transition: none;
  }

  .cn-modal-spinner {
    animation: none;
    border-top-color: var(--cn-border-soft);
  }
}
