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

:root {
    --sidebar-width: 280px;
    --sidebar-bg: #1e1e2e;
    --sidebar-hover: #2a2a3c;
    --sidebar-active: #363650;
    --text-primary: #e4e4e7;
    --text-secondary: #a1a1aa;
    --bg-primary: #fafafa;
    --bg-chat: #ffffff;
    --bubble-user: #3b82f6;
    --bubble-assistant: #f4f4f5;
    --border-color: #e4e4e7;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --danger: #ef4444;
    --radius: 12px;
    --mobile-topbar-h: 48px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
    background: var(--bg-primary);
    color: #18181b;
    -webkit-tap-highlight-color: transparent;
}

#app { display: flex; height: 100vh; overflow: hidden; }

/* ── Login Overlay ─────────────────────────────────────── */
.login-overlay {
    position: fixed;
    inset: 0;
    background: #f0f0f5;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}
.login-card {
    background: #fff;
    border-radius: 16px;
    padding: 40px 36px;
    width: 380px;
    max-width: 90vw;
    box-shadow: 0 4px 24px rgba(0,0,0,0.10);
    text-align: center;
}
.login-logo { font-size: 48px; margin-bottom: 8px; }
.login-card h2 { font-size: 20px; margin-bottom: 4px; color: #18181b; }
.login-subtitle { font-size: 13px; color: #71717a; margin-bottom: 28px; }

.form-group { text-align: left; margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; color: #3f3f46; margin-bottom: 6px; }
.form-group input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #d4d4d8;
    border-radius: 8px;
    font-size: 15px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
    box-sizing: border-box;
}
.form-group input:focus { border-color: var(--accent); }

.login-error {
    color: var(--danger);
    font-size: 13px;
    margin-bottom: 16px;
    padding: 8px 12px;
    background: #fef2f2;
    border-radius: 6px;
    text-align: center;
}

.btn-login {
    width: 100%;
    padding: 11px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    font-family: inherit;
    font-weight: 500;
    transition: background 0.15s;
}
.btn-login:hover { background: var(--accent-hover); }
.btn-login:disabled { opacity: 0.6; cursor: not-allowed; }

/* ── Sidebar footer ────────────────────────────────────── */
.sidebar-footer {
    padding: 10px 12px;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.user-email {
    font-size: 12px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}
.btn-logout {
    padding: 6px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    transition: color 0.12s;
}
.btn-logout:hover { color: var(--danger); }

/* ── Sidebar ─────────────────────────────────────────── */
#sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 100;
}

.sidebar-header {
    padding: 14px 12px;
    display: flex;
    gap: 8px;
    align-items: center;
}
.btn-new-chat {
    flex: 1;
    padding: 10px 16px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s;
}
.btn-new-chat:hover { background: var(--accent-hover); }

.btn-close-sidebar {
    display: none;
    padding: 8px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
}

.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 2px;
    font-size: 13px;
    transition: background 0.12s;
    position: relative;
}
.conv-item:hover { background: var(--sidebar-hover); }
.conv-item.active { background: var(--sidebar-active); }
.conv-item .conv-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.4;
}
.conv-item .conv-delete {
    display: none;
    margin-left: 8px;
    padding: 4px 8px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 4px;
    font-size: 16px;
    flex-shrink: 0;
    line-height: 1;
}
.conv-item:hover .conv-delete { display: block; }
.conv-item .conv-delete:hover { color: var(--danger); background: rgba(239,68,68,0.15); }
.conv-item .conv-date { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }

/* Sidebar backdrop (mobile) */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 99;
}

/* ── Mobile top bar ──────────────────────────────────── */
.mobile-topbar {
    display: none;
    height: var(--mobile-topbar-h);
    align-items: center;
    padding: 0 12px;
    border-bottom: 1px solid var(--border-color);
    background: #fff;
    flex-shrink: 0;
}
.btn-menu {
    padding: 6px;
    background: none;
    border: none;
    color: #3f3f46;
    cursor: pointer;
    border-radius: 6px;
    margin-right: 10px;
}
.mobile-title { font-size: 15px; font-weight: 600; color: #18181b; }

/* ── Main Chat Area ──────────────────────────────────── */
#chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-chat);
}

/* Empty state */
.chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px;
    text-align: center;
}
.chat-empty .empty-logo { font-size: 48px; margin-bottom: 12px; }
.chat-empty h2 { font-size: 22px; margin-bottom: 6px; color: #18181b; }
.chat-empty p { font-size: 13px; color: #71717a; margin-bottom: 24px; }
.quick-questions { max-width: 520px; width: 100%; }
.quick-label { font-size: 12px; color: #a1a1aa; display: block; margin-bottom: 10px; }
.quick-btn {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 16px;
    margin-bottom: 6px;
    background: #f4f4f5;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
    color: #3f3f46;
    transition: all 0.12s;
}
.quick-btn:hover { background: #e4e4e7; border-color: #d4d4d8; }
.quick-btn:active { background: #d4d4d8; }

/* Messages */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
    -webkit-overflow-scrolling: touch;
}
.msg-row { display: flex; padding: 6px 16px; }
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.msg-bubble {
    max-width: 72%;
    padding: 10px 16px;
    border-radius: var(--radius);
    line-height: 1.6;
    font-size: 14px;
    word-break: break-word;
}
.msg-row.user .msg-bubble { background: var(--bubble-user); color: #fff; border-bottom-right-radius: 4px; }
.msg-row.assistant .msg-bubble { background: var(--bubble-assistant); color: #18181b; border-bottom-left-radius: 4px; }

.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble code { background: rgba(0,0,0,0.06); padding: 2px 6px; border-radius: 4px; font-size: 13px; }
.msg-bubble pre { background: #1e1e2e; color: #e4e4e7; padding: 12px; border-radius: 8px; overflow-x: auto; margin: 8px 0; font-size: 13px; }
.msg-bubble pre code { background: none; padding: 0; color: inherit; white-space: pre-wrap; word-break: break-all; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 6px 0; }
.msg-bubble li { margin-bottom: 4px; }
.msg-bubble blockquote { border-left: 3px solid #d4d4d8; padding-left: 12px; color: #71717a; margin: 8px 0; }
.msg-bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.msg-bubble th, .msg-bubble td { border: 1px solid #d4d4d8; padding: 6px 10px; text-align: left; }
.msg-bubble th { background: #f4f4f5; }

/* Sources inside assistant bubble */
.sources-toggle {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #e4e4e7;
}
.sources-toggle-btn {
    background: none;
    border: none;
    color: #3b82f6;
    cursor: pointer;
    font-size: 12px;
    padding: 4px 0;
    -webkit-tap-highlight-color: transparent;
}
.sources-toggle-btn:hover { text-decoration: underline; }
.sources-list { margin-top: 8px; font-size: 12px; display: none; }
.sources-list.open { display: block; }
.source-item {
    padding: 6px 10px;
    margin-bottom: 4px;
    background: #e4e4e7;
    border-radius: 6px;
    color: #3f3f46;
}
.source-item .source-name { font-weight: 600; margin-bottom: 2px; }
.source-item .source-snippet { color: #71717a; font-size: 11px; }

/* Loading dots */
.typing-dots { display: flex; gap: 4px; padding: 2px 0; }
.typing-dots span { width: 6px; height: 6px; border-radius: 50%; background: #a1a1aa; animation: bounce 1.4s infinite both; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-6px); } }

/* ── Input Area ──────────────────────────────────────── */
.chat-input-area { border-top: 1px solid var(--border-color); padding: 10px 16px 8px; flex-shrink: 0; }
.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: #f4f4f5;
    border: 1px solid #d4d4d8;
    border-radius: 14px;
    padding: 6px 6px 6px 14px;
    transition: border-color 0.15s;
}
.input-wrapper:focus-within { border-color: var(--accent); background: #fff; }

#chat-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 15px;
    line-height: 1.5;
    resize: none;
    max-height: 120px;
    font-family: inherit;
}
#chat-input::placeholder { color: #a1a1aa; }

.btn-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.input-hint { font-size: 11px; color: #a1a1aa; text-align: center; margin-top: 6px; }

/* ── Scrollbar ───────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #d4d4d8; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #a1a1aa; }

/* ============================================== */
/* ── Mobile: <= 768px ────────────────────────── */
/* ============================================== */
@media (max-width: 768px) {
    /* Sidebar slides in from left as overlay */
    #sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 101;
    }
    #sidebar.open { transform: translateX(0); }

    .sidebar-backdrop {
        display: block;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }
    .sidebar-backdrop.visible {
        opacity: 1;
        pointer-events: auto;
    }

    .btn-close-sidebar { display: block; }

    /* Mobile top bar */
    .mobile-topbar { display: flex; }
    .mobile-title { display: block; }

    /* Messages go wider */
    .msg-row { padding: 4px 10px; }
    .msg-bubble { max-width: 90%; font-size: 14px; padding: 10px 14px; }

    /* Empty state */
    .chat-empty { padding: 20px 16px; }
    .chat-empty .empty-logo { font-size: 40px; }
    .chat-empty h2 { font-size: 18px; }

    /* Input */
    .chat-input-area { padding: 8px 10px 6px; }
    #chat-input { font-size: 16px; }  /* prevent iOS zoom */
    .input-hint { display: none; }

    /* Larger touch targets */
    .conv-item { padding: 12px 14px; font-size: 14px; }
    .conv-item .conv-delete {
        display: block;  /* always show on mobile */
        padding: 6px 10px;
        font-size: 18px;
    }

    /* Hide source snippet on mobile to save space */
    .source-item .source-snippet { display: none; }
}
