/* ── Full-width override for library pages ───────────────────── */

body:has(.library-container) .header-content {
    max-width: 100%;
}

.main-content:has(.library-container) {
    max-width: 100%;
    padding: 0;
    padding-bottom: 0;
    margin: 0;
    align-items: stretch;
    justify-content: stretch;
}

/* ── Library layout ───────────────────────────────────────────── */

.library-container {
    display: flex;
    width: 100%;
    height: calc(100vh - 60px);
    overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────── */

.library-sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

.sidebar-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-btn {
    background: var(--accent-soft);
    border: none;
    color: var(--accent-light);
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    transition: background 0.15s;
}
.sidebar-btn:hover { background: var(--accent); color: #fff; }

.sidebar-search {
    padding: 8px 12px;
}

.sidebar-search input {
    width: 100%;
    padding: 6px 10px;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-size: 0.82rem;
    outline: none;
}
.sidebar-search input:focus { border-color: var(--accent); }
.sidebar-search input::placeholder { color: var(--text-muted); }

.sidebar-tree {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

/* ── Tree items ──────────────────────────────────────────────── */

.tree-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 5px 12px;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--text);
    transition: background 0.1s;
    user-select: none;
}
.tree-item:hover { background: var(--bg-hover); }

.tree-book.active > .tree-item,
.tree-chapter.active > .tree-item,
.tree-page.active > .tree-item {
    background: var(--accent-soft);
    color: var(--accent-light);
}

.tree-toggle {
    width: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 0.65rem;
    color: var(--text-muted);
    transition: transform 0.15s;
}
.tree-toggle.open { transform: rotate(90deg); }
.tree-toggle:hover { color: var(--text); }

.tree-icon {
    font-size: 0.9rem;
    flex-shrink: 0;
    margin-right: 4px;
}

.tree-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.chapter-item { padding-left: 8px; }
.page-item { padding-left: 8px; }

.tree-loading, .tree-error, .tree-empty {
    padding: 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.82rem;
}

/* ── Main content area ───────────────────────────────────────── */

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

.main-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.library-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.content-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    min-height: 46px;
}

.breadcrumb {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.82rem;
    color: var(--text-muted);
    overflow: hidden;
}

.crumb {
    display: flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
}
.crumb.clickable { cursor: pointer; }
.crumb.clickable:hover { color: var(--accent-light); }

.crumb-sep {
    font-size: 0.7rem;
    color: var(--text-muted);
    opacity: 0.5;
}

.content-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.action-btn {
    background: var(--accent-soft);
    border: none;
    color: var(--accent-light);
    padding: 5px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.78rem;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: background 0.15s;
}
.action-btn:hover { background: var(--accent); color: #fff; }
.action-btn.danger { color: #e07070; background: rgba(224,112,112,0.12); }
.action-btn.danger:hover { background: #c04040; color: #fff; }

/* ── Summary banner ──────────────────────────────────────────── */

.summary-banner {
    padding: 10px 20px;
    background: var(--accent-soft);
    border-bottom: 1px solid var(--border);
    font-size: 0.82rem;
    color: var(--text);
}

/* ── Page editor ─────────────────────────────────────────────── */

.page-editor-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 16px 24px;
    gap: 8px;
    overflow: hidden;
}

.page-title-input {
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 1.4rem;
    font-weight: 600;
    padding: 4px 0;
    outline: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.15s;
}
.page-title-input:focus { border-bottom-color: var(--accent); }
.page-title-input::placeholder { color: var(--text-muted); }

.page-editor {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-family: 'Inter', system-ui, sans-serif;
    font-size: 0.9rem;
    line-height: 1.7;
    resize: none;
    outline: none;
    padding: 8px 0;
}
.page-editor::placeholder { color: var(--text-muted); }

.editor-status {
    font-size: 0.72rem;
    color: var(--text-muted);
    min-height: 16px;
}

/* ── Item list (for book/chapter views) ──────────────────────── */

.item-list-wrap {
    flex: 1;
    padding: 16px 24px;
    overflow-y: auto;
}

.view-hint {
    color: var(--text-muted);
    font-size: 0.85rem;
    text-align: center;
    margin-top: 40px;
}

/* ── Chat panel (right side) ─────────────────────────────────── */

.chat-panel {
    width: 340px;
    min-width: 340px;
    background: var(--panel);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: width 0.2s, min-width 0.2s;
}

.chat-panel.collapsed {
    width: 40px;
    min-width: 40px;
}
.chat-panel.collapsed .chat-messages,
.chat-panel.collapsed .chat-input-wrap,
.chat-panel.collapsed .chat-title { display: none; }

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    min-height: 46px;
}

.chat-title {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--accent-light);
    display: flex;
    align-items: center;
    gap: 6px;
}

.chat-toggle {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 4px;
}
.chat-toggle:hover { color: var(--text); }

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

.chat-welcome {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.82rem;
    margin-top: 40px;
}

.chat-msg {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.chat-msg.user .msg-role { color: var(--accent-light); }
.chat-msg.assistant .msg-role { color: #b090d0; }
.chat-msg.msg-error .msg-text { color: #e07070; }

.msg-role {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.msg-text {
    font-size: 0.84rem;
    line-height: 1.55;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
}

.msg-page-created {
    margin-top: 6px;
    padding: 6px 10px;
    background: var(--accent-soft);
    border-radius: 6px;
    font-size: 0.78rem;
    color: var(--accent-light);
    display: flex;
    align-items: center;
    gap: 6px;
}
.msg-page-created a {
    color: var(--accent-light);
    text-decoration: underline;
    cursor: pointer;
}

.typing-dots span {
    animation: typingBlink 1.2s infinite;
    font-size: 1.2rem;
    line-height: 1;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBlink {
    0%, 60%, 100% { opacity: 0.3; }
    30% { opacity: 1; }
}

.chat-input-wrap {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
}

.chat-input {
    flex: 1;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: 0.84rem;
    padding: 8px 10px;
    resize: none;
    outline: none;
    font-family: inherit;
    line-height: 1.4;
    max-height: 120px;
}
.chat-input:focus { border-color: var(--accent); }
.chat-input::placeholder { color: var(--text-muted); }

.chat-send {
    background: var(--accent);
    border: none;
    color: #fff;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    flex-shrink: 0;
    transition: background 0.15s;
}
.chat-send:hover { background: var(--accent-light); }
.chat-send:disabled { opacity: 0.5; cursor: default; }

/* ── Responsive ──────────────────────────────────────────────── */

@media (max-width: 900px) {
    .chat-panel { width: 280px; min-width: 280px; }
}

@media (max-width: 700px) {
    .library-sidebar {
        width: 220px;
        min-width: 220px;
    }
    .chat-panel { width: 240px; min-width: 240px; }
    .page-editor-wrap {
        padding: 12px 16px;
    }
}
