/* ============================================================
   Scanner Web – Estilos principais (tema escuro, mobile-first)
   ============================================================ */

/* ── Variáveis ──────────────────────────────────────────── */
:root {
  --bg:        #0B0F1A;
  --surface:   #131929;
  --surface2:  #1A2236;
  --border:    #232E45;
  --primary:   #3B82F6;
  --primary-h: #2563EB;
  --accent:    #00E678;
  --danger:    #EF4444;
  --warn:      #F59E0B;
  --text:      #E2E8F0;
  --text-muted:#7C8FA6;
  --radius:    12px;
  --radius-sm: 8px;
  --shadow:    0 4px 24px rgba(0,0,0,.4);
  --transition: 0.2s ease;
}

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

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
}

/* ── App wrapper ────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ── Header ─────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-logo {
  width: 32px; height: 32px;
  background: var(--primary);
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}

.app-title {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -.3px;
}

.app-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 1px;
}

/* ── Step indicator ──────────────────────────────────────── */
.steps-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

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

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

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

.step-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-left: 4px;
}

/* ── Seções de etapas ────────────────────────────────────── */
.step-section {
  display: flex;
  flex-direction: column;
  flex: 1;
}

/* ─────────────────────────────────────────────────────────
   ETAPA 1 – CAPTURA
   ───────────────────────────────────────────────────────── */
.camera-wrap {
  position: relative;
  background: #000;
  width: 100%;
  overflow: hidden;
  flex: 1;
  min-height: 0;
}

#camera-feed {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

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

/* Indicador de rotação sobre o preview */
.rotation-badge {
  position: absolute;
  top: 8px; right: 8px;
  background: rgba(0,0,0,.65);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
  pointer-events: none;
  display: none;
}

/* Grid de enquadramento */
.camera-grid {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.camera-grid::before,
.camera-grid::after {
  content: '';
  position: absolute;
  background: rgba(255,255,255,0.08);
}

.camera-grid::before {
  left: 33.33%; right: 33.33%;
  top: 0; bottom: 0;
}

.camera-grid::after {
  top: 33.33%; bottom: 33.33%;
  left: 0; right: 0;
}

/* Mensagem de erro de câmera */
#camera-error {
  position: absolute;
  inset: 0;
  background: var(--surface);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
}

#camera-error .icon { font-size: 48px; }
#camera-error p { color: var(--text-muted); font-size: 0.9rem; }

/* Barra de controles da câmera */
.capture-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  /* Empurra o conteúdo acima da barra de navegação do celular */
  padding-bottom: max(20px, env(safe-area-inset-bottom, 20px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  gap: 12px;
  flex-shrink: 0;
}

.btn-icon {
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.btn-icon:hover, .btn-icon:active {
  border-color: var(--primary);
  background: var(--primary);
}

/* Botão principal de captura */
.btn-capture-main {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: transparent;
  border: 4px solid #fff;
  cursor: pointer;
  position: relative;
  display: flex; align-items: center; justify-content: center;
  transition: transform var(--transition);
  touch-action: manipulation; /* elimina delay de 300ms no mobile */
  -webkit-tap-highlight-color: transparent;
}

.btn-capture-main::before {
  content: '';
  width: 52px; height: 52px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--transition);
}

.btn-capture-main:active { transform: scale(0.92); }
.btn-capture-main:active::before { transform: scale(0.88); }

/* ─────────────────────────────────────────────────────────
   ETAPA 2 – AJUSTE
   ───────────────────────────────────────────────────────── */
.adjust-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow-y: auto;
}

.preview-box {
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

#preview-canvas {
  display: block;
  max-width: 100%;
  border-radius: var(--radius-sm);
  touch-action: none;
  cursor: crosshair;
}

.controls-panel {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Seletor de filtros */
.filter-group {
  display: flex;
  gap: 8px;
}

.filter-btn {
  flex: 1;
  padding: 8px 6px;
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
}

.filter-btn.active {
  border-color: var(--primary);
  background: rgba(59,130,246,.15);
  color: var(--primary);
}

/* Prévia dos filtros (ícone) */
.filter-btn .ficon {
  display: block;
  font-size: 18px;
  margin-bottom: 3px;
}

/* Sliders */
.sliders-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 80px;
}

.slider-row input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 4px;
  border-radius: 4px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}

.slider-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
  cursor: pointer;
}

.slider-row input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid #fff;
  cursor: pointer;
}

.slider-value {
  font-size: 0.78rem;
  color: var(--text);
  min-width: 32px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Rotação */
.rotation-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.rotation-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 80px;
}

.btn-rotate {
  width: 40px; height: 40px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
}

.btn-rotate:hover, .btn-rotate:active {
  border-color: var(--primary);
  color: var(--primary);
}

/* Barra de ação inferior (ajuste) */
.adjust-actions {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom, 16px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ─────────────────────────────────────────────────────────
   ETAPA 3 – RESULTADO
   ───────────────────────────────────────────────────────── */
.result-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow-y: auto;
}

.result-preview {
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  flex-shrink: 0;
}

#result-canvas {
  display: block;
  max-width: 100%;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 14px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.result-actions .btn-full {
  grid-column: 1 / -1;
}

/* OCR section */
#ocr-section {
  margin: 14px 16px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.ocr-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.ocr-header h3 {
  font-size: 0.9rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

#ocr-text {
  padding: 14px;
  font-size: 0.88rem;
  line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 300px;
  overflow-y: auto;
}

/* ── Botões gerais ───────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px 18px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-h); }
.btn-primary:active:not(:disabled) { transform: scale(0.97); }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 2px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }

.btn-accent {
  background: var(--accent);
  color: #0B1A12;
}
.btn-accent:hover:not(:disabled) { filter: brightness(1.1); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 2px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { color: var(--text); border-color: var(--text-muted); }

.btn-sm { padding: 7px 12px; font-size: 0.8rem; }

/* ── Loader overlay ──────────────────────────────────────── */
#loader {
  position: fixed;
  inset: 0;
  background: rgba(11,15,26,.85);
  backdrop-filter: blur(4px);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 9999;
}

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

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

#loader-msg {
  font-size: 0.95rem;
  color: var(--text-muted);
}

/* ── Toast ───────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 0.88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
  z-index: 10000;
  max-width: 90vw;
  text-align: center;
  box-shadow: var(--shadow);
}

/* ── Utilitários ─────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Scrollbar customizada ───────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--surface); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Responsive – tablets / desktop ─────────────────────── */
@media (min-width: 640px) {
  .camera-wrap { max-height: 70vh; }

  .controls-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px 30px;
  }

  .filter-group { grid-column: 1 / -1; }

  .result-actions {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .result-actions .btn-full {
    grid-column: auto;
  }
}

@media (min-width: 1024px) {
  #app {
    max-width: 900px;
    margin: 0 auto;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    min-height: 100vh;
  }

  body { background: #060910; }
}

/* ── Animação de entrada ─────────────────────────────────── */
.step-section {
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Seleção de imagem / highlight ───────────────────────── */
::selection { background: rgba(59,130,246,.3); }
