/* ===== 全局布局 ===== */
html {
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: #f8fafc;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    overflow-y: scroll; /* 强制显示滚动条，避免滚动问题 */
}

.page-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ===== 顶部导航栏 (只针对直接子元素) ===== */
.page-container > header {
    height: 64px;
    background: white;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    flex-shrink: 0;
    z-index: 100;
    position: sticky;
    top: 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.page-container > header input[type="text"] {
    font-size: 1.25rem;
    font-weight: 700;
    color: #1e293b;
    border: none;
    outline: none;
    background: transparent;
    width: 100%;
}

/* ===== x-cloak ===== */
[x-cloak] {
    display: none !important;
}

/* ===== 阅读模式布局 ===== */
.reading-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    max-width: 1480px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    width: 100%;
}

/* 两列布局（内容 + 右侧目录） */
@media (min-width: 1024px) {
    .reading-layout {
        grid-template-columns: minmax(0, 1fr) 280px;
        gap: 3rem;
    }
}

/* 三列布局（仅当有引用链接时） - 左侧栏 + 内容 + 右侧目录 */
@media (min-width: 1440px) {
    .reading-layout.has-references {
        grid-template-columns: 360px minmax(650px, 1fr) 280px;
        max-width: 1920px;
        gap: 3rem;
        margin-left: auto;
        margin-right: auto;
        margin-left: calc(50% - 960px - 80px);
        padding-left: 2rem;
        padding-right: 2rem;
    }
}

/* ===== 编辑模式布局 ===== */
.editing-layout {
    display: grid;
    grid-template-columns: 1fr 280px;
    gap: 3rem;
    max-width: 1280px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    /* flex: 1 1 auto; - 移除，可能导致高度限制 */
    width: 100%;
}

/* 编辑器主内容区 */


/* TOC在右侧 - 与阅读模式一致 */
.editing-layout .toc-sidebar {
    position: sticky;
    top: 5rem; /* 与阅读模式保持一致 */
    max-height: calc(100vh - 8rem);
    overflow-y: auto;
    padding-left: 0.5rem;
}

@media (max-width: 1024px) {
    .editing-layout {
        grid-template-columns: 1fr;
    }

    .editing-layout .toc-sidebar {
        display: none;
    }
}

/* ===== 阅读模式内容区域 ===== */
.reading-main {
    background: white;
    border-radius: 1.25rem;
    box-shadow: 0 20px 40px rgba(15, 23, 42, 0.08);
    border: 1px solid rgba(148, 163, 184, 0.3);
    padding: 3rem;
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}

/* ===== 文章头部 ===== */
.article-header {
    text-align: center;
    margin-bottom: 3rem;
    padding-bottom: 2rem;
    border-bottom: 1px solid #e2e8f0;
}

.article-title {
    font-size: 2.5rem;
    font-weight: 800;
    color: #1e293b;
    margin-bottom: 1.5rem;
    line-height: 1.2;
}

@media (max-width: 768px) {
    .article-title {
        font-size: 1.875rem;
    }
}

.article-meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    font-size: 0.875rem;
    color: #64748b;
}

.article-meta-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    color: inherit;
}

.article-meta-item:hover {
    color: #ff7e5f;
}

.article-meta-separator {
    width: 4px;
    height: 4px;
    background: #cbd5e1;
    border-radius: 50%;
}

.article-tags {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 1rem;
    flex-wrap: wrap;
}

.tag-badge {
    font-size: 0.75rem;
    padding: 0.25rem 0.75rem;
    background: #f8fafc;
    color: #64748b;
    border: 1px solid #e2e8f0;
    border-radius: 9999px;
    text-decoration: none;
    transition: all 0.2s;
}

.tag-badge:hover {
    border-color: #ff7e5f;
    color: #ff7e5f;
    background: #fff7ed;
}

/* ===== TOC 侧边栏 ===== */
.toc-sidebar {
    position: sticky;
    top: 5rem;
    max-height: calc(100vh - 8rem);
    overflow-y: auto;
    padding-right: 0.5rem;
}

.toc-sidebar::-webkit-scrollbar {
    width: 4px;
}

.toc-sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.toc-sidebar::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 2px;
}

.toc-sidebar::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

.toc-title {
    font-weight: 700;
    font-size: 0.875rem;
    color: #1e293b;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.toc-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.toc-link {
    display: block;
    padding: 0.375rem 0 0.375rem 1rem;
    border-left: 2px solid transparent;
    color: #64748b;
    transition: all 0.2s;
    text-decoration: none;
    font-size: 0.875rem;
    line-height: 1.5;
}

.toc-link:hover {
    color: #ff7e5f;
    border-left-color: #ffc9b8;
}

.toc-link.active {
    color: #ea580c;
    border-left-color: #ff7e5f;
    font-weight: 600;
}

.toc-link.depth-1 { 
    padding-left: 0.5rem; 
    font-weight: 600; 
}

.toc-link.depth-2 { 
    padding-left: 1.5rem; 
}

.toc-link.depth-3 { 
    padding-left: 2.5rem; 
    font-size: 0.8125rem; 
}

.toc-link.depth-4 { 
    padding-left: 3.5rem; 
    font-size: 0.75rem; 
    color: #94a3b8; 
}

/* ===== Prose 样式 ===== */
.prose {
    max-width: none !important;
    color: #334155;
    line-height: 1.75;
}

.prose h1, 
.prose h2, 
.prose h3, 
.prose h4 {
    color: #1e293b;
    font-weight: 700;
    margin-top: 2em;
    margin-bottom: 1em;
    line-height: 1.3;
    scroll-margin-top: 5rem;
}

.prose h1 { font-size: 2.25em; }
.prose h2 { font-size: 1.875em; }
.prose h3 { font-size: 1.5em; }
.prose h4 { font-size: 1.25em; }

.prose p {
    margin-top: 1.25em;
    margin-bottom: 1.25em;
}

/* 注意：段落和标题的 text-align 通过 inline style 设置，
   inline style 的优先级高于 class 选择器，所以会自动生效 */

.prose a {
    color: #ff7e5f;
    text-decoration: underline;
    text-decoration-color: #ffc9b8;
    transition: all 0.2s;
}

.prose a:hover {
    color: #ea580c;
    text-decoration-color: #ff7e5f;
}

.prose img {
    border-radius: 0.75rem;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    max-width: 100%;
    height: auto;
}

.prose ul,
.prose ol {
    margin-top: 1.25em;
    margin-bottom: 1.25em;
    padding-left: 1.625em;
}

.prose li {
    margin-top: 0.5em;
    margin-bottom: 0.5em;
}

/* 多级有序列表的样式：使用 data-depth 属性来精确控制 */
.prose ol[data-depth="0"] {
    list-style-type: decimal; /* 1, 2, 3, ... */
}

.prose ol[data-depth="1"] {
    list-style-type: upper-alpha; /* A, B, C, ... */
}

.prose ol[data-depth="2"] {
    list-style-type: lower-alpha; /* a, b, c, ... */
}

.prose ol[data-depth="3"] {
    list-style-type: lower-roman; /* i, ii, iii, ... */
}

.prose ol[data-depth="4"] {
    list-style-type: decimal; /* 回到数字 */
}

/* 默认样式（无 data-depth 属性时的降级方案） */
.prose ol:not([data-depth]) {
    list-style-type: decimal;
}

.prose ol:not([data-depth]) ol:not([data-depth]) {
    list-style-type: upper-alpha;
}

.prose ol:not([data-depth]) ol:not([data-depth]) ol:not([data-depth]) {
    list-style-type: lower-alpha;
}

.prose ol:not([data-depth]) ol:not([data-depth]) ol:not([data-depth]) ol:not([data-depth]) {
    list-style-type: lower-roman;
}

/* ===== Code 样式 ===== */
.prose pre[class*="language-"] {
    margin: 1.5em 0;
    padding: 1.25em;
    border-radius: 0.75rem;
    background-color: #282c34;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow-x: auto;
}

.prose pre[class*="language-"] code {
    background: transparent;
    color: #abb2bf;
    font-family: 'Fira Code', 'Courier New', monospace;
    font-size: 14px;
    line-height: 1.6;
    padding: 0;
    margin: 0;
    display: block;
    white-space: pre;
}

/* ===== Math 样式 ===== */
.arithmatex,
mjx-container,
mjx-math,
.MathJax,
.MathJax_Display,
.katex,
.katex-display,
.math-inline {
    color: #2563eb !important;
    background-color: transparent !important;
}

/* 块级公式容器 - 使用绝对定位实现公式居中、tag 右对齐 */
div.arithmatex {
    display: block;
    text-align: center;
    position: relative;
    padding: 0.5em;
    margin: 1em 0;
    min-height: 3em;
}

/* KaTeX display 容器 - 公式主体居中显示 */
div.arithmatex .katex-display {
    display: inline-block !important;
    text-align: left !important;
    margin: 0 !important;
    position: static !important;  /* 不创建定位上下文 */
}

/* 确保 katex 和 katex-html 也不创建定位上下文 */
div.arithmatex .katex,
div.arithmatex .katex-html {
    position: static !important;
}

/* 隐藏 KaTeX MathML（仅供屏幕阅读器） */
div.arithmatex .katex-mathml {
    display: none !important;
}

/* Tag 绝对定位到 div.arithmatex 容器的最右侧并垂直居中 */
div.arithmatex .tag {
    position: absolute !important;
    right: 0.5em !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    margin: 0 !important;
    padding: 0 !important;
}

/* 行内公式容器 */
span.arithmatex {
    display: inline;
    padding: 0;
    margin: 0;
}

/* 带ID的公式（可跳转） */
.arithmatex[id] {
    scroll-margin-top: 80px; /* 为固定header留出空间 */
    border-left: 3px solid transparent;
    transition: all 0.3s ease;
}

/* 当跳转到公式时的高亮效果 */
.arithmatex[id]:target {
    border-left-color: #f97316;
    background-color: rgba(249, 115, 22, 0.05);
    padding-left: 1em;
}

/* 公式引用链接样式 */
a[href^="#eq-"],
a.equation-ref {
    color: #f97316;
    text-decoration: none;
    font-weight: 600;
    padding: 0.1em 0.3em;
    border-radius: 3px;
    background-color: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.3);
    transition: all 0.2s ease;
    font-size: 0.9em;
}

a[href^="#eq-"]:hover,
a.equation-ref:hover {
    color: #2563eb;
    background-color: rgba(59, 130, 246, 0.2);
    border-color: #3b82f6;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(59, 130, 246, 0.2);
}

/* ===== Marker 高亮样式 ===== */
mark {
        background-color: #fff3cd;
        color: inherit;
        padding: 2px 0;
    }

/* KaTeX 行间公式 */
.katex-display {
    display: block !important;
    overflow-x: visible !important;
    margin: 1em 0 !important;
    padding: 0.5em 0 !important;
}

/* KaTeX 行内公式 */
.katex:not(.katex-display),
.math-inline {
    display: inline-block !important;
    overflow: visible !important;
    max-width: none !important;
    vertical-align: baseline !important;
}

/* MathJax 支持（向后兼容） */
mjx-container[display="true"] {
    display: block !important;
    overflow-x: auto !important;
    margin: 1em 0;
    padding: 0.5em 0;
}

mjx-container:not([display="true"]) {
    display: inline-block !important;
    overflow: visible !important;
    max-width: none !important;
    vertical-align: baseline !important;
}

/* ===== 相关文章 ===== */
.related-posts {
    margin-top: 4rem;
    padding-top: 2.5rem;
    border-top: 2px solid #e2e8f0;
}

.related-posts-title {
    font-size: 1.25rem;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 1.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.related-posts-title::before {
    content: '';
    width: 4px;
    height: 1.5rem;
    background: linear-gradient(to bottom, #ff7e5f, #feb47b);
    border-radius: 2px;
}

.related-posts-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

@media (min-width: 640px) {
    .related-posts-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .related-posts-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

.related-post-card {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 1.25rem;
    background: white;
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-decoration: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
    position: relative;
    overflow: hidden;
}

.related-post-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(to right, #ff7e5f, #feb47b);
    opacity: 0;
    transition: opacity 0.3s;
}

.related-post-card:hover::before {
    opacity: 1;
}

.related-post-card:hover {
    transform: translateY(-4px);
    border-color: rgba(255, 126, 95, 0.3);
    box-shadow: 0 12px 24px -8px rgba(255, 126, 95, 0.15),
                0 4px 12px -4px rgba(0, 0, 0, 0.08);
}

/* 类别标签 */
.related-post-category {
    display: inline-block;
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.25rem 0.625rem;
    border-radius: 6px;
    color: white;
    align-self: flex-start;
}

/* 标题 */
.related-post-title {
    font-weight: 600;
    font-size: 1rem;
    color: #1e293b;
    line-height: 1.5;
    transition: color 0.2s;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.related-post-card:hover .related-post-title {
    color: #ff7e5f;
}

/* 摘要 */
.related-post-excerpt {
    font-size: 0.8125rem;
    color: #64748b;
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* 标签容器 */
.related-post-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
}

.related-post-tag {
    font-size: 0.6875rem;
    font-weight: 600;
    padding: 0.125rem 0.5rem;
    border-radius: 4px;
    color: white;
}

/* 底部元数据 */
.related-post-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: auto;
    padding-top: 0.75rem;
    border-top: 1px solid #f1f5f9;
}

.related-post-date {
    font-size: 0.75rem;
    color: #94a3b8;
    font-weight: 500;
    font-family: 'Courier New', monospace;
}

.related-post-arrow {
    margin-left: auto;
    width: 16px;
    height: 16px;
    color: #cbd5e1;
    transition: all 0.3s;
}

.related-post-card:hover .related-post-arrow {
    color: #ff7e5f;
    transform: translateX(4px);
}

.related-post-card:hover .related-post-arrow {
    color: #ff7e5f;
    transform: translateX(4px);
}

/* 移动端优化 */
@media (max-width: 640px) {
    .related-posts {
        margin-top: 3rem;
    }

    .related-posts-title {
        font-size: 1.125rem;
    }

    .related-post-card {
        padding: 1rem;
    }

    .related-post-title {
        font-size: 0.875rem;
    }
}

/* ===== 左侧引用链接侧边栏 ===== */
.references-sidebar {
    display: none; /* 默认隐藏 */
    order: -1; /* 确保在左侧 */
}

/* 只在大屏幕且有引用时显示 */
@media (min-width: 1440px) {
    .has-references .references-sidebar {
        display: block;
    }
}

.references-title {
    font-size: 0.875rem;
    font-weight: 700;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.625rem;
}

.references-title svg {
    width: 18px;
    height: 18px;
    color: #94a3b8;
}

.references-list {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.reference-card {
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
    padding: 1rem;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    text-decoration: none;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
}

.reference-card:hover {
    transform: translateX(4px);
    border-color: rgba(255, 126, 95, 0.3);
    box-shadow: 0 4px 10px rgba(255, 126, 95, 0.1);
}

.reference-image {
    width: 100%;
    height: 120px;
    border-radius: 8px;
    overflow: hidden;
    background: #f1f5f9;
}

.reference-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.reference-card:hover .reference-image img {
    transform: scale(1.05);
}

.reference-content {
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
}

.reference-category {
    display: inline-block;
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.25rem 0.625rem;
    border-radius: 4px;
    color: white;
    align-self: flex-start;
}

.reference-title {
    font-size: 0.9375rem;
    font-weight: 600;
    color: #1e293b;
    line-height: 1.5;
    display: -webkit-box;
    line-clamp: 2;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    transition: color 0.2s;
}

.reference-card:hover .reference-title {
    color: #ff7e5f;
}

.reference-excerpt {
    font-size: 0.8125rem;
    color: #64748b;
    line-height: 1.6;
    display: -webkit-box;
    line-clamp: 3;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    margin: 0;
}

.reference-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
}

.reference-tag {
    font-size: 0.6875rem;
    font-weight: 600;
    padding: 0.1875rem 0.5rem;
    border-radius: 4px;
    color: white;
}

/* ===== 响应式 ===== */
@media (max-width: 1024px) {
    .toc-sidebar {
        display: none;
    }
}

@media (max-width: 768px) {
    .reading-layout {
        padding: 1rem;
    }
    
    .article-header {
        margin-bottom: 2rem;
        padding-bottom: 1.5rem;
    }
    
    .article-meta {
        gap: 0.75rem;
        font-size: 0.8125rem;
    }
    
    .page-container > header {
        padding: 0 1rem;
    }
}

/* ===== 图片对齐样式 ===== */
.image-tool {
    margin: 1.5rem 0;
}

.image-tool img {
    display: inline-block;
    max-width: 100%;
    height: auto;
}

/* 遗留的对齐类支持 */
.image-tool.align-left,
.image-enhanced-wrapper.align-left {
    text-align: left;
}

.image-tool.align-center,
.image-enhanced-wrapper.align-center {
    text-align: center;
}

.image-tool.align-right,
.image-enhanced-wrapper.align-right {
    text-align: right;
}

