/* --- THEME VARIABLES (From demo.html) --- */
:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #0f172a;
    --text-secondary: #64748b;
    --border: #cbd5e1;
    --success-bg: #dcfce7;
    --success-text: #166534;
    --error-bg: #fee2e2;
    --error-text: #991b1b;
    --warning-bg: #fef3c7;
    --warning-text: #d97706;
    --font-body: 'IBM Plex Sans', system-ui, sans-serif;
    --font-mono: 'IBM Plex Mono', ui-monospace, monospace;
    --highlight: #fff7ed;
    --highlight-error: #ffe4e6;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html {
    scroll-behavior: smooth;
}
body {
    font-family: var(--font-body);
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* --- LAYOUT --- */
.container {
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
    padding: 2rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

header {
    margin-bottom: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
h1 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -0.01em;
}

/* --- NAVIGATION TABS (App-level: Upload/Dashboard/Review/Import) --- */
.nav-tabs {
    display: flex;
    gap: 1rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 2rem;
}
.nav-tab {
    padding: 1rem 1.5rem;
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
}
.nav-tab:hover {
    color: var(--primary);
}
.nav-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    background: rgba(37, 99, 235, 0.06);
    border-radius: 0.375rem 0.375rem 0 0;
}
.nav-tab:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transition: opacity 0.3s;
}

/* View Container */
.view-section {
    display: none;
    flex: 1;
    overflow-y: auto;
    padding-bottom: 2rem;
}
.view-section.active {
    display: block;
}

/* --- DASHBOARD CARDS --- */
.overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}
.card {
    background: var(--surface);
    padding: 1.5rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
.card-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    margin-bottom: 0.5rem;
}
.card-value {
    font-size: 2rem;
    font-weight: 700;
}

/* --- DASHBOARD LAYOUT --- */
.dashboard-status {
    text-align: center;
    padding: 1.5rem;
}
.dashboard-status .badge {
    font-size: 1.1rem;
    padding: 0.5rem 1.5rem;
}
.dashboard-section {
    margin-bottom: 2rem;
}
.dashboard-section-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.dashboard-currency-note {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-align: right;
    margin-top: 0.5rem;
    font-style: italic;
}
.dashboard-warning {
    background: var(--warning-bg);
    color: var(--warning-text);
    padding: 1rem 1.5rem;
    border-radius: 0.5rem;
    border: 1px solid #fde68a;
    font-size: 0.875rem;
}
.dashboard-warning ul {
    margin-top: 0.5rem;
    padding-left: 1.5rem;
}
.dashboard-warning li {
    margin-bottom: 0.25rem;
}
.dashboard-warning-card {
    background: var(--warning-bg);
    color: var(--warning-text);
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    border: 1px solid #fde68a;
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
}
.dashboard-streams-more {
    font-style: italic;
    list-style: none;
    margin-left: -1.5rem;
    margin-top: 0.5rem;
}
.dashboard-table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    overflow: hidden;
}
.dashboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}
.dashboard-table th {
    text-align: left;
    padding: 0.75rem 1rem;
    background: #f8fafc;
    border-bottom: 2px solid var(--border);
    color: var(--text-secondary);
    font-weight: 600;
    cursor: default;
}
.dashboard-table th::after {
    content: none;
}
.dashboard-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}
.dashboard-errors-more td {
    color: var(--text-secondary);
    font-style: italic;
    text-align: center;
}

/* --- ENTITY DETAILS --- */
.entity-toggle {
    text-decoration: underline;
    text-decoration-style: dotted;
    cursor: pointer;
    color: inherit;
}
.entity-toggle:hover {
    text-decoration-style: solid;
}
.entity-panel {
    margin-top: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 1rem;
    font-size: 0.875rem;
}
.entity-columns {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}
.entity-col h4 {
    margin: 0 0 0.5rem 0;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.entity-col-matched h4 { color: var(--success-text); }
.entity-col-new h4 { color: var(--warning-text); }
.entity-col ul {
    margin: 0;
    padding: 0;
    list-style: none;
}
.entity-col li {
    padding: 0.2rem 0;
    border-bottom: 1px solid var(--border);
    color: var(--text);
}
.entity-col li:last-child {
    border-bottom: none;
}
.entity-db-name {
    color: var(--text-secondary);
    font-size: 0.8rem;
}
.entity-empty {
    color: var(--text-secondary);
    font-style: italic;
}

/* --- BADGES --- */
.badge {
    padding: 0.25rem 0.75rem;
    border-radius: 999px;
    font-size: 0.875rem;
    font-weight: 600;
}
.badge.success {
    background: var(--success-bg);
    color: var(--success-text);
}
.badge.failed {
    background: var(--error-bg);
    color: var(--error-text);
}
.badge.warning {
    background: var(--warning-bg);
    color: var(--warning-text);
}

/* --- PILLS (commodities) --- */
.dashboard-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.pill {
    display: inline-block;
    padding: 0.3rem 0.75rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 500;
    background: #e0f2fe;
    color: #0c4a6e;
    border: 1px solid #bae6fd;
}

/* --- UPLOAD ZONE --- */
.upload-container {
    background:
        linear-gradient(var(--surface), var(--surface)) padding-box,
        linear-gradient(135deg, var(--primary), #93c5fd) border-box;
    border: 2px dashed transparent;
    border-radius: 1rem;
    padding: 4rem;
    text-align: center;
    transition: max-width 0.4s ease, padding 0.4s ease, height 0.4s ease, transform 0.4s ease, border 0.3s, border-radius 0.4s ease, background 0.3s;
    cursor: pointer;
    max-width: 600px;
    margin: 2rem auto;
}
.upload-container:hover,
.upload-container.drag-active {
    background:
        linear-gradient(#f0f9ff, #f0f9ff) padding-box,
        linear-gradient(135deg, var(--primary), #60a5fa) border-box;
}
.upload-icon {
    font-size: 3rem;
    color: var(--primary);
    margin-bottom: 1rem;
    animation: uploadPulse 2.5s ease-in-out infinite;
    transition: transform 0.2s;
}
.upload-container:hover .upload-icon {
    animation: none;
    transform: scale(1.12);
}
@keyframes uploadPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.06); }
}

/* --- Upload pill animation --- */

.upload-container.morphing {
    max-width: var(--morph-w, 200px);
    padding: 0;
    height: var(--morph-h, 2rem);
    border: 2px solid transparent;
    border-radius: 999px;
    cursor: default;
    background:
        linear-gradient(var(--surface), var(--surface)) padding-box,
        linear-gradient(135deg, var(--primary), #93c5fd) border-box;
}

.upload-container.morphing:hover {
    background:
        linear-gradient(var(--surface), var(--surface)) padding-box,
        linear-gradient(135deg, var(--primary), #93c5fd) border-box;
}

.upload-content.fade-out {
    opacity: 0;
    transition: opacity 0.15s;
}

/* SVG comet spinner — constant-speed orbit via pathLength + WAAPI */
.badge.status-pill-processing {
    position: relative;
    display: inline-block;
}

.pill-spinner {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.pill-spinner .comet-head {
    filter: drop-shadow(0 0 3px #fff) drop-shadow(0 0 6px rgba(37, 99, 235, 0.5));
}

.badge.flying {
    position: fixed;
    z-index: 3001;
    transition: top 0.5s cubic-bezier(0.4, 0, 0.2, 1), left 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn-reset {
    margin-top: 2rem;
    padding: 0.5rem 1rem;
    background: var(--error-bg);
    color: var(--error-text);
    border: 1px solid var(--error-text);
    border-radius: 4px;
    cursor: pointer;
}

#file-input {
    display: none !important;
}

/* --- EXAMPLE DROPDOWN --- */
.example-container {
    max-width: 600px;
    margin: 1.5rem auto 0;
    text-align: center;
}
.example-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
}
.example-controls {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}
.example-select {
    flex: 1;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    background: var(--surface);
    color: var(--text);
    font-size: 0.875rem;
    cursor: pointer;
}
.example-select:hover {
    border-color: var(--primary);
}
.example-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
.btn-example {
    padding: 0.5rem 1.25rem;
    background: var(--primary);
    color: white;
    border: 1px solid var(--primary);
    border-radius: 0.375rem;
    font-weight: 600;
    font-size: 0.875rem;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, transform 0.1s;
}
.btn-example:active:not(:disabled) { transform: scale(0.97); }
.btn-example:hover:not(:disabled) {
    filter: brightness(1.1);
}
.btn-example:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* --- MODAL --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 2000;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
}
.modal-overlay.open {
    display: flex;
    opacity: 1;
}
.modal {
    background: white;
    padding: 2rem;
    border-radius: 8px;
    width: 400px;
    max-width: 90%;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    transform: translateY(20px);
    transition: transform 0.2s;
}
.modal-overlay.open .modal {
    transform: translateY(0);
}
.modal h2 {
    margin-top: 0;
    font-size: 1.25rem;
    margin-bottom: 1.5rem;
}
.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.modal-btn {
    padding: 0.8rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: white;
    font-weight: 600;
    cursor: pointer;
    text-align: center;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    color: var(--text-secondary);
}
.modal-btn svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}
.modal-btn.primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
.modal-btn.primary:hover {
    background: var(--primary-hover);
    color: white;
}
.modal-btn.secondary:hover {
    background: #f8fafc;
    color: var(--primary);
    border-color: var(--border);
}
.modal-btn.cancel {
    margin-top: 1rem;
    color: var(--text-secondary);
    border: none;
}
.modal-btn.cancel:hover {
    background: #f1f5f9;
    color: var(--text);
}
/* Legacy block-style modal buttons (template picker) */
.modal-btn-block {
    display: block;
    width: 100%;
    padding: 0.8rem;
    margin-bottom: 0.5rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    text-align: left;
}
.modal-btn-block:hover {
    border-color: var(--primary);
    color: var(--primary);
}

/* --- TABLE TABS (Review: per-table tab bar) --- */
.tabs-container {
    position: relative;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0;
    background: var(--surface);
    padding: 0 2.5rem;
    border-radius: 0.5rem 0.5rem 0 0;
    display: flex;
    align-items: center;
}
.tabs-scroll-area {
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    white-space: nowrap;
    width: 100%;
    scroll-behavior: smooth;
}
.tabs-scroll-area::-webkit-scrollbar {
    display: none;
}
.tabs {
    display: flex;
    gap: 1.5rem;
}
.tab {
    padding: 1rem 0;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    font-weight: 500;
    border-bottom: 3px solid transparent;
    white-space: nowrap;
    transition: all 0.2s;
}
.tab:hover {
    color: var(--primary);
}
.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}
.tab-count {
    background: var(--error-bg);
    color: var(--error-text);
    padding: 0.1rem 0.5rem;
    border-radius: 99px;
    font-size: 0.7rem;
    margin-left: 0.4rem;
    font-weight: 700;
}
.tab-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: var(--surface);
    border: 1px solid var(--border);
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    color: var(--text-secondary);
    font-size: 1.2rem;
    line-height: 1;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}
.tab-arrow.visible {
    opacity: 1;
    pointer-events: auto;
}
.tab-arrow.left {
    left: 0.5rem;
}
.tab-arrow.right {
    right: 0.5rem;
}
.tab-arrow:hover {
    color: var(--primary);
    border-color: var(--primary);
}

/* --- TAB CONTENT AREA --- */
.tab-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 0.5rem 0.5rem;
    min-height: 500px;
    padding-bottom: 2rem;
}

/* --- STICKY HEADER --- */
.sticky-header {
    position: sticky;
    top: 0;
    z-index: 90;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1.5rem;
    box-shadow: 0 4px 12px -2px rgba(0, 0, 0, 0.05);
}

/* --- DIAGNOSTICS LAYOUT --- */
.diagnostics-layout {
    display: flex;
    gap: 2rem;
    align-items: flex-start;
}

/* --- HEATMAP --- */
.heatmap-container {
    width: 300px;
    flex-shrink: 0;
    background: white;
    border: 1px solid var(--border);
    border-radius: 6px;
    position: relative;
    z-index: 5;
}
.heatmap-title {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    padding: 0.5rem;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    text-align: center;
    border-radius: 6px 6px 0 0;
}
.heatmap-grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    gap: 1px;
    background: #cbd5e1;
    border: 1px solid #cbd5e1;
    height: 140px;
    padding: 1px;
    border-radius: 0 0 6px 6px;
    overflow: visible;
}
.heatmap-cell {
    position: relative;
    background: #fff;
    cursor: default;
}
.heatmap-cell.header-row {
    background: #f1f5f9;
}
.heatmap-cell[data-intensity]:not([data-intensity="0"]) {
    cursor: pointer;
}

/* Heatmap Bloom Effect */
.heatmap-cell[data-intensity]::before {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(
        circle,
        var(--heat-color) 0%,
        transparent 75%
    );
    opacity: var(--heat-opacity);
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1;
    pointer-events: none;
}
.heatmap-cell:hover {
    z-index: 100;
}
.heatmap-cell:hover::before {
    transform: scale(2.5);
    opacity: 1;
}

/* Heatmap Intensity Colors */
.heatmap-cell[data-intensity="0"] {
    --heat-color: transparent;
    --heat-opacity: 0;
}
.heatmap-cell[data-intensity="1"] {
    --heat-color: #fcd34d;
    --heat-opacity: 0.6;
}
.heatmap-cell[data-intensity="2"] {
    --heat-color: #fbbf24;
    --heat-opacity: 0.7;
}
.heatmap-cell[data-intensity="3"] {
    --heat-color: #f59e0b;
    --heat-opacity: 0.8;
}
.heatmap-cell[data-intensity="4"] {
    --heat-color: #ef4444;
    --heat-opacity: 0.9;
}
.heatmap-cell[data-intensity="5"] {
    --heat-color: #dc2626;
    --heat-opacity: 1;
}

/* --- HEATMAP TOOLTIPS --- */
.heatmap-cell .tooltip {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #ffffff;
    color: var(--text);
    padding: 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    z-index: 1000;
    display: none;
    width: 240px;
    border: 1px solid var(--border);
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.15);
    margin-top: 10px;
    text-align: left;
    pointer-events: auto;
}
.heatmap-cell:nth-child(9n) .tooltip,
.heatmap-cell:nth-child(9n-1) .tooltip {
    left: auto;
    right: 0;
    transform: translateX(10%);
}
.heatmap-cell:nth-child(9n) .tooltip::before,
.heatmap-cell:nth-child(9n-1) .tooltip::before {
    left: auto;
    right: 20px;
    transform: rotate(45deg);
}
.heatmap-cell .tooltip::after {
    content: "";
    position: absolute;
    top: -15px;
    left: 0;
    width: 100%;
    height: 15px;
    background: transparent;
}
.heatmap-cell:hover .tooltip {
    display: block;
}
.tooltip-link {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem;
    color: var(--text);
    text-decoration: none;
    border-bottom: 1px solid var(--bg);
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.1s;
}
.tooltip-link:hover {
    background-color: #f1f5f9;
}
.tooltip-link strong {
    color: var(--primary);
    font-family: var(--font-mono);
}

/* --- FILTER CONTROLS --- */
.filter-controls {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.header-row-flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
    gap: 1.5rem;
}
.filter-controls h3 {
    font-size: 1rem;
    margin: 0;
    color: var(--text);
    white-space: nowrap;
    flex-shrink: 0;
}
.filter-controls input,
.filter-controls select {
    padding: 0.6rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    width: 100%;
    font-size: 0.9rem;
}

/* --- EXPORT BUTTON --- */
.export-btn {
    padding: 0.4rem 0.8rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
}
.export-btn svg {
    width: 14px;
    height: 14px;
    fill: currentColor;
    transition: fill 0.2s;
}
.export-btn:hover {
    background: #f1f5f9;
    color: var(--primary);
    border-color: var(--primary);
}

/* --- TABLES --- */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}
th {
    cursor: pointer;
    user-select: none;
    position: relative;
    white-space: nowrap;
    font-size: 0.7rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-secondary);
}
th:hover {
    background: #f1f5f9;
}
th::after {
    content: " \2195";
    opacity: 0.3;
    font-size: 0.7rem;
}
th.asc::after {
    content: " \25B2";
    opacity: 1;
    color: var(--primary);
}
th.desc::after {
    content: " \25BC";
    opacity: 1;
    color: var(--primary);
}

.main-table th {
    position: sticky;
    top: 0;
    text-align: left;
    padding: 0.75rem 1rem;
    background: #fff;
    border-bottom: 2px solid var(--border);
    color: var(--text-secondary);
    white-space: nowrap;
    z-index: 10;
}
.main-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    color: var(--text-secondary);
}
.main-table td.has-value {
    color: var(--text);
}

.error-table th {
    position: sticky;
    top: 0;
    text-align: left;
    padding: 0.75rem 1.5rem;
    border-bottom: 2px solid var(--border);
    background: #fff;
    color: var(--text-secondary);
    z-index: 10;
}
.error-table th:nth-child(2) {
    min-width: 100px;
}
.error-table td {
    padding: 0.75rem 1.5rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.error-table tr:hover {
    background: #f1f5f9;
    cursor: pointer;
}

/* --- BADGES --- */
.code-badge {
    background: #fee2e2;
    color: #991b1b;
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-weight: 600;
    font-size: 0.75rem;
    font-family: var(--font-mono);
}
.loc-badge {
    font-family: var(--font-mono);
    font-weight: 600;
    color: var(--text);
    background: #f1f5f9;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
}
.val-cell {
    font-family: var(--font-mono);
    color: var(--text);
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- ROW HIGHLIGHTS --- */
tr.highlighted-error {
    background-color: var(--highlight-error) !important;
    transition: background-color 0.5s;
}
tr.highlighted-data {
    background-color: var(--highlight) !important;
    transition: background-color 0.5s;
}

/* --- ABOUT TABLE --- */
.about-table th {
    cursor: default;
    width: 25%;
    background: #f8fafc;
    color: var(--text-secondary);
    text-align: left;
    padding: 0.75rem 1.5rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
    font-weight: 600;
}
.about-table th::after {
    content: none;
}
.about-table td {
    padding: 0.75rem 1.5rem;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    vertical-align: top;
}

/* --- COLLAPSIBLE DATA DETAILS --- */
details.data-details {
    border-bottom: 1px solid var(--border);
    background: #fff;
    margin-bottom: 0;
}
details.data-details summary {
    padding: 1rem 1.5rem;
    cursor: pointer;
    font-weight: 600;
    color: var(--text);
    border: 1px solid var(--border);
    border-left: none;
    border-right: none;
    border-top: none;
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 0.5rem;
    background: #f8fafc;
    transition: background 0.2s;
}
details.data-details summary:hover {
    background: #e2e8f0;
}
details.data-details summary::-webkit-details-marker {
    display: none;
}
details.data-details summary::before {
    content: "\25B8";
    font-size: 0.9rem;
    color: var(--text-secondary);
    width: 1rem;
    display: inline-block;
    transition: transform 0.2s ease;
}
details.data-details[open] summary::before {
    transform: rotate(90deg);
}
details.data-details summary span.hint {
    font-weight: 400;
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-left: auto;
}
.data-content {
    padding: 0;
    max-height: 600px;
    overflow-y: auto;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

/* --- STATES --- */
.loading,
.empty-state {
    padding: 4rem;
    text-align: center;
    color: var(--text-secondary);
}
.success-state {
    padding: 3rem;
    text-align: center;
    color: var(--success-text);
    background: #f0fdf4;
    margin: 1rem;
    border-radius: 8px;
    border: 1px dashed var(--success-bg);
}

/* --- RETURN TO TOP --- */
#returnTop {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--primary);
    color: white;
    width: 3rem;
    height: 3rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    font-size: 1.5rem;
    z-index: 500;
    border: none;
}
#returnTop.visible {
    opacity: 1;
    pointer-events: auto;
}
#returnTop:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
}

/* --- IMPORT TAB --- */
.import-gate {
    text-align: center;
    max-width: 600px;
    margin: 4rem auto;
}

/* --- NEW STATE MACHINE UI STYLES --- */

/* Header controls */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.header-controls {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.status-message { font-size: 0.875rem; color: var(--text-secondary); }
.status-message.error { color: var(--error-text); }
.status-message.success { color: var(--success-text); }
.status-message.warning { color: var(--warning-text); }
.status-message.muted { color: var(--text-secondary); }

.btn-danger {
    background: var(--error-text);
    color: white;
    border: none;
    padding: 0.375rem 0.75rem;
    border-radius: 0.375rem;
    cursor: pointer;
    font-size: 0.8rem;
}
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.75rem; }
.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 0.375rem;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.15s, transform 0.1s;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: scale(0.97); }
.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.5rem 1rem;
    border-radius: 0.375rem;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}
.btn-secondary:active { transform: scale(0.97); }

/* STALE banner */
.stale-banner {
    display: none;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    background: var(--warning-bg);
    color: var(--warning-text);
    border-radius: 0.5rem;
    margin: 0.5rem 0;
}

/* Status banner in dashboard */
.status-banner {
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.status-success { border-left: 4px solid var(--success-text); background: rgba(220,252,231,0.4); color: var(--success-text); }
.status-error { border-left: 4px solid var(--error-text); background: rgba(254,226,226,0.4); color: var(--error-text); }
.status-warning { border-left: 4px solid var(--warning-text); background: rgba(254,243,199,0.4); color: var(--warning-text); }

/* Cards */
.cards-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1.25rem;
    margin: 1.5rem 0;
}
.card-value-success { color: var(--success-text); }
.card-value-error { color: var(--error-text); }
.card-value-warning { color: var(--warning-text); }
.card-accent-success { border-left: 3px solid var(--success-text); }
.card-accent-warning { border-left: 3px solid var(--warning-text); }
.card-accent-error { border-left: 3px solid var(--error-text); }
.card-subtitle { font-size: 0.75rem; color: var(--text-secondary); }
.card-secondary {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 0.35rem;
  font-variant-numeric: tabular-nums;
}
.card-notice {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 0.35rem;
}

/* Dashboard metadata line */
.dashboard-meta {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
}

/* Sections */
.section { margin: 2.5rem 0; }
.section h3 { margin-bottom: 1rem; }
.dim { color: var(--text-secondary); }

/* Table wrapping */
.dashboard-table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    overflow: hidden;
}

/* Entity resolution drill-down */
.entity-toggle {
    text-decoration: underline;
    text-decoration-style: dotted;
    cursor: pointer;
    color: inherit;
}
.entity-toggle:hover {
    text-decoration-style: solid;
}
.entity-panel {
    margin-top: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 1rem;
}
.entity-columns {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}
.entity-col h4 {
    margin: 0 0 0.5rem 0;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.entity-col-matched h4 { color: var(--success-text); }
.entity-col-new h4 { color: var(--warning-text); }
.entity-col ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.entity-col li {
    padding: 0.25rem 0;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border);
}
.entity-col li:last-child { border-bottom: none; }
.entity-db-name {
    color: var(--text-secondary);
    font-size: 0.8rem;
}
.entity-empty {
    color: var(--text-secondary);
    font-style: italic;
}

/* Findings section */
.findings-section { margin: 1.5rem 0; }
.error-table { width: 100%; border-collapse: collapse; }
.error-table th, .error-table td { padding: 0.5rem; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.875rem; }
.cell-value { font-family: var(--font-mono); font-size: 0.8rem; max-width: 150px; overflow: hidden; text-overflow: ellipsis; }

/* Correction dropdown */
.correction-select {
    padding: 0.25rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    font-size: 0.8rem;
    min-width: 120px;
}
.correction-cell { white-space: nowrap; }

/* Data details */
.data-details { margin: 1rem 0; }
.data-details summary { cursor: pointer; font-weight: 600; padding: 0.5rem 0; }
.table-wrapper { overflow-x: auto; }
.main-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.main-table th, .main-table td { padding: 0.375rem 0.5rem; border-bottom: 1px solid var(--border); }
.main-table th { background: var(--bg); position: sticky; top: 0; }
.main-table th.sortable { cursor: pointer; }
.main-table th.sortable:hover { background: var(--border); }
.row-num { color: var(--text-secondary); font-size: 0.75rem; }


/* Import form */
.import-preview { max-width: 700px; margin: 0 auto; padding: 1.5rem 0; }
.import-readiness { max-width: 700px; margin: 0 auto; padding: 1.5rem 0; }
.import-readiness h2, .import-preview h2 { margin-bottom: 0.5rem; }
.import-readiness .dim, .import-preview .dim { margin-bottom: 1.5rem; }
.import-readiness .main-table, .import-preview .main-table { font-size: 0.9rem; }
.import-readiness .main-table th,
.import-readiness .main-table td,
.import-preview .main-table th,
.import-preview .main-table td { padding: 0.625rem 0.875rem; }
.import-form { margin: 1.5rem 0; }
.form-group { margin: 1rem 0; }
.form-group label { display: block; font-weight: 500; margin-bottom: 0.375rem; }
.form-select, .form-textarea {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    font-size: 0.9rem;
}
.import-actions { display: flex; gap: 0.75rem; margin-top: 1.5rem; }
.import-status {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 1rem 1.25rem;
    border-radius: 0.5rem;
    margin: 1.5rem 0;
    font-size: 0.9rem;
    line-height: 1.5;
}
.import-status i { font-size: 1.5rem; flex-shrink: 0; margin-top: 0.1rem; }
.import-status p { margin: 0.25rem 0 0; color: var(--text-secondary); }
.import-status-ready { border-left: 4px solid var(--success-text); background: rgba(220,252,231,0.4); color: var(--success-text); }
.import-status-blocked { border-left: 4px solid var(--error-text); background: rgba(254,226,226,0.4); color: var(--error-text); }
.import-status-partial { border-left: 4px solid var(--warning-text); background: rgba(254,243,199,0.4); color: var(--warning-text); }
.import-table-link {
    color: var(--primary);
    text-decoration: none;
    font-weight: 500;
}
.import-table-link:hover { text-decoration: underline; }

/* Template modal options */
.template-option { margin: 0.25rem; }

/* Badge styles */
.badge-info { background: #dbeafe; color: #1e40af; }
.badge-error { background: var(--error-bg); color: var(--error-text); }
.badge-success { background: var(--success-bg); color: var(--success-text); }
.badge-warning { background: var(--warning-bg); color: var(--warning-text); }
.badge-muted { background: #f1f5f9; color: #64748b; }

/* --- DASHBOARD RESTRUCTURE --- */

/* Section headings with separators */
.dashboard-heading {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text);
    margin: 2.5rem 0 0.25rem 0;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.dashboard-separator {
    border: none;
    border-top: 2px solid var(--border);
    margin: 0 0 1.25rem 0;
}

/* Cross-table checks detail panel */
.crosscheck-detail-wrap {
    margin-top: 0.75rem;
    max-height: 300px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-top: 0.5rem;
}

/* Entity heading row with toggle button */
.entity-heading-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
}
.entity-heading-row h3 { margin-bottom: 0; }
.entity-expand-btn { flex-shrink: 0; }

/* Scrollable entity panels */
.entity-panel-scrollable {
    max-height: 350px;
    overflow-y: auto;
}

/* Ambiguous entity styling */
.card-value-ambiguous { color: #b45309; }
.entity-col-ambiguous h4 { color: #b45309; }
.entity-ambiguous-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

/* Entity toggle buttons (inline in table cells) */
button.entity-toggle {
    background: none;
    border: none;
    padding: 0;
    font: inherit;
    text-decoration: underline;
    text-decoration-style: dotted;
    cursor: pointer;
    color: inherit;
}
button.entity-toggle:hover {
    text-decoration-style: solid;
}

/* Error navigation buttons in dashboard sample */
button.error-nav-link {
    border: none;
    cursor: pointer;
    font: inherit;
}
button.error-nav-link:hover {
    filter: brightness(0.9);
}

/* Source filename in header */
.status-filename {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-weight: 500;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- EXPORT MODAL --- */
.export-options {
    margin: 1rem 0;
}
.export-option-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.export-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    cursor: pointer;
    font-size: 0.875rem;
    transition: background 0.15s;
}
.export-option:hover {
    background: var(--bg);
}
.export-option input[type="checkbox"] {
    accent-color: var(--primary);
}

/* --- REVIEW LAYOUT (vertical sidebar + content) --- */
.review-layout {
    display: flex;
    gap: 0;
    height: 100%;
    min-height: 400px;
    position: relative;
}
.review-sidebar {
    width: 160px;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    border-right: 1px solid var(--border);
    background: var(--bg);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.75rem 0;
    z-index: 100;
    overflow-x: hidden;
    overflow-y: auto;
    transition: width 0.15s ease;
}
.review-sidebar:hover {
    width: 240px;
    box-shadow: 2px 0 12px rgba(0,0,0,0.08);
}
.review-content {
    flex: 1;
    min-width: 0;
    overflow-y: auto;
    margin-left: 160px;
}

/* Vertical tab buttons */
.review-tab {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 0.75rem;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 0.8rem;
    color: var(--text-secondary);
    white-space: nowrap;
    text-align: left;
    width: 100%;
    transition: background 0.15s;
}
.review-tab:hover {
    background: var(--surface);
}
.review-tab.active {
    background: rgba(37, 99, 235, 0.04);
    color: var(--text);
    font-weight: 600;
    border-right: 3px solid var(--primary);
}
.review-tab-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.review-tab-dot-green { background: var(--success-text); }
.review-tab-dot-orange { background: #b45309; }
.review-tab-dot-red { background: var(--error-text); }
.review-tab-label {
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}
.review-tab-count {
    font-size: 0.7rem;
    background: var(--bg);
    color: var(--text-secondary);
    padding: 0.1rem 0.35rem;
    border-radius: 99px;
    flex-shrink: 0;
}

/* Mobile table selector (hidden on desktop) */
.review-mobile-select {
    display: none;
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    background: var(--surface);
    color: var(--text);
    font-size: 0.875rem;
    margin-bottom: 0.75rem;
}

/* Segmented control (fixable/source filter) */
.segmented-control {
    display: inline-flex;
    align-self: flex-start;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    overflow: hidden;
    margin-bottom: 0.5rem;
}
.segmented-btn {
    padding: 0.35rem 1rem;
    min-width: 5rem;
    text-align: center;
    border: none;
    border-right: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.segmented-btn:last-child { border-right: none; }
.segmented-btn:hover { background: var(--bg); }
.segmented-btn.active {
    background: var(--primary);
    color: white;
}

/* --- RESPONSIVE --- */
@media (max-width: 768px) {
    .container { padding: 1rem; }
    h1 { font-size: 1.25rem; }
    .diagnostics-layout { flex-direction: column; }
    .heatmap-container { width: 100%; }
    .cards-row { grid-template-columns: 1fr 1fr; }
    .app-header { flex-direction: column; align-items: flex-start; gap: 0.75rem; }
    .header-controls { width: 100%; justify-content: space-between; flex-wrap: wrap; }
    .nav-tabs { overflow-x: auto; gap: 0; }
    .nav-tab { padding: 0.75rem 1rem; font-size: 0.85rem; }
    .upload-container { padding: 2.5rem 1.5rem; margin: 1rem auto; }
    .import-actions { flex-direction: column; }
    .import-preview, .import-readiness { padding: 1rem 0; }
    /* Review: sidebar → dropdown on mobile */
    .review-sidebar { display: none; }
    .review-mobile-select { display: block; }
    .review-layout { flex-direction: column; }
    .review-content { margin-left: 0; }
    .sticky-header { padding: 1rem; }
    .filter-controls input, .filter-controls select { font-size: 0.85rem; }
}
@media (max-width: 480px) {
    .cards-row { grid-template-columns: 1fr; }
    .entity-columns { grid-template-columns: 1fr; }
}

/* Screen-reader-only utility */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Respect user preference for reduced motion */
@media (prefers-reduced-motion: reduce) {
    html,
    .tabs-scroll-area {
        scroll-behavior: auto;
    }
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ---------------------------------------------------------------------- */
/* Flag modal (FLAGGED feedback flow)                                     */
/* ---------------------------------------------------------------------- */

.flag-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.flag-modal-overlay.flag-modal-open { display: flex; }

.flag-modal-card {
  background: #fff;
  border-radius: 8px;
  width: min(900px, 95vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
  gap: 0.75rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.flag-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e2e8f0;
  padding-bottom: 0.5rem;
}
.flag-modal-header h2 { margin: 0; font-size: 1.15rem; }
.flag-modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0 0.25rem;
}

.flag-modal-hint { color: #64748b; font-size: 0.85rem; margin: 0; }

.flag-modal-tabs { display: flex; gap: 0.25rem; }
.flag-tab {
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  padding: 0.25rem 0.6rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.flag-tab.active { background: #2563eb; color: #fff; border-color: #2563eb; }

.flag-modal-filter {
  width: 100%;
  padding: 0.4rem 0.6rem;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  font-size: 0.9rem;
}

.flag-modal-list {
  flex: 1;
  overflow-y: auto;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  background: #f8fafc;
  min-height: 200px;
  max-height: 50vh;
}

.flag-row {
  display: grid;
  grid-template-columns: auto auto 1fr 1fr 2fr auto;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid #e2e8f0;
  align-items: center;
  font-size: 0.85rem;
  cursor: pointer;
}
.flag-row:hover { background: #eff6ff; }
.flag-row-checked { background: #fef3c7; }

.flag-row-target {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  white-space: nowrap;
}
.flag-row-target-auto_correction { background: #dbeafe; color: #1d4ed8; }
.flag-row-target-error { background: #fee2e2; color: #b91c1c; }
.flag-row-target-source_only { background: #fef3c7; color: #92400e; }

.flag-row-loc { font-family: ui-monospace, monospace; color: #475569; }
.flag-row-value { color: #1e293b; }
.flag-row-message { color: #64748b; }

.flag-modal-comment-label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: #475569;
}
.flag-modal-comment {
  width: 100%;
  padding: 0.4rem 0.6rem;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.9rem;
  resize: vertical;
}

.flag-modal-send-hint { color: #92400e; font-size: 0.8rem; margin: 0; }

.flag-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  align-items: center;
  border-top: 1px solid #e2e8f0;
  padding-top: 0.5rem;
}
.flag-modal-count { margin-right: auto; color: #64748b; font-size: 0.85rem; }

.btn-warn {
  background: #f59e0b;
  border-color: #d97706;
  color: #fff;
}
.btn-warn:hover { background: #d97706; }

.status-warn {
  background: #fef3c7;
  color: #92400e;
  border-left: 4px solid #f59e0b;
}
.badge-warn { background: #f59e0b; color: #fff; }
