/* KaTeX 公式排版：统一文章页与知识库文档页的行内、块级公式表现。 */
:where(.post-content, .dm-content__body) .katex {
  color:inherit;
  font-size:1.08em;
  line-height:1.2;
  text-rendering:auto;
}

/* 编辑器有时会把整道题包成行内公式。混合题干会按完整 LaTex 结构拆成
 * 文本和数学片段，使其可随页面换行；每个根号、分式和 \left…\right 保持完整。 */
:where(.post-content, .dm-content__body) .katex-formula-inline {
  max-width:100%;
}

:where(.post-content, .dm-content__body) .katex-formula-inline > .katex {
  max-width:100%;
  font-size:var(--katex-inline-fitted-size, 1.08em);
  vertical-align:baseline;
  transition:font-size .12s ease;
}

/* 方程组等结构化行内公式保留在文字所在行，垂直方向与正文居中。 */
:where(.post-content, .dm-content__body) .katex-formula-inline--structured {
  display:inline-block;
  vertical-align:middle;
}

:where(.post-content, .dm-content__body) .katex-formula-mixed {
  white-space:normal;
  line-height:inherit;
}

:where(.post-content, .dm-content__body) .katex-inline-text {
  font-size:1em;
  white-space:normal;
  overflow-wrap:anywhere;
  word-break:break-word;
}

:where(.post-content, .dm-content__body) .katex-inline-math {
  display:inline-block;
  max-width:100%;
  white-space:nowrap;
  margin:0 .04em;
  line-height:1.55;
  vertical-align:baseline;
}

:where(.post-content, .dm-content__body) .katex-inline-math > .katex {
  font-size:1em;
  line-height:1.2;
}

/* 分式会同时向基线上下延展：仅提高含分式正文行的行盒高度，避免相邻行重叠。 */
:is(.post-content, .dm-content__body) :is(p,li,td,th,blockquote).katex-has-fraction-line {
  line-height:2.35;
}

:where(.post-content, .dm-content__body) .katex-inline-math--fraction {
  line-height:1.75;
}

/* 含文字说明的多行公式：说明行随正文自然换行；数学行保留原环境的对齐规则。 */
:where(.post-content, .dm-content__body) .katex-formula-multiline-mixed {
  display:block;
  margin:1rem 0;
  line-height:1.9;
}

:where(.post-content, .dm-content__body) .katex-mixed-line {
  display:block;
  min-width:0;
}

:where(.post-content, .dm-content__body) .katex-mixed-line--text {
  text-align:left;
}

:where(.post-content, .dm-content__body) .katex-mixed-math-group {
  display:block;
  overflow:hidden;
}

:where(.post-content, .dm-content__body) .katex-mixed-math-group--center {
  text-align:center;
}

:where(.post-content, .dm-content__body) .katex-mixed-math-group--aligned {
  text-align:left;
}

:where(.post-content, .dm-content__body) .katex-mixed-display {
  display:inline-block;
  margin:.1rem 0;
  max-width:100%;
  font-size:var(--katex-mixed-fitted-size, 1em);
}

:where(.post-content, .dm-content__body) .katex-mixed-display > .katex {
  font-size:1em;
}

:where(.post-content, .dm-content__body) .katex-mixed-display--line {
  display:block;
  width:max-content;
  max-width:100%;
  margin:.1rem 0;
  line-height:1.45;
  text-align:left;
}

:where(.post-content, .dm-content__body) .katex-mixed-display--fraction {
  margin:.32rem 0;
  line-height:1.75;
}

:where(.post-content, .dm-content__body) .katex-formula-scrollable {
  overflow-x:auto;
  overflow-y:hidden;
  -webkit-overflow-scrolling:touch;
  scrollbar-width:none;
}

:where(.post-content, .dm-content__body) .katex-formula-scrollable::-webkit-scrollbar {
  display:none;
}

:where(.post-content, .dm-content__body) .katex-display,
:where(.post-content, .dm-content__body) [math-display],
:where(.post-content, .dm-content__body) .katex-block,
:where(.post-content, .dm-content__body) .katex-formula-block {
  display:block;
  width:100%;
  margin:.7rem 0;
  padding:0;
  overflow-x:auto;
  overflow-y:hidden;
  text-align:center;
  -webkit-overflow-scrolling:touch;
}

/* KaTeX 会在块级容器内再生成 .katex-display；只保留外层间距，避免双重留白。 */
:where(.post-content, .dm-content__body) .katex-formula-block > .katex-display {
  width:auto;
  margin:0;
  padding:0;
  overflow:visible;
}

:where(.post-content, .dm-content__body) .katex-display > .katex,
:where(.post-content, .dm-content__body) .katex-block > .katex,
:where(.post-content, .dm-content__body) .katex-formula-block > .katex {
  display:inline-block;
  min-width:max-content;
  max-width:none;
  white-space:normal;
}

/*
 * 数学结构不能在任意字符间断开：由脚本只缩小超宽块级公式；超过最小可读
 * 尺寸时仍在公式容器内横向滚动，不让正文或页面产生横向溢出。
 */
:where(.post-content, .dm-content__body) .katex-formula-block > .katex-display > .katex {
  font-size:var(--katex-fitted-size, 1.08em);
  transition:font-size .12s ease;
}

@media (prefers-reduced-motion:reduce) {
  :where(.post-content, .dm-content__body) .katex-formula-block > .katex-display > .katex,
  :where(.post-content, .dm-content__body) .katex-formula-inline:not(.katex-formula-mixed) > .katex {
    transition:none;
  }
}

:where(.post-content, .dm-content__body) .katex-error {
  color:#dc2626;
  font-size:.95em;
}

body.dark :where(.post-content, .dm-content__body) .katex {
  color:#e5edf8;
}

body.dark :where(.post-content, .dm-content__body) .katex-error {
  color:#fca5a5;
}

.archive-card-desc .katex {
  font-size:1em;
  line-height:1.2;
}

.archive-card-desc--formula {
  overflow-x:auto;
  white-space:nowrap;
  -webkit-overflow-scrolling:touch;
}

@media(max-width:1000px){
  :where(.post-content, .dm-content__body) .katex {
    font-size:1em;
  }

  :where(.post-content, .dm-content__body) .katex-display,
  :where(.post-content, .dm-content__body) [math-display],
  :where(.post-content, .dm-content__body) .katex-block,
  :where(.post-content, .dm-content__body) .katex-formula-block {
    margin:.6rem 0;
  }
}
