/* ════════════════════════════════════════════════════════
   AP.CSS — Estilos globales Agro Parallel OS
   Aplicado a TODOS los módulos vía
     <link rel="stylesheet" href="../public/css/ap.css" />
   o desde la raíz:
     <link rel="stylesheet" href="public/css/ap.css" />

   Foco principal: hacer responsive los módulos
   sin tocar el CSS inline de cada uno.
   ════════════════════════════════════════════════════════ */

/* ── Base ─────────────────────────────────────────── */
html, body { -webkit-text-size-adjust: 100%; }
* { box-sizing: border-box; }
img { max-width: 100%; height: auto; }

/* Mejor scroll en mobile + iOS rebote */
html, body { overscroll-behavior-y: contain; }

/* Touch target mínimo (iOS HIG / Material) */
@media (pointer: coarse) {
  button, .tab, a.back, input[type="checkbox"], input[type="radio"] {
    min-height: 36px;
  }
}

/* ════════════════════════════════════════════════════════
   MOBILE (≤ 768px)
   ════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* Padding del body más chico */
  body { padding: 12px !important; }

  /* ── Layouts dashboard (sidebar + main + topbar) ── */
  /* Permitir scroll en body cuando tiene overflow:hidden */
  body[style*="overflow"], body {
    overflow-x: hidden !important;
  }
  body.has-sidebar,
  body:has(> .sidebar),
  body:has(> aside.sidebar) {
    overflow: auto !important;
    display: block !important;
    min-height: auto !important;
  }

  /* Sidebar como drawer overlay */
  .sidebar,
  aside.sidebar {
    position: fixed !important;
    top: 0; left: 0;
    height: 100vh;
    width: 260px !important;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 1000 !important;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    overflow-y: auto;
  }
  .sidebar.open,
  aside.sidebar.open { transform: translateX(0); }

  /* Backdrop cuando el sidebar está abierto */
  .ap-mobile-backdrop {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 999;
  }
  .ap-mobile-backdrop.on { display: block; }

  /* Hamburger button (lo agrega ap-mobile.js) */
  .ap-mobile-burger {
    position: fixed !important;
    top: 10px; left: 10px;
    z-index: 1001;
    width: 38px; height: 38px;
    background: rgba(20,30,15,0.92);
    border: 1px solid rgba(91,191,53,0.4);
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    color: #5bbf35;
    backdrop-filter: blur(6px);
  }
  .ap-mobile-burger svg { width: 20px; height: 20px; }

  /* Main / content full-width sin overflow hidden */
  .main {
    overflow: visible !important;
    height: auto !important;
    width: 100% !important;
  }
  .content {
    padding: 12px !important;
    overflow: visible !important;
  }

  /* Topbar adaptado */
  .topbar {
    flex-wrap: wrap;
    height: auto !important;
    padding: 10px 12px 10px 56px !important; /* deja lugar al hamburger */
    gap: 8px !important;
  }
  .topbar-title { font-size: 15px !important; }
  .topbar-sub { font-size: 11px !important; }
  .topbar-actions {
    margin-left: 0 !important;
    width: 100%;
    flex-wrap: wrap;
  }
  .topbar-actions .btn { flex: 1 1 auto; justify-content: center; }
  .btn { font-size: 11px !important; padding: 7px 10px !important; }
  .btn-sm { font-size: 10px !important; padding: 5px 8px !important; }

  /* Header en columna */
  header {
    flex-wrap: wrap;
    gap: 10px;
  }
  header h1 { font-size: 18px !important; }

  /* Grids en una columna */
  .grid,
  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }
  .row2,
  .row3,
  .row-2,
  .row-3 {
    grid-template-columns: 1fr !important;
    display: grid !important;
    gap: 10px !important;
  }

  /* Cards y secciones */
  .card,
  .panel,
  .seccion {
    padding: 14px !important;
    border-radius: 8px;
  }
  .card h2 { font-size: 15px !important; }

  /* Tabs scrolleables horizontalmente */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    flex-wrap: nowrap !important;
  }
  .tab {
    padding: 9px 12px !important;
    font-size: 13px;
    flex-shrink: 0;
  }

  /* Tablas con scroll horizontal en mobile */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    width: 100% !important;
    font-size: 12px !important;
  }
  table thead, table tbody, table tr { width: max-content; min-width: 100%; }
  th, td { padding: 7px 8px !important; }

  /* Inputs táctiles más cómodos */
  input,
  select,
  textarea,
  button {
    font-size: 16px !important; /* evita zoom de iOS al focus */
    padding: 10px 12px !important;
  }
  /* Excepción: campos chicos */
  input[type="checkbox"],
  input[type="radio"] {
    font-size: inherit !important;
    padding: 0 !important;
    transform: scale(1.2);
  }

  /* Botones full width donde sea natural */
  .card > button,
  .panel > button,
  .seccion > button {
    width: 100%;
    margin-top: 6px;
  }

  /* Toast más visible */
  .toast {
    left: 12px !important;
    right: 12px !important;
    bottom: 12px !important;
    text-align: center;
  }

  /* Diálogos full-width */
  dialog {
    width: calc(100vw - 24px);
    min-width: 0 !important;
    max-width: 100% !important;
    padding: 18px !important;
  }

  /* Filtros / search bars */
  .filtros,
  .filters,
  .toolbar {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .filtros > *,
  .filters > * { flex: 1 1 100% !important; }

  /* PDF preview en módulo etiquetas: que entre */
  .preview {
    transform: scale(0.7) !important;
    transform-origin: top left !important;
    margin-bottom: -120px;
  }

  /* Index hub — tarjetas a ancho cómodo táctil */
  .modules,
  .module-grid,
  .modules-grid,
  .cards,
  .global-kpis,
  .system-row {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)) !important;
    gap: 10px !important;
  }

  /* Hide cosas no esenciales en mobile */
  .desktop-only,
  .only-desktop { display: none !important; }
}

/* ════════════════════════════════════════════════════════
   TABLET (≤ 1024px)
   ════════════════════════════════════════════════════════ */
@media (max-width: 1024px) and (min-width: 769px) {
  .grid,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr 1fr !important;
  }
  .row3 { grid-template-columns: 1fr 1fr !important; }
  body { padding: 18px !important; }
}

/* ════════════════════════════════════════════════════════
   Utility helpers (opcionales, no rompen nada existente)
   ════════════════════════════════════════════════════════ */
.mobile-only { display: none; }
@media (max-width: 768px) {
  .mobile-only { display: block; }
}
