:root {
  /* Base Defaults (Light Mode) */
  --primary: #379434;
  --primary-dark: #2d7a2a;
  --bg: #F8F9FA;
  --card-bg: #FFFFFF;
  --text-main: #111827;
  --text-secondary: #6B7280;
  --error: #EF4444;
  --error-bg: #FEF2F2;
  --warning: #F59E0B;
  --warning-bg: #FFFBEB;
  --border: #E2E8F0;
  --bubble-bg: #FFFFFF;
  --bubble-border: rgba(55, 148, 52, 0.1);
}

[data-theme="dark"] {
  --primary: #45EA69;
  --primary-dark: #379434;
  --bg: #0A0D14;
  --card-bg: #20232D;
  --text-main: #F9FAFB;
  --text-secondary: #9CA3AF;
  --error: #F87171;
  --error-bg: #451a1a;
  --warning: #FBBF24;
  --warning-bg: #451a1a;
  --border: #31353F;
  --bubble-bg: #20232D;
  --bubble-border: rgba(69, 234, 105, 0.2);
}

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

body {
  background: var(--bg);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

/* Screen Management */
.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background: var(--bg);
  position: absolute;
  top: 0;
  left: 0;
  z-index: 10;
}

.screen.active {
  display: flex;
}

/* Common UI Elements */
.navbar {
  height: 56px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  background: var(--bg);
}

.back-arrow {
  font-size: 24px;
  margin-right: 16px;
  color: var(--text-main);
  cursor: pointer;
  user-select: none;
}

.nav-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-main);
}

.container {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 24px;
  padding-top: 20px;
  align-items: center;
  overflow-y: auto;
}

/* Theme-based Graphic Visibility */
.graphic-light { display: block; }
.graphic-dark { display: none; }

[data-theme="dark"] .graphic-light { display: none; }
[data-theme="dark"] .graphic-dark { display: block; }

/* Animated Feature Icon Container */
.feature-icon-container {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 0 auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Glow Rings */
.glow-ring {
  position: absolute;
  border-radius: 50%;
  background: rgba(55, 148, 52, 0.08);
}

[data-theme="dark"] .glow-ring {
  background: rgba(69, 234, 105, 0.08);
}

.glow-ring.outer {
  width: 180px;
  height: 180px;
  animation: pulse-glow 2.2s infinite ease-in-out;
}

.glow-ring.middle {
  width: 130px;
  height: 130px;
  background: rgba(55, 148, 52, 0.12);
}

[data-theme="dark"] .glow-ring.middle {
  background: rgba(69, 234, 105, 0.12);
}

/* Main Icon Circle */
.main-icon-circle {
  width: 96px;
  height: 96px;
  border-radius: 99px;
  padding: 24px;
  background: linear-gradient(135deg, var(--card-bg) 0%, var(--bg) 100%);
  border: 2px solid rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 25px rgba(55, 148, 52, 0.15);
  position: relative;
  z-index: 2;
  animation: pulse-icon 1.8s infinite ease-in-out;
}

[data-theme="dark"] .main-icon-circle {
  border: 2px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 10px 25px rgba(69, 234, 105, 0.1);
}

.main-icon-svg {
  width: 100%;
  height: 100%;
  stroke: var(--primary);
  stroke-width: 1.5;
}

/* Floating Bubbles */
.floating-bubble {
  position: absolute;
  background: var(--bubble-bg);
  border: 1px solid var(--bubble-border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 8px rgba(0,0,0,0.06);
  z-index: 3;
}

.floating-bubble svg {
  width: 50%;
  height: 50%;
  stroke: var(--primary);
  stroke-width: 1.5;
}

.bubble-1 { width: 32px; height: 32px; top: 20px; left: 30px; animation: float-1 2.4s infinite ease-in-out; }
.bubble-2 { width: 28px; height: 28px; top: 30px; right: 30px; animation: float-2 2.8s infinite ease-in-out 0.2s; }
.bubble-3 { width: 24px; height: 24px; bottom: 40px; right: 40px; animation: float-3 2.6s infinite ease-in-out 0.4s; }
.bubble-4 { width: 30px; height: 30px; bottom: 30px; left: 30px; animation: float-4 3.0s infinite ease-in-out 0.6s; }

/* Keyframes */
@keyframes pulse-glow {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.05); opacity: 1; }
}

@keyframes pulse-icon {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

@keyframes float-1 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
@keyframes float-2 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-12px); } }
@keyframes float-3 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes float-4 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-11px); } }

/* Buttons */
.btn {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s;
  margin-top: auto;
  margin-bottom: 24px;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.1), 0 2px 4px -1px rgba(16, 185, 129, 0.06);
}

[data-theme="dark"] .btn-primary {
  color: #0A0D14;
}

.btn-secondary {
  background: transparent;
  color: var(--text-secondary);
  margin-top: 0;
  margin-bottom: 16px;
}

.btn-danger {
  background: var(--error);
  color: white;
}

/* Step Progress Bar */
.step-indicator {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  width: 100%;
}

.step-indicator .step-label {
  float: right;
  color: var(--text-main);
  font-weight: 500;
}

.step-progress {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-bottom: 32px;
  position: relative;
  overflow: hidden;
}

.step-progress-bar {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  width: 33%;
  transition: width 0.3s ease;
}

.step-progress-bar.step-2 { width: 66%; }
.step-progress-bar.step-3 { width: 100%; }

/* Headings */
h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 12px;
  text-align: center;
}

p.subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: 32px;
  line-height: 1.5;
}

/* Instructions List */
.instructions {
  list-style: none;
  width: 100%;
  margin-bottom: 32px;
}

.instructions li {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  font-size: 14px;
  color: var(--text-main);
}

.instructions li::before {
  content: "";
  width: 6px;
  height: 6px;
  background: var(--primary);
  border-radius: 50%;
  margin-right: 12px;
  flex-shrink: 0;
}

/* Permission Screen */
.permission-icon {
  width: 64px;
  height: 64px;
  background: #ECFDF5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 60px 0 24px;
}

[data-theme="dark"] .permission-icon {
  background: rgba(69, 234, 105, 0.15);
}

.permission-icon svg { stroke: var(--primary); width: 32px; height: 32px; }

/* Capture Screen */
#screen-capture { background: black; }

#screen-capture .navbar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 20;
  background: transparent;
}

#screen-capture .back-arrow, #screen-capture .nav-title {
  color: white;
}

#video-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* Mirror video for front camera (user-facing) - feels natural */
  /* Note: Back camera (environment) should not be mirrored for document scanning */
  transform: scaleX(-1);
}

/* When using back camera, remove mirroring */
#video.no-mirror {
  transform: scaleX(1);
}

.status-pill {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  color: #111827;
  display: flex;
  align-items: center;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  z-index: 20;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  background: var(--primary);
  border-radius: 50%;
  margin-right: 8px;
  animation: blink 2s infinite;
}

.status-dot.error { background: var(--error); }
.status-dot.warning { background: var(--warning); }

@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

.frame-guide {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 85%;
  aspect-ratio: 1.58;
  max-width: 400px;
  z-index: 10;
}

.corner {
  position: absolute;
  width: 40px;
  height: 40px;
  border-color: rgba(255, 255, 255, 0.8);
  border-style: solid;
  border-width: 0;
  transition: all 0.3s;
}

.corner.tl { top: 0; left: 0; border-top-width: 4px; border-left-width: 4px; border-radius: 16px 0 0 0; }
.corner.tr { top: 0; right: 0; border-top-width: 4px; border-right-width: 4px; border-radius: 0 16px 0 0; }
.corner.bl { bottom: 0; left: 0; border-bottom-width: 4px; border-left-width: 4px; border-radius: 0 0 0 16px; }
.corner.br { bottom: 0; right: 0; border-bottom-width: 4px; border-right-width: 4px; border-radius: 0 0 16px 0; }

.frame-guide.success .corner { border-color: var(--primary); }
.frame-guide.error .corner { border-color: var(--error); }

.scan-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--primary);
  box-shadow: 0 0 10px var(--primary);
  top: 10%;
  opacity: 0;
  z-index: 11;
}

.scan-line.active {
  animation: scan 2s ease-in-out infinite;
  opacity: 1;
}

@keyframes scan {
  0% { top: 10%; opacity: 0; }
  10% { opacity: 1; }
  90% { opacity: 1; }
  100% { top: 90%; opacity: 0; }
}

/* Processing Screen */
.loader-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  width: 100%;
}

.loader-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 24px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
}

.segmented-loader {
  width: 120px;
  height: 120px;
  position: relative;
  margin: 20px 0 30px;
}

.segment {
  position: absolute;
  top: 0;
  left: 50%;
  width: 4px;
  height: 100%;
  margin-left: -2px;
  animation: fade 1.2s linear infinite;
}

.segment::before {
  content: "";
  display: block;
  width: 100%;
  height: 16px;
  border-radius: 2px;
  background: var(--primary);
}

@keyframes fade {
  0% { opacity: 1; }
  100% { opacity: 0.2; }
}

.loading-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  text-align: center;
}

.loading-text {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 30px;
  text-align: center;
}

/* Checklist */
.checklist {
  width: 100%;
  max-width: 280px;
}

.check-item {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 500;
  transition: all 0.3s;
}

.check-item.active { color: var(--text-main); }
.check-item.completed { color: var(--primary); }
.check-item.failed { color: var(--error); }

.check-icon {
  width: 24px;
  height: 24px;
  margin-right: 12px;
  border: 2px solid var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  font-size: 14px;
}

.check-item.active .check-icon {
  border-color: var(--primary);
  animation: pulse-border 1.5s infinite;
}

@keyframes pulse-border {
  0%, 100% { box-shadow: 0 0 0 0 rgba(55, 148, 52, 0.4); }
  50% { box-shadow: 0 0 0 4px rgba(55, 148, 52, 0); }
}

.check-item.completed .check-icon {
  background-color: var(--primary);
  border-color: var(--primary);
  color: white;
}

.check-item.failed .check-icon {
  background-color: var(--error);
  border-color: var(--error);
  color: white;
}

canvas { display: none; }

/* Result Screen */
.result-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 20px;
}

.result-icon {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 32px;
}

.result-icon.success { background: #ECFDF5; }
.result-icon.error { background: var(--error-bg); }
.result-icon.warning { background: var(--warning-bg); }

[data-theme="dark"] .result-icon.success { background: rgba(69, 234, 105, 0.15); }

.result-icon svg { width: 60px; height: 60px; stroke-width: 2.5; }
.result-icon.success svg { stroke: var(--primary); }
.result-icon.error svg { stroke: var(--error); }
.result-icon.warning svg { stroke: var(--warning); }

.result-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 12px;
  text-align: center;
}

.result-message {
  font-size: 15px;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.5;
  margin-bottom: 16px;
  max-width: 300px;
}

.error-code-badge {
  background: var(--error-bg);
  color: var(--error);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  font-family: monospace;
  margin-bottom: 24px;
}

[data-theme="dark"] .error-code-badge {
  background: rgba(239, 68, 68, 0.2);
}

/* Ready Status */
.ready-status {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 24px;
  margin-bottom: 32px;
}

.ready-dot {
  width: 8px;
  height: 8px;
  background: var(--primary);
  border-radius: 50%;
  margin-right: 8px;
}

/* Theme-based SVG visibility */
.id-graphic-light { display: block; }
.id-graphic-dark { display: none; }

[data-theme="dark"] .id-graphic-light { display: none; }
[data-theme="dark"] .id-graphic-dark { display: block; }

/* Error Screen Overlay */
#error-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  z-index: 200;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

#error-overlay.show { display: flex; }

/* Flash Effect */
#flash {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  z-index: 150;
  animation: flash-anim 0.3s ease-out forwards;
}

#flash.show { display: block; }

@keyframes flash-anim {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* Verification Steps in Processing */
.verification-section {
  width: 100%;
  max-width: 280px;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

/* Countdown Screen */
#screen-countdown {
  position: relative;
  overflow: hidden;
}

#countdown-video-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  overflow: hidden;
}

#countdown-video {
  position: absolute;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  transform: translate(-50%, -50%);
  object-fit: cover;
  filter: blur(20px) brightness(0.7);
  -webkit-filter: blur(20px) brightness(0.7);
}

/* Mirror front camera, no mirror for back camera */
#countdown-video.no-mirror {
  transform: translate(-50%, -50%);
}

#screen-countdown .navbar,
#screen-countdown .container {
  position: relative;
  z-index: 2;
}

#screen-countdown .navbar {
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.countdown-container {
  justify-content: center;
  align-items: center;
  text-align: center;
  background: transparent;
}

.countdown-circle {
  position: relative;
  width: 200px;
  height: 200px;
  margin-bottom: 40px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.countdown-ring {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.countdown-ring-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.2);
  stroke-width: 8;
}

.countdown-ring-progress {
  fill: none;
  stroke: var(--primary);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 565.48;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s linear;
  filter: drop-shadow(0 0 8px var(--primary));
}

.countdown-number {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 72px;
  font-weight: 700;
  color: #FFFFFF;
  text-shadow: 0 4px 12px rgba(0, 0, 0, 0.6);
  animation: countdown-pulse 1s ease-in-out infinite;
}

@keyframes countdown-pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.1); }
}

#countdownMessage {
  font-size: 24px;
  font-weight: 600;
  color: #FFFFFF;
  margin-bottom: 12px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 16px 24px;
  border-radius: 12px;
}

.countdown-subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.9);
  margin-top: 8px;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
}

/* Safe Area Support */
@supports (padding-top: env(safe-area-inset-top)) {
  .navbar { padding-top: env(safe-area-inset-top); }
  .container { padding-bottom: env(safe-area-inset-bottom); }
}
