/* --- Reset / base --- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #0F2E1E;
  color: #F5F5F5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden; /* uma tela por vez */
  overscroll-behavior: none;
}
input, button { font: inherit; color: inherit; }
button { border: none; background: none; cursor: pointer; }
a { color: #86efac; }

/* --- Screens --- */
.screen {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: none;
  flex-direction: column;
}
.screen.active { display: flex; }
.hidden { display: none !important; }

/* --- Topbar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(0,0,0,0.25);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  min-height: 56px;
}
.topbar h3 { margin: 0; font-size: 16px; font-weight: 600; }
.topbar > span { min-width: 40px; }

/* --- Buttons --- */
.btn {
  border-radius: 10px;
  padding: 12px 16px;
  font-weight: 600;
  transition: transform .05s ease, opacity .15s ease;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: #22c55e; color: #0F2E1E; }
.btn.danger  { background: #ef4444; color: white; }
.btn.ghost   { background: transparent; color: #F5F5F5; border: 1px solid rgba(255,255,255,0.2); }
.btn.big     { padding: 18px; font-size: 18px; width: 100%; }
.btn.small   { padding: 8px 12px; font-size: 13px; }
.btn.back    { padding: 8px 12px; }

/* --- Login --- */
#scr-login {
  justify-content: center;
  align-items: center;
  padding: 32px 20px;
  background: linear-gradient(180deg, #0F2E1E 0%, #052012 100%);
}
.brand { text-align: center; margin-bottom: 24px; }
.brand img { width: min(72vw, 320px); max-height: 130px; object-fit: contain; }
.tagline { margin: 8px 0 0; color: #86efac; font-size: 14px; letter-spacing: .5px; }
.card {
  width: 100%;
  max-width: 380px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 20px;
  display: grid;
  gap: 14px;
}
label { display: grid; gap: 6px; font-size: 13px; color: #d1d5db; }
input[type=text], input[type=password] {
  width: 100%;
  padding: 14px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.25);
  color: #fff;
  font-size: 16px;
}
input:focus { outline: 2px solid #22c55e; outline-offset: 1px; }
.err { color: #fca5a5; min-height: 18px; margin: 0; font-size: 13px; text-align: center; }

/* --- Home --- */
#scr-home { padding: 0 20px 20px; }
.hero { padding: 24px 4px 12px; }
.hero h2 { margin: 0 0 6px; font-size: 22px; }
.muted { color: #9ca3af; font-size: 14px; margin: 0; }
.section-actions { margin-top: 16px; }
#btn-start { margin-top: auto; margin-bottom: 20px; }

/* --- Truck --- */
#scr-truck { padding-bottom: 20px; }
.truck-grid {
  display: grid;
  gap: 12px;
  padding: 20px;
  grid-template-columns: 1fr 1fr;
}
.truck {
  padding: 26px 12px;
  border-radius: 14px;
  background: #1a4a30;
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  border: 2px solid transparent;
  min-height: 96px;
}
.truck:active { border-color: #22c55e; }
#outros-wrap { padding: 0 20px; display: grid; gap: 12px; }

/* --- Seleção de redes --- */
#scr-nets { padding-bottom: 20px; }
.nets-hint {
  padding: 8px 20px 12px;
  margin: 0;
  color: #9ca3af;
  font-size: 13px;
  line-height: 1.45;
}
.nets-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 4px 20px 100px;
}
.net-card {
  position: relative;
  padding: 18px 12px 14px;
  border-radius: 14px;
  background: #1a4a30;
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  border: 2px solid transparent;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  transition: transform .05s, border-color .12s, background-color .12s;
}
.net-card:active { transform: scale(0.98); }
.net-card.selected {
  border-color: #22c55e;
  background: #22553a;
}
.net-card .net-type {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: #86efac;
  opacity: 0.85;
}
.net-card .net-order {
  position: absolute;
  top: 6px; right: 8px;
  min-width: 22px; height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: #22c55e;
  color: #052e12;
  font-size: 12px;
  font-weight: 800;
  display: none;
  align-items: center;
  justify-content: center;
}
.net-card.selected .net-order { display: inline-flex; }
.nets-footer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  padding: 14px 20px calc(14px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(15,46,30,0) 0%, rgba(15,46,30,0.95) 40%, #0F2E1E 100%);
}
.nets-footer .btn:disabled { opacity: 0.4; }

/* --- Seletor de rede no scanner --- */
.col-stats {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  color: #86efac;
  font-weight: 600;
}
.col-stats > #col-count { flex: 0 0 auto; }
.net-selector { position: relative; flex: 1 1 auto; display: flex; justify-content: center; }
.col-net-badge {
  padding: 5px 14px;
  border-radius: 999px;
  background: rgba(34,197,94,0.18);
  color: #86efac;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.5px;
  border: 1px solid rgba(34,197,94,0.35);
  cursor: pointer;
  transition: background .12s;
}
.col-net-badge::after {
  content: ' ▾';
  font-size: 10px;
  opacity: 0.7;
}
.col-net-badge:active { background: rgba(34,197,94,0.35); }
.net-dropdown {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  min-width: 220px;
  max-width: 90vw;
  background: #163e28;
  border: 1px solid rgba(34,197,94,0.35);
  border-radius: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  z-index: 50;
  overflow: hidden;
}
.net-dropdown-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 14px 16px;
  font-size: 14px;
  font-weight: 600;
  color: #d1d5db;
  border: none;
  background: none;
  cursor: pointer;
  text-align: left;
  transition: background .1s;
}
.net-dropdown-item:active { background: rgba(34,197,94,0.15); }
.net-dropdown-item.active {
  color: #86efac;
  background: rgba(34,197,94,0.12);
}
.net-dropdown-item .net-check {
  width: 20px;
  text-align: center;
  font-size: 14px;
  flex-shrink: 0;
}
.net-dropdown-divider {
  height: 1px;
  background: rgba(255,255,255,0.08);
  margin: 0 12px;
}
.net-dropdown-add {
  display: block;
  width: 100%;
  padding: 14px 16px;
  font-size: 13px;
  font-weight: 600;
  color: #86efac;
  border: none;
  background: none;
  cursor: pointer;
  text-align: left;
}
.net-dropdown-add:active { background: rgba(34,197,94,0.1); }

/* --- Coleta --- */
#scr-collect { padding: 0; }
/* .screen.active já faz display:flex; aqui só definimos a orientação
   quando esse .screen específico está ativo — sem sobrescrever display:none. */
#scr-collect.active { flex-direction: column; }
#scr-collect .topbar { flex-shrink: 0; z-index: 3; }

#scanner-wrap {
  position: relative;
  width: 100%;
  flex: 1 1 auto;      /* ocupa TODO o espaço disponível entre topbar e o resto */
  min-height: 55vh;
  background: #000;
  overflow: hidden;
}
#video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;   /* preenche cortando bordas — sem faixa verde */
  display: block;
  background: #000;
}
.scan-frame {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 82%; height: 24%;
  border: 3px solid rgba(34,197,94,0.95);
  border-radius: 10px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.35);
  pointer-events: none;
  z-index: 2;
}
/* linha de scan animada — feedback de que a câmera está processando */
.scan-frame::after {
  content: '';
  position: absolute;
  left: 6%; right: 6%; top: 50%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #22c55e, transparent);
  box-shadow: 0 0 10px #22c55e;
  animation: scan-sweep 1.6s ease-in-out infinite;
}
@keyframes scan-sweep {
  0%   { transform: translateY(-40px); opacity: .3; }
  50%  { transform: translateY(0);     opacity: 1;  }
  100% { transform: translateY(40px);  opacity: .3; }
}

.scan-hint {
  position: absolute;
  bottom: 66px; left: 0; right: 0;
  text-align: center; color: #fff; margin: 0;
  font-size: 13px; text-shadow: 0 1px 3px rgba(0,0,0,.9);
  z-index: 2;
}
.scanner-toolbar {
  position: absolute;
  bottom: 10px; left: 10px; right: 10px;
  display: flex; justify-content: space-between; gap: 8px;
  z-index: 2;
}
.btn-sn {
  background: rgba(251,191,36,0.2) !important;
  border: 1px solid rgba(251,191,36,0.5) !important;
  color: #fbbf24 !important;
  font-weight: 700 !important;
}
.sn-code {
  color: #fbbf24 !important;
}
.torch-btn {
  position: absolute;
  top: 12px; right: 12px;
  width: 42px; height: 42px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 20px;
  display: none;   /* aparece quando o track suportar torch */
  align-items: center; justify-content: center;
  z-index: 3;
  border: 1px solid rgba(255,255,255,0.25);
}
.torch-btn.available { display: inline-flex !important; }
.torch-btn.on { background: #fde047; color: #000; }

/* Painel de diagnóstico no scanner — oculto por padrão. Só aparece se o
   app for aberto com ?debug=1 na URL (útil pra depurar problemas de câmera
   no futuro sem precisar mexer em código). */
.scan-debug {
  display: none;
  position: absolute;
  top: 12px; left: 12px;
  max-width: 62%;
  z-index: 3;
  background: rgba(0,0,0,0.65);
  color: #86efac;
  font: 500 11px/1.35 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(134,239,172,0.25);
  white-space: pre-wrap;
  word-break: break-all;
  pointer-events: none;
  min-height: 20px;
}
body.debug .scan-debug { display: block; }

.items {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}
.items li {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.items li .info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.items li .code { font-weight: 700; color: #86efac; font-size: 15px; }
.items li .loja { color: #d1d5db; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.items li .del { color: #f87171; padding: 8px; font-size: 20px; }

.fab {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #22c55e;
  color: #0F2E1E;
  font-size: 32px;
  font-weight: 700;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  display: none; /* aparece só quando scanner pausado */
}
.fab.show { display: block; }

/* --- Histórico --- */
#hist-list li .meta { color: #9ca3af; font-size: 12px; }
.hist-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.hist-resume { animation: pulse-green 1.5s ease-in-out infinite; }
@keyframes pulse-green { 0%,100% { box-shadow: none; } 50% { box-shadow: 0 0 10px rgba(34,197,94,0.5); } }
#hist-list li .status-badge {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}
.badge-em_andamento { background: #fbbf24; color: #78350f; }
.badge-concluida { background: #22c55e; color: #052e12; }
.badge-cancelada { background: #6b7280; color: white; }

/* --- Modais --- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal-card {
  background: #163e28;
  border-radius: 16px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  display: grid;
  gap: 14px;
}
/* --- Input com prefixo CAR --- */
.manual-input-row {
  display: flex;
  align-items: center;
  gap: 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.25);
}
.manual-prefix {
  padding: 14px 12px;
  font-size: 16px;
  font-weight: 700;
  color: #22c55e;
  background: rgba(34,197,94,0.15);
  border-right: 1px solid rgba(255,255,255,0.1);
  user-select: none;
  flex-shrink: 0;
}
.manual-input-row input {
  border: none !important;
  border-radius: 0 !important;
  background: transparent !important;
  flex: 1;
  min-width: 0;
}

.modal-card h3 { margin: 0; font-size: 18px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.code-badge {
  background: rgba(34,197,94,0.15);
  border: 1px solid rgba(34,197,94,0.4);
  padding: 10px 14px;
  border-radius: 10px;
  margin: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.code-badge span { color: #86efac; font-size: 12px; letter-spacing: 1px; }
.code-badge strong { font-size: 18px; letter-spacing: 1px; }

.loja-input-wrap {
  display: flex;
  align-items: stretch;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.25);
}
.loja-prefix {
  display: none;
  align-items: center;
  padding: 0 12px;
  background: rgba(34,197,94,0.18);
  color: #86efac;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.5px;
  border-right: 1px solid rgba(255,255,255,0.12);
  white-space: nowrap;
}
.loja-prefix.show { display: inline-flex; }
.loja-input-wrap input {
  flex: 1;
  border: none !important;
  background: transparent !important;
  padding: 14px !important;
  font-size: 16px;
  color: #fff;
}
.loja-input-wrap input:focus { outline: none; }
.loja-input-wrap:focus-within { outline: 2px solid #22c55e; outline-offset: 1px; }

/* --- Grid do modal Adicionar Rede --- */
.add-net-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  z-index: 200;
  animation: fadein .2s ease;
  max-width: 90vw;
  text-align: center;
  font-size: 14px;
}
@keyframes fadein { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }
