﻿/* ── Reset ──────────────────────────────────────────────────────── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* ── Tokens / design system ────────────────────────────────────── */
:root {
    --accent: #5b6ef5;
    --accent-dark: #4a5ce0;
    --accent-dim: #eef0fe;
    --accent-ring: #c7cafc;
    --green: #22a06b;
    --red: #e5334b;

    --bg: #f0f2f5;
    --surface: #ffffff;
    --border: #e8eaed;
    --border-mid: #d0d5dd;

    --text-primary: #1a1d23;
    --text-secondary: #475467;
    --text-muted: #8b92a5;
    --text-faint: #b0b7c9;

    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, .06), 0 1px 2px rgba(0, 0, 0, .04);
    --shadow: 0 4px 12px rgba(0, 0, 0, .07), 0 1px 3px rgba(0, 0, 0, .04);

    --sidebar-w: 248px;
    --panel-w: 340px;
    --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

/* ── Base ───────────────────────────────────────────────────────── */
html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.55;
    color: var(--text-primary);
    background: var(--bg);
}

/* ── App shell ──────────────────────────────────────────────────── */
.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr var(--panel-w);
    height: 100vh;
}

/* ─────────────────────────────────────────────────────────────────
   SIDEBAR
───────────────────────────────────────────────────────────────── */
.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 16px 16px;
    overflow-y: auto;
    gap: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 28px;
    flex-shrink: 0;
}

.brand-icon {
    width: 36px;
    height: 36px;
    background: var(--accent);
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 800;
    font-size: 12px;
    letter-spacing: -.5px;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(91, 110, 245, .35);
}

.brand-name {
    font-weight: 700;
    font-size: 15px;
    color: var(--text-primary);
    letter-spacing: -.2px;
}

.brand-sub {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 1px;
}

.sidebar-label {
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 10px;
    flex-shrink: 0;
}

/* Book list */
.book-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-bottom: 14px;
    min-height: 40px;
}

.book-item {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background .12s, border-color .12s;
    border: 1px solid transparent;
    user-select: none;
}

.book-item:hover {
    background: #f5f6fc;
}

.book-item.checked {
    background: var(--accent-dim);
    border-color: var(--accent-ring);
}

.book-item input[type=checkbox] {
    accent-color: var(--accent);
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    margin-top: 3px;
    cursor: pointer;
}

.book-item-info {
    flex: 1;
    min-width: 0;
}

.book-item-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-item-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* Apply button */
.btn-primary {
    width: 100%;
    padding: 9px 0;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, transform .1s;
    flex-shrink: 0;
    margin-bottom: 8px;
}

.btn-primary:hover {
    background: var(--accent-dark);
}

.btn-primary:active {
    transform: scale(.98);
}

.btn-primary.applied {
    background: var(--green);
}

.btn-primary:disabled {
    opacity: .6;
    cursor: not-allowed;
}

.status-text {
    font-size: 11px;
    color: var(--text-muted);
    text-align: center;
    flex-shrink: 0;
}

/* Selection feedback badge */
#selection-feedback {
    min-height: 18px;
    margin-bottom: 8px;
    transition: background .15s, color .15s, border-color .15s, opacity .15s;
}

#selection-feedback.show {
    padding: 6px 8px;
    border-radius: 8px;
    background: var(--accent-dim);
    color: var(--text-primary);
    border: 1px solid var(--border-mid);
    box-shadow: var(--shadow-sm);
    animation: popin .16s ease-out;
}

#selection-feedback.add {
    background: rgba(34, 160, 107, .14);
    color: var(--green);
    border-color: var(--green);
}

#selection-feedback.remove {
    background: rgba(229, 51, 75, .16);
    color: var(--red);
    border-color: var(--red);
}

#selection-feedback.add::before {
    content: '✓ ';
    font-weight: 800;
}

#selection-feedback.remove::before {
    content: '– ';
    font-weight: 800;
}

@keyframes popin {
    from {
        transform: scale(.98);
        opacity: .85;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

/* ─────────────────────────────────────────────────────────────────
   MAIN AREA
───────────────────────────────────────────────────────────────── */
.main {
    display: flex;
    flex-direction: column;
    background: var(--bg);
    min-width: 0;
    min-height: 0;
    transition: background .2s;
}

/* Top header bar */
.qa-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 46px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    font-weight: 600;
    font-size: 14px;
    letter-spacing: -.1px;
    transition: background .2s, border-color .2s;
}

.chat-header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Stream badge */
.stream-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 500;
    color: var(--accent);
    background: var(--accent-dim);
    padding: 3px 10px;
    border-radius: 999px;
}

.stream-dot {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: 50%;
    animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
        transform: scale(1);
    }

    50% {
        opacity: .4;
        transform: scale(.75);
    }
}

.hidden {
    display: none !important;
}

/* Dark mode toggle */
.btn-icon {
    width: 30px;
    height: 30px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    transition: background .15s, color .15s, border-color .15s;
    padding: 0;
}

.btn-icon:hover {
    background: var(--accent-dim);
    color: var(--accent);
}

.btn-icon svg {
    width: 15px;
    height: 15px;
}

/* ── Q&A input section ─────────────────────────────────────────── */
.qa-input-section {
    flex-shrink: 0;
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    transition: background .2s, border-color .2s;
}

#qa-form {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

#qa-input {
    flex: 1;
    padding: 10px 14px;
    font-size: 14px;
    font-family: var(--font);
    line-height: 1.55;
    color: var(--text-primary);
    background: var(--bg);
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    resize: none;
    outline: none;
    transition: border-color .15s, box-shadow .15s, background .2s;
}

#qa-input::placeholder {
    color: var(--text-faint);
}

#qa-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(91, 110, 245, .12);
}

.btn-ask {
    padding: 0 22px;
    height: 42px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s, transform .1s, opacity .15s;
}

.btn-ask:hover {
    background: var(--accent-dark);
}

.btn-ask:active {
    transform: scale(.97);
}

.btn-ask:disabled {
    opacity: .45;
    cursor: not-allowed;
    background: var(--accent);
}

/* ── Q&A answer body ───────────────────────────────────────────── */
.qa-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    min-height: 0;
}

.qa-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 14px;
    max-width: 380px;
    margin: 60px auto 0;
    color: var(--text-muted);
}

.qa-empty svg {
    width: 40px;
    height: 40px;
    opacity: .3;
}

.qa-empty p {
    font-size: 13.5px;
    line-height: 1.65;
}

/* Q&A result */
#qa-result {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.qa-q-label,
.qa-a-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.qa-question {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.5;
    padding: 12px 16px;
    background: var(--accent-dim);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-sm);
    white-space: pre-wrap;
    word-break: break-word;
}

.qa-answer {
    font-size: 14px;
    line-height: 1.75;
    color: var(--text-primary);
    word-break: break-word;
    padding: 14px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    min-height: 80px;
    transition: background .2s, border-color .2s;
}

/* ── Markdown content inside qa-answer ──────────────────────────── */
.qa-answer h1,
.qa-answer h2,
.qa-answer h3,
.qa-answer h4,
.qa-answer h5,
.qa-answer h6 {
    margin: 18px 0 8px;
    font-weight: 700;
    line-height: 1.35;
    color: var(--text-primary);
}

.qa-answer h1 {
    font-size: 1.3em;
}

.qa-answer h2 {
    font-size: 1.15em;
}

.qa-answer h3 {
    font-size: 1.05em;
}

.qa-answer h4 {
    font-size: 1em;
}

.qa-answer h1:first-child,
.qa-answer h2:first-child,
.qa-answer h3:first-child {
    margin-top: 0;
}

.qa-answer p {
    margin: 0 0 10px;
}

.qa-answer p:last-child {
    margin-bottom: 0;
}

.qa-answer ul,
.qa-answer ol {
    margin: 6px 0 12px;
    padding-left: 22px;
}

.qa-answer li {
    margin-bottom: 4px;
}

.qa-answer li>ul,
.qa-answer li>ol {
    margin-top: 4px;
    margin-bottom: 4px;
}

.qa-answer strong {
    font-weight: 700;
    color: var(--text-primary);
}

.qa-answer em {
    font-style: italic;
}

.qa-answer code {
    font-family: 'Menlo', 'Consolas', monospace;
    font-size: 0.88em;
    padding: 2px 5px;
    background: var(--accent-dim);
    border-radius: 4px;
}

.qa-answer pre {
    background: var(--accent-dim);
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}

.qa-answer pre code {
    padding: 0;
    background: none;
}

.qa-answer blockquote {
    border-left: 3px solid var(--accent);
    margin: 8px 0;
    padding: 6px 14px;
    color: var(--text-muted);
}

.qa-answer hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 14px 0;
}

.qa-answer table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 13px;
}

.qa-answer th,
.qa-answer td {
    border: 1px solid var(--border-mid);
    padding: 7px 10px;
    text-align: left;
}

.qa-answer th {
    background: var(--accent-dim);
    font-weight: 600;
}

.qa-answer a {
    color: var(--accent);
    text-decoration: underline;
}

.qa-answer.streaming::after {
    content: '\25C4';
    color: var(--accent);
    animation: blink .7s step-end infinite;
    margin-left: 2px;
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

/* Thinking dots */
.thinking-dots {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
}

.thinking-dots span {
    display: block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: dot .8s infinite;
}

.thinking-dots span:nth-child(2) {
    animation-delay: .15s;
}

.thinking-dots span:nth-child(3) {
    animation-delay: .30s;
}

@keyframes dot {

    0%,
    80%,
    100% {
        transform: scale(.65);
        opacity: .4;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ─────────────────────────────────────────────────────────────────
   RIGHT PANEL
───────────────────────────────────────────────────────────────── */
.panel {
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background .2s, border-color .2s;
}

.panel-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    border-bottom: 1px solid var(--border);
}

.panel-section:last-child {
    border-bottom: none;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--text-muted);
    background: var(--surface);
    transition: background .2s, border-color .2s;
}

.badge {
    font-size: 11px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--accent-dim);
    color: var(--accent);
    text-transform: none;
    letter-spacing: 0;
}

.panel-body {
    overflow-y: auto;
    flex: 1;
    padding: 12px;
}

.empty-state {
    color: var(--text-faint);
    font-size: 12px;
    text-align: center;
    padding: 20px 8px;
    line-height: 1.6;
}

/* ── Reasoning items ───────────────────────────────────────────── */
.r-book {
    margin-bottom: 14px;
}

.r-book-name {
    font-size: 11px;
    font-weight: 700;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: .04em;
    margin-bottom: 7px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.r-book-name::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--accent);
    border-radius: 50%;
}

.r-depth {
    margin-bottom: 6px;
    padding: 8px 10px;
    background: var(--surface-raised, #f8f9fb);
    border-radius: var(--radius-sm);
    border-left: 2px solid var(--accent-ring);
}

.r-depth-label {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.r-thinking {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.r-nodes {
    margin-top: 7px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.r-node-tag {
    background: var(--accent-dim);
    color: var(--accent);
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 700;
    font-family: monospace;
}

/* ── Chunk cards ───────────────────────────────────────────────── */
.chunk-card {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
    overflow: hidden;
    transition: box-shadow .15s, border-color .2s;
}

.chunk-card:hover {
    box-shadow: var(--shadow-sm);
}

.chunk-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 7px 10px;
    background: var(--surface-raised, #f8f9fb);
    border-bottom: 1px solid var(--border);
    gap: 6px;
    transition: background .2s;
}

.chunk-source {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-primary);
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chunk-meta {
    display: flex;
    gap: 5px;
    flex-shrink: 0;
}

.chunk-page,
.chunk-idx {
    font-size: 10px;
    font-weight: 600;
    background: var(--accent-dim);
    color: var(--accent);
    padding: 2px 6px;
    border-radius: 4px;
}

.chunk-body {
    padding: 9px 11px;
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.55;
    white-space: pre-wrap;
    max-height: 110px;
    overflow-y: auto;
}

/* ── Dark mode ─────────────────────────────────────────────────── */
[data-theme="dark"] {
    --bg: #0f1117;
    --surface: #1a1d27;
    --border: #272b3d;
    --border-mid: #353a52;

    --text-primary: #e8eaf0;
    --text-secondary: #9ba3b8;
    --text-muted: #6b7394;
    --text-faint: #454d68;

    --accent-dim: #1e2142;
    --accent-ring: #3540a0;

    --shadow-sm: 0 1px 4px rgba(0, 0, 0, .35);
    --shadow: 0 4px 16px rgba(0, 0, 0, .45);

    color-scheme: dark;
}

[data-theme="dark"] .qa-header,
[data-theme="dark"] .panel,
[data-theme="dark"] .panel-header,
[data-theme="dark"] .qa-input-section {
    background: var(--surface);
}

[data-theme="dark"] .qa-answer {
    background: var(--surface);
}

[data-theme="dark"] .r-depth {
    background: #20243a;
}

[data-theme="dark"] .chunk-card-head {
    background: #20243a;
}

[data-theme="dark"] .book-item:hover {
    background: #20243a;
}

/* ── Scrollbar ─────────────────────────────────────────────────── */
::-webkit-scrollbar {
    width: 5px;
    height: 5px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-mid);
    border-radius: 99px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-faint);
}