/* 养老知识库 UI（基于 TalkBreed 风格）*/
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --primary: #0ea5e9;
    --primary-dark: #0284c7;
    --primary-light: #e0f2fe;
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --user-bubble: #0ea5e9;
    --radius: 14px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    font-size: 15px;
}

.hidden { display: none !important; }
.app { display: flex; flex-direction: column; height: 100vh; }

/* ===== 登录页 ===== */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
    background: var(--surface);
    padding: 40px;
    border-radius: 16px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
    text-align: center;
    max-width: 400px;
    width: 100%;
}

.login-logo {
    font-size: 48px;
    margin-bottom: 16px;
}

.login-box h1 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text);
}

.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 24px;
    font-size: 14px;
}

#login-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

#password-input {
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    outline: none;
    transition: border-color 0.2s;
}

#password-input:focus {
    border-color: var(--primary);
}

#login-btn {
    padding: 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

#login-btn:hover:not(:disabled) {
    background: var(--primary-dark);
}

#login-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.login-error {
    color: #dc2626;
    font-size: 14px;
    margin-top: -8px;
}

/* ===== 顶栏 ===== */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo { font-size: 28px; }

.topbar-left h1 {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.tagline {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.topbar-right {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 7px 14px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text);
    transition: all 0.15s;
}

.icon-btn:hover {
    background: var(--bg);
    border-color: var(--primary);
    color: var(--primary);
}

.icon-btn:active {
    transform: translateY(1px);
}

/* ===== 对话页 ===== */
.chat-page {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 28px 16px 8px;
    scroll-behavior: smooth;
}

.messages > * {
    max-width: 800px;
    margin: 0 auto 18px;
}

/* 欢迎屏 */
.welcome {
    text-align: center;
    padding: 56px 16px;
}

.welcome-emoji {
    font-size: 52px;
    margin-bottom: 14px;
}

.welcome h2 {
    font-size: 26px;
    margin-bottom: 10px;
    font-weight: 700;
}

.welcome p {
    color: var(--text-muted);
    margin-bottom: 28px;
    line-height: 1.6;
}

.suggest {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    max-width: 560px;
    margin: 0 auto;
}

.suggest button {
    padding: 11px 18px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 22px;
    cursor: pointer;
    font-size: 13.5px;
    color: var(--text);
    transition: all 0.15s;
}

.suggest button:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

/* 消息 */
.msg {
    display: flex;
    margin-bottom: 18px;
    animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

.msg.user {
    justify-content: flex-end;
}

.msg.user .bubble {
    background: var(--user-bubble);
    color: #fff;
    padding: 11px 16px;
    border-radius: 16px 16px 4px 16px;
    max-width: 72%;
    line-height: 1.55;
    word-break: break-word;
}

.msg.assistant .ai-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 18px 22px;
    width: 100%;
    line-height: 1.75;
    box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

/* AI 内容样式 */
.ai-content h1 { font-size: 20px; margin: 14px 0 8px; }
.ai-content h2 { font-size: 17px; margin: 14px 0 6px; }
.ai-content h3 { font-size: 15px; margin: 12px 0 6px; }
.ai-content p { margin: 8px 0; }
.ai-content code {
    background: var(--bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: 'SF Mono', Menlo, monospace;
}
.ai-content pre {
    background: var(--bg);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 13px;
    white-space: pre-wrap;
}
.ai-content pre code { background: none; padding: 0; }
.ai-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 13.5px;
}
.ai-content td, .ai-content th {
    border: 1px solid var(--border);
    padding: 7px 10px;
    text-align: left;
}
.ai-content th { background: var(--bg); font-weight: 600; }
.ai-content ul, .ai-content ol { margin: 8px 0 8px 22px; }
.ai-content a { color: var(--primary); text-decoration: none; }
.ai-content a:hover { text-decoration: underline; }

.typing { color: var(--text-muted); }
.status { color: var(--primary); font-size: 14px; padding: 4px 0; }
.error { color: #dc2626; }

/* 来源 chips */
.sources-row {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px dashed var(--border);
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.source-chip {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 16px;
    padding: 5px 12px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}

.source-chip:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

/* 追问参考 */
.followups-row {
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px dashed var(--border);
}

.followups-label {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.followups-chips {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.followup-chip {
    padding: 7px 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 10px;
    cursor: pointer;
    font-size: 13.5px;
    color: var(--text);
    transition: all 0.15s;
    text-align: left;
    line-height: 1.4;
}

.followup-chip:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

/* 正在阅读侧栏 */
.reading-sidebar {
    position: fixed;
    right: 0;
    top: 0;
    width: 280px;
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 16px rgba(0,0,0,0.06);
    display: flex;
    flex-direction: column;
    z-index: 100;
}

.reading-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.reading-item {
    padding: 11px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 6px;
    font-size: 13.5px;
    line-height: 1.45;
    transition: background 0.12s;
}

.reading-item:hover {
    background: var(--bg);
    color: var(--primary);
}

/* 输入区 */
.composer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 12px 16px 10px;
}

.composer-inner {
    max-width: 800px;
    margin: 0 auto;
}

.input-row {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 8px 8px 8px 14px;
    background: var(--bg);
    transition: border-color 0.15s;
}

.input-row:focus-within {
    border-color: var(--primary);
}

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

.send-btn {
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 38px;
    height: 38px;
    cursor: pointer;
    font-size: 16px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.send-btn:hover:not(:disabled) {
    background: var(--primary-dark);
}

.send-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.hint {
    font-size: 11.5px;
    color: var(--text-muted);
    text-align: center;
    margin-top: 8px;
}

/* ===== 浏览页 ===== */
.browse-page {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.browse-main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.browse-sidebar {
    width: 320px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.browse-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
}

/* Wiki 树 */
.wiki-tree {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.tree-item {
    margin-bottom: 4px;
}

.tree-dir, .tree-file {
    padding: 8px 12px;
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.12s;
    display: flex;
    align-items: center;
    gap: 8px;
}

.tree-dir:hover, .tree-file:hover {
    background: var(--bg);
}

.tree-arrow {
    font-size: 10px;
    transition: transform 0.2s;
}

.tree-name {
    font-size: 14px;
}

.tree-children {
    margin-left: 20px;
}

.tree-file {
    color: var(--text);
}

.tree-file:hover {
    color: var(--primary);
}

/* Wiki 内容 */
.browse-search {
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 10px;
}

.browse-search input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    outline: none;
}

.browse-search input:focus {
    border-color: var(--primary);
}

.browse-search button {
    padding: 10px 16px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
}

.browse-search button:hover {
    background: var(--primary-dark);
}

.search-results {
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    max-height: 300px;
    overflow-y: auto;
}

.search-result {
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 8px;
    transition: background 0.12s;
}

.search-result:hover {
    background: var(--bg);
}

.search-title {
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 4px;
}

.search-path {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.search-snippet {
    font-size: 13px;
    color: var(--text);
}

.search-empty {
    text-align: center;
    color: var(--text-muted);
    padding: 20px;
}

.wiki-content {
    flex: 1;
    overflow-y: auto;
    padding: 40px;
    max-width: 800px;
    margin: 0 auto;
}

.wiki-placeholder {
    text-align: center;
    color: var(--text-muted);
    padding: 80px 20px;
    font-size: 16px;
}

.wiki-content h1 {
    font-size: 28px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid var(--border);
}

/* 反向链接 */
.backlinks-section {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}

.backlinks-section h3 {
    font-size: 16px;
    margin-bottom: 12px;
    color: var(--text-muted);
}

.backlinks-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.backlink-item {
    padding: 10px 14px;
    background: var(--bg);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.12s;
    font-size: 14px;
}

.backlink-item:hover {
    background: var(--primary-light);
    color: var(--primary);
}

/* ===== 会话侧栏 ===== */
.sessions-sidebar {
    position: fixed;
    right: 0;
    top: 0;
    width: 320px;
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: -4px 0 16px rgba(0,0,0,0.06);
    display: flex;
    flex-direction: column;
    z-index: 100;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h3 {
    font-size: 15px;
}

.sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.session-item {
    padding: 11px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 6px;
    transition: background 0.12s;
}

.session-item:hover {
    background: var(--bg);
}

.session-title {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
}

.session-time {
    font-size: 12px;
    color: var(--text-muted);
}

.btn-danger {
    margin: 12px;
    padding: 9px;
    border: 1px solid #fecaca;
    background: #fef2f2;
    color: #dc2626;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
}

.btn-danger:hover {
    background: #fee2e2;
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 28px;
    left: 50%;
    transform: translateX(-50%);
    background: #1f2937;
    color: #fff;
    padding: 11px 22px;
    border-radius: 10px;
    z-index: 300;
    font-size: 14px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

/* ===== 滚动条 ===== */
.messages::-webkit-scrollbar,
.wiki-tree::-webkit-scrollbar,
.wiki-content::-webkit-scrollbar,
.sessions-list::-webkit-scrollbar,
.reading-list::-webkit-scrollbar {
    width: 8px;
}

.messages::-webkit-scrollbar-thumb,
.wiki-tree::-webkit-scrollbar-thumb,
.wiki-content::-webkit-scrollbar-thumb,
.sessions-list::-webkit-scrollbar-thumb,
.reading-list::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 4px;
}

.messages::-webkit-scrollbar-thumb:hover,
.wiki-tree::-webkit-scrollbar-thumb:hover,
.wiki-content::-webkit-scrollbar-thumb:hover,
.sessions-list::-webkit-scrollbar-thumb:hover,
.reading-list::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

/* ===== 图谱页 ===== */
.graph-page {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
}

.graph-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

.graph-header h2 {
    font-size: 18px;
    font-weight: 700;
    margin: 0;
}

.graph-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.graph-hint {
    font-size: 12px;
    color: var(--text-muted);
}

.graph-legend {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
}

.graph-legend.hidden {
    display: none;
}

.graph-legend h3 {
    font-size: 13px;
    font-weight: 600;
    margin: 0;
    margin-right: 8px;
    color: var(--text-muted);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.legend-label {
    font-size: 12px;
    color: var(--text);
}

.graph-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

#graph-canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: grab;
}

#graph-canvas:active {
    cursor: grabbing;
}

.graph-tooltip {
    position: absolute;
    background: rgba(31, 41, 55, 0.95);
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    pointer-events: none;
    z-index: 100;
    white-space: nowrap;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.graph-tooltip.hidden {
    display: none;
}

/* ===== 构建浮层 ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.modal-box {
    background: var(--surface);
    border-radius: 14px;
    width: 420px;
    max-width: 92vw;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    overflow: hidden;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 {
    font-size: 16px;
    font-weight: 700;
}

.modal-body {
    padding: 18px;
}

.build-status-text {
    font-size: 14px;
    color: var(--text);
    margin-bottom: 12px;
}

.progress-bar {
    height: 8px;
    background: var(--bg);
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.progress-fill {
    height: 100%;
    width: 0;
    background: var(--primary);
    transition: width 0.3s;
}

.build-stats {
    display: flex;
    gap: 14px;
    margin-top: 10px;
    font-size: 13px;
    color: var(--text-muted);
}

.build-ok { color: #10b981; }
.build-fail { color: #ef4444; }

.build-error {
    margin-top: 10px;
    font-size: 12.5px;
    color: #dc2626;
    background: #fef2f2;
    padding: 8px 10px;
    border-radius: 6px;
    word-break: break-all;
    max-height: 120px;
    overflow-y: auto;
}

.modal-footer {
    display: flex;
    gap: 8px;
    padding: 12px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg);
}

.btn-primary {
    flex: 1;
    padding: 9px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13.5px;
    transition: background 0.15s;
}

.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
    flex: 1;
    padding: 9px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    font-size: 13.5px;
    transition: all 0.15s;
}

.btn-secondary:hover {
    border-color: var(--primary);
    color: var(--primary);
}

/* ===== 响应式 ===== */
@media (max-width: 640px) {
    .topbar {
        padding: 10px 14px;
    }

    .tagline {
        display: none;
    }

    .icon-btn {
        padding: 6px 10px;
        font-size: 12px;
    }

    .messages > * {
        padding: 0 4px;
    }

    .msg.user .bubble {
        max-width: 85%;
    }

    .browse-sidebar {
        width: 100%;
        display: none;
    }

    .browse-sidebar.show {
        display: flex;
    }

    .wiki-content {
        padding: 20px;
    }

    .sessions-sidebar {
        width: 100%;
    }

    .graph-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .graph-legend {
        padding: 10px 16px;
    }

    .graph-hint {
        display: none;
    }
}