/* =========================================================
   Table Chart — Publication-quality data table styling
   Follows Schwabish's "Ten Guidelines for Better Tables"
   ========================================================= */

/* ── Wrapper ─────────────────────────────────────────────── */

.table-chart-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

/* ── Title area ───────────────────────────────────────────── */

.table-title-area {
    flex-shrink: 0;
    padding: 0 0 12px 0;
}

.table-title-area .chart-title {
    font-size: 1.55em;
    font-weight: 700;
    margin: 0 0 4px 0;
    line-height: 1.3;
    color: #1a1a1a;
    cursor: text;
    min-height: 1.3em;
}

.table-title-area .chart-description {
    font-size: 1.08em;
    color: #555;
    margin: 0 0 2px 0;
    line-height: 1.4;
    cursor: text;
}

.table-title-area .chart-subtitle {
    font-size: 1em;
    color: #777;
    margin: 0;
    line-height: 1.4;
    cursor: text;
}

/* Hide empty text elements — they appear only when populated or selected in elements panel */
.table-title-area .chart-title:empty {
    display: none;
}

.table-title-area .chart-description:empty {
    display: none;
}

.table-title-area .chart-subtitle:empty {
    display: none;
}

/* ── Footer area (inside <tfoot>) ────────────────────────── */

tfoot.table-footer-area td {
    padding: 4px 0.6em 2px;
    border-bottom: none;
}

tfoot.table-footer-area .chart-caption {
    font-size: 11px;
    color: #777;
    margin: 0;
    line-height: 1.4;
    cursor: text;
}

tfoot.table-footer-area .chart-source {
    display: block;
    font-size: 11px;
    color: #999;
    margin: 0;
    line-height: 1.4;
    cursor: text;
}

tfoot.table-footer-area .chart-caption:empty {
    display: none;
}

tfoot.table-footer-area .chart-source:empty {
    display: none;
}

/* Hide tfoot rows when their content element is empty */
tfoot.table-footer-area tr:has(.chart-caption:empty) td,
tfoot.table-footer-area tr:has(.chart-source:empty) td {
    padding: 0;
}

.table-empty {
    color: var(--text-secondary, #888);
    text-align: center;
    padding: 40px 20px;
    font-size: 14px;
}

/* ── Controls (search bar) ────────────────────────────────── */

.table-controls {
    flex-shrink: 0;
    padding: 0 0 10px 0;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.table-controls:empty {
    display: none;
}

.table-search {
    width: 260px;
    max-width: 100%;
    padding: 6px 10px 6px 30px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 13px;
    outline: none;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'/%3E%3C/svg%3E") 8px center no-repeat;
    transition: border-color 0.15s;
}

.table-search:focus {
    border-color: #4285f4;
    box-shadow: 0 0 0 2px rgba(66,133,244,0.15);
}

.table-search-count {
    font-size: 11px;
    color: #888;
}

/* ── Scroll container ────────────────────────────────────── */

.table-scroll-container {
    flex: 1;
    min-height: 0;
    overflow-x: auto;
    overflow-y: auto;
    position: relative;
}

/* Scroll shadow indicators */
.table-scroll-container::before,
.table-scroll-container::after {
    content: '';
    position: sticky;
    top: 0;
    bottom: 0;
    width: 0;
    display: block;
    pointer-events: none;
    z-index: 3;
    transition: width 0.2s ease;
}

.table-scroll-container.has-scroll-left::before {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 20px;
    background: linear-gradient(to right, rgba(0,0,0,0.06), transparent);
}

.table-scroll-container.has-scroll-right::after {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 20px;
    background: linear-gradient(to left, rgba(0,0,0,0.06), transparent);
}

/* ── Base table ──────────────────────────────────────────── */

.data-table {
    width: auto;
    border-collapse: separate;
    border-spacing: 0;
    font-size: inherit; /* inherits from .table-chart-wrapper which applies FontScale */
    line-height: 1.5;
}

/* ── Header ──────────────────────────────────────────────── */

.data-table thead th {
    position: sticky;
    top: 0;
    background: #f8f9fa;
    border-bottom: 2px solid #333;
    padding: 0.5em 0.6em;
    font-weight: 600;
    font-size: 0.92em;
    letter-spacing: 0.01em;
    text-transform: none;
    white-space: normal;
    overflow-wrap: break-word;
    word-break: break-word;
    z-index: 2;
    vertical-align: bottom;
}

/* No vertical borders anywhere (Schwabish guideline) */
.data-table th,
.data-table td {
    border-left: none;
    border-right: none;
}

/* ── Body cells ──────────────────────────────────────────── */

.data-table tbody td {
    padding: 0.45em 0.6em;
    border-bottom: 1px solid #f0f0f0;
    vertical-align: middle;
    overflow-wrap: break-word;
}

/* Last row: slightly heavier bottom border */
.data-table tbody tr:last-child td {
    border-bottom: 1px solid #ddd;
}

/* ── Column alignment ────────────────────────────────────── */

.data-table .col-number,
.data-table .col-right {
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
}

.data-table tbody .col-number,
.data-table tbody .col-right {
    white-space: nowrap;
}

.data-table .col-text,
.data-table .col-left {
    text-align: left;
}

.data-table .col-center {
    text-align: center;
}

.data-table .col-date {
    text-align: left;
    color: #555;
}

.data-table tbody .col-date {
    white-space: nowrap;
}

.data-table .col-rownumber {
    text-align: center;
    color: #999;
    font-size: 11px;
    width: 36px;
    min-width: 36px;
}

.data-table .col-sparkline {
    text-align: center;
    padding: 4px 10px;
}

/* ── Sortable headers ────────────────────────────────────── */

.data-table th.sortable {
    cursor: pointer;
    user-select: none;
    padding-right: 22px;
    position: relative;
}

.data-table th.sortable:hover {
    background: #eef1f4;
}

/* Sort indicator arrow */
.data-table th.sortable::after {
    content: '';
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    opacity: 0;
    transition: opacity 0.15s;
}

.data-table th.sortable:hover::after {
    border-bottom: 5px solid #aaa;
    opacity: 0.5;
}

.data-table th.sort-active.sort-asc::after {
    border-bottom: 5px solid #333;
    border-top: none;
    opacity: 1;
}

.data-table th.sort-active.sort-desc::after {
    border-top: 5px solid #333;
    border-bottom: none;
    opacity: 1;
}

/* ── Hover highlighting ──────────────────────────────────── */

.data-table tbody tr.hoverable:hover {
    background-color: rgba(66, 133, 244, 0.04);
}

/* ── Striped rows ────────────────────────────────────────── */

.data-table.striped tbody tr:nth-child(even) {
    background-color: #fafbfc;
}

.data-table.striped tbody tr.hoverable:hover {
    background-color: rgba(66, 133, 244, 0.06);
}

/* ── Compact mode ────────────────────────────────────────── */

.data-table.compact thead th {
    padding: 6px 10px;
    font-size: 11px;
}

.data-table.compact tbody td {
    padding: 4px 10px;
    font-size: 12px;
}

/* ── Minimal mode ────────────────────────────────────────── */

.table-chart-wrapper.minimal {
    height: auto;
    width: fit-content;
    max-width: 100%;
    max-height: 100%;
}

.table-chart-wrapper.minimal .table-scroll-container {
    flex: none;
    overflow: visible;
}

.table-chart-wrapper.minimal .data-table {
    width: 1px;  /* forces browser to use minimum column widths */
}

.table-chart-wrapper.minimal .data-table thead th,
.table-chart-wrapper.minimal .data-table tbody td {
    white-space: normal;
    overflow-wrap: break-word;
    word-break: normal;
    padding: 3px 8px;
    width: 1px;  /* each column shrinks to its minimum content width */
}

/* Override fixed date-column width in minimal mode */
.table-chart-wrapper.minimal .data-table thead th.col-date,
.table-chart-wrapper.minimal .data-table tbody td.col-date {
    width: 1px;
}

.table-chart-wrapper.minimal .table-title-area {
    padding: 0 0 6px 0;
}

/* ── Data bars ───────────────────────────────────────────── */

.data-table td.has-databar {
    position: relative;
    padding: 8px 14px;
}

.cell-databar-track {
    position: absolute;
    left: 4px;
    right: 4px;
    top: 4px;
    bottom: 4px;
    border-radius: 2px;
    overflow: hidden;
}

.cell-databar-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.2s ease;
}

.cell-value {
    position: relative;
    z-index: 1;
}

/* ── Heatmap cells ───────────────────────────────────────── */

.data-table td[style*="background-color"] {
    transition: background-color 0.15s ease;
}

/* ── Change indicators ───────────────────────────────────── */

.change-up {
    color: #0a7c42;
    font-weight: 500;
}

.change-down {
    color: #c62828;
    font-weight: 500;
}

.change-zero {
    color: #888;
}

/* ── Sparklines ──────────────────────────────────────────── */

.cell-sparkline {
    display: inline-block;
    vertical-align: middle;
}

/* ── Sticky first column ─────────────────────────────────── */

.sticky-first-col .data-table th:first-child,
.sticky-first-col .data-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    background: inherit;
}

.sticky-first-col .data-table thead th:first-child {
    z-index: 3; /* above both sticky header and sticky column */
    background: #f8f9fa;
}

.sticky-first-col .data-table td:first-child {
    background: #fff;
}

.sticky-first-col .data-table.striped tbody tr:nth-child(even) td:first-child {
    background: #fafbfc;
}

.sticky-first-col .data-table td:first-child::after {
    content: '';
    position: absolute;
    right: -4px;
    top: 0;
    bottom: 0;
    width: 4px;
    background: linear-gradient(to right, rgba(0,0,0,0.04), transparent);
    pointer-events: none;
}

/* ── Responsive: mobile card layout ──────────────────────── */

.table-cards {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.table-card {
    border: 1px solid #eee;
    border-radius: 8px;
    padding: 12px 16px;
    background: #fff;
}

.table-card .card-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 4px 0;
    border-bottom: 1px solid #f5f5f5;
}

.table-card .card-row:last-child {
    border-bottom: none;
}

.table-card .card-label {
    font-size: 11px;
    font-weight: 600;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    flex-shrink: 0;
    margin-right: 12px;
}

.table-card .card-value {
    font-size: 14px;
    font-variant-numeric: tabular-nums;
    text-align: right;
    color: #333;
}

/* ── Pagination ──────────────────────────────────────────── */

.table-pagination {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0 0 0;
    font-size: 12px;
    color: #666;
}

.table-pagination-info {
    font-variant-numeric: tabular-nums;
}

.table-pagination-nav {
    display: flex;
    align-items: center;
    gap: 2px;
}

.table-page-btn {
    min-width: 28px;
    height: 28px;
    padding: 0 6px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #fff;
    cursor: pointer;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    transition: background 0.1s, border-color 0.1s;
}

.table-page-btn:hover:not(:disabled) {
    background: #f0f0f0;
    border-color: #bbb;
}

.table-page-btn.active {
    background: #4285f4;
    color: #fff;
    border-color: #4285f4;
    font-weight: 600;
}

.table-page-btn:disabled {
    opacity: 0.35;
    cursor: default;
}

.table-page-dots {
    padding: 0 4px;
    color: #999;
}

/* ── Sticky reference rows ───────────────────────────────── */

.sticky-ref-rows {
    position: sticky;
    z-index: 1;
}

.sticky-ref-rows tr {
    background: #fffde7 !important;
}

.sticky-ref-rows td {
    border-bottom: 1px solid #e8e0a0 !important;
    font-weight: 600;
}

/* ── Responsive breakpoints ──────────────────────────────── */

@media (max-width: 768px) {
    .data-table {
        font-size: 12px;
    }

    .data-table thead th {
        padding: 6px 8px;
        font-size: 11px;
    }

    .data-table tbody td {
        padding: 5px 8px;
    }

    .data-table .col-rownumber {
        width: 28px;
        min-width: 28px;
    }
}

@media (max-width: 480px) {
    .data-table {
        font-size: 11px;
    }

    .data-table thead th {
        padding: 5px 6px;
        font-size: 10px;
    }

    .data-table tbody td {
        padding: 4px 6px;
    }
}
