/* Reset y variables de diseño */
:root {
  --bg-color: #0d0e15;
  --surface-color: #161925;
  --surface-hover: #222638;
  --surface-card: #1f2336;
  --primary: #4f46e5;
  --primary-glow: rgba(79, 70, 229, 0.4);
  --accent: #06b6d4;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  
  /* Gradientes de las Zonas */
  --grad-norte: linear-gradient(135deg, #1e3a8a, #3b82f6);
  --grad-sur: linear-gradient(135deg, #7f1d1d, #ef4444);
  --grad-este: linear-gradient(135deg, #064e3b, #10b981);
  --grad-oeste: linear-gradient(135deg, #581c87, #a855f7);
  --grad-unknown: linear-gradient(135deg, #374151, #4b5563);

  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --shadow-sm: 0 2px 4px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 15px var(--primary-glow);
  --border-radius-sm: 8px;
  --border-radius-md: 14px;
  --border-radius-lg: 24px;
  --header-height: 60px;
  --nav-height: 70px;
}

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

body {
  font-family: var(--font-family);
  background-color: var(--bg-color);
  color: var(--text-main);
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* Contenedor principal */
#app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

/* HEADER */
.app-header {
  height: var(--header-height);
  background-color: rgba(22, 25, 37, 0.85);
  backdrop-filter: blur(10px);
  border-b: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: relative;
  z-index: 10;
  box-shadow: var(--shadow-sm);
}

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

.header-icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
}

.logo-area h1 {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.badge-offline {
  font-size: 0.65rem;
  background-color: var(--success);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
  background-color: rgba(255, 255, 255, 0.03);
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--success);
  box-shadow: 0 0 8px var(--success);
}

/* ÁREA DE CONTENIDO */
.app-main {
  flex: 1;
  overflow-y: auto;
  position: relative;
  height: calc(100vh - var(--header-height) - var(--nav-height));
}

.app-view {
  display: none;
  height: 100%;
  width: 100%;
  padding: 16px;
}

.app-view.active {
  display: flex;
  flex-direction: column;
}

/* PESTAÑA: ESCANEAR */
#view-scan {
  padding: 0; /* Ocupa toda la pantalla */
  position: relative;
  overflow: hidden;
}

.scanner-container {
  flex: 1;
  width: 100%;
  position: relative;
  background-color: #000;
  overflow: hidden;
}

#video-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(1); /* No reflejado ya que se usa la cámara trasera para escanear */
}

/* Máscara de enfoque y animación */
.scanner-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  background-color: rgba(0, 0, 0, 0.4);
}

.scanner-frame {
  width: 80%;
  height: 180px;
  max-width: 320px;
  position: relative;
  border-radius: var(--border-radius-sm);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.6); /* Oscurece el exterior del recuadro */
}

.corner {
  position: absolute;
  width: 24px;
  height: 24px;
  border: 4px solid var(--accent);
}

.top-left { top: -2px; left: -2px; border-right: none; border-bottom: none; border-top-left-radius: 8px; }
.top-right { top: -2px; right: -2px; border-left: none; border-bottom: none; border-top-right-radius: 8px; }
.bottom-left { bottom: -2px; left: -2px; border-right: none; border-top: none; border-bottom-left-radius: 8px; }
.bottom-right { bottom: -2px; right: -2px; border-left: none; border-top: none; border-bottom-right-radius: 8px; }

/* Animación de escaneo */
.scan-line {
  position: absolute;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  box-shadow: 0 0 8px var(--accent);
  animation: scanning 2.5s ease-in-out infinite;
}

@keyframes scanning {
  0% { top: 0%; }
  50% { top: 100%; }
  100% { top: 0%; }
}

.scan-instructions {
  margin-top: 24px;
  font-size: 0.9rem;
  color: #fff;
  background-color: rgba(0,0,0,0.6);
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid rgba(255,255,255,0.1);
  text-align: center;
  font-weight: 500;
  letter-spacing: 0.3px;
}

/* Barra de Progreso y Mensajes OCR */
.progress-container {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  background-color: rgba(22, 25, 37, 0.9);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--border-radius-md);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 5;
  box-shadow: var(--shadow-lg);
  transition: all 0.3s ease;
}

.progress-container.hidden {
  display: none !important;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(6, 182, 212, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  flex-shrink: 0;
}

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

.progress-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#ocr-status-message {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-main);
}

.progress-bar {
  width: 100%;
  height: 6px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
  overflow: hidden;
}

#ocr-progress-fill {
  height: 100%;
  background-color: var(--accent);
  border-radius: 3px;
  transition: width 0.15s ease-out;
}

/* Controles de Escaneo */
.scanner-controls {
  position: absolute;
  bottom: 24px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 28px;
  z-index: 4;
}

/* PANEL DE RESULTADOS (Drawer deslizable) */
.result-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: var(--surface-color);
  border-top-left-radius: var(--border-radius-lg);
  border-top-right-radius: var(--border-radius-lg);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 -8px 32px rgba(0,0,0,0.6);
  z-index: 8;
  max-height: 85%;
  overflow-y: auto;
  transform: translateY(0);
  transition: transform 0.3s cubic-bezier(0.32, 0.94, 0.6, 1);
}

.result-panel.hidden {
  transform: translateY(100%);
  display: none !important;
}

.result-handle {
  width: 40px;
  height: 5px;
  background-color: rgba(255,255,255,0.2);
  border-radius: 3px;
  margin: 12px auto 4px auto;
  cursor: pointer;
}

.result-content {
  padding: 8px 20px 24px 20px;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 12px;
}

.result-header h3 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-main);
}

.btn-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.8rem;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.result-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.result-thumbnail-wrapper {
  width: 100%;
  max-height: 120px;
  border-radius: var(--border-radius-sm);
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #000;
}

#result-image-preview {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Tarjeta del Grupo de Reparto Asignado (SUPER VISIBLE) */
.group-card {
  padding: 20px;
  border-radius: var(--border-radius-md);
  text-align: center;
  color: white;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: var(--shadow-md);
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.group-card::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.15) 0%, transparent 60%);
  pointer-events: none;
}

/* Estilos de gradiente según la zona */
.group-card.zona-norte { background: var(--grad-norte); box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4); border: 1px solid #3b82f6; }
.group-card.zona-sur { background: var(--grad-sur); box-shadow: 0 4px 20px rgba(239, 68, 68, 0.4); border: 1px solid #ef4444; }
.group-card.zona-este { background: var(--grad-este); box-shadow: 0 4px 20px rgba(16, 185, 129, 0.4); border: 1px solid #10b981; }
.group-card.zona-oeste { background: var(--grad-oeste); box-shadow: 0 4px 20px rgba(168, 85, 247, 0.4); border: 1px solid #a855f7; }
.group-card.desconocida { background: var(--grad-unknown); border: 1px solid #6b7280; }

.group-label {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 2px;
  opacity: 0.8;
}

.group-value {
  font-size: 1.8rem;
  font-weight: 900;
  letter-spacing: 0.5px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.address-details-card {
  background-color: var(--surface-card);
  border-radius: var(--border-radius-md);
  border: 1px solid rgba(255,255,255,0.05);
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
}

.detail-label {
  color: var(--text-muted);
}

.detail-value {
  font-weight: 600;
  color: var(--text-main);
}

.text-highlight {
  color: #fff;
  font-size: 1.05rem;
  background-color: rgba(255,255,255,0.03);
  padding: 2px 8px;
  border-radius: 4px;
}

.raw-text-details {
  margin-top: 8px;
}

.raw-text-details summary {
  font-size: 0.8rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px 0;
  outline: none;
}

.raw-text-details pre {
  background-color: rgba(0,0,0,0.4);
  color: #38bdf8;
  font-family: monospace;
  font-size: 0.8rem;
  padding: 10px;
  border-radius: var(--border-radius-sm);
  overflow-x: auto;
  white-space: pre-wrap;
  margin-top: 6px;
  border: 1px solid rgba(255,255,255,0.03);
}

/* PESTAÑA: CALLEJERO / BASE DE DATOS */
.view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.view-header h2 {
  font-size: 1.3rem;
  font-weight: 800;
}

.search-box {
  position: relative;
  margin-bottom: 16px;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-muted);
}

.search-box input {
  width: 100%;
  padding: 12px 14px 12px 42px;
  background-color: var(--surface-color);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--border-radius-md);
  color: white;
  font-size: 0.95rem;
  outline: none;
  transition: all 0.2s ease;
}

.search-box input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.25);
}

.streets-list-wrapper {
  flex: 1;
  overflow-y: auto;
}

.streets-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.street-item {
  background-color: var(--surface-color);
  border-radius: var(--border-radius-md);
  border: 1px solid rgba(255, 255, 255, 0.05);
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background-color 0.2s ease;
}

.street-item:hover {
  background-color: var(--surface-hover);
}

.street-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.street-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.street-group-tag {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 20px;
  width: fit-content;
  color: white;
}

/* Colores de las etiquetas de zonas */
.tag-norte { background: var(--grad-norte); }
.tag-sur { background: var(--grad-sur); }
.tag-este { background: var(--grad-este); }
.tag-oeste { background: var(--grad-oeste); }

.street-actions {
  display: flex;
  gap: 8px;
}

.btn-action-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-action-icon:hover {
  color: #fff;
  background-color: rgba(255, 255, 255, 0.05);
}

.btn-action-icon.btn-delete:hover {
  color: var(--danger);
  background-color: rgba(239, 68, 68, 0.1);
}

/* PESTAÑA: AJUSTES */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.settings-group {
  background-color: var(--surface-color);
  border-radius: var(--border-radius-md);
  border: 1px solid rgba(255, 255, 255, 0.05);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.settings-group h3 {
  font-size: 0.9rem;
  color: var(--accent);
  letter-spacing: 0.5px;
  text-transform: uppercase;
  font-weight: 700;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 8px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.setting-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
}

.setting-info h4 {
  font-size: 0.95rem;
  font-weight: 600;
}

.setting-info p {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.info-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  padding: 4px 0;
}

.info-row span {
  color: var(--text-muted);
}

.info-row strong {
  color: var(--text-main);
  font-weight: 600;
}

/* Diagnósticos */
.diagnostic-result {
  background-color: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.05);
  border-radius: var(--border-radius-sm);
  padding: 10px;
  font-family: monospace;
  font-size: 0.8rem;
  white-space: pre-wrap;
  color: #10b981;
}

.diagnostic-result.error {
  color: var(--danger);
}

/* NAVEGACIÓN INFERIOR */
.app-nav {
  height: var(--nav-height);
  background-color: rgba(22, 25, 37, 0.95);
  backdrop-filter: blur(12px);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 0 8px;
  position: relative;
  z-index: 10;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.3);
}

.nav-item {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--text-muted);
  flex: 1;
  height: 100%;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.7rem;
  font-weight: 600;
}

.nav-item svg {
  width: 22px;
  height: 22px;
  transition: transform 0.2s ease;
}

.nav-item.active {
  color: var(--accent);
}

.nav-item.active svg {
  transform: translateY(-2px);
  color: var(--accent);
  filter: drop-shadow(0 0 4px rgba(6, 182, 212, 0.5));
}

/* BOTONES */
.btn {
  border: none;
  padding: 10px 18px;
  border-radius: var(--border-radius-sm);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s ease;
}

.btn-icon svg {
  width: 20px;
  height: 20px;
}

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

.btn-primary:hover:not(:disabled) {
  background-color: #4338ca;
  box-shadow: 0 0 12px rgba(79, 70, 229, 0.4);
}

.btn-primary:disabled {
  background-color: rgba(79, 70, 229, 0.4);
  color: rgba(255, 255, 255, 0.4);
  cursor: not-allowed;
}

.btn-secondary {
  background-color: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.btn-secondary:hover {
  background-color: rgba(255, 255, 255, 0.12);
}

.btn-warning {
  background-color: rgba(245, 158, 11, 0.15);
  color: var(--warning);
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.btn-warning:hover {
  background-color: rgba(245, 158, 11, 0.25);
}

.btn-danger {
  background-color: rgba(239, 68, 68, 0.15);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.btn-danger:hover {
  background-color: rgba(239, 68, 68, 0.25);
}

.btn-large {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(79, 70, 229, 0.5);
  border: 4px solid rgba(255,255,255,0.1);
  position: relative;
}

.btn-large svg {
  width: 32px;
  height: 32px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.75rem;
  border-radius: 6px;
}

/* MODALES */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fadeIn 0.2s ease-out;
}

.modal-overlay.hidden {
  display: none !important;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background-color: var(--surface-color);
  border-radius: var(--border-radius-md);
  border: 1px solid rgba(255, 255, 255, 0.08);
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: scaleUp 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes scaleUp {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  font-size: 1.1rem;
  font-weight: 700;
}

.modal-form {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
}

.form-group input, .form-group select {
  padding: 10px 12px;
  background-color: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--border-radius-sm);
  color: white;
  font-size: 0.9rem;
  outline: none;
  width: 100%;
}

.form-group input:focus, .form-group select:focus {
  border-color: var(--primary);
  background-color: rgba(255,255,255,0.05);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

/* Scrollbar personalizado */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: var(--bg-color);
}

::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255,255,255,0.2);
}
