/*
 * Listbox: a scrolling stack of row Containers. Row states (hover, highlight, selection, drop) are `.dx-row`'s
 * (row.css); this file lays the rows out.
 */

@layer dx-components {
  .dx-listbox {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    max-height: 100%;
    font-size: var(--dx-font-size);
    line-height: var(--dx-line-height);
  }

  .dx-scroll-root.dx-listbox-scroll {
    flex: 1 1 auto;
  }
  /* An empty list's viewport would take the space ahead of the Empty message and push it to the bottom. */
  .dx-listbox:has(> .dx-empty) > .dx-scroll-root.dx-listbox-scroll {
    flex: 0 0 auto;
  }

  /* Content without its own ScrollArea: the Root passes its host's rails through to the rows, as ScrollArea.Root does. */
  .dx-listbox:has(> .dx-container[data-gutter='inherit']) {
    grid-column: full;
    display: grid;
    grid-template-columns: subgrid;
    max-height: none;
  }
  .dx-listbox:has(> .dx-container[data-gutter='inherit']) > :not(.dx-container) {
    grid-column: content;
  }

  .dx-listbox-content:focus {
    outline: none;
  }

  /* A picker's choice stays marked after focus leaves the list, and is drawn inside the row so no viewport clips it. */
  .dx-listbox[data-selection-style='outline'] .dx-row:is([aria-selected='true'], [data-selected]) {
    outline: 2px solid var(--color-focus);
    outline-offset: -2px;
  }
  .dx-listbox[data-selection-style='outline'] .dx-row[data-selectable]:hover:not([aria-selected='true']) {
    background: var(--color-hover-surface);
  }

  /*
   * Tiles (Root `tiles`; `data-tiles`, since the Container owns `data-layout`): equal columns inside the content's
   * gutters, the count from zag's `--column-count`; each tile
   * stacks its parts and keeps `.dx-row`'s states.
   */
  .dx-listbox-content.dx-container[data-tiles] {
    /* Important: container.css gives a Content without a Viewport one track through a long selector chain. */
    grid-template-columns: repeat(var(--column-count), minmax(0, 1fr)) !important;
    padding-inline: var(--dx-gutter, 0px);
    /* Room above the first row and below the last for the tiles' outline, which is drawn outside them. */
    padding-block: var(--dx-gap-size);
    gap: var(--dx-gap-size);
  }
  /*
   * Root `tileWidth`: the column count fitted at the minimum width (set by the content as it resizes), each column as
   * wide as the row allows up to the maximum; a row narrower than its columns' maximum leaves its slack at the end.
   */
  .dx-listbox-content.dx-container[data-tiles='auto'] {
    grid-template-columns: repeat(var(--dx-tile-count, 1), minmax(0, var(--dx-tile-max, 1fr))) !important;
    justify-content: start;
  }
  .dx-listbox-content[data-tiles] > * {
    grid-column: auto;
  }
  .dx-listbox-tile {
    display: flex;
    flex-direction: column;
    gap: var(--dx-gap-size);
    min-width: 0;
    padding: var(--dx-gap-size);
  }
  /*
   * A tile is a card, so its states are one outline around it rather than a fill or the rows' inset ring: neutral when
   * current (the keys' tile) or under the pointer, the focus colour when selected.
   */
  .dx-row.dx-listbox-tile {
    --dx-row-ring-color: transparent;
    /* Scrolling a tile into view stops at its box, which would clip the outline drawn outside it. */
    scroll-margin: calc(2px + var(--dx-gap-size));
  }
  .dx-listbox-tile:is(:hover, [data-highlighted]) {
    outline: 2px solid var(--color-neutral-border);
  }
  .dx-listbox-tile:is([aria-selected='true'], [data-selected]) {
    outline: 2px solid var(--color-focus);
  }
  /* A drag preview is a picture of the tile, which the snapshot would clip its outline to. */
  [data-drag-preview] .dx-listbox-tile {
    outline: none;
  }

  /*
   * Rows place their parts by part, so a composed row lays out like the default one: block-sized leading cells (an
   * icon, a drag handle, a checkbox: whatever precedes the text), the text over its description in the growing column,
   * then every other part in its own trailing column.
   */
  .dx-listbox-item {
    --dx-item-columns: minmax(0, 1fr);
    --dx-item-text-column: 1;
    grid-template-rows: auto;
    grid-auto-columns: auto;
    align-items: center;
    min-height: var(--dx-block-size);
  }
  .dx-listbox-item:has(> .dx-listbox-item-text:nth-child(2)) {
    --dx-item-columns: var(--dx-block-size) minmax(0, 1fr);
    --dx-item-text-column: 2;
  }
  .dx-listbox-item:has(> .dx-listbox-item-text:nth-child(3)) {
    --dx-item-columns: repeat(2, var(--dx-block-size)) minmax(0, 1fr);
    --dx-item-text-column: 3;
  }
  .dx-listbox-item:has(> .dx-listbox-item-text:nth-child(4)) {
    --dx-item-columns: repeat(3, var(--dx-block-size)) minmax(0, 1fr);
    --dx-item-text-column: 4;
  }
  .dx-listbox-item:has(> .dx-listbox-item-description) {
    grid-template-rows: auto auto;
  }
  .dx-listbox-item.dx-container > * {
    grid-column: auto;
    grid-row: 1 / -1;
  }
  /* A Container packs its tracks to the start; a row of custom cells shorter than the block would sit at its top. */
  .dx-listbox-item.dx-container {
    align-content: center;
  }
  .dx-listbox-item.dx-container > .dx-listbox-item-text {
    grid-column: var(--dx-item-text-column);
    grid-row: 1;
  }
  .dx-listbox-item.dx-container > .dx-listbox-item-description {
    grid-column: var(--dx-item-text-column);
    grid-row: 2;
  }
  /* A full-width part (an expanded detail) takes its own row under the others, across every column. */
  .dx-listbox-item.dx-container > [data-place='full'] {
    grid-column: 1 / -1;
    grid-row: auto;
  }
  /*
   * With a description the other parts sit on the title's line rather than centring across both: each is a block tall
   * from the row's top, and the title's top padding is half a block less its line, so their centres meet.
   */
  .dx-listbox-item.dx-container:has(> .dx-listbox-item-description)
    > :not(.dx-listbox-item-text, .dx-listbox-item-description, .dx-drop-indicator) {
    align-self: start;
  }

  /* Text over description: together they take one block plus a line, not a block each. */
  .dx-listbox-item:has(> .dx-listbox-item-description) > .dx-listbox-item-text {
    padding-block-end: 0;
  }
  .dx-listbox-item > .dx-listbox-item-description {
    padding-block-start: 0;
  }
  .dx-listbox-item > :is(.dx-listbox-item-text, .dx-listbox-item-description) {
    padding-inline-end: var(--dx-gap-size);
  }
  .dx-listbox-item > .dx-listbox-item-text:first-child,
  .dx-listbox-item > .dx-listbox-item-text:first-child + .dx-listbox-item-description {
    padding-inline-start: var(--dx-gap-size);
  }
  /* An avatar fills its block where an icon sits inside one, so the text keeps an icon's distance from it. */
  .dx-listbox-item > .dx-listbox-item-icon:has(.dx-avatar) ~ :is(.dx-listbox-item-text, .dx-listbox-item-description) {
    padding-inline-start: var(--dx-gap-size);
  }

  /* Trailing parts are an inset apart, and an icon-only Button pads out to its block like it does in a toolbar. */
  .dx-listbox-item.dx-container
    > .dx-listbox-item-text
    ~ :not(.dx-listbox-item-description, .dx-listbox-item-indicator, .dx-drop-indicator) {
    margin-inline-end: var(--dx-control-inset);
  }
  .dx-listbox-item.dx-container > .dx-button[data-square] {
    margin: var(--dx-control-inset);
  }
  .dx-listbox-item.dx-container > .dx-collapsible-trigger[data-caret-only] {
    margin-inline-end: 0;
  }

  .dx-listbox-item-indicator {
    display: grid;
    place-items: center;
    width: var(--dx-block-size);
    height: var(--dx-block-size);
  }

  /*
   * Root `columns`: the Content's template holds the tracks and each row is a subgrid of them, placing its children in
   * order; a description takes the next track like any part.
   */
  .dx-listbox-content[data-columns] > .dx-listbox-item.dx-container {
    grid-column: content;
    grid-template-columns: subgrid;
  }
  .dx-listbox-content[data-columns] > .dx-collapsible > .dx-listbox-item.dx-container {
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
  }
  :is(.dx-listbox-content[data-columns], .dx-listbox-content[data-columns] > .dx-collapsible)
    > .dx-listbox-item.dx-container
    > * {
    grid-column: auto;
    grid-row: auto;
    align-self: center;
  }
  /* `align='start'` on the content tops every part of a row on its first line, for cells that wrap. */
  :is(
      .dx-listbox-content[data-columns][data-align='start'],
      .dx-listbox-content[data-columns][data-align='start'] > .dx-collapsible
    )
    > .dx-listbox-item.dx-container
    > * {
    align-self: start;
  }
  /* A spanning cell (Container `span`) takes as many of the shared tracks as it asks for. */
  :is(.dx-listbox-content[data-columns], .dx-listbox-content[data-columns] > .dx-collapsible)
    > .dx-listbox-item.dx-container
    > [data-span] {
    grid-column-end: span var(--dx-span, 1);
  }
  /*
   * Custom end cells keep a gap from the row's edges, as the text part does, so its highlight ring frames them. A cell
   * that is a template root (a form's array item) is exempt: its fields carry their own inset and line up with the form's.
   */
  :is(.dx-listbox-content[data-columns], .dx-listbox-content[data-columns] > .dx-collapsible)
    > .dx-listbox-item.dx-container
    > :first-child:not(
      .dx-listbox-item-icon,
      .dx-listbox-item-text,
      .dx-button,
      .dx-block,
      .dx-container[data-gutter='none']
    ) {
    padding-inline-start: var(--dx-gap-size);
  }
  :is(.dx-listbox-content[data-columns], .dx-listbox-content[data-columns] > .dx-collapsible)
    > .dx-listbox-item.dx-container
    > :last-child:not(.dx-listbox-item-icon, .dx-listbox-item-indicator, .dx-button, .dx-block) {
    padding-inline-end: var(--dx-gap-size);
  }
  /* A description here is a track on the title's line, not a line under it, so neither keeps the stacked padding. */
  :is(.dx-listbox-content[data-columns], .dx-listbox-content[data-columns] > .dx-collapsible)
    > .dx-listbox-item.dx-container
    > :is(.dx-listbox-item-text, .dx-listbox-item-description) {
    padding-block: 0;
  }

  /* A collapsible row: the Collapsible root spans the content track, the row above its detail, both full width. */
  .dx-listbox-content > .dx-collapsible {
    display: grid;
    grid-column: content;
    grid-template-columns: subgrid;
  }
  .dx-listbox-content > .dx-collapsible > * {
    grid-column: 1 / -1;
  }
}
