:root {
  --bg: #0f1419;
  --card: #1a2332;
  --card2: #243044;
  --text: #e8eef7;
  --muted: #8b9bb4;
  --accent: #3b82f6;
  --accent2: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 15px; line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
body { min-height: 100dvh; padding-bottom: calc(16px + var(--safe-bottom)); }
.app { max-width: 520px; margin: 0 auto; padding: 12px 14px 24px; }
header.app-h {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-bottom: 12px;
}
header.app-h h1 { font-size: 1.15rem; margin: 0; font-weight: 700; flex: 1; }
.badge {
  font-size: 11px; padding: 3px 8px; border-radius: 999px;
  background: var(--card2); color: var(--muted);
}
.badge.ok { background: rgba(34,197,94,.18); color: #86efac; }
.badge.warn { background: rgba(245,158,11,.18); color: #fcd34d; }
.badge.err { background: rgba(239,68,68,.18); color: #fca5a5; }

.mode-tabs {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  margin-bottom: 14px;
}
.mode-tabs button {
  border: 1px solid transparent; background: var(--card); color: var(--muted);
  padding: 12px 10px; border-radius: var(--radius); font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.mode-tabs button.active {
  background: linear-gradient(135deg, #1e3a5f, #1d4ed8);
  color: #fff; border-color: #3b82f6;
}
.mode-panel { display: none; }
.mode-panel.active { display: block; }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 12px; margin-bottom: 12px;
  border: 1px solid rgba(255,255,255,.06);
}
.card h2 { margin: 0 0 8px; font-size: 0.95rem; font-weight: 650; }
.hint { color: var(--muted); font-size: 12px; margin: 0 0 10px; }

.video-wrap, .preview-wrap {
  position: relative; width: 100%;
  background: #000; border-radius: 12px; overflow: hidden;
  aspect-ratio: 3/4; max-height: 62vh;
}
.video-wrap video, .preview-wrap img, .video-wrap canvas.ar-overlay {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.video-wrap canvas.ar-overlay {
  position: absolute; left: 0; top: 0; pointer-events: none;
}
.preview-wrap { display: none; }
.preview-wrap.show { display: block; }
.preview-row {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.preview-row .cell { background: #000; border-radius: 10px; overflow: hidden; }
.preview-row img { width: 100%; display: block; aspect-ratio: 3/4; object-fit: cover; }
.preview-row .lbl { font-size: 11px; color: var(--muted); padding: 4px 6px; text-align: center; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
button.btn, label.btn {
  appearance: none; border: none; border-radius: 12px;
  padding: 12px 14px; font-size: 14px; font-weight: 650;
  cursor: pointer; color: #fff; background: var(--accent);
  flex: 1; min-width: 110px; text-align: center;
}
button.btn:disabled { opacity: .45; cursor: not-allowed; }
button.btn.secondary { background: var(--card2); color: var(--text); }
button.btn.success { background: var(--accent2); color: #052e16; }
button.btn.warn { background: var(--warn); color: #1c1400; }
button.btn.ghost { background: transparent; border: 1px solid var(--card2); color: var(--muted); }
label.btn input { display: none; }

.status-line {
  font-size: 13px; color: var(--muted); margin-top: 8px; min-height: 1.3em;
}
.status-line.err { color: #fca5a5; }
.status-line.ok { color: #86efac; }

.desc-box {
  white-space: pre-wrap; background: var(--card2); border-radius: 10px;
  padding: 12px; font-size: 14px; max-height: 280px; overflow: auto;
  margin-top: 8px;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.chip {
  border: 1px solid #3b82f6; background: rgba(59,130,246,.12);
  color: #93c5fd; border-radius: 999px; padding: 8px 12px;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.chip.active { background: #2563eb; color: #fff; }

.form-stub label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 4px; }
.form-stub input, .form-stub textarea, .form-stub select {
  width: 100%; background: #0f1419; border: 1px solid #334155;
  color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: 14px;
}
.form-stub textarea { min-height: 64px; resize: vertical; }
.demo-tag {
  display: inline-block; font-size: 11px; color: #fcd34d;
  background: rgba(245,158,11,.15); padding: 2px 8px; border-radius: 6px;
  margin-bottom: 8px;
}

.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: flex; gap: 10px; padding: 8px 0;
  border-bottom: 1px solid rgba(255,255,255,.06);
}
.history img {
  width: 56px; height: 74px; object-fit: cover; border-radius: 8px; background: #000;
}
.history .meta { flex: 1; min-width: 0; }
.history .meta time { font-size: 11px; color: var(--muted); }
.history .meta p {
  margin: 2px 0 0; font-size: 12px;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}

.ar-effects {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px;
}
.ar-effects button {
  background: var(--card2); border: 1px solid transparent; color: var(--text);
  border-radius: 12px; padding: 10px 6px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.ar-effects button.active { border-color: #22c55e; background: rgba(34,197,94,.15); }
@media (max-width: 420px) {
  .ar-effects { grid-template-columns: 1fr 1fr; }
}
.ar-note {
  font-size: 12px; color: var(--muted); margin-top: 10px;
  padding: 10px; background: rgba(245,158,11,.08); border-radius: 10px;
  border: 1px dashed rgba(245,158,11,.35);
}

footer.note {
  margin-top: 16px; font-size: 11px; color: var(--muted); text-align: center;
}

/* three modes */
.mode-tabs { grid-template-columns: 1fr 1fr 1fr 1fr; }
.mode-tabs button { font-size: 12px; padding: 10px 6px; }

.gaze-stage {
  position: relative; width: 100%;
  background: #0a1628; border-radius: 12px; overflow: hidden;
  aspect-ratio: 3/4; max-height: 62vh;
}
/* Hidden capture source — face is drawn mirrored on canvas */
.gaze-stage video {
  position: absolute; width: 1px; height: 1px; opacity: 0;
  pointer-events: none; z-index: 0;
}
.gaze-stage canvas#gazeCanvas {
  width: 100%; height: 100%; display: block; position: relative; z-index: 1;
}
/* Sequential 5-step calib: one jumping button */
.calib-overlay {
  position: absolute; inset: 0; z-index: 5;
  pointer-events: none;
}
.calib-overlay[hidden] { display: none !important; }
.calib-overlay button,
#calibBtn {
  position: absolute;
  width: clamp(52px, 14vw, 72px);
  height: clamp(52px, 14vw, 72px);
  border-radius: 50%;
  border: 3px solid #fbbf24;
  background: rgba(251,191,36,.55);
  color: #1c1400;
  font-weight: 800;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  transform: translate(-50%, -50%);
  pointer-events: auto;
  box-shadow: 0 0 0 4px rgba(251,191,36,.25), 0 8px 24px rgba(0,0,0,.35);
  padding: 0;
  animation: calib-pulse 1.2s ease-in-out infinite;
}
.calib-overlay button.done,
#calibBtn.done {
  background: rgba(34,197,94,.65);
  border-color: #22c55e;
  color: #052e16;
  animation: none;
  box-shadow: 0 0 0 4px rgba(34,197,94,.3);
}
.calib-overlay button:active,
#calibBtn:active { transform: translate(-50%, -50%) scale(0.92); }
@keyframes calib-pulse {
  0%, 100% { box-shadow: 0 0 0 4px rgba(251,191,36,.25), 0 8px 24px rgba(0,0,0,.35); }
  50% { box-shadow: 0 0 0 10px rgba(251,191,36,.12), 0 8px 24px rgba(0,0,0,.35); }
}

/* Game HUD over stage */
.gaze-game-hud {
  position: absolute; top: 8px; left: 8px; right: 8px; z-index: 6;
  display: flex; gap: 8px; pointer-events: none; flex-wrap: wrap;
}
.gaze-game-hud[hidden] { display: none !important; }
.gaze-score-pill {
  background: rgba(15,20,25,.72);
  border: 1px solid rgba(255,255,255,.12);
  color: #e8eef7;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
}
.gaze-score-pill strong { color: #86efac; margin-left: 4px; }
.gaze-score-pill.miss strong { color: #fca5a5; }
.calib-dots { display: none; }
button.btn.danger { background: var(--danger); color: #fff; }
a.btn { text-decoration:none; display:inline-block; }

/* Front camera selfie mirror (preview only; capture stays unmirrored) */
.video-wrap video.mirror { transform: scaleX(-1); }

/* Mode D bite — reuse gaze-stage layout */
#biteStage video {
  position: absolute; width: 1px; height: 1px; opacity: 0;
  pointer-events: none; z-index: 0;
}
#biteStage canvas#biteCanvas {
  width: 100%; height: 100%; display: block; position: relative; z-index: 1;
}
@media (max-width: 420px) {
  .mode-tabs button { font-size: 11px; padding: 8px 4px; }
}
