@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;

@layer cinder.components {
  /* ========================================
   * FILTER BAR
   * ======================================== */

  /* One row. Width is handled by the layout, not by flowing: `wide` draws the
   * segments, the facet chips and the search field; `search-folded` folds the search field into an icon
   * button first and keeps the segments; `medium` folds the segments into a select and
   * the search field into an icon button; `narrow` keeps the select and a Filter menu. The layout
   * is chosen in script (data-cinder-layout), because the controls that disappear move into a
   * menu, which CSS alone cannot do. Only the narrow form, the last step, wraps its Filter button
   * under the select when the two cannot share a line. */
  .cinder-filter-bar {
    container-type: inline-size;
    container-name: cinder-filter-bar;
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
    /* The one height every control in the row is painted at: a medium Button's, in any density
     * and on any pointer. A coarse pointer raises `--cinder-control-height-*` to 44px, and the
     * comfortable density lifts a medium Select to 48px; neither reaches the row. The 44px touch
     * target is each control's hit area, which takes no layout space. */
    --_cinder-filter-bar-control-height: var(--_cinder-painted-height-md);
  }

  /* The root is the last place focus can land when the control that held it was removed. */
  .cinder-filter-bar:focus {
    outline: none;
  }

  .cinder-filter-bar__row {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: var(--cinder-space-2);
    min-inline-size: 0;
  }

  /* Every control in the row shares one height, so segments, selects, chips and the search field
   * line up. */
  .cinder-filter-bar__row > * {
    flex: 0 0 auto;
  }

  /* The search field is the one control that gives way: it takes what is left of the row and
   * stops growing at 20rem. At the end of the row it is pushed to the inline end. */
  .cinder-filter-bar__row > .cinder-filter-bar__search {
    flex: 1 1 11rem;
    min-inline-size: 0;
    max-inline-size: 20rem;
  }

  /* The first paint. The server has no viewport and measures nothing, so it draws the wide form;
   * until the client sets `data-cinder-measured` (its layout effect has run) that form wraps, and
   * the search field takes its own line when the row is full instead of shrinking to nothing, so
   * the bar never overflows at any width. The status options wrap inside their group when they
   * cannot fit on one line. A bar with the attribute keeps the one-row declarations above. */
  .cinder-filter-bar:not([data-cinder-measured]) .cinder-filter-bar__row {
    flex-wrap: wrap;
  }

  /* Keep the segmented control's focus-ring padding inside the unmeasured row. Its usual
   * negative margins would otherwise let a full-width control extend past both edges. */
  .cinder-filter-bar:not([data-cinder-measured])
    .cinder-filter-bar__row
    > .cinder-segmented-control-container {
    margin-inline: 0;
    max-inline-size: 100%;
  }

  .cinder-filter-bar:not([data-cinder-measured])
    .cinder-filter-bar__row
    > .cinder-segmented-control-container
    .cinder-segmented-control {
    flex-wrap: wrap;
    max-inline-size: 100%;
  }

  .cinder-filter-bar:not([data-cinder-measured])
    .cinder-filter-bar__row
    > .cinder-filter-bar__search {
    flex: 1 1 min(11rem, 100%);
    min-inline-size: min(11rem, 100%);
  }

  /* The icon button sits inside the popover's display-contents wrapper, so it is reached by
   * descent rather than as a direct child. */
  .cinder-filter-bar[data-cinder-search-position='end']
    .cinder-filter-bar__row
    > .cinder-filter-bar__search,
  .cinder-filter-bar[data-cinder-search-position='end'] .cinder-filter-bar__search-button {
    margin-inline-start: auto;
  }

  /* A Select is a form field, which fills its container. In the row each one takes the width of its
   * value, so the selects sit beside the chips instead of pushing them out of the bar. */
  .cinder-filter-bar__row > .cinder-form-field.cinder-filter-bar__segments-select,
  .cinder-filter-bar__facet > .cinder-form-field.cinder-filter-bar__select {
    inline-size: auto;
  }

  .cinder-filter-bar__segments-select {
    min-inline-size: 11rem;
  }

  /* Every Select the row draws, the segments' and the facets', is painted at the row's height. The
   * density rule that lifts a medium Select to the `lg` step has fewer classes than this selector,
   * so it loses on a fine pointer; a coarse pointer reaches the same height through the Select's
   * own compact-touch rule, which also draws its 44px hit area. */
  .cinder-filter-bar
    .cinder-form-field
    .cinder-select[data-cinder-presentation='listbox'].select-size-medium {
    min-height: var(--_cinder-filter-bar-control-height);
  }

  /* In the narrow form the select shares the row with the Filter button and takes the rest. It is
   * never narrower than its value (or its prefix, when that is wider): when the two do not fit
   * side by side, as at 200% text on a phone, the Filter button moves under the select instead of
   * the value being cut (WCAG 1.4.4). */
  .cinder-filter-bar[data-cinder-layout='narrow'] .cinder-filter-bar__row {
    flex-wrap: wrap;
  }

  /* A selected choice chip is deliberately kept in the medium form so its remove control remains
   * available. On a narrow phone it cannot share a line with the status select, so it takes the
   * next line instead of being clipped beyond the viewport. Unselected chips still fit beside the
   * select; the search control remains available at the end of the wrapped line. */
  @container cinder-filter-bar (max-inline-size: 24rem) {
    .cinder-filter-bar[data-cinder-layout='medium'] .cinder-filter-bar__row {
      flex-wrap: wrap;
    }

    /* A search field on its own first-paint line starts with the other controls. */
    .cinder-filter-bar:not([data-cinder-measured])
      .cinder-filter-bar__row
      > .cinder-filter-bar__search {
      flex-basis: 100%;
      max-inline-size: 100%;
      margin-inline-start: 0;
    }
  }

  .cinder-filter-bar[data-cinder-layout='narrow']
    .cinder-filter-bar__row
    > .cinder-form-field.cinder-filter-bar__segments-select {
    flex: 1 1 auto;
    min-inline-size: min-content;
  }

  /* The folded select's prefix ("Status:") gives way before its value. The prefix, value and the
   * gap between them share one line that wraps in the order value, prefix (`order`), drawn
   * reversed so the prefix still reads first, and only the first line shows: a trigger too narrow
   * for both drops the prefix whole, and the value is truncated last. The prefix is decorative;
   * the label names the control, so nothing is lost to assistive technology. */
  .cinder-filter-bar .cinder-select__text:has(> .cinder-select__prefix) {
    flex-flow: row-reverse wrap;
    justify-content: flex-end;
    align-content: flex-start;
    max-block-size: 1lh;
    overflow: hidden;
  }

  .cinder-filter-bar .cinder-select__text > .cinder-select__prefix {
    order: 1;
  }

  .cinder-filter-bar__facet {
    display: inline-flex;
    align-items: center;
    min-inline-size: 0;
  }

  .cinder-filter-bar__select {
    min-inline-size: 8rem;
  }

  /* A bar of selects and a search field alone has nothing to collapse. In a narrow container, such
   * as a list pane's filters, the controls stack in one column at the pane's width. */
  @container cinder-filter-bar (max-inline-size: 40rem) {
    .cinder-filter-bar:not([data-cinder-collapsible]) .cinder-filter-bar__row {
      flex-direction: column;
      align-items: stretch;
    }

    .cinder-filter-bar:not([data-cinder-collapsible]) .cinder-filter-bar__row > * {
      flex: 0 0 auto;
      max-inline-size: none;
      margin-inline-start: 0;
    }

    .cinder-filter-bar:not([data-cinder-collapsible]) .cinder-filter-bar__facet,
    .cinder-filter-bar:not([data-cinder-collapsible]) .cinder-filter-bar__select {
      display: flex;
      inline-size: 100%;
    }
  }

  /* ----------------------------------------
   * Facet chips: "+ Started by", "Started by: Chats, Goals"
   * ---------------------------------------- */

  .cinder-filter-bar__chip {
    display: inline-flex;
    align-items: center;
    box-sizing: border-box;
    min-block-size: var(--_cinder-filter-bar-control-height);
    border: 1px dashed var(--cinder-border-control);
    border-radius: var(--cinder-radius-full);
    background: transparent;
    color: var(--cinder-text-default);
    font-size: var(--cinder-text-sm);
    line-height: var(--cinder-leading-tight);
    white-space: nowrap;
    transition:
      background-color var(--cinder-duration-fast) var(--cinder-ease-standard),
      border-color var(--cinder-duration-fast) var(--cinder-ease-standard);
  }

  /* An applied facet is a tinted chip with a solid edge: the accent fill says it filters now. It
   * is drawn after the chips still waiting for a value (the bar orders them), unless the bar asks for
   * `appliedPlacement="leading"`, which draws the applied chips first. */
  .cinder-filter-bar__chip[data-cinder-applied] {
    border-style: solid;
    border-color: var(--cinder-accent-border);
    background: var(--cinder-accent-background);
    color: var(--cinder-accent-text);
  }

  .cinder-filter-bar__chip-trigger,
  .cinder-filter-bar__chip-remove {
    appearance: none;
    display: inline-flex;
    align-items: center;
    gap: var(--cinder-space-1);
    margin: 0;
    border: none;
    background: transparent;
    color: inherit;
    font: inherit;
    cursor: pointer;
  }

  .cinder-filter-bar__chip-trigger {
    align-self: stretch;
    padding-inline: var(--cinder-space-3);
    border-top-left-radius: var(--cinder-radius-full);
    border-bottom-left-radius: var(--cinder-radius-full);
    border-top-right-radius: var(--cinder-radius-full);
    border-bottom-right-radius: var(--cinder-radius-full);
  }

  .cinder-filter-bar__chip[data-cinder-applied] .cinder-filter-bar__chip-trigger {
    padding-inline-end: var(--cinder-space-1);
  }

  .cinder-filter-bar__chip[data-cinder-applied]
    .cinder-filter-bar__chip-trigger:where(
      :not([dir='rtl'], [dir='rtl'] *),
      [dir='rtl'] [dir='ltr'],
      [dir='rtl'] [dir='ltr'] *
    ) {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }

  .cinder-filter-bar__chip[data-cinder-applied]
    .cinder-filter-bar__chip-trigger:where(
      :is([dir='rtl'], [dir='rtl'] *):not([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
    ) {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }

  /* An applied filter with no facet has a label that is not a button. */
  .cinder-filter-bar__chip[data-cinder-static] .cinder-filter-bar__chip-trigger {
    cursor: default;
  }

  .cinder-filter-bar__chip-value {
    font-weight: var(--cinder-font-semibold);
  }

  .cinder-filter-bar__chip-remove {
    align-self: stretch;
    justify-content: center;
    padding-inline: var(--cinder-space-2);
  }

  .cinder-filter-bar__chip-remove:where(
    :not([dir='rtl'], [dir='rtl'] *),
    [dir='rtl'] [dir='ltr'],
    [dir='rtl'] [dir='ltr'] *
  ) {
    border-top-right-radius: var(--cinder-radius-full);
    border-bottom-right-radius: var(--cinder-radius-full);
  }

  .cinder-filter-bar__chip-remove:where(
    :is([dir='rtl'], [dir='rtl'] *):not([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
  ) {
    border-top-left-radius: var(--cinder-radius-full);
    border-bottom-left-radius: var(--cinder-radius-full);
  }

  /* Choice chips keep their compact painted face on touch. Their two independent actions still
   * each reach the shared 44px contact floor through an invisible hit area, rather than making a
   * dense filter row taller or letting the remove action become a narrow icon target. */
  @media (any-pointer: coarse) {
    .cinder-filter-bar__chip-trigger,
    .cinder-filter-bar__chip-remove {
      position: relative;
    }

    .cinder-filter-bar__chip-trigger::before,
    .cinder-filter-bar__chip-remove::before {
      content: '';
      position: absolute;
      inset-inline: 0;
      inset-block: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
    }

    /* The actions abut inside one chip. Grow them only toward their respective outer edges so
     * the remove target cannot steal the visible trailing edge of the chip trigger. */
    button.cinder-filter-bar__chip-trigger::before {
      margin-inline-start: min(0px, calc(100% - var(--cinder-touch-target-min)));
    }

    button.cinder-filter-bar__chip-remove::before {
      margin-inline-end: min(0px, calc(100% - var(--cinder-touch-target-min)));
    }

    /* The horizontal scroll container clips block overflow too. Give each option's
     * touch area room past the painted strip without making the row taller. */
    .cinder-filter-bar__row
      .cinder-segmented-control-container:has(> .cinder-filter-bar__segments) {
      --_cinder-filter-bar-segment-touch-room: max(
        var(--_cinder-segmented-control-ring-room),
        calc((var(--cinder-touch-target-min) - var(--_cinder-filter-bar-control-height) + 2px) / 2)
      );
      padding-block: var(--_cinder-filter-bar-segment-touch-room);
      margin-block: calc(-1 * var(--_cinder-filter-bar-segment-touch-room));
    }
  }

  @media (hover: hover) {
    .cinder-filter-bar__chip:not([data-cinder-disabled], [data-cinder-applied]):hover {
      background: var(--cinder-surface-hover);
    }
  }

  .cinder-filter-bar__chip[data-cinder-open] {
    border-color: var(--cinder-accent-border);
  }

  .cinder-filter-bar__chip-trigger:focus-visible,
  .cinder-filter-bar__chip-remove:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  @media (forced-colors: active) {
    .cinder-filter-bar__chip-trigger:focus-visible,
    .cinder-filter-bar__chip-remove:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
    }
  }

  .cinder-filter-bar__chip[data-cinder-disabled] {
    border-color: var(--cinder-control-border-disabled);
    color: var(--cinder-text-disabled);
  }

  .cinder-filter-bar__chip[data-cinder-disabled] button {
    cursor: not-allowed;
  }

  /* ----------------------------------------
   * Toggle facet: "Missed runs 1" with a dot
   * ---------------------------------------- */

  /* The chip's own toolbar density is 28px; the bar's controls are one height, so the toggle is
   * lifted to it. The extra classes outrank the chip's density rule, and the chip draws its own
   * 44px hit area on a coarse pointer. */
  .cinder-filter-bar .cinder-chip.cinder-filter-bar__toggle {
    min-block-size: var(--_cinder-filter-bar-control-height);
    padding-inline: var(--cinder-space-3);
    font-size: var(--cinder-text-sm);
  }

  .cinder-filter-bar__dot {
    display: block;
    inline-size: 0.5rem;
    block-size: 0.5rem;
    border-radius: 50%;
    background: var(--cinder-status-warning-indicator);
  }

  .cinder-filter-bar__dot[data-cinder-tone='danger'] {
    background: var(--cinder-status-danger-solid);
  }

  .cinder-filter-bar__dot[data-cinder-tone='info'] {
    background: var(--cinder-status-info-solid);
  }

  .cinder-filter-bar__dot[data-cinder-tone='success'] {
    background: var(--cinder-status-success-solid);
  }

  .cinder-filter-bar__dot[data-cinder-tone='neutral'] {
    background: var(--cinder-text-muted);
  }

  /* A pressed toggle is quiet: the tone's tint with its quiet border, and the dot as the one
   * leading mark. The chip's own pressed recipe (a solid status fill and a check) is the loudest
   * thing in a row of tinted chips, and the dot already says it is on, so the check is left out.
   * The extra classes outrank the chip's pressed rules. */
  .cinder-filter-bar .cinder-chip.cinder-filter-bar__toggle[aria-pressed='true'] {
    background: var(--cinder-status-warning-background);
    color: var(--cinder-status-warning-text);
    border-color: var(--cinder-status-warning-border);
    box-shadow: none;
  }

  .cinder-filter-bar .cinder-filter-bar__toggle[aria-pressed='true'] .cinder-chip__check {
    display: none;
  }

  /* ----------------------------------------
   * Choice popover: a titled list of rows with counts
   * ---------------------------------------- */

  .cinder-_floating-surface.cinder-filter-bar__choice-popover {
    overflow: auto;
  }

  .cinder-filter-bar__options {
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-2);
    min-inline-size: 14rem;
  }

  .cinder-filter-bar__options-title {
    margin: 0;
    font-size: var(--cinder-text-sm);
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-tight);
    color: var(--cinder-text-default);
  }

  /* A single choice list is a radio group, so its legend is the popover's title. */
  .cinder-filter-bar__options .cinder-radio-group__legend {
    font-size: var(--cinder-text-sm);
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-tight);
  }

  /* The Filter menu names the section with its own row, so the legend is only for assistive technology. */
  .cinder-filter-bar__options[data-cinder-title-hidden] .cinder-radio-group__legend {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }

  .cinder-filter-bar__option-list {
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-1);
    margin: 0;
    padding: 0;
    border: none;
  }

  .cinder-filter-bar__option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--cinder-space-4);
    min-block-size: var(--cinder-control-height-md);
  }

  .cinder-filter-bar__option > :first-child {
    flex: 1 1 auto;
    min-inline-size: 0;
  }

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

  .cinder-filter-bar__options-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--cinder-space-4);
    padding-block-start: var(--cinder-space-2);
    border-block-start: 1px solid var(--cinder-border-muted);
    color: var(--cinder-text-muted);
    font-size: var(--cinder-text-sm);
  }

  .cinder-filter-bar__options-clear {
    color: var(--cinder-accent-text);
  }

  /* ----------------------------------------
   * Narrow form: the Filter button and its menu
   * ---------------------------------------- */

  .cinder-filter-bar__menu-count {
    min-inline-size: 1.125rem;
    padding-inline: var(--cinder-space-1);
    border-radius: var(--cinder-radius-full);
    background: var(--cinder-accent-background);
    color: var(--cinder-accent-text);
    font-size: var(--cinder-text-xs);
    font-variant-numeric: tabular-nums;
    text-align: center;
  }

  .cinder-filter-bar__menu {
    display: flex;
    flex-direction: column;
    min-inline-size: min(16rem, 100%);
  }

  .cinder-filter-bar__menu-section + .cinder-filter-bar__menu-section {
    border-block-start: 1px solid var(--cinder-border-muted);
  }

  .cinder-filter-bar__menu-heading {
    display: flex;
  }

  .cinder-filter-bar__menu-heading > .cinder-filter-bar__chip-remove {
    flex: none;
    border-radius: var(--cinder-radius-sm);
  }

  @media (any-pointer: coarse) {
    .cinder-filter-bar__menu-heading > .cinder-filter-bar__chip-remove {
      /* Reserve the target's width so its hit area stays inside the popover. The shared chip
       * pseudo-element supplies any extra height without increasing the row height. */
      min-inline-size: var(--cinder-touch-target-min);
    }
  }

  .cinder-filter-bar__menu-row {
    appearance: none;
    display: flex;
    align-items: center;
    gap: var(--cinder-space-2);
    inline-size: 100%;
    min-inline-size: 0;
    min-block-size: var(--cinder-control-height-md);
    padding-inline: var(--cinder-space-2);
    border: none;
    border-radius: var(--cinder-radius-sm);
    background: transparent;
    color: var(--cinder-text-default);
    font: inherit;
    font-size: var(--cinder-text-sm);
    text-align: start;
    cursor: pointer;
  }

  @media (hover: hover) {
    button.cinder-filter-bar__menu-row:hover:not(:disabled) {
      background: var(--cinder-surface-hover);
    }
  }

  .cinder-filter-bar__menu-row:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  .cinder-filter-bar__menu-label {
    flex: 1 1 auto;
    min-inline-size: 0;
    overflow-wrap: anywhere;
  }

  .cinder-filter-bar__menu-row[data-cinder-static] {
    cursor: default;
  }

  .cinder-filter-bar__menu-toggle > .cinder-checkbox-field {
    flex: 1;
  }

  .cinder-filter-bar__menu-value {
    flex: 0 1 auto;
    min-inline-size: 0;
    overflow: hidden;
    color: var(--cinder-text-muted);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .cinder-filter-bar__menu-body {
    padding: var(--cinder-space-2);
  }

  .cinder-filter-bar__search-popover {
    min-inline-size: min(16rem, 100%);
  }

  /* A medium bar whose search icon has a query shows it is filtering. */
  .cinder-filter-bar__search-button[data-cinder-active] {
    border-color: var(--cinder-accent-border);
    background: var(--cinder-accent-background);
    color: var(--cinder-accent-text);
  }
}
