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

:root {
    --bg: #f5f5f5; --surface: #fff; --sidebar-bg: #1e293b; --sidebar-text: #cbd5e1;
    --primary: #3b82f6; --primary-hover: #2563eb; --danger: #ef4444;
    --text: #1e293b; --text-muted: #64748b; --border: #e2e8f0;
    --score-good: #22c55e; --score-mid: #eab308; --score-low: #ef4444;
    --radius: 8px; --shadow: 0 1px 3px rgba(0,0,0,.1);
    --sidebar-width: 280px;
}

body { font-family: system-ui, -apple-system, sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }

/* Chat layout */
.chat-layout { display: flex; height: 100vh; position: relative; }

/* Sidebar — hidden off-screen on mobile */
.sidebar {
    width: var(--sidebar-width); background: var(--sidebar-bg); color: var(--sidebar-text);
    display: flex; flex-direction: column;
    position: fixed; top: 0; left: 0; height: 100vh; z-index: 100;
    transform: translateX(-100%); transition: transform 0.2s ease;
}
.sidebar.open { transform: translateX(0); }

/* Overlay behind sidebar on mobile */
.sidebar-overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 99;
}
.sidebar-overlay.open { display: block; }

.sidebar-header { padding: 16px; border-bottom: 1px solid rgba(255,255,255,.1); display: flex; justify-content: space-between; align-items: center; }
.sidebar-header h2 { font-size: 16px; color: #fff; }
.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item { padding: 10px 12px; border-radius: var(--radius); cursor: pointer; margin-bottom: 4px; font-size: 14px; display: flex; justify-content: space-between; align-items: center; }
.session-item:hover, .session-item.active { background: rgba(255,255,255,.1); }
.session-info { display: flex; flex-direction: column; overflow: hidden; flex: 1; min-width: 0; }
.session-name { font-size: 13px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-date { font-size: 11px; color: var(--sidebar-text); opacity: 0.6; margin-top: 2px; }
.session-item .delete-btn { opacity: 0; background: none; border: none; color: var(--danger); cursor: pointer; font-size: 16px; flex-shrink: 0; }
.session-item:hover .delete-btn { opacity: 1; }

.chat-main { flex: 1; display: flex; flex-direction: column; width: 100%; }
.chat-header { padding: 16px 24px; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; }
.chat-header h1 { font-size: 20px; }
.header-nav { margin-left: auto; display: flex; align-items: center; gap: 16px; }
.header-nav a { font-size: 14px; }
.logout-form { display: inline; }
.btn-logout { background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 14px; }
.btn-logout:hover { color: var(--danger); }
.messages { flex: 1; overflow-y: auto; padding: 24px; }
.message { margin-bottom: 16px; }
.message.user { margin-left: auto; max-width: 85%; }
.message.assistant { max-width: 100%; }
.message-bubble { padding: 12px 16px; border-radius: var(--radius); line-height: 1.5; font-size: 14px; overflow-wrap: break-word; }
.message.user .message-bubble { background: var(--primary); color: #fff; white-space: pre-wrap; }
.message.assistant .message-bubble { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); }
.message.assistant .message-bubble p { margin-bottom: 8px; }
.message.assistant .message-bubble p:last-child { margin-bottom: 0; }
.message.assistant .message-bubble ul,
.message.assistant .message-bubble ol { padding-left: 20px; margin-bottom: 8px; }
.message.assistant .message-bubble li { margin-bottom: 4px; }
.message.assistant .message-bubble code {
    background: rgba(0,0,0,.06); padding: 2px 4px; border-radius: 3px; font-size: 13px;
}
.message.assistant .message-bubble pre {
    background: #1e293b; color: #e2e8f0; padding: 12px; border-radius: var(--radius);
    overflow-x: auto; margin-bottom: 8px; font-size: 13px;
}
.message.assistant .message-bubble pre code { background: none; padding: 0; color: inherit; }
.message.assistant .message-bubble h1,
.message.assistant .message-bubble h2,
.message.assistant .message-bubble h3 { margin: 12px 0 6px; font-size: 15px; font-weight: 600; }
.message.assistant .message-bubble strong { font-weight: 600; }
.message.assistant .message-bubble a { color: var(--primary); text-decoration: underline; }
.message-sources { margin-top: 8px; font-size: 12px; color: var(--text-muted); }
.message-sources span { background: var(--bg); padding: 2px 8px; border-radius: 4px; margin-right: 4px; display: inline-block; margin-bottom: 4px; }
.message-sources .source-tag { cursor: pointer; transition: background 0.15s ease, color 0.15s ease; }
.message-sources .source-tag:hover { background: var(--border); }
.message-sources .source-tag.highlighted { background: var(--primary); color: #fff; }

/* Citation links */
.citation-link {
    color: var(--primary); font-weight: 600; text-decoration: none;
    border-bottom: 1px dashed var(--primary); cursor: pointer; font-size: 13px;
}
.citation-link:hover { color: var(--primary-hover); border-bottom-style: solid; }

/* Source detail popover */
.source-detail-popover {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 4px 24px rgba(0,0,0,.15); padding: 16px; z-index: 200;
    max-width: 480px; width: 90%;
}
.popover-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.popover-header strong { font-size: 15px; }
.popover-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); padding: 0 4px; }
.popover-close:hover { color: var(--text); }
.popover-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.meta-tag { background: var(--bg); padding: 2px 8px; border-radius: 4px; font-size: 12px; color: var(--text-muted); }
.popover-snippet {
    background: var(--bg); padding: 10px; border-radius: var(--radius);
    font-size: 13px; line-height: 1.5; color: var(--text); max-height: 200px; overflow-y: auto;
}
.chat-input { padding: 16px 24px; background: var(--surface); border-top: 1px solid var(--border); }
.chat-input form { display: flex; gap: 8px; }
.chat-input input { flex: 1; padding: 10px 16px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 16px; }
.chat-input input:focus { outline: none; border-color: var(--primary); }
.empty-state { text-align: center; color: var(--text-muted); padding: 48px 16px; font-size: 15px; }

/* Sidebar toggle (hamburger) — visible on mobile */
.sidebar-toggle { display: block; font-size: 22px; flex-shrink: 0; }

/* Buttons */
.btn { padding: 8px 16px; border-radius: var(--radius); border: none; cursor: pointer; font-size: 14px; font-weight: 500; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-icon { background: none; border: none; cursor: pointer; padding: 4px 8px; border-radius: 4px; color: var(--sidebar-text); }
.btn-icon:hover { background: rgba(255,255,255,.15); }

/* Dashboard */
.dashboard { max-width: 1024px; margin: 0 auto; padding: 24px 16px; }
.dashboard h1 { font-size: 24px; }
.dashboard h2 { margin-bottom: 16px; }
.dashboard-header { display: flex; align-items: center; gap: 16px; margin-bottom: 24px; }
.metrics-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 32px; }
.metric-card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); text-align: center; }
.metric-card .value { font-size: 24px; font-weight: 700; }
.metric-card .label { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.score-bar { height: 6px; background: var(--border); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.score-bar-fill { height: 100%; border-radius: 3px; transition: width .3s; }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
th, td { padding: 12px 16px; text-align: left; font-size: 14px; white-space: nowrap; }
th { background: var(--bg); font-weight: 600; color: var(--text-muted); font-size: 12px; text-transform: uppercase; }
tr:not(:last-child) td { border-bottom: 1px solid var(--border); }
.badge { padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 500; }
.badge-completed { background: #dcfce7; color: #166534; }
.badge-pending { background: #fef9c3; color: #854d0e; }
.badge-failed { background: #fee2e2; color: #991b1b; }

/* Login page */
.login-container {
    display: flex; justify-content: center; align-items: center;
    min-height: 100vh; padding: 20px;
    background: linear-gradient(135deg, #f0f4ff 0%, var(--bg) 50%, #f0fdf4 100%);
}
.login-card {
    max-width: 420px; width: 100%; background: var(--surface);
    border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,.08);
    padding: 40px 32px;
}
.login-brand {
    text-align: center; margin-bottom: 8px;
}
.login-brand-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 56px; height: 56px; border-radius: 14px;
    background: linear-gradient(135deg, var(--primary) 0%, #6366f1 100%);
    color: #fff; font-size: 24px; font-weight: 700;
    box-shadow: 0 2px 12px rgba(59,130,246,.3);
    margin-bottom: 4px;
}
.login-title {
    text-align: center; font-size: 22px; font-weight: 700;
    color: var(--text); margin-bottom: 6px;
}
.login-subtitle {
    text-align: center; font-size: 14px; color: var(--text-muted);
    margin-bottom: 24px; line-height: 1.5;
}
.login-alert {
    padding: 12px 14px; border-radius: 8px; margin-bottom: 20px;
    font-size: 14px; line-height: 1.4;
    border-left: 4px solid transparent;
}
.login-alert-error {
    background: #fef2f2; color: #dc2626; border-left-color: #dc2626;
}
.login-alert-success {
    background: #f0fdf4; color: #16a34a; border-left-color: #16a34a;
}
.login-label {
    display: block; font-size: 14px; font-weight: 500;
    color: var(--text); margin-bottom: 6px;
}
.login-input {
    width: 100%; padding: 10px 14px; font-size: 15px;
    border: 1.5px solid var(--border); border-radius: 8px;
    background: var(--surface); color: var(--text);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.login-input:focus {
    outline: none; border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}
.login-input::placeholder { color: #94a3b8; }
.login-field { margin-bottom: 20px; }
.login-btn {
    width: 100%; padding: 12px; font-size: 15px; font-weight: 600;
    border: none; border-radius: 8px; cursor: pointer;
    background: linear-gradient(135deg, var(--primary) 0%, #6366f1 100%);
    color: #fff; transition: transform 0.1s ease, box-shadow 0.15s ease;
    box-shadow: 0 2px 8px rgba(59,130,246,.25);
}
.login-btn:hover {
    box-shadow: 0 4px 16px rgba(59,130,246,.35);
    transform: translateY(-1px);
}
.login-btn:active { transform: translateY(0); }

/* Verify page */
.verify-text { font-size: 16px; color: var(--text-muted); margin-bottom: 12px; }
.verify-spinner {
    width: 32px; height: 32px; border: 3px solid var(--border);
    border-top-color: var(--primary); border-radius: 50%;
    animation: spin 0.8s linear infinite; margin: 0 auto 16px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (hover: none) {
    .session-item .delete-btn { opacity: 0.7; }
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.chat-input input:disabled { opacity: 0.6; }
.loading { text-align: center; padding: 24px; color: var(--text-muted); }
.typing-indicator::after { content: ""; animation: dots 1.5s infinite; display: inline; }
@keyframes dots { 0%,20% { content: "."; } 40% { content: ".."; } 60%,100% { content: "..."; } }

/* Desktop breakpoint */
@media (min-width: 768px) {
    .sidebar {
        position: static; transform: none; transition: none;
    }
    .sidebar-overlay { display: none !important; }
    .sidebar-toggle { display: none; }
    .chat-input form { max-width: 720px; }
    .message { max-width: 720px; }
    .message.user { max-width: 720px; }
    .metrics-grid { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
    .metric-card { padding: 20px; }
    .metric-card .value { font-size: 28px; }
    .dashboard { padding: 32px 24px; }
    th, td { white-space: normal; }
}
