:root {
  color-scheme: light;
  /* 与 analyze.py 的 BG 保持一致：淡灰色底 */
  --bg: #e8e8e8;
  --fg: #1d2024;
  --muted: #6b7280;
  --line: #cfd3d8;
  --accent: #22c55e;
  --accent-ink: #15803d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.5 ui-sans-serif, system-ui, "Segoe UI", sans-serif;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---------------- 摄像头预览 ---------------- */
/* 默认在视口外。标定时移到右下角；测试时留在原位继续解码，但不显示 */
#cam {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 200px;
  height: auto;
  z-index: 15;
}
body.calibrating #cam {
  left: auto;
  right: 16px;
  bottom: 16px;
  top: auto;
  border-radius: 10px;
  outline: 1px solid var(--line);
  opacity: 0.9;
}

/* ---------------- 开始面板 ---------------- */
#panel {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  text-align: center;
  padding: 24px;
  background: var(--bg);
}
#panel h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: 0.2px; }
#panel .sub { margin: 0; color: var(--muted); font-size: 13px; }
.hint { max-width: 520px; color: var(--muted); font-size: 12px; line-height: 1.7; }

/* 状态条：独立于面板，标定/出错时仍然可见 */
#status {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  margin: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.88);
  outline: 1px solid var(--line);
  color: var(--accent-ink);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  white-space: nowrap;
}
body.testing #status { opacity: 0; }
body.testing #status.warn { opacity: 1; color: #b91c1c; }

/* ---------------- 标定靶 ---------------- */
#target {
  position: fixed;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 8px rgba(34, 197, 94, 0.22);
  display: grid;
  place-items: center;
  transition: left 90ms linear, top 90ms linear, opacity 200ms ease, transform 200ms ease;
  z-index: 30;
}
#target.out { opacity: 0; transform: translate(-50%, -50%) scale(1.8); }
#target-label { font-size: 11px; font-weight: 700; color: #06280f; }

/* ---------------- 注视点（测试中始终显示） ---------------- */
#dot {
  position: fixed;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.28), 0 0 16px rgba(239, 68, 68, 0.7);
  transform: translate(-50%, -50%);
  transition: left 70ms linear, top 70ms linear;
  z-index: 28;
  pointer-events: none;
}

/* 交叉准星，和绿色鼠标圈区分开 */
#dot::before,
#dot::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  background: #fff;
  opacity: 0.9;
}
#dot::before { width: 8px; height: 1.5px; transform: translate(-50%, -50%); }
#dot::after { width: 1.5px; height: 8px; transform: translate(-50%, -50%); }

/* ---------------- 测试舞台 ---------------- */
#stage {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  overflow: hidden;
  cursor: none;
  user-select: none;
}

/* 等比例缩放到「完整可见」，不裁剪、不拉伸 —— 与 analyze.py 的 contain_rect 一致 */
#stimulus {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* 绿色圆圈光标 */
#cursor {
  position: fixed;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2.5px solid var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18), 0 0 14px rgba(34, 197, 94, 0.5);
  transform: translate(-50%, -50%);
  z-index: 26;
  pointer-events: none;
}

#hud {
  position: fixed;
  left: 16px;
  top: 16px;
  z-index: 24;
  display: grid;
  gap: 8px;
  justify-items: start;
  pointer-events: none;
}
#hud button { pointer-events: auto; cursor: pointer; }
#stats {
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.72);
  outline: 1px solid var(--line);
  font: 11px/1.6 ui-monospace, "Cascadia Mono", monospace;
  color: #4b5563;
  min-width: 190px;
}

#end {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 30;
  cursor: pointer;
}

/* ---------------- 按钮 ---------------- */
button {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--fg);
  padding: 10px 20px;
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
button:hover:not(:disabled) { background: #f6f7f8; border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.danger { border-color: #f0a3a3; color: #b91c1c; }
button.danger:hover:not(:disabled) { background: #fff5f5; border-color: #dc2626; }

button.ghost {
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.72);
  color: #4b5563;
}
button.ghost:hover:not(:disabled) { background: #fff; }

/* ---------------- 标定收尾过渡 ---------------- */
#transition {
  position: fixed;
  inset: 0;
  z-index: 65;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  opacity: 1;
  transition: opacity 0.45s ease;
}
#transition.fade { opacity: 0; }
.tr-inner { display: grid; gap: 12px; justify-items: center; text-align: center; max-width: 520px; }
.tr-title { margin: 0; font-size: 20px; font-weight: 650; }
.tr-sub { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.7; }
.tr-count {
  margin: 0;
  min-height: 84px;
  font-size: 68px;
  font-weight: 700;
  color: var(--accent-ink);
  font-variant-numeric: tabular-nums;
}
.tr-count.pop { animation: tr-pop 0.5s ease-out; }
@keyframes tr-pop {
  from { transform: scale(0.55); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* 转圈用 gif：主线程被 calibrate() 占住时，CSS 动画会停，gif 由图片解码器驱动，通常还能动 */
.tr-gif {
  width: 48px;
  height: 48px;
  display: block;
}
.tr-gif.hidden { display: none !important; }

/* ---------------- 全屏闸门 ---------------- */
#gate {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(232, 232, 232, 0.97);
}
.gate-box { display: grid; gap: 12px; justify-items: center; max-width: 460px; text-align: center; }
.gate-box h2 { margin: 0; font-size: 18px; font-weight: 650; }
.gate-box p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.7; }

/* ---------------- 结果 ---------------- */
#result {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
#result-body {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 12px;
}
#heatmap {
  display: none;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
  outline: 1px solid var(--line);
}
#result.ready #heatmap { display: block; }
#result.ready #result-msg { display: none; }
#result-msg { margin: 0; color: var(--muted); }

#result-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: #fff;
}
.result-info { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.result-info strong { font-weight: 650; }
#result-stats {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-actions { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.link { color: var(--accent-ink); font-size: 12px; text-decoration: none; }
.link:hover { text-decoration: underline; }
