/* ==================================================================
   Modal windows (js/modal.js). Shared by the style guide and the app.

   Component aliases resolve to style guide tokens first, then to the app's
   web/src/styles.css tokens, then to literal dark defaults, so the same
   file works on both kinds of page.
   ================================================================== */
.modal {
  --m-bg: var(--surface-panel, var(--panel, #181B21));
  --m-fg: var(--text-primary, var(--ink, #E8E9EC));
  --m-muted: var(--text-muted, var(--muted, #9BA1AB));
  --m-line: var(--border-default, var(--line, #2A2E36));
  --m-line-subtle: var(--border-subtle, var(--line, #22252C));
  --m-inset: var(--surface-inset, var(--panel-soft, #0B0C0F));
  --m-hover: var(--surface-hover, var(--panel-soft, #1D2027));
  --m-danger: var(--danger, var(--bad, #FF8A8A));
  --m-danger-bg: var(--danger-bg, rgb(255 138 138 / 0.10));
  --m-danger-line: var(--danger-line, rgb(255 138 138 / 0.30));
  --m-focus: var(--focus-color, var(--accent, #89A3F2));
  --m-overlay: var(--surface-overlay, rgb(5 6 8 / 0.72));
  --m-shadow: var(--shadow-3, 0 16px 40px rgb(0 0 0 / 0.45));
  --m-radius: var(--radius-xl, 14px);
  --m-pad: var(--space-6, 24px);
  --m-gap: var(--space-4, 16px);
  --m-gutter: 16px;

  width: min(var(--modal-width, 520px), calc(100vw - 2 * var(--m-gutter)));
  max-width: none;
  max-height: min(var(--modal-height, 85vh), calc(100dvh - 2 * var(--m-gutter)));
  height: var(--modal-height, fit-content);
  padding: 0;
  translate: var(--modal-offset, 0px) var(--modal-offset, 0px);
  background: var(--m-bg);
  color: var(--m-fg);
  border: 1px solid var(--m-line);
  border-radius: var(--m-radius);
  box-shadow: var(--m-shadow);
  font: inherit;
}
.modal[open] { display: flex; flex-direction: column; }
.modal.modal-full { height: calc(100dvh - 2 * var(--m-gutter)); translate: none; }
.modal::backdrop { background: var(--m-overlay); }
/* Stacked modals: lighter backdrops so the page does not go black. */
.modal:not([data-depth="0"])::backdrop { background: rgb(5 6 8 / 0.35); }

.modal-head { display: flex; align-items: flex-start; gap: var(--space-3, 12px); padding: var(--m-pad) var(--m-pad) 0; }
.modal-title { flex: 1; margin: 0; font-size: var(--text-2xl, 1.25rem); font-weight: 600; line-height: 1.3; }
.modal-close {
  flex-shrink: 0; width: 32px; height: 32px; margin: -4px -8px 0 0;
  display: inline-grid; place-items: center; padding: 0;
  border: 0; border-radius: var(--radius-md, 8px); background: transparent; color: var(--m-muted); cursor: pointer;
}
.modal-close:hover { background: var(--m-hover); color: var(--m-fg); }
.modal-close:focus-visible { outline: 2px solid var(--m-focus); outline-offset: 2px; }
.modal-close svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

.modal-body { flex: 1; min-height: 0; overflow: auto; display: grid; align-content: start; gap: var(--m-gap); padding: var(--space-4, 16px) var(--m-pad) var(--m-pad); }
.modal-text { margin: 0; color: var(--m-muted); font-size: var(--text-md, 0.9rem); white-space: pre-line; }
.modal-error { padding: var(--space-3, 12px); border-radius: var(--radius-md, 8px); background: var(--m-danger-bg); border: 1px solid var(--m-danger-line); color: var(--m-danger); font-size: var(--text-sm, 0.85rem); }
.modal-error[hidden] { display: none; }

.modal-foot { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--space-2, 8px); padding: var(--space-4, 16px) var(--m-pad); border-top: 1px solid var(--m-line-subtle); }

.modal-field { display: grid; gap: 6px; }
.modal-field-label { font-size: var(--text-sm, 0.85rem); font-weight: 500; }
.modal-input {
  width: 100%; min-height: var(--control-h, 2.4rem); padding: 0 var(--space-3, 12px);
  background: var(--m-inset); color: var(--m-fg); font: inherit;
  border: 1px solid var(--m-line); border-radius: var(--radius-md, 8px);
}
.modal-input:focus-visible { outline: none; border-color: var(--m-focus); box-shadow: 0 0 0 1px var(--m-focus); }
.modal-input[aria-invalid="true"] { border-color: var(--m-danger); }

@media (prefers-reduced-motion: no-preference) {
  .modal[open] { animation: modal-in var(--dur-med, 220ms) var(--ease, ease-out); }
}
@keyframes modal-in { from { opacity: 0; scale: 0.98; } }
