/* ============================================
   kb-blog.css — KB 专题文章通用移动适配 + 主题按钮兜底层
   加载位置：所有 /kb/blog/*.html + /kb/write.html 的 head 末尾
   设计：!important 强制覆盖各文章内联样式，避免 specificity 战争
   覆盖元素：article / main / .article-body / .article-header
   ============================================ */

/* 主题切换按钮兜底（theme.js 创建 class="theme-toggle" 而非 .site-nav-theme） */
.theme-toggle {
    width: 28px; height: 28px;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.1);
    background: transparent;
    color: #a0a0b8;
    cursor: pointer;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    line-height: 1;
    padding: 0;
    transition: all 0.15s;
    margin-left: 0;
}
.theme-toggle:hover {
    background: rgba(255,255,255,0.08);
    color: #fff;
}
[data-theme="light"] .theme-toggle {
    border-color: rgba(0,0,0,0.1);
    color: #5a5a7a;
}
[data-theme="light"] .theme-toggle:hover {
    background: rgba(0,0,0,0.05);
    color: #1a1a2e;
}

/* 横向溢出兜底（最关键） */
html, body { overflow-x: hidden; }
body { max-width: 100vw; }

/* 容器宽度限制（防止 max-width 硬撑开） */
article, main, .article-body, .article-header {
    max-width: 100% !important;
    box-sizing: border-box;
    width: auto;
}

/* 表格响应式：横向滚动而不是撑开 */
article table, .article-body table, table {
    display: block !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    word-break: break-word;
    max-width: 100% !important;
}

/* 图片不溢出 */
article img, .article-body img, main img {
    max-width: 100% !important;
    height: auto !important;
}

/* pre/code 长字符串换行，不撑开容器 */
article pre, .article-body pre, pre {
    white-space: pre-wrap !important;
    word-break: break-word !important;
    max-width: 100% !important;
    overflow-x: auto !important;
}
article code, .article-body code, code {
    word-break: break-word !important;
    overflow-wrap: anywhere;
}

.wrap, .post-container, .files, .file-item, .toc, section, .content, .post-content {
    min-width: 0 !important;
    box-sizing: border-box;
}
.files, .file-item, .preview, .video-wrapper, .map-wrap, .arch-box, .corr-diagram, .bar-chart, .pie-chart {
    max-width: 100% !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.preview {
    width: 100% !important;
    min-height: 320px;
}
.preview iframe, .files iframe {
    width: 100% !important;
    min-height: 320px;
    border: 0;
}
video, audio {
    width: 100% !important;
}
.file-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.file-actions .dl-btn {
    margin-bottom: 0;
}
.kpi-row, .arch-layer-mid, .legend {
    min-width: 0;
}
.kpi-box, .arch-node, .corr-card {
    min-width: 0 !important;
}
.bar-row {
    min-width: 0;
}
.bar-label, .bar-val {
    overflow-wrap: anywhere;
}
.map-grid {
    max-width: 100%;
    overflow: hidden;
}

/* 平板：768px */
@media (max-width: 768px) {
    article h1, .article-header h1, main h1 {
        font-size: 1.5rem !important;
        line-height: 1.3 !important;
    }
    article h2, .article-body h2, main h2 { font-size: 1.25rem !important; }
    article h3, .article-body h3, main h3 { font-size: 1.05rem !important; }
    article p, .article-body p, main p {
        font-size: 0.95rem !important;
        line-height: 1.7 !important;
    }
    article, main, .article-body, .article-header {
        padding: 24px 16px 40px !important;
    }
    article table, .article-body table {
        font-size: 0.85rem !important;
    }
    .wrap, .post-container, .files, .toc, section {
        padding-left: 14px !important;
        padding-right: 14px !important;
    }
    .preview, .preview iframe, .files iframe {
        min-height: 360px !important;
    }
    .kpi-row {
        display: grid !important;
        grid-template-columns: repeat(2,minmax(0,1fr)) !important;
    }
    .corr-diagram {
        grid-template-columns: 1fr !important;
    }
}

/* 手机：480px */
@media (max-width: 480px) {
    article h1, .article-header h1, main h1 { font-size: 1.25rem !important; }
    article h2, .article-body h2, main h2 { font-size: 1.1rem !important; }
    article h3, .article-body h3, main h3 { font-size: 0.95rem !important; }
    article p, .article-body p, main p { font-size: 0.88rem !important; line-height: 1.65 !important; }
    article, main, .article-body, .article-header {
        padding: 18px 12px 32px !important;
    }
    article pre, .article-body pre, pre {
        padding: 12px 14px !important;
        font-size: 0.78rem !important;
    }
    article blockquote, .article-body blockquote, blockquote {
        padding: 10px 14px !important;
        margin: 14px 0 !important;
    }
    .wrap, .post-container, .files, .toc, section {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }
    .file-item {
        padding: 12px !important;
    }
    .file-actions {
        flex-direction: column;
        align-items: stretch;
    }
    .file-actions .dl-btn {
        width: 100%;
        text-align: center;
    }
    .preview, .preview iframe, .files iframe {
        min-height: 300px !important;
        height: 300px !important;
    }
    .kpi-row {
        grid-template-columns: repeat(2,minmax(0,1fr)) !important;
        gap: 8px !important;
    }
    .kpi-box {
        padding: 10px 8px !important;
    }
    .bar-chart {
        padding: 12px !important;
    }
    .bar-row {
        gap: 6px !important;
    }
    .bar-label {
        width: 68px !important;
        text-align: left !important;
        font-size: 0.7rem !important;
    }
    .bar-val {
        width: 56px !important;
        font-size: 0.7rem !important;
    }
    .map-wrap {
        margin-left: -4px !important;
        margin-right: -4px !important;
    }
    .map-grid {
        min-height: 320px !important;
        grid-template-rows: repeat(9, 36px) !important;
    }
    #comments-section {
        padding-left: 0 !important;
        padding-right: 0 !important;
    }
}

@media (max-width: 380px) {
    .kpi-row {
        grid-template-columns: 1fr !important;
    }
    .preview, .preview iframe, .files iframe {
        min-height: 260px !important;
        height: 260px !important;
    }
}
