/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #ffffff;
    --bg-subtle: #f8f8f8;
    --surface: #ffffff;
    --border: #e5e5e5;
    --border-light: #f0f0f0;
    --text: #171717;
    --text-secondary: #737373;
    --text-tertiary: #a3a3a3;
    --primary: #171717;
    --primary-hover: #404040;
    --highlight: #fef9c3;
    --highlight-border: #facc15;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.06), 0 1px 3px rgba(0,0,0,0.04);
    --transition: 150ms ease;
}

html { font-size: 15px; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.84rem;
    font-weight: 500;
    border: none;
    cursor: pointer;
    text-decoration: none;
    transition: all var(--transition);
    font-family: inherit;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg-subtle); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }

/* === Index Page === */
.index-page {
    max-width: 480px;
    margin: 0 auto;
    padding: 80px 24px 120px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 32px;
}

.upload-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 14px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all var(--transition);
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 24px;
}

.upload-area:hover {
    border-color: var(--primary);
    color: var(--text);
    background: var(--bg-subtle);
}

.upload-area.drag-over {
    border-color: var(--primary);
    background: var(--bg-subtle);
    color: var(--text);
}

.datasets-list {
    display: flex;
    flex-direction: column;
    gap: 1px;
    background: var(--border-light);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.dataset-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--surface);
    text-decoration: none;
    color: var(--text);
    transition: background var(--transition);
}

.dataset-row:hover { background: var(--bg-subtle); }

.dataset-name { font-weight: 500; font-size: 0.9rem; }

.dataset-meta { font-size: 0.78rem; color: var(--text-tertiary); }

/* === Dataset Page === */
.dataset-page {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px 24px;
}

.dataset-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    gap: 12px;
    flex-wrap: wrap;
}

.dataset-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.back-link {
    color: var(--text-tertiary);
    text-decoration: none;
    display: flex;
    transition: color var(--transition);
}

.back-link:hover { color: var(--text); }

.dataset-name {
    font-size: 1.2rem;
    font-weight: 700;
}

.badge {
    padding: 2px 10px;
    background: var(--bg-subtle);
    border: 1px solid var(--border);
    border-radius: 100px;
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.dataset-header-actions {
    display: flex;
    gap: 6px;
}

/* === Toolbar === */
.toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.search-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0 10px;
    flex: 1;
    min-width: 180px;
    transition: border-color var(--transition);
}

.search-box:focus-within { border-color: var(--primary); }
.search-box svg { color: var(--text-tertiary); flex-shrink: 0; }

.search-box input {
    border: none;
    background: none;
    padding: 8px 0;
    font-size: 0.85rem;
    font-family: inherit;
    color: var(--text);
    width: 100%;
    outline: none;
}


/* === Layout === */
.dataset-layout {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 16px;
    align-items: start;
}

/* === Table === */
.table-wrapper {
    min-width: 0; /* Prevent grid blowout on wide tables */
}

.table-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    overflow: hidden;
}

.table-scroll {
    overflow-x: auto;
    max-height: 78vh;
    overflow-y: auto;
}

.data-table {
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
    font-size: 0.84rem;
}

.data-table thead { position: sticky; top: 0; z-index: 10; }

.data-table th {
    background: var(--bg-subtle);
    padding: 8px 12px;
    text-align: left;
    font-weight: 500;
    font-size: 0.78rem;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.data-table th.sortable {
    cursor: pointer;
    user-select: none;
    transition: color var(--transition);
}

.data-table th.sortable:hover {
    color: var(--text);
}

.sort-icon::after {
    content: '⇅';
    margin-left: 4px;
    opacity: 0.3;
    font-size: 0.7rem;
}

.sort-asc .sort-icon::after {
    content: '↑';
    opacity: 0.8;
}

.sort-desc .sort-icon::after {
    content: '↓';
    opacity: 0.8;
}

.data-table td {
    padding: 6px 12px;
    border-bottom: 1px solid var(--border-light);
    white-space: nowrap;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.data-table tbody tr { transition: background var(--transition); }
.data-table tbody tr:hover { background: var(--bg-subtle); }

.data-table tbody tr.highlighted {
    background: var(--highlight);
}

.data-table th.row-num,
.data-table td.row-num {
    position: sticky;
    left: 0;
    z-index: 5;
    background: var(--bg-subtle);
    color: var(--text-tertiary);
    font-size: 0.74rem;
    width: 44px;
    min-width: 44px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    border-right: 1px solid var(--border);
}

.data-table th.row-num {
    z-index: 15;
}

.data-table td.row-num {
    background: var(--surface);
}

.table-footer {
    padding: 8px 12px;
    font-size: 0.78rem;
    color: var(--text-tertiary);
    border-top: 1px solid var(--border);
}

/* === Insights Sidebar === */
.insights-sidebar {
    position: sticky;
    top: 20px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 7px;
    margin-bottom: 12px;
}

.sidebar-header svg { color: var(--text-secondary); }
.sidebar-header h3 { font-size: 0.85rem; font-weight: 600; }

.insights-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.insight-card {
    display: flex;
    flex-direction: column;
    gap: 5px;
    width: 100%;
    padding: 10px;
    background: var(--bg-subtle);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
    text-align: left;
    font-family: inherit;
    font-size: 0.82rem;
    color: var(--text);
}

.insight-card:hover { border-color: var(--border); }
.insight-card.active { border-color: var(--highlight-border); background: var(--highlight); }

.insight-type-badge {
    display: inline-flex;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    width: fit-content;
}

.insight-type-badge.info { background: #f0f9ff; color: #0369a1; }
.insight-type-badge.stat { background: #f0fdf4; color: #15803d; }
.insight-type-badge.anomaly { background: #fef2f2; color: #b91c1c; }
.insight-type-badge.correlation { background: #faf5ff; color: #7e22ce; }
.insight-type-badge.distribution { background: #fff7ed; color: #c2410c; }
.insight-type-badge.quality { background: #fefce8; color: #a16207; }

.insight-text { line-height: 1.4; }
.insight-row-count { font-size: 0.72rem; color: var(--text-tertiary); }

/* === Pagination === */
.pagination {
    border-top: 1px solid var(--border);
    background: var(--surface);
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}

.pagination-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    gap: 12px;
    flex-wrap: wrap;
}

.pagination-left {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 0.78rem;
    color: var(--text-tertiary);
}

.pagination-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

.pagination-rpp { display: flex; align-items: center; gap: 4px; }

.rpp-btn {
    background: none;
    border: none;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.78rem;
    font-family: inherit;
    color: var(--text-tertiary);
    cursor: pointer;
    transition: all var(--transition);
    min-height: 28px;
}

.rpp-btn:hover { background: var(--bg-subtle); color: var(--text); }
.rpp-btn.active { background: var(--primary); color: white; }

.page-indicator {
    font-size: 0.78rem;
    color: var(--text-tertiary);
    min-width: 50px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.btn:disabled { opacity: 0.3; cursor: not-allowed; }

/* === Filter Pills === */
.filter-pills {
    display: flex;
    gap: 4px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 2px 0;
}

.filter-pills::-webkit-scrollbar { display: none; }

.filter-pill {
    flex-shrink: 0;
    padding: 4px 12px;
    border-radius: 100px;
    font-size: 0.78rem;
    font-weight: 500;
    font-family: inherit;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
    min-height: 32px;
}

.filter-pill:hover { background: var(--bg-subtle); color: var(--text); }
.filter-pill.active { background: var(--primary); color: white; border-color: var(--primary); }

/* === HTMX Loading States === */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator { display: block; }
.htmx-request.htmx-indicator { display: block; }

.table-loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0.6);
    z-index: 20;
    pointer-events: none;
}

.table-scroll { position: relative; }

/* Subtle pulse on table body during HTMX request */
#table-body.htmx-request { opacity: 0.5; transition: opacity 0.15s; }

/* === First Visit Banner === */
.first-visit-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 0.84rem;
}

.first-visit-banner code {
    background: white;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    border: 1px solid #d1fae5;
}

.banner-dismiss {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

/* === Malformed Rows Banner === */
.malformed-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: #fffbeb;
    border: 1px solid #fde68a;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 0.84rem;
    color: #92400e;
}

/* === Upload Error Banner === */
.upload-error {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 0.84rem;
    color: #991b1b;
}

/* === Client-Side Preview === */
.preview-area {
    margin-bottom: 24px;
}

.preview-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.preview-filename {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text);
}

.preview-progress {
    flex: 1;
    height: 3px;
    background: var(--border-light);
    border-radius: 2px;
    overflow: hidden;
}

.preview-progress-bar {
    height: 100%;
    background: var(--primary);
    border-radius: 2px;
    transition: width 0.3s ease;
    width: 0;
}

/* === Insights States === */
.insights-loading {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 0;
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.pulse-dot {
    width: 8px;
    height: 8px;
    background: var(--primary);
    border-radius: 50%;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

.insights-empty {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 12px 0;
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.insights-footer {
    padding-top: 10px;
    border-top: 1px solid var(--border-light);
    margin-top: 10px;
    font-size: 0.72rem;
    color: var(--text-tertiary);
    font-style: italic;
}

/* === Empty State === */
.empty-state {
    text-align: center;
    padding: 40px 20px !important;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* === Title Editing === */
.editable-title {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.editable-title .edit-icon {
    opacity: 0;
    transition: opacity var(--transition);
    color: var(--text-tertiary);
}

.editable-title:hover .edit-icon { opacity: 0.6; }

.title-form { display: inline-flex; }

.title-input {
    font-size: 1.2rem;
    font-weight: 700;
    font-family: inherit;
    color: var(--text);
    background: transparent;
    border: none;
    border-bottom: 2px solid var(--primary);
    outline: none;
    padding: 0 0 2px;
    width: 300px;
    max-width: 60vw;
}

/* === Text Utilities === */
.text-tertiary { color: var(--text-tertiary); font-size: 0.78rem; }

/* === Focus Styles === */
.btn:focus-visible,
.filter-pill:focus-visible,
.search-box input:focus-visible,
.data-table th.sortable:focus-visible,
.rpp-btn:focus-visible,
.insight-card:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* === Sort Icon in HTMX mode === */
.sort-icon.sort-asc::after { content: '\2191'; opacity: 0.8; }
.sort-icon.sort-desc::after { content: '\2193'; opacity: 0.8; }

/* === Error Page === */
.error-page {
    max-width: 400px;
    margin: 120px auto;
    text-align: center;
    padding: 0 24px;
}

.error-page h1 { font-size: 1.3rem; margin-bottom: 8px; }
.error-page p { color: var(--text-secondary); margin-bottom: 24px; font-size: 0.9rem; }

/* === Responsive === */
@media (max-width: 900px) {
    .dataset-layout { grid-template-columns: 1fr; }
    .insights-sidebar { position: static; max-height: none; }
    .dataset-header { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 600px) {
    .toolbar { flex-direction: column; }
    .search-box { min-width: unset; }
    .index-page { padding: 48px 20px 80px; }
    .pagination-rpp { display: none; }
    .pagination-inner { justify-content: center; }
    .first-visit-banner { flex-wrap: wrap; font-size: 0.78rem; }
    .first-visit-banner code { font-size: 0.72rem; }
    .filter-pills { max-width: 100%; }
    /* Sticky first data column on mobile */
    .data-table td:nth-child(2),
    .data-table th:nth-child(2) {
        position: sticky;
        left: 44px;
        z-index: 4;
        background: var(--surface);
    }
    .data-table th:nth-child(2) {
        background: var(--bg-subtle);
        z-index: 14;
    }
}
