:root {
  --night: #1e2140;
  --night-2: #282c55;
  --night-3: #363b6e;
  --lamp: #ffc861;
  --lamp-hot: #ffe3a3;
  --ember: #c9541e;
  --paper: #f6e7c8;
  --muted: #aaa6c4;
  --jade: #5cc8a8;
  --danger: #ff8a7a;

  --font-display: "Mali", "Anuphan", system-ui, sans-serif;
  --font-ui: "Anuphan", system-ui, -apple-system, sans-serif;
  --font-zh: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;

  --gutter: clamp(16px, 2.4vw, 28px);
  --stage-radius: 26px;
}

* { box-sizing: border-box; }

html { color-scheme: dark; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(1200px 600px at 30% -10%, rgba(255, 200, 97, 0.08), transparent 60%),
    var(--night);
  color: var(--paper);
  font: 400 16px/1.6 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

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

:focus-visible {
  outline: 3px solid var(--jade);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

:lang(zh-CN) { font-family: var(--font-zh); }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: center;
  justify-content: space-between;
  padding: 18px var(--gutter) 6px;
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-mark { font-size: 26px; align-self: center; }
.brand-name { font: 700 30px/1 var(--font-display); color: var(--lamp); }
.brand-en { color: var(--muted); font-size: 14px; }

.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

.segmented {
  display: inline-flex;
  padding: 4px;
  border-radius: 999px;
  background: var(--night-2);
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 6px 16px;
  border-radius: 999px;
  cursor: pointer;
  color: var(--muted);
}
.segmented button[aria-pressed="true"] {
  background: var(--lamp);
  color: #000;
  font-weight: 600;
}

.ghost {
  border: 1px solid var(--night-3);
  background: transparent;
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
}
.ghost:hover { border-color: var(--muted); }
.ghost[aria-pressed="true"] { border-color: var(--jade); color: var(--jade); }
.ghost.danger { color: var(--danger); }

.voice-pick {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--night-3);
  border-radius: 999px;
  padding: 0 6px 0 12px;
}
.voice-pick:focus-within { border-color: var(--jade); }
.voice-pick select {
  border: 0;
  background: transparent;
  padding: 7px 4px;
  cursor: pointer;
}
.voice-pick select:focus-visible { outline: none; }
.voice-pick option { background: var(--night-2); }

.primary {
  border: 0;
  background: var(--lamp);
  color: #000;
  font-weight: 600;
  padding: 12px 28px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 17px;
}
.primary:hover:not(:disabled) { background: var(--lamp-hot); }
.primary:disabled { opacity: 0.55; cursor: progress; }
.primary.wide { width: 100%; }

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: var(--gutter);
  padding: 14px var(--gutter) var(--gutter);
  align-items: start;
}

/* ---------- Stage: the lit shadow screen ---------- */

.stage {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--stage-radius);
  overflow: hidden;
  background: #000;
  box-shadow:
    0 0 0 10px var(--night-2),
    0 0 0 11px var(--night-3),
    0 30px 80px -20px rgba(255, 200, 97, 0.25);
}

.stage video,
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.stage canvas { pointer-events: none; }
.stage video { transform: scaleX(-1); }
.stage[data-view="shadow"] video { visibility: hidden; }
/* Soft penumbra so the 3D animal's silhouette matches the blurred hand shadows. */
.stage[data-view="shadow"] .ar-layer { filter: drop-shadow(0 0 12px rgba(0, 0, 0, 0.45)); }

.intro {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  background: radial-gradient(ellipse at 50% 42%, var(--lamp-hot) 0%, var(--lamp) 34%, #e8913a 70%, var(--ember) 100%);
  color: #000;
}
.intro h1 {
  margin: 0;
  font: 700 clamp(30px, 5vw, 58px)/1.15 var(--font-display);
}
.intro p { margin: 0; max-width: 46ch; font-size: clamp(14px, 1.6vw, 17px); }
.intro .primary { background: #000; color: var(--lamp); margin-top: 6px; }
.intro .primary:hover:not(:disabled) { background: var(--night); }
.intro-note:empty { display: none; }
.intro-note { font-weight: 600; }

.result {
  position: absolute;
  left: 18px;
  bottom: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 22px 12px 14px;
  border-radius: 22px;
  background: rgba(30, 33, 64, 0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  max-width: calc(100% - 36px);
}
.result-emoji { font-size: clamp(44px, 6vw, 72px); line-height: 1; flex: none; }
.result-text { flex: 1; min-width: 0; }
.result-name { margin: 0; font: 700 clamp(28px, 4vw, 48px)/1.1 var(--font-display); color: var(--lamp); }
.result-zh { margin: 4px 0 0; display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 10px; }
.result-zh [lang] { font-size: clamp(22px, 3vw, 34px); font-weight: 600; color: var(--paper); line-height: 1.1; white-space: nowrap; }
.pinyin { font-size: clamp(14px, 1.6vw, 18px); color: var(--lamp-hot); }
.result-sub { margin: 4px 0 6px; color: var(--muted); font-size: 14px; display: flex; gap: 10px; }
.result-source { color: var(--jade); }

.replay {
  flex: none;
  align-self: flex-start;
  border: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--night-3);
  cursor: pointer;
  font-size: 18px;
}
.replay:hover { background: var(--jade); }
.result.is-near .result-name { color: var(--paper); font-size: clamp(20px, 2.6vw, 30px); }
.result.is-near .result-emoji { opacity: 0.55; }
.result.is-idle .result-name { color: var(--muted); font-size: clamp(18px, 2.2vw, 24px); font-weight: 600; }

.meter { width: 100%; max-width: 160px; height: 6px; border-radius: 3px; background: var(--night-3); overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: var(--jade); border-radius: 3px; transition: width 120ms linear; }

.result.pop .result-emoji { animation: pop 380ms cubic-bezier(0.2, 1.6, 0.4, 1); }
@keyframes pop {
  from { transform: scale(0.6) rotate(-12deg); }
  to { transform: scale(1) rotate(0); }
}

.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font: 700 clamp(48px, 12vw, 140px)/1 var(--font-display);
  color: var(--lamp);
  text-shadow: 0 6px 30px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}
.countdown.is-text { font-size: clamp(28px, 5vw, 56px); }

.fps {
  position: absolute;
  top: 12px;
  right: 14px;
  font-size: 12px;
  color: var(--muted);
  background: rgba(30, 33, 64, 0.6);
  padding: 2px 8px;
  border-radius: 8px;
}

/* ---------- Finger readout ---------- */

.readout {
  margin-top: 26px;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  min-height: 74px;
}
.readout-empty { margin: 0; color: var(--muted); align-self: center; }

.hand {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-radius: 16px;
  background: var(--night-2);
}
.hand-side { font-weight: 600; min-width: 4.5em; }
.fingers { display: flex; gap: 8px; }
.finger { display: grid; justify-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.finger i {
  width: 10px;
  height: 30px;
  border-radius: 5px;
  background: linear-gradient(to top, var(--lamp) calc(var(--v) * 100%), var(--night-3) 0);
}
.hand-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { font-size: 13px; padding: 2px 10px; border-radius: 999px; background: var(--night-3); }

/* ---------- Side panel ---------- */

.panel {
  background: var(--night-2);
  border-radius: 22px;
  padding: 8px;
  max-height: calc(100vh - 110px);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 16px;
}

.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; }
.tabs [role="tab"] {
  border: 0;
  background: transparent;
  padding: 10px;
  border-radius: 14px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
}
.tabs [role="tab"][aria-selected="true"] { background: var(--night-3); color: var(--paper); }

.pane { overflow-y: auto; padding: 10px 12px 14px; }

.guide-group { margin: 14px 4px 6px; font-size: 14px; color: var(--muted); font-weight: 500; }
.guide-group:first-child { margin-top: 4px; }

.guide-list { list-style: none; margin: 0; padding: 0; }
.guide-item {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  gap: 12px;
  align-items: start;
  padding: 10px 8px;
  border-radius: 14px;
}
.guide-item + .guide-item { border-top: 1px solid rgba(170, 166, 196, 0.12); }
.guide-item.is-active { background: rgba(255, 200, 97, 0.14); border-top-color: transparent; }
.guide-item.is-active + .guide-item { border-top-color: transparent; }
.guide-emoji { font-size: 34px; line-height: 1.1; text-align: center; transition: transform 200ms; }
.guide-item.is-active .guide-emoji { transform: scale(1.18); }
.guide-name { margin: 0; font: 600 18px/1.3 var(--font-display); }
.guide-langs { margin: 0; display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 8px; font-size: 15px; }
.guide-langs [lang="en"] { font-weight: 500; }
.guide-langs .pinyin { font-size: 13px; color: var(--lamp-hot); }
.guide-say {
  border: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  font-size: 16px;
  opacity: 0.7;
}
.guide-say:hover { background: var(--night-3); opacity: 1; }
.guide-hint { margin: 2px 0 0; font-size: 14px; line-height: 1.5; color: #d6cfe0; }

.pane-lead { margin: 4px 0 16px; font-size: 15px; color: #d6cfe0; }
.pane-lead a { color: var(--jade); }
.pane-heading { font: 600 16px var(--font-ui); margin: 24px 0 8px; }

.field { display: grid; gap: 6px; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
.field select,
.field input {
  background: var(--night);
  border: 1px solid var(--night-3);
  border-radius: 12px;
  padding: 10px 12px;
  color: var(--paper);
  font-size: 16px;
}
.custom-fields { display: grid; grid-template-columns: 84px 1fr; gap: 0 10px; }
.custom-names { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.custom-hint { grid-column: 1 / -1; margin: 0 0 14px; }

.hint { font-size: 14px; color: var(--muted); margin: 10px 0 0; }
.hint.is-ok { color: var(--jade); }
.hint.is-warn { color: var(--danger); }

.trained-list { list-style: none; margin: 0; padding: 0; }
.trained-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-top: 1px solid rgba(170, 166, 196, 0.12);
}
.trained-list li:first-child { border-top: 0; }
.trained-list .t-emoji { font-size: 24px; }
.trained-list .t-name { flex: 1; }
.trained-list .t-count { color: var(--muted); font-size: 14px; }
.trained-list .t-delete { border: 0; background: transparent; color: var(--danger); cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.trained-empty { color: var(--muted); font-size: 14px; }

.check { display: flex; gap: 10px; align-items: start; margin: 20px 0 14px; font-size: 14px; cursor: pointer; }
.check input { margin-top: 4px; accent-color: var(--jade); }

.file-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.file-actions .ghost { font-size: 14px; }

/* ---------- Small screens ---------- */

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .panel { position: static; max-height: none; }
  .stage { aspect-ratio: 4 / 3; --stage-radius: 18px; }
}

@media (max-width: 520px) {
  .brand-en { display: none; }
  .stage { aspect-ratio: 3 / 4; }
  .result { left: 10px; bottom: 10px; right: 10px; gap: 10px; padding: 10px 10px 10px 10px; }
  .hand { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  .result.pop .result-emoji { animation: none; }
  .guide-emoji, .meter i { transition: none; }
}
