/**
 * 全项目通用表格样式
 *
 * 使用说明：
 * 1. 定义基础表格样式类 .core-table
 * 2. 提供多个变体：compact（紧凑）、spacious（宽松）、bordered（带边框）
 * 3. 所有表格样式基于 variables.css 的 CSS 变量
 * 4. 支持响应式和自定义扩展
 */

/* ==================== 基础表格样式 ==================== */

.core-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-family-base);
  font-size: var(--font-size-sm);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  table-layout: auto;
}

/* 表头 */
.core-table thead {
  position: relative;
}

.core-table thead th {
  background: linear-gradient(180deg, var(--bg-tertiary) 0%, var(--bg-secondary) 100%);
  border-bottom: var(--border-width-medium) solid var(--border-color);
  padding: var(--spacing-sm) var(--spacing-md);
  font-weight: var(--font-weight-semibold);
  font-size: var(--font-size-sm);
  color: var(--text-primary);
  text-align: left;
  white-space: nowrap;
  user-select: none;
  position: relative;
}

/* 表头右边框分隔线（可选） */
.core-table thead th:not(:last-child)::after {
  content: '';
  position: absolute;
  right: 0;
  top: var(--spacing-xs);
  bottom: var(--spacing-xs);
  width: 1px;
  background-color: var(--border-color);
  opacity: 0.5;
}

/* 单元格 */
.core-table tbody td {
  padding: var(--spacing-sm) var(--spacing-md);
  border-bottom: 1px solid var(--border-light);
  color: var(--text-secondary);
  vertical-align: middle;
  white-space: nowrap; /* 默认不换行 */
  overflow: visible; /* 不隐藏内容 */
  text-overflow: clip; /* 不显示省略号 */
}

/* ==================== 单元格文本颜色 ==================== */

/* 默认次要颜色（已在 .core-table tbody td 中定义） */

/* 主要颜色 - 标题、重要信息 */
.core-table tbody td.text-primary {
  color: var(--text-primary);
}

/* 第三颜色 - 辅助说明、提示信息 */
.core-table tbody td.text-tertiary {
  color: var(--text-tertiary);
}

/* 成功颜色 - 正常状态、已确认等 */
.core-table tbody td.text-success {
  color: var(--success-color);
}

/* 警告颜色 - 待处理、注意等 */
.core-table tbody td.text-warning {
  color: var(--warning-color);
}

/* 危险颜色 - 删除、取消、错误等 */
.core-table tbody td.text-danger {
  color: var(--danger-color);
}

/* 信息颜色 - 信息提示、链接等 */
.core-table tbody td.text-info {
  color: var(--info-color);
}

/* 主色调 - 主题色、强调 */
.core-table tbody td.text-theme {
  color: var(--primary-color);
}

/* ==================== 财务专用颜色 ==================== */
/*
 * 财务颜色规则（股票风格）：
 * - 收入/资金流入：红色 (#dc3545)
 * - 支出/资金流出：绿色 (#198754)
 *
 * 适用场景：资金流水、收入支出、财务报表等
 */

/* 收入/资金流入 - 红色 */
.core-table tbody td.text-finance-income {
  color: #dc3545;
}

/* 支出/资金流出 - 绿色 */
.core-table tbody td.text-finance-outcome {
  color: #198754;
}

/* 单元格右边框分隔线（可选） */
.core-table tbody td:not(:last-child)::after {
  content: '';
  position: absolute;
  right: 0;
  top: var(--spacing-xs);
  bottom: var(--spacing-xs);
  width: 1px;
  background-color: var(--border-light);
  opacity: 0.5;
}

/* 空行提示 */
.core-table tbody tr.empty-row td {
  padding: var(--spacing-2xl);
  text-align: center;
  color: var(--text-tertiary);
}

/* ==================== 行样式 ==================== */

/* 斑马纹 - 默认开启 */
.core-table tbody tr:nth-child(even) {
  background-color: var(--bg-secondary);
}

/* Hover 效果 */
.core-table tbody tr:hover {
  background-color: var(--hover-bg);
  transition: background-color var(--transition-fast);
}

/* 选中行 */
.core-table tbody tr.selected {
  background-color: var(--primary-light);
}

/* ==================== 粘性表头 ==================== */

/* sticky-header 容器样式 */
.sticky-header {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* sticky-header 作为滚动容器时 */
.sticky-header.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.core-table.sticky-header thead th {
  position: sticky;
  top: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* ==================== 表格变体 ==================== */

/* 紧凑型表格 - 用于数据密集场景 */
.core-table.compact {
  font-size: var(--font-size-xs);
}

.core-table.compact thead th {
  padding: var(--spacing-xs) var(--spacing-sm);
}

.core-table.compact tbody td {
  padding: var(--spacing-xs) var(--spacing-sm);
}

/* 宽松型表格 - 用于展示场景 */
.core-table.spacious {
  font-size: var(--font-size-base);
}

.core-table.spacious thead th {
  padding: var(--spacing-md) var(--spacing-lg);
}

.core-table.spacious tbody td {
  padding: var(--spacing-md) var(--spacing-lg);
}

/* 无边框表格 */
.core-table.bordered thead th {
  border: 1px solid var(--border-color);
}

.core-table.bordered tbody td {
  border: 1px solid var(--border-light);
}

.core-table.bordered tbody td:not(:last-child)::after,
.core-table.bordered thead th:not(:last-child)::after {
  display: none;
}

/* 无斑马纹 */
.core-table.no-zebra tbody tr:nth-child(even) {
  background-color: transparent;
}

/* ==================== 单元格对齐 ==================== */

.core-table thead th.text-center,
.core-table tbody td.text-center {
  text-align: center;
}

.core-table thead th.text-right,
.core-table tbody td.text-right {
  text-align: right;
}

/* ==================== 单元格宽度控制 ==================== */

/* 自动宽度（默认） */
.core-table thead th,
.core-table tbody td {
  width: auto;
}

/* 固定宽度列 - 使用 HTML width 属性或内联样式设置宽度 */

/* 紧凑列（最小宽度） */
.core-table thead th.col-compact,
.core-table tbody td.col-compact {
  width: 1px;
  white-space: nowrap;
}

/* 宽列 */
.core-table thead th.col-wide,
.core-table tbody td.col-wide {
  min-width: 200px;
}

/* ==================== 单元格换行控制 ==================== */

/* 不换行（单行显示） */
.core-table thead th.col-nowrap,
.core-table tbody td.col-nowrap {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 允许换行（多行显示） */
.core-table tbody td.col-wrap,
.core-table tbody td.allow-wrap {
  white-space: normal !important;
  word-wrap: break-word;
  word-break: break-word;
  overflow: visible;
  text-overflow: clip;
}

/* 在 .table-fixed-first 容器中允许换行（覆盖默认不换行） */
.table-fixed-first .core-table tbody td.col-wrap,
.table-fixed-first .core-table tbody td.allow-wrap {
  white-space: normal !important;
  word-wrap: break-word;
  word-break: break-word;
}

/* 截断（超出显示省略号） */
.core-table tbody td.col-truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px; /* 默认最大宽度，可覆盖 */
}

/* ==================== 单元格内容样式 ==================== */

/* 文本截断提示 */
.core-table tbody td[title]:hover {
  cursor: help;
}

/* 多行文本 */
.core-table tbody td.col-multiline {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
}

/* ==================== 单元格状态 ==================== */

/* 成功状态单元格 */
.core-table tbody td.cell-success {
  color: var(--success-color);
  background-color: var(--success-light);
}

/* 警告状态单元格 */
.core-table tbody td.cell-warning {
  color: var(--warning-color);
  background-color: var(--warning-light);
}

/* 危险状态单元格 */
.core-table tbody td.cell-danger {
  color: var(--danger-color);
  background-color: var(--danger-light);
}

/* 信息状态单元格 */
.core-table tbody td.cell-info {
  color: var(--info-color);
  background-color: var(--info-light);
}

/* ==================== 响应式表格 ==================== */

/* 小屏幕横向滚动容器 */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table-responsive .core-table {
  min-width: 600px;
}

/* ==================== 第一列固定、其余横向滚动 ==================== */

/* 容器：第一列固定、其余可滚动 */
.table-fixed-first {
  display: block;
  width: 100%;
  overflow-x: auto;
  border: var(--border-width-thin) solid var(--border-color);
  border-radius: var(--radius-sm);
}

/* 表格：100% 宽度、TD 不换行 */
.table-fixed-first .core-table {
  width: max-content; /* 表格宽度根据内容自适应 */
  min-width: 100%; /* 最小宽度为 100%，确保表格至少占满容器 */
  table-layout: auto; /* 自动表格布局，根据内容调整列宽 */
  border-collapse: collapse;
  margin: 0;
}

/* 表头 TH：不换行 */
.table-fixed-first .core-table thead th {
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
}

/* 单元格 TD：不换行 */
.table-fixed-first .core-table tbody td {
  white-space: nowrap !important;
  overflow: visible;
  text-overflow: clip;
}

/* 第2列表体单元格不继承全局padding，使用自定义padding */
.table-fixed-first .core-table tbody td:nth-child(2) {
  padding: 8px 16px !important;
}

/* 第一列固定（左侧） */
.table-fixed-first .core-table thead th:first-child,
.table-fixed-first .core-table tbody td:first-child {
  position: sticky !important;
  left: 0 !important;
  z-index: 50;
  background-color: var(--bg-primary);
  white-space: nowrap !important;
  overflow: visible !important;
  text-overflow: clip !important;
}

/* 移除第一列表头的右边框（::after伪元素） */
.table-fixed-first .core-table thead th:first-child::after {
  display: none;
}

/* 表头第一列（更高 z-index，同时固定顶部和左侧） */
.table-fixed-first .core-table thead th:first-child {
  z-index: 60 !important;
  top: 0 !important;
}

/* 前两列固定（左侧） - 使用宽度类 u-w-40 和 u-w-70 */
.table-fixed-first .core-table.sticky-first-two-columns thead th:nth-child(2),
.table-fixed-first .core-table.sticky-first-two-columns tbody td:nth-child(2) {
  position: sticky !important;
  left: 40px !important; /* 第一列宽度40px */
  z-index: 49 !important;
  background-color: var(--bg-primary) !important;
  border-right: 1px solid var(--border-light);
  white-space: nowrap !important;
  overflow: visible !important;
  text-overflow: clip !important;
  padding: 8px 16px !important;
}

/* 表头第二列 */
.table-fixed-first .core-table.sticky-first-two-columns thead th:nth-child(2) {
  z-index: 59 !important;
  top: 0 !important;
}

/* 表头第二列（同时固定顶部和左侧） */
.table-fixed-first .core-table.sticky-first-two-columns thead th:nth-child(2) {
  z-index: 59 !important;
  top: 0 !important;
}

/* 确保第1列的宽度固定 */
.table-fixed-first .core-table.sticky-first-two-columns thead th:nth-child(1),
.table-fixed-first .core-table.sticky-first-two-columns tbody td:nth-child(1) {
  min-width: 40px !important;
  max-width: 40px !important;
  width: 40px !important;
}

/* 第2列的宽度根据内容自适应，不要限制宽度，让padding生效 */
.table-fixed-first .core-table.sticky-first-two-columns thead th:nth-child(2) {
  min-width: 70px !important;
}

.table-fixed-first .core-table.sticky-first-two-columns tbody td:nth-child(2) {
  min-width: 70px !important;
}

/* 表头固定（非固定列，只固定顶部） */
.table-fixed-first.sticky-header .core-table thead th:not(:first-child):not(:nth-child(2)) {
  position: sticky !important;
  top: 0 !important;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* ==================== 单列固定（独立使用） ==================== */

/* 单独表格的第一列固定（不需要 .table-fixed-first 容器） */
.core-table.sticky-first-column {
  border-collapse: separate;
  border-spacing: 0;
}

.core-table.sticky-first-column thead th:first-child,
.core-table.sticky-first-column tbody td:first-child {
  position: sticky;
  left: 0;
  z-index: 5;
  background-color: var(--bg-primary);
  border-right: 1px solid var(--border-color);
}

/* 表头第一列需要更高的 z-index */
.core-table.sticky-first-column thead th:first-child {
  z-index: 15;
}

/* 单独表格的前两列固定（不需要 .table-fixed-first 容器） */
.core-table.sticky-first-two-columns {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content; /* 表格宽度根据内容自适应 */
  min-width: 100%; /* 最小宽度100%，确保表格至少占满容器 */
}

/* 在 table-responsive 容器中的 sticky-first-two-columns */
.table-responsive .core-table.sticky-first-two-columns {
  width: max-content;
  min-width: 100%;
}

/* 在 sticky-header 容器中的 sticky-first-two-columns */
.sticky-header .core-table.sticky-first-two-columns {
  width: max-content;
  min-width: 100%;
}

/* 第一列固定 */
.core-table.sticky-first-two-columns thead th:first-child,
.core-table.sticky-first-two-columns tbody td:first-child {
  position: sticky;
  left: 0;
  z-index: 5;
  background-color: var(--bg-primary);
  border-right: 1px solid var(--border-color);
}

.core-table.sticky-first-two-columns thead th:first-child {
  z-index: 15;
}

/* 第二列固定（需要指定第一列的宽度） */
.core-table.sticky-first-two-columns thead th:nth-child(2),
.core-table.sticky-first-two-columns tbody td:nth-child(2) {
  position: sticky;
  left: 40px; /* 第一列宽度40px */
  z-index: 4;
  background-color: var(--bg-primary);
  border-right: 1px solid var(--border-color);
}

.core-table.sticky-first-two-columns thead th:nth-child(2) {
  z-index: 14;
}

/* 行悬停效果 */
.core-table.sticky-first-two-columns tbody tr:hover td:first-child,
.core-table.sticky-first-two-columns tbody tr:hover td:nth-child(2) {
  background-color: var(--bg-tertiary);
}

/* ==================== 第一列固定 + 粘性表头 ==================== */

/* 表头：粘性定位 */
.table-fixed-first.sticky-header .core-table thead th {
  position: sticky;
  top: 0;
  z-index: 55;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* 表头第一列（最高 z-index） */
.table-fixed-first.sticky-header .core-table thead th:first-child {
  z-index: 65;
}

/* 第一列内容（底部粘性，确保覆盖滚动内容） */
.table-fixed-first.sticky-header .core-table tbody td:first-child {
  z-index: 50;
  background-color: var(--bg-primary);
  white-space: nowrap !important;
  overflow: visible !important;
  text-overflow: clip !important;
}

/* sticky-header 容器下的 sticky-first-two-columns 表头 */
.sticky-header .core-table.sticky-first-two-columns thead th:first-child {
  z-index: 20;
}

.sticky-header .core-table.sticky-first-two-columns thead th:nth-child(2) {
  z-index: 19;
}

/* 粘性表头样式（当同时使用 sticky-header 和 sticky-first-two-columns） */
.sticky-header .core-table.sticky-first-two-columns thead th {
  position: sticky;
  top: 0;
}

/* ==================== 暗色模式适配 ==================== */

[data-theme="dark"] .table-fixed-first {
  border-color: var(--border-color);
}

[data-theme="dark"] .table-fixed-first .core-table thead th:first-child,
[data-theme="dark"] .table-fixed-first .core-table tbody td:first-child {
  background-color: var(--bg-primary);
  border-right-color: var(--border-light);
  box-shadow: 1px 0 0 var(--border-light);
}

[data-theme="dark"] .table-fixed-first .core-table thead th:first-child {
  background: linear-gradient(180deg, var(--bg-tertiary) 0%, var(--bg-secondary) 100%);
}

/* ==================== 表格操作列 ==================== */

.core-table .action-cell {
  white-space: nowrap;
  width: 1px;
}

.core-table .action-link {
  display: inline-block;
  margin-right: var(--spacing-sm);
  color: var(--primary-color);
  text-decoration: none;
  cursor: pointer;
  transition: color var(--transition-fast);
}

.core-table .action-link:hover {
  color: var(--primary-hover);
}

.core-table .action-link:last-child {
  margin-right: 0;
}

/* ==================== 图片单元格 ==================== */

.core-table .image-cell img {
  max-width: 48px;
  max-height: 48px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-light);
}

/* 固定第一列中的图片 */
.table-fixed-first .core-table tbody td:first-child img {
  max-width: 42px;
  max-height: 42px;
  object-fit: cover;
  border-radius: 3px;
  border: 1px solid var(--border-light);
}

/* ==================== 图标统一定义 ==================== */

/* 表格内所有图标统一定义大小 */
.core-table i {
  font-size: var(--icon-size-sm);
  vertical-align: middle;
}

/* 圆形图标统一定义大小（实心和空心） */
.core-table .bi-arrow-up-circle,
.core-table .bi-arrow-down-circle,
.core-table .bi-arrow-up-circle-fill,
.core-table .bi-arrow-down-circle-fill,
.core-table .bi-dash-circle {
  font-size: var(--icon-size-base) !important;
  vertical-align: middle;
}

/* 表头中的图标 */
.core-table thead th i {
  font-size: var(--icon-size-sm);
  margin-right: 4px;
}

/* 单元格中的图标（带间距） */
.core-table tbody td i {
  margin-right: 4px;
}

.core-table tbody td i:last-child {
  margin-right: 0;
}

/* 固定第一列中的图标 */
.table-fixed-first .core-table tbody td:first-child i {
  font-size: var(--icon-size-xl);
  margin-right: 0;
}

/* 紧凑表格中的图标 */
.core-table.compact i {
  font-size: var(--icon-size-xs);
}

.core-table.compact .bi-arrow-up-circle,
.core-table.compact .bi-arrow-down-circle,
.core-table.compact .bi-arrow-up-circle-fill,
.core-table.compact .bi-arrow-down-circle-fill,
.core-table.compact .bi-dash-circle {
  font-size: var(--icon-size-sm) !important;
}

/* 宽松表格中的图标 */
.core-table.spacious i {
  font-size: var(--icon-size-base);
}

.core-table.spacious .bi-arrow-up-circle,
.core-table.spacious .bi-arrow-down-circle,
.core-table.spacious .bi-arrow-up-circle-fill,
.core-table.spacious .bi-arrow-down-circle-fill,
.core-table.spacious .bi-dash-circle {
  font-size: var(--icon-size-lg) !important;
}

/* ==================== 排序指示器 ==================== */

.core-table thead th.sortable {
  cursor: pointer;
  padding-right: var(--spacing-lg);
}

.core-table thead th.sortable:hover {
  background-color: var(--bg-light);
}

.core-table thead th.sortable::before {
  content: '';
  position: absolute;
  right: var(--spacing-sm);
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  opacity: 0.3;
}

.core-table thead th.sortable.sort-asc::before {
  border-bottom: 4px solid var(--text-secondary);
}

.core-table thead th.sortable.sort-desc::before {
  border-top: 4px solid var(--text-secondary);
}

/* ==================== 表格工具栏 ==================== */

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-sm) 0;
  margin-bottom: var(--spacing-sm);
}

.table-toolbar-left,
.table-toolbar-right {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

/* ==================== 表格加载状态 ==================== */

.core-table.loading tbody tr:not(:first-child) {
  opacity: 0.5;
}

.core-table.loading tbody tr:not(:first-child)::after {
  content: '';
  display: block;
  width: 100%;
  height: 1px;
  background: var(--skeleton-bg);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* ==================== 表格空状态 ==================== */

.table-empty {
  padding: var(--spacing-2xl);
  text-align: center;
  color: var(--text-tertiary);
}

.table-empty-icon {
  font-size: var(--size-48px);
  margin-bottom: var(--spacing-md);
  opacity: 0.3;
}

.table-empty-text {
  font-size: var(--font-size-base);
  margin-bottom: var(--spacing-sm);
}

.table-empty-hint {
  font-size: var(--font-size-sm);
  color: var(--text-tertiary);
}

/* ==================== 表格分页 ==================== */

.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-md) 0;
  margin-top: var(--spacing-sm);
}

.table-pagination-info {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.table-pagination-controls {
  display: flex;
  gap: var(--spacing-xs);
}

.table-pagination-btn {
  min-width: var(--pagination-min-width);
  height: 32px;
  padding: 0 var(--pagination-padding);
  font-size: var(--pagination-font-size);
  border: 1px solid var(--border-color);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.table-pagination-btn:hover:not(:disabled) {
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.table-pagination-btn.active {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  color: var(--text-on-primary);
}

.table-pagination-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ==================== 打印样式 ==================== */

@media print {
  .core-table {
    page-break-inside: auto;
  }

  .core-table tr {
    page-break-inside: avoid;
    page-break-after: auto;
  }

  .core-table thead th {
    background: #f0f0f0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .core-table tbody tr:hover {
    background: transparent !important;
  }

  .table-toolbar,
  .table-pagination {
    display: none;
  }
}

/* ==================== 暗色模式适配 ==================== */

[data-theme="dark"] .core-table {
  background-color: var(--bg-primary);
  color: var(--text-primary);
}

[data-theme="dark"] .core-table thead th {
  background: linear-gradient(180deg, var(--bg-tertiary) 0%, var(--bg-secondary) 100%);
  border-bottom-color: var(--border-color);
}

[data-theme="dark"] .core-table tbody td {
  border-bottom-color: var(--border-color);
}

[data-theme="dark"] .core-table tbody tr:nth-child(even) {
  background-color: var(--bg-secondary);
}

[data-theme="dark"] .core-table tbody tr:hover {
  background-color: var(--bg-tertiary);
}

[data-theme="dark"] .core-table tbody tr.selected {
  background-color: rgba(255, 80, 0, 0.15);
}
