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

:root {
    --bg: #f8f9fb;
    --surface: #ffffff;
    --border: #e2e5ea;
    --text: #1a1d23;
    --text-muted: #6b7280;
    --primary: #0C385E;
    --primary-light: #E8EDF2;
    --accent: #F26E21;
    --danger: #EF4444;
    --warning: #F59E0B;
    --chat-bg: #ffffff;
    --msg-user-bg: #0C385E;
    --msg-user-text: #ffffff;
    --msg-assistant-bg: #f3f4f6;
    --msg-assistant-text: #1a1d23;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,.08);
    --shadow-lg: 0 4px 12px rgba(0,0,0,.12);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    --mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
}

html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); }

/* ===== App Layout ===== */
.app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ===== Top Bar ===== */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 20px; height: 56px; background: var(--surface);
    border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-logo { height: 32px; width: auto; }
.topbar h1 { font-size: 18px; font-weight: 700; color: var(--text); }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.topbar select {
    padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-size: 13px; background: var(--surface); color: var(--text); cursor: pointer;
}
.btn-icon {
    width: 36px; height: 36px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); cursor: pointer; display: flex; align-items: center; justify-content: center;
    transition: background .15s;
}
.btn-icon:hover { background: var(--primary-light); }
.btn-icon svg { width: 18px; height: 18px; color: var(--text-muted); }
.btn-icon.syncing svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Main Area ===== */
.main-area {
    display: flex; flex: 1; overflow: hidden;
}

/* ===== Chat Panel ===== */
.chat-panel {
    width: 45%; min-width: 380px; display: flex; flex-direction: column;
    border-right: 1px solid var(--border); background: var(--chat-bg);
}
.chat-messages {
    flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 3px; }

/* Messages */
.msg { display: flex; max-width: 85%; }
.msg.user { align-self: flex-end; }
.msg.assistant { align-self: flex-start; }

.msg-content {
    padding: 12px 16px; border-radius: var(--radius); font-size: 14px; line-height: 1.6;
    word-break: break-word;
}
.msg.user .msg-content {
    background: var(--msg-user-bg); color: var(--msg-user-text);
    border-bottom-right-radius: 4px;
}
.msg.assistant .msg-content {
    background: var(--msg-assistant-bg); color: var(--msg-assistant-text);
    border-bottom-left-radius: 4px;
}
.msg-content p { margin-bottom: 8px; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content ul { padding-left: 18px; margin-bottom: 8px; }
.msg-content strong { font-weight: 600; }

/* Suggestion list */
.suggestion-list { list-style: none; padding: 0; margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.suggestion-list li {
    padding: 6px 12px; background: var(--primary-light); color: var(--primary);
    border-radius: 20px; font-size: 13px; cursor: pointer; font-weight: 500;
    transition: background .15s, transform .1s;
}
.suggestion-list li:hover { background: var(--accent); color: #fff; transform: translateY(-1px); }

/* Typing indicator */
.typing { display: flex; gap: 4px; padding: 8px 16px; }
.typing span {
    width: 8px; height: 8px; background: #9ca3af; border-radius: 50%;
    animation: bounce 1.4s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-8px); }
}

/* Chat input */
.chat-input-area {
    display: flex; align-items: flex-end; gap: 8px;
    padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface);
}
#chat-input {
    flex: 1; resize: none; border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 10px 14px; font-size: 14px; font-family: var(--font); line-height: 1.5;
    max-height: 120px; outline: none; transition: border-color .15s;
}
#chat-input:focus { border-color: var(--accent); }
#btn-send {
    width: 40px; height: 40px; border: none; border-radius: var(--radius-sm);
    background: var(--accent); color: #fff; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background .15s, transform .1s; flex-shrink: 0;
}
#btn-send:hover { background: #D95E18; transform: scale(1.05); }
#btn-send:disabled { background: #F2A96E; cursor: not-allowed; transform: none; }
#btn-send svg { width: 18px; height: 18px; }

/* ===== Viz Panel ===== */
.viz-panel {
    flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--bg);
}
.viz-placeholder {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    color: var(--text-muted); gap: 12px;
}
.viz-icon { width: 64px; height: 64px; opacity: .3; }
.viz-placeholder p { font-size: 14px; }

.viz-content { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.viz-header { display: flex; align-items: center; justify-content: space-between; }
.viz-header h2 { font-size: 16px; font-weight: 600; }
.viz-actions { display: flex; gap: 6px; }
.btn-sm {
    padding: 5px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); font-size: 12px; font-weight: 600; cursor: pointer;
    color: var(--text-muted); transition: all .15s;
}
.btn-sm:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-export-dashboard {
    background: var(--primary); color: #fff; border-color: var(--primary);
    display: inline-flex; align-items: center;
}
.btn-export-dashboard:hover { background: var(--accent); border-color: var(--accent); }
.btn-export-dashboard:disabled { opacity: 0.6; cursor: not-allowed; }

/* Chart */
.chart-container {
    background: var(--surface); border-radius: var(--radius); padding: 20px;
    box-shadow: var(--shadow); max-height: 400px; position: relative;
}

/* Table */
.table-container {
    background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
    overflow-x: auto;
}
#data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
#data-table th {
    text-align: left; padding: 10px 14px; background: #f9fafb; border-bottom: 2px solid var(--border);
    font-weight: 600; color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px;
    white-space: nowrap;
}
#data-table td {
    padding: 10px 14px; border-bottom: 1px solid #f3f4f6;
}
#data-table tbody tr:hover { background: #fafbfc; }
#data-table tfoot td {
    padding: 10px 14px; font-weight: 700; background: #f9fafb;
    border-top: 2px solid var(--border);
}
.cell-currency { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12px; }
.cell-number { text-align: right; font-variant-numeric: tabular-nums; }
.cell-percentage { text-align: right; }

/* ===== Dashboard Grid ===== */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    width: 100%;
}
.dashboard-widget {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    min-height: 0;
    overflow: hidden;
    transition: box-shadow .15s;
}
.dashboard-widget:hover { box-shadow: var(--shadow-lg); }
.dashboard-widget.widget-span-2 { grid-column: span 2; }
.dashboard-widget-title {
    font-size: 11px; font-weight: 600; text-transform: uppercase;
    letter-spacing: .5px; color: var(--text-muted); margin-bottom: 12px;
}

/* KPI Card */
.kpi-card { display: flex; flex-direction: column; gap: 4px; }
.kpi-value {
    font-size: 32px; font-weight: 700; line-height: 1.1;
    font-variant-numeric: tabular-nums;
}
.kpi-label { font-size: 13px; color: var(--text-muted); }
.kpi-trend { font-size: 13px; font-weight: 600; margin-top: 4px; }
.kpi-trend.trend-up { color: #10B981; }
.kpi-trend.trend-down { color: var(--danger); }
.kpi-trend.trend-flat { color: var(--text-muted); }

/* Dashboard chart widget */
.dashboard-widget .widget-chart-container { position: relative; height: 220px; }

/* Dashboard table widget */
.dashboard-widget .widget-table-container { overflow-x: auto; }
.dashboard-widget .widget-table-container table {
    width: 100%; border-collapse: collapse; font-size: 13px;
}
.dashboard-widget .widget-table-container th {
    text-align: left; padding: 8px 12px; background: #f9fafb;
    border-bottom: 2px solid var(--border); font-weight: 600;
    color: var(--text-muted); font-size: 11px; text-transform: uppercase;
    letter-spacing: .5px; white-space: nowrap;
}
.dashboard-widget .widget-table-container td {
    padding: 8px 12px; border-bottom: 1px solid #f3f4f6;
}
.dashboard-widget .widget-table-container tbody tr:hover { background: #fafbfc; }
.dashboard-widget .widget-table-container tfoot td {
    padding: 8px 12px; font-weight: 700; background: #f9fafb;
    border-top: 2px solid var(--border);
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
    .main-area { flex-direction: column; }
    .chat-panel { width: 100%; min-width: 0; border-right: none; border-bottom: 1px solid var(--border); height: 50%; }
    .viz-panel { height: 50%; }
    .dashboard-grid { grid-template-columns: 1fr; }
    .dashboard-widget.widget-span-2 { grid-column: span 1; }
}

/* ===== Login Overlay ===== */
.login-overlay {
    position: fixed; inset: 0; z-index: 2000;
    display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #0C385E 0%, #F26E21 100%);
}
.login-card {
    background: var(--surface); border-radius: 16px; padding: 40px;
    width: 420px; max-width: 90vw; box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.login-header {
    text-align: center; margin-bottom: 32px;
}
.login-logo {
    height: 48px; width: auto; margin-bottom: 12px;
}
.login-header h1 {
    font-size: 24px; font-weight: 700; color: var(--text); margin-bottom: 4px;
}
.login-subtitle {
    font-size: 14px; color: var(--text-muted);
}
.login-form {
    display: flex; flex-direction: column; gap: 12px;
}
.login-form label {
    font-size: 13px; font-weight: 600; color: var(--text-muted);
}
.login-form input {
    padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-size: 14px; font-family: var(--mono); outline: none; transition: border-color .15s;
}
.login-form input:focus {
    border-color: var(--accent); box-shadow: 0 0 0 3px rgba(242,110,33,.1);
}
.btn-login {
    padding: 12px; border: none; border-radius: var(--radius-sm);
    background: var(--accent); color: #fff; font-size: 15px; font-weight: 600;
    cursor: pointer; transition: background .15s, transform .1s;
    display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-login:hover { background: #D95E18; transform: translateY(-1px); }
.btn-login:disabled { background: #F2A96E; cursor: not-allowed; transform: none; }
.login-spinner {
    width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.3);
    border-top-color: #fff; border-radius: 50%;
    animation: spin .6s linear infinite;
}
.login-error {
    padding: 10px 14px; background: #fef2f2; color: #dc2626; border-radius: var(--radius-sm);
    font-size: 13px; border: 1px solid #fecaca;
}
.login-footer {
    margin-top: 24px; text-align: center;
}
.login-footer p {
    font-size: 12px; color: var(--text-muted); line-height: 1.5;
}

/* ===== Agent Mode Toggle ===== */
.agent-toggle {
    display: flex; align-items: center; gap: 6px; cursor: pointer;
    padding: 4px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    transition: border-color .15s;
}
.agent-toggle:hover { border-color: var(--accent); }
.agent-toggle input { display: none; }
.agent-toggle-slider {
    width: 32px; height: 18px; background: #d1d5db; border-radius: 9px;
    position: relative; transition: background .2s; flex-shrink: 0;
}
.agent-toggle-slider::after {
    content: ""; position: absolute; top: 2px; left: 2px;
    width: 14px; height: 14px; background: #fff; border-radius: 50%;
    transition: transform .2s;
}
.agent-toggle input:checked + .agent-toggle-slider {
    background: var(--accent);
}
.agent-toggle input:checked + .agent-toggle-slider::after {
    transform: translateX(14px);
}
.agent-toggle-label {
    font-size: 12px; font-weight: 600; color: var(--text-muted);
    transition: color .15s; user-select: none;
}
.agent-toggle input:checked ~ .agent-toggle-label {
    color: var(--accent);
}

/* ===== User Name ===== */
.user-name {
    font-size: 13px; font-weight: 600; color: var(--primary);
    padding: 4px 10px; background: var(--primary-light); border-radius: 16px;
}

/* ===== Toast ===== */
.toast {
    position: fixed; bottom: 20px; right: 20px; padding: 10px 20px;
    background: #0C385E; color: #fff; border-radius: var(--radius-sm);
    font-size: 13px; z-index: 1000; box-shadow: var(--shadow-lg);
    animation: slideUp .3s ease;
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
