:root {
    --brand: #312F82;
    --brand-light: #4063BC;
    --accent: #1E88E5;
    --bg: #EEF2FB;
    --surface: #FFFFFF;
    --user-bubble: #312F82;
    --bot-bubble: #FFFFFF;
    --text: #1F2333;
    --muted: #6B7185;
    --border: #DDE3F0;
    --sidebar-width: 280px;
}

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

html, body {
    height: 100%;
}

body {
    display: flex;
    background: var(--bg);
    color: var(--text);
    font-family: 'Poppins', system-ui, sans-serif;
    font-size: 15px;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 24px 20px;
    background: linear-gradient(180deg, var(--brand) 0%, var(--brand-light) 100%);
    color: #fff;
    overflow-y: auto;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand h1 {
    font-size: 20px;
    font-weight: 600;
}

.brand p {
    font-size: 12px;
    opacity: 0.8;
}

.logo {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: #fff;
    color: var(--brand);
    font-weight: 600;
    font-size: 14px;
}

.new-chat {
    padding: 10px 14px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 10px;
    background: transparent;
    color: #fff;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.new-chat:hover {
    background: rgba(255, 255, 255, 0.12);
}

.sidebar h2 {
    margin-top: 8px;
    font-size: 12px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    opacity: 0.75;
}

.suggestions {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.suggestions button {
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    font: inherit;
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    transition: background 0.2s;
}

.suggestions button:hover {
    background: rgba(255, 255, 255, 0.24);
}

/* Chat area */
.chat {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-header {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--brand);
    color: #fff;
    font-weight: 600;
}

.menu-btn {
    border: none;
    background: none;
    color: inherit;
    font-size: 22px;
    cursor: pointer;
}

.messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 28px max(24px, calc((100% - 820px) / 2));
}

.message {
    display: flex;
}

.message.user {
    justify-content: flex-end;
}

.bubble {
    max-width: min(85%, 720px);
    padding: 12px 16px;
    border-radius: 16px;
    line-height: 1.6;
    overflow-wrap: anywhere;
}

.message.user .bubble {
    background: var(--user-bubble);
    color: #fff;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}

.message.bot .bubble {
    background: var(--bot-bubble);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(20, 30, 60, 0.05);
}

.message.error .bubble {
    border-color: #F2B8B5;
    background: #FFF5F5;
}

/* Markdown content inside bot bubbles */
.bubble > * + * {
    margin-top: 0.6em;
}

.bubble ul, .bubble ol {
    padding-left: 1.4em;
}

.bubble a {
    color: var(--accent);
}

.bubble code {
    padding: 1px 5px;
    border-radius: 4px;
    background: #F1F3F9;
    font-size: 0.9em;
}

.bubble pre {
    padding: 12px;
    border-radius: 8px;
    background: #F1F3F9;
    overflow-x: auto;
}

.bubble table {
    border-collapse: collapse;
    display: block;
    overflow-x: auto;
}

.bubble th, .bubble td {
    padding: 6px 10px;
    border: 1px solid var(--border);
    text-align: left;
}

.bubble th {
    background: #F5F7FC;
}

/* Citations */
.cite {
    display: inline-block;
    min-width: 1.4em;
    margin-left: 2px;
    padding: 0 4px;
    border-radius: 6px;
    background: #E3E9F8;
    color: var(--brand);
    font-size: 0.7em;
    font-weight: 600;
    line-height: 1.5;
    text-align: center;
    vertical-align: super;
}

.bubble a:has(> .cite) {
    text-decoration: none;
}

.sources {
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
    font-size: 13px;
}

.sources summary {
    color: var(--muted);
    font-weight: 500;
    cursor: pointer;
}

.sources ol {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
    padding: 0;
}

.sources li {
    display: flex;
    gap: 8px;
}

.source-n {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    border-radius: 6px;
    background: #E3E9F8;
    color: var(--brand);
    font-size: 11px;
    font-weight: 600;
    line-height: 20px;
    text-align: center;
}

.sources p {
    margin-top: 2px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Tool chips */
.tools {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.tool-chip {
    display: inline-flex;
    align-items: baseline;
    gap: 6px;
    padding: 3px 10px;
    border-radius: 999px;
    background: #E8F3EC;
    color: #1F6B3A;
    font-size: 12px;
    font-weight: 500;
}

.tool-chip small {
    color: #4C7A5D;
    font-weight: 400;
}

.tool-chip.running {
    background: #E3E9F8;
    color: var(--brand);
    animation: pulse 1.2s infinite ease-in-out;
}

@keyframes pulse {
    50% { opacity: 0.55; }
}

.answered-by {
    margin-top: 8px;
    color: var(--muted);
    font-size: 11px;
}

/* Typing indicator */
.typing {
    display: inline-flex;
    gap: 4px;
    padding: 4px 0;
}

.typing i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted);
    animation: blink 1.2s infinite ease-in-out;
}

.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
    0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

/* Composer */
.composer {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    width: min(820px, calc(100% - 32px));
    margin: 0 auto;
    padding: 10px 10px 10px 16px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    box-shadow: 0 2px 8px rgba(20, 30, 60, 0.06);
}

.composer:focus-within {
    border-color: var(--accent);
}

#user-input {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    background: transparent;
    font: inherit;
    line-height: 1.5;
    padding: 6px 0;
    max-height: 160px;
}

#send-btn {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    border: none;
    border-radius: 12px;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    transition: background 0.2s;
}

#send-btn:hover {
    background: #1976D2;
}

#send-btn:disabled {
    background: #A9C7EE;
    cursor: not-allowed;
}

.disclaimer {
    padding: 8px 16px 14px;
    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

/* Mobile: sidebar becomes a slide-in drawer */
@media (max-width: 768px) {
    .chat-header {
        display: flex;
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 10;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .messages {
        padding: 16px;
    }

    .bubble {
        max-width: 92%;
    }
}
