/* ========== Reset & Base ========== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --radius: 8px;
    --radius-sm: 4px;
    --transition: all 0.25s ease;
    --sidebar-width: 280px;
    --toolbar-height: 60px;
    --font-mono: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', Consolas, monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
}

/* ========== Theme: Dark (Default) ========== */
[data-theme="dark"] {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --bg-card: #1e2746;
    --bg-hover: #253255;
    --text-primary: #e8e8e8;
    --text-secondary: #a0aec0;
    --text-muted: #636e7f;
    --accent: #e94560;
    --accent-hover: #ff6b81;
    --accent-secondary: #0f3460;
    --border-color: #2a3a5c;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    --code-color: #e6c384;
    --strong-color: #f0e6d3;
    --em-color: #d4c5a9;
}

/* ========== Theme: Green Eye-Care ========== */
[data-theme="green"] {
    --bg-primary: #e8f5e9;
    --bg-secondary: #dcedc8;
    --bg-tertiary: #c8e6c9;
    --bg-card: #f1f8e9;
    --bg-hover: #d0e0d0;
    --text-primary: #2e4632;
    --text-secondary: #4a5d4e;
    --text-muted: #7a8b7d;
    --accent: #2e7d32;
    --accent-hover: #388e3c;
    --accent-secondary: #a5d6a7;
    --border-color: #b8d0b8;
    --shadow: 0 4px 20px rgba(46, 125, 50, 0.15);
    --code-color: #558b2f;
    --strong-color: #1b5e20;
    --em-color: #33691e;
}

/* ========== Theme: Paper ========== */
[data-theme="paper"] {
    --bg-primary: #faf8f5;
    --bg-secondary: #f0ece6;
    --bg-tertiary: #e8e0d5;
    --bg-card: #ffffff;
    --bg-hover: #e5ddd0;
    --text-primary: #3d3229;
    --text-secondary: #6b5f52;
    --text-muted: #9a8f80;
    --accent: #c0392b;
    --accent-hover: #e74c3c;
    --accent-secondary: #d4a373;
    --border-color: #d9d0c4;
    --shadow: 0 4px 20px rgba(61, 50, 41, 0.1);
    --code-color: #b7410e;
    --strong-color: #2c1810;
    --em-color: #5d4037;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    line-height: 1.6;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* ========== Scrollbar ========== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ========== Toolbar ========== */
.toolbar {
    height: var(--toolbar-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    flex-shrink: 0;
    z-index: 100;
}

.app-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 1px;
}

.book-title-display {
    font-size: 14px;
    color: var(--text-muted);
    margin-left: 12px;
    padding-left: 12px;
    border-left: 1px solid var(--border-color);
    font-weight: 400;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.toolbar-left,
.toolbar-center,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.toolbar-center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

/* ========== Theme Switch ========== */
.theme-switch {
    display: flex;
    background: var(--bg-primary);
    border-radius: var(--radius);
    padding: 3px;
    border: 1px solid var(--border-color);
    gap: 2px;
}

.theme-btn {
    padding: 6px 10px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    border-radius: 6px;
    transition: var(--transition);
    font-family: var(--font-sans);
    font-weight: 500;
    line-height: 1;
}

.theme-btn:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.theme-btn.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

/* ========== Mode Switch ========== */
.mode-switch {
    display: flex;
    background: var(--bg-primary);
    border-radius: var(--radius);
    padding: 3px;
    border: 1px solid var(--border-color);
}

.mode-btn {
    padding: 6px 20px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    border-radius: 6px;
    transition: var(--transition);
    font-family: var(--font-sans);
    font-weight: 500;
}

.mode-btn:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.mode-btn.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 2px 8px rgba(233, 69, 96, 0.3);
}

/* ========== Buttons ========== */
.btn {
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    font-family: var(--font-sans);
    white-space: nowrap;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
    box-shadow: 0 2px 10px rgba(233, 69, 96, 0.4);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-save {
    background: #27ae60;
    color: #fff;
}

.btn-save:hover {
    background: #2ecc71;
    box-shadow: 0 2px 10px rgba(39, 174, 96, 0.4);
}

.btn-refresh {
    background: #2980b9;
    color: #fff;
}

.btn-refresh:hover {
    background: #3498db;
    box-shadow: 0 2px 10px rgba(41, 128, 185, 0.4);
}

.btn-refresh:active {
    transform: scale(0.96);
}

.btn-danger {
    background: #c0392b;
    color: #fff;
}

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

.btn-icon {
    padding: 6px 8px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    font-size: 14px;
    opacity: 0;
}

.chapter-item:hover .btn-icon {
    opacity: 1;
}

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

.btn-icon.danger:hover {
    color: var(--accent);
}

/* ========== Main Container ========== */
.main-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ========== Sidebar ========== */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

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

.sidebar-header h2 {
    font-size: 16px;
    font-weight: 600;
}

.chapter-count {
    font-size: 12px;
    color: var(--text-muted);
    background: var(--bg-primary);
    padding: 2px 10px;
    border-radius: 12px;
}

.chapter-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chapter-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    margin-bottom: 2px;
    gap: 8px;
    user-select: none;
}

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

.chapter-item.active {
    background: var(--bg-tertiary);
    border-left: 3px solid var(--accent);
}

.chapter-item.active .chapter-name {
    color: var(--text-primary);
    font-weight: 600;
}

.chapter-number {
    color: var(--text-muted);
    font-size: 12px;
    min-width: 20px;
    text-align: right;
    font-family: var(--font-mono);
}

.chapter-name {
    flex: 1;
    font-size: 14px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chapter-item-actions {
    display: flex;
    gap: 2px;
    margin-left: auto;
}

/* ========== Content Area ========== */
.content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-primary);
}

.chapter-title-bar {
    padding: 16px 24px 12px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.chapter-title-input {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 22px;
    font-weight: 700;
    font-family: var(--font-sans);
    outline: none;
    padding: 4px 0;
}

.chapter-title-input::placeholder {
    color: var(--text-muted);
}

/* ========== Editor Panel ========== */
.editor-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.editor-textarea {
    flex: 1;
    width: 100%;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 16px;
    line-height: 1.9;
    padding: 24px 32px;
    font-family: var(--font-sans);
    resize: none;
    outline: none;
    overflow-y: auto;
    letter-spacing: 0.3px;
}

.editor-textarea::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.editor-statusbar {
    display: flex;
    gap: 20px;
    padding: 8px 24px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    font-size: 12px;
    color: var(--text-muted);
    font-family: var(--font-mono);
    flex-shrink: 0;
}

/* ========== Preview Panel ========== */
.preview-panel {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px;
}

.preview-content {
    max-width: 820px;
    margin: 0 auto;
    min-height: 100%;
    font-size: 17px;
    line-height: 1.9;
    letter-spacing: 0.3px;
}

.preview-placeholder {
    color: var(--text-muted);
    text-align: center;
    padding: 60px 0;
    font-size: 16px;
}

/* ========== Preview Markdown Styles ========== */
.preview-content h1 {
    font-size: 28px;
    font-weight: 700;
    margin: 24px 0 16px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--border-color);
    color: var(--text-primary);
}

.preview-content h2 {
    font-size: 22px;
    font-weight: 600;
    margin: 20px 0 12px;
    color: var(--text-primary);
}

.preview-content h3 {
    font-size: 18px;
    font-weight: 600;
    margin: 16px 0 10px;
    color: var(--text-primary);
}

.preview-content h4,
.preview-content h5,
.preview-content h6 {
    font-size: 16px;
    font-weight: 600;
    margin: 14px 0 8px;
    color: var(--text-primary);
}

.preview-content p {
    margin: 10px 0;
    line-height: 1.8;
    font-size: 16px;
}

.preview-content strong {
    font-weight: 700;
    color: var(--strong-color);
}

.preview-content em {
    font-style: italic;
    color: var(--em-color);
}

.preview-content blockquote {
    border-left: 4px solid var(--accent);
    padding: 10px 16px;
    margin: 14px 0;
    background: var(--bg-card);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    color: var(--text-secondary);
    font-style: italic;
}

.preview-content blockquote p {
    margin: 4px 0;
}

.preview-content ul,
.preview-content ol {
    margin: 10px 0;
    padding-left: 28px;
}

.preview-content li {
    margin: 4px 0;
    line-height: 1.7;
}

.preview-content hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 24px 0;
}

.preview-content code {
    background: var(--bg-card);
    padding: 2px 8px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 14px;
    color: var(--code-color);
}

.preview-content pre {
    background: var(--bg-card);
    padding: 16px 20px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 14px 0;
    border: 1px solid var(--border-color);
}

.preview-content pre code {
    background: none;
    padding: 0;
    border-radius: 0;
    color: inherit;
}

.preview-content a {
    color: var(--accent);
    text-decoration: none;
}

.preview-content a:hover {
    text-decoration: underline;
}

.preview-content img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin: 14px 0;
}

.preview-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 14px 0;
}

.preview-content th,
.preview-content td {
    padding: 10px 14px;
    border: 1px solid var(--border-color);
    text-align: left;
}

.preview-content th {
    background: var(--bg-card);
    font-weight: 600;
}

.preview-content tr:nth-child(even) {
    background: rgba(255, 255, 255, 0.02);
}

/* ========== Modal ========== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    width: 400px;
    max-width: 90%;
    box-shadow: var(--shadow);
}

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

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

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 22px;
    cursor: pointer;
    padding: 0 4px;
    transition: var(--transition);
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

.modal-body p {
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.6;
}

.modal-input {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    font-family: var(--font-sans);
    outline: none;
    transition: var(--transition);
}

.modal-input:focus {
    border-color: var(--accent);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 14px 20px;
    border-top: 1px solid var(--border-color);
}

/* ========== Responsive ========== */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 220px;
    }

    .toolbar {
        padding: 0 12px;
    }

    .app-title {
        font-size: 15px;
    }

    .toolbar-center {
        position: static;
        transform: none;
    }

    .mode-btn {
        padding: 5px 12px;
        font-size: 13px;
    }

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

    .chapter-title-input {
        font-size: 18px;
    }

    .editor-textarea {
        font-size: 14px;
        padding: 14px 16px;
    }

    .preview-panel {
        padding: 14px 16px;
    }

    .preview-content h1 {
        font-size: 22px;
    }

    .preview-content h2 {
        font-size: 18px;
    }

    .preview-content p {
        font-size: 14px;
    }
}

@media (max-width: 600px) {
    :root {
        --sidebar-width: 180px;
    }

    .sidebar-header h2 {
        font-size: 14px;
    }

    .chapter-name {
        font-size: 13px;
    }

    .btn-icon {
        opacity: 1;
        padding: 4px 6px;
    }

    .editor-statusbar {
        font-size: 11px;
        gap: 12px;
        padding: 6px 16px;
    }
}

/* ========== Animations ========== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.preview-content {
    animation: fadeIn 0.3s ease;
}

.chapter-item {
    animation: fadeIn 0.2s ease;
}

/* ========== Empty State ========== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    gap: 12px;
}

.empty-state-icon {
    font-size: 48px;
    opacity: 0.5;
}

.empty-state-text {
    font-size: 14px;
}

/* ========== Word Count Highlight ========== */
.editor-statusbar span {
    position: relative;
}

.editor-statusbar span::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: middle;
}

.editor-statusbar span:nth-child(1)::before {
    background: var(--accent);
}

.editor-statusbar span:nth-child(2)::before {
    background: #3498db;
}

.editor-statusbar span:nth-child(3)::before {
    background: #2ecc71;
}
