/*
 * MIYU 博客样式.
 * 视觉主线是一条 "追踪线": 首页的 现象 -> 复现 -> 根因, 文章页左侧的阅读进度线.
 * 颜色全部走 token, 明暗两套; 暗色模式下强调色饱和度更高.
 */

/* ---------- token ---------- */

:root {
  --bg: #FAF9F5;
  --bg-soft: #F1EFE7;
  --ink: #24231F;
  --ink-soft: #5F5C54;
  --ink-faint: #9B978C;
  --rule: #E1DED3;
  --accent: #A65D3F;
  --accent-soft: rgba(166, 93, 63, 0.12);
  --grain-opacity: 0.035;

  --display: "Fraunces", "Noto Serif SC", "Songti SC", serif;
  --text: "Noto Serif SC", "Songti SC", serif;
  --code: "JetBrains Mono", ui-monospace, monospace;

  --measure: 38rem;
  --gutter: 16px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413;
    --bg-soft: #1C1C1A;
    --ink: #E8E5DC;
    --ink-soft: #AAA69B;
    --ink-faint: #6E6B63;
    --rule: #2B2A27;
    --accent: #EA7B4B;
    --accent-soft: rgba(234, 123, 75, 0.15);
    --grain-opacity: 0.05;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #141413;
  --bg-soft: #1C1C1A;
  --ink: #E8E5DC;
  --ink-soft: #AAA69B;
  --ink-faint: #6E6B63;
  --rule: #2B2A27;
  --accent: #EA7B4B;
  --accent-soft: rgba(234, 123, 75, 0.15);
  --grain-opacity: 0.05;
  color-scheme: dark;
}

/* ---------- 基础 ---------- */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--text);
  font-size: 1.0625rem;
  line-height: 1.85;
  text-rendering: optimizeLegibility;
  transition: background-color 0.4s var(--ease), color 0.4s var(--ease);
}

/* 纸面颗粒, 只做氛围, 不参与交互. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: var(--grain-opacity);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

::selection { background: var(--accent-soft); }

a {
  color: inherit;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.22em;
  transition: color 0.2s var(--ease);
}
a:hover { color: var(--accent); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 2px;
}

/* ---------- 页眉页脚 ---------- */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: min(100% - 2 * var(--gutter), 52rem);
  margin: 0 auto;
  padding: 1.5rem 0;
  view-transition-name: site-header;
}

.site-name {
  font-family: var(--display);
  font-style: italic;
  font-weight: 400;
  font-size: 1.3rem;
  text-decoration: none;
  letter-spacing: 0.01em;
}

.site-nav {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  font-size: 0.9375rem;
}

.site-nav a {
  color: var(--ink-soft);
  text-decoration: none;
}
.site-nav a:hover,
.site-nav a[aria-current="page"] { color: var(--ink); }
.site-nav a[aria-current="page"] {
  text-decoration: underline;
  text-decoration-color: var(--accent);
}

/* 半圆图标: 暗色时转半圈. */
.theme-toggle {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}
.theme-toggle span {
  width: 12px;
  height: 12px;
  border: 1.5px solid var(--ink-soft);
  border-radius: 50%;
  background: linear-gradient(90deg, var(--ink-soft) 50%, transparent 50%);
  transition: transform 0.6s var(--ease);
}
:root[data-theme="dark"] .theme-toggle span { transform: rotate(180deg); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle span { transform: rotate(180deg); }
}

.site-main {
  width: min(100% - 2 * var(--gutter), 46rem);
  margin: 0 auto;
}

.site-footer {
  width: min(100% - 2 * var(--gutter), 46rem);
  margin: 7rem auto 0;
  padding: 1.75rem 0 3rem;
  border-top: 1px solid var(--rule);
  color: var(--ink-faint);
  font-size: 0.8125rem;
}
.site-footer p { margin: 0; }

/* ---------- 首页 ---------- */

.hero {
  display: grid;
  grid-template-columns: 1fr auto;
  column-gap: 2.5rem;
  padding: 4.5rem 0 4rem;
}
.hero > :not(.trace) { grid-column: 1; }

.hero-kicker {
  margin: 0 0 0.75rem;
  color: var(--ink-faint);
  font-size: 0.8125rem;
  letter-spacing: 0.18em;
}

.hero-title {
  margin: 0;
  font-family: var(--display);
  font-style: italic;
  font-weight: 300;
  font-variation-settings: "opsz" 144;
  font-size: clamp(4.5rem, 17vw, 8.5rem);
  line-height: 0.95;
  letter-spacing: -0.025em;
}

.hero-lede {
  margin: 1.75rem 0 0.5rem;
  font-size: 1.25rem;
  font-weight: 600;
}

.hero-note {
  max-width: 28rem;
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.9375rem;
}

/* 现象 -> 复现 -> 根因: 线先画出, 节点依次出现, 最后一个节点是首页的视觉落点. */
.trace {
  grid-column: 2;
  grid-row: 1 / span 4;
  align-self: center;
  position: relative;
  display: grid;
  gap: 1.6rem;
  margin: 0;
  padding: 0 0 0 1.4rem;
  list-style: none;
  color: var(--ink-faint);
  font-size: 0.9375rem;
}
.trace::before {
  content: "";
  position: absolute;
  left: 3.5px;
  top: 0.6em;
  bottom: 0.6em;
  width: 1px;
  background: var(--rule);
  transform-origin: top;
  animation: draw 1s 0.35s var(--ease) both;
}
.trace li {
  position: relative;
  animation: rise 0.7s var(--ease) both;
}
.trace li::before {
  content: "";
  position: absolute;
  left: -1.4rem;
  top: 0.6em;
  width: 8px;
  height: 8px;
  border: 1px solid var(--ink-faint);
  border-radius: 50%;
  background: var(--bg);
}
.trace li:nth-child(1) { animation-delay: 0.5s; }
.trace li:nth-child(2) { animation-delay: 0.8s; }
.trace li:nth-child(3) {
  animation-delay: 1.1s;
  color: var(--ink);
  font-weight: 600;
}
.trace li:nth-child(3)::before {
  border-color: var(--accent);
  background: var(--accent);
  animation: ping 1.6s 1.4s var(--ease) both;
}

.hero > :nth-child(1) { animation: rise 0.8s var(--ease) both; }
.hero > :nth-child(2) { animation: rise 0.9s 0.06s var(--ease) both; }
.hero > :nth-child(3) { animation: rise 0.8s 0.18s var(--ease) both; }
.hero > :nth-child(4) { animation: rise 0.8s 0.26s var(--ease) both; }

.section-label {
  margin: 0 0 0.5rem;
  color: var(--ink-faint);
  font-size: 0.8125rem;
  font-weight: 400;
  letter-spacing: 0.18em;
}

.latest + .latest { margin-top: 4.5rem; }

.more {
  display: inline-block;
  margin-top: 1.5rem;
  font-size: 0.9375rem;
}

/* ---------- 文章列表 ---------- */

.post-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.post-item { border-top: 1px solid var(--rule); }
.post-item:last-child { border-bottom: 1px solid var(--rule); }

.post-item a {
  display: grid;
  grid-template-columns: 6.5rem 1fr;
  gap: 1rem;
  padding: 1.35rem 0;
  text-decoration: none;
}

.post-item time {
  padding-top: 0.2em;
  color: var(--ink-faint);
  font-family: var(--display);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  transition: color 0.25s var(--ease);
}

.post-body { display: grid; gap: 0.3rem; }

/* 标题下划线从左向右展开. */
.post-title {
  justify-self: start;
  font-size: 1.125rem;
  font-weight: 600;
  line-height: 1.5;
  background: linear-gradient(var(--accent), var(--accent)) left bottom / 0 1px no-repeat;
  transition: background-size 0.45s var(--ease);
}

.post-desc {
  color: var(--ink-soft);
  font-size: 0.9375rem;
}

.post-errata {
  justify-self: start;
  padding: 0 0.5em;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.75rem;
}

.post-item a:hover { color: inherit; }
.post-item a:hover time { color: var(--accent); }
.post-item a:hover .post-title { background-size: 100% 1px; }

.post-empty {
  margin: 0;
  padding: 1.35rem 0;
  border-top: 1px solid var(--rule);
  color: var(--ink-faint);
  font-size: 0.9375rem;
}

.post-item.is-featured .post-title {
  font-size: 1.5rem;
  line-height: 1.4;
}

.page-head { padding: 3.5rem 0 2.5rem; }
.page-title {
  margin: 0;
  font-size: clamp(2rem, 6vw, 2.75rem);
  font-weight: 900;
  line-height: 1.25;
  animation: rise 0.8s var(--ease) both;
}
.page-lede {
  max-width: var(--measure);
  margin: 0.75rem 0 0;
  color: var(--ink-soft);
  animation: rise 0.8s 0.05s var(--ease) both;
}
.page-count {
  margin: 0.5rem 0 0;
  color: var(--ink-faint);
  font-size: 0.875rem;
  animation: rise 0.8s 0.1s var(--ease) both;
}

/* ---------- 文章 ---------- */

/* 左侧追踪线: 底线常驻, 强调色的进度线随页面滚动填满 (浏览器支持时). */
.article {
  position: relative;
  padding: 3.5rem 0 0 2.25rem;
}
.article::before,
.article::after {
  content: "";
  position: absolute;
  left: 0;
  top: 3.5rem;
  bottom: 0;
  width: 1px;
  background: var(--rule);
}
.article::after {
  background: var(--accent);
  transform: scaleY(0);
  transform-origin: top;
}

.article-head > * { animation: rise 0.8s var(--ease) both; }
.article-head > :nth-child(2) { animation-delay: 0.08s; }
.article-head > :nth-child(3) { animation-delay: 0.16s; }

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1rem;
  margin: 0 0 1rem;
  color: var(--ink-faint);
  font-size: 0.875rem;
}

.article-title {
  margin: 0;
  font-size: clamp(2rem, 6.5vw, 3rem);
  font-weight: 900;
  line-height: 1.25;
  letter-spacing: -0.01em;
}

.article-lede {
  max-width: var(--measure);
  margin: 1.25rem 0 0;
  color: var(--ink-soft);
  font-size: 1.125rem;
}

.prose {
  max-width: var(--measure);
  margin-top: 3rem;
  animation: rise 0.9s 0.24s var(--ease) both;
}

.prose p { margin: 0 0 1.2em; }

/* 项目页的交互舞台: 在标题和正文之间, 内容由项目自己的脚本挂载. */
.project-stage {
  margin-top: 2.75rem;
  animation: rise 0.9s 0.2s var(--ease) both;
}
.project-noscript { margin: 0; color: var(--ink-faint); }

.article h2 {
  position: relative;
  margin: 2.8em 0 0.9em;
  font-size: 1.375rem;
  font-weight: 600;
  line-height: 1.4;
}

/* 每个小节标题是追踪线上的一个节点. */
.article h2::before {
  content: "";
  position: absolute;
  left: calc(-2.25rem - 4px);
  top: 0.55em;
  width: 9px;
  height: 9px;
  border: 1px solid var(--ink-faint);
  border-radius: 50%;
  background: var(--bg);
}

.prose strong { font-weight: 600; }

.prose ul,
.prose ol { margin: 0 0 1.2em; padding-left: 1.4em; }
.prose li { margin: 0.35em 0; }
.prose li::marker { color: var(--ink-faint); }

.prose blockquote {
  margin: 1.6em 0;
  padding: 0 0 0 1.1em;
  border-left: 2px solid var(--accent-soft);
  color: var(--ink-soft);
}

.prose code {
  padding: 0.12em 0.38em;
  border-radius: 4px;
  background: var(--bg-soft);
  font-family: var(--code);
  font-size: 0.85em;
}

.prose pre {
  margin: 1.6em 0;
  padding: 1rem 1.2rem;
  overflow-x: auto;
  border-radius: 8px;
  background: var(--bg-soft);
  font-size: 0.875rem;
  line-height: 1.65;
}
.prose pre code { padding: 0; background: none; font-size: inherit; }

/* 表格: 窄屏时在表格内部横向滚动, 不撑开页面. */
.prose table {
  display: block;
  max-width: 100%;
  margin: 1.6em 0;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 0.9375rem;
  font-variant-numeric: tabular-nums;
}
.prose th,
.prose td {
  padding: 0.45em 0.9em;
  border-bottom: 1px solid var(--rule);
  text-align: center;
  white-space: nowrap;
}
.prose th { color: var(--ink-faint); font-weight: 400; }
.prose th:first-child,
.prose td:first-child { text-align: left; color: var(--ink-soft); }

/* 表格里的加粗内容用强调色, 例如对角线上的那一格. */
.prose td strong { color: var(--accent); }

/* 勘误区: 追踪线的终点. */
.errata {
  max-width: var(--measure);
  margin-top: 4.5rem;
  padding-bottom: 0.5rem;
}
.errata h2 { margin-top: 0; font-size: 1.125rem; }
.errata h2::before { border-color: var(--accent); }
.errata-empty { margin: 0; color: var(--ink-faint); }
.errata-list { margin: 0; padding: 0; list-style: none; }
.errata-list li { display: grid; gap: 0.2rem; margin-bottom: 1rem; }
.errata-list time { color: var(--accent); font-size: 0.875rem; }
.errata-list p { margin: 0; }

/* ---------- 动画 ---------- */

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}

@keyframes draw {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}

@keyframes draw-x {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

@keyframes ping {
  0% { box-shadow: 0 0 0 0 var(--accent-soft); }
  60% { box-shadow: 0 0 0 9px var(--accent-soft); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

@keyframes node-on {
  to { border-color: var(--accent); background-color: var(--accent); }
}

@keyframes fade-out {
  to { opacity: 0; }
}

/* 滚动驱动: 阅读进度线, 节点进入视口后点亮, 列表项依次浮现. 不支持的浏览器保持静态. */
@supports (animation-timeline: scroll()) {
  .article::after {
    animation: draw linear both;
    animation-timeline: scroll(root);
  }
  .article h2::before {
    animation: node-on linear both;
    animation-timeline: view();
    animation-range: entry 100% cover 45%;
  }
  .post-item {
    animation: rise linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 60%;
  }
}

/* 站内跳转的页面过渡 (支持跨文档 View Transition 的浏览器). */
@view-transition { navigation: auto; }
::view-transition-old(root) { animation: 0.2s ease-out both fade-out; }
::view-transition-new(root) { animation: 0.45s var(--ease) both rise; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
  ::view-transition-old(root),
  ::view-transition-new(root) { animation: none !important; }
}

/* ---------- 窄屏 ---------- */

@media (max-width: 640px) {
  .hero {
    grid-template-columns: 1fr;
    padding: 3rem 0;
  }

  /* 窄屏改为横向: 线在节点上方. */
  .trace {
    grid-column: 1;
    grid-row: auto;
    grid-auto-flow: column;
    justify-content: start;
    gap: 2.25rem;
    margin-top: 2.25rem;
    padding: 1.1rem 0 0;
  }
  .trace::before {
    right: 0;
    top: 3.5px;
    bottom: auto;
    width: auto;
    height: 1px;
    transform-origin: left;
    animation-name: draw-x;
  }
  .trace li::before { left: 0; top: -1.1rem; }

  .post-item a { grid-template-columns: 1fr; gap: 0.35rem; }
  .article { padding-left: 1.25rem; }
  .article h2::before { left: calc(-1.25rem - 4px); }
}
