/* ============================================================================
 * styles-mobile.css — ERP responsive móvil
 * Breakpoint principal: <=768px (phone + tablet vertical)
 * Cargado DESPUES de styles.css. Necesita !important para vencer al bloque
 * "FIX FINAL V1.36" de styles.css (línea ~2573-2820) que define el menu
 * horizontal-top-bar de desktop con muchas reglas !important.
 *
 * Scope mobile (7 modulos visibles): Inicio, Ventas, Compras, Stock,
 * Clientes, Tesoreria, Informes.
 *
 * Modulos ocultos en mobile (data-mobile-hide en index.html):
 * Productos, Integraciones, CRM, Proveedores, Configuracion.
 * ============================================================================ */

/* Footer del sidebar (user + logout) - oculto por defecto en desktop */
.sidebar-mobile-footer {
  display: none;
}

@media (max-width: 768px) {
  /* === LAYOUT base === */
  html {
    -webkit-text-size-adjust: 100%;
  }

  /* Override v1.36 #app display:block para que el drawer sea fixed sobre todo */
  #app {
    display: block !important;
    flex-direction: row;
  }

  /* === SIDEBAR: drawer deslizante vertical (override v1.36 horizontal bar) === */
  .sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: min(86vw, 320px) !important;
    height: 100vh !important;
    min-height: 100vh !important;
    background: #0f172a !important;
    color: #e5e7eb !important;
    z-index: 1001 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch;
    padding: 14px 12px 24px !important;
    border-bottom: 0 !important;
    box-shadow: 0 0 30px rgba(0,0,0,0) !important;
    transform: translateX(-100%);
    transition: transform .25s ease, box-shadow .25s ease;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 0 0 30px rgba(0,0,0,.45) !important;
  }

  /* Main toma todo el ancho cuando el drawer esta cerrado */
  main, .main {
    width: 100% !important;
    margin-left: 0 !important;
  }

  /* === SIDEBAR: ocultar modulos out-of-scope === */
  .sidebar [data-mobile-hide] {
    display: none !important;
  }

  /* === SIDEBAR HEADER (brand) — version mobile, override v1.36 === */
  .sidebar-header {
    padding: 8px 4px 12px !important;
    margin-bottom: 10px !important;
    border-bottom: 1px solid rgba(255,255,255,.12) !important;
    background: transparent !important;
  }
  .sidebar .brand {
    max-width: none !important;
    margin: 0 !important;
    padding: 4px 8px !important;
    border-bottom: 0 !important;
    color: #fff !important;
  }
  .sidebar .brand-icon {
    width: 36px !important;
    height: 36px !important;
    border-radius: 10px !important;
    font-size: 13px !important;
  }
  .sidebar .brand h1 {
    color: #fff !important;
    font-size: 14px !important;
  }
  .sidebar .brand p {
    color: #cbd5e1 !important;
    font-size: 11px !important;
  }
  .sidebar-header .brand-empresa span {
    color: #fff !important;
  }

  /* === MENU vertical, override v1.36 horizontal === */
  .sidebar .menu {
    display: flex !important;
    flex-direction: column !important;
    flex-wrap: nowrap !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    gap: 2px !important;
    padding: 0 !important;
    overflow-x: visible !important;
    overflow-y: visible !important;
    background: transparent !important;
    position: relative !important;
    z-index: auto !important;
  }

  /* === Menu groups vertical block === */
  .sidebar .menu-group,
  .sidebar .menu-group.single {
    position: relative !important;
    display: block !important;
    flex: 0 0 auto !important;
    width: 100% !important;
    min-width: 0 !important;
    overflow: visible !important;
  }

  /* === Menu-parent: oculto en mobile (lista flat sin section labels) === */
  .sidebar .menu-parent {
    display: none !important;
  }
  .sidebar .menu-parent::before,
  .sidebar .menu-parent::after {
    display: none !important;
    content: none !important;
  }

  /* === Submenu: bloque vertical inline (no absolute dropdown) === */
  .sidebar .submenu {
    display: block !important;
    position: static !important;
    left: auto !important;
    top: auto !important;
    min-width: 0 !important;
    width: 100% !important;
    max-width: none !important;
    padding: 0 !important;
    background: transparent !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    z-index: auto !important;
    overflow: visible !important;
  }

  /* === Items leaves: full-width, vertical, touch-friendly === */
  .sidebar .menu-item,
  .sidebar .submenu .menu-item {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    border: 0 !important;
    border-radius: 10px !important;
    margin: 0 !important;
    padding: 13px 14px !important;
    background: transparent !important;
    color: #e5e7eb !important;
    font-size: 15px !important;
    font-weight: 500 !important;
    line-height: 1.3 !important;
    white-space: normal !important;
    text-align: left !important;
    box-shadow: none !important;
    min-height: 48px !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(96,165,250,.25);
    cursor: pointer;
  }

  .sidebar .menu-item:hover,
  .sidebar .menu-item:active,
  .sidebar .submenu .menu-item:hover,
  .sidebar .submenu .menu-item:active {
    background: rgba(255,255,255,.08) !important;
    color: #fff !important;
  }

  .sidebar .menu-item.active,
  .sidebar .submenu .menu-item.active {
    background: #2563eb !important;
    color: #fff !important;
    font-weight: 700 !important;
  }

  /* === Sidebar mobile footer (user + logout) === */
  .sidebar-mobile-footer {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    margin-top: 16px !important;
    padding: 14px 8px !important;
    border-top: 1px solid rgba(255,255,255,.12) !important;
  }

  .sidebar-mobile-user {
    color: #cbd5e1 !important;
    font-size: 12px !important;
    font-weight: 600 !important;
    word-break: break-word;
    line-height: 1.3;
  }

  .sidebar-mobile-footer #sidebar-mobile-logout {
    width: 100% !important;
    min-height: 44px !important;
    font-size: 14px !important;
    font-weight: 600 !important;
    background: #ef4444 !important;
    color: #fff !important;
    border: 0 !important;
    border-radius: 8px !important;
    padding: 10px 14px !important;
  }
  .sidebar-mobile-footer #sidebar-mobile-logout:hover,
  .sidebar-mobile-footer #sidebar-mobile-logout:active {
    background: #dc2626 !important;
  }

  /* === BACKDROP === */
  .mobile-backdrop {
    z-index: 1000 !important;
  }

  /* === TOPBAR: override v1.36 top: 116px (sidebar es horizontal en desktop) === */
  .topbar {
    position: sticky !important;
    top: 0 !important;
    z-index: 20 !important;
    flex-direction: row !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
    gap: 8px !important;
    padding: 10px 12px !important;
    justify-content: flex-start !important;
  }

  .topbar h2 {
    font-size: 17px !important;
    margin: 0 !important;
    line-height: 1.2 !important;
  }
  .topbar p,
  #page-subtitle {
    display: none !important;
  }

  .top-actions {
    margin-left: auto !important;
    display: flex !important;
    align-items: center !important;
    gap: 6px !important;
    flex-wrap: nowrap !important;
  }

  /* Hide noisy top elements */
  .top-actions .global-master-search,
  #global-master-search-input,
  #topbar-empresa-switch,
  #btn-candado,
  #ml-status-badge,
  #current-user-badge,
  #api-status,
  #crm-notif-wrap,
  #btn-logout,
  /* Sprint J6 (2026-05-10): "Mi cuenta" se mueve al footer mobile del
     drawer junto al logout. En el topbar reducido apretaba demasiado. */
  #btn-mi-cuenta {
    display: none !important;
  }

  #btn-refresh {
    padding: 8px 10px !important;
    font-size: 13px !important;
    min-height: 38px !important;
    white-space: nowrap;
  }

  .mobile-menu-toggle {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 26px !important;
    padding: 4px 10px !important;
    min-width: 44px !important;
    min-height: 44px !important;
    margin-right: 0 !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(0,0,0,.1);
  }

  /* === VIEW: override v1.36 max-width 1440px === */
  .view {
    max-width: none !important;
    margin: 0 !important;
    padding: 12px !important;
  }

  /* === GRIDS: 1 columna === */
  .grid-2,
  .grid-3,
  .grid-4,
  .form-grid {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }

  /* === CARDS: padding compacto === */
  .card {
    padding: 14px;
    border-radius: 10px;
  }
  .card h3 {
    font-size: 16px;
    margin: 0 0 10px;
  }

  /* === INPUTS / BOTONES touch-friendly === */
  .input,
  select.input,
  textarea.input,
  input[type="text"].input,
  input[type="number"].input,
  input[type="email"].input,
  input[type="tel"].input,
  input[type="password"].input,
  input[type="date"].input {
    min-height: 42px;
    font-size: 15px;
  }
  .btn,
  button.btn {
    min-height: 40px;
    padding: 8px 14px;
    font-size: 14px;
  }

  /* === TABLAS data-entry / autocomplete: scroll horizontal con touch ===
     Las tablas con .table-wrap-autocomplete tienen comportamiento especial
     (sticky, autocomplete) y no se transforman a cards — siguen scrolleando. */
  .table-wrap-autocomplete {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-left: -12px;
    margin-right: -12px;
    padding-left: 12px;
    padding-right: 12px;
  }
  .table-wrap-autocomplete table { font-size: 13px; }
  .table-wrap-autocomplete th,
  .table-wrap-autocomplete td {
    padding: 8px 6px;
    white-space: nowrap;
  }

  /* === LISTADOS: tablas como cards verticales (estilo PWA) ===
     Para listados regulares (.table-wrap.mobile-cards) en mobile, cada fila
     se vuelve un card con label/valor por columna. El helper JS
     applyMobileTableLabels() agrega `data-label` a cada <td> leyendo
     los <th> del thead — eso es lo que aparece como "etiqueta" via ::before.
     Tablas que no quieren la transformación: agregar clase no-mobile-cards. */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-left: -12px;
    margin-right: -12px;
    padding-left: 12px;
    padding-right: 12px;
  }
  .table-wrap.no-mobile-cards table { font-size: 13px; }
  .table-wrap.no-mobile-cards th,
  .table-wrap.no-mobile-cards td {
    padding: 8px 6px;
    white-space: nowrap;
  }

  /* Card layout: solo si la tabla está marcada con .mobile-cards
     (lo agrega el helper auto al detectar .table-wrap sin no-mobile-cards). */
  .table-wrap.mobile-cards { overflow-x: visible; }
  .table-wrap.mobile-cards table {
    display: block;
    width: 100%;
    font-size: 14px;
    border-collapse: separate;
    border-spacing: 0;
  }
  .table-wrap.mobile-cards thead { display: none; }
  .table-wrap.mobile-cards tbody { display: block; }
  .table-wrap.mobile-cards tr {
    display: block;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    margin-bottom: 8px;
    padding: 10px 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
  }
  .table-wrap.mobile-cards tr.cliente-row-selected,
  .table-wrap.mobile-cards tr.venta-row-selected,
  .table-wrap.mobile-cards tr.compra-row-selected,
  .table-wrap.mobile-cards tr.row-selected {
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, .18);
  }
  .table-wrap.mobile-cards td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
    border: 0;
    border-bottom: 1px dashed #f3f4f6;
    white-space: normal;
    word-break: break-word;
    text-align: right;
  }
  .table-wrap.mobile-cards td:last-child { border-bottom: 0; }
  .table-wrap.mobile-cards td::before {
    content: attr(data-label);
    color: #6b7280;
    font-size: 12px;
    font-weight: 500;
    text-align: left;
    flex: 0 0 38%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  /* Celdas que no llevan label (sin data-label = celdas de acciones,
     checkboxes, etc.) se centran sin mostrar el ::before. */
  .table-wrap.mobile-cards td:not([data-label])::before,
  .table-wrap.mobile-cards td[data-label=""]::before { display: none; }
  .table-wrap.mobile-cards td:not([data-label]),
  .table-wrap.mobile-cards td[data-label=""] {
    justify-content: center;
    text-align: center;
  }
  /* Celdas con botones de acción (.actions) — bloque centrado, sin label. */
  .table-wrap.mobile-cards td:has(> .actions),
  .table-wrap.mobile-cards td:has(> button:only-child) {
    justify-content: center;
  }
  .table-wrap.mobile-cards td:has(> .actions)::before,
  .table-wrap.mobile-cards td:has(> button:only-child)::before { display: none; }
  .table-wrap.mobile-cards td .actions {
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
  }
  /* Sortable headers no se ven cuando el thead está oculto, pero por las
     dudas anular el cursor para no confundir si algún día se muestran. */
  .table-wrap.mobile-cards th.sortable-th { cursor: default; }

  /* === MODALES: fullscreen === */
  .app-modal,
  .bulk-modal,
  .comprobante-modal {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
    margin: 0 !important;
    inset: 0 !important;
  }
  .app-modal-overlay,
  .bulk-modal-backdrop,
  .comprobante-modal-overlay {
    padding: 0 !important;
  }
  .app-modal-body,
  .comprobante-modal-body {
    max-height: calc(100vh - 110px) !important;
  }

  /* === OPEN-TABS hidden === */
  .open-tabs {
    display: none !important;
  }
}

/* ============================================================================
 * PHONE estricto <=480px - mas compactacion
 * ============================================================================ */
@media (max-width: 480px) {
  .topbar {
    padding: 8px 10px !important;
  }
  .topbar h2 {
    font-size: 15px !important;
  }
  #btn-refresh {
    padding: 6px !important;
    font-size: 0 !important;
    min-width: 36px !important;
    min-height: 36px !important;
  }
  #btn-refresh::before {
    content: "↻";
    font-size: 18px;
  }
  .view {
    padding: 10px !important;
  }
  .card {
    padding: 12px;
    border-radius: 8px;
  }
  .sidebar .menu-item,
  .sidebar .submenu .menu-item {
    font-size: 14px !important;
    padding: 12px 14px !important;
  }
}
