/* ==========================================================================
   Modals — collision-safe modal shell family
   ========================================================================== */

/*
  Modals owns overlay/shell anatomy only.

  This file intentionally uses the md-modal namespace and must not target or
  redefine existing live selectors such as ds-modal-shell, ds-modal-backdrop,
  ds-modal-head, ds-modal-body, ds-modal-actions or ds-modal-close.

  Modals consumes:
  - Typography foundation for title/support/meta roles
  - Colour foundation for surfaces, text and borders
  - Controls for buttons, dropdowns, async status, retry and skeleton
  - Questions for inputs, textareas and answer assemblies
  - Helper Notes for semantic notes
  - Review/Sanity/Project content components where feature-owned content appears
*/

#modalLayer {
  --surface-02: #161817;
}

.rebuild-modal-stack {
  display: grid;
  gap: var(--space-3);
}

.rebuild-modal-muted {
  margin: 0;
  color: var(--helper);
  font-size: 0.86rem;
  line-height: 1.52;
}

.rebuild-modal-status {
  margin-top: var(--space-1);
}

/* Overlay wrapper */
.md-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-6, 32px);
  background: rgba(0, 0, 0, 0.44);
  backdrop-filter: blur(1px);
}

.md-modal-backdrop.is-visible {
  display: flex;
}

/* Base panel */
.md-modal {
  width: min(100%, 560px);
  max-height: min(88vh, 960px);
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border: 1px solid var(--border-02, rgba(255, 255, 255, 0.14));
  border-radius: var(--radius-xl, 16px);
  background: var(--surface-02, #161817);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.38);
  color: var(--text-primary, #f2eee8);
  overflow: hidden;
}

/* Local modal size scale.
   Width belongs to the modal panel, not to the backdrop or consuming feature. */
.md-modal--small {
  width: min(100%, 440px);
}

.md-modal--medium {
  width: min(100%, 560px);
}

.md-modal--large {
  width: min(100%, 720px);
}

.md-modal--wide {
  width: min(100%, 880px);
}

/* Non-overlay / gate panel variant.
   The consuming page owns placement; modal owns shell proportions. */
.md-modal--gate {
  position: relative;
  max-height: none;
}

/* Header */
.md-modal__header {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-4, 16px);
  align-items: start;
  padding: var(--space-5, 24px) var(--space-5, 24px) var(--space-4, 16px);
  border-bottom: 1px solid var(--border-01, rgba(255, 255, 255, 0.08));
}

.md-modal__header-copy {
  min-width: 0;
  display: grid;
  gap: var(--space-2, 8px);
}

/* Consumes TY-03 question-component-heading */
.md-modal__title {
  margin: 0;
  color: var(--text-primary, #f2eee8);
  font-family: var(--font, "DM Sans", sans-serif);
  font-size: 17px;
  font-weight: 600;
  line-height: 1.35;
}

/* Consumes TY-02 helper-copy */
.md-modal__support {
  margin: 0;
  max-width: 72ch;
  color: var(--helper-text, #a9b6bd);
  font-family: var(--font, "DM Sans", sans-serif);
  font-size: 13px;
  font-weight: 400;
  line-height: 1.52;
}

/* Modal-owned close affordance candidate.
   Runtime owns close behaviour. */
.md-modal__close {
  appearance: none;
  -webkit-appearance: none;
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border-01, rgba(255, 255, 255, 0.08));
  border-radius: var(--radius-pill, 999px);
  background: rgba(255, 255, 255, 0.035);
  color: var(--text-muted, rgba(255, 255, 255, 0.68));
  cursor: pointer;
}

.md-modal__close:hover,
.md-modal__close:focus-visible {
  color: var(--text-primary, #f2eee8);
  border-color: var(--border-02, rgba(255, 255, 255, 0.14));
  background: rgba(255, 255, 255, 0.055);
}

.md-modal__close:focus-visible {
  outline: 2px solid var(--tab-active-underline, #ffc000);
  outline-offset: 2px;
}

.md-modal__close svg {
  width: 14px;
  height: 14px;
  display: block;
}

.md-modal__close path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
}

/* Body */
.md-modal__body {
  min-width: 0;
  display: grid;
  gap: var(--space-4, 16px);
  padding: var(--space-5, 24px);
  overflow: auto;
}

.md-modal__body--no-scroll {
  overflow: visible;
}

/* Modal-owned divider for separating modal-local content sections.
   Use sparingly; do not replace fieldset/question/card dividers. */
.md-modal__divider {
  height: 1px;
  width: 100%;
  background: var(--border-01, rgba(255, 255, 255, 0.08));
}

/* Footer */
.md-modal__footer {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3, 12px);
  padding: var(--space-4, 16px) var(--space-5, 24px) var(--space-5, 24px);
  border-top: 1px solid var(--border-01, rgba(255, 255, 255, 0.08));
}

.md-modal__footer-actions {
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2, 8px);
  flex-wrap: wrap;
  margin-left: auto;
}

.md-modal__footer-danger {
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: var(--space-2, 8px);
  margin-right: auto;
}

/* Optional local label role for modal-owned section labels.
   Feature-owned labels can consume the same TY-01 role without using this class. */
.md-modal__section-label {
  margin: 0;
  color: var(--guidance-blue, #6ea8ff);
  font-family: var(--font, "DM Sans", sans-serif);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.25;
  text-transform: uppercase;
}

/* Responsive behaviour */
@media (max-width: 720px) {
  .md-modal-backdrop {
    align-items: stretch;
    padding: var(--space-4, 16px);
  }

  .md-modal {
    width: 100%;
    max-height: calc(100vh - (var(--space-4, 16px) * 2));
  }

  .md-modal__header,
  .md-modal__body,
  .md-modal__footer {
    padding-left: var(--space-4, 16px);
    padding-right: var(--space-4, 16px);
  }

  .md-modal__footer {
    display: grid;
    align-items: stretch;
  }

  .md-modal__footer-actions,
  .md-modal__footer-danger {
    width: 100%;
    justify-content: stretch;
    margin-left: 0;
    margin-right: 0;
  }

  .md-modal__footer-actions > *,
  .md-modal__footer-danger > * {
    width: 100%;
    justify-content: center;
  }
}
