/*
 * DEX V5, Track B: the mobile trading layout (behaviour in dex5-mobile.js).
 *
 * html.dx5-m   phone, or a narrow device held landscape (set by dex5-mobile.js from PHONE_QUERY)
 * html.dx5-t   tablet, 768-820 px (TABLET_QUERY); from 821 px dex5-desktop.css owns the grid
 *
 * Tokens: the V5 contract names (--dx-*), falling back to the current sheet's names until
 * src/dex5-tokens.css lands. Load this file AFTER every other stylesheet.
 *
 * NOTHING HERE HIDES THE MANDATORY REVIEW: the review <dialog> is restyled to fit a phone,
 * never collapsed, and it sits in the top layer above every layer this file adds.
 */

:root {
  --dx5-bg: var(--dx-bg, var(--bg));
  --dx5-surface: var(--dx-surface, var(--surface));
  --dx5-surface-2: var(--dx-surface-2, var(--surface-2));
  --dx5-border: var(--dx-border, var(--line));
  --dx5-text: var(--dx-text, var(--ink));
  --dx5-dim: var(--dx-text-dim, var(--ink-2));
  --dx5-accent: var(--dx-accent, var(--accent));
  --dx5-buy: var(--dx-buy, var(--up600));
  --dx5-sell: var(--dx-sell, var(--down600));
  --dx5-focus: var(--dx-focus, var(--accent));
  --dx5-mono: var(--dx-mono, var(--mono));
  --dx5-radius: var(--dx-radius-m, var(--radius));
  --dx5-radius-l: var(--dx-radius-l, var(--radius-lg));
  --dx5-shadow: var(--dx-shadow-2, 0 -10px 36px rgb(0 0 0 / 24%));
  --dx5-sp-1: var(--dx-sp-1, 4px);
  --dx5-sp-2: var(--dx-sp-2, 8px);
  --dx5-sp-3: var(--dx-sp-3, 12px);
  --dx5-sp-4: var(--dx-sp-4, 16px);

  --dx5-safe-t: env(safe-area-inset-top, 0px);
  --dx5-safe-r: env(safe-area-inset-right, 0px);
  --dx5-safe-b: env(safe-area-inset-bottom, 0px);
  --dx5-safe-l: env(safe-area-inset-left, 0px);
  --dx5-pair-h: 52px;
  --dx5-tabs-h: 44px;
  --dx5-act-h: 60px;
  --dx5-tap: 44px;
}

/* Desktop: none of the phone furniture exists. */
.dx5-pairbar,
.dx5-pair-acts,
.dx5-tabs,
.dx5-actionbar,
.dx5-backdrop,
.dx5-fs-exit,
.dx5-sheet-close,
.terminal > .dx5-balances {
  display: none;
}

/* =================================================================== phone === */

html.dx5-m.dx5-has-terminal .section-tabs {
  display: none;
}

html.dx5-m .dx5-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: var(--dx5-tap);
  min-height: var(--dx5-tap);
  padding: 0;
  border: 1px solid var(--dx5-border);
  border-radius: var(--dx5-radius);
  background: var(--dx5-surface);
  color: var(--dx5-text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

html.dx5-m :is(.dx5-pairbar, .dx5-tabs, .dx5-actionbar) button:focus-visible,
html.dx5-m .dx5-icon-btn:focus-visible {
  outline: 2px solid var(--dx5-focus);
  outline-offset: 2px;
}

/* ---- pair bar: the pair and its last price never leave the screen ---------- */

html.dx5-m .dx5-pairbar {
  display: flex;
  align-items: center;
  gap: var(--dx5-sp-2);
  position: sticky;
  top: 0;
  z-index: 22;
  height: var(--dx5-pair-h);
  padding-inline: max(var(--dx5-sp-2), var(--dx5-safe-l)) max(var(--dx5-sp-2), var(--dx5-safe-r));
  background: var(--dx5-surface);
  border-bottom: 1px solid var(--dx5-border);
  box-sizing: border-box;
  min-width: 0;
}

html.dx5-m .dx5-pair {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  min-height: var(--dx5-tap);
  padding: 0 var(--dx5-sp-2);
  border: 0;
  border-radius: var(--dx5-radius);
  background: transparent;
  color: var(--dx5-text);
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  flex: 0 1 auto;
}

html.dx5-m .dx5-pair-symbols {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

html.dx5-m .dx5-pair-symbols img {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
}

html.dx5-m .dx5-caret {
  color: var(--dx5-dim);
  font-size: 12px;
}

html.dx5-m .dx5-price {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  margin-inline-start: auto;
  min-width: 0;
  font-family: var(--dx5-mono);
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  text-align: end;
}

html.dx5-m .dx5-last {
  font-size: 17px;
  font-weight: 700;
  color: var(--dx5-text);
  white-space: nowrap;
}

html.dx5-m .dx5-change {
  font-size: 12px;
  color: var(--dx5-dim);
  white-space: nowrap;
}

html.dx5-m .dx5-price [data-tone="up"] {
  color: var(--dx5-buy);
}

html.dx5-m .dx5-price [data-tone="down"] {
  color: var(--dx5-sell);
}

/* ---- section tabs ------------------------------------------------------------ */

html.dx5-m .dx5-tabs {
  display: flex;
  position: sticky;
  top: var(--dx5-pair-h);
  z-index: 21;
  height: var(--dx5-tabs-h);
  padding-inline: var(--dx5-safe-l) var(--dx5-safe-r);
  background: var(--dx5-surface);
  border-bottom: 1px solid var(--dx5-border);
  box-sizing: border-box;
}

html.dx5-m .dx5-tabs button {
  flex: 1 1 0;
  min-width: 0;
  min-height: var(--dx5-tabs-h);
  padding: 0 2px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--dx5-dim);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

html.dx5-m .dx5-tabs button[aria-selected="true"] {
  color: var(--dx5-text);
  border-bottom-color: var(--dx5-accent);
}

@media (max-width: 359.98px) {
  html.dx5-m .dx5-tabs button {
    font-size: 12px;
  }
}

/* ---- one section at a time --------------------------------------------------- */

html.dx5-m .terminal {
  display: flex;
  flex-direction: column;
  gap: var(--dx5-sp-2);
  height: auto;
  /* At least one screen under the sticky bars, so a short section still has room to swipe on. */
  min-height: calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h));
  box-sizing: border-box;
  padding: var(--dx5-sp-2) max(var(--dx5-sp-2), var(--dx5-safe-l)) calc(var(--dx5-act-h) + var(--dx5-safe-b) + var(--dx5-sp-4)) max(var(--dx5-sp-2), var(--dx5-safe-r));
  overflow-anchor: none;
}

html.dx5-m .terminal > * {
  margin: 0;
}

html.dx5-m .terminal[data-dx5-tab] > :is(.market-bar, .chart-panel, .book-panel, .tape-panel, .tables-panel, .dx5-balances) {
  display: none;
}

html.dx5-m .terminal[data-dx5-tab="chart"] > .chart-panel,
html.dx5-m .terminal[data-dx5-tab="book"] > .book-panel,
html.dx5-m .terminal[data-dx5-tab="trades"] > .tape-panel,
html.dx5-m .terminal[data-dx5-tab="orders"] > .tables-panel,
html.dx5-m .terminal[data-dx5-tab="balances"] > .dx5-balances {
  display: flex;
}

/* The market's statistics follow the chart: the chart comes first, the pair bar carries the price. */
html.dx5-m .terminal[data-dx5-tab="chart"] > .market-bar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  overflow: visible;
  order: 2;
}

html.dx5-m .terminal > .market-bar > .pair {
  grid-column: 1 / -1;
  min-width: 0;
}

html.dx5-m .terminal > .market-bar > .stat {
  min-width: 0;
}

html.dx5-m .terminal > .market-bar [data-field="pair-symbols"] {
  display: none;
}

/* The chart uses the screen it is given, never a strip. */
html.dx5-m .terminal > .chart-panel .md-stage,
html.dx5-m .terminal .md-stage {
  flex: 0 0 auto;
  min-height: 0;
  height: clamp(240px, calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h) - var(--dx5-act-h) - var(--dx5-safe-b) - 150px), 640px);
}

/* Portrait phone: the plot height dex5-mobile.js measured so the whole chart panel ends above the Buy/Sell bar with
   the pair bar at the top (fitChart). Without the measurement the estimate above stands. */
@media (orientation: portrait) {
  html.dx5-m .terminal > .chart-panel .md-stage,
  html.dx5-m .terminal .md-stage {
    height: var(--dx5-stage-h, clamp(240px, calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h) - var(--dx5-act-h) - var(--dx5-safe-b) - 150px), 640px));
  }
}

/* The book keeps a definite height (its two sides share it and scroll inside it): with an auto
   height its flex-basis-0 sides collapse to nothing and the book shows no level at all. */
html.dx5-m .terminal > .book-panel > .panel-body {
  flex: 0 0 auto;
  height: max(300px, calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h) - var(--dx5-act-h) - var(--dx5-safe-b) - 84px));
  max-height: none;
}

html.dx5-m .terminal > .tape-panel .panel-body,
html.dx5-m .terminal > .tables-panel .panel-body {
  max-height: none;
  height: auto;
}

/* Touch-sized controls inside the terminal. The order-book rows stay dense: a tap there only
   copies a price into the ticket, and every price is also typed in the ticket. */
html.dx5-m .terminal :is(button, select, input:not([type="checkbox"]):not([type="radio"])):not(.book-panel .panel-body button) {
  min-height: 40px;
}

html.dx5-m .terminal :is(button, select):not(.book-panel .panel-body button) {
  min-width: 40px;
}

/* The chart attribution stays (the licence asks for it) and becomes a reachable target. The library's notice (name,
   then its copyright line) is set small and wraps inside one 40px target, so the plot keeps its height. */
html.dx5-m [data-field="chart-attribution"] {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: center;
  column-gap: 4px;
  min-height: 40px;
  max-width: 300px;
  font-size: 10px;
  line-height: 1.25;
}

/* On a phone the foot says how the chart is kept current only when that is news (stream reconnecting, market data
   failing); a normal stream or poll is the quiet default, and the time of the last read is in the note's title. */
html.dx5-m .md-foot > .md-updated,
html.dx5-m .md-foot > .md-feed:is([data-mode="stream"], [data-mode="poll"], [data-mode="loading"]) {
  display: none;
}

/* Sideways gestures on these sections belong to the section swipe, vertical ones to the page. */
html.dx5-m .terminal > :is(.book-panel, .tape-panel, .dx5-balances),
html.dx5-m .terminal > :is(.book-panel, .tape-panel, .dx5-balances) *,
html.dx5-m .terminal > .tables-panel > .panel-head {
  touch-action: pan-y;
}

html.dx5-m .terminal .book-panel .panel-body button {
  min-height: 30px;
}

/* Every finger target in the app, every area (Spot, Swap, Liquidity, Markets, Cross-chain, the header). */
html.dx5-m #app :is(button, select, input:not([type="checkbox"]):not([type="radio"])):not(.book-panel .panel-body button),
html.dx5-m dialog :is(button, select, input:not([type="checkbox"]):not([type="radio"])) {
  min-height: 40px;
}

html.dx5-m #app :is(button, select):not(.book-panel .panel-body button),
html.dx5-m dialog :is(button, select) {
  min-width: 40px;
}

/* ---- the shell header on a phone (layout only; its content is the shell's) ---------- */

/* The network name is read whole: the trigger may use the row beside the brand. */
html.dx5-m .net-trigger {
  max-width: calc(100vw - 100px);
}

html.dx5-m .net-trigger-name {
  overflow: visible;
  text-overflow: clip;
  white-space: nowrap;
}

/* The wallet chip wraps instead of cutting the network name. */
html.dx5-m .wallet-chip {
  flex-wrap: wrap;
  max-width: 100%;
  row-gap: 2px;
}

html.dx5-m .wallet-chip .wallet-net {
  overflow: visible;
  text-overflow: clip;
}

/* The six areas as a grid, never a sideways strip. */
html.dx5-m .nav {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  overflow: visible;
}

html.dx5-m .nav button {
  justify-content: center;
  min-width: 0;
  white-space: nowrap;
}

/* A panel head wraps its state chip under the title rather than pushing it off the screen. */
html.dx5-m #app .panel-head {
  flex-wrap: wrap;
  row-gap: 4px;
}

html.dx5-m #app .panel-head > .panel-tools {
  flex-wrap: wrap;
  max-width: 100%;
}

html.dx5-m #app .panel-title {
  white-space: normal;
}

/* A table wider than the phone becomes one labelled record per row (dex5-mobile.js copies
   each column heading into its cells). Every cell survives; none is hidden. */
html.dx5-m table.grid[data-dx5-stack] thead {
  display: none;
}

html.dx5-m table.grid[data-dx5-stack],
html.dx5-m table.grid[data-dx5-stack] tbody,
html.dx5-m table.grid[data-dx5-stack] tr,
html.dx5-m table.grid[data-dx5-stack] td {
  display: block;
  width: 100%;
  box-sizing: border-box;
}

html.dx5-m table.grid[data-dx5-stack] tr {
  padding: var(--dx5-sp-2) var(--dx5-sp-3);
  border-bottom: 1px solid var(--dx5-border);
}

html.dx5-m table.grid[data-dx5-stack] td {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--dx5-sp-3);
  padding: 3px 0;
  border-bottom: 0;
  text-align: end;
  white-space: normal;
  overflow-wrap: anywhere;
}

html.dx5-m table.grid[data-dx5-stack] td::before {
  content: attr(data-label);
  flex: none;
  max-width: 45%;
  text-align: start;
  font-size: var(--t-micro, 10px);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--dx5-dim);
}

html.dx5-m table.grid[data-dx5-stack] td:not([data-label])::before,
html.dx5-m table.grid[data-dx5-stack] td[data-label=""]::before {
  content: none;
}

/* ---- balances ----------------------------------------------------------------- */

html.dx5-m .dx5-balances-body {
  display: grid;
  gap: var(--dx5-sp-3);
  padding: var(--dx5-sp-3);
}

html.dx5-m .dx5-note {
  margin: 0;
}

html.dx5-m .dx5-wide {
  width: 100%;
  min-height: var(--dx5-tap);
}

html.dx5-m .dx5-balance-list {
  display: grid;
  gap: var(--dx5-sp-3);
  margin: 0;
}

html.dx5-m .dx5-balance dt {
  font-weight: 700;
  margin-bottom: var(--dx5-sp-1);
}

html.dx5-m .dx5-balance dd {
  margin: 0;
  display: grid;
  gap: 2px;
}

html.dx5-m .dx5-balance-row {
  display: flex;
  justify-content: space-between;
  gap: var(--dx5-sp-3);
  color: var(--dx5-dim);
}

html.dx5-m .dx5-num {
  color: var(--dx5-text);
  font-family: var(--dx5-mono);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  text-align: end;
}

/* ---- the Buy / Sell bar --------------------------------------------------------- */

html.dx5-m .dx5-actionbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--dx5-sp-2);
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 30;
  padding: var(--dx5-sp-2) max(var(--dx5-sp-3), var(--dx5-safe-r)) calc(var(--dx5-sp-2) + var(--dx5-safe-b)) max(var(--dx5-sp-3), var(--dx5-safe-l));
  background: var(--dx5-surface);
  border-top: 1px solid var(--dx5-border);
  box-sizing: border-box;
}

html.dx5-m .dx5-act {
  min-height: var(--dx5-tap);
  border: 0;
  border-radius: var(--dx5-radius);
  color: #fff;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

html.dx5-m .dx5-act-buy {
  background: var(--dx5-buy);
}

html.dx5-m .dx5-act-sell {
  background: var(--dx5-sell);
}

html.dx5-m:is(.dx5-sheet-open, .dx5-fs, .dx5-kb-open) .dx5-actionbar {
  display: none;
}

/* ---- the ticket as a bottom sheet ------------------------------------------------ */

html.dx5-m .terminal > .ticket-panel {
  display: flex;
  flex-direction: column;
  position: fixed;
  inset-inline: 0;
  bottom: var(--dx5-kb, 0px);
  z-index: 41;
  width: 100%;
  max-width: 560px;
  margin-inline: auto;
  max-height: calc(var(--dx5-vvh, 100dvh) - var(--dx5-safe-t) - 16px);
  border-radius: var(--dx5-radius-l) var(--dx5-radius-l) 0 0;
  border-bottom: 0;
  box-shadow: var(--dx5-shadow);
  box-sizing: border-box;
  transform: translateY(calc(100% + 24px));
  visibility: hidden;
  transition: transform 0.22s ease, visibility 0s linear 0.22s;
}

html.dx5-m .terminal > .ticket-panel.dx5-sheet {
  transform: none;
  visibility: visible;
  transition: transform 0.22s ease, visibility 0s;
}

html.dx5-m .terminal > .ticket-panel > .panel-head {
  position: relative;
  min-height: 52px;
  padding-top: 14px;
}

/* The grab handle is decoration; the close button and Escape are the controls. */
html.dx5-m .terminal > .ticket-panel > .panel-head::before {
  content: "";
  position: absolute;
  top: 5px;
  left: 50%;
  width: 36px;
  height: 4px;
  margin-left: -18px;
  border-radius: 2px;
  background: var(--dx5-border);
}

html.dx5-m .terminal > .ticket-panel .dx5-sheet-close {
  display: inline-flex;
}

html.dx5-m .terminal > .ticket-panel > .panel-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--dx5-sp-3) + var(--dx5-safe-b));
  scroll-padding-block: 24px;
}

html.dx5-m .dx5-backdrop {
  display: block;
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgb(8 12 15 / 48%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

html.dx5-m.dx5-sheet-open .dx5-backdrop {
  opacity: 1;
  pointer-events: auto;
}

html.dx5-m.dx5-lock,
html.dx5-m.dx5-lock body {
  overflow: hidden;
  overscroll-behavior: none;
}

/* ---- full-screen chart (analysis mode) ---------------------------------------- */

html.dx5-m.dx5-fs .terminal[data-dx5-tab] > .chart-panel.dx5-fs-chart {
  display: flex;
  position: fixed;
  inset: 0;
  z-index: 50;
  margin: 0;
  border: 0;
  border-radius: 0;
  padding: var(--dx5-safe-t) var(--dx5-safe-r) var(--dx5-safe-b) var(--dx5-safe-l);
  box-sizing: border-box;
  background: var(--dx5-surface);
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart > .panel-head {
  padding-inline-end: calc(var(--dx5-tap) + 12px);
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart > .panel-body {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-stage {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
}

html.dx5-m.dx5-fs .dx5-fs-exit {
  display: inline-flex;
  position: fixed;
  top: calc(var(--dx5-safe-t) + 4px);
  inset-inline-end: calc(var(--dx5-safe-r) + 6px);
  z-index: 51;
}

/* ---- the mandatory review on a phone: a full sheet, never trimmed ------------------ */

html.dx5-m dialog.sheet[open] {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  max-height: calc(100dvh - var(--dx5-safe-t) - 8px);
  border-radius: var(--dx5-radius-l) var(--dx5-radius-l) 0 0;
  border-bottom: 0;
  box-sizing: border-box;
}

html.dx5-m dialog.sheet[open] > .sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

html.dx5-m dialog.sheet[open] > .sheet-foot {
  flex: 0 0 auto;
  padding-bottom: calc(var(--s4, 10px) + var(--dx5-safe-b));
}

html.dx5-m dialog.sheet[open] > .sheet-foot > button {
  flex: 1 1 0;
  min-height: var(--dx5-tap);
}

html.dx5-m dialog.sheet .rows dd {
  overflow-wrap: anywhere;
}

/* ---- narrow landscape: the height is the scarce side ---------------------------- */

@media (max-height: 500px) and (orientation: landscape) {
  html.dx5-m {
    --dx5-pair-h: 46px;
    --dx5-tabs-h: 40px;
    --dx5-act-h: 52px;
  }
  html.dx5-m .terminal > .chart-panel .md-stage,
  html.dx5-m .terminal .md-stage {
    height: max(180px, calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h) - 108px));
  }
  html.dx5-m .nav {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  html.dx5-m .terminal > .book-panel > .panel-body {
    height: max(220px, calc(100svh - var(--dx5-pair-h) - var(--dx5-tabs-h) - 72px));
  }
  html.dx5-m .dx5-actionbar {
    display: none;
  }
  html.dx5-m .dx5-pair-acts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--dx5-sp-2);
    flex: 0 0 auto;
    width: min(240px, 36vw);
  }
  html.dx5-m:is(.dx5-sheet-open, .dx5-fs) .dx5-pair-acts {
    visibility: hidden;
  }
  html.dx5-m .dx5-pair-acts .dx5-act {
    min-height: 40px;
    font-size: 14px;
  }
  html.dx5-m .terminal {
    padding-bottom: calc(var(--dx5-sp-4) + var(--dx5-safe-b));
  }
  html.dx5-m .terminal > .ticket-panel {
    max-height: calc(var(--dx5-vvh, 100dvh) - var(--dx5-safe-t) - 8px);
  }
}

/* ================================================================== tablet === */

/* The chart keeps its height: the book sits beside it, the ticket under the book, the tables
   under the chart. (Between 821 and 1179 px the earlier sheets combined a three-column area
   map with a two-column track list, and the implicit third column crushed the chart.) */
html.dx5-t .terminal {
  display: grid;
  grid-template-areas:
    "bar    bar"
    "chart  book"
    "tables ticket"
    "tape   ticket";
  grid-template-columns: minmax(0, 1fr) minmax(280px, 320px);
  grid-template-rows: auto clamp(420px, 60vh, 680px) minmax(220px, auto) minmax(220px, 32vh);
  height: auto;
  min-height: 0;
  padding: var(--dx5-sp-2) max(var(--dx5-sp-2), var(--dx5-safe-r)) calc(var(--dx5-sp-3) + var(--dx5-safe-b)) max(var(--dx5-sp-2), var(--dx5-safe-l));
}

/* Portrait tablet: a little less chart height, so the ticket starts on the first screen's edge. */
@media (orientation: portrait) {
  html.dx5-t .terminal {
    grid-template-rows: auto clamp(400px, 50vh, 600px) minmax(220px, auto) minmax(220px, 30vh);
  }
}

/* The statistics wrap to as many columns as fit: two rows on a tablet, not three. */
html.dx5-t .terminal > .market-bar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  overflow: visible;
}

html.dx5-t .terminal > .market-bar > .pair {
  grid-column: 1 / -1;
  min-width: 0;
}

html.dx5-t .terminal > .market-bar > .stat {
  min-width: 0;
}

html.dx5-t.dx5-has-terminal .section-tabs {
  display: none;
}

html.dx5-t .terminal[data-section] > :is(.chart-panel, .book-panel, .tape-panel, .ticket-panel, .tables-panel) {
  display: flex;
  margin: 0;
}

html.dx5-t .terminal > .market-bar {
  margin: 0;
}

html.dx5-t .terminal > .chart-panel .md-stage,
html.dx5-t .terminal .md-stage {
  flex: 1 1 auto;
  height: auto;
  min-height: 160px;
}

html.dx5-t .terminal > .chart-panel > .panel-body,
html.dx5-t .terminal > .book-panel > .panel-body {
  height: auto;
  min-height: 0;
}

html.dx5-t .terminal > .book-panel > .panel-body,
html.dx5-t .terminal > .tape-panel > .panel-body {
  overflow: auto;
}

html.dx5-t .terminal > .ticket-panel {
  align-self: start;
}

@media (prefers-reduced-motion: reduce) {
  html.dx5-m .terminal > .ticket-panel,
  html.dx5-m .dx5-backdrop {
    transition: none;
  }
}

/* =================================================== integration (DEX V5) === */

/* Desktop and tablet: the phone Balances section is never shown. Track A's grid gives every `.terminal > .panel`
   display:flex at a higher specificity than the hiding rule above, so this one outranks it. */
html:not(.dx5-m) .terminal > .dx5-balances.panel {
  display: none;
}

/* Seven areas now (Local Spot, Cross-chain Spot, Cross-chain status): the phone nav stays a grid, a long label
   (Arabic "Cross-chain Spot") takes two lines instead of being cut, and the kind separators take no cell. */
html.dx5-m .nav button {
  white-space: normal;
  line-height: 1.15;
  text-align: center;
  overflow-wrap: anywhere;
}

html.dx5-m .nav .dx5-nav-sep {
  display: none;
}

@media (max-height: 500px) and (orientation: landscape) {
  html.dx5-m .nav {
    grid-template-columns: repeat(7, minmax(0, 1fr));
  }
}

/* Track C's chart toolbar: on a tablet or desktop it wraps to a second row rather than scrolling sideways (Track H
   H-6bc430). On a phone it stays ONE row that scrolls inside the chart, like the chart itself: wrapped it took four
   rows and pushed the plot off the first screen, under the Buy/Sell bar. */
html:not(.dx5-m) .c5-toolbar {
  flex-wrap: wrap;
  overflow-x: visible;
  row-gap: 4px;
}

/* Seven areas: four columns on a portrait phone (two rows, not three), so the header costs one row less. */
@media (orientation: portrait) {
  html.dx5-m .nav {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* Full screen: the OHLC legend sits over the top of the plot (it was a 38px row above it), so the plot keeps the
   height on a short landscape screen. It never takes a pointer event from the chart. */
html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-body > .c5-toolbar {
  grid-row: 1;
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-body > .md-stage {
  grid-row: 2;
  grid-column: 1;
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-body > .md-legend {
  grid-row: 2;
  grid-column: 1;
  align-self: start;
  z-index: 3;
  pointer-events: none;
  background: color-mix(in srgb, var(--dx5-surface) 70%, transparent);
}

html.dx5-m.dx5-fs .chart-panel.dx5-fs-chart .md-body > .md-foot {
  grid-row: 3;
}

/* The full-screen exit exists only in full screen. `html.dx5-m .dx5-icon-btn` (display:inline-flex) outranked the
   hiding rule, so a stray ✕ sat at the foot of every phone page (found by the integrated Track H run). */
html.dx5-m:not(.dx5-fs) .dx5-fs-exit {
  display: none;
}

/* ===================================================== DEX V5 fix: phone chart chrome === */

/* Portrait phone: the chart panel is fitted between the tabs and the Buy/Sell bar (fitChart), so its chrome is kept
   lean. The "Chart" title repeats the selected tab and stays as the panel's accessible name only; the OHLC legend sits
   over the top of the plot (as in full screen) instead of taking its own row. It never takes a pointer event. */
@media (orientation: portrait) {
  html.dx5-m .terminal > .chart-panel > .panel-head > .panel-title {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  html.dx5-m .terminal > .chart-panel .md-body {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
  }

  html.dx5-m .terminal > .chart-panel .md-body > .md-stage {
    grid-row: 3;
    grid-column: 1;
  }

  html.dx5-m .terminal > .chart-panel .md-body > .md-legend {
    grid-row: 3;
    grid-column: 1;
    align-self: start;
    z-index: 3;
    pointer-events: none;
    background: color-mix(in srgb, var(--dx5-surface) 70%, transparent);
  }

  html.dx5-m .terminal > .chart-panel .md-body > .c5-toolbar {
    grid-row: 1;
    padding-block: 2px;
  }

  html.dx5-m .terminal > .chart-panel > .md-head {
    padding-block: 2px;
    row-gap: 2px;
  }

  html.dx5-m .terminal > .chart-panel .md-body > .md-health {
    grid-row: 2;
  }

  html.dx5-m .terminal > .chart-panel .md-body > .md-foot {
    grid-row: 4;
    padding: 4px var(--dx5-sp-2);
    row-gap: 0;
    line-height: 1.35;
  }
}
