/*
 * MIRCO DEX V5 — the desktop trading terminal (Track A). Linked LAST, so it is the one desktop layout.
 *
 * ```text
 * TOP     the market bar: market switch, pair + chain + token identity, last price, 24h change, verified volume
 * MAIN    chart (large) | order book over recent trades | the Buy/Sell ticket, full height
 * BOTTOM  open orders · history · trades · balances · transaction status, under the chart and the book
 * ```
 *
 * WHY THIS FILE EXISTS. Three sheets each declared `.terminal`'s grid (spot.css, chart-v1.css, shell-v1.css), and
 * the last unconditional one won over the earlier media queries: at 1024-1180px the template had two columns and the
 * areas three, so the chart collapsed into a 70px strip and the ticket took the rest (ISSUES.md A-001). One owner,
 * one grid, keyed on `[data-dx-layout="v5"]` (set by dex5-desktop.js) so it outranks all three without !important.
 *
 * Below 1024px nothing here applies: the mobile sections (spot.css) are untouched.
 */

@media (min-width: 1024px) {
  .terminal[data-dx-layout="v5"] {
    --dx-gap: 4px;
    display: grid;
    gap: var(--dx-gap);
    padding: var(--dx-gap);
    grid-template-areas:
      "bar    bar    bar"
      "chart  book   ticket"
      "chart  tape   ticket"
      "tables tables ticket";
    grid-template-columns: minmax(360px, 1fr) var(--dx-book-w) var(--dx-ticket-w);
    grid-template-rows: auto minmax(0, 1.2fr) minmax(0, 1fr) var(--dx-bottom-h);
    /* The terminal fills the viewport under the header (dex5-desktop.js measures where it starts). */
    block-size: max(640px, calc(100vh - var(--dx-term-top, 48px)));
    min-block-size: 0;
    max-inline-size: none;
    inline-size: 100%;
    margin: 0;
    position: relative;
  }

  /* Each panel scrolls inside itself; the page never grows because a tape got long. */
  .terminal[data-dx-layout="v5"] > .panel {
    min-inline-size: 0;
    min-block-size: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .terminal[data-dx-layout="v5"] > .panel > .panel-body {
    flex: 1 1 auto;
    min-block-size: 0;
    overflow: auto;
    overscroll-behavior: contain;
  }

  .terminal[data-dx-layout="v5"] > .chart-panel > .panel-body,
  .terminal[data-dx-layout="v5"] > .md-panel > .panel-body {
    overflow: hidden;
  }

  .terminal[data-dx-layout="v5"] > .market-bar {
    grid-area: bar;
    min-inline-size: 0;
    /* Never clipped: a narrow window scrolls the figures sideways inside the bar (ISSUES.md A-002). */
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: thin;
    padding-inline-start: 40px;
  }

  .terminal[data-dx-layout="v5"] > .chart-panel {
    grid-area: chart;
  }

  .terminal[data-dx-layout="v5"] > .book-panel {
    grid-area: book;
  }

  .terminal[data-dx-layout="v5"] > .tape-panel {
    grid-area: tape;
  }

  .terminal[data-dx-layout="v5"] > .ticket-panel {
    grid-area: ticket;
  }

  .terminal[data-dx-layout="v5"] > .tables-panel {
    grid-area: tables;
  }

  /* The ticket's action stays on screen: the ticket scrolls inside its own column. */
  .terminal[data-dx-layout="v5"] > .ticket-panel > .panel-body {
    overflow-y: auto;
  }

  /* A tape column that did not fit was cut off at the panel's edge (A-010): its table scrolls sideways instead. */
  .terminal[data-dx-layout="v5"] > .tape-panel .panel-body {
    overflow-x: auto;
  }

  .terminal[data-dx-layout="v5"][data-collapsed-book="true"] {
    grid-template-columns: minmax(360px, 1fr) 36px var(--dx-ticket-w);
  }

  /* ---- the split handles (dex5-desktop.js; the look is .dx-resizer in dex5-components.css) ---- */

  .terminal[data-dx-layout="v5"] > .dx5-resizer-bookW {
    grid-row: 2 / 4;
    grid-column: 1;
    justify-self: end;
    align-self: stretch;
    margin-inline-end: calc(-0.5 * var(--dx-gap) - 3px);
  }

  .terminal[data-dx-layout="v5"] > .dx5-resizer-bottomH {
    grid-row: 4;
    grid-column: 1 / 3;
    align-self: start;
    justify-self: stretch;
    margin-block-start: calc(-0.5 * var(--dx-gap) - 3px);
  }

  .terminal[data-dx-layout="v5"][data-collapsed-book="true"] > .dx5-resizer-bookW {
    display: none;
  }

  /* While a handle is dragged nothing else may take the pointer or select text. */
  .terminal[data-dx-resizing="true"] {
    user-select: none;
  }

  .terminal[data-dx-resizing="true"] > .panel {
    pointer-events: none;
  }

  /* ---- the market switch, at the inline start of the market bar ---- */

  .terminal[data-dx-layout="v5"] > .dx5-market-switch {
    grid-area: bar;
    justify-self: start;
    align-self: center;
    z-index: 2;
    margin-inline-start: 6px;
    inline-size: 28px;
    min-block-size: 28px;
    padding: 0;
    /* Opaque, so figures scrolled sideways in the bar pass under it rather than through it. */
    background: var(--dx-surface);
    border: 1px solid var(--dx-border);
    color: var(--dx-text);
  }

  .terminal[data-dx-layout="v5"] > .dx5-market-switch:hover {
    background: var(--dx-surface-3);
  }

  /* A down chevron is not directional: physical borders, so it points down in RTL too. */
  .dx5-caret {
    display: block;
    inline-size: 7px;
    block-size: 7px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: translateY(-2px) rotate(45deg);
  }
}

/* Column widths by viewport: the chart keeps the most room at every width. */
@media (min-width: 1024px) {
  :root {
    --dx-book-w: 240px;
    --dx-ticket-w: 280px;
    --dx-bottom-h: clamp(170px, 26vh, 300px);
  }
}

@media (min-width: 1280px) {
  :root {
    --dx-book-w: 272px;
    --dx-ticket-w: 300px;
  }
}

@media (min-width: 1440px) {
  :root {
    --dx-book-w: 296px;
    --dx-ticket-w: 320px;
    --dx-bottom-h: clamp(190px, 28vh, 320px);
  }
}

@media (min-width: 1880px) {
  :root {
    --dx-book-w: 328px;
    --dx-ticket-w: 340px;
  }
}

/* Under the desktop width the handles and the extra switch do not exist for the viewer. */
@media (max-width: 1023.98px) {
  .terminal > .dx-resizer,
  .terminal > .dx5-market-switch {
    display: none;
  }
}

/* ---- the header at desktop widths (ISSUES.md A-003: it wrapped to two rows at 1024-1279) ---- */

@media (min-width: 1024px) {
  .topbar {
    flex-wrap: nowrap;
    min-block-size: var(--dx-topbar-h);
  }

  .topbar .nav {
    flex-wrap: nowrap;
    min-inline-size: 0;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .topbar .nav::-webkit-scrollbar {
    display: none;
  }

  /* Wrap rather than overlap: a connected wallet's chip that does not fit moves to its own line (H-b4afa3). */
  .topbar-right {
    flex-wrap: wrap;
    justify-content: flex-end;
    min-inline-size: 0;
  }

  /* The locale and theme toggles were 19-20px wide: a hit target, not a speck (A-014). */
  .topbar [data-field="locale"],
  .topbar [data-field="theme"] {
    min-inline-size: 28px;
    min-block-size: 28px;
    font-size: var(--dx-fs-sm);
  }

  /* The Spot group: Local Spot and Cross-chain Spot read as one pair of modes. */
  .nav button[data-group="spot"] + button[data-group="spot"] {
    margin-inline-start: -2px;
  }

  .nav .dx5-nav-sep {
    inline-size: 1px;
    align-self: center;
    block-size: 18px;
    background: var(--dx-border);
    margin-inline: var(--dx-sp-1);
    flex: none;
  }
}

/* 1024-1399: two deliberate rows rather than a squeezed one. Identity, network and state chips on the first, the
   seven sections on the second at full width, so no section is scrolled out of sight and no chip is crushed. */
@media (min-width: 1024px) and (max-width: 1399.98px) {
  .topbar {
    flex-wrap: wrap;
    row-gap: 0;
  }

  .topbar .nav {
    order: 10;
    flex: 1 0 100%;
    border-block-start: 1px solid var(--dx-border-soft);
    padding-block: 2px;
  }

  .topbar .nav button {
    min-block-size: 30px;
  }

  .topbar-right {
    margin-inline-start: auto;
  }
}

/* A connected wallet's chip (name, address, network, disconnect) is ~220px wider than the connect button, so the
   one-row header needs more room with it: the same two rows up to 1700px. */
@media (min-width: 1400px) and (max-width: 1699.98px) {
  .topbar:has([data-field="wallet-account"]) {
    flex-wrap: wrap;
    row-gap: 0;
  }

  .topbar:has([data-field="wallet-account"]) .nav {
    order: 10;
    flex: 1 0 100%;
    border-block-start: 1px solid var(--dx-border-soft);
    padding-block: 2px;
  }
}

.topbar .net-kind,
.topbar .state-chip,
.topbar [data-field="wallet-connect"],
.topbar [data-field="open-selector"] {
  white-space: nowrap;
  flex: none;
}

@media (min-width: 1024px) and (max-width: 1365.98px) {
  /* Tighter, never hidden: a state chip that disappeared at a narrow width could hide a fault. */
  .topbar {
    gap: var(--dx-sp-2);
    padding-inline: var(--dx-sp-3);
  }

  .topbar-right {
    gap: var(--dx-sp-1);
  }

  .topbar .nav button {
    padding-inline: var(--dx-sp-2);
  }
}

/* ---- 768-1023 (ISSUES.md A-027 / Track H V-11): the header wrapped to three rows (identity, chips, sections) ----
   Identity and chips share the first row, the sections take the second. The market selector button shows its
   pair marks only (its text stays its accessible name), which is what lets the chips fit beside the identity. */
@media (min-width: 768px) and (max-width: 1023.98px) {
  .topbar {
    flex-wrap: wrap;
    row-gap: 0;
    gap: var(--dx-sp-2);
    padding-inline: var(--dx-sp-3);
  }

  .topbar .nav {
    order: 10;
    flex: 1 0 100%;
    inline-size: auto;
    border-block-start: 1px solid var(--dx-border-soft);
  }

  .topbar-right {
    order: 2;
    inline-size: auto;
    flex: 1 1 auto;
    flex-wrap: wrap;
    justify-content: flex-end;
    margin-inline-start: auto;
    gap: var(--dx-sp-1);
  }

  /* The selector button keeps its text as its accessible name; only the pair marks (if any) and a caret are drawn. */
  .topbar [data-field="open-selector"] {
    font-size: 0;
    min-inline-size: 36px;
    padding-inline: var(--dx-sp-2);
  }

  .topbar [data-field="open-selector"]::after {
    content: "";
    display: inline-block;
    inline-size: 6px;
    block-size: 6px;
    margin-inline-start: 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: translateY(-2px) rotate(45deg);
  }

  .topbar [data-field="network-current-badge"] {
    display: none;
  }

  .topbar [data-field="locale"],
  .topbar [data-field="theme"],
  .topbar [data-field="open-selector"],
  .topbar [data-field="wallet-connect"],
  .topbar .nav button {
    min-inline-size: 36px;
    min-block-size: 36px;
  }
}

/* ---- the market bar fits its row at every desktop width (Track H H-eaa79e): figures share the width, and wrap
   to a second line rather than scroll sideways or clip ---- */
@media (min-width: 1024px) {
  .terminal[data-dx-layout="v5"] > .market-bar {
    flex-wrap: wrap;
    overflow: visible;
  }

  .terminal[data-dx-layout="v5"] > .market-bar .stat {
    flex: 1 1 auto;
    min-inline-size: 80px;
    padding: var(--dx-sp-2) var(--dx-sp-3);
  }

  .terminal[data-dx-layout="v5"] > .market-bar .pair {
    min-inline-size: 0;
    padding: var(--dx-sp-2) var(--dx-sp-3);
  }

  /* Recent trades fits its column (Track H V-07, ISSUES.md A-009): compact cells instead of a sideways scroll. */
  .terminal[data-dx-layout="v5"] > .tape-panel table.grid th,
  .terminal[data-dx-layout="v5"] > .tape-panel table.grid td {
    padding-inline: 5px;
  }

  .terminal[data-dx-layout="v5"] > .tape-panel table.grid {
    table-layout: auto;
  }
}

/* ---- tablet, 821-1023 (ISSUES.md A-028): the same three-sheet grid conflict left the chart 2-80px wide here too.
   Below 821 the mobile sections (spot.css) take over and nothing here applies. The page scrolls on a tablet;
   each panel keeps its own height. ---- */
@media (min-width: 821px) and (max-width: 1023.98px) {
  .terminal[data-dx-layout="v5"] {
    --dx-gap: 4px;
    display: grid;
    gap: var(--dx-gap);
    padding: var(--dx-gap);
    grid-template-areas:
      "bar    bar    bar"
      "chart  chart  ticket"
      "book   tape   ticket"
      "tables tables tables";
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 280px;
    grid-template-rows: auto 430px 300px minmax(220px, auto);
    block-size: auto;
    min-block-size: 0;
    max-inline-size: none;
    margin: 0;
  }

  .terminal[data-dx-layout="v5"] > .panel {
    min-inline-size: 0;
    min-block-size: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .terminal[data-dx-layout="v5"] > .panel > .panel-body {
    flex: 1 1 auto;
    min-block-size: 0;
    overflow: auto;
  }

  .terminal[data-dx-layout="v5"] > .market-bar {
    grid-area: bar;
    flex-wrap: wrap;
    overflow: visible;
    min-inline-size: 0;
  }

  .terminal[data-dx-layout="v5"] > .market-bar .stat {
    flex: 1 1 auto;
    min-inline-size: 80px;
    padding: var(--dx-sp-2) var(--dx-sp-3);
  }

  .terminal[data-dx-layout="v5"] > .market-bar .pair {
    min-inline-size: 0;
    padding: var(--dx-sp-2) var(--dx-sp-3);
  }

  .terminal[data-dx-layout="v5"] > .chart-panel {
    grid-area: chart;
  }

  .terminal[data-dx-layout="v5"] > .book-panel {
    grid-area: book;
  }

  .terminal[data-dx-layout="v5"] > .tape-panel {
    grid-area: tape;
  }

  .terminal[data-dx-layout="v5"] > .ticket-panel {
    grid-area: ticket;
  }

  .terminal[data-dx-layout="v5"] > .tables-panel {
    grid-area: tables;
  }

  .terminal[data-dx-layout="v5"] > .tape-panel table.grid th,
  .terminal[data-dx-layout="v5"] > .tape-panel table.grid td {
    padding-inline: 4px;
  }
}

/* The book column at 1024-1279 is wide enough for the five tape columns. */
@media (min-width: 1024px) and (max-width: 1279.98px) {
  :root {
    --dx-book-w: 264px;
    --dx-ticket-w: 272px;
  }

  .terminal[data-dx-layout="v5"] > .tape-panel table.grid th,
  .terminal[data-dx-layout="v5"] > .tape-panel table.grid td {
    padding-inline: 3px;
  }
}

/* ---- the chart fits its grid cell (Track H H-fcc6ad..H-b9250d at 1024x768): chart-v1.css gives the stage a
   300px floor inside the terminal, so on a short viewport the plot ran under the bottom tabs. In the V5 grid the
   stage takes what the cell leaves, with a 160px floor, and a short viewport scrolls the page instead
   (the terminal never goes below 720px). ---- */
@media (min-width: 1024px) {
  .terminal[data-dx-layout="v5"] {
    block-size: max(720px, calc(100vh - var(--dx-term-top, 48px)));
  }

  .terminal[data-dx-layout="v5"] .md-stage {
    min-block-size: 160px;
    flex: 1 1 auto;
    block-size: auto;
  }

  .terminal[data-dx-layout="v5"] > .chart-panel .md-foot {
    flex: none;
  }
}

@media (min-width: 821px) and (max-width: 1023.98px) {
  .terminal[data-dx-layout="v5"] .md-stage {
    min-block-size: 160px;
    flex: 1 1 auto;
    block-size: auto;
  }
}

/* The wallet's network name is an identifier: it is shown whole, never cut to 58px (Track H H-1e40a5). */
.topbar .wallet-chip .wallet-net {
  overflow: visible;
  text-overflow: clip;
  max-inline-size: none;
}

/* 1400-1699 on one row (no wallet chip): the seven sections take their own width, so none is scrolled under the
   nav's edge (Track H H-6bd522: 5px at 1440 with the connect button). */
@media (min-width: 1400px) and (max-width: 1699.98px) {
  .topbar:not(:has([data-field="wallet-account"])) .nav button {
    padding-inline: 5px;
  }
}

/* The two deliberate rows again whenever the sections do not fit one row (fitHeader in dex5-desktop.js). */
@media (min-width: 1400px) {
  .topbar.dx-topbar-wrap {
    flex-wrap: wrap;
    row-gap: 0;
  }

  .topbar.dx-topbar-wrap .nav {
    order: 10;
    flex: 1 0 100%;
    border-block-start: 1px solid var(--dx-border-soft);
    padding-block: 2px;
  }
}

/* From 1400px without a wallet chip the one-row header fits (measured); there the group never wraps. */
@media (min-width: 1400px) {
  .topbar:not(:has([data-field="wallet-account"])) .topbar-right {
    flex-wrap: nowrap;
  }
}

/* Integration: with Track C's toolbar wrapping to a second row (no sideways strip) and a degraded state's note, the
   160px stage floor pushed the chart's foot, which carries the required "Chart: TradingView Lightweight Charts"
   attribution, out of its 1024x768 cell (Track H H-2d599b). The plot gives way first: a 120px floor keeps the foot. */
@media (min-width: 1024px) {
  .terminal[data-dx-layout="v5"] .md-stage {
    min-block-size: 120px;
  }
}
