:root {
    --primary: #2E4A7A;
    --primary-light: #3B6BA5;
    --accent: #92B4E0;
    --accent-light: #B8CEE8;
    --muted: #8B9DC3;
    --bg: #F4F6FA;
    --text: #2D3748;
    --text-light: #5A6B84;
    --border: #DDE4EE;
    --white: #FFFFFF;
    --success: #38A169;
    --warning: #D69E2E;
    --error: #C53030;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* Nav */
nav {
    background: var(--primary);
    padding: 0 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    box-shadow: 0 1px 6px rgba(44, 82, 130, 0.15);
}
nav .brand {
    color: var(--white);
    font-size: 1.15rem;
    font-weight: 700;
    text-decoration: none;
    letter-spacing: 0.3px;
}
nav .links { display: flex; gap: 0.25rem; align-items: center; }
nav .links a {
    color: var(--accent-light);
    text-decoration: none;
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    font-size: 0.875rem;
    transition: all 0.15s;
}
nav .links a:hover, nav .links a.active {
    background: rgba(255,255,255,0.15);
    color: var(--white);
}
nav .user-info {
    color: var(--accent-light);
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
nav .user-info a { color: var(--accent-light); font-size: 0.85rem; }

/* Layout */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem 2rem;
}

/* Cards */
.card {
    background: var(--white);
    border-radius: 10px;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    border: 1px solid var(--border);
}
.card h2 {
    font-size: 1rem;
    color: var(--primary);
    margin-bottom: 1rem;
    font-weight: 600;
}

/* Stats row */
.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.stat-card {
    background: var(--white);
    border-radius: 10px;
    padding: 1.25rem;
    border: 1px solid var(--border);
    text-align: center;
}
.stat-card .number {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary);
}
.stat-card .label {
    font-size: 0.85rem;
    color: var(--text-light);
    margin-top: 0.25rem;
}

/* Grid */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; margin-bottom: 1.5rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1.5rem; margin-bottom: 1.5rem; }

/* Tables */
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 0.875rem; }
th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    background: #EDF1F7;
    color: var(--primary);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 2;
    box-shadow: inset 0 -2px 0 var(--border);
}
td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border);
}
tr:hover td { background: rgba(74, 144, 196, 0.04); }
td a { color: var(--accent); text-decoration: none; }
td a:hover { color: var(--primary); text-decoration: underline; }

/* Clickable rows */
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: rgba(74, 144, 196, 0.08); }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 0.3rem;
}
input[type="text"], input[type="password"], input[type="number"], select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    background: var(--white);
    color: var(--text);
    transition: border-color 0.15s;
}
input:focus, select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(74, 144, 196, 0.15);
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.5rem 1.25rem;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 600;
    text-decoration: none;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
}
.btn-primary { background: var(--primary); color: var(--white); }
.btn-primary:hover { background: var(--primary-light); }
.btn-secondary { background: var(--bg); color: var(--primary); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--error); color: var(--white); }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 0.3rem 0.75rem; font-size: 0.8rem; }

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.875rem;
}
.alert-success { background: #F0FFF4; color: var(--success); border: 1px solid #C6F6D5; }
.alert-error { background: #FFF5F5; color: var(--error); border: 1px solid #FED7D7; }
.alert-warning { background: #FFFFF0; color: #975A16; border: 1px solid #FEFCBF; }

/* Upload dropzone */
.dropzone {
    border: 2px dashed var(--border);
    border-radius: 12px;
    padding: 3rem;
    text-align: center;
    transition: all 0.2s;
    cursor: pointer;
    background: var(--bg);
}
.dropzone:hover, .dropzone.dragover {
    border-color: var(--accent);
    background: rgba(74, 144, 196, 0.04);
}
.dropzone p { color: var(--text-light); margin-bottom: 0.5rem; }
.dropzone .icon { font-size: 2.5rem; margin-bottom: 0.5rem; color: var(--accent); }

/* Filters bar */
.filters {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}
.filters .form-group { margin-bottom: 0; }
.filters .form-group label { white-space: nowrap; }
.filters input, .filters select { font-size: 0.8rem; padding: 0.4rem 0.6rem; }

/* Pagination */
.pagination {
    display: flex;
    gap: 0.25rem;
    justify-content: center;
    margin-top: 1rem;
}
.pagination a, .pagination span {
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    font-size: 0.85rem;
    text-decoration: none;
    border: 1px solid var(--border);
    color: var(--text);
}
.pagination a:hover { background: var(--accent-light); }
.pagination .active { background: var(--primary); color: var(--white); border-color: var(--primary); }

/* Login page */
.login-wrapper {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #2E4A7A 0%, #3B6BA5 50%, #8B9DC3 100%);
}
.login-card {
    background: var(--white);
    border-radius: 16px;
    padding: 2.5rem;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
}
.login-card h1 {
    color: var(--primary);
    font-size: 1.5rem;
    margin-bottom: 0.25rem;
}
.login-card p { color: var(--text-light); font-size: 0.9rem; margin-bottom: 1.5rem; }

/* Chart containers */
.chart-container { position: relative; height: 280px; }

/* AI Chat */
#chatBubble {
    position: fixed; bottom: 24px; right: 24px; z-index: 1000;
    width: 52px; height: 52px; border-radius: 50%;
    background: var(--primary); color: var(--white);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; box-shadow: 0 4px 16px rgba(0,0,0,0.18);
    transition: transform 0.15s, box-shadow 0.15s;
}
#chatBubble:hover { transform: scale(1.08); box-shadow: 0 6px 24px rgba(0,0,0,0.25); }

#chatPanel {
    position: fixed; bottom: 88px; right: 24px; z-index: 1001;
    width: 420px; height: 520px; border-radius: 14px;
    background: var(--white); box-shadow: 0 8px 40px rgba(0,0,0,0.18);
    display: flex; flex-direction: column; overflow: hidden;
    border: 1px solid var(--border);
    min-width: 320px; min-height: 300px; max-width: 90vw; max-height: 85vh;
}
#chatPanel.chat-hidden { display: none; }

.chat-header {
    background: var(--primary); color: var(--white);
    padding: 0.75rem 1rem; display: flex; justify-content: space-between; align-items: center;
    font-weight: 600; font-size: 0.95rem;
}
.chat-messages {
    flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem;
    min-height: 0;
}
.chat-resize {
    position: absolute; top: 0; left: 0; width: 16px; height: 16px; cursor: nw-resize; z-index: 1002;
}
.chat-resize::after {
    content: ''; position: absolute; top: 3px; left: 3px;
    width: 8px; height: 8px; border-top: 2px solid rgba(255,255,255,0.5); border-left: 2px solid rgba(255,255,255,0.5);
}
.chat-msg {
    padding: 0.6rem 0.85rem; border-radius: 12px; font-size: 0.85rem; line-height: 1.5;
    max-width: 92%; word-wrap: break-word;
}
.chat-msg.user {
    background: var(--primary); color: var(--white); align-self: flex-end; border-bottom-right-radius: 4px;
}
.chat-msg.ai {
    background: var(--bg); color: var(--text); align-self: flex-start; border-bottom-left-radius: 4px;
}
.chat-msg.thinking { opacity: 0.7; font-style: italic; }
.dot-pulse { display: inline-block; animation: dotPulse 1.2s infinite; }
@keyframes dotPulse { 0%,80%,100%{ opacity:0.3; } 40%{ opacity:1; } }

.chat-input-wrap {
    display: flex; padding: 0.75rem; border-top: 1px solid var(--border); gap: 0.5rem;
}
.chat-input-wrap input {
    flex: 1; padding: 0.5rem 0.75rem; border: 1px solid var(--border); border-radius: 8px;
    font-size: 0.85rem; outline: none;
}
.chat-input-wrap input:focus { border-color: var(--accent); }
.chat-input-wrap button {
    padding: 0.5rem 1rem; background: var(--primary); color: var(--white);
    border: none; border-radius: 8px; font-size: 0.85rem; font-weight: 600; cursor: pointer;
}
.chat-input-wrap button:disabled { opacity: 0.5; cursor: not-allowed; }

.chat-sql-toggle {
    color: var(--accent); font-size: 0.75rem; cursor: pointer; margin-top: 0.4rem;
    text-decoration: underline;
}
.chat-sql {
    background: #1a1a2e; color: #a8d8a8; padding: 0.5rem; border-radius: 6px;
    font-size: 0.75rem; margin-top: 0.3rem; overflow-x: auto; white-space: pre-wrap;
}
.chat-table { width: 100%; border-collapse: collapse; font-size: 0.78rem; margin: 0.5rem 0; }
.chat-table th { background: var(--border); padding: 0.3rem 0.5rem; text-align: left; font-weight: 600; }
.chat-table td { padding: 0.3rem 0.5rem; border-bottom: 1px solid var(--border); }

/* Responsive */
@media (max-width: 768px) {
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
    nav { padding: 0 1rem; }
    .container { padding: 1rem; }
    #chatPanel { width: calc(100vw - 32px); right: 16px; bottom: 80px; }
}
