/* Shared alert, confirmation, and destructive-action dialog.
   Markup and behavior live in public/js/ui/modal.js. */
html.app-modal-open,
body.app-modal-open {
  overflow: hidden;
  overscroll-behavior: none;
}

.app-modal-layer[hidden] { display: none; }
.app-modal-layer {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: grid;
  place-items: center;
  padding: 24px;
  overscroll-behavior: contain;
  touch-action: none;
}
.app-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 30, 60, 0.62);
}
.app-modal-card {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100%, 460px);
  max-height: calc(100dvh - 48px);
  overflow: hidden;
  overscroll-behavior: contain;
  touch-action: pan-y;
  padding: 28px;
  border: var(--border-thin);
  border-radius: var(--radius-xl);
  background: var(--color-surface);
  box-shadow: var(--shadow-lg);
}
.app-modal-copy {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}
.app-modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  background: transparent;
}
.app-modal-title {
  margin: 0;
  padding-right: 42px;
  color: var(--navy);
  font-family: var(--font-display);
  font-size: 1.3rem;
  line-height: 1.25;
}
.app-modal-message {
  margin: 12px 0 0;
  color: var(--color-text-muted);
  white-space: pre-line;
}
.app-modal-section + .app-modal-section {
  margin-top: 16px;
}
.app-modal-section-title,
.app-modal-section-body {
  display: block;
}
.app-modal-section-title {
  color: var(--color-text);
  font-weight: 700;
}
.app-modal-actions {
  flex: 0 0 auto;
  display: flex;
  /* Two labels too long for one row wrap, the action above Cancel, rather
     than overflowing past the card's left padding. */
  flex-wrap: wrap-reverse;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 24px;
}
.app-modal-actions [hidden] { display: none; }
/* A three-way choice stacks its full-width buttons at every width: the main
   action first, then the alternative, then Cancel. Three equal columns
   clipped real labels ("Reload newer scenario", "Restore changes") at
   desktop and in-between widths, and a stack makes the main action obvious. */
.app-modal-layer.is-choice .app-modal-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}
.app-modal-layer.is-choice:not(.is-two-choice) .app-modal-confirm {
  grid-row: 1;
}
.app-modal-layer.is-choice:not(.is-two-choice) .app-modal-secondary {
  grid-row: 2;
}
.app-modal-layer.is-two-choice .app-modal-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.app-modal-layer.is-two-choice .app-modal-card {
  width: min(100%, 460px);
}

/* A dialog with its own controls (modal.form), such as "Copy … to" (#265). */
.app-modal-body[hidden] { display: none; }
.app-modal-body { margin-top: 12px; }
.copy-picker-all {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: var(--border-thin);
  font-weight: 700;
  cursor: pointer;
}
.app-switch {
  appearance: none;
  flex: 0 0 auto;
  position: relative;
  width: 44px;
  height: 26px;
  margin: 0;
  border-radius: 13px;
  background: var(--color-line);
  cursor: pointer;
  transition: background-color 0.15s ease;
}
.app-switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-surface);
  box-shadow: 0 1px 2px rgba(15, 30, 60, 0.3);
  transition: transform 0.15s ease;
}
.app-switch:checked { background: var(--color-action); }
.app-switch:checked::after { transform: translateX(18px); }
.app-switch:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}
.copy-picker-list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.copy-picker-target {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: 12px;
  align-items: center;
  padding: 10px 0;
  border-bottom: var(--border-thin);
  cursor: pointer;
}
.copy-picker-target input {
  grid-row: span 2;
  width: 20px;
  height: 20px;
  margin: 0;
}
.copy-picker-name {
  color: var(--color-text);
  font-weight: 600;
  overflow-wrap: anywhere;
}
.copy-picker-change {
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}
.copy-picker-target.is-selected .copy-picker-change { color: var(--color-text); }

@media (max-width: 480px) {
  .app-modal-layer {
    align-items: end;
    padding: 12px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  .app-modal-card {
    max-height: calc(100dvh - 24px - env(safe-area-inset-bottom, 0px));
    padding: 22px;
  }
  .app-modal-close {
    top: 10px;
    right: 10px;
  }
  /* Two buttons share a row while both labels fit with their padding. A
     longer label, such as "Replace and save My plan", wraps the row instead
     of clipping: wrap-reverse puts the action above Cancel, matching the
     stacked choice layout below. */
  .app-modal-actions {
    flex-wrap: wrap-reverse;
    width: 100%;
  }
  .app-modal-actions .btn {
    flex: 1 1 auto;
  }
  /* Two choices side by side leave about 155px each on a phone, too little
     for the longest label with its padding, so they stack too. */
  .app-modal-layer.is-two-choice .app-modal-actions {
    grid-template-columns: minmax(0, 1fr);
  }
  .app-modal-layer.is-two-choice .app-modal-confirm {
    grid-row: 1;
  }
  .app-modal-layer.is-two-choice .app-modal-secondary {
    grid-row: 2;
  }
  /* A form is a full-screen sheet on phones, so a long plan list scrolls
     with the actions always in reach. */
  .app-modal-layer.is-form {
    padding: 0;
  }
  .app-modal-layer.is-form .app-modal-card {
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    padding-bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  }
  .app-modal-layer.is-form .app-modal-copy {
    flex: 1 1 auto;
  }
}
