/* Showbound on phones and tablets (mobile phase 1, shell). Loaded last, after every module stylesheet.

   B1 API (landed) — for the other builders. Details: docs/MOBILE-PHASE1-BRIEF.md, "B1 API (landed)".
   Breakpoints: phone max-width 640px, tablet 641–1024px, desktop above. Media queries work without JS; the classes below too.
   <html> classes (public/js/device.js): is-phone | is-tablet | is-desktop, is-touch (coarse pointer), is-standalone (installed),
     is-ios, is-android, m-kb-open (on-screen keyboard up), m-drawer-open, m-search-open, m-modal-open, m-has-tabbar.
   JS: SF.device.isPhone() isTablet() isDesktop() isTouch() isStandalone() platform(); SF.device.on('change', fn) → off().
   CSS vars on <html>: --vvh / --vv-top (visible viewport), --m-tabbar-h (height of the bottom tab bar on phones, 0px otherwise).
   Cards: SF.mobileTable({columns, rows, rowHref, empty, cls, rowClass, rowAttrs}) — SF.table plus column m: 'title' | 'sub' |
     'hide' | 'actions' | 'full' (and mLabel). SF.mobileTable.enhance(tableOrRoot) adds the hooks to a table you built yourself.
     Hand-written markup: <table class="tbl m-cards"> with <td data-label="Due" data-m="title|sub|hide|actions|full">.
     Plain list tables (thead, 2–12 columns, no fields in rows) become cards by themselves; opt out with class "m-keep".
   Sheets: every SF.modal / SF.formModal / SF.confirm is a full-screen sheet on phones; Back and route changes close it.
     Your own overlay: const L = SF.layer.open({ name, close(reason) {…} }); call L.done() when it closes itself.
   Sticky actions: <div class="m-actionbar">…buttons…</div> sticks to the bottom of the screen above the tab bar on phones.
   Helpers: .m-only (phones only), .m-hide (hidden on phones), .m-stack (children stack on phones), .m-full (full width on phones).
*/

:root { --m-tabbar-h: 0px; --m-tap: 44px; }

/* phone-only chrome is hidden on bigger screens */
.m-only, .m-tabbar, .m-scrim, .m-search-cancel, .m-search-hint, .m-more-menu { display: none; }
.m-more-menu[hidden] { display: none !important; }

/* update notice (every size) */
.m-update { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(16px + var(--m-tabbar-h) + env(safe-area-inset-bottom, 0px)); z-index: 210;
  display: flex; align-items: center; gap: 12px; background: var(--graphite); color: #fff; padding: 8px 8px 8px 16px; border-radius: var(--r-panel); box-shadow: var(--float); max-width: calc(100vw - 24px); }
.m-update .btn { flex-shrink: 0; }
.m-install .m-steps { margin: 8px 0 14px; padding-left: 22px; } .m-install .m-steps li { margin: 6px 0; }
/* Screen-reader-only text is positioned absolutely; pinned to the left of its box so it never widens the page from inside a
   sideways scroller (the sales pipeline on phones and tablets). Invisible either way. */
@media screen { .sr-only, .crm-sr, .sh-sr, .wh-sr { left: 0; } }
/* Dashboard receivables legend: five fixed columns pushed the 1440px dashboard 43px past the screen (before phase 1 too);
   it now wraps when its card is narrow. Same five columns when there is room. */
@media screen { .fin-aging-legend { grid-template-columns: repeat(auto-fit, minmax(88px, 1fr)); } } /* auto-fit: a wide card spreads the five like before */
.m-note { background: var(--warn-wash); color: #87590F; padding: 9px 12px; border-radius: var(--r-ctl); font-size: 13px; }

/* Every phone rule is screen-only: printing (labels, quotes, pick lists) is untouched.
   ═════════════ Touch screens up to tablet size, and every phone: tap targets and no zoom on focus ═════════════ */
@media screen and (max-width: 640px), screen and (max-width: 1024px) and (pointer: coarse) {
  button, [role=button], [role=tab], summary { min-height: var(--m-tap); }
  button { min-width: var(--m-tap); }
  .btn, .btn.sm { min-height: var(--m-tap); height: auto; }
  .btn.sm { padding: 0 12px; }
  .icon-btn { min-width: var(--m-tap); min-height: var(--m-tap); display: inline-flex; align-items: center; justify-content: center; }
  .tab { min-height: var(--m-tap); }
  input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=color]):not([type=file]), select { min-height: var(--m-tap); }
  input, select, textarea { font-size: 16px !important; } /* iOS zooms into fields under 16px */
  input[type=checkbox], input[type=radio] { width: 22px; height: 22px; }
  .check { min-height: var(--m-tap); }
  .nav a { min-height: var(--m-tap); }
  .sf-user-menu button { min-height: var(--m-tap); }
  .gsearch-results a { min-height: var(--m-tap); }
  .hp-top-btn { width: var(--m-tap); }
}

/* ═════════════ Phones ═════════════ */
@media screen and (max-width: 640px) {
  .m-only { display: inline-flex; }
  .m-hide { display: none !important; }
  .m-stack { flex-direction: column !important; align-items: stretch !important; }
  .m-stack > * { width: 100%; }
  .m-full { width: 100% !important; }

  html.m-has-tabbar { --m-tabbar-h: calc(58px + env(safe-area-inset-bottom, 0px)); }
  html.m-kb-open, body.sf-locked { --m-tabbar-h: 0px; }
  body { display: block; min-height: 100vh; min-height: 100dvh; }
  html.m-modal-open body, html.m-drawer-open body, html.m-search-open body { overflow: hidden; }

  /* ── Drawer: the side menu with its labels ── */
  .sidebar { display: none; position: fixed; top: 0; bottom: 0; left: 0; width: min(304px, 86vw); height: auto; z-index: 96; box-shadow: var(--float);
    padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }
  html.m-drawer-open .sidebar { display: flex; }
  html.m-drawer-open .m-scrim { display: block; position: fixed; inset: 0; z-index: 95; background: rgba(38, 48, 58, .42); }
  .sidebar .brand { font-size: 19px; padding: 14px 56px 12px 18px; justify-content: flex-start; gap: 10px; }
  .m-drawer-close { position: absolute; top: calc(8px + env(safe-area-inset-top, 0px)); right: 8px; }
  .nav { padding: 2px 10px 12px; }
  .nav-title { font-size: 12px; height: auto; padding: 10px 10px 4px; margin: 0; background: none; }
  .nav a { font-size: 15px; justify-content: flex-start; padding: 0 12px; gap: 12px; }
  .nav a .ico { font-size: 16px; width: 20px; }
  .nav a.active::before { left: -10px; }
  .sidebar-foot { padding: 8px 10px; }
  .sidebar-foot .sf-user-txt { display: flex; }
  .sidebar-foot .sf-user-caret { display: grid; }

  /* ── Top bar: menu, search, help, more ── */
  .topbar { gap: 6px; padding: 8px 8px; padding-top: calc(8px + env(safe-area-inset-top, 0px)); padding-left: calc(8px + env(safe-area-inset-left, 0px)); padding-right: calc(8px + env(safe-area-inset-right, 0px)); }
  .m-menu-btn { color: var(--graphite); flex-shrink: 0; }
  .gsearch { min-width: 0; }
  .gsearch input { height: var(--m-tap); padding-right: 12px; }
  .gsearch.has-scam input { padding-right: 48px; } /* the placeholder stops before the camera button */
  html.m-search-open .gsearch.has-scam input { padding-right: 12px; } /* full-screen search: the button sits beside the field */
  .gsearch .kbd { display: none; }
  .top-actions { gap: 6px; flex-shrink: 0; }
  .top-actions > a.btn { display: none !important; }
  .m-more-btn { width: var(--m-tap); padding: 0; justify-content: center; color: var(--graphite); }
  .m-more-menu { display: block; position: fixed; top: calc(58px + env(safe-area-inset-top, 0px)); right: 8px; z-index: 92; min-width: 240px; max-width: calc(100vw - 16px);
    background: var(--paper); border: 1px solid var(--rule); border-radius: var(--r-panel); box-shadow: var(--float); padding: 6px; }
  .m-menu-item { display: flex; align-items: center; gap: 10px; width: 100%; min-height: var(--m-tap); padding: 0 12px; border: 0; background: none; border-radius: var(--r-ctl);
    font: inherit; font-size: 15px; color: var(--graphite); text-align: left; cursor: pointer; text-decoration: none !important; }
  .m-menu-item svg { color: var(--steel); flex-shrink: 0; }
  .m-menu-item:hover, .m-menu-item:focus-visible { background: var(--concrete); }

  /* ── Full-screen search ── */
  html.m-search-open .gsearch { position: fixed; inset: 0; z-index: 110; max-width: none; background: var(--paper); display: flex; flex-wrap: wrap; align-content: flex-start; gap: 8px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 12px; height: var(--vvh, 100dvh); top: var(--vv-top, 0px); bottom: auto; }
  html.m-search-open .topbar { z-index: 115; } /* the search sheet lives inside the sticky top bar: lift it over the tab bar */
  html.m-search-open .gsearch > * { order: 3; }
  html.m-search-open .gsearch input { flex: 1 1 0; width: auto; order: 0; }
  html.m-search-open .gsearch .scam-attach { position: static; order: 1; min-height: var(--m-tap); min-width: var(--m-tap); } /* camera button (B4) beside the field */
  html.m-search-open .gsearch .m-search-cancel { order: 1; }
  html.m-search-open .gsearch .m-search-hint { order: 2; }
  html.m-search-open .m-search-cancel { display: inline-flex; flex-shrink: 0; }
  html.m-search-open .m-search-hint { display: block; flex: 1 0 100%; margin: 4px 2px; font-size: 14px; }
  html.m-search-open .gsearch-results:not([hidden]) ~ .m-search-hint { display: none; }
  html.m-search-open .gsearch-results { position: static; flex: 1 0 100%; max-height: calc(var(--vvh, 100dvh) - 76px - env(safe-area-inset-top, 0px)); border: 0; box-shadow: none; padding: 0; overflow-y: auto; }
  .gsearch-results a { flex-wrap: wrap; row-gap: 2px; font-size: 15px; }
  .gsearch-results a small { flex-basis: 100%; padding-left: 0; }

  /* ── Bottom tab bar ── */
  .m-tabbar { display: grid; grid-template-columns: repeat(var(--m-tabs, 5), minmax(0, 1fr)); position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: var(--paper); border-top: 1px solid var(--rule); padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px); }
  body.sf-locked .m-tabbar, html.m-kb-open .m-tabbar { display: none; }
  .m-tab { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 58px; min-width: var(--m-tap); padding: 6px 2px 4px;
    background: none; border: 0; font: inherit; font-size: 12px; font-weight: 600; color: var(--steel); text-decoration: none !important; cursor: pointer; position: relative; }
  .m-tab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .m-tab.active { color: var(--teal-dark); }
  .m-tab.active::before { content: ""; position: absolute; top: 0; left: 22%; right: 22%; height: 3px; border-radius: 0 0 3px 3px; background: var(--teal); }

  /* ── Page ── */
  main { padding: 14px 12px calc(28px + var(--m-tabbar-h)); padding-left: calc(12px + env(safe-area-inset-left, 0px)); padding-right: calc(12px + env(safe-area-inset-right, 0px)); }
  h1 { font-size: 22px; }
  .page-head { margin-bottom: 14px; gap: 10px; }
  .page-head > div:first-child { min-width: 0; }
  .page-actions { width: 100%; }
  .card-head { padding: 12px 12px 8px; flex-wrap: wrap; }
  .card-body { padding: 4px 12px 12px; }
  .card-body > .table-wrap { margin: 0 -12px -12px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat { padding: 12px 12px; border-bottom: 1px solid var(--rule-soft); }
  .stat-value { font-size: 22px; overflow-wrap: anywhere; }
  .dl { grid-template-columns: minmax(0, max-content) minmax(0, 1fr); gap: 6px 12px; }
  .dl dd { min-width: 0; overflow-wrap: anywhere; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .toolbar > * { min-width: 0; }
  .toolbar input[type=search] { min-width: 0; flex: 1 1 100%; }
  .row { min-width: 0; }
  .empty.big { padding: 36px 16px; }
  pre, code { overflow-wrap: anywhere; white-space: pre-wrap; }
  img, svg, video, canvas { max-width: 100%; }

  /* ── Sticky action bar above the tab bar ── */
  .m-actionbar { position: sticky; bottom: var(--m-tabbar-h); z-index: 30; display: flex; gap: 8px; flex-wrap: wrap; margin: 12px -12px calc(-28px - var(--m-tabbar-h)); padding: 10px 12px;
    background: var(--paper); border-top: 1px solid var(--rule); }
  .m-actionbar .btn { flex: 1 1 0; justify-content: center; }

  /* ── Sheets: every dialog is full screen; Save and Cancel stay above the keyboard ── */
  .modal-backdrop { padding: 0; display: block; top: var(--vv-top, 0px); bottom: auto; height: 100%; height: var(--vvh, 100dvh); }
  .modal, .modal.wide { width: 100%; max-width: none; height: 100%; max-height: none; border-radius: 0; }
  .modal-head { gap: 8px; padding: calc(6px + env(safe-area-inset-top, 0px)) 6px 6px 16px; border-bottom: 1px solid var(--rule-soft); flex-shrink: 0; }
  .modal-head h2 { font-size: 18px; min-width: 0; overflow-wrap: anywhere; }
  .modal-body { flex: 1 1 auto; padding: 14px 16px 20px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
  .modal-foot { flex-shrink: 0; flex-wrap: wrap; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--paper); }
  html.m-kb-open .modal-foot { padding-bottom: 10px; }
  .modal-foot .btn { flex: 1 1 0; justify-content: center; }

  /* ── Help panel: full screen ── */
  .hp-panel { width: 100%; max-width: none; border-left: 0; padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); z-index: 105; }
  .hp-panel-head .btn.sm { display: none; }

  /* ── Toasts above the tab bar ── */
  .toasts { left: 12px; right: 12px; bottom: calc(12px + var(--m-tabbar-h)); }
  .toast { max-width: none; }

  /* ── Sign-in screen ── */
  body.sf-locked > .shell, body.sf-locked > .sidebar { display: none; }

  /* ── List tables as stacked cards ── */
  .table-wrap.m-cards-wrap { background: transparent; border: 0; border-radius: 0; overflow: visible; }
  .card-body > .table-wrap.m-cards-wrap { margin: 0; border-top: 0; }
  table.m-cards, table.m-cards > tbody, table.m-cards > tfoot, table.m-cards > tbody > tr, table.m-cards > tfoot > tr { display: block; width: 100%; }
  table.m-cards > thead { position: absolute; left: 0; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table.m-cards > tbody > tr, table.m-cards > tfoot > tr { background: var(--paper); border: 1px solid var(--rule); border-radius: var(--r-panel); padding: 6px 12px; margin: 0 0 10px; }
  table.m-cards > tfoot > tr { background: #FAFBFC; }
  table.m-cards > tbody > tr > td, table.m-cards > tfoot > tr > td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; width: auto; height: auto;
    min-height: 0; padding: 5px 0; border: 0; text-align: right; overflow-wrap: anywhere; box-shadow: none !important; }
  table.m-cards td::before { content: attr(data-label); flex: 0 0 auto; max-width: 48%; color: var(--steel); font-family: var(--font-ui); font-size: 13px; font-weight: 600; text-align: left; }
  table.m-cards td[data-label=""]::before { content: none; }
  table.m-cards td[data-label=""] { justify-content: flex-start; text-align: left; }
  table.m-cards td:empty, table.m-cards td[data-m="hide"] { display: none; }
  table.m-cards td[data-m="title"] { display: block; text-align: left; font-size: 16px; font-weight: 600; padding: 6px 0 4px; }
  table.m-cards td[data-m="title"]::before, table.m-cards td[data-m="sub"]::before, table.m-cards td[data-m="actions"]::before { content: none; }
  table.m-cards td[data-m="sub"] { display: block; text-align: left; color: var(--steel); font-size: 13px; padding-top: 0; }
  table.m-cards td[data-m="full"] { display: block; text-align: left; }
  table.m-cards td[data-m="full"]::before { display: block; max-width: none; margin-bottom: 2px; }
  table.m-cards td[data-m="actions"] { justify-content: flex-end; flex-wrap: wrap; gap: 8px; padding-top: 8px; }
  table.m-cards tr.m-row-full > td, table.m-cards tr.section-row > td { display: block; text-align: left; }
  table.m-cards > tbody > tr.section-row { background: none; border: 0; padding: 10px 2px 2px; margin: 0; }
  table.m-cards > tbody > tr.section-row > td { background: none; font-size: 14px; }
  table.m-cards > tbody > tr.tape-strip, table.m-cards > tbody > tr[style*="--tape"] { box-shadow: inset 4px 0 0 var(--tape, transparent); padding-left: 16px; }
  table.m-cards td.r { font-size: 15px; }
  table.m-cards tr.clickable:hover td { background: none; }


  /* Pages no phase 1 builder owns: Agenda and the sales pipeline */
  .ag-layout, .ag-side { grid-template-columns: minmax(0, 1fr); }
  .ag-mini-txt { white-space: normal; }
  .ag-mini-stops { gap: 8px; }
  .ag-step { width: var(--m-tap); }
  .ag-seg-btn { min-height: var(--m-tap); }
  .kanban { grid-template-columns: repeat(4, minmax(260px, 82vw)); scroll-snap-type: x mandatory; }
  .kanban-col { scroll-snap-align: start; }
  .kanban-move { height: var(--m-tap); }
  /* Crew list filter "Free from … to …" (no phase 1 screen covers the crew list; B2 may move this into staffing.css) */
  .cr-avail { flex-wrap: wrap; max-width: 100%; }
  .cr-avail input { width: auto; flex: 1 1 130px; min-width: 0; }
}

/* ═════════════ Tablets: the icon rail stays; a little more room for touch ═════════════ */
@media screen and (min-width: 641px) and (max-width: 1024px) and (pointer: coarse) {
  .nav a { padding-top: 0; padding-bottom: 0; align-items: center; }
}

/* .m-only is for phones: the touch sizing above gives icon buttons a display, which showed the drawer's Close menu on tablets */
@media screen and (min-width: 641px) { .m-only { display: none !important; } }

@media print {
  .m-tabbar, .m-scrim, .m-update, .m-more-menu, .m-only, .m-search-cancel, .m-search-hint, .m-actionbar { display: none !important; }
}
