/* ==========================================================================
   实验4 · 学生生活费流向了哪里
   纯 HTML5 + CSS3：不使用 JavaScript、SVG、Canvas 或任何图表库。
   颜色、尺寸与间距集中在 CSS 自定义属性中，便于统一替换和切换主题。
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. 基础复位与字体
   中文只使用本地系统字体，不请求在线字体或外部资源。
   -------------------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background: #f3f5fa;
  -webkit-text-size-adjust: 100%;
  /* 平滑滚动，但可被 prefers-reduced-motion 覆盖 */
  scroll-behavior: smooth;
}

/* 阅读进度条：一条 3px 的强调色细条，用 transform: scaleX(0 → 1) 从左向右展开。
   只动画 transform，缩放由合成层完成，不触发重排与重绘；
   transform-origin: left 保证从左边开始生长。
   颜色取自 .page 的 --accent，因此深浅主题下都会同步变色。 */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  z-index: 1000;
  pointer-events: none;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left center;
  will-change: transform;
}

@supports (animation-timeline: scroll()) {
  /* 用根滚动条时间线 scroll(root block)，进度严格从页面顶部 0 到页面底部 1，
     不依赖 html 的 view 区间，避免顶部就已有进度、底部又不满的现象。 */
  .reading-progress {
    display: block;
    animation: progress-grow linear both;
    animation-timeline: scroll(root block);
  }

  @keyframes progress-grow {
    from {
      transform: scaleX(0);
    }
    to {
      transform: scaleX(1);
    }
  }
}

@supports not (animation-timeline: scroll()) {
  .reading-progress {
    display: none;
  }
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", "Microsoft YaHei",
    "PingFang SC", "Noto Sans SC", "Source Han Sans SC", sans-serif;
  line-height: 1.75;
}

h1,
h2,
h3,
h4,
p,
figure,
blockquote,
ul,
ol {
  margin: 0;
}

ul,
ol {
  padding: 0;
}

a {
  color: inherit;
  text-decoration: none;
}

table {
  border-collapse: collapse;
}

/* 键盘焦点：链接、主题开关和数据表摘要都有清晰的焦点环 */
:where(a, input, summary):focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
  border-radius: 4px;
}

/* --------------------------------------------------------------------------
   2. CSS 变量与主题切换
   .page 统一持有浅色主题变量；顶部隐藏的 checkbox 被选中后，
   通过相邻兄弟选择器覆盖同一批变量，实现纯 CSS 深色主题。
   -------------------------------------------------------------------------- */
:root {
  --content: 1120px;
  --pad: clamp(16px, 4vw, 32px);
  --radius: 14px;
}

.page {
  /* 颜色 */
  --bg: #f3f5fa;
  --surface: #ffffff;
  --surface-soft: #f7f8fd;
  --ink: #1b1f2e;
  --muted: #5c6274;
  --line: #e1e5f0;
  --accent: #4b57c8;
  --accent-soft: #e8eafe;
  --track: #e9ecf7;

  /* 环形图四档同色系亮度：
     浅色主题 38% / 55% / 70% / 86%，相邻亮度差为 17、15、16 个百分点。
     转灰度后仍保留明显明度差。 */
  --seg-1: hsl(245 62% 38%);
  --seg-2: hsl(248 62% 55%);
  --seg-3: hsl(251 65% 70%);
  --seg-4: hsl(255 70% 86%);

  /* 非当前扇区的压暗层颜色，以及当前扇区的强调色 */
  --veil: rgba(243, 245, 250, 0.72);
  --focus-tint: rgba(75, 87, 200, 0.38);

  /* 尺寸 */
  --gap: clamp(16px, 3vw, 28px);
  --section-pad: clamp(40px, 6.5vw, 80px);

  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  transition: background-color 0.25s ease, color 0.25s ease;
}

/* 深色主题：同一组变量整体覆盖，各个组件无需写第二套样式。
   四档亮度改为 50% / 65% / 80% / 95%，相邻仍相差 15 个百分点。 */
#theme-toggle:checked ~ .page {
  color-scheme: dark;
  --bg: #10121a;
  --surface: #171a24;
  --surface-soft: #1c202c;
  --ink: #eef1fb;
  --muted: #a7aec4;
  --line: #2b3142;
  --accent: #96a3ff;
  --accent-soft: #242a4a;
  --track: #252b3b;
  --seg-1: hsl(245 70% 50%);
  --seg-2: hsl(248 72% 65%);
  --seg-3: hsl(251 76% 80%);
  --seg-4: hsl(255 80% 95%);
  --veil: rgba(16, 18, 26, 0.7);
  --focus-tint: rgba(150, 163, 255, 0.42);
}

/* 视觉隐藏但仍可聚焦的 checkbox（不能用 display:none，否则无法键盘操作）。
   position: fixed 让它在获得焦点时不会把页面滚回顶部。 */
.theme-checkbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.theme-switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  color: var(--muted);
  user-select: none;
}

.switch-track {
  position: relative;
  width: 42px;
  height: 24px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--track);
  transition: background-color 0.2s ease;
}

.switch-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  transition: transform 0.2s ease;
}

#theme-toggle:checked ~ .page .switch-knob {
  transform: translateX(18px);
}

.switch-text .t-dark {
  display: none;
}

#theme-toggle:checked ~ .page .switch-text .t-light {
  display: none;
}

#theme-toggle:checked ~ .page .switch-text .t-dark {
  display: inline;
}

/* 键盘聚焦时，把焦点环画在可见的 label 上 */
#theme-toggle:focus-visible ~ .page .theme-switch {
  outline: 3px solid var(--accent);
  outline-offset: 4px;
  border-radius: 999px;
}

/* --------------------------------------------------------------------------
   3. 布局与页头
   -------------------------------------------------------------------------- */
.wrap {
  width: min(var(--content), calc(100% - 2 * var(--pad)));
  margin-inline: auto;
}

.skip-link {
  position: absolute;
  left: 16px;
  top: -60px;
  z-index: 10;
  padding: 10px 16px;
  border-radius: 0 0 10px 10px;
  background: var(--accent);
  color: var(--bg);
  font-size: 0.88rem;
  transition: top 0.2s ease;
}

.skip-link:focus-visible {
  top: 0;
}

.site-header {
  /* 毛玻璃：背景半透明 + backdrop-filter 模糊；不支持的浏览器降级为纯色 */
  position: sticky;
  top: 0;
  z-index: 50;
  border-bottom: 1px solid var(--line);
  background: color-mix(in oklab, var(--surface) 72%, transparent);
  backdrop-filter: saturate(160%) blur(10px);
  -webkit-backdrop-filter: saturate(160%) blur(10px);
}

/* 不支持 backdrop-filter 时，退化为不透明纯色，保证文字可读 */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .site-header {
    background: var(--surface);
  }
}

.header-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 24px;
  padding-block: 14px;
}

.brand {
  font-size: 0.92rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--ink);
}

.brand:hover {
  color: var(--accent);
}

.header-tag {
  margin-right: auto;
  font-size: 0.78rem;
  color: var(--muted);
  letter-spacing: 0.06em;
}

.site-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin-left: auto;
  font-size: 0.88rem;
}

.site-nav a {
  padding: 4px 0;
  color: var(--muted);
  border-bottom: 1px solid transparent;
}

.site-nav a:hover {
  color: var(--accent);
  border-bottom-color: currentColor;
}

.header-inner .theme-switch {
  margin-left: 4px;
}

/* --------------------------------------------------------------------------
   4. 标题层级与通用区块
   -------------------------------------------------------------------------- */
.eyebrow {
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  color: var(--accent);
}

.hero {
  padding-block: clamp(48px, 8vw, 96px) clamp(32px, 5vw, 56px);
}

.hero h1 {
  margin: 10px 0 18px;
  font-size: clamp(2.25rem, 6.2vw, 4.4rem);
  line-height: 1.08;
  letter-spacing: -0.02em;
  font-weight: 800;
}

.hero h1 .accent {
  color: var(--accent);
}

.deck {
  max-width: 40em;
  font-size: clamp(1.02rem, 1.6vw, 1.22rem);
  line-height: 1.85;
  color: var(--muted);
}

.hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
  margin-top: 26px;
}

.hero-meta span {
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.78rem;
  color: var(--muted);
}

.section {
  padding-block: var(--section-pad);
  scroll-margin-top: 24px;
}

.section + .section {
  border-top: 1px solid var(--line);
}

.section-head {
  max-width: 46rem;
  margin-bottom: clamp(24px, 4vw, 40px);
}

.section-head h2 {
  margin: 8px 0 12px;
  font-size: clamp(1.55rem, 3.1vw, 2.35rem);
  line-height: 1.28;
  letter-spacing: -0.01em;
}

.lede {
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.85;
}

/* 作品介绍 */
.intro-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.intro-card {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.intro-card h3 {
  margin-bottom: 8px;
  font-size: 1rem;
}

.intro-card p {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.78;
}

/* --------------------------------------------------------------------------
   5. 图表卡片、图注、表格与链接
   -------------------------------------------------------------------------- */
.chart-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr);
  gap: var(--gap);
  align-items: start;
}

.chart-card,
.chart-side {
  padding: clamp(18px, 3vw, 28px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 25, 50, 0.04);
}

.figure-head {
  margin-bottom: 20px;
}

.figure-head h3 {
  margin-bottom: 4px;
  font-size: 1.08rem;
  line-height: 1.4;
}

.figure-head p {
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.figure-note {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
  font-size: 0.8rem;
  line-height: 1.75;
  color: var(--muted);
}

/* 环形图灰度预览开关：checkbox 与 label 都位于卡片内部，
   Tab 顺序出现在环形图区域，而不是页面最顶部。
   点击 label 后 filter: grayscale(1) 作用于环形图与图例色块，
   不影响文字本身。键盘 Tab 可聚焦、空格可切换。 */
.chart-card {
  position: relative;
}

/* 卡片内的隐藏 checkbox：绝对定位到 label 所在位置，
   视觉裁剪为 1px 但保留可聚焦能力；不使用 position: fixed，
   避免聚焦时把页面滚回顶部。 */
.chart-card .theme-checkbox {
  position: absolute;
  top: 16px;
  right: 16px;
  left: auto;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.grayscale-switch {
  position: absolute;
  top: 16px;
  right: 16px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  z-index: 5;
  cursor: pointer;
  font-size: 0.74rem;
  color: var(--muted);
  user-select: none;
  padding: 3px 8px 3px 4px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  transition: border-color 0.2s ease;
}

/* 键盘聚焦到隐藏 checkbox 时，在 label 上显示焦点环 */
#grayscale-toggle:focus-visible ~ .grayscale-switch {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.grayscale-switch .switch-track {
  width: 32px;
  height: 18px;
}

.grayscale-switch .switch-knob {
  width: 14px;
  height: 14px;
}

/* checkbox 与 label / 图形 / 图例处于同一父级，直接用相邻兄弟选择器 */
#grayscale-toggle:checked ~ .grayscale-switch .switch-knob {
  transform: translateX(14px);
}

#grayscale-toggle:checked ~ .donut-stage .donut,
#grayscale-toggle:checked ~ .legend .swatch {
  filter: grayscale(1);
  transition: filter 0.3s ease;
}

/* 自然语言文字说明（读屏友好） */
.donut-plaintext {
  margin-top: 12px;
  padding: 10px 14px;
  background: var(--surface-soft);
  border-radius: 8px;
  font-size: 0.82rem;
  line-height: 1.7;
  color: var(--muted);
}

.donut-plaintext strong {
  color: var(--ink);
  font-weight: 600;
}

.chart-side h3 {
  margin-bottom: 10px;
  font-size: 1.02rem;
}

.chart-side p,
.chart-side li {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.78;
}

.chart-side ul {
  margin: 0 0 4px;
  padding-left: 1.15em;
}

.chart-side li + li {
  margin-top: 4px;
}

.side-links {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  margin-top: 18px;
}

.text-link {
  color: var(--accent);
  font-size: 0.88rem;
  font-weight: 600;
  border-bottom: 1px solid transparent;
}

.text-link:hover {
  border-bottom-color: currentColor;
}

/* 表格：外层允许局部横向滚动，但不影响页面其他区域 */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  margin-top: 16px;
}

table {
  width: 100%;
  min-width: 240px;
  font-size: 0.85rem;
}

caption {
  padding-bottom: 8px;
  text-align: left;
  font-size: 0.78rem;
  color: var(--muted);
}

th,
td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

thead th {
  background: var(--surface-soft);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
}

tbody th {
  font-weight: 500;
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   6. 柱状图
   原始数据（按作业要求固定顺序）：
     餐饮 42% / 房租 21% / 学习资料 14% / 社交 12% / 交通 11%
   归一化绘图高度 = 当前值 ÷ 最大值(42) × 100：
     餐饮      42 ÷ 42 × 100 = 100
     房租      21 ÷ 42 × 100 = 50
     学习资料  14 ÷ 42 × 100 ≈ 33.3333
     社交      12 ÷ 42 × 100 ≈ 28.5714
     交通      11 ÷ 42 × 100 ≈ 26.1905
   把结果写进行内 --v，柱高统一用 calc(var(--v) * 1%) 计算。
   父级 .bar-plot 有明确高度，百分比高度才能实际生效。

   ─── 为什么不能直接把百分比数值当柱高 ───
   ① 绘图区高度固定（clamp(330px,36vw,400px)），如果把 42% 的柱子画成
     42% × 绘图区高度，它只有 ~140px 高，视觉上非常矮小，五根柱子差距也
     不够明显，做不到"餐饮明显高于其余四项"的数据新闻效果。
   ② 归一化让最大值占满绘图区（100%），其余柱子按相对比例缩放，
     真正体现出"倍数关系"——餐饮比房租高 2 倍、比交通高近 4 倍。
   ③ 原始占比仍然放在柱顶的 .bar-value 上，读者同时看到"相对高度"和"真实数值"，
     不产生"最高柱代表 100% 支出"的误解。

   视觉增强：同色系微渐变、顶部圆角、max/mid 水平参考线、单位标注、:has() 悬停。
   -------------------------------------------------------------------------- */
.bar-plot {
  /* 绘图区有明确高度；上下内边距分别留给数值标签和类别名称 */
  height: clamp(330px, 36vw, 400px);
  padding: 32px 0 42px;
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: clamp(8px, 2vw, 22px);
  border-bottom: 0;
  /* 右上角单位标注：相对绘图区定位，浮动在柱子上方 */
  counter-reset: bar-scale-mark 100;
}

/* 水平参考线：max (100%, 绘图区顶) 和 mid (50%)，淡色细线帮助读高度。
   baseline 已由 ::after 提供 */
.bar-plot::before,
.bar-plot::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
}

.bar-plot::before {
  /* max 参考线 */
  top: 32px;
  height: 1px;
  background: var(--line);
  opacity: 0.6;
}

.bar-plot::after {
  /* baseline */
  bottom: 42px;
  height: 1px;
  background: var(--line);
}

/* mid 参考线（50%）：用一个中间伪元素做不到，加一个额外层 */
.bar-plot .bar-plot-guide {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(32px + (100% - 74px) / 2);
  height: 1px;
  border-top: 1px dashed var(--line);
  opacity: 0.55;
  pointer-events: none;
}

/* 右上角"单位：%"标注 */
.bar-plot .bar-unit {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.03em;
}

.bar-col {
  position: relative;
  height: 100%;
}

/* 柱子本体：高度完全由 --v 与父级高度决定 */
.bar-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--v) * 1%);
  border-radius: 6px 6px 0 0;
  background: var(--track);
  overflow: hidden;
}

/* 入场动画只作用于内部填充层，且只改变 transform / opacity；
   柱子的 height、文字与布局都不参与动画。
   填充层使用同色系微渐变，从深到浅，增加立体感而不喧宾夺主。 */
.bar-fill {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  /* 同色系微渐变：顶部稍亮，底部保留 accent 主色 */
  background: linear-gradient(
    to top,
    var(--accent) 0%,
    hsl(from var(--accent) h s calc(l * 1.18)) 60%,
    hsl(from var(--accent) h s calc(l * 1.35)) 100%
  );
  transform-origin: bottom center;
  animation: bar-grow 700ms cubic-bezier(0.22, 0.7, 0.3, 1) both;
  animation-delay: calc(var(--i, 0) * 90ms + 120ms);
  transition: filter 0.2s ease, transform 0.15s ease;
}

/* :has() 悬停微交互：悬停任意一根柱子，其余柱子压暗，
   当前柱子稍微提亮。移动端 :hover 效果自然被 tap 触发一次后保持，
   不影响无悬停时的正常阅读。 */
.bar-plot:has(.bar-col:hover) .bar-col:not(:hover) .bar-fill {
  filter: brightness(0.82) saturate(0.6);
}

.bar-plot .bar-col:hover .bar-fill {
  filter: brightness(1.08) saturate(1.1);
}

@keyframes bar-grow {
  from {
    transform: scaleY(0.02);
    opacity: 0.25;
  }
  to {
    transform: scaleY(1);
    opacity: 1;
  }
}

/* 数值锚定在柱顶，位置同样由 --v 计算 */
.bar-value {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(var(--v) * 1% + 10px);
  text-align: center;
  font-size: clamp(0.8rem, 1.5vw, 1rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

/* 类别名称在基线下方 */
.bar-category {
  position: absolute;
  top: calc(100% + 12px);
  left: 0;
  right: 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
}

/* 375px 及以下：同一组 --v 改控制横向条形的宽度。
   显式指定 grid-row: 1，避免三列自动换行导致
   类别 / 条形 / 百分比垂直错开、"学习资料"被拆开。
   同时收起纵向柱状图专用的参考线与基线，避免横线穿过条形；
   单位标注改为静态块，独占一行，不与第一行的百分比重叠。 */
@media (max-width: 375px) {
  .bar-plot {
    display: flex;
    flex-direction: column;
    height: auto;
    padding: 2px 0 0;
    gap: 10px;
  }

  /* 纵向模式的参考线 / 基线在横向条形图里没有意义，全部隐藏 */
  .bar-plot::before,
  .bar-plot::after,
  .bar-plot .bar-plot-guide {
    display: none;
  }

  /* 单位标注退出绝对定位，作为 flex 的首个普通子项单独占一行 */
  .bar-plot .bar-unit {
    position: static;
    top: auto;
    right: auto;
    text-align: right;
    margin-bottom: -4px;
  }

  .bar-col {
    height: 28px;
    display: grid;
    grid-template-columns: 4.8em minmax(0, 1fr) 3em;
    grid-template-rows: 1fr;
    align-items: center;
    gap: 6px 10px;
  }

  .bar-bar {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    grid-column: 2;
    grid-row: 1;
    height: 20px;
    width: calc(var(--v) * 1%);
    border-radius: 0 5px 5px 0;
  }

  .bar-fill {
    animation-name: bar-grow-x;
    transform-origin: left center;
  }

  .bar-category {
    position: static;
    top: auto;
    grid-column: 1;
    grid-row: 1;
    text-align: left;
    font-size: 0.8rem;
    color: var(--ink);
    white-space: nowrap;
  }

  .bar-value {
    position: static;
    bottom: auto;
    grid-column: 3;
    grid-row: 1;
    text-align: right;
    font-size: 0.8rem;
    white-space: nowrap;
  }
}

@keyframes bar-grow-x {
  from {
    transform: scaleX(0.02);
    opacity: 0.25;
  }
  to {
    transform: scaleX(1);
    opacity: 1;
  }
}

@media (max-width: 560px) {
  .bar-category,
  .bar-value {
    font-size: 0.78rem;
  }
}

/* --------------------------------------------------------------------------
   7. 环形图
   另一套数据（与柱状图不同，不能混用）：
     餐饮 43% / 房租 27% / 学习资料 18% / 其他 12%
   角度 = 占比 × 360°（1% 对应 3.6°）：
     餐饮      43% × 360° = 154.8°
     房租      27% × 360° = 97.2°，累加结束 154.8° + 97.2° = 252°
     学习资料  18% × 360° = 64.8°，累加结束 252° + 64.8° = 316.8°
     其他      12% × 360° = 43.2°，累加结束 316.8° + 43.2° = 360°
   下面 .donut 内先用 --p1..--p4 声明占比，再用 calc() 推导出
   --deg-1..--deg-4 四个累加边界，让扇区大小完全由百分比驱动。
   conic-gradient 直接引用 --deg-* 变量作为色标边界，改一处全更新。
   -------------------------------------------------------------------------- */
.donut {
  /* 四个占比变量：1% 对应 3.6°，用累加方式推导每段结束边界，
     让扇区大小完全由百分比驱动，便于后续调整数据。
     餐饮    43% × 3.6° = 154.8°
     房租    27% × 3.6° = 97.2°，累加结束 252°
     学习资料 18% × 3.6° = 64.8°，累加结束 316.8°
     其他    12% × 3.6° = 43.2°，累加结束 360° */
  --p1: 43;
  --p2: 27;
  --p3: 18;
  --p4: 12;

  /* 四个累加边界（conic-gradient 的色标 stop 位置） */
  --deg-1: calc(var(--p1) * 3.6deg);
  --deg-2: calc((var(--p1) + var(--p2)) * 3.6deg);
  --deg-3: calc((var(--p1) + var(--p2) + var(--p3)) * 3.6deg);
  --deg-4: calc((var(--p1) + var(--p2) + var(--p3) + var(--p4)) * 3.6deg);

  position: relative;
  width: min(320px, 100%);
  aspect-ratio: 1 / 1;
  margin-inline: auto;
  border-radius: 50%;
  /* conic-gradient 四段边界直接引用上面的 --deg-1..--deg-4，
     渲染结果为：0°–154.8° 餐饮 / 154.8°–252° 房租 /
                 252°–316.8° 学习资料 / 316.8°–360° 其他 */
  background: conic-gradient(
    var(--seg-1) 0deg var(--deg-1),
    var(--seg-2) var(--deg-1) var(--deg-2),
    var(--seg-3) var(--deg-2) var(--deg-3),
    var(--seg-4) var(--deg-3) var(--deg-4)
  );
}

/* 中心留白：显示 100% 或当前叙事状态的占比 */
.donut-center {
  position: absolute;
  inset: 28%;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--surface);
  text-align: center;
}

.center-layer {
  grid-area: 1 / 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px;
  /* 基础状态隐藏；只有滚动驱动动画或静态状态类会把某一层打开 */
  opacity: 0;
}

/* 不支持滚动动画时，默认显示 100% 总览，避免中心空白 */
.center-default {
  opacity: 1;
}

.center-layer strong {
  font-size: clamp(1.5rem, 4.2vw, 2.2rem);
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.center-layer span {
  font-size: clamp(0.68rem, 1.6vw, 0.8rem);
  color: var(--muted);
}

/* 图例说明文字，放在 <ul class="legend"> 之前，说明整体口径 */
.legend-caption {
  margin: 0 0 8px;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.5;
}

/* 图例 */
.legend {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 16px;
  margin-top: 20px;
  list-style: none;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.84rem;
  color: var(--muted);
}

.legend .swatch {
  width: 12px;
  height: 12px;
  flex: none;
  border-radius: 3px;
  background: var(--sw);
}

.legend strong {
  margin-left: auto;
  color: var(--ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* 图例项内的圆心角（占比后面的补充） */
.legend .legend-angle {
  margin-left: 6px;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.donut-stage {
  display: grid;
  place-items: center;
  padding-block: 4px;
}

/* 中心留白同样需要正圆，手机尺寸随卡片收缩但不拉伸 */
@media (max-width: 768px) {
  .donut {
    width: min(300px, 100%);
  }
}

/* --------------------------------------------------------------------------
   8. 滚动叙事
   Grid 负责左右两栏；sticky 负责让右侧图表在阅读左侧文字时留在视口内。

   sticky 的三个关键条件（必须同时满足）：
   1) 父容器高度足够：.story-grid 的高度由左侧四段文字撑高，右侧
      .story-chart-wrap 因 align-items:stretch 自动获得与文字栏等高的容器。
      sticky 的"可移动行程" = 父容器高度 − 图表自身高度；父容器过矮，
      图表刚吸顶就会被整体带走，无法在阅读期间保持吸附。
   2) top + 自身高度 < 视口高度：.story-chart 的 top（clamp 16-72px）与
      图表高度之和要小于视口，保证吸顶后仍能看到图注与下一段文字开头。
   3) 祖先元素不要设置 overflow: hidden / auto / scroll，否则会生成新的
      滚动容器，吸顶会异常；本页只对表格等局部元素使用 overflow。

   让图表自然离开：
   每段 .story-step 的 min-height: 68vh（65–75vh 范围内）拉开阅读节奏并
   撑高父容器；第四段之后的 .story-tail 再提供一段行程，让图表随父容器
   自然离场，不留下人为的大块空白。

   768px 以下的退化机制（见文件末尾的媒体查询）：
   - .story-grid 退化为单列；
   - .story-chart-wrap（右侧吸顶大图）display:none，取消不适合手机的吸顶；
   - .story-step 的 min-height 归零，按内容自然高度顺序排列；
   - 每段文字下方显示对应的小环形图 .step-chart。
   -------------------------------------------------------------------------- */
.story-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.06fr) minmax(0, 0.94fr);
  gap: clamp(24px, 5vw, 64px);
  /* 不要用 align-items:start，否则右栏不会获得与文字栏等高的 sticky 容器 */
  align-items: stretch;
}

.story-steps {
  min-width: 0;
}

/* 每段 min-height 68vh 拉开阅读节奏，让 sticky 有足够停留行程。
   用 flex + justify-content:center 让文字在每段中垂直居中，阅读更舒展。 */
.story-step {
  min-height: 68vh;
  padding-bottom: clamp(20px, 3vw, 40px);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.story-step + .story-step {
  padding-top: clamp(20px, 3vw, 40px);
  border-top: 1px solid var(--line);
}

/* 叙事收尾：既是内容说明，也为 sticky 提供最后一段行程，
   让图表在第四段读完后自然离开，而不是提前被容器裁切。 */
.story-tail {
  padding: 28px 0 96px;
  border-top: 1px dashed var(--line);
  font-size: 0.8rem;
  line-height: 1.8;
  color: var(--muted);
}

.story-tail strong {
  color: var(--ink);
  font-weight: 600;
}

.step-index {
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
}

.story-step h3 {
  margin: 6px 0 10px;
  font-size: clamp(1.3rem, 2.2vw, 1.7rem);
  line-height: 1.35;
}

.step-figure {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 12px;
}

.step-figure strong {
  font-size: clamp(2rem, 3.6vw, 2.8rem);
  line-height: 1;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.step-figure span {
  font-size: 0.8rem;
  color: var(--muted);
}

.story-step > p:not(.step-index, .step-figure) {
  max-width: 34em;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.9;
}

.story-chart-wrap {
  min-width: 0;
}

.story-chart {
  position: sticky;
  top: clamp(16px, 8vh, 72px);
  padding: clamp(16px, 2.5vw, 24px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 25, 50, 0.04);
}

.story-chart .donut {
  width: min(320px, 100%);
}

.chart-kicker {
  margin-bottom: 14px;
  text-align: center;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
}

.story-chart .legend {
  margin-top: 16px;
}

.story-chart-note {
  margin-top: 14px;
  text-align: center;
  font-size: 0.76rem;
  color: var(--muted);
}

/* 压暗层：完整覆盖圆盘，只在当前扇区留出透明窗口。
   不改变扇区角度，只改变视觉权重；同一时刻最多显示一层。 */
.dim-layer {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  opacity: 0;
  pointer-events: none;
}

.dim-1 {
  /* 保留餐饮 (0°--deg-1, 即 0°-154.8°)，其余扇区压暗 */
  background: conic-gradient(
    transparent 0deg var(--deg-1),
    var(--veil) var(--deg-1) 360deg
  );
}

.dim-2 {
  /* 保留房租 (--deg-1--deg-2, 即 154.8°-252°)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-1),
    transparent var(--deg-1) var(--deg-2),
    var(--veil) var(--deg-2) 360deg
  );
}

.dim-3 {
  /* 保留学习资料 (--deg-2--deg-3, 即 252°-316.8°)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-2),
    transparent var(--deg-2) var(--deg-3),
    var(--veil) var(--deg-3) 360deg
  );
}

.dim-4 {
  /* 保留其他 (--deg-3--360°, 即 316.8°-360°)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-3),
    transparent var(--deg-3) 360deg
  );
}

/* 当前扇区的强调层：只覆盖该扇区的真实角度，用半透明强调色加深，
   让最浅的 12% 扇区也能被清楚识别。中心圆盘会盖住内圈。 */
.focus-layer {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  opacity: 0;
  pointer-events: none;
}

.focus-1 {
  /* 强调餐饮扇区 (0°--deg-1) */
  background: conic-gradient(
    var(--focus-tint) 0deg var(--deg-1),
    transparent var(--deg-1) 360deg
  );
}

.focus-2 {
  /* 强调房租扇区 (--deg-1--deg-2) */
  background: conic-gradient(
    transparent 0deg var(--deg-1),
    var(--focus-tint) var(--deg-1) var(--deg-2),
    transparent var(--deg-2) 360deg
  );
}

.focus-3 {
  /* 强调学习资料扇区 (--deg-2--deg-3) */
  background: conic-gradient(
    transparent 0deg var(--deg-2),
    var(--focus-tint) var(--deg-2) var(--deg-3),
    transparent var(--deg-3) 360deg
  );
}

.focus-4 {
  /* 强调其他扇区 (--deg-3--360°) */
  background: conic-gradient(
    transparent 0deg var(--deg-3),
    var(--focus-tint) var(--deg-3) 360deg
  );
}

/* 手机端：单栏顺序阅读，取消吸顶，每段文字后面紧跟对应的小环形图 */
.step-chart {
  display: none;
}

@media (max-width: 768px) {
  .story-grid {
    grid-template-columns: 1fr;
  }

  .story-chart-wrap {
    display: none;
  }

  .story-step {
    min-height: 0;
    padding-bottom: 32px;
  }

  .story-tail {
    padding-bottom: 40px;
  }

  .story-step + .story-step {
    padding-top: 32px;
  }

  .story-step > p:not(.step-index, .step-figure) {
    font-size: 0.94rem;
  }

  .step-chart {
    display: block;
    margin: 20px 0 4px;
  }

  .step-chart figcaption {
    margin-top: 10px;
    text-align: center;
    font-size: 0.78rem;
    color: var(--muted);
  }
}

/* 手机端小环形图：固定正圆，只压暗其他扇区 */
.donut--mini {
  width: clamp(150px, 46vw, 190px);
}

.donut--mini .donut-center {
  inset: 30%;
}

.donut--mini .center-layer {
  opacity: 1;
}

.donut--mini .center-layer strong {
  font-size: 1.35rem;
}

.mini-veil {
  position: absolute;
  inset: 0;
  border-radius: 50%;
}

.donut--mini .focus-layer {
  opacity: 1;
}

.mini-1 .mini-veil {
  /* 小图：保留餐饮 (0°--deg-1)，其余压暗 */
  background: conic-gradient(
    transparent 0deg var(--deg-1),
    var(--veil) var(--deg-1) 360deg
  );
}

.mini-2 .mini-veil {
  /* 小图：保留房租 (--deg-1--deg-2)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-1),
    transparent var(--deg-1) var(--deg-2),
    var(--veil) var(--deg-2) 360deg
  );
}

.mini-3 .mini-veil {
  /* 小图：保留学习资料 (--deg-2--deg-3)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-2),
    transparent var(--deg-2) var(--deg-3),
    var(--veil) var(--deg-3) 360deg
  );
}

.mini-4 .mini-veil {
  /* 小图：保留其他 (--deg-3--360°)，其余压暗 */
  background: conic-gradient(
    var(--veil) 0deg var(--deg-3),
    transparent var(--deg-3) 360deg
  );
}

/* --------------------------------------------------------------------------
   9. 滚动对应状态（纯 CSS 滚动驱动动画）
   说明：:has()、相邻兄弟选择器、:target 都不能自动感知元素是否进入视口。
   这里把 .story-grid 声明为具名 view 时间线，右侧图表中的状态层使用
   animation-timeline 引用同一条时间线，随页面正常滚动推进。
   四个状态共用同一套 43/27/18/12 的角度变量，只切换压暗层与中心文字，
   不改变任何扇区大小，也不是定时轮播。

   animation-range 把四段文字对应的阅读区间映射为 0—100%：
     0—25%   餐饮
     25—50%  房租
     50—75%  学习资料
     75—100% 其他
   关键帧在边界处直接切换，保证任一时刻只显示一个中心数字和一层压暗层。
   在支持该特性的浏览器中才启用；不支持时基础样式已给出完整的 100% 总览。
   -------------------------------------------------------------------------- */
@supports (animation-timeline: view()) {
  .story-grid {
    view-timeline-name: --story;
    view-timeline-axis: block;
  }

  .donut--story .dim-layer,
  .donut--story .focus-layer,
  .donut--story .center-layer {
    animation-duration: 1ms;
    /* step-end 让状态在边界处直接切换，避免 0.1% 的交叉淡入窗口里
       出现两个中心数字都不完全可见（或同时可见）的情况。 */
    animation-timing-function: steps(1, end);
    animation-fill-mode: both;
    animation-timeline: --story;
    animation-range: cover 26.5% cover 86.5%;
  }

  .donut--story .dim-1,
  .donut--story .focus-1,
  .donut--story .center-1 {
    animation-name: story-state-1;
  }

  .donut--story .dim-2,
  .donut--story .focus-2,
  .donut--story .center-2 {
    animation-name: story-state-2;
  }

  .donut--story .dim-3,
  .donut--story .focus-3,
  .donut--story .center-3 {
    animation-name: story-state-3;
  }

  .donut--story .dim-4,
  .donut--story .focus-4,
  .donut--story .center-4 {
    animation-name: story-state-4;
  }

  .donut--story .center-default {
    animation-name: story-state-default;
  }
}

@keyframes story-state-1 {
  0% {
    opacity: 1;
  }
  25%,
  100% {
    opacity: 0;
  }
}

@keyframes story-state-2 {
  0% {
    opacity: 0;
  }
  25% {
    opacity: 1;
  }
  50%,
  100% {
    opacity: 0;
  }
}

@keyframes story-state-3 {
  0%,
  50% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  75%,
  100% {
    opacity: 0;
  }
}

@keyframes story-state-4 {
  0%,
  75% {
    opacity: 0;
  }
  75% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}

/* 支持滚动驱动时，100% 总览层让位给分段状态；不支持时它仍是默认内容 */
@keyframes story-state-default {
  0%,
  100% {
    opacity: 0;
  }
}

/* --------------------------------------------------------------------------
   10. 入场动画
   每个 .reveal 通过行内 --i 控制顺序，延迟 = var(--i) × 90ms。
   只动画 transform 与 opacity，时长 600ms，fill-mode: both。
   基础样式不隐藏内容；即使动画被禁用，元素仍然完整可见。

   增强：在 @supports (animation-timeline: view()) 的浏览器里，
   把 .reveal 绑定到 view() 时间线，让入场动画跟随元素进入视口触发，
   而不是页面加载就一次性全部播放。animation-range: entry 0% entry 85%
   确保动画在元素"刚进视口"到"完全可见"之间完成一次。
   不支持该特性的浏览器自动降级为基础样式：页面加载即播放 + 顺序延迟。
   两种方式都保证内容立即可见（opacity 默认 1，fill-mode: both 只是暂存初始帧）。
   -------------------------------------------------------------------------- */
.reveal {
  animation: reveal-up 600ms cubic-bezier(0.22, 0.7, 0.3, 1) both;
  animation-delay: calc(var(--i, 0) * 90ms);
}

@supports (animation-timeline: view()) {
  .reveal {
    animation: reveal-up 600ms cubic-bezier(0.22, 0.7, 0.3, 1) both;
    animation-timeline: view();
    animation-range: entry 0% entry 85%;
    /* view() 驱动时不需要按 --i 顺序延迟，元素各进各的 */
    animation-delay: 0ms;
  }
}

@keyframes reveal-up {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* --------------------------------------------------------------------------
   11. 数据说明、演示页入口与页脚
   -------------------------------------------------------------------------- */
.notes-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.note-card {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.note-card h3 {
  margin-bottom: 8px;
  font-size: 1rem;
}

.note-card p {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.8;
}

.note-card code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 0.84em;
}

.demo-links {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.demo-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  transition: border-color 0.2s ease, transform 0.2s ease;
}

.demo-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.demo-num {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
}

.demo-card h3 {
  font-size: 1.05rem;
}

.demo-card p {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.75;
}

.demo-arrow {
  margin-top: auto;
  padding-top: 10px;
  color: var(--accent);
  font-size: 0.84rem;
  font-weight: 600;
}

.site-footer {
  padding-block: 32px 48px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.footer-inner {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px 24px;
  font-size: 0.8rem;
  color: var(--muted);
}

/* 独立演示页 */
.demo-main {
  padding-block: clamp(32px, 6vw, 64px) 72px;
}

.demo-hero {
  max-width: 50rem;
  margin-bottom: 28px;
}

.demo-hero h1 {
  margin: 8px 0 12px;
  font-size: clamp(1.8rem, 4vw, 2.8rem);
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.demo-figure {
  max-width: 880px;
}

.demo-figure .bar-plot {
  max-width: 760px;
}

.demo-note {
  max-width: 760px;
  margin-top: 20px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--surface);
  font-size: 0.86rem;
  line-height: 1.8;
  color: var(--muted);
}

.demo-back {
  display: inline-flex;
  gap: 6px;
  margin-top: 28px;
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 600;
}

.demo-back:hover {
  text-decoration: underline;
}

/* 滚动叙事演示页中的图表更宽一些 */
.story-demo {
  max-width: 1080px;
}

/* --------------------------------------------------------------------------
   12. 响应式：中等屏幕与手机
   -------------------------------------------------------------------------- */
@media (max-width: 900px) {
  .chart-layout {
    grid-template-columns: 1fr;
  }

  .intro-grid,
  .demo-links {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .notes-grid {
    grid-template-columns: 1fr;
  }

  .header-inner {
    gap: 10px 16px;
  }

  .site-nav {
    order: 3;
    flex-basis: 100%;
    margin-left: 0;
    gap: 4px 16px;
    font-size: 0.84rem;
  }

  .header-inner .theme-switch {
    margin-left: auto;
  }

  .hero-meta span {
    font-size: 0.74rem;
  }

  .legend {
    gap: 8px 12px;
  }

  .footer-inner {
    flex-direction: column;
  }
}

@media (max-width: 480px) {
  .chart-card,
  .chart-side,
  .note-card,
  .demo-card {
    padding: 16px;
  }

  .story-step h3 {
    font-size: 1.2rem;
  }

  .step-figure strong {
    font-size: 2rem;
  }

  .center-layer strong {
    font-size: 1.55rem;
  }

  .center-layer span {
    font-size: 0.7rem;
  }
}

/* --------------------------------------------------------------------------
   13. 减少动态效果
   关闭入场动画、柱形生长动画和滚动切换动画；所有文字、数字保持可见。
   当 scroll-driven 动画被关闭后，中心回到 100% 总览，图例和数据表仍然完整。
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto !important;
  }

  .page,
  .switch-track,
  .switch-knob,
  .demo-card,
  .skip-link,
  .grayscale-switch,
  .donut-stage .donut,
  .donut-stage .swatch,
  .bar-fill {
    transition: none !important;
  }

  .reading-progress {
    display: none;
  }

  .reveal {
    animation: none !important;
    opacity: 1;
    transform: none;
  }

  .bar-fill {
    animation: none !important;
    opacity: 1;
    transform: none;
    filter: none !important;
  }

  .donut--story .dim-layer,
  .donut--story .focus-layer,
  .donut--story .center-layer {
    animation: none !important;
  }

  .donut--story .dim-layer,
  .donut--story .focus-layer,
  .donut--story .center-1,
  .donut--story .center-2,
  .donut--story .center-3,
  .donut--story .center-4 {
    opacity: 0;
  }

  .donut--story .center-default {
    opacity: 1;
  }
}
