:root {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --surface-strong: #ffffff;
    --surface-alt: #eef1f5;
    --text: #1a2233;
    --muted: #64748b;
    --border: #e1e5eb;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
    --primary: #1f6feb;
    --accent: #f97316;
    --danger: #ef4444;
}

body[data-theme="dark"] {
    --bg: #0e1420;
    --surface: #151c29;
    --surface-strong: #1a2232;
    --surface-alt: #1f2938;
    --text: #e8edf5;
    --muted: #8b98ac;
    --border: #2a3446;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.22);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, sans-serif;
    color: var(--text);
    background: var(--bg);
    min-height: 100vh;
}

button, input, select, textarea {
    font: inherit;
}

.eyebrow {
    margin: 0 0 6px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 11px;
    font-weight: 600;
    color: var(--muted);
}

.glass-card {
    background: var(--surface-strong);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
}

input, select, textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    background: var(--surface-strong);
    color: var(--text);
}

input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

button {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 14px;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.primary-button {
    background: var(--primary);
    color: white;
    border: 1px solid var(--primary);
    font-weight: 600;
}

.primary-button:hover {
    background: color-mix(in srgb, var(--primary) 88%, black);
}

.secondary-button {
    background: var(--surface-strong);
    color: var(--text);
}

.secondary-button:hover {
    background: var(--surface-alt);
}

.small-button {
    padding: 5px 10px;
    font-size: 12px;
}

/* ── Login ─────────────────────────────────────────────────────────────── */

.login-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(10, 14, 22, 0.6);
}

body.authenticated .login-overlay {
    display: none;
}

.login-card {
    width: min(400px, 100%);
    border-radius: 12px;
    padding: 26px;
    display: grid;
    gap: 12px;
}

.login-hint {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

/* ── App shell ─────────────────────────────────────────────────────────── */

.app-shell {
    display: none;
    grid-template-columns: 280px 1fr;
    min-height: 100vh;
}

body.authenticated .app-shell {
    display: grid;
}

.sidebar {
    display: flex;
    flex-direction: column;
    padding: 20px 16px;
    border-right: 1px solid var(--border);
    background: var(--surface-strong);
}

.brand-panel {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 4px;
    margin-bottom: 16px;
}

.brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    font-size: 15px;
    font-weight: 700;
    color: white;
    background: var(--primary);
    flex-shrink: 0;
}

.brand-panel h1 {
    font-size: 16px;
    margin: 0;
}

.new-chat-button {
    width: 100%;
    margin-bottom: 14px;
}

.sidebar-scroll {
    flex: 1;
    overflow-y: auto;
    display: grid;
    gap: 4px;
    align-content: start;
    padding-right: 2px;
}

.sidebar-footer {
    display: grid;
    gap: 10px;
    padding-top: 14px;
    margin-top: 10px;
    border-top: 1px solid var(--border);
}

.session-card {
    padding: 12px;
    border-radius: 10px;
    display: grid;
    gap: 6px;
}

.role-pill {
    width: fit-content;
    padding: 3px 9px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--primary) 12%, var(--surface-alt));
    border: 1px solid color-mix(in srgb, var(--primary) 28%, var(--border));
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-size: 10px;
    font-weight: 700;
}

.theme-switcher {
    display: grid;
    gap: 4px;
}

/* ── Folder / chat tree ────────────────────────────────────────────────── */

.folder-group {
    display: grid;
    gap: 2px;
}

.folder-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    cursor: pointer;
}

.folder-header:hover {
    background: var(--surface-alt);
}

.folder-header-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.folder-delete-button {
    flex-shrink: 0;
    border: none;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    line-height: 1;
    padding: 2px 4px;
    cursor: pointer;
    border-radius: 4px;
}

.folder-delete-button:hover {
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 110;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(10, 14, 22, 0.6);
}

.modal-card {
    width: min(440px, 100%);
    border-radius: 12px;
    padding: 22px;
    display: grid;
    gap: 10px;
}

.modal-card h3 {
    margin: 0;
}

.modal-card p {
    margin: 0;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.5;
}

.chat-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 9px 10px;
    border-radius: 8px;
    color: var(--text);
    font-size: 13px;
    cursor: pointer;
    border: 1px solid transparent;
}

.chat-item:hover {
    background: var(--surface-alt);
}

.chat-item.active {
    background: color-mix(in srgb, var(--primary) 12%, var(--surface-alt));
    border-color: color-mix(in srgb, var(--primary) 35%, var(--border));
}

.chat-item-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ── Main chat panel ───────────────────────────────────────────────────── */

.chat-main {
    display: grid;
    grid-template-rows: auto 1fr auto;
    min-height: 100vh;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-strong);
}

.chat-header-title h2 {
    margin: 0;
    font-size: 16px;
}

.chat-header-meta {
    font-size: 12px;
    color: var(--muted);
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.chat-header-actions select {
    width: auto;
    max-width: 220px;
}

.chat-messages {
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.chat-empty-state {
    margin: auto;
    max-width: 420px;
    text-align: center;
    color: var(--muted);
}

.msg-bubble {
    max-width: 70ch;
    padding: 12px 16px;
    border-radius: 14px;
    line-height: 1.55;
    font-size: 14px;
    white-space: normal;
}

.msg-bubble.user {
    align-self: flex-end;
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.msg-bubble.assistant {
    align-self: flex-start;
    background: var(--surface-strong);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.msg-bubble.pending {
    color: var(--muted);
    font-style: italic;
}

.msg-bubble p {
    margin: 0 0 8px;
}

.msg-bubble p:last-child {
    margin-bottom: 0;
}

.msg-bubble pre {
    background: color-mix(in srgb, black 6%, transparent);
    border-radius: 8px;
    padding: 10px 12px;
    overflow-x: auto;
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 12.5px;
}

.msg-bubble.user pre {
    background: color-mix(in srgb, white 15%, transparent);
}

.msg-bubble code {
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 0.92em;
    background: color-mix(in srgb, black 6%, transparent);
    padding: 1px 5px;
    border-radius: 4px;
}

.msg-bubble.user code {
    background: color-mix(in srgb, white 18%, transparent);
}

.msg-bubble ul, .msg-bubble ol {
    margin: 0 0 8px;
    padding-left: 20px;
}

.msg-attachment-note {
    margin-top: 8px;
    font-size: 12px;
    opacity: 0.85;
}

.chat-composer {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    background: var(--surface-strong);
    display: grid;
    gap: 8px;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
}

.composer-row textarea {
    flex: 1;
    min-height: 44px;
    max-height: 200px;
    resize: vertical;
}

.attach-button {
    flex-shrink: 0;
    font-size: 16px;
    padding: 9px 12px;
}

.attachment-chip-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border-radius: 999px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    font-size: 12px;
}

.attachment-chip button {
    border: none;
    background: none;
    padding: 0;
    cursor: pointer;
    color: var(--muted);
    font-size: 13px;
}

[hidden] {
    display: none !important;
}

@media (max-width: 900px) {
    .app-shell {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
    }

    .sidebar {
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 40vh;
    }
}
