/*
 * The Spot terminal layout, and how it survives being made small.
 *
 * Desktop is a real terminal: market statistics across the top, chart in the
 * centre, book on the side, ticket beside it, order tables along the bottom. It is
 * a grid rather than a stack of cards, because a trader watches four things at
 * once and a card wastes the space that would have shown the fourth.
 *
 * Mobile is NOT this layout scaled down. A dense grid at 390px is a grid nobody
 * can read, so the panels become sections you switch between and the action
 * region stays where a thumb is.
 */

/* ------------------------------------------------------------ market stats --- */

.market-bar {
  display: flex;
  align-items: stretch;
  gap: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow-x: auto;
  flex-wrap: nowrap;
}

.market-bar .pair {
  display: flex;
  flex-direction: column;
  gap: var(--s1);
  padding: var(--s4) var(--s5);
  border-inline-end: 1px solid var(--line-soft);
  min-width: 190px;
  flex: 0 0 auto;
}

.market-bar .pair-symbols {
  font-size: var(--t-head);
  font-weight: 700;
  letter-spacing: 0.01em;
  display: flex;
  align-items: center;
  gap: var(--s3);
}

.market-bar .pair-identity {
  font-size: var(--t-micro);
  color: var(--ink-3);
  font-family: var(--mono);
}

.stat {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: var(--s1);
  padding: var(--s4) var(--s5);
  min-width: 96px;
  flex: 0 0 auto;
  border-inline-end: 1px solid var(--line-soft);
}

.stat:last-child {
  border-inline-end: 0;
}

.stat .k {
  font-size: var(--t-micro);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-3);
  white-space: nowrap;
}

.stat .v {
  font-size: var(--t-mid);
  font-weight: 650;
  font-family: var(--mono);
  color: var(--ink);
  direction: ltr;
  unicode-bidi: isolate;
}

.stat .v[data-absent="true"] {
  color: var(--ink-3);
  font-weight: 500;
}

.stat.headline .v {
  font-size: var(--t-big);
  line-height: 1.1;
}

/*
 * The mobile section switcher, hidden until the layout becomes sections.
 *
 * DECLARED BEFORE the media query that shows it. It used to sit at the end of this
 * file, and at equal specificity the later rule won -- so `display: flex` inside the
 * phone media query was overridden by this `display: none` and the tabs were never
 * visible at any width. A capture run found it by failing to click one.
 */
.section-tabs {
  display: none;
}

/* ---------------------------------------------------------------- terminal --- */

.terminal {
  display: grid;
  gap: var(--s3);
  padding: var(--s3);
  /* The bottom tables get their own row so they can scroll without the page.
     Recent trades takes the lower half of the BOOK column: the tape belongs with
     the market, beside the book it is the other half of, and putting it there adds
     a panel without moving the chart, the book or the ticket. */
  grid-template-areas:
    "bar    bar    bar"
    "chart  book   ticket"
    "chart  tape   ticket"
    "tables tables tables";
  grid-template-columns: minmax(0, 1fr) 248px 268px;
  grid-template-rows: auto minmax(220px, 1fr) minmax(150px, 0.55fr) minmax(190px, 30vh);
  min-height: calc(100vh - 44px);
}

.terminal > .market-bar {
  grid-area: bar;
}
.terminal > .chart-panel {
  grid-area: chart;
}
.terminal > .book-panel {
  grid-area: book;
}
.terminal > .ticket-panel {
  grid-area: ticket;
}
.terminal > .tape-panel {
  grid-area: tape;
  /* Its own scroll, so a long tape never grows the page. */
  min-height: 0;
}
.terminal > .tables-panel {
  grid-area: tables;
}

.tape-panel .panel-body {
  overflow: auto;
  min-height: 0;
}
/* The scope line is quiet, and read once. */
.tape-panel [data-field="recent-trades-scope"] {
  padding: var(--s2) var(--s3);
  margin: 0;
}

/* A collapsed panel keeps its header, so it can be brought back. */
.panel[data-collapsed="true"] .panel-body {
  display: none;
}

.terminal[data-collapsed-book="true"] {
  grid-template-columns: minmax(0, 1fr) 36px 268px;
}

/* ------------------------------------------------------------------- chart --- */

.chart-panel .panel-body {
  position: relative;
  overflow: hidden;
}

/*
 * Pinned to its container rather than sized at 100%.
 *
 * A canvas has an INTRINSIC size (its width and height attributes, which the drawing
 * code sets in device pixels) as well as a CSS size, and `width: 100%` resolves
 * against a parent whose own width the intrinsic size can influence. The result was a
 * canvas a couple of pixels wider than its box, and a page that scrolled sideways by
 * those pixels. `inset: 0` on an absolutely positioned element fills the padding box
 * exactly and cannot exceed it, which removes the circularity rather than tuning it.
 */
.chart-surface {
  display: block;
  position: absolute;
  inset: 0;
  width: auto;
  height: auto;
}

.chart-readout {
  position: absolute;
  inset-block-start: var(--s3);
  inset-inline-start: var(--s4);
  display: flex;
  gap: var(--s4);
  font-size: var(--t-micro);
  font-family: var(--mono);
  color: var(--ink-2);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  padding: var(--s2) var(--s3);
  border-radius: 4px;
  pointer-events: none;
  direction: ltr;
  unicode-bidi: isolate;
  flex-wrap: wrap;
  max-width: calc(100% - 2 * var(--s4));
}

.chart-readout b {
  color: var(--ink);
  font-weight: 700;
}

.timeframes {
  display: flex;
  gap: 1px;
}

/* ------------------------------------------------------------------- book --- */

.book {
  display: flex;
  flex-direction: column;
  height: 100%;
  font-family: var(--mono);
  font-size: var(--t-small);
}

.book-side {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden auto;
  display: flex;
  flex-direction: column;
}

.book-side[data-side="ask"] {
  /* Asks read upward toward the spread, as a book does. */
  flex-direction: column-reverse;
  justify-content: flex-start;
}

.book-row {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--s3);
  padding: 1px var(--s4);
  cursor: pointer;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: end;
  width: 100%;
  min-height: 18px;
  align-items: center;
}

.book-row:hover {
  background: var(--surface-2);
}

/* Depth shading is a bar behind the row, sized by cumulative total. */
.book-row .depth {
  position: absolute;
  inset-block: 0;
  inset-inline-end: 0;
  background: var(--up50);
  z-index: 0;
  pointer-events: none;
}

.book-row[data-side="ask"] .depth {
  background: var(--down50);
}

.book-row > span {
  position: relative;
  z-index: 1;
}

.book-row .price {
  text-align: start;
  font-weight: 650;
}

.book-row[data-side="bid"] .price {
  color: var(--up600);
}

.book-row[data-side="ask"] .price {
  color: var(--down600);
}

.book-mid {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  border-block: 1px solid var(--line);
  background: var(--surface-2);
  flex: 0 0 auto;
}

.book-mid .last {
  font-size: var(--t-mid);
  font-weight: 700;
  font-family: var(--mono);
  direction: ltr;
  unicode-bidi: isolate;
}

.book-mid .spread {
  font-size: var(--t-micro);
  color: var(--ink-2);
  font-family: var(--mono);
}

.book-head {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--s3);
  padding: var(--s2) var(--s4);
  font-size: var(--t-micro);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-3);
  border-bottom: 1px solid var(--line-soft);
  text-align: end;
  flex: 0 0 auto;
}

.book-head span:first-child {
  text-align: start;
}

/* ----------------------------------------------------------------- ticket --- */

.ticket-panel .panel-body {
  padding: var(--s4);
}

.side-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s2);
  margin-bottom: var(--s4);
}

.side-toggle button {
  appearance: none;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink-2);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.side-toggle button[aria-pressed="true"][data-side="buy"] {
  background: var(--up600);
  border-color: var(--up600);
  color: #ffffff;
}

.side-toggle button[aria-pressed="true"][data-side="sell"] {
  background: var(--down600);
  border-color: var(--down600);
  color: #ffffff;
}

.percentages {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--s2);
  margin-bottom: var(--s4);
}

/* The order types: a label is never broken inside its button ("Stop-limit", "Protected market" wrapped in a
 * narrow ticket, ISSUES.md A-022). The buttons share the row and a whole button moves to the next line instead. */
.percentages.dx-type-row {
  display: flex;
  flex-wrap: wrap;
}

.percentages.dx-type-row > button {
  flex: 1 1 auto;
  white-space: nowrap;
}

.balances {
  display: grid;
  gap: var(--s2);
  padding: var(--s3) 0 var(--s4);
  border-top: 1px solid var(--line-soft);
  margin-top: var(--s3);
}

/* ---------------------------------------------------------------- tables --- */

.tables-panel .panel-body {
  padding: 0;
}

.table-scroll {
  overflow: auto;
  max-height: 100%;
}

.row-actions {
  display: flex;
  gap: var(--s2);
  justify-content: flex-end;
}

/* --------------------------------------------------------------- markets --- */

.markets-page {
  padding: var(--s5);
  display: grid;
  gap: var(--s4);
  max-width: 1400px;
  margin: 0 auto;
}

.markets-toolbar {
  display: flex;
  gap: var(--s4);
  align-items: center;
  flex-wrap: wrap;
}

.markets-toolbar input[type="search"] {
  flex: 1 1 240px;
  min-height: var(--tap);
  padding: var(--s3) var(--s4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
}

.watch-star {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-3);
  cursor: pointer;
  font-size: var(--t-mid);
  min-width: 24px;
  min-height: 24px;
}

.watch-star[aria-pressed="true"] {
  color: var(--warn600);
}

/* ------------------------------------------------------- swap and pools --- */

.centred {
  max-width: 460px;
  margin: 0 auto;
  padding: var(--s6) var(--s5);
  display: grid;
  gap: var(--s4);
}

.wide {
  max-width: 1100px;
  margin: 0 auto;
  padding: var(--s6) var(--s5);
  display: grid;
  gap: var(--s4);
}

.swap-legs {
  display: grid;
  gap: var(--s2);
  position: relative;
}

.flip {
  justify-self: center;
  margin-block: calc(-1 * var(--s3));
  z-index: 1;
}

/* ------------------------------------------------------------ responsive --- */

/* Tablet: the ticket drops under the chart, the book keeps its column. */
@media (max-width: 1180px) {
  .terminal {
    grid-template-areas:
      "bar    bar"
      "chart  book"
      "ticket book"
      "tape   tape"
      "tables tables";
    grid-template-columns: minmax(0, 1fr) 260px;
    grid-template-rows: auto minmax(260px, 1fr) auto minmax(140px, 22vh) minmax(180px, 28vh);
  }
  .terminal[data-collapsed-book="true"] {
    grid-template-columns: minmax(0, 1fr) 36px;
  }
}

/* Mobile: sections, not a shrunken terminal. */
@media (max-width: 820px) {
  .terminal {
    display: block;
    padding: var(--s3);
    min-height: auto;
  }
  .section-tabs {
    display: flex;
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--bg);
    padding-block: var(--s2);
  }
  .terminal > .panel,
  .terminal > .market-bar {
    margin-bottom: var(--s3);
  }
  /* One section at a time, chosen by the tabs above. Each row hides everything
     the chosen section is not, including the tape, which is its own section. */
  .terminal[data-section="chart"] > .book-panel,
  .terminal[data-section="chart"] > .tape-panel,
  .terminal[data-section="chart"] > .ticket-panel,
  .terminal[data-section="chart"] > .tables-panel,
  .terminal[data-section="book"] > .chart-panel,
  .terminal[data-section="book"] > .tape-panel,
  .terminal[data-section="book"] > .ticket-panel,
  .terminal[data-section="book"] > .tables-panel,
  .terminal[data-section="trades"] > .chart-panel,
  .terminal[data-section="trades"] > .book-panel,
  .terminal[data-section="trades"] > .ticket-panel,
  .terminal[data-section="trades"] > .tables-panel,
  .terminal[data-section="trade"] > .chart-panel,
  .terminal[data-section="trade"] > .book-panel,
  .terminal[data-section="trade"] > .tape-panel,
  .terminal[data-section="trade"] > .tables-panel,
  .terminal[data-section="orders"] > .chart-panel,
  .terminal[data-section="orders"] > .book-panel,
  .terminal[data-section="orders"] > .tape-panel,
  .terminal[data-section="orders"] > .ticket-panel {
    display: none;
  }
  .chart-panel .panel-body {
    height: 260px;
  }
  .book-panel .panel-body {
    height: 340px;
  }
  .markets-page,
  .centred,
  .wide {
    padding: var(--s4);
  }
  /* The market bar scrolls sideways on purpose; the PAGE never does. */
  .market-bar {
    scrollbar-width: thin;
  }
}

/* ======================================================= cross-chain Spot ===
 *
 * The cross-chain market is the same terminal. These are the three things it adds
 * and nothing else: a badge that names the networks and the assurance class, a
 * definition list for a settlement's quantities, and a reasons list for the orders
 * that could not be taken.
 *
 * Deliberately no new colour, no new panel and no new layout. A cross-chain market
 * that LOOKED different would be a different product wearing the same name.
 */
.cross-badge {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex-wrap: wrap;
  padding-inline-end: var(--s3);
  border-inline-end: 1px solid var(--line);
}

/* A settlement's quantities: label above value, in a row that wraps. */
.facts {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2) var(--s5);
  margin: 0;
  padding: var(--s3) var(--s4);
}
.facts dt {
  font-size: var(--t-micro);
  color: var(--ink-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex-basis: 100%;
  margin: 0;
}
.facts dd {
  margin: 0 0 var(--s2) 0;
  font-variant-numeric: tabular-nums;
}
@supports (display: grid) {
  .facts {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    align-items: start;
  }
  .facts dt {
    flex-basis: auto;
  }
  .facts dd {
    margin-bottom: 0;
  }
}

/* WHY AN ORDER COULD NOT BE TAKEN. Not an error list: each line is a fact about
 * one resting order, and a trader reads it to decide whether to wait or widen. */
.reasons {
  margin: 0;
  padding: var(--s2) var(--s4) var(--s3);
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--s1);
  font-size: var(--t-micro);
  color: var(--ink-2);
}
.reasons li {
  display: flex;
  gap: var(--s2);
  align-items: baseline;
}
.reasons li b {
  font-variant-numeric: tabular-nums;
}

/* ======================================================= DEX V5 · Track D ===
 *
 * Local Spot, the order book and the ticket, on Track A's design tokens (`--dx-*`,
 * src/dex5-tokens.css). Every token falls back to the variable this file used before, so
 * the terminal renders the same way with or without the tokens file loaded, and picks the
 * tokens up the moment it is.
 *
 * Numbers: tabular figures everywhere a figure is read against another one, so columns
 * align digit for digit. The book pads both sides to the same fraction digits (orderbook.js)
 * and this file keeps every cell on one line, ellipsised rather than overflowing its column.
 */

.terminal {
  --dx5d-buy: var(--dx-buy, var(--up600));
  --dx5d-sell: var(--dx-sell, var(--down600));
  --dx5d-buy-wash: color-mix(in srgb, var(--dx5d-buy) 14%, transparent);
  --dx5d-sell-wash: color-mix(in srgb, var(--dx5d-sell) 14%, transparent);
  --dx5d-row-h: var(--dx-row-h, 20px);
  --dx5d-mono: var(--dx-mono, var(--mono));
  --dx5d-dim: var(--dx-text-dim, var(--ink-2));
  --dx5d-border: var(--dx-border, var(--line));
  --dx5d-surface: var(--dx-surface, var(--surface));
  --dx5d-surface-2: var(--dx-surface-2, var(--surface-2));
  --dx5d-focus: var(--dx-focus, var(--accent));
}

.terminal .panel {
  background: var(--dx5d-surface);
  border-color: var(--dx5d-border);
  border-radius: var(--dx-radius-m, var(--radius));
}

/* ---- the book ---------------------------------------------------------- */

.book,
.book-head,
.book-row,
.book-mid,
.stat .v,
.dx-estimate .v,
.balances .v,
.tape-panel td,
.tables-panel td {
  font-variant-numeric: tabular-nums;
}

.book {
  font-family: var(--dx5d-mono);
  font-size: var(--dx-fs-sm, var(--t-small));
}

.book-head,
.book-row {
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: var(--dx-sp-2, var(--s3));
  padding-inline: var(--dx-sp-3, var(--s4));
}

.book-row {
  min-height: var(--dx5d-row-h);
  line-height: var(--dx5d-row-h);
  padding-block: 0;
}

.book-row > span:not(.depth) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  direction: ltr;
  unicode-bidi: isolate;
}

.book-row:hover,
.book-row:focus-visible {
  background: var(--dx5d-surface-2);
}

.book-row:focus-visible {
  outline: 2px solid var(--dx5d-focus);
  outline-offset: -2px;
}

/* Cumulative depth: a wash behind the row, sized by the running total. */
.book-row .depth {
  background: var(--dx5d-buy-wash);
  transition: width 120ms linear;
}
.book-row[data-side="ask"] .depth {
  background: var(--dx5d-sell-wash);
}
@media (prefers-reduced-motion: reduce) {
  .book-row .depth {
    transition: none;
  }
}

.book-row[data-side="bid"] .price {
  color: var(--dx5d-buy);
}
.book-row[data-side="ask"] .price {
  color: var(--dx5d-sell);
}

.book-mid {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: baseline;
  background: var(--dx5d-surface-2);
  border-color: var(--dx5d-border);
}
.book-mid .mid {
  font-size: var(--dx-fs-xs, var(--t-micro));
  color: var(--dx5d-dim);
  font-family: var(--dx5d-mono);
  text-align: center;
  direction: ltr;
  unicode-bidi: isolate;
}
.book-mid .last.up {
  color: var(--dx5d-buy);
}
.book-mid .last.down {
  color: var(--dx5d-sell);
}

/* ---- the ticket -------------------------------------------------------- */

.ticket-panel .field input {
  min-height: var(--dx-input-h, var(--tap));
  font-family: var(--dx5d-mono);
  font-variant-numeric: tabular-nums;
}

.side-toggle button[aria-pressed="true"][data-side="buy"] {
  background: var(--dx5d-buy);
  border-color: var(--dx5d-buy);
}
.side-toggle button[aria-pressed="true"][data-side="sell"] {
  background: var(--dx5d-sell);
  border-color: var(--dx5d-sell);
}

.ticket-panel button.action[data-field="review"][data-kind="buy"]:not(:disabled) {
  background: var(--dx5d-buy);
  border-color: var(--dx5d-buy);
  color: #ffffff;
}
.ticket-panel button.action[data-field="review"][data-kind="sell"]:not(:disabled) {
  background: var(--dx5d-sell);
  border-color: var(--dx5d-sell);
  color: #ffffff;
}
.ticket-panel button.action[data-field="review"][data-pending]:not([data-pending=""]) {
  opacity: 0.75;
  cursor: progress;
}

.ticket-panel button.tiny:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.dx-rules {
  font-size: var(--dx-fs-xs, var(--t-micro));
  color: var(--dx5d-dim);
  margin-block: 0 var(--dx-sp-2, var(--s3));
  font-variant-numeric: tabular-nums;
}

.dx-estimate {
  margin-top: 0;
  border-top: 1px dashed var(--dx5d-border);
}
.dx-estimate .row .v {
  font-family: var(--dx5d-mono);
  direction: ltr;
  unicode-bidi: isolate;
  text-align: end;
  overflow-wrap: anywhere;
}
.dx-estimate [data-field="est-age"] {
  margin: 0;
  font-size: var(--dx-fs-xs, var(--t-micro));
  color: var(--dx5d-dim);
}

.dx-pending:not([hidden]) {
  margin-top: var(--dx-sp-2, var(--s3));
  padding: var(--dx-sp-2, var(--s3)) var(--dx-sp-3, var(--s4));
  border-inline-start: 3px solid var(--dx-warn, var(--warn600));
  background: color-mix(in srgb, var(--dx-warn, var(--warn600)) 10%, transparent);
  border-radius: var(--dx-radius-s, 4px);
}

.dx-submission[data-state="applied"] {
  color: var(--dx5d-buy);
}
.dx-submission[data-state="rejected"] {
  color: var(--dx-danger, var(--down600));
}
.dx-submission[data-state="unknown"],
.dx-submission[data-state="expired"] {
  color: var(--dx-warn, var(--warn600));
}

/* ---- the market selector ----------------------------------------------- */

.dx-market-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}
.dx-market-row .book-row {
  min-width: 0;
}
.dx-market-row .book-row .quiet {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 28ch;
}
.dx-market-row .watch-star {
  min-width: var(--dx-input-h, var(--tap));
  min-height: var(--dx-input-h, var(--tap));
}

/* ---- the tape ---------------------------------------------------------- */

/* Four columns that fit the book column without an inner horizontal scroll (V-07). */
.dx-tape {
  width: 100%;
  table-layout: fixed;
}
.dx-tape th,
.dx-tape td {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-inline: var(--dx-sp-1, 4px);
  font-variant-numeric: tabular-nums;
}
.dx-tape th:nth-child(1),
.dx-tape td:nth-child(1) {
  width: 27%;
}
.dx-tape th:nth-child(3),
.dx-tape td:nth-child(3) {
  width: 16%;
}
.dx-tape th:nth-child(4),
.dx-tape td:nth-child(4) {
  width: 19%;
}
.dx-tape td:nth-child(1),
.dx-tape td:nth-child(2),
.dx-tape td:nth-child(3) {
  text-align: end;
}
.tape-panel .panel-body {
  overflow-x: hidden;
}

.dx-raw-detail {
  margin-top: var(--dx-sp-1, 4px);
  font-size: var(--dx-fs-xs, var(--t-micro));
}
.dx-raw-detail code {
  display: block;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: var(--dx-text-dim, var(--ink-2));
}

@media (max-width: 820px) {
  .terminal {
    --dx5d-row-h: var(--dx-row-h, 24px);
  }
  /* A long statistic label wraps in its own cell instead of running into the next one. */
  .market-bar .stat .k {
    white-space: normal;
    overflow-wrap: anywhere;
  }
  .market-bar .stat {
    min-width: 0;
  }
}
