/*
 * Card: a padded column lifted one level above its host, whose Header, Body and Footer share its content edge and whose
 * Poster spans its padding. With `grid` it is a rail Container instead: Body, Section and Row become subgrids, and a
 * Header's and Row's leading and trailing cells sit in the rails, one gap inside the border. The mode sets variables
 * here, so a part is styled by its own card, never an enclosing one; grid placement is set unconditionally, since a
 * flex item ignores it.
 */

@layer dx-components {
  .dx-card {
    --dx-card-inset: var(--dx-gutter-md);
    --dx-card-display: flex;
    --dx-card-header-gap: var(--dx-gap-size);
    --dx-card-title-inset: 0px;
    --dx-card-trailing-min: 0px;
    /* A card's icons read a step larger than its text; a sized card (`data-size`) keeps that size's scale. */
    --dx-icon-size: 1.25rem;
    display: flex;
    flex-direction: column;
    min-width: 0;
    box-sizing: border-box;
    padding-block: var(--dx-gap-size);
    padding-inline: var(--dx-card-inset);
    /* In light mode the soft shadow is the card's edge, so its border is transparent (kept for its width, which a
       selected card colours); dark mode has no shadow and keeps the separator. */
    border: 1px solid light-dark(transparent, var(--color-separator));
    border-radius: var(--radius-md, 0.375rem);
    box-shadow:
      0 1px 2px 0 light-dark(rgb(0 0 0 / 0.06), transparent),
      0 2px 6px -1px light-dark(rgb(0 0 0 / 0.08), transparent);
    font-size: var(--dx-font-size);
    line-height: var(--dx-line-height);
  }
  .dx-card[data-grid] {
    --dx-card-inset: var(--dx-gap-size);
    --dx-card-display: grid;
    --dx-card-header-gap: 0px;
    --dx-card-title-inset: var(--dx-gutter);
    --dx-card-trailing-min: var(--dx-gutter);
    display: grid;
  }
  /* A leading poster is flush with the card's top edge, and every poster bleeds across the inline padding. */
  .dx-card:has(> .dx-card-poster:first-child) {
    padding-block-start: 0;
  }
  .dx-card > .dx-card-poster {
    margin-inline: calc(-1 * var(--dx-card-inset));
  }
  /* A wide card would otherwise grow its poster to dominate the card; the image covers the capped frame. */
  .dx-card-poster {
    max-height: 200px;
  }
  /* Inside the border, so the inner radius is the card's less its border width. */
  .dx-card > .dx-card-poster:first-child {
    border-start-start-radius: calc(var(--radius-md, 0.375rem) - 1px);
    border-start-end-radius: calc(var(--radius-md, 0.375rem) - 1px);
  }

  /*
   * Tile: a leading full-height hue block with a large icon. The card becomes a row with no start or block padding, so
   * the tile meets the border on three sides, and the column after it takes the block padding instead.
   */
  .dx-card:has(> .dx-card-tile:first-child) {
    flex-direction: row;
    gap: var(--dx-card-inset);
    padding-block: 0;
    padding-inline-start: 0;
    overflow: hidden;
  }
  .dx-card:has(> .dx-card-tile:first-child) > :not(.dx-card-tile) {
    flex: 1;
    padding-block: var(--dx-gap-size);
  }
  .dx-card-tile {
    --dx-hue-bg: var(--color-neutral-surface);
    --dx-hue-fg: var(--color-neutral-fg);
    display: flex;
    justify-content: center;
    flex-shrink: 0;
    width: var(--dx-card-tile-size, 5rem);
    padding-block-start: var(--dx-block-size);
    background: var(--dx-hue-bg);
  }
  .dx-card-tile > .dx-icon {
    width: calc(var(--dx-card-tile-size, 5rem) * 0.7);
    height: calc(var(--dx-card-tile-size, 5rem) * 0.7);
    color: var(--dx-hue-fg);
  }
  .dx-card-tile-clickable {
    cursor: pointer;
  }
  /* A Body beside a Tile ends with its Footer, whatever the description's length. */
  .dx-card:has(> .dx-card-tile:first-child) > .dx-card-body > .dx-card-footer {
    margin-block-start: auto;
  }

  /* Body and Section: flex columns, or subgrids across a grid card's rails whose children sit in the content track. */
  .dx-card-body,
  .dx-card-section {
    display: var(--dx-card-display);
    flex-direction: column;
    grid-template-columns: subgrid;
    min-width: 0;
  }
  /* A plugin surface (`<dx-surface>`, box-less in dev builds) adds no box, so its children take the content track too. */
  :is(.dx-card-body, .dx-card-section) > *,
  :is(.dx-card-body, .dx-card-section) > dx-surface > * {
    grid-column: content;
  }
  /*
   * Also through a plugin surface (`<dx-surface>`, box-less), e.g. the Body a `CardContent` surface renders, or two
   * when that surface nests another (a Person card's `Related` surface).
   */
  :is(.dx-card, .dx-card-body, .dx-card-section) > :is(.dx-card-header, .dx-card-body, .dx-card-section, .dx-card-row),
  :is(.dx-card, .dx-card-body, .dx-card-section)
    > dx-surface
    > :is(.dx-card-header, .dx-card-body, .dx-card-section, .dx-card-row),
  :is(.dx-card, .dx-card-body, .dx-card-section)
    > dx-surface
    > dx-surface
    > :is(.dx-card-header, .dx-card-body, .dx-card-section, .dx-card-row) {
    grid-column: full;
  }

  /*
   * In a grid card the header spans the rails, and each square before or after the Title (a Block, or an icon-only
   * Button's inset cell) is one block wide, the rail's width: the first fills the start rail and the last the end rail,
   * so a Title with neither takes a rail-wide margin instead.
   */
  .dx-card-header {
    display: flex;
    align-items: center;
    gap: var(--dx-card-header-gap);
    min-height: var(--dx-block-size);
  }
  .dx-card-header > .dx-block {
    width: var(--dx-control-size);
    height: var(--dx-control-size);
    margin: var(--dx-control-inset);
  }
  /* A wrapping Title grows the row, so its squares stay beside the first line, which Typography's padding centres in a block. */
  .dx-card-header > :is(.dx-block, .dx-button[data-square]) {
    align-self: flex-start;
  }
  .dx-card-header > .dx-card-title:first-child {
    margin-inline-start: var(--dx-card-title-inset);
  }
  .dx-card-header > .dx-card-title:last-child {
    margin-inline-end: var(--dx-card-title-inset);
  }

  .dx-card-title {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: var(--dx-font-size);
    line-height: var(--dx-line-height);
    font-weight: 500;
  }

  .dx-card-description {
    margin: 0;
    color: var(--color-fg-muted);
  }

  /* Framed by its host, so neither edge nor rounding of its own; the border keeps its width so nothing shifts. */
  .dx-card[data-border='false'] {
    border-color: transparent;
    border-radius: 0;
  }
  .dx-card[data-border='false'] > .dx-card-poster:first-child {
    border-start-start-radius: 0;
    border-start-end-radius: 0;
  }
  .dx-card[data-selected] {
    border-color: var(--dx-selected-color);
  }
  /* In a stack the focus ring already marks the keyboard's card, so the chosen one reads as current, not a second edge. */
  .dx-focus-item.dx-card[data-selected] {
    border-color: var(--color-separator);
    background: var(--color-current-surface);
  }

  /* A clickable card or row: the hover surface, and the shared inset ring on keyboard focus. */
  .dx-card-clickable {
    cursor: pointer;
  }
  .dx-card-clickable:hover {
    background: var(--color-hover-surface);
  }

  .dx-card-section-title {
    display: flex;
    align-items: center;
    min-height: var(--dx-control-size);
    font-size: var(--dx-label-font-size);
    line-height: var(--dx-label-line-height);
    color: var(--color-fg-muted);
  }

  /*
   * Row (and Link): a flex row of icon, text and trailing cell, or in a grid card a subgrid whose icon Block takes the
   * start rail and text the content track. With trailing content the text and trailing cell share the content and end
   * tracks: in a grid card the trailing cell is at least the rail's width and ends one inset short, so an icon-only
   * action fills the rail exactly and a wider one (a Tag) extends back over the text, which truncates before it.
   */
  .dx-card-row {
    display: var(--dx-card-display);
    grid-template-columns: subgrid;
    grid-auto-rows: minmax(var(--dx-block-size), auto);
    align-items: center;
    min-width: 0;
    min-height: var(--dx-block-size);
    border-radius: var(--radius-sm, 0.25rem);
    color: inherit;
    text-decoration: none;
  }
  .dx-card-row > * {
    grid-row: 1;
    grid-column: content;
  }
  /* A row of wrapped text tops its icon against the first line; text pads its first line into a block, as a rail does. */
  .dx-card-row:has(.dx-typography[data-lines]),
  .dx-card-row[data-align='start'] {
    align-items: start;
  }
  /* First-line alignment reaches the trailing cell too, which sits a block tall so it centres on that first line. */
  .dx-card-row[data-align='start'] > .dx-card-row-main {
    align-items: start;
  }
  .dx-card-row[data-align='start'] .dx-card-row-trailing {
    min-height: var(--dx-block-size);
  }
  /* A tag is a line box shorter than a block, so it insets onto one and centres on the first line as text does. */
  .dx-card-row[data-align='start'] .dx-tag {
    margin-block: calc((var(--dx-block-size) - var(--dx-label-line-height)) / 2);
  }
  .dx-card-row > [data-rail='start'] {
    grid-column: full-start / content-start;
  }
  .dx-card-row > [data-rail='end'] {
    grid-column: content-end / full-end;
  }
  /* The end rail's content is centred in the rail, and may be wider than a block (a unit such as `KB/s`). */
  .dx-card-row > .dx-block[data-rail='end'] {
    justify-self: stretch;
    width: auto;
    min-width: var(--dx-block-size);
    white-space: nowrap;
  }
  .dx-card-row-main {
    display: grid;
    flex: 1;
    grid-template-columns: minmax(0, 1fr);
    align-items: center;
    min-width: 0;
  }
  .dx-card-row[data-trailing] > .dx-card-row-main {
    grid-column: content-start / full-end;
    grid-template-columns: minmax(0, 1fr) minmax(var(--dx-card-trailing-min), auto);
  }
  .dx-card-row[data-span='end'] > .dx-card-row-main {
    grid-column: content-start / full-end;
  }
  .dx-card-row[data-span='full'] > .dx-card-row-main {
    grid-column: full-start / full-end;
  }
  .dx-card-row-content {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .dx-card-row-trailing {
    display: flex;
    align-items: center;
    justify-content: end;
    gap: var(--dx-control-inset);
    padding-inline: var(--dx-control-inset);
  }
  .dx-card-row[aria-current='true'] {
    background: var(--color-hover-surface);
  }
  .dx-card-link:hover {
    background: var(--color-hover-surface);
  }
  .dx-card-link > .dx-block:last-child {
    visibility: hidden;
  }
  .dx-card-link:is(:hover, :focus-visible) > .dx-block:last-child {
    visibility: visible;
  }
  /* The trailing cell's padding is the rail's inset, so an icon-only Button drops its own. */
  .dx-card-row-trailing > .dx-button[data-square] {
    margin: 0;
  }
  /* A toolbar fills its parent's width, which in a trailing cell is the cell itself: it then overflows over the row's
     text instead of taking the width of its items. */
  .dx-card-row-trailing > .dx-scroll-root.dx-toolbar-scroll,
  .dx-card-row-trailing > .dx-toolbar-scroll > .dx-toolbar,
  .dx-card-row-trailing > .dx-toolbar {
    width: auto;
  }
}
