/* Customer form (customers#new, #edit, leads/customers#new).
   One scrolling sheet instead of tabs: each section is a row with its legend on
   the left and a 6-column field grid on the right, so field widths follow the
   data (UF and Número narrow, Endereço wide). A sticky rail indexes the sections
   on wide screens; the actions stay pinned to the bottom of the viewport. */
@scope (.customer-form) {
  :scope {
    --_border: var(--color-border-muted);
    --_ink: var(--color-text-heading);
    --_muted: var(--color-text-secondary);
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-2xl);
    inline-size: 100%;
    container: customer-form / inline-size;
  }

  [hidden] {
    display: none !important;
  }

  /* ── Rail ── */
  .customer-form__rail {
    display: none;
  }

  .customer-form__rail ol {
    display: grid;
    gap: var(--space-2xs);
    padding: 0;
    margin: 0;
    list-style: none;
    border-inline-start: 1px solid var(--_border);
  }

  .customer-form__rail a {
    display: block;
    margin-inline-start: -1px;
    padding: var(--space-xs) var(--space-md);
    border-inline-start: 2px solid transparent;
    color: var(--_muted);
    font-size: var(--text-body-sm);
    text-decoration: none;
    transition: color 0.15s ease, border-color 0.15s ease;
  }

  .customer-form__rail a:hover,
  .customer-form__rail a:focus-visible {
    color: var(--_ink);
  }

  .customer-form__rail a[aria-current] {
    border-inline-start-color: var(--color-primary);
    color: var(--color-primary);
    font-weight: var(--font-semibold);
  }

  /* ── Sheet ── */
  .customer-form__body {
    display: grid;
    min-inline-size: 0;
    border: 1px solid var(--_border);
    border-radius: var(--radius-xl);
    background: var(--color-surface);
    box-shadow: var(--shadow-xs);
  }

  .form__errors {
    margin: var(--space-lg) var(--space-lg) 0;
  }

  .customer-form__section {
    display: grid;
    gap: var(--space-md) var(--space-2xl);
    padding: var(--space-xl) var(--space-lg);
    scroll-margin-block-start: calc(var(--topbar-height) + var(--space-lg));
  }

  .customer-form__section + .customer-form__section {
    border-block-start: 1px solid var(--_border);
  }

  .customer-form__legend h2 {
    color: var(--_ink);
    font-size: var(--text-body);
    font-weight: var(--font-semibold);
  }

  .customer-form__legend p {
    margin-top: var(--space-2xs);
    color: var(--_muted);
    font-size: var(--text-body-sm);
    text-wrap: pretty;
  }

  .customer-form__fields {
    display: grid;
    gap: var(--space-lg);
    min-inline-size: 0;
  }

  .customer-form__grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: var(--space-md);
    min-inline-size: 0;
  }

  .customer-form__grid > *,
  .customer-form__grid > turbo-frame > * {
    grid-column: 1 / -1;
    min-inline-size: 0;
  }

  /* ── Lead routing switch ── */
  .customer-form__switch {
    display: flex;
    align-items: flex-start;
    gap: var(--space-md);
    cursor: pointer;
  }

  .customer-form__switch input {
    appearance: none;
    flex: 0 0 auto;
    position: relative;
    inline-size: 2.25rem;
    block-size: 1.25rem;
    margin-block-start: 0.125rem;
    border-radius: var(--radius-full);
    background: var(--color-border);
    cursor: pointer;
    transition: background-color 0.15s ease;
  }

  .customer-form__switch input::after {
    content: "";
    position: absolute;
    inset-block-start: 2px;
    inset-inline-start: 2px;
    inline-size: calc(1.25rem - 4px);
    aspect-ratio: 1;
    border-radius: 50%;
    background: var(--white);
    box-shadow: var(--shadow-xs);
    transition: translate 0.15s ease;
  }

  .customer-form__switch input:checked {
    background: var(--color-primary);
  }

  .customer-form__switch input:checked::after {
    translate: 1rem 0;
  }

  .customer-form__switch input:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
  }

  .customer-form__switch strong {
    display: block;
    color: var(--_ink);
    font-size: var(--text-body-sm);
    font-weight: var(--font-medium);
  }

  .customer-form__switch small {
    display: block;
    color: var(--_muted);
    font-size: var(--text-xs);
  }

  /* ── Shipping addresses ── */
  .customer-form__addresses {
    display: grid;
    gap: var(--space-md);
  }

  .customer-form__addresses:empty {
    display: none;
  }

  .shipping-address-card {
    margin: 0;
  }

  .customer-form__add {
    justify-self: start;
  }

  /* ── Actions ── */
  .customer-form__actions {
    position: sticky;
    inset-block-end: 0;
    z-index: 2;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: end;
    gap: var(--space-sm);
    padding: var(--space-md) var(--space-lg);
    border-block-start: 1px solid var(--_border);
    border-end-start-radius: var(--radius-xl);
    border-end-end-radius: var(--radius-xl);
    background: color-mix(in oklab, var(--color-surface) 92%, transparent);
    backdrop-filter: blur(6px);
  }

  .customer-form__required-note {
    margin-inline-end: auto;
    color: var(--_muted);
    font-size: var(--text-xs);
  }

  /* ── Widths ── */
  @container customer-form (width >= 36rem) {
    .customer-form__grid > .span-1, .customer-form__grid > turbo-frame > .span-1 { grid-column: span 2; }
    .customer-form__grid > .span-2, .customer-form__grid > turbo-frame > .span-2 { grid-column: span 3; }
    .customer-form__grid > .span-3, .customer-form__grid > turbo-frame > .span-3 { grid-column: span 3; }
  }

  @container customer-form (width >= 48rem) {
    .customer-form__section {
      grid-template-columns: 14rem minmax(0, 1fr);
      padding: var(--space-xl);
    }

    .customer-form__grid > .span-1, .customer-form__grid > turbo-frame > .span-1 { grid-column: span 1; }
    .customer-form__grid > .span-2, .customer-form__grid > turbo-frame > .span-2 { grid-column: span 2; }
    .customer-form__grid > .span-3, .customer-form__grid > turbo-frame > .span-3 { grid-column: span 3; }
    .customer-form__grid > .span-4, .customer-form__grid > turbo-frame > .span-4 { grid-column: span 4; }

    /* Address cards sit inside the field column: keep them two-up. */
    .customer-form__grid--compact > * { grid-column: span 3 !important; }
  }

  /* A container cannot query itself, so the rail keys off the viewport and
     stays out of modals. */
  @media (width >= 84rem) {
    :scope:not(.dialog *) {
      grid-template-columns: 11rem minmax(0, 1fr);
      align-items: start;
    }

    :scope:not(.dialog *) .customer-form__rail {
      display: block;
      position: sticky;
      inset-block-start: calc(var(--topbar-height) + var(--space-xl));
    }
  }

  @media (width < 40rem) {
    .customer-form__actions {
      justify-content: stretch;
    }

    .customer-form__actions .button {
      flex: 1;
    }

    .customer-form__required-note {
      flex-basis: 100%;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .customer-form__switch input,
    .customer-form__switch input::after,
    .customer-form__rail a {
      transition: none;
    }
  }
}
