/* -- Data Grid --------------------------------------------------- */
/* One scroll container holds a sticky head and the windowed body,   */
/* so the head and every row scroll sideways together and locked     */
/* columns stick with position: sticky - no JS scroll syncing.       */

@layer components {
  .data-grid {
    --data-grid-row-height: 36px;
    --data-grid-template: repeat(4, minmax(8rem, 1fr));
    /* data-select-model="checkbox": the column of checkboxes in front of the rows */
    --data-grid-check-width: 2.5rem;

    display: flex;
    flex-direction: column;
    min-height: 12rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--card);
    color: var(--card-foreground);
    font-size: 0.875rem;
    overflow: hidden;

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }
  }

  /* the one scroll container: both axes */
  .data-grid-viewport {
    position: relative;
    flex: 1;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    /* the windowing owns the scroll position: Firefox's scroll anchoring would follow
       the recycled rows, move scrollTop, re-render and move it again - a scroll
       that runs on by itself (big-data-firefox.e2e) */
    overflow-anchor: none;
    scrollbar-gutter: stable;
  }

  .data-grid-head {
    position: sticky;
    top: 0;
    z-index: 2;
    width: max-content;
    min-width: 100%;
    background: var(--muted);
    /* muted text on the muted head measured 4.34:1 in the default theme - a quarter of the foreground mixed in lifts
       it past AA (4.5:1) in light and dark, in any theme whose foreground reads on its muted surface */
    color: color-mix(in oklch, var(--muted-foreground) 75%, var(--foreground));
    border-bottom: 1px solid var(--border);
  }

  /* every row - head, filters, body - lays out the same tracks */
  .data-grid-row {
    display: grid;
    grid-template-columns: var(--data-grid-template);
    width: max-content;
    min-width: 100%;
  }

  /* a cell adds no width of its own: a row is as wide as the grid (or its fixed columns), and a 1fr column takes what
     is left in every row alike - sized by content, a long tree label widened its rows past the header (VERIFIED: the
     Knowledge Graph's hierarchy, label 205px against the header's 143px, its count cut at the panel edge) */
  .data-grid-row > * {
    contain: inline-size;
  }

  .data-grid-header,
  .data-grid-cell,
  .data-grid-filter-cell {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    min-width: 0;
    padding-inline: 0.75rem;
    background: inherit;

    &[data-locked] {
      position: sticky;
      z-index: 1;
    }

    &[data-align="end"] {
      justify-content: flex-end;
      font-variant-numeric: tabular-nums;
    }
  }

  /* the last locked column draws the edge the rest scrolls under */
  .data-grid[data-has-locked]
    :is(.data-grid-header, .data-grid-cell, .data-grid-filter-cell)[data-locked]:not(:has(~ [data-locked])) {
    /* a selected cell keeps its edges (--_ring) under the locked edge */
    box-shadow:
      var(--_ring, 0 0 #0000),
      1px 0 0 var(--border),
      6px 0 8px -6px color-mix(in oklch, var(--foreground) 25%, transparent);
  }

  .data-grid-header {
    position: relative;
    height: var(--data-grid-row-height);
    font-weight: 500;
    font-size: 0.8125rem;
    background: var(--muted);
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    /* a long press lifts the column - no callout over it (iOS) */
    -webkit-touch-callout: none;
    white-space: nowrap;

    /* a hidden column: the attribute alone loses to display: flex */
    &[hidden] {
      display: none;
    }

    /* a finger rests on it: lifted, ready to move */
    &[data-pressed] {
      background: color-mix(in oklch, var(--accent) 70%, var(--muted));
      color: var(--foreground);
    }

    /* the column being moved follows the pointer above the others */
    &[data-dragging] {
      z-index: 3;
      border-radius: var(--radius-sm);
      outline: 1px solid var(--border);
      background: var(--popover);
      color: var(--foreground);
      box-shadow: var(--shadow-md);
      cursor: grabbing;
    }

    &[data-type="number"] {
      justify-content: flex-end;
    }

    &[data-sortable="false"] {
      cursor: default;
    }

    /* the sort direction - an arrow with a shaft, unlike the menu's chevron - and the priority of a multisort (1, 2, …) */
    &[aria-sort]::after {
      content: "";
      flex: none;
      width: 0.8125rem;
      height: 0.8125rem;
      background: var(--foreground);
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 5v14'/%3E%3Cpath d='m19 12-7 7-7-7'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    &[aria-sort="ascending"]::after {
      mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 12 7-7 7 7'/%3E%3Cpath d='M12 19V5'/%3E%3C/svg%3E");
    }

    &[data-sort-index]::before {
      content: attr(data-sort-index);
      order: 2;
      font-size: 0.6875rem;
      font-variant-numeric: tabular-nums;
      color: var(--foreground);
    }

    &[aria-sort] {
      color: var(--foreground);
    }

    &:hover {
      color: var(--foreground);
    }

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: -2px;
    }
  }

  /* the column menu's arrow, at the end of every header; a locked column shows a pin in it */
  .data-grid-menu-button {
    order: 3;
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.125rem;
    min-width: 1.375rem;
    height: 1.375rem;
    margin-inline-start: auto;
    padding: 0 0.125rem;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--muted-foreground);
    cursor: pointer;

    /* icons are masks over currentColor, so they follow the theme */
    &::before,
    &::after {
      content: "";
      flex: none;
      width: 0.875rem;
      height: 0.875rem;
      background: currentColor;
    }

    &::before {
      display: none;
      width: 0.75rem;
      height: 0.75rem;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 17v5'/%3E%3Cpath d='M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z'/%3E%3C/svg%3E")
        center / contain no-repeat;
      color: var(--primary);
    }

    &::after {
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    &:hover,
    &[aria-expanded="true"] {
      background: var(--accent);
      color: var(--accent-foreground);
    }
  }

  .data-grid-header[data-locked] .data-grid-menu-button::before {
    display: block;
  }

  /* right-aligned headers pack at the end: the arrow follows the label */
  .data-grid-header[data-type="number"] .data-grid-menu-button {
    margin-inline-start: 0;
  }

  /* the end edge resizes the column: a line shows where it is */
  .data-grid-resizer {
    position: absolute;
    inset-block: 0;
    inset-inline-end: 0;
    z-index: 1;
    width: 0.5rem;
    cursor: col-resize;
    touch-action: none;

    &::after {
      content: "";
      position: absolute;
      inset-block: 25%;
      inset-inline-end: 0;
      width: 1px;
      background: var(--border);
    }

    &:hover::after,
    &[data-active]::after {
      inset-block: 0;
      width: 2px;
      background: var(--ring);
    }
  }

  .data-grid[data-resizing] {
    cursor: col-resize;
    user-select: none;
    -webkit-user-select: none;
  }

  /* where a moved column lands */
  .data-grid-drop-marker {
    position: absolute;
    top: 0;
    z-index: 4;
    width: 2px;
    height: var(--data-grid-row-height);
    margin-inline-start: -1px;
    background: var(--primary);
    pointer-events: none;
  }

  /* a finger needs room: the arrow and the edge grow on coarse pointers */
  @media (pointer: coarse) {
    .data-grid-menu-button {
      min-width: 1.75rem;
      height: 1.75rem;
    }

    .data-grid-resizer {
      width: 1rem;
    }
  }

  /* -- the column menu: a popover anchored to the arrow it opened from */
  .data-grid-menu {
    position: fixed;
    inset: auto;
    top: anchor(bottom);
    right: anchor(right);
    margin: 0.25rem 0 0;
    /* no room below or at the end: above the arrow, or toward the other side */
    position-try-fallbacks:
      flip-block,
      flip-inline,
      flip-block flip-inline;
    min-width: 12rem;
    max-height: min(26rem, calc(100dvh - 1rem));
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--popover);
    color: var(--popover-foreground);
    box-shadow: var(--shadow-md);
    font-size: 0.875rem;

    &:popover-open {
      display: flex;
      flex-direction: column;
    }
  }

  .data-grid-menu-group {
    display: flex;
    flex-direction: column;
  }

  .data-grid-menu-label {
    padding: 0.375rem 0.5rem 0.25rem;
    color: var(--muted-foreground);
    font-size: 0.75rem;
    font-weight: 500;
  }

  .data-grid-menu-separator {
    flex: none;
    height: 1px;
    margin: 0.25rem -0.25rem;
    background: var(--border);
  }

  .data-grid-menu-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    min-height: 2rem;
    padding: 0.25rem 0.5rem;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: start;
    white-space: nowrap;
    cursor: pointer;

    /* the leading slot: an icon, or the checkbox */
    &::before {
      content: "";
      flex: none;
      width: 1rem;
      height: 1rem;
      background: currentColor;
    }

    &[data-action="asc"]::before {
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 12 7-7 7 7'/%3E%3Cpath d='M12 19V5'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    &[data-action="desc"]::before {
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 5v14'/%3E%3Cpath d='m19 12-7 7-7-7'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    &[data-action="reset"]::before {
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8'/%3E%3Cpath d='M3 3v5h5'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    /* the sort in force: a dot at the end */
    &[role="menuitemradio"][aria-checked="true"]::after {
      content: "";
      width: 0.375rem;
      height: 0.375rem;
      margin-inline-start: auto;
      border-radius: 50%;
      background: currentColor;
    }

    &[role="menuitemcheckbox"]::before {
      box-sizing: border-box;
      border: 1px solid var(--input);
      border-radius: calc(var(--radius-sm) - 2px);
      background: var(--background);
    }

    &[role="menuitemcheckbox"][aria-checked="true"]::before {
      border-color: var(--primary);
      background: var(--primary);
    }

    /* the tick, drawn over the box */
    &[role="menuitemcheckbox"][aria-checked="true"]::after {
      content: "";
      position: absolute;
      inset-inline-start: calc(0.5rem + 0.3125rem);
      top: 50%;
      width: 0.3125rem;
      height: 0.5625rem;
      border-inline-end: 2px solid var(--primary-foreground);
      border-bottom: 2px solid var(--primary-foreground);
      rotate: 45deg;
      translate: 0 -60%;
    }

    &:is(:hover, :focus-visible):not(:disabled) {
      outline: none;
      background: var(--accent);
      color: var(--accent-foreground);
    }

    &:disabled {
      opacity: 0.5;
      cursor: default;
    }
  }

  @media (pointer: coarse) {
    .data-grid-menu-item {
      min-height: 2.75rem;
    }
  }

  /* -- the filter row ------------------------------------------ */
  .data-grid-filters {
    border-top: 1px solid var(--border);
    background: var(--card);
  }

  .data-grid-filter-cell {
    height: calc(var(--data-grid-row-height) + 4px);
    padding-inline: 0.375rem;
    background: var(--card);
  }

  .data-grid-filter {
    width: 100%;
    min-width: 0;
    height: 1.75rem;
    padding-inline: 0.5rem;
    border: 1px solid var(--input);
    border-radius: var(--radius-sm);
    background: var(--background);
    color: var(--foreground);
    font: inherit;
    font-size: 0.8125rem;

    &::placeholder {
      color: var(--muted-foreground);
    }

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: -1px;
      border-color: var(--ring);
    }
  }

  /* -- the windowed body ----------------------------------------- */
  .data-grid-body {
    position: relative;
    width: max-content;
    min-width: 100%;
    contain: layout paint;
  }

  .data-grid-rows {
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    width: max-content;
    min-width: 100%;
    will-change: translate;
  }

  .data-grid-body .data-grid-row {
    height: var(--data-grid-row-height);
    background: var(--card);
    border-bottom: 1px solid var(--border);
    cursor: default;

    &:hover {
      background: color-mix(in oklch, var(--accent) 60%, var(--card));
    }
  }

  .data-grid-cell {
    white-space: nowrap;

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: -2px;
    }
  }

  /* -- selection models -----------------------------------------------------------
     Hover is a soft neutral tint; a selected cell a mid tint of the primary colour with a strong 2px line along the
     edges of its region (data-edge: top bottom start end, written per cell) - the two never look alike. A grid that
     selects keeps the browser's text selection out of the body: a drag sweeps cells, Ctrl / Cmd + C copies them. */
  .data-grid[data-select] .data-grid-body {
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;

    & :is(input, textarea, [contenteditable]) {
      user-select: text;
      -webkit-user-select: text;
    }
  }

  /* the cell and column models hover a cell or a column, never the row */
  .data-grid[data-select]:is([data-select-model~="cell"], [data-select-model="column"])
    .data-grid-body
    .data-grid-row:hover {
    background: var(--card);
  }
  .data-grid[data-select][data-select-model~="cell"] .data-grid-body {
    & .data-grid-cell {
      cursor: cell;
    }
    & .data-grid-cell:hover:not([data-selected]) {
      background: color-mix(in oklch, var(--accent) 60%, var(--card));
    }
  }

  .data-grid-body .data-grid-cell[data-hover]:not([data-selected]) {
    background: color-mix(in oklch, var(--accent) 60%, var(--card));
  }

  /* a selected cell (and a selected row's checkbox) under the pointer - the cell, its column, its row - is more
     selected, never the soft hover: a deeper tint, the same strong line */
  .data-grid-body :is(.data-grid-cell, .data-grid-check)[data-selected] {
    --_edge-top: 0px;
    --_edge-bottom: 0px;
    --_edge-start: 0px;
    --_edge-end: 0px;
    --_ring:
      inset 0 var(--_edge-top) 0 0 var(--primary), inset 0 calc(-1 * var(--_edge-bottom)) 0 0 var(--primary),
      inset var(--_edge-start) 0 0 0 var(--primary), inset calc(-1 * var(--_edge-end)) 0 0 0 var(--primary);
    /* the tint is the cells' own: a selected row's tint ends where its frame does */
    background: color-mix(in oklch, var(--primary) 20%, var(--card));
    box-shadow: var(--_ring);

    &:hover,
    &[data-hover] {
      background: color-mix(in oklch, var(--primary) 32%, var(--card));
    }
    &[data-edge~="top"] {
      --_edge-top: 2px;
    }
    &[data-edge~="bottom"] {
      --_edge-bottom: 2px;
    }
    &[data-edge~="start"] {
      --_edge-start: 2px;
    }
    &[data-edge~="end"] {
      --_edge-end: 2px;
    }
    /* start and end are logical; the shadows are physical */
    &:dir(rtl) {
      --_ring:
        inset 0 var(--_edge-top) 0 0 var(--primary), inset 0 calc(-1 * var(--_edge-bottom)) 0 0 var(--primary),
        inset calc(-1 * var(--_edge-start)) 0 0 0 var(--primary), inset var(--_edge-end) 0 0 0 var(--primary);
    }
  }

  /* the row models: the row under the pointer deepens its selected cells alike */
  .data-grid:not([data-select-model~="cell"], [data-select-model="column"])
    .data-grid-body
    .data-grid-row:hover
    > :is(.data-grid-cell, .data-grid-check)[data-selected] {
    background: color-mix(in oklch, var(--primary) 32%, var(--card));
  }

  /* the checkbox model: a checkbox in front of every row (sticky at the start, first whatever the column order - the
     element is its row's last), one in the head for every row of the query */
  .data-grid-check {
    order: -1;
    position: sticky;
    inset-inline-start: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    background: inherit;
    cursor: pointer;
  }
  .data-grid-head > .data-grid-row:not(.data-grid-filters) > .data-grid-check {
    height: var(--data-grid-row-height);
    background: var(--muted);
  }
  .data-grid-filters .data-grid-check {
    cursor: default;
  }
  .data-grid[data-select="single"] .data-grid-head .data-grid-check .checkbox {
    visibility: hidden;
  }

  /* -- drag and drop: rows that drag (data-drag), where dropped records would land (data-drop) -- */
  .data-grid[data-drag] .data-grid-body .data-grid-row[draggable="true"] {
    cursor: grab;
  }
  .data-grid[data-dragging-rows] .data-grid-body .data-grid-row[aria-selected="true"] {
    opacity: 0.6;
  }
  .data-grid-row-drop {
    position: absolute;
    inset-inline: 0;
    z-index: 4;
    height: 2px;
    margin-top: -1px;
    background: var(--primary);
    pointer-events: none;

    /* into a row: it becomes its parent - framed */
    &[data-place="into"] {
      height: var(--data-grid-row-height);
      margin-top: 0;
      background: color-mix(in oklch, var(--primary) 12%, transparent);
      box-shadow: inset 0 0 0 2px var(--primary);
    }
  }

  /* the column models: a selected column's header */
  .data-grid-header[aria-selected="true"] {
    --_ring: inset 0 -2px 0 0 var(--primary);
    background: color-mix(in oklch, var(--primary) 20%, var(--muted));
    color: var(--foreground);
    box-shadow: var(--_ring);
  }

  .data-grid[data-sweeping] {
    cursor: cell;
  }

  /* the stand-in that carries a copy or a paste for a moment (Ctrl / Cmd + C, V) - never seen */
  .data-grid-clipboard {
    position: fixed;
    inset-block-start: 0;
    inset-inline-start: 0;
    inline-size: 1px;
    block-size: 1px;
    padding: 0;
    border: 0;
    opacity: 0;
    pointer-events: none;
    resize: none;
  }

  .data-grid-content {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* -- inline editing: an editable column's cell holds its editor ------------------
     The editor fills the cell (a row keeps its height); a refused value shows its message below the cell,
     over the next row. */
  .data-grid-cell[data-editing] {
    overflow: visible;
    padding-inline: 0.25rem;
    z-index: 2;
  }
  .data-grid-editor {
    position: relative;
    display: flex;
    flex: 1;
    align-items: center;
    min-width: 0;
    overflow: visible;

    & > :is(.input, .select, .textarea) {
      flex: 1;
      min-width: 0;
      inline-size: 100%;
    }
    & > .textarea {
      position: absolute;
      inset-block-start: 0;
      inset-inline: 0;
      z-index: 3;
      min-block-size: 6rem;
      box-shadow: var(--shadow-md);
    }
  }
  .data-grid-color-input {
    inline-size: 2.5rem;
    block-size: calc(var(--data-grid-row-height) - 0.75rem);
    padding: 0;
    border: 1px solid var(--input);
    border-radius: var(--radius-sm);
    background: var(--background);
    cursor: pointer;
  }
  .data-grid-error {
    position: absolute;
    inset-block-start: calc(100% + 0.25rem);
    inset-inline-start: 0;
    z-index: 4;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    background: var(--destructive);
    color: var(--destructive-foreground);
    font-size: 0.75rem;
    white-space: nowrap;
    box-shadow: var(--shadow-md);
  }
  /* an editable cell says so on hover: a double click edits it */
  .data-grid-row > .data-grid-cell[data-editable]:not([data-editing]):hover {
    box-shadow:
      var(--_ring, 0 0 #0000),
      inset 0 0 0 1px var(--input);
  }

  /* -- tree grid: the first column indents and carries the toggle --- */
  .data-grid-toggle {
    flex: none;
    display: grid;
    place-items: center;
    width: 1.25rem;
    height: 1.25rem;
    margin-inline-start: calc(var(--depth, 0) * 1.25rem);
    border-radius: var(--radius-sm);
    color: var(--muted-foreground);
    cursor: pointer;

    &::before {
      content: "";
      width: 0.375rem;
      height: 0.375rem;
      border-inline-end: 1.5px solid currentColor;
      border-bottom: 1.5px solid currentColor;
      rotate: -45deg;
      transition: rotate 150ms ease;
    }

    &[data-leaf] {
      cursor: default;

      &::before {
        content: none;
      }
    }

    &:not([data-leaf]):hover {
      background: var(--accent);
      color: var(--accent-foreground);
    }
  }

  .data-grid-row[aria-expanded="true"] .data-grid-toggle::before {
    rotate: 45deg;
  }

  /* -- footer: counts + pager --------------------------------------- */
  .data-grid-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 1rem;
    padding: 0.5rem 0.75rem;
    border-top: 1px solid var(--border);
    color: var(--muted-foreground);
    font-size: 0.8125rem;
    font-variant-numeric: tabular-nums;

    &:empty {
      display: none;
    }
  }

  .data-grid-pager {
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }

  .data-grid-page-label {
    padding-inline: 0.5rem;
    color: var(--foreground);
  }

  .data-grid-page-btn {
    display: grid;
    place-items: center;
    width: 1.75rem;
    height: 1.75rem;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--background);
    color: var(--foreground);
    cursor: pointer;

    /* chevrons as masks over currentColor */
    &::before {
      content: "";
      width: 1rem;
      height: 1rem;
      background: currentColor;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m15 18-6-6 6-6'/%3E%3C/svg%3E")
        center / contain no-repeat;
    }

    &[data-page="first"]::before {
      mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m11 17-5-5 5-5'/%3E%3Cpath d='m18 17-5-5 5-5'/%3E%3C/svg%3E");
    }
    &[data-page="next"]::before {
      mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 18 6-6-6-6'/%3E%3C/svg%3E");
    }
    &[data-page="last"]::before {
      mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 17 5-5-5-5'/%3E%3Cpath d='m13 17 5-5-5-5'/%3E%3C/svg%3E");
    }

    &:dir(rtl)::before {
      scale: -1 1;
    }

    &:hover:not(:disabled) {
      background: var(--accent);
      color: var(--accent-foreground);
    }

    &:disabled {
      opacity: 0.5;
      cursor: default;
    }

    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }
  }

  /* infinite: the next page is on its way */
  .data-grid[data-loading-more] .data-grid-status::after {
    content: " · loading more…";
  }

  /* -- loading and empty -------------------------------------------- */
  .data-grid[data-state="loading"],
  .data-grid[data-state="empty"] {
    & .data-grid-rows {
      display: none;
    }

    & .data-grid-body {
      height: auto !important; /* nothing to scroll in these states */
      min-height: calc(var(--data-grid-row-height) * 4);
    }
  }

  .data-grid[data-state="loading"] .data-grid-body {
    background: repeating-linear-gradient(
      to bottom,
      var(--muted) 0,
      var(--muted) calc(var(--data-grid-row-height) - 1px),
      transparent calc(var(--data-grid-row-height) - 1px),
      transparent var(--data-grid-row-height)
    );
    opacity: 0.6;
    animation: data-grid-pulse 1.6s ease-in-out infinite;
  }

  /* attr() reads the body's own attribute: init copies the grid's
     data-empty-text onto it */
  .data-grid[data-state="empty"] .data-grid-body::after {
    content: attr(data-empty-text);
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--muted-foreground);
  }

  @keyframes data-grid-pulse {
    0%,
    100% {
      opacity: 0.6;
    }
    50% {
      opacity: 0.3;
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  @layer components {
    .data-grid,
    .data-grid *,
    .data-grid *::before,
    .data-grid *::after,
    .data-grid[data-state="loading"] .data-grid-body {
      transition: none;
      animation: none;
      scroll-behavior: auto;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .data-grid,
    .data-grid-head,
    .data-grid-body .data-grid-row,
    .data-grid-filter,
    .data-grid-page-btn,
    .data-grid-menu,
    .data-grid-menu-item[role="menuitemcheckbox"]::before {
      border-color: var(--muted-foreground);
    }

    .data-grid-resizer::after {
      background: var(--muted-foreground);
    }

    .data-grid-header,
    .data-grid-footer {
      color: var(--foreground);
    }

    .data-grid-body :is(.data-grid-cell, .data-grid-check)[data-selected] {
      background: color-mix(in oklch, var(--primary) 26%, var(--card));

      &:hover,
      &[data-hover] {
        background: color-mix(in oklch, var(--primary) 38%, var(--card));
      }
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .data-grid,
    .data-grid-head,
    .data-grid-body .data-grid-row,
    .data-grid-filter,
    .data-grid-page-btn {
      border-color: ButtonText;
    }

    .data-grid-body .data-grid-row[aria-selected="true"],
    .data-grid-body :is(.data-grid-cell, .data-grid-check)[data-selected],
    .data-grid-header[aria-selected="true"] {
      background: Highlight;
      color: HighlightText;
    }

    .data-grid-cell:focus-visible,
    .data-grid-header:focus-visible {
      outline-color: Highlight;
    }

    .data-grid-menu {
      border-color: CanvasText;
    }

    .data-grid-menu-item:is(:hover, :focus-visible):not(:disabled) {
      background: Highlight;
      color: HighlightText;
    }

    .data-grid-menu-item[role="menuitemcheckbox"]::before {
      border-color: ButtonText;
    }

    .data-grid-menu-item[role="menuitemcheckbox"][aria-checked="true"]::after {
      border-color: ButtonText;
    }

    .data-grid-drop-marker,
    .data-grid-row-drop,
    .data-grid-resizer::after {
      background: Highlight;
    }

    .data-grid-toggle::before {
      border-color: ButtonText;
    }

    .data-grid-header[aria-sort]::after {
      forced-color-adjust: none;
      background: ButtonText;
    }

    /* masked icons paint their background - keep it the text color */
    .data-grid-menu-button::before,
    .data-grid-menu-button::after,
    .data-grid-menu-item:not([role="menuitemcheckbox"])::before,
    .data-grid-page-btn::before {
      forced-color-adjust: none;
      background: ButtonText;
    }
  }
}
