/* ═══════════════════════════════════════════════════════
   SELLPOINT POS — RESPONSIVE STYLES
   Breakpoints:
     ≥ 1600px  → Large monitor / TV
     ≥ 1920px  → Ultra-wide / 4K
     ≤ 1280px  → Laptop medium
     ≤ 1024px  → Tablet / small laptop
     ≤ 768px   → Large phone / portrait tablet
     ≤ 480px   → Phone
═══════════════════════════════════════════════════════ */

/* ══════════════════════════════════════
   LARGE SCREENS ≥ 1600px (TV / Big Monitor)
══════════════════════════════════════ */
@media (min-width: 1600px) {
  html { font-size: 15.5px; }
  :root { --sidebar-width: 256px; --topbar-height: 64px; }

  .section { padding: 2.25rem 2.75rem; }
  .pos-ticket-panel { width: 380px; min-width: 360px; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 1rem; }
  .product-card-img { width: 88px; height: 88px; }
  .stats-grid { grid-template-columns: repeat(6, 1fr); }
  .charts-grid { grid-template-columns: 2fr 1fr 1fr; }
  .modal { max-width: 620px; }
}

@media (min-width: 1920px) {
  html { font-size: 16.5px; }
  :root { --sidebar-width: 280px; --topbar-height: 68px; }

  .section { padding: 2.75rem 3.5rem; }
  .pos-ticket-panel { width: 440px; min-width: 420px; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(185px, 1fr)); gap: 1.1rem; }
  .product-card-img { width: 100px; height: 100px; }
}

/* ══════════════════════════════════════
   LAPTOP MEDIUM ≤ 1280px
══════════════════════════════════════ */
@media (max-width: 1280px) {
  :root { --sidebar-width: 210px; }
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
  .pos-ticket-panel { width: 290px; min-width: 270px; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
}

/* ══════════════════════════════════════
   TABLET / SMALL LAPTOP ≤ 1024px
══════════════════════════════════════ */
@media (max-width: 1024px) {
  /* Auto-collapse sidebar to icon-only */
  .sidebar {
    width: 64px !important;
    min-width: 64px !important;
    position: relative;
  }
  .sidebar .nav-label,
  .sidebar .logo-text,
  .sidebar .seller-info { display: none !important; }
  .sidebar .sidebar-header { justify-content: center; padding: 1rem 0; }
  .sidebar .nav-item { justify-content: center; padding: 0.65rem 0; }
  .sidebar .seller-profile { justify-content: center; padding: 0.65rem 0; }

  /* When sidebar-toggle re-opens it on tablet */
  .sidebar.force-open {
    width: var(--sidebar-width) !important;
    min-width: var(--sidebar-width) !important;
    position: absolute; height: 100%; z-index: 200;
    box-shadow: 4px 0 24px rgba(0,0,0,0.18);
  }
  .sidebar.force-open .nav-label,
  .sidebar.force-open .logo-text,
  .sidebar.force-open .seller-info { display: block !important; }
  .sidebar.force-open .sidebar-header { justify-content: flex-start; padding: 1.35rem 1.1rem 1.1rem; }
  .sidebar.force-open .nav-item { justify-content: flex-start; padding: 0.6rem 0.75rem; }
  .sidebar.force-open .seller-profile { justify-content: flex-start; padding: 0.6rem 0.75rem; }

  .section { padding: 1.25rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 2fr 1fr 1fr; }
  .charts-grid .chart-card.wide { grid-column: auto; }
  .pos-ticket-panel { width: 270px; min-width: 250px; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); }
  .section-header { flex-wrap: wrap; }
}

/* ══════════════════════════════════════
   LARGE PHONE / PORTRAIT TABLET ≤ 768px
══════════════════════════════════════ */
@media (max-width: 768px) {
  html { font-size: 13px; }

  /* Sidebar becomes a slide-over drawer */
  .sidebar {
    position: fixed !important;
    top: 0; left: 0; height: 100%;
    width: var(--sidebar-width) !important;
    min-width: var(--sidebar-width) !important;
    transform: translateX(-100%);
    transition: transform 0.22s ease, width 0.22s ease;
    z-index: 300;
    box-shadow: none;
  }
  .sidebar .nav-label,
  .sidebar .logo-text,
  .sidebar .seller-info { display: block !important; }
  .sidebar .sidebar-header { justify-content: flex-start; padding: 1.35rem 1.1rem 1.1rem; }
  .sidebar .nav-item { justify-content: flex-start; padding: 0.6rem 0.75rem; }
  .sidebar .seller-profile { justify-content: flex-start; padding: 0.6rem 0.75rem; }
  .sidebar.mobile-open {
    transform: translateX(0);
    box-shadow: 4px 0 32px rgba(0,0,0,0.25);
  }

  /* Overlay backdrop when sidebar open on mobile */
  .sidebar-backdrop {
    display: none; position: fixed; inset: 0;
    background: rgba(0,0,0,0.45); z-index: 299;
  }
  .sidebar-backdrop.active { display: block; }

  /* Main content takes full width */
  .main-content { width: 100%; }

  /* Topbar compact */
  .topbar { padding: 0 0.85rem; gap: 0.6rem; }
  .topbar-date { display: none; }

  /* Topbar siempre visible en móvil (botón ☰ para abrir el menú) */
  .main-content > .topbar {
    position: sticky;
    top: 0;
    z-index: 100;
  }
  .sidebar-toggle {
    font-size: 1.5rem !important;
    padding: 8px 10px !important;
    min-width: 40px !important;
    min-height: 40px !important;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  /* Sections */
  .section { padding: 1rem; }
  .section-header h2 { font-size: 1.1rem; }
  .section-header-actions { width: 100%; justify-content: space-between; }

  /* Dashboard */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 0.65rem; }
  .charts-grid { grid-template-columns: 1fr; gap: 0.75rem; }
  .charts-grid .chart-card.wide { grid-column: 1; }

  /* POS — stacked layout: products on top, ticket slides up */
  #section-pos.active { flex-direction: column !important; }
  .pos-layout { flex-direction: column; height: 100%; }
  .pos-products-panel { flex: 1; min-height: 0; padding: 0.75rem 0.75rem 5rem; }
  .pos-ticket-panel {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 210;
    width: 100% !important; min-width: 0 !important;
    border-left: none; border-top: 2px solid var(--border);
    max-height: 85vh; flex-shrink: 0;
    display: none;   /* hidden until toggle */
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -6px 32px rgba(0,0,0,0.14);
    overflow-y: auto;            /* permite scroll del panel completo */
    -webkit-overflow-scrolling: touch;
  }
  /* En móvil: la lista de items ya no fija su propio scroll;
     todo el panel desliza para que los botones siempre sean accesibles */
  .pos-ticket-panel .ticket-items {
    overflow-y: visible !important;
    flex: 0 0 auto !important;
    max-height: none !important;
  }
  .pos-ticket-panel .ticket-footer {
    position: sticky; bottom: 0; z-index: 2;
    background: var(--bg);
  }
  .ticket-tabs-wrap { display: none; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 0.55rem; }
  .product-card { padding: 0.65rem 0.5rem; gap: 0.3rem; }
  .product-card-img { width: 64px; height: 64px; }
  .product-card-name {
    display: block !important;
    width: 100%;
    line-height: 1.2;
    word-break: break-word;
    white-space: normal;
  }

  /* Floating ticket toggle button */
  .mobile-ticket-toggle {
    display: flex !important;
    position: fixed; bottom: 1.5rem; right: 1rem; z-index: 250;
    background: var(--primary); color: #fff; border: none;
    border-radius: 50px; padding: 0.65rem 1.2rem;
    font-size: 0.875rem; font-weight: 700;
    box-shadow: 0 4px 20px rgba(0,0,0,0.22);
    align-items: center; gap: 0.45rem;
    transition: background var(--transition), transform var(--transition);
  }
  .mobile-ticket-toggle:hover { background: var(--primary-dark); transform: translateY(-2px); }

  /* Modals — full screen on mobile */
  .modal {
    width: 100% !important; max-width: 100% !important;
    max-height: 95vh !important;
    bottom: 0; top: auto; left: 0 !important;
    transform: translateY(4%) !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
  }
  .modal.visible { transform: translateY(0) !important; }

  /* Tables — scrollable */
  .inventory-table-wrap,
  .reports-content .report-section-box { overflow-x: auto; }
  .data-table th, .data-table td { padding: 0.55rem 0.65rem; white-space: nowrap; }

  /* Period tabs — smaller */
  .period-btn { padding: 5px 12px; font-size: 0.77rem; }

  /* Settings panels — single column */
  .settings-layout { flex-direction: column; }
  .settings-group {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    resize: none !important;
  }
  .settings-save-bar {
    justify-content: stretch !important;
    padding: 0.75rem !important;
    flex-basis: auto !important;
    flex: 0 0 auto !important;
  }
  .btn-save-settings {
    width: 100% !important;
    min-width: 0 !important;
  }
}

/* ══════════════════════════════════════
   PHONE ≤ 480px
══════════════════════════════════════ */
@media (max-width: 480px) {
  html { font-size: 12.5px; }
  :root { --topbar-height: 54px; }

  .topbar { padding: 0 0.65rem; }
  .btn-primary { padding: 0.5rem 0.85rem; font-size: 0.82rem; }

  /* Dashboard: single column */
  .stats-grid { grid-template-columns: 1fr; gap: 0.55rem; }

  /* POS products — 2 cols, compactas y uniformes */
  .products-grid { grid-template-columns: repeat(2, 1fr); gap: 0.5rem; }
  .product-card {
    padding: 0.6rem 0.45rem !important;
    gap: 0.3rem !important;
    min-height: 150px !important;
    justify-content: flex-start !important;
    align-items: center !important;
    overflow: visible !important;
  }
  .product-card-img {
    width: 56px !important; height: 56px !important;
    flex-shrink: 0 !important; flex-grow: 0 !important;
    overflow: hidden;
  }
  .product-card-img img {
    width: 100% !important; height: 100% !important;
    max-width: 100% !important; max-height: 100% !important;
    object-fit: cover;
  }
  .product-card-name {
    display: block !important;
    font-size: 0.78rem !important;
    line-height: 1.2 !important;
    width: 100% !important;
    word-break: break-word !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
    visibility: visible !important;
    opacity: 1 !important;
    margin: 0 !important;
  }
  .product-card-price { font-size: 0.85rem !important; line-height: 1.2 !important; margin: 0 !important; }
  .product-card-stock { font-size: 0.65rem !important; margin: 0 !important; }
  .pos-ticket-panel { max-height: 80vh; }

  /* Tickets/forms: single column */
  .form-grid { grid-template-columns: 1fr !important; }
  .form-grid .span2 { grid-column: 1 !important; }

  /* Ticket actions */
  .ticket-actions { grid-template-columns: 1fr; }
  .ticket-actions .btn-primary.wide { grid-column: 1; }

  /* Section padding */
  .section { padding: 0.75rem; }
  .section-header { margin-bottom: 1rem; }

  /* Payment modal */
  .payment-methods { grid-template-columns: 1fr 1fr; gap: 0.5rem; }
  .payment-method-btn { padding: 0.7rem 0.5rem; }

  /* Stat cards compact */
  .stat-card { padding: 0.9rem 1rem; }
  .stat-value { font-size: 1.3rem !important; }

  /* Topbar: hide date and show only icon buttons */
  .topbar-date { display: none; }

  /* Users grid: single column */
  .users-grid { grid-template-columns: 1fr !important; }
}

/* ══════════════════════════════════════
   MOBILE INTERACTIVE IMPROVEMENTS (iPhone XR ~414px)
   Mejorar controles táctiles y espaciado
══════════════════════════════════════ */
@media (max-width: 768px) {
  /* Touch-friendly buttons (mínimo 44x44px iOS) */
  button, .btn, .btn-primary, .btn-secondary, .btn-danger, 
  input[type="submit"], input[type="button"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Modal padding para mejor lectura */
  .modal-body { padding: 1.15rem; }
  .modal-header { padding: 1rem 1.15rem; }
  .modal-footer { padding: 1rem 1.15rem; gap: 0.6rem; }

  /* QR Modal optimizado para móvil */
  #qr-modal .modal-body { 
    padding: 1.25rem; 
    max-height: 80vh; 
    overflow-y: auto; 
  }
  #qr-local-wrap, #qr-public-wrap {
    padding: 1rem 0.5rem;
  }
  #qr-local-img, #qr-public-img {
    max-width: 180px !important;
    height: auto;
  }

  /* Form inputs más grandes para tocar */
  input[type="text"], 
  input[type="number"], 
  input[type="password"],
  input[type="email"],
  select, textarea {
    min-height: 44px;
    font-size: 16px; /* Evitar zoom automático iOS */
    padding: 0.6rem 0.85rem;
  }

  /* Tables — mejorar scroll horizontal */
  .data-table-wrap, .inventory-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Stat cards — espaciado vertical */
  .stat-card {
    padding: 1.1rem 1rem;
  }
  .stat-label {
    font-size: 0.85rem;
    margin-bottom: 0.4rem;
  }
  .stat-value {
    font-size: 1.6rem;
    line-height: 1.2;
  }

  /* Product cards — mejor tap targets */
  .product-card {
    min-height: 120px;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
  }
  .product-card:active {
    transform: scale(0.97);
  }

  /* Ticket items — espaciado */
  .ticket-item {
    padding: 0.75rem 0.65rem;
    min-height: 56px;
  }

  /* Search bars — legibles */
  .search-box input {
    font-size: 16px;
    min-height: 44px;
  }

  /* Section headers — wrap flexible */
  .section-header {
    flex-wrap: wrap;
    gap: 0.75rem;
  }
  .section-header-actions {
    width: 100%;
    justify-content: flex-start;
    gap: 0.6rem;
  }
}

/* ══════════════════════════════════════
   MOBILE TICKET TOGGLE (hidden on desktop)
══════════════════════════════════════ */
.mobile-ticket-toggle { display: none; }

/* ══════════════════════════════════════
   DASHBOARD MOBILE TWEAKS ≤ 768px
   - Stats 2 por fila, más compactos
   - Top/Bottom products: layout en 2 col
   - Charts: sin altura fija
══════════════════════════════════════ */
@media (max-width: 768px) {
  /* Stats grid más compacto */
  .stat-card { padding: 0.85rem 0.9rem; gap: 0.3rem; }
  .stat-icon { font-size: 1.3rem; }
  .stat-value { font-size: 1.25rem !important; letter-spacing: -0.3px; }
  .stat-label { font-size: 0.72rem; }
  .stat-change, .stat-sub { font-size: 0.7rem; }

  /* Top/Bottom products: nombre + stats más pequeños, barras visibles */
  .tp-name { font-size: 0.75rem; }
  .tp-lbl-rev, .tp-lbl-cost { font-size: 0.62rem; }
  .tp-qty, .tp-profit { font-size: 0.68rem; }
  .tp-img, .tp-img-placeholder { width: 26px; height: 26px; font-size: 0.75rem; }
  .tp-rank { width: 1.4rem; font-size: 0.9rem; }

  /* rendimiento-card en móvil: no comprimir, dejar scrollear la sección */
  .rendimiento-card { overflow-y: visible; min-height: unset; flex: unset; }
  #chart-performance { max-height: 130px !important; }
  .rendimiento-bottom { padding-top: 0.5rem; }

  /* Chart cards: sin altura fija para que crezcan con el contenido */
  .chart-card { min-height: unset; }

  /* Section no debe clip overflow, que el usuario haga scroll */
  #section-dashboard { overflow-y: auto !important; }
  #section-dashboard .section { padding-bottom: 2rem; }
}

/* ══════════════════════════════════════
   PHONE ≤ 480px — ajustes adicionales
══════════════════════════════════════ */
@media (max-width: 480px) {
  /* Una sola columna de stats en pantallas muy pequeñas */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 0.45rem; }
  .stat-value { font-size: 1.15rem !important; }
  .stat-card { padding: 0.75rem 0.8rem; }

  /* Top/Bottom products: quitar foto en pantalla muy chica para ganar espacio */
  .tp-img, .tp-img-placeholder { display: none; }
  .tp-rank { font-size: 0.85rem; width: 1.2rem; }
  .tp-name { font-size: 0.78rem; }

  /* Topbar: solo logo e iconos */
  .topbar-brand span { display: none; }

  /* Period tabs: no wrap, scroll horizontal */
  .period-tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
  .period-btn { white-space: nowrap; flex-shrink: 0; }

  /* Modals: altura máxima y scroll interno */
  .modal { max-height: 92vh !important; overflow-y: auto; }
  .modal-body { max-height: 70vh; overflow-y: auto; }

  /* Tablas: fuente más pequeña */
  .data-table th, .data-table td { font-size: 0.75rem; padding: 0.45rem 0.5rem; }

  /* Form rows: siempre una columna */
  .form-row { flex-direction: column !important; gap: 0.5rem; }

  /* Section headers: apilados */
  .section-header { flex-direction: column; align-items: flex-start; }
  .section-header-actions { flex-wrap: wrap; }

  /* Buttons en header: más pequeños */
  .section-header-actions .btn-primary,
  .section-header-actions .btn-secondary { font-size: 0.78rem; padding: 0.4rem 0.7rem; min-height: 36px; }
}

/* ══════════════════════════════════════
   MOBILE BUG FIXES (phone ≤ 768px)
   - Tablas con muchas acciones: compactar
   - Charts: altura acotada
   - POS ticket: garantizar lista visible
══════════════════════════════════════ */
@media (max-width: 768px) {
  /* Cotizaciones / Cobros / Pagos / cualquier tabla con columna Acciones:
     los botones no deben apilarse verticalmente, ni convertir la fila en una columna.
     Forzamos nowrap + botones compactos tipo icono. */
  .data-table td[style*="flex"],
  .data-table td .actions,
  .data-table td .row-actions {
    display: inline-flex !important;
    flex-wrap: nowrap !important;
    gap: 0.25rem !important;
    white-space: nowrap;
  }
  .data-table .btn-edit,
  .data-table .btn-delete,
  .data-table td .btn-primary,
  .data-table td .btn-secondary {
    padding: 0.3rem 0.55rem !important;
    font-size: 0.72rem !important;
    min-height: 32px !important;
    min-width: 32px !important;
    line-height: 1;
  }
  /* Envolver cualquier .data-table en scroll horizontal aunque el padre no lo tenga */
  .data-table { min-width: max-content; }

  /* Charts canvas: limitar altura para evitar scroll infinito en móvil
     (Chart.js respeta la altura del padre si maintainAspectRatio=false,
     pero por defecto crece según el ancho) */
  .chart-card canvas,
  .contab-chart-card canvas,
  #chart-hourly,
  #chart-performance,
  #chart-ventas-mes {
    max-height: 240px !important;
    height: auto !important;
  }
  .chart-card { overflow: hidden; }

  /* POS ticket: garantizar que la lista de items sea visible */
  .pos-ticket-panel { max-height: 80vh !important; }
  .pos-ticket-panel .ticket-header { padding: 0.6rem 0.75rem; gap: 0.4rem; }
  .pos-ticket-panel .ticket-items { min-height: 110px; padding: 0.5rem 0.75rem; }
  .pos-ticket-panel .ticket-footer { padding: 0.6rem 0.75rem; }
  .pos-ticket-panel .ticket-meta { gap: 0.35rem; }
  .pos-ticket-panel .ticket-meta-item { flex: 1 1 100px; }
}

@media (max-width: 480px) {
  /* Tablas de contabilidad y cotizaciones: fuente mínima legible */
  .contab-table-wrap .data-table th,
  .contab-table-wrap .data-table td,
  #cotizaciones-table th,
  #cotizaciones-table td { font-size: 0.72rem; padding: 0.4rem 0.45rem; }

  /* Charts aún más acotados */
  .chart-card canvas,
  .contab-chart-card canvas { max-height: 200px !important; }
}
