/* ===== Base & Scrollbar ===== */
* { box-sizing: border-box; }
body { font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; background: #f8fafc; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 3px; }
::-webkit-scrollbar-thumb { background: #94a3b8; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #64748b; }

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity:0; transform:translateY(8px); } to { opacity:1; transform:translateY(0); } }
@keyframes slideDown { from { opacity:0; transform:translateY(-12px); } to { opacity:1; transform:translateY(0); } }
@keyframes pulse-border { 0%,100% { border-color: #fbbf24; } 50% { border-color: #f59e0b; } }
.animate-fade-in { animation: fadeIn 0.35s ease-out; }
.animate-slide-down { animation: slideDown 0.3s ease-out; }
.upload-zone:hover { animation: pulse-border 1.5s infinite; }

/* ===== Table ===== */
.data-table { border-collapse: separate; border-spacing: 0; width: 100%; }
.data-table thead th {
    position: sticky; top: 0; z-index: 10;
    background: #1e293b; color: #f8fafc;
    font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em;
    padding: 10px 12px; cursor: pointer; user-select: none; white-space: nowrap;
    border-bottom: 2px solid #f59e0b;
    overflow: hidden; text-overflow: ellipsis;
}
.data-table thead th:hover { background: #334155; }
.data-table tbody tr { transition: background 0.15s; cursor: pointer; }
.data-table tbody tr:hover { background: #fef3c7 !important; }
.data-table tbody td { padding: 8px 12px; border-bottom: 1px solid #e2e8f0; font-size: 0.875rem; }
.data-table tbody td.number { text-align: right; font-variant-numeric: tabular-nums; }

/* ===== Badges ===== */
.badge { display: inline-flex; align-items: center; padding: 2px 10px; border-radius: 9999px; font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
.badge-exceed { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }
.badge-under { background: #fffbeb; color: #d97706; border: 1px solid #fde68a; }
.badge-normal { background: #f0fdf4; color: #16a34a; border: 1px solid #bbf7d0; }

/* ===== Modal Overlay ===== */
.modal-overlay {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(15,23,42,0.5); backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center;
    opacity: 0; transition: opacity 0.25s;
}
.modal-overlay.active { opacity: 1; }
.modal-overlay.active .modal-content { transform: scale(1); opacity: 1; }
.modal-content {
    background: white; border-radius: 16px; box-shadow: 0 25px 50px rgba(0,0,0,0.15);
    max-height: 90vh; overflow: hidden; display: flex; flex-direction: column;
    transform: scale(0.95); opacity: 0; transition: all 0.3s ease;
}

/* ===== Tabs ===== */
.tab-btn { padding: 8px 20px; font-size: 0.875rem; font-weight: 500; border-bottom: 2px solid transparent; color: #64748b; cursor: pointer; transition: all 0.2s; }
.tab-btn:hover { color: #1e293b; }
.tab-btn.active { color: #f59e0b; border-bottom-color: #f59e0b; }

/* ===== Analytics Tabs ===== */
.analytics-tabs { gap: 0; }
.analytics-tab {
    padding: 10px 20px; font-size: 0.875rem; font-weight: 500;
    border: none; border-bottom: 2px solid transparent;
    color: #64748b; cursor: pointer; transition: all 0.2s; background: none;
}
.analytics-tab:hover { color: #1e293b; background: #f8fafc; }
.analytics-tab.active { color: #f59e0b; border-bottom-color: #f59e0b; font-weight: 600; }

/* ===== Settings Table ===== */
.settings-table { border-collapse: collapse; width: 100%; font-size: 0.8rem; }
.settings-table th, .settings-table td { padding: 4px 6px; border: 1px solid #e2e8f0; }
.settings-table th { background: #f1f5f9; font-weight: 600; position: relative; }

/* ===== Column Resize Handle ===== */
.col-resize-handle {
    background: transparent;
    transition: background 0.15s;
}
.col-resize-handle:hover,
.col-resize-handle:active {
    background: rgba(245, 158, 11, 0.5) !important;
}
.data-table thead th.resizing {
    background: #334155 !important;
}
.settings-table input {
    width: 70px; padding: 3px 6px; border: 1px solid #e2e8f0; border-radius: 4px;
    font-size: 0.8rem; text-align: right; background: white;
}
.settings-table input:focus { outline: none; border-color: #f59e0b; box-shadow: 0 0 0 2px rgba(245,158,11,0.2); }
.settings-table .combo-input { width: 160px; text-align: left; font-weight: 500; }

/* ===== Debug Panel ===== */
.debug-toggle { cursor: pointer; user-select: none; }
.debug-content { max-height: 300px; overflow-y: auto; }

/* ===== Drag-and-Drop ===== */
.upload-zone.drag-over { border-color: #f59e0b !important; background: rgba(245,158,11,0.08) !important; }
.upload-zone.drag-over .drag-icon { transform: scale(1.15); transition: transform 0.2s; }

/* ===== Tooltip ===== */
.deviation-tooltip {
    position: absolute; z-index: 60; pointer-events: none;
    background: #1e293b; color: #f8fafc; border-radius: 10px;
    padding: 10px 14px; font-size: 0.8rem; line-height: 1.5;
    box-shadow: 0 8px 25px rgba(0,0,0,0.2); max-width: 320px;
    opacity: 0; transition: opacity 0.15s;
}
.deviation-tooltip.visible { opacity: 1; }
.deviation-tooltip::after {
    content: ''; position: absolute; top: 100%; left: 20px;
    border: 6px solid transparent; border-top-color: #1e293b;
}
.deviation-tooltip .tt-label { color: #94a3b8; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em; }
.deviation-tooltip .tt-value { color: #f8fafc; font-weight: 600; }
.deviation-tooltip .tt-formula { color: #fbbf24; font-family: monospace; font-size: 0.75rem; margin-top: 4px; }

/* ===== Nav Buttons ===== */
.nav-btn { padding: 6px 16px; font-size: 0.875rem; font-weight: 500; border-radius: 8px; color: #cbd5e1; transition: all 0.2s; cursor: pointer; background: transparent; border: none; }
.nav-btn:hover { color: #f8fafc; background: rgba(255,255,255,0.1); }
.nav-btn.active { color: #f59e0b; background: rgba(245,158,11,0.15); }

/* ===== Hypothesis Badges ===== */
.badge-ht { display: inline-flex; align-items: center; padding: 1px 8px; border-radius: 6px; font-size: 0.65rem; font-weight: 700; white-space: nowrap; margin-right: 2px; }
.badge-ht-1 { background: #dbeafe; color: #1d4ed8; }
.badge-ht-2 { background: #fef3c7; color: #b45309; }
.badge-ht-3 { background: #fce7f3; color: #be185d; }
.badge-ht-4 { background: #d1fae5; color: #065f46; }
.badge-ht-5 { background: #ede9fe; color: #6d28d9; }
.badge-ht-6 { background: #e0e7ff; color: #4338ca; }
.badge-ht-custom { background: #f5f3ff; color: #7c3aed; border: 1px dashed #a78bfa; }

/* ===== Toggle Switch ===== */
.toggle-slider {
    position: relative; display: inline-block;
    width: 36px; height: 20px; background: #cbd5e1;
    border-radius: 10px; transition: background 0.2s; cursor: pointer;
    margin-left: 4px;
}
.rule-active-toggle { display: none; }
.rule-active-toggle:checked + .toggle-slider { background: #f59e0b; }
.toggle-slider::after {
    content: ''; position: absolute; top: 2px; left: 2px;
    width: 16px; height: 16px; background: white;
    border-radius: 50%; transition: transform 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.rule-active-toggle:checked + .toggle-slider::after { transform: translateX(16px); }

/* ===== Severity Dots ===== */
.severity-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }
.severity-low { background: #22c55e; }
.severity-medium { background: #f59e0b; }
.severity-high { background: #ef4444; }

/* ===== Catalog Card ===== */
.catalog-card { background: white; border: 1px solid #e2e8f0; border-radius: 12px; padding: 16px; transition: box-shadow 0.2s, transform 0.2s; }
.catalog-card:hover { box-shadow: 0 8px 25px rgba(0,0,0,0.08); transform: translateY(-2px); }

/* ===== Checkbox Column ===== */
.row-checkbox { width: 16px; height: 16px; accent-color: #f59e0b; cursor: pointer; }

/* ===== Breadcrumb Drill-down ===== */
.drill-breadcrumb {
    display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: #64748b;
    padding: 6px 12px; background: #fffbeb; border-radius: 8px; margin-bottom: 8px;
}
.drill-breadcrumb .drill-link { color: #f59e0b; cursor: pointer; font-weight: 600; text-decoration: underline; }
.drill-breadcrumb .drill-sep { color: #cbd5e1; }


/* ===== Mobile Optimization ===== */
@media (max-width: 768px) {
    .data-table thead th, .data-table tbody td { padding: 6px 4px; font-size: 0.7rem; }
    .data-table thead th { font-size: 0.65rem; }
    header .flex { flex-wrap: wrap; gap: 8px; }
    nav.flex { width: 100%; justify-content: center; }
    #stats-cards { grid-template-columns: repeat(4, 1fr) !important; }
    .chart-container { min-height: 200px; }
    .modal-content { max-width: 95vw; margin: 10px; }
    .upload-zone { padding: 24px 12px !important; }
    .upload-zone svg.w-16 { width: 48px; height: 48px; }
    .flex.flex-wrap.items-center.gap-2, .flex.flex-wrap.items-center.gap-3 { gap: 6px !important; }
    button { font-size: 0.8rem !important; padding: 6px 10px !important; }
    .badge { font-size: 0.65rem; padding: 1px 6px; }
    #filter-preset, button[onclick="saveFilterPreset()"], button[onclick="deleteFilterPreset()"] { display: none; }
}
@media (max-width: 480px) {
    #stats-cards { grid-template-columns: repeat(2, 1fr) !important; }
    .data-table { font-size: 0.65rem; }
    .max-w-\[1400px\] { padding-left: 8px !important; padding-right: 8px !important; }
}

/* ===== Dark Theme ===== */
:root {
    --bg-primary: #f8fafc;
    --bg-card: #ffffff;
    --bg-header: #1e293b;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --table-hover: #fef3c7;
    --input-bg: #ffffff;
}
body.dark-theme {
    --bg-primary: #0f172a;
    --bg-card: #1e293b;
    --bg-header: #0f172a;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border-color: #334155;
    --table-hover: #334155;
    --input-bg: #1e293b;
}
body.dark-theme { background: var(--bg-primary); color: var(--text-primary); }
body.dark-theme .bg-white { background: var(--bg-card) !important; }
body.dark-theme .text-slate-700, body.dark-theme .text-slate-600, body.dark-theme .text-slate-800 { color: var(--text-primary) !important; }
body.dark-theme .text-slate-500, body.dark-theme .text-slate-400, body.dark-theme .text-slate-300 { color: var(--text-secondary) !important; }
body.dark-theme .border-slate-100, body.dark-theme .border-slate-200 { border-color: var(--border-color) !important; }
body.dark-theme .bg-slate-50 { background: #1e293b !important; }
body.dark-theme .data-table tbody tr:hover { background: var(--table-hover) !important; }
body.dark-theme .data-table tbody td { border-bottom-color: var(--border-color); }
body.dark-theme select, body.dark-theme input[type="text"], body.dark-theme input[type="number"] {
    background: var(--input-bg); color: var(--text-primary); border-color: var(--border-color);
}
body.dark-theme .settings-table th { background: #334155; }
body.dark-theme .settings-table input { background: var(--input-bg); color: var(--text-primary); border-color: var(--border-color); }
/* ===== Print ===== */
@media print {
    body { background: white !important; }
    header, footer, #upload-zone, #debug-panel,
    .no-print { display: none !important; }
    #data-content { display: block !important; }
    .card-hover:hover { box-shadow: none; transform: none; }
    .data-table thead th { background: #333 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .badge { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ===== Misc ===== */
.card-hover { transition: box-shadow 0.2s, transform 0.2s; }
.card-hover:hover { box-shadow: 0 8px 25px rgba(0,0,0,0.08); transform: translateY(-2px); }
.chart-container { position: relative; width: 100%; }
select, input[type="text"], input[type="number"] {
    border: 1px solid #cbd5e1; border-radius: 8px; padding: 6px 12px;
    font-size: 0.875rem; transition: border-color 0.2s;
}
select:focus, input[type="text"]:focus, input[type="number"]:focus {
    outline: none; border-color: #f59e0b; box-shadow: 0 0 0 2px rgba(245,158,11,0.15);
}
.period-select {
    font-size: 0.8rem; padding: 4px 10px; border-radius: 6px;
    min-width: 160px; color: #1e293b; background: #fff;
    border: 1px solid #cbd5e1; cursor: pointer;
}
.period-select:focus {
    outline: none; border-color: #f59e0b; box-shadow: 0 0 0 2px rgba(245,158,11,0.15);
}

/* ===== Table Filter Buttons & Dropdowns ===== */
.tf-btn {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 5px 10px; font-size: 0.8rem; font-weight: 500;
    border: 1px solid #cbd5e1; border-radius: 8px;
    background: #fff; color: #475569; cursor: pointer;
    transition: all 0.15s; white-space: nowrap;
}
.tf-btn:hover { border-color: #f59e0b; background: #fffbeb; }
.tf-btn.tf-active { border-color: #f59e0b; background: #fef3c7; color: #92400e; }
.tf-count {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 18px; height: 18px; padding: 0 4px;
    font-size: 0.7rem; font-weight: 700; border-radius: 9px;
    background: #f59e0b; color: #fff;
}
/* Compact filter icon inside <th> header cells */
.tf-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 20px; height: 20px; padding: 0;
    font-size: 0.7rem; font-weight: 600;
    border: none; border-radius: 4px;
    background: transparent; color: #94a3b8; cursor: pointer;
    transition: all 0.15s; vertical-align: middle;
    margin-left: 4px;
}
.tf-icon:hover { color: #f59e0b; background: rgba(245,158,11,0.15); }
.tf-icon.tf-active { color: #f59e0b; background: rgba(245,158,11,0.2); }
.tf-icon-count {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 14px; height: 14px; padding: 0 3px;
    font-size: 0.6rem; font-weight: 700; border-radius: 7px;
    background: #f59e0b; color: #fff; margin-right: 1px;
}
.tf-dropdown {
    background: #fff; border: 1px solid #e2e8f0; border-radius: 12px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.12); overflow: hidden;
    max-height: 320px; display: flex; flex-direction: column;
}
.tf-dd-header {
    display: flex; gap: 8px; padding: 8px 12px;
    border-bottom: 1px solid #e2e8f0; background: #f8fafc;
}
.tf-dd-action {
    padding: 3px 10px; font-size: 0.75rem; font-weight: 600;
    border: 1px solid #cbd5e1; border-radius: 6px;
    background: #fff; color: #475569; cursor: pointer;
    transition: all 0.15s;
}
.tf-dd-action:hover { background: #f1f5f9; border-color: #94a3b8; }
.tf-dd-list {
    overflow-y: auto; max-height: 260px; padding: 4px 0;
}
.tf-dd-item {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 12px; cursor: pointer; font-size: 0.8rem;
    color: #334155; transition: background 0.1s;
}
.tf-dd-item:hover { background: #f1f5f9; }
.tf-dd-item input[type="checkbox"] {
    width: 15px; height: 15px; accent-color: #f59e0b; cursor: pointer; flex-shrink: 0;
}
