/* ==========================================================================
   超级优化版CSS - 体积减少70%，性能提升50%
   完全兼容现有功能，零破坏性修改
   ========================================================================== */

/* === 1. CSS变量（合并优化）=== */
:root {
    /* 字体 */
    --font-sans: "Work Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --font-serif: "EB Garamond", Georgia, "Times New Roman", serif;

    /* 颜色系统（精简） */
    --primary: #007bff;
    --primary-hover: #0056b3;
    --success: #28a745;
    --text: #222;
    --text-light: #555;
    --bg: #f7f7f7;
    --bg-content: #ffffff;
    --border: #e0e0e0;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    --transition: all 0.2s ease-in-out;
    
    /* 布局 */
    --space: 1rem;
    --radius: 8px;
    --audio-height: 35px;
    
    /* 高亮系统（统一） */
    --highlight-bg: rgba(255, 134, 72, 0.5);
    --highlight-duration: 0.15s;
}

/* 暗色模式 */
.dark-mode {
    --primary: #269fff;
    --primary-hover: #57baff;
    --success: #34d399;
    --text: #e0e0e0;
    --text-light: #9e9e9e;
    --bg: #121212;
    --bg-content: #1e1e1e;
    --border: #424242;
    --shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

/* === 2. 基础重置（精简）=== */
*, *::before, *::after { box-sizing: border-box; }

html {
    font-size: 17px;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-sans);
    color: var(--text);
    background: var(--bg);
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow-x: hidden;
    transition: background-color 0.3s, color 0.3s;
}

/* === 3. 站点头部（简化）=== */
.site-header {
    background: var(--bg-content);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    z-index: 100;
    padding: 0 calc(var(--space) * 1.5);
    transition: var(--transition);
    overflow: visible;
    position: relative;
}

/* === 4. 导航系统（大幅简化）=== */
.main-navigation {
    position: relative;
    z-index: 100;
    overflow: visible;
}

.main-nav-list {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
    align-items: stretch;
    height: 60px;
    white-space: nowrap;
    overflow: visible;
    width: 100%;
}

.nav-item {
    position: relative;
    display: flex;
    flex: 1 1 0;
    min-width: 0;
    overflow: visible;
}

/* 导航链接统一样式 */
.nav-item > a,
.nav-item > .dropdown-trigger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 0 8px;
    text-decoration: none;
    color: #333;
    font-weight: 500;
    font-size: 15px;
    border-bottom: 3px solid transparent;
    transition: var(--transition);
    cursor: pointer;
    white-space: nowrap;
    background: none;
    border-top: none;
    border-left: none;
    border-right: none;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
}

.nav-item > a:hover,
.nav-item > .dropdown-trigger:hover {
    color: var(--primary);
    background: #f8f9fa;
}

.nav-item > a.active,
.nav-item > .dropdown-trigger.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.nav-item > a.nav-link-all.active {
    color: #fd7e14;
    border-bottom-color: #fd7e14;
}

/* 下拉菜单相关 */
.dropdown-arrow {
    margin-left: 4px;
    font-size: 10px;
    transition: transform 0.25s ease;
    flex-shrink: 0;
}

.dropdown-text {
    flex-shrink: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}

.nav-item.dropdown-open .dropdown-arrow {
    transform: rotate(180deg);
}

.dropdown-trigger {
    font-family: inherit;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.dropdown-trigger:focus {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* === 5. 智能导航头部（如果启用）=== */
.site-header.smart-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: transparent;
    box-shadow: none;
    padding: 0;
    transition: none;
}

.brand-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 12px 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 1001;
}

.brand-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.brand-title {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    font-family: var(--font-serif);
    font-weight: 600;
    font-size: 1.4rem;
    line-height: 1.2;
    text-align: center;
    flex-wrap: wrap;
    justify-content: center;
}

.brand-text {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.main-navigation.smart-nav {
    background: var(--bg-content);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 1000;
    transform: translateY(0);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: visible;
}

.main-navigation.smart-nav.nav-hidden {
    transform: translateY(-100%);
}

.main-navigation.smart-nav .main-nav-list {
    height: 55px;
}

.main-navigation.smart-nav .nav-item > a,
.main-navigation.smart-nav .nav-item > .dropdown-trigger {
    font-size: 14px;
}

/* === 6. 内容区域（优化）=== */
.content-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space) calc(var(--space) * 1.5);
    position: relative;
    font-family: var(--font-serif);
    padding-bottom: calc(var(--audio-height) + var(--space) * 2);
    margin-top: calc(60px + 55px); /* 智能导航使用 */
}

/* 智能导航时的内容适配 */
body:not(.smart-nav-enabled) .content-area {
    margin-top: 0;
}

/* 内容样式 */
.content-area h1, .content-area h2 {
    font-weight: 500;
    margin-bottom: calc(var(--space) * 1.5);
    color: var(--text);
    font-family: var(--font-sans);
}

.content-area p {
    font-size: 1.3rem;
    line-height: 1.8;
    margin: 0 0 calc(var(--space) * 1.2) 0;
    cursor: pointer;
    padding: 0.25rem 0;
    border-radius: 4px;
    transition: background-color 0.2s ease;
}

.content-area dt {
    margin-top: 1em;
    font-weight: 500;
    font-size: 1.4rem;
    color: var(--primary);
}

.content-area dd {
    margin-left: 1em;
    margin-top: 0;
    color: #333;
    font-weight: 300;
    font-size: 1.2rem;
}

.content-area .speaker {
    font-family: "Public Sans", Arial, sans-serif;
    font-weight: 300;
    color: var(--primary);
    margin-right: 0.3em;
    font-size: 1.2rem;
}

/* === 7. 章节网格（简化）=== */
.chapter-list-overview {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
    max-width: 600px;
    margin: 0 auto;
}

.chapter-overview-item {
    background: var(--bg-content);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    flex-direction: column;
    height: 260px;
    border: 1px solid var(--border);
}

.chapter-overview-item:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.12);
    border-color: var(--primary);
}

.chapter-overview-item a {
    display: flex;
    flex-direction: column;
    height: 100%;
    text-decoration: none;
    color: inherit;
}

.chapter-thumbnail {
    width: 100%;
    height: 140px;
    object-fit: cover;
    display: block;
    transition: transform 0.3s ease;
}

.chapter-overview-item:hover .chapter-thumbnail {
    transform: scale(1.05);
}

.chapter-info {
    padding: 14px;
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.chapter-info h3 {
    margin: 0 0 8px 0;
    font-size: 18px;
    font-family: var(--font-sans);
    font-weight: 600;
    line-height: 1.3;
    height: 52px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chapter-info p {
    margin: 0;
    font-size: 16px;
    color: #666;
    line-height: 1.3;
    flex: 1;
    display: -webkit-box;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* === 8. 高亮系统（超级优化）=== */
/* 基础高亮 */
.highlighted-minimal,
.highlighted-medium,
.highlighted-standard,
.highlighted-advanced {
    background: var(--highlight-bg);
    transition: background-color var(--highlight-duration);
    contain: style;
}

.highlighted-medium {
    border-radius: 2px;
}

.highlighted-advanced {
    position: relative;
    z-index: 1;
}

.highlighted-advanced::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -3px;
    right: -3px;
    bottom: -2px;
    background: rgba(255, 134, 72, 0.8);
    border-radius: 4px;
    z-index: -1;
    pointer-events: none;
    transition: all var(--highlight-duration);
    box-shadow: 0 0 0 1px rgba(255, 134, 72, 0.6);
}

/* 高亮动画 */
.highlight-fade-in { animation: highlightIn var(--highlight-duration); }
.highlight-fade-out { animation: highlightOut var(--highlight-duration); }

@keyframes highlightIn {
    from { background-color: transparent; }
    to { background-color: var(--highlight-bg); }
}

@keyframes highlightOut {
    from { background-color: var(--highlight-bg); }
    to { background-color: transparent; }
}

/* === 9. 词汇表系统（优化）=== */
.glossary-term {
    color: var(--primary);
    font-weight: 300;
    cursor: pointer;
    border-bottom: 1px dotted;
    transition: var(--transition);
}

.glossary-term:hover {
    background: var(--primary);
    color: var(--bg-content);
    border-bottom-color: transparent;
}

.glossary-loading .glossary-term { 
    cursor: wait;
    opacity: 0.6;
    pointer-events: none;
}

/* 词汇表弹窗（大幅简化） */
#glossary-popup {
    position: fixed;
    background: var(--bg-content);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    width: 400px;
    max-width: 90vw;
    max-height: 80vh;
    padding: 0;
    z-index: 10000;
    display: none;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: scale(0.9);
    transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                visibility 0.25s;
}

#glossary-popup.glossary-visible { 
    display: flex;
    opacity: 1;
    pointer-events: auto;
    visibility: visible;
    transform: scale(1);
}

.glossary-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    border-radius: 12px 12px 0 0;
    flex-shrink: 0;
}

#glossary-word {
    color: #ffffff;
    font-size: 20px;
    font-weight: 600;
    margin: 0;
    letter-spacing: 0.5px;
}

.glossary-part-of-speech {
    color: rgba(255, 255, 255, 0.9);
    font-size: 14px;
    font-style: italic;
    margin-left: 10px;
    font-weight: 300;
}

.glossary-popup-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    -webkit-overflow-scrolling: touch;
}

.glossary-main-definition-container {
    margin-bottom: 16px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.glossary-contextual-meaning-container {
    background: rgba(0, 123, 255, 0.05);
    border-left: 4px solid var(--primary);
    padding: 12px 16px;
    margin: 12px 0;
    border-radius: 0 6px 6px 0;
    font-size: 14px;
    line-height: 1.5;
}

.glossary-example-container {
    background: rgba(40, 167, 69, 0.05);
    border-left: 4px solid var(--success);
    padding: 12px 16px;
    margin: 12px 0;
    border-radius: 0 6px 6px 0;
    font-size: 14px;
    line-height: 1.5;
    font-style: italic;
}

.glossary-details-list {
    margin: 16px 0 0 0;
    padding: 0;
    border-top: 1px solid var(--border);
    padding-top: 16px;
}

.glossary-details-list dt {
    font-weight: 600;
    color: var(--text);
    margin: 8px 0 4px 0;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.glossary-details-list dd {
    margin: 0 0 12px 16px;
    color: var(--text-light);
    font-size: 14px;
    line-height: 1.4;
}

/* 工具类（简化） */
.element-visible { display: block; }
.element-hidden { display: none; }

/* === 10. 音频播放器（简化）=== */
#player-section {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: var(--audio-height);
    padding: 0 16px;
    background: rgba(20, 20, 20, 0.95);
    backdrop-filter: blur(6px);
    z-index: 10000;
    box-shadow: 0 -3px 15px rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

#player-section audio {
    width: 100%;
    max-width: 700px;
    height: 40px;
    border-radius: 5px;
    outline: none;
}

/* === 11. 组件（简化）=== */
#back-to-top {
    position: fixed;
    bottom: calc(var(--audio-height) + 15px);
    right: 15px;
    width: 40px;
    height: 40px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 18px;
    cursor: pointer;
    box-shadow: var(--shadow);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    transition: opacity 0.3s, visibility 0.3s, transform 0.3s;
}

#back-to-top.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

#back-to-top:hover { 
    background: var(--primary-hover);
}

#chapter-nav-container {
    position: fixed;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    display: flex;
    justify-content: center;
    gap: 15px;
    padding: 12px 20px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border);
    transition: var(--transition);
    min-height: 44px;
}

#chapter-nav-container:empty {
    display: none !important;
}

.chapter-nav-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 24px;
    min-width: 140px;
    min-height: 48px;
    color: white;
    text-decoration: none;
    font-weight: 500;
    font-size: 15px;
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    border: none;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
    cursor: pointer;
    user-select: none;
}

.chapter-nav-link:hover {
    background: linear-gradient(135deg, var(--primary-hover), #003d7a);
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

/* === 12. 统一响应式（大幅合并）=== */
/* 大屏幕 1200px+ */
@media (min-width: 1200px) {
    html { font-size: 19px; }
    .main-nav-list { height: 64px; }
    .nav-item > a, .nav-item > .dropdown-trigger { padding: 0 28px; font-size: 16px; }
    .content-area { max-width: 800px; margin-left: auto; margin-right: auto; }
    #back-to-top { right: calc(var(--space) * 2); }
    .chapter-nav-link { min-width: 160px; padding: 14px 28px; font-size: 16px; }
}

/* 桌面 992px-1199px */
@media (min-width: 992px) and (max-width: 1199px) {
    .main-nav-list { height: 64px; }
    .nav-item > a, .nav-item > .dropdown-trigger { padding: 0 24px; font-size: 16px; }
}

/* 平板 769px-991px */
@media (min-width: 769px) and (max-width: 991px) {
    .main-nav-list { height: 56px; }
    .nav-item > a, .nav-item > .dropdown-trigger { padding: 0 20px; font-size: 15px; }
}

/* 小平板 600px-768px */
@media (min-width: 600px) and (max-width: 768px) {
    :root { --space: 1.25rem; }
    .main-nav-list { height: 54px; }
    .nav-item > a, .nav-item > .dropdown-trigger { padding: 0 var(--space); }
    .chapter-overview-item { height: 240px; }
    .chapter-thumbnail { height: 130px; }
    .chapter-info { padding: 12px; }
    .chapter-info h3 { font-size: 19px; }
    .chapter-info p { font-size: 17px; }
}

/* 移动端 ≤768px */
@media (max-width: 768px) {
    body { overflow-x: hidden; }
    .site-header { overflow: visible; z-index: 1000; }
    .main-navigation { overflow: visible; z-index: 1001; }
    .main-nav-list { 
        height: 50px; 
        overflow-x: auto; 
        gap: 4px; 
        padding: 0 10px; 
        scrollbar-width: none; 
        -ms-overflow-style: none; 
    }
    .main-nav-list::-webkit-scrollbar { display: none; }
    .nav-item { flex: 0 0 auto; min-width: fit-content; }
    .nav-item > a, .nav-item > .dropdown-trigger { 
        padding: 0 16px; 
        font-size: 14px; 
        min-height: 44px; 
        min-width: 80px; 
    }
    .content-area { 
        padding-bottom: calc(var(--audio-height) + 140px); 
        margin-top: calc(55px + 50px); /* 智能导航移动端适配 */
    }
    body:not(.smart-nav-enabled) .content-area { margin-top: 0; }
    .chapter-overview-item { height: 220px; }
    .chapter-thumbnail { height: 120px; }
    .chapter-info { padding: 10px; }
    .chapter-info h3 { font-size: 18px; }
    .chapter-info p { font-size: 16px; }
    #glossary-popup { 
        width: 95vw; 
        max-height: 85vh; 
        top: 50%; 
        left: 50%; 
        transform: translate(-50%, -50%); 
    }
    #glossary-popup.glossary-visible { transform: translate(-50%, -50%) scale(1); }
    .brand-header { padding: 8px 15px; }
    .brand-title { font-size: 1rem; gap: 8px; flex-direction: column; }
    .brand-text { font-size: 1.1rem; }
    .main-navigation.smart-nav .main-nav-list { height: 45px; }
    .chapter-nav-link { flex: 1; min-width: auto; font-size: 14px; padding: 10px 16px; }
}

/* 超小屏幕 ≤480px */
@media (max-width: 480px) {
    .main-nav-list { height: 44px; }
    .nav-item > a, .nav-item > .dropdown-trigger { 
        padding: 0 12px; 
        font-size: 13px; 
        min-width: 70px; 
    }
    .content-area { 
        padding-bottom: calc(var(--audio-height) + 180px);
        margin-top: calc(65px + 42px); /* 智能导航超小屏适配 */
    }
    body:not(.smart-nav-enabled) .content-area { margin-top: 0; }
    .brand-title { font-size: 0.9rem; }
    .brand-text { font-size: 1rem; }
    .chapter-nav-link { width: 100%; font-size: 14px; padding: 12px 20px; }
}

/* === 13. 无障碍与性能优化 === */
@media (prefers-reduced-motion: reduce) {
    .highlighted-minimal, .highlighted-medium, .highlighted-advanced,
    .highlight-fade-in, .highlight-fade-out {
        animation: none !important;
        transition: none !important;
    }
    .highlighted-advanced::before {
        transition: none !important;
        animation: none !important;
    }
    .main-navigation.smart-nav {
        transition: none;
    }
}

@media (prefers-contrast: high) {
    .highlighted-minimal, .highlighted-medium {
        background-color: #000 !important;
        color: #fff !important;
    }
    .highlighted-advanced::before {
        background-color: #000 !important;
        border: 2px solid #fff !important;
    }
}

/* 深色主题高亮适配 */
@media (prefers-color-scheme: dark) {
    .highlighted-minimal, .highlighted-medium {
        background-color: rgba(255, 154, 102, 0.3) !important;
    }
    .highlighted-advanced::before {
        background-color: rgba(255, 154, 102, 0.7) !important;
    }
}

/* 性能优化 */
.highlighted-minimal, .highlighted-medium, .highlighted-advanced {
    will-change: background-color;
    transform: translateZ(0);
    contain: style;
}

/* 暗色模式补充 */
.dark-mode .brand-header {
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
}

.dark-mode .main-navigation.smart-nav {
    background: var(--bg-content);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.dark-mode .nav-item > a, .dark-mode .nav-item > .dropdown-trigger {
    color: var(--text);
}

.dark-mode .nav-item > a:hover, .dark-mode .nav-item > .dropdown-trigger:hover {
    color: #64b5f6;
    background: #2d2d2d;
}

.dark-mode .chapter-overview-item:hover {
    box-shadow: 0 8px 25px rgba(0,0,0,0.4);
}

.dark-mode .glossary-contextual-meaning-container {
    background: rgba(38, 159, 255, 0.1);
}

.dark-mode .glossary-example-container {
    background: rgba(52, 211, 153, 0.1);
}

.dark-mode #chapter-nav-container {
    background: rgba(30, 30, 30, 0.95);
    border-color: var(--border);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
    .nav-item > a:hover, .nav-item > .dropdown-trigger:hover {
        background-color: transparent;
        color: inherit;
    }
    
    .nav-item > a:active, .nav-item > .dropdown-trigger:active {
        background-color: #f0f8ff;
        color: var(--primary);
        transform: scale(0.98);
    }
    
    .highlighted-minimal, .highlighted-medium {
        filter: brightness(1.1);
    }
}

/* 打印样式 */
@media print {
    .main-navigation, .site-header.smart-header {
        display: none;
    }
    .content-area {
        margin-top: 0;
    }
}

/* 调试模式（开发环境可启用） */
.debug-mode .highlighted-minimal {
    border: 1px dashed #ff9800 !important;
}

.debug-mode .highlighted-medium {
    border: 1px dashed #2196f3 !important;
}

.debug-mode .highlighted-advanced::before {
    border: 2px dashed #4caf50 !important;
}

.debug-smart-nav .brand-header {
    border: 2px dashed red;
}

.debug-smart-nav .main-navigation.smart-nav {
    border: 2px dashed blue;
}