/* Include legacy tables inside chart results as well as standalone previews. */
:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) {
  --result-primary: var(--primary, var(--rp-primary, #2563eb));
  --result-on-primary: var(--theme-on-primary, var(--rp-on-primary, #fff));
  --result-soft: var(--primary-light, var(--rp-primary-soft, #eef4ff));
  --result-surface: var(--white, var(--rp-surface, #fff));
  --result-text: var(--text-primary, var(--rp-text, #1f2937));
  --result-border: var(--border-light, var(--rp-border, #e3e7ee));
  overflow-x: auto;
  border: 1px solid var(--result-border) !important;
  border-radius: 8px;
  background: var(--result-surface) !important;
}

/* Short chat previews fit their rows; long tables scroll within 500px. */
:is(#chat-container .table-result, #chat-container .chart-result .table-container) {
  box-sizing: border-box;
  height: auto !important;
  max-height: none !important;
  overflow: auto !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container):has(tbody > tr:nth-child(11)) {
  max-height: 500px !important;
  scrollbar-gutter: stable;
}

/* The preview is the scroll container; legacy wrappers must not scroll again. */
:is(#chat-container .table-result, #chat-container .chart-result .table-container) :is(.table-responsive, .knowledge-table-wrap) {
  height: auto !important;
  max-height: none !important;
  overflow: visible !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container) thead th {
  position: sticky;
  top: 0;
  z-index: 2;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) .data-section {
  background: var(--result-surface) !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) table {
  width: 100% !important;
  margin: 0 !important;
  border: 0 !important;
  border-collapse: separate !important;
  border-spacing: 0 !important;
  background: var(--result-surface) !important;
  color: var(--result-text) !important;
  font-family: var(--ui-font-sans, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif) !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 1.5 !important;
}

/* The wrapper ID outranks the per-table ID styles stored in older messages. */
:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) :is(th, td) {
  padding: 12px 16px !important;
  border: 0 !important;
  border-bottom: 1px solid var(--result-border) !important;
  font-family: inherit !important;
  font-size: 13px !important;
  line-height: 1.5 !important;
  letter-spacing: 0 !important;
  text-align: left;
  vertical-align: middle;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) thead th {
  background: var(--result-primary) !important;
  color: var(--result-on-primary) !important;
  font-weight: 500 !important;
  white-space: nowrap;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) thead th :is(span, strong, b) {
  color: inherit !important;
  font: inherit !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) tbody td {
  background: var(--result-surface) !important;
  color: var(--result-text) !important;
  font-weight: 400 !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) tbody tr:nth-child(even) td {
  background: color-mix(in srgb, var(--result-soft) 34%, var(--result-surface)) !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) tbody tr:hover td {
  background: color-mix(in srgb, var(--result-soft) 55%, var(--result-surface)) !important;
}

:is(#chat-container .table-result, #chat-container .chart-result .table-container, #app .data-table-surface) tbody tr:last-child td {
  border-bottom: 0 !important;
}

.result-table-image-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 96px;
  height: 72px;
  vertical-align: middle;
  overflow: hidden;
  border: 1px solid var(--result-border, #e3e7ee);
  border-radius: 6px;
  background: var(--bg-light, var(--rp-page, #f8f9fa));
  color: var(--result-text, #1f2937);
  text-decoration: none;
  cursor: zoom-in;
}

.result-table-image-link:focus-visible {
  outline: 2px solid var(--result-primary, #7657d5);
  outline-offset: 2px;
}

.result-table-image-link .result-table-image {
  display: block;
  width: 96px !important;
  height: 72px !important;
  max-width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  object-fit: contain;
}

.result-table-image-link .result-table-image[hidden] {
  display: none;
}

.result-table-image-failed {
  cursor: pointer;
}

.result-table-image-failed::after {
  content: attr(data-fallback);
  padding: 6px;
  font-size: 12px;
  line-height: 1.5;
  white-space: normal;
  text-align: center;
}
