* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #6B4423;
    --secondary-color: #A67C52;
    --bg-light: #F5F1ED;
    --text-color: #505050;
    --accent-color: #D4A574;
    --border-color: #E0D5CC;
}

body {
    font-family: 'Microsoft YaHei', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
    background: var(--bg-light);
    height: 100vh;
    overflow: hidden;
    color: var(--text-color);
}

/* 开屏动画 */
#splashScreen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeOut 0.5s ease 2s forwards;
}

@keyframes fadeOut {
    to {
        opacity: 0;
        pointer-events: none;
    }
}

.splash-content {
    text-align: center;
}

.splash-title {
    font-size: 32px;
    color: #999;
    font-weight: 300;
    margin-bottom: 10px;
    letter-spacing: 2px;
}

.splash-subtitle {
    font-size: 14px;
    color: #bbb;
    font-weight: 300;
}

/* 顶部标题栏 */
header {
    background: var(--primary-color);
    color: white;
    padding: 12px 20px;
    padding-top: max(12px, env(safe-area-inset-top));  /* 🆕 适配刘海屏 */
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

header h1 {
    font-size: 18px;
    font-weight: 500;
    letter-spacing: 1px;
}

.header-buttons {
    display: flex;
    gap: 12px;
}

.header-btn {
    background: rgba(255,255,255,0.2);
    color: white;
    border: none;
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-btn:hover {
    background: rgba(255,255,255,0.3);
}

.header-btn svg {
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.1));
}

/* 聊天区域 */
#chatContainer {
    height: calc(100vh - 56px - env(safe-area-inset-top));
    display: flex;
    flex-direction: column;
    padding: 12px;  /* ✅ 从 20px 改成 12px,左右边距更小 */
}

#messages {
    flex: 1;
    overflow-y: auto;
    margin-bottom: 15px;
}

.welcome-message {
    text-align: center;
    color: #999;
    padding: 40px 20px;
    line-height: 1.8;
}

.message {
    margin-bottom: 15px;
    display: flex;
    gap: 6px;  /* ✅ 从 10px 改成 6px,头像靠边一点 */
    align-items: flex-start;
}

.message.user {
    justify-content: flex-end;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #ddd;
    flex-shrink: 0;
    object-fit: cover;
}

.message-bubble {
    max-width: 85%;  /* ✅ 从 70% 改成 85% */
    padding: 12px 16px;
    border-radius: 16px;
    background: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.08);
    line-height: 1.6;
    white-space: pre-wrap;
}

.message.user .message-bubble {
    background: var(--secondary-color);
    color: white;
}

#typingIndicator {
    padding: 8px 15px;
    text-align: center;
    color: var(--secondary-color);
    font-size: 13px;
}

#typingIndicator.hidden {
    display: none;
}

#typingIndicator span::after {
    content: '...';
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

#inputArea {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

#userInput {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--border-color);
    border-radius: 20px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 100px;
    overflow-y: auto;
    line-height: 1.5;
}

#userInput:focus {
    border-color: var(--secondary-color);
}

#sendBtn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 25px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
    flex-shrink: 0;
}

#sendBtn:hover {
    background: var(--secondary-color);
}

/* 阅读器 */
#reader {
    position: fixed;
    top: 0;  /* 🆕 改成 0，顶到状态栏 */
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
    z-index: 100;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s;
}

#reader.reader-hidden {
    transform: translateY(100%);
}

#reader.reader-hidden {
    transform: translateY(100%);
}

#readerHeader {
    background: var(--primary-color);
    color: white;
    padding: 12px 15px;
    padding-top: max(12px, env(safe-area-inset-top));  /* 🆕 加这行 */
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    gap: 10px;
}

.reader-header-left {
    display: flex;
    gap: 8px;
}

#readerHeader button {
    background: rgba(255,255,255,0.2);
    color: white;
    border: none;
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s;
}

#readerHeader button:hover {
    background: rgba(255,255,255,0.3);
}

#bookTitle {
    font-weight: 500;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
}

.reader-controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

.reader-controls button {
    display: flex;
    align-items: center;
    justify-content: center;
}

#bookContent {
    flex: 1;
    padding: 30px 20px;
    overflow-y: auto;
    font-size: 16px;
    line-height: 1.9;
    color: #505050;
    white-space: pre-wrap;
    position: relative;
    user-select: none;  /* 🆕 防止长按选中文字 */
}

#readerFooter {
    padding: 12px 15px;
    background: var(--bg-light);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.page-display {
    text-align: center;
    margin-bottom: 8px;
    font-size: 13px;
    color: var(--text-color);
}

.page-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
}

.page-controls button {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    white-space: nowrap;
}

.page-controls button:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.page-jump {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
}

.page-jump span {
    color: var(--text-color);
}

.page-jump input {
    width: 50px;
    padding: 6px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    text-align: center;
    font-size: 12px;
}

.page-jump button {
    padding: 6px 10px;
}

/* 阅读器悬浮球 */
#readerFloatBall {
    position: fixed;
    bottom: 80px;
    right: 20px;
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    box-shadow: 0 4px 12px rgba(107, 68, 35, 0.3);
    cursor: pointer;
    z-index: 150;
    transition: all 0.3s;
    user-select: none;
}

#readerFloatBall:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(107, 68, 35, 0.4);
}

#readerFloatBall:active {
    transform: scale(0.95);
}

#readerFloatBall.hidden {
    display: none;
}

/* 书架面板 */
#libraryPanel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

#libraryPanel.hidden {
    display: none;
}

#bookList {
    max-height: 400px;
    overflow-y: auto;
    margin-bottom: 20px;
}

.book-item {
    background: #f9f9f9;
    padding: 15px;
    margin-bottom: 10px;
    border-radius: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.book-info {
    flex: 1;
    min-width: 0;
}

.book-info h4 {
    margin-bottom: 5px;
    color: var(--text-color);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.book-info p {
    font-size: 12px;
    color: #999;
}

.book-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.book-actions button {
    padding: 6px 12px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
}

.btn-open {
    background: var(--primary-color);
    color: white;
}

.btn-delete {
    background: #ff6b6b;
    color: white;
}

.empty-library {
    text-align: center;
    padding: 40px 20px;
    color: #999;
}

/* 设置/记忆库面板 */
#settingsPanel,
#memoryPanel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

#settingsPanel.hidden,
#memoryPanel.hidden {
    display: none;
}

.panel-content {
    background: white;
    padding: 25px;
    border-radius: 15px;
    max-width: 600px;
    width: 100%;
    max-height: 85vh;
    overflow: hidden;  /* 🆕 改为 hidden,因为内部会有独立滚动区域 */
    display: flex;
    flex-direction: column;
}

.panel-content h2 {
    margin-bottom: 20px;
    color: var(--primary-color);
    font-size: 20px;
}

.settings-section {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

.settings-section:last-of-type {
    border-bottom: none;
}

.settings-section h3 {
    margin-bottom: 12px;
    font-size: 15px;
    color: var(--text-color);
}

.settings-section h4 {
    margin-top: 15px;
    margin-bottom: 8px;
    font-size: 14px;
    color: #666;
}

.panel-content label {
    display: block;
    margin-bottom: 5px;
    font-size: 13px;
    color: #666;
    font-weight: 500;
}

.panel-content input,
.panel-content textarea,
.panel-content select {
    width: 100%;
    padding: 10px 12px;
    margin-bottom: 10px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

.panel-content input:focus,
.panel-content textarea:focus,
.panel-content select:focus {
    border-color: var(--secondary-color);
}

.panel-content textarea {
    min-height: 80px;
    resize: vertical;
}

.panel-content small {
    display: block;
    margin-top: -8px;
    margin-bottom: 10px;
}

.small-btn {
    background: var(--bg-light);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    padding: 8px 12px;  /* ✅ 从 8px 15px 改成 8px 12px */
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;  /* ✅ 从 13px 改成 12px */
    margin-bottom: 10px;
    transition: all 0.2s;
    white-space: nowrap;  /* ✅ 新增：强制不换行 */
}

.small-btn:hover {
    background: var(--border-color);
}

.button-group {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.primary-btn {
    flex: 1;
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}

.primary-btn:hover {
    background: var(--secondary-color);
}

.secondary-btn {
    background: #999;
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

.secondary-btn:hover {
    background: #888;
}

/* 记忆库 */
.memory-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--border-color);
}

.tab-btn {
    background: none;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    font-size: 14px;
    color: #666;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
}

.tab-btn:hover {
    color: var(--secondary-color);
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
    font-weight: 600;
}

.tab-content {
    display: none;
    padding: 20px 0;
    min-height: 200px;  /* 🆕 确保即使内容少也有最小高度 */
}

.tab-content.active {
    display: block;
}

.empty-memory {
    text-align: center;
    padding: 40px 20px;
    color: #999;
}

/* 🆕 记忆库面板布局 */
#memoryPanel .panel-content {
    position: relative;
}

#memoryPanel h2 {
    flex-shrink: 0;  /* 标题固定 */
}

.memory-tabs {
    flex-shrink: 0;  /* 标签栏固定 */
}

#memoryContent {
    flex: 1;
    overflow-y: auto;  /* 只有这部分可以滚动 */
    overflow-x: hidden;
    margin: 0 -25px;  /* 抵消父容器的 padding */
    padding: 0 25px;
}

/* 🆕 设置面板布局 */
#settingsPanel .panel-content {
    position: relative;
}

#settingsPanel h2 {
    flex-shrink: 0;  /* 标题固定 */
}

#settingsPanel .settings-content {
    flex: 1;
    overflow-y: auto;  /* 只有这部分可以滚动 */
    overflow-x: hidden;
    margin: 0 -25px;  /* 抵消父容器的 padding */
    padding: 0 25px;
}

#settingsPanel .button-group {
    flex-shrink: 0;  /* 底部按钮固定 */
    margin-top: 15px;
}

.manual-summary-section {
    flex-shrink: 0;  /* 底部固定 */
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

#memoryPanel .secondary-btn {
    flex-shrink: 0;  /* 关闭按钮固定 */
    margin-top: 15px;
}

.memory-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 20px;
    font-size: 12px;
}

.memory-table th,
.memory-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    word-break: break-all;  /* 🆕 允许换行 */
}

.memory-table th:first-child,
.memory-table td:first-child {
    min-width: 60px;  /* 🆕 给第一列最小宽度 */
}

.memory-table th {
    background: var(--bg-light);
    font-weight: 600;
    color: var(--text-color);
    font-size: 12px;
}

.memory-table tr:hover {
    background: var(--bg-light);
}

/* 手动总结范围选择 */

.manual-summary-section h3 {
    font-size: 15px;
    color: var(--text-color);
    margin-bottom: 12px;
}

.page-range-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 15px;
}

.page-range-selector input {
    width: 80px;
    padding: 8px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    text-align: center;
}

.page-range-selector span {
    color: #999;
}

/* 处理中提示 */
#processingHint {
    position: fixed;
    top: 70px;
    right: 20px;
    background: rgba(255, 255, 255, 0.95);
    color: #333;
    padding: 8px 15px;
    border-radius: 20px;
    font-size: 12px;
    z-index: 150;
    animation: pulse 2s infinite;
}

#processingHint.hidden {
    display: none;
}

@keyframes pulse {
    0%, 100% { opacity: 0.8; }
    50% { opacity: 1; }
}

/* 滚动条美化 */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-light);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--secondary-color);
}

/* 响应式调整 */
@media (max-width: 768px) {
    .panel-content {
        padding: 20px;
    }
    
    .memory-table {
        font-size: 11px;
    }
    
    .memory-table th,
    .memory-table td {
        padding: 8px 6px;
    }
}

/* 翻页按钮 */
.page-nav-btn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 16px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.page-nav-btn:hover {
    background: var(--secondary-color);
}

.page-nav-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.page-nav-btn svg {
    pointer-events: none;
}