<!DOCTYPE html>
<html lang="es">
<head>
  <script>/*pos_theme_early*/(function(){var t=localStorage.getItem("pos_theme")||"industrial";document.documentElement.setAttribute("data-theme",t);})()</script>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Catalogo — Nexus Autoparts POS</title>
  <link rel="stylesheet" href="/pos/static/css/tokens.css" />
  <link rel="stylesheet" href="/pos/static/css/common.css" />
  <link rel="stylesheet" href="/pos/static/css/pos-ui.css?v=33" />
  <link rel="stylesheet" href="/pos/static/css/sidebar.css" />
  <link rel="stylesheet" href="/pos/static/css/pos-glass.css" />
  <link rel="stylesheet" href="/pos/static/css/chat.css" />
  <link rel="stylesheet" href="/pos/static/css/onboarding.css" />
  <link rel="manifest" href="/pos/static/pwa/manifest.json" />
  <meta name="theme-color" content="#F5A623" />
  <link rel="shortcut icon" type="image/png" href="/pos/static/pwa/icon-192.png" />
  <script src="/pos/static/js/native-bridge.js" defer></script>

  <link rel="stylesheet" href="/pos/static/css/catalog.css"></head>

<body>

  <!-- Theme switcher bar -->
  <div class="theme-bar" role="toolbar" aria-label="Cambiar tema">
    <span class="theme-bar__label">Tema:</span>
    <button class="theme-btn is-active" data-theme-switch="industrial" aria-pressed="true">
      <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M12 2l2.4 1.6L17 3l1 2.6 2.4 1.6-.2 2.8L22 12l-1.8 2 .2 2.8-2.4 1.6-1 2.6-2.6-.2L12 22l-2.4-1.6L7 21l-1-2.6-2.4-1.6.2-2.8L2 12l1.8-2-.2-2.8L6 5.6 7 3l2.6.2z"/><circle cx="12" cy="12" r="3"/></svg>
      Industrial
    </button>
    <button class="theme-btn" data-theme-switch="modern" aria-pressed="false">
      <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83"/></svg>
      Moderno
    </button>
  </div>

  <!-- Mobile sidebar overlay -->
  <div class="sidebar-overlay" id="sidebarOverlay" role="presentation"></div>

  <!-- APP SHELL -->
  <div class="app-shell">

    <!-- SIDEBAR -->
    <aside class="sidebar themed-scrollbar" id="sidebar" role="navigation" aria-label="Menu principal">
      <div class="sidebar__brand">
        <div class="brand-logo" aria-hidden="true">N</div>
        <div class="brand-name">
          <span class="brand-name__primary">Nexus</span>
          <span class="brand-name__sub">Autoparts POS</span>
        </div>
      </div>
      <nav class="sidebar__nav">
        <div class="nav-section-label">Principal</div>
        <a class="nav-item" href="/pos/dashboard" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></svg>
          Dashboard
        </a>
        <a class="nav-item" href="/pos/sale" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 01-8 0"/></svg>
          Punto de Venta
        </a>
        <a class="nav-item" href="/pos/inventory" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M21 16V8a2 2 0 00-1-1.73l-7-4a2 2 0 00-2 0l-7 4A2 2 0 003 8v8a2 2 0 001 1.73l7 4a2 2 0 002 0l7-4A2 2 0 0021 16z"/></svg>
          Inventario
        </a>
        <a class="nav-item is-active" href="/pos/catalog" role="menuitem" aria-current="page">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M8 7h8M8 12h8M8 17h5"/></svg>
          Catalogo
        </a>
        <a class="nav-item" href="/pos/supplier-catalog" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M2 3h6l2 3h12v14H2V3z"/></svg>
          Cat. Proveedores
        </a>
        <div class="nav-section-label" style="margin-top: var(--space-2);">Gestion</div>
        <a class="nav-item" href="/pos/customers" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/></svg>
          Clientes
        </a>
        <a class="nav-item" href="/pos/invoicing" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14,2 14,8 20,8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
          Facturacion
        </a>
        <a class="nav-item" href="/pos/accounting" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6"/></svg>
          Contabilidad
        </a>
        <a class="nav-item" href="/pos/reports" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><polyline points="22,12 18,12 15,21 9,3 6,12 2,12"/></svg>
          Reportes
        </a>
        <div class="nav-section-label" style="margin-top: var(--space-2);">Sistema</div>
        <a class="nav-item" href="/pos/config" role="menuitem">
          <svg class="nav-item__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75"><circle cx="12" cy="12" r="3"/><path d="M19.07 4.93l-1.41 1.41M6.34 17.66l-1.41 1.41M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M12 2v2M12 20v2M2 12h2M20 12h2"/></svg>
          Configuracion
        </a>
      </nav>
      <div class="sidebar__profile">
        <div class="profile-avatar" id="profileAvatar" aria-hidden="true">--</div>
        <div class="profile-info">
          <div class="profile-info__name" id="profileName">--</div>
          <div class="profile-info__role" id="profileRole">--</div>
        </div>
      </div>
    </aside>

    <!-- MAIN CONTENT -->
    <main class="main-content">

      <!-- Header: breadcrumb + search -->
      <header class="content-header">
        <nav class="breadcrumb" id="breadcrumb" aria-label="Navegacion del catalogo">
          <span class="breadcrumb__current">Catalogo</span>
        </nav>
        <div class="header-actions" style="position:relative;">
          <button class="btn btn--sm" id="uploadPricesBtn" onclick="CatalogApp.openUploadPricesModal()" title="Subir precios de proveedor" style="margin-right:var(--space-2);display:none;">
            💰 Precios proveedor
          </button>
          <div class="mode-toggle" id="modeToggle" title="Cambiar entre catalogo OEM (TecDoc), marcas locales, por marca de vehiculo y consumibles">
            <button data-mode="oem" onclick="CatalogApp.setMode('oem')" disabled style="opacity:0.5;cursor:not-allowed;" title="Próximamente">OEM 🔒</button>
            <button data-mode="local" onclick="CatalogApp.setMode('local')">Local</button>
            <button data-mode="brand" onclick="BrandCatalog.show()" title="Catalogo por marca de vehiculo">Por Marca</button>
            <button data-mode="supplies" onclick="CatalogApp.setMode('supplies')" title="Aceites, quimicos, herramientas — sin vehiculo">Supplies</button>
          </div>
          <div class="search-bar" id="searchBar">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="M21 21l-4.35-4.35"/></svg>
            <input type="text" id="searchInput" placeholder="Buscar por numero de parte o nombre... (F1)" autocomplete="off" aria-label="Buscar productos" />
            <button type="button" id="btnScanBarcode" title="Escanear codigo de barras" style="background:none;border:none;cursor:pointer;padding:4px 8px;color:var(--color-text-muted);display:flex;align-items:center;" onclick="CatalogApp.startBarcodeScan()">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 7V5a2 2 0 012-2h2"/><path d="M17 3h2a2 2 0 012 2v2"/><path d="M21 17v2a2 2 0 01-2 2h-2"/><path d="M7 21H5a2 2 0 01-2-2v-2"/><line x1="7" y1="12" x2="17" y2="12"/><line x1="7" y1="8" x2="17" y2="8"/><line x1="7" y1="16" x2="17" y2="16"/></svg>
            </button>
          </div>
          <div class="search-dropdown" id="searchDropdown"></div>
        </div>
      </header>

      <!-- Vehicle selector bar -->
      <div class="vehicle-selector" id="vehicleSelector">
        <div class="vehicle-selector__inner">
          <div class="vs-group">
            <label class="vs-label">Año</label>
            <select class="vs-select" id="vsYear" onchange="CatalogApp.vsYearChanged()">
              <option value="">Seleccionar...</option>
            </select>
          </div>
          <div class="vs-arrow">›</div>
          <div class="vs-group">
            <label class="vs-label">Marca</label>
            <select class="vs-select" id="vsBrand"  onchange="CatalogApp.vsBrandChanged()">
              <option value="">Seleccionar...</option>
            </select>
          </div>
          <div class="vs-arrow">›</div>
          <div class="vs-group">
            <label class="vs-label">Modelo</label>
            <select class="vs-select" id="vsModel"  onchange="CatalogApp.vsModelChanged()">
              <option value="">Seleccionar...</option>
            </select>
          </div>
          <div class="vs-arrow">›</div>
          <div class="vs-group">
            <label class="vs-label">Motor</label>
            <select class="vs-select" id="vsEngine" disabled onchange="CatalogApp.vsEngineChanged()">
              <option value="">Seleccionar...</option>
            </select>
          </div>
          <button class="vs-clear" id="vsClear" onclick="CatalogApp.vsClear()" title="Limpiar seleccion" style="display:none;">✕</button>
          <span class="vs-vin-divider" style="color:var(--color-text-disabled);padding-bottom:6px;flex-shrink:0;">|</span>
          <div class="vs-group" id="plateGroup" style="position:relative;">
            <a class="vs-label" id="plateToggle" href="#" onclick="event.preventDefault();CatalogApp.togglePlate();" style="color:var(--color-primary);cursor:pointer;text-decoration:underline;white-space:nowrap;">Tienes las placas?</a>
            <div id="plateInputWrap" style="display:none;">
              <div style="display:flex;gap:4px;">
                <input type="text" class="vs-select" id="plateInput" placeholder="Ej: ABC-1234" maxlength="12" style="text-transform:uppercase;font-family:var(--font-mono,monospace);letter-spacing:0.05em;flex:1;" />
                <button class="btn btn-primary" id="plateLookupBtn" onclick="CatalogApp.lookupPlate()" style="height:auto;padding:var(--space-2) var(--space-3);font-size:var(--text-body-sm);">Buscar</button>
              </div>
              <div id="plateStatus" style="font-size:var(--text-caption);margin-top:4px;color:var(--color-text-muted);display:none;"></div>
            </div>
          </div>
          <span class="vs-vin-divider" style="color:var(--color-text-disabled);padding-bottom:6px;flex-shrink:0;">|</span>
          <div class="vs-group" id="vinGroup" style="position:relative;">
            <a class="vs-label" id="vinToggle" href="#" onclick="event.preventDefault();CatalogApp.toggleVin();" style="color:var(--color-primary);cursor:pointer;text-decoration:underline;white-space:nowrap;">Tienes el VIN?</a>
            <div id="vinInputWrap" style="display:none;">
              <div style="display:flex;gap:4px;">
                <input type="text" class="vs-select" id="vinInput" placeholder="Ej: 1HGBH41JXMN109186" maxlength="17" style="text-transform:uppercase;font-family:var(--font-mono,monospace);letter-spacing:0.05em;flex:1;" />
                <button class="btn btn-primary" id="vinDecodeBtn" onclick="CatalogApp.decodeVin()" style="height:auto;padding:var(--space-2) var(--space-3);font-size:var(--text-body-sm);">Decodificar</button>
              </div>
              <div id="vinStatus" style="font-size:var(--text-caption);margin-top:4px;color:var(--color-text-muted);display:none;"></div>
            </div>
          </div>
        </div>
      </div>

      <!-- Scrollable page body -->
      <div class="page-body" id="pageBody">

        <!-- Level title + optional filter -->
        <div style="display:flex; align-items:center; justify-content:space-between; gap:var(--space-4); flex-wrap:wrap;">
          <h2 class="level-title" id="levelTitle">Selecciona una marca</h2>
          <input type="text" class="level-filter" id="levelFilter" placeholder="Filtrar..." style="display:none;" aria-label="Filtrar niveles de catalogo" />
        </div>

        <!-- Loading spinner -->
        <div class="loading" id="loading"><div class="spinner"></div></div>

        <!-- Empty state -->
        <div class="empty-state" id="emptyState">
          <div class="empty-state__title" id="emptyTitle">Sin resultados</div>
          <div class="empty-state__subtitle" id="emptySubtitle">No se encontraron datos para este nivel.</div>
        </div>

        <!-- Navigation grid (brands, models, years, engines, categories, groups) -->
        <div class="nav-grid" id="navGrid" role="list"></div>

        <!-- Parts grid (only for parts level) -->
        <div class="nav-grid nav-grid--parts" id="partsGrid" role="list" style="display:none;"></div>

        <!-- Pagination -->
        <nav class="pagination" id="pagination" aria-label="Paginacion"></nav>

      </div>
    </main>
  </div>

  <!-- Detail panel overlay -->
  <div class="detail-overlay" id="detailOverlay"></div>

  <!-- Detail panel (slide-in) -->
  <aside class="detail-panel" id="detailPanel">
    <div class="detail-header">
      <h3>Detalle de parte</h3>
      <button class="detail-close" id="detailClose" aria-label="Cerrar detalle">&#10005;</button>
    </div>
    <div class="detail-body" id="detailBody">
      <!-- Populated by JS -->
    </div>
    <div class="detail-footer" id="detailFooter">
      <div class="qty-row">
        <button class="qty-btn" id="qtyMinus">-</button>
        <span class="qty-display" id="qtyDisplay">1</span>
        <button class="qty-btn" id="qtyPlus">+</button>
      </div>
      <button class="btn btn-primary" id="addToCartBtn" style="width:100%;">
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 002 1.61h9.72a2 2 0 002-1.61L23 6H6"/></svg>
        Agregar al carrito
      </button>
    </div>
  </aside>

  <!-- Cart FAB -->
  <button class="cart-fab" id="cartFab" aria-label="Abrir carrito">
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 002 1.61h9.72a2 2 0 002-1.61L23 6H6"/></svg>
    <span class="cart-fab__badge" id="cartBadge">0</span>
  </button>

  <!-- Cart overlay -->
  <div class="cart-overlay" id="cartOverlay"></div>

  <!-- Cart sidebar -->
  <aside class="cart-sidebar" id="cartSidebar">
    <div class="cart-header">
      <h3>Carrito</h3>
      <button id="cartCloseBtn" style="background:none;border:none;cursor:pointer;font-size:1.4rem;color:var(--color-text-secondary);padding:var(--space-1);">&#10005;</button>
    </div>
    <div class="cart-items" id="cartItems"></div>
    <div class="cart-empty" id="cartEmpty" style="display:none;padding:2rem;text-align:center;color:var(--color-text-muted);">Carrito vacio</div>
    <div class="cart-footer">
      <div class="cart-totals">
        <div>Subtotal: <span id="cartSubtotal">$0.00</span></div>
        <div>IVA 16%: <span id="cartTax">$0.00</span></div>
        <div style="font-weight:bold;font-size:1.2em;">Total: <span id="cartTotal">$0.00</span></div>
      </div>
      <button id="checkoutBtn" class="btn btn-primary" style="width:100%;margin-top:var(--space-3);">Ir a cobrar &rarr;</button>
    </div>
  </aside>

  <!-- Offline Banner -->
  <div id="offlineBanner" class="banner banner--warning" style="display:none;position:fixed;top:0;left:0;right:0;z-index:9999;border-radius:0;">
    <span class="banner__text" id="offlineBannerText"><strong>Modo offline</strong> — Mostrando solo tu inventario local.</span>
    <button class="banner__dismiss" onclick="document.getElementById('offlineBanner').style.display='none'" aria-label="Cerrar">&times;</button>
  </div>

  <!-- Upload Supplier Prices Modal -->
  <div id="uploadPricesModal" style="display:none;position:fixed;top:0;left:0;right:0;bottom:0;z-index:9500;background:rgba(0,0,0,0.6);align-items:center;justify-content:center;padding:var(--space-4);">
    <div style="background:var(--color-surface-1);border:1px solid var(--color-border);border-radius:var(--radius-md);max-width:520px;width:100%;max-height:90vh;overflow:auto;padding:var(--space-5);">
      <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:var(--space-4);">
        <h2 style="margin:0;font-family:var(--font-heading);font-size:var(--text-h4);">Subir precios de proveedor</h2>
        <button onclick="CatalogApp.closeUploadPricesModal()" style="background:none;border:none;cursor:pointer;font-size:1.4rem;color:var(--color-text-secondary);">&#10005;</button>
      </div>
      <p style="color:var(--color-text-secondary);font-size:var(--text-body-sm);margin-bottom:var(--space-3);">
        Sube un CSV o Excel con las columnas: <code>supplier_name, sku, price, currency, effective_from</code>.<br>
        El precio se mostrará en el catálogo junto a cada parte del proveedor.
      </p>
      <div style="margin-bottom:var(--space-3);">
        <label style="display:block;font-size:var(--text-caption);color:var(--color-text-muted);margin-bottom:4px;">Archivo CSV / Excel</label>
        <input type="file" id="uploadPricesFile" accept=".csv,.xlsx,.xls" style="width:100%;" />
      </div>
      <div style="display:flex;gap:var(--space-2);justify-content:flex-end;">
        <button class="btn btn--ghost" onclick="CatalogApp.downloadPriceTemplate()">Descargar plantilla</button>
        <button class="btn btn--primary" onclick="CatalogApp.submitUploadPrices()">Subir precios</button>
      </div>
      <div id="uploadPricesStatus" style="margin-top:var(--space-3);font-size:var(--text-body-sm);"></div>
    </div>
  </div>

  <!-- Brand Catalog Overlay (full-screen overlay for brand-first browsing) -->
  <div id="brandCatalogOverlay" style="display:none;position:relative;top:0;left:0;right:0;bottom:0;background:var(--color-bg-base);overflow:auto;padding:var(--space-4);">
    <div style="max-width:1200px;margin:0 auto;">
      <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:var(--space-4);">
        <h2 style="margin:0;font-family:var(--font-heading);font-size:var(--text-h3);">Catalogo por Marca</h2>
        <button onclick="BrandCatalog.hide()" class="btn btn--sm" style="background:none;border:1px solid var(--color-border);color:var(--color-text-primary);padding:8px 16px;border-radius:var(--radius-md);cursor:pointer;">&#10005; Cerrar</button>
      </div>
      <div id="brandCatalogBreadcrumb" style="margin-bottom:var(--space-3);"></div>
      <div id="brandCatalogSearch" style="margin-bottom:var(--space-3);"></div>
      <div id="brandCatalogLoading" class="loading"><div class="spinner"></div></div>
      <div id="brandCatalogContent"></div>
    </div>
  </div>

  <script src="/pos/static/js/i18n.js" defer></script>
  <script src="/pos/static/js/kiosk.js" defer></script>
  <script src="/pos/static/js/app-init.js" defer></script>
  <script src="/pos/static/js/splash-loader.js?v=33" defer></script>
  <script src="/pos/static/js/pos-utils.js?v=33" defer></script>
  <script src="/pos/static/js/sidebar.js" defer></script>
  <script src="/pos/static/js/catalog.js?v=33" defer></script>
  <script src="/pos/static/js/offline-banner.js" defer></script>
  <script src="/pos/static/js/chat.js" defer></script>
  <script src="/pos/static/js/sync-engine.js" defer></script>
  <script src="/pos/static/js/onboarding.js?v=33" defer></script>
  <script>
    if('serviceWorker' in navigator){
      navigator.serviceWorker.register('/pos/sw.js',{scope:'/pos/'}).then(function(reg){
        reg.addEventListener('updatefound', function(){
          var newWorker = reg.installing;
          newWorker.addEventListener('statechange', function(){
            if(newWorker.state === 'activated'){
              window.location.reload();
            }
          });
        });
      });
    }
  </script>
  <script src="/pos/static/js/pwa-install.js" defer></script>
  <script src="/pos/static/js/brand-catalog.js?v=33" defer></script>
</body>
</html>
