:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev2: #21262d;
  --fg: #c9d1d9;
  --fg-dim: #8b949e;
  --border: #30363d;
  --accent: #58a6ff;
  --heat-0: #2d333b;       /* 与 --bg-elev (#161b22) 明显区分，可见 */
  --heat-1: #0e4429;
  --heat-2: #006d32;
  --heat-3: #26a641;
  --heat-4: #39d353;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --bg-elev: #f6f8fa;
    --bg-elev2: #eef1f4;
    --fg: #1f2328;
    --fg-dim: #656d76;
    --border: #d0d7de;
    --accent: #0969da;
    --heat-0: #ebedf0;
    --heat-1: #9be9a8;
    --heat-2: #40c463;
    --heat-3: #30a14e;
    --heat-4: #216e39;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}
header {
  padding: 24px 32px 8px;
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; }
.subtitle { margin: 4px 0 0; color: var(--fg-dim); font-size: 13px; }

main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px 32px 64px;
}
section { margin-bottom: 32px; }
h2 { margin: 0 0 12px; text-transform: capitalize; }

.section-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px;
}
.controls { color: var(--fg-dim); font-size: 13px; }
.controls select {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
}
.metric { font-size: 28px; font-weight: 600; font-variant-numeric: tabular-nums; }
.label { font-size: 14px; color: var(--fg-dim); margin-top: 4px; }

/* Heatmap */
.heatmap-wrap {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  overflow-x: auto;
}
.heatmap-wrap svg { display: block; }
.cell { stroke: rgba(0,0,0,0.25); stroke-width: 0.5; }
.cell:hover { stroke: var(--accent); stroke-width: 1.5; }
.cell.future { opacity: 0.55; pointer-events: none; }
.cell.today { stroke: var(--accent); stroke-width: 2; }
.month-label, .day-label { fill: var(--fg-dim); font-size: 12px; }

.legend {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--fg-dim);
  margin-top: 8px; padding-left: 16px;
}
.legend .swatch {
  width: 12px; height: 12px; border-radius: 2px;
  display: inline-block;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  /* 去掉 overflow: hidden；它会限制 sticky thead 的生效范围 */
  font-variant-numeric: tabular-nums;
}
thead { background: var(--bg-elev2); }
th, td {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
th {
  font-weight: 600;
  color: var(--fg-dim);
  /* 表头钉在视口顶部；sticky 限在 table 内部生效，表格滑出视口后表头也跟着走 */
  position: sticky;
  top: -1px;             /* 多上提 1px，遮住 sub-pixel 渲染造成的顶部缝隙 */
  z-index: 5;
  background: var(--bg-elev2);
  /* sticky 状态下 border-bottom 不会随元素一起钉住，用 box-shadow 代替下边线 */
  box-shadow: inset 0 -1px 0 var(--border);
}
td.num { text-align: right; }
tr:last-child td { border-bottom: none; }

/* 聚合表分组 / 小计 / 总计样式 */
.group-cell {
  vertical-align: top;
  font-weight: 600;
  background: var(--bg-elev2);
  border-right: 1px solid var(--border);
  width: 1%;             /* 限缩到刚好包住内容 */
  white-space: nowrap;
}
tr.subtotal td {
  background: rgba(88, 166, 255, 0.06);
  font-weight: 600;
}
tr.subtotal .subtotal-label {
  color: var(--fg-dim);
  font-style: italic;
}
tr.grand-total td {
  background: var(--bg-elev2);
  font-weight: 700;
  border-top: 2px solid var(--border);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}
@media (max-width: 800px) {
  .grid-2 { grid-template-columns: 1fr; }
}

footer {
  position: sticky; bottom: 0; z-index: 10;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 8px 32px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--fg-dim);
}
footer #meta {
  justify-self: start;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
footer #theme-toggle { justify-self: center; }
footer #refresh { justify-self: end; }
button {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--fg);
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); color: var(--accent); }

/* Tooltip */
.tooltip {
  position: fixed;
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  pointer-events: none;
  white-space: pre-line;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  z-index: 100;
}

/* ===== Cyberpunk theme (opt-in via <html data-theme="cyberpunk">) ===== */
:root[data-theme="cyberpunk"] {
  color-scheme: dark;
  --bg: #07000f;
  --bg-elev: #0e0420;
  --bg-elev2: #160a30;
  --fg: #d1f7ff;
  --fg-dim: #8a7fb8;
  --border: #3a1f6e;
  --accent: #05d9e8;
  --neon-pink: #ff2a6d;
  --neon-cyan: #05d9e8;
  --neon-yellow: #f9f871;
  --heat-0: #1a0b3a;
  --heat-1: #2a1170;
  --heat-2: #6e1a8f;
  --heat-3: #d11385;
  --heat-4: #ff2a6d;
}
:root[data-theme="cyberpunk"] body {
  font-family: "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
  background:
    radial-gradient(ellipse at top left, rgba(255, 42, 109, 0.10), transparent 55%),
    radial-gradient(ellipse at bottom right, rgba(5, 217, 232, 0.10), transparent 55%),
    url("/bg-cyberpunk.svg") repeat,
    var(--bg);
}
:root[data-theme="cyberpunk"] header {
  border-bottom-color: var(--neon-pink);
  box-shadow: 0 0 24px rgba(255, 42, 109, 0.25);
}
:root[data-theme="cyberpunk"] header h1 {
  color: var(--neon-pink);
  text-shadow: 0 0 8px rgba(255, 42, 109, 0.75), 0 0 22px rgba(255, 42, 109, 0.4);
  letter-spacing: 2px;
}
:root[data-theme="cyberpunk"] .subtitle {
  color: var(--neon-cyan);
  text-shadow: 0 0 6px rgba(5, 217, 232, 0.4);
}
:root[data-theme="cyberpunk"] h2 {
  color: var(--neon-cyan);
  text-shadow: 0 0 6px rgba(5, 217, 232, 0.5);
  letter-spacing: 1px;
}
:root[data-theme="cyberpunk"] .card {
  border-color: var(--neon-cyan);
  box-shadow:
    0 0 0 1px var(--neon-cyan),
    0 0 12px rgba(5, 217, 232, 0.55),
    0 0 28px rgba(5, 217, 232, 0.28),
    inset 0 0 14px rgba(5, 217, 232, 0.08);
}
:root[data-theme="cyberpunk"] .metric {
  color: var(--neon-yellow);
  text-shadow: 0 0 6px rgba(249, 248, 113, 0.55);
}
:root[data-theme="cyberpunk"] .heatmap-wrap,
:root[data-theme="cyberpunk"] table {
  border-color: var(--neon-pink);
  box-shadow:
    0 0 0 1px var(--neon-pink),
    0 0 14px rgba(255, 42, 109, 0.55),
    0 0 32px rgba(255, 42, 109, 0.28),
    inset 0 0 14px rgba(255, 42, 109, 0.06);
}
:root[data-theme="cyberpunk"] thead,
:root[data-theme="cyberpunk"] th,
:root[data-theme="cyberpunk"] tr.grand-total td,
:root[data-theme="cyberpunk"] .group-cell {
  background: var(--bg-elev2);
}
:root[data-theme="cyberpunk"] th {
  color: var(--neon-cyan);
  text-transform: uppercase;
  letter-spacing: 1px;
  box-shadow: inset 0 -1px 0 var(--neon-pink);
}
:root[data-theme="cyberpunk"] tr.subtotal td {
  background: rgba(5, 217, 232, 0.06);
}
:root[data-theme="cyberpunk"] button,
:root[data-theme="cyberpunk"] .controls select {
  border-color: var(--neon-pink);
  color: var(--neon-pink);
  background: transparent;
  text-transform: uppercase;
  letter-spacing: 1px;
}
:root[data-theme="cyberpunk"] button:hover {
  background: var(--neon-pink);
  color: #07000f;
  box-shadow: 0 0 14px var(--neon-pink);
}
:root[data-theme="cyberpunk"] .cell:hover {
  stroke: var(--neon-yellow);
  stroke-width: 1.5;
  filter: drop-shadow(0 0 4px var(--neon-yellow));
}
:root[data-theme="cyberpunk"] .cell.today {
  stroke: var(--neon-yellow);
  filter: drop-shadow(0 0 6px var(--neon-yellow));
}
:root[data-theme="cyberpunk"] footer {
  border-top-color: var(--neon-cyan);
  box-shadow: 0 0 18px rgba(5, 217, 232, 0.2);
}

/* Cyberpunk - 表格内部线条点亮 + 行 hover 霓虹扫光 */
:root[data-theme="cyberpunk"] td,
:root[data-theme="cyberpunk"] th {
  border-bottom-color: rgba(5, 217, 232, 0.22);
}
:root[data-theme="cyberpunk"] .group-cell {
  border-right-color: rgba(5, 217, 232, 0.35);
}
:root[data-theme="cyberpunk"] tr.grand-total td {
  border-top-color: var(--neon-pink);
  box-shadow: inset 0 1px 0 var(--neon-pink), inset 0 6px 16px rgba(255, 42, 109, 0.18);
}
:root[data-theme="cyberpunk"] tbody tr:hover td {
  background: rgba(255, 42, 109, 0.10);
  color: #fff;
  text-shadow: 0 0 6px rgba(255, 42, 109, 0.6);
}
:root[data-theme="cyberpunk"] tbody tr:hover .group-cell {
  background: rgba(5, 217, 232, 0.10);
  color: var(--neon-cyan);
  text-shadow: 0 0 6px rgba(5, 217, 232, 0.6);
}
/* footer 边框真发光 */
:root[data-theme="cyberpunk"] footer {
  box-shadow:
    0 -1px 0 var(--neon-cyan),
    0 -8px 24px rgba(5, 217, 232, 0.4),
    0 -20px 40px rgba(5, 217, 232, 0.18);
}

/* ===== Cyberpunk 氛围层：扫描线 / 故障 / 霓虹粒子 ===== */
:root[data-theme="cyberpunk"] body { position: relative; }

/* 扫描线 + CRT 暗角 + 缓慢下移的亮带 */
:root[data-theme="cyberpunk"] body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 9998;
  pointer-events: none;
  background:
    /* 移动的扫描亮带 */
    linear-gradient(to bottom, transparent 0%, rgba(5, 217, 232, 0.14) 45%, rgba(5, 217, 232, 0.20) 50%, rgba(5, 217, 232, 0.14) 55%, transparent 100%) 0 0 / 100% 180px no-repeat,
    /* 密集扫描线：暗线 + 青色微高光交替 */
    repeating-linear-gradient(to bottom,
      rgba(5, 217, 232, 0.05) 0px,
      rgba(0, 0, 0, 0) 1px,
      rgba(0, 0, 0, 0) 2px,
      rgba(0, 0, 0, 0.45) 3px,
      rgba(0, 0, 0, 0.45) 4px),
    /* CRT 暗角 */
    radial-gradient(ellipse at center, transparent 55%, rgba(7, 0, 15, 0.55) 100%);
  animation: cp-scan 15s linear infinite, cp-flicker 5s steps(2, end) infinite;
}
@keyframes cp-scan {
  /* 前 2/3（约 10s）以原速扫过一趟，后 1/3（约 5s）停在屏幕外作为间隔 */
  0%        { background-position: 0 -240px, 0 0, 0 0; }
  66.67%    { background-position: 0 100vh, 0 0, 0 0; }
  100%      { background-position: 0 100vh, 0 0, 0 0; }
}
@keyframes cp-flicker {
  0%, 96%, 100% { opacity: 1; }
  97%           { opacity: 0.82; }
  98%           { opacity: 0.96; }
  99%           { opacity: 0.78; }
}

/* 霓虹粒子：多组 radial-gradient 光点缓慢上浮漂移 */
:root[data-theme="cyberpunk"] body::after {
  content: "";
  position: fixed;
  inset: -10%;
  z-index: 9997;
  pointer-events: none;
  background-repeat: repeat;
  background-size: 420px 520px;
  background-image:
    radial-gradient(1.6px 1.6px at 40px 60px, rgba(5, 217, 232, 0.9), transparent),
    radial-gradient(1.4px 1.4px at 180px 200px, rgba(255, 42, 109, 0.85), transparent),
    radial-gradient(1.2px 1.2px at 320px 120px, rgba(249, 248, 113, 0.8), transparent),
    radial-gradient(2px 2px at 250px 420px, rgba(5, 217, 232, 0.75), transparent),
    radial-gradient(1.3px 1.3px at 90px 360px, rgba(255, 42, 109, 0.7), transparent),
    radial-gradient(1.5px 1.5px at 380px 300px, rgba(5, 217, 232, 0.7), transparent);
  filter: drop-shadow(0 0 4px rgba(5, 217, 232, 0.6));
  animation: cp-drift 24s linear infinite;
}
@keyframes cp-drift {
  0%   { background-position: 0 0, 0 0, 0 0, 0 0, 0 0, 0 0; }
  100% { background-position: 0 -520px, 30px -520px, -20px -520px, 0 -520px, 15px -520px, -10px -520px; }
}

/* 标题故障效果：RGB 错位 + 偶发抖动裁切 */
:root[data-theme="cyberpunk"] header h1 {
  position: relative;
  animation: cp-glitch-skew 4s infinite steps(1, end);
}
:root[data-theme="cyberpunk"] header h1::before,
:root[data-theme="cyberpunk"] header h1::after {
  content: "🔥 pi-tokamak";
  position: absolute;
  inset: 0;
  background: var(--bg);
  overflow: hidden;
}
:root[data-theme="cyberpunk"] header h1::before {
  left: 2px;
  color: var(--neon-cyan);
  text-shadow: -2px 0 var(--neon-cyan);
  animation: cp-glitch-1 2.6s infinite linear alternate-reverse;
}
:root[data-theme="cyberpunk"] header h1::after {
  left: -2px;
  color: var(--neon-pink);
  text-shadow: 2px 0 var(--neon-pink);
  animation: cp-glitch-2 3.4s infinite linear alternate-reverse;
}
@keyframes cp-glitch-1 {
  0%, 100% { clip-path: inset(0 0 85% 0); }
  20%      { clip-path: inset(40% 0 35% 0); }
  40%      { clip-path: inset(70% 0 8% 0); }
  60%      { clip-path: inset(15% 0 60% 0); }
  80%      { clip-path: inset(55% 0 25% 0); }
}
@keyframes cp-glitch-2 {
  0%, 100% { clip-path: inset(60% 0 20% 0); }
  25%      { clip-path: inset(10% 0 75% 0); }
  50%      { clip-path: inset(80% 0 5% 0); }
  75%      { clip-path: inset(30% 0 50% 0); }
}
@keyframes cp-glitch-skew {
  0%, 92%, 100% { transform: skew(0deg); }
  93%           { transform: skew(-0.6deg); }
  95%           { transform: skew(0.8deg); }
  97%           { transform: skew(-0.3deg); }
}

/* 随机故障引擎：JS 动态生成的图层与色块 */
#glitch-layer {
  position: fixed;
  inset: 0;
  z-index: 9999;
  pointer-events: none;
  overflow: hidden;
}
.glitch-block {
  position: absolute;
  background: var(--c, #05d9e8);
  mix-blend-mode: screen;
  transform: translateX(var(--sx, 0));
  box-shadow: 0 0 10px var(--c, #05d9e8);
  animation: gb-flick 0.1s steps(2, end) infinite;
}
.glitch-block[data-stripe] {
  background: repeating-linear-gradient(90deg, var(--c) 0 4px, transparent 4px 9px);
  box-shadow: none;
}
@keyframes gb-flick {
  0%   { opacity: 0.8; clip-path: inset(0 0 0 0); }
  50%  { opacity: 0.3; clip-path: inset(0 35% 0 0); }
  100% { opacity: 0.7; clip-path: inset(0 0 0 25%); }
}
/* 满屏 RGB 抖动 */
:root[data-theme="cyberpunk"] body.glitch-rgb main,
:root[data-theme="cyberpunk"] body.glitch-rgb header {
  animation: gb-rgb 0.2s steps(2, end) 1;
}
@keyframes gb-rgb {
  0%   { transform: translate(0, 0); filter: none; }
  25%  { transform: translate(-3px, 1px); filter: drop-shadow(2px 0 var(--neon-pink)) drop-shadow(-2px 0 var(--neon-cyan)); }
  50%  { transform: translate(3px, -1px); filter: drop-shadow(-3px 0 var(--neon-cyan)) drop-shadow(3px 0 var(--neon-pink)); }
  75%  { transform: translate(-2px, 0); filter: drop-shadow(2px 0 var(--neon-pink)); }
  100% { transform: translate(0, 0); filter: none; }
}

/* 表头断电线 / 火星 / 发光带：JS 动态生成 */
#wire-layer {
  position: fixed;
  inset: 0;
  z-index: 9999;                     /* 压在扫描线/暗角之上，避免边缘被压暗 */
  pointer-events: none;
  overflow: hidden;
}
.wire {
  position: absolute;
  overflow: visible;
  opacity: 0;
  animation: wire-life var(--life, 3.5s) ease-in-out forwards;
}
@keyframes wire-life {
  0%   { opacity: 0; }
  12%  { opacity: 1; }
  82%  { opacity: 1; }
  100% { opacity: 0; }
}
/* 铜质金属线：底色阴影 / 中间本色 / 细高光，叠出金属管的立体光泽 */
.wire .cable-base {
  fill: none;
  stroke: #4a2a0f;
  stroke-width: 3.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.wire .cable-mid {
  fill: none;
  stroke: #b3702f;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.wire .cable-hi {
  fill: none;
  stroke: #f3c690;
  stroke-width: 0.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.85;
  filter: drop-shadow(0 0 2px rgba(255, 185, 115, 0.5));
}
.wire .pulse {                       /* 残余电流：蓝白发光带沿线流动 */
  fill: none;
  stroke: #d6ffff;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-dasharray: 10 800;
  filter: drop-shadow(0 0 5px #05d9e8);
  animation: wire-pulse 1.5s linear infinite;
}
@keyframes wire-pulse {
  from { stroke-dashoffset: 810; }
  to   { stroke-dashoffset: 0; }
}
.wire .spark {                       /* 断口火星：放射状闪烁短线 */
  stroke: var(--s, #f9f871);
  stroke-width: 1.4;
  stroke-linecap: round;
  filter: drop-shadow(0 0 4px var(--s, #f9f871));
  animation: wire-spark 0.16s steps(2, end) infinite;
}
@keyframes wire-spark {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.1; }
}
.wire .ember {                       /* 坠落的火花 */
  fill: var(--s, #f9f871);
  filter: drop-shadow(0 0 4px var(--s, #f9f871));
  transform-box: fill-box;
  animation: wire-ember var(--ed, 0.9s) ease-in infinite;
}
@keyframes wire-ember {
  0%   { opacity: 1; transform: translate(0, 0); }
  100% { opacity: 0; transform: translate(var(--dx, 4px), 20px); }
}

/* 尊重无障碍偏好 */
@media (prefers-reduced-motion: reduce) {
  :root[data-theme="cyberpunk"] body::before,
  :root[data-theme="cyberpunk"] body::after,
  :root[data-theme="cyberpunk"] header h1,
  :root[data-theme="cyberpunk"] header h1::before,
  :root[data-theme="cyberpunk"] header h1::after {
    animation: none;
  }
  #glitch-layer,
  #wire-layer { display: none; }
}
