:root {
  --bg: #0b0b0b;
  --fg: #f2f2f2;
  --mute: #8b8b8b;
  --line: #2a2a2a;
  --panel: #121212;
  --accent: #ff3b3b;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --cam-aspect: 1.3333; /* tỉ lệ khung camera, JS cập nhật theo video thật */
  --chrome: 88px;       /* chiều cao header + lề, để khung camera vừa một màn hình */
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.hidden { display: none !important; }
b { font-weight: 700; }

/* ---------- Nút ---------- */
button {
  font: inherit;
  color: var(--fg);
  background: none;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 8px 14px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--fg); }
button:disabled { opacity: .4; cursor: default; }

.btn {
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: 13px;
  font-weight: 600;
  padding: 12px 22px;
}
.btn.primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.btn.primary:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.link { border: none; color: var(--mute); padding: 6px; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--fg); }

/* Chọn chế độ */
.modes { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.modes button { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 128px; padding: 10px 12px; text-align: left; }
.modes button b { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }
.modes button span { font-size: 11px; color: var(--mute); line-height: 1.3; }
.modes button.active { border-color: var(--fg); background: var(--fg); color: var(--bg); }
.modes button.active span { color: var(--bg); opacity: .7; }
.modes button:disabled { opacity: .45; }
.modes button:disabled span { text-transform: uppercase; letter-spacing: .08em; font-size: 10px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; font-family: var(--mono); padding: 8px 16px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--fg); color: var(--bg); }

/* ---------- Header ---------- */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  max-width: 960px; width: 100%; margin: 0 auto;
}
.logo { font-weight: 800; letter-spacing: .12em; font-size: 15px; }
.logo::before { content: ""; display: inline-block; width: 10px; height: 10px; background: var(--accent); margin-right: 10px; }
.lang { display: flex; gap: 2px; }
.lang button { font-size: 12px; padding: 4px 8px; border: none; color: var(--mute); font-family: var(--mono); }
.lang button.active { color: var(--fg); text-decoration: underline; text-underline-offset: 4px; }

/* ---------- Sân khấu ---------- */
.stage {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 840px) 56px;
  gap: 12px;
  justify-content: center;
  align-content: start;
  align-items: start;
  padding: 0 20px 20px;
}

.cam-wrap {
  position: relative;
  justify-self: center;
  width: min(100%, calc((100vh - var(--chrome)) * var(--cam-aspect)));
  width: min(100%, calc((100dvh - var(--chrome)) * var(--cam-aspect)));
  aspect-ratio: var(--cam-aspect);
  touch-action: manipulation;
  background: #000;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

#video, #overlay {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  transform: scaleX(-1); /* gương */
}

.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 24px;
  background: rgba(11, 11, 11, .92);
}
.screen.clear { background: transparent; }
/* Nội dung dài hơn khung thì cuộn; margin auto giữ nội dung ở giữa khi còn chỗ */
.screen:not(.clear) { justify-content: flex-start; overflow-y: auto; }
.screen:not(.clear) > :first-child { margin-top: auto; }
.screen:not(.clear) > :last-child { margin-bottom: auto; }
.screen:not(.clear) > :not(.board) { flex-shrink: 0; }
h1 { margin: 0; font-size: 34px; font-weight: 800; letter-spacing: -.01em; }
h2 { margin: 0; font-size: 24px; font-weight: 800; }
.sub { color: var(--mute); margin: 0; max-width: 46ch; }
/* Mô tả chế độ luôn chiếm 3 dòng, link phím và dòng kỷ lục giữ chỗ dù ẩn */
#screen-start .sub { min-height: calc(3 * 1.5em); display: flex; align-items: center; }
.ghost { visibility: hidden; }
#screen-start .best { min-height: 1.5em; }
.fine { color: var(--mute); font-size: 12px; margin: 8px 0 0; max-width: 52ch; }
.best { margin: 0; font-family: var(--mono); font-size: 13px; color: var(--mute); }
.best:empty:not(#best-line) { display: none; }

.card {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  background: rgba(11, 11, 11, .88); border: 1px solid var(--line); border-radius: 6px;
  padding: 14px 18px; width: 100%; max-width: 400px;
}
.hint { margin: 0; color: var(--mute); }
.hint:empty { display: none; }
/* Thẻ chỉ còn nội dung ẩn/rỗng (camera đã sẵn sàng) thì không hiện khung trống */
#screen-ready .card:not(:has(> :not(.hidden, :empty))) { display: none; }
#screen-ready { justify-content: flex-end; }

.countdown { font-family: var(--mono); font-size: 140px; font-weight: 700; line-height: 1; animation: pop .8s ease-out; }
@keyframes pop { from { transform: scale(1.25); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- Hướng dẫn cử chỉ ---------- */
.gesture {
  position: absolute; top: 12px; left: 12px; right: 12px; z-index: 5;
  background: rgba(11, 11, 11, .88); border: 1px solid var(--line); border-radius: 4px;
  padding: 10px 14px 0;
}
.gbar { margin-top: 10px; height: 2px; background: var(--line); }
#gesture-fill { height: 100%; width: 0; background: var(--fg); }

/* ---------- HUD ---------- */
.hud { position: absolute; inset: 0; pointer-events: none; font-family: var(--mono); }
.hud-timer {
  position: absolute; top: 12px; left: 12px;
  font-size: 22px; font-weight: 700;
  background: rgba(11, 11, 11, .75); padding: 4px 10px; border-radius: 4px;
  font-variant-numeric: tabular-nums;
}
.hud-status {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #fff; font-family: var(--sans); font-weight: 600; font-size: 13px;
  padding: 5px 12px; border-radius: 4px; white-space: nowrap;
}
.hud-status:empty { display: none; }
.hud-count {
  position: absolute; bottom: 8px; left: 16px;
  font-size: 96px; font-weight: 700; line-height: 1;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0,0,0,.8);
}
.hud-count small { font-family: var(--sans); font-size: 13px; font-weight: 400; color: var(--mute); margin-left: 8px; text-transform: uppercase; letter-spacing: .08em; }
.hud-split { font-family: var(--mono); font-size: 14px; color: var(--mute); margin-left: 10px; }
.hud-split:empty { display: none; }
.hud-count.bump { animation: bump .12s ease-out; }
@keyframes bump { 50% { transform: scale(1.06); } }

/* ---------- Kết quả ---------- */
.res-hero { display: flex; flex-direction: column; align-items: center; }
.res-num { font-family: var(--mono); font-size: 84px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.res-unit { color: var(--mute); font-size: 12px; text-transform: uppercase; letter-spacing: .1em; margin-top: 6px; }
.res-note { margin: -6px 0 0; color: var(--mute); }
.res-stats { display: flex; gap: 28px; margin: 4px 0 0; }
.res-stats div { display: flex; flex-direction: column; align-items: center; }
.res-stats dt { color: var(--mute); font-size: 11px; text-transform: uppercase; letter-spacing: .1em; order: 2; }
.res-stats dd { margin: 0; font-family: var(--mono); font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
#res-chart { width: min(560px, 100%); height: auto; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: center; }

/* ---------- Thanh lực ---------- */
.power {
  align-self: stretch;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 12px 0;
}
.power-label, .power-value small { font-size: 10px; text-transform: uppercase; letter-spacing: .12em; color: var(--mute); }
.power-bar {
  position: relative; flex: 1; width: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 2px; overflow: hidden;
}
.power-fill { position: absolute; left: 0; right: 0; bottom: 0; height: 0%; background: var(--fg); transition: height .08s linear; }
.power-bar.maxed .power-fill { background: var(--accent); }
.power-bar.low .power-fill { background: var(--accent); }
.power-hand { position: absolute; left: 0; right: 0; height: 2px; top: 50%; background: var(--accent); opacity: 0; }
.power-hand.on { opacity: 1; }
.power-value { font-family: var(--mono); font-size: 16px; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }
.power-value small { display: block; font-family: var(--sans); font-weight: 400; }

.links { gap: 2px 12px; }

/* ---------- Gửi điểm ---------- */
.submit { display: flex; align-items: center; justify-content: center; gap: 4px 10px; flex-wrap: wrap; min-height: 38px; }
.submit-form { display: flex; gap: 6px; }
.submit input {
  font: inherit; color: var(--fg); background: var(--panel);
  border: 1px solid var(--line); border-radius: 4px; padding: 7px 12px; width: 210px;
}
.submit input:focus { outline: none; border-color: var(--fg); }
.submit .btn { padding: 8px 14px; }
.submit .best { color: var(--fg); }

/* ---------- Bảng xếp hạng ---------- */
#screen-board { justify-content: flex-start; }
.board {
  list-style: none; margin: 0; padding: 0;
  width: min(460px, 100%); flex: 1 1 0; min-height: 0; overflow-y: auto;
  border-top: 1px solid var(--line);
}
.board li {
  display: grid; grid-template-columns: 3ch minmax(0, 1fr) auto 9ch; gap: 12px; align-items: baseline;
  padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left;
}
.board .rk { font-family: var(--mono); color: var(--mute); text-align: right; }
.board li:nth-child(-n+3) .rk { color: var(--accent); font-weight: 700; }
.board .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .sc { font-family: var(--mono); font-weight: 700; font-variant-numeric: tabular-nums; }
.board .ct { font-family: var(--mono); font-size: 12px; color: var(--mute); text-align: right; }
.board li.me { background: var(--fg); color: var(--bg); }
.board li.me .rk, .board li.me .ct { color: var(--bg); }
.board li.note { display: block; color: var(--mute); text-align: center; padding: 24px 8px; border: 0; }
/* Dòng chẩn đoán khi mở ?debug */
.diag {
  position: fixed; left: 8px; bottom: 8px; z-index: 50; margin: 0; pointer-events: none;
  font: 11px/1.4 var(--mono); color: var(--fg); background: rgba(0, 0, 0, .8);
  padding: 4px 8px; border-radius: 4px; white-space: pre-wrap; max-width: calc(100vw - 16px);
}


@media (max-width: 640px) {
  :root { --cam-aspect: 0.75; --chrome: 124px; }
  .top { padding: 12px 14px; }
  .stage { grid-template-columns: minmax(0, 1fr); gap: 10px; padding: 0 12px 12px; }
  .screen { padding: 16px 14px; gap: 10px; }
  .modes { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; max-width: 360px; }
  .modes button { width: auto; padding: 8px 10px; }
  .modes button:not(:disabled) span { display: none; } /* mô tả chế độ đã có ở đoạn bên dưới */
  .sub { font-size: 14px; }
  .fine { font-size: 11px; margin-top: 0; }
  .btn { padding: 10px 16px; }
  .submit input { width: 170px; }
  .res-stats { gap: 18px; }
  .res-stats dd { font-size: 20px; }
  .hud-status { white-space: normal; width: max-content; max-width: 60%; text-align: center; }
  .power { flex-direction: row; padding: 0; gap: 12px; }
  .power-bar { width: auto; height: 10px; }
  .power-fill { left: 0; right: auto; top: 0; bottom: 0; width: var(--pct, 0%); height: 100% !important; }
  .power-hand { display: none; }
  h1 { font-size: 22px; }
  h2 { font-size: 20px; }
  .res-num { font-size: 56px; }
  .hud-count { font-size: 64px; }
  .countdown { font-size: 100px; }
}
