/* Constrain intrinsic widths in the existing New API key editor. */
#api-key-form,
#api-key-form section,
#api-key-form [data-slot='form-item'],
#api-key-form [data-slot='form-item'] > div,
#api-key-form [role='group'] {
  box-sizing: border-box;
  min-width: 0;
  max-width: 100%;
}

#api-key-form button[role='combobox'] {
  min-width: 0;
  max-width: 100%;
}

#api-key-form button[role='combobox'] > span:not([aria-hidden='true']) {
  min-width: 0;
  max-width: 100%;
  flex: 1 1 0%;
}

#api-key-form button[role='combobox'] > span > span:first-child {
  min-width: 0;
  flex: 1 1 0%;
}

/* Mobile uses the main form scroll area and comfortable touch targets. */
@media (max-width: 639px) {
  [data-slot='sheet-content']:has(#api-key-form) {
    box-sizing: border-box;
    width: 100%;
    max-width: 100vw;
    height: 100dvh;
    min-width: 0;
  }

  #api-key-form,
  #api-key-form section,
  #api-key-form [data-slot='form-item'],
  #api-key-form [data-slot='form-item'] > div,
  #api-key-form [role='group'] {
    box-sizing: border-box;
    min-width: 0;
    max-width: 100%;
  }

  #api-key-form {
    overflow-x: hidden;
    scrollbar-gutter: stable;
    padding-bottom: 20px;
  }

  #api-key-form input,
  #api-key-form textarea {
    font-size: 16px;
  }

  #api-key-form button[role='combobox'] {
    min-width: 0;
    max-width: 100%;
    min-height: 48px;
    height: auto;
    gap: 8px;
    padding: 10px 12px;
    white-space: normal;
  }

  #api-key-form button[role='combobox'] > span:not([aria-hidden='true']) {
    min-width: 0;
    max-width: 100%;
    flex: 1 1 0%;
    align-items: flex-start;
  }

  #api-key-form button[role='combobox'] > span > span:first-child {
    min-width: 0;
    flex: 1 1 0%;
  }

  #api-key-form
    button[role='combobox']
    > span
    > span:last-child:not(:first-child) {
    display: block;
    flex: 0 0 auto;
  }

  #api-key-form button[role='combobox'] > span > span:first-child > span {
    white-space: normal;
    overflow-wrap: anywhere;
    line-height: 1.5;
  }

  #api-key-form
    button[role='combobox']
    > span
    > span:first-child
    > span
    + span {
    margin-top: 3px;
    font-size: 12px;
  }

  /* The picker is portalled outside the form; scope it to the open editor. */
  body:has(#api-key-form)
    [data-slot='popover-content']:has([data-slot='command-list']) {
    width: min(var(--anchor-width), calc(100vw - 24px));
    max-width: calc(100vw - 24px);
    max-height: min(60dvh, var(--available-height, 60dvh));
    min-height: 0;
    overflow: hidden;
  }

  body:has(#api-key-form) [data-slot='popover-content'] [data-slot='command'] {
    height: auto;
    min-height: 0;
    max-height: inherit;
  }

  body:has(#api-key-form)
    [data-slot='popover-content']
    [data-slot='command-list'] {
    max-height: min(
      18rem,
      calc(60dvh - 3rem),
      calc(var(--available-height, 60dvh) - 3rem)
    );
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }

  body:has(#api-key-form) [data-slot='popover-content'] input {
    font-size: 16px;
  }

  body:has(#api-key-form) [data-slot='command-item'] {
    min-width: 0;
    min-height: 48px;
    gap: 8px;
    padding: 10px 8px;
  }

  body:has(#api-key-form) [data-slot='command-item'] > svg:last-child {
    display: none;
  }

  body:has(#api-key-form) [data-slot='command-item'] > span.flex-1 {
    min-width: 0;
  }

  body:has(#api-key-form) [data-slot='command-item'] > span.flex-1 > span {
    white-space: normal;
    overflow-wrap: anywhere;
    line-height: 1.5;
  }

  body:has(#api-key-form)
    [data-slot='command-item']
    > span.flex-1
    > span
    + span {
    display: -webkit-box;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  #api-key-form [role='group'] > div:first-child {
    min-width: 0;
    flex-wrap: wrap;
    gap: 8px;
  }

  #api-key-form [role='group'] > div:first-child > p {
    min-width: 120px;
    flex: 1 1 120px;
    line-height: 1.6;
  }

  /* Keep every inherited group visible in the form's main scroll area. */
  #api-key-form [data-slot='global-auto-order'] {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    max-height: none;
    gap: 6px;
    overflow: visible;
  }

  #api-key-form [data-slot='global-auto-order'] > li {
    min-width: 0;
    width: 100%;
  }

  #api-key-form [data-slot='global-auto-order-connector'] {
    display: none;
  }

  #api-key-form [data-slot='global-auto-order-chip'] {
    box-sizing: border-box;
    width: 100%;
    min-width: 0;
    min-height: 40px;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
  }

  #api-key-form [data-slot='global-auto-order-index'] {
    width: 22px;
    height: 22px;
    font-size: 11px;
  }

  #api-key-form [data-slot='global-auto-order-name'] {
    min-width: 0;
    max-width: none;
    flex: 1 1 0%;
    white-space: normal;
    overflow-wrap: anywhere;
    font-size: 13px;
    line-height: 20px;
  }

  #api-key-form [role='switch'] {
    flex-shrink: 0;
  }

  #api-key-form [data-slot='form-description'] {
    overflow-wrap: anywhere;
    -webkit-line-clamp: unset;
  }

  #api-key-form [role='group'] ul > li button {
    min-width: 36px;
    min-height: 36px;
  }

  [data-slot='sheet-content']:has(#api-key-form) [data-slot='sheet-footer'] {
    flex-shrink: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  [data-slot='sheet-content']:has(#api-key-form)
    [data-slot='sheet-footer']
    button {
    min-height: 44px;
  }
}
