@use '../variables' as v;
@use '../mixins' as m;

// ─────────────────────────────────────────────
// Table wrapper
// ─────────────────────────────────────────────
.table-wrap {
  @include m.card-base;
  overflow: hidden;
}

.table-toolbar {
  @include m.flex-between;
  padding: 14px 20px;
  gap: 12px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border-color);
  transition: all 0.2s ease;

  &.has-selection {
    background: var(--active-bg);
  }

  &-left,
  &-right {
    @include m.flex-start;
    gap: 8px;
    flex-wrap: wrap;
  }

  .selection-status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 13.5px;
    font-weight: 600;
    color: var(--active-text);
  }

  .selection-count {
    color: var(--active-text);
    font-weight: 600;
  }

  .re-selected-count {
    font-size: 13.5px;
    font-weight: 600;
    color: var(--active-text);
    padding-right: 4px;
    white-space: nowrap;
  }

  @media (max-width: 640px) {
    flex-direction: column;
    align-items: stretch;
    padding: 10px 12px;

    &-left,
    &-right {
      width: 100%;
      justify-content: space-between;

      .table-search {
        flex: 1;
        width: 100%;

        input {
          width: 100%;
        }
      }
    }
  }
}

.table-scroll {
  overflow-x: auto;
  @include m.scrollbar-thin;
}

// ─────────────────────────────────────────────
// Table
// ─────────────────────────────────────────────
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: v.$font-size-sm;

  thead {
    background: var(--bg-page);
    border-bottom: 1px solid var(--border-color);
  }

  th {
    padding: 10px 16px;
    font-size: v.$font-size-xs;
    font-weight: 600;
    color: var(--text-muted);
    text-align: left;
    white-space: nowrap;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    user-select: none;

    &.sortable {
      cursor: pointer;

      &:hover { color: var(--text-primary); }

      .sort-icon {
        display: inline-flex;
        flex-direction: column;
        gap: 1px;
        margin-left: 4px;
        vertical-align: middle;

        svg { width: 12px; height: 12px; color: var(--text-muted); }
      }

      &.sort-asc  .sort-icon svg:first-child  { color: v.$primary; }
      &.sort-desc .sort-icon svg:last-child   { color: v.$primary; }
    }
  }

  td {
    padding: 12px 16px;
    color: var(--text-secondary);
    vertical-align: middle;
    border-bottom: 1px solid var(--border-color);
  }

  tbody tr {
    transition: background v.$transition-fast;
    background: var(--bg-surface);

    &:hover { background: var(--bg-hover); }
    &:last-child td { border-bottom: none; }

    &.is-selected { background: var(--active-bg); }
  }
}

// ─────────────────────────────────────────────
// Table footer / pagination
// ─────────────────────────────────────────────
.table-footer {
  @include m.flex-between;
  padding: 12px 20px;
  border-top: 1px solid var(--border-color);
  gap: 12px;
  flex-wrap: wrap;

  &-info {
    font-size: v.$font-size-xs;
    color: var(--text-muted);
  }
}

.pagination {
  @include m.flex-start;
  gap: 4px;
}

.page-btn {
  @include m.flex-center;
  min-width: 30px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius-sm;
  font-size: v.$font-size-xs;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--bg-surface);
  cursor: pointer;
  transition: background v.$transition-base, border-color v.$transition-base, color v.$transition-base;

  &:hover { background: var(--bg-hover); border-color: var(--border-color); }

  &.is-active {
    background: v.$primary;
    border-color: v.$primary;
    color: v.$white;
  }

  &:disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }

  svg { width: 14px; height: 14px; }
}

// ─────────────────────────────────────────────
// Table search input
// ─────────────────────────────────────────────
.table-search {
  position: relative;

  svg {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    color: var(--text-muted);
    pointer-events: none;
  }

  input {
    -webkit-appearance: none;
    appearance: none;
    height: 34px;
    padding: 0 10px 0 32px;
    border: 1px solid var(--border-color);
    border-radius: v.$radius;
    font-size: v.$font-size-sm;
    color: var(--text-primary);
    background: var(--bg-surface);
    width: 220px;
    transition: border-color v.$transition-base, box-shadow v.$transition-base;

    &::placeholder { color: var(--text-muted); }
    &:focus { outline: none; border-color: var(--active-text); @include m.focus-ring; }
  }
}

// ─────────────────────────────────────────────
// Select / filter dropdown
// ─────────────────────────────────────────────
.table-filter-select {
  height: 34px;
  padding: 0 30px 0 10px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  font-size: v.$font-size-sm;
  color: var(--text-primary);
  background: var(--bg-surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%239ca3af'%3E%3Cpath fill-rule='evenodd' d='M5.23 7.21a.75.75 0 011.06.02L10 11.17l3.71-3.94a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z' clip-rule='evenodd'/%3E%3C/svg%3E") no-repeat right 8px center / 16px;
  appearance: none;
  cursor: pointer;
  transition: border-color v.$transition-base;

  &:focus { outline: none; border-color: var(--active-text); }
}

// Checkbox in table
.table-checkbox {
  width: 16px;
  height: 16px;
  accent-color: v.$primary;
  cursor: pointer;
}

// Contact email cell
.contact-email-cell {
  @include m.flex-start;
  gap: 10px;
}

.contact-avatar {
  width: 28px;
  height: 28px;
  border-radius: v.$radius-full;
  background: var(--active-bg);
  @include m.flex-center;
  font-size: v.$font-size-xs;
  font-weight: 600;
  color: var(--active-text);
  flex-shrink: 0;
  border: 1px solid var(--border-color);
}

// Rate cell with mini bar
.rate-cell {
  &-value {
    font-weight: 600;
    color: var(--text-primary);
  }

  &-bar {
    height: 2px;
    background: var(--bg-hover-strong);
    border-radius: v.$radius-full;
    margin-top: 4px;
    width: 60px;

    &-fill {
      height: 100%;
      background: v.$primary;
      border-radius: v.$radius-full;
    }
  }
}
