/* Phone terminal (<640px) additions: drawing sheet + floating bar, all-timeframes sheet, Replay bar, tab-bar pins,
   watchlist 4 columns + long-press menu, 44px touch targets. Desktop: only the hide rules at the end. */
#mDraw, #mTfSheet, #mDrawBar { display: none; }

@media (max-width: 639px) {
  body.is-mobile { --m-tf: 46px; }

  /* ---------- touch targets ≥ 44px (shell header, TF strip, sheets) ---------- */
  body.is-mobile #mTop { gap: 4px; }
  body.is-mobile #mBack, body.is-mobile #mAdd, body.is-mobile #hhChipM { height: var(--tap-min, 44px); min-width: var(--tap-min, 44px); }
  body.is-mobile #mAdd { width: var(--tap-min, 44px); display: grid; place-items: center; padding: 0; }
  body.is-mobile #hhChipM { justify-content: center; }
  /* the pair name's tap area covers the whole name + price block (44px tall) */
  body.is-mobile #mMarket { position: relative; min-height: var(--tap-min, 44px); justify-content: center; }
  body.is-mobile #mSymbol::after { content: ""; position: absolute; inset: 0; }
  body.is-mobile #mTf { padding: 0 4px; gap: 2px; }
  body.is-mobile #mTf button { height: var(--tap-min, 44px); min-width: var(--tap-min, 44px); -webkit-touch-callout: none; user-select: none; touch-action: pan-x; }
  body.is-mobile #mMore button, body.is-mobile #mQuick button { min-height: 48px; display: flex; align-items: center; gap: 10px; }
  body.is-mobile #mMore button[hidden], body.is-mobile #mQuick button[hidden] { display: none; }
  body.is-mobile #mMore button { -webkit-touch-callout: none; user-select: none; }
  body.is-mobile #mNav button { min-width: var(--tap-min, 44px); min-height: var(--tap-min, 44px); }
  body.is-mobile #mNav button[hidden] { display: none; }
  body.is-mobile #mNav:has(> [data-pin]) button { font-size: 10px; }
  body.is-mobile #mNav button span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
  /* indicator legend (lesson #5): eye / gear / × are 44px when shown; the collapse arrow gets a 44px hit area */
  /* the icons open on a line of their own: the tap that reveals them must not land on an icon that slid under the finger */
  body.is-mobile #indLegend .ind-leg-row { flex-wrap: wrap; }
  body.is-mobile #indLegend .ind-leg-icons { transform: none; gap: 0; flex-basis: 100%; margin-left: 0; }
  body.is-mobile #indLegend .ind-ico { width: var(--tap-min, 44px); height: var(--tap-min, 44px); display: grid; place-items: center; }
  body.is-mobile #indLegend .ind-ico .icon { width: 18px; height: 18px; }
  body.is-mobile #indLegend .ind-collapse { position: relative; }
  body.is-mobile #indLegend .ind-collapse::after { content: ""; position: absolute; inset: -13px; }

  /* ---------- bottom sheets (drawing tools, all timeframes) ---------- */
  body.is-mobile .m-sheet {
    position: fixed; left: 0; right: 0; bottom: var(--m-nav); z-index: 58;
    max-height: 72dvh; overflow: auto; overscroll-behavior: contain;
    padding: 4px 12px 14px; background: var(--panel); border-radius: 16px 16px 0 0;
    border-top: 1px solid var(--border); box-shadow: var(--shadow-2);
  }
  body.m-draw-open #mDraw, body.m-tf-open #mTfSheet { display: block; }
  body.m-draw-open #mBackdrop, body.m-tf-open #mBackdrop { display: block !important; }
  .m-sheet-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; min-height: 48px; background: var(--panel); }
  .m-sheet-head b { font-size: 15px; }
  .m-sheet-x { width: var(--tap-min, 44px); height: var(--tap-min, 44px); display: grid; place-items: center; border: 0; border-radius: 10px; background: transparent; color: var(--muted); }
  .m-sheet-cap { margin: 10px 2px 6px; color: var(--muted); font: 600 11px/1 var(--sans, inherit); letter-spacing: .06em; text-transform: uppercase; }
  .m-draw-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
  .m-draw-tool { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; min-height: 64px; padding: 6px 4px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel2); color: var(--text); font-size: 11px; line-height: 1.2; text-align: center; }
  .m-draw-tool .icon { width: 20px; height: 20px; }
  .m-draw-tool span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .m-draw-tool:active, .m-tf-grid button:active { background: var(--hover); }
  .m-tf-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; padding-top: 4px; }
  .m-tf-grid button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 56px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel2); color: var(--text); }
  .m-tf-grid button b { font: 600 14px var(--mono, ui-monospace, monospace); }
  .m-tf-grid button span { color: var(--muted); font-size: 10px; }
  .m-tf-grid button.active { background: var(--accent); border-color: var(--accent); color: var(--btn-fg); }
  .m-tf-grid button.active span { color: inherit; opacity: .85; }

  /* ---------- floating drawing bar ---------- */
  body.is-mobile #mDrawBar:not([hidden]) {
    display: flex; align-items: center; gap: 4px;
    position: fixed; left: 8px; right: 8px; bottom: calc(var(--m-nav) + var(--m-strip) + 8px); z-index: 45;
    padding: 4px; border: 1px solid var(--border); border-radius: 14px;
    background: color-mix(in srgb, var(--panel) 92%, transparent); box-shadow: var(--shadow-2);
    -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  }
  #mDrawBar button { min-height: var(--tap-min, 44px); min-width: var(--tap-min, 44px); border: 0; border-radius: 10px; background: transparent; color: var(--text); display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
  #mDrawBar button:disabled { color: var(--muted); opacity: .45; }
  #mDrawBar .m-db-tool { flex: 1; min-width: 0; justify-content: flex-start; padding: 0 10px; background: var(--panel2); font-size: 13px; }
  #mDrawBar .m-db-tool span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #mDrawBar .m-db-done { padding: 0 14px; background: var(--accent); color: var(--btn-fg); font-weight: 600; font-size: 13px; }
  body.m-drawing #mFab { display: none !important; }
  body.m-drawing #mStrip { visibility: hidden; }

  /* ---------- Replay on the phone: start menu as a sheet, controls as a floating bar ---------- */
  body.is-mobile #replayStartMenu:not([hidden]) {
    left: 8px !important; right: 8px; top: auto !important; bottom: calc(var(--m-nav) + 8px); min-width: 0; z-index: 70;
  }
  body.is-mobile #replayStartMenu button { min-height: 48px; font-size: 14px; }
  body.is-mobile #replayStartMenu p { display: none; } /* keyboard hints: no keyboard on a phone */
  body.is-mobile .replay-bar:not([hidden]) {
    position: fixed; left: 8px; right: 8px; bottom: calc(var(--m-nav) + 8px); z-index: 45;
    display: flex; flex-wrap: wrap; align-items: center; gap: 4px; height: auto;
    padding: 6px; border: 1px solid var(--border); border-radius: 14px;
    background: var(--panel); box-shadow: var(--shadow-2);
  }
  body.is-mobile:has(#chartWrap.replay-on) #mStrip { visibility: hidden; } /* the bar sits over the gesture hint */
  body.is-mobile .replay-bar > b { display: none; }
  body.is-mobile .replay-bar .tb, body.is-mobile .replay-bar select, body.is-mobile .replay-bar input[type=date] { min-height: var(--tap-min, 44px); min-width: var(--tap-min, 44px); font-size: 16px; border-radius: 10px; }
  body.is-mobile .replay-bar input[type=date] { flex: 1 1 130px; min-width: 0; font-size: 14px; }
  body.is-mobile .replay-bar .replay-status, body.is-mobile .replay-bar .replay-stats { order: 9; flex: 1 1 100%; margin: 0 4px; font-size: 11px; }
  body.is-mobile .replay-bar .tb:last-of-type { font-size: 13px; padding: 0 12px; }
  body.is-mobile #chartWrap.replay-on #chart { inset: 0 0 0; }
  body.is-mobile:has(#chartWrap.replay-on) #mFab { display: none !important; }

  /* ---------- tab-bar pins and the «Ще» pin menu ---------- */
  body.is-mobile #mMore button.m-pinned::after { content: ""; width: 6px; height: 6px; margin-left: auto; border-radius: 50%; background: var(--accent); }
  body.is-mobile .m-pin-pop { position: absolute; right: 16px; z-index: 2; padding: 4px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel2); box-shadow: var(--shadow-2); }
  body.is-mobile .m-pin-pop[hidden] { display: none; }
  body.is-mobile #mMore .m-pin-pop button { width: auto; min-height: var(--tap-min, 44px); padding: 0 14px; border: 0; border-radius: 8px; font-size: 14px; }

  /* ---------- watchlist: 4 columns, actions via long-press ---------- */
  body.is-mobile table.watch { width: 100%; min-width: 0; table-layout: auto; }
  body.is-mobile .watch tr > th:nth-child(n+5), body.is-mobile .watch tr[data-s] > td:nth-child(n+5),
  body.is-mobile .watch th.acts-h, body.is-mobile .watch td.acts { display: none; }
  body.is-mobile .watch th, body.is-mobile .watch td { min-width: 0; }
  body.is-mobile .watch th:first-child, body.is-mobile .watch td.sym { position: static; box-shadow: none; min-width: 0; }
  body.is-mobile .watch tr[data-s] > td { height: var(--tap-min, 44px); }
  body.is-mobile .watch tr[data-s] { -webkit-touch-callout: none; user-select: none; }
  body.is-mobile .watch .pin-btn, body.is-mobile .watch .tag-dot { pointer-events: none; }
  body.is-mobile #watchWrap { overflow-x: hidden; }
  body.is-mobile #watchRowMenu:popover-open {
    position: fixed; inset: auto 8px calc(var(--m-nav) + 8px) 8px !important; margin: 0;
    width: auto; max-height: 70dvh; overflow: auto; padding: 6px; border-radius: 14px;
  }
  body.is-mobile #watchRowMenu::backdrop { background: var(--backdrop); }
  body.is-mobile #watchRowMenu .menu-item { min-height: var(--tap-min, 44px); width: 100%; font-size: 14px; }
  body.is-mobile #watchRowMenu .flag-row { display: flex; flex-wrap: wrap; gap: 0; }
  body.is-mobile #watchRowMenu .flag-pick { width: var(--tap-min, 44px); height: var(--tap-min, 44px); background-clip: content-box; padding: 13px; }
}

@media (min-width: 640px) {
  #mDraw, #mTfSheet, #mDrawBar, .m-pin-pop { display: none !important; }
}
