@layer components {
  /* Lead modal (LEAD-01/LEAD-02, D3-01, D3-16; 03-UI-SPEC "Lead modal", "Form controls", "States").
     Lives in the components layer: the dialog, its form partial and the no-JS page share it.
     Mobile-first: <768 px the native <dialog> is full-screen; >=768 px it is a centred 650 px card
     on a scrim. Only Phase-2 tokens plus --c-error / --c-backdrop; no shadows (SCOPE §4.2).
     The .btn (incl. :disabled, [aria-busy], focus ring) comes from components.css. */

  /* --- Dialog shell: mobile full-screen (every UA dialog value overridden, RESEARCH Pitfall 8) --- */
  .lead-modal {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
    padding: var(--space-7) var(--gutter) var(--space-5);
    background: var(--c-white);
    color: var(--c-black);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .lead-modal::backdrop {
    background: var(--c-backdrop);
  }

  .lead-modal[open] {
    animation: lead-modal-in 200ms ease-out;
  }

  .lead-modal[open]::backdrop {
    animation: lead-modal-fade 200ms ease-out;
  }

  /* D5-15 i (03-UI-REVIEW item 7): <768 the success block sits vertically centred in the
     full-screen sheet and «Закрити» spans the content width. `[open]` stays on the selector:
     a closed dialog must never get a display value. Reset at >=768 below (these rules come
     first so the reset wins at equal specificity). */
  .lead-modal[open]:has(.lead-success) {
    display: grid;
    align-content: center;
  }

  .lead-success .btn {
    width: 100%;
  }

  /* --- Tablet / desktop: centred card on the scrim --- */
  @media (min-width: 768px) {
    .lead-modal {
      width: min(100% - 2 * var(--gutter), var(--container-narrow));
      /* fit-content, not auto: the UA's dialog:modal sets inset-block: 0, so `auto` would
         stretch the card to max-height with empty space under the submit (03-09 visual). */
      height: fit-content;
      max-height: calc(100dvh - 2 * var(--space-5));
      margin: auto;
      padding: var(--space-7);
      border-radius: var(--radius-btn);
    }

    .lead-modal[open] {
      animation-name: lead-modal-in-lift;
    }

    /* D5-15 i: >=768 unchanged (the card's height already hugs the success block). */
    .lead-modal[open]:has(.lead-success) {
      display: block;
    }

    .lead-success .btn {
      width: auto;
    }
  }

  /* D5-15 k (03-UI-REVIEW item 3): short desktop screens get tighter padding, so the form fits
     without an inner scroll at 1280x800. A separate block after the main 768 one (Pitfall 4). */
  @media (min-width: 768px) and (max-height: 840px) {
    .lead-modal {
      padding: var(--space-5);
    }
  }

  /* Open motion only; closing is instant (03-UI-SPEC "Open / close motion"). */
  @keyframes lead-modal-in {
    from {
      opacity: 0;
    }

    to {
      opacity: 1;
    }
  }

  @keyframes lead-modal-in-lift {
    from {
      opacity: 0;
      transform: translateY(16px);
    }

    to {
      opacity: 1;
      transform: none;
    }
  }

  @keyframes lead-modal-fade {
    from {
      opacity: 0;
    }

    to {
      opacity: 1;
    }
  }

  /* The page behind neither scrolls nor shifts while the dialog is open. */
  html:has(dialog[open]) {
    overflow: hidden;
    scrollbar-gutter: stable;
  }

  /* --- Close button: 44x44, top-right inside the dialog's scroll box --- */
  .lead-modal__close {
    position: absolute;
    top: var(--space-2);
    right: var(--space-2);
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-btn);
    background: transparent;
    color: var(--c-black);
    cursor: pointer;
    transition: background-color 150ms ease;
  }

  .lead-modal__close svg {
    width: 24px;
    height: 24px;
  }

  @media (hover: hover) {
    .lead-modal__close:hover {
      background: var(--c-grey-100);
    }
  }

  .lead-modal__close:focus-visible {
    outline: 2px solid var(--c-black);
    outline-offset: 2px;
  }

  /* Admin-edited copy can hold long unbroken words: break them inside the modal and the no-JS
     page instead of overflowing (UI Considerations "long-text", 03-09 visual at 320/768).
     `anywhere` (not break-word) also shrinks flex/grid items such as the button label. */
  .lead-form,
  .lead-form :is(h1, h2, p, label, a, span),
  .lead-success,
  .lead-success :is(h1, h2, p, span) {
    overflow-wrap: anywhere;
  }

  /* --- Title: Heading role, clear of the close button ---
     D5-15 l (03-UI-REVIEW item 8): the box hugs the text (fit-content), and the clearance is a
     margin, not padding, so the focus ring (drawn around the border box) stays off the close
     button at 320. */
  .lead-form__title {
    width: fit-content;
    max-width: 100%;
    margin: 0;
    margin-inline-end: var(--space-7);
    font-size: var(--fs-heading);
    line-height: var(--lh-heading);
    font-weight: 700;
    text-wrap: balance;
  }

  /* The title is a programmatic focus target (tabindex="-1"): the Phase-2 black ring for
     keyboard users, no UA (blue) ring after a pointer click (03-09 visual). */
  .lead-form__title:focus {
    outline: none;
  }

  .lead-form__title:focus-visible {
    outline: 2px solid var(--c-black);
    outline-offset: 2px;
  }

  /* --- Vertical rhythm (03-UI-SPEC): title → 16 → intro → 32 → field → 24 → field → 24 → field
     → 24 → consent → (24 → alert → 24 | 32) → submit. Inside a field: label → 8 → input → 8 →
     hint → 8 → error. Field labels carry the 24; the first one after the title/intro gets 32
     (works for flat markup and for a wrapper whose first child is the label). --- */
  .lead-form__intro {
    margin-top: var(--space-3);
  }

  .lead-form label {
    display: block;
    margin-top: var(--space-4);
    font-size: var(--fs-small);
    line-height: var(--lh-small);
    font-weight: 700;
    color: var(--c-black);
  }

  .lead-form :is(.lead-form__title, .lead-form__intro) + :is(label, .lead-form__field) {
    margin-top: var(--space-5);
  }

  /* --- Text inputs (visible labels above; 16 px text avoids iOS zoom) --- */
  .lead-form input:is([type="text"], [type="tel"]) {
    display: block;
    width: 100%;
    min-height: 48px;
    margin-top: var(--space-2);
    padding: 0 var(--space-3);
    border: 1px solid var(--c-muted);
    border-radius: var(--radius-btn);
    background: var(--c-white);
    color: var(--c-black);
    font: 400 var(--fs-body) / var(--lh-body) var(--font);
    appearance: none;
    transition: border-color 150ms ease;
  }

  @media (hover: hover) {
    .lead-form input:is([type="text"], [type="tel"]):hover {
      border-color: var(--c-black);
    }
  }

  /* Ring on any focus, not only :focus-visible: keyboard entry is always intended. */
  .lead-form input:is([type="text"], [type="tel"]):focus {
    border-color: var(--c-black);
    outline: 2px solid var(--c-black);
    outline-offset: 2px;
  }

  /* After hover/focus so the error border stays; the focus ring stays black. */
  .lead-form input:is([type="text"], [type="tel"])[aria-invalid="true"] {
    border-color: var(--c-error);
  }

  .lead-form input:autofill {
    box-shadow: inset 0 0 0 100px var(--c-white);
    -webkit-text-fill-color: var(--c-black);
    color: var(--c-black);
  }

  .lead-form__hint,
  .lead-form__error {
    margin-top: var(--space-2);
    font-size: var(--fs-small);
    line-height: var(--lh-small);
    font-weight: 400;
  }

  .lead-form__hint {
    color: var(--c-muted);
  }

  .lead-form__error {
    color: var(--c-error);
  }

  /* --- Consent row: 20 px checkbox + wrapping Small text in column 2 --- */
  .lead-form__consent {
    display: grid;
    grid-template-columns: 20px 1fr;
    column-gap: var(--space-2);
    align-items: start;
    margin-top: var(--space-4);
  }

  .lead-form__consent label {
    grid-column: 2;
    margin-top: 0;
    font-weight: 400;
    cursor: pointer;
  }

  .lead-form__consent .lead-form__error {
    grid-column: 2;
  }

  /* Error slot rendered right after the consent row instead of inside it: align with the text. */
  .lead-form__consent + .lead-form__error {
    padding-inline-start: calc(20px + var(--space-2));
  }

  .lead-form__consent a {
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 4px;
  }

  .lead-form input[type="checkbox"] {
    grid-column: 1;
    width: 20px;
    height: 20px;
    margin: 0;
    accent-color: var(--c-black);
    cursor: pointer;
  }

  .lead-form input[type="checkbox"]:focus-visible {
    outline: 2px solid var(--c-black);
    outline-offset: 2px;
  }

  .lead-form input[type="checkbox"][aria-invalid="true"]:not(:focus) {
    outline: 2px solid var(--c-error);
    outline-offset: 2px;
  }

  /* D5-15 h (03-UI-REVIEW item 2): while a resubmit is in flight, the previous response's field
     errors step aside, so a corrected value is not shown as rejected. The hook is the existing
     `form.dataset.submitting` from lead-modal.js (L-0039: replaces the UI-SPEC `data-pending`,
     no JS change). The server stays the only validator (D3-03): its 400/200 swap replaces the
     markup, and the `finally` branch removes the flag when nothing was swapped. */
  .lead-form[data-submitting] .lead-form__error {
    visibility: hidden;
  }

  .lead-form[data-submitting] input:is([type="text"], [type="tel"])[aria-invalid="true"] {
    border-color: var(--c-muted);
  }

  .lead-form[data-submitting] input[type="checkbox"][aria-invalid="true"] {
    outline: none;
  }

  /* --- Honeypot: off-screen, never display:none (some bots skip hidden fields) --- */
  .lead-form__hp {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
  }

  /* --- Form-alert (server error / rate limit), role="alert" --- */
  .lead-form__alert {
    margin-top: var(--space-4);
    padding: var(--space-3);
    border-left: 4px solid var(--c-error);
    border-radius: 0 var(--radius-btn) var(--radius-btn) 0;
    background: var(--c-grey-100);
    color: var(--c-black);
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    font-weight: 400;
  }

  .lead-form__alert a {
    /* The phone number never breaks across lines (03-09 visual, 375 px). */
    white-space: nowrap;
    font-weight: 700;
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 4px;
  }

  /* --- Submit: .btn states come from components.css (disabled colour, cursor: progress) --- */
  .lead-form__submit {
    width: 100%;
    margin-top: var(--space-5);
  }

  .lead-form:has(.lead-form__alert) .lead-form__submit {
    margin-top: var(--space-4);
  }

  /* Busy: the 16 px ring replaces the arrow while aria-busy="true". */
  .btn__spinner {
    display: none;
    flex: none;
    width: 16px;
    height: 16px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: lead-spin 800ms linear infinite;
  }

  .btn[aria-busy="true"] .btn__spinner {
    display: inline-block;
  }

  .btn[aria-busy="true"] .btn__arrow {
    display: none;
  }

  @keyframes lead-spin {
    to {
      transform: rotate(360deg);
    }
  }

  /* --- Success block: title → 16 → text → 24 → action --- */
  .lead-success p {
    margin-top: var(--space-3);
  }

  .lead-success .btn {
    margin-top: var(--space-4);
  }

  /* --- No-JS page: the 404 shell metrics, content left-aligned in the narrow container --- */
  .lead-page {
    min-height: 100svh;
    padding-block: var(--space-8);
  }

  @media (min-width: 1024px) {
    .lead-page {
      padding-block: var(--space-10);
    }
  }

  .lead-page__logo {
    display: inline-block;
  }

  .lead-page__logo img {
    width: 200px;
    height: 16px;
  }

  @media (min-width: 768px) {
    .lead-page__logo img {
      width: 225px;
      height: 18px;
    }
  }

  .lead-page :is(.lead-form, .lead-success) {
    margin-top: var(--space-7);
  }

  /* --- Reduced motion: no open animation, the spinner stays still (the label carries the state) --- */
  @media (prefers-reduced-motion: reduce) {
    .lead-modal[open],
    .lead-modal[open]::backdrop {
      animation: none;
    }

    .btn__spinner {
      animation: none;
    }

    .lead-modal__close,
    .lead-form input {
      transition: none;
    }
  }
}
