/* ============================================================================
   tokens.css —「WEB 前端开发基础 · 期末复习」
   语义 token 的唯一实现来源。配套文档：UI-SPEC.md
   ----------------------------------------------------------------------------
   规则：
   · 组件只引用本文件的变量，禁止写死颜色 / 尺寸。
   · 深色模式只写差异项，不重复浅色已有的值。
   · 纯 CSS 变量 + 最小 reset，不含任何组件样式。
   ============================================================================ */

:root {
  /* ---------- 字体 ------------------------------------------------------ */
  --font-sans:
    system-ui, -apple-system, "Segoe UI",
    "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei UI", "Microsoft YaHei",
    "Noto Sans CJK SC", "Source Han Sans SC",
    "Helvetica Neue", Arial, sans-serif;
  --font-mono:
    "Cascadia Code", "Cascadia Mono", Consolas,
    "SF Mono", Menlo, "DejaVu Sans Mono",
    "Microsoft YaHei UI", monospace;

  /* ---------- 字号阶梯（rem，跟随浏览器字号设置）----------------------- */
  --text-xs: 0.75rem;    /* 12px 徽章 / 计时 / 行号 */
  --text-sm: 0.875rem;   /* 14px 按钮 / 标签 / 次要说明 */
  --text-base: 1rem;     /* 16px 正文基准 */
  --text-lg: 1.125rem;   /* 18px 题干 */
  --text-xl: 1.375rem;   /* 22px 卡片标题 */
  --text-2xl: 1.75rem;   /* 28px 区块标题 */
  --text-3xl: 2.125rem;  /* 34px 页面主标题 */

  /* ---------- 行高 ------------------------------------------------------ */
  --leading-tight: 1.2;
  --leading-snug: 1.4;
  --leading-normal: 1.55;
  --leading-relaxed: 1.7;

  /* ---------- 字距（与字号反相关）-------------------------------------- */
  --tracking-tight: -0.01em;
  --tracking-normal: 0;
  --tracking-wide: 0.01em;

  /* ---------- 字重（不用 700 以上）------------------------------------- */
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600;

  /* ---------- 间距（4px 基线）------------------------------------------ */
  --space-1: 0.25rem;   /*  4px */
  --space-2: 0.5rem;    /*  8px */
  --space-3: 0.75rem;   /* 12px */
  --space-4: 1rem;      /* 16px */
  --space-5: 1.25rem;   /* 20px */
  --space-6: 1.5rem;    /* 24px */
  --space-8: 2rem;      /* 32px */
  --space-10: 2.5rem;   /* 40px */
  --space-12: 3rem;     /* 48px */
  --space-16: 4rem;     /* 64px */
  --space-24: 6rem;     /* 96px */

  /* ---------- 圆角 ------------------------------------------------------ */
  --radius-xs: 2px;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 999px;

  /* ---------- 边框宽度 -------------------------------------------------- */
  --border-width: 1px;
  --border-width-strong: 2px;

  /* ---------- 层级 ------------------------------------------------------ */
  --z-base: 0;
  --z-raised: 10;
  --z-sticky: 100;
  --z-dropdown: 1000;
  --z-drawer: 1100;
  --z-modal: 1200;
  --z-popover: 1300;
  --z-toast: 1400;
  --z-tooltip: 1500;

  /* ---------- 动效时长 -------------------------------------------------- */
  --dur-1: 100ms;   /* 按下反馈 */
  --dur-2: 150ms;   /* 颜色 / 边框 */
  --dur-3: 200ms;   /* 小位移 / 透明度 / 折叠 */
  --dur-4: 300ms;   /* Modal / Drawer 进入 */
  --dur-5: 500ms;   /* 大面板 / 进度环 */
  --dur-exit: 140ms;/* 所有退出（≈ 进入 × 0.7）*/

  /* ---------- 缓动 ------------------------------------------------------ */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-in: cubic-bezier(0.64, 0, 0.78, 0);
  --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
  /* 临界阻尼弹簧（damping 1.0，response 0.3s / 0.4s）采样所得，配 --dur-4 / --dur-5 */
  --ease-spring: linear(
    0, 0.0974 6%, 0.2816 12%, 0.4656 18%, 0.6189 24%, 0.7361 29%,
    0.821 35%, 0.8806 41%, 0.9213 47%, 0.9487 53%, 0.9668 59%,
    0.9787 65%, 0.9864 71%, 0.9914 76%, 0.9945 82%, 0.9966 88%,
    0.9978 94%, 0.9987 100%
  );
  --ease-spring-soft: linear(
    0, 0.0596 5%, 0.186 10%, 0.3294 15%, 0.4656 20%, 0.584 25%,
    0.6819 30%, 0.7601 35%, 0.821 40%, 0.8677 45%, 0.9029 50%,
    0.9292 55%, 0.9487 60%, 0.963 65%, 0.9734 70%, 0.9809 75%,
    0.9864 80%, 0.9903 85%, 0.9931 90%, 0.9951 95%, 0.9966 100%
  );

  /* ---------- 布局 ------------------------------------------------------ */
  --layout-sidenav-w: 248px;
  --layout-sidenav-w-rail: 72px;
  --layout-sidenav-w-drawer: 280px;
  --layout-header-h: 56px;
  --layout-content-max: 1120px;
  --layout-prose-max: 640px;
  --layout-practice-analysis-w: 418px;
  --layout-actionbar-h: 56px;
  --layout-gutter: var(--space-8);
  /* JS 用（CSS 变量不能作为 @media 条件，媒体查询里请写死 768 / 1280）*/
  --bp-tablet: 768px;
  --bp-desktop: 1280px;

  /* ---------- 阴影（浅色：暖调；深色在下方覆盖）------------------------ */
  --shadow-1: 0 1px 2px rgb(38 36 31 / 0.06), 0 1px 1px rgb(38 36 31 / 0.04);
  --shadow-2: 0 2px 6px rgb(38 36 31 / 0.08), 0 1px 2px rgb(38 36 31 / 0.05);
  --shadow-3: 0 8px 20px -6px rgb(38 36 31 / 0.16), 0 2px 6px rgb(38 36 31 / 0.06);
  --shadow-4: 0 20px 44px -12px rgb(38 36 31 / 0.26), 0 6px 14px rgb(38 36 31 / 0.10);
  --shadow-focus: 0 0 0 2px var(--color-bg), 0 0 0 4px var(--color-focus-ring);
  --shadow-inset-highlight: inset 0 1px 0 rgb(255 255 255 / 0.60);

  /* ====================== 浅色语义色（默认）============================ */
  color-scheme: light;

  /* 底色与表面 */
  --color-bg: #F4F2EE;
  --color-bg-elevated: #FBFAF8;
  --color-bg-inset: #EDEAE4;
  --color-surface: #FFFFFF;
  --color-surface-hover: #FAF9F7;
  --color-surface-active: #EFECE6;
  --color-scrim: rgb(26 25 23 / 0.36);

  /* 描边（border 为装饰；border-control 承担 3:1 识别职责）*/
  --color-border: #E3DFD8;
  --color-border-strong: #D2CDC4;
  --color-border-control: #8E877D;

  /* 文字（对比度：surface 17.57 / 7.16 / 5.43）*/
  --color-text-primary: #1A1917;
  --color-text-secondary: #5C574F;
  --color-text-tertiary: #6F6961;
  --color-text-disabled: #A8A29A;

  /* 强调（实心用 accent + accent-fg；文字用 accent-text）*/
  --color-accent: #37548F;
  --color-accent-hover: #2C4478;
  --color-accent-active: #233763;
  --color-accent-fg: #FFFFFF;
  --color-accent-text: #2F4C86;
  --color-accent-subtle: #E9EDF6;
  --color-accent-subtle-hover: #DFE6F3;

  /* 语义状态 */
  --color-success: #256B45;
  --color-success-subtle: #E8F1EB;
  --color-warning: #8A5A00;
  --color-warning-subtle: #FAF0DC;
  --color-danger: #A82E26;
  --color-danger-solid: #A82E26;
  --color-danger-fg: #FFFFFF;
  --color-danger-subtle: #FBEBE9;
  --color-info: #1F6B8C;
  --color-info-subtle: #E7F0F4;

  /* 焦点 / 代码 / 浮层 */
  --color-focus-ring: #2F5C9E;
  --color-code-bg: #F7F5F1;
  --color-code-gutter: #EFECE6;
  --color-code-border: #E3DFD8;
  --color-code-text: #26241F;
  --color-code-comment: #686C63;
  --color-code-keyword: #8A3E8C;
  --color-code-string: #2C6E49;
  --color-code-number: #9A5313;
  --color-code-func: #2F4C86;
  --color-skeleton: #EFECE6;
  --color-tooltip-bg: #26241F;
  --color-tooltip-fg: #F3F1EC;

  /* 学习统计热力图（连续量，单向明度梯）*/
  --heat-0: #EDEAE4;
  --heat-1: #D9E1F0;
  --heat-2: #B8C9E6;
  --heat-3: #8CA5D3;
  --heat-4: #37548F;
}

/* ============================================================================
   深色主题 —— 只写差异项
   ============================================================================ */

[data-theme="dark"] {
  color-scheme: dark;

  /* 底色与表面：层级靠"更亮"，不靠更暗 */
  --color-bg: #131211;
  --color-bg-elevated: #1B1A18;
  --color-bg-inset: #0E0D0C;
  --color-surface: #232220;
  --color-surface-hover: #2B2A27;
  --color-surface-active: #33312D;
  --color-scrim: rgb(0 0 0 / 0.62);

  /* 描边 */
  --color-border: #34322E;
  --color-border-strong: #46433D;
  --color-border-control: #7A756C;

  /* 文字（surface 14.08 / 7.97 / 4.90）*/
  --color-text-primary: #F3F1EC;
  --color-text-secondary: #BCB7AE;
  --color-text-tertiary: #948E85;
  --color-text-disabled: #5E5A54;

  /* 强调：hover 提亮，active 压暗以保住白字 ≥4.5:1 */
  --color-accent: #4B6CA8;
  --color-accent-hover: #5474AC;
  --color-accent-active: #44639A;
  --color-accent-fg: #FFFFFF;
  --color-accent-text: #9DB4DE;
  --color-accent-subtle: #1E2634;
  --color-accent-subtle-hover: #263048;

  /* 语义状态 */
  --color-success: #7FC29B;
  --color-success-subtle: #17251C;
  --color-warning: #E8B45E;
  --color-warning-subtle: #2A2318;
  --color-danger: #F08A82;
  --color-danger-solid: #C4443B;
  --color-danger-fg: #FFFFFF;
  --color-danger-subtle: #2E1D1C;
  --color-info: #7EC0D8;
  --color-info-subtle: #17262B;

  /* 焦点 / 代码 / 浮层 */
  --color-focus-ring: #7FA6E0;
  --color-code-bg: #101312;
  --color-code-gutter: #171A19;
  --color-code-border: #2C302E;
  --color-code-text: #E8E5DF;
  --color-code-comment: #8B9089;
  --color-code-keyword: #C79BE0;
  --color-code-string: #8FD3A9;
  --color-code-number: #E0B879;
  --color-code-func: #9DB4DE;
  --color-skeleton: #2A2927;
  --color-tooltip-bg: #F3F1EC;
  --color-tooltip-fg: #1A1917;

  /* 热力图 */
  --heat-0: #1C1B19;
  --heat-1: #242E42;
  --heat-2: #33456A;
  --heat-3: #4B6CA8;
  --heat-4: #8FA9DA;

  /* 阴影：深色下近乎纯黑 + 顶部内亮边 */
  --shadow-1: 0 1px 2px rgb(0 0 0 / 0.50);
  --shadow-2: 0 2px 8px rgb(0 0 0 / 0.55);
  --shadow-3: 0 10px 24px -8px rgb(0 0 0 / 0.70);
  --shadow-4: 0 24px 48px -16px rgb(0 0 0 / 0.80);
  --shadow-inset-highlight: inset 0 1px 0 rgb(255 255 255 / 0.06);
}

/* ============================================================================
   最小公共基础（reset + 可访问性辅助）—— 不含任何组件样式
   ============================================================================ */

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

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

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--color-bg);
  color: var(--color-text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: var(--weight-regular);
  line-height: var(--leading-relaxed);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd {
  margin: 0;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: var(--weight-semibold);
  line-height: var(--leading-tight);
  letter-spacing: var(--tracking-tight);
  text-wrap: balance;
}

ul, ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

img, svg, video {
  display: block;
  max-width: 100%;
}

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

button {
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

code, pre, kbd, samp {
  font-family: var(--font-mono);
  font-variant-ligatures: none; /* 连字会让 `=>` 变成单字符，学生数不清 */
}

pre {
  margin: 0;
  white-space: pre;
}

::selection {
  background-color: var(--color-accent-subtle);
  color: var(--color-text-primary);
}

/* 焦点环：唯一写法，禁止 outline: none 而无替代 */
:focus-visible {
  outline: var(--border-width-strong) solid var(--color-focus-ring);
  outline-offset: 2px;
}

/* 锚点跳转不被粘性头部遮住 */
:target {
  scroll-margin-top: calc(var(--layout-header-h) + var(--space-2));
}

/* 仅供读屏 */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* 手动切换主题时的 200ms 颜色过渡（JS 加 class，220ms 后摘掉）*/
.theme-transition,
.theme-transition *,
.theme-transition *::before,
.theme-transition *::after {
  transition:
    background-color var(--dur-3) var(--ease-out),
    border-color var(--dur-3) var(--ease-out),
    color var(--dur-3) var(--ease-out),
    fill var(--dur-3) var(--ease-out) !important;
}

/* ---------- 响应式 token 覆盖（只改值，不写布局）----------------------- */
@media (max-width: 1279px) {
  :root {
    --layout-gutter: var(--space-6);
  }
}

@media (max-width: 767px) {
  :root {
    --layout-header-h: 52px;
    --layout-gutter: var(--space-4);
  }
}

/* ---------- 用户辅助偏好 ---------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --color-border: #C4BEB4;
    --color-text-tertiary: var(--color-text-secondary);
  }
  [data-theme="dark"] {
    --color-border: #5A564E;
  }
}

@media (prefers-reduced-transparency: reduce) {
  :root {
    --color-scrim: rgb(26 25 23 / 0.72);
  }
  [data-theme="dark"] {
    --color-scrim: rgb(0 0 0 / 0.86);
  }
}
