/* Mobile / tablet layer. Loaded last so it can override the desktop rules.
   <=1024px: sidebar becomes a drawer, multi-column layouts collapse.
   <=640px : phone tuning (touch targets, full-width panels, 1-col grids). */

.mbar, .nav-scrim { display: none; }

@media (max-width: 1024px) {
  html, body { height: auto; min-height: 100%; }
  body { overflow-x: hidden; -webkit-text-size-adjust: 100%; }
  #shell, #shell.on { height: 100vh; height: 100dvh; }
  #shell.on { grid-template-columns: minmax(0, 1fr); }
  .main { height: 100vh; height: 100dvh; }

  /* ---- sidebar -> off-canvas drawer ---- */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 70; height: 100vh; height: 100dvh;
    width: min(280px, 86vw); transform: translateX(-102%); transition: transform .2s ease;
    box-shadow: none; padding-top: calc(14px + env(safe-area-inset-top));
  }
  body.nav-open .sidebar { transform: none; box-shadow: var(--e2); }
  .nav-scrim { display: block; position: fixed; inset: 0; z-index: 65; background: rgba(26,28,31,.5); opacity: 0; pointer-events: none; transition: opacity .2s; }
  body.nav-open .nav-scrim { opacity: 1; pointer-events: auto; }
  body.nav-open { overflow: hidden; }
  .nav-item { height: 42px; font-size: 14.5px; }
  .nav-item.active::before { left: -10px; }
  .menu-item { padding: 11px 10px; }

  .mbar {
    flex: none; display: flex; align-items: center; gap: 10px; min-height: 48px;
    padding: env(safe-area-inset-top) 12px 0; background: var(--night); color: #fff;
  }
  .mbar button {
    width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
    border: 0; border-radius: var(--r-control); background: transparent; color: #fff; cursor: pointer;
  }
  .mbar button:active { background: var(--night-3); }
  .mbar svg { width: 22px; height: 22px; }
  .mbar .brand-name { font-size: 15px; font-weight: 600; letter-spacing: -.01em; }
  .mbar .mbar-page { margin-left: auto; color: var(--night-ink3); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }

  /* ---- top bars / tabs ---- */
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 10px 14px; min-height: var(--topbar-h); }
  .topbar h1 { font-size: 17px; white-space: normal; }
  .topbar .sub { white-space: normal; }
  .topbar .spacer { flex: 1 1 0; min-width: 0; }
  .topbar-tabs { padding: 0 10px; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .topbar-tabs::-webkit-scrollbar { display: none; }
  .topbar-tabs > * { flex: none; white-space: nowrap; }
  .content { padding: 12px; }

  /* ---- generic grids ---- */
  .grid-3, .kpis, .kpis.five, .exc-grid, .zs-zones, .zs-2col, .set-nets, .seg-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .today-grid, .tt-layout, .tt-view.tt-layout, .fr-layout, .nt-grid, .nt-body, .zs-editor, .sos-grid,
  .bz-cols, .st-cols { grid-template-columns: minmax(0, 1fr); }
  .zs-fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sos-grid, .st-cols, .nt-grid, .fr-layout { min-height: 0; height: auto; }
  .tt-edit { position: static; height: auto; min-height: 0; }
  .zs-map-col { border-left: 0; border-top: 1px solid var(--line); min-height: 300px; }
  .zs-editor { min-height: 0; overflow: visible; }
  .map-wrap { min-height: 320px; }
  .dv-cards { grid-template-columns: minmax(0, 1fr); }
  .kcol, .q-wrap .kcol { min-height: 0; }

  /* kanban + steppers: swipe sideways instead of crushing */
  .kanban { grid-auto-flow: column; grid-template-columns: none; grid-auto-columns: minmax(260px, 78%); overflow-x: auto; scroll-snap-type: x proximity; padding-bottom: 6px; min-height: 0; }
  .kanban > * { scroll-snap-align: start; }
  .stepper-h { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: minmax(150px, 1fr); overflow-x: auto; }

  /* ---- tables scroll sideways inside their card ---- */
  .table-card, .card:has(> table.t) { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table.t { min-width: 640px; }
  table.t.compact { min-width: 520px; }
  table.tt { min-width: 620px; }

  /* ---- search / filters ---- */
  .search, .bk .bk-search, .dv-search, .tx .tx-search { flex: 1 1 100%; min-width: 0; }
  .toolbar { gap: 8px; }

  /* ---- overlays ---- */
  .panel { width: 100vw; max-width: 100vw; }
  .modal-scrim { padding: 12px; align-items: flex-end; }
  .modal { width: 100%; max-height: 92dvh; overflow-y: auto; }
  .toasts { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); align-items: stretch; }
  .toast { min-width: 0; max-width: none; }
}

@media (max-width: 640px) {
  body { font-size: 14px; }
  .content { padding: 10px; }
  .topbar { padding: 10px 12px; }
  .topbar .row, .topbar .toolbar { flex-wrap: wrap; }

  /* tap-friendly controls; 16px inputs stop iOS zooming on focus */
  .btn { height: 40px; padding: 0 14px; }
  .btn.sm, .btn.small { height: 34px; }
  input, select, textarea, .input, .select { font-size: 16px !important; min-height: 40px; }
  .seg button { height: 38px; padding: 0 12px; }
  .tabs { overflow-x: auto; }

  .grid-2, .grid-3, .kpis, .kpis.five, .exc-grid, .zs-zones, .zs-2col, .zs-fields, .set-nets,
  .dv-pairs, .dv-pairs.four, .dv-decl, .dv-grid, .dv-slots, .dv-slots.three, .dv-checks, .emerg { grid-template-columns: minmax(0, 1fr); }
  .kpis, .kpis.five { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 12px; }
  .kpi .value { font-size: 20px; white-space: normal; }
  .t-wall { font-size: 30px; }
  .seg-stats { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; padding: 10px; }
  .seg-stats .v, .set-nets .v { font-size: 17px; }

  .card-head { flex-wrap: wrap; row-gap: 6px; }
  .card-head .title, .card-head .sub { white-space: normal; }
  .card-body { padding: 12px; }
  .card-foot { flex-wrap: wrap; padding: 10px 12px; }

  /* key/value + fixed-column rows */
  .kv, .bk-kv, .sos-kv, .tt-kv { grid-template-columns: 96px minmax(0, 1fr); }
  .zone-row, .zs-zone .zone-row { grid-template-columns: 22px minmax(0, 1fr) auto; height: auto; min-height: 44px; padding: 8px 12px; }
  .zone-row > :nth-child(3) { grid-column: 2 / -1; grid-row: 2; }
  .exc-row, .exc-screen .exc-row { grid-template-columns: minmax(0, 1fr) auto; }
  .exc-row > :first-child { grid-column: 1 / -1; }
  .ttc, .tt-view .ttc { height: auto; min-height: 40px; padding: 6px 10px; }
  .fr-h { grid-template-columns: 72px minmax(0, 1fr); }
  .fr-foot { flex-wrap: wrap; }
  .fr-foot .field { width: 100%; }
  .todo { grid-template-columns: 24px minmax(0, 1fr); }
  .todo > :last-child { grid-column: 2; }
  .st-bar { grid-template-columns: 72px minmax(0, 1fr) 48px; }
  .st-bus { grid-template-columns: minmax(0, 1fr) 40px 84px; }
  .bz-fuel .lr { grid-template-columns: 46px minmax(0, 1fr) 40px 76px; }
  .tt-datepick .input, .dv-date { width: 100%; max-width: none; }
  .set-role { width: 140px; }
  .nt-body { grid-template-columns: minmax(0, 1fr); }
  .phone, .nt-preview .phone { width: min(270px, 100%); }
  .tag-input input { min-width: 100px; }

  .panel-head, .panel-body, .panel-foot { padding-left: 14px; padding-right: 14px; }
  .panel-foot { flex-wrap: wrap; position: sticky; bottom: 0; background: var(--surface); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .panel-foot .btn { flex: 1 1 auto; }
  .modal .foot { flex-wrap: wrap; }
  .modal .foot .btn { flex: 1 1 auto; }
  .dv-bar { padding-bottom: env(safe-area-inset-bottom); }

  /* sign-in */
  #signin { padding: 16px; min-height: 100dvh; }
  .signin-card { padding: 22px 18px; }
  .signin-foot { position: static; margin-top: 16px; }

  /* table: tighter cells */
  table.t th, table.t td { padding-left: 10px; padding-right: 10px; }
  .bk-table td.bk-trip { max-width: 160px; }
}
