/* ── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }

:root {
  --bg:        #f0f4f9;
  --surface:   #ffffff;
  --surface2:  #e8f0fe;
  --border:    #dde5f0;
  --text:      #1e293b;
  --dim:       #64748b;
  --yellow:    #d97706;
  --green:     #059669;
  --red:       #dc2626;
  --blue:      #2563eb;
  --shadow-sm: 0 1px 4px rgba(30,41,59,0.07);
  --shadow:    0 4px 18px rgba(30,41,59,0.1);
}

body {
  font-family: 'Noto Sans TC', -apple-system, 'Microsoft JhengHei', 'PingFang TC', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
  min-height: 100dvh;
}

/* ── Camera / Canvas ────────────────────────────────────────── */
.camera-wrapper {
  position: relative;
  width: 100%;
  background: #0a0e1a;
  aspect-ratio: 4/3;
  overflow: hidden;
  border-radius: 1.1rem;
  flex-shrink: 0;
  border: 2px solid var(--border);
  box-shadow: var(--shadow);
}
.camera-wrapper video,
.camera-wrapper canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
}
.camera-wrapper video { transform: scaleX(-1); }
.camera-wrapper canvas { z-index: 10; }

/* ── Game page layout ───────────────────────────────────────── */
.game-page {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 0.75rem)
           max(1rem, env(safe-area-inset-right, 1rem))
           env(safe-area-inset-bottom, 0.75rem)
           max(1rem, env(safe-area-inset-left, 1rem));
  gap: 0.9rem;
  overflow: hidden;
}

/* camera + panel row wrapper */
.game-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 0.9rem;
  min-height: 0;
}

@media (min-width: 768px) and (orientation: landscape) {
  .game-page {
    padding: 1rem 1.5rem 1.25rem;
    gap: 1rem;
  }
  .game-body {
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(300px, 0.9fr);
    gap: 1.25rem;
    align-items: center;
    flex: 1;
    min-height: 0;
  }
  .camera-wrapper {
    width: 100%;
    aspect-ratio: 4 / 3;
    height: auto;
    max-height: min(68vh, 480px);
    justify-self: stretch;
  }
  .game-panel {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 1rem;
    min-width: 0;
    max-height: min(68vh, 480px);
    padding: 1.4rem 1.35rem;
    overflow-y: auto;
  }
}

@media (max-width: 767px), (orientation: portrait) {
  .camera-wrapper { aspect-ratio: 4/3; }
  .game-panel { flex: 1; overflow-y: auto; }
}

/* ── Game panel card ────────────────────────────────────────── */
.game-panel {
  background: var(--surface);
  border-radius: 1.1rem;
  padding: 1.1rem 1.25rem;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: clamp(0.65rem, 2vw, 0.9rem) clamp(1.1rem, 4vw, 1.8rem);
  font-size: clamp(0.95rem, 2.8vw, 1.2rem);
  font-weight: 700;
  border: none;
  border-radius: 0.75rem;
  cursor: pointer;
  transition: transform 0.1s, filter 0.1s, box-shadow 0.1s;
  -webkit-tap-highlight-color: transparent;
  width: 100%;
  min-height: 50px;
}
.btn:active { transform: scale(0.96); filter: brightness(0.92); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--blue); color: #fff; box-shadow: 0 3px 10px rgba(37,99,235,0.25); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger  { background: var(--red); color: #fff; box-shadow: 0 3px 10px rgba(220,38,38,0.2); }
.btn-back {
  background: var(--surface);
  color: var(--dim);
  border: 1.5px solid var(--border);
  font-size: clamp(0.8rem, 2vw, 0.95rem);
  width: auto;
  padding: 0.45rem 0.9rem;
  min-height: 36px;
  box-shadow: var(--shadow-sm);
}
.btn-back:hover { background: var(--surface2); }

/* ── Status text ─────────────────────────────────────────────── */
.status-text {
  text-align: center;
  font-size: clamp(1.4rem, 4.5vw, 2.2rem);
  font-weight: 900;
  line-height: 1.2;
}
.status-sub {
  text-align: center;
  font-size: clamp(0.82rem, 2.4vw, 1.05rem);
  color: var(--dim);
  line-height: 1.5;
}

/* ── Big number ─────────────────────────────────────────────── */
.score-big {
  font-size: clamp(3rem, 11vw, 5.5rem);
  font-weight: 900;
  text-align: center;
  color: var(--blue);
  line-height: 1;
}

/* ── Progress bar ───────────────────────────────────────────── */
.progress-bar-wrap {
  background: var(--border);
  border-radius: 99px;
  height: clamp(10px, 1.8vw, 16px);
  overflow: hidden;
  width: 100%;
}
.progress-bar-fill {
  height: 100%;
  border-radius: 99px;
  transition: width 0.15s linear;
  background: linear-gradient(90deg, var(--green), #34d399);
}

/* ── Stable gesture progress ─────────────────────────────────── */
.stable-bar-wrap {
  background: var(--border);
  border-radius: 99px;
  height: 8px;
  overflow: hidden;
  width: 100%;
}
.stable-bar-fill {
  height: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--blue), var(--green));
  transition: width 0.1s ease;
}

/* ── Leaderboard ────────────────────────────────────────────── */
.leaderboard { list-style: none; display: flex; flex-direction: column; gap: 0.35rem; }
.lb-row {
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.45rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  font-size: clamp(0.78rem, 2.4vw, 0.95rem);
}
.lb-rank  { font-weight: 900; min-width: 1.6rem; color: var(--yellow); }
.lb-name  { flex: 1; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { font-weight: 700; color: var(--green); }
.lb-time  { font-size: 0.72em; color: var(--dim); }

/* ── Camera permission overlay ──────────────────────────────── */
.cam-permission {
  position: absolute; inset: 0; z-index: 50;
  background: rgba(10,14,26,0.93);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 0.9rem; text-align: center; padding: 1.5rem;
  border-radius: inherit;
  color: #f1f5f9;
}
.cam-permission .icon { font-size: 2.8rem; }
.cam-permission h3 { font-size: 1.2rem; color: #f1f5f9; }
.cam-permission p  { font-size: 0.88rem; color: #94a3b8; }

/* ── Top nav ────────────────────────────────────────────────── */
.top-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
  width: 100%;
}
.top-nav h1 {
  flex: 1;
  min-width: 0;
  font-size: clamp(0.95rem, 2.4vw, 1.25rem);
  font-weight: 800;
  color: var(--blue);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Result badge ───────────────────────────────────────────── */
.result-badge {
  font-size: clamp(1.5rem, 4.5vh, 2.2rem);
  font-weight: 900; text-align: center;
  padding: 0.55rem 1.2rem;
  border-radius: 0.9rem;
  width: 100%;
  max-width: 100%;
}
.result-win  {
  background: rgba(5,150,105,0.1);
  color: var(--green);
  border: 2px solid var(--green);
}
.result-lose {
  background: rgba(220,38,38,0.08);
  color: var(--red);
  border: 2px solid var(--red);
}
.result-draw {
  background: rgba(217,119,6,0.1);
  color: var(--yellow);
  border: 2px solid var(--yellow);
}

/* ── Gesture indicator on canvas ───────────────────────────── */
.gesture-pill {
  position: absolute;
  bottom: 0.7rem; left: 0.7rem;
  z-index: 20;
  background: rgba(10,14,26,0.8);
  color: #fcd34d;
  font-size: clamp(0.85rem, 2.5vw, 1.1rem);
  font-weight: 700;
  padding: 0.3rem 0.75rem;
  border-radius: 99px;
  pointer-events: none;
  border: 1px solid rgba(255,255,255,0.15);
  backdrop-filter: blur(4px);
}

/* ── Loading spinner ────────────────────────────────────────── */
.spinner {
  width: 32px; height: 32px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: #60a5fa;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Hand status chip ───────────────────────────────────────── */
.hand-status {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.8rem;
  border-radius: 99px;
  font-size: clamp(0.78rem, 2.2vw, 0.92rem);
  font-weight: 600;
  transition: background 0.2s, color 0.2s;
}
.hand-status.no-hand   { background: rgba(100,116,139,0.12); color: var(--dim); }
.hand-status.detecting { background: rgba(217,119,6,0.12);   color: var(--yellow); }
.hand-status.stable    { background: rgba(5,150,105,0.12);   color: var(--green); }
