:root {
  --tg-bg: var(--tg-theme-bg-color, #0f1115);
  --tg-text: var(--tg-theme-text-color, #ffffff);
  --tg-hint: var(--tg-theme-hint-color, #8a8f98);
  --tg-btn: var(--tg-theme-button-color, #2ea6ff);
  --tg-btn-text: var(--tg-theme-button-text-color, #ffffff);
  --accent-good: #34d399;
  --accent-warn: #fbbf24;
  --accent-bad: #f87171;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

#app { height: 100vh; width: 100vw; position: relative; }

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

/* Loading */
#loading-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.loader {
  width: 42px; height: 42px;
  border: 4px solid rgba(255,255,255,0.15);
  border-top-color: var(--tg-btn);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Camera */
.camera-wrap {
  position: relative;
  height: 100%;
  width: 100%;
  background: #000;
}
#video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}
#overlay {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  transform: scaleX(-1);
}

.top-bar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 12px) + 8px) 14px 8px;
  z-index: 5;
}
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.45);
  color: #fff;
  font-size: 18px;
  backdrop-filter: blur(6px);
}
.rep-badge {
  display: flex; flex-direction: column; align-items: center;
  background: rgba(0,0,0,0.45);
  border-radius: 16px;
  padding: 6px 22px;
  backdrop-filter: blur(6px);
}
.rep-badge span { font-size: 30px; font-weight: 800; line-height: 1; }
.rep-badge small { font-size: 11px; color: var(--tg-hint); }

.form-flag {
  position: absolute;
  top: 90px; left: 50%; transform: translateX(-50%);
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  transition: opacity 0.2s, background 0.2s;
  z-index: 5;
}
.form-flag.show { opacity: 1; }
.form-flag.good { background: var(--accent-good); color: #063; }
.form-flag.warn { background: var(--accent-warn); color: #533; }
.form-flag.bad { background: var(--accent-bad); color: #400; }

.bottom-panel {
  position: absolute; left: 0; right: 0; bottom: 128px;
  display: flex; justify-content: space-around;
  padding: 10px 8px;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(6px);
  z-index: 5;
}
.stat { display: flex; flex-direction: column; align-items: center; }
.stat-value { font-size: 20px; font-weight: 700; }
.stat-label { font-size: 11px; color: var(--tg-hint); }
.quality-stat .stat-value { color: var(--accent-good); }

.feedback-line {
  position: absolute; left: 12px; right: 12px; bottom: 84px;
  text-align: center;
  font-size: 13px;
  color: #fff;
  background: rgba(0,0,0,0.35);
  border-radius: 10px;
  padding: 6px 10px;
  z-index: 5;
}

.controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; gap: 8px;
  padding: 10px calc(env(safe-area-inset-bottom, 10px) + 6px) calc(env(safe-area-inset-bottom, 10px) + 10px);
  background: rgba(0,0,0,0.55);
  z-index: 5;
}
.control-btn {
  flex: 1;
  padding: 12px 6px;
  border-radius: 12px;
  border: none;
  font-size: 13px;
  font-weight: 600;
}
.control-btn.primary { background: var(--tg-btn); color: var(--tg-btn-text); }
.control-btn.secondary { background: rgba(255,255,255,0.14); color: #fff; }
.control-btn.danger { background: rgba(248,113,113,0.2); color: var(--accent-bad); }

/* Summary */
#summary-screen {
  padding: 24px 18px;
  overflow-y: auto;
}
#summary-screen h2 { margin-top: env(safe-area-inset-top, 0); }
.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 16px 0;
}
.summary-card {
  background: rgba(255,255,255,0.06);
  border-radius: 14px;
  padding: 14px;
  text-align: center;
}
.summary-card .val { font-size: 26px; font-weight: 800; }
.summary-card .lbl { font-size: 12px; color: var(--tg-hint); margin-top: 4px; }
.sets-history { display: flex; flex-direction: column; gap: 8px; margin-bottom: 20px; }
.set-row {
  display: flex; justify-content: space-between;
  background: rgba(255,255,255,0.05);
  padding: 10px 14px; border-radius: 10px;
  font-size: 14px;
}

/* Modal */
.modal {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 20;
}
.modal-content {
  background: #1b1e25;
  width: 100%;
  border-radius: 20px 20px 0 0;
  padding: 20px;
  padding-bottom: calc(env(safe-area-inset-bottom, 12px) + 20px);
}
.modal-content h3 { margin-top: 0; }
.modal-content label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; color: var(--tg-hint);
  margin-bottom: 16px;
}
.modal-content input[type="range"] { width: 100%; }
.modal-content select {
  padding: 8px; border-radius: 8px; border: none;
  background: rgba(255,255,255,0.1); color: #fff;
}
