/* Етап 5: мобільна оболонка лише <768px. Десктоп не змінюється. */
#offlineBadge { display: none; }
#mChrome, #mBackdrop, #mQuick, #mMore, #mFab { display: none; }

@media (max-width: 767px) {
  html, body { overflow: hidden; }
  body.is-mobile {
    --m-top: 52px;
    --m-tf: 40px;
    --m-strip: 44px;
    --m-nav: calc(56px + env(safe-area-inset-bottom, 0px));
  }

  #drawBar, #topbar, #chartTools, #bottom,
  #side .panel-grip, #leftRail .panel-grip { display: none !important; }
  .chart-foot { display: none !important; }

  #app {
    grid-template-columns: 1fr !important;
    grid-template-rows: var(--m-top) var(--m-tf) minmax(0, 1fr) var(--m-strip) var(--m-nav) !important;
    grid-template-areas:
      "mtop"
      "mtf"
      "chart"
      "mstrip"
      "mnav" !important;
    height: 100dvh;
  }

  #mChrome { display: contents; }
  #mTop { grid-area: mtop; display: flex; align-items: center; gap: 6px; padding: 0 8px 0 calc(8px + env(safe-area-inset-left, 0px)); padding-right: calc(8px + env(safe-area-inset-right, 0px)); background: var(--panel); border-bottom: 1px solid var(--border); min-width: 0; }
  #mTf { grid-area: mtf; display: flex; align-items: center; gap: 4px; padding: 0 8px; overflow-x: auto; scrollbar-width: none; background: var(--panel); border-bottom: 1px solid var(--border); }
  #mTf button { flex: 1 0 auto; height: 30px; min-width: 40px; border: 0; border-radius: 6px; background: transparent; color: var(--text); font: 600 12px var(--mono, ui-monospace, monospace); }
  #mTf button.active { background: var(--accent); color: var(--btn-fg); }
  #mStrip { grid-area: mstrip; display: flex; align-items: center; justify-content: center; height: var(--m-strip); background: var(--panel); border-top: 1px solid var(--border); color: var(--muted); font-size: 11px; text-align: center; padding: 0 12px; touch-action: none; user-select: none; }
  #mNav { grid-area: mnav; display: flex; align-items: stretch; background: var(--panel); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom, 0px); z-index: 60; }
  #mNav button { flex: 1; border: 0; background: transparent; color: var(--muted); font-size: 11px; font-weight: 600; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
  #mNav button.active { color: var(--accent); }
  #chartWrap { grid-area: chart; }
  #mBook { grid-area: chart; position: absolute; top: 0; left: 0; right: 0; z-index: 4; pointer-events: none; font: 10px var(--mono, ui-monospace, monospace); background: color-mix(in srgb, var(--panel) 86%, transparent); padding: 2px 6px; display: flex; justify-content: space-between; gap: 8px; }
  #mBook[hidden] { display: none !important; }
  #mBook .ask { color: var(--red); }
  #mBook .bid { color: var(--green); }

  #mBack, #mAdd { border: 0; background: var(--hover); color: var(--text); border-radius: 8px; height: 32px; padding: 0 8px; font-size: 12px; flex: none; }
  #mAdd { width: 32px; font-size: 20px; line-height: 1; }
  #mMarket { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.15; }
  #mSymbol { border: 0; background: transparent; color: var(--text); font-weight: 700; font-size: 14px; text-align: left; padding: 0; }
  #mPxRow { display: flex; gap: 8px; align-items: baseline; }
  #mPrice { font-size: 13px; }
  #mChange { font-size: 11px; }

  #mFab {
    display: grid; place-items: center;
    position: fixed; z-index: 35;
    width: 52px; height: 52px; border-radius: 50%;
    border: 0; background: var(--accent); color: var(--btn-fg);
    font-size: 28px; font-weight: 600; box-shadow: var(--shadow-2);
    touch-action: none;
  }
  #mFab[hidden], body.m-fab-hidden #mFab { display: none !important; }

  #mBackdrop { display: none; position: fixed; inset: 0 0 var(--m-nav) 0; background: var(--backdrop); z-index: 50; }
  body.m-sheet-left #mBackdrop,
  body.m-sheet-side #mBackdrop,
  body.m-more-open #mBackdrop,
  body.m-quick-open #mBackdrop { display: block !important; }

  #leftRail, #side {
    position: fixed !important;
    left: 0; right: 0; bottom: var(--m-nav);
    top: auto;
    width: 100% !important;
    height: min(78dvh, calc(100dvh - var(--m-nav) - var(--m-top)));
    max-width: none !important;
    z-index: 55;
    transform: translateY(110%);
    transition: transform .28s ease;
    border-radius: 16px 16px 0 0;
    border: 0;
    border-top: 1px solid var(--border);
    display: flex !important;
    background: var(--panel);
  }
  body.m-sheet-left #leftRail,
  body.m-view-watchlist #leftRail { transform: none; }
  body.m-sheet-side #side { transform: none; }
  body.m-view-watchlist #leftRail {
    top: var(--m-top);
    bottom: var(--m-nav);
    height: auto;
    border-radius: 0;
    z-index: 40;
  }
  body.m-view-watchlist #mTf,
  body.m-view-watchlist #mStrip { visibility: hidden; pointer-events: none; }
  body.m-view-watchlist #mBackdrop { display: none !important; }
  body.m-view-watchlist #mTop { z-index: 41; position: relative; }
  body.m-view-watchlist #mFab { display: none !important; }

  #mMore, #mQuick {
    display: none;
    position: fixed; left: 0; right: 0; bottom: var(--m-nav);
    z-index: 58;
    background: var(--panel);
    border-radius: 16px 16px 0 0;
    padding: 10px 12px 12px;
    max-height: 80dvh;
    overflow: auto;
    box-shadow: var(--shadow-2);
  }
  body.m-more-open #mMore,
  body.m-quick-open #mQuick { display: block; }
  #mMore button, #mQuick button {
    display: block; width: 100%; text-align: left;
    border: 0; background: transparent; color: var(--text);
    padding: 12px 8px; border-bottom: 1px solid var(--border); font-size: 15px;
  }


  /* Легенда: компактна, не накриває свічки */
  #indLegend {
    font-size: 10px;
    top: 4px;
    left: 4px;
    max-width: min(300px, calc(100% - 80px));
    padding: 3px 6px;
    line-height: 1.25;
    pointer-events: auto;
    background: color-mix(in srgb, var(--panel) 82%, transparent);
  }
  #indLegend .ind-leg-sym,
  #indLegend .ind-leg-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }
  #indLegend .ind-leg-row { gap: 4px; }
  #indLegend .ind-collapse { width: 18px; height: 18px; font-size: 11px; }
  #indLegend .ind-leg-icons { transform: scale(.85); transform-origin: right center; }

  /* Список пар: горизонтальний скрол, липка колонка «Пара» */
  #watchWrap {
    overflow-x: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  table.watch {
    width: max-content;
    min-width: 100%;
  }
  .watch th, .watch td { min-width: 4.25rem; }
  .watch th:first-child, .watch td.sym {
    position: sticky;
    left: 0;
    z-index: 2;
    min-width: 7.6rem;
    background: var(--panel);
    box-shadow: var(--shadow-1);
  }
  .watch thead th:first-child { z-index: 3; top: 0; }
  .watch tr.active td.sym { background: var(--selected); }
  .watch tbody tr:hover td.sym { background: var(--hover); }

  #offlineBadge:not([hidden]) {
    display: inline-flex;
    position: absolute;
    top: 6px;
    right: 72px;
    z-index: 9;
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: .01em;
    background: var(--warning-soft);
    color: var(--warning);
    border: 1px solid var(--warning);
    pointer-events: none;
    max-width: 46%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Приховані шторки не лишають смужки в сітці */
  #leftRail, #side { visibility: hidden; pointer-events: none; }
  body.m-sheet-left #leftRail,
  body.m-view-watchlist #leftRail,
  body.m-sheet-side #side { visibility: visible; pointer-events: auto; }
  #app { grid-auto-columns: 0; grid-auto-rows: 0; }
  #mFab, #mBackdrop, #mMore, #mQuick, #mBook, #toasts { grid-area: unset; }
  #toasts {
    left: 10px;
    right: 10px;
    top: auto;
    bottom: calc(var(--m-nav) + var(--m-strip) + 8px);
    align-items: stretch;
  }
  #toasts .toast { max-width: none; width: 100%; box-sizing: border-box; }
  #indLegend .ind-leg-icons { display: none; }
  #indLegend.is-hot .ind-leg-icons,
  #indLegend .ind-leg-row:focus-within .ind-leg-icons { display: inline-flex; }

  dialog.dlg, dialog.help-dlg, dialog.cmd-palette, #indDialog, #commandPalette {
    width: 100% !important;
    max-width: 100% !important;
    margin: auto 0 var(--m-nav) 0 !important;
    border-radius: 16px 16px 0 0;
    max-height: calc(100dvh - var(--m-nav) - 12px);
  }
}

@media (min-width: 768px) {
  #mChrome, #mBackdrop, #mQuick, #mMore, #mFab { display: none !important; }
}
