/**
 * ============================================================================
 * SarFact HN — Ajuste para celular y tablet
 * ----------------------------------------------------------------------------
 * Solo actúa en pantallas de 860px o menos. En computadora la app se ve igual
 * que siempre. No cambia ninguna regla de negocio.
 * ============================================================================
 */

/* Barra superior y fondo del menú: ocultos en computadora */
.sfm-barra, .sfm-fondo { display:none; }

@media (max-width: 860px) {

  /* ── Menú lateral deslizable ─────────────────────────────── */
  body .sidebar {
    width: min(84vw, 300px);
    transform: translateX(-105%);
    transition: transform .24s ease;
    box-shadow: none;
    z-index: 960;
    height: 100%;
    height: 100dvh;
  }
  body.sfm-menu .sidebar { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.55); }
  body .sidebar .nav { -webkit-overflow-scrolling: touch; }
  body .sidebar { overflow-y: auto; }
  body .sidebar .nav { flex: none; overflow: visible; }
  body .nav-item { padding: 12px 12px; font-size: 15px; }
  body .nav-item:hover { transform: none; }

  .sfm-fondo {
    display: block; position: fixed; inset: 0; z-index: 950;
    background: rgba(0,0,0,.5); opacity: 0; pointer-events: none;
    transition: opacity .2s ease;
  }
  body.sfm-menu .sfm-fondo { opacity: 1; pointer-events: auto; }
  body.sfm-menu { overflow: hidden; }

  /* ── Barra superior fija ─────────────────────────────────── */
  .sfm-barra {
    display: flex; align-items: center; gap: 10px;
    position: fixed; top: 0; left: 0; right: 0; z-index: 940;
    height: 56px; padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    background: var(--surface); border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
  }
  .sfm-btn {
    width: 42px; height: 42px; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 10px; border: 1px solid var(--border);
    background: var(--surface2); color: var(--text);
    font-size: 20px; cursor: pointer; line-height: 1;
  }
  .sfm-titulo { flex: 1; min-width: 0; }
  .sfm-marca { font-family: 'Syne', sans-serif; font-weight: 800; font-size: 13px; color: var(--accent); letter-spacing: .5px; }
  .sfm-seccion { font-size: 16px; font-weight: 800; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .sfm-nueva {
    height: 40px; padding: 0 14px; border: none; border-radius: 10px; flex-shrink: 0;
    background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff;
    font-weight: 800; font-size: 14px; font-family: 'Nunito', sans-serif; cursor: pointer;
  }
  .sfm-nueva[hidden] { display: none; }

  [data-theme="light"] .sfm-barra { background: #fff; }

  /* ── Contenido a todo lo ancho ───────────────────────────── */
  body .main {
    margin-left: 0 !important;
    width: 100% !important;
    padding: 72px 12px 28px !important;
  }
  body .topbar { margin-bottom: 16px; padding-bottom: 14px; gap: 10px; }
  body .topbar > div:first-child { width: 100%; }
  body .page-title { font-size: 22px; }
  body .btn-group { width: 100%; }
  body .topbar .btn-group > select,
  body .topbar .btn-group > input { flex: 1 1 120px; min-width: 0; }

  /* Tarjetas de resumen: dos por fila */
  body .cards-row, body .cards-row-3 { grid-template-columns: 1fr 1fr !important; gap: 10px; margin-bottom: 16px; }
  body .card { padding: 14px; }
  body .card-value { font-size: 18px; word-break: break-word; }
  body .card-label { font-size: 9.5px; letter-spacing: 1px; }

  /* Cuadrículas de dos y tres columnas → una columna */
  body .form-grid, body .form-grid-3, body .report-grid, body .arqueo-grid,
  body .admin-two-col, body .backup-grid { grid-template-columns: 1fr !important; }
  body .main [style*="grid-template-columns:1fr 1fr"],
  body .main [style*="grid-template-columns: 1fr 1fr"],
  body .modal [style*="grid-template-columns:1fr 1fr"],
  body .modal [style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: 1fr !important; }
  body .form-group[style*="visibility:hidden"] { display: none; }

  /* Campos: 16px evita que el iPhone haga zoom al tocar */
  body input, body select, body textarea { font-size: 16px !important; max-width: 100%; }
  body .search-input { max-width: none !important; flex: 1 1 100%; }
  body .search-bar > * { flex: 1 1 100%; }
  body [style*="min-width:200px"] { min-width: 0 !important; }
  body .main [style*="max-width:300px"], body .main [style*="max-width:320px"],
  body .main [style*="max-width:340px"] { max-width: none !important; }

  /* Tablas: se desplazan de lado dentro de su caja, no la pantalla entera */
  body .table-wrap, .sfm-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
  body .table-wrap table, .sfm-scroll > table { min-width: 100%; width: max-content; }
  body .table-wrap th, body .table-wrap td, .sfm-scroll th, .sfm-scroll td { white-space: nowrap; }
  body .table-header { flex-wrap: wrap; gap: 8px; }
  body thead th { padding: 10px 10px; }
  body tbody td { padding: 10px 10px; font-size: 13px; }
  body .action-row { flex-wrap: nowrap; }

  /* Pestañas: deslizables */
  body .tab-row { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  body .tab-row::-webkit-scrollbar { display: none; }
  body .tab { white-space: nowrap; flex-shrink: 0; }

  /* Arqueo */
  body .denom-row { gap: 8px; }
  body .denom-label { width: 70px; font-size: 12px; }
  body .denom-input { width: 64px !important; }
  body .denom-subtotal { min-width: 0; }
  body #sec-arqueo [style*="width:130px"], body #sec-arqueo [style*="width:100px"] { width: auto !important; flex: 1 1 0; min-width: 0; }
  body #sec-arqueo [style*="display:flex"] { flex-wrap: wrap; }
  body .arqueo-total-box, body .arqueo-diff-box { flex-wrap: wrap; gap: 6px; }

  /* Gráficas de barras de reportes */
  body .bar-label { width: 64px; }
  body .bar-val { width: 76px; font-size: 10.5px; }

  /* Mi Empresa: tarjeta de bloqueo */
  body #sec-empresa [style*="width:360px"] { width: 100% !important; padding: 28px 20px !important; }

  /* ── Ventanas (modales) como hoja desde abajo ────────────── */
  body .modal-overlay { padding: 0; align-items: flex-end; }
  body .modal {
    width: 100% !important; max-width: 100% !important;
    max-height: 94vh !important; max-height: 94dvh !important;
    border-radius: 18px 18px 0 0; padding: 18px 14px 0;
    border-left: none; border-right: none; border-bottom: none;
  }
  body .modal-title { font-size: 18px; margin-bottom: 14px; }
  body .modal-footer {
    position: sticky; bottom: 0; z-index: 2;
    background: var(--surface); margin: 16px -14px 0; padding: 12px 14px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    display: flex; gap: 8px; flex-wrap: wrap;
  }
  body .modal-footer .btn { flex: 1 1 0; justify-content: center; padding: 12px 10px; }

  /* Factura: cada ítem se vuelve una tarjeta */
  #modal-factura .invoice-items-table thead { display: none; }
  #modal-factura .invoice-items-table, #modal-factura .invoice-items-table tbody { display: block; width: 100%; }
  #modal-factura .invoice-items-table tr {
    display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px 10px;
    padding: 12px; margin-bottom: 10px; border: 1px solid var(--border);
    border-radius: 12px; background: var(--surface2); position: relative;
  }
  #modal-factura .invoice-items-table td { display: block; padding: 0; min-width: 0; }
  #modal-factura .invoice-items-table td:nth-child(1) { grid-column: 1 / -1; padding-right: 44px; }
  #modal-factura .invoice-items-table td:nth-child(7) { position: absolute; top: 12px; right: 12px; }
  #modal-factura .invoice-items-table td:nth-child(7) .btn { width: 36px; height: 40px; justify-content: center; }
  #modal-factura .invoice-items-table td:nth-child(n+2):nth-child(-n+6)::before {
    display: block; font-size: 10px; font-weight: 800; color: var(--text3);
    text-transform: uppercase; letter-spacing: .8px; margin-bottom: 4px;
  }
  #modal-factura .invoice-items-table td:nth-child(2)::before { content: 'Cantidad'; }
  #modal-factura .invoice-items-table td:nth-child(3)::before { content: 'Precio (L)'; }
  #modal-factura .invoice-items-table td:nth-child(4)::before { content: 'Desc. %'; }
  #modal-factura .invoice-items-table td:nth-child(5)::before { content: 'ISV'; }
  #modal-factura .invoice-items-table td:nth-child(6) { grid-column: 2 / -1; text-align: right; font-size: 15px !important; font-weight: 800; color: var(--text) !important; }
  #modal-factura .invoice-items-table td:nth-child(6)::before { content: 'Subtotal'; text-align: right; }
  #modal-factura .invoice-items-table td input,
  #modal-factura .invoice-items-table td select { width: 100% !important; padding: 10px !important; font-size: 16px !important; }
  #modal-factura .invoice-total-row { justify-content: space-between; gap: 10px; }
  #modal-factura .invoice-total-row.grand { font-size: 20px; }
  #mp-filas > div { flex-wrap: wrap; }
  #mp-filas > div > select { flex: 1 1 100% !important; padding: 11px 10px !important; }
  #mp-filas > div > input { flex: 1 1 auto; width: auto !important; min-width: 0; }
  #mp-filas > div > .btn { height: 44px; width: 44px; justify-content: center; }

  /* Factura: encabezado de productos con botones que no se salen */
  #modal-factura .modal > div[style*="justify-content:space-between"] { flex-wrap: wrap; gap: 8px; }

  /* Avisos */
  #toast-container { left: 12px; right: 12px; bottom: auto; top: calc(64px + env(safe-area-inset-top)); z-index: 99999; }
  #toast-container > * { max-width: none; }

  /* Botones más fáciles de tocar */
  body .btn { padding: 10px 14px; }
  body .btn-sm { padding: 8px 12px; }
  body .btn-xs { padding: 6px 10px; }
}

@media (max-width: 380px) {
  body .cards-row, body .cards-row-3 { grid-template-columns: 1fr !important; }
  .sfm-nueva { padding: 0 10px; font-size: 13px; }
}

@media print {
  .sfm-barra, .sfm-fondo { display: none !important; }
}

/* Tarjeta de la empresa en el Dashboard: apilada en pantallas angostas */
@media (max-width: 768px) {
  #dash-empresa-banner { flex-wrap: wrap !important; gap: 12px !important; padding: 14px !important; }
  #dash-logo-wrap { width: 56px !important; height: 56px !important; }
  #dash-logo-placeholder { font-size: 26px !important; }
  #dash-banner-nombre { font-size: 17px !important; white-space: normal !important; }
  #dash-empresa-banner > div:last-child { flex-basis: 100% !important; text-align: left !important; }
  #dash-turno-badge { display: inline-block !important; }
}
