/* ---------- Tokens ---------- */

:root {
    --bg: #f7f5f0;
    --surface: #ffffff;
    --sidebar-bg: #fbfaf6;
    --border: #e7e2d8;
    --border-strong: #d8d2c4;
    --text: #1c1b18;
    --text-muted: #6e6a62;
    --text-faint: #9b968c;

    --accent: #0d9a95;
    --accent-strong: #0b7f7b;
    --accent-soft: rgba(13, 154, 149, 0.12);
    --accent-contrast: #ffffff;

    --danger: #c04545;
    --danger-soft: rgba(192, 69, 69, 0.1);

    --shadow-sm: 0 1px 2px rgba(28, 27, 24, 0.06);
    --shadow-md: 0 8px 24px rgba(28, 27, 24, 0.08);

    --font-display: "Space Grotesk", "Inter", system-ui, sans-serif;
    --font-body: "Inter", system-ui, sans-serif;
    --font-mono: "IBM Plex Mono", ui-monospace, monospace;

    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --sidebar-width: 288px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #101012;
        --surface: #19191c;
        --sidebar-bg: #141416;
        --border: #29292d;
        --border-strong: #35353a;
        --text: #f1f0ec;
        --text-muted: #96938c;
        --text-faint: #67645e;

        --accent: #22b8b0;
        --accent-strong: #3fcac2;
        --accent-soft: rgba(34, 184, 176, 0.16);
        --accent-contrast: #06201e;

        --danger: #e07a7a;
        --danger-soft: rgba(224, 122, 122, 0.14);

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.4);
    }
}

:root[data-theme="dark"] {
    --bg: #101012;
    --surface: #19191c;
    --sidebar-bg: #141416;
    --border: #29292d;
    --border-strong: #35353a;
    --text: #f1f0ec;
    --text-muted: #96938c;
    --text-faint: #67645e;

    --accent: #22b8b0;
    --accent-strong: #3fcac2;
    --accent-soft: rgba(34, 184, 176, 0.16);
    --accent-contrast: #06201e;

    --danger: #e07a7a;
    --danger-soft: rgba(224, 122, 122, 0.14);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.4);
}

/* ---------- Reset ---------- */

*, *::before, *::after { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

button, input, textarea {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

button {
    cursor: pointer;
    background: none;
    border: none;
}

a { color: var(--accent); }

.hidden { display: none !important; }

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        transition-duration: 0.001ms !important;
    }
}

/* ---------- Login ---------- */

.login-view {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    background:
        radial-gradient(circle at 15% 10%, var(--accent-soft), transparent 45%),
        var(--bg);
}

.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: 2.25rem 2rem;
    text-align: center;
}

.login-mark {
    width: 52px;
    height: 52px;
    margin: 0 auto 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
}

.login-card h1 {
    font-family: var(--font-display);
    font-size: 1.4rem;
    margin: 0 0 0.35rem;
}

.login-sub {
    color: var(--text-muted);
    font-size: 0.88rem;
    margin: 0 0 1.75rem;
}

.login-card form {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    text-align: left;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.field input {
    padding: 0.65rem 0.75rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--bg);
    color: var(--text);
}

.field input:focus-visible {
    border-color: var(--accent);
}

.primary-btn {
    background: var(--accent);
    color: var(--accent-contrast);
    border-radius: var(--radius-sm);
    padding: 0.7rem 1rem;
    font-weight: 600;
    margin-top: 0.25rem;
    transition: background 0.15s ease;
}

.primary-btn:hover { background: var(--accent-strong); }

.login-error {
    min-height: 1.2em;
    color: var(--danger);
    font-size: 0.85rem;
    margin: 1rem 0 0;
}

/* ---------- App shell ---------- */

.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    height: 100vh;
}

.sidebar {
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1.1rem 0.9rem;
    min-height: 0;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.3rem 0.4rem 1.1rem;
}

.brand-mark {
    color: var(--accent);
    display: flex;
}

.brand-name {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 0.98rem;
    letter-spacing: -0.01em;
}

.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    justify-content: center;
    background: var(--accent);
    color: var(--accent-contrast);
    font-weight: 600;
    font-size: 0.87rem;
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius-sm);
    transition: background 0.15s ease;
}

.new-chat-btn:hover { background: var(--accent-strong); }

.sidebar-section-label {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-faint);
    padding: 1.1rem 0.5rem 0.4rem;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-height: 0;
}

.conversation-empty {
    color: var(--text-faint);
    font-size: 0.82rem;
    padding: 0.5rem;
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    width: 100%;
    text-align: left;
    padding: 0.55rem 0.6rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    color: var(--text-muted);
}

.conversation-item:hover {
    background: var(--surface);
    color: var(--text);
}

.conversation-item.active {
    background: var(--accent-soft);
    color: var(--text);
}

.conversation-item .dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--border-strong);
    flex-shrink: 0;
}

.conversation-item.active .dot { background: var(--accent); }

.conversation-item .title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conversation-item .delete-btn {
    opacity: 0;
    color: var(--text-faint);
    padding: 0.15rem;
    border-radius: 4px;
    flex-shrink: 0;
}

.conversation-item:hover .delete-btn { opacity: 1; }
.conversation-item .delete-btn:hover { color: var(--danger); background: var(--danger-soft); }

.sidebar-footer {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    padding-top: 0.8rem;
    border-top: 1px solid var(--border);
    margin-top: 0.6rem;
}

.sidebar-link-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.6rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    color: var(--text-muted);
}

.sidebar-link-btn:hover { background: var(--surface); color: var(--text); }

.theme-toggle {
    display: flex;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px;
}

.theme-option {
    flex: 1;
    padding: 0.35rem 0;
    border-radius: 999px;
    font-size: 0.78rem;
    color: var(--text-muted);
    transition: background 0.15s ease, color 0.15s ease;
}

.theme-option.active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
    font-weight: 600;
}

.user-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.2rem 0.4rem;
}

.user-name {
    font-size: 0.82rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ghost-btn {
    color: var(--text-faint);
    font-size: 0.8rem;
    padding: 0.25rem 0.4rem;
    border-radius: 6px;
}

.ghost-btn:hover { color: var(--text); background: var(--surface); }

/* ---------- Chat main ---------- */

.chat-main {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--bg);
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 1.1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.chat-header h1 {
    font-family: var(--font-display);
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-toggle-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    flex-shrink: 0;
}

.sidebar-toggle-btn:hover { background: var(--surface); color: var(--text); }

.sidebar-backdrop {
    display: none;
}

.signal-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.messages-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
}

.welcome-state {
    max-width: 34rem;
    margin: auto;
    text-align: center;
    padding: 2rem 1rem;
}

.welcome-mark {
    display: inline-flex;
    color: var(--accent);
    margin-bottom: 0.75rem;
}

.welcome-state h2 {
    font-family: var(--font-display);
    font-size: 1.55rem;
    font-weight: 600;
    margin: 0 0 0.5rem;
    letter-spacing: -0.01em;
}

.welcome-state p {
    color: var(--text-muted);
    margin: 0 0 1.5rem;
}

.suggestion-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
    justify-content: center;
}

.suggestion-chip {
    border: 1px solid var(--border-strong);
    background: var(--surface);
    padding: 0.5rem 0.9rem;
    border-radius: 999px;
    font-size: 0.82rem;
    color: var(--text);
    transition: border-color 0.15s ease, background 0.15s ease;
}

.suggestion-chip:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.message-row {
    display: flex;
    max-width: 40rem;
    width: 100%;
    margin: 0 auto;
}

.message-row.user { justify-content: flex-end; }
.message-row.assistant { justify-content: flex-start; }

.message-bubble {
    max-width: 85%;
    padding: 0.75rem 1rem;
    border-radius: var(--radius-md);
    white-space: pre-wrap;
    font-size: 0.92rem;
}

.message-row.user .message-bubble {
    background: var(--accent);
    color: var(--accent-contrast);
    border-bottom-right-radius: 4px;
}

.message-row.assistant .message-bubble {
    background: var(--surface);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message-row.error .message-bubble {
    background: var(--danger-soft);
    border: 1px solid transparent;
    color: var(--danger);
}

.message-sources {
    margin-top: 0.6rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--text-muted);
}

.connecting-row {
    display: flex;
    max-width: 40rem;
    width: 100%;
    margin: 0 auto;
    justify-content: flex-start;
}

.connecting-bars {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 16px;
    padding: 0.75rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    border-bottom-left-radius: 4px;
}

.connecting-bars span {
    width: 3px;
    background: var(--accent);
    border-radius: 2px;
    animation: signal-pulse 1.1s ease-in-out infinite;
}

.connecting-bars span:nth-child(1) { height: 40%; animation-delay: 0s; }
.connecting-bars span:nth-child(2) { height: 100%; animation-delay: 0.15s; }
.connecting-bars span:nth-child(3) { height: 65%; animation-delay: 0.3s; }
.connecting-bars span:nth-child(4) { height: 85%; animation-delay: 0.45s; }

@keyframes signal-pulse {
    0%, 100% { transform: scaleY(0.4); opacity: 0.6; }
    50% { transform: scaleY(1); opacity: 1; }
}

.chat-input-form {
    display: flex;
    align-items: flex-end;
    gap: 0.6rem;
    margin: 0 1.5rem 1.5rem;
    padding: 0.6rem 0.6rem 0.6rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    max-width: 40rem;
    width: calc(100% - 3rem);
    margin-left: auto;
    margin-right: auto;
}

.chat-input-form textarea {
    flex: 1;
    border: none;
    background: none;
    resize: none;
    max-height: 8.5rem;
    padding: 0.4rem 0;
    line-height: 1.4;
}

.chat-input-form textarea:focus-visible { outline: none; }

.send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--accent-contrast);
    flex-shrink: 0;
    transition: background 0.15s ease;
}

.send-btn:hover { background: var(--accent-strong); }

/* ---------- Knowledge panel ---------- */

.knowledge-panel {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    justify-content: flex-end;
}

.knowledge-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(16, 16, 18, 0.4);
}

.knowledge-sheet {
    position: relative;
    width: 100%;
    max-width: 420px;
    height: 100%;
    background: var(--surface);
    border-left: 1px solid var(--border);
    padding: 1.5rem;
    overflow-y: auto;
    box-shadow: var(--shadow-md);
}

.knowledge-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.knowledge-header h2 {
    font-family: var(--font-display);
    font-size: 1.1rem;
    margin: 0;
}

.knowledge-sub {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin: 0.4rem 0 1.25rem;
}

.upload-form {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.25rem;
}

.upload-form input[type="file"] {
    flex: 1;
    font-size: 0.8rem;
    min-width: 0;
}

.documents-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.documents-list li {
    padding: 0.6rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.doc-status {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--text-muted);
}

.doc-status.failed { color: var(--danger); }
.doc-status.done { color: var(--accent-strong); }

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar-toggle-btn { display: flex; }

    .sidebar {
        position: fixed;
        inset: 0 22% 0 0;
        z-index: 25;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: var(--shadow-md);
    }

    .sidebar.open { transform: translateX(0); }

    .sidebar-backdrop.open {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 20;
        background: rgba(16, 16, 18, 0.4);
    }

    .knowledge-sheet { max-width: 100%; }
}
