:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --accent: #10b981;
  --accent-dark: #059669;
  --danger: #ef4444;
  --danger-dark: #dc2626;
  --warning: #f59e0b;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-300: #cbd5e1;
  --gray-400: #94a3b8;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1e293b;
  --gray-900: #0f172a;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 12px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-family);
  background-color: var(--gray-100);
  color: var(--gray-900);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(75px + var(--safe-bottom));
}

/* Header */
.app-header {
  background: #ffffff;
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.header-inner {
  max-width: 900px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-logo {
  background: linear-gradient(135deg, var(--primary), #4f46e5);
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
}

.brand-text h1 {
  font-size: 17px;
  font-weight: 700;
  color: var(--gray-900);
  line-height: 1.2;
}

.brand-text p {
  font-size: 11px;
  color: var(--gray-600);
}

.header-status {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sql-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 9999px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  background: var(--gray-100);
  border: 1px solid var(--gray-300);
  color: var(--gray-700);
  transition: all 0.2s;
}

.sql-badge.connected {
  background: #ecfdf5;
  color: #065f46;
  border-color: #a7f3d0;
}

.sql-badge.disconnected {
  background: #fef2f2;
  color: #991b1b;
  border-color: #fecaca;
}

.sql-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gray-400);
}

.sql-badge.connected .sql-dot {
  background: #10b981;
  box-shadow: 0 0 6px #10b981;
}

.sql-badge.disconnected .sql-dot {
  background: #ef4444;
}

.user-pill {
  font-size: 12px;
  color: var(--gray-700);
  background: var(--gray-100);
  padding: 4px 8px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-logout {
  background: none;
  border: none;
  color: var(--gray-600);
  cursor: pointer;
  padding: 4px 6px;
  font-size: 14px;
}
.btn-logout:hover {
  color: var(--danger);
}

/* Conteúdo Principal */
.app-main {
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 12px 14px;
  flex: 1;
}

.view-section {
  display: none;
}
.view-section.active {
  display: block;
}

/* Barra de Navegação Inferior (Mobile Friendly) */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  border-top: 1px solid var(--gray-200);
  display: flex;
  justify-content: space-around;
  padding: 6px 0 calc(6px + var(--safe-bottom));
  z-index: 40;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
}

.nav-item {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  color: var(--gray-600);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: all 0.15s;
  flex: 1;
  max-width: 110px;
}

.nav-item .nav-icon {
  font-size: 20px;
  line-height: 1;
}

.nav-item.active {
  color: var(--primary);
  font-weight: 700;
}

.nav-item.active .nav-icon {
  transform: scale(1.1);
}

/* Cards & Containers */
.card {
  background: #ffffff;
  border-radius: 14px;
  border: 1px solid var(--gray-200);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.03);
}

.card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--gray-900);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Picking Dashboard Stats */
.picking-stats-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.stat-box {
  background: #ffffff;
  border: 1px solid var(--gray-200);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.stat-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}
.stat-icon.blue { background: var(--primary-light); color: var(--primary); }
.stat-icon.green { background: #d1fae5; color: var(--accent-dark); }

.stat-info .stat-num {
  font-size: 22px;
  font-weight: 800;
  line-height: 1.1;
  color: var(--gray-900);
}
.stat-info .stat-lbl {
  font-size: 11px;
  color: var(--gray-600);
  text-transform: uppercase;
  font-weight: 600;
}

/* Leitor e Input de Código */
.scanner-input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-row {
  display: flex;
  gap: 8px;
}

.input-code {
  flex: 1;
  font-size: 16px;
  padding: 14px 14px;
  border: 2px solid var(--gray-300);
  border-radius: 12px;
  font-weight: 600;
  outline: none;
  transition: border-color 0.15s;
  text-transform: uppercase;
}

.input-code:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.btn-cam {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 12px;
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

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

.qty-control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 4px;
}

.qty-selector {
  display: flex;
  align-items: center;
  background: var(--gray-100);
  border-radius: 10px;
  padding: 3px;
  gap: 4px;
}

.btn-qty-step {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: #ffffff;
  color: var(--gray-800);
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.input-qty {
  width: 50px;
  height: 36px;
  border: none;
  background: transparent;
  font-size: 18px;
  font-weight: 800;
  text-align: center;
  color: var(--gray-900);
  outline: none;
}

.quick-qty-pills {
  display: flex;
  gap: 6px;
}

.pill-qty {
  background: var(--gray-200);
  border: none;
  color: var(--gray-800);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.pill-qty.active {
  background: var(--primary);
  color: white;
}

.btn-add-item {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-weight: 700;
  font-size: 14px;
  padding: 10px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s;
}

.btn-add-item:active {
  background: var(--accent-dark);
}

/* Leitor de Câmara Container */
#cameraScannerContainer,
#cameraScannerContainerSearch,
.camera-scanner-box {
  display: none;
  margin-top: 10px;
  background: #000;
  border-radius: 14px;
  overflow: hidden;
  position: relative;
}

#cameraScannerContainer.active,
#cameraScannerContainerSearch.active,
.camera-scanner-box.active {
  display: block;
}

#reader,
#readerSearch {
  width: 100%;
}

.camera-controls {
  padding: 8px;
  background: rgba(0,0,0,0.8);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.btn-config-pill {
  transition: all 0.2s ease;
  user-select: none;
}

.btn-config-pill:hover {
  background: #e2e8f0 !important;
  border-color: var(--primary) !important;
}

.btn-config-pill:active {
  transform: scale(0.97);
}

.supplier-card-box {
  transition: border-color 0.2s;
}

.supplier-card-box:hover {
  border-color: #94a3b8 !important;
}

/* Feedback do último artigo lido */
.last-item-card {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 12px;
  animation: fadeIn 0.25s ease;
}

.last-item-card.error {
  background: #fef2f2;
  border-color: #fecaca;
}

.last-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.last-item-title {
  font-size: 15px;
  font-weight: 700;
  color: #166534;
}
.last-item-card.error .last-item-title {
  color: #991b1b;
}

.badge-ref {
  background: #dcfce7;
  color: #166534;
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
}

.warehouse-badges {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.wh-badge {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--gray-700);
}

.wh-badge span {
  font-weight: 800;
  color: var(--primary);
}

/* Lista de Itens do Picking */
.picking-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.picking-item-row {
  background: #ffffff;
  border: 1px solid var(--gray-200);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.02);
}

.item-left {
  flex: 1;
  min-width: 0;
}

.item-ref-line {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}

.item-ref {
  font-weight: 700;
  font-size: 14px;
  color: var(--gray-900);
}

.item-barcode {
  font-size: 11px;
  color: var(--gray-600);
  background: var(--gray-100);
  padding: 1px 6px;
  border-radius: 4px;
}

.item-desc {
  font-size: 12px;
  color: var(--gray-600);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-wh-summary {
  font-size: 10px;
  color: var(--gray-400);
  margin-top: 3px;
}

.item-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.item-qty-actions {
  display: flex;
  align-items: center;
  background: var(--gray-100);
  border-radius: 8px;
  padding: 2px;
}

.btn-mini-step {
  width: 28px;
  height: 28px;
  border: none;
  background: white;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.item-qty-val {
  min-width: 32px;
  text-align: center;
  font-size: 15px;
  font-weight: 800;
  color: var(--primary-dark);
}

.btn-del-item {
  background: none;
  border: none;
  color: var(--gray-400);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}
.btn-del-item:hover {
  color: var(--danger);
}

/* Ações Rápidas de Picking (Rodapé da página ou cabeçalho) */
.picking-actions-bar {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.btn-action {
  flex: 1;
  min-width: 130px;
  padding: 12px;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s;
}

.btn-save {
  background: var(--primary);
  color: white;
}
.btn-save:hover { background: var(--primary-dark); }

.btn-export {
  background: #059669;
  color: white;
}
.btn-export:hover { background: #047857; }

.btn-clear {
  background: var(--gray-200);
  color: var(--gray-700);
  flex: 0 0 auto;
  min-width: 44px;
}
.btn-clear:hover { background: #fee2e2; color: var(--danger); }

/* Histórico de Leituras */
.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.btn-danger-outline {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.btn-danger-outline:hover {
  background: #fef2f2;
}

.history-card {
  background: #ffffff;
  border: 1px solid var(--gray-200);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.history-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--gray-900);
}

.history-date {
  font-size: 11px;
  color: var(--gray-600);
}

.history-meta {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: var(--gray-600);
}

.history-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
  border-top: 1px solid var(--gray-100);
  padding-top: 8px;
}

.btn-hist-btn {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--gray-300);
  border-radius: 8px;
  background: #ffffff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.btn-hist-btn.export {
  background: #ecfdf5;
  color: #065f46;
  border-color: #a7f3d0;
}
.btn-hist-btn.open {
  background: var(--primary-light);
  color: var(--primary-dark);
  border-color: #bfdbfe;
}
.btn-hist-btn.delete {
  background: #fef2f2;
  color: #991b1b;
  border-color: #fecaca;
  flex: 0 0 auto;
}

/* Formulário de Configuração */
.form-group {
  margin-bottom: 14px;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--gray-700);
  margin-bottom: 5px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-control {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--gray-300);
  border-radius: 10px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
.form-control:focus {
  border-color: var(--primary);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.form-hint {
  font-size: 11px;
  color: var(--gray-600);
  margin-top: 4px;
}

.instance-select-wrapper {
  display: flex;
  gap: 8px;
}

/* Modais */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(2px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-overlay.active {
  display: flex;
}

.modal-content {
  background: #ffffff;
  border-radius: 16px;
  width: 100%;
  max-width: 450px;
  padding: 20px;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.15);
  animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-title {
  font-size: 18px;
  font-weight: 800;
  margin-bottom: 12px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

/* Toast Notificações */
.toast-container {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 90%;
  max-width: 380px;
  pointer-events: none;
}

.toast {
  background: var(--gray-900);
  color: white;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 10px 15px -3px rgba(0,0,0,0.2);
  display: flex;
  align-items: center;
  gap: 8px;
  animation: slideDown 0.2s ease;
  pointer-events: auto;
}
.toast.success { background: #059669; }
.toast.error { background: #dc2626; }
.toast.warning { background: #d97706; }

/* QR Code Modal Box */
.qr-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 0;
}

.qr-image {
  width: 220px;
  height: 220px;
  border-radius: 12px;
  border: 1px solid var(--gray-200);
}

.qr-url {
  font-family: monospace;
  font-size: 14px;
  background: var(--gray-100);
  padding: 8px 12px;
  border-radius: 8px;
  margin-top: 12px;
  color: var(--primary);
  font-weight: bold;
  word-break: break-all;
  text-align: center;
}

/* Animações */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes modalPop {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Estado Vazio */
.empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--gray-600);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 8px;
  opacity: 0.6;
}

.empty-text {
  font-size: 14px;
  font-weight: 600;
}

.empty-sub {
  font-size: 12px;
  margin-top: 4px;
}

/* =========================================================
   OTIMIZAÇÕES RESPONSIVAS MOBILE (Smartphones / Safari iOS / Android)
   ========================================================= */
@media (max-width: 640px) {
  :root {
    --safe-bottom: env(safe-area-inset-bottom, 16px);
  }

  body {
    padding-bottom: calc(72px + var(--safe-bottom));
  }

  /* Header Compacto */
  .header-inner {
    padding: 7px 10px;
  }

  .brand-logo {
    width: 28px;
    height: 28px;
    font-size: 14px;
    border-radius: 6px;
  }

  .brand-text h1 {
    font-size: 14px;
  }

  .brand-text p {
    display: none;
  }

  .header-status {
    gap: 6px;
  }

  .sql-badge {
    padding: 3px 7px;
    font-size: 10px;
    max-width: 135px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .user-pill {
    padding: 3px 6px;
    font-size: 11px;
  }

  .btn-logout {
    padding: 2px 4px;
    font-size: 13px;
  }

  /* Área Principal */
  .app-main {
    padding: 8px 10px;
  }

  /* Stats Bar Compacta (não corta mais no ecrã!) */
  .picking-stats-bar {
    gap: 8px;
    margin-bottom: 8px;
  }

  .stat-box {
    padding: 8px 10px;
    gap: 8px;
    border-radius: 10px;
  }

  .stat-icon {
    width: 32px;
    height: 32px;
    font-size: 16px;
    border-radius: 8px;
  }

  .stat-info .stat-num {
    font-size: 18px;
  }

  .stat-info .stat-lbl {
    font-size: 10px;
  }

  /* Cards */
  .card {
    padding: 12px 10px;
    margin-bottom: 8px;
    border-radius: 12px;
  }

  .card-title {
    font-size: 14px;
    margin-bottom: 8px;
  }

  /* Input de Código, Câmara e Botão Ler alinhados perfeitamente */
  .input-row {
    gap: 6px;
  }

  .input-code {
    height: 44px;
    padding: 0 10px;
    font-size: 14px;
    border-radius: 10px;
    min-width: 0;
  }

  .btn-cam {
    width: 44px;
    height: 44px;
    font-size: 20px;
    border-radius: 10px;
    flex-shrink: 0;
  }

  .btn-add-item {
    height: 44px;
    padding: 0 12px;
    font-size: 13px;
    border-radius: 10px;
    flex-shrink: 0;
  }

  /* Seletor de Quantidade a somar */
  .qty-control-row {
    gap: 6px;
    padding-top: 2px;
  }

  .qty-selector {
    padding: 2px;
    border-radius: 8px;
  }

  .btn-qty-step {
    width: 30px;
    height: 30px;
    font-size: 16px;
    border-radius: 6px;
  }

  .input-qty {
    width: 38px;
    height: 30px;
    font-size: 15px;
  }

  .quick-qty-pills {
    gap: 4px;
  }

  .pill-qty {
    padding: 5px 8px;
    font-size: 11px;
    border-radius: 6px;
  }

  /* Estado Vazio de Artigos mais compacto */
  .empty-state {
    padding: 18px 10px;
  }

  .empty-icon {
    font-size: 32px;
    margin-bottom: 4px;
  }

  .empty-text {
    font-size: 13px;
  }

  .empty-sub {
    font-size: 11px;
    margin-top: 2px;
  }

  /* Linhas de Artigos Registados */
  .picking-item-row {
    padding: 8px 10px;
    gap: 6px;
  }

  .item-desc {
    font-size: 13px;
  }

  .btn-mini-step {
    width: 30px;
    height: 30px;
    font-size: 16px;
  }

  .item-qty-val {
    min-width: 28px;
    font-size: 14px;
  }

  /* Ações de Picking (Gravar / Exportar / Limpar) */
  .picking-actions-bar {
    gap: 6px;
    margin-top: 8px;
  }

  .btn-action {
    padding: 11px 8px;
    font-size: 13px;
    border-radius: 10px;
    min-width: 0;
  }

  .btn-clear {
    flex: 0 0 42px;
    min-width: 42px;
  }

  /* Barra de Navegação Inferior (Mobile Safari / Dynamic Island) */
  .bottom-nav {
    padding: 6px 4px calc(8px + var(--safe-bottom));
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }

  .nav-item {
    font-size: 10px;
    gap: 2px;
    padding: 3px 4px;
  }

  .nav-item .nav-icon {
    font-size: 18px;
  }

  /* Botão de Configuração de Filtros na Consulta */
  .btn-config-pill {
    padding: 4px 8px;
    font-size: 11px;
  }

  /* Cartão Agrupado de Artigo na Consulta */
  .grouped-search-item {
    padding: 12px 10px !important;
  }

  .supplier-card-box {
    padding: 8px 10px !important;
  }
}
