@use '../../vars.scss' as *;
@use '../../fonts.scss' as *;
@use 'sass:color' as color;

// MD Datatable Theme
// Clean, minimalist Material Design inspired theme for ngx-datatable
// Works on white, grey and dark backgrounds
// --------------------------------------------------

.md-datatable-style {
  box-sizing: border-box;
  font-size: 13px;
  font-family: $base-font-family;

  // Remove default shadows and borders
  box-shadow: none !important;
  border: none;

  * {
    box-sizing: border-box;
  }

  // Header styling
  .datatable-header {
    background: $Light-Gray-2;
    border: none;
    box-shadow: none;
    overflow: visible;

    height: 46px !important;

    .datatable-header-inner {
      padding: 0;
    }

    .datatable-header-cell {
      padding: 13px 8px 10px 8px;

      background: transparent;

      &:not(:first-child) {
        border-left: 2px solid $White;
      }

      font-family: $font-header;
      font-size: 14px;
      font-weight: 300;
      text-transform: uppercase;
      letter-spacing: 0.5px;
      color: $Dark-Gray-2;
      vertical-align: middle;

      .datatable-header-cell-template-wrap {
        display: flex;
        align-items: center;
        height: 100%;
        position: relative;
      }

      .datatable-header-cell-label {
        line-height: 1.2;
        flex: 1;
      }

      // Sortable columns
      &.sortable {
        cursor: pointer;
        user-select: none;

        .datatable-header-cell-template-wrap {
          padding-right: 20px;
        }

        .sort-btn {
          display: inline-block;
          position: absolute;
          right: 0;
          width: 16px;
          height: 16px;
          color: $Light-Gray-1;

          &::after {
            // content: '▼';
            font-size: 10px;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            transition: transform 0.2s ease;
          }
        }

        &.sort-active .sort-btn {
          color: $Dark-Gray-2;
        }

        &.sort-asc .sort-btn::after {
          transform: translateY(-50%) rotate(180deg);
        }

        &.sort-desc .sort-btn::after {
          transform: translateY(-50%);
        }

        &:hover .sort-btn {
          color: $Dark-Gray-2;
        }
      }
    }
  }

  // Body styling - NO zebra by default, all rows white
  .datatable-body {
    overflow-x: auto;
    overflow-y: auto;

    .datatable-row-wrapper {
      border: none;
    }

    .datatable-body-row {
      border: none;

      &:last-child {
        border-bottom: none;
      }

      .datatable-body-cell {
        font-family: 'Open Sans', Arial, sans-serif;
        padding: 8px;
        font-size: 13px;
        line-height: 1;
        color: $Black-Color;
        background: $White;
        vertical-align: middle;
        border: none;

        .datatable-body-cell-label {
          display: block;
          width: 100%;
        }
      }

      // Hover effect
      &:hover .datatable-body-cell {
        background: color.mix($Light-Gray-2, $White, 30%) !important;
      }

      // Active/selected row
      &.active .datatable-body-cell {
        background: color.mix($Link, $White, 10%) !important;
      }
    }

    // Row detail expansion
    .datatable-row-detail {
      background: $White;
      padding: 16px 20px;
      overflow: visible;
    }
  }

  // Footer styling
  .datatable-footer {
    background: $Light-Gray-2;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    padding: 8px 0px 8px 8px;
    font-size: 12px;
    color: $Dark-Gray-2;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .page-count {
      line-height: 1.5;
      padding: 0;
    }

    .datatable-pager {
      ul {
        li {
          &.disabled a {
            opacity: 0.3;
            cursor: not-allowed;
            pointer-events: none;
          }

          &.active a {
            background: $Dark-Gray-2;
            color: $White;
            font-weight: 600;
          }

          a {
            min-width: 24px;
            height: 24px;
            padding: 0 10px;
            border-radius: 4px;
            color: $Dark-Gray-2;
            text-decoration: none;
            font-size: 12px;
            transition: background-color 0.2s ease;

            &:hover:not(.disabled) {
              // background: color.mix($Light-Gray-1, $White, 20%);
              color: $Red;
            }
          }
        }
      }

      .datatable-icon-left::before,
      .datatable-icon-right::before,
      .datatable-icon-skip::before,
      .datatable-icon-prev::before {
        font-size: 14px;
      }
    }
  }

  // Group header styling
  .datatable-group-header {
    background: color.mix($Light-Gray-2, $White, 50%);
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    padding: 12px 20px;
    font-weight: 600;
    font-size: 13px;
    color: $Dark-Gray-1;
  }

  // Selection checkbox styling
  .datatable-checkbox {
    input[type='checkbox'] {
      width: 16px;
      height: 16px;
      cursor: pointer;
    }
  }

  // Summary row styling
  .datatable-summary-row {
    background: $Light-Gray-2;
    border-top: 2px solid $Dark-Gray-2;

    .datatable-body-cell {
      background: $Light-Gray-2;
      font-weight: 600;
      padding: 12px 20px;
    }
  }

  // Responsive adjustments
  @media (max-width: 768px) {
    font-size: 12px;

    .datatable-header .datatable-header-cell {
      padding: 10px 12px;
      font-size: 10px;
    }

    .datatable-body .datatable-body-row .datatable-body-cell {
      padding: 12px;
      font-size: 12px;
    }

    .datatable-footer {
      flex-direction: column;
      gap: 8px;
      align-items: flex-start;
      padding: 8px 12px;
    }
  }
}

// Modifier: Enable zebra striping with white even rows
.md-datatable-style.zebra-white {
  .datatable-header {
    background: $White;
    .datatable-header-cell {
      &:not(:first-child) {
        border-left: 2px solid $Light-Gray-3;
      }
    }
  }
  .datatable-body {
    .datatable-body-row {
      // Odd rows transparent (inherit page background)
      &.datatable-row-even .datatable-body-cell {
        background: transparent;
      }

      // Even rows white
      &.datatable-row-odd .datatable-body-cell {
        background: $White;
      }
    }

    .datatable-row-detail {
      background: $White;
    }
  }
  .datatable-footer {
    background: $White;
  }
}

// Modifier: Enable zebra striping with grey even rows
.md-datatable-style.zebra-grey {
  .datatable-body {
    .datatable-body-row {
      // Odd rows transparent (inherit page background)
      &.datatable-row-even .datatable-body-cell {
        background: transparent;
      }

      // Even rows light grey
      &.datatable-row-odd .datatable-body-cell {
        background: $Light-Gray-2;
      }
    }

    .datatable-row-detail {
      background: $Light-Gray-2;
    }
  }
}

// ═══════════════════════════════════════════════════════════════════════════
// MD Cards Grid Table Style
// Makes md-grid look like ngx-datatable
// ═══════════════════════════════════════════════════════════════════════════
//
// USAGE:
// <md-grid class="md-grid-table" ...>
// <md-grid class="md-grid-table zebra-white" ...>
// <md-grid class="md-grid-table zebra-grey" ...>
//
// ═══════════════════════════════════════════════════════════════════════════

.md-grid-table {
  // ═════════════════════════════════════════════════════════════════════════
  // HOST STYLES
  // ═════════════════════════════════════════════════════════════════════════

  &.md-grid,
  .md-grid {
    @extend .body-6;
    background: tansparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // HEADER
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-header {
    background: $Light-Gray-2;
    border: none;
    box-shadow: none;
  }

  .md-grid-header-inner {
    padding: 0;
    width: 100%;
  }

  .md-grid-header-cell {
    padding: 13px 8px 10px 8px;
    background: transparent;
    font-family: $font-header;
    font-size: 14px;
    font-weight: 300;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: $Dark-Gray-2;
    vertical-align: middle;

    &:not(:first-child) {
      border-left: 2px solid $White;
    }

    &.sortable {
      cursor: pointer;
      user-select: none;

      &:hover {
        background: transparent;
      }
    }

    &.sort-active {
      color: $Dark-Gray-2;
    }
  }

  .md-grid-header-cell-label {
    line-height: 1.2;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // SORT ICONS
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-sort-icon {
    color: $Light-Gray-1;

    &.md-grid-sort-asc,
    &.md-grid-sort-desc {
      color: $Dark-Gray-2;
    }
  }

  // ═════════════════════════════════════════════════════════════════════════
  // BODY
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-body {
    padding: 0;
    background: transparent;
    gap: 0 !important;

    &::-webkit-scrollbar-corner {
      background: transparent; 
    }
  }

  .md-grid-group-rows {
    gap: 0 !important;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // CARD (ROW) - Table row style
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-card {
    background: $White;
    border: none;
    border-radius: 0;
    box-shadow: none;

    // Hover effect same as ngx-datatable
    &:hover {
      background: color.mix($Light-Gray-2, $White, 30%);
      box-shadow: none;
      transform: none;
    }

    // Active/selected row
    &.md-grid-card-editing {
      background: color.mix($Link, $White, 10%) !important;
      border: none;
      box-shadow: none;
    }
  }

  // ═════════════════════════════════════════════════════════════════════════
  // CELL
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-cell {
    padding: 8px;
    @extend .body-6;
    color: $Black-Color;
    background: transparent;
    max-height: 30px;
    border: none;
  }

  .md-grid-cell-value {
    display: block;
    width: 100%;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // ACTIONS CELL
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-actions-cell {
    border-left: none;
    background: transparent;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // FOOTER / PAGINATION
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-footer {
    background: $Light-Gray-2;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    padding: 8px;
    font-size: 12px;
    color: $Dark-Gray-2;
  }

  .md-grid-pager {
    gap: 2px;
  }

  .md-grid-pager-btn {
    min-width: 24px;
    height: 24px;
    padding: 0 10px;
    border-radius: 4px;
    border: none;
    background: transparent;
    color: $Dark-Gray-2;
    font-size: 12px;

    &:hover:not(:disabled) {
      background: transparent;
      color: $Red;
    }

    &:disabled {
      opacity: 0.3;
      cursor: not-allowed;
    }
  }

  .md-grid-pager-info {
    padding: 0 8px;
    font-weight: 400;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // GROUP HEADER
  // ═════════════════════════════════════════════════════════════════════════

  .md-grid-group-header {
    background: color.mix($Light-Gray-2, $White, 50%);
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    padding: 12px 20px;
    margin-bottom: 0;
    border-radius: 0;
    font-weight: 600;
    font-size: 13px;
    color: $Dark-Gray-1;
  }

  // ═════════════════════════════════════════════════════════════════════════
  // RESPONSIVE
  // ═════════════════════════════════════════════════════════════════════════

  @media (max-width: 768px) {
    font-size: 12px;

    .md-grid-footer {
      flex-direction: column;
      gap: 8px;
      align-items: flex-start;
      padding: 8px 12px;
    }
  }

  @media (max-width: 540px) {
    .md-grid-body {
      padding: 6px;
    }

    .horizontal-scroll-container,
    .md-grid-group-rows {
      display: flex;
      flex-direction: column;
      gap: 10px !important;
    }

    .md-grid-card {
      border-radius: 8px;
    }

    .md-grid-cell {
      padding: 7px 12px;
      max-height: none;
    }

    .md-grid-actions-cell,
    .md-grid-column-actions-cell {
      padding: 7px 4px;
    }

    .md-grid-first-action-cell,
    .md-grid-row-actions-cell {
      margin-left: auto;
    }

    .md-grid-last-action-cell,
    .md-grid-row-actions-cell {
      padding-right: 16px;
    }
  }
}

// ═════════════════════════════════════════════════════════════════════════
// Modifier: zebra-white (white header, zebra rows)
// ═════════════════════════════════════════════════════════════════════════

.md-grid-table.zebra-white {
  .md-grid-header {
    background: $White;
  }

  .md-grid-header-cell {
    &:not(:first-child) {
      border-left: 2px solid $Light-Gray-3;
    }
  }

  .md-grid-card {
    &:nth-child(odd) {
      background: transparent;
    }

    &:nth-child(even) {
      background: $White;
    }

    &:hover {
      background: color.mix($Light-Gray-2, $White, 30%) !important;
    }
  }

  .md-grid-group-rows {
    .md-grid-card {
      &:nth-child(odd) {
        background: transparent;
      }

      &:nth-child(even) {
        background: $White;
      }

      &:hover {
        background: color.mix($Light-Gray-2, $White, 30%) !important;
      }
    }
  }

  .md-grid-footer {
    background: $White;
  }
}

// ═════════════════════════════════════════════════════════════════════════
// Modifier: zebra-grey (grey header, zebra with grey)
// Same as md-datatable-style.zebra-grey
// ═════════════════════════════════════════════════════════════════════════

.md-grid-table.zebra-grey {
  .md-grid-card {
    // White rows (odd)
    &:nth-child(odd) {
      background: $White;

      &:hover {
        background: color.mix($Light-Gray-2, $White, 30%) !important;
      }
    }

    // Grey rows (even)
    &:nth-child(even) {
      background: $Light-Gray-2;

      &:hover {
        background: color.mix($Light-Gray-2, $White, 30%) !important;
      }
    }
  }

  .md-grid-group-rows {
    .md-grid-card {
      &:nth-child(odd) {
        background: $White;

        &:hover {
          background: color.mix($Light-Gray-2, $White, 30%) !important;
        }
      }

      &:nth-child(even) {
        background: $Light-Gray-2;

        &:hover {
          background: color.mix($Light-Gray-2, $White, 30%) !important;
        }
      }
    }
  }
}

// Dark background variant
.bg-dark .md-datatable-style,
.dark-mode .md-datatable-style,
.md-datatable-style.dark-theme {
  .datatable-header {
    background: color.mix($Dark-Gray-2, $Black-Color, 90%);

    .datatable-header-cell {
      background: color.mix($Dark-Gray-2, $Black-Color, 90%);
      color: $White;
      border-bottom-color: rgba(255, 255, 255, 0.1);
    }
  }

  .datatable-body {
    .datatable-body-row {
      border-bottom-color: rgba(255, 255, 255, 0.08);

      .datatable-body-cell {
        background: color.mix($Dark-Gray-1, $Black-Color, 85%);
        color: $White;
      }

      &:hover .datatable-body-cell {
        background: color.mix($Dark-Gray-2, $Black-Color, 75%) !important;
      }

      &.active .datatable-body-cell {
        background: color.mix($Link-on-dark, $Black-Color, 30%) !important;
      }
    }

    .datatable-row-detail {
      background: color.mix($Dark-Gray-1, $Black-Color, 85%);
      border-bottom-color: rgba(255, 255, 255, 0.08);
      color: $White;
    }
  }

  .datatable-footer {
    background: color.mix($Dark-Gray-2, $Black-Color, 90%);
    border-top-color: rgba(255, 255, 255, 0.1);
    color: $White;
    height: 25px !important;
  }

  .datatable-group-header {
    background: color.mix($Dark-Gray-2, $Black-Color, 80%);
    color: $White;
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }

  .datatable-summary-row {
    background: color.mix($Dark-Gray-2, $Black-Color, 90%);

    .datatable-body-cell {
      background: color.mix($Dark-Gray-2, $Black-Color, 90%);
      color: $White;
    }
  }

  // Zebra for dark theme
  &.zebra-white,
  &.zebra-grey {
    .datatable-body {
      .datatable-body-row {
        &.datatable-row-odd .datatable-body-cell {
          background: transparent; // Uses dark page background
        }

        &.datatable-row-even .datatable-body-cell {
          background: color.mix($Dark-Gray-1, $Black-Color, 85%);
        }
      }
    }
  }
}
