/*
 * A modal <dialog> opened with showModal() or an invoker command
 * (command="show-modal"). base.css keeps inline <dialog open> static.
 *
 * The <dialog> itself is a transparent, full-viewport overlay that scrolls;
 * the visible card is .dialog__panel inside it, which grows with its content.
 * So a long dialog runs past the bottom of the screen and scrolls like the
 * page does, instead of scrolling inside a fixed box.
 */
@layer components {
  .dialog:modal {
    position: fixed;
    inset: 0;
    inline-size: 100%;
    max-inline-size: none;
    block-size: 100%;
    max-block-size: none;
    margin: 0;
    padding: var(--space-8) 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 0;
    border-radius: 0;
    background: transparent;
  }

  .dialog::backdrop {
    background: var(--color-backdrop);
  }

  /* The page underneath stops scrolling; only the overlay's scrollbar shows. */
  html:has(.dialog:modal) {
    overflow: hidden;
  }

  /* Fills the overlay behind the panel, so clicking beside the panel closes. */
  .dialog__dismiss {
    position: fixed;
    inset: 0;
    inline-size: 100%;
    block-size: 100%;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    cursor: default;

    &:hover {
      background: transparent;
    }
  }

  .dialog__panel {
    position: relative;
    inline-size: 80vw;
    margin-inline: auto;
    border: var(--border);
    border-radius: var(--radius-l);
    background: var(--color-paper);
    box-shadow: var(--shadow-overlay);
  }

  /* Scrolls away with the panel; it does not pin to the viewport. */
  .dialog__header {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-7);
    border-block-end: var(--border);
    border-start-start-radius: var(--radius-l);
    border-start-end-radius: var(--radius-l);
    background: var(--color-paper);
  }

  .dialog__close {
    margin-inline-start: auto;
  }

  .dialog__meta {
    font: var(--text-small);
    color: var(--color-muted);
  }

  /* A short dialog (one form): narrow, and set down from the top. */
  .dialog__panel--narrow {
    inline-size: min(40rem, 80vw);
    margin-block-start: 12vh;
  }

  .dialog__title {
    font: var(--text-title);
    color: var(--color-ink);
  }

  .dialog__body {
    display: grid;
    gap: var(--space-4);
    padding: var(--space-6) var(--space-7) var(--space-7);

    & > * {
      margin: 0;
    }
  }

  /* Every form in a dialog on the budget page (#239 acceptance): one field
     per line, its label above its input, inputs as wide as the panel. A
     checkbox or radio keeps its label beside it. A field hidden by its
     [hidden] attribute (the select under a combobox) stays hidden. */
  .dialog-form {
    display: grid;
    gap: var(--space-4);

    & :is(p, fieldset, .form-errors) {
      margin: 0;
      max-inline-size: none;
    }

    & fieldset {
      display: grid;
      gap: var(--space-3);
    }

    & label {
      display: block;
      margin-block-end: var(--space-1);
      font: var(--text-body-strong);
    }

    & :is(input:not([type="checkbox"], [type="radio"], [type="hidden"], [type="submit"]), select, textarea):not([hidden]) {
      display: block;
      inline-size: 100%;
      max-inline-size: none;
    }

    & :is(input[type="checkbox"], input[type="radio"]) + label {
      display: inline;
      margin: 0;
      font: var(--text-body);
    }

    & textarea {
      field-sizing: content;
    }
  }
}
