/* Reset & Base */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #0a0a0a;
  --bg-secondary: #141414;
  --bg-card: #1a1a1a;
  --bg-card-hover: #222;
  --text-primary: #f0f0f0;
  --text-secondary: #999;
  --text-muted: #666;
  --accent: #00c853;
  --accent-dim: #00963e;
  --accent-glow: rgba(0, 200, 83, 0.3);
  --warning: #ff9800;
  --error: #f44336;
  --info: #2196f3;
  --border: #2a2a2a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 12px 16px;
  padding-top: calc(12px + var(--safe-top));
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.app-header h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.app-header .subtitle {
  font-size: 13px;
  color: var(--text-secondary);
}

/* Screens */
.screen {
  display: none;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

/* Upload Screen */
.upload-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
  padding-bottom: calc(24px + var(--safe-bottom));
  text-align: center;
  gap: 16px;
}

.upload-icon {
  color: var(--accent);
  margin-bottom: 4px;
}

.upload-area h2 {
  font-size: 24px;
  font-weight: 700;
}

.upload-area > p {
  color: var(--text-secondary);
  max-width: 300px;
  line-height: 1.5;
}

.upload-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 300px;
  margin-top: 8px;
}

/* Processing Mode Toggle */
.processing-mode-toggle {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 300px;
}

.mode-option {
  flex: 1;
  cursor: pointer;
}

.mode-option input[type="radio"] {
  display: none;
}

.mode-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 8px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: var(--bg-card);
  transition: all 0.2s;
}

.mode-option input[type="radio"]:checked + .mode-label {
  border-color: var(--accent);
  background: rgba(0, 200, 83, 0.08);
}

.mode-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-secondary);
  transition: color 0.2s;
}

.mode-option input[type="radio"]:checked + .mode-label .mode-name {
  color: var(--accent);
}

.mode-desc {
  font-size: 11px;
  color: var(--text-muted);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.btn-large {
  padding: 14px 24px;
  font-size: 16px;
  border-radius: var(--radius);
}

.btn-small {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 6px;
}

.btn-primary {
  background: var(--accent);
  color: #000;
}

.btn-primary:active {
  background: var(--accent-dim);
  transform: scale(0.97);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-secondary:active {
  background: var(--bg-card-hover);
  transform: scale(0.97);
}

.btn-icon {
  background: rgba(255,255,255,0.15);
  color: white;
  border: none;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-small.active {
  background: var(--accent);
  color: #000;
}

/* Tips */
.tips {
  text-align: left;
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  max-width: 340px;
  width: 100%;
}

.tips h3 {
  font-size: 13px;
  color: var(--accent);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.tips ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tips li {
  font-size: 13px;
  color: var(--text-secondary);
  padding-left: 16px;
  position: relative;
  line-height: 1.4;
}

.tips li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 7px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-dim);
}

/* Camera Screen */
.camera-container {
  flex: 1;
  position: relative;
  background: #000;
  display: flex;
  flex-direction: column;
}

#camera-preview {
  flex: 1;
  width: 100%;
  object-fit: cover;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.guide-silhouette {
  position: absolute;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  width: 40%;
  height: 70%;
  border: 2px dashed rgba(255,255,255,0.2);
  border-radius: 20px;
}

.camera-controls {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
  background: rgba(0,0,0,0.6);
}

.btn-record-circle {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 4px solid white;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.record-inner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #f44336;
  transition: all 0.2s;
}

.btn-record-circle.recording .record-inner {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.record-timer {
  position: absolute;
  top: calc(16px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(244, 67, 54, 0.9);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Processing Screen */
.processing-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 16px;
  text-align: center;
}

.processing-animation {
  margin-bottom: 8px;
}

.swing-loader {
  position: relative;
  width: 80px;
  height: 80px;
}

.loader-ring {
  width: 80px;
  height: 80px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.processing-container h2 {
  font-size: 20px;
}

.processing-container > p {
  color: var(--text-secondary);
  font-size: 14px;
}

.progress-bar {
  width: 100%;
  max-width: 300px;
  height: 6px;
  background: var(--bg-card);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 14px;
  color: var(--accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.processing-steps {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
  text-align: left;
}

.step {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--text-muted);
  transition: color 0.3s;
}

.step.active {
  color: var(--text-primary);
}

.step.done {
  color: var(--accent);
}

.step .step-icon {
  font-size: 14px;
  width: 20px;
  text-align: center;
}

.step.active .step-icon::after {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 2px solid var(--accent);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.step.active .step-icon {
  font-size: 0;
}

.step.done .step-icon {
  color: var(--accent);
}

/* Results Screen */
.results-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(24px + var(--safe-bottom));
}

.video-player-container {
  position: relative;
  background: #000;
  width: 100%;
  flex-shrink: 0;
}

.video-overlay-wrapper {
  position: relative;
  width: 100%;
  line-height: 0; /* remove gap below inline video */
}

#result-video {
  width: 100%;
  display: block;
}

#overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.video-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg-secondary);
}

.btn-play {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.scrubber {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

.scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.video-time {
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  min-width: 36px;
  text-align: right;
}

.playback-controls {
  display: flex;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.overlay-toggles {
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 20px;
  background: var(--bg-card);
}

.toggle input {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
}

.toggle input:checked + span {
  color: var(--accent);
}

/* Phase Indicator */
.phase-indicator {
  padding: 12px;
  background: var(--bg-secondary);
}

.phase-bar {
  display: flex;
  gap: 2px;
  border-radius: 6px;
  overflow: hidden;
}

.phase {
  flex: 1;
  text-align: center;
  padding: 6px 2px;
  font-size: 9px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  background: var(--bg-card);
  color: var(--text-muted);
  transition: all 0.3s;
}

.phase.active {
  background: var(--accent);
  color: #000;
}

.phase.passed {
  background: var(--accent-dim);
  color: rgba(0,0,0,0.7);
}

/* Metrics Dashboard */
.metrics-dashboard {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.metrics-dashboard h3 {
  font-size: 18px;
  font-weight: 700;
}

.score-card {
  display: flex;
  justify-content: center;
  padding: 20px;
}

.overall-score {
  text-align: center;
}

.score-value {
  font-size: 56px;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}

.score-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.metric-card {
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  padding: 14px;
  text-align: center;
  border: 1px solid var(--border);
}

.metric-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.metric-label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.metric-unit {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 1px;
}

.metrics-section {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  border: 1px solid var(--border);
}

.metrics-section h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.metrics-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.metric-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}

.metric-row:last-child {
  border-bottom: none;
}

.metric-name {
  font-size: 13px;
  color: var(--text-secondary);
}

.metric-val {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* AI Notes */
.ai-notes {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.ai-notes p {
  margin-bottom: 8px;
}

.ai-notes p:last-child {
  margin-bottom: 0;
}

.ai-note-good {
  color: var(--accent);
}

.ai-note-warn {
  color: var(--warning);
}

.ai-note-bad {
  color: var(--error);
}

/* Result Actions */
.result-actions {
  display: flex;
  gap: 12px;
  padding: 16px;
  flex-shrink: 0;
}

.result-actions .btn {
  flex: 1;
}

/* History Section */
.history-section {
  width: 100%;
  max-width: 400px;
  margin-top: 8px;
}

.history-section h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
  padding: 0 4px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: pointer;
  transition: background 0.2s;
}

.history-item:active {
  background: var(--bg-card-hover);
}

.history-thumb {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  object-fit: cover;
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.history-info {
  flex: 1;
  min-width: 0;
}

.history-date {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.history-score {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.history-stats {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-delete {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.history-delete:active {
  background: rgba(244, 67, 54, 0.2);
  color: var(--error);
}

/* Auth Screen */
.auth-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 20px;
  text-align: center;
  gap: 16px;
  max-width: 380px;
  margin: 0 auto;
  width: 100%;
}

.auth-container h2 {
  font-size: 22px;
  font-weight: 700;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.4;
}

.auth-tabs {
  display: flex;
  width: 100%;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
}

.auth-tab {
  flex: 1;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.auth-tab.active {
  background: var(--accent);
  color: #000;
}

.auth-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
}

.auth-field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.auth-field .optional {
  text-transform: none;
  font-weight: 400;
  color: var(--text-muted);
}

.auth-field input {
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}

.auth-field input:focus {
  border-color: var(--accent);
}

.auth-error {
  background: rgba(244, 67, 54, 0.15);
  color: var(--error);
  padding: 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  text-align: left;
}

.auth-submit {
  margin-top: 4px;
}

.auth-back-link {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 8px;
  text-decoration: underline;
}

.auth-back-link:hover {
  color: var(--text-secondary);
}

/* Header Auth */
.header-auth {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.user-email {
  font-size: 12px;
  color: var(--text-secondary);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-logout {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-logout:hover {
  border-color: var(--error);
  color: var(--error);
}

/* Upgrade Banner */
.upgrade-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px;
  background: rgba(244, 67, 54, 0.15);
  color: var(--error);
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}

.btn-upgrade {
  background: var(--error);
  color: white;
  border: none;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.btn-upgrade:hover {
  opacity: 0.85;
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  color: white;
  z-index: 9999;
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast-show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast-success { background: var(--accent-dim); }
.toast-error { background: var(--error); }
.toast-warn { background: #e65100; }
.toast-info { background: #1565c0; }

/* Utility */
.hidden {
  display: none !important;
}

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.screen.active {
  animation: fadeIn 0.3s ease;
}

/* Landscape adjustments */
@media (orientation: landscape) and (max-height: 500px) {
  .camera-controls {
    padding: 10px;
  }
  .btn-record-circle {
    width: 56px;
    height: 56px;
  }
  .record-inner {
    width: 40px;
    height: 40px;
  }
}

/* Tablet and above */
@media (min-width: 768px) {
  .metrics-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  .upload-area {
    justify-content: center;
  }
  .results-container {
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
  }
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}
