/* Table: scroll contained in wrapper (page does not scroll horizontally) */

.table-wrapper {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  margin: 1em 0;
  -webkit-overflow-scrolling: touch;
  border-radius: 6px;
  border: 1px solid var(--md-border, rgba(27, 31, 35, 0.12));
  background: var(--md-canvas, #ffffff);
}

.table-wrapper > table {
  width: max-content;
  min-width: 100%;
  table-layout: auto;
  border-collapse: collapse;
  border-spacing: 0;
  font-size: 0.9em;
}

.table-wrapper > table th,
.table-wrapper > table td {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--md-border, rgba(27, 31, 35, 0.12));
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
  vertical-align: top;
  line-height: 1.5;
  overflow: visible;
  box-sizing: border-box;
}

.table-wrapper > table thead th {
  background-color: var(--md-canvas-subtle, #f6f8fa);
  font-weight: 600;
  color: var(--md-fg, #1f2328);
}

.table-wrapper > table tbody tr,
.table-wrapper > table thead tr {
  height: auto;
}

.table-wrapper > table tbody tr:nth-child(even) {
  background-color: rgba(27, 31, 35, 0.03);
}

.table-wrapper > table tbody tr:hover {
  background-color: rgba(27, 31, 35, 0.05);
}

/* Prevent content in cells from overlapping */
.table-wrapper > table th p,
.table-wrapper > table td p {
  margin: 0.35em 0;
}

.table-wrapper > table th p:first-child,
.table-wrapper > table td p:first-child {
  margin-top: 0;
}

.table-wrapper > table th p:last-child,
.table-wrapper > table td p:last-child {
  margin-bottom: 0;
}

/* Fallback when table is not inside .table-wrapper (e.g. theme version) */
.content table,
#post-content table,
.post-content table {
  width: 100%;
  table-layout: auto;
  font-size: 0.9em;
}

.content table th,
.content table td,
#post-content table th,
#post-content table td,
.post-content table th,
.post-content table td {
  padding: 0.6rem 0.75rem;
  border: 1px solid rgba(27, 31, 35, 0.12);
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
  vertical-align: top;
  line-height: 1.5;
  overflow: visible;
  box-sizing: border-box;
}

/* Dark mode */
[data-theme="dark"] .table-wrapper > table th,
.theme-dark .table-wrapper > table th {
  background-color: var(--md-canvas-subtle, #21262d);
  color: var(--md-fg, #e6edf3);
}

[data-theme="dark"] .table-wrapper,
.theme-dark .table-wrapper {
  border-color: var(--md-border, rgba(255, 255, 255, 0.16));
  background: var(--md-canvas, #0d1117);
}

[data-theme="dark"] .table-wrapper > table th,
[data-theme="dark"] .table-wrapper > table td,
.theme-dark .table-wrapper > table th,
.theme-dark .table-wrapper > table td {
  border-color: var(--md-border, rgba(255, 255, 255, 0.12));
}

[data-theme="dark"] .table-wrapper > table tbody tr:nth-child(even),
.theme-dark .table-wrapper > table tbody tr:nth-child(even) {
  background-color: rgba(255, 255, 255, 0.03);
}

/* ----- 窄屏卡片布局（可选：在表格后加 {: .table-cards } 启用）----- */
@media (max-width: 960px) {
  .table-wrapper:has(table.table-cards) {
    overflow-x: visible;
    box-shadow: none;
  }

  .table-wrapper:has(table.table-cards) > table.table-cards {
    width: 100%;
    display: block;
  }

  .table-wrapper:has(table.table-cards) > table.table-cards thead {
    display: none;
  }

  .table-wrapper:has(table.table-cards) > table.table-cards tbody tr {
    display: block;
    margin-bottom: 1.25rem;
    padding: 0.75rem;
    background: var(--md-canvas-subtle, #f6f8fa);
    border: 1px solid var(--md-border, rgba(27, 31, 35, 0.12));
    border-radius: 6px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  }

  .table-wrapper:has(table.table-cards) > table.table-cards tbody td {
    display: block;
    padding: 0.4rem 0 0.5rem;
    border: none;
    border-bottom: 1px solid rgba(27, 31, 35, 0.08);
  }

  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:last-child {
    border-bottom: none;
  }

  .table-wrapper:has(table.table-cards) > table.table-cards tbody td::before {
    display: block;
    font-weight: 600;
    color: var(--md-fg-muted, #57606a);
    margin-bottom: 0.2rem;
    font-size: 0.85em;
  }

  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(1)::before { content: "算法"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(2)::before { content: "核心机制"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(3)::before { content: "查询速度"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(4)::before { content: "内存占用"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(5)::before { content: "均衡性 (负载分布)"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(6)::before { content: "扩缩容影响 (扰动)"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(7)::before { content: "实现复杂度"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(8)::before { content: "最大缺点"; }
  .table-wrapper:has(table.table-cards) > table.table-cards tbody td:nth-child(9)::before { content: "最大优点"; }

  [data-theme="dark"] .table-wrapper:has(table.table-cards) > table.table-cards tbody tr,
  .theme-dark .table-wrapper:has(table.table-cards) > table.table-cards tbody tr {
    background: var(--md-canvas-subtle, #21262d);
    border-color: var(--md-border, rgba(255, 255, 255, 0.12));
  }

  [data-theme="dark"] .table-wrapper:has(table.table-cards) > table.table-cards tbody td,
  .theme-dark .table-wrapper:has(table.table-cards) > table.table-cards tbody td {
    border-bottom-color: rgba(255, 255, 255, 0.08);
  }

  [data-theme="dark"] .table-wrapper:has(table.table-cards) > table.table-cards tbody td::before,
  .theme-dark .table-wrapper:has(table.table-cards) > table.table-cards tbody td::before {
    color: var(--md-fg-muted, #8b949e);
  }
}
