/* Mobile shell (2026-10-29, docs/systems/MOBILE_APP.md Phase 1).
   Phone layout for the Platform's shell and the client pages' common pieces: the page
   header, page padding, text boxes, safe areas, tap sizes and the client pop-ups.
   Loaded LAST in vested-demo.html so it wins over the page CSS blocks, which each
   repeat .topbar / .content at equal specificity. Everything sits inside the 768px
   query, so desktop is untouched. Generic subjects carry :not(:where(.crm-app *)), as
   in the page's own CSS (crmHostCss.test.js), so the hosted CRM keeps its own look. */

/* Safe areas: zero everywhere except a notched phone with viewport-fit=cover (the
   Capacitor app, a home-screen web app, landscape Safari). */
:root {
  --ms-top: env(safe-area-inset-top, 0px);
  --ms-bottom: env(safe-area-inset-bottom, 0px);
  --ms-left: env(safe-area-inset-left, 0px);
  --ms-right: env(safe-area-inset-right, 0px);
  --ms-bar: 56px;                         /* the white top bar (quickSearch.css) */
}

@media (max-width:768px) {

  /* ── Full height that tracks the iPhone toolbar and keyboard (100vh does not) ── */
  .content-area { height:100vh; height:100dvh; }
  body.vtop-on .content-area {
    margin-top:calc(var(--ms-bar) + var(--ms-top));
    height:calc(100vh - var(--ms-bar) - var(--ms-top));
    height:calc(100dvh - var(--ms-bar) - var(--ms-top));
  }
  .shell-sidebar { height:100vh; height:100dvh;
    padding-top:var(--ms-top); padding-bottom:var(--ms-bottom); padding-left:var(--ms-left); }

  /* ── Top bar + menu button clear the notch ── */
  .vtop { height:calc(var(--ms-bar) + var(--ms-top)); padding-top:var(--ms-top);
    padding-right:max(12px, var(--ms-right)); padding-left:calc(62px + var(--ms-left)); }
  #shell-hamburger { top:calc(8px + var(--ms-top)); left:calc(10px + var(--ms-left)); }

  /* The menu button has no job before sign-in or on a must-finish screen (first-login
     password, terms, two-factor setup), and it sat above them all (z 100030). */
  body:has(#authOverlay.show) #shell-hamburger,
  body:has(#fpc-overlay:not([style*="display:none"]):not([style*="display: none"])) #shell-hamburger,
  body:has(#terms-overlay:not([style*="display:none"]):not([style*="display: none"])) #shell-hamburger,
  body:has(#totp-enrol-overlay:not([style*="display:none"]):not([style*="display: none"])) #shell-hamburger { display:none; }

  /* ── Page header: title on the first row, the page's controls on a second row that
     scrolls sideways. It was one fixed 60px row: title + date range + icons could not
     fit in 295px. With the top bar showing, the menu button lives in the bar, so the
     header no longer needs the 52px left clearance. ── */
  .topbar:not(:where(.crm-app *)) {
    height:auto !important; min-height:52px; flex-wrap:wrap; align-items:center;
    row-gap:8px; column-gap:12px; padding:10px 16px !important;
  }
  body:not(.vtop-on) .topbar:not(:where(.crm-app *)) { padding-left:calc(60px + var(--ms-left)) !important; }
  .topbar:not(:where(.crm-app *)) > .topbar-left { flex:1 1 auto; min-width:0; flex-wrap:wrap; gap:8px; }
  .topbar:not(:where(.crm-app *)) .page-title { font-size:22px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:100%; }
  .topbar:not(:where(.crm-app *)) > :not(.topbar-left) {
    flex:1 1 100%; max-width:100%; min-width:0; margin-left:0 !important;
    display:flex; align-items:center; gap:8px;
    overflow-x:auto; -webkit-overflow-scrolling:touch; scrollbar-width:none;
  }
  .topbar:not(:where(.crm-app *)) > :not(.topbar-left)::-webkit-scrollbar { display:none; }
  .topbar .date-range, .topbar .tog, .topbar .vrec-status-tog { flex-shrink:0; }

  /* Charts: the ticker box had an inline 340px minimum and the chart a 720px one. */
  #page-charts .topbar input { min-width:0 !important; }
  #page-charts .topbar > :not(.topbar-left) { overflow:visible; }
  #charts-tv-container { min-height:420px !important; height:calc(100dvh - 220px) !important; }

  /* ── Page padding: 16px a side (was 24–32px, several pages hard-coded) ── */
  .page-content .content:not(:where(.crm-app *)) {
    padding-left:max(16px, var(--ms-left)) !important;
    padding-right:max(16px, var(--ms-right)) !important;
  }

  /* ── Stat cards two across. The shell's own 2-up rule never applied: the page CSS
     blocks set .stat-row back to 4 columns after it, so Dashboard, Performance and
     Dividends ran 530–640px wide. ── */
  .stat-row:not(:where(.crm-app *)) { grid-template-columns:repeat(2, minmax(0, 1fr)) !important; }

  /* ── Text boxes at 16px: iOS zooms the page in on focus when a box is smaller ── */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not(#totp-enrol-code),
  select, textarea { font-size:16px !important; }

  /* ── Tap sizes: segmented buttons and pop-up close buttons were ~22–30px ── */
  .date-btn, .tog-btn, .vrec-status-tog-btn {
    min-height:36px; padding:8px 12px !important; display:inline-flex; align-items:center; justify-content:center;
  }
  .vri-close-btn { min-width:44px; min-height:44px; display:inline-flex; align-items:center; justify-content:center; }

  /* ── Client pop-ups open full screen: Register Interest and Trade Now ── */
  .vri-modal-overlay { padding:0 !important; align-items:stretch !important; }
  .vri-modal { max-width:none !important; min-height:100%; border-radius:0 !important;
    padding-top:var(--ms-top); padding-bottom:var(--ms-bottom); }
  .vri-modal-header { position:sticky; top:0; z-index:2; background:#fff; padding:14px 16px 12px !important; }
  .vri-modal-body { padding:16px !important; }
  .vri-modal-footer { padding-left:16px !important; padding-right:16px !important; padding-bottom:calc(16px + var(--ms-bottom)) !important; }

  /* Research reader (a morning note or Weekly Edge in an iframe): full screen, and the
     iframe is the only thing that scrolls. Inside a scrolling overlay it scrolled twice. */
  .rsh-iframe-modal { padding:0 !important; overflow:hidden !important; align-items:stretch !important; }
  .rsh-iframe-modal > .rsh-iframe-card { max-width:none !important; border-radius:0 !important; padding-top:var(--ms-top); }
  .rsh-iframe-modal iframe { height:calc(100dvh - var(--ms-top)) !important; min-height:0 !important; border-radius:0 !important; }
  .rsh-iframe-modal .rsh-iframe-close { width:44px !important; height:44px !important; top:calc(10px + var(--ms-top)) !important; right:12px !important; }

  /* Recommendation detail: Back + status on one row, Register Interest / Trade Now
     share the next (it was one ~430px row). */
  .vdetail-topbar { flex-wrap:wrap; row-gap:10px; padding:calc(10px + var(--ms-top)) 16px 10px calc(60px + var(--ms-left)) !important; }
  .vdetail-topbar > div:last-child { flex:1 1 100%; }
  .vdetail-topbar > div:last-child > .vdetail-cta { flex:1; min-height:44px; }

  /* ═══ Phase 2: page fixes ═══ */

  /* Dashboard holdings: one card per holding instead of a 9-column table that only
     showed the name. Security + Value on top, price (with today's move) + return
     below; tap a card for units, weight, return $, yield and income
     (shared/mobileShell.js toggles .ms-open). Column order is fixed by
     cdRenderHoldingRows: 1 Security, 2 Units, 3 Price, 4 Value, 5 Weight,
     6 Return %, 7 Return $, 8 Div Yield, 9 Annual Div. */
  #page-dashboard .holdings-table, #page-dashboard .holdings-table tbody,
  #page-dashboard .holdings-table tfoot { display:block; width:100%; }
  #page-dashboard .holdings-table thead { display:none; }
  #page-dashboard .holdings-table tr {
    display:grid; grid-template-columns:minmax(0,1fr) auto; column-gap:12px; row-gap:4px;
    padding:12px 16px; border-bottom:1px solid var(--border); cursor:pointer;
  }
  #page-dashboard .holdings-table td { display:none; padding:0 !important; border:0 !important; text-align:left; width:auto !important; }
  #page-dashboard .holdings-table td:nth-child(1) { display:flex; align-items:center; gap:8px; grid-column:1; grid-row:1; min-width:0; }
  #page-dashboard .holdings-table tfoot td:nth-child(3) { display:none; }
  #page-dashboard .holdings-table td:nth-child(4) { display:block; grid-column:2; grid-row:1; text-align:right; font-weight:600; }
  #page-dashboard .holdings-table td:nth-child(3) { display:block; grid-column:1; grid-row:2; color:var(--text-muted); }
  #page-dashboard .holdings-table td:nth-child(6) { display:block; grid-column:2; grid-row:2; text-align:right; }
  #page-dashboard .holdings-table td[colspan] { display:block; grid-column:1 / -1; text-align:center; }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(2),
  #page-dashboard .holdings-table tr.ms-open td:nth-child(5),
  #page-dashboard .holdings-table tr.ms-open td:nth-child(7),
  #page-dashboard .holdings-table tr.ms-open td:nth-child(8),
  #page-dashboard .holdings-table tr.ms-open td:nth-child(9) {
    display:flex; justify-content:space-between; grid-column:1 / -1; font-size:13px;
  }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(2) { margin-top:6px; padding-top:8px !important; border-top:1px dashed var(--border) !important; }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(2)::before { content:"Units"; color:var(--text-muted); }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(5)::before { content:"Weight"; color:var(--text-muted); }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(7)::before { content:"Return $"; color:var(--text-muted); }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(8)::before { content:"Dividend yield"; color:var(--text-muted); }
  #page-dashboard .holdings-table tr.ms-open td:nth-child(9)::before { content:"Annual dividend"; color:var(--text-muted); }
  #page-dashboard .holdings-table tfoot tr { cursor:default; border-bottom:0; }
  #page-dashboard .holdings-card .card-action { display:none; }   /* "Export →" has no action behind it */

  /* Allocation: the 320px donut column + bars column never stacked; the model
     buttons ran off the right. */
  #page-allocation .top-row { grid-template-columns:minmax(0,1fr) !important; }
  #page-allocation .model-selector { min-width:0; overflow-x:auto; scrollbar-width:none; }
  #page-allocation .model-btn { flex-shrink:0; min-height:36px; display:inline-flex; align-items:center; font-size:13px; }

  /* Optimise: the intro + reviews box squeezed into one narrow column; report rows
     (180px + text + button) collapsed the summary to nothing. */
  #page-optimise .hero { grid-template-columns:minmax(0,1fr) !important; padding:22px 18px !important; }
  #page-optimise .report-row { grid-template-columns:minmax(0,1fr) !important; padding:14px 16px !important; }

  /* Wide minimums: Model Portfolios list (320px), Corporate Deals cards (380px). */
  #page-model-portfolios .cmp-my-list,
  #page-corporate-deals .vdeal-list.vdeal-cards { grid-template-columns:minmax(0,1fr) !important; }
  #page-model-portfolios .content, #page-model-detail .content { padding-top:16px !important; }

  /* Research notes grid is set inline to minmax(300px) by rshRender. */
  #rsh-list { grid-template-columns:minmax(0,1fr) !important; }
  /* The daily report iframe was a fixed 1400px; mobileShell.js sizes it to its
     content, this is the floor until it loads. */
  #page-research iframe[srcdoc] { height:900px; max-width:100% !important; }

  /* Paywall card and pricing pop-up: desktop padding, and a tip about Esc. */
  .client-gate-card { padding:32px 22px 24px !important; }
  .client-gate-tip { display:none; }
  .pm-overlay { padding:0 !important; }
  .pm-modal { border-radius:0 !important; min-height:100%; padding-top:var(--ms-top); padding-bottom:var(--ms-bottom); }
  .pm-header { padding:16px !important; position:sticky; top:0; background:#fff; z-index:2; }
  .pm-body { padding:16px !important; }
  .pm-close { min-width:44px; min-height:44px; }

  /* ═══ Phase 4: bottom tab bar for clients (shared/mobileShell.js adds #ms-tabbar and
     body.ms-tabs). More opens the menu, so the ☰ button goes and the top bar and page
     header take back its space. ═══ */
  #ms-tabbar {
    position:fixed; left:0; right:0; bottom:0; z-index:100020;
    display:grid; grid-auto-flow:column; grid-auto-columns:1fr;
    background:#fff; border-top:1px solid var(--border, #E4E5E3);
    padding:4px max(4px, var(--ms-right)) calc(4px + var(--ms-bottom)) max(4px, var(--ms-left));
    font-family:var(--brand-font-body);
  }
  #ms-tabbar[hidden] { display:none; }
  #ms-tabbar button {
    display:flex; flex-direction:column; align-items:center; justify-content:center; gap:2px;
    min-height:50px; background:none; border:0; padding:4px 2px; cursor:pointer;
    color:var(--text-muted, #848484); font:inherit; font-size:11px; font-weight:600;
  }
  #ms-tabbar button i { font-size:22px; line-height:1; }
  #ms-tabbar button.on { color:var(--brand-accent); }
  body.ms-tabs #shell-hamburger { display:none; }
  body.ms-tabs .vtop { padding-left:max(12px, var(--ms-left)); }
  body.ms-tabs:not(.vtop-on) .topbar:not(:where(.crm-app *)) { padding-left:16px !important; }
  body.ms-tabs .vdetail-topbar { padding-left:16px !important; }
  body.ms-tabs .content-area { padding-bottom:calc(62px + var(--ms-bottom)); }
  body.ms-tabs #demo-plan-switcher { bottom:calc(70px + var(--ms-bottom)) !important; }
  body.ms-tabs.drawer-open #ms-tabbar { z-index:100060; }   /* stays tappable over the menu */
}
