:root {
    --bg: #0f0f11;
    --bg-elevated: #17171a;
    --sidebar-bg: #141416;
    --card-bg: #1a1a1e;
    --border: #2a2a30;
    --border-soft: #232328;
    --text: #ececf1;
    --text-muted: #9a9aa6;
    --text-faint: #858592;
    --accent: #7c5cff;
    --accent-hover: #8d70ff;
    --accent-soft: rgba(124, 92, 255, 0.15);
    --user-bubble: #26272e;
    --danger: #ff6b6b;
    --danger-soft: rgba(255, 107, 107, 0.12);
    --ok: #4ec98f;
    --ok-soft: rgba(78, 201, 143, 0.12);
    --radius: 12px;
    --sidebar-width: 270px;
    --content-width: 768px;
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
}

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

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.muted { color: var(--text-muted); }
.small { font-size: 0.85em; }
.warn { color: #e6b54a; }

/* Element defaults the component rules build on (browser defaults would add
   top margins to headings/paragraphs and give form controls their own font). */
h1, h2, h3, h4, h5, h6 { margin: 0 0 0.5rem; line-height: 1.2; }
h1, h2, h3 { font-weight: 600; }
h1 { font-size: calc(1.375rem + 1.5vw); }
h2 { font-size: calc(1.325rem + 0.9vw); }
h3 { font-size: calc(1.3rem + 0.6vw); }
h4 { font-size: calc(1.275rem + 0.3vw); }
h5 { font-size: 1.25rem; }
h6 { font-size: 1rem; }
@media (min-width: 1200px) {
    h1 { font-size: 2.5rem; }
    h2 { font-size: 2rem; }
    h3 { font-size: 1.75rem; }
    h4 { font-size: 1.5rem; }
}
p { margin: 0 0 1rem; }
code { font-size: 0.875em; }
button, input, optgroup, select, textarea { margin: 0; font-family: inherit; font-size: inherit; line-height: inherit; }
button:not(:disabled) { cursor: pointer; }
textarea { resize: vertical; }

/* ---------- App shell ---------- */
.app-shell {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.app-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

/* ---------- Sidebar ---------- */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.sidebar-header {
    padding: 18px 18px 6px;
}

.sidebar-header .brand {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.3px;
    background: linear-gradient(90deg, #a78bfa, #7c5cff);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.new-chat-btn {
    margin: 10px 14px 8px;
    padding: 10px 14px;
    background: var(--accent-soft);
    color: var(--text);
    border: 1px solid var(--accent);
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.95rem;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.15s;
}

.new-chat-btn:hover { background: rgba(124, 92, 255, 0.25); }
.new-chat-btn .plus { font-size: 1.2rem; line-height: 1; }

.thread-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.thread-empty {
    padding: 14px;
    font-size: 0.9rem;
}

.thread-item {
    display: flex;
    align-items: center;
    border-radius: 8px;
    transition: background 0.12s;
}

.thread-item:hover { background: var(--bg-elevated); }
.thread-item.active { background: var(--accent-soft); }

.thread-link {
    flex: 1;
    min-width: 0;
    padding: 9px 10px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.9rem;
}

.thread-link:hover { text-decoration: none; }

.thread-delete {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px 8px;
    font-size: 0.85rem;
    border-radius: 6px;
}

.thread-item:hover .thread-delete { opacity: 1; }
.thread-delete:hover { color: var(--danger); }

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.footer-link {
    display: block;
    width: 100%;
    text-align: left;
    padding: 9px 12px;
    color: var(--text-muted);
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
}

.footer-link:hover { background: var(--bg-elevated); color: var(--text); text-decoration: none; }
.footer-link.active { color: var(--text); background: var(--bg-elevated); }
.logout-form { margin: 0; }
.logout-btn { font-family: inherit; }

/* ---------- Chat ---------- */
.chat {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

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

.model-picker {
    display: flex;
    align-items: center;
    gap: 8px;
}

.model-picker label {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.model-picker select {
    background: var(--bg-elevated);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.9rem;
    cursor: pointer;
    max-width: 340px;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
}

.messages-inner {
    max-width: var(--content-width);
    margin: 0 auto;
}

.empty-state {
    max-width: var(--content-width);
    margin: 12vh auto 0;
    text-align: center;
    color: var(--text-muted);
}

.empty-state h1 {
    font-size: 2rem;
    color: var(--text);
    margin-bottom: 12px;
}

.notice {
    display: inline-block;
    margin-top: 8px;
    padding: 10px 14px;
    border-radius: 10px;
    background: var(--bg-elevated);
}

.msg {
    display: flex;
    gap: 14px;
    padding: 18px 0;
    border-bottom: 1px solid var(--border-soft);
}

.msg:last-child { border-bottom: none; }

.msg-avatar {
    flex-shrink: 0;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.95rem;
    background: var(--bg-elevated);
}

.msg-user .msg-avatar { background: #2f3340; }
.msg-assistant .msg-avatar { background: linear-gradient(135deg, #7c5cff, #a78bfa); color: #fff; }

.msg-content { flex: 1; min-width: 0; }

.msg-role {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.user-text {
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.6;
}

.msg-error .markdown { color: var(--danger); }

/* Markdown body */
.markdown { line-height: 1.65; word-wrap: break-word; }
.markdown p { margin: 0 0 0.85em; }
.markdown p:last-child { margin-bottom: 0; }
.markdown ul, .markdown ol { margin: 0 0 0.85em; padding-left: 1.5em; }
.markdown li { margin: 0.2em 0; }
.markdown h1, .markdown h2, .markdown h3 { margin: 1em 0 0.5em; line-height: 1.3; }
.markdown a { text-decoration: underline; }
.markdown code {
    background: #0c0c0e;
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 1px 5px;
    font-family: "Cascadia Code", Consolas, "Courier New", monospace;
    font-size: 0.88em;
}
.markdown pre {
    background: #0c0c0e;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px 16px;
    overflow-x: auto;
    margin: 0 0 0.85em;
}
.markdown pre code { background: none; border: none; padding: 0; font-size: 0.86em; }
.markdown blockquote {
    margin: 0 0 0.85em;
    padding-left: 14px;
    border-left: 3px solid var(--border);
    color: var(--text-muted);
}
.markdown table { border-collapse: collapse; margin-bottom: 0.85em; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }

/* Typing indicator */
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* Composer */
.composer-wrap {
    flex-shrink: 0;
    padding: 12px 20px 18px;
    background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.composer {
    max-width: var(--content-width);
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 8px 8px 8px 16px;
    transition: border-color 0.15s;
}

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

.composer textarea {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.5;
    max-height: 220px;
    padding: 6px 0;
}

.composer textarea::placeholder { color: var(--text-faint); }
.composer textarea:disabled { opacity: 0.5; }

.composer-btn {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, opacity 0.15s;
}

.composer-btn.send { background: var(--accent); color: #fff; }
.composer-btn.send:hover:not(:disabled) { background: var(--accent-hover); }
.composer-btn.send:disabled { background: #34343c; color: var(--text-faint); cursor: not-allowed; }
.composer-btn.stop { background: #34343c; color: var(--text); }
.composer-btn.stop:hover { background: #44444e; }

.composer-hint {
    max-width: var(--content-width);
    margin: 6px auto 0;
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-faint);
}

/* ---------- Auth / login ---------- */
.auth-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(1200px 600px at 50% -10%, rgba(124, 92, 255, 0.18), transparent), var(--bg);
}

.login-card {
    width: 360px;
    max-width: 92vw;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.login-logo {
    font-size: 1.8rem;
    font-weight: 700;
    text-align: center;
    background: linear-gradient(90deg, #a78bfa, #7c5cff);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.login-sub {
    text-align: center;
    color: var(--text-muted);
    margin: 6px 0 22px;
}

.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-btn { margin-top: 6px; width: 100%; justify-content: center; }

.login-error {
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid rgba(255, 107, 107, 0.3);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 16px;
    font-size: 0.9rem;
}

.login-divider {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 16px 0 12px;
    color: var(--text-faint);
    font-size: 0.8rem;
}

.login-divider::before,
.login-divider::after {
    content: "";
    flex: 1;
    border-top: 1px solid var(--border);
}

.passkey-btn { width: 100%; }
#passkey-error { margin: 12px 0 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 0.85rem; color: var(--text-muted); }

input, select, textarea, .input {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 9px;
    padding: 10px 12px;
    font-size: 0.95rem;
    font-family: inherit;
    outline: none;
}

input:focus, select:focus, textarea:focus, .input:focus { border-color: var(--accent); }

/* ---------- Buttons ---------- */
.btn, .btn-primary {
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text);
    border-radius: 9px;
    padding: 9px 16px;
    font-size: 0.9rem;
    font-family: inherit;
    vertical-align: middle;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}

.btn:hover:not(:disabled), .btn-primary:hover:not(:disabled) { background: #232329; }
.btn:disabled, .btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:focus-visible, .btn-primary:focus-visible { box-shadow: 0 0 0 3px var(--accent-soft); }
.btn.small { padding: 5px 12px; font-size: 0.82rem; }

.btn.primary, .btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.btn.primary:hover:not(:disabled), .btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { background: var(--danger-soft); border-color: rgba(255, 107, 107, 0.35); color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: rgba(255, 107, 107, 0.2); }

.btn-link {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 4px 6px;
}
.btn-link.danger { color: var(--danger); }

/* ---------- Admin ---------- */
.admin-page {
    max-width: 920px;
    margin: 0 auto;
    padding: 28px 24px 60px;
    overflow-y: auto;
    width: 100%;
}

.admin-header { margin-bottom: 20px; }
.admin-header h1 { margin: 0 0 4px; }
.admin-header p { margin: 0; }

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px 22px;
    margin-bottom: 18px;
}

.card h2 { margin: 0 0 6px; font-size: 1.1rem; }

.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.card-head h2 { margin: 0; }
.card-actions { margin-top: 14px; display: flex; gap: 10px; }

.input.prompt, textarea.prompt { width: 100%; resize: vertical; line-height: 1.5; }
.input { width: 100%; }

.field-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
}
.field-row .field { flex: 1; min-width: 220px; }

.banner {
    border-radius: 9px;
    padding: 11px 14px;
    margin-bottom: 16px;
    font-size: 0.9rem;
}
.banner.ok { background: var(--ok-soft); color: var(--ok); border: 1px solid rgba(78, 201, 143, 0.3); }
.banner.err { background: var(--danger-soft); color: var(--danger); border: 1px solid rgba(255, 107, 107, 0.3); }

.tag {
    display: inline-block;
    padding: 2px 9px;
    border-radius: 20px;
    font-size: 0.78rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text-muted);
}
.tag.ok { color: var(--ok); border-color: rgba(78, 201, 143, 0.4); }
.tag.warn { color: #e6b54a; border-color: rgba(230, 181, 74, 0.4); }

/* Grid table */
.grid { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.grid th {
    text-align: left;
    padding: 8px 10px;
    color: var(--text-muted);
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    font-size: 0.82rem;
}
.grid td { padding: 10px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
.grid code { font-size: 0.85em; color: var(--text-muted); }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 38px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: #3a3a42;
    border-radius: 22px;
    transition: 0.2s;
}
.slider::before {
    content: "";
    position: absolute;
    height: 16px;
    width: 16px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(16px); }

/* Catalog */
.search { width: 100%; margin-bottom: 8px; }
.catalog { list-style: none; margin: 0; padding: 0; max-height: 480px; overflow-y: auto; }
.catalog li {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 6px;
    border-bottom: 1px solid var(--border-soft);
}
.catalog-info { flex: 1; min-width: 0; }
.catalog-name { font-size: 0.92rem; }
.catalog-id { font-size: 0.8rem; color: var(--text-muted); }
.catalog-meta { flex-shrink: 0; }

/* Scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #2e2e36; border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: #3a3a44; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Blazor error UI ---------- */
#blazor-error-ui {
    color-scheme: light dark;
    background: #2a1215;
    color: #ffb4b4;
    bottom: 0;
    box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.4);
    box-sizing: border-box;
    display: none;
    left: 0;
    padding: 0.9rem 1.4rem;
    position: fixed;
    width: 100%;
    z-index: 1000;
}

#blazor-error-ui .dismiss {
    cursor: pointer;
    position: absolute;
    right: 1rem;
    top: 0.75rem;
}

.blazor-error-boundary {
    background: #2a1215;
    padding: 1rem;
    color: #ffb4b4;
    border-radius: 8px;
}

.blazor-error-boundary::after {
    content: "An error has occurred.";
}

.loading-progress, .loading-progress-text { display: none; }

/* ---------- Mobile navigation drawer ---------- */
/* Hidden on desktop; the sidebar stays docked in the flex row. */
.drawer-toggle,
.drawer-backdrop { display: none; }

@media (max-width: 768px) {
    /* Sidebar becomes an off-canvas drawer, out of the flex flow so the
       content column gets the full viewport width. */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 40;
        width: min(84vw, var(--sidebar-width));
        transform: translateX(-100%);
        transition: transform 0.22s ease;
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
    }
    .app-shell.drawer-open .sidebar { transform: translateX(0); }

    /* Dimming backdrop behind the open drawer. */
    .drawer-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 30;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.22s ease;
    }
    .app-shell.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

    /* Hamburger toggle, fixed at the top-left. Sits under the open drawer. */
    .drawer-toggle {
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 4px;
        position: fixed;
        top: 9px;
        left: 10px;
        z-index: 25;
        width: 38px;
        height: 34px;
        padding: 0 8px;
        background: var(--bg-elevated);
        border: 1px solid var(--border);
        border-radius: 9px;
        cursor: pointer;
    }
    .drawer-toggle span {
        display: block;
        height: 2px;
        background: var(--text);
        border-radius: 2px;
    }

    /* Chat: clear the hamburger in the top bar, go edge-to-edge. */
    .chat-topbar { padding-left: 58px; }
    .messages { padding: 16px 12px; }
    .composer-wrap { padding: 10px 12px 14px; }

    /* Admin: clear the fixed hamburger and tighten padding. */
    .admin-page { padding: 60px 14px 48px; }
    .card { padding: 16px 14px; }
    .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

    /* Catalog rows stack their meta line on narrow screens. */
    .catalog li { flex-wrap: wrap; }
    .catalog-meta { flex-basis: 100%; }
}
