/* Phone layout for every ERP module and the dashboard (≤ 640px).
   One layer, loaded after app.css. Desktop is untouched: every rule sits inside the phone media query.
   Principles: the page never scrolls sideways; a wide table scrolls inside its own box; fixed grids collapse to one
   column; flex rows wrap; the dashboard's side rail becomes a horizontal, scrollable row of destinations. */

@media (max-width: 640px) {

  /* ── Page frame ─────────────────────────────────────────────────────────── */
  /* No overflow-x hiding: content must fit, not be clipped. */
  html, body { max-width: 100%; }
  #main, #content { min-width: 0; max-width: 100%; }
  .topbar { flex-wrap: wrap; height: auto; row-gap: 8px; }
  .tb-left, .tb-right { min-width: 0; flex-wrap: wrap; }
  .tb-right { justify-content: flex-start; }
  #content img, #content canvas, #content svg, #content video { max-width: 100%; height: auto; }

  /* ── Tables scroll inside their own box, never widen the page ───────────── */
  #content table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #content .table-wrap, #content .table-card, #content .log-table-card, #content .log-table-scroll,
  #content [class*="table-scroll"], #content [class*="-table-card"] { max-width: 100%; overflow-x: auto; }

  /* ── Grids collapse to one column; flex rows wrap ───────────────────────── */
  #content [style*="grid-template-columns"] { grid-template-columns: minmax(0, 1fr) !important; }
  #content [style*="display:grid"], #content [style*="display: grid"] { min-width: 0; }
  #content [style*="display:flex"], #content [style*="display: flex"] { flex-wrap: wrap; min-width: 0; }
  #content [class*="grid"], #content [class*="cards"], #content [class*="kpi"], #content [class*="summary"] { min-width: 0; max-width: 100%; }
  #content [class*="grid"] > *, #content [class*="cards"] > *, #content [class*="summary"] > * { min-width: 0; max-width: 100%; }

  /* ── Dashboard: one column, KPI pairs, the rail becomes a scrollable row ── */
  #content .dash2 { grid-template-columns: minmax(0, 1fr) !important; min-height: 0; }
  #content .dash2-rail {
    position: sticky; top: 0; height: auto; flex-direction: row; align-items: center; gap: 10px;
    padding: 10px max(12px, env(safe-area-inset-left, 0px)); overflow-x: auto; overflow-y: hidden; z-index: 40;
  }
  #content .dash2-rail .dash2-brand-name , #content .dash2-rail .dash2-rail-foot, #content .dash2-rail .dash2-brand-mark { display: none; }
  #content .dash2-rail .dash2-nav { flex-direction: row; gap: 4px; flex: 0 0 auto; }
  #content .dash2-rail .dash2-nav-item { flex: 0 0 auto; white-space: nowrap; }
  #content .dash2-main { min-width: 0; max-width: 100%; padding-left: max(16px, env(safe-area-inset-left, 0px)) !important; padding-right: max(16px, env(safe-area-inset-right, 0px)) !important; }
  #content .dash2-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  #content .dash2-grid , #content .dash2-col { display: flex !important; flex-direction: column; min-width: 0; }
  #content .dash2-card , #content .dash2-tower, #content .dash2-stream, #content .dash2-ship { min-width: 0; max-width: 100%; }
  #content .dash2 table { display: block; max-width: 100%; overflow-x: auto; }
  #content .dash2-main { display: flex; flex-direction: column; grid-template-columns: none !important; width: 100%; }
  #content .dash2-grid { width: 100%; }
  /* The shipment tower spans the full width; its heading and KPI values are sized for a phone */
  #content .dash2-tower, #content .dash2-col > * { grid-column: 1 / -1; width: 100%; min-width: 0; max-width: 100%; }
  #content .dash2-tower h2, #content .dash2-tower .dash2-card-hd h2 { font-size: 18px; line-height: 1.25; }
  #content .dash2-kpi-value { font-size: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  /* Shipment rows stack to one column: the logo, identity, route and ETA each take the full width */
  #content .dash2-ship { grid-template-columns: minmax(0, 1fr) !important; padding: 10px 12px; gap: 10px; }
  #content .dash2-ship > * { min-width: 0; max-width: 100%; }
  #content .dash2-vessel img { max-width: 100%; height: auto; }

  /* ── Settings panels: the desktop padding is too wide for a phone ── */
  .settings-content { padding: 18px !important; border-radius: 20px !important; }
  .settings-section-card { padding: 16px !important; margin-bottom: 16px !important; border-radius: 18px !important; }

  /* ── Summary grids (g2 to g6) become two columns; stat cards may shrink ── */
  #content .grid.g2, #content .grid.g3, #content .grid.g4, #content .grid.g5, #content .grid.g6 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  #content .stat-card { min-width: 0; }

  /* ── Data tables: the wrapper scrolls, and the table keeps a readable width ── */
  #content .tw, #content .table-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
  #content table { min-width: 560px; }
  #content .dash2 table { min-width: 0 !important; }

  /* ── Touch targets: every control at least 36px tall, fields 40px ── */
  #content button, #content a[href], #content [role="button"], #content [role="tab"],
  .topbar button, #topnav button, .modal button, .modal-ov button { min-height: 36px; }
  #content input:not([type="checkbox"]):not([type="radio"]), #content select, #content textarea,
  .modal input:not([type="checkbox"]):not([type="radio"]), .modal select, .modal textarea { min-height: 40px; min-width: 0; max-width: 100%; }

  /* ── Dialogs fit the phone: full width, full height when large, forms in one column ── */
  .modal { width: 100% !important; max-width: 100vw !important; }
  .modal.modal-full, .modal-full { height: 100% !important; max-height: 100% !important; border-radius: 0 !important; }
  .modal [style*="grid-template-columns"], .modal [class*="grid"], .modal [class*="form-row"], .modal [class*="fields"] { grid-template-columns: minmax(0, 1fr) !important; }
  .modal table { display: block; max-width: 100%; overflow-x: auto; }
  .modal [class*="fr2"], .modal [class*="fr3"], .modal [class*="fr4"], .modal [class*="fr5"], .modal .fr,
  #content [class*="fr2"], #content [class*="fr3"], #content [class*="fr4"], #content [class*="fr5"] { grid-template-columns: minmax(0, 1fr) !important; }
  #content input[type="checkbox"], #content input[type="radio"], .modal input[type="checkbox"], .modal input[type="radio"] { width: 24px; height: 24px; min-height: 24px; }

  /* ── Form fields never wider than the screen ── */
  #content input, #content select, #content textarea { max-width: 100%; }
}
