/* ========== CSS Variables ========== */
:root {
    --bg: #f5f5f5;
    --bg-card: #fff;
    --text: #333;
    --text-secondary: #666;
    --text-muted: #999;
    --border: #e8e8e8;
    --primary: #1890ff;
    --primary-hover: #40a9ff;
    --shadow: 0 1px 4px rgba(0,0,0,0.08);
    --radius: 8px;
    --max-width: 960px;
    --reader-bg: #f9f3e8;
    --reader-text: #333;
    --reader-bottom-height: 56px;
    --reader-safe-bottom: env(safe-area-inset-bottom, 0px);
    --reader-bottom-offset: calc(var(--reader-bottom-height) + var(--reader-safe-bottom));
}

.night-mode {
    --bg: #1a1a1a;
    --bg-card: #2a2a2a;
    --text: #ccc;
    --text-secondary: #999;
    --text-muted: #666;
    --border: #333;
    --shadow: 0 1px 4px rgba(0,0,0,0.3);
    --reader-bg: #1a1a1a;
    --reader-text: #b0b0b0;
}

.reader-theme-paper {
    --reader-bg: #f9f3e8;
    --reader-text: #333;
}

.reader-theme-green {
    --reader-bg: #eaf3e2;
    --reader-text: #2e3a2c;
}

.reader-theme-dark {
    --reader-bg: #1a1a1a;
    --reader-text: #b0b0b0;
}

/* ========== Anti-copy ========== */
body {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
/* 保留表单元素的选中能力 */
input, textarea, select {
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
}

/* ========== Reset ========== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    -webkit-text-size-adjust: 100%;
}

a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    color: var(--primary-hover);
}

img {
    max-width: 100%;
    height: auto;
}

ul {
    list-style: none;
}

/* ========== Layout ========== */
.container {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 16px;
}

/* ========== Header ========== */
.site-header {
    background: var(--bg-card);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-inner {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
    flex-shrink: 0;
}

.search-box {
    display: flex;
    flex: 1;
    max-width: 400px;
}

.search-box input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-right: none;
    border-radius: var(--radius) 0 0 var(--radius);
    font-size: 14px;
    background: var(--bg);
    color: var(--text);
    outline: none;
}

.search-box input:focus {
    border-color: var(--primary);
}

.search-box button {
    padding: 8px 16px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 0 var(--radius) var(--radius) 0;
    cursor: pointer;
    font-size: 14px;
    white-space: nowrap;
}

.search-box button:hover {
    background: var(--primary-hover);
}

/* ========== Category Nav ========== */
.category-nav {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 0 16px 8px;
    display: flex;
    gap: 4px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.category-nav::-webkit-scrollbar {
    display: none;
}

.cat-link {
    padding: 4px 12px;
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    border-radius: 16px;
    transition: background 0.2s;
}

.cat-link:hover,
.cat-link.active {
    background: var(--primary);
    color: #fff;
}

/* ========== Section Title ========== */
.section-title {
    font-size: 20px;
    font-weight: 600;
    margin: 16px 0 12px;
    color: var(--text);
}

/* ========== Book List ========== */
.book-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* ========== Book Card ========== */
.book-card {
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    transition: box-shadow 0.2s;
}

.book-card:hover {
    box-shadow: 0 2px 12px rgba(0,0,0,0.12);
}

.book-card-link {
    display: flex;
    padding: 12px;
    gap: 12px;
    color: var(--text);
}

.book-cover {
    flex-shrink: 0;
    width: 80px;
    height: 107px;
    border-radius: 4px;
    overflow: hidden;
    background: var(--border);
}

.cover-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.cover-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary), #722ed1);
    color: #fff;
    font-size: 28px;
    font-weight: 700;
}

.cover-placeholder.large {
    width: 120px;
    height: 160px;
    font-size: 40px;
}

.book-info {
    flex: 1;
    min-width: 0;
}

.book-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.book-author {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.book-meta {
    display: flex;
    gap: 8px;
    margin-bottom: 6px;
}

.book-category,
.book-status {
    font-size: 12px;
    padding: 1px 8px;
    border-radius: 10px;
    background: var(--bg);
    color: var(--text-muted);
}

.book-intro {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-latest {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ========== Book Detail ========== */
.book-detail {
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 16px;
}

.detail-header {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
}

.detail-cover {
    flex-shrink: 0;
}

.detail-cover img {
    width: 120px;
    height: 160px;
    object-fit: cover;
    border-radius: 4px;
}

.detail-info {
    flex: 1;
}

.detail-info h1 {
    font-size: 20px;
    margin-bottom: 8px;
}

.detail-author {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.detail-meta {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}

.tag {
    font-size: 12px;
    padding: 2px 10px;
    border-radius: 12px;
    background: var(--bg);
    color: var(--text-muted);
}

.detail-latest {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.detail-actions {
    display: flex;
    gap: 10px;
}

.btn {
    display: inline-block;
    padding: 8px 20px;
    border-radius: var(--radius);
    font-size: 14px;
    text-align: center;
    cursor: pointer;
    border: none;
    transition: background 0.2s;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
    color: #fff;
}

.btn-outline {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-secondary);
}

.btn-outline:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.detail-intro {
    border-top: 1px solid var(--border);
    padding-top: 12px;
}

.detail-intro h2 {
    font-size: 16px;
    margin-bottom: 8px;
}

.detail-intro p {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.8;
    white-space: pre-line;
}

/* ========== Chapter Section ========== */
.chapter-section {
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.section-header h2 {
    font-size: 16px;
}

.view-all {
    font-size: 13px;
    color: var(--text-muted);
}

.chapter-count {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 12px;
}

.chapter-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
}

.chapter-grid li {
    border-bottom: 1px solid var(--border);
}

.chapter-grid li:last-child {
    border-bottom: none;
}

.chapter-grid a {
    display: block;
    padding: 10px 4px;
    font-size: 14px;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chapter-grid a:hover {
    color: var(--primary);
}

/* ========== Reader ========== */
.reader-body {
    padding-bottom: calc(var(--reader-bottom-offset) + 12px);
}

.reader-body.reader-lock-scroll {
    overflow: hidden;
}

.reader-header {
    background: var(--bg-card);
    padding: 12px 16px;
    box-shadow: var(--shadow);
}

.back-link {
    font-size: 14px;
    color: var(--text-secondary);
}

.reader-container {
    max-width: 720px;
    margin: 0 auto;
    padding: 16px;
}

.reader-content {
    background: var(--reader-bg);
    border-radius: var(--radius);
    padding: 20px 16px;
    margin-bottom: 16px;
}

.chapter-title {
    font-size: 20px;
    text-align: center;
    margin-bottom: 20px;
    color: var(--reader-text);
}

.chapter-text {
    font-size: 18px;
    line-height: 1.9;
    color: var(--reader-text);
    overflow-wrap: anywhere;
    word-break: break-word;
}

.chapter-text p {
    text-indent: 2em;
    margin-bottom: var(--reader-paragraph-gap, 0.8em);
}

/* ========== Chapter Nav ========== */
.chapter-nav {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin: 16px 0;
}

.nav-btn {
    padding: 10px 20px;
    background: var(--bg-card);
    border-radius: var(--radius);
    font-size: 14px;
    color: var(--text);
    box-shadow: var(--shadow);
    text-align: center;
}

.nav-btn:hover {
    color: var(--primary);
}

.nav-btn.disabled {
    color: var(--text-muted);
    pointer-events: none;
}

/* ========== Bottom Bar ========== */
.reader-bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-around;
    align-items: center;
    gap: 4px;
    min-height: var(--reader-bottom-height);
    padding: 6px 8px;
    z-index: 100;
    padding-bottom: calc(6px + var(--reader-safe-bottom));
    transition: transform 0.2s ease;
}

.reader-bottom-bar.is-hidden {
    transform: translateY(110%);
}

.bar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-width: 0;
    min-height: 44px;
    padding: 8px 6px;
    font-size: 14px;
    color: var(--text-secondary);
    background: none;
    border: none;
    cursor: pointer;
    text-align: center;
    border-radius: var(--radius);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.bar-btn:hover {
    color: var(--primary);
}

.bar-btn.disabled {
    color: var(--text-muted);
    pointer-events: none;
}

/* ========== Settings Panel ========== */
.settings-panel {
    position: fixed;
    bottom: var(--reader-bottom-offset);
    left: 0;
    right: 0;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    padding: 16px;
    z-index: 99;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.08);
}

.settings-panel.is-hidden {
    display: none;
}

/* ========== TOC Drawer ========== */
.toc-drawer {
    position: fixed;
    inset: 0;
    z-index: 140;
}

.toc-drawer.is-hidden {
    display: none;
}

.toc-mask {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.42);
}

.toc-panel {
    position: absolute;
    top: 0;
    right: 0;
    width: min(88vw, 420px);
    max-width: 420px;
    height: 100%;
    background: var(--bg-card);
    box-shadow: -4px 0 18px rgba(0,0,0,0.16);
    display: flex;
    flex-direction: column;
}

.toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
}

.toc-title {
    font-size: 16px;
    color: var(--text);
}

.toc-close {
    min-height: 36px;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-secondary);
    cursor: pointer;
}

.toc-search {
    padding: 10px 14px 8px;
}

.toc-search input {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    outline: none;
}

.toc-search input:focus {
    border-color: var(--primary);
}

.toc-summary {
    padding: 0 14px 10px;
    font-size: 12px;
    color: var(--text-muted);
}

.toc-list-wrap {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 14px 14px;
}

.toc-list a {
    display: block;
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.toc-list a.active {
    color: var(--primary);
    background: rgba(24, 144, 255, 0.09);
    font-weight: 600;
}

.toc-list a:hover {
    color: var(--primary);
}

.toc-empty {
    text-align: center;
    padding: 18px 8px;
    color: var(--text-muted);
    font-size: 13px;
}

.toc-empty.is-hidden {
    display: none;
}

.toc-load-more {
    width: 100%;
    margin-top: 12px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-secondary);
    cursor: pointer;
}

.toc-load-more.is-hidden {
    display: none;
}

.settings-inner {
    max-width: 400px;
    margin: 0 auto;
}

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
}

.setting-row span {
    font-size: 14px;
    color: var(--text);
    flex-shrink: 0;
}

.font-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.font-controls button {
    width: 44px;
    height: 44px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    touch-action: manipulation;
}

.font-controls button:hover {
    border-color: var(--primary);
    color: var(--primary);
}

#fontSizeDisplay {
    font-size: 14px;
    min-width: 24px;
    text-align: center;
}

#lineHeightDisplay,
#paragraphDisplay {
    font-size: 14px;
    min-width: 32px;
    text-align: center;
}

.theme-controls {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
}

.theme-btn {
    min-height: 36px;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 13px;
}

.theme-btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.reader-toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--reader-bottom-offset) + 14px);
    transform: translate(-50%, 12px);
    opacity: 0;
    pointer-events: none;
    transition: all 0.2s ease;
    z-index: 120;
    padding: 8px 14px;
    border-radius: 16px;
    background: rgba(0,0,0,0.72);
    color: #fff;
    font-size: 13px;
    white-space: nowrap;
    max-width: calc(100vw - 32px);
    overflow: hidden;
    text-overflow: ellipsis;
}

.reader-toast.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}


/* ========== Load More ========== */
.load-more {
    text-align: center;
    padding: 20px 0;
    color: var(--text-muted);
    font-size: 14px;
}

.load-more-spinner::before {
    content: "";
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    vertical-align: middle;
    margin-right: 6px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ========== Pagination ========== */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    margin: 20px 0;
}

.page-btn {
    padding: 8px 16px;
    background: var(--bg-card);
    border-radius: var(--radius);
    font-size: 14px;
    color: var(--text);
    box-shadow: var(--shadow);
}

.page-btn:hover {
    color: var(--primary);
}

.page-btn.disabled {
    color: var(--text-muted);
    pointer-events: none;
}

.page-info {
    font-size: 13px;
    color: var(--text-muted);
}

/* ========== Empty State ========== */
.empty-state {
    text-align: center;
    padding: 40px 16px;
    color: var(--text-muted);
    font-size: 15px;
}

/* ========== Error Page ========== */
.error-page {
    text-align: center;
    padding: 60px 16px;
}

.error-page h1 {
    font-size: 72px;
    color: var(--text-muted);
    margin-bottom: 16px;
}

.error-page p {
    font-size: 16px;
    color: var(--text-secondary);
    margin-bottom: 24px;
}

/* ========== Footer ========== */
.site-footer {
    text-align: center;
    padding: 20px 16px;
    font-size: 12px;
    color: var(--text-muted);
    border-top: 1px solid var(--border);
    margin-top: 20px;
}

.footer-links {
    margin-top: 10px;
}

.footer-links a {
    color: var(--text-secondary);
    margin: 0 8px;
}

.legal-page {
    padding: 16px;
    line-height: 1.8;
    color: var(--text-primary);
}

.legal-page h1 {
    margin-bottom: 16px;
}

.legal-page h2 {
    margin: 20px 0 10px;
    font-size: 18px;
}

.legal-page p {
    margin-bottom: 12px;
}

.legal-page ul {
    margin: 0 0 14px 20px;
}

/* ========== Desktop (min-width: 768px) ========== */
@media (min-width: 768px) {
    .book-list {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
    }

    .chapter-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .reader-content {
        padding: 32px 24px;
    }

    .chapter-text {
        font-size: 18px;
    }
}

/* ========== Reader Mobile (max-width: 767px) ========== */
@media (max-width: 767px) {
    .reader-container {
        padding: 12px;
    }

    .chapter-title {
        font-size: 18px;
        margin-bottom: 16px;
    }

    .chapter-nav {
        display: none;
    }

    .setting-row {
        align-items: flex-start;
    }

    .theme-controls {
        justify-content: flex-start;
    }

    .toc-panel {
        width: 100%;
        max-width: none;
    }
}

/* ========== Large Desktop (min-width: 1024px) ========== */
@media (min-width: 1024px) {
    .book-list {
        grid-template-columns: repeat(3, 1fr);
    }

    .chapter-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}
