/* SIA — Sales Intelligence Analyst UI */

:root {
    --bg-primary: #FAFAF8;
    --bg-chat: #F4F2EE;
    --bg-secondary: #F7F6F3;
    --bg-tertiary: #EFEEE9;
    --bg-card: #FFFFFF;
    --text-primary: #1A1A1A;
    --text-secondary: #6B6B6B;
    --text-muted: #A3A09A;
    --accent: #4A7FE5;
    --accent-hover: #3A6FD5;
    --accent-glow: rgba(74, 127, 229, 0.08);
    --red: #DC4A3D;
    --orange: #E88D2A;
    --green: #3DA06B;
    --border: #E8E5DF;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.06);
    --font: 'Inter', -apple-system, sans-serif;
}

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

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

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ── Top Bar ─────────────────────────────── */

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    height: 52px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 20px; font-weight: 700; }
.subtitle { font-size: 13px; color: var(--text-secondary); font-weight: 400; }

.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-info { display: flex; align-items: center; gap: 8px; }
.user-email { font-size: 13px; color: var(--text-secondary); font-weight: 500; }
.admin-link { font-size: 16px; text-decoration: none; opacity: 0.7; transition: opacity 0.15s; }
.admin-link:hover { opacity: 1; }
.logout-btn { font-size: 14px !important; opacity: 0.6; }
.logout-btn:hover { opacity: 1; }
.status-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--green);
    box-shadow: 0 0 6px var(--green);
}
.status-dot.error { background: var(--red); box-shadow: 0 0 6px var(--red); }

.btn-icon {
    background: none; border: none; color: var(--text-secondary);
    font-size: 18px; cursor: pointer; padding: 4px 8px;
    border-radius: var(--radius-sm);
    transition: all 0.15s;
}
.btn-icon:hover { background: var(--bg-tertiary); color: var(--text-primary); }

/* ── Main Content ────────────────────────── */

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.panel { display: flex; flex-direction: column; overflow: hidden; }

.chat-panel {
    width: 33.3%;
    min-width: 320px;
    border-right: 1px solid var(--border);
    background: var(--bg-chat);
}

.dashboard-panel {
    flex: 1;
    min-width: 400px;
    background: var(--bg-primary);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}
.panel-header h2 { font-size: 15px; font-weight: 600; }
.data-period { font-size: 11px; color: var(--text-muted); margin-left: 12px; }

.btn-small {
    font-size: 12px; padding: 4px 12px;
    background: var(--accent); color: white;
    border: none; border-radius: var(--radius-sm);
    cursor: pointer; font-weight: 500;
    transition: background 0.15s;
}
.btn-small:hover { background: var(--accent-hover); }

/* ── Chat Messages ───────────────────────── */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.welcome-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    text-align: center;
    box-shadow: var(--shadow);
}
.welcome-card h3 { margin-bottom: 8px; font-size: 18px; }
.welcome-card p { color: var(--text-secondary); font-size: 14px; margin-bottom: 16px; }

.example-queries { display: flex; flex-direction: column; gap: 8px; }
.example-btn {
    background: var(--bg-chat);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    text-align: left;
    transition: all 0.15s;
    font-family: var(--font);
}
.example-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-glow);
}

.msg { max-width: 90%; padding: 12px 16px; border-radius: var(--radius); font-size: 14px; line-height: 1.6; flex-shrink: 0; }
.msg-user {
    align-self: flex-end;
    background: var(--accent);
    color: white;
    border-bottom-right-radius: 4px;
}
.msg-ai {
    align-self: flex-start;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow);
    color: var(--text-primary);
}
.msg-ai pre {
    background: var(--bg-tertiary);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    font-size: 12px;
    margin: 8px 0;
}
.msg-ai table {
    border-collapse: collapse;
    width: 100%;
    font-size: 12px;
    margin: 8px 0;
}
.msg-ai th, .msg-ai td {
    border: 1px solid var(--border);
    padding: 4px 8px;
    text-align: left;
}
.msg-ai th { background: var(--bg-tertiary); font-weight: 600; color: var(--text-secondary); }
.msg-ai ul, .msg-ai ol {
    padding-left: 1.5em;
    margin: 4px 0;
}
.msg-ai li { margin: 2px 0; }

/* ── Status Log (stacked processing steps) ─── */

.status-log {
    align-self: flex-start;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 8px 0;
    font-size: 12px;
    color: var(--text-muted);
    transition: opacity 1s ease;
    max-width: 100%;
    overflow: hidden;
    flex-shrink: 0;
}
.status-log.fade { opacity: 0.6; }

.status-step {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 2px 0;
    transition: opacity 0.3s;
}
.status-step-active { color: var(--text-secondary); }
.status-step-done { color: var(--text-muted); }
.status-check {
    color: var(--green);
    font-size: 12px;
    font-weight: 600;
    width: 14px;
    text-align: center;
    flex-shrink: 0;
}

.spinner {
    width: 14px; height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.thinking-block {
    padding: 2px 0 2px 20px;
    font-size: 11px;
    color: var(--text-muted);
    font-style: italic;
    line-height: 1.4;
    overflow: hidden;
    cursor: pointer;
    white-space: pre-wrap;
    word-break: break-word;
    max-width: 100%;
}
.thinking-block.collapsed {
    max-height: 1.4em;
    white-space: nowrap;
    text-overflow: ellipsis;
}

/* ── Guard Badge ─────────────────────────── */

.guard-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 500;
    width: fit-content;
    transition: opacity 0.5s ease;
}
.guard-badge.fade { opacity: 0.4; }

.guard-reviewing {
    color: var(--accent);
    background: var(--accent-glow);
    border: 1px solid rgba(74, 127, 229, 0.2);
}
.guard-approved {
    color: var(--green);
    background: rgba(61, 160, 107, 0.08);
    border: 1px solid rgba(61, 160, 107, 0.2);
}
.guard-rejected {
    color: var(--orange);
    background: rgba(232, 141, 42, 0.08);
    border: 1px solid rgba(232, 141, 42, 0.2);
}

.spinner-small {
    width: 10px; height: 10px;
    border: 1.5px solid rgba(74, 127, 229, 0.3);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    display: inline-block;
}

/* ── Chat Input ──────────────────────────── */

.chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
}

.upload-zone {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.upload-btn { font-size: 20px; }
.upload-name { font-size: 12px; color: var(--text-muted); }

.input-row { display: flex; gap: 8px; align-items: flex-end; }
#chatInput {
    flex: 1;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    padding: 10px 14px;
    font-size: 14px;
    font-family: var(--font);
    resize: none;
    max-height: 120px;
    outline: none;
    transition: border-color 0.15s;
}
#chatInput:focus { border-color: var(--accent); }

.btn-send {
    background: var(--accent);
    border: none;
    color: white;
    width: 40px; height: 40px;
    border-radius: var(--radius-sm);
    font-size: 18px;
    cursor: pointer;
    transition: background 0.15s;
    flex-shrink: 0;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Resize Handle ───────────────────────── */

.resize-handle {
    width: 5px;
    cursor: col-resize;
    background: var(--border);
    transition: background 0.15s;
    flex-shrink: 0;
}
.resize-handle:hover, .resize-handle.active { background: var(--accent); }

/* ── Dashboard ───────────────────────────── */

.dashboard-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.kpi-bar {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}
.kpi-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    box-shadow: var(--shadow);
}
.kpi-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 700; color: var(--text-primary); }

.widget-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    margin-bottom: 20px;
}

.widget {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}
.widget-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}
.widget-header h3 { font-size: 13px; font-weight: 600; flex: 1; }
.widget-title-group { flex: 1; }
.widget-title-group h3 { flex: none; }
.widget-subtitle { display: block; font-size: 10px; color: var(--text-muted); font-weight: 400; margin-top: 2px; }
.widget-icon { font-size: 14px; }
.btn-refresh {
    background: none; border: none;
    color: var(--text-muted); font-size: 14px;
    cursor: pointer; padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.15s;
}
.btn-refresh:hover { color: var(--accent); background: var(--accent-glow); }

.btn-research {
    background: none; border: none;
    color: var(--text-muted); font-size: 14px;
    cursor: pointer; padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.15s;
    opacity: 0.6;
}
.btn-research:hover { color: var(--accent); background: var(--accent-glow); opacity: 1; }
.btn-research.done { opacity: 0.9; }
.btn-research.done:hover { color: #60a5fa; }

/* Research spinner */
.research-spinner {
    display: inline-block;
    width: 14px; height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
    margin-left: 4px;
}

/* Research report modal */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex; align-items: center; justify-content: center;
    z-index: 2000;
    opacity: 0;
    transition: opacity 0.2s;
}
.modal-overlay.show { opacity: 1; }
.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    width: min(800px, 90vw);
    max-height: 80vh;
    display: flex; flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 600; margin: 0; }
.modal-close {
    background: none; border: none; color: var(--text-muted);
    font-size: 24px; cursor: pointer; padding: 0 4px;
    line-height: 1;
}
.modal-close:hover { color: var(--text-primary); }
.modal-body {
    padding: 20px;
    overflow-y: auto;
    font-size: 14px;
    line-height: 1.6;
}
.modal-body h1, .modal-body h2, .modal-body h3 { margin-top: 16px; margin-bottom: 8px; }
.modal-body blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    margin: 8px 0;
    color: var(--text-secondary);
}

/* Toast notification */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-card);
    color: var(--text-primary);
    padding: 10px 24px;
    border-radius: 8px;
    border: 1px solid var(--border);
    font-size: 13px;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.widget-body {
    padding: 12px 16px;
    min-height: 60px;
    max-height: clamp(180px, calc(24vw - 44px), 280px);
    overflow-y: auto;
    font-size: 13px;
}
.widget-empty { color: var(--text-muted); font-style: italic; text-align: center; padding: 16px; }

.widget-body table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}
.widget-body th, .widget-body td {
    border: 1px solid var(--border);
    padding: 6px 8px;
    text-align: left;
}
.widget-body th { background: var(--bg-secondary); font-weight: 600; color: var(--text-secondary); }

.pinned-section h3 { font-size: 14px; margin-bottom: 12px; color: var(--text-secondary); }

/* ── Session Sidebar ─────────────────────── */

.sidebar {
    position: fixed;
    right: -400px;
    top: 0;
    width: 360px;
    height: 100vh;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 24px rgba(0,0,0,0.08);
    z-index: 100;
    transition: right 0.25s ease;
    display: flex;
    flex-direction: column;
}
.sidebar.open { right: 0; }

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}
.sidebar-header h3 { font-size: 15px; font-weight: 600; }

.sidebar-body {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.session-item {
    padding: 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    margin-bottom: 4px;
    transition: background 0.15s;
}
.session-item:hover { background: var(--bg-tertiary); }
.session-title { font-size: 14px; font-weight: 500; margin-bottom: 4px; }
.session-meta { font-size: 11px; color: var(--text-muted); }

/* ── Scrollbar ───────────────────────────── */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Pin Bar (inline pin button after SQL results) ── */

.pin-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}
.pin-label { font-size: 0.82rem; color: var(--text-muted); }
.btn-pin {
    padding: 4px 12px;
    font-size: 0.8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s;
}
.btn-pin:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
/* Copy button */
.copy-bar { margin-top: 8px; display: flex; align-items: center; gap: 8px; }
.btn-copy {
    background: transparent; border: 1px solid var(--border); border-radius: 6px;
    padding: 4px 10px; font-size: 12px; color: var(--text-secondary);
    cursor: pointer; display: inline-flex; align-items: center; gap: 4px;
    transition: all 0.2s;
}
.btn-copy:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-copy.copied { background: #22c55e; color: #fff; border-color: #22c55e; pointer-events: none; }


/* ── Responsive ──────────────────────────── */

@media (max-width: 768px) {
    .main-content { flex-direction: column; }
    .chat-panel { width: 100%; min-width: unset; height: 50%; border-right: none; border-bottom: 1px solid var(--border); }
    .dashboard-panel { min-width: unset; }
    .resize-handle { display: none; }
    .kpi-bar { grid-template-columns: repeat(2, 1fr); }
    .widget-grid { grid-template-columns: 1fr; }
}
