// stylelint-disable
@mixin pf-mobile-layout {
  .pf-m-grid.pf-c-table {
    @content;
  }

  .pf-m-grid-md.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--md) {
      @content;
    }
  }

  .pf-m-grid-lg.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--lg) {
      @content;
    }
  }

  .pf-m-grid-xl.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--xl) {
      @content;
    }
  }

  .pf-m-grid-2xl.pf-c-table {
    @media screen and (max-width: $pf-global--breakpoint--2xl) {
      @content;
    }
  }
}

.pf-c-table[class*="pf-m-grid"] {
  // ============================================================ //
  // Start non-conformant variables
  //
  // these variables do not conform to guidelines as styling targets individual elements
  // this is purposeful and necessary to avoid adding selectors to each td/th
  // ============================================================ //

  // Responsive vars
  // ==================================================================

  // Table
  --pf-c-table--responsive--BorderColor: var(--pf-global--BorderColor--300);

  // Body
  --pf-c-table-tbody--responsive--MarginTop: var(--pf-global--spacer--xs);
  --pf-c-table-tbody--m-expanded--before--Top: var(--pf-global--spacer--sm);
  --pf-c-table-tbody--responsive--BorderWidth: var(--pf-global--spacer--sm);

  // Row
  --pf-c-table-tr--responsive--BorderWidth: var(--pf-global--spacer--sm);
  --pf-c-table-tr--responsive--last-child--BorderWidth: var(--pf-global--BorderWidth--sm);
  --pf-c-table-tr--responsive--GridColumnGap: var(--pf-global--spacer--md);
  --pf-c-table-tr--responsive--MarginTop: var(--pf-global--spacer--sm);
  --pf-c-table-tr--responsive--PaddingTop: var(--pf-global--spacer--md);
  --pf-c-table-tr--responsive--PaddingRight: var(--pf-global--spacer--lg);
  --pf-c-table-tr--responsive--md--PaddingRight: var(--pf-global--spacer--md);
  --pf-c-table-tr--responsive--PaddingBottom: var(--pf-global--spacer--md);
  --pf-c-table-tr--responsive--PaddingLeft: var(--pf-global--spacer--lg);
  --pf-c-table-tr--responsive--md--PaddingLeft: var(--pf-global--spacer--md);

  @media screen and (max-width: $pf-global--breakpoint--md) {
    --pf-c-table-tr--responsive--PaddingRight: var(--pf-c-table-tr--responsive--md--PaddingRight);
    --pf-c-table-tr--responsive--PaddingLeft: var(--pf-c-table-tr--responsive--md--PaddingLeft);
  }

  --pf-c-table-tr--responsive--nested-table--PaddingTop: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--nested-table--PaddingRight: var(--pf-global--spacer--lg);
  --pf-c-table-tr--responsive--nested-table--PaddingBottom: var(--pf-global--spacer--xl);
  --pf-c-table-tr--responsive--nested-table--PaddingLeft: var(--pf-global--spacer--lg);

  // Cell
  --pf-c-table-cell--m-grid--hidden-visible--Display: grid;

  // Td
  --pf-c-table-td--responsive--GridColumnGap: var(--pf-global--spacer--md);
  --pf-c-table-cell--responsive--PaddingTop: var(--pf-global--spacer--md);
  --pf-c-table-cell--responsive--PaddingBottom: var(--pf-global--spacer--sm);
  --pf-c-table-cell-th--responsive--PaddingTop: var(--pf-global--spacer--sm);
  --pf-c-table-cell--responsive--PaddingRight: 0;
  --pf-c-table-cell--responsive--PaddingLeft: 0;

  // Compact table
  --pf-c-table--m-compact-tr-td--responsive--PaddingTop: var(--pf-global--spacer--sm);
  --pf-c-table--m-compact-tr-td--responsive--PaddingBottom: var(--pf-global--spacer--sm);

  // Expandable row content
  --pf-c-table__expandable-row-content--responsive--PaddingRight: var(--pf-global--spacer--lg);
  --pf-c-table__expandable-row-content--responsive--PaddingLeft: var(--pf-global--spacer--lg);
  --pf-c-table__expandable-row-content--BackgroundColor: var(--pf-global--BackgroundColor--100);

  // Table check
  --pf-c-table__check--responsive--MarginLeft: var(--pf-global--spacer--sm);
  --pf-c-table__check--responsive--MarginTop: #{pf-size-prem(6px)};

  // Table action
  --pf-c-table__action--responsive--MarginLeft: var(--pf-global--spacer--xl);

  // Table toggle icons
  --pf-c-table__toggle__icon--Transition: .2s ease-in 0s;
  --pf-c-table__toggle--m-expanded__icon--Transform: rotate(180deg);

  // End responsive vars
  // ==================================================================
}

// Check table cell
@include pf-mobile-layout {
  display: grid;
  border: none;

  // Thead
  thead {
    display: none;
    visibility: hidden;
  }

  // Tbody
  tbody {
    display: block;

    &:first-of-type {
      border-top: var(--pf-c-table-tbody--responsive--BorderWidth) solid var(--pf-c-table--responsive--BorderColor);
    }
  }

  // Remove the border from the body inside of the nested table
  table.pf-m-compact > tbody {
    border-top: 0;
  }

  // Table row
  tr:not(.pf-c-table__expandable-row) {
    border-bottom: var(--pf-c-table-tr--responsive--BorderWidth) solid var(--pf-c-table--responsive--BorderColor);
  }

  // The last tr should always have a border width of 1px
  tr:last-child,
  tbody:last-of-type:not(:only-of-type) > tr {
    border-bottom-width: var(--pf-c-table-tr--responsive--last-child--BorderWidth);
  }

  tbody.pf-m-expanded {
    border-bottom: var(--pf-c-table--BorderWidth) solid var(--pf-c-table--BorderColor);

    tr:not(.pf-c-table__expandable-row) {
      border-bottom: 0;
    }

    &:not(:last-of-type) {
      border-bottom: var(--pf-c-table-tbody--responsive--BorderWidth) solid var(--pf-c-table--responsive--BorderColor);
    }
  }

  // Standard table row (non-expandable)
  // exclude expandable rows
  tr:not(.pf-c-table__expandable-row) {
    display: grid;
    grid-template-columns: 1fr;
    height: auto;

    // set subsequent auto column width to max-content
    grid-auto-columns: max-content;
    grid-column-gap: var(--pf-c-table-tr--responsive--GridColumnGap);

    // set base variables to reset later
    padding: var(--pf-c-table-tr--responsive--PaddingTop) var(--pf-c-table-tr--responsive--PaddingRight) var(--pf-c-table-tr--responsive--PaddingBottom) var(--pf-c-table-tr--responsive--PaddingLeft);

    // Reset td padding
    & > * {
      padding: var(--pf-c-table-cell--responsive--PaddingTop) var(--pf-c-table-cell--responsive--PaddingRight) var(--pf-c-table-cell--responsive--PaddingBottom) var(--pf-c-table-cell--responsive--PaddingLeft);
    }

    // remove padding from first th to align with kebab
    th {
      padding-top: var(--pf-c-table-cell-th--responsive--PaddingTop);
    }
  }

  // Remove min-height on buttons
  td.pf-c-table__compound-expansion-toggle,
  th.pf-c-table__compound-expansion-toggle {
    .pf-c-button {
      min-height: 0;
    }
  }

  .pf-c-table.pf-m-compact {
    tr {
      & > * {
        padding-top: var(--pf-c-table--m-compact-tr-td--responsive--PaddingTop);
        padding-bottom: var(--pf-c-table--m-compact-tr-td--responsive--PaddingBottom);
      }
    }
  }

  .pf-c-table__icon > * {
    text-align: left;
  }

  // Standard td, in standard row (non-expandable)
  [data-label] {
    // default pf-hidden-visible() mixin is called in table.scss. redefining variable here
    --pf-c-table-cell--hidden-visible--Display: var(--pf-c-table-cell--m-grid--hidden-visible--Display);

    // responsive layout, set td to two column grid
    grid-column: 1;
    grid-column-gap: var(--pf-c-table-td--responsive--GridColumnGap);
    align-items: start;

    // @mobile, breakpoint small
    @media screen and (min-width: $pf-global--breakpoint--xs) {
      grid-template-columns: 1fr 1.5fr;
      grid-column: 1;
    }

    // set contents of td to start at column two of td grid
    > * {
      @media screen and (min-width: $pf-global--breakpoint--xs) {
        grid-column: 2;
      }
    }

    &::before {
      font-weight: bold;
      text-align: left;
      content: attr(data-label);
    }
  }

  tr > * {
    // Remove first child padding left
    &:first-child {
      --pf-c-table-cell--PaddingLeft: 0;
    }

    // Remove last child padding right
    &:last-child {
      --pf-c-table-cell--PaddingRight: 0;
    }
  }

  // nested tables
  .pf-c-table {
    --pf-c-table-tr--responsive--PaddingTop: var(--pf-c-table-tr--responsive--nested-table--PaddingTop);
    --pf-c-table-tr--responsive--PaddingRight: var(--pf-c-table-tr--responsive--nested-table--PaddingRight);
    --pf-c-table-tr--responsive--PaddingBottom: var(--pf-c-table-tr--responsive--nested-table--PaddingBottom);
    --pf-c-table-tr--responsive--PaddingLeft: var(--pf-c-table-tr--responsive--nested-table--PaddingLeft);

    border: 0;

    tr:not(.pf-c-table__expandable-row) + tr:not(.pf-c-table__expandable-row) {
      --pf-c-table-tr--responsive--PaddingTop: 0;
    }
  }

  .pf-c-table__compound-expansion-toggle {
    // Compound expansion toggle
    // reset compound expansion toggle::before
    &::before {
      position: static;
      border: 0;
    }

    // reset compound expansion toggle::after
    &::after {
      top: auto;
      right: 0;
      left: 0;
    }

    // add bottom border to expansion toggle::after
    &.pf-m-expanded::after {
      border-bottom: var(--pf-c-table__compound-expansion-toggle--BorderTop--BorderWidth) solid var(--pf-c-table__compound-expansion-toggle--BorderTop--BorderColor);
    }

    // reset button padding
    .pf-c-button {
      padding: 0;
    }

    &.pf-m-expanded .pf-c-button::after,
    &:hover .pf-c-button::after {
      border-right: 0;
      border-left: 0;
    }

    &.pf-m-expanded .pf-c-button::before,
    &:hover .pf-c-button::before {
      top: auto;
      bottom: 0;
    }
  }

  // Compound expansion responsive
  // Tbody
  tbody {
    position: relative;

    &::after {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      width: var(--pf-c-table__expandable-row--before--Width);
      content: "";
    }

    &.pf-m-expanded {
      &::after {
        background-color: var(--pf-c-table__expandable-row--before--BackgroundColor);
      }
    }

    > tr > :first-child:not(.pf-c-table__check)::before {
      position: static;
      width: auto;
      background-color: transparent;
    }
  }
  // Compound expansion responsive end


  // Expandable row
  .pf-c-table__expandable-row {
    --pf-c-table-cell--responsive--PaddingTop: 0;
    --pf-c-table-cell--responsive--PaddingRight: 0;
    --pf-c-table-cell--responsive--PaddingBottom: 0;
    --pf-c-table-cell--responsive--PaddingLeft: 0;
    --pf-c-table-cell--PaddingRight: 0;
    --pf-c-table-cell--PaddingLeft: 0;

    display: block;
    max-height: var(--pf-c-table__expandable-row--MaxHeight);  // Overflow scroll should only happen on responsive
    overflow-y: auto;
    border-bottom: none;
    box-shadow: none;

    // cells
    > * {
      position: static;
      display: block;
    }

    // Modifier - expanded tr
    &.pf-m-expanded {
      border-top-color: var(--pf-c-table--BorderColor);
    }

    td:first-child:not(.pf-c-table__check)::before {
      // Border treatment
      display: none;
      visibility: hidden;
    }

    td {
      &.pf-m-no-padding {
        .pf-c-table__expandable-row-content {
          padding: 0;
        }
      }
    }

    &:not(.pf-m-expanded) {
      display: none;
      visibility: hidden;
    }

    .pf-c-table__expandable-row-content {
      padding-right: var(--pf-c-table__expandable-row-content--responsive--PaddingRight);
      padding-left: var(--pf-c-table__expandable-row-content--responsive--PaddingLeft);
    }
  }

  // Set defaults
  tr:not(.pf-c-table__expandable-row) {
    .pf-c-table__toggle,
    .pf-c-table__check,
    .pf-c-table__action {
      width: auto;
      padding: 0;
    }
  }

  .pf-c-table__toggle {
    grid-row-start: 20;
    grid-column: -1;
    justify-self: end;
    padding-right: 0;
  }

  // Check table cell
  .pf-c-table__check {
    margin-top: var(--pf-c-table__check--responsive--MarginTop);
    margin-left: var(--pf-c-table__check--responsive--MarginLeft);
    grid-row-start: 1;
    grid-column-start: 2;

    // if action is present, add margin left to accommodate checkbox
    & ~ .pf-c-table__action {
      margin-left: var(--pf-c-table__action--responsive--MarginLeft);
    }
  }

  // Action cell
  .pf-c-table__action {
    grid-row-start: 1;
    grid-column-start: 2;
    text-align: right;

    // @smallest breakpoint
    @media screen and (max-width: $pf-global--breakpoint--sm) {
      grid-row-start: 1;
      grid-column-start: 2;
      margin-left: 0;
    }
  }

  .pf-c-table__inline-edit-action {
    grid-column: 2;
    grid-row: 2;
  }

  // Toggles
  .pf-c-table__toggle {
    .pf-c-button > * {
      transition: var(--pf-c-table__toggle__icon--Transition);
    }

    .pf-c-button.pf-m-expanded > * {
      transform: var(--pf-c-table__toggle--m-expanded__icon--Transform);
    }
  }

  // Fit content
  .pf-m-fit-content {
    width: auto;
    white-space: normal;
  }
}

// stylelint-enable
