/* ============================================================
   MOBILE REDESIGN (Feature #14)
   Sits on top of style.css. Activates at <= 767px only.
   Driven by `body[data-mobile-screen="assets|chart|menu"]`.
   ============================================================ */

/* Disable legacy mobile-shell overlay (replaced by this layer). */
#mobile-shell, #mobile-nav, #mob-symbol-bar, .mob-back-btn,
#mobile-user-dropdown { display: none !important; }

/* New mobile layer elements are display:none on desktop and only
   un-hidden by the @media query below. Without these defaults the
   FAB Buy/Sell buttons + asset-list screen + bottom-sheet leak into
   the desktop layout (showed up as a stray "BUY SELL" text chip at
   the top-left of the chart toolbar). */
#mobile-hamburger-btn,
#mobile-fab,
#mobile-asset-list-screen,
#mobile-menu-sheet { display: none; }

@media (max-width: 767px) {

  body { overflow: hidden; }

  #mobile-hamburger-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; background: transparent; border: 0;
    color: var(--text-primary); cursor: pointer; padding: 0;
    margin-right: 4px; border-radius: 6px;
  }
  #mobile-hamburger-btn:hover { background: var(--hover-surface); }

  #top-bar { padding: 0 8px !important; height: 48px !important; flex-shrink: 0; }
  .topbar-metrics, .topbar-time-block, .topbar-divider,
  #market-status, .topbar-user-wrap, #topbar-deposit-btn {
    display: none !important;
  }

  #app {
    display: flex !important; flex-direction: column !important;
    height: 100dvh !important; overflow: hidden !important;
  }

  /* Hide desktop structural blocks; re-show per screen below. */
  #left-sidebar, #lp-drag-handle, #bottom-area,
  #center-area, #cal-movers-panel { display: none !important; }

  /* ---------- ASSETS SCREEN (home) ---------- */
  body[data-mobile-screen="assets"] #mobile-asset-list-screen { display: flex; }
  #mobile-asset-list-screen {
    display: none; flex-direction: column; flex: 1;
    min-height: 0; background: var(--bg-primary);
  }
  .m-asset-search {
    flex-shrink: 0; padding: 8px 12px; background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
  }

  /* Category tabs row — All / FX / Crypto / Stocks / Indices / Commodities */
  .m-asset-tabs {
    flex-shrink: 0; display: flex; gap: 4px;
    padding: 6px 8px 8px; background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-primary);
    overflow-x: auto; scrollbar-width: none;
  }
  .m-asset-tabs::-webkit-scrollbar { display: none; }
  .m-asset-tab {
    flex-shrink: 0; padding: 6px 12px; border: 0;
    background: transparent; color: var(--text-secondary);
    font-size: 12px; font-weight: 500; cursor: pointer; border-radius: 14px;
    white-space: nowrap; transition: background .12s, color .12s;
  }
  .m-asset-tab:hover { color: var(--text-primary); }
  .m-asset-tab.active {
    background: rgba(108, 92, 231, 0.18);
    color: #c8bdf3;
    border: 1px solid rgba(108, 92, 231, 0.4);
    padding: 5px 11px;        /* compensate for the 1px border */
  }
  .ar-change.muted { color: var(--text-muted) !important; }
  .m-asset-search input {
    width: 100%; box-sizing: border-box; padding: 8px 12px;
    background: var(--bg-deepest, rgba(0,0,0,.25)); color: var(--text-primary);
    border: 1px solid var(--border-primary); border-radius: 8px;
    font-size: 14px; outline: none;
  }
  .m-asset-search input:focus { border-color: #2962FF; }
  .m-asset-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .m-asset-row {
    display: grid; grid-template-columns: 1fr auto; gap: 8px;
    padding: 12px 14px; border-bottom: 1px solid var(--border-primary);
    cursor: pointer; align-items: center;
  }
  .m-asset-row:active { background: var(--hover-surface); }
  .m-asset-row .ar-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .m-asset-row .ar-meta { min-width: 0; }
  .m-asset-row .ar-sym { font-weight: 700; font-size: 15px; color: var(--text-primary); }
  .m-asset-row .ar-class { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
  /* Per-asset-class icon badge. 24x24 circle, glyph centered. */
  .m-asset-row .ar-icon {
    flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center;
    width: 24px; height: 24px; border-radius: 50%;
    font-size: 11px; font-weight: 700; line-height: 1;
    color: #fff; font-family: Inter, sans-serif;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,0.08);
  }
  .m-asset-row .ar-icon-forex     { background: #2962FF; }
  .m-asset-row .ar-icon-crypto    { background: #f7931a; font-size: 14px; }
  .m-asset-row .ar-icon-commodity { background: #b8860b; }
  .m-asset-row .ar-icon-index     { background: #6a5acd; font-size: 13px; }
  .m-asset-row .ar-icon-stock     { background: #26a69a; font-size: 10px; }
  .m-asset-row .ar-icon-other     { background: #555; }
  .m-asset-row .ar-prices {
    display: flex; flex-direction: column; align-items: flex-end;
    gap: 2px; font-family: 'JetBrains Mono', monospace;
  }
  .m-asset-row .ar-bidask { font-size: 13px; display: flex; gap: 8px; }
  .m-asset-row .ar-bid.up   { color: var(--green, #26a69a); }
  .m-asset-row .ar-bid.down { color: var(--red,   #ef5350); }
  .m-asset-row .ar-ask      { color: var(--text-secondary); }
  .m-asset-row .ar-change   { font-size: 11px; font-weight: 600; }
  .m-asset-row .ar-change.up   { color: var(--green, #26a69a); }
  .m-asset-row .ar-change.down { color: var(--red,   #ef5350); }
  .m-asset-empty {
    padding: 30px 16px; text-align: center;
    color: var(--text-muted); font-size: 13px;
  }

  /* ---------- CHART SCREEN ---------- */
  body[data-mobile-screen="chart"] #center-area {
    display: flex !important; flex-direction: column !important;
    flex: 1; min-height: 0; position: relative;
    height: auto !important; width: 100% !important;
  }
  body[data-mobile-screen="chart"] #chart-toolbar {
    padding: 0 8px; gap: 4px; height: 36px; flex-shrink: 0; overflow-x: auto;
  }
  body[data-mobile-screen="chart"] #chart-container {
    flex: 1 !important; height: auto !important;
    min-height: 0 !important; width: 100% !important; position: relative;
  }
  .m-chart-back-btn, .m-chart-fullscreen-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; background: transparent; border: 0;
    color: var(--text-primary); cursor: pointer; flex-shrink: 0;
  }
  .m-chart-back-btn:active, .m-chart-fullscreen-btn:active { background: var(--hover-surface); border-radius: 6px; }
  /* Landscape: free chart real-estate by hiding the FAB buttons. */
  body.m-landscape[data-mobile-screen="chart"] #mobile-fab { display: none !important; }

  /* FAB Buy/Sell — only shown on the chart screen */
  #mobile-fab {
    display: none; position: fixed; right: 14px; bottom: 22px;
    z-index: 950; flex-direction: column; gap: 10px;
  }
  body[data-mobile-screen="chart"] #mobile-fab { display: flex; }
  .m-fab-btn {
    width: 60px; height: 60px; border-radius: 50%; border: 0;
    font-family: Inter, sans-serif; font-weight: 700; font-size: 13px;
    color: #fff; box-shadow: 0 4px 14px rgba(0,0,0,0.4); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: transform 0.1s;
  }
  .m-fab-btn:active { transform: scale(0.92); }
  .m-fab-buy  { background: var(--green, #26a69a); }
  .m-fab-sell { background: var(--red,   #ef5350); }

  /* ---------- BOTTOM-SHEET MENU ---------- */
  #mobile-menu-sheet[hidden] { display: none !important; }
  #mobile-menu-sheet {
    position: fixed; inset: 0; z-index: 1500;
    display: flex; flex-direction: column; justify-content: flex-end;
  }
  .m-sheet-backdrop {
    position: absolute; inset: 0; background: rgba(0,0,0,0.55);
    opacity: 0; transition: opacity 0.22s;
  }
  #mobile-menu-sheet.open .m-sheet-backdrop { opacity: 1; }
  .m-sheet-panel {
    position: relative; background: var(--bg-secondary);
    border-top-left-radius: 16px; border-top-right-radius: 16px;
    padding: 12px 0 calc(env(safe-area-inset-bottom, 0px) + 16px);
    max-height: 80vh; overflow-y: auto;
    transform: translateY(100%); transition: transform 0.25s ease-out;
    box-shadow: 0 -8px 24px rgba(0,0,0,0.4);
  }
  #mobile-menu-sheet.open .m-sheet-panel { transform: translateY(0); }
  .m-sheet-handle {
    width: 36px; height: 4px; background: var(--text-muted);
    opacity: 0.4; border-radius: 2px; margin: 0 auto 8px;
  }
  .m-sheet-header {
    padding: 6px 18px 12px; border-bottom: 1px solid var(--border-primary);
    color: var(--text-primary); font-weight: 600; font-size: 14px;
    display: flex; align-items: center; gap: 10px;
  }
  .m-sheet-userblock { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .m-sheet-plan-badge {
    align-self: flex-start; display: inline-block;
    padding: 2px 8px; border-radius: 10px;
    font-size: 10px; font-weight: 700; letter-spacing: 0.4px;
    text-transform: uppercase; line-height: 1.4;
    background: #2d3748; color: #fff; border: 1px solid #2d3748;
  }
  .m-sheet-acct-type {
    align-self: flex-start; display: inline-block;
    padding: 1px 7px; border-radius: 8px;
    font-size: 10px; font-weight: 600; letter-spacing: 0.3px;
    text-transform: uppercase; line-height: 1.4;
  }
  .m-sheet-acct-type.live { background: rgba(38,166,154,0.18); color: #26a69a; }
  .m-sheet-acct-type.demo { background: rgba(251,191,36,0.18); color: #fbbf24; }
  .m-sheet-item {
    display: flex; align-items: center; gap: 12px;
    padding: 13px 18px; background: none; border: 0; width: 100%;
    text-align: start; color: var(--text-primary);
    font-family: inherit; font-size: 14px; cursor: pointer;
  }
  .m-sheet-item:active { background: var(--hover-surface); }
  .m-sheet-item .ico { font-size: 18px; width: 22px; text-align: center; }
  .m-sheet-divider { height: 1px; background: var(--border-primary); margin: 6px 0; }
  .m-sheet-item.logout { color: var(--red, #ef5350); }
  .m-sheet-sub {
    padding: 4px 18px 8px 52px; display: flex; flex-wrap: wrap; gap: 6px;
  }
  .m-sheet-sub button {
    padding: 6px 12px; background: var(--hover-surface);
    border: 1px solid var(--border-primary); border-radius: 14px;
    color: var(--text-primary); font-size: 12px; cursor: pointer;
  }
  .m-sheet-sub button.active {
    background: #2962FF; color: #fff; border-color: #2962FF;
  }

  /* ---------- Full-screen modals (deposit/withdraw/etc.) ----------
     openTerminalModal() builds .tm-overlay > .tm-panel; we override
     the centered-dialog look with a full-screen sheet on mobile. */
  .tm-overlay { align-items: stretch !important; padding: 0 !important; }
  .tm-panel {
    width: 100vw !important; max-width: none !important;
    height: 100dvh !important; max-height: none !important;
    border-radius: 0 !important;
    display: flex; flex-direction: column;
  }
  .tm-header {
    padding: 12px 16px !important; flex-shrink: 0;
    padding-top: calc(env(safe-area-inset-top, 0px) + 12px) !important;
  }
  .tm-body {
    flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 16px) !important;
  }

  /* Draggable chat: JS sets inline left/top + adds .m-draggable. */
  #cw-btn.m-draggable { right: auto !important; bottom: auto !important; touch-action: none; }
  /* Minimize button injected into the chat header on mobile. */
  .m-cw-minimize {
    background: none; border: 0; color: rgba(255,255,255,0.5);
    font-size: 22px; line-height: 1; cursor: pointer; padding: 0 8px;
    margin-inline-start: auto;
  }
  .m-cw-minimize:hover { color: #fff; }
  #cw-panel {
    width: 100vw !important; max-width: none !important;
    height: calc(100dvh - 60px) !important; max-height: none !important;
    right: 0 !important; left: 0 !important; bottom: 60px !important;
    border-radius: 0 !important;
  }
}

/* Light-theme tweaks (mobile-only classes that don't inherit). */
html[data-theme="light"] .m-sheet-backdrop { background: rgba(0,0,0,0.35); }
html[data-theme="light"] .m-asset-search input { background: #fff; }
