/* =============================================
   Data Table Component — UI-0067, UI-0068
   ============================================= */

/* --- Table Wrapper --- */
.table-wrap {
  background-color: var(--color-white);
  border: 1px solid var(--color-gray-200);
  border-radius: var(--radius-lg); /* 12px */
  overflow: hidden;
}

/* --- Scroll Container for horizontal overflow --- */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* --- Base Table --- */
.table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
}

/* --- Table Head --- */
.table thead {
  background-color: var(--color-gray-50);
}

.table thead tr {
  border-bottom: 1px solid var(--color-gray-200);
}

.table th {
  padding: var(--space-3) var(--space-4);
  font-size: 0.8125rem; /* 13px */
  font-weight: var(--font-semibold);
  color: var(--color-gray-500);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: left;
  white-space: nowrap;
}

/* --- Table Body --- */
.table tbody tr {
  min-height: 56px;
  border-bottom: 1px solid var(--color-gray-100);
  transition: background-color var(--duration-fast) var(--ease-default);
}

.table tbody tr:last-child {
  border-bottom: none;
}

.table tbody tr:hover {
  background-color: var(--color-gray-50);
}

.table td {
  padding: var(--space-4);
  color: var(--color-gray-700);
  vertical-align: middle;
}

/* Ensure rows have a minimum height via td */
.table td,
.table th {
  min-height: 56px;
  box-sizing: border-box;
}

/* --- Sortable Column Header (UI-0068) --- */
.table__sort-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  background: none;
  border: none;
  cursor: pointer;
  font-size: inherit;
  font-weight: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  padding: 0;
  transition: color var(--duration-fast) var(--ease-default);
}

.table__sort-btn:hover {
  color: var(--color-gray-900);
}

/* Sort icon container */
.table__sort-icon {
  display: inline-flex;
  flex-direction: column;
  gap: 1px;
  opacity: 0.4;
  transition: opacity var(--duration-fast) var(--ease-default);
}

.table__sort-icon svg {
  width: 12px;
  height: 12px;
  stroke: currentColor;
  fill: none;
}

/* Active sort state */
.table th.is-sort-asc .table__sort-btn,
.table th.is-sort-desc .table__sort-btn {
  color: var(--color-electric-blue);
}

.table th.is-sort-asc .table__sort-icon,
.table th.is-sort-desc .table__sort-icon {
  opacity: 1;
  color: var(--color-electric-blue);
}

.table th.is-sort-asc .table__sort-icon svg.sort-down,
.table th.is-sort-desc .table__sort-icon svg.sort-up {
  opacity: 0.3;
}

/* --- Table Empty State --- */
.table__empty {
  padding: var(--space-16) var(--space-8);
  text-align: center;
}

.table__empty-icon {
  margin: 0 auto var(--space-4);
  width: 48px;
  height: 48px;
  color: var(--color-gray-300);
}

.table__empty-text {
  font-size: var(--text-sm);
  color: var(--color-gray-300);
}

/* --- Truncated Cell Text --- */
.table__cell-truncate {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Sticky First Column --- */
.table .table__col-sticky {
  position: sticky;
  left: 0;
  background-color: inherit;
  z-index: 1;
}

.table thead .table__col-sticky {
  background-color: var(--color-gray-50);
}

/* --- Table Action Buttons --- */
.table__actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.table__action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid var(--color-border, #e2e8f0);
  background: transparent;
  color: var(--color-text-secondary, #64748b);
  cursor: pointer;
  transition: all 150ms ease;
}

.table__action-btn:hover {
  background: var(--color-gray-50, #f8fafc);
  color: var(--color-text-primary, #0f172a);
  border-color: var(--color-gray-300, #cbd5e1);
}

.table__action-btn--danger:hover {
  background: rgba(239, 68, 68, 0.08);
  color: #ef4444;
  border-color: rgba(239, 68, 68, 0.3);
}

/* =============================================
   Responsive Table — Mobile
   ============================================= */

/* Scroll wrappers — ensure all table containers scroll horizontally */
.table-scroll-wrapper,
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Minimum font size for readability on mobile (iPhone = 13px min) */
@media (max-width: 768px) {
  .table {
    font-size: 13px;
    min-width: 600px; /* force horizontal scroll rather than squish */
  }

  .table th,
  .table td {
    font-size: 13px;
    padding: var(--space-2, 8px) var(--space-3, 12px);
    white-space: nowrap;
  }

  /* Allow keyword and name cells to wrap if needed */
  .table td:first-child,
  .table .table-keyword {
    white-space: normal;
    word-break: break-word;
    min-width: 120px;
  }

  /* Hide less important columns on small screens */
  .table .col-hide-mobile {
    display: none;
  }

  /* Table actions: compact on mobile */
  .table-actions {
    display: flex;
    gap: 4px;
    flex-wrap: nowrap;
  }

  /* Ensure cards don't overflow */
  .card {
    overflow-x: auto;
  }

  /* Pipeline / client table wrappers */
  .pipeline-table-wrapper,
  .client-table-wrapper {
    overflow-x: auto;
  }

  /* Admin filters: allow wrapping on mobile */
  .admin-filters__group {
    flex-wrap: wrap;
    gap: var(--space-2, 8px);
  }

  .admin-filters__group > input,
  .admin-filters__group > select {
    min-width: 0;
    flex: 1 1 140px;
  }
}

@media (max-width: 480px) {
  .table {
    font-size: 12px;
    min-width: 500px;
  }

  .table th,
  .table td {
    padding: var(--space-2, 8px);
    font-size: 12px;
  }

  .table__cell-truncate {
    max-width: 140px;
  }

  .table__action-btn {
    width: 28px;
    height: 28px;
  }

  .table-wrap {
    border-radius: var(--radius-md, 8px);
  }
}
