/* ==========================================================================
   OSAR homes - Design System & Styles (Impeccable Operate Mode)
   Palette: Deep Teal (#0A292D), Petrol (#0E3B43), Amber Accent (#E07A28), Slate
   ========================================================================== */

:root {
  --primary-900: #05191C;
  --primary-800: #0A292D;
  --primary-700: #0E3B43;
  --primary-600: #165662;
  --primary-100: #E6F0F2;
  --primary-50:  #F0F6F7;

  --accent-600: #C2631C;
  --accent-500: #E07A28;
  --accent-100: #FDF1E7;

  --success-700: #15803D;
  --success-600: #16A34A;
  --success-50:  #F0FDF4;

  --danger-700: #B91C1C;
  --danger-600: #DC2626;
  --danger-50:  #FEF2F2;

  --warning-800: #92400E;
  --warning-500: #F59E0B;
  --warning-50:  #FFFBEB;

  --surface-ground: #F8FAFC;
  --surface-card:   #FFFFFF;
  --surface-border: #E2E8F0;
  --surface-border-subtle: #F1F5F9;

  --text-main:    #0F172A;
  --text-muted:   #475569;
  --text-subtle:  #64748B;
  --text-inverse: #FFFFFF;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(10, 41, 45, 0.05);
  --shadow-md: 0 4px 16px -2px rgba(10, 41, 45, 0.08), 0 2px 6px -1px rgba(10, 41, 45, 0.04);
  --shadow-lg: 0 12px 32px -4px rgba(10, 41, 45, 0.12), 0 4px 12px -2px rgba(10, 41, 45, 0.06);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

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

html, body {
  font-family: var(--font-sans);
  background-color: var(--surface-ground);
  color: var(--text-main);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
}

body {
  padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px));
}

/* Números tabulares para medidas y contadores */
.tabular, .metric-num, .qr-code-text, .meta-val, .piece-tag {
  font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   PANTALLA DE LOGIN (Full Screen Auth)
   ========================================================================== */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--primary-900);
  background: radial-gradient(circle at 50% 20%, #165662 0%, #0A292D 60%, #05191C 100%);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(16px, env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
}

.auth-overlay.hidden {
  display: none;
}

.auth-card {
  background: var(--surface-card);
  width: 100%;
  max-width: 400px;
  border-radius: var(--radius-lg);
  padding: 28px 22px;
  box-shadow: var(--shadow-lg);
  border: 1px solid rgba(255, 255, 255, 0.15);
  margin: auto;
}

.auth-header {
  text-align: center;
  margin-bottom: 24px;
}

.auth-logo-img {
  height: 48px;
  width: auto;
  margin-bottom: 12px;
}

.auth-title {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--primary-800);
  letter-spacing: -0.02em;
}

.auth-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.auth-error {
  background: var(--danger-50);
  color: var(--danger-700);
  border: 1px solid #FECACA;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 16px;
  display: none;
}

.auth-footer-text {
  text-align: center;
  margin-top: 20px;
  font-size: 0.75rem;
  color: var(--text-subtle);
  line-height: 1.4;
}

/* ==========================================================================
   HEADER DE LA APLICACIÓN
   ========================================================================== */
.app-header {
  background: var(--primary-800);
  color: var(--text-inverse);
  padding-top: max(10px, env(safe-area-inset-top, 0px));
  padding-bottom: 10px;
  padding-left: max(14px, env(safe-area-inset-left, 0px));
  padding-right: max(14px, env(safe-area-inset-right, 0px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

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

.brand-logo-img {
  height: 30px;
  width: auto;
  background: #fff;
  border-radius: 4px;
  padding: 2px 4px;
}

.brand-title {
  font-size: 1.05rem;
  font-weight: 900;
  letter-spacing: -0.01em;
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.brand-title-accent {
  font-weight: 400;
  color: var(--accent-500);
  font-size: 0.95rem;
}

.brand-subtitle {
  font-size: 0.68rem;
  font-weight: 500;
  color: #94A3B8;
  display: block;
}

.header-user-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.user-badge {
  font-size: 0.72rem;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  padding: 4px 8px;
  border-radius: 20px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 140px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.role-pill {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.role-admin   { background: var(--accent-100); color: var(--accent-600); }
.role-almacen { background: var(--primary-100); color: var(--primary-700); }
.role-operario{ background: #F1F5F9; color: var(--text-muted); }

.btn-icon-header {
  background: none;
  border: none;
  color: #94A3B8;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  touch-action: manipulation;
  min-height: 40px;
  min-width: 40px;
  justify-content: center;
}
.btn-icon-header:hover { color: #fff; background: rgba(255, 255, 255, 0.1); }

/* ==========================================================================
   NAVEGACIÓN INFERIOR (Bottom Bar - Thumb Zone)
   ========================================================================== */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #FFFFFF;
  border-top: 1px solid var(--surface-border);
  display: flex;
  justify-content: space-around;
  padding-top: 6px;
  padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  z-index: 100;
  box-shadow: 0 -2px 12px rgba(10, 41, 45, 0.06);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text-subtle);
  font-size: 0.68rem;
  font-weight: 600;
  gap: 3px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  transition: all 0.15s ease;
  min-height: 48px;
  min-width: 48px;
  touch-action: manipulation;
  flex: 1;
}

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

.nav-item svg {
  width: 22px;
  height: 22px;
  stroke-width: 1.8;
}

/* Ocultar pestaña admin para no-admins */
.nav-item.admin-only.hidden {
  display: none;
}

/* ==========================================================================
   CONTENIDO PRINCIPAL & CARDS
   ========================================================================== */
.tab-content {
  display: none;
  padding: 12px 14px;
  max-width: 900px;
  margin: 0 auto;
}

.tab-content.active {
  display: block;
  animation: fadeIn 0.18s ease-out;
}

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

.card {
  background: var(--surface-card);
  border-radius: var(--radius-md);
  padding: 14px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--surface-border);
}

@media (min-width: 600px) {
  .card {
    padding: 18px 20px;
    margin-bottom: 16px;
  }
  .tab-content {
    padding: 16px;
  }
}

.card-title {
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--primary-800);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  letter-spacing: -0.01em;
}

/* ==========================================================================
   PANTALLA DE ESCÁNER DE CÁMARA (Mobile Optimized)
   ========================================================================== */
.scanner-viewport-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 350px;
  background: #000;
  border-radius: var(--radius-md);
  overflow: hidden;
  margin-bottom: 12px;
  box-shadow: var(--shadow-md);
}

@media (max-height: 720px) {
  .scanner-viewport-wrap {
    max-height: 280px;
  }
}

#scanner-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scanner-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.scanner-box {
  width: min(65vw, 210px);
  height: min(65vw, 210px);
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: var(--radius-md);
  box-shadow: 0 0 0 4000px rgba(0, 0, 0, 0.48);
  position: relative;
}

.scanner-box::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--accent-500);
  box-shadow: 0 0 12px var(--accent-500);
  animation: scanLine 2s infinite ease-in-out;
}

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

.scanner-tools {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 10;
}

.tool-btn {
  background: rgba(10, 41, 45, 0.75);
  backdrop-filter: blur(8px);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease;
}

.tool-btn:active { transform: scale(0.92); }

/* Banners de Retroalimentación de Escaneo */
.scan-alert-banner {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 0.88rem;
  margin-bottom: 12px;
  display: none;
  border: 1px solid transparent;
  line-height: 1.35;
}

.scan-alert-banner.success {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--success-50);
  color: var(--success-700);
  border-color: #BBF7D0;
}

.scan-alert-banner.duplicate {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--warning-50);
  color: var(--warning-800);
  border-color: #FDE68A;
}

.scan-alert-banner.error {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--danger-50);
  color: var(--danger-700);
  border-color: #FECACA;
}

/* Métricas en Obra */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.metric-card {
  background: var(--surface-card);
  border: 1px solid var(--surface-border);
  padding: 10px 4px;
  border-radius: var(--radius-sm);
  text-align: center;
}

.metric-num {
  font-size: 1.35rem;
  font-weight: 900;
  color: var(--primary-800);
  line-height: 1.1;
}

.metric-label {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-subtle);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 3px;
}

/* ==========================================================================
   BOTONES & CONTROLES (Ergonomía Táctil 48px)
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  font-size: 0.9rem;
  font-weight: 700;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
  width: 100%;
  min-height: 48px;
  touch-action: manipulation;
  user-select: none;
}

.btn:active {
  transform: scale(0.98);
}

.btn svg {
  width: 20px;
  height: 20px;
  stroke-width: 2;
  flex-shrink: 0;
}

.btn-primary {
  background: var(--primary-700);
  color: #fff;
}
.btn-primary:active { background: var(--primary-900); }

.btn-accent {
  background: var(--accent-500);
  color: #fff;
}
.btn-accent:active { background: var(--accent-600); }

.btn-outline {
  background: #fff;
  border: 1px solid var(--surface-border);
  color: var(--text-main);
}
.btn-outline:active { background: var(--surface-ground); }

.btn-danger {
  background: var(--danger-600);
  color: #fff;
}
.btn-danger:active { background: var(--danger-700); }

.btn-sm {
  padding: 8px 12px;
  font-size: 0.8rem;
  min-height: 38px;
  width: auto;
}

/* ==========================================================================
   FORMULARIOS (Evitar Zoom iOS con font-size 16px)
   ========================================================================== */
.form-group {
  margin-bottom: 12px;
}

.filter-grid-2col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 10px;
}

@media (max-width: 480px) {
  .filter-grid-2col {
    grid-template-columns: 1fr;
    gap: 8px;
  }
}

.form-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.form-input, .form-select {
  width: 100%;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--surface-border);
  font-size: 16px; /* Imprescindible para evitar auto-zoom en iOS Safari */
  background: #fff;
  color: var(--text-main);
  outline: none;
  min-height: 44px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input:focus, .form-select:focus {
  border-color: var(--primary-600);
  box-shadow: 0 0 0 3px rgba(14, 59, 67, 0.12);
}

/* ==========================================================================
   LISTAS DE PIEZAS & USUARIOS
   ========================================================================== */
.piece-list {
  max-height: 340px;
  overflow-y: auto;
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  background: #fff;
  -webkit-overflow-scrolling: touch;
}

.piece-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 11px 12px;
  border-bottom: 1px solid var(--surface-border-subtle);
  font-size: 0.82rem;
  gap: 8px;
}

.piece-row:last-child { border-bottom: none; }

.piece-row-main {
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.25;
}

.piece-row-sub {
  font-size: 0.73rem;
  color: var(--text-subtle);
  margin-top: 2px;
}

.piece-tag {
  font-family: var(--font-mono);
  font-weight: 800;
  background: var(--primary-50);
  color: var(--primary-800);
  padding: 3px 6px;
  border-radius: 4px;
  font-size: 0.74rem;
  border: 1px solid var(--surface-border);
  white-space: nowrap;
  flex-shrink: 0;
}

/* ==========================================================================
   TABLA DE USUARIOS (Módulo Admin)
   ========================================================================== */
.user-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.user-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  text-align: left;
}

.user-table th {
  background: var(--surface-ground);
  color: var(--text-muted);
  font-weight: 700;
  padding: 10px 10px;
  border-bottom: 1px solid var(--surface-border);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.user-table td {
  padding: 11px 10px;
  border-bottom: 1px solid var(--surface-border-subtle);
}

.user-table tr:last-child td { border-bottom: none; }

/* ==========================================================================
   VISTA PREVIA DE ETIQUETAS QR
   ========================================================================== */
.labels-preview-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: 14px;
}

@media (min-width: 540px) {
  .labels-preview-grid {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  }
}

.mold-label {
  background: #fff;
  border: 1.5px solid #000;
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: var(--shadow-sm);
}

.label-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1.5px solid #000;
  padding-bottom: 3px;
}

.brand-osar { font-weight: 900; font-size: 1rem; }
.brand-homes { font-weight: 400; font-size: 0.85rem; }
.label-badge-set {
  font-size: 0.7rem;
  font-weight: 800;
  border: 1px solid #000;
  padding: 1px 5px;
  border-radius: 3px;
}

.label-desc {
  font-size: 0.85rem;
  font-weight: 800;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.label-meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  margin: 4px 0;
  background: #F1F5F9;
  padding: 3px 6px;
  border-radius: 4px;
}

.meta-val { font-weight: 800; }
.meta-val.highlight { font-size: 0.85rem; }

.label-body {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
}

.label-qr-wrap {
  width: 85px;
  height: 85px;
  flex-shrink: 0;
}

.label-qr-wrap svg {
  width: 100% !important;
  height: 100% !important;
}

.label-details {
  font-size: 0.7rem;
  line-height: 1.3;
}

.qr-code-text {
  font-family: var(--font-mono);
  font-weight: 900;
  font-size: 0.85rem;
  margin-bottom: 3px;
}

.label-footer {
  border-top: 1px solid #000;
  padding-top: 3px;
  display: flex;
  justify-content: space-between;
  font-size: 0.6rem;
  font-weight: 700;
}

/* ==========================================================================
   MODAL DIALOG (Para Nuevo Usuario)
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 41, 45, 0.65);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
  overflow-y: auto;
}

.modal-backdrop.active {
  display: flex;
  animation: fadeIn 0.15s ease-out;
}

.modal-card {
  background: #fff;
  border-radius: var(--radius-md);
  padding: 20px 18px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--surface-border);
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-title {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--primary-800);
}

.btn-close-modal {
  background: none;
  border: none;
  color: var(--text-subtle);
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  min-height: 40px;
  min-width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-close-modal:hover { color: var(--text-main); }

.labels-preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
  margin-top: 14px;
}

.mold-label {
  background: #fff;
  border: 1.5px solid #000;
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: var(--shadow-sm);
}

.label-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1.5px solid #000;
  padding-bottom: 3px;
}

.brand-osar { font-weight: 900; font-size: 1rem; }
.brand-homes { font-weight: 400; font-size: 0.85rem; }
.label-badge-set {
  font-size: 0.7rem;
  font-weight: 800;
  border: 1px solid #000;
  padding: 1px 5px;
  border-radius: 3px;
}

.label-desc {
  font-size: 0.85rem;
  font-weight: 800;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.label-meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  margin: 4px 0;
  background: #F1F5F9;
  padding: 3px 6px;
  border-radius: 4px;
}

.meta-val { font-weight: 800; }
.meta-val.highlight { font-size: 0.85rem; }

.label-body {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
}

.label-qr-wrap {
  width: 90px;
  height: 90px;
  flex-shrink: 0;
}

.label-qr-wrap svg {
  width: 100% !important;
  height: 100% !important;
}

.label-details {
  font-size: 0.7rem;
  line-height: 1.3;
}

.qr-code-text {
  font-family: var(--font-mono);
  font-weight: 900;
  font-size: 0.85rem;
  margin-bottom: 3px;
}

.label-footer {
  border-top: 1px solid #000;
  padding-top: 3px;
  display: flex;
  justify-content: space-between;
  font-size: 0.6rem;
  font-weight: 700;
}

/* ==========================================================================
   MODAL DIALOG (Para Nuevo Usuario)
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 41, 45, 0.65);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal-backdrop.active {
  display: flex;
  animation: fadeIn 0.15s ease-out;
}

.modal-card {
  background: #fff;
  border-radius: var(--radius-md);
  padding: 24px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--surface-border);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--primary-800);
}

.btn-close-modal {
  background: none;
  border: none;
  color: var(--text-subtle);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
}
.btn-close-modal:hover { color: var(--text-main); }
