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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface-2: #232340;
  --primary: #4f6ef7;
  --primary-hover: #3d5ce5;
  --text: #e8e8f0;
  --text-secondary: #9595b0;
  --border: #2e2e4a;
  --success: #4ade80;
  --danger: #f87171;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tg-viewport-height: 100vh;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  width: 100%;
  height: var(--tg-viewport-height, 100vh);
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* === Screen transitions === */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transform: translateX(30px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}

.screen.exit-left {
  opacity: 0;
  transform: translateX(-30px);
}

/* === Screen 1: Selection === */
.screen-header {
  padding: 24px 20px 0;
  flex-shrink: 0;
}

.screen-header h1 {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-top: 4px;
}

/* Search */
.search-wrap {
  position: relative;
  padding: 16px 20px 8px;
  flex-shrink: 0;
}

.search-wrap input {
  width: 100%;
  padding: 12px 12px 12px 42px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}

.search-wrap input:focus {
  border-color: var(--primary);
}

.search-wrap input::placeholder {
  color: var(--text-secondary);
}

.search-icon {
  position: absolute;
  left: 34px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

/* Country list */
.country-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 20px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.country-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
  user-select: none;
}

.country-item:active,
.country-item.selected {
  background: var(--surface-2);
}

.country-item.selected {
  outline: 2px solid var(--primary);
}

.country-flag {
  font-size: 26px;
  line-height: 1;
  flex-shrink: 0;
  width: 34px;
  text-align: center;
}

.country-name {
  font-size: 15px;
  font-weight: 500;
}

/* Document type */
.doc-type-section {
  flex-shrink: 0;
  padding: 12px 20px 0;
}

.doc-type-label {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.doc-type-toggle {
  display: flex;
  gap: 10px;
}

.doc-type-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 10px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}

.doc-type-btn.active {
  border-color: var(--primary);
  color: var(--text);
  background: rgba(79, 110, 247, 0.1);
}

.doc-type-btn svg {
  flex-shrink: 0;
}

/* Primary button */
.btn-primary {
  margin: 16px 20px;
  padding: 16px;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
  flex-shrink: 0;
}

.btn-primary:active {
  background: var(--primary-hover);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* === Screen 2 & 3: Camera screens === */
.capture-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px 12px;
  flex-shrink: 0;
}

.btn-back {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: none;
  border-radius: 50%;
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

.btn-back:active {
  background: var(--surface-2);
}

.capture-title h2 {
  font-size: 18px;
  font-weight: 700;
}

.capture-title p {
  font-size: 13px;
  color: var(--text-secondary);
}

/* Camera container */
.camera-container {
  flex: 1;
  position: relative;
  margin: 0 20px;
  border-radius: 16px;
  overflow: hidden;
  background: #000;
  min-height: 0;
}

.camera-container video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Document overlay */
.doc-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.doc-cutout {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 88%;
  aspect-ratio: 1.586 / 1; /* standard ID card ratio */
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.65);
}

/* Corner markers */
.doc-corners {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 88%;
  aspect-ratio: 1.586 / 1;
  transform: translate(-50%, -50%);
  z-index: 3;
  pointer-events: none;
}

.corner {
  position: absolute;
  width: 24px;
  height: 24px;
  border-color: var(--primary);
  border-style: solid;
}

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

/* Selfie overlay */
.selfie-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.selfie-cutout {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 240px;
  height: 240px;
  border-radius: 50%;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
  border: 3px solid var(--primary);
  animation: pulse-ring 2s ease-in-out infinite;
}

@keyframes pulse-ring {
  0%, 100% { border-color: var(--primary); }
  50% { border-color: rgba(79, 110, 247, 0.4); }
}

.selfie-container video {
  transform: scaleX(-1); /* mirror front camera */
}

/* Capture hint */
.capture-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  font-size: 13px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.hint-icon {
  font-size: 16px;
  color: var(--primary);
}

/* Shutter button */
.btn-shutter {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: none;
  background: transparent;
  cursor: pointer;
  position: relative;
  margin: 12px auto 20px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.shutter-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 4px solid #fff;
}

.shutter-inner {
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s ease, background 0.15s;
}

.btn-shutter:active .shutter-inner {
  transform: scale(0.85);
  background: #ccc;
}

/* Step indicator (front/back dots for ID card) */
.step-indicator {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding-bottom: 12px;
  flex-shrink: 0;
}

.step-indicator.hidden {
  display: none;
}

.step-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
  transition: background 0.3s;
}

.step-dot.active {
  background: var(--primary);
}

.step-dot.done {
  background: var(--success);
}

/* Liveness status */
.liveness-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px 20px 24px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2.5px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* === Screen 4: Complete === */
.complete-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 32px;
  text-align: center;
}

.checkmark-circle {
  margin-bottom: 24px;
  animation: pop-in 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes pop-in {
  0% { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.complete-content h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 12px;
}

.complete-desc {
  color: var(--text-secondary);
  font-size: 15px;
  line-height: 1.6;
}

/* === Flash effect on photo capture === */
.flash {
  animation: flash-anim 0.3s ease-out;
}

@keyframes flash-anim {
  0% { filter: brightness(3); }
  100% { filter: brightness(1); }
}

/* === Scrollbar styling === */
.country-list::-webkit-scrollbar {
  width: 4px;
}

.country-list::-webkit-scrollbar-track {
  background: transparent;
}

.country-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* === Telegram Mini App adjustments === */
@supports (padding: env(safe-area-inset-top)) {
  .screen {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
}
