@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * LIST DETAIL
 *
 * A list pane and a detail pane. Wide, they sit in ResizablePanels; narrow, one fills the root
 * and the detail leads with a Back button. Both layouts fill the height they are given and
 * scroll each pane on its own.
 * ======================================== */

  .cinder-list-detail {
    display: flex;
    flex-direction: column;
    inline-size: 100%;
    block-size: 100%;
    min-inline-size: 0;
    min-block-size: 0;
  }

  /* The panes are the content surface (white in light mode, the 18.5% surface in dark). On the white
     light page they part from it by the page hairline; in dark by fill (COR-2697). */
  .cinder-list-detail__panels {
    position: relative;
    flex: 1 1 auto;
    min-block-size: 0;
    background: var(--cinder-surface);
  }

  /* The page hairline, on an overlay that takes no layout in either theme. */
  .cinder-list-detail__panels::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    border: var(--cinder-border-width) solid var(--_cinder-page-edge);
  }

  .cinder-list-detail__list,
  .cinder-list-detail__detail {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-inline-size: 0;
    min-block-size: 0;
    block-size: 100%;
  }

  /* Without a Back button the detail region takes the focus when an item opens; it is a target to
     land on, not a control, so it draws no ring. */
  .cinder-list-detail__detail:focus {
    outline: none;
  }

  .cinder-list-detail__header {
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-3);
    padding: var(--cinder-space-4);
    border-block-end: 1px solid var(--cinder-border-muted);
  }

  .cinder-list-detail[data-cinder-flush-list] .cinder-list-detail__header {
    padding-inline: 0;
  }

  /* A ghost text button in the header draws at the compact height, but keeps the side padding of
   * the `sm` step it is asked for, so its icon and label stay on the column's text inset rather
   * than stepping left by the difference between the two paddings (COR-2388). */
  .cinder-list-detail__header
    .cinder-button[data-cinder-compact].button-variant-ghost:not([data-cinder-icon-only]) {
    padding-inline: var(--cinder-button-padding-x-sm);
  }

  /* Side by side: the rail, whenever the list can be hidden, then the panels. */
  .cinder-list-detail__split {
    position: relative;
    display: flex;
    flex: 1 1 auto;
    min-inline-size: 0;
    min-block-size: 0;
  }

  .cinder-list-detail__below {
    position: relative;
  }

  /* The toggle is one button, mounted in both states so focus stays on it as it is pressed. Once the
   * list is hidden it sits at the top of the rail that holds its place. */
  .cinder-list-detail__toggle {
    position: absolute;
    z-index: 1;
    inset-block-start: var(--cinder-space-3-5);
  }
  .cinder-list-detail__toggle:where(
    :not([dir='rtl'], [dir='rtl'] *),
    [dir='rtl'] [dir='ltr'],
    [dir='rtl'] [dir='ltr'] *
  ) {
    left: var(--cinder-space-2);
  }
  .cinder-list-detail__toggle:where(
    :is([dir='rtl'], [dir='rtl'] *):not([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
  ) {
    right: var(--cinder-space-2);
  }

  /* While the list shows the toggle is a handle on the divider (COR-2438): inside the list pane,
   * against the divider, centred on its length, so it never crowds the header's title or its "+",
   * and the header keeps its whole width, the title starting on the search field's edge. The list
   * pane's size, measured as it is dragged, places it. It draws on the raised surface so it reads
   * over the rows it floats above. */
  .cinder-list-detail:not([data-cinder-list-collapsed]) .cinder-list-detail__toggle {
    inset-block-start: calc(50% - var(--cinder-button-height-sm) / 2);
  }
  .cinder-list-detail:not([data-cinder-list-collapsed])
    .cinder-list-detail__toggle:where(
      :not([dir='rtl'], [dir='rtl'] *),
      [dir='rtl'] [dir='ltr'],
      [dir='rtl'] [dir='ltr'] *
    ) {
    left: calc(
      var(--_cinder-list-detail-list-width, 0px) - var(--cinder-button-height-sm) -
        var(--cinder-space-1)
    );
  }
  .cinder-list-detail:not([data-cinder-list-collapsed])
    .cinder-list-detail__toggle:where(
      :is([dir='rtl'], [dir='rtl'] *):not([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
    ) {
    right: calc(
      var(--_cinder-list-detail-list-width, 0px) - var(--cinder-button-height-sm) -
        var(--cinder-space-1)
    );
  }

  /* One above the other the divider is the list's bottom edge: the handle sits above it, centred. */
  .cinder-list-detail:not([data-cinder-list-collapsed])
    .cinder-list-detail__below
    > .cinder-list-detail__toggle {
    inset-block-start: calc(
      var(--_cinder-list-detail-list-height, 0px) - var(--cinder-button-height-sm) -
        var(--cinder-space-1)
    );
  }
  .cinder-list-detail:not([data-cinder-list-collapsed])
    .cinder-list-detail__below
    > .cinder-list-detail__toggle:where(
      :not([dir='rtl'], [dir='rtl'] *),
      [dir='rtl'] [dir='ltr'],
      [dir='rtl'] [dir='ltr'] *
    ) {
    left: calc(50% - var(--cinder-button-height-sm) / 2);
  }
  .cinder-list-detail:not([data-cinder-list-collapsed])
    .cinder-list-detail__below
    > .cinder-list-detail__toggle:where(
      :is([dir='rtl'], [dir='rtl'] *):not([dir='rtl'] [dir='ltr'], [dir='rtl'] [dir='ltr'] *)
    ) {
    right: calc(50% - var(--cinder-button-height-sm) / 2);
  }

  .cinder-list-detail:not([data-cinder-list-collapsed]) .cinder-list-detail__toggle .cinder-button {
    background: var(--cinder-surface-raised);
    box-shadow: var(--cinder-shadow-sm);
  }

  /* The handle floats over the list's rows at the divider. The pane is never padded for it: its
   * search, segments and heading bands keep their whole width. The list instead names how far a row's
   * text keeps from the row's end, the handle's width past a row's end padding, and a row that reads
   * the name (`StackedListItem`, `SelectableRow`, `GroupedListItem`, `DataList` headings) keeps its
   * text clear, so a time, a heading's label or a chevron is never under the handle. The name is set
   * only while the handle is drawn, at every list width: with a pointer that hovers, while the list,
   * the divider or the handle is hovered or the handle has focus; with a finger, always. Otherwise
   * the rows keep their whole width. Only the side-by-side layout draws the handle beside the list,
   * and the component marks the root `data-cinder-handle-inset` only there (the header's search and
   * segments never read the name). */
  @media not (hover: hover) {
    .cinder-list-detail[data-cinder-handle-inset] .cinder-list-detail__rows {
      /* Private, shared with the rows and heading bands this region holds: they read it, nothing
         outside Cinder sets or reads it. */
      --_cinder-list-detail-handle-inset: calc(
        var(--cinder-button-height-sm) + var(--cinder-space-1) * 2 - var(--cinder-space-3)
      );
    }
  }

  /* With a pointer that hovers, the handle shows while the list, the divider or the handle itself
   * is hovered, and whenever it has focus, so it is always reachable by keyboard. A touch screen,
   * which cannot hover, always shows it. */
  @media (hover: hover) {
    .cinder-list-detail:not([data-cinder-list-collapsed]) .cinder-list-detail__toggle {
      opacity: 0;
      transition: opacity var(--cinder-duration-fast) var(--cinder-ease-standard);
    }

    .cinder-list-detail:not([data-cinder-list-collapsed])
      :is(.cinder-list-detail__split, .cinder-list-detail__below):has(
        .cinder-list-detail__list:hover,
        .cinder-resizable-panels__handle:hover,
        .cinder-list-detail__toggle:hover,
        .cinder-list-detail__toggle:focus-within
      )
      > .cinder-list-detail__toggle {
      opacity: 1;
    }

    /* The same condition that draws the handle reserves its room. */
    .cinder-list-detail[data-cinder-handle-inset]
      .cinder-list-detail__split:has(
        .cinder-list-detail__list:hover,
        .cinder-resizable-panels__handle:hover,
        .cinder-list-detail__toggle:hover,
        .cinder-list-detail__toggle:focus-within
      )
      .cinder-list-detail__rows {
      --_cinder-list-detail-handle-inset: calc(
        var(--cinder-button-height-sm) + var(--cinder-space-1) * 2 - var(--cinder-space-3)
      );
    }
  }

  /* Keep the panel hairline rounded when the consumer clips the surrounding ListDetail. */
  .cinder-list-detail__split,
  .cinder-list-detail__split > .cinder-list-detail__panels {
    border-radius: inherit;
  }

  .cinder-list-detail__split > .cinder-list-detail__panels {
    flex: 1 1 auto;
    min-inline-size: 0;
  }

  /* While the list is hidden a narrow rail holds the toggle's place at the start edge, set off from
   * the detail by the same hairline the divider draws; no rail is drawn while the list shows. The
   * panels stay mounted, so the open item keeps its state; the list pane and the divider leave the
   * layout, and the detail takes the width. */
  .cinder-list-detail__rail {
    flex: none;
    inline-size: calc(var(--cinder-button-height-sm) + 2 * var(--cinder-space-2));
    border-inline-end: 1px solid var(--cinder-border-muted);
  }

  .cinder-list-detail[data-cinder-list-collapsed]
    .cinder-list-detail__panels
    > :is(.cinder-resizable-panels__pane:first-child, .cinder-resizable-panels__handle) {
    display: none;
  }

  .cinder-list-detail[data-cinder-list-collapsed]
    .cinder-list-detail__panels
    > .cinder-resizable-panels__pane {
    flex: 1 1 0;
  }

  /* One above the other, the rail is a row across the top of the detail. */
  .cinder-list-detail__below > .cinder-list-detail__rail {
    inline-size: auto;
    block-size: calc(var(--cinder-button-height-sm) + 2 * var(--cinder-space-3-5));
    border-inline-end: 0;
    border-block-end: 1px solid var(--cinder-border-muted);
  }

  @media (forced-colors: active) {
    .cinder-list-detail__rail {
      border-color: CanvasText;
    }
  }

  .cinder-list-detail__rows {
    flex: 1 1 auto;
    min-block-size: 0;
    overflow-y: auto;
  }

  /* Rows sit inset in the pane, as side navigation and menu rows sit in theirs: a fill or a ring
   * rounds all four corners clear of both pane edges and of a group heading's rule, wherever the
   * row is. The row's own inline padding drops to match, so its text stays on the header's edge.
   * The padding, not the global `li` margin, sets the pitch, so the first row under a heading
   * and the last above the next get the same space. */
  .cinder-list-detail__row {
    margin: 0;
    padding: var(--cinder-space-0-5) var(--cinder-space-2);
    list-style: none;
  }

  .cinder-list-detail__row .cinder-selectable-row__primary {
    padding-inline-start: var(--cinder-space-2);
  }

  .cinder-list-detail__row
    .cinder-selectable-row:not(.cinder-selectable-row--has-trailing-actions)
    > .cinder-selectable-row__primary {
    padding-inline-end: calc(var(--cinder-space-2) + var(--_cinder-list-detail-handle-inset, 0px));
  }

  .cinder-list-detail__row
    .cinder-selectable-row.cinder-selectable-row--has-trailing-actions
    > .cinder-selectable-row__primary {
    padding-inline-end: var(--cinder-space-2);
  }

  .cinder-list-detail__row .cinder-selectable-row__trailing-actions {
    padding-inline-end: calc(var(--cinder-space-2) + var(--_cinder-list-detail-handle-inset, 0px));
  }

  .cinder-list-detail[data-cinder-flush-list] .cinder-list-detail__row {
    padding-inline: 0;
  }

  .cinder-list-detail[data-cinder-flush-list]
    .cinder-list-detail__row
    .cinder-selectable-row__primary {
    padding-inline: 0;
  }

  .cinder-list-detail[data-cinder-flush-list]
    .cinder-list-detail__row
    .cinder-selectable-row__trailing-actions {
    padding-inline-end: 0;
  }

  .cinder-list-detail__row:not(.cinder-list-detail__row + .cinder-list-detail__row) {
    padding-block-start: var(--cinder-space-2);
  }

  .cinder-list-detail__row:not(:has(+ .cinder-list-detail__row)) {
    padding-block-end: var(--cinder-space-2);
  }

  /* The footer sits under the rows, or under the empty state. What it wraps is a row of its own
   * (a link, a list item) that already carries the rows' inset, so the footer adds block space
   * only, and no inline padding of its own on top of that inset (COR-2195). */
  .cinder-list-detail__footer {
    padding-block: var(--cinder-space-2);
    padding-inline: 0;
  }

  /* Under the empty state the footer follows the empty state's own bottom padding (1.5rem), which
   * already is a gap, so the empty state gives up most of it and the footer takes its usual space. */
  .cinder-list-detail__rows
    > .cinder-data-list:has(+ .cinder-list-detail__footer)
    > .cinder-data-list-empty {
    padding-block-end: var(--cinder-space-2);
  }

  .cinder-list-detail__rows .cinder-data-list-group {
    margin: 0;
  }

  .cinder-list-detail__back {
    padding: var(--cinder-space-2) var(--cinder-space-3);
    border-block-end: 1px solid var(--cinder-border-muted);
  }

  /* The detail pane keeps its own inset, so a detail view needs no compensating padding of its
   * own. A view that bleeds edge to edge (a table, a code frame) sets
   * `flushDetail`, or `--cinder-list-detail-detail-padding: 0` on an ancestor outside Interborough. */
  .cinder-list-detail__detail-body {
    flex: 1 1 auto;
    min-block-size: 0;
    overflow-y: auto;
    padding: var(--cinder-list-detail-detail-padding, var(--cinder-space-4));
  }

  .cinder-list-detail[data-cinder-flush-detail] .cinder-list-detail__detail-body {
    padding: 0;
  }

  /* A table in the list slot fills the pane edge to edge: the pane is its frame, so the table draws
   * no border or radius of its own. */
  .cinder-list-detail__rows .cinder-table-scroll {
    border: 0;
    border-radius: 0;
  }

  /* The loading placeholder for the list pane draws its own rows and keeps the pane's width. */
  .cinder-list-detail__loading {
    inline-size: 100%;
  }

  /* A detail placeholder represents prose and field values, not a full-width data surface. Keep
   * its bars to a readable measure when a wide detail pane has no content to constrain them yet. */
  .cinder-list-detail__loading-detail {
    max-inline-size: 40rem;
  }

  /* One pane at a time: the pane follows the page, not a box of its own. The list and the open item
   * grow with their content and the page scrolls, so a phone never holds a second, inner scroll. */
  .cinder-list-detail:is([data-cinder-layout='stacked'], [data-cinder-layout='below']) {
    block-size: auto;
    min-block-size: 0;
  }

  .cinder-list-detail:is([data-cinder-layout='stacked'], [data-cinder-layout='below'])
    :is(.cinder-list-detail__list, .cinder-list-detail__detail) {
    block-size: auto;
  }

  .cinder-list-detail:is([data-cinder-layout='stacked'], [data-cinder-layout='below'])
    :is(.cinder-list-detail__rows, .cinder-list-detail__detail-body) {
    overflow-y: visible;
  }

  /* Both panes at full width, the detail under the list and set off from it by a rule. */
  .cinder-list-detail__below {
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
  }

  .cinder-list-detail__below > .cinder-list-detail__detail {
    border-block-start: 1px solid var(--cinder-border-muted);
  }

  /* A flush detail (an open conversation) keeps the height it is given when it is the one pane
   * showing, so what it docks at its bottom edge sits at the bottom of the screen. The list and a
   * detail with its own padding keep growing with their content. */
  .cinder-list-detail[data-cinder-layout='stacked'][data-cinder-flush-detail][data-cinder-showing='detail'],
  .cinder-list-detail[data-cinder-layout='stacked'][data-cinder-flush-detail][data-cinder-showing='detail']
    .cinder-list-detail__detail,
  .cinder-list-detail[data-cinder-layout='stacked'][data-cinder-flush-detail][data-cinder-showing='detail']
    .cinder-list-detail__detail-body {
    block-size: 100%;
  }

  /* One surface split by a hairline (COR-2311): the handle is the rule itself, 1px wide, so the
   * panes sit flush with no gap between them and no card around either. The 44px hit area is the
   * handle's ::after, so the thin line stays easy to grab. */
  .cinder-list-detail__panels
    > .cinder-resizable-panels__handle[data-cinder-orientation='horizontal'] {
    inline-size: 1px;
    overflow: visible;
  }

  /* The divider between the panes is a full-height 1px rule. The grip is drawn on it only while the
   * pointer is over the divider or it has focus, so a resting list-and-detail shows a quiet line. */
  .cinder-list-detail__panels > .cinder-resizable-panels__handle::before {
    content: '';
    position: absolute;
    inset-block: 0;
    left: 50%;
    inline-size: 0;
    transform: translateX(-50%);
    border-inline-start: 1px solid var(--cinder-border-muted);
  }

  .cinder-list-detail__panels
    > .cinder-resizable-panels__handle
    .cinder-resizable-panels__handle-line {
    opacity: 0;
    transition: opacity var(--cinder-duration-fast) var(--cinder-ease-standard);
  }

  .cinder-list-detail__panels
    > .cinder-resizable-panels__handle:is(:hover, :focus-visible)
    .cinder-resizable-panels__handle-line {
    opacity: 1;
  }

  @media (forced-colors: active) {
    .cinder-list-detail__panels > .cinder-resizable-panels__handle::before {
      border-inline-start-color: CanvasText;
    }
  }
}
