/* --- 1. 基础变量与复位 (Modernized) --- */
:root {
    --primary-color: #1e293b;
    --accent-color: #3b82f6; /* 更明亮的蓝色 */
    --accent-soft: rgba(59, 130, 246, 0.12);
    --rose-color: #f43f5e;
    --rose-soft: rgba(244, 63, 94, 0.12);

    /* UI 可自定义（细分项） */
    --emphasis-color: #f43f5e;
    --emphasis-soft: rgba(244, 63, 94, 0.12);
    --emphasis-soft-2: rgba(244, 63, 94, 0.08);

    --analysis-color: #3b82f6;
    --analysis-bg-1: rgba(59,130,246,0.08);
    --analysis-bg-2: rgba(59,130,246,0.02);
    --analysis-border: rgba(59,130,246,0.15);
    --analysis-bar: #3b82f6;
    --analysis-title: #1d4ed8;

    --knowledge-color: #0e7490;
    --knowledge-bg-1: rgba(14,116,144,0.08);
    --knowledge-bg-2: rgba(14,116,144,0.02);
    --knowledge-border: rgba(14,116,144,0.15);
    --knowledge-bar: #0e7490;
    --knowledge-title: #0e7490;
    
    --correct-bg: #ecfdf5;
    --correct-text: #047857;
    --correct-border: #a7f3d0;
    
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #334155;
    --sidebar-width: 280px;

    --shadow-sm: 0 4px 12px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 12px 32px -4px rgba(15, 23, 42, 0.08);
    --radius-sm: 12px;
    --radius-md: 16px;
    --radius-lg: 24px;
    
    --glass-border: rgba(255, 255, 255, 0.6);
    --glass-bg: rgba(255, 255, 255, 0.75);
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
html, body {
    overflow-x: hidden;
    max-width: 100vw;
}
body {
    font-family: 'Inter', 'Segoe UI', 'Microsoft YaHei', sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.65;
    letter-spacing: -0.015em;
    /* 防止移动端长按选中文本干扰拖拽 */
    -webkit-tap-highlight-color: transparent;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
}

/* When a modal is open, prevent background scroll (home uses its own scroll container). */
body.modal-open .home-scroll,
body.modal-open .chapter-list {
    overflow: hidden !important;
}

/* ============================================
   APP (non-home) visual system (2025 refresh)
   - Home view + book opening animation must stay unchanged.
   ============================================ */
body:not(.home-mode) {
    --primary-color: #0f172a;
    --text-color: #1e293b;
    --accent-color: #6366f1;
    --accent-soft: rgba(99, 102, 241, 0.12);
    --rose-color: #f43f5e;
    --rose-soft: rgba(244, 63, 94, 0.10);

    /* App background - Refined Artistic Gradient */
    --bg-color:
        radial-gradient(1000px 1000px at 0% 0%, rgba(99, 102, 241, 0.08), transparent 60%),
        radial-gradient(800px 800px at 100% 0%, rgba(56, 189, 248, 0.08), transparent 50%),
        radial-gradient(800px 600px at 50% 100%, rgba(244, 63, 94, 0.05), transparent 50%),
        #f8fafc;

    --card-bg: rgba(255, 255, 255, 0.85);

    --shadow-sm: 0 4px 16px -2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 20px 50px -10px rgba(15, 23, 42, 0.12);

    /* App (chapter view) sizing tokens - adjusted per device breakpoints. */
    --app-main-pad: 22px;
    --app-main-pad-top: 18px;
    --app-main-pad-bottom: 22px;
    --app-topbar-gap: 16px;
    --app-topbar-pad-y: 14px;
    --app-topbar-pad-x: 20px;
    --app-topbar-title-size: 17.6px; /* ~= 1.1rem @ 16px */
    --app-topbar-title-max: 56vw;
    --app-card-pad: 24px;
    --app-card-gap: 24px;
    --app-q-id-size: 13.6px; /* ~= 0.85rem @ 16px */
    --app-q-text-size: 17.6px; /* ~= 1.1rem @ 16px */
    --app-opt-gap: 12px;
    --app-opt-pad-y: 14px;
    --app-opt-pad-x: 18px;
    --app-analysis-font-size: 15.7px; /* ~= 0.98rem @ 16px */
}

/* Active book theme -> app accent (book view only). */
body:not(.home-mode).app-theme-blue { --accent-color: #3b82f6; --accent-soft: rgba(59,130,246,0.12); }
body:not(.home-mode).app-theme-indigo { --accent-color: #6366f1; --accent-soft: rgba(99,102,241,0.12); }
body:not(.home-mode).app-theme-red { --accent-color: #e11d48; --accent-soft: rgba(225,29,72,0.12); }
body:not(.home-mode).app-theme-rose { --accent-color: #f43f5e; --accent-soft: rgba(244,63,94,0.12); }
body:not(.home-mode).app-theme-teal { --accent-color: #14b8a6; --accent-soft: rgba(20,184,166,0.12); }
body:not(.home-mode).app-theme-emerald { --accent-color: #10b981; --accent-soft: rgba(16,185,129,0.12); }
body:not(.home-mode).app-theme-sage { --accent-color: #84cc16; --accent-soft: rgba(132,204,22,0.14); }
body:not(.home-mode).app-theme-amber { --accent-color: #f59e0b; --accent-soft: rgba(245,158,11,0.14); }
body:not(.home-mode).app-theme-cocoa { --accent-color: #b45309; --accent-soft: rgba(180,83,9,0.14); }
body:not(.home-mode).app-theme-plum { --accent-color: #8b5cf6; --accent-soft: rgba(139,92,246,0.12); }
body:not(.home-mode).app-theme-slate { --accent-color: #64748b; --accent-soft: rgba(100,116,139,0.14); }
body:not(.home-mode).app-theme-midnight { --accent-color: #38bdf8; --accent-soft: rgba(56,189,248,0.12); }

body.sidebar-collapsed { --sidebar-width: 72px; }
body.sidebar-collapsed .sidebar { transform: none; width: 72px; }
body.sidebar-collapsed .chapter-list { display: none; }
body.sidebar-collapsed .brand { padding: 12px 10px; justify-content: center; flex-direction: column; gap: 12px; }
body.sidebar-collapsed .brand-text { display: none; }
body.sidebar-collapsed .brand-actions { flex-direction: column; gap: 10px; }
body.sidebar-collapsed .collapse-btn { width: 44px; height: 44px; border-radius: 16px; }
body.sidebar-collapsed .sidebar-footer { flex-direction: column; gap: 12px; padding: 12px; }
body.sidebar-collapsed .icon-btn { width: 46px; height: 46px; flex: none; border-radius: 16px; }

/* 统一的可访问焦点样式 - Refined */
:where(button, [role="button"], a, summary, input, textarea):focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 3px;
    border-radius: 8px;
    transition: outline-offset 0.2s ease;
}

@media (prefers-reduced-motion: reduce) {
    * { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}

/* --- 文本强调（题库解析/知识点用）--- */
.highlight {
    padding: 0 4px;
    border-radius: 4px;
    background: transparent;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}
.highlight--yellow { background: rgba(255, 221, 87, 0.62); }
.highlight--pink { background: rgba(255, 182, 213, 0.50); }
.highlight--orange { background: rgba(255, 207, 160, 0.55); }
.bold-em {
    font-weight: 800;
    color: var(--emphasis-color);
    letter-spacing: 0.2px;
}
.italic-em { font-style: italic; }
.underline-em {
    text-decoration: underline;
    text-decoration-thickness: 2px;
    text-underline-offset: 2px;
}

/* 解析/知识点排版增强 */
.analysis-content, .knowledge-content {
    font-size: var(--app-analysis-font-size, 0.98rem);
    line-height: 1.85;
    color: #2c3e50;
}
.analysis-content b, .knowledge-content b { font-weight: 700; }
.analysis-content a, .knowledge-content a { color: var(--accent-color); text-decoration: none; border-bottom: 1px dashed rgba(52,152,219,0.55); }
.analysis-content a:hover, .knowledge-content a:hover { border-bottom-style: solid; }
.analysis-content code, .knowledge-content code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    background: rgba(27,31,35,0.06);
    padding: 2px 6px;
    border-radius: 6px;
    font-size: 0.95em;
}
.analysis-content ul, .analysis-content ol,
.knowledge-content ul, .knowledge-content ol { padding-left: 22px; margin: 10px 0; }
.analysis-content li, .knowledge-content li { margin: 6px 0; }
.analysis-content p, .knowledge-content p { margin: 10px 0; }
.analysis-content hr, .knowledge-content hr { border: none; height: 1px; background: #e9eef5; margin: 12px 0; }
.analysis-content blockquote {
    margin: 12px 0;
    padding: 10px 12px;
    border-left: 4px solid var(--analysis-bar);
    background: var(--analysis-bg-1);
    border-radius: 0 12px 12px 0;
}
.knowledge-content blockquote {
    margin: 12px 0;
    padding: 10px 12px;
    border-left: 4px solid var(--knowledge-bar);
    background: var(--knowledge-bg-1);
    border-radius: 0 12px 12px 0;
}
.analysis-content table, .knowledge-content table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin: 12px 0;
    border: 1px solid rgba(148,163,184,0.26);
    border-radius: 14px;
    overflow: hidden;
    background: rgba(255,255,255,0.75);
}
.q-text table, .option-content table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin: 12px 0;
    border: 1px solid rgba(148,163,184,0.26);
    border-radius: 14px;
    overflow: hidden;
    background: rgba(255,255,255,0.85);
}
.analysis-content th, .analysis-content td,
.knowledge-content th, .knowledge-content td {
    padding: 9px 10px;
    border-bottom: 1px solid rgba(148,163,184,0.22);
    border-right: 1px solid rgba(148,163,184,0.18);
    vertical-align: top;
}
.q-text th, .q-text td,
.option-content th, .option-content td {
    padding: 9px 10px;
    border-bottom: 1px solid rgba(148,163,184,0.22);
    border-right: 1px solid rgba(148,163,184,0.18);
    vertical-align: top;
}
.analysis-content th:last-child, .analysis-content td:last-child,
.knowledge-content th:last-child, .knowledge-content td:last-child { border-right: 0; }
.q-text th:last-child, .q-text td:last-child,
.option-content th:last-child, .option-content td:last-child { border-right: 0; }
.analysis-content tr:last-child td,
.knowledge-content tr:last-child td { border-bottom: 0; }
.q-text tr:last-child td,
.option-content tr:last-child td { border-bottom: 0; }
.analysis-content thead th,
.knowledge-content thead th {
    background: rgba(99,102,241,0.08);
    color: #0f172a;
    font-weight: 850;
}
.q-text thead th,
.option-content thead th {
    background: rgba(99,102,241,0.08);
    color: #0f172a;
    font-weight: 850;
}

/* --- 2. 布局框架 --- */
.app-container {
    display: flex;
    min-height: 100vh;
}

body.home-mode { --sidebar-width: 0px; }
body.home-mode .sidebar { transform: translateX(-100%); }
body.home-mode .sidebar-overlay { display: none !important; }
body.home-mode .top-bar { display: none; }
body.home-mode .home-view { display: flex; }
body.home-mode .fab-menu { display: inline-flex; }

/* ============================================
   HOME VIEW - Modernized & Animated
   ============================================ */
.home-view {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1500;
    background-color: #f1f5f9;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    perspective: 1500px; /* Adjusted for better 3D depth */
    font-family: 'Inter', ui-serif, Georgia, serif;
    isolation: isolate;
    touch-action: pan-y;

    /* layout */
    --home-max: 1200px;
    --home-pad: clamp(20px, 4vw, 40px);
    --home-gap: clamp(24px, 3vw, 36px);
    --home-surface: rgba(255,255,255,0.65);
    --home-surface-2: rgba(255,255,255,0.5);
    --home-border: rgba(255,255,255,0.4);
    --home-shadow: 0 32px 64px -12px rgba(15,23,42,0.15);

    /* Book dimensions */
    --book-h: clamp(320px, 38vh, 460px);
    --book-w: calc(var(--book-h) * 0.72);
    --book-d: calc(var(--book-h) * 0.14);
    --paper-inset: 3px;
    --paper-thick: calc(var(--book-d) - 6px);
    --anim-ease: cubic-bezier(0.34, 1.56, 0.64, 1); /* Springy */

    /* Mobile-friendly internal page scaling (tie typography/padding to book size) */
    --page-pad: clamp(12px, calc(var(--book-h) * 0.055), 20px);
    --page-label-size: clamp(8px, calc(var(--book-h) * 0.024), 11px);
    --page-title-size: clamp(16px, calc(var(--book-h) * 0.060), 26px);
    --page-sub-size: clamp(8px, calc(var(--book-h) * 0.020), 10px);
    --cover-pad: clamp(16px, calc(var(--book-h) * 0.085), 30px);
    --cover-title-size: clamp(20px, calc(var(--book-h) * 0.095), 36px);
    --cover-icon-size: clamp(28px, calc(var(--book-h) * 0.135), 52px);
    --spine-text-size: clamp(16px, calc(var(--book-h) * 0.040), 16px);
    --spine-text-spacing: clamp(1px, calc(var(--book-h) * 0.004), 2px);
}

/* Background layer - Modern Mesh */
.home-bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background:
        radial-gradient(circle at 15% 15%, rgba(244, 63, 94, 0.08), transparent 50%),
        radial-gradient(circle at 85% 20%, rgba(59, 130, 246, 0.08), transparent 50%),
        radial-gradient(circle at 50% 80%, rgba(99, 102, 241, 0.06), transparent 50%),
        linear-gradient(to bottom, #f8fafc, #f1f5f9);
}
.home-bg::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg width='24' height='24' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1h2v2H1V1zm4 0h2v2H5V1zm4 0h2v2H9V1z' fill='%2394a3b8' fill-opacity='0.07' fill-rule='evenodd'/%3E%3C/svg%3E");
    mask-image: radial-gradient(circle at center, black, transparent 80%);
    opacity: 0.6;
}

/* Scroll container */
.home-scroll {
    position: absolute;
    inset: 0;
    z-index: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: var(--home-gap);
    padding: var(--home-pad);
    padding-top: calc(var(--home-pad) + env(safe-area-inset-top));
    box-sizing: border-box;
}

/* HUD - warm, product-like toolbar */
.home-hud {
    position: relative;
    top: auto;
    z-index: 5000;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px 16px;
    pointer-events: auto;
    width: min(var(--home-max), 100%);
    padding: 16px 18px 14px;
    border-radius: 20px;
    background: var(--home-surface);
    border: 1px solid var(--home-border);
    box-shadow: var(--home-shadow);
    backdrop-filter: blur(16px);
    font-family: 'Segoe UI', 'Microsoft YaHei', system-ui, -apple-system, Arial, sans-serif;
}

.home-hud::before { display: none; }

.home-hud-brand { min-width: 0; }

.home-hud-title {
    font-family: ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif;
    font-weight: 900;
    color: #0f172a;
    font-size: 1.45rem;
    letter-spacing: 0.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.home-hud-title-full { display: inline; }
.home-hud-title-phone { display: none; }

.home-hud-subtitle {
    color: #475569;
    font-size: 0.96rem;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.home-hud-actions { 
    display: flex; 
    gap: 10px; 
    align-items: center;
    flex-wrap: nowrap;
    justify-content: flex-end;
}
.home-hud-primary { justify-self: end; align-self: start; }

/* Divider between utility actions and "library" actions */
.home-hud-divider {
    width: 1px;
    height: 24px;
    background: rgba(148,163,184,0.35);
    margin: 0 2px;
}

body.home-mode .home-hud .modal-btn {
    border-radius: 999px;
    padding: 10px 16px;
    background: rgba(255,255,255,0.62);
    border: 1px solid rgba(148,163,184,0.26);
    box-shadow: none;
    font-weight: 600;
    color: #475569;
    font-size: 0.9rem;
    white-space: nowrap;
    transition: background 180ms ease, border-color 180ms ease, box-shadow 180ms ease, color 180ms ease;
}
body.home-mode .home-hud .modal-btn i { margin-right: 8px; }

body.home-mode .home-hud .modal-btn:hover {
    background: rgba(255,255,255,0.78);
    border-color: rgba(148,163,184,0.34);
    box-shadow: none;
}

body.home-mode .home-hud .modal-btn.primary {
    background: #0f172a;
    color: #fff;
    border-color: #0f172a;
    box-shadow: none;
}

body.home-mode .home-hud .modal-btn.primary:hover {
    background: #1e293b;
}

body.home-transitioning .home-hud { opacity: 0; pointer-events: none; }

.home-hud.collapsed { padding: 12px 14px; }
.home-hud.collapsed .home-hud-subtitle { display: none; }
.home-hud.collapsed .home-hud-actions { display: none; }
.home-hud.collapsed .home-hud-divider { display: none; }

/* White overlay */
.white-overlay {
    position: fixed;
    inset: 0;
    background: #fff;
    z-index: 998;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.6s ease;
}
.white-overlay.visible { opacity: 1; }

/* Library layout (no bookshelf) */
.home-library {
    width: min(var(--home-max), 100%);
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 8px;
}

.home-library-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 14px;
    padding: 2px 2px 0;
    font-family: 'Segoe UI', 'Microsoft YaHei', system-ui, -apple-system, Arial, sans-serif;
}

.home-library-title {
    font-weight: 900;
    letter-spacing: 0.2px;
    color: #0f172a;
    font-size: 1.1rem;
}

.home-library-hint {
    color: #64748b;
    font-size: 0.92rem;
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(calc(var(--book-w) + 96px), 1fr));
    gap: clamp(18px, 3.2vw, 46px);
    align-items: end;
    justify-items: center;
    padding: clamp(18px, 2.6vw, 30px);
    width: 100%;
    border-radius: 28px;
    border: 1px solid rgba(148,163,184,0.20);
    background: var(--home-surface-2);
    box-shadow: 0 26px 90px rgba(15,23,42,0.10);
    backdrop-filter: blur(16px);
}

.library-grid .home-empty {
    grid-column: 1 / -1;
    width: min(560px, 100%);
    padding: 18px 18px;
    border-radius: 24px;
    background: rgba(255,255,255,0.70);
    border: 1px dashed rgba(148,163,184,0.45);
    box-shadow: 0 18px 70px rgba(15,23,42,0.08);
    text-align: center;
    font-family: 'Segoe UI', 'Microsoft YaHei', system-ui, -apple-system, Arial, sans-serif;
}

.library-grid .home-empty-title {
    font-weight: 900;
    letter-spacing: 0.2px;
    color: #0f172a;
}

.library-grid .home-empty-desc {
    margin-top: 6px;
    color: #64748b;
    font-size: 0.95rem;
    line-height: 1.6;
}

/* ============================================
   3D BOOK - 100% ARTIFACT EXACT
   ============================================ */
.book-container {
    width: var(--book-w);
    height: var(--book-h);
    position: relative;
    transform-style: preserve-3d;
    z-index: 1;
    /* ARTIFACT EXACT */
    transform: rotateY(30deg) rotateZ(-2deg);
    transition: transform 0.4s ease-out;
    will-change: transform;
    cursor: pointer;
}

.book-container.idle:hover {
    /* ARTIFACT EXACT */
    transform: rotateY(25deg) translateZ(20px) translateY(-10px);
    z-index: 7000;
}
/* Ensure book tooltip/menus can float above the Home HUD (z-index stacking context). */
body.home-mode .book-container:hover { z-index: 7000; }
/* During the open-book animation, JS sets an inline z-index that can still be under the Home HUD.
   Keep the animation visually “on top” without touching the JS state machine. */
.book-container.book-opening { z-index: 8000 !important; }

@media (hover: none) {
    .book-container.idle:hover { transform: rotateY(30deg) rotateZ(-2deg); }
}

/* Back cover with better texture */
.back-cover {
    position: absolute;
    inset: 0;
    background: #1e293b;
    border-radius: 4px 8px 8px 4px;
    transform: translateZ(calc(var(--book-d) * -1));
    box-shadow: -15px 20px 40px rgba(0,0,0,0.3);
}

/* Spine with realistic lighting */
.spine {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: var(--book-d);
    transform: translateX(calc(var(--book-d) * -1)) rotateY(-90deg);
    transform-origin: right;
    background: #0f172a;
    border-radius: 4px 0 0 4px;
    background-image: linear-gradient(90deg, rgba(255,255,255,0.15) 0%, rgba(255,255,255,0.05) 15%, rgba(0,0,0,0.4) 85%, rgba(0,0,0,0.6) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ARTIFACT EXACT spine-text */
.spine-text {
    writing-mode: vertical-rl;
    transform: none;
    text-orientation: mixed;
    color: #fbbf24;
    font-weight: bold;
    letter-spacing: var(--spine-text-spacing, 2px);
    font-size: var(--spine-text-size, 14px);
}

/* ARTIFACT EXACT text-block */
.text-block {
    position: absolute;
    top: var(--paper-inset);
    bottom: var(--paper-inset);
    left: 0;
    width: calc(var(--book-w) - var(--paper-inset));
    transform: translateZ(calc(var(--book-d) * -1 + 3px));
    transform-style: preserve-3d;
}

/* ARTIFACT EXACT pages */
.pages-top {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--paper-thick);
    transform: rotateX(90deg);
    transform-origin: top;
    background: #f1f5f9;
    background-image: repeating-linear-gradient(to top, #e2e8f0 0px, #f8fafc 1px, #e2e8f0 2px);
}

.pages-right {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: var(--paper-thick);
    transform: rotateY(90deg);
    transform-origin: right;
    background: #f1f5f9;
    background-image: repeating-linear-gradient(to right, #e2e8f0 0px, #f8fafc 1px, #e2e8f0 2px);
}

.pages-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: var(--paper-thick);
    transform: rotateX(-90deg);
    transform-origin: bottom;
    background: #f1f5f9;
    background-image: repeating-linear-gradient(to bottom, #e2e8f0 0px, #f8fafc 1px, #e2e8f0 2px);
}

/* ARTIFACT EXACT first-page */
.first-page {
    position: absolute;
    inset: 0;
    background: #fff;
    transform: translateZ(var(--paper-thick));
    box-shadow: inset 10px 0 15px rgba(0,0,0,0.05);
    padding: var(--page-pad, 20px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.first-page .chapter-label {
    margin-top: calc(var(--page-pad, 20px) * 0.9);
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: calc(var(--page-pad, 20px) * 0.45);
    font-size: var(--page-label-size, 10px);
    color: #94a3b8;
    letter-spacing: 1px;
}

.first-page h1 {
    font-size: var(--page-title-size, 24px);
    color: #334155;
    margin: calc(var(--page-pad, 20px) * 0.7) 0;
}

.first-page p {
    font-size: var(--page-sub-size, 8px);
    color: #cbd5e1;
    line-height: 1.5;
    overflow: hidden;
}

/* Front Cover - Dynamic & Textured */
.front-cover {
    position: absolute;
    inset: 0;
    transform-origin: left;
    transform-style: preserve-3d;
    z-index: 10;
    transition: transform 0.6s var(--anim-ease);
}

.cover-face {
    position: absolute;
    inset: 0;
    background: #1e293b;
    border-radius: 4px 8px 8px 4px;
    backface-visibility: hidden;
    background-image: 
        linear-gradient(135deg, rgba(255,255,255,0.15) 0%, transparent 40%),
        linear-gradient(to right, rgba(0,0,0,0.3) 0%, transparent 6px);
    border-left: 2px solid rgba(255,255,255,0.1);
    padding: var(--cover-pad, 30px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-shadow: inset 4px 0 10px rgba(0,0,0,0.2);
}

.cover-title {
    color: #fff;
    margin: 0;
    font-size: var(--cover-title-size, 2.2rem);
    font-weight: 800;
    text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    font-family: 'Inter', sans-serif;
    letter-spacing: -0.02em;
}

.cover-icon {
    font-size: var(--cover-icon-size, 48px);
    color: rgba(255,255,255,0.8);
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.2));
}

/* Inside Cover */
.cover-inside {
    position: absolute;
    inset: 0;
    background: #f8fafc;
    transform: rotateY(180deg);
    backface-visibility: hidden;
    border-radius: 8px 4px 4px 8px;
    box-shadow: inset 10px 0 20px rgba(0,0,0,0.06);
    background-image: radial-gradient(circle at center, #f1f5f9 2px, transparent 2px);
    background-size: 20px 20px;
    opacity: 0.98;
}

/* Theme colors (book cover/spine) */
.theme-blue .back-cover, .theme-blue .cover-face { background-color: #1e3a8a; }
.theme-blue .spine { background-color: #172554; }
.theme-indigo .back-cover, .theme-indigo .cover-face { background-color: #3730a3; }
.theme-indigo .spine { background-color: #1e1b4b; }
.theme-red .back-cover, .theme-red .cover-face { background-color: #881337; }
.theme-red .spine { background-color: #4c0519; }
.theme-rose .back-cover, .theme-rose .cover-face { background-color: #9f1239; }
.theme-rose .spine { background-color: #500724; }
.theme-teal .back-cover, .theme-teal .cover-face { background-color: #0f766e; }
.theme-teal .spine { background-color: #134e4a; }
.theme-emerald .back-cover, .theme-emerald .cover-face { background-color: #047857; }
.theme-emerald .spine { background-color: #064e3b; }
.theme-sage .back-cover, .theme-sage .cover-face { background-color: #4d7c0f; }
.theme-sage .spine { background-color: #365314; }
.theme-amber .back-cover, .theme-amber .cover-face { background-color: #92400e; }
.theme-amber .spine { background-color: #78350f; }
.theme-cocoa .back-cover, .theme-cocoa .cover-face { background-color: #7c2d12; }
.theme-cocoa .spine { background-color: #431407; }
.theme-plum .back-cover, .theme-plum .cover-face { background-color: #6d28d9; }
.theme-plum .spine { background-color: #4c1d95; }
.theme-slate .back-cover, .theme-slate .cover-face { background-color: #334155; }
.theme-slate .spine { background-color: #1f2937; }
.theme-midnight .back-cover, .theme-midnight .cover-face { background-color: #0f172a; }
.theme-midnight .spine { background-color: #020617; }

/* Animation states */
.book-container.open-state { z-index: 999; }
.book-container.open-state .front-cover { 
    transform: rotateY(-180deg);
}
.book-container.zooming { 
    transition: transform 1.2s cubic-bezier(0.25, 1, 0.5, 1);
}

/* ============================================
   RESPONSIVE - 3 breakpoints (layout + scale)
   ============================================ */
@media (max-width: 1100px) {
    .home-view { --book-h: clamp(260px, 34vh, 380px); }
    .library-grid { grid-template-columns: repeat(auto-fill, minmax(calc(var(--book-w) + 86px), 1fr)); }
}

/* Tablet home HUD: buttons shrink with screen size (still one row). */
@media (min-width: 721px) and (max-width: 1024px) {
    body.home-mode .home-hud .modal-btn {
        padding: 9px 12px;
        font-size: 0.86rem;
        background: rgba(255,255,255,0.58);
    }
    body.home-mode .home-hud .modal-btn i { margin-right: 6px; }
    .home-hud-actions { gap: 8px; }
    .home-hud-divider { height: 22px; }
}

@media (max-width: 720px) {
    .home-view { --book-h: clamp(220px, 36vh, 320px); }
    .home-scroll { padding: 14px; padding-top: calc(14px + env(safe-area-inset-top)); }
    .home-hud { padding: 14px 14px 12px; border-radius: 18px; }
    .home-hud-actions { justify-content: flex-end; }
    .home-hud-title { font-size: 1.25rem; }
    .home-hud-title-full { display: none; }
    .home-hud-title-phone { display: inline; }
    .home-hud-subtitle { display: none; }
    body.home-mode .home-hud .modal-btn { padding: 9px 12px; font-size: 0.88rem; }
    /* Phone: icon-only buttons (no text) while staying one row. */
    .home-hud-actions { gap: 8px; }
    .home-hud-actions .btn-text { display: none; }
    body.home-mode .home-hud .modal-btn {
        width: 42px;
        height: 42px;
        padding: 0;
        border-radius: 999px;
        justify-content: center;
        font-size: 0.95rem;
        background: rgba(255,255,255,0.52);
    }
    body.home-mode .home-hud .modal-btn i { margin-right: 0; font-size: 1.05rem; }
    body.home-mode .home-hud .modal-btn.primary { width: 42px; }
    .home-hud-divider { display: none; }
    .home-library-head { flex-direction: column; align-items: flex-start; gap: 4px; }
    .library-grid { padding: 16px; border-radius: 22px; grid-template-columns: repeat(auto-fill, minmax(calc(var(--book-w) + 68px), 1fr)); }
}

/* Narrow phones: keep the five buttons visible in one row and prevent subtitle orphan chars. */
@media (max-width: 360px) {
    .home-hud { padding: 12px 12px 10px; }
    .home-hud-title { font-size: 1.12rem; }
    .home-hud-subtitle { display: none; }
    .home-hud-actions { gap: 6px; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
    .home-hud-actions::-webkit-scrollbar { display: none; }
    .home-hud-actions { scrollbar-width: none; }

    body.home-mode .home-hud .modal-btn { width: 40px; height: 40px; }
    body.home-mode .home-hud .modal-btn.primary { width: 40px; }
    body.home-mode .home-hud .modal-btn i { font-size: 1.02rem; }
}

@media (max-width: 520px) {
    /* Free space for the title on narrow phones. */
    #syncStatus { display: none; }
    body:not(.home-mode) {
        --app-topbar-gap: 8px;
        --app-topbar-title-max: 42vw;
    }
    .top-bar .icon-btn { width: 40px; height: 40px; border-radius: 14px; }
    .top-bar .menu-toggle { width: 40px; height: 40px; }
}

/* 3-dots / long-press affordance */
.book-more {
    position: absolute;
    right: 10px;
    bottom: 10px;
    width: 56px;
    height: 56px;
    border: none;
    background: transparent;
    color: rgba(255,255,255,0.92);
    display: grid;
    place-items: center;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    z-index: 20;
    opacity: 0;
    transition: opacity 220ms ease, transform 220ms ease, background 220ms ease;
    transform: translateZ(26px);
}
.book-container:hover .book-more { opacity: 1; }
@media (hover: none) { .book-more { opacity: 1; } }
@media (hover: none) and (pointer: coarse) {
    /* iOS/WebView: flatten 3D transform for reliable hit-testing on touch. */
    .book-more { transform: translateZ(0); }
    .book-more:hover { transform: translateZ(0); }
}
.book-more:hover { transform: translateZ(26px) translateY(-2px); }

.book-more-visual {
    width: 36px;
    height: 36px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.28);
    background: rgba(15,23,42,0.22);
    backdrop-filter: blur(10px);
    display: grid;
    place-items: center;
    box-shadow: 0 16px 30px rgba(15,23,42,0.18);
    transition: background 220ms ease, box-shadow 220ms ease, transform 220ms ease;
}
.book-more:hover .book-more-visual {
    background: rgba(15,23,42,0.30);
    box-shadow: 0 20px 36px rgba(15,23,42,0.22);
}

.book-tooltip {
    position: absolute;
    left: 10px;
    right: 10px;
    top: -10px;
    transform: translateY(-100%) translateZ(24px);
    padding: 10px 12px;
    border-radius: 14px;
    background: rgba(255,255,255,0.92);
    border: 1px solid rgba(148,163,184,0.24);
    box-shadow: 0 22px 60px rgba(15,23,42,0.16);
    backdrop-filter: blur(12px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease, transform 200ms ease;
    z-index: 6200;
    font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}
.book-container:hover .book-tooltip { opacity: 1; transform: translateY(-100%) translateZ(24px) translateY(-6px); }
.book-tooltip-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; color: #0f172a; font-weight: 700; font-size: 0.86rem; }

/* Touch devices: disable hover-tooltips (avoids stuck overlays covering HUD buttons) */
@media (hover: none) {
    .book-tooltip { display: none !important; }
}

/* During immersive transition: keep only the active book visible */
body.home-transitioning #booksGrid .book-container { pointer-events: none; opacity: 0; filter: blur(1.2px); }
body.home-transitioning #booksGrid .book-container.book-opening { opacity: 1; filter: none; }
body.home-transitioning #booksGrid .book-more { opacity: 0 !important; }

.book-menu {
    position: fixed;
    z-index: 5600;
    min-width: 160px;
    padding: 8px;
    border-radius: 14px;
    background: rgba(255,255,255,0.92);
    border: 1px solid rgba(148,163,184,0.28);
    box-shadow: 0 28px 90px rgba(15,23,42,0.22);
    backdrop-filter: blur(12px);
}
.book-menu button {
    width: 100%;
    text-align: left;
    padding: 10px 10px;
    border: none;
    background: transparent;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
}
.book-menu button:hover { background: rgba(248,250,252,0.9); }
.book-menu button[data-act="delete"] { color: #9c2f3b; }
.book-menu button[data-act="delete"]:hover { background: rgba(197, 74, 90, 0.12); }

/* --- 3. 侧边栏整体结构 (Glassmorphism Refined) --- */
.sidebar {
    width: var(--sidebar-width);
    position: fixed;
    inset: 0 auto 0 0;
    height: 100dvh;
    z-index: 2400;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
    overflow: hidden;

    background:
        radial-gradient(900px 620px at 12% 0%, var(--accent-soft), transparent 62%),
        linear-gradient(180deg, rgba(255,255,255,0.86), rgba(255,255,255,0.74));
    background:
        radial-gradient(900px 620px at 12% 0%, color-mix(in srgb, var(--accent-color) 10%, transparent), transparent 62%),
        linear-gradient(180deg, rgba(255,255,255,0.86), rgba(255,255,255,0.74));
    border-right: 1px solid rgba(148,163,184,0.20);
    box-shadow: 10px 0 34px rgba(15, 23, 42, 0.08);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
}
.sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: 2300;
    background: rgba(15, 23, 42, 0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
    touch-action: none;
}
.brand {
    padding: 20px 18px;
    font-weight: 800;
    color: var(--primary-color);
    border-bottom: 1px solid rgba(0,0,0,0.04);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: rgba(255,255,255,0.4);
}
.brand-left { display: flex; align-items: center; gap: 12px; overflow: hidden; }
.brand-text { 
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 
    font-size: 1.1rem; letter-spacing: -0.01em;
}
.brand-text-full { display: inline; }
.brand-text-short { display: none; }
.brand-actions { display: flex; gap: 8px; align-items: center; }

.collapse-btn {
    border: 1px solid rgba(148,163,184,0.2);
    background: rgba(255,255,255,0.6);
    width: 36px;
    height: 36px;
    border-radius: 10px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.2s ease;
}
.collapse-btn:hover { 
    color: var(--accent-color);
    background: white;
    box-shadow: 0 10px 26px rgba(15,23,42,0.10);
}

.chapter-list {
    flex: 1;
    padding: 16px 12px;
    overflow-y: auto;
    position: relative;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain; 
    scrollbar-gutter: stable;
}

/* Sidebar item refined */
.list-item {
    padding: 10px 14px;
    margin-bottom: 4px;
    border-radius: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.94rem;
    color: #475569;
    border: 1px solid transparent;
    transition: background 180ms ease, border-color 180ms ease, box-shadow 180ms ease, color 180ms ease;
    font-weight: 500;
    position: relative;
}
.list-item:hover {
    background: rgba(255,255,255,0.7);
    color: #1e293b;
    border-color: rgba(148,163,184,0.22);
    box-shadow: 0 8px 22px rgba(15,23,42,0.08);
}
.chapter-item { color: #1e293b; }
.chapter-item:hover { color: #1e293b; }
.list-item.active {
    background: white;
    box-shadow: 0 8px 22px rgba(15,23,42,0.08);
    color: var(--accent-color);
    font-weight: 700;
    border-color: var(--accent-soft);
}

/* 源元素在拖拽时变淡 */
.list-item.dragging-source {
    opacity: 0.3;
    background: rgba(241,245,249,0.85);
    border: 1px dashed rgba(148,163,184,0.65);
    transform: none;
    box-shadow: none;
}

/* Chapter reordering insertion target */
.chapter-item.drag-insert-target {
    position: relative;
}
.chapter-item.drag-insert-target::before {
    content: '';
    position: absolute;
    left: 14px;
    right: 14px;
    top: 0;
    height: 0;
    border-top: 2px solid rgba(99,102,241,0.70);
    border-radius: 999px;
}
.chapter-item.drag-insert-target.drag-insert-after::before {
    top: auto;
    bottom: 0;
}

.item-title { font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Pinned favorites chapter count */
.favorites-count {
    flex: none;
    padding: 2px 8px;
    border-radius: 999px;
    background: rgba(148,163,184,0.12);
    border: 1px solid rgba(148,163,184,0.18);
    font-weight: 950;
    color: #64748b;
    font-size: 0.82rem;
    pointer-events: none;
}
.list-item.active .favorites-count {
    color: var(--accent-color);
    background: var(--accent-soft);
    border-color: var(--accent-soft);
}

/* --- 文件夹样式 --- */
.folder-container {
    margin-bottom: 5px;
    border-radius: 14px;
    transition: background-color 0.2s;
}
.folder-header {
    font-weight: 900;
    color: #0f172a;
}

/* [关键修复] 文件夹作为投放目标的反馈样式 */
/* 当拖拽物悬停在该文件夹（或其子元素）上方时 */
.folder-container.drag-target {
    background: var(--accent-soft); /* 整个块变色 */
    border: 1px dashed rgba(148,163,184,0.55);
}
.folder-container.drag-target .folder-header {
    color: var(--accent-color);
}

.folder-icon {
    margin-right: 8px;
    color: #f39c12;
}
.folder-arrow {
    margin-right: 8px;
    font-size: 0.8rem;
    color: #bbb;
    transition: 0.2s;
}
.folder-container.open .folder-arrow {
    transform: rotate(90deg);
}
.folder-content {
    margin-left: 20px;
    border-left: 2px solid rgba(148,163,184,0.22);
    padding-left: 5px;
    display: none;
    min-height: 5px;
}
.folder-container.open .folder-content {
    display: block;
}

/* --- 幽灵元素 (Ghost) --- */
.drag-ghost {
    position: fixed;
    top: 0; left: 0;
    z-index: 9999;
    pointer-events: none; /* 关键：确保JS能检测到下层元素 */
    background: rgba(255,255,255,0.92);
    border: 1px solid rgba(99,102,241,0.35);
    box-shadow: 0 22px 70px rgba(15,23,42,0.22);
    padding: 12px;
    border-radius: 14px;
    width: 240px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0.95;
    font-weight: bold;
    transform: translate3d(0,0,0);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  
}

/* --- 操作按钮 --- */
.item-actions {
    display: none; /* 默认隐藏 */
    gap: 12px;
}
/* 桌面端 Hover 显示 */
.list-item:hover .item-actions {
    display: flex;
}
/* 移动端 (无Hover) 始终显示，稍微透明 */
@media (hover: none) {
    .item-actions {
        display: flex;
        opacity: 0.4;
    }
    .folder-container.open .item-actions {
        opacity: 0.7;
    }
}
.action-icon {
    font-size: 0.92rem;
    color: rgba(15,23,42,0.45);
    padding: 6px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: rgba(248,250,252,0.0);
    transition: transform 120ms ease, box-shadow 120ms ease, border-color 120ms ease, background 120ms ease, color 120ms ease;
}
.action-icon:hover {
    color: var(--accent-color);
    background: var(--accent-soft);
    border-color: var(--accent-soft);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}
.action-icon.delete:hover {
    color: #e11d48;
    background: rgba(225,29,72,0.10);
    border-color: rgba(225,29,72,0.22);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}

/* 移动端友好的拖拽把手：避免和滚动手势冲突 */
.drag-handle {
    font-size: 0.98rem;
    color: rgba(15,23,42,0.45);
    padding: 6px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: rgba(248,250,252,0.0);
    cursor: grab;
    touch-action: none;
    transition: transform 120ms ease, box-shadow 120ms ease, border-color 120ms ease, background 120ms ease, color 120ms ease;
}
.drag-handle:hover {
    color: var(--accent-color);
    background: var(--accent-soft);
    border-color: var(--accent-soft);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}
.drag-handle:active { cursor: grabbing; }

/* --- 其他UI组件 (保持不变) --- */
.sidebar-footer {
    padding: 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.88));
    border-top: 1px solid rgba(148,163,184,0.22);
    display: flex;
    gap: 8px;
    position: sticky;
    bottom: 0;
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
}
.icon-btn {
    flex: 1;
    height: 40px;
    border: 1px solid rgba(148,163,184,0.32);
    background: rgba(255,255,255,0.86);
    border-radius: 14px;
    cursor: pointer;
    color: #334155;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1rem;
    transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}
.icon-btn:hover {
    border-color: var(--accent-soft);
    box-shadow: 0 18px 44px rgba(15,23,42,0.14);
    background: rgba(255,255,255,0.96);
}
.icon-btn.danger { border-color: rgba(197, 74, 90, 0.22); background: rgba(197, 74, 90, 0.06); color: #9c2f3b; }
.icon-btn.danger:hover { border-color: rgba(197, 74, 90, 0.36); background: rgba(197, 74, 90, 0.10); }
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    padding: var(--app-main-pad, 22px);
    padding-top: calc(var(--app-main-pad-top, 18px) + env(safe-area-inset-top));
    padding-bottom: calc(var(--app-main-pad-bottom, 22px) + env(safe-area-inset-bottom));
    transition: margin-left 260ms cubic-bezier(0.4, 0, 0.2, 1);
    background: transparent;
}
/* Top Bar Refined */
.top-bar {
    display: flex;
    align-items: center;
    gap: var(--app-topbar-gap, 16px);
    margin: 0 auto var(--app-card-gap, 24px);
    width: min(980px, 100%);
    background: rgba(255,255,255,0.8);
    padding: var(--app-topbar-pad-y, 14px) var(--app-topbar-pad-x, 20px);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.05);
    border: 1px solid rgba(255,255,255,0.6);
    position: relative;
    z-index: 1400;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    transition: background 220ms ease, box-shadow 220ms ease, border-color 220ms ease;
}
.top-bar:hover {
    background: rgba(255,255,255,0.9);
    box-shadow: 0 8px 30px rgba(0,0,0,0.08);
}
.top-bar h2 {
    font-size: var(--app-topbar-title-size, 1.1rem);
    font-weight: 700;
    color: #1e293b;
    margin: 0;
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: var(--app-topbar-title-max, 100%);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.top-bar h2:hover { color: var(--accent-color); }
.menu-toggle {
    display: none;
    border: none;
    background: transparent;
    font-size: 1.4rem;
    margin-right: -8px;
    cursor: pointer;
    color: #64748b;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    padding: 12px 6px 6px 6px;
    border-radius: 14px;
    transition: color 0.2s;
}
.menu-toggle:hover { color: var(--accent-color); }

#syncStatus {
    margin-left: auto;
    font-size: 0.85rem !important;
    color: #64748b !important;
    font-weight: 700;
    background: rgba(241, 245, 249, 0.8);
    padding: 4px 10px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: min(360px, 48vw);
    overflow: hidden;
}
.sync-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Sync traffic light */
.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: #ef4444;
    box-shadow: 0 0 0 4px rgba(239,68,68,0.16);
    flex: none;
}
.status-dot--ok { background: #22c55e; box-shadow: 0 0 0 4px rgba(34,197,94,0.16); }
.status-dot--warn { background: #f59e0b; box-shadow: 0 0 0 4px rgba(245,158,11,0.16); }
.status-dot--err { background: #ef4444; box-shadow: 0 0 0 4px rgba(239,68,68,0.18); }
.status-dot--off { background: #ef4444; box-shadow: 0 0 0 4px rgba(239,68,68,0.16); }

/* Keep top-bar icon buttons compact (avoid stretching because `.icon-btn { flex: 1; }`) */
.top-bar .icon-btn { flex: none; width: 44px; height: 44px; border-radius: 16px; }
.top-bar .menu-toggle { flex: none; width: 44px; height: 44px; }

.to-top-btn {
    position: fixed;
    right: 16px;
    bottom: 88px;
    width: 48px;
    height: 48px;
    border-radius: 999px;
    border: 1px solid rgba(148,163,184,0.22);
    background: rgba(255,255,255,0.92);
    box-shadow: 0 10px 24px rgba(15,23,42,0.12), 0 2px 6px rgba(15, 23, 42, 0.04);
    display: none;
    align-items: center;
    justify-content: center;
    color: #334155;
    cursor: pointer;
    z-index: 1950;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.to-top-btn:hover {
    background: rgba(255,255,255,0.98);
    border-color: var(--accent-soft);
    color: var(--accent-color);
}
.to-top-btn:active { transform: translateY(1px); }
.to-top-btn i { pointer-events: none; }

.fab-menu {
    /* --- 基础定位 --- */
    position: fixed;
    right: 16px;
    bottom: 16px;
    width: 56px; /* 稍微加大一点，让彩环更明显 */
    height: 56px;
    
    /* --- 布局 --- */
    display: flex;
    align-items: center;
    justify-content: center;

    /* Ensure pseudo ring doesn't block taps */
    position: fixed;
    isolation: isolate;
    
    /* --- 形状与背景 --- */
    border-radius: 50%;
    /* 纯白背景，去掉所有灰色边框 */
    background: #ffffff; 
    
    /* 避免系统默认描边/黑色 focus 圈；阴影用偏冷的蓝灰色（不出“黑圈”） */
    border: 0;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    box-shadow:
        0 10px 24px rgba(66, 133, 244, 0.14),
        0 2px 6px rgba(15, 23, 42, 0.04);
    
    cursor: pointer;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    z-index: 2000;
    transition: transform 0.2s ease;
}
.fab-menu i { position: relative; z-index: 1; }
.fab-menu:focus-visible {
    box-shadow:
        0 0 0 4px rgba(66, 133, 244, 0.22),
        0 10px 24px rgba(66, 133, 244, 0.14),
        0 2px 6px rgba(15, 23, 42, 0.04);
}

/* --- 核心：参考图风格的彩色厚环 --- */
.fab-menu::before {
    content: "";
    position: absolute;
    inset: 0; /* 撑满整个按钮 */
    border-radius: 50%;
    
    /* 关键点1：厚度 */
    /* 这里控制环的粗细*/
    padding: 2.5px; 
    
    /* 关键点2：Google 经典四色 (红黄绿蓝) */
    /* 使用 conic-gradient 模拟参考图的色块分布 */
    background: conic-gradient(
        from 0deg, 
        #EA4335, /* 红 */
        #FBBC05, /* 黄 */
        #34A853, /* 绿 */
        #4285F4, /* 蓝 */
        #EA4335  /* 闭环回到红 */
    );
    
    /* 关键点3：挖空中间，只留边框 */
    -webkit-mask: 
        linear-gradient(#fff 0 0) content-box, 
        linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    
    /* 关键点4：默认静止，不转 */
    animation: spin-ring 1.5s linear infinite;
    animation-play-state: paused; 
    pointer-events: none;
    z-index: 0;
}

/* --- 交互：鼠标放上去才转 --- */
.fab-menu:hover {
    transform: scale(1.05); /* 整体轻微放大 */
}

.fab-menu:hover::before {
    /* 鼠标悬停时，开始旋转 */
    animation-play-state: running;
}

/* --- 点击反馈 --- */
.fab-menu:active {
    transform: scale(0.95);
}

/* --- 旋转动画定义 --- */
@keyframes spin-ring {
    to { transform: rotate(360deg); }
}




.toast-host {
    position: fixed;
    left: 50%;
    bottom: 16px;
    transform: translateX(-50%);
    z-index: 3600;
    display: grid;
    gap: 10px;
    width: min(520px, calc(100vw - 24px));
    pointer-events: none;
}
.toast {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    background: rgba(255,255,255,0.92);
    border: 1px solid rgba(148,163,184,0.35);
    border-radius: 14px;
    box-shadow: 0 24px 70px rgba(15,23,42,0.18);
    backdrop-filter: blur(10px);
    transition: opacity 160ms ease, transform 160ms ease;
}
.toast.closing { opacity: 0; transform: translateY(6px); }
.toast .toast-msg { color: #334155; font-weight: 600; flex: 1; }
.toast .toast-btn {
    border: 1px solid rgba(148,163,184,0.35);
    background: rgba(248,250,252,0.9);
    border-radius: 12px;
    padding: 8px 12px;
    cursor: pointer;
}
.toast .toast-btn:hover { border-color: rgba(75,143,226,0.35); }

.seg-tabs { display: flex; gap: 8px; align-items: center; }
.seg-tab {
    border: 1px solid rgba(148,163,184,0.35);
    background: rgba(248,250,252,0.9);
    border-radius: 999px;
    padding: 8px 12px;
    cursor: pointer;
    font-weight: 700;
    color: #334155;
}
.seg-tab.active {
    border-color: rgba(99,102,241,0.35);
    background: rgba(99,102,241,0.12);
    color: #3730a3;
}

.settings-section { border: 1px solid rgba(148,163,184,0.22); border-radius: 16px; padding: 12px; background: rgba(248,250,252,0.55); overflow: hidden; }
.settings-title { font-weight: 900; color: #0f172a; margin-bottom: 10px; }
.settings-row { display: grid; grid-template-columns: 130px 1fr; gap: 12px; padding: 10px 0; border-top: 1px dashed rgba(148,163,184,0.35); }
.settings-row:first-of-type { border-top: none; }
.settings-label { font-weight: 800; color: #334155; padding-top: 6px; }
.settings-control { min-width: 0; }
.settings-inline { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 10px; color: #334155; font-weight: 700; }
.settings-inline select { border: 1px solid rgba(148,163,184,0.55); border-radius: 10px; padding: 8px 10px; background: rgba(255,255,255,0.9); }
.settings-preview { margin-top: 12px; display: grid; gap: 12px; }

.swatch-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; }
.swatch-item { display: grid; gap: 6px; justify-items: center; }
.swatch { width: 26px; height: 26px; border-radius: 999px; border: 1px solid rgba(148,163,184,0.55); box-shadow: 0 10px 18px rgba(15,23,42,0.08); }
.swatch-item input { width: 18px; height: 18px; }

@media (max-width: 560px) {
    .settings-row { grid-template-columns: 1fr; }
    .settings-label { padding-top: 0; }
    .swatch-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.save-section { border: 1px solid rgba(148,163,184,0.28); border-radius: 14px; padding: 12px; background: rgba(248,250,252,0.55); overflow: hidden; }
.save-title { font-weight: 800; color: #334155; margin-bottom: 8px; }
.save-list { display: grid; gap: 10px; width: 100%; }
.save-item {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(148,163,184,0.28);
    background: rgba(255,255,255,0.9);
}
.save-meta { flex: 1; overflow: hidden; min-width: 0; }
.save-actions { flex-shrink: 0; }
.save-name { font-weight: 800; color: #0f172a; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.save-tags { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
    display: inline-flex;
    align-items: center;
    max-width: 100%;
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 800;
    color: #334155;
    background: rgba(226, 232, 240, 0.75);
    border: 1px solid rgba(148, 163, 184, 0.35);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.tag--date { background: rgba(219, 234, 254, 0.85); border-color: rgba(59, 130, 246, 0.18); color: #1e3a8a; }
.tag--device { background: rgba(236, 253, 245, 0.9); border-color: rgba(16, 185, 129, 0.18); color: #065f46; }
.tag--muted { background: rgba(241, 245, 249, 0.9); border-color: rgba(148, 163, 184, 0.25); color: #64748b; }

.save-item--editing .save-time { opacity: 0.8; }
.save-rename-input {
    width: 100%;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid rgba(148, 163, 184, 0.55);
    background: rgba(255,255,255,0.95);
    font-weight: 800;
    color: #0f172a;
    outline: none;
}
.save-rename-input:focus { border-color: var(--accent-color); box-shadow: 0 0 0 4px var(--accent-soft); }
.save-time { font-size: 0.86rem; color: #64748b; margin-top: 2px; }
.save-actions { display: flex; gap: 8px; }
.save-actions .modal-btn { padding: 8px 12px; }
.save-actions .modal-btn { white-space: nowrap; }

@media (max-width: 560px) {
    .save-item { grid-template-columns: 1fr; }
    .save-actions { justify-content: flex-end; }
}

.danger-zone { border: 1px solid rgba(197, 74, 90, 0.22); border-radius: 14px; padding: 12px; background: rgba(197, 74, 90, 0.06); }
.danger-title { font-weight: 900; color: #9c2f3b; }
.danger-desc { margin-top: 6px; color: #6b1d28; font-size: 0.95rem; }
h2 {
    font-size: 1.5rem;
    color: var(--primary-color);
    letter-spacing: -0.02em;
    font-weight: 800;
}
.question-card {
    background: var(--card-bg);
    border-radius: 20px;
    padding: var(--app-card-pad, 24px);
    margin-bottom: var(--app-card-gap, 24px);
    box-shadow: var(--shadow-sm);
    border: 1px solid rgba(255,255,255,0.6);
    transition: background 220ms ease, border-color 220ms ease, box-shadow 220ms ease;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    position: relative;
    overflow: hidden;
}
.question-card:hover {
    box-shadow: var(--shadow-md);
    background: rgba(255,255,255,0.95);
    border-color: rgba(255,255,255,0.8);
}

.favorites-empty {
    padding: 18px 18px;
    border-radius: 18px;
    background: rgba(255,255,255,0.76);
    border: 1px dashed rgba(148,163,184,0.35);
    color: #64748b;
    font-weight: 650;
    line-height: 1.6;
}

.q-header { display: flex; gap: 16px; margin-bottom: 20px; align-items: flex-start; }
.q-header { min-width: 0; }
.q-actions {
    margin-left: auto;
    display: flex;
    gap: 10px;
    align-items: flex-start;
    flex: none;
}
.q-id {
    background: var(--primary-color);
    color: white;
    padding: 6px 12px;
    border-radius: 8px;
    font-weight: 700;
    font-size: var(--app-q-id-size, 0.85rem);
    height: fit-content;
    box-shadow: 0 4px 12px rgba(15,23,42,0.15);
}
.q-text { 
    font-size: var(--app-q-text-size, 1.1rem);
    font-weight: 600; 
    line-height: 1.7; 
    color: var(--primary-color);
    letter-spacing: -0.01em; 
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.options-list { list-style: none; margin: 20px 0; display: grid; gap: var(--app-opt-gap, 12px); }
.option-item {
    display: flex;
    padding: var(--app-opt-pad-y, 14px) var(--app-opt-pad-x, 18px);
    border: 1px solid rgba(148,163,184,0.15);
    border-radius: 12px;
    background: rgba(255,255,255,0.5);
    transition: all 0.2s ease;
    cursor: pointer;
    align-items: center;
    min-width: 0;
}
.option-item:hover { 
    background: white; 
    border-color: var(--accent-color); 
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}
.option-item.correct { 
    background: var(--correct-bg); 
    border-color: var(--correct-border); 
    color: var(--correct-text); 
}
.option-label {
    font-weight: 800;
    margin-right: 14px;
    width: 32px;
    flex-shrink: 0;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: rgba(241, 245, 249, 0.8);
    color: #64748b;
    font-size: 15.2px; /* ~= 0.95rem @ 16px; keep fixed for alignment */
}
.option-item:hover .option-label { background: var(--accent-color); color: white; }
.option-item.correct .option-label { background: #059669; color: white; }
.option-content {
    min-width: 0;
    flex: 1;
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* Prevent page-level horizontal scroll: let long formulas scroll inside their own block */
.katex-display {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden; /* avoid vertical scrollbar; use padding to prevent clipping */
    -webkit-overflow-scrolling: touch;
    padding: 0.45em 0.1em;
    scrollbar-width: thin;
    scrollbar-color: rgba(148,163,184,0.55) transparent;
}
.katex-display::-webkit-scrollbar { height: 8px; }
.katex-display::-webkit-scrollbar-track { background: transparent; }
.katex-display::-webkit-scrollbar-thumb {
    background: rgba(148,163,184,0.55);
    border-radius: 999px;
    border: 2px solid transparent;
    background-clip: content-box;
}
.katex-display::-webkit-scrollbar-thumb:hover { background: rgba(148,163,184,0.75); }

/* Markdown content: make media/tables responsive on mobile */
.q-text img,
.option-content img,
.analysis-content img,
.knowledge-content img,
.ai-bubble-content img,
.ai-chat-context-text img {
    max-width: 100%;
    height: auto;
}
.q-text pre,
.option-content pre,
.analysis-content pre,
.knowledge-content pre,
.ai-bubble-content pre,
.ai-chat-context-text pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.answer-box {
    margin-top: 16px;
    border-radius: 18px;
    border: 1px solid var(--correct-border);
    background: rgba(255,255,255,0.90);
    padding: 16px 16px 14px;
    box-shadow: 0 16px 50px rgba(15,23,42,0.08);
}
.answer-title {
    font-weight: 900;
    color: #0f172a;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
    letter-spacing: 0.2px;
}
.answer-title i {
    width: 30px;
    height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    border-radius: 999px;
    background: var(--correct-bg);
    border: 1px solid var(--correct-border);
    color: var(--correct-text);
}
.answer-content {
    /* Short-answer / reference answer should be readable (not as large as question stem) */
    font-size: var(--app-analysis-font-size, 0.98rem);
    line-height: 1.85;
    color: #2c3e50;
    font-weight: 500;
}
.answer-content b { font-weight: 700; }
.answer-content a { color: var(--accent-color); text-decoration: none; border-bottom: 1px dashed rgba(52,152,219,0.55); }
.answer-content a:hover { border-bottom-style: solid; }
.answer-content code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    background: rgba(27,31,35,0.06);
    padding: 2px 6px;
    border-radius: 6px;
    font-size: 0.95em;
}
.answer-content ul, .answer-content ol { padding-left: 22px; margin: 10px 0; }
.answer-content li { margin: 6px 0; }
.answer-content p { margin: 10px 0; }
.answer-content p:first-child { margin-top: 0; }
.answer-content hr { border: none; height: 1px; background: #e9eef5; margin: 12px 0; }
.answer-content blockquote {
    margin: 12px 0;
    padding: 10px 12px;
    border-left: 4px solid var(--correct-border);
    background: var(--correct-bg);
    border-radius: 0 12px 12px 0;
}
.answer-content table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin: 12px 0;
    border: 1px solid rgba(148,163,184,0.26);
    border-radius: 14px;
    overflow: hidden;
    background: rgba(255,255,255,0.75);
}
.answer-content th, .answer-content td {
    padding: 10px 12px;
    border-bottom: 1px solid rgba(148,163,184,0.18);
    border-right: 1px solid rgba(148,163,184,0.14);
}
.answer-content th {
    text-align: left;
    font-weight: 900;
    background: rgba(16,185,129,0.10);
    color: #0f172a;
}
.answer-content tr:last-child td { border-bottom: none; }
.answer-content td:last-child, .answer-content th:last-child { border-right: none; }
.answer-content pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

@media (max-width: 520px) {
    .q-text table, .option-content table,
    .analysis-content table, .knowledge-content table,
    .answer-content table {
        display: block;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

.analysis-box {
    margin-top: 24px;
    border-radius: 18px;
    border: 1px solid var(--analysis-border);
    background:
        radial-gradient(900px 520px at 20% -10%, var(--analysis-bg-1), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.96), rgba(255,255,255,0.88));
    background:
        radial-gradient(900px 520px at 20% -10%, color-mix(in srgb, var(--analysis-color) 14%, transparent), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.96), rgba(255,255,255,0.88));
    padding: 18px 18px 16px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 18px 60px rgba(15,23,42,0.10);
}
.analysis-title {
    font-weight: 900;
    color: #0f172a;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
    letter-spacing: 0.2px;
}
.analysis-title i {
    width: 30px;
    height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    border-radius: 999px;
    background: var(--analysis-bg-1);
    border: 1px solid var(--analysis-border);
    color: var(--analysis-color);
    box-shadow:
        0 0 0 6px var(--analysis-bg-2),
        0 18px 40px rgba(15,23,42,0.10);
}
@supports (background: color-mix(in srgb, red 50%, white)) {
    .analysis-title i {
        background: color-mix(in srgb, var(--analysis-color) 12%, rgba(255,255,255,0.92));
        border-color: color-mix(in srgb, var(--analysis-color) 18%, rgba(148,163,184,0.22));
        box-shadow:
            0 0 0 6px color-mix(in srgb, var(--analysis-color) 12%, transparent),
            0 0 0 14px color-mix(in srgb, var(--analysis-color) 6%, transparent),
            0 18px 40px rgba(15,23,42,0.10);
    }
}
/* Touch devices: some mobile browsers over-render `color-mix(..., transparent)` spread shadows (turns the halo into a solid, oversized ring). */
@media (hover: none) and (pointer: coarse) {
    .analysis-title i {
        box-shadow:
            0 0 0 6px var(--analysis-bg-2),
            0 10px 22px rgba(15,23,42,0.10);
    }
}
.knowledge-details {
    margin-top: 14px;
    border: 1px solid var(--knowledge-border);
    border-radius: 18px;
    background: rgba(255,255,255,0.92);
    overflow: hidden;
    box-shadow: 0 16px 50px rgba(15,23,42,0.08);
}
.knowledge-summary {
    background:
        radial-gradient(900px 520px at 20% -10%, var(--knowledge-bg-1), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.94), rgba(255,255,255,0.80));
    background:
        radial-gradient(900px 520px at 20% -10%, color-mix(in srgb, var(--knowledge-color) 14%, transparent), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.94), rgba(255,255,255,0.80));
    padding: 12px 16px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
    letter-spacing: 0.2px;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: background 0.15s ease, color 0.15s ease;
}
.knowledge-summary:hover {
    background:
        radial-gradient(900px 520px at 20% -10%, var(--knowledge-bg-1), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.94), rgba(255,255,255,0.80));
    background:
        radial-gradient(900px 520px at 20% -10%, color-mix(in srgb, var(--knowledge-color) 14%, transparent), transparent 60%),
        linear-gradient(180deg, rgba(255,255,255,0.94), rgba(255,255,255,0.80));
    color: #0f172a;
    box-shadow: inset 0 0 0 1px var(--knowledge-border);
}
.knowledge-summary i {
    width: 34px;
    height: 34px;
    display: inline-grid;
    place-items: center;
    border-radius: 12px;
    background: color-mix(in srgb, var(--knowledge-color) 10%, rgba(255,255,255,0.94));
    border: 1px solid color-mix(in srgb, var(--knowledge-color) 18%, rgba(148,163,184,0.22));
    color: var(--knowledge-color);
    box-shadow: 0 14px 26px rgba(15,23,42,0.08);
    flex: none;
    margin: 0;
}
.knowledge-summary i {
    background: color-mix(in srgb, var(--knowledge-color) 10%, rgba(255,255,255,0.94));
    border-color: color-mix(in srgb, var(--knowledge-color) 18%, rgba(148,163,184,0.22));
    box-shadow: 0 14px 26px rgba(15,23,42,0.08);
}
.knowledge-summary::-webkit-details-marker { display: none; }
.knowledge-summary::after { content: '\f078'; font-family: "Font Awesome 6 Free"; font-weight: 900; margin-left: auto; transition: 0.2s; }
.knowledge-details[open] .knowledge-summary::after { transform: rotate(180deg); }
.knowledge-content { padding: 14px 16px 16px; color: #2b3b46; }
.knowledge-content ul { padding-left: 20px; margin: 10px 0; }
.knowledge-content li { margin-bottom: 5px; }
.knowledge-details[open] .knowledge-content { animation: fadeIn 160ms ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-2px); } to { opacity: 1; transform: translateY(0); } }

/* 模态框 - Modern Clean */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.65);
    z-index: 3200;
    display: none;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 0;
    transition: opacity 0.3s ease;
}
.modal-overlay.open { 
    display: flex; 
    overflow: hidden; 
    padding: 20px;
    padding: calc(20px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(20px + env(safe-area-inset-left));
    opacity: 1; 
    overscroll-behavior: contain;
}

.modal-box {
    background: #ffffff;
    padding: 32px;
    width: 100%;
    max-width: 600px;
    border-radius: 24px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    max-height: calc(100vh - 40px);
    max-height: calc(100dvh - 40px);
    max-height: calc(100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow: auto;
    overscroll-behavior: contain;
    transform: scale(0.95);
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    scrollbar-gutter: stable;
    scrollbar-width: thin;
    scrollbar-color: rgba(148,163,184,0.60) transparent;
}
.modal-overlay.open .modal-box { transform: scale(1); }

.modal-sticky-close {
    position: sticky;
    top: 0;
    z-index: 20;
    height: 0;
    pointer-events: none;
}
.modal-sticky-close .modal-close-x {
    position: absolute;
    top: 12px;
    right: 12px;
    pointer-events: auto;
    background: rgba(255,255,255,0.92);
    border: 1px solid rgba(148,163,184,0.26);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: none;
}
.modal-sticky-close .modal-close-x:hover {
    background: rgba(255,255,255,0.92);
    border-color: rgba(148,163,184,0.36);
    box-shadow: none;
}

.search-box {
    max-width: 980px;
    height: 86vh;
    height: min(86vh, 880px);
    height: min(86dvh, 880px);
    max-height: calc(100vh - 40px);
    max-height: calc(100dvh - 40px);
    padding: 22px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
}
.search-sticky {
    position: sticky;
    top: 0;
    z-index: 15;
    background: rgba(255,255,255,0.98);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid rgba(148,163,184,0.22);
    box-shadow: 0 10px 30px rgba(15,23,42,0.04);
    transform: translateZ(0);
    padding-bottom: 10px;
}
.search-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding-right: 44px; /* leave space for sticky close X */
}
.search-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 950;
    color: #0f172a;
    letter-spacing: -0.01em;
}
.search-title i { color: var(--accent-color); }
.search-status {
    color: #64748b;
    font-weight: 700;
    font-size: 0.92rem;
    line-height: 1.45;
    min-height: 1.2em;
    text-align: right;
}
.search-bar { display: flex; gap: 10px; align-items: center; }
.search-input { flex: 1; font-size: 1.05rem; }
.search-results {
    flex: none;
    overflow: visible;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 2px 2px 4px;
    scrollbar-gutter: stable;
}
.search-empty {
    padding: 14px 14px;
    border-radius: 16px;
    border: 1px dashed rgba(148,163,184,0.36);
    background: rgba(255,255,255,0.75);
    color: #64748b;
    font-weight: 650;
    line-height: 1.6;
}
.search-group {
    border-radius: 18px;
    border: 1px solid rgba(148,163,184,0.18);
    background: rgba(255,255,255,0.72);
    overflow: hidden;
}
.search-group-head {
    width: 100%;
    border: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: rgba(255,255,255,0.55);
    cursor: pointer;
    text-align: left;
    -webkit-tap-highlight-color: transparent;
}
.search-group-head:hover { background: rgba(255,255,255,0.72); }
.search-group-head:active { background: rgba(255,255,255,0.82); }
.search-group-head::after {
    content: '▾';
    margin-left: 10px;
    color: rgba(148,163,184,0.95);
    font-weight: 900;
    line-height: 1;
    transform: rotate(0deg);
    transition: transform 160ms ease;
}
.search-group.collapsed .search-group-head::after { transform: rotate(-90deg); }
.search-group-title {
    min-width: 0;
    flex: 1;
    display: flex;
    align-items: baseline;
    gap: 6px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-weight: 900;
    color: #0f172a;
}
.search-group-folder { color: #334155; }
.search-group-sep { color: rgba(148,163,184,0.85); font-weight: 800; }
.search-group-chapter { color: #0f172a; }
.search-group-count {
    flex: none;
    padding: 2px 10px;
    border-radius: 999px;
    background: rgba(148,163,184,0.12);
    border: 1px solid rgba(148,163,184,0.18);
    color: #64748b;
    font-weight: 950;
    font-variant-numeric: tabular-nums;
}
.search-group-items { display: grid; }
.search-group.collapsed .search-group-items { display: none; }
.search-hit {
    width: 100%;
    border: 0;
    border-top: 1px solid rgba(148,163,184,0.14);
    background: transparent;
    padding: 10px 12px;
    cursor: pointer;
    text-align: left;
    display: grid;
    gap: 6px;
    -webkit-tap-highlight-color: transparent;
}
.search-hit:hover { background: rgba(255,255,255,0.92); }
.search-hit:active { background: rgba(255,255,255,0.98); }
.search-hit-title {
    display: flex;
    align-items: baseline;
    gap: 10px;
    font-weight: 950;
    color: #0f172a;
}
.search-hit-qno { min-width: 42px; color: #0f172a; font-variant-numeric: tabular-nums; }
.search-hit-field {
    padding: 1px 8px;
    border-radius: 999px;
    border: 1px solid rgba(148,163,184,0.22);
    background: rgba(148,163,184,0.10);
    color: #64748b;
    font-weight: 900;
    font-size: 0.82rem;
    flex: none;
}
.search-hit-snippet {
    color: #334155;
    font-weight: 650;
    font-size: 0.92rem;
    line-height: 1.45;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
.search-hit-snippet mark {
    padding: 0 2px;
    border-radius: 4px;
    background: rgba(59, 130, 246, 0.18);
    color: inherit;
}

.question-card.search-flash {
    outline: 2px solid rgba(99,102,241,0.70);
    outline-offset: 3px;
    box-shadow: 0 0 0 6px rgba(99,102,241,0.12), var(--shadow-md);
}

.modal-box::-webkit-scrollbar { width: 12px; height: 12px; }
.modal-box::-webkit-scrollbar-track { background: transparent; border-radius: 999px; }
.modal-box::-webkit-scrollbar-thumb {
    background-color: rgba(148,163,184,0.55);
    border-radius: 999px;
    border: 4px solid transparent;
    background-clip: content-box;
}
.modal-box::-webkit-scrollbar-thumb:hover { background-color: rgba(148,163,184,0.70); }

.modal-box .icon-btn {
    width: 38px;
    height: 38px;
    flex: none;
    border-radius: 14px;
    font-size: 0.98rem;
}

.modal-box textarea, .modal-box input.auth-input {
    width: 100%;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 14px;
    background: #f8fafc;
    transition: all 0.2s;
    font-family: inherit;
    font-size: 0.95rem;
    color: #334155;
}
.modal-box textarea { resize: vertical; }

#importTextarea {
    resize: vertical;
    min-height: 160px;
    max-height: 52vh;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    line-height: 1.45;
}

.import-paste-hint {
    color: #667085;
    font-size: 0.92rem;
    line-height: 1.55;
    margin: 8px 0 10px;
}
.import-paste-hint code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.9em;
}

.import-paste-help {
    margin: 10px 0 12px;
    border: 1px solid rgba(148,163,184,0.35);
    border-radius: 12px;
    padding: 10px 12px;
    background: rgba(255,255,255,0.78);
}
.import-paste-help summary {
    cursor: pointer;
    font-weight: 800;
    color: #334155;
}
.import-paste-help-body {
    margin-top: 10px;
    display: grid;
    gap: 10px;
}
.import-paste-help-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}
.import-paste-copy-btn {
    border: 1px solid transparent;
    background: var(--accent-color);
    border-radius: 12px;
    padding: 8px 12px;
    cursor: pointer;
    font-weight: 900;
    color: white;
    min-height: 38px;
    transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease, background 0.12s ease;
    box-shadow: 0 10px 26px rgba(15,23,42,0.16);
}
.import-paste-copy-btn:hover {
    filter: brightness(0.96);
    box-shadow: 0 14px 34px rgba(15,23,42,0.18);
}
.import-paste-copy-btn:active { transform: translateY(1px); }
.import-paste-help-title {
    font-weight: 800;
    color: #334155;
    font-size: 0.92rem;
}
.import-paste-help-pre {
    margin: 0;
    padding: 10px 12px;
    border: 1px solid rgba(226,232,240,0.95);
    border-radius: 12px;
    background: #f8fafc;
    color: #0f172a;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    overflow: auto;
}

#importFileInput {
    width: 100%;
    padding: 10px 12px;
    border-radius: 16px;
    border: 1px dashed rgba(148,163,184,0.45);
    background: rgba(255,255,255,0.86);
    color: #334155;
    font-weight: 650;
}
#importFileInput::file-selector-button {
    border: 1px solid rgba(148,163,184,0.30);
    background: rgba(255,255,255,0.92);
    border-radius: 12px;
    padding: 8px 12px;
    margin-right: 12px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
}
#importFileInput::file-selector-button:hover {
    border-color: rgba(99,102,241,0.28);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}
#importFileInput::-webkit-file-upload-button {
    border: 1px solid rgba(148,163,184,0.30);
    background: rgba(255,255,255,0.92);
    border-radius: 12px;
    padding: 8px 12px;
    margin-right: 12px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
}
#importFileInput::-webkit-file-upload-button:hover {
    border-color: rgba(99,102,241,0.28);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}
.modal-box textarea:focus, .modal-box input.auth-input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px var(--accent-soft);
    background: white;
}

.modal-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 24px; }
.modal-btn {
    padding: 10px 20px;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    cursor: pointer;
    background: white;
    font-weight: 600;
    color: #475569;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.modal-btn:hover { background: #f8fafc; border-color: #cbd5e1; }
.modal-btn.primary {
    background: var(--accent-color);
    border-color: transparent;
    color: white;
    box-shadow: 0 10px 26px rgba(15,23,42,0.16);
}
.modal-btn.primary:hover {
    filter: brightness(0.96);
    box-shadow: 0 14px 34px rgba(15,23,42,0.18);
}
.modal-btn.danger {
    background: #fef2f2;
    border-color: #fecaca;
    color: #dc2626;
}
.modal-btn.danger:hover { background: #fee2e2; border-color: #fca5a5; }

/* Book modal choices (theme/icon) */
.choice-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(118px, 1fr));
    gap: 10px;
}
.choice-grid--icons { grid-template-columns: repeat(auto-fit, minmax(62px, 1fr)); }
.choice-btn {
    display: inline-flex;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 14px;
    border: 1px solid rgba(148,163,184,0.40);
    background: rgba(248,250,252,0.92);
    cursor: pointer;
    transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}
.choice-btn:hover {
    border-color: rgba(75,143,226,0.35);
    box-shadow: 0 12px 22px rgba(15,23,42,0.10);
}
.choice-btn.selected {
    border-color: rgba(44,62,80,0.55);
    box-shadow: 0 16px 36px rgba(17,24,39,0.14);
    background: rgba(255,255,255,0.98);
}
.choice-btn--icon { justify-content: center; }
.choice-dot {
    width: 18px;
    height: 18px;
    border-radius: 999px;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.55);
    border: 1px solid rgba(255,255,255,0.35);
}
.choice-dot.theme-blue { background: #1e3a8a; }
.choice-dot.theme-indigo { background: #3730a3; }
.choice-dot.theme-red { background: #881337; }
.choice-dot.theme-rose { background: #9f1239; }
.choice-dot.theme-teal { background: #0f766e; }
.choice-dot.theme-emerald { background: #047857; }
.choice-dot.theme-sage { background: #4d7c0f; }
.choice-dot.theme-amber { background: #92400e; }
.choice-dot.theme-cocoa { background: #7c2d12; }
.choice-dot.theme-plum { background: #6d28d9; }
.choice-dot.theme-slate { background: #334155; }
.choice-dot.theme-midnight { background: #0f172a; }
.choice-label { font-weight: 800; color: #334155; font-size: 0.95rem; }
.choice-icon { font-size: 22px; line-height: 1; }

/* Tablet (separate from phone). */
@media (min-width: 721px) and (max-width: 1024px) {
    body:not(.home-mode) {
        --app-main-pad: 18px;
        --app-main-pad-top: 16px;
        --app-main-pad-bottom: 18px;
        --app-topbar-gap: 14px;
        --app-topbar-pad-y: 12px;
        --app-topbar-pad-x: 16px;
        --app-topbar-title-size: 17px;
        --app-topbar-title-max: 52vw;
        --app-card-pad: 20px;
        --app-card-gap: 18px;
        --app-q-id-size: 13px;
        --app-q-text-size: 17px;
        --app-opt-gap: 10px;
        --app-opt-pad-y: 12px;
        --app-opt-pad-x: 16px;
        --app-analysis-font-size: 15.5px;
    }

    :root { --sidebar-width: 0px; }
    body.sidebar-collapsed { --sidebar-width: 0px; }
    body.sidebar-collapsed .sidebar { transform: translateX(-100%); width: 320px; }
    body.sidebar-collapsed .chapter-list { display: block; }
    body.sidebar-collapsed .brand-text { display: inline; }
    body.sidebar-collapsed .sidebar-footer { flex-direction: row; gap: 8px; }
    body.sidebar-collapsed .icon-btn { width: auto; height: 40px; flex: 1; border-radius: 14px; }

    .sidebar { transform: translateX(-100%); width: 320px; }
    .sidebar.active { transform: translateX(0); }
    .sidebar.active + .sidebar-overlay { opacity: 1; pointer-events: auto; }
    .main-content { margin-left: 0; }
    .fab-menu { display: inline-flex; }
    .menu-toggle { display: inline-flex; }
    .top-bar { position: relative; top: auto; }
    #syncStatus { background: transparent; padding: 0; max-width: none; }
    #syncStatus .sync-label { display: none; }

    /* Phone-only: make the draggable hamburger respond immediately on Android (avoid scroll/selection gesture hijack). */
    .fab-menu { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

    /* Phone-only patch: fix the "解析" icon circle rendering (avoid heavy rings/shadows on mobile). */
    .analysis-title i {
        width: 26px;
        height: 26px;
        font-size: 0.92rem;
        background: rgba(59,130,246,0.10);
        border-color: rgba(59,130,246,0.20);
        color: rgba(59,130,246,1);
        box-shadow: 0 10px 22px rgba(15,23,42,0.10);
    }
}

/* Phone (separate from tablet). */
@media (max-width: 720px) {
    body:not(.home-mode) {
        --app-main-pad: 14px;
        --app-main-pad-top: 12px;
        --app-main-pad-bottom: 14px;
        --app-topbar-gap: 10px;
        --app-topbar-pad-y: 12px;
        --app-topbar-pad-x: 12px;
        --app-topbar-title-size: 16px;
        --app-topbar-title-max: 48vw;
        --app-card-pad: 16px;
        --app-card-gap: 16px;
        --app-q-id-size: 12.5px;
        --app-q-text-size: 16px;
        --app-opt-gap: 10px;
        --app-opt-pad-y: 12px;
        --app-opt-pad-x: 14px;
        --app-analysis-font-size: 15px;
    }

    :root { --sidebar-width: 0px; }
    body.sidebar-collapsed { --sidebar-width: 0px; }
    body.sidebar-collapsed .sidebar { transform: translateX(-100%); width: min(86vw, 320px); }
    body.sidebar-collapsed .chapter-list { display: block; }
    body.sidebar-collapsed .brand-text { display: inline; }
    body.sidebar-collapsed .sidebar-footer { flex-direction: row; gap: 8px; }
    body.sidebar-collapsed .icon-btn { width: auto; height: 40px; flex: 1; border-radius: 14px; }

    .sidebar { transform: translateX(-100%); width: min(86vw, 320px); }
    .sidebar.active { transform: translateX(0); }
    .sidebar.active + .sidebar-overlay { opacity: 1; pointer-events: auto; }
    .main-content { margin-left: 0; }
    .fab-menu { display: inline-flex; }
    .menu-toggle { display: inline-flex; }
    .top-bar { position: relative; top: auto; }
    #syncStatus { background: transparent; padding: 0; max-width: none; }
    #syncStatus .sync-label { display: none; }

    /* Sidebar brand: avoid being covered by 4 buttons */
    .brand-text-full { display: none; }
    .brand-text-short { display: inline; }
}

/* Touch devices with coarse pointer but wide screens: keep mobile-like navigation without merging tablet/phone rules. */
@media (hover: none) and (pointer: coarse) and (min-width: 1025px) {
    :root { --sidebar-width: 0px; }
    body.sidebar-collapsed { --sidebar-width: 0px; }
    body.sidebar-collapsed .sidebar { transform: translateX(-100%); width: 320px; }
    body.sidebar-collapsed .chapter-list { display: block; }
    body.sidebar-collapsed .brand-text { display: inline; }
    body.sidebar-collapsed .sidebar-footer { flex-direction: row; gap: 8px; }
    body.sidebar-collapsed .icon-btn { width: auto; height: 40px; flex: 1; border-radius: 14px; }

    .sidebar { transform: translateX(-100%); width: 320px; }
    .sidebar.active { transform: translateX(0); }
    .sidebar.active + .sidebar-overlay { opacity: 1; pointer-events: auto; }
    .main-content { margin-left: 0; }
    .fab-menu { display: inline-flex; }
    .menu-toggle { display: inline-flex; }
    .top-bar { position: relative; top: auto; }
    #syncStatus { background: transparent; padding: 0; max-width: none; }
    #syncStatus .sync-label { display: none; }
}

@media (max-width: 420px) {
    body:not(.home-mode) {
        --app-main-pad: 14px;
        --app-main-pad-top: 12px;
        --app-main-pad-bottom: 14px;
        --app-topbar-gap: 8px;
        --app-topbar-pad-y: 12px;
        --app-topbar-pad-x: 12px;
        --app-topbar-title-size: 16px;
        --app-topbar-title-max: 44vw;
    }

    .top-bar { flex-wrap: nowrap; }
    .top-bar h2 {
        flex: 1;
        min-width: 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
    #syncStatus {
        display: none;
    }
    #aiHistoryBtn { order: 5; }
    #aiImportBtn { order: 6; }
}

@media (max-width: 360px) {
    body:not(.home-mode) {
        --app-main-pad: 12px;
        --app-main-pad-top: 10px;
        --app-main-pad-bottom: 12px;
        --app-topbar-gap: 6px;
        --app-topbar-pad-y: 10px;
        --app-topbar-pad-x: 10px;
        --app-topbar-title-size: 15px;
        --app-topbar-title-max: 40vw;
    }
    .top-bar .icon-btn { width: 40px; height: 40px; border-radius: 14px; }
    .top-bar .menu-toggle { width: 40px; height: 40px; }
}

/* ---------------------------
 * AI chat (modal)
 * --------------------------- */
.fav-btn {
    align-self: flex-start;
    padding: 6px 10px;
    border-radius: 10px;
    border: 1px solid rgba(148,163,184,0.22);
    background: rgba(255,255,255,0.86);
    color: rgba(15,23,42,0.55);
    font-weight: 900;
    font-size: 0.85rem;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.fav-btn i { pointer-events: none; }
.fav-btn:hover { background: rgba(255,255,255,0.96); border-color: rgba(245,158,11,0.55); color: #f59e0b; }
.fav-btn:active { transform: translateY(1px); }
.fav-btn.on {
    background: rgba(245,158,11,0.10);
    border-color: rgba(245,158,11,0.38);
    color: #f59e0b;
}

.ai-ask-btn {
    margin-left: auto;
    align-self: flex-start;
    padding: 6px 10px;
    border-radius: 10px;
    border: 1px solid var(--accent-soft);
    background: rgba(255,255,255,0.86);
    color: var(--accent-color);
    font-weight: 800;
    font-size: 0.85rem;
    cursor: pointer;
    user-select: none;
}
.ai-ask-btn:hover { background: rgba(255,255,255,0.96); border-color: var(--accent-color); }
.ai-ask-btn:active { transform: translateY(1px); }
.q-actions .ai-ask-btn { margin-left: 0; }

.ai-sel-btn {
    position: fixed;
    z-index: 9999;
    display: none;
    padding: 8px 10px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.22);
    background: var(--accent-color);
    color: white;
    font-weight: 900;
    font-size: 0.86rem;
    box-shadow: 0 14px 34px rgba(15, 23, 42, 0.22);
    cursor: pointer;
    user-select: none;
}
.ai-sel-btn:active { transform: translateY(1px); }

.ai-chat-box {
    width: min(900px, calc(100vw - 40px));
    max-width: none;
    height: min(86vh, 920px);
    max-height: min(92vh, 980px);
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 0;
    overflow: hidden;
    overscroll-behavior: contain;
    border: 1px solid rgba(148,163,184,0.28);
    background:
        radial-gradient(1200px 700px at 10% 0%, rgba(99,102,241,0.10), transparent 55%),
        radial-gradient(900px 600px at 90% 0%, rgba(56,189,248,0.10), transparent 55%),
        radial-gradient(900px 700px at 50% 120%, rgba(244,63,94,0.06), transparent 55%),
        rgba(255,255,255,0.92);
    box-shadow: 0 46px 140px rgba(15,23,42,0.32);
}

.ai-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px 12px;
    background: rgba(255,255,255,0.82);
    border-bottom: 1px solid rgba(148,163,184,0.22);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
}
.ai-chat-title {
    font-weight: 900;
    color: #0f172a;
    letter-spacing: 0.2px;
    font-size: 1.05rem;
}
.ai-chat-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}
/* 容器：深一点的凹槽，为了衬托亮色的按钮 */
.ai-model-switch {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px;
    border-radius: 999px;
    /* 容器背景稍微深一点，增加对比度 */
    background: rgba(230, 235, 240, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.4);
    /* 深色内阴影，制造“坑”的感觉 */
    box-shadow: 
        inset 0 2px 6px rgba(0, 0, 0, 0.06),
        0 1px 0 rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.ai-model-switch--small { padding: 3px; gap: 3px; }

/* 选项通用样式 */
.ai-model-option {
    position: relative;
    border: 1px solid transparent;
    background: transparent;
    padding: 8px 18px;
    border-radius: 999px;
    cursor: pointer;
    /* 未选中状态颜色 */
    color: #64748b;
    font-weight: 700;
    font-size: 0.92rem;
    line-height: 1;
    transition: all 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    user-select: none;
    z-index: 1;
}

.ai-model-switch--small .ai-model-option { padding: 6px 12px; font-size: 0.85rem; }

/* 悬停未选中态 */
.ai-model-option:hover:not(.active) {
    color: #334155;
    background: rgba(255, 255, 255, 0.4);
}

/* 点击时的Q弹缩放 */
.ai-model-option:active {
    transform: scale(0.94);
}

/* --- 核心：彩色果冻选中态 --- */
.ai-model-option.active {
    /* 1. 字体变白，加一点点阴影增加可读性 */
    color: #ffffff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
    
    /* 2. 默认苹果蓝：从左上到右下的微渐变 */
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    
    /* 3. 复杂的果冻光影 */
    box-shadow: 
        /* 顶部高光：锐利的白色半透明线，制造玻璃感 */
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        /* 底部反光：微弱的深色内阴影，增加厚度 */
        inset 0 -2px 0 rgba(0, 0, 0, 0.1),
        /* 外部投影：关键！使用同色系的蓝色投影，而不是黑色，制造发光感 */
        0 4px 10px rgba(37, 99, 235, 0.3),
        0 2px 4px rgba(37, 99, 235, 0.1);
        
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* --- 额外口味：可以通过添加类名来改变颜色 --- */

/* 莓果紫 (Berry) */
.ai-model-option.active.berry {
    background: linear-gradient(135deg, #a855f7 0%, #7c3aed 100%);
    box-shadow: 
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        inset 0 -2px 0 rgba(0, 0, 0, 0.1),
        0 4px 10px rgba(124, 58, 237, 0.3);
}

/* 蜜桃粉 (Peach) */
.ai-model-option.active.peach {
    background: linear-gradient(135deg, #f472b6 0%, #db2777 100%);
    box-shadow: 
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        inset 0 -2px 0 rgba(0, 0, 0, 0.1),
        0 4px 10px rgba(219, 39, 119, 0.35);
}

/* 苹果绿 (Mint) */
.ai-model-option.active.mint {
    background: linear-gradient(135deg, #2dd4bf 0%, #0d9488 100%);
    box-shadow: 
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        inset 0 -2px 0 rgba(0, 0, 0, 0.1),
        0 4px 10px rgba(13, 148, 136, 0.3);
}


.ai-chat-model,
.ai-import-model,
.ai-history-scope,
.settings-inline select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    cursor: pointer;
    padding-right: 38px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 20 20'%3E%3Cpath fill='%2364758b' d='M5.6 7.4a1 1 0 0 1 1.4 0L10 10.4l3-3a1 1 0 1 1 1.4 1.4l-3.7 3.7a1 1 0 0 1-1.4 0L5.6 8.8a1 1 0 0 1 0-1.4Z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 16px 16px;
}
.ai-chat-model:hover,
.ai-import-model:hover,
.ai-history-scope:hover,
.settings-inline select:hover {
    border-color: var(--accent-soft);
}

.ai-chat-context {
    margin: 10px 16px 0;
    border: 1px solid rgba(148,163,184,0.22);
    border-radius: 16px;
    background: rgba(255,255,255,0.62);
    overflow: hidden;
}
.ai-chat-context summary {
    padding: 10px 12px;
    cursor: pointer;
    color: #334155;
    font-weight: 800;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 10px;
}
.ai-chat-context summary:hover {
    background: var(--accent-soft);
}
.ai-chat-context[open] summary {
    border-bottom: 1px dashed rgba(148,163,184,0.30);
    background: var(--accent-soft);
}
.ai-chat-context summary::-webkit-details-marker { display: none; }
.ai-chat-context summary::after {
    content: '\f078';
    font-family: "Font Awesome 6 Free";
    font-weight: 900;
    margin-left: auto;
    opacity: 0.75;
    transition: transform 180ms ease;
}
.ai-chat-context[open] summary::after { transform: rotate(180deg); }
.ai-chat-context-text {
    padding: 12px 12px 14px;
    max-height: 22vh;
    overflow: auto;
    overscroll-behavior: contain;
    font-size: 0.95rem;
    line-height: 1.8;
    color: #0f172a;
}
.ai-chat-context-text p { margin: 0 0 8px; }
.ai-chat-context-text p:last-child { margin-bottom: 0; }
.ai-chat-context-text ul, .ai-chat-context-text ol {
    margin: 8px 0;
    padding-left: 1.4em;
}
.ai-chat-context-text li { margin: 6px 0; }
.ai-chat-context-text code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.92em;
    background: rgba(15, 23, 42, 0.06);
    border: 1px solid rgba(15, 23, 42, 0.08);
    padding: 0 6px;
    border-radius: 8px;
}

.ai-chat-messages {
    flex: 1;
    overflow: auto;
    overscroll-behavior: contain;
    margin: 10px 16px 0;
    border: 1px solid rgba(148,163,184,0.22);
    border-radius: 18px;
    padding: 14px;
    background:
        radial-gradient(600px 260px at 20% 0%, rgba(99,102,241,0.10), transparent 55%),
        radial-gradient(600px 260px at 80% 0%, rgba(56,189,248,0.10), transparent 55%),
        rgba(255,255,255,0.78);
}

.ai-msg {
    display: flex;
    margin: 10px 0;
}
.ai-msg.user { justify-content: flex-end; }
.ai-msg.assistant { justify-content: flex-start; }
.ai-msg.system { justify-content: flex-start; }

.ai-bubble {
    max-width: min(620px, 88%);
    padding: 12px 14px;
    border-radius: 18px;
    line-height: 1.7;
    font-size: 0.98rem;
    word-break: break-word;
    box-shadow: 0 16px 40px rgba(15,23,42,0.10);
}
.ai-msg.assistant .ai-bubble,
.ai-msg.system .ai-bubble {
    width: 100%;
    max-width: none;
    box-shadow: none;
}
.ai-bubble.user {
    background: linear-gradient(180deg, rgba(99,102,241,0.94), rgba(79,70,229,0.94));
    border: 1px solid rgba(99,102,241,0.45);
    color: rgba(255,255,255,0.96);
}
.ai-bubble.assistant {
    background: rgba(255,255,255,0.84);
    border: 1px solid rgba(148,163,184,0.24);
    color: #0f172a;
}
.ai-bubble.system {
    background: rgba(245, 158, 11, 0.10);
    border: 1px solid rgba(245, 158, 11, 0.22);
    color: #7c2d12;
}

.ai-bubble-content { color: inherit; }
.ai-bubble.user .ai-bubble-content a { color: rgba(255,255,255,0.96); border-bottom-color: rgba(255,255,255,0.38); }
.ai-bubble[data-streaming="1"] .ai-bubble-content {
    white-space: pre-wrap;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.94rem;
    opacity: 0.96;
}
.ai-bubble[data-placeholder="1"] .ai-bubble-content {
    font-family: inherit;
    font-style: italic;
    opacity: 0.78;
}

/* Markdown baseline */
.q-text p, .option-content p { margin: 0; }
.analysis-content p, .knowledge-content p { margin: 0 0 8px; }
.analysis-content p:last-child, .knowledge-content p:last-child { margin-bottom: 0; }
.analysis-content ul, .analysis-content ol,
.knowledge-content ul, .knowledge-content ol { margin: 8px 0 8px 18px; }

.ai-bubble-content p { margin: 0 0 8px; }
.ai-bubble-content p:last-child { margin-bottom: 0; }
.ai-bubble-content ul, .ai-bubble-content ol { margin: 8px 0 8px 18px; }
.ai-bubble-content code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.92em;
    background: rgba(15, 23, 42, 0.06);
    border: 1px solid rgba(15, 23, 42, 0.08);
    padding: 0 6px;
    border-radius: 8px;
}
.ai-bubble-content table, .ai-chat-context-text table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin: 10px 0;
    border: 1px solid rgba(148,163,184,0.26);
    border-radius: 14px;
    overflow: hidden;
    background: rgba(255,255,255,0.80);
}
.ai-bubble-content th, .ai-bubble-content td,
.ai-chat-context-text th, .ai-chat-context-text td {
    padding: 9px 10px;
    border-bottom: 1px solid rgba(148,163,184,0.22);
    border-right: 1px solid rgba(148,163,184,0.18);
    vertical-align: top;
}
.ai-bubble-content th:last-child, .ai-bubble-content td:last-child,
.ai-chat-context-text th:last-child, .ai-chat-context-text td:last-child { border-right: 0; }
.ai-bubble-content tr:last-child td,
.ai-chat-context-text tr:last-child td { border-bottom: 0; }
.ai-bubble-content thead th,
.ai-chat-context-text thead th {
    background: rgba(99,102,241,0.08);
    color: #0f172a;
    font-weight: 850;
}
.ai-bubble-content pre {
    margin: 10px 0;
    padding: 10px 12px;
    border-radius: 12px;
    background: rgba(15, 23, 42, 0.05);
    border: 1px solid rgba(15, 23, 42, 0.10);
    overflow: auto;
}
.ai-bubble-content pre code { background: transparent; border: 0; padding: 0; }

.ai-chat-input {
    position: sticky;
    bottom: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin: 10px 16px 12px;
    padding: 12px 12px;
    border-radius: 18px;
    border: 1px solid rgba(148,163,184,0.22);
    background: rgba(255,255,255,0.88);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: 0 22px 70px rgba(15,23,42,0.14);
}
.ai-chat-input-row {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}
.ai-chat-quote {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 10px 12px;
    border-radius: 16px;
    border: 1px solid rgba(148,163,184,0.22);
    background: var(--accent-soft);
    box-shadow: inset 0 0 0 1px rgba(255,255,255,0.25);
}
.ai-chat-quote-badge {
    flex: none;
    height: 22px;
    padding: 0 10px;
    border-radius: 999px;
    background: rgba(255,255,255,0.75);
    color: var(--accent-color);
    font-weight: 900;
    font-size: 0.82rem;
    display: inline-flex;
    align-items: center;
}
.ai-chat-quote-text {
    flex: 1;
    min-width: 0;
    color: #0f172a;
    font-size: 0.94rem;
    line-height: 1.45;
    white-space: pre-wrap;
    max-height: 120px;
    overflow: auto;
    border-left: 4px solid var(--accent-color);
    padding-left: 10px;
}
.ai-chat-quote-clear {
    width: 34px;
    height: 34px;
    flex: none;
    border-radius: 12px;
    border: 1px solid rgba(148,163,184,0.22);
    background: rgba(255,255,255,0.88);
    cursor: pointer;
    color: #334155;
}
.ai-chat-quote-clear:hover { background: rgba(255,255,255,0.98); border-color: var(--accent-soft); }
.ai-chat-input textarea {
    flex: 1;
    resize: none;
    min-height: 44px;
    max-height: 220px;
    border-radius: 16px;
    border: 1px solid rgba(148,163,184,0.26);
    padding: 10px 12px;
    font-size: 0.98rem;
    line-height: 1.4;
    outline: none;
    background: rgba(255,255,255,0.95);
}
.ai-chat-input textarea:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 4px var(--accent-soft);
}

.ai-chat-hint {
    display: none;
    margin: 0 2px;
    padding: 0 2px;
    color: #64748b;
    font-size: 0.88rem;
    white-space: pre-line;
}

/* Tablet: near full-screen sheet */
@media (max-width: 1024px) {
    #aiChatModal.open { padding: 12px; overflow: hidden; }
    .ai-chat-box {
        width: calc(100vw - 24px);
        height: calc(100vh - 24px);
        height: calc(100dvh - 24px);
        max-height: none;
        border-radius: 22px;
    }
    .ai-chat-context { margin: 10px 12px 0; }
    .ai-chat-messages { margin: 10px 12px 0; padding: 12px; }
    .ai-chat-input { margin: 10px 12px 12px; }
    .ai-bubble { max-width: 94%; }
}

/* Phone: true full-screen (safe-area aware) */
@media (max-width: 600px) {
    #aiChatModal.open { padding: 0; }
    .ai-chat-box {
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        border-radius: 0;
    }
    .ai-chat-header { padding-top: calc(14px + env(safe-area-inset-top)); }
    .ai-chat-input { margin-bottom: calc(12px + env(safe-area-inset-bottom)); }
}

@media (hover: hover) and (pointer: fine) and (min-width: 900px) {
    .ai-chat-box { resize: both; min-width: 560px; min-height: 640px; }
    .ai-chat-header { cursor: move; user-select: none; }
}

/* ---------------------------
 * AI import (modal)
 * --------------------------- */
.ai-import-box {
    width: min(760px, calc(100vw - 28px));
    max-width: none;
    max-height: min(84vh, 860px);
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.ai-import-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.ai-import-header .icon-btn {
    width: 32px;
    height: 32px;
    border-radius: 12px;
}
.ai-import-title {
    font-weight: 900;
    color: #0f172a;
    letter-spacing: 0.2px;
}
.ai-import-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.ai-import-inline {
    flex-direction: row;
    align-items: center;
    gap: 10px;
}
.ai-import-label {
    font-weight: 900;
    color: #334155;
}
.ai-import-sub {
    color: #64748b;
    font-size: 0.9rem;
}

#aiImportFilesInput {
    width: 100%;
    padding: 10px 12px;
    border-radius: 16px;
    border: 1px dashed rgba(148,163,184,0.45);
    background: rgba(255,255,255,0.86);
    color: #334155;
    font-weight: 650;
}
#aiImportFilesInput::file-selector-button {
    border: 1px solid rgba(148,163,184,0.30);
    background: rgba(255,255,255,0.92);
    border-radius: 12px;
    padding: 8px 12px;
    margin-right: 12px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
}
#aiImportFilesInput::file-selector-button:hover {
    border-color: rgba(99,102,241,0.28);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}
#aiImportFilesInput::-webkit-file-upload-button {
    border: 1px solid rgba(148,163,184,0.30);
    background: rgba(255,255,255,0.92);
    border-radius: 12px;
    padding: 8px 12px;
    margin-right: 12px;
    cursor: pointer;
    font-weight: 900;
    color: #0f172a;
}
#aiImportFilesInput::-webkit-file-upload-button:hover {
    border-color: rgba(99,102,241,0.28);
    box-shadow: 0 12px 26px rgba(15,23,42,0.10);
}

.settings-control input[type="color"] {
    width: 44px;
    height: 34px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 12px;
}
.settings-control input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.settings-control input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 12px;
    box-shadow: inset 0 0 0 1px rgba(148,163,184,0.55), 0 10px 18px rgba(15,23,42,0.08);
}
.settings-control input[type="color"]::-moz-color-swatch {
    border: none;
    border-radius: 12px;
    box-shadow: inset 0 0 0 1px rgba(148,163,184,0.55), 0 10px 18px rgba(15,23,42,0.08);
}
.settings-control input[type="color"]:focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 3px;
}
.ai-import-files {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.ai-import-file {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 1px solid rgba(15, 23, 42, 0.10);
    border-radius: 12px;
    padding: 8px 10px;
    background: rgba(255,255,255,0.92);
    cursor: grab;
    user-select: none;
}
.ai-import-file:active { cursor: grabbing; }
.ai-import-file-idx {
    width: 20px;
    text-align: center;
    font-weight: 900;
    color: #334155;
}
.ai-import-thumb {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    object-fit: cover;
    border: 1px solid rgba(15, 23, 42, 0.12);
    background: rgba(2, 6, 23, 0.04);
}
.ai-import-file-name {
    font-size: 0.92rem;
    color: #0f172a;
    font-weight: 700;
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.ai-import-file-remove {
    border: 0;
    background: rgba(239, 68, 68, 0.10);
    color: #b91c1c;
    border-radius: 10px;
    padding: 6px 8px;
    cursor: pointer;
    font-weight: 900;
}
.ai-import-file-remove:hover { background: rgba(239, 68, 68, 0.16); }

.ai-import-move-up, .ai-import-move-down {
    border: 0;
    border-radius: 10px;
    padding: 6px 8px;
    cursor: pointer;
    font-weight: 900;
    background: rgba(99, 102, 241, 0.10);
    color: #3730a3;
}
.ai-import-move-up:hover, .ai-import-move-down:hover { background: rgba(99, 102, 241, 0.16); }

.ai-import-model {
    height: 36px;
    border-radius: 12px;
    border: 1px solid rgba(15, 23, 42, 0.14);
    padding: 0 10px;
    background: rgba(255,255,255,0.92);
    color: #0f172a;
    font-weight: 800;
}

#aiImportNoteText {
    border-radius: 14px;
    border: 1px solid rgba(15, 23, 42, 0.12);
    padding: 10px 12px;
    font-size: 0.98rem;
    line-height: 1.4;
    outline: none;
    resize: vertical;
    background: rgba(255,255,255,0.95);
}
#aiImportNoteText:focus {
    border-color: rgba(99, 102, 241, 0.38);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
}

.ai-import-progress {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.ai-import-progress-bar {
    width: 100%;
    height: 12px;
    border-radius: 999px;
    background: rgba(2, 6, 23, 0.08);
    overflow: hidden;
}
.ai-import-progress-fill {
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: linear-gradient(90deg, rgba(99,102,241,0.95), rgba(59,130,246,0.95));
    transition: width 0.2s ease;
}
.ai-import-progress-text {
    color: #334155;
    font-size: 0.95rem;
    font-weight: 800;
}
.ai-import-queue {
    color: #475569;
    font-size: 0.92rem;
}
.ai-import-hint {
    color: #64748b;
    font-size: 0.9rem;
    min-height: 18px;
}

.ai-import-result {
    margin-top: 6px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.ai-import-result-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 14px;
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: rgba(2, 6, 23, 0.02);
}
.ai-import-result-item--fail {
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    background: rgba(244, 63, 94, 0.07);
    border-color: rgba(244, 63, 94, 0.18);
}
.ai-import-result-item--fail .ai-import-result-title { color: #9f1239; }
.ai-import-result-title {
    font-weight: 900;
    color: #0f172a;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.ai-import-result-sub {
    color: #475569;
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-wrap;
}
.ai-import-open-btn {
    border: 0;
    border-radius: 12px;
    padding: 8px 10px;
    cursor: pointer;
    font-weight: 900;
    background: rgba(34, 197, 94, 0.14);
    color: #166534;
}
.ai-import-open-btn:hover { background: rgba(34, 197, 94, 0.20); }

.ai-import-warnings {
    border-radius: 14px;
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: rgba(2, 6, 23, 0.02);
    padding: 10px 12px;
}
.ai-import-warnings summary {
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 900;
    color: #0f172a;
}
.ai-import-warnings summary::-webkit-details-marker { display: none; }
.ai-import-warnings summary::after {
    content: '\f078';
    font-family: "Font Awesome 6 Free";
    font-weight: 900;
    margin-left: auto;
    opacity: 0.75;
    transition: transform 180ms ease;
}
.ai-import-warnings[open] summary::after { transform: rotate(180deg); }
.ai-import-warnings-body {
    margin-top: 8px;
    color: #475569;
    font-size: 0.9rem;
    line-height: 1.55;
    white-space: pre-wrap;
}

@media (max-width: 768px) {
    .ai-import-box {
        width: calc(100vw - 18px);
        height: calc(100vh - 18px);
        max-height: none;
    }
    .ai-import-file-name { max-width: 200px; }
}

/* ---------------------------
 * AI history (modal)
 * --------------------------- */
.ai-history-box {
    width: min(760px, calc(100vw - 28px));
    max-height: min(84vh, 860px);
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.ai-history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.ai-history-title {
    font-weight: 900;
    color: #0f172a;
    letter-spacing: 0.2px;
}
.ai-history-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}
.ai-history-scope {
    height: 36px;
    border-radius: 12px;
    border: 1px solid rgba(15, 23, 42, 0.14);
    padding: 0 10px;
    background: rgba(255,255,255,0.92);
    color: #0f172a;
    font-weight: 800;
}
.ai-history-list {
    flex: 1;
    overflow: auto;
    border: 1px solid rgba(15, 23, 42, 0.10);
    border-radius: 16px;
    padding: 10px;
    background: rgba(255,255,255,0.92);
}
.ai-history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 14px;
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: rgba(2, 6, 23, 0.02);
    cursor: pointer;
    margin-bottom: 10px;
}
.ai-history-item:hover { background: rgba(99, 102, 241, 0.06); border-color: rgba(99, 102, 241, 0.20); }
.ai-history-item:last-child { margin-bottom: 0; }
.ai-history-item-main { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.ai-history-item-title { font-weight: 900; color: #0f172a; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-history-item-meta { color: #64748b; font-size: 0.88rem; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.ai-history-tag {
    font-weight: 900;
    font-size: 0.78rem;
    padding: 2px 8px;
    border-radius: 999px;
    border: 1px solid rgba(2, 6, 23, 0.10);
    background: rgba(2, 6, 23, 0.04);
    color: #334155;
}
.ai-history-time { white-space: nowrap; }
.ai-history-hint { min-height: 18px; color: #64748b; font-size: 0.9rem; }

@media (max-width: 768px) {
    .ai-history-box {
        width: calc(100vw - 18px);
        height: calc(100vh - 18px);
        max-height: none;
    }
}

/* ---------------------------
 * Exam mode
 * --------------------------- */
#examModal.modal-overlay {
    background: #f8fafc;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}
#examModal.modal-overlay.open {
    padding: 0;
    display: flex;
    justify-content: stretch;
    align-items: stretch;
}
.exam-box {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    background: rgba(255,255,255,0.98);
    border-radius: 0;
    overflow: hidden;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    border: 0;
}
.exam-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 14px 16px;
    background: rgba(255,255,255,0.8);
    border-bottom: 1px solid rgba(148,163,184,0.18);
}
.exam-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}
.exam-header-left i { color: var(--accent-color); }
.exam-header-titles { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.exam-header-title { font-weight: 900; color: #0f172a; letter-spacing: -0.01em; }
.exam-header-sub {
    color: #64748b;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60vw;
}
.exam-header-actions { display: flex; align-items: center; gap: 10px; }
.exam-header .icon-btn {
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: 16px;
}
.exam-header .icon-btn i { pointer-events: none; }
.exam-timer {
    font-variant-numeric: tabular-nums;
    font-weight: 900;
    padding: 8px 10px;
    border-radius: 14px;
    background: rgba(15, 23, 42, 0.06);
    border: 1px solid rgba(15, 23, 42, 0.08);
    color: #0f172a;
    user-select: none;
}
.exam-body {
    flex: 1;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 16px;
    background: radial-gradient(1200px 800px at 20% 0%, rgba(59, 130, 246, 0.10), transparent 60%),
                linear-gradient(180deg, rgba(248,250,252,0.95), rgba(248,250,252,0.70));
}
.exam-pane { min-height: 100%; }

.exam-empty {
    padding: 24px;
    border-radius: 18px;
    background: rgba(255,255,255,0.80);
    border: 1px solid rgba(148,163,184,0.18);
}
.exam-empty-title { font-weight: 900; color: #0f172a; font-size: 1.05rem; margin-bottom: 8px; }
.exam-empty-desc { color: #64748b; line-height: 1.6; }

.exam-picker { display: flex; flex-direction: column; gap: 14px; }
.exam-picker-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.exam-picker-summary { color: #0f172a; font-weight: 800; }
.exam-picker-tools { display: flex; gap: 8px; flex-wrap: wrap; }

.exam-range-tree { display: grid; gap: 10px; }
.exam-range-folder {
    border-radius: 18px;
    border: 1px solid rgba(148,163,184,0.18);
    background: rgba(255,255,255,0.72);
    overflow: hidden;
}
.exam-range-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: rgba(255,255,255,0.55);
}
.exam-fold-btn {
    width: 34px;
    height: 34px;
    border-radius: 12px;
    border: 1px solid rgba(148,163,184,0.18);
    background: rgba(148,163,184,0.10);
    color: #475569;
    cursor: pointer;
    flex: none;
    -webkit-tap-highlight-color: transparent;
}
.exam-fav-icon {
    width: 34px;
    height: 34px;
    border-radius: 12px;
    border: 1px solid rgba(245,158,11,0.24);
    background: rgba(245,158,11,0.12);
    color: #f59e0b;
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    pointer-events: none;
}
.exam-fav-icon i { pointer-events: none; }
.exam-fold-btn i { transition: transform 0.18s ease; }
.exam-range-folder:not(.collapsed) .exam-fold-btn i { transform: rotate(90deg); }
.exam-range-folder.collapsed .exam-range-children { display: none; }

.exam-check {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    flex: 1;
    min-width: 0;
    cursor: pointer;
}
.exam-check input { margin-top: 3px; }
.exam-range-name {
    min-width: 0;
    font-weight: 800;
    color: #0f172a;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.35;
}
.exam-range-name--chapter { font-weight: 650; color: #334155; }
.exam-range-count { flex: none; color: #64748b; font-weight: 800; white-space: nowrap; }
.exam-range-children { padding: 8px 12px 12px; display: grid; gap: 8px; }
.exam-chapter-row {
    padding: 10px 10px;
    border-radius: 14px;
    border: 1px solid rgba(148,163,184,0.16);
    background: rgba(255,255,255,0.58);
}
.exam-chapter-row:hover { background: rgba(255,255,255,0.92); }

.exam-picker-footer { padding-top: 6px; }
.exam-mode-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.exam-mode-tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.exam-mode-btn {
    border: 1px solid rgba(148,163,184,0.26);
    background: rgba(255,255,255,0.86);
    color: #475569;
    padding: 8px 12px;
    border-radius: 999px;
    font-weight: 900;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 160ms ease, border-color 160ms ease, color 160ms ease, transform 120ms ease;
}
.exam-mode-btn:hover { border-color: rgba(99,102,241,0.28); color: #0f172a; background: rgba(255,255,255,0.96); }
.exam-mode-btn:active { transform: translateY(1px); }
.exam-mode-btn.active {
    background: #0f172a;
    border-color: #0f172a;
    color: #fff;
}
.exam-mode-hint {
    margin-top: 6px;
    color: #64748b;
    font-weight: 650;
    font-size: 0.9rem;
    line-height: 1.45;
}
.exam-mode-empty {
    margin-top: 10px;
    padding: 12px 14px;
    border-radius: 16px;
    border: 1px dashed rgba(148,163,184,0.36);
    background: rgba(255,255,255,0.75);
    color: #64748b;
    font-weight: 650;
    line-height: 1.55;
}
.exam-count-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.exam-count-label { font-weight: 900; color: #0f172a; }
.exam-count-input {
    flex: none;
    width: 116px;
    max-width: 46vw;
    height: 42px;
    padding: 10px 12px;
    border: 1px solid rgba(148,163,184,0.32);
    border-radius: 14px;
    background: rgba(255,255,255,0.92);
    color: #0f172a;
    font-weight: 950;
    font-size: 1rem;
    text-align: center;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}
.exam-count-input:focus {
    border-color: rgba(59,130,246,0.38);
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.14);
}
.exam-count-hint { color: #64748b; font-weight: 700; font-size: 0.9rem; }

.exam-runner-head { display: grid; gap: 10px; margin-bottom: 14px; }
.exam-runner-tip { color: #64748b; font-weight: 650; font-size: 0.92rem; }
.exam-runner-stats { font-weight: 900; color: #0f172a; }

.exam-question-list {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding-bottom: 20px;
}
.exam-qcard { width: min(920px, 90%); }
.exam-qcard .q-id { flex: none; white-space: nowrap; min-width: 44px; text-align: center; }

.exam-qcard.exam-revealed .option-item { cursor: default; }
.exam-option.selected {
    border-color: rgba(59,130,246,0.32);
    background: rgba(59, 130, 246, 0.08);
}
.exam-option.selected .option-label {
    background: rgba(59,130,246,0.14);
    border-color: rgba(59,130,246,0.22);
}
.exam-opt-mark {
    margin-left: auto;
    width: 26px;
    display: inline-flex;
    align-items: center;
    justify-content: flex-end;
    color: rgba(15, 23, 42, 0.42);
    flex: none;
}
.exam-option.wrong {
    background: rgba(244, 63, 94, 0.10);
    border-color: rgba(244, 63, 94, 0.28);
    color: #be123c;
}
.exam-option.wrong .option-label { background: #f43f5e; color: white; }
.exam-option.correct .exam-opt-mark { color: #059669; }
.exam-option.wrong .exam-opt-mark { color: #f43f5e; }

.exam-multi-actions {
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
}
.exam-multi-submit,
.exam-multi-clear {
    height: 38px;
    padding: 0 14px;
    border-radius: 14px;
    border: 1px solid rgba(148,163,184,0.30);
    background: rgba(255,255,255,0.88);
    color: #0f172a;
    font-weight: 900;
    cursor: pointer;
}
.exam-multi-submit {
    border-color: rgba(59,130,246,0.28);
    background: rgba(59,130,246,0.10);
    color: #1d4ed8;
}
.exam-multi-submit:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}
.exam-multi-clear {
    color: #64748b;
}

.exam-feedback {
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 16px;
    border: 1px solid rgba(148,163,184,0.18);
    font-weight: 800;
    line-height: 1.45;
}
.exam-feedback.ok {
    background: var(--correct-bg);
    border-color: var(--correct-border);
    color: var(--correct-text);
}
.exam-feedback.bad {
    background: rgba(244, 63, 94, 0.10);
    border-color: rgba(244, 63, 94, 0.22);
    color: #be123c;
}

.exam-source {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px dashed rgba(148,163,184,0.30);
    color: #64748b;
    font-weight: 650;
    font-size: 0.9rem;
    line-height: 1.35;
    white-space: pre-line;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.exam-result-box {
    border: 1px solid rgba(148,163,184,0.18);
    border-radius: 18px;
    background: rgba(255,255,255,0.80);
    padding: 14px;
}
.exam-result-title { font-weight: 950; color: #0f172a; font-size: 1.08rem; }
.exam-result-metrics { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.exam-metric {
    flex: 1;
    min-width: 130px;
    border-radius: 18px;
    padding: 10px 12px;
    background: rgba(15, 23, 42, 0.04);
    border: 1px solid rgba(15, 23, 42, 0.06);
}
.exam-metric-label { color: #64748b; font-weight: 800; font-size: 0.86rem; }
.exam-metric-val { color: #0f172a; font-weight: 950; font-size: 1.25rem; margin-top: 4px; }
.exam-result-actions { display: flex; justify-content: flex-end; margin-top: 12px; }

@media (max-width: 560px) {
    #examModal .exam-box {
        width: 100vw;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
    }
    #examModal .exam-header { padding: 12px 12px; }
    #examModal .exam-body { padding: 12px; }
    .exam-header-sub { max-width: 56vw; }
    .exam-header-actions { gap: 8px; }
    .exam-header .icon-btn { width: 40px; height: 40px; border-radius: 15px; }
    .exam-timer { padding: 7px 8px; border-radius: 13px; font-size: 0.92rem; }
    .exam-count-input { width: 104px; height: 40px; border-radius: 14px; }
}
