/* ──────────────────────────────────────────────────────────────────
   Leveraged Market — "The Exchange"
   Spec: docs/game-renovation-v2/specs/leveraged-market.md §Layout

   Every class is `exch-*`, deliberately NOT the legacy `mkt-*`: the
   old markup is removed at mount, so the stale games-compact.css
   `.mkt-chart-wrap` / `.mkt-actions` rules (which already matched
   nothing) stay inert and the inline <style> block in index.html has
   no surface here either.

   Budget on the 692 px play area (812 − 56 header − 64 nav):
     State A  header 56 + ticker 30 + risk 28 + lev 44 + cockpit 48
              + CTA 60 + gaps/safe ≈ 44  →  chart ≈ 382 px
     State B  header 56 + ticker 30 + strip 84 + CTA 60 + ≈36
              →  chart ≈ 426 px
   ────────────────────────────────────────────────────────────────── */

#leveraged-market-screen.active .exch-wrapper {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 8px;
  padding: 0 0 calc(12px + var(--tg-content-safe-area-inset-bottom, 0px));
  font-family: var(--font-body);
}

/* ── header ─────────────────────────────────────────────────────── */

#leveraged-market-screen.active .exch-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  padding: 4px 0;
  margin: 0;
}
.exch-title {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.10em;
  margin: 0;
  background: linear-gradient(135deg, var(--accent), var(--accent-hi));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.exch-tools {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ── ticker row (30 px + 2 px progress) ─────────────────────────── */

.exch-ticker {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  padding: 0 2px 4px;
  font-size: 12px;
  color: var(--muted);
}
.exch-tick-sym {
  font-size: 11px;
  letter-spacing: 0.14em;
  color: var(--faint);
}
.exch-tick-price {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.exch-regime {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--raised);
  border: 1px solid var(--border);
  font-size: 11px;
  color: var(--accent);
  max-width: 45%;
}
.exch-clock {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.exch-progress {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 2px;
  background: var(--border);
  border-radius: 1px;
  overflow: hidden;
}
.exch-progress b {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-hi));
  transition: width 0.4s linear;
}

/* ── chart stage ────────────────────────────────────────────────── */

.exch-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 280px;
  border-radius: 10px;
  overflow: hidden;
  background: #100f0d;
  border: 1px solid rgba(220, 200, 160, 0.14);
}
.exch-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Danger vignette — opacity only, so it stays on the compositor. */
.exch-vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 160ms linear;
  background: radial-gradient(ellipse at center,
              rgba(215, 95, 77, 0) 38%, rgba(215, 95, 77, 0.85) 100%);
}

/* ── stamps (LIQUIDATED / SURVÉCU) ──────────────────────────────── */

.exch-stamp {
  position: absolute;
  left: 0; right: 0;
  top: 38%;
  text-align: center;
  pointer-events: none;
  opacity: 0;
}
.exch-stamp b {
  display: block;
  font-family: var(--font-display);
  font-size: 40px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--loss);
}
.exch-stamp i {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  font-style: normal;
  color: var(--muted);
}
.exch-stamp[data-kind="survived"] { top: 44%; }
.exch-stamp[data-kind="survived"] b {
  font-size: 11px;
  letter-spacing: 0.20em;
  color: var(--accent-hi);
}
.exch-stamp.is-in {
  animation: exchStamp 500ms cubic-bezier(.22, 1.4, .36, 1) forwards;
}
@keyframes exchStamp {
  0%   { opacity: 0; transform: scale(1.6); }
  55%  { opacity: 1; transform: scale(0.92); }
  75%  { transform: scale(1.05); }
  100% { opacity: 1; transform: scale(1); }
}
/* Chromatic double-draw for the first frames of the slam. */
.exch-stamp[data-kind="liq"].is-in b {
  text-shadow: 2px 0 rgba(215, 95, 77, 0.3), -2px 0 rgba(239, 230, 208, 0.3);
  animation: exchChroma 60ms steps(1) 2 forwards;
}
@keyframes exchChroma {
  to { text-shadow: none; }
}

/* ── floating win chip ──────────────────────────────────────────── */

.exch-chip {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 700;
  color: var(--accent-hi);
  white-space: nowrap;
}
.exch-chip.is-up {
  animation: exchChipUp 1400ms cubic-bezier(.22, 1, .36, 1) forwards;
}
@keyframes exchChipUp {
  0%   { opacity: 0; transform: translateY(0) scale(0.9); }
  15%  { opacity: 1; transform: translateY(-10px) scale(1); }
  100% { opacity: 0; transform: translateY(-72px) scale(1); }
}

/* ── settlement card ────────────────────────────────────────────── */

.exch-card {
  position: absolute;
  left: 8px; right: 8px; bottom: 8px;
  min-height: 148px;
  padding: 14px 16px 12px;
  border-radius: 14px;
  background: color-mix(in oklch, var(--surface) 94%, transparent);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-elevated);
  transform: translateY(110%);
  opacity: 0;
  transition: transform 240ms cubic-bezier(.22, 1, .36, 1), opacity 240ms linear;
  text-align: center;
}
.exch-card.is-in { transform: translateY(0); opacity: 1; }
.exch-card-word {
  font-family: var(--font-display);
  font-size: 15px;
  letter-spacing: 0.16em;
  color: var(--muted);
}
.exch-card[data-kind="liquidated"] .exch-card-word { color: var(--loss); }
.exch-card[data-kind="settled"] .exch-card-word { color: var(--accent); }
.exch-card-net {
  font-family: var(--font-display);
  font-size: 32px;
  font-weight: 700;
  margin: 4px 0 2px;
  font-variant-numeric: tabular-nums;
}
.exch-card-sub {
  font-size: 11px;
  color: var(--muted);
}
.exch-card-line {
  margin-top: 6px;
  font-size: 11px;
  color: var(--accent);
}
.exch-card-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.exch-card-btn {
  flex: 1;
  min-height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--raised);
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: pointer;
}
.exch-card-btn.is-go {
  border-color: color-mix(in oklch, var(--accent) 60%, transparent);
  color: var(--accent-hi);
}
.exch-spark { display: block; margin: 8px auto 0; }
/* A deep loss gets one slow red underline sweep — acknowledged, never
   silent, never celebratory. */
.exch-card.is-deep-loss::after {
  content: "";
  position: absolute;
  left: 16px; right: 16px; bottom: 6px;
  height: 2px;
  background: var(--loss);
  transform-origin: left center;
  animation: exchSweep 400ms ease-out forwards;
}
@keyframes exchSweep {
  from { transform: scaleX(0); opacity: 0.9; }
  to   { transform: scaleX(1); opacity: 0.25; }
}

/* ── deck (state A / state B swap in place) ─────────────────────── */

.exch-deck { flex: 0 0 auto; }
.exch-deck-a, .exch-deck-b {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.exch-deck-a[hidden], .exch-deck-b[hidden] { display: none; }

.exch-risk {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 28px;
  padding: 0 2px;
  font-size: 12px;
  color: var(--muted);
}
.exch-risk-liq { color: var(--loss); font-variant-numeric: tabular-nums; }
.exch-risk-away { color: var(--muted); }
.exch-risk-fee { color: var(--faint); }

.exch-lev {
  display: flex;
  gap: 6px;
}
.exch-lev-chip {
  flex: 1;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, border-color 140ms ease;
}
.exch-lev-chip.is-active {
  background: linear-gradient(180deg, var(--accent-hi), var(--accent));
  border-color: var(--accent);
  color: oklch(0.18 0.02 80);
}

/* The cockpit's own CTA row is hidden — LONG and SHORT are the real
   buttons; only its submit PIPELINE is reused. */
.exch-cockpit .fym-ck-ctarow { display: none; }
.exch-cockpit .fym-ck { gap: 6px; }

.exch-cta-row {
  display: flex;
  gap: 8px;
}
.exch-cta {
  min-height: 60px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: oklch(0.15 0.02 80);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
}
.exch-cta i {
  font-size: 9px;
  font-style: normal;
  font-weight: 600;
  opacity: 0.72;
  letter-spacing: 0.10em;
}
.exch-long  { flex: 1; background: var(--win); }
.exch-short { flex: 1; background: var(--loss); color: oklch(0.98 0.01 80); }
.exch-margin {
  flex: 0 0 116px;
  background: var(--raised);
  border: 1px solid color-mix(in oklch, var(--accent) 55%, transparent);
  color: var(--accent-hi);
  font-size: 12px;
}
.exch-close {
  flex: 1;
  background: linear-gradient(180deg, oklch(0.85 0.13 80), oklch(0.68 0.12 78));
  color: oklch(0.18 0.02 80);
  animation: exchCashPulse 1.1s ease-in-out infinite;
}
@keyframes exchCashPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(232, 199, 119, 0.0); }
  50%      { box-shadow: 0 0 18px 2px rgba(232, 199, 119, 0.35); }
}

/* ── position strip (84 px) ─────────────────────────────────────── */

.exch-pos {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
  min-height: 84px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.exch-pos-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.exch-pos-tag {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.04em;
}
.exch-pnl {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: transform 120ms ease-out;
}
.exch-dist {
  flex: 1;
  display: block;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  overflow: hidden;
}
.exch-dist b {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--loss));
  transition: width 120ms linear;
}
.exch-liq {
  font-size: 11px;
  color: var(--loss);
  font-variant-numeric: tabular-nums;
}

/* ── below the fold ─────────────────────────────────────────────── */

.exch-tail {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 4px;
}
.exch-ring-row { min-height: 26px; }
.exch-section {
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--faint);
  padding-top: 4px;
}
.exch-history {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.exch-history-empty {
  padding: 14px;
  text-align: center;
  font-size: 12px;
  color: var(--faint);
}
.exch-trade {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12px;
}
.exch-trade-side {
  min-width: 64px;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.exch-trade.is-win  .exch-trade-pnl { color: var(--win); }
.exch-trade.is-loss .exch-trade-pnl { color: var(--loss); }
.exch-trade.is-liq  .exch-trade-side { color: var(--loss); }
.exch-trade.is-liq  .exch-trade-pnl { color: var(--warn); }
.exch-trade-px { color: var(--faint); font-variant-numeric: tabular-nums; }
.exch-trade-pnl { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ── add-margin sheet ───────────────────────────────────────────── */

.exch-sheet {
  position: fixed;
  inset: 0;
  z-index: 9998;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}
.exch-sheet[hidden] { display: none; }
.exch-sheet-card {
  width: 100%;
  max-width: 420px;
  padding: 16px 16px calc(20px + var(--tg-content-safe-area-inset-bottom, 0px));
  border-radius: 16px 16px 0 0;
  background: var(--surface);
  border: 1px solid var(--border);
  animation: exchSheetUp 240ms cubic-bezier(.22, 1, .36, 1);
}
@keyframes exchSheetUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.exch-sheet-title {
  font-family: var(--font-display);
  font-size: 17px;
  color: var(--text);
  margin-bottom: 10px;
}
.exch-sheet-chips { display: flex; gap: 6px; margin-bottom: 10px; }
.exch-sheet-chip {
  flex: 1;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--raised);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.exch-sheet-input {
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}
.exch-sheet-prev {
  margin: 10px 0 12px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.exch-sheet-prev b { color: var(--loss); }
.exch-sheet-prev i { color: var(--accent); font-style: normal; }
.exch-sheet-actions { display: flex; gap: 8px; }
.exch-sheet-btn {
  flex: 1;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--raised);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.exch-sheet-btn.is-go {
  background: linear-gradient(180deg, var(--accent-hi), var(--accent));
  border-color: var(--accent);
  color: oklch(0.18 0.02 80);
}

/* ── short viewport (375×667, SE-class) ─────────────────────────── */
/* Both levers are required: the ticker merges into the header (−30 px)
   AND the chart floor drops 280 → 200. Fixed ≈ 280 + chart 200 = 480,
   inside the 547 px available, leaving 67 px of safe-area slack. */

@media (max-height: 700px) {
  #leveraged-market-screen.active .exch-wrapper { position: relative; }
  #leveraged-market-screen.active .exch-header { min-height: 44px; }
  #leveraged-market-screen.active .exch-title { font-size: 17px; }
  #leveraged-market-screen.active .exch-ticker {
    position: absolute;
    top: 8px; left: 52px; right: 84px;
    height: 20px;
    padding: 0;
    font-size: 11px;
  }
  #leveraged-market-screen.active .exch-ticker .exch-regime { display: none; }
  #leveraged-market-screen.active .exch-progress { bottom: -4px; }
  #leveraged-market-screen.active .exch-stage { min-height: 200px; }
  #leveraged-market-screen.active .exch-wrapper { gap: 6px; }
  #leveraged-market-screen.active .exch-cta { min-height: 52px; }
  #leveraged-market-screen.active .exch-pos { min-height: 72px; }
}

@media (prefers-reduced-motion: reduce) {
  .exch-close { animation: none; }
  .exch-chip.is-up { animation-duration: 1ms; }
}
