.mobile-nav-toggle { display: none; }

@media (max-width: 1100px) {
  :root { --terminal-header-height: calc(52px + env(safe-area-inset-top, 0px)); }
  header {
    height: var(--terminal-header-height) !important;
    min-height: var(--terminal-header-height) !important;
    padding: env(safe-area-inset-top, 0px) 8px 0 !important;
    gap: 6px !important;
    overflow: visible !important;
    position: relative !important;
    z-index: 50 !important;
  }
  .header-left { gap: 6px; min-width: 0; flex: 0 0 auto !important; }
  .header-left .brand { font-size: 14px !important; }
  .header-left .brand a { gap: 2px !important; }
  .header-left .brand-name { display: none !important; }
  .header-left .brand-icon { margin-right: 0 !important; }
  .header-left .brand small, header .status, header .unofficial-badge { display: none !important; }
  .header-end { gap: 6px !important; flex: 1 1 0 !important; min-width: 0; margin-left: auto; justify-content: flex-end !important; }
  .header-left .brand { display: flex !important; }
  .header-left .brand a::before { content: ""; display: block; width: 30px; height: 30px; background: url(/xterminal/logo.svg) center / contain no-repeat; }
  .mobile-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 40px;
    width: 40px;
    height: 44px;
    padding: 0;
    font-size: 22px;
  }
  .header-nav-tabs { display: none !important; }
  .mobile-nav-open .header-nav-tabs {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    position: absolute;
    top: calc(100% + 4px);
    left: 8px;
    right: 8px;
    margin: 0;
    padding: 8px;
    gap: 4px;
    background: #1a2035;
    border: 1px solid #2e3d57;
    box-shadow: 0 12px 24px #0006;
  }
  .header-nav-tabs .header-tab { min-height: 44px; padding: 8px; }
  .acc-pill-wrap { flex: 0 1 auto !important; min-width: 0; }
  .acc-pill { width: auto !important; max-width: 100% !important; height: 44px !important; gap: 8px !important; padding-left: 6px !important; padding-right: 10px !important; }
  .acc-pill-balance { flex: 0 1 auto !important; }
  .acc-pill-balance { font-size: clamp(12px, 3.8vw, 15px) !important; overflow: visible !important; text-overflow: clip !important; }
  .profile-pill-wrap { flex: 0 0 auto; }
  .profile-icon-btn { width: 40px !important; height: 44px !important; min-width: 40px !important; }
  .acc-dropdown, .profile-dropdown { max-width: calc(100vw - 16px) !important; }
  main { height: calc(100dvh - var(--terminal-header-height)) !important; max-height: calc(100dvh - var(--terminal-header-height)) !important; }
  .toolbar {
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    min-height: 44px !important;
    height: 44px !important;
    touch-action: pan-x;
  }
  .toolbar::-webkit-scrollbar { display: none; }
  .toolbar > * { flex-shrink: 0; }
  .toolbar .button-label { white-space: nowrap; }
  .welcome-header { flex: 0 0 52px; height: 52px; padding: 0 16px; }
  .welcome-brand { font-size: 17px; flex-wrap: wrap; }
  .welcome-top-nav { display: none; }
  .auth-welcome-overlay { background: #111624; backdrop-filter: none; -webkit-backdrop-filter: none; }
  .welcome-container { flex: 1 0 auto; padding: 16px 12px; }
  .welcome-pane { padding: 20px 18px; }
  .welcome-input, .auth-input, input, select, textarea { font-size: 16px !important; }
  .welcome-card-tab, .welcome-submit-btn { min-height: 44px; }
  .welcome-options { flex-wrap: wrap; gap: 10px; }
  #session-modal { width: calc(100vw - 24px); max-height: calc(100dvh - 24px); overflow-y: auto; padding: 16px !important; }
  .asset-modal.asset-flyout-panel { top: calc(var(--terminal-header-height) + 4px) !important; max-height: calc(100dvh - var(--terminal-header-height) - 12px) !important; }
}

@media (max-width: 650px) and (orientation: portrait) {
  .workspace { min-width: 0; }
}

@media (max-width: 950px) and (max-height: 500px) and (orientation: landscape) {
  html, body { height: 100dvh !important; overflow: hidden !important; }
  main { display: grid !important; grid-template-columns: minmax(0, 1fr) 240px !important; grid-template-rows: minmax(0, 1fr); }
  .rail { display: none !important; }
  .workspace { min-width: 0 !important; min-height: 0 !important; width: 100% !important; height: 100% !important; }
  .trade-sidebar { width: 240px !important; min-width: 0 !important; height: 100% !important; min-height: 0 !important; overflow-y: auto !important; overflow-x: hidden !important; }
  .trade-sidebar .order-panel { padding: 8px !important; }
  .panel-heading, #duration-label-heading, label[for="amount"], #amount-label-heading { display: none !important; }
  .po-trade-actions-row { display: flex !important; flex-direction: row !important; gap: 6px !important; }
  .po-trade-actions-row .trade { flex: 1 1 0 !important; min-width: 0; height: 44px !important; min-height: 44px; margin: 0 !important; font-size: 13px !important; }
  .po-trade-actions-row .trade span { font-size: 13px !important; line-height: 1.2; }
  .desktop-arrow, .desktop-text { display: none !important; }
  .mobile-arrow, .mobile-text { display: inline !important; }
  .trade small { display: none !important; }
  .chart-controls { display: none !important; }
  .welcome-container { align-items: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  .auth-welcome-overlay, .welcome-pane.active, .chart-loader-box * { animation: none !important; }
}

/* Phones and foldables: floating windows and dashboard cards must fit the screen */
@media (max-width: 760px) {
  .webapp-frame-container:not(.minimized) {
    top: var(--terminal-header-height, 52px) !important;
    left: 0 !important;
    right: 0 !important;
    width: 100vw !important;
    height: calc(100dvh - var(--terminal-header-height, 52px)) !important;
    min-width: 0 !important;
    min-height: 0 !important;
    max-width: 100vw !important;
    max-height: none !important;
    border-radius: 0 !important;
  }
  .webapp-frame-container.minimized {
    top: auto !important;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    min-width: 0 !important;
  }
  .webapp-frame-container .tma-topbar { flex-wrap: wrap; min-width: 0; }
  .webapp-frame-container .tma-topbar > * { flex: 1 1 auto; min-width: 0; max-width: 100%; }
  #dash-ai-radar-card { min-width: 0; overflow-wrap: anywhere; }
  #dash-ai-radar-card [style*="grid-template-columns"] { grid-template-columns: minmax(0, 1fr) !important; }
  #dash-ai-radar-card [style*="display:flex"] { flex-wrap: wrap; }
  #dash-ai-radar-card [style*="text-align:right"] { text-align: left !important; }
}

@media (max-width: 380px) {
  .po-trading-inputs-row { gap: 4px !important; }
  .po-time-control-box, .amount { min-width: 0 !important; }
  .mobile-time-prefix { display: none !important; }
  #duration-toggle { min-width: 0 !important; justify-content: center; }
  #duration-label { font-size: 13px !important; letter-spacing: -0.2px; }
  #duration-mode-btn { width: 24px !important; min-width: 24px !important; height: 24px !important; }
  #duration-mode-btn svg { width: 18px; height: 18px; }
  .mobile-currency-prefix { font-size: 11px !important; }
  #amount { min-width: 24px !important; }
  #amount-unit-toggle, #amount-toggle { width: 24px !important; min-width: 24px !important; height: 24px !important; padding: 0 !important; }
}

/* Amount keypad opens by tapping the amount itself; the separate toggle is redundant */
#amount-toggle { display: none !important; }
