/**
 * Data Grid Web Component v2.0.13
 * https://github.com/lekoala/data-grid
 */

@keyframes dataGridShow {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0.5;
  }
}

data-grid {
  --padding: 0.5rem;
  --padding-x: 0.75rem;
  --padding-y: 0.5rem;
  --padding-y-header: 0.75rem;
  --padding-half: calc(var(--padding) / 2);
  --color-rgb: var(--bs-primary-rgb, 13, 110, 253);
  --color: rgb(var(--color-rgb));

  --highlight-color: #fffcee;
  --selected-bgcolor: var(--bs-primary-bg-subtle, #cfe2ff);
  --body-background: var(--bs-table-bg, #fff);
  --striped-background: rgba(0, 0, 0, 0.05);
  --header-background: var(--bs-gray-200, #e9ecef);
  --header-color: var(--bs-dark, #212529);
  --input-background: var(--bs-body-bg, #ffffff);
  --input-border-color: var(--bs-border-color, #e9ecef);
  --btn-background: var(--bs-body-bg, #ffffff);
  --btn-color: var(--color);
  --btn-border-color: var(--bs-border-color, #e9ecef);
  --body-bg: var(--bs-body-bg, #212529);
  --body-color: var(--bs-body-color, #212529);
  --icon-scale: 1;
  --border-radius: 0.25rem;
  --row-border-color: #f2f2f2;
  --responsive-width: 60%;

  --black: var(--bs-black, #000);
  --white: var(--bs-white, #fff);
  --gray: var(--bs-gray, #6c757d);
  --gray-dark: var(--bs-gray-dark, #343a40);
  --gray-100: var(--bs-gray-100, #f8f9fa);
  --gray-200: var(--bs-gray-200, #e9ecef);
  --gray-300: var(--bs-gray-300, #dee2e6);
  --gray-400: var(--bs-gray-400, #ced4da);
  --gray-500: var(--bs-gray-500, #adb5bd);
  --gray-600: var(--bs-gray-600, #6c757d);
  --gray-700: var(--bs-gray-700, #495057);
  --gray-800: var(--bs-gray-800, #343a40);
  --gray-900: var(--bs-gray-900, #212529);

  display: block;
  min-height: 6rem;
  position: relative;

  // When used with fixed height, have a nice scrollbar
  & {
    --scroller-color: 0, 0%;
    --scroller-color-lightness: 80%;
    --scroller-hover-factor: 0.8;
    --scroller-thumb: hsl(var(--scroller-color), var(--scroller-color-lightness));
    /* Replicate hover for webkit */
    --scroller-thumb-hover: hsl(var(--scroller-color), calc(var(--scroller-color-lightness) * var(--scroller-hover-factor)));
    --scroller-background: transparent;
    scrollbar-color: var(--scroller-thumb) var(--scroller-background);
    scrollbar-width: thin;

    &::-webkit-scrollbar {
      width: 8px;
      height: 8px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
    }

    &::-webkit-scrollbar-thumb {
      background: var(--scroller-thumb);
    }

    &::-webkit-scrollbar-thumb:hover {
      background: var(--scroller-thumb-hover);
    }
  }

  // Hides column header row when it's not populated
  > table {
    &[role=grid] {
      tr.dg-head-columns:is(:empty, :has(>:first-child:not([scope=col]))) {
        display: none;
      }
    }
  }

  img {
    border: none;
    height: auto;
    max-width: 100%;
    vertical-align: middle;
  }

  [hidden] {
    display: none;
  }

  table {
    display: table;
    // Table and column widths are set by the widths of table and col elements
    // or by the width of the first row of cells. Cells in subsequent rows do not affect column widths.
    table-layout: fixed;
    width: 100%;
    max-width: 100%;
    white-space: normal;
    // Background needs to be on the table, not on the component in case the table overflows the component
    background: var(--body-background);
  }

  thead,
  tfoot {
    background-color: var(--header-background);
    color: var(--header-color);
  }

  &.dg-loading {
    &:not(.dg-initialized) {

      thead,
      tfoot {
        background: none;
      }
    }

    tbody {
      animation-name: dataGridShow;
      animation-timing-function: ease-in;
      animation-fill-mode: forwards;
      animation-duration: 0.3s; // only triggers after 300ms
      pointer-events: none; // disable clicks while loading
    }

    &:not(:has(th)) {
      tfoot {
        display: none;
      }
    }
  }

  tr {
    position: relative;
  }

  th,
  td {
    empty-cells: show;
    padding: var(--padding-y) var(--padding-x);
    text-align: left;

    &[tabindex] {
      outline: none;
      word-break: normal;
    }
  }

  th {
    font-weight: bold;
    padding: var(--padding-y-header) var(--padding-x);
  }

  // Prevent overflow on headers and cells
  th,
  td {
    position: relative;
    overflow: hidden;
    text-align: left;
    // These two properties keep content on one line
    text-overflow: ellipsis;
    white-space: nowrap;

    // Use this to allow content to flow over multiple lines
    &.dg-wrap {
      white-space: normal;
      word-break: break-all;
    }
  }

  // Expand on multiple lines on row click
  .dg-expandable {
    cursor: pointer;

    &.dg-expanded td {
      white-space: normal;
      word-break: break-all;
    }
  }

  .dg-selectable {
    font-size: 1em;
    label {
      display: flex;
      align-items: center;
      margin:0;
    }
  }

  .dg-clickable-cell {
    margin: 0;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  // Stick headers
  // https://css-tricks.com/making-tables-with-sticky-header-and-footers-got-a-bit-easier/
  &[sticky] table {

    thead,
    tfoot {
      z-index: 2;
      position: sticky;
      margin: 0;
      border: 0;
    }

    // Use silly value to prevent sub pixel alignment issue
    thead {
      inset-block-start: -1px;
    }

    tfoot {
      inset-block-end: -1px;
    }
  }

  // Pagination icons
  .dg-nav-icon,
  .dg-skip-icon {
    width: 22px;
    height: 22px;
    box-sizing: border-box;
    position: absolute;
    display: block;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(var(--icon-scale, 1));

    &:before,
    &:after {
      content: "";
      display: block;
      box-sizing: border-box;
      position: absolute;
    }
  }

  .dg-nav-icon::before {
    width: 0;
    height: 10px;
    border-top: 5px solid transparent;
    border-bottom: 5px solid transparent;
    border-left: 6px solid;
    top: 6px;
    left: 9px;
  }

  .dg-skip-icon {
    &::before {
      width: 3px;
      height: 10px;
      background: currentColor;
      top: 6px;
      left: 14px;
    }

    &::after {
      width: 0;
      height: 10px;
      border-top: 5px solid transparent;
      border-bottom: 5px solid transparent;
      border-left: 6px solid;
      top: 6px;
      left: 7px;
    }
  }

  .dg-rotate {
    transform: rotate(-180deg);
  }

  // Default actions buttons
  button {
    background-color: var(--btn-background);
    border: solid 1px var(--btn-border-color);
    border-radius: var(--border-radius);
    color: var(--body-color);
    height: 2rem;
    margin: 0 0.2rem;
    padding: 0 0.5rem;
    pointer-events: all;
    text-align: center;
    cursor: pointer;

    &:hover {
      opacity: 0.7;
    }

    &:disabled:hover {
      background-color: inherit;
      border-color: inherit;
    }
  }

  // Form elements
  input[type="checkbox"] {
    margin: 0;
    padding: 0;
  }

  input:not([type="checkbox"],[type="radio"]),
  select {
    background-color: var(--input-background, "#fff");
    color: currentColor;
    box-sizing: border-box;
    border: 1px solid var(--input-border-color, "#f0f0f0");
    border-radius: var(--border-radius);
    height: 2rem;
    margin: 0 0.2rem;
    padding: 0 var(--padding);
    max-width: none;
  }

  input:not([type="checkbox"]),
  select,
  button {
    &:focus {
      box-shadow: 0 0 0 var(--padding-half) rgba(var(--color-rgb), 0.25);
      outline: 0;
    }

    &[disabled],
    &:disabled {
      opacity: 0.35;
      pointer-events: none;
    }
  }

  // Filters (2nd header row)
  thead tr:nth-child(2) th {
    padding: 0 2px 0 0;
    background-color: transparent;

    > * {
      width: 100%;
      border-radius: 0;
      margin: 0;
    }

    input {
      &:focus {
        box-shadow: inset 0px 0px 0px 1px var(--color);
      }
    }
  }

  tbody {
    // Editable
    td.dg-editable-col {
      padding: 0;
      height: 0; // it needs a height for height 100%
    }

    td input.dg-editable {
      width: 100%;
      background: transparent;
      border: 0;
      border-radius: 0;
      margin: 0;
      box-shadow: none;
      height: 100%;

      &:focus {
        box-shadow: inset 0px 0px 0px 1px var(--color);
      }
    }

    tr:has(.dg-selectable [type=radio]:checked) {
      background-color: var(--selected-bgcolor);
    }
  }

  // Empty table or error message (don't use :empty as it would create fouc on load)
  &.dg-empty {
    tbody {
      height: 4rem;
      position: relative;

      &:before {
        position: absolute;
        top: 50%;
        left: 50%;
        content: attr(data-empty);
        transform: translate(-50%, -50%);
        opacity: 0.5;
        font-size: 1.5rem;
        text-align: center;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        width: 90%;
      }
    }

    tr {
      &.dg-fake-row {
        height: 4rem;
        border: none;
      }
    }

    &.dg-network-error {
      table {
        color: #842029;
        background-color: #f8d7da;
        border: 1px solid #f5c2c7;
      }

      tbody {
        height: auto;

        &:before {
          font-size: 1.2rem;
        }

        tr:not(.dg-fake-row) {
          display: none;
        }
      }
    }
  }

  // Zebra/striped rows
  tbody tr {
    &:nth-child(even) {
      background-color: var(--striped-background);
    }

    border-bottom: solid 1px var(--row-border-color);

    &:hover {
      background-color: var(--highlight-color) !important;
    }

    &:focus {
      background-color: var(--highlight-color) !important;
      border-bottom-color: var(--highlight-color) !important;
      outline: none;
    }
  }

  // Meta footer
  tfoot {
    min-width: 280px;

    td {
      padding: var(--padding-y-header) var(--padding-x);
    }

    .dg-page-nav {
      display: flex;
      align-items: center;
      /*min-width: 160px;*/
    }

    .dg-input-page {
      width: 4rem;
    }

    .dg-footer {
      display: flex;
      align-items: center;
      flex-direction: row;
      justify-content: space-between;
    }

    .dg-pagination {
      display: flex;
      text-align: center;

      button {
        // Fix icon inside button
        position: relative;
        width: 2rem;
      }
    }

    .dg-meta {
      // Prevent layout from jumping around
      /*min-width: 160px;*/
      text-align: right;
    }

    &.dg-footer-compact {
      .dg-meta {
        display: none;
      }

      .dg-input-page {
        display: none;
      }
    }
  }

  // Pagination in header
  [aria-sort] {
    cursor: pointer;
    padding-right: 1.5rem;
  }

  [aria-sort="none"]:after,
  [aria-sort="none"]:before,
  [aria-sort="ascending"]:before,
  [aria-sort="descending"]:after {
    right: 0.5rem;
    top: calc(50% - 0.5rem);
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    border-color: rgba(0, 0, 0, 0);
    border-width: 0.25rem;
    margin-left: -0.25rem;
  }

  [aria-sort="none"] {
    &:before {
      border-bottom-color: currentColor;
      opacity: 0.25;
      top: calc(50% - 0.6rem);
    }

    &:after {
      border-top-color: currentColor;
      opacity: 0.25;
      bottom: calc(50% - 0.5rem);
      top: auto;
    }
  }

  [aria-sort="ascending"]:before {
    border-bottom-color: var(--body-color);
    opacity: 0.75;
  }

  [aria-sort="descending"]:after {
    border-top-color: var(--body-color);
    opacity: 0.75;
    bottom: calc(50% - 0.5rem);
    top: auto;
  }
}

[data-bs-theme="dark"] data-grid {
  --scroller-color-lightness: 20%;
  --highlight-color: #43423e;
  --selected-bgcolor: var(--bs-primary-bg-subtle, #031633);
  --body-background: #212529;
  --striped-background: #2c3034;
  --header-background: var(--bs-gray-800, #34373b);
  --header-color: var(--bs-light, #e9ecef);
  --body-color: var(--bs-body-color, #494e53);
  --row-border-color: var(--bs-gray-900, #212325);
}

