/* ─────────────────────────────────────────────────────────────────────
   mobile.css — global mobile layer (Phase 1)
   Loaded LAST in <head> on every page, AFTER chrome-mobile.css, so it
   wins the cascade for shared chrome. Per-page inline !important is
   handled in each page's own phase (style-extract); here we use raised
   specificity rather than an !important war wherever possible.

   Owns:
     • mobile hygiene (tap-highlight, overscroll, 16px inputs, focus ring)
     • top-chrome on ≤768px: hide nav LINKS, keep Search/Watch/Connect
     • bottom tab bar (.mnav) + More drawer (.mdrawer) styling
     • safe-area for bottom-nav + ops-pill reflow
   The single mobile boundary is 768px (--bp-md). 480px (--bp-sm) is the
   small-phone refinement. See tokens.css.
   ───────────────────────────────────────────────────────────────────── */

/* ── Mobile hygiene (all viewports; cheap + safe) ─────────────────────── */
* { -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body { overscroll-behavior-y: none; }

/* Fixed overlays (drawer/backdrop/bottom-nav) are off-canvas when closed but
   their box still counts toward document scrollWidth → phantom horizontal
   scroll. Pin them to the visual viewport width and stop the page from ever
   scrolling sideways on mobile. */
@media (max-width: 768px) {
  html, body { max-width: 100%; overflow-x: clip; }
}
.mnav, .mdrawer, .mdrawer-backdrop {
  width: 100vw; max-width: 100vw; box-sizing: border-box;
}

/* Visible keyboard focus without hurting mouse UX */
:focus-visible {
  outline: 2px solid var(--c-color-brand, #3861FB);
  outline-offset: 2px;
  border-radius: 4px;
}
:focus:not(:focus-visible) { outline: none; }

/* iOS: inputs <16px trigger auto-zoom on focus. Force ≥16px on phones. */
@media (max-width: 768px) {
  input, select, textarea { font-size: 16px; }
}

/* ── Bottom tab bar — hidden on desktop, shown ≤768px ─────────────────── */
.mnav { display: none; }

/* The "More" tab is a <button>; without an explicit reset it renders with
   the OS native look (grey fill, outset border) — that was the ugly white
   box. Reset native chrome unconditionally so it matches the <a> tabs. */
button.mnav__item {
  -webkit-appearance: none; appearance: none;
  background: transparent; border: 0; margin: 0; padding: 0;
  font: inherit; cursor: pointer; color: inherit;
}

@media (max-width: 768px) {
  /* Top chrome: collapse primary NAV links (they live in bottom-tab now),
     but KEEP the persistent actions (Search · Watch · Connect). Raised
     specificity (.cmc-header nav) beats chrome-mobile.css's nav rules. */
  .cmc-header nav { display: none; }
  .cmc-header .actions { gap: 8px; flex: 0 0 auto; flex-wrap: nowrap; }
  /* Persistent conversion + retention chrome MUST stay visible on mobile
     (red-team blocker: Connect/Watch/Search must not be buried).
     chrome-mobile.css hides `.cmc-btn:nth-of-type(2)` to drop an EN·USD chip,
     but on pages WITHOUT that chip the 2nd button IS Connect — so it was
     wrongly hidden. Restore it with higher specificity (no !important). */
  .cmc-header .actions .cmc-btn.cmc-btn-primary { display: inline-flex; }  /* Connect */
  .cmc-header .actions .watch-menu,
  .cmc-header .actions [data-cmdk] { display: inline-flex; }               /* Watch · Search */
  /* ops-pill is reachable via More drawer; keep its fixed widget but lift
     it above the bottom-nav and respect safe-area. */
  .ops-pill {
    bottom: calc(var(--bottom-nav-h) + var(--safe-b) + 10px) !important;
  }
  /* watch dropdown must not exceed the viewport on small phones */
  .watch-menu-pop {
    min-width: auto;
    max-width: calc(100vw - 16px);
    right: 8px;
  }

  /* Give every page room so the fixed bottom-nav never covers content. */
  body {
    padding-bottom: calc(var(--bottom-nav-h) + var(--safe-b));
  }

  .mnav {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 150;                 /* above watch-pop/overlays(100), below modals(9000) */
    height: calc(var(--bottom-nav-h) + var(--safe-b));
    padding-bottom: var(--safe-b);
    padding-left: var(--safe-l);
    padding-right: var(--safe-r);
    /* Near-opaque so page content never ghosts through the bar (was .88→.94
       and faint text bled under it). Blur still gives the glass feel. */
    background: linear-gradient(180deg, rgba(11,14,20,.97) 0%, rgba(9,12,17,.99) 100%);
    backdrop-filter: blur(24px) saturate(180%);
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    border-top: 1px solid rgba(255,255,255,.07);
    box-shadow: 0 -8px 24px -16px rgba(0,0,0,.6);
  }
  .mnav__item {
    flex: 1 1 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3px;
    min-height: 44px;             /* HIG touch target */
    text-decoration: none;
    color: var(--c-color-text-secondary, #8b95a7);
    font-size: 10px; font-weight: 600; letter-spacing: .02em;
    -webkit-user-select: none; user-select: none;
    transition: color .15s ease;
  }
  .mnav__item svg { width: 22px; height: 22px; display: block; }
  .mnav__item:active { color: var(--c-color-text-primary, #e8ecf3); }
  .mnav__item.is-active { color: var(--c-color-brand, #6188FF); }
  .mnav__item.is-active svg { filter: drop-shadow(0 0 6px rgba(97,136,255,.35)); }
}

/* ── More drawer (bottom sheet) ───────────────────────────────────────── */
.mdrawer-backdrop {
  position: fixed; inset: 0;
  z-index: 9100;                  /* above bottom-nav, below top modals(9000)?  */
  background: rgba(5,8,13,.55);
  opacity: 0; pointer-events: none;
  transition: opacity .22s ease;
  -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
}
.mdrawer-backdrop.is-open { opacity: 1; pointer-events: auto; }

.mdrawer {
  position: fixed; left: 0; right: 0; bottom: 0;
  z-index: 9101;
  transform: translateY(100%);
  transition: transform .26s cubic-bezier(.32,.72,0,1);
  background: linear-gradient(180deg, rgba(20,26,36,.98), rgba(13,17,24,.99));
  border-top-left-radius: 18px; border-top-right-radius: 18px;
  border-top: 1px solid rgba(255,255,255,.08);
  padding: 8px 0 calc(12px + var(--safe-b));
  max-height: 80dvh; overflow-y: auto;
  overscroll-behavior: contain;
}
.mdrawer.is-open { transform: translateY(0); }
.mdrawer__grip {
  width: 40px; height: 4px; border-radius: 2px;
  background: rgba(255,255,255,.18);
  margin: 6px auto 10px;
}
.mdrawer__section-label {
  font-size: 10px; font-weight: 700; letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--c-color-text-caption, #6a7488);
  padding: 10px 20px 4px;
}
.mdrawer__item {
  display: flex; align-items: center; gap: 14px;
  min-height: 52px; padding: 0 20px;
  text-decoration: none;
  color: var(--c-color-text-primary, #e8ecf3);
  font-size: 15px; font-weight: 500;
}
.mdrawer__item svg { width: 20px; height: 20px; flex: 0 0 auto; opacity: .85; }
.mdrawer__item:active { background: rgba(255,255,255,.05); }
.mdrawer__item.is-active { color: var(--c-color-brand, #6188FF); }
.mdrawer__badge {
  margin-left: auto;
  font-size: 11px; font-weight: 700;
  padding: 2px 8px; border-radius: 10px;
  background: rgba(97,136,255,.14); color: #a4d0ff;
}

/* Respect reduced-motion (Phase 4 hardening, placed early). */
@media (prefers-reduced-motion: reduce) {
  .mdrawer, .mdrawer-backdrop { transition: none; }
  .mnav__item { transition: none; }
}

/* ── Trade-chart drawer: escape the table's horizontal scroller ──────────
   .pos-detail-row is a <td> inside .pos-table-wrap, which scrolls sideways
   whenever the table is wider than the screen. A canvas inside it sizes
   itself to the TABLE, not the viewport — the price scale ends up off-screen
   and chart pans fight the wrapper's own scrolling. (The old SVG survived
   this because viewBox scaled it down; canvas has no such forgiveness.)

   The breakpoint is 1024px, not 768px: a phone in LANDSCAPE is 844px wide
   and still has the narrow-table problem, which a phones-only rule missed. */
@media (max-width: 1024px) {
  /* Two failed attempts worth recording, because both look right in CSS:
       1. `position: sticky` on the panel — a sticky box inside a <td> still
          starts at that cell's origin, so the panel stayed in a narrow column
          with dead space beside it.
       2. `display: block` on the row — .pos-table is itself display:block
          here, so `width:100%` resolved against a shrink-to-fit parent and the
          whole chain collapsed (row 126px → panel 74 → slot 30 → canvas 0).
     What works is keeping table layout intact and sizing the PANEL against the
     viewport directly, pinned to the scroller's left edge. */
  .pos-detail-row > td {
    padding-left: 0;
    padding-right: 0;
  }
  .pos-detail-row .pos-detail-panel {
    position: sticky;
    left: 0;
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
    box-sizing: border-box;
    margin: 0 6px;
    padding: 8px;
    width: calc(100vw - 12px);
    max-width: calc(100vw - 12px);
  }
  /* The real reason the chart looked squeezed on a phone: .pd-top is a
     two-column grid (chart 1.65fr + metrics sidebar 1fr) that never collapsed,
     so the chart only ever got ~60% of an already narrow screen with the
     sidebar beside it. Stack them. Same for the three-column fills row. */
  .pos-detail-panel .pd-top,
  .pos-detail-panel .pd-bottom {
    grid-template-columns: minmax(0, 1fr);
  }
  /* min-height 420 was sized for a desktop column; on a phone it forces the
     drawer taller than the screen before the chart even draws. */
  /* Every wrapper's padding comes straight out of the plot area: the price
     scale already claims ~60px of a 390px screen, so 10px here is 10px of
     candles lost. Trim them on phones. */
  .pos-detail-panel .pd-chart-slot { min-height: 300px; padding: 4px; }
  /* The desktop gesture hint gets clipped mid-sentence at this width. */
  .tc-hint { display: none; }
  /* A one-finger vertical drag over the chart must scroll the PAGE. The old
     rule named `.pd-chart-slot svg` only, so it stopped applying the moment
     the chart became a canvas — and a chart that eats vertical drags traps
     the reader inside a row of a long table. */
  .pd-chart-slot .tc-wrap,
  .pd-chart-slot .tc-wrap canvas { touch-action: pan-y; }
}

/* ── Charts on phones (Phase 2) ──────────────────────────────────────────
   SVG charts already scale via viewBox + preserveAspectRatio and the resize
   handler is debounced (trader.html) — so no JS change needed. We only:
   • stop accidental pinch-zoom hijacking vertical scroll over the chart
   • make range tabs (1d/7d/30d/all) full-width segmented for fat fingers */
@media (max-width: 768px) {
  /* `.pd-chart-slot svg` only ever matched the legacy SVG chart. The v2 trade
     chart draws on a canvas, so the same protection has to name it explicitly
     — otherwise the rule silently stops applying and a finger dragged down
     the chart traps the page instead of scrolling it. */
  .pnl-svg, #pnl-svg, .pd-chart-slot svg { touch-action: pan-y; }

  /* The drawer lives in a <td> inside .pos-table-wrap, which scrolls
     horizontally on phones. A canvas would otherwise size itself to the
     TABLE's width — pushing the price scale off-screen and making chart pans
     fight the wrapper's own scrolling. Pin the panel to the viewport instead.
     (The SVG survived this because viewBox scaled it down; canvas has no such
     forgiveness.) */
  .pnl-tabs { width: 100%; }
  .pnl-tabs .tab {
    flex: 1 1 0; text-align: center; min-height: 36px;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px;
  }
}

/* ── Phase 4: polish — modals, landscape, touch-targets ──────────────────
   Modals on phones: keep them clear of the fixed bottom-nav, use dvh (the
   mobile-correct viewport unit — 100vh over-counts by the browser chrome),
   and become near-full-width sheets. Targets generic overlays used here:
   deposit (.dep-panel), DCA (.dca-overlay), cmd-k (.cmdk-modal already 92vw). */
@media (max-width: 768px) {
  .dep-overlay, .dca-overlay, .lbl-modal-overlay {
    align-items: flex-end;                 /* bottom-sheet feel */
    padding-bottom: calc(var(--bottom-nav-h) + var(--safe-b) + 12px);
  }
  .dep-panel {
    width: 100%;
    max-height: calc(100dvh - var(--bottom-nav-h) - var(--safe-b) - 48px);
    border-bottom-left-radius: 18px; border-bottom-right-radius: 18px;
  }
  /* Touch targets: small interactive bits to ≥40px min (HIG ≥44 ideal).
     Scoped to interactive roles so we don't bloat text/static nodes. */
  a.copy, button.copy, .watch-btn, .pos-tab, .lb-tab, [role="button"] {
    min-height: 40px;
  }
}

/* Landscape phones: viewport is WIDE but SHORT (e.g. 844×390). The portrait
   ≤768px rule misses them, so the bottom-nav would vanish. Re-enable the
   full mobile chrome for short-landscape, slimmed so it doesn't eat a third
   of the screen. Mirrors the ≤768px chrome rules. */
@media (orientation: landscape) and (max-height: 480px) {
  .cmc-header nav { display: none; }
  .cmc-header .actions .cmc-btn.cmc-btn-primary,
  .cmc-header .actions .watch-menu,
  .cmc-header .actions [data-cmdk] { display: inline-flex; }
  html, body { overflow-x: clip; }
  body { padding-bottom: calc(46px + var(--safe-b)); }

  .mnav {
    display: flex;
    position: fixed; left: 0; right: 0; bottom: 0;
    z-index: 150;
    height: calc(46px + var(--safe-b));
    padding-bottom: var(--safe-b);
    background: linear-gradient(180deg, rgba(13,17,24,.88), rgba(13,17,24,.94));
    backdrop-filter: blur(24px) saturate(180%);
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    border-top: 1px solid rgba(255,255,255,.07);
  }
  .mnav__item { flex: 1 1 0; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 2px;
    min-height: 40px; text-decoration: none; font-size: 9px; font-weight: 600;
    color: var(--c-color-text-secondary, #8b95a7); }
  .mnav__item svg { width: 18px; height: 18px; }
  .mnav__item.is-active { color: var(--c-color-brand, #6188FF); }
  .ops-pill { bottom: calc(46px + var(--safe-b) + 8px) !important; }
  .dep-panel, .cmdk-modal { max-height: calc(100dvh - 24px); }
}

/* ── Phase 6: visual-audit fixes (screenshot review on prod) ──────────────
   Issues found by actually looking at every mobile page: */
@media (max-width: 768px) {
  /* (1) Toolbars with many buttons (watching Watched/Compare/Share/Export/
     Import) overflowed the card edge. Let them wrap onto multiple rows. */
  .pos-toolbar { flex-wrap: wrap; row-gap: 8px; }
  .pos-toolbar .grow { display: none; }   /* the flex spacer forces overflow */

  /* (2) Fleet sentiment heatmap is a JS treemap of absolutely-positioned
     tiles computed to a min canvas width — on 390px it clipped labels
     (z:XYZ1, ZEC cut off). Let the body scroll horizontally so every tile
     is reachable instead of sliced at the card edge. */
  #fleet-heatmap-body { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* (3) ⌘K is a desktop keyboard metaphor — on touch it's noise. Replace the
     "⌘K Search" label with a clean "Search". Collapse the original text node
     to zero width (it's a direct child text node) and inject our own label.
     Using a 0-height/clip wrapper isn't possible on a bare text node, so we
     hide it with font-size:0 + transparent and rely on ::before for the label;
     to avoid double text we also zero out letter/word spacing artifacts. */
  .cmc-header .actions [data-cmdk] {
    font-size: 0 !important; color: transparent;
    line-height: 0; word-spacing: -1em;
  }
  .cmc-header .actions [data-cmdk]::before {
    content: "Search";
    font-size: 12px; font-weight: 600; line-height: 1;
    color: var(--c-color-text-primary, #e8ecf3);
    word-spacing: normal;
  }

  /* (4) Tighten the three persistent actions so they sit comfortably at
     375–390px without crowding the logo. */
  .cmc-header .actions { gap: 6px; }
  .cmc-header .actions .cmc-btn,
  .cmc-header .actions .watch-trigger { padding: 6px 9px; }
}

/* ── Phase 7: home (Market Pulse) tables + ticker ribbon ──────────────────
   Found by scrolling the live home page on a phone: the "Top 5 Longs/Shorts/
   Winners" tables use `table.t` (NOT .pos-table, so the Phase-2 rule missed
   them) and have 7 columns — the last PnL column was sliced off the right
   edge. And the sticky ticker ribbon collided with content. */
@media (max-width: 768px) {
  /* Make the table body a horizontal scroll container so all columns are
     reachable instead of clipped. The .glass panel keeps its rounded edge.
     NO sticky columns: with sticky #+Coin the opaque sticky cells overlapped
     the Trader column on scroll (looked broken). A clean horizontal scroll of
     a 5-row summary table reads far better than fighting sticky overlap. */
  .panel .body { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table.t { min-width: max-content; }          /* don't squeeze cols to illegible */
  table.t thead th, table.t tbody td { padding: 8px 11px; font-size: 12px; }

  /* Ticker ribbon: it's sticky at top:60px (header height). On phones it's a
     scrolling marquee — fine — but ensure it never adds horizontal scroll and
     its edges fade rather than hard-clip a half-word. */
  .ribbon { left: 0; right: 0; max-width: 100vw; }
}

/* ── Phase 7b: home duel "OPEN" badge overlap ────────────────────────────
   .open-badge is position:absolute; bottom:16px — on desktop it floats in the
   card's empty lower band, but on the taller stacked mobile card it landed ON
   TOP of the Entry stat ($66,243 ghosted behind it). Let it flow normally on
   phones. */
@media (max-width: 768px) {
  .open-badge {
    position: static; transform: none;
    display: inline-flex; margin: 4px auto 0;
  }
}

/* ── Phase 8: trader #real-banner (THE squished 9-card strip) ─────────────
   trader.html builds this banner in JS with an INLINE
   `grid-template-columns: repeat(9, 1fr)` (8–9 tiles: Real ROI, Win ratio,
   Closed Perp, Closed Spot, Open Now, Current Balance, Deposited, Withdrawn,
   Max DD). On a 390px phone, 9 columns = unreadable vertical slivers (this is
   the bug from the user's screenshot). Inline style → must override with id +
   !important. Re-flow to a 2-column grid on phones, 3 on small tablets. */
@media (max-width: 768px) {
  #real-banner {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 1px 0 !important;
    padding: 8px 0 !important;
  }
  /* each tile: add internal padding + a hairline divider so the 2-col grid
     reads as cells, not a cramped block. The tiles are bare divs. */
  #real-banner > div {
    padding: 10px 12px !important;
    border-top: 1px solid rgba(255,255,255,.05);
  }
}
@media (min-width: 481px) and (max-width: 768px) {
  #real-banner { grid-template-columns: repeat(3, 1fr) !important; }
}
