@layer uniform.components {
u-data-grid {
  container: u-data-grid / inline-size;
  width: 100%;
}

u-data-grid .u-table {
  max-height: 70dvh;
  overflow: auto;
}

u-data-grid .u-table > table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

u-data-grid .u-table :is(th, td) { white-space: nowrap; }

u-data-grid .u-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--u-color-bg-surface);
}

u-data-grid .u-table :is(th, td):first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  border-right: 1px solid var(--u-color-divider);
  background: var(--u-color-bg-surface);
}

u-data-grid .u-table thead th:first-child { z-index: 3; }

u-data-grid .u-table { --u-data-grid-check: calc(var(--u-space-4) + 16px); }

u-data-grid .u-card > .u-table { --u-data-grid-check: calc(var(--u-space-6) + 16px); }

u-data-grid .u-table tr > :first-child:has(> .u-checkbox:only-child) { border-right: 0; }

u-data-grid .u-table tr:has(> :first-child > .u-checkbox:only-child) > :nth-child(2) {
  position: sticky;
  left: var(--u-data-grid-check);
  z-index: 1;
  border-right: 1px solid var(--u-color-divider);
}

u-data-grid .u-table :is(th, td):nth-child(2) { background: var(--u-color-bg-surface); }

u-data-grid .u-table thead tr:has(> :first-child > .u-checkbox:only-child) > :nth-child(2) { z-index: 3; }

u-data-grid .u-table thead .u-btn-icon {
  width: var(--u-size-target);
  min-height: var(--u-size-target);
  margin-block: calc(-1 * var(--u-space-1));
}

u-data-grid .u-table thead .u-btn-icon > svg {
  width: 16px;
  height: 16px;
}

u-data-grid .u-table :is(th, td):focus-visible { outline-offset: -2px; }

u-data-grid td:has(> template) {
  position: relative;
  cursor: pointer;
}

u-data-grid td:has(> template):hover { background: var(--u-color-divider); }

u-data-grid .u-data-grid-value {
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  cursor: pointer;
}

u-data-grid .u-data-grid-value::after {
  content: "";
  position: absolute;
  inset: 0;
}

u-data-grid .u-data-grid-value:focus-visible { outline: 0; }

u-data-grid .u-data-grid-value:focus-visible::after { outline: 2px solid var(--u-color-info); outline-offset: -2px; }

.u-data-grid-editor {
  position: absolute;
  inset-block: 0;
  left: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  min-width: 100%;
  padding-inline: var(--u-space-2);
  background: var(--u-color-bg-surface);
}

.u-data-grid-editor > form {
  display: flex;
  flex: 1;
  margin: 0;
}

.u-data-grid-editor > form > * { flex: 1; }

u-data-grid:not(:has(tbody input[type="checkbox"]:checked)) form:has(output) { display: none; }

u-data-grid:has(tbody input[type="checkbox"]:checked) .u-table-controls > form:first-child > :not(:has(input[type="search"]), input[type="search"], label) { visibility: hidden; }

u-data-grid tbody tr:has(input[type="checkbox"]:checked) > * { background: var(--u-color-divider); }

form:has(> span > output) > span {
  color: var(--u-color-text-default);
  font-size: var(--u-font-size-small);
}

form:has(> span > output) output {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

u-data-grid .u-table td.u-data-grid-changed { background: var(--u-color-highlight); }

u-data-grid .u-table > table tbody tr:has(> :first-child a:not(.u-btn)) { cursor: pointer; }

u-data-grid .u-table > table tbody tr:has(> :first-child a:not(.u-btn)):hover > * { background: var(--u-color-divider); }

u-data-grid .u-table > table tbody tr > :first-child > a:not(.u-btn)::after { content: none; }

:where(u-data-grid) .u-table > table tbody tr:where(:hover) > * { background: none; }

u-data-grid .u-table > table tbody tr:has(> :first-child > a:not(.u-btn)) :is(a, button, input, label, select, summary):not(tr > :first-child > a) {
  position: static;
  z-index: auto;
}

@container u-data-grid (max-width: 37.4375rem) {
  u-data-grid:has(thead [data-keep-narrow]) .u-table {
    max-height: none;
    overflow: visible;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table,
  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody { display: block; width: auto; min-width: 0; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > thead { display: none; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--u-space-1) var(--u-space-4);
    padding: var(--u-space-2) var(--u-space-4);
    cursor: pointer;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr + tr { border-top: 1px solid var(--u-color-divider); }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr:hover { background: var(--u-color-divider); }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > * {
    position: static;
    display: none;
    padding: 0;
    border: 0;
    background: none;
    white-space: normal;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > :is(:first-child, [data-keep-narrow]):not([hidden]) { display: block; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > :is(:first-child, :last-child) { padding: 0; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > :first-child {
    flex: 1 0 calc(100% - 8rem);
    order: -2;
    font-weight: 500;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > .u-table-num {
    order: -1;
    margin-left: auto;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody .u-checkbox { display: none; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > [data-label]::before {
    content: attr(data-label) ": ";
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-data-grid-value { cursor: inherit; }

  u-data-grid:has(thead [data-keep-narrow]) .u-data-grid-value::after { content: none; }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody > tr > :first-child a::after {
    content: "";
    position: absolute;
    inset: 0;
  }

  u-data-grid:has(thead [data-keep-narrow]) .u-table > table > tbody td:has(> template) { cursor: pointer; }
}
}
