:root {
  --paper: #edeae3;      /* light setup background */
  --paper-2: #f6f4ef;
  --ink: #23282b;        /* dark text on light */
  --ink-soft: #23282b8c;
  --hair: #23282b26;     /* hairline on light */

  --cream: #f7f4ee;      /* text on camera */
  --cream-soft: #f7f4eeb3;
  --cream-faint: #f7f4ee59;
  --line-cam: #f7f4ee40; /* hairline on camera */
  --good: #b7e3c0;
  --shadow-cam: 0 1px 16px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Inter", -apple-system, "Segoe UI", Roboto, Helvetica, Arial, system-ui, sans-serif;
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

.setup, .drill { display: none; }
body[data-screen="setup"] .setup { display: flex; }
body[data-screen="drill"] .drill { display: block; }

/* mode gating: drill vs tutorial share the setup + camera screens */
body[data-mode="drill"] .tutorial-only { display: none !important; }
body[data-mode="tutorial"] .drill-only { display: none !important; }

/* small-caps tracked label helper */
.k, .control-k, .stat .k, .answer-hint, .live .k, .grp-place {
  font-size: 11px; font-weight: 500; letter-spacing: 0.18em; text-transform: uppercase;
}

/* ================= SETUP (light) ================= */
.setup { min-height: 100vh; align-items: center; justify-content: center; padding: 32px; }
.setup-inner { width: 100%; max-width: 400px; display: flex; flex-direction: column; align-items: center; }
.mark { color: var(--ink); }
.mark svg { width: 44px; height: 44px; display: block; }
.setup h1 { margin: 18px 0 4px; font-size: 26px; font-weight: 500; letter-spacing: -0.01em; }
.setup .sub { margin: 0 0 34px; color: var(--ink-soft); font-size: 14px; font-weight: 300; }

.control { width: 100%; margin-bottom: 24px; }
.control-k { display: block; color: var(--ink-soft); margin-bottom: 10px; }
.control-note { display: block; margin-top: 8px; font-size: 12px; font-weight: 300; color: var(--ink-soft); }

.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg-b {
  flex: 1 1 auto; min-width: fit-content; padding: 10px 16px;
  border: 1px solid var(--hair); background: transparent; color: var(--ink);
  border-radius: 999px; font-family: inherit; font-size: 14px; font-weight: 400;
  cursor: pointer; transition: background .18s, color .18s, border-color .18s, transform .08s;
}
#digitGrid .seg-b, #carryGrid .seg-b { flex: 1; text-align: center; }
.seg-b:hover { border-color: var(--ink-soft); }
.seg-b:active { transform: scale(.97); }
.seg-b.selected { background: var(--ink); color: var(--paper-2); border-color: var(--ink); }
.seg-b:disabled { opacity: .32; cursor: not-allowed; }
.seg-b:disabled:hover { border-color: var(--hair); }

.best-line {
  text-align: center; font-size: 13px; color: var(--ink-soft); margin: -4px 0 4px;
  font-variant-numeric: tabular-nums;
}

/* ---- lesson picker (tutorial mode, setup screen) ---- */
.lessons { display: flex; flex-direction: column; gap: 8px; }
.lesson {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 12px 16px; border: 1px solid var(--hair); background: transparent; color: var(--ink);
  border-radius: 16px; font-family: inherit; cursor: pointer;
  transition: background .18s, border-color .18s, transform .08s;
}
.lesson:hover:not(:disabled) { border-color: var(--ink-soft); }
.lesson:active:not(:disabled) { transform: scale(.99); }
.lesson.selected { background: var(--ink); color: var(--paper-2); border-color: var(--ink); }
.lesson:disabled { opacity: .38; cursor: not-allowed; }
.lesson-txt { display: flex; flex-direction: column; gap: 3px; flex: 1; min-width: 0; }
.lesson-t { font-size: 14px; font-weight: 400; }
.lesson-s { font-size: 12px; font-weight: 300; opacity: .68; }
.lesson-tick {
  flex: 0 0 auto; width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center;
  font-size: 11px; border: 1px solid currentColor; opacity: .3;
}
.lesson.done .lesson-tick { opacity: 1; background: currentColor; }
.lesson.done .lesson-tick::after { content: "\2713"; color: var(--paper-2); }
.lesson.selected.done .lesson-tick::after { color: var(--ink); }

.begin {
  margin-top: 12px; width: 100%; padding: 15px; border: none; border-radius: 999px;
  background: var(--ink); color: var(--paper-2); font-family: inherit; font-size: 15px; font-weight: 500;
  letter-spacing: .01em; cursor: pointer; transition: transform .08s, opacity .18s;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.begin span { transition: transform .2s; }
.begin:hover span { transform: translateX(4px); }
.begin:active { transform: scale(.99); }

/* ================= DRILL (camera + overlay) ================= */
.drill { position: fixed; inset: 0; background: #0a0c0f; overflow: hidden; }
.camera, #video, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#video, #overlay { object-fit: cover; transform: scaleX(-1); }

/* slight, even dark overlay so the white text stays legible over any camera */
.cam-tint { position: absolute; inset: 0; z-index: 1; pointer-events: none; background: rgba(8, 10, 13, 0.34); }

/* thin question-progress bar along the very top edge */
.progress { position: absolute; top: 0; left: 0; right: 0; height: 3px; z-index: 3; background: rgba(247,244,238,.14); pointer-events: none; }
.progress i { display: block; height: 100%; width: 0%; background: var(--cream); transition: width .35s ease; }
.progress.hidden { display: none; }

/* completion overlay */
.complete { position: absolute; inset: 0; z-index: 4; display: flex; align-items: center; justify-content: center;
  background: rgba(8, 10, 13, 0.62); backdrop-filter: blur(3px); padding: 24px; }
.complete[hidden] { display: none; }
.complete-card { text-align: center; color: var(--cream); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.complete-title { font-size: 13px; letter-spacing: .2em; text-transform: uppercase; color: var(--cream-soft); }
.complete-time { font-variant-numeric: tabular-nums; font-size: clamp(52px, 12vw, 96px); font-weight: 300; line-height: 1; letter-spacing: -0.03em; }
.complete-sub { font-size: 15px; color: var(--cream-soft); min-height: 20px; margin-bottom: 8px; }
.complete-sub.best { color: #dffbe6; }
.complete-actions { display: flex; gap: 10px; margin-top: 6px; }

.hud {
  position: absolute; inset: 0; z-index: 2; pointer-events: none;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: clamp(18px, 3.2vw, 34px);
  color: var(--cream); text-shadow: var(--shadow-cam);
}
.hud button { pointer-events: auto; }

/* top bar: clickable logo (home) + score/streak */
.hud-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; }
.mark.small { color: var(--cream); flex: 0 0 auto; opacity: .85; background: none; border: none; padding: 0; cursor: pointer; }
.mark.small:hover { opacity: 1; }
.mark.small svg { width: 30px; height: 30px; display: block; }
.stats { display: flex; gap: clamp(18px, 5vw, 44px); }
.stat { display: flex; flex-direction: column; gap: 4px; align-items: flex-end; text-align: right; }
.stat[hidden] { display: none; }
.stat .k { color: var(--cream-soft); }
.stat .v { font-variant-numeric: tabular-nums; font-size: clamp(18px, 2.4vw, 24px); font-weight: 400; letter-spacing: -0.01em; }

/* bottom group: equation + answer stacked above the live/tools bar */
.hud-lower { display: flex; flex-direction: column; gap: clamp(14px, 2.6vh, 24px); }

.hud-center { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 12px; }
.equation {
  font-variant-numeric: tabular-nums lining-nums;
  font-size: clamp(34px, 7vw, 62px); font-weight: 300; letter-spacing: -0.02em; line-height: 1;
}
.equation .op { font-weight: 300; margin: 0 .28em; opacity: .85; }
.equation .eq { opacity: .55; }
/* contiguous: pairs sit flush so the answer reads as one number */
.chunks { display: flex; flex-direction: row-reverse; justify-content: center; align-items: flex-start; gap: 0; margin-top: 6px; }
.grp { position: relative; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.grp-val {
  font-variant-numeric: tabular-nums; font-size: clamp(34px, 6vw, 56px); font-weight: 300; line-height: 1;
  min-height: 1em; color: var(--cream); transition: color .2s;
}
.grp-val.blank { visibility: hidden; }        /* reserves digit width, shows only the underline */
.grp.solved .grp-val { color: #eafff0; }
.grp-underline { width: 100%; height: 1px; background: var(--line-cam); position: relative; overflow: hidden; }
.grp.active .grp-underline { background: var(--cream-faint); }
.grp-underline i { position: absolute; inset: 0; width: 0%; background: var(--cream); transition: width .08s linear; display: block; }
.grp.solved .grp-underline { background: var(--good); }
.grp.solved .grp-underline i { width: 100% !important; background: var(--good); }
.grp-carry {
  position: absolute; top: -22px; left: 50%; transform: translateX(-50%);
  font-size: 10px; font-weight: 500; letter-spacing: .16em; text-transform: uppercase;
  color: var(--cream); white-space: nowrap; opacity: .9;
  padding-bottom: 3px;
}
.grp-carry::after { content: ""; position: absolute; left: 50%; bottom: 0; width: 14px; height: 1px; background: var(--cream-faint); transform: translateX(-50%); }

.feedback { min-height: 22px; font-size: 15px; font-weight: 400; letter-spacing: .01em; }
.feedback.good { color: #dffbe6; }

.drill-hud { display: flex; flex-direction: column; align-items: center; gap: 12px; }

/* ================= TUTORIAL ================= */
.stat .v.tiny { font-size: clamp(13px, 1.5vw, 15px); font-weight: 400; letter-spacing: 0; max-width: 42vw; }

.tut { display: flex; flex-direction: column; align-items: center; gap: clamp(10px, 1.8vh, 18px); width: 100%; }
.tut-say {
  margin: 0; max-width: 34ch; font-size: clamp(15px, 2.1vw, 19px); font-weight: 300;
  line-height: 1.45; color: var(--cream);
}
.tut-say b { font-weight: 500; }
.tut-target { display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 108px; }
.tut-target[hidden] { display: none; }
.tut-k { color: var(--cream-soft); font-size: 11px; font-weight: 500; letter-spacing: .18em; text-transform: uppercase; }
.tut-num {
  font-variant-numeric: tabular-nums; font-size: clamp(46px, 9vw, 78px); font-weight: 300;
  line-height: 1; letter-spacing: -0.03em;
}
.tut-target .grp-underline { width: 100%; background: var(--cream-faint); }
.tut-target.got .tut-num { color: #eafff0; }
.tut-target.got .grp-underline, .tut-target.got .grp-underline i { background: var(--good); width: 100%; }

.tut-nudge { min-height: 18px; font-size: 13px; font-weight: 300; color: var(--cream-soft); }
.tut-tip { font-size: 12px; font-weight: 300; color: var(--cream-faint); max-width: 34ch; }
.tut-tip[hidden] { display: none; }
.tut-tick { font-size: clamp(48px, 10vw, 80px); color: #dffbe6; }

/* gesture gate: the bar that fills while you hold the pose that advances a step */
.tut-gate { display: flex; flex-direction: column; align-items: center; gap: 7px; min-width: 210px; }
.tut-gate[hidden] { display: none; }
.tut-gate .tut-k { color: var(--cream); opacity: .82; }
.tut-gate .grp-underline { width: 100%; background: var(--cream-faint); }
.tut-gate.got .tut-k { color: #eafff0; }
.tut-gate.got .grp-underline, .tut-gate.got .grp-underline i { background: var(--good); width: 100%; }
.tut-gate.back .grp-underline i { background: #f3dca6; }

/* ---- schematic hands ---- */
.tut-hands { display: flex; gap: clamp(16px, 4vw, 40px); align-items: flex-start; }
.tut-hands[hidden] { display: none; }
.hand { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.hand-k { color: var(--cream-soft); font-size: 10px; font-weight: 500; letter-spacing: .18em; text-transform: uppercase; }
.hand svg { width: clamp(66px, 11vw, 96px); height: auto; display: block; }
.hand.tens svg { transform: scaleX(-1); }
.hand .palm { fill: rgba(247, 244, 238, .16); stroke: var(--line-cam); stroke-width: 2; }
.hand .down { fill: rgba(247, 244, 238, .13); stroke: var(--line-cam); stroke-width: 2; }
.hand .up { fill: var(--cream); }
.hand-cap {
  font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 300; color: var(--cream-soft);
  white-space: nowrap;
}
.hand-cap b { font-weight: 500; color: var(--cream); }

/* bottom bar: live "your hands" value + controls */
.hud-bottom { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; }
.live { display: flex; flex-direction: column; gap: 4px; }
.live .k { color: var(--cream-soft); }
.live .v { font-variant-numeric: tabular-nums; font-size: clamp(40px, 8vw, 72px); font-weight: 300; line-height: .95; letter-spacing: -0.03em; }
.tools { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.tool {
  border: 1px solid var(--line-cam); background: rgba(10,12,15,.28); backdrop-filter: blur(6px);
  color: var(--cream); font-family: inherit; font-size: 13px; font-weight: 400;
  padding: 9px 15px; border-radius: 999px; cursor: pointer; transition: background .18s, transform .08s;
  text-shadow: none;
}
.tool:hover:not(:disabled) { background: rgba(10,12,15,.5); }
.tool:active:not(:disabled) { transform: scale(.96); }
.tool:disabled { opacity: .35; cursor: not-allowed; }
.tool.solid { background: var(--cream); color: var(--ink); border-color: var(--cream); }
/* Solid buttons keep dark ink, so they need their own hover: the plain .tool:hover rule
   is more specific and would darken the background out from under that text. */
.tool.solid:hover:not(:disabled) { background: #fff; color: var(--ink); border-color: #fff; }

/* ---- quick reference sheet (drill screen) ---- */
.sheet {
  position: absolute; inset: 0; z-index: 6; display: flex; align-items: center; justify-content: center;
  padding: clamp(14px, 3vw, 32px); background: rgba(8, 10, 13, .72); backdrop-filter: blur(4px);
}
.sheet[hidden] { display: none; }
.sheet-card {
  width: 100%; max-width: 680px; max-height: 100%; overflow-y: auto;
  display: flex; flex-direction: column; gap: 18px;
  padding: clamp(16px, 2.4vw, 24px); border-radius: 20px;
  background: rgba(16, 19, 24, .96); border: 1px solid var(--line-cam);
  color: var(--cream); text-shadow: none;
}
.sheet-head, .sheet-foot { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.sheet-title { font-size: 13px; font-weight: 500; letter-spacing: .2em; text-transform: uppercase; color: var(--cream-soft); }
.sheet-note { font-size: 12px; font-weight: 300; color: var(--cream-faint); }
.sheet-body { display: flex; flex-direction: column; gap: 20px; }

.ref-k { margin: 0 0 10px; font-size: 11px; font-weight: 500; letter-spacing: .18em; text-transform: uppercase; color: var(--cream-soft); }
.ref-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(52px, 1fr)); gap: 10px; }
.ref-cell { display: flex; flex-direction: column-reverse; align-items: center; gap: 4px; }
.ref-val { font-variant-numeric: tabular-nums; font-size: 13px; font-weight: 400; color: var(--cream); }
.sheet .hand svg { width: 100%; max-width: 46px; height: auto; }

/* diagnostics readout (?debug=1) — what the hand tracker actually reports */
.dbg {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%); z-index: 5;
  margin: 0; padding: 10px 12px; border-radius: 10px; pointer-events: none;
  background: rgba(8, 10, 13, .74); color: #cfe9d6; border: 1px solid var(--line-cam);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px; line-height: 1.55; white-space: pre; letter-spacing: .01em;
}
.dbg[hidden] { display: none; }

/* loading */
.cam-loading {
  position: absolute; inset: 0; z-index: 3; display: none;
  flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: #0a0c0f; color: var(--cream-soft); font-size: 13px; letter-spacing: .04em; font-weight: 300;
}
.cam-loading.show { display: flex; }
.dot-spin { width: 30px; height: 30px; border: 2px solid var(--line-cam); border-top-color: var(--cream); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.05); } 100% { transform: scale(1); } }
.pop { animation: pop .4s; }

@media (max-width: 620px) {
  .stats { gap: 22px; }
  .hud-bottom { flex-direction: column; align-items: flex-start; gap: 12px; }
  .tools { justify-content: flex-start; }
}
