:root {
  --bg: #0f1020;
  --bg2: #1a1c3a;
  --accent: #ff4d8d;
  --accent2: #4dd0ff;
  --p1: #4dd0ff;
  --p2: #ffd24d;
  --ok: #57e08a;
  --text: #f4f5ff;
  --muted: #a9adcf;
  font-size: 16px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: radial-gradient(1200px 800px at 50% -10%, #26295a 0%, var(--bg) 60%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

#app { height: 100%; width: 100%; }

.screen { display: none; height: 100%; width: 100%; }
.screen.active { display: flex; }
.hidden { display: none !important; }

/* ---------------- MENU ---------------- */
#menu { align-items: center; justify-content: center; padding: clamp(12px, 3vw, 32px); overflow-y: auto; }

.menu-card {
  width: min(560px, 96vw);
  background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02));
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 24px;
  padding: clamp(20px, 3.5vw, 32px) clamp(18px, 3vw, 30px) clamp(16px, 2.5vw, 24px);
  backdrop-filter: blur(8px);
  box-shadow: 0 30px 80px rgba(0,0,0,0.45);
}

.logo {
  font-weight: 900;
  font-size: clamp(34px, 6vw, 58px);
  line-height: 0.95;
  letter-spacing: 1px;
  text-align: center;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 18px;
}
.logo span { -webkit-text-fill-color: transparent; }

.rules {
  background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.07);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 18px;
}
.rules h3 { font-size: 13px; letter-spacing: 2px; color: var(--accent); margin-bottom: 8px; }
.rules ol { padding-left: 18px; color: var(--muted); font-size: clamp(13px, 1.3vw, 15px); line-height: 1.6; }
.rules b { color: var(--text); }

.option-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.option-label { color: var(--muted); font-size: 14px; }
.seg { display: inline-flex; background: rgba(0,0,0,0.3); border-radius: 12px; padding: 4px; }
.seg-btn {
  border: none; background: transparent; color: var(--muted);
  padding: 8px 16px; border-radius: 9px; font-weight: 700; cursor: pointer; transition: .15s;
  font-size: clamp(13px, 1.3vw, 15px);
}
.seg-btn.active { background: linear-gradient(90deg, var(--accent), var(--accent2)); color: #14152b; }

.mode-buttons { display: grid; gap: 12px; }
.btn {
  cursor: pointer; border: none; border-radius: 16px; color: var(--text);
  font-family: inherit; transition: transform .12s ease, box-shadow .12s ease, opacity .12s;
}
.btn:active { transform: scale(0.98); }
.btn.primary {
  display: flex; flex-direction: column; gap: 3px; align-items: flex-start;
  padding: clamp(14px, 1.8vw, 20px) clamp(16px, 2vw, 22px); text-align: left;
  background: linear-gradient(120deg, rgba(255,77,141,0.22), rgba(77,208,255,0.22));
  border: 1px solid rgba(255,255,255,0.14);
}
.btn.primary:hover { box-shadow: 0 12px 30px rgba(255,77,141,0.25); }
.btn.primary .big { font-weight: 900; font-size: clamp(16px, 1.8vw, 20px); letter-spacing: 1px; }
.btn.primary .sub { font-size: clamp(12px, 1.2vw, 14px); color: var(--muted); }

.credit { text-align: center; color: var(--text, #fff); font-size: clamp(13px, 1.3vw, 15px); font-weight: 700; margin-top: 16px; opacity: 1; line-height: 1.6; }
.credit a { color: inherit; text-decoration: underline; }
.credit a:hover { color: var(--accent, #fff); }

/* --- Tablet & laptop (≥ 768px): kartu melebar, layout 2 kolom --- */
@media (min-width: 768px) {
  .menu-card {
    width: min(860px, 92vw);
    display: grid;
    grid-template-columns: 1.05fr 1fr;
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas:
      "logo    logo"
      "rules   options"
      "rules   modes"
      "credit  credit";
    column-gap: clamp(18px, 3vw, 32px);
    align-items: stretch;
  }
  .logo { grid-area: logo; }
  .rules { grid-area: rules; margin-bottom: 0; display: flex; flex-direction: column; justify-content: center; }
  .option-row { grid-area: options; margin-bottom: 12px; }
  .mode-buttons { grid-area: modes; align-content: end; }
  .credit { grid-area: credit; margin-top: 20px; }
}

/* --- Tablet portrait (≥ 768px tapi sempit/tinggi): tetap 1 kolom, lebih besar --- */
@media (min-width: 600px) and (max-width: 767px) {
  .menu-card { width: min(640px, 94vw); }
  .mode-buttons { grid-template-columns: 1fr 1fr; }
  .btn.primary { min-height: 92px; }
}

/* --- Layar landscape pendek (laptop kecil / tablet landscape): rapatkan agar tak scroll --- */
@media (min-height: 500px) and (max-height: 720px) and (min-width: 768px) {
  .logo { margin-bottom: 12px; font-size: clamp(30px, 5vh, 44px); }
  .menu-card { padding-top: 18px; padding-bottom: 14px; }
  .rules { padding: 10px 14px; }
  .rules ol { line-height: 1.45; }
  .credit { margin-top: 10px; font-size: 12.5px; }
}

/* ---------------- GAME ---------------- */
#game { flex-direction: column; align-items: center; justify-content: center; padding: 12px; gap: 10px; }

.stage {
  position: relative;
  width: min(100%, 1100px);
  aspect-ratio: 16 / 9;
  max-height: 82vh;
  background: #000;
  border-radius: 18px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.1);
  box-shadow: 0 24px 60px rgba(0,0,0,0.5);
}

#video, #overlay {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
}
#video { transform: scaleX(-1); }         /* mirror preview */
#overlay { pointer-events: none; z-index: 16; }  /* above boards so hand skeleton stays visible */

#cropLayer { position: absolute; inset: 0; }

/* crop box */
.crop-box {
  position: absolute;
  border: 2.5px solid var(--accent2);
  border-radius: 6px;
  box-shadow: 0 0 0 2000px rgba(0,0,0,0.35);
  touch-action: none;
  cursor: move;
}
.crop-box.p1 { border-color: var(--p1); }
.crop-box.p2 { border-color: var(--p2); }
.crop-box.live { box-shadow: 0 0 0 2000px rgba(0,0,0,0.35), 0 0 18px 2px var(--ok); border-color: var(--ok); }
.crop-box .tag {
  position: absolute; top: -26px; left: 0;
  font-size: 12px; font-weight: 800; letter-spacing: 1px;
  padding: 2px 8px; border-radius: 6px; background: var(--p1); color: #06121a;
}
.crop-box.p2 .tag { background: var(--p2); }
.crop-box .handle {
  position: absolute; width: 16px; height: 16px;
  background: #fff; border: 2px solid var(--accent2); border-radius: 50%;
  touch-action: none;
}
.crop-box.p1 .handle { border-color: var(--p1); }
.crop-box.p2 .handle { border-color: var(--p2); }
.handle.nw { left: -8px;  top: -8px;  cursor: nwse-resize; }
.handle.n  { left: 50%;   top: -8px;  transform: translateX(-50%); cursor: ns-resize; }
.handle.ne { right: -8px; top: -8px;  cursor: nesw-resize; }
.handle.e  { right: -8px; top: 50%;   transform: translateY(-50%); cursor: ew-resize; }
.handle.se { right: -8px; bottom: -8px; cursor: nwse-resize; }
.handle.s  { left: 50%;   bottom: -8px; transform: translateX(-50%); cursor: ns-resize; }
.handle.sw { left: -8px;  bottom: -8px; cursor: nesw-resize; }
.handle.w  { left: -8px;  top: 50%;    transform: translateY(-50%); cursor: ew-resize; }

/* hold-trigger progress ring on the box */
.crop-box .hold {
  position: absolute; bottom: 6px; left: 50%; transform: translateX(-50%);
  width: 70%; height: 6px; background: rgba(0,0,0,0.5); border-radius: 4px; overflow: hidden;
}
.crop-box .hold i { display: block; height: 100%; width: 0%; background: var(--accent); transition: width .05s linear; }

/* pose countdown number inside the box after the gesture is confirmed */
.crop-box .snap-count {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: clamp(48px, 14vw, 120px); font-weight: 900; pointer-events: none;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 14px rgba(0,0,0,0.55));
  animation: pop .5s ease;
}
.crop-box .snap-count.hidden { display: none; }

.divider {
  position: absolute; top: 0; bottom: 0; left: 50%; width: 0;
  border-left: 3px dashed rgba(255,255,255,0.6);
  transform: translateX(-50%);
  pointer-events: none;
}

/* HUD chips */
.hud { position: absolute; top: 10px; left: 0; right: 0; display: flex; justify-content: space-between; padding: 0 12px; pointer-events: none; }
.chip {
  background: rgba(10,12,30,0.7); border: 1px solid rgba(255,255,255,0.14);
  padding: 6px 12px; border-radius: 999px; font-weight: 700; font-size: 13px;
  display: flex; gap: 8px; align-items: center;
}
.chip.p1 { border-color: var(--p1); }
.chip.p2 { border-color: var(--p2); }
.chip .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.chip.ready .dot { background: var(--ok); }
.chip .t { font-variant-numeric: tabular-nums; }

/* countdown */
.countdown {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(6,7,20,0.55); backdrop-filter: blur(3px); z-index: 20;
}
.countdown span { font-size: clamp(80px, 22vw, 200px); font-weight: 900;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: pop .6s ease; }
@keyframes pop { from { transform: scale(0.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* loading */
.loading { position: absolute; inset: 0; display: grid; place-items: center; gap: 14px; background: #05060f; z-index: 30; grid-auto-flow: row; align-content: center; }
.loading p { color: var(--muted); font-size: 14px; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid rgba(255,255,255,0.15); border-top-color: var(--accent2); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* boards */
.boards { position: absolute; inset: 0; display: flex; z-index: 15; background: rgba(6,7,20,0.72); backdrop-filter: blur(2px); }
.board-wrap { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 10px; min-width: 0; }
.board-wrap + .board-wrap { border-left: 3px dashed rgba(255,255,255,0.35); }
.board-title { font-weight: 800; letter-spacing: 1px; font-size: 14px; }
.board-title.p1 { color: var(--p1); }
.board-title.p2 { color: var(--p2); }
.board {
  position: relative;
  display: grid;
  gap: 3px;
  background: rgba(255,255,255,0.08);
  padding: 3px; border-radius: 10px;
  touch-action: none;
  width: min(46vh, 92%);
  aspect-ratio: 1 / 1;
}
.board.solved { box-shadow: 0 0 0 3px var(--ok), 0 0 40px var(--ok); }
.tile {
  position: relative;
  background-size: cover; background-repeat: no-repeat;
  border-radius: 5px; cursor: grab;
  transition: outline-color .1s, box-shadow .1s;
  outline: 2px solid transparent;
}
.tile.hover { outline-color: var(--accent2); }
.tile.drop  { outline-color: var(--ok); box-shadow: 0 0 0 2px var(--ok); }
.tile.dragging {
  cursor: grabbing; z-index: 10;
  outline-color: var(--accent);
  box-shadow: 0 10px 26px rgba(0,0,0,0.6);
  transition: none;                 /* follow the hand/pointer instantly */
}
.tile .num { position: absolute; top: 3px; left: 5px; font-size: 11px; font-weight: 800; color: #fff; text-shadow: 0 1px 2px #000; opacity: .55; }

/* gesture cursor */
.gcursor { position: absolute; width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%; border: 3px solid var(--accent); pointer-events: none; z-index: 40; transition: transform .05s; box-shadow: 0 0 12px var(--accent); }
.gcursor.p2 { border-color: var(--p2); box-shadow: 0 0 12px var(--p2); }
.gcursor.pinch { background: var(--accent); }
.gcursor.holding { border-color: var(--ok); box-shadow: 0 0 16px var(--ok); width: 32px; height: 32px; margin: -16px 0 0 -16px; }
.gcursor.holding.pinch { background: var(--ok); }

/* result */
.result { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(5,6,18,0.75); backdrop-filter: blur(6px); z-index: 50; }
.result-card { text-align: center; padding: 30px 34px; border-radius: 22px;
  background: linear-gradient(180deg, rgba(255,255,255,0.08), rgba(255,255,255,0.03));
  border: 1px solid rgba(255,255,255,0.14); }
.result-card h2 { font-size: 40px; font-weight: 900; margin-bottom: 6px;
  background: linear-gradient(90deg, var(--accent), var(--accent2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.result-card p { color: var(--muted); margin-bottom: 20px; font-size: 15px; }
.result-photo { margin: 10px auto 14px; }
.result-photo img { display: block; max-width: 180px; max-height: 180px; margin: 0 auto; border-radius: 14px;
  border: 2px solid rgba(255,255,255,0.25); box-shadow: 0 0 24px rgba(255,77,141,0.35); }
.result-photo.hidden { display: none; }
.result-btns { display: flex; gap: 12px; justify-content: center; }
.btn.ghost { padding: 12px 22px; font-weight: 800; letter-spacing: 1px; border: 1px solid rgba(255,255,255,0.2); background: rgba(255,255,255,0.06); }
.btn.ghost:hover { background: rgba(255,255,255,0.12); }

/* banner */
.banner { min-height: 24px; text-align: center; color: var(--muted); font-size: 14px; padding: 4px 12px; }
.banner b { color: var(--text); }
