:root {
    color-scheme: light;
    --bg: #ffffff;
    --bg-soft: #f4f4f5;
    --sidebar-bg: #f9f9f9;
    --surface: #ffffff;
    --surface-hover: #ececf1;
    --border: #e4e4e7;
    --ink: #0d0d0d;
    --muted: #6b6b76;
    --accent: #0f766e;
    --user-bubble: #f1f1f3;
    --code-bg: #0d1117;
    --code-ink: #e6edf3;
    --code-header: #1f2630;
    --danger: #dc2626;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.08);
    --tok-keyword: #ff7b72;
    --tok-string: #a5d6ff;
    --tok-number: #79c0ff;
    --tok-comment: #8b949e;
    --sidebar-width: 268px;
    --content-width: 780px;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #212121;
    --bg-soft: #2a2a2a;
    --sidebar-bg: #171717;
    --surface: #2f2f2f;
    --surface-hover: #3a3a3a;
    --border: #3a3a3a;
    --ink: #ececec;
    --muted: #a1a1aa;
    --accent: #2dd4bf;
    --user-bubble: #303030;
    --code-bg: #0d0d0d;
    --code-header: #262626;
    --danger: #f87171;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg: #212121;
        --bg-soft: #2a2a2a;
        --sidebar-bg: #171717;
        --surface: #2f2f2f;
        --surface-hover: #3a3a3a;
        --border: #3a3a3a;
        --ink: #ececec;
        --muted: #a1a1aa;
        --accent: #2dd4bf;
        --user-bubble: #303030;
        --code-bg: #0d0d0d;
        --code-header: #262626;
        --danger: #f87171;
        --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.6;
    background: var(--bg);
    color: var(--ink);
}

button, input, select, textarea { font: inherit; color: inherit; }
svg { display: block; }

/* ---------- Layout ---------- */

.app { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    transition: margin-left 0.2s ease;
}

.app.sidebar-collapsed .sidebar { margin-left: calc(-1 * var(--sidebar-width)); }
.sidebar-backdrop { display: none; }

.sidebar-top { display: flex; align-items: center; gap: 0.25rem; }
.brand { flex: 1; display: flex; align-items: center; gap: 0.4rem; font-weight: 700; padding-left: 0.25rem; }

.search {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.6rem;
    border-radius: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--muted);
}

.search svg { width: 16px; height: 16px; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--ink); }

.chat-list { flex: 1; overflow-y: auto; margin: 0 -0.25rem; padding: 0 0.25rem; }

.chat-group h2 {
    margin: 1rem 0.5rem 0.3rem;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
}

.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--surface-hover); }

.chat-title {
    flex: 1;
    min-width: 0;
    padding: 0.45rem 0.6rem;
    border: 0;
    background: none;
    text-align: left;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

.chat-title.fresh { animation: chunk-fade 0.8s ease-out; }
.chat-item:hover .chat-item-actions, .chat-item:focus-within .chat-item-actions { display: flex; }
.chat-item-actions .icon-button { width: 26px; height: 26px; }
.chat-item-actions .icon-button svg { width: 15px; height: 15px; }

.rename-input {
    width: 100%;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--accent);
    border-radius: 8px;
    background: var(--surface);
    outline: 0;
    font-size: 0.9rem;
}

.chat-list-empty { margin-top: 2rem; text-align: center; font-size: 0.85rem; color: var(--muted); }

.sidebar-bottom { border-top: 1px solid var(--border); padding-top: 0.5rem; }

.sidebar-button {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0.6rem;
    border: 0;
    border-radius: 8px;
    background: none;
    cursor: pointer;
}

.sidebar-button:hover { background: var(--surface-hover); }
.sidebar-button svg { width: 18px; height: 18px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar { display: flex; align-items: center; gap: 0.25rem; padding: 0.5rem 0.75rem; }
.topbar-actions { margin-left: auto; display: flex; gap: 0.15rem; }
.icon-button.open-sidebar { display: none; }
.app.sidebar-collapsed .icon-button.open-sidebar { display: inline-grid; }

.topbar-title { display: flex; align-items: center; gap: 0.4rem; padding: 0.4rem 0.7rem; font-size: 1.05rem; font-weight: 600; }

.mode-select {
    position: relative;
    top: 1px;
    padding: 0.3rem 0.4rem;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 0.95rem;
    cursor: pointer;
}
.mode-select:hover { background: var(--surface-hover); }
.mode-select option { background: var(--bg); color: var(--ink); }

/* ---------- Buttons ---------- */

.icon-button {
    display: inline-grid;
    place-items: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.icon-button:hover:not(:disabled) { background: var(--surface-hover); color: var(--ink); }
.icon-button:disabled { opacity: 0.35; cursor: default; }
.icon-button svg { width: 18px; height: 18px; }

.button {
    padding: 0.5rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
}

.button:hover { background: var(--surface-hover); }
.button.primary { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.button.primary:hover { opacity: 0.85; }
.button.danger { color: var(--danger); }
.button.small { padding: 0.2rem 0.6rem; font-size: 0.8rem; }

/* ---------- Conversation ---------- */

.conversation { flex: 1; overflow-y: auto; }

.empty-state {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.75rem;
    padding: 2rem 1rem;
}

.empty-state h1 { margin: 0; font-size: 1.9rem; font-weight: 600; text-align: center; }

.suggestions {
    width: min(100%, 680px);
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
}

.suggestion {
    padding: 0.85rem 1rem;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.suggestion:hover { background: var(--bg-soft); }
.suggestion strong { display: block; font-size: 0.92rem; font-weight: 600; }
.suggestion span { font-size: 0.85rem; color: var(--muted); }

.messages {
    max-width: var(--content-width);
    margin: 0 auto;
    padding: 1rem 1rem 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.75rem;
}

.messages:empty { display: none; }

.message { display: flex; gap: 0.9rem; }
.message.entering { animation: fade-in 0.2s ease-out; }
.fade-chunk { animation: chunk-fade 0.4s ease-out; }

.message.user { flex-direction: column; align-items: flex-end; gap: 0.2rem; }
.message.user .bubble { max-width: 80%; padding: 0.6rem 1rem; border-radius: 20px; background: var(--user-bubble); }
.message.user .bubble .attachment-chips + .user-text { margin-top: 0.4rem; }
.message.user .message-actions { opacity: 0; transition: opacity 0.15s ease; }
.message.user:hover .message-actions, .message.user:focus-within .message-actions { opacity: 1; }
.user-text { white-space: pre-wrap; overflow-wrap: anywhere; }

.message.user.editing { align-items: stretch; }
.edit-box { padding: 0.75rem; border-radius: 18px; background: var(--user-bubble); }
.edit-input { width: 100%; min-height: 80px; border: 0; outline: 0; background: transparent; resize: vertical; }
.edit-actions { display: flex; justify-content: flex-end; gap: 0.5rem; }

.avatar {
    width: 30px;
    height: 30px;
    flex-shrink: 0;
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 50%;
    color: var(--accent);
}

.avatar svg { width: 16px; height: 16px; }
.avatar .ribbon-logo { width: 18px; height: 18px; fill: currentColor; }
/* Masked so the logo follows the theme color instead of the file's fixed fill. */
.model-logo {
    flex-shrink: 0;
    width: 1.25em;
    height: 1.25em;
    background: var(--accent);
    mask: url("/static/vendor/google/link_off_64dp_E3E3E3_FILL0_wght400_GRAD0_opsz48.svg") center / contain no-repeat;
}
.message-main { flex: 1; min-width: 0; padding-top: 0.15rem; }

.message-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 0.1rem; margin-top: 0.3rem; color: var(--muted); }
.message-actions .icon-button { width: 30px; height: 30px; }
.message-actions .icon-button svg { width: 16px; height: 16px; }
.message-meta { margin-left: 0.5rem; font-size: 0.75rem; color: var(--muted); }
.variant-nav { display: flex; align-items: center; margin-right: 0.25rem; font-size: 0.8rem; }

.attachment-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }

.attachment-chip {
    max-width: 220px;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.3rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    font-size: 0.8rem;
}

.chip-icon svg { width: 14px; height: 14px; color: var(--muted); }
.chip-thumb { width: 20px; height: 20px; flex: none; border-radius: 4px; object-fit: cover; }
.chip-name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chip-remove { display: grid; place-items: center; padding: 0; border: 0; background: none; color: var(--muted); cursor: pointer; }
.chip-remove svg { width: 14px; height: 14px; }

.reasoning { margin-bottom: 0.75rem; font-size: 0.9rem; color: var(--muted); }
.reasoning summary { cursor: pointer; font-weight: 500; user-select: none; }

.reasoning-text {
    max-height: 320px;
    margin-top: 0.5rem;
    padding-left: 0.85rem;
    border-left: 2px solid var(--border);
    overflow-y: auto;
}

.plain-text { white-space: pre-wrap; }

.typing { display: flex; gap: 4px; padding: 0.6rem 0; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: bounce 1s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

.message.streaming .reply > :last-child::after {
    content: "";
    display: inline-block;
    width: 0.6em;
    height: 0.6em;
    margin-left: 0.25em;
    border-radius: 50%;
    background: var(--ink);
    vertical-align: middle;
    animation: pulse 1s infinite;
}

.status-note { display: flex; align-items: center; gap: 0.75rem; margin-top: 0.5rem; font-size: 0.88rem; color: var(--muted); }

.status-note.error {
    padding: 0.6rem 0.8rem;
    border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
    border-radius: 10px;
    background: color-mix(in srgb, var(--danger) 8%, transparent);
    color: var(--danger);
}

/* ---------- Markdown ---------- */

.markdown { overflow-wrap: anywhere; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 0.9em; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 { margin: 1.3em 0 0.5em; line-height: 1.3; }
.markdown h1 { font-size: 1.5em; }
.markdown h2 { font-size: 1.3em; }
.markdown h3 { font-size: 1.15em; }
.markdown ul, .markdown ol { margin: 0 0 0.9em; padding-left: 1.5em; }
.markdown li { margin: 0.25em 0; }
.markdown li > ul, .markdown li > ol { margin: 0.25em 0 0; }
.markdown blockquote { margin: 0 0 0.9em; padding-left: 1em; border-left: 3px solid var(--border); color: var(--muted); }
.markdown hr { margin: 1.5em 0; border: 0; border-top: 1px solid var(--border); }
.markdown a { color: var(--accent); }
.markdown code { padding: 0.15em 0.35em; border-radius: 5px; background: var(--bg-soft); font-family: var(--mono); font-size: 0.875em; }

.code-block { margin: 0 0 1em; border-radius: 10px; overflow: hidden; background: var(--code-bg); }

.code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.35rem 0.9rem;
    background: var(--code-header);
    color: #b4b4b4;
    font-size: 0.78rem;
}

.code-actions { display: flex; gap: 0.9rem; }
.code-copy, .code-toggle { padding: 0; border: 0; background: none; color: inherit; font-size: 0.78rem; cursor: pointer; }
.code-copy:hover, .code-toggle:hover { color: #ffffff; }
.code-toggle { display: none; }
.mermaid-rendered .code-toggle { display: inline-flex; align-items: center; gap: 0.3rem; }
.code-toggle svg { width: 14px; height: 14px; }
.code-block pre { margin: 0; padding: 0.9rem 1rem; overflow-x: auto; }
.code-block code { padding: 0; background: none; color: var(--code-ink); font-size: 0.85rem; line-height: 1.55; }
.mermaid-diagram { display: none; }
.mermaid-rendered { border: 1px solid var(--border); background: var(--surface); }
.mermaid-rendered pre { display: none; }
.mermaid-rendered .mermaid-diagram { display: flex; justify-content: center; padding: 1rem; overflow-x: auto; }
.mermaid-rendered.show-code { background: var(--code-bg); }
.mermaid-rendered.show-code pre { display: block; }
.mermaid-rendered.show-code .mermaid-diagram { display: none; }
.mermaid-diagram svg { max-width: 100%; height: auto; }
.mermaid-failed .mermaid-diagram { display: block; padding: 0.5rem 1rem 0; color: var(--danger); font-size: 0.8rem; }
.tok-keyword { color: var(--tok-keyword); }
.tok-string { color: var(--tok-string); }
.tok-number { color: var(--tok-number); }
.tok-comment { color: var(--tok-comment); font-style: italic; }

.table-wrap { margin: 0 0 1em; overflow-x: auto; }
.markdown table { border-collapse: collapse; font-size: 0.92em; }
.markdown th, .markdown td { padding: 0.4rem 0.7rem; border: 1px solid var(--border); text-align: left; }
.markdown th { background: var(--bg-soft); }

/* ---------- Composer ---------- */

.composer-area { position: relative; padding: 0 1rem; }

.scroll-bottom {
    position: absolute;
    bottom: calc(100% + 0.75rem);
    left: 50%;
    transform: translateX(-50%);
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    box-shadow: var(--shadow);
    color: var(--ink);
    cursor: pointer;
}

.scroll-bottom svg { width: 18px; height: 18px; }

.composer {
    max-width: var(--content-width);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding: 0.6rem 0.6rem 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 24px;
    background: var(--surface);
    box-shadow: var(--shadow);
    transition: border-color 0.15s ease;
}

.composer:focus-within, .composer.dragging { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
.composer textarea { max-height: 240px; padding: 0.3rem 0.35rem; border: 0; outline: 0; background: transparent; resize: none; line-height: 1.5; }
.composer-bar { display: flex; align-items: center; gap: 0.15rem; }
.composer-hint { margin: 0 0.5rem 0 auto; font-size: 0.75rem; color: var(--muted); }

.send-button {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--ink);
    color: var(--bg);
    cursor: pointer;
    transition: opacity 0.15s ease;
}

.send-button:disabled { opacity: 0.3; cursor: default; }
.send-button svg { width: 18px; height: 18px; }

.disclaimer { margin: 0.4rem 0 0.6rem; text-align: center; font-size: 0.75rem; color: var(--muted); }

/* ---------- Dialog & toast ---------- */

.dialog {
    width: min(560px, calc(100vw - 2rem));
    padding: 0;
    border: 0;
    border-radius: 16px;
    background: var(--surface);
    color: var(--ink);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.settings-form { max-height: 85vh; display: flex; flex-direction: column; gap: 1rem; padding: 1.25rem 1.5rem 1.5rem; overflow-y: auto; }
.dialog-header { display: flex; align-items: center; justify-content: space-between; }
.dialog-header h2 { margin: 0; font-size: 1.15rem; }

.field { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; }
.field > span { display: flex; justify-content: space-between; font-weight: 500; }
.field.checkbox { flex-direction: row; align-items: center; gap: 0.6rem; }
.field.checkbox input { width: 1rem; height: 1rem; accent-color: var(--accent); }
.field output { color: var(--muted); font-variant-numeric: tabular-nums; }
.field textarea, .field input[type="number"], .field select { padding: 0.5rem 0.7rem; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); }
.field textarea { resize: vertical; }
.field input[type="range"] { accent-color: var(--accent); }

.dialog-section h3 { margin: 0.25rem 0 0.5rem; font-size: 0.9rem; }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.muted { margin: 0.5rem 0 0; font-size: 0.82rem; color: var(--muted); }

.shortcuts { display: grid; grid-template-columns: 1fr auto; gap: 0.35rem 1rem; margin: 0; font-size: 0.85rem; }
.shortcuts dd { margin: 0; }
kbd { padding: 0.05rem 0.4rem; border: 1px solid var(--border); border-radius: 5px; background: var(--bg-soft); font-family: var(--mono); font-size: 0.78rem; }

.toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    z-index: 50;
    padding: 0.6rem 1rem;
    border-radius: 10px;
    background: var(--ink);
    color: var(--bg);
    font-size: 0.88rem;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 1rem);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.visible { opacity: 1; transform: translate(-50%, 0); }

@keyframes fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes chunk-fade {
    from { opacity: 0; }
    to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .message.entering, .fade-chunk, .chat-title.fresh { animation: none; }
}

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

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

/* ---------- Mobile ---------- */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 40;
        margin-left: calc(-1 * var(--sidebar-width));
        box-shadow: var(--shadow);
    }

    .app.sidebar-open .sidebar { margin-left: 0; }
    .app.sidebar-open .sidebar-backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.4); }
    .icon-button.open-sidebar { display: inline-grid; }
    .composer-hint { display: none; }
    .suggestions { grid-template-columns: 1fr; }
    .message.user .bubble { max-width: 90%; }
    .message-meta { display: none; }
}
