/* Phone layout (≤760px). Everything here sits inside the media query, so desktop is
   untouched. Bottom bar + "More" sheet: static/js/shared/mobile.js. */
#m-bar, #m-more, #wh-msheet, .whm-filter-btn { display: none; }

@media (max-width: 760px) {
  /* ── shell ──────────────────────────────────────────────────────────────── */
  #sidebar { display: none !important; }
  #main { width: 100%; min-width: 0; padding-bottom: calc(64px + env(safe-area-inset-bottom)); overflow-x: clip; }
  .page { padding: 14px 12px; gap: 14px; min-width: 0; overflow-x: clip; }
  .page-header { flex-wrap: wrap; gap: 8px; position: relative; }
  .page-header > * { min-width: 0; }
  .page-title { font-size: 19px; }

  #m-bar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 900;
    height: calc(58px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--bg2); border-top: 1px solid var(--border);
  }
  #m-bar .m-slot {
    flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; background: none; border: 0; color: var(--text2); font-size: 11px; padding: 6px 2px; min-height: 0;
  }
  #m-bar .m-slot.active { color: var(--accent); }
  .m-ic { position: relative; display: inline-flex; }
  .m-ic svg { width: 22px; height: 22px; }
  .m-lbl { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .m-badge {
    display: none; position: absolute; top: -5px; right: -10px; min-width: 16px; height: 16px; padding: 0 4px;
    border-radius: 8px; background: var(--red); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; font-style: normal; text-align: center;
  }
  .m-badge.on { display: block; }
  .m-badge.on:empty { min-width: 8px; width: 8px; height: 8px; padding: 0; top: -2px; right: -4px; }
  body.m-sheet-open #m-bar { display: none; }

  #m-more {
    position: fixed; inset: 0 0 calc(58px + env(safe-area-inset-bottom)) 0; z-index: 899;
    background: var(--bg); overflow-y: auto; padding: 14px 12px 20px;
  }
  #m-more.open { display: block; }
  .m-more-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; font-size: 17px; }
  .m-more-icons { margin-left: auto; margin-right: 6px; }
  .m-more-icons > div { gap: 18px !important; font-size: 19px; }
  .m-more-close { background: none; border: 0; color: var(--text2); font-size: 20px; width: 40px; height: 40px; }
  .m-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .m-tile {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
    padding: 14px 4px; background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
    color: var(--text); font-size: 12px; min-height: 78px;
  }
  .m-tile .m-ic svg { width: 24px; height: 24px; color: var(--text2); }
  .m-account { margin-top: 18px; display: flex; flex-direction: column; gap: 12px; }
  .m-account .sidebar-bottom { position: static; padding: 0; border: 0; margin: 0; }
  .m-account .seg-switch { margin: 0; }

  /* ── dialogs: full-screen sheets ───────────────────────────────────────── */
  .modal-overlay { padding: 0 !important; align-items: stretch !important; }
  .modal-overlay .modal {
    width: 100% !important; max-width: 100% !important; min-height: 100dvh; margin: 0 !important;
    border-radius: 0 !important; border: 0; padding: 16px 14px calc(20px + env(safe-area-inset-bottom)) !important;
  }

  /* ── tables: scroll inside their own box, never the page ──────────────── */
  .table-wrap { overflow-x: auto !important; -webkit-overflow-scrolling: touch; max-width: 100%; }
  .table-wrap > table:not(#orders-table) { min-width: max-content; }

  /* a box that holds a wide table scrolls sideways instead of cutting it off */
  .page div:has(> table:not(#orders-table)) { overflow-x: auto !important; max-width: 100%; -webkit-overflow-scrolling: touch; }
  /* page blocks: less side padding, fixed multi-column grids fold */
  .page div[style^="padding:24px 28px"], .page div[style^="padding:18px 24px"] { padding: 4px 0 !important; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px; }
  .stat-card { padding: 14px; min-width: 0; }
  .page div[style*="grid-template-columns:1fr 1fr"], .page div[style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: minmax(0, 1fr) !important; }
  .page div[style*="display:flex"][style*="margin-left:auto"] { flex-wrap: wrap; }
  .chk-tabbar { padding: 8px 8px 0; scrollbar-width: none; }
  #wh-sticky-top > div[style*="padding:0 32px"] { padding: 0 8px !important; overflow-x: auto; scrollbar-width: none; }

  /* ── touch & typing ───────────────────────────────────────────────────── */
  .btn, select, input:not([type=checkbox]):not([type=radio]):not([type=range]) { min-height: 40px; }
  input, select, textarea { font-size: 16px !important; }   /* iOS zooms in below 16px */
  .qf-bar, .obar { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
  .qf-bar::-webkit-scrollbar, .obar::-webkit-scrollbar { display: none; }

  /* ── floating things stay above the bar ───────────────────────────────── */
  #toast { left: 12px; right: 12px; bottom: calc(70px + env(safe-area-inset-bottom)); }
  #aiw-fab { bottom: calc(70px + env(safe-area-inset-bottom)) !important; right: 12px !important; width: 46px !important; height: 46px !important; }
  #tasks-fab { bottom: calc(126px + env(safe-area-inset-bottom)) !important; right: 12px !important; width: 46px !important; height: 46px !important; }
  #aiw-panel { left: 8px !important; right: 8px !important; width: auto !important; bottom: calc(126px + env(safe-area-inset-bottom)) !important; max-height: 70dvh !important; }
  body.m-sheet-open #aiw-fab, body.m-sheet-open #tasks-fab,
  body.m-more-open #aiw-fab, body.m-more-open #tasks-fab,
  body:has(.modal-overlay.open) #aiw-fab, body:has(.modal-overlay.open) #tasks-fab { display: none !important; }
  #notif-panel, #presence-panel, #activity-panel {
    left: 8px !important; right: 8px !important; width: auto !important; max-width: none !important;
    max-height: calc(100dvh - 160px) !important; overflow-y: auto;
  }
  #status-quick-drop, #wh-status-drop { max-width: calc(100vw - 16px); max-height: 70dvh; overflow-y: auto; }

  /* ── Orders: cards + full-screen order ────────────────────────────────── */
  #page-orders #btn-view-kanban { display: none; }   /* no Kanban on phones; list / «В роботі» stay */
  #orders-table-view > button[onclick^="Orders.toggleColPanel"], #orders-col-panel { display: none !important; }
  /* search first, then every filter in one sideways-swipe row */
  #page-orders .search-row { display: flex; flex-wrap: nowrap; overflow-x: auto; gap: 8px; scrollbar-width: none; }
  #page-orders .search-row::-webkit-scrollbar { display: none; }
  #page-orders .search-row > * { flex: 0 0 auto; width: auto; }
  #page-orders .search-row > .search-input-wrapper { flex: 0 0 calc(100vw - 72px); }
  #page-orders .page-header > div { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
  #orders-kanban-view { display: none !important; }
  #page-orders .table-wrap { background: none; border: 0; overflow: visible !important; }
  #orders-table { display: block; table-layout: auto !important; width: 100% !important; min-width: 0 !important; }
  #orders-thead { display: none; }
  #orders-tbody { display: block; }
  #orders-tbody tr.clickable-row {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "id platform" "client status" "delivery delivery" "payment payment" "ttn ttn";
    gap: 4px 10px; margin: 0 0 8px; padding: 10px 12px;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
  }
  #orders-tbody tr.clickable-row { height: auto !important; }
  #orders-tbody tr.clickable-row > td {
    height: auto !important; max-height: none !important;
    display: block; padding: 0 !important; border: 0 !important; width: auto !important; max-width: none;
    overflow: visible; background: none !important; box-shadow: none !important; min-width: 0;
  }
  #orders-tbody tr.clickable-row > td.expand-arrow,
  #orders-tbody tr.clickable-row > td[data-col]:not([data-col=marketplace_id]):not([data-col=platform]):not([data-col=client]):not([data-col=status]):not([data-col=delivery_status]):not([data-col=payment]):not([data-col=ttn]) { display: none !important; }
  #orders-tbody td[data-col=marketplace_id] { grid-area: id; font-size: 14px; }
  #orders-tbody td[data-col=platform] { grid-area: platform; justify-self: end; }
  #orders-tbody td[data-col=client] { grid-area: client; }
  #orders-tbody td[data-col=status] { grid-area: status; justify-self: end; }
  #orders-tbody td[data-col=delivery_status] { grid-area: delivery; font-size: 12px; }
  #orders-tbody td[data-col=payment] { grid-area: payment; }
  #orders-tbody td[data-col=ttn] { grid-area: ttn; }
  #orders-tbody td[data-col=ttn]:has(> span:only-child) { display: none !important; }   /* lone "—" */
  #orders-tbody td[data-col] * { max-width: 100%; }
  /* desktop layout v2 merges platform / NP status / chat into other cells and hides
     their columns; the phone card keeps its own fields, so show those, drop the extras */
  #orders-tbody tr.clickable-row > td[data-col=platform],
  #orders-tbody tr.clickable-row > td[data-col=delivery_status] { display: block !important; }
  #orders-tbody .o2-x { display: none !important; }
  #orders-tbody tr.order-ready   { box-shadow: inset 4px 0 0 #22c55e; }
  #orders-tbody tr.order-warning { box-shadow: inset 4px 0 0 #f59e0b; }
  #orders-tbody tr.order-blocked { box-shadow: inset 4px 0 0 #94a3b8; }
  #orders-tbody tr.is-duplicate  { border-color: rgba(239, 68, 68, .55); }
  #orders-tbody tr.empty-row, #orders-tbody tr.empty-row > td { display: block; text-align: center; padding: 30px 0; }

  #orders-tbody tr.detail-row:not(.m-sheet) { display: none !important; }
  #orders-tbody tr.detail-row.m-sheet {
    display: block !important; position: fixed; inset: 0; z-index: 950; background: var(--bg);
    overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(132px + env(safe-area-inset-bottom));
  }
  #orders-tbody tr.detail-row.m-sheet > td { display: block; padding: 0 12px !important; border: 0; }
  .m-sheet .detail-content { width: auto !important; max-width: 100% !important; padding-left: 0 !important; padding-right: 0 !important; margin-left: 0 !important; margin-right: 0 !important; }
  /* the tiny "no waybill needed" link is too easy to hit on a card: inside the order only */
  #orders-tbody tr.clickable-row [onclick*="excludeMissingTtn"] { display: none !important; }
  .m-sheet-head {
    position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 10px;
    margin: 0 -12px 10px; padding: 8px 12px; background: var(--bg2); border-bottom: 1px solid var(--border); font-size: 16px;
  }
  .m-sheet-head b { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: monospace; }
  .m-sheet-back { width: 40px; height: 40px; border: 0; border-radius: 10px; background: var(--bg3); color: var(--text); font-size: 20px; }
  .m-sheet-actions {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 951; display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--bg2); border-top: 1px solid var(--border);
  }
  .m-sheet-actions .btn { justify-content: center; min-height: 46px; font-size: 14px; text-decoration: none; }
  .m-sheet-actions .btn:only-child, .m-sheet-actions .btn:last-child:nth-child(odd) { grid-column: 1 / -1; }

  /* ── Warehouse: product cards, filters behind one button, product sheet ─ */
  #wh-sticky-top .page-header { padding: 12px 12px 6px !important; }
  #wh-sticky-top .page-header button[onclick*="openImport"] { display: none; }
  #wh-filter-bar { padding: 8px 12px !important; position: static !important; }
  #wh-filter-bar .search-row { display: flex; flex-wrap: wrap; gap: 8px; overflow: visible; }
  #wh-filter-bar .search-input-wrapper { flex: 1 1 0; min-width: 0; }
  #wh-filter-bar .whm-filter-btn { display: inline-flex; flex: 0 0 auto; }
  #wh-filter-bar .search-row > select, #wh-filter-bar .search-row > button:not(.whm-filter-btn) { display: none; }
  #wh-filter-bar.whm-filters-open .search-row > select { display: block; flex: 1 1 100%; width: 100%; }
  #wh-filter-bar.whm-filters-open #wh-reset-filters:not([style*="display: none"]):not([style*="display:none"]) { display: inline-flex; }
  .whm-fn:not(:empty) { background: var(--accent); color: #fff; border-radius: 8px; padding: 0 6px; font-size: 11px; line-height: 16px; }
  #wh-sticky-thead, #wh-tab-content .table-wrap, #wh-pg-bottom { display: none !important; }
  #wh-mcards { padding: 8px 12px 16px; }
  .whm-card {
    display: flex; align-items: center; gap: 10px; padding: 8px; margin-bottom: 8px;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
  }
  .whm-img { width: 52px; height: 52px; flex: 0 0 52px; border-radius: 9px; object-fit: cover; background: var(--bg3); }
  .whm-noimg { display: flex; align-items: center; justify-content: center; font-size: 22px; color: var(--text2); }
  .whm-main { flex: 1; min-width: 0; }
  .whm-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .whm-sku { font-family: monospace; font-size: 12px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .whm-qty { text-align: right; flex: 0 0 auto; line-height: 1.1; }
  .whm-qty b { display: block; font-size: 18px; }
  .whm-qty span { font-size: 11px; color: var(--text2); }
  .whm-more { width: 100%; justify-content: center; margin-top: 4px; }
  .whm-empty { text-align: center; color: var(--text2); padding: 40px 0; }

  #wh-msheet.open {
    display: block; position: fixed; inset: 0; z-index: 950; background: var(--bg); overflow-y: auto;
    padding: 0 12px calc(132px + env(safe-area-inset-bottom));
  }
  #wh-msheet .m-sheet-head b { font-family: monospace; }
  .whm-photo { display: block; width: 100%; aspect-ratio: 4 / 3; max-height: 46dvh; object-fit: contain; border-radius: 12px; background: var(--bg2); margin-bottom: 10px; }
  .whm-photo.whm-noimg { font-size: 40px; border: 2px dashed var(--border); }
  .whm-title { font-size: 17px; font-weight: 700; margin-bottom: 2px; }
  .whm-meta { color: var(--text2); font-size: 13px; margin-bottom: 10px; }
  .whm-box { background: var(--bg2); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; margin-bottom: 10px; }
  .whm-box:empty { display: none; }
  .whm-box-h { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--text2); margin-bottom: 4px; }
  .whm-row { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 14px; }
  .whm-row b { font-weight: 600; }
  .whm-total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 8px; }
  #wh-msheet .m-sheet-actions .btn { grid-column: auto !important; }

  /* Warehouse other tabs: operations form above the recent list */
  #wh-tab-content > div[style*="grid-template-columns:380px"] { grid-template-columns: minmax(0, 1fr) !important; padding: 12px; gap: 14px !important; }

  /* ── Packing: tab counts, compact items, big "Packed" ─────────────────── */
  #packing-root > div { padding: 12px !important; }
  #packing-root > div > div:first-child > div[style*="margin-left:auto"] { margin-left: 0 !important; width: 100%; }
  #packing-root > div > div:first-child > div[style*="margin-left:auto"] > .btn { flex: 1; justify-content: center; min-height: 42px; }
  .pk-n:not(:empty)::before { content: "· "; }
  .pk-card { padding: 12px !important; border-radius: 12px !important; }
  .pk-item { gap: 10px !important; }
  .pk-item > img, .pk-item > div:first-child:not([style*="flex:1"]) { width: 60px !important; height: 60px !important; }
  .pk-item > div[style*="min-width:70px"], .pk-item > div[style*="min-width:80px"] { min-width: 44px !important; }
  .pk-item > div[style*="min-width"] > div:last-child { font-size: 20px !important; }
  .pk-actions { display: grid !important; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .pk-actions > .btn { justify-content: center; margin-left: 0 !important; white-space: normal; text-align: center; padding: 8px 6px; font-size: 13px; }
  .pk-actions > .pk-packed { grid-column: 1 / -1; min-height: 50px; font-size: 16px; }
  #pk-wh-sel { flex: 1 1 100%; min-height: 42px; }
  /* one order per screen: swipe left / right between cards, the pager steps too */
  #pk-list { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; overscroll-behavior-x: contain; scrollbar-width: none; gap: 0; align-items: flex-start; }
  #pk-list::-webkit-scrollbar { display: none; }
  #pk-list > .pk-card { flex: 0 0 100%; min-width: 0; margin-bottom: 0 !important; scroll-snap-align: center; scroll-snap-stop: always; }
  #pk-list > .pk-card + .pk-card { margin-left: 0; }
  .pk-pager { display: flex !important; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
  .pk-pager > .btn { min-width: 64px; min-height: 42px; justify-content: center; font-size: 22px; line-height: 1; }
  .pk-pager > span { color: var(--text2); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
}
