//
// Tables
// --------------------------------------------------

table {
  background-color: $table-bg;

  col[class*="col-"] {
    position: static;
    display: table-column;
    float: none;
  }

  td,
  th {
    &[class*="col-"] {
      position: static;
      display: table-cell;
      float: none;
    }
  }
}

caption {
  padding-top: $table-cell-padding;
  padding-bottom: $table-cell-padding;
  text-align: left;

  @include themes(color, color-new-body-light);
}

th {
  text-align: left;
}

.table {
  width: 100%;
  max-width: 100%;
  margin-bottom: $line-height-computed;

  > thead,
  > tbody,
  > tfoot {
    > tr {
      > th,
      > td {
        vertical-align: top;
        line-height: $grid-unit-y * 2;
        font-weight: $font-weight-regular;
        padding: (ceil($grid-unit-y * 3) - 1) ceil($grid-unit-x * 2)
          ceil($grid-unit-y * 3);

        @include themes(color, color-new-heading);

        &:first-child {
          padding-left: $grid-unit-y * 3;
        }

        &:last-child {
          padding-right: $grid-unit-y * 3;
        }
      }
    }
  }

  > thead,
  > tbody {
    > tr {
      > th,
      > td {
        @include themes-border-top(table-border-color);
        @include themes-border-bottom(table-border-color);
      }
    }
  }

  > thead > tr > th {
    font-weight: $font-weight-medium;
    font-size: $font-size-base;
    vertical-align: middle;
    text-transform: uppercase;
    padding-top: $grid-unit-y * 1.5;
    padding-bottom: $grid-unit-y * 1.5;

    @include themes(color, color-new-body-light);
    @include themes-border-bottom(table-border-color);
  }

  > tfoot {
    > tr {
      > td {
        text-align: right;
        text-transform: uppercase;
        padding-top: $grid-unit-y * 1.5;
        padding-bottom: $grid-unit-y * 1.5;
        font-weight: $font-weight-medium;

        @include themes(color, color-new-body-light);
      }

      &:first-child {
        > td {
          padding-top: $grid-unit-y * 3;
        }
      }
    }
  }

  > caption + thead,
  > colgroup + thead,
  > thead:first-child {
    > tr:first-child {
      > th,
      > td {
        border-top: 0;
      }
    }
  }

  > tbody + tbody {
    @include themes-border-top(table-border-color, 2px);
  }

  // Nesting
  .table {
    @include themes(background-color, table-nesting-bg);
  }

  // Elements

  &-bold {
    &,
    > th,
    > td {
      font-weight: $font-weight-bold;
    }
  }

  // Table Variations

  &-condensed {
    > thead,
    > tbody,
    > tfoot {
      > tr {
        > th,
        > td {
          padding: ($table-condensed-cell-padding - 1)
            $table-condensed-cell-padding $table-condensed-cell-padding;
        }
      }
    }
  }

  &-bordered {
    @include themes-border(table-border-color);

    > thead,
    > tbody,
    > tfoot {
      > tr {
        > th,
        > td {
          @include themes-border(table-border-color);
        }
      }
    }
  }

  &-striped {
    > tbody > tr:nth-of-type(odd) {
      @include themes(background-color, table-bg-accent);
    }
  }

  &-hover {
    > tbody > tr:hover {
      @include themes(background-color, table-bg-hover);
    }
  }

  &-grey {
    > thead,
    > tbody,
    > tfoot {
      > tr {
        > th {
          @include themes(background-color, table-bg-grey);
        }
      }
    }

    > thead,
    > tbody,
    > tfoot {
      > tr {
        > td {
          @include themes(background-color, table-bg-grey);
        }
      }
    }
  }

  &-no-paddings-hor {
    > thead,
    > tbody,
    > tfoot {
      > tr {
        > th,
        > td {
          &:first-child {
            padding-left: 0;
          }

          &:last-child {
            padding-right: 0;
          }
        }
      }
    }
  }

  &-responsive {
    &,
    [data-ng-style] {
      @include styled-scroll-hor();
    }

    min-height: 0.01%; // Workaround for IE9 bug (see https://github.com/twbs/bootstrap/issues/14837)
    overflow-x: auto;

    @include media-breakpoint-down(md) {
      width: 100%;
      margin-bottom: ($line-height-computed * 0.75);
      overflow-y: hidden;
      -ms-overflow-style: -ms-autohiding-scrollbar;

      @include themes-border(table-border-color);

      > .table {
        margin-bottom: 0;

        > thead,
        > tbody,
        > tfoot {
          > tr {
            > th,
            > td {
              white-space: nowrap;
            }
          }
        }
      }

      > .table-bordered {
        border: 0;

        // Nuke the appropriate borders so that the parent can handle them
        > thead,
        > tbody,
        > tfoot {
          > tr {
            > th:first-child,
            > td:first-child {
              border-left: 0;
            }
            > th:last-child,
            > td:last-child {
              border-right: 0;
            }
          }
        }

        // Only nuke the last row's bottom-border in `tbody` and `tfoot` since
        // chances are there will be only one `tr` in a `thead` and that would
        // remove the border altogether.
        > tbody,
        > tfoot {
          > tr:last-child {
            > th,
            > td {
              border-bottom: 0;
            }
          }
        }
      }
    }
  }

  &-vertical-align-middle {
    &,
    & > td {
      vertical-align: middle !important;
    }
  }

  &-center {
    > thead > tr > th,
    > tbody > tr > td {
      text-align: center;

      &:first-child {
        text-align: left;
      }

      &:last-child {
        text-align: right;
      }
    }
  }

  &-no-border-top-bottom {
    > thead,
    > tbody,
    > tfoot {
      > tr:first-child {
        > th,
        > td {
          border-top-width: 0;
        }
      }

      > tr:last-child {
        > th,
        > td {
          border-bottom-width: 0;
        }
      }
    }
  }

  &-fixed {
    table {
      > thead,
      > tbody,
      > tfoot {
        > tr {
          > th,
          > td {
            width: $grid-unit-y * 20;
            min-width: $grid-unit-y * 20;
            max-width: 0;
          }

          > th.table-fixed-sm,
          > td.table-fixed-sm {
            width: $grid-unit-y * 15;
            min-width: $grid-unit-y * 15;
          }

          > th.table-fixed-lg,
          > td.table-fixed-lg {
            width: $grid-unit-y * 30;
            min-width: $grid-unit-y * 30;
          }
        }
      }
    }

    &-headers {
      //
    }

    &-body {
      overflow-y: auto;
      overflow-x: hidden;

      @include styled-scroll-vert();
    }
  }
}

td.table-border-left {
  @include themes-border-left(table-border-color);
}

// Table backgrounds

@include table-row-variant-themes(
  "active",
  table-bg-active,
  table-bg-active-hover
);
@include table-row-variant("success", $state-success-bg);
@include table-row-variant("info", $state-info-bg);
@include table-row-variant("warning", $state-warning-bg);
@include table-row-variant("danger", $state-danger-bg);
