:root {
  color-scheme: light;
  --bg: #f9f5ed;
  --surface: #fdfaf3;
  --ink: #191917;
  --body: #24231f;
  --muted: #4f4c46;
  --faint: #a8a59c;
  --line: #ddd8ca;
  --control-line: #bcb6a7;
  --accent: #b4531f;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC", "Segoe UI", "Microsoft YaHei", sans-serif;
  /* 页眉、正文、页脚共用这一个宽度，左边缘才落在同一条竖线上。 */
  --measure: 45rem;
  --gutter: clamp(1.375rem, 4vw, 4rem);
  --toc-gap: 2rem;
  --toc-width: 7.5rem;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #161814;
  --surface: #1d201b;
  --ink: #f0f2ea;
  --body: #e6e8e0;
  --muted: #d3d5cd;
  --faint: #666b60;
  --line: #2c2f28;
  --control-line: #4d5147;
  --accent: #96c08a;
}

* { box-sizing: border-box; }

html {
  background: var(--bg);
  scroll-behavior: smooth;
  scrollbar-gutter: stable;
}

/* 大屏上放大整体尺度，而不是单纯拉长行。
   --measure 和字号都是 rem，跟着根字号一起走，每行字数保持不变（约 46 字）。
   用百分比而不是 px，用户在浏览器里调过默认字号仍然生效。 */
@media (min-width: 1500px) { html { font-size: 106.25%; } }
@media (min-width: 1800px) { html { font-size: 112.5%; } }
@media (min-width: 2200px) { html { font-size: 118.75%; } }

body {
  display: flex;
  flex-direction: column;
  min-height: 100svh;
  margin: 0;
  color: var(--body);
  background: var(--bg);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.75;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select,
textarea { font: inherit; }

a { color: inherit; text-decoration: none; }

a:focus-visible,
button:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* 日期、字数这类数字用等宽，切换文章时不跳。 */
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }

.skip-link {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 1rem;
  padding: .4rem .75rem;
  color: var(--bg);
  background: var(--ink);
  transform: translateY(-120%);
}

.skip-link:focus { transform: translateY(0); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* 页眉、正文、页脚共用同一条中轴。 */
.site-header__inner,
.page-shell,
.article-shell,
.site-footer {
  width: min(calc(100% - var(--gutter) * 2), var(--measure));
  margin-inline: auto;
}

.page-shell,
.article-shell { flex: 1 0 auto; }

.site-header__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 4.75rem;
  gap: 1rem;
}

.wordmark {
  min-width: 0;
  color: var(--ink);
  font-size: .9375rem;
  font-weight: 600;
  letter-spacing: -.01em;
  line-height: 1.25;
}

.wordmark--back {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  color: var(--muted);
  font-size: .8125rem;
  font-weight: 400;
  letter-spacing: 0;
}

.wordmark[href]:hover,
.wordmark--back:hover { color: var(--accent); }

.site-nav {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 1.125rem;
  color: var(--muted);
  font-size: .8125rem;
}

.site-nav a { transition: color 160ms ease; }
.site-nav a:hover { color: var(--ink); }

.language-toggle {
  min-width: 2.125rem;
  height: 1.875rem;
  padding: 0 .375rem;
  color: var(--muted);
  border: 1px solid var(--control-line);
  border-radius: 999px;
  background: transparent;
  cursor: pointer;
  font-size: .75rem;
  font-weight: 600;
  letter-spacing: .02em;
  line-height: 1;
  transition: color 160ms ease, border-color 160ms ease;
}

.language-toggle:hover { color: var(--ink); border-color: var(--ink); }

.theme-toggle {
  display: grid;
  flex: 0 0 auto;
  width: 1.875rem;
  height: 1.875rem;
  place-items: center;
  padding: 0;
  color: var(--muted);
  border: 1px solid var(--control-line);
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  line-height: 1;
}

.theme-toggle:hover { color: var(--ink); border-color: var(--ink); }
.theme-toggle svg { display: block; transition: transform 240ms ease, opacity 240ms ease; }

/* 手机上把点击区撑到 44px，视觉上还是 30px 的圆。 */
@media (pointer: coarse) {
  .language-toggle {
    min-width: 2.75rem;
    height: 2.75rem;
    margin-inline: -.3125rem;
    border-color: transparent;
  }
  .language-toggle::before {
    content: "";
    position: absolute;
    inset: .4375rem .3125rem;
    border: 1px solid var(--control-line);
    border-radius: 999px;
  }
  .theme-toggle {
    width: 2.75rem;
    height: 2.75rem;
    margin-right: -.75rem;
    border-color: transparent;
  }
  .theme-toggle::before {
    content: "";
    position: absolute;
    width: 1.875rem;
    height: 1.875rem;
    border: 1px solid var(--control-line);
    border-radius: 50%;
  }
  .site-nav,
  .language-toggle { position: relative; }
  .theme-toggle { position: relative; }
}

/* 阅读进度 */
.reading-progress {
  position: sticky;
  z-index: 10;
  top: 0;
  height: 2px;
  background: var(--line);
}

.reading-progress div {
  width: 100%;
  height: 100%;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left;
}

/* 首页 */
.page-shell--home { padding: 2.75rem 0 0; }

.post-row { padding: 0 0 2rem; }

.post-row + .post-row { border-top: 1px solid var(--line); padding-top: 2rem; }

.post-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .625rem;
  margin: 0 0 .75rem;
  color: var(--muted);
  font-size: .8125rem;
  line-height: 1.6;
}

.post-meta span[aria-hidden] {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--faint);
}

.post-row h2 {
  margin: 0;
  font-size: 1.625rem;
  font-weight: 650;
  letter-spacing: -.015em;
  line-height: 1.35;
  text-wrap: pretty;
}

.post-row h2 .post-title__date { font-weight: 550; }
.post-row h2 a:hover { color: var(--accent); }

.post-sections {
  display: flex;
  flex-wrap: wrap;
  gap: .375rem 1.125rem;
  margin-top: .875rem;
}

.post-sections a {
  display: inline-flex;
  gap: .4375rem;
  color: var(--muted);
  font-size: .875rem;
}

.post-sections a b { color: var(--faint); font-weight: inherit; }
.post-sections a:hover { color: var(--accent); }
.post-sections a:hover b { color: var(--accent); }

.site-footer {
  flex: 0 0 auto;
  padding: 3.5rem 0 2.5rem;
  color: var(--muted);
  font-size: .8125rem;
}

.site-footer p { margin: 0; }

/* 文章页 */
.article-shell {
  position: relative;
  padding: 4.5rem 0 0;
}

.article-header { padding-bottom: 2.75rem; }

.article-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .625rem;
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: .8125rem;
  line-height: 1.6;
}

.article-meta span[aria-hidden] {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--faint);
}

.article-header h1 {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.5625rem, 1.125rem + 2vw, 2.25rem);
  font-weight: 650;
  letter-spacing: -.02em;
  line-height: 1.3;
  text-wrap: pretty;
}

.article-header h1 .article-title__date { font-weight: 550; }

.article-actions { margin-top: 1.25rem; }

.article-actions a {
  display: inline-flex;
  align-items: center;
  gap: .4375rem;
  height: 2.125rem;
  padding: 0 .75rem;
  color: var(--muted);
  border: 1px solid var(--control-line);
  border-radius: 3px;
  font-size: .8125rem;
}

.article-actions a:hover { color: var(--ink); border-color: var(--ink); }

.article-toc {
  margin: 0 0 2rem;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.article-toc summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 3rem;
  color: var(--ink);
  font-size: .8125rem;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}

.article-toc summary::-webkit-details-marker { display: none; }

.article-toc summary::after {
  content: "";
  width: .5rem;
  height: .5rem;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: translateY(-2px) rotate(45deg);
  transition: transform 160ms ease;
}

.article-toc[open] summary::after { transform: translateY(2px) rotate(-135deg); }

.article-toc nav {
  display: grid;
  padding-bottom: .625rem;
}

.article-toc a {
  display: flex;
  gap: .5625rem;
  align-items: center;
  min-height: 2.5rem;
  color: var(--muted);
  font-size: .875rem;
  line-height: 1.5;
}

.article-toc a b { color: var(--faint); font-weight: inherit; }
.article-toc a:hover { color: var(--ink); }

.article-toc a[aria-current="location"] { color: var(--ink); font-weight: 600; }
.article-toc a[aria-current="location"] b { color: var(--accent); }

/* 目录浮到正文列左侧的页边距里，正文列的中轴和首页保持一致。
   门槛 1080px 是算出来的：正文 720 + 两侧各 (32 + 120) = 1024，
   竖屏显示器最常见的 1080 宽正好放得下。
   1240 以上正文加宽到 832，832 + 304 = 1136 仍然放得下。 */
@media (min-width: 1080px) {
  /* 目录变成常驻侧栏：文章区做成「目录 | 正文」两列网格，
     整体居中，两侧留白自然等宽——不需要再手动位移正文。
     页眉和页脚在文章页也展开到同样的宽度，边缘才对得上。 */
  .article-page .site-header__inner,
  .article-page .site-footer,
  .article-shell {
    width: min(
      calc(100% - var(--gutter) * 2),
      calc(var(--measure) + var(--toc-gap) + var(--toc-width))
    );
  }

  .article {
    display: grid;
    grid-template-columns: var(--toc-width) minmax(0, 1fr);
    column-gap: var(--toc-gap);
  }

  .article-header { grid-area: 1 / 2; }
  .article-content { grid-area: 2 / 2; }

  /* sticky 必须挂在 <details> 本身。挂在它的子元素上，包含块会变成
     浏览器给 details 生成的 slot——那东西只有内容那么高，没有可滑动的
     余量，于是滚动一下目录就整个消失了。 */
  .article-toc {
    grid-area: 1 / 1 / 3 / 2;
    position: sticky;
    top: 1.5rem;
    align-self: start;
    max-height: calc(100svh - 3rem);
    margin: 0;
    border: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .article-toc summary {
    min-height: 0;
    padding-bottom: .75rem;
  }

  .article-toc summary::after { display: none; }

  .article-toc nav { padding: 0; }

  .article-toc a {
    min-height: 0;
    padding: .4375rem 0 .4375rem .75rem;
    border-left: 2px solid var(--line);
    font-size: .84375rem;
  }

  .article-toc a[aria-current="location"] { border-left-color: var(--accent); }
}

/* 放得下的时候把正文列加宽到 52rem。
   低于 1240px 保持 45rem，否则侧栏在 1080 竖屏上挤不下。 */
@media (min-width: 1240px) {
  :root { --measure: 52rem; }
}

/* 宽屏上把侧栏和间距放开一点。 */
@media (min-width: 1400px) {
  :root { --toc-gap: 3rem; --toc-width: 10rem; }
}

/* ——— 各种非常规视口 ——— */

/* 折叠屏外屏、小手机：把页边距收到 1rem，标题降一档，别让一行只剩几个字。 */
@media (max-width: 380px) {
  :root { --gutter: 1rem; }
  .wordmark { font-size: .84375rem; }
  .site-nav { gap: .625rem; font-size: .78125rem; }
  .post-row h2 { font-size: 1.1875rem; }
  .article-header h1 { font-size: 1.375rem; }
  .article-content p,
  .article-content ul,
  .article-content ol { font-size: 1rem; }
  .post-meta,
  .article-meta { gap: .4375rem; font-size: .75rem; }
}

/* 手机横屏、平板横屏这类矮视口：上下留白吃掉了太多屏，压扁一点。 */
@media (max-height: 560px) and (min-width: 480px) {
  .site-header__inner { min-height: 3.25rem; }
  .page-shell--home { padding-top: 1.5rem; }
  .article-shell { padding-top: 1.75rem; }
  .article-toc { top: 1.75rem; height: calc(100% - 1.75rem); }
  .article-header { padding-bottom: 1.5rem; }
  .article-content h2 { margin-top: 2rem; }
  .site-footer { padding: 2rem 0 1.5rem; }
}

/* 折叠屏展开后是两段视口，内容整体让进左边那段，别被折痕从中间劈开。
   属于新标准，支持还不广，当成锦上添花，不支持时上面的规则照常生效。 */
@media (horizontal-viewport-segments: 2) {
  .site-header__inner,
  .page-shell,
  .article-shell,
  .site-footer {
    width: min(calc(env(viewport-segment-width 0 0) - var(--gutter) * 2), var(--measure));
    margin-inline: auto 0;
  }

  .article { display: block; }
  .article-toc { position: static; grid-area: auto; max-height: none; margin: 0 0 2rem; border-block: 1px solid var(--line); overflow: visible; }
  .article-toc summary { min-height: 3rem; padding-bottom: 0; }
  .article-toc summary::after { display: flex; }
  .article-toc nav { padding-bottom: .625rem; }
  .article-toc a { min-height: 2.5rem; padding: 0; border-left: 0; font-size: .875rem; }
}

.article-content { color: var(--body); }

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

/* 中文正文两端对齐：汉字等宽，左对齐时右边缘会参差得很明显。
   不写 text-justify——默认的 auto 会中文按字距、英文按词距地撑；
   指定 inter-character 会把 offer、Leader 这类单词内部的字母也拉开。 */
.article-content p {
  margin: 0 0 1.5rem;
  font-size: 1.125rem;
  line-height: 1.85;
  text-align: justify;
  text-wrap: pretty;
}

html[data-lang="en"] .article-content p { text-align: left; }

.article-content h2,
.article-content h3 {
  color: var(--ink);
  scroll-margin-top: 1.5rem;
  font-weight: 650;
  text-wrap: pretty;
}

.article-content h2 {
  margin: 3rem 0 1.25rem;
  font-size: clamp(1.25rem, 1.05rem + .85vw, 1.5rem);
  letter-spacing: -.01em;
  line-height: 1.5;
}

.article-content h3 {
  margin: 2rem 0 1rem;
  font-size: 1.1875rem;
  line-height: 1.55;
}

.article-content ul,
.article-content ol {
  margin: 0 0 1.5rem;
  padding-left: 1.4rem;
  font-size: 1.125rem;
  line-height: 1.85;
  text-align: justify;
}

.article-content li + li { margin-top: .375rem; }

.article-content blockquote {
  margin: 2rem 0;
  padding: .25rem 0 .25rem 1.25rem;
  border-left: 2px solid var(--line);
  color: var(--muted);
}

.article-content blockquote p { margin: 0; }

.article-content code {
  padding: .12em .35em;
  border-radius: 2px;
  background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .875em;
}

.article-content pre {
  overflow-x: auto;
  margin: 2rem 0;
  padding: 1.15rem;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--surface);
}

.article-content pre code { padding: 0; background: transparent; }

.article-content a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--accent), transparent 55%);
  text-underline-offset: 3px;
}

.article-content a:hover { text-decoration-color: var(--accent); }

.load-error { color: var(--muted); }

@media (max-width: 700px) {
  .site-header__inner { min-height: 3.75rem; }
  .wordmark { font-size: .90625rem; }
  .site-nav { gap: .875rem; }

  .page-shell--home { padding-top: 2rem; }
  .post-row h2 { font-size: 1.3125rem; line-height: 1.42; }
  .post-sections a { min-height: 2rem; align-items: center; font-size: .84375rem; }

  .article-shell { padding-top: 2rem; }
  .article-header { padding-bottom: 1.75rem; }
  .article-actions a { height: 2.5rem; }
  .article-content p,
  .article-content ul,
  .article-content ol { font-size: 1.03125rem; }
  .site-footer { padding: 2.75rem 0 2rem; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *,
  *::before,
  *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
