/* 阅读·魔改助手 —— 深色主题 */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --bg: #14161a;
    --panel: #1c1f26;
    --panel-2: #232732;
    --border: #313746;
    --text: #d7dce4;
    --text-dim: #8b93a3;
    --accent: #4f8cff;
    --accent-dim: #2c4a80;
    --good: #3fb26f;
    --bad: #e05d5d;
}
html, body { height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font: 14px/1.7 -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.hidden { display: none !important; }

/* 头部 */
.app-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.app-title { font-size: 16px; font-weight: 600; }
.header-right { display: flex; gap: 10px; align-items: center; }
.link-button { color: var(--text-dim); text-decoration: none; font-size: 13px; }
.link-button:hover { color: var(--text); }
select, input[type="text"], input[type="number"], textarea {
    background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
    border-radius: 6px; padding: 6px 8px; font-size: 13px; font-family: inherit;
}
textarea { width: 100%; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 1px solid var(--accent-dim); }

/* 布局 */
.app-layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
    width: 240px; flex-shrink: 0; background: var(--panel);
    border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.sidebar-head {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 12px; font-weight: 600; border-bottom: 1px solid var(--border);
}
.book-list { flex: 1; overflow-y: auto; padding: 6px; }
.book-item {
    padding: 8px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 4px;
}
.book-item:hover { background: var(--panel-2); }
.book-item.active { background: var(--accent-dim); }
.book-item .name { display: flex; align-items: center; gap: 6px; }
.book-item .meta { font-size: 12px; color: var(--text-dim); }
.badge {
    font-size: 11px; padding: 1px 6px; border-radius: 4px;
    background: #6b4f1d; color: #ffd479;
}
.badge.created { background: #1d4f3a; color: #7fe0b0; }
.import-button { margin: 10px; }

/* 按钮 */
.primary-button {
    background: var(--accent); color: #fff; border: none; border-radius: 8px;
    padding: 8px 14px; font-size: 13px; cursor: pointer;
}
.primary-button:hover { filter: brightness(1.1); }
.primary-button:disabled { background: var(--accent-dim); cursor: not-allowed; }
.mini-button {
    background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
    border-radius: 6px; padding: 4px 10px; font-size: 12px; cursor: pointer;
}
.mini-button:hover { border-color: var(--accent); }
.row-buttons { display: flex; gap: 8px; margin-top: 8px; align-items: center; }

/* 中间栏 */
.main-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.empty-state { margin: auto; text-align: center; color: var(--text-dim); }
.empty-state .hint { font-size: 13px; margin-top: 8px; }
.book-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.book-toolbar {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 14px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.book-title { font-size: 15px; font-weight: 600; }
.book-title-wrap { display: flex; align-items: center; gap: 8px; }
.kb-chip { font-size: 12px; color: var(--text-dim); }
.book-body { flex: 1; display: flex; min-height: 0; }
.toc-panel {
    width: 260px; flex-shrink: 0; overflow-y: auto; padding: 8px;
    border-right: 1px solid var(--border); background: var(--panel);
}
.toc-panel.collapsed { display: none; }
.toc-volume > summary {
    cursor: pointer; padding: 6px 8px; font-weight: 600; color: var(--text-dim);
    list-style: none;
}
.toc-volume > summary::before { content: '▸ '; }
.toc-volume[open] > summary::before { content: '▾ '; }
.toc-chapter {
    padding: 4px 10px 4px 22px; cursor: pointer; border-radius: 6px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px;
}
.toc-chapter:hover { background: var(--panel-2); }
.toc-chapter.active { background: var(--accent-dim); color: #fff; }
.toc-chapter .draft-dot { color: #ffd479; margin-left: 4px; }
.content-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chapter-nav {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 20px; border-bottom: 1px solid var(--border);
}
.chapter-title { font-weight: 600; }
.chapter-title-wrap { display: flex; align-items: center; gap: 8px; min-width: 0; }
.bookmark-btn.marked { border-color: #ffd479; color: #ffd479; background: rgba(255, 212, 121, .08); }
.toc-bookmarks { margin-bottom: 8px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.toc-bookmarks > summary { cursor: pointer; padding: 6px 8px; font-weight: 600; color: #ffd479; list-style: none; }
.toc-bookmarks > summary::before { content: '▸ '; }
.toc-bookmarks[open] > summary::before { content: '▾ '; }
.bookmark-row {
    display: flex; align-items: center; gap: 6px; padding: 4px 8px 4px 18px;
    border-radius: 6px; font-size: 13px; cursor: pointer;
}
.bookmark-row:hover { background: var(--panel-2); }
.bookmark-row .bm-main { flex: 1; min-width: 0; overflow: hidden; }
.bookmark-row .bm-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bookmark-row .bm-note {
    font-size: 12px; color: var(--text-dim);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bookmark-row .bm-del { flex-shrink: 0; padding: 2px 7px; }
.toc-chapter .bm-mark { color: #ffd479; margin-right: 4px; }
.chapter-content {
    flex: 1; overflow-y: auto; padding: 24px 48px; font-size: 16px; line-height: 2;
    max-width: 100%;
}
.chapter-content p { margin-bottom: 1em; text-indent: 2em; }
.chapter-content .doc-view { text-indent: 0; white-space: pre-wrap; font-size: 14px; }

/* 右侧助手面板 */
.assist-panel {
    width: 400px; flex-shrink: 0; background: var(--panel);
    border-left: 1px solid var(--border); display: flex; flex-direction: column;
}
.tab-bar { display: flex; border-bottom: 1px solid var(--border); }
.tab-button {
    flex: 1; background: none; border: none; color: var(--text-dim);
    padding: 10px 4px; cursor: pointer; font-size: 13px;
    border-bottom: 2px solid transparent;
}
.tab-button.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-page { flex: 1; display: none; flex-direction: column; min-height: 0; padding: 12px; overflow-y: auto; }
.tab-page.active { display: flex; }
.panel-hint { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; }
.current-chapter-label { font-weight: 600; margin-bottom: 8px; font-size: 13px; }
.sub-block { margin-bottom: 18px; }
.sub-block input[type="text"] { width: 100%; margin-bottom: 6px; }
.sub-title { font-weight: 600; margin-bottom: 6px; }

/* 对话 */
#tab-chat { display: none; }
#tab-chat.active { display: flex; }
.chat-session-row { display: flex; gap: 6px; margin-bottom: 8px; align-items: center; }
.chat-session-row select { flex: 1; min-width: 0; }
.chat-history { flex: 1; overflow-y: auto; margin-bottom: 10px; min-height: 120px; }
.chat-msg { margin-bottom: 10px; }
.chat-msg .who { font-size: 12px; color: var(--text-dim); margin-bottom: 2px; }
.chat-msg .bubble {
    background: var(--panel-2); border-radius: 10px; padding: 8px 12px;
    white-space: pre-wrap; word-break: break-word; font-size: 13px;
}
.chat-msg.user .bubble { background: var(--accent-dim); }
.chat-input-row textarea { margin-bottom: 6px; }

/* 流式输出 */
.stream-output {
    margin-top: 10px; background: var(--panel-2); border: 1px solid var(--border);
    border-radius: 8px; padding: 10px; white-space: pre-wrap; font-size: 13px;
    max-height: 45vh; overflow-y: auto;
}
.refine-box { margin-top: 8px; }
.refine-history { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.refine-history div { padding: 1px 0; }
.refine-row { display: flex; gap: 6px; align-items: flex-end; }
.refine-row textarea { flex: 1; }
.drafts-head { font-weight: 600; margin: 14px 0 6px; }
.draft-item {
    background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
    padding: 8px 10px; margin-bottom: 6px; font-size: 12px;
}
.draft-item .draft-label { font-weight: 600; font-size: 13px; }
.draft-item .row-buttons { margin-top: 6px; }

/* 知识库 */
.kb-form label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-dim); }
.kb-form input[type="number"] { width: 70px; }
.checkbox-label input { vertical-align: middle; }
.kb-status { font-size: 13px; margin-top: 8px; min-height: 20px; }
.kb-status.error { color: var(--bad); }
.kb-status.done { color: var(--good); }
.progress-track {
    height: 8px; background: var(--panel-2); border-radius: 4px; margin-top: 6px; overflow: hidden;
}
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.kb-doc-list .kb-doc {
    padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.kb-doc-list .kb-doc:hover { background: var(--panel-2); }

/* 导入向导 */
.modal-mask {
    position: fixed; inset: 0; background: rgba(0,0,0,.55);
    display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal {
    width: min(860px, 94vw); max-height: 88vh; background: var(--panel);
    border: 1px solid var(--border); border-radius: 12px;
    display: flex; flex-direction: column;
}
.modal-head {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.modal-body { padding: 14px 16px; overflow-y: auto; display: flex; flex-direction: column; min-height: 0; }
.preview-toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.preview-toolbar input[type="text"] { width: 260px; }
.range-label input { width: 70px; }
.preview-list {
    flex: 1; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px;
    min-height: 200px; max-height: 46vh;
}
.preview-volume > summary {
    padding: 6px 10px; background: var(--panel-2); cursor: pointer; font-weight: 600;
    position: sticky; top: 0;
}
.preview-row {
    display: flex; align-items: center; gap: 8px; padding: 3px 10px;
    border-bottom: 1px solid rgba(255,255,255,.04); font-size: 13px;
}
.preview-row input[type="text"] { flex: 1; padding: 3px 6px; }
.preview-row .chars { width: 72px; color: var(--text-dim); text-align: right; font-size: 12px; }
.modal-foot {
    display: flex; justify-content: flex-end; gap: 12px; align-items: center;
    padding-top: 10px;
}

.drawer-toggle { display: none; }
.nav-mask { display: none; }

@media (max-width: 768px) {
    body { overflow: hidden; }
    .app-header {
        flex-wrap: wrap;
        gap: 8px;
        padding: 8px 10px;
        padding-top: max(8px, env(safe-area-inset-top));
    }
    .header-left, .header-right {
        gap: 8px;
        min-width: 0;
    }
    .header-right { flex-wrap: wrap; justify-content: flex-end; }
    .app-title { font-size: 14px; white-space: nowrap; }
    .drawer-toggle { display: inline-flex; min-height: 36px; }
    .header-right select { max-width: 42vw; }

    .nav-mask {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 35;
    }
    .nav-mask.hidden { display: none !important; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(300px, 86vw);
        z-index: 40;
        transform: translateX(-105%);
        transition: transform 0.22s ease;
        padding-top: max(10px, env(safe-area-inset-top));
    }
    body.books-open .sidebar { transform: translateX(0); }

    .toc-panel {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 20;
        width: min(280px, 80vw);
        box-shadow: 14px 0 32px rgba(0, 0, 0, 0.35);
    }

    .assist-panel {
        position: fixed;
        inset: 0;
        width: 100%;
        z-index: 40;
        transform: translateY(105%);
        transition: transform 0.22s ease;
        padding-top: max(8px, env(safe-area-inset-top));
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }
    body.assist-open .assist-panel { transform: translateY(0); }

    .chapter-content { padding: 16px 16px 32px; }
    .chapter-nav { padding: 8px 10px; gap: 6px; flex-wrap: wrap; }
    .chapter-title { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .book-toolbar { padding: 8px 10px; gap: 8px; flex-wrap: wrap; }
    .modal { width: 100%; max-height: 94dvh; border-radius: 0; }
    select, input[type="text"], input[type="number"], textarea { font-size: 16px; }
    .primary-button, .mini-button { min-height: 36px; }
}
