@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * COMBOBOX
 * Single-select with synchronous local filtering.
 * ======================================== */

  .cinder-combobox {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-1-5);
  }

  .cinder-combobox__label {
    font-size: var(--cinder-text-xs);
    font-weight: var(--cinder-font-medium);
    color: var(--cinder-text-default);
  }

  .cinder-combobox__label[data-disabled] {
    color: var(--cinder-text-disabled);
  }

  .cinder-combobox__control {
    position: relative;
  }

  .cinder-combobox__input {
    display: block;
    width: 100%;
    min-height: var(--cinder-control-height-md);
    padding-block: var(--cinder-space-1);
    /* 40px at the end clears the shared chevron (18px, 12px in from the end), as Select's does. */
    padding-inline: var(--cinder-space-3) var(--cinder-space-10);
    font-size: var(--cinder-text-sm);
    font-family: inherit;
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-default);
    background: var(--cinder-control-background);
    border: 1px solid var(--cinder-control-border);
    border-radius: var(--cinder-control-radius);
    appearance: none;
    transition:
      border-color var(--cinder-duration-fast) var(--cinder-ease-standard),
      box-shadow var(--cinder-duration-fast) var(--cinder-ease-standard);
  }
  /* Size: the shared control height for the step, with block padding that
     keeps the line box inside it. */
  .cinder-combobox__input[data-cinder-size='sm'] {
    min-height: var(--cinder-control-height-sm);
    padding-block: var(--cinder-space-0-5);
  }

  .cinder-combobox__input[data-cinder-size='lg'] {
    min-height: var(--cinder-control-height-lg);
    padding-block: var(--cinder-space-2);
  }

  /* A composed field follows Input's appearance contract: comfortable density defaults its medium
     control to the 48px filled treatment, while an explicit outlined appearance keeps the base
     frame. The visible 1px control boundary remains in the base rule above. */
  .cinder-combobox__input[data-cinder-appearance='filled'],
  .cinder-combobox__input:where([data-density='comfortable'] *):not(
      [data-cinder-appearance='outlined']
    ) {
    background: var(--cinder-surface-inset);
    border-radius: var(--cinder-radius-lg);
  }

  .cinder-combobox__input:where([data-density='comfortable'] *):not(
      [data-cinder-appearance='outlined']
    )[data-cinder-size='md'] {
    min-height: var(--cinder-control-height-lg);
  }

  .cinder-combobox__input::placeholder {
    color: var(--cinder-text-subtle);
  }

  /* The trigger of a picker is a button: it reads like the input and fills the field. */
  .cinder-combobox__trigger {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0;
    text-align: start;
    cursor: pointer;
  }

  /* Two lines when the chosen option has a description: its name, then the sentence. */
  .cinder-combobox__trigger-heading {
    display: flex;
    align-items: baseline;
    gap: var(--cinder-space-2);
    min-inline-size: 0;
  }

  .cinder-combobox__trigger-value {
    flex: 1 1 auto;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The name is the heading of a two-line field only when a description follows it; a name that
     stands alone (a time zone) reads as the plain value of the field. */
  .cinder-combobox__trigger-heading:has(+ .cinder-combobox__trigger-description)
    .cinder-combobox__trigger-value {
    font-weight: var(--cinder-font-semibold);
  }

  .cinder-combobox__trigger-value[data-cinder-placeholder] {
    color: var(--cinder-text-muted);
  }

  .cinder-combobox[data-cinder-monospace] .cinder-combobox__trigger-value[data-cinder-placeholder] {
    /* Center the code face inside the full-height picker value. */
    align-content: center;
    font-family: var(--cinder-font-mono);
  }

  .cinder-combobox__trigger-description {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-muted);
  }

  .cinder-combobox__trigger:disabled {
    cursor: not-allowed;
  }

  .cinder-combobox__trigger:disabled .cinder-combobox__trigger-value,
  .cinder-combobox__trigger:disabled .cinder-combobox__trigger-description {
    color: var(--cinder-text-disabled);
  }

  /* The decorative chevron is the shared field chevron (`styles/components/_field-chevron.css`),
     at the same size and inset as Select's and MultiSelect's. The control keeps it in every
     state: loading, error, open. */
  .cinder-combobox__input:disabled + .cinder-combobox__chevron {
    color: var(--cinder-text-disabled);
  }

  /* Sticky-hover suppression on touch. */
  @media (hover: hover) {
    .cinder-combobox__input:hover:not(:disabled):not(:focus-visible) {
      border-color: var(--cinder-control-border-hover);
    }
  }

  .cinder-combobox__input:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    /* Ring replaces the border while focused rather than stacking on it — the two
       together read as one heavier shape than either was designed to be. */
    border-color: transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  .cinder-combobox__input[data-cinder-appearance='filled']:focus-visible,
  .cinder-combobox__input:where([data-density='comfortable'] *):not(
      [data-cinder-appearance='outlined']
    ):focus-visible {
    background: var(--cinder-control-background);
  }

  .cinder-combobox__input[aria-invalid='true'] {
    border-color: var(--cinder-control-border-invalid);
  }

  /* COR-1498: the tight ring sits directly on the control edge, so a visible
     border inside it would read as one heavier 3px shape (the COR-1072
     problem). Keep the border transparent while focused, in every state,
     and keep hover from repainting it. */
  .cinder-combobox__input[aria-invalid='true']:focus-visible {
    border-color: transparent;
  }

  .cinder-combobox__input:disabled {
    background: var(--cinder-control-background-disabled);
    color: var(--cinder-text-disabled);
    border-color: var(--cinder-control-border-disabled);
    cursor: not-allowed;
  }

  /* COR-1981: the `role="listbox"` element the input's `aria-controls` names is the scroller.
     axe's `scrollable-region-focusable` exempts a scroller that is itself a combobox's popup and
     fails a presentational wrapper that scrolls with nothing in the tab order; a tab stop on
     the scroller would instead compete with the field, which keeps focus. The search field, the
     load error and the footer sit outside the listbox, so they stay put while it scrolls and
     are never inside a `role="listbox"` that may hold only options.

     The list stays 16rem tall; the panel's own viewport limit stays in force, so on a short
     screen the panel is no taller than the viewport and the list still scrolls. */
  .cinder-combobox__panel.cinder-popover {
    margin: var(--cinder-space-1) 0 0 0;
    padding: var(--cinder-space-1);
    min-inline-size: 0;
    max-inline-size: min(32rem, calc(100vw - var(--cinder-space-4)));
    overflow: hidden;
  }

  .cinder-combobox__content {
    display: flex;
    flex-direction: column;
    min-block-size: 0;
    max-block-size: min(28rem, calc(100vh - var(--cinder-space-8)));
  }

  /* Keep the anchored panel's inline cap and clear the docked sheet's bottom safe area. */
  .cinder-combobox__panel.cinder-popover[data-cinder-sheet='docked'] {
    /* The anchored panel's inline cap would otherwise outrank the sheet's `max-width: none` and
       stop the sheet 16px short of the right edge (COR-2922). */
    max-inline-size: none;
    padding-block-end: max(var(--cinder-space-1), env(safe-area-inset-bottom));
  }

  .cinder-combobox__listbox {
    margin: 0;
    padding: 0;
    list-style: none;
    min-block-size: 0;
    max-block-size: 16rem;
    overflow-y: auto;
  }

  /* The picker's rows are two lines tall, so its list shows a little more before it scrolls. */
  .cinder-combobox__content[data-cinder-presentation='picker'] .cinder-combobox__listbox {
    max-block-size: 20rem;
  }

  .cinder-combobox__listbox[hidden] {
    display: none;
  }

  /* The picker's search field sits above the options with its match count beside it. */
  .cinder-combobox__search {
    flex: none;
    display: flex;
    align-items: center;
    gap: var(--cinder-space-2);
    padding: var(--cinder-space-1) var(--cinder-space-1) var(--cinder-space-2);
    border-block-end: 1px solid var(--cinder-border-muted);
    margin-block-end: var(--cinder-space-1);
  }

  .cinder-combobox__search-field {
    flex: 1 1 auto;
    min-inline-size: 0;
  }

  .cinder-combobox__match-count {
    flex: none;
    font-size: var(--cinder-text-xs);
    font-variant-numeric: tabular-nums;
    color: var(--cinder-text-muted);
  }

  /* The list failed to load: what failed, what still works, and a way to try again below the
     text, above the options that did load. It is a message in the panel, not a banner, so the
     title reads in neutral ink and only the alert role carries the urgency. */
  .cinder-combobox__load-error {
    flex: none;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--cinder-space-2);
    padding: var(--cinder-space-2);
  }

  .cinder-combobox__load-error:has(~ .cinder-combobox__listbox:not([hidden])),
  .cinder-combobox__load-error:has(~ .cinder-combobox__empty) {
    margin-block-end: var(--cinder-space-1);
    border-block-end: 1px solid var(--cinder-border-muted);
  }

  .cinder-combobox__load-error-text {
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-snug);
  }

  .cinder-combobox__load-error-title {
    font-size: var(--cinder-text-sm);
    font-weight: var(--cinder-font-semibold);
    color: var(--cinder-text-default);
  }

  .cinder-combobox__load-error-description {
    color: var(--cinder-text-muted);
  }

  /* Content under the options that stays put while they scroll: "No match? Create one". */
  /* The line under a list the cap shortened: how many are shown, and how to see the rest. */
  .cinder-combobox__cap-note {
    flex: none;
    padding: var(--cinder-space-2) var(--cinder-space-2) var(--cinder-space-1);
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-muted);
  }

  .cinder-combobox__footer {
    flex: none;
    margin-block-start: var(--cinder-space-1);
    padding: var(--cinder-space-2) var(--cinder-space-2) var(--cinder-space-1);
    border-block-start: 1px solid var(--cinder-border-muted);
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-muted);
  }

  /* A link in the footer reads as an accent word, not as underlined body text. */
  .cinder-combobox__footer a {
    color: var(--cinder-accent-text);
    font-weight: var(--cinder-font-semibold);
    text-decoration: none;
  }

  .cinder-combobox__footer a:hover {
    text-decoration: underline;
  }

  /* Skeleton's status line, drawn alone under the field: its placeholder bars are the field. */
  .cinder-combobox__still-loading .cinder-skeleton-lines {
    display: none;
  }

  /* The shared row primitive supplies padding, centering, and interaction states. */
  .cinder-combobox__option {
    cursor: pointer;
    font-size: var(--cinder-text-sm);
    color: var(--cinder-text-default);
  }

  @media (any-pointer: coarse) {
    .cinder-combobox__option {
      min-block-size: var(--cinder-touch-target-min);
    }
  }

  .cinder-combobox__option-avatar {
    flex: 0 0 auto;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: var(--cinder-radius-full);
    object-fit: cover;
  }

  .cinder-combobox__option-text {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
  }

  /* The label, with a raw identifier beside it when the option has one. An identifier that does not
     fit beside the label drops under it, at its inline start, so the label keeps whole words. */
  .cinder-combobox__option-heading {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0 var(--cinder-space-2);
    min-inline-size: 0;
  }

  .cinder-combobox__option-label {
    flex: 1 1 auto;
    min-inline-size: 0;
    line-height: inherit;
    overflow-wrap: anywhere;
  }

  .cinder-combobox[data-cinder-monospace] input.cinder-combobox__input,
  .cinder-combobox[data-cinder-monospace] input.cinder-combobox__input::placeholder,
  .cinder-combobox[data-cinder-monospace] .cinder-combobox__trigger-value,
  .cinder-combobox__content[data-cinder-monospace] .cinder-combobox__option-label {
    font-family: var(--cinder-font-mono);
  }

  .cinder-combobox__content[data-cinder-monospace] .cinder-combobox__option-suffix {
    font-family: var(--cinder-font-sans);
  }

  /* On a line of its own, an identifier longer than the panel is wide shrinks and wraps anywhere
     rather than overflowing it. */
  .cinder-combobox__option-code {
    flex: 0 1 auto;
    min-inline-size: 0;
    overflow-wrap: anywhere;
    font-family: var(--cinder-font-mono);
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-default);
  }

  /* Muted words after the name on its own line (an option's suffix), plain even in the chosen row. */
  .cinder-combobox__option-suffix {
    font-weight: var(--cinder-font-normal);
    color: var(--cinder-text-muted);
  }

  .cinder-combobox__option-description {
    font-size: var(--cinder-text-xs);
    color: var(--cinder-text-muted);
    line-height: var(--cinder-leading-snug);
    overflow-wrap: anywhere;
  }

  /* A reason the row is wrong: the words carry it, the colour agrees. */
  .cinder-combobox__option-description[data-cinder-tone='danger'] {
    color: var(--cinder-status-danger-text);
  }

  .cinder-combobox__option-meta {
    flex: none;
    min-inline-size: 5.25rem;
    text-align: end;
    font-size: var(--cinder-text-xs);
    color: var(--cinder-text-muted);
  }

  /* A row with lines under the label keeps its trailing text on the label's line. */
  .cinder-combobox__option:has(.cinder-combobox__option-description) {
    align-items: flex-start;
  }

  .cinder-combobox__option:has(.cinder-combobox__option-description) .cinder-combobox__option-meta {
    line-height: var(--cinder-leading-normal);
  }

  .cinder-combobox__option:has(.cinder-combobox__option-description)
    .cinder-combobox__option-label {
    font-weight: var(--cinder-font-semibold);
  }

  /* The chosen option: the accent tint and a check, the same current-item look the sidebar uses.
     The keyboard cursor keeps its inset ring on top. */
  .cinder-combobox__listbox .cinder-combobox__option[aria-selected='true'] {
    background: var(--cinder-accent-background);
  }

  .cinder-combobox__option[aria-selected='true'] .cinder-combobox__option-label {
    font-weight: var(--cinder-font-semibold);
    color: var(--cinder-accent-text);
  }

  .cinder-combobox__option-check {
    flex: none;
    align-self: center;
    color: var(--cinder-accent-text);
  }

  .cinder-combobox__option-check:not([data-cinder-visible]) {
    visibility: hidden;
  }

  /* After the text, the check sits at the trailing edge. */
  .cinder-combobox__option-text ~ .cinder-combobox__option-check {
    margin-inline-start: auto;
  }

  .cinder-combobox__option[aria-disabled='true'] {
    cursor: not-allowed;
  }

  .cinder-combobox__option[aria-disabled='true']
    .cinder-combobox__option-description:not([data-cinder-tone]),
  .cinder-combobox__option[aria-disabled='true'] .cinder-combobox__option-meta {
    color: var(--cinder-text-muted);
  }

  .cinder-combobox__empty {
    padding: var(--cinder-space-3);
    font-size: var(--cinder-text-sm);
    color: var(--cinder-text-muted);
    text-align: center;
  }

  .cinder-combobox__empty-status {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .cinder-combobox__description {
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-muted);
    margin: 0;
  }

  .cinder-combobox__error {
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-status-danger-text);
    margin: 0;
  }

  /* The shared `_form-field-error.css` partial (imported by the required
     `cinder/styles` base, CIN-315) keeps the always-present live region out
     of layout when no error is active — no per-component copy needed here. */

  /* Forced Colors Mode: restore outline-based focus ring for the combobox input
 * because box-shadow is ignored in Windows High Contrast Mode. */
  @media (forced-colors: active) {
    .cinder-combobox__input:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: 3px;
    }
  }
}
