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

:root {
  --bg: #1a1a2e;
  --bg-light: #16213e;
  --surface: #0f3460;
  --primary: #6c63ff;
  --primary-light: #8b83ff;
  --accent: #e94560;
  --text: #f0f0f0;
  --text-muted: #a0a0b0;
  --success: #4ecdc4;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* === Screens === */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}
.screen.active { display: flex; }

/* === Auth === */
#auth-screen {
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, var(--bg) 0%, var(--bg-light) 100%);
}
.auth-container {
  text-align: center;
  padding: 2rem;
}
.logo { margin-bottom: 1.5rem; }
.auth-container h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}
.subtitle {
  color: var(--text-muted);
  font-size: 1rem;
  margin-bottom: 2rem;
}
.auth-error {
  color: var(--accent);
  font-size: 0.875rem;
  display: none;
  margin-top: 1rem;
}
.auth-error.visible { display: block; }
.auth-container.loading .logo svg {
  animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1); opacity: 0.7; }
}

/* === Header === */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: max(0.75rem, env(safe-area-inset-top));
  background: var(--bg-light);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  z-index: 10;
}
header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}
.user-badge {
  background: var(--primary);
  color: #fff;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
}
.btn-icon {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 50%;
  transition: background 0.2s;
}
.btn-icon:active { background: rgba(255,255,255,0.1); }

/* === Camera === */
.camera-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}
#camera-feed {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.scan-frame {
  width: 240px;
  height: 240px;
  border: 2px solid rgba(108, 99, 255, 0.6);
  border-radius: 16px;
  position: relative;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.3);
}
.scan-frame::before {
  content: '';
  position: absolute;
  top: -2px; left: -2px; right: -2px; bottom: -2px;
  border-radius: 16px;
  background: linear-gradient(0deg, transparent 48%, var(--primary) 50%, transparent 52%);
  background-size: 100% 200%;
  animation: scanline 2.5s ease-in-out infinite;
  opacity: 0.5;
}
@keyframes scanline {
  0%, 100% { background-position: 0 0; }
  50% { background-position: 0 100%; }
}
.qr-status {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  padding: 0.5rem 1rem;
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}
.qr-status.success {
  background: rgba(78, 205, 196, 0.2);
  color: var(--success);
}

/* === Controls === */
.controls {
  display: flex;
  justify-content: center;
  padding: 1.25rem;
  padding-bottom: max(1.25rem, var(--safe-bottom));
  background: var(--bg-light);
}
.btn-capture {
  background: none;
  border: none;
  cursor: pointer;
  transition: transform 0.15s;
}
.btn-capture:active { transform: scale(0.9); }

/* === Modal === */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--bg-light);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 1.5rem;
  padding-bottom: max(1.5rem, var(--safe-bottom));
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
}
.modal-content h3 {
  font-size: 1.2rem;
  margin-bottom: 1rem;
}
.preview-container {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 1rem;
  background: #000;
  max-height: 200px;
}
.preview-container img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  max-height: 200px;
}
.qr-data {
  background: rgba(0,0,0,0.3);
  padding: 0.75rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.8rem;
}
.qr-data.hidden { display: none; }
.qr-data pre {
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

/* === Extracted Info === */
.extracted-info {
  background: rgba(108, 99, 255, 0.08);
  border: 1px solid rgba(108, 99, 255, 0.25);
  border-radius: 8px;
  padding: 0.75rem;
  margin-bottom: 1rem;
}
.extracted-info.hidden { display: none; }
.extracted-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.extracted-badge {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-weight: 700;
  background: rgba(108, 99, 255, 0.25);
  color: var(--primary-light);
}
.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(108, 99, 255, 0.3);
  border-top-color: var(--primary-light);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.spinner.hidden { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.extracted-fields {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.extracted-row {
  display: flex;
  gap: 0.5rem;
  font-size: 0.8rem;
  line-height: 1.4;
}
.extracted-row .label {
  color: var(--text-muted);
  min-width: 60px;
  flex-shrink: 0;
}
.extracted-row .value {
  color: var(--text);
  word-break: break-word;
}
.extracted-error {
  font-size: 0.75rem;
  color: var(--accent);
  margin-top: 0.25rem;
}
.extracted-error.hidden { display: none; }

/* === Form === */
.form-group {
  margin-bottom: 1rem;
}
.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}
.toggle-group {
  display: flex;
  gap: 0.5rem;
}
.toggle-btn {
  flex: 1;
  padding: 0.6rem;
  border: 2px solid var(--surface);
  background: transparent;
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.toggle-btn.active {
  border-color: var(--primary);
  background: rgba(108, 99, 255, 0.15);
  color: var(--primary-light);
}
input[type="text"] {
  width: 100%;
  padding: 0.7rem;
  border: 2px solid var(--surface);
  background: rgba(0,0,0,0.2);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s;
}
input[type="text"]:focus {
  border-color: var(--primary);
}
input[type="text"]::placeholder {
  color: var(--text-muted);
  opacity: 0.5;
}

/* === Buttons === */
.modal-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}
.btn {
  flex: 1;
  padding: 0.75rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn:active { opacity: 0.8; }
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-secondary {
  background: var(--surface);
  color: var(--text-muted);
}

/* === Leads List === */
#leads-screen {
  background: var(--bg);
}
.leads-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
}
.lead-count {
  background: var(--primary);
  color: #fff;
  min-width: 24px;
  height: 24px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0 0.5rem;
}
.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 1rem;
}
.lead-card {
  background: var(--bg-light);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 0.75rem;
  border: 1px solid rgba(255,255,255,0.05);
}
.lead-card .lead-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}
.lead-card .lead-type {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-weight: 600;
}
.lead-type.qr { background: rgba(108,99,255,0.2); color: var(--primary-light); }
.lead-type.photo { background: rgba(78,205,196,0.2); color: var(--success); }
.lead-card .lead-time {
  font-size: 0.75rem;
  color: var(--text-muted);
}
.lead-card .lead-contact {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.25rem;
}
.lead-card .lead-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.lead-card .lead-note {
  font-size: 0.8rem;
  color: var(--text);
  margin-top: 0.25rem;
  font-style: italic;
}
.lead-thumb {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
  float: right;
  margin-left: 0.75rem;
}
.sync-status {
  font-size: 0.65rem;
  margin-top: 0.4rem;
}
.sync-status.synced { color: var(--success); }
.sync-status.pending { color: #f0ad4e; }
.sync-status.failed { color: var(--accent); }

/* === Toast === */
.toast {
  position: fixed;
  bottom: 5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  color: var(--text);
  padding: 0.75rem 1.25rem;
  border-radius: 999px;
  font-size: 0.85rem;
  z-index: 200;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  transition: opacity 0.3s, transform 0.3s;
}
.toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(1rem);
  pointer-events: none;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--accent); }

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

/* === Landscape adjustments === */
@media (orientation: landscape) and (max-height: 500px) {
  .scan-frame { width: 180px; height: 180px; }
  .controls { padding: 0.5rem; }
}
