
  :root {
    --green: #F47920;
    --green-light: #F69444;
    --green-pale: #FEF0E6;
    --yellow: #58595B;
    --border: #ddd;
    --text: #222;
    --subtext: #666;
    --shadow: 0 2px 8px rgba(0,0,0,0.1);
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body { font-family: 'Segoe UI', sans-serif; background: #f5f5f5; color: var(--text); }
  header {
    background: white;
    padding: 8px 16px;
    padding-top: env(safe-area-inset-top, 8px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
    border-bottom: 3px solid var(--green);
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  }
  .header-logo { height: 36px; object-fit: contain; }
  .header-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--green);
    letter-spacing: 0.3px;
    white-space: nowrap;
  }
  @media (max-width: 400px) {
    .header-title { display: none; }
  }
  #sync-btn {
    background: var(--green);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 7px 16px;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
  }
  #sync-btn:hover { background: var(--green-light); }
  #sync-btn:disabled { opacity: 0.6; }
  #logout-btn {
    background: #eee;
    color: #555;
    border: none;
    border-radius: 8px;
    padding: 7px 10px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: background 0.2s;
  }
  #logout-btn:hover { background: #e0e0e0; }

  /* GO TOP BUTTON */
  #go-top-btn {
    display: none;
    position: fixed;
    bottom: 24px;
    right: 20px;
    background: var(--green);
    color: white;
    border: none;
    border-radius: 24px;
    padding: 10px 18px;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(244,121,32,0.4);
    z-index: 50;
    transition: all 0.2s;
  }
  #go-top-btn.visible { display: flex; align-items: center; gap: 6px; }
  #go-top-btn:hover { background: var(--green-light); transform: translateY(-2px); }
  main { max-width: 900px; margin: 0 auto; padding: 16px; }
  .instructions {
    background: white;
    border-left: 4px solid var(--yellow);
    border-radius: 8px;
    padding: 10px 16px;
    margin-bottom: 16px;
    font-size: 0.82rem;
    color: var(--subtext);
    box-shadow: var(--shadow);
  }
  .instructions strong { color: var(--green); display: block; margin-bottom: 6px; }
  .search-box {
    background: white;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
  }
  .search-row { display: flex; gap: 8px; margin-bottom: 12px; }
  #search-input {
    flex: 1;
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    outline: none;
  }
  #search-input:focus { border-color: var(--green-light); }
  #clear-btn {
    background: #eee;
    border: none;
    border-radius: 8px;
    padding: 10px 14px;
    cursor: pointer;
    font-size: 0.85rem;
    color: #555;
  }
  .filters-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 8px;
  }
  .ms-wrap { position: relative; }
  .ms-btn { width:100%; padding:8px 10px; border:1.5px solid var(--border); border-radius:8px; font-size:0.82rem; background:white; text-align:left; cursor:pointer; display:flex; align-items:center; justify-content:space-between; gap:4px; color:var(--text); white-space:nowrap; overflow:hidden; }
  .ms-btn.active { border-color:var(--green); background:var(--green-pale); color:var(--green); font-weight:600; }
  .ms-btn .ms-label { overflow:hidden; text-overflow:ellipsis; flex:1; }
  .ms-btn .ms-arrow { font-size:0.65rem; flex-shrink:0; }
  .ms-dropdown { display:none; position:absolute; top:calc(100% + 4px); left:0; min-width:220px; background:white; border:1.5px solid var(--border); border-radius:10px; box-shadow:0 8px 24px rgba(0,0,0,0.12); z-index:300; overflow:hidden; }
  .ms-dropdown.open { display:block; }
  .ms-search { width:100%; padding:8px 12px; border:none; border-bottom:1px solid var(--border); font-size:0.82rem; outline:none; background:#fafafa; }
  .ms-options { max-height:200px; overflow-y:auto; }
  .ms-option { display:flex; align-items:center; gap:8px; padding:8px 12px; font-size:0.82rem; cursor:pointer; color:var(--text); }
  .ms-option:hover { background:var(--green-pale); }
  .ms-option input[type=checkbox] { accent-color:var(--green); width:14px; height:14px; flex-shrink:0; }
  .ms-option.checked { background:var(--green-pale); color:var(--green); font-weight:600; }
  .ms-empty { padding:10px 12px; font-size:0.8rem; color:var(--subtext); text-align:center; }
  .ms-more { padding:8px 12px; text-align:center; border-top:1px solid var(--border); }
  .ms-more button {
    width:100%;
    background:var(--green-pale);
    color:var(--green);
    border:1px solid var(--green);
    border-radius:6px;
    padding:6px 14px;
    font-size:0.78rem;
    font-weight:700;
    cursor:pointer;
    transition:background 0.2s, color 0.2s;
  }
  .ms-more button:hover { background:var(--green); color:white; }
  .ms-footer { padding:8px 12px; border-top:1px solid var(--border); display:flex; justify-content:space-between; align-items:center; background:#fafafa; }
  .ms-clear { font-size:0.75rem; color:var(--subtext); cursor:pointer; background:none; border:none; padding:0; }
  .ms-clear:hover { color:var(--green); }
  .ms-ok { background:var(--green); color:white; border:none; border-radius:6px; padding:5px 14px; font-size:0.8rem; font-weight:700; cursor:pointer; }
  .ms-ok:hover { background:var(--green-light); }
  .results-meta { font-size: 0.85rem; color: var(--subtext); margin-bottom: 10px; }
  .results-meta strong { color: var(--green); }
  .table-wrapper {
    background: white;
    border-radius: 12px;
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: 16px;
  }
  table { width: 100%; border-collapse: collapse; }
  thead { background: var(--green); color: white; }
  thead th { padding: 11px 14px; text-align: left; font-size: 0.82rem; font-weight: 600; }
  tbody tr {
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: filter 0.15s;
    border-left: 4px solid transparent;
  }
  tbody tr:hover { filter: brightness(0.95); }
  tbody tr.selected { box-shadow: inset 0 0 0 2px var(--green); }
  tbody td { padding: 10px 14px; font-size: 0.85rem; vertical-align: middle; }
  td.td-precio { font-weight: 700; color: var(--green); white-space: nowrap; text-align: right; }
  td.td-inv { text-align: center; font-weight: 600; }
  .inv-pill {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.78rem;
    font-weight: 700;
    min-width: 36px;
  }
  .inv-pill.ok   { background: #d4edda; color: #155724; }
  .inv-pill.low  { background: #fff3cd; color: #856404; }
  .inv-pill.none { background: #f8d7da; color: #721c24; }
  .tag {
    background: var(--green-pale);
    color: var(--green);
    border-radius: 12px;
    padding: 2px 8px;
    font-size: 0.75rem;
    font-weight: 600;
  }
  .detail-panel {
    background: white;
    border-radius: 12px;
    box-shadow: var(--shadow);
    overflow: hidden;
    display: none;
  }
  .detail-panel.visible { display: block; }
  .detail-header {
    background: var(--green);
    color: white;
    padding: 14px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
  /* INICIO: Logo Fertrac en el banner de detalle */
  /* El logo va pegado al título (no centrado en todo el banner), para que
     nunca quede montado sobre el texto en pantallas angostas. */
  .detail-title-group {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1 1 auto;
  }
  .detail-header h2 {
    font-size: 1rem;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .detail-header-logo {
    height: 32px;
    width: auto;
    object-fit: contain;
    flex-shrink: 0;
  }
  @media (max-width: 600px) {
    .detail-header-logo { height: 24px; }
  }
  /* FIN: Logo Fertrac en el banner de detalle */
  .close-btn {
    background: rgba(255,255,255,0.2);
    border: none;
    color: white;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    cursor: pointer;
    font-size: 1.1rem;
  }
  .detail-body { padding: 18px; }
  .detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
  }
  @media (max-width: 600px) {
    .detail-grid { grid-template-columns: 1fr; }
    .filters-grid { grid-template-columns: 1fr 1fr; }
  }
  .detail-section h3 {
    font-size: 0.78rem;
    text-transform: uppercase;
    color: var(--subtext);
    letter-spacing: 0.8px;
    margin-bottom: 10px;
    border-bottom: 2px solid var(--green-pale);
    padding-bottom: 4px;
  }
  /* INICIO: Logo Fertrac junto a "Producto" (thead) y "Precios" (h3) (agregado por Claude) */
  .th-logo,
  .h3-logo {
    height: 38px;
    width: auto;
    object-fit: contain;
    vertical-align: middle;
    margin-left: 8px;
  }
  /* FIN: Logo Fertrac junto a "Producto" y "Precios" */
  .detail-row { margin-bottom: 8px; }
  .detail-row label { font-size: 0.75rem; color: var(--subtext); display: block; }
  .detail-row span { font-size: 0.9rem; font-weight: 600; }
  .price-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
    margin-top: 8px;
  }
  .price-card {
    background: var(--green-pale);
    border-radius: 10px;
    padding: 12px;
    text-align: center;
  }
  .price-card label { font-size: 0.72rem; color: var(--subtext); display: block; margin-bottom: 4px; }
  .price-card .amount { font-size: 1.05rem; font-weight: 700; color: var(--green); }
  .price-card.promo { background: #e6d7cf; }
  .price-card.promo .amount { color: #ef5f06; }
  .price-card.neto5 { background: #E8D7CF; }
  .price-card.neto5 .amount { color: #A14A2A; }
  .price-card.neto8 { background: #DDBFB2; }
  .price-card.neto8 .amount { color: #7F3218; }
  .info-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
    margin-top: 8px;
  }
  .info-card {
    border-radius: 10px;
    padding: 12px;
    text-align: center;
  }
  .info-card label { font-size: 0.72rem; color: var(--subtext); display: block; margin-bottom: 6px; }
  .info-card .value { font-size: 0.95rem; font-weight: 700; }
  .info-card.condicion { background: #9bc0d9; }
  .info-card.condicion .value { color: #455A64; }
  .product-image {
    width: 100%;
    max-width: 200px;
    border-radius: 10px;
    border: 1px solid var(--border);
    display: block;
    margin: 0 auto 12px;
    object-fit: contain;
    height: 160px;
    background: #f5f5f5;
  }
  .no-image {
    width: 100%;
    max-width: 200px;
    height: 160px;
    border-radius: 10px;
    border: 2px dashed var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--subtext);
    font-size: 0.8rem;
    margin: 0 auto 12px;
  }
  .inv-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.9rem;
  }
  .inv-ok { background: #d4edda; color: #155724; }
  .inv-low { background: #fff3cd; color: #856404; }
  .inv-none { background: #f8d7da; color: #721c24; }
  #sync-status {
    font-size: 0.75rem;
    text-align: center;
    color: var(--subtext);
    padding: 6px;
    margin-bottom: 10px;
  }
  .loading { text-align: center; padding: 40px; color: var(--subtext); }
  .spinner {
    width: 36px; height: 36px;
    border: 4px solid #eee;
    border-top-color: var(--green);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
  .empty-state { text-align: center; padding: 40px; color: var(--subtext); font-size: 0.9rem; }

  /* MOBILE TABLE */
  @media (max-width: 600px) {
    thead { display: none; }
    tbody tr {
      display: grid;
      grid-template-columns: 1fr auto;
      grid-column-gap: 10px;
      padding: 10px 14px;
      border-bottom: 1px solid var(--border);
    }
    tbody td { display: block; padding: 0; border: none; font-size: 0.83rem; }
    tbody td.td-ref       { grid-column: 1; grid-row: 1; font-weight: 700; font-size: 0.9rem; margin-bottom: 4px; }
    tbody td.td-inv       { grid-column: 2; grid-row: 1; text-align: right; align-self: start; }
    tbody td.td-marca     { grid-column: 1; grid-row: 2; margin-bottom: 3px; }
    tbody td.td-precio    { grid-column: 2; grid-row: 2; text-align: right; align-self: center; font-size: 0.88rem; }
    tbody td.td-producto  { grid-column: 1 / -1; grid-row: 3; color: var(--subtext); font-size: 0.8rem; margin-top: 3px; }
  }
  .offline-banner {
    background: #fff3cd;
    color: #856404;
    text-align: center;
    padding: 8px;
    font-size: 0.82rem;
    display: none;
  }
  .offline-banner.visible { display: block; }

  /* LOGIN SCREEN */
  #login-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: #f5f5f5;
    padding: 20px;
  }
  .login-card {
    background: white;
    border-radius: 14px;
    padding: 40px 32px;
    max-width: 380px;
    width: 100%;
    box-shadow: 0 4px 24px rgba(0,0,0,0.1);
    text-align: center;
  }
  .login-logo { height: 60px; object-fit: contain; margin-bottom: 20px; }
  .login-card h2 { font-size: 1.3rem; color: var(--gray-dark); margin-bottom: 8px; }
  .login-card p { font-size: 0.85rem; color: var(--gray-mid); margin-bottom: 28px; }
  .login-divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
  #access-denied {
    display: none;
    background: #f8d7da;
    color: #721c24;
    border-radius: 8px;
    padding: 12px;
    font-size: 0.85rem;
    margin-top: 16px;
  }
  #app-content { display: none; }

  /* iOS INSTALL BANNER */
  #ios-install-banner {
    display: none;
    background: white;
    border-top: 3px solid var(--green);
    padding: 12px 16px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 200;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.12);
    align-items: center;
    gap: 12px;
  }
  #ios-install-banner.visible { display: flex; }
  .ios-banner-text { flex: 1; font-size: 0.82rem; color: var(--text); line-height: 1.4; }
  .ios-banner-text strong { color: var(--green); display: block; margin-bottom: 2px; }
  #ios-banner-close {
    background: none;
    border: none;
    font-size: 1.2rem;
    color: #aaa;
    cursor: pointer;
    padding: 4px;
    flex-shrink: 0;
  }

  /* ══════════════════════════════════════════════════════════════════════
     FASE 2 — RESPONSIVE v2
     dvh/svh (iOS), meta delgada, safe-area (notch/home-indicator),
     doblez/dobles pliegues (~280-320px), landscape bajo, clamp() fluido.
     Va al final: sobrescribe reglas previas por cascada.
     ══════════════════════════════════════════════════════════════════════ */

  /* Altura dinámica del viewport (la barra de direcciones de iOS colapsa) */
  #login-screen { min-height: 100vh; min-height: 100dvh; }

  /* Botón "ir arriba": nunca debajo del home-indicator / rincón redondeado */
  #go-top-btn {
    right: max(16px, env(safe-area-inset-right, 0px));
    bottom: max(16px, env(safe-area-inset-bottom, 0px));
  }

  /* Banner de instalación iOS pegado abajo: respeta la zona segura */
  #ios-install-banner {
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  /* Tipografía fluida: escala continua, sin saltos bruscos */
  .header-title { font-size: clamp(0.75rem, 2.5vw, 0.95rem); }
  .login-card h2 { font-size: clamp(1.15rem, 6vw, 1.4rem); }
  #search-input  { font-size: clamp(0.9rem, 2.6vw, 1rem); }
  .price-card .amount { font-size: clamp(0.95rem, 3.2vw, 1.15rem); }

  /* Logos de marca: más chicos en pantallas angostas para no robar espacio */
  @media (max-width: 600px) {
    .detail-header-logo { height: 22px; }
    .th-logo, .h3-logo { height: 26px; margin-left: 6px; }
  }

  /* Doblez/pliegues: ~380px y menos (Galaxy Fold/Z Flip exterior, mini) */
  @media (max-width: 380px) {
    header {
      padding: 8px 10px;
      gap: 6px;
    }
    main { padding: 12px; }
    .search-box { padding: 12px; }
    .search-row { gap: 6px; }
    #search-input { padding: 9px 12px; font-size: 0.9rem; }
    #clear-btn { padding: 9px 10px; font-size: 0.8rem; }
    .filters-grid { grid-template-columns: 1fr 1fr; gap: 6px; }
    .ms-btn { font-size: 0.78rem; padding: 7px 8px; }
    .ms-dropdown {
      min-width: 0;
      max-width: calc(100vw - 20px);
      left: 0;
      right: auto;
    }
    .instructions { padding: 8px 12px; font-size: 0.76rem; }
    .detail-body { padding: 14px; }
    .detail-section h3 { font-size: 0.74rem; }
    #sync-btn { padding: 7px 10px; font-size: 0.75rem; }
    #logout-btn { padding: 7px 8px; font-size: 0.75rem; }
    #sync-status { display: none; }      /* gana aire al header angosto */
    tbody tr { padding: 8px 10px; grid-column-gap: 8px; }
    tbody td { font-size: 0.8rem; }
  }

  /* Pliegue cerrado (~280px): una sola columna de filtros y compacto */
  @media (max-width: 320px) {
    .filters-grid { grid-template-columns: 1fr; }
    main { padding: 10px; }
    .price-cards, .info-cards { grid-template-columns: 1fr 1fr; gap: 8px; }
    .detail-grid { gap: 12px; }
    .login-card { padding: 28px 20px; }
    .login-logo { height: 52px; }
    #go-top-btn { padding: 8px 14px; font-size: 0.8rem; }
  }

  /* Landscape con poca altura (móvil acostado): el buscador manda */
  @media (orientation: landscape) and (max-height: 480px) {
    #login-screen { min-height: 100vh; min-height: 100svh; min-height: 100dvh; }
    header { padding: 6px 12px; }
    main { padding: 10px; }
    .instructions { display: none; }
    .search-box { padding: 10px; margin-bottom: 10px; }
    .table-wrapper { margin-bottom: 10px; }
    .detail-body { padding: 12px; }
    #go-top-btn { bottom: max(10px, env(safe-area-inset-bottom, 0px)); }
  }

  /* Notch largo / pantallas 21:9 */
  body { -webkit-tap-highlight-color: transparent; }

  /* Escritorios amplios: el listado aprovecha el ancho extra */
  @media (min-width: 1000px) {
    main { max-width: 1080px; }
    .detail-header h2 { font-size: 1.15rem; }
  }

/* ============================================================
   FASE 4 — Miniaturas en la tabla (carga lazy + caché runtime)
   ============================================================ */
td.td-img {
  width: 64px;
  padding: 6px 4px;
  vertical-align: middle;
}
.cell-thumb {
  width: 56px;
  height: 56px;
  margin: 0 auto;
  border-radius: 8px;
  overflow: hidden;
  background: #f2f2f2;
  border: 1px solid #e2e2e2;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.cell-thumb.empty {
  background: repeating-linear-gradient(45deg, #f6f6f6, #f6f6f6 6px, #eeeeee 6px, #eeeeee 12px);
}
.cell-thumb.empty::after {
  content: '📦';
  opacity: 0.35;
  font-size: 20px;
}
.row-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
@media (max-width: 600px) {
  td.td-img { width: 48px; padding: 6px 2px; }
  .cell-thumb { width: 42px; height: 42px; }
}
@media (max-width: 380px) {
  td.td-img { width: 40px; }
  .cell-thumb { width: 36px; height: 36px; }
}
