:root {
  color-scheme: dark;
  --bg: #000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --text: #f5f5f7;
  --text-dim: #a1a1a6;
  --accent: #0a84ff;
  --ok: #22e05a;
  --warn: #ffcc00;
  --error: #ff453a;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Buttons ---------- */
.button {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}
.button:disabled { opacity: 0.5; }
.button-primary {
  background: var(--accent);
  font-size: 1.35rem;
  padding: 1.1rem 2rem;
  min-height: 64px;
  width: min(100%, 22rem);
}
.button-secondary {
  background: var(--surface-2);
  font-size: 1.1rem;
  padding: 0.85rem 1.2rem;
  min-height: 54px;
}
.button-wide { width: 100%; }
.button-small {
  background: rgba(44, 44, 46, 0.85);
  font-size: 0.85rem;
  padding: 0.5rem 0.8rem;
  min-height: 36px;
}

/* ---------- Start screen ---------- */
.start-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  text-align: center;
  padding: calc(24px + var(--safe-top)) calc(20px + var(--safe-right))
           calc(24px + var(--safe-bottom)) calc(20px + var(--safe-left));
}
.start-screen h1 { font-size: clamp(3rem, 18vw, 5rem); margin: 0; letter-spacing: 0.04em; }
.subtitle { margin: 0; font-size: 1.15rem; color: var(--text-dim); }
.hint { margin: 0.5rem 0 1rem; max-width: 22rem; line-height: 1.4; color: var(--text-dim); }
.error-text { color: var(--error); min-height: 1.4em; margin: 0; max-width: 22rem; line-height: 1.4; }
.version-badge {
  margin: 0;
  padding: 0.3rem 0.8rem;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.95rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.settings {
  width: min(100%, 22rem);
  display: grid;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.4rem 0.4rem 0.4rem 0.9rem;
  background: var(--surface);
  border-radius: 12px;
  text-align: left;
  font-size: 1rem;
}
.setting .select,
.setting .input { flex: 0 0 auto; max-width: 11rem; }
.input {
  font: inherit;
  font-size: 1rem; /* ≥ 16px avoids the iOS zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 0;
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  min-height: 44px;
  width: 9rem;
  text-align: right;
}
.input::placeholder { color: var(--text-dim); }
.setting.is-disabled { opacity: 0.5; }

/* ---------- Live screen ---------- */
.live-screen {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
}

.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: #000;
  overflow: hidden;
}
.stage video,
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain; /* Same fit for both so the overlay matches the video exactly */
}
.stage.mirrored video,
.stage.mirrored canvas { transform: scaleX(-1); }

.banner {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  padding: 0.8rem 1rem;
  border-radius: var(--radius);
  background: rgba(255, 204, 0, 0.92);
  color: #000;
  font-weight: 700;
  font-size: 1.05rem;
  line-height: 1.3;
  text-align: center;
}

.banner-notice {
  top: auto;
  bottom: 64px;
  background: rgba(255, 69, 58, 0.94);
  color: #fff;
  font-size: 1.2rem;
  z-index: 2;
}

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.6);
  font-size: 1.4rem;
  font-weight: 600;
}
.loading.is-error { color: var(--error); font-size: 1.1rem; line-height: 1.4; }

.stage-button-left { position: absolute; left: 12px; bottom: 12px; }
.stage-button-right { position: absolute; right: 12px; bottom: 12px; }

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(8rem, 50vw, 16rem);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 24px rgba(0, 0, 0, 0.8);
  background: rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.6rem calc(16px + var(--safe-right)) calc(14px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: var(--surface);
}

.angle-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}
.angle-value {
  font-size: clamp(4.5rem, 24vw, 10rem);
  font-weight: 800;
  line-height: 1;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.rep-counter {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 4.5rem;
}
.rep-counter-label { color: var(--text-dim); font-size: 0.95rem; }
.rep-counter strong {
  font-size: clamp(2.5rem, 13vw, 5rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.button-stop { background: var(--error); }
.angle-value.on-target { color: var(--ok); }
.angle-label { text-align: center; color: var(--text-dim); font-size: 1rem; margin-top: -0.2rem; }

.min-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 1.2rem;
}
.min-row strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; }

/* ---------- Summary screen ---------- */
.summary-screen {
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
}
.summary-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.summary-header h1 { font-size: 1.5rem; margin: 0; }
.score {
  text-align: center;
  margin: 1rem 0 0.25rem;
  font-variant-numeric: tabular-nums;
}
#summary-score { font-size: clamp(5rem, 30vw, 9rem); font-weight: 800; line-height: 1; }
.score-max { font-size: 1.6rem; color: var(--text-dim); margin-left: 0.25rem; }
.summary-reps { text-align: center; color: var(--text-dim); margin: 0 0 1rem; }
.summary-goal {
  text-align: center;
  font-size: 1.25rem;
  font-weight: 700;
  margin: -0.5rem 0 1rem;
  color: var(--warn);
}
.summary-goal.is-good { color: var(--ok); }
.correction {
  font-size: clamp(1.4rem, 6.5vw, 2rem);
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  padding: 1rem;
  margin: 0 0 1rem;
  border-radius: var(--radius);
  background: rgba(255, 69, 58, 0.15);
  color: #ff8a80;
}
.correction.is-good { background: rgba(34, 224, 90, 0.12); color: var(--ok); }
.components {
  display: grid;
  gap: 0.4rem;
  margin-bottom: 1.2rem;
  font-size: 1rem;
}
.component-row {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 0.8rem;
  background: var(--surface);
  border-radius: 10px;
}
.component-row strong { font-variant-numeric: tabular-nums; }
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
}
.rep-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.6rem;
  min-width: 0;
}
.rep-caption { font-size: 0.95rem; line-height: 1.3; margin: 0 0 0.5rem; }
.rep-caption.is-good { color: var(--ok); }
.rep-frame { display: block; width: 100%; height: auto; border-radius: 8px; background: #000; }
.rep-frame.mirrored { transform: scaleX(-1); }
.rep-frame-missing {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 9 / 16;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--warn);
  font-weight: 700;
  text-align: center;
  padding: 0.5rem;
}
.rep-meta { margin: 0.4rem 0 0; font-size: 0.85rem; color: var(--text-dim); }
.rep-posture { margin: 0.3rem 0 0; font-size: 0.85rem; color: var(--warn); font-weight: 600; }
.summary-footer {
  position: sticky;
  bottom: 0;
  margin: 1rem calc(-16px - var(--safe-right)) 0 calc(-16px - var(--safe-left));
  padding: 0.75rem calc(16px + var(--safe-right)) calc(14px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: linear-gradient(to top, var(--bg) 70%, rgba(0, 0, 0, 0));
}

/* ---------- Debug panel ---------- */
.debug-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 75vh;
  max-height: 75dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: rgba(18, 18, 20, 0.97);
  border-top: 1px solid var(--surface-2);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 12px calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  z-index: 10;
}
.debug-header { display: flex; align-items: center; justify-content: space-between; }
.debug-header h2 { margin: 0; font-size: 1.1rem; }
.debug-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.8rem;
  font-size: 1rem;
}
.select {
  font: inherit;
  font-size: 1rem; /* ≥ 16px avoids the iOS zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 0;
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  min-height: 44px;
}
.select:disabled { opacity: 0.5; }
.toggle { width: 28px; height: 28px; accent-color: var(--ok); flex: 0 0 auto; }
.debug-note { margin: 0.2rem 0 0; font-size: 0.8rem; color: var(--warn); }
.hint-small { margin: 0; font-size: 0.9rem; }
.countdown.is-text { font-size: clamp(2rem, 10vw, 3.5rem); text-align: center; padding: 1rem; }
.debug-test-voice { margin-top: 0.8rem; }
.debug-stats,
.debug-log {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0.6rem 0;
}
.debug-log {
  background: #000;
  border-radius: 8px;
  padding: 0.6rem;
  max-height: 30vh;
  overflow-y: auto;
}
.debug-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.debug-actions .button { flex: 1 1 8rem; }
.copy-status { margin: 0.4rem 0 0; min-height: 1.2em; color: var(--ok); font-size: 0.9rem; }
.log-error { color: var(--error); }
.log-warn { color: var(--warn); }
