/* ============================================
   文章页面布局系统 - 优化版 v2.0
   改进：TOC滚动高亮修复、焦点环、打印样式、响应式微调
   ============================================ */

:root {
    --article-max-width: 1300px;
    --sidebar-width: 280px;
    --content-padding: 2rem;
    --article-font-size: 1.05rem;
    --article-line-height: 1.75;
    --toc-max-height: 500px;
    --sidebar-sticky-top: 94px;
}

/* ========== 双栏网格容器 ========== */
.article-grid {
    max-width: var(--article-max-width);
    margin: var(--sidebar-sticky-top) auto 48px;
    padding: 0 1.5rem;
    display: grid;
    grid-template-columns: 1fr var(--sidebar-width);
    gap: 2.5rem;
    position: relative;
}

/* 左侧主内容区：防止溢出 */
.article-main {
    min-width: 0;
    overflow-x: hidden;
}

/* 右侧边栏：粘性定位，跟随滚动 */
.article-sidebar {
    position: sticky;
    top: var(--sidebar-sticky-top);
    align-self: start;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* ========== 文章卡片 ========== */
.article-card {
    background: var(--surface-color);
    border-radius: var(--radius-xl);
    border: 1px solid var(--border-color);
    padding: var(--content-padding);
    margin-bottom: 2rem;
    transition: all var(--transition-normal);
    box-shadow: var(--shadow-sm);
}

.article-card:hover {
    box-shadow: var(--shadow-md);
    border-color: var(--border-color);
}

.article-title {
    font-size: 2.8rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin: 0 0 0.75rem 0;
    line-height: 1.2;
    color: var(--accent-color);
    text-align: center;
    font-family: var(--font-serif);
}

.article-subtitle {
    text-align: center;
    font-size: 1.2rem;
    color: var(--text-secondary);
    margin-bottom: 1.75rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border-light);
    font-weight: 400;
}

.article-meta-line {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1.5rem;
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.article-meta-updated {
    margin-top: -0.25rem;
    margin-bottom: 1rem;
}

.meta-item {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.meta-item i {
    color: var(--accent-color);
    width: 1rem;
    font-size: 0.9rem;
}

/* ========== GitHub 风格 Markdown 正文 ========== */
.article-body {
    font-size: var(--article-font-size);
    line-height: var(--article-line-height);
    color: var(--text-color);
}

.article-body > :first-child {
    margin-top: 0;
}

/* 标题样式与锚点偏移（平滑滚动偏移量） */
.article-body h1,
.article-body h2,
.article-body h3,
.article-body h4 {
    scroll-margin-top: 90px;
    margin-top: 1.8em;
    margin-bottom: 0.6em;
    font-weight: 600;
    letter-spacing: -0.01em;
    font-family: var(--font-serif);
    position: relative;
}

.article-body h1 {
    font-size: 2.2rem;
    padding-bottom: 0.3em;
    border-bottom: 1px solid var(--border-color);
}

.article-body h2 {
    font-size: 1.8rem;
    padding-bottom: 0.3em;
    border-bottom: 1px solid var(--border-light);
}

.article-body h3 {
    font-size: 1.5rem;
}

.article-body h4 {
    font-size: 1.25rem;
}

/* 标题锚点 */
.article-body .anchor {
    position: absolute;
    left: -1.2rem;
    top: 0;
    opacity: 0;
    text-decoration: none;
    color: var(--accent-color);
    transition: opacity 0.2s;
    font-weight: normal;
}

.article-body .anchor::before {
    content: '#';
    font-size: 0.9em;
}

.article-body h1:hover .anchor,
.article-body h2:hover .anchor,
.article-body h3:hover .anchor,
.article-body h4:hover .anchor {
    opacity: 1;
}

.article-body p {
    margin-bottom: 1.2em;
    max-width: 100%;
}

.article-body a {
    color: var(--accent-color);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color var(--transition-fast);
}

.article-body a:hover {
    border-bottom-color: var(--accent-color);
}

.article-body ul,
.article-body ol {
    margin: 0 0 1.2em 0;
    padding-left: 2em;
}

.article-body ul {
    list-style-type: disc;
}

.article-body ul ul {
    list-style-type: circle;
}

.article-body ul ul ul {
    list-style-type: square;
}

.article-body ol {
    list-style-type: decimal;
}

.article-body li {
    margin-bottom: 0.3em;
}

.article-body li > p {
    margin-bottom: 0.5em;
}

.article-body table {
    display: block;
    width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    margin: 1.5em 0;
    font-size: 0.9em;
}

.article-body table th,
.article-body table td {
    padding: 0.5rem 1rem;
    border: 1px solid var(--border-color);
    text-align: left;
}

.article-body table th {
    font-weight: 600;
    background-color: var(--surface-color-secondary);
    border-bottom-width: 2px;
}

.article-body table tr:nth-child(even) {
    background-color: var(--surface-color-secondary);
}

.article-body table tr:hover {
    background-color: color-mix(in srgb, var(--accent-color) 8%, transparent);
}

.article-body blockquote {
    margin: 1.5em 0;
    padding: 0.5em 1em;
    border-left: 4px solid var(--accent-color);
    background-color: var(--blockquote-bg);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    color: var(--text-secondary);
}

.article-body blockquote p {
    margin-bottom: 0.5em;
}

.article-body blockquote > :last-child {
    margin-bottom: 0;
}

.article-body code {
    font-family: var(--font-mono);
    background: var(--code-bg);
    padding: 0.2rem 0.4rem;
    border-radius: var(--radius-sm);
    font-size: 0.9em;
    color: var(--code-color);
}

.article-body pre {
    background: var(--code-bg);
    padding: 1rem;
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin: 1.2rem 0;
    border: 1px solid var(--border-light);
}

.article-body pre code {
    background: transparent;
    padding: 0;
    font-size: 0.9em;
    color: inherit;
}

.article-body hr {
    height: 0.25em;
    padding: 0;
    margin: 1.5em 0;
    background-color: var(--border-color);
    border: 0;
}

.article-body img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 1.8rem auto;
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.article-body img:hover {
    transform: scale(1.01);
    box-shadow: var(--shadow-md);
}

.lazy-image {
    background-color: var(--tag-bg);
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="%23999" stroke-width="1.5"><rect x="2" y="2" width="20" height="20" rx="2.18"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-4-3 3-5-5-4 5"/></svg>');
    background-repeat: no-repeat;
    background-position: center;
    background-size: 40px;
    min-height: 200px;
}

.lazy-image.loaded {
    background: none;
}

.image-alt-text {
    display: block;
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: -0.5rem;
    margin-bottom: 1rem;
    font-style: italic;
}

.article-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-light);
}

.article-tags .tag {
    background: var(--tag-bg);
    padding: 0.4rem 1rem;
    border-radius: var(--radius-full);
    font-size: 0.85rem;
    transition: all var(--transition-fast);
}

.article-tags .tag:hover {
    background: var(--accent-color);
    color: white;
    transform: translateY(-2px);
}

/* ========== 评论区卡片 ========== */
.comments-card {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);
    padding: 1.8rem;
    box-shadow: var(--shadow-sm);
}

.comments-card h3 {
    font-size: 1.3rem;
    margin: 0 0 1rem 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--accent-color);
}

/* ========== 侧边栏卡片通用样式 ========== */
.sidebar-card {
    background: var(--surface-color-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.2rem;
    transition: all var(--transition-normal);
}

.sidebar-card:hover {
    border-color: var(--accent-light);
    box-shadow: var(--shadow-sm);
}

/* ========== TOC 卡片 - 严格分离标题与滚动区 ========== */
.sidebar-card.toc-card {
    display: flex;
    flex-direction: column;
    max-height: min(500px, calc(100vh - 160px));
    overflow: hidden;
    padding: 0;
}

.toc-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent-color);
    padding: 1rem 1.2rem;
    border-bottom: 1px solid var(--border-light);
    background: inherit;
    cursor: default;
    user-select: none;
}

.toc-header i {
    font-size: 1rem;
}

/* 可滚动容器：独立滚动，自适应剩余高度 */
.toc-list-wrapper {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
    scrollbar-width: thin;
    scrollbar-color: var(--accent-color) var(--bg-color);
    scroll-behavior: smooth;
}

.toc-nav {
    padding: 0;
    margin: 0;
}

.toc-list,
.toc-list ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

.toc-list li {
    margin: 0;
    padding: 0;
    line-height: 1.4;
}

.toc-list a {
    display: block;
    padding: 0.4rem 0.8rem 0.4rem 1.5rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    word-break: break-word;
    white-space: normal;
}

.toc-list ul a {
    padding-left: 2rem;
}

.toc-list ul ul a {
    padding-left: 2.8rem;
}

.toc-list ul ul ul a {
    padding-left: 3.6rem;
}

.toc-list ul ul ul ul a {
    padding-left: 4.4rem;
}

.toc-list a:hover {
    color: var(--accent-color);
    background: rgba(180, 91, 99, 0.08);
    transform: translateX(2px);
}

/* 当前激活项高亮（修复滚动高亮） */
.toc-list li.active > a {
    color: var(--accent-color);
    background: rgba(180, 91, 99, 0.12);
    font-weight: 500;
    border-left: 3px solid var(--accent-color);
    padding-left: calc(1.5rem - 3px);
}

.toc-empty {
    padding: 1rem;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.reading-progress-wrapper {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(0, 0, 0, 0.05);
    padding: 2px 10px;
    border-radius: 9999px;
}

.reading-percent {
    font-weight: 600;
    color: var(--accent-color);
    min-width: 42px;
    text-align: center;
    font-family: var(--font-mono);
    font-size: 0.75rem;
}

.reading-progress-container {
    width: 60px;
    height: 4px;
    background: var(--border-color);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.reading-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--accent-color), var(--accent-light));
    border-radius: var(--radius-full);
    transition: width 0.1s linear;
}

.info-card {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.info-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
}

.info-item i {
    width: 1.4rem;
    color: var(--accent-color);
    font-size: 1rem;
}

.info-item div {
    display: flex;
    justify-content: space-between;
    flex: 1;
}

.info-label {
    color: var(--text-secondary);
}

.info-value {
    color: var(--text-color);
    font-weight: 500;
}

/* ========== 顶部阅读进度条 ========== */
.reading-progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: transparent;
    z-index: 1000;
    pointer-events: none;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--accent-color), var(--accent-light));
    width: 0%;
    transition: width 0.1s linear;
}

/* ========== 图片模态框 ========== */
.image-modal {
    display: none;
    position: fixed;
    z-index: 1050;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.92);
    backdrop-filter: blur(8px);
    cursor: pointer;
}

.image-modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

.image-modal .close {
    position: absolute;
    top: 20px;
    right: 35px;
    color: #fff;
    font-size: 2rem;
    cursor: pointer;
    z-index: 1051;
    transition: transform 0.2s;
}

.image-modal .close:hover {
    transform: scale(1.1);
}

.image-modal img {
    max-width: 90vw;
    max-height: 85vh;
    object-fit: contain;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
}

/* ========== 代码块复制工具栏 ========== */
.code-block-wrapper {
    position: relative;
    margin: 1.2rem 0;
}

.code-block-wrapper pre {
    padding-top: 2.5rem;
}

.code-toolbar {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    display: flex;
    gap: 0.5rem;
    z-index: 5;
}

.code-filetype {
    background: rgba(0, 0, 0, 0.5);
    color: #ddd;
    padding: 0.2rem 0.6rem;
    border-radius: var(--radius-sm);
    font-size: 0.7rem;
    font-family: var(--font-mono);
}

.code-copy-btn {
    background: rgba(0, 0, 0, 0.5);
    border: none;
    color: #ddd;
    padding: 0.2rem 0.6rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.7rem;
    transition: background 0.2s;
}

.code-copy-btn:hover {
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
}

@media (max-width: 768px) {
    .code-block-wrapper pre {
        padding-top: 2rem;
    }
}

/* ========== 移动端侧边栏（滑入） ========== */
.article-sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.article-sidebar-overlay.open {
    opacity: 1;
    visibility: visible;
}

@media (max-width: 768px) {
    .article-grid {
        grid-template-columns: 1fr;
        margin-top: 80px;
        padding: 0 1rem;
        gap: 1.5rem;
    }

    .article-sidebar {
        position: fixed;
        top: 0;
        right: -100%;
        width: 280px;
        height: 100vh;
        z-index: 1001;
        transition: right 0.3s cubic-bezier(0.2, 0.9, 0.4, 1.1);
        padding: 1rem;
        background: var(--surface-color-secondary);
        backdrop-filter: blur(16px);
        border-left: 1px solid var(--border-color);
        overflow-y: auto;
    }

    .article-sidebar.open {
        right: 0;
    }

    .article-sidebar.open .sidebar-card.toc-card {
        max-height: 60vh;
    }

    .article-card {
        padding: 1.5rem;
    }

    .article-title {
        font-size: 2rem;
    }

    .article-meta-line {
        gap: 1rem;
        font-size: 0.8rem;
    }
}

@media (max-width: 480px) {
    .article-card {
        padding: 1.2rem;
    }

    .article-title {
        font-size: 1.6rem;
    }

    .article-body {
        font-size: 1rem;
    }
}

/* ========== 焦点环统一 ========== */
:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px rgba(180, 91, 99, 0.3);
    border-radius: var(--radius-sm);
    transition: box-shadow 0.1s;
}

/* ========== 减少动画支持 ========== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ========== 暗色模式微调 ========== */
[data-theme="dark"] .sidebar-card {
    background: rgba(24, 24, 31, 0.9);
    backdrop-filter: blur(8px);
}

[data-theme="dark"] .code-copy-btn,
[data-theme="dark"] .code-filetype {
    background: rgba(255, 255, 255, 0.1);
    color: #ccc;
}

/* ========== 打印样式 ========== */
@media print {
    .article-sidebar,
    .comments-card,
    .floating-buttons,
    .reading-progress-bar,
    .image-modal,
    .back-to-top-btn {
        display: none !important;
    }
    .article-grid {
        display: block;
        margin: 0;
        padding: 0;
    }
    .article-card {
        box-shadow: none;
        border: none;
        padding: 0;
        margin: 0;
    }
    .article-body a {
        text-decoration: underline;
        color: black;
    }
    .article-body a::after {
        content: " (" attr(href) ")";
        font-size: 0.8rem;
    }
    .article-tags .tag {
        border: 1px solid #ccc;
        background: none;
        color: black;
    }
}