/* KCEX Farm — exchange terminal, dark gold. Density first: a trader must read price,
   position and PnL without scrolling, and fire an order without hunting for the button. */
:root {
  --bg: #080604;
  --surface: #0f0c07;
  --surface-2: #14100a;
  --elevated: #1b150c;
  --raised: #241c10;
  --border: #3a2e17;
  --border-soft: #241c11;
  --text: #f7f1e3;
  --dim: #a7967a;
  --dim-2: #6a5d44;
  --accent: #e8b54d;
  --accent-2: #f5cf7a;
  --accent-soft: rgba(232, 181, 77, 0.15);
  --accent-glow: rgba(232, 181, 77, 0.42);
  --long: #16c784;
  --long-dim: rgba(22, 199, 132, 0.14);
  --short: #ff4d6a;
  --short-dim: rgba(255, 77, 106, 0.14);
  --warn: #ff8c42;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 22px;
  --r: 12px; --r-sm: 9px;
  --tab-h: 58px;
  --bar-h: 74px;

  --f-xs: 10px; --f-sm: 11px; --f-md: 13px; --f-lg: 15px; --f-xl: 19px; --f-2xl: 27px;

  /* Plain system UI text, no webfont: it is what every native exchange app uses. */
  --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto,
             "Helvetica Neue", Arial, sans-serif;
  /* Every number is monospaced so columns line up and ticking prices never reflow. */
  --font-num: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; user-select: none; -webkit-user-select: none; font: inherit; }
button:focus { outline: none; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--f-md)/1.4 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background: radial-gradient(900px 420px at 50% -18%, #3a2b0c 0%, rgba(8, 6, 4, 0) 60%);
  pointer-events: none;
}

.num, input[type="number"], .book, table.tbl td, .stat .v, .equity, .sym-price .p,
.book-spread, .pos-grid, .acct .meta {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
  letter-spacing: -0.2px;
}

#app {
  position: relative;
  padding: var(--s2) var(--s3) calc(var(--tab-h) + var(--s5));
  max-width: 620px;
  margin: 0 auto;
}
#app.with-bar { padding-bottom: calc(var(--tab-h) + var(--bar-h) + var(--s3)); }

/* Stale data must look stale — a dropped socket cannot pass for a live market. */
body.stale .num, body.stale .book, body.stale .acct, body.stale .pos-card { opacity: .45; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s2) 0 var(--s3);
}
.topbar h1 { margin: 0; font-size: var(--f-lg); font-weight: 600; letter-spacing: -0.1px; }
.topbar h1 span { color: var(--accent-2); }
.link-state { display: flex; align-items: center; gap: 6px; font-size: var(--f-sm); color: var(--dim); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dim-2); flex: none; }
.dot.ok { background: var(--long); box-shadow: 0 0 7px rgba(22, 199, 132, .7); }
.dot.warn { background: var(--warn); box-shadow: 0 0 7px rgba(255, 176, 32, .6); }
.dot.err { background: var(--short); box-shadow: 0 0 7px rgba(255, 77, 106, .6); }

/* ---------------------------------------------------------------- surfaces */
.card {
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border-soft);
  border-radius: var(--r);
  padding: var(--s3);
  margin-bottom: var(--s2);
}
.card.flush { padding: var(--s2); }
.card-title {
  margin: 0 0 var(--s2);
  font-size: var(--f-xs); font-weight: 600; letter-spacing: .5px;
  text-transform: uppercase; color: var(--dim-2);
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s2); }
.stat {
  background: var(--elevated); border: 1px solid var(--border-soft);
  border-radius: var(--r-sm); padding: var(--s2) 10px;
}
.stat .k { font-size: var(--f-xs); letter-spacing: .5px; text-transform: uppercase; color: var(--dim-2); }
.stat .v { font-size: var(--f-lg); font-weight: 650; margin-top: 2px; }
.stat .v small { font-size: var(--f-sm); color: var(--dim); font-weight: 500; }

.pos { color: var(--long); } .neg { color: var(--short); }
.muted { color: var(--dim); } .accent { color: var(--accent-2); }

.chip-delta {
  display: inline-block; padding: 1px 6px; border-radius: 6px;
  font-size: var(--f-sm); font-weight: 650;
}
.chip-delta.up { background: var(--long-dim); color: var(--long); }
.chip-delta.down { background: var(--short-dim); color: var(--short); }

/* ---------------------------------------------------------------- accounts */
.totals { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s2); margin-bottom: var(--s2); }
.acct {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) auto;
  align-items: center; gap: 10px;
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border-soft); border-radius: var(--r);
  padding: 10px var(--s3); margin-bottom: 6px;
  transition: border-color .15s, box-shadow .15s;
}
.acct.sel { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent), 0 0 18px var(--accent-soft); }
.acct.off { opacity: .5; }
/* MEXC refuses to trade this account until a human clears verification or risk control. */
.acct.blocked { border-color: var(--short); box-shadow: inset 3px 0 0 var(--short); }
.acct .blocked-note, .item .blocked-note { color: var(--short); font-size: var(--f-sm); margin-top: 3px; }
.badge.red { background: var(--short-dim); color: var(--short); border-color: rgba(255,77,106,.4); }
.check {
  width: 20px; height: 20px; border-radius: 6px; background: var(--elevated);
  border: 1.5px solid var(--border); display: grid; place-items: center;
  font-size: 12px; color: transparent;
}
.acct.sel .check { background: var(--accent); border-color: var(--accent); color: #1a1305; }
.acct .name { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: var(--f-md); }
.acct .name .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct .meta {
  display: flex; align-items: center; margin-top: 3px;
  font-size: var(--f-xs); color: var(--dim); white-space: nowrap;
  overflow: hidden;
}
/* One rhythm for the whole line instead of hand-placed gaps of different widths. */
.acct .meta > span { display: inline-flex; align-items: center; flex: 0 0 auto; }
.acct .meta > span:last-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.acct .meta > span + span::before {
  content: "·"; color: var(--dim-2); margin: 0 6px;
}
.acct .right { text-align: right; display: grid; gap: 2px; justify-items: end; }
.acct .equity { font-size: var(--f-md); font-weight: 650; }
.acct .sub { font-size: var(--f-sm); }
.badge {
  padding: 1px 5px; border-radius: 5px; font-size: 9px; letter-spacing: .5px;
  text-transform: uppercase; font-weight: 700;
  background: var(--accent-soft); color: var(--accent-2); border: 1px solid rgba(232, 181, 77, .35);
}
.badge.grey { background: rgba(123, 141, 168, .12); color: var(--dim); border-color: var(--border); }
.badge.web-ok   { background: var(--long-dim); color: var(--long); border-color: rgba(22,199,132,.4); }
.badge.web-warm { background: rgba(255,176,32,.14); color: var(--warn); border-color: rgba(255,176,32,.4); }
.badge.web-miss { background: rgba(123,141,168,.12); color: var(--dim); border-color: var(--border); }
.badge.web-err  { background: var(--short-dim); color: var(--short); border-color: rgba(255,77,106,.4); }

/* ---------------------------------------------------------------- controls */
.seg {
  display: flex; gap: 2px; padding: 2px;
  background: var(--elevated); border: 1px solid var(--border-soft); border-radius: var(--r-sm);
}
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--dim);
  font-size: var(--f-md); font-weight: 620; padding: 7px 4px; border-radius: 7px;
  transition: background .14s, color .14s;
}
.seg button.on { background: var(--raised); color: var(--text); }
.seg.dir button.on[data-v="long"] { background: var(--long); color: #04160e; }
.seg.dir button.on[data-v="short"] { background: var(--short); color: #1c0409; }

.chips { display: flex; gap: 5px; }
.chip {
  flex: 1; padding: 6px 0; border-radius: 7px; border: 1px solid var(--border-soft);
  background: var(--elevated); color: var(--dim); font-size: var(--f-sm); font-weight: 600;
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-2); }

label.field { display: block; margin-bottom: var(--s2); }
label.field .lbl {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: var(--f-sm); color: var(--dim); margin-bottom: 4px;
}
.select-btn {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 10px; border-radius: var(--r-sm);
  background: var(--elevated); border: 1px solid var(--border); color: var(--text);
  font-size: var(--f-md); text-align: left;
}
.select-btn:active { border-color: var(--accent); }
.select-value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.select-chev { color: var(--dim); flex: none; }
.opt-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 10px; border-bottom: 1px solid var(--border-soft); border-radius: var(--r-sm);
}
.opt-row:last-child { border-bottom: 0; }
.opt-row.on { background: var(--accent-soft); color: var(--accent-2); }
.opt-row:active { background: var(--elevated); }
.opt-check { color: var(--accent-2); }

input[type="text"], input[type="number"], input[type="password"], select, textarea {
  width: 100%; padding: 9px 10px; border-radius: var(--r-sm);
  background: var(--elevated); border: 1px solid var(--border); color: var(--text);
  font-size: var(--f-md);
}
textarea { font-family: var(--font-num); font-size: var(--f-sm); resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input::placeholder { color: var(--dim-2); }

/* Leverage: the track itself warns where liquidation risk starts. */
input[type="range"] { width: 100%; height: 22px; -webkit-appearance: none; background: transparent; }
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 3px;
  background: linear-gradient(90deg, var(--long) 0 18%, var(--warn) 40%, var(--short) 72%, #7a1d2c 100%);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 17px; height: 17px; margin-top: -6.5px;
  border-radius: 50%; background: #fff; box-shadow: 0 0 0 3px var(--accent-soft), 0 1px 4px rgba(0,0,0,.6);
}

.btn {
  width: 100%; padding: 10px; border-radius: var(--r-sm);
  border: 1px solid var(--border); background: var(--elevated); color: var(--text);
  font-weight: 650; font-size: var(--f-md);
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1a1305; }
.btn.long { background: var(--long); border-color: var(--long); color: #04160e; }
.btn.short { background: var(--short); border-color: var(--short); color: #1c0409; }
.btn.danger { background: var(--short-dim); border-color: var(--short); color: var(--short); }
.btn.ghost { background: transparent; }
.btn.sm { width: auto; padding: 6px 10px; font-size: var(--f-sm); }
.btn:disabled { opacity: .45; }

.toggle {
  width: 40px; height: 24px; border-radius: 12px; border: 0;
  background: var(--border); position: relative; transition: background .16s; flex: none;
}
.toggle::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; transition: left .16s;
}
.toggle.on { background: var(--accent); }
.toggle.on::after { left: 19px; }

.row {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s2);
  padding: 8px 0; border-bottom: 1px solid var(--border-soft); flex-wrap: wrap;
}
.row:last-child { border-bottom: 0; }
.row .k { color: var(--dim); min-width: 0; }
/* Long proxy urls and account labels truncate instead of pushing controls off the card. */
.row .grow { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .btns { flex: 0 0 auto; display: flex; gap: 5px; }

/* Settings list rows: label on one line, controls on their own, never wrapping mid-button. */
.item {
  display: grid; gap: 6px; padding: 9px 0; border-bottom: 1px solid var(--border-soft);
}
.item:last-child { border-bottom: 0; }
.item .head { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.item .head .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.item .head .sub { color: var(--dim); font-size: var(--f-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .controls { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 6px; }
.item .controls .btn { flex: 1 1 auto; min-width: 72px; }
/* Mode picker sits on its own line: label left, segmented control taking the rest. */
.item .mode-row { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 8px; margin-top: 8px; }
.item .mode-label { font-size: var(--f-xs); text-transform: uppercase; letter-spacing: .5px; color: var(--dim-2); }
.seg.sm button { padding: 5px 4px; font-size: var(--f-sm); }

/* ---------------------------------------------------------------- trade */
.sym-head {
  display: grid; grid-template-columns: auto 1fr; align-items: center; gap: var(--s3);
  padding: var(--s2) var(--s3); margin-bottom: var(--s2);
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border-soft); border-radius: var(--r);
}
.sym-btn {
  display: flex; align-items: center; gap: 5px; padding: 7px 10px;
  background: var(--elevated); border: 1px solid var(--border); border-radius: var(--r-sm);
  color: var(--text); font-weight: 700; font-size: var(--f-md);
}
.sym-price { text-align: right; }
.sym-price .p { font-size: var(--f-2xl); font-weight: 600; line-height: 1.05; }
.sym-price .meta { font-size: var(--f-sm); color: var(--dim); margin-top: 2px; display: flex; gap: 8px; justify-content: flex-end; }

.book { display: grid; gap: 1px; }
.book-row {
  position: relative; display: grid; grid-template-columns: 1fr auto;
  padding: 2px var(--s2); font-size: var(--f-sm); border-radius: 4px;
}
.book-row .bar { position: absolute; top: 0; bottom: 0; right: 0; opacity: .16; border-radius: 4px; }
.book-row.ask .bar { background: var(--short); }
.book-row.bid .bar { background: var(--long); }
.book-row span { position: relative; }
.book-row.ask .p { color: var(--short); }
.book-row.bid .p { color: var(--long); }
.book-row .v { color: var(--dim); }
.book-spread {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 5px var(--s2); margin: 3px 0;
  border-top: 1px solid var(--border-soft); border-bottom: 1px solid var(--border-soft);
}
.book-spread .last { font-size: var(--f-lg); font-weight: 700; }
.book-spread .sp { font-size: var(--f-sm); color: var(--dim); }

/* Skeletons: an empty book must not read as a book full of zeroes. */
.skeleton {
  height: 15px; margin: 2px var(--s2); border-radius: 4px;
  background: linear-gradient(90deg, var(--elevated) 25%, var(--raised) 50%, var(--elevated) 75%);
  background-size: 240% 100%; animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { from { background-position: 140% 0; } to { background-position: -40% 0; } }

/* Sticky order bar: the submit button never hides below the fold. */
.orderbar {
  position: fixed; left: 0; right: 0; bottom: var(--tab-h); z-index: 18;
  padding: var(--s2) var(--s3) 10px;
  background: linear-gradient(180deg, rgba(9, 15, 26, .82), rgba(4, 7, 13, .97));
  backdrop-filter: blur(14px); border-top: 1px solid var(--border-soft);
}
.orderbar .preview {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: var(--f-sm); color: var(--dim); margin-bottom: 6px;
}
.orderbar .preview b { color: var(--text); }
.orderbar .btn { padding: 12px; font-size: var(--f-lg); }

.result { display: flex; justify-content: space-between; gap: var(--s2); font-size: var(--f-sm);
  padding: 5px var(--s2); border-radius: 6px; margin-top: 4px; }
.result.ok { background: var(--long-dim); }
.result.err { background: var(--short-dim); }
.result .why { color: var(--dim); max-width: 60%; text-align: right; overflow-wrap: anywhere; }

/* ---------------------------------------------------------------- positions */
.pos-card {
  position: relative; padding: 10px var(--s3); margin-bottom: 6px;
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border-soft); border-radius: var(--r); overflow: hidden;
}
.pos-card::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; }
.pos-card.long::before { background: var(--long); }
.pos-card.short::before { background: var(--short); }
.pos-head { display: flex; justify-content: space-between; align-items: baseline; gap: var(--s2); }
.pos-sym { font-weight: 700; display: flex; align-items: center; gap: 6px; }
.pos-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
  margin: var(--s2) 0; font-size: var(--f-sm);
}
.pos-grid .k { font-size: var(--f-xs); text-transform: uppercase; color: var(--dim-2); }
.actions { display: flex; gap: 5px; }
.actions .btn { flex: 1; padding: 7px 0; font-size: var(--f-sm); }

/* ---------------------------------------------------------------- stats */
.bars { display: flex; align-items: flex-end; gap: 3px; height: 96px; padding-top: var(--s2); }
.bars .b { flex: 1; min-height: 2px; max-width: 56px; border-radius: 3px 3px 0 0;
  background: linear-gradient(180deg, var(--accent-2), rgba(232, 181, 77, .2)); }
/* A day with no trades still gets a tick, so the axis stays readable. */
.bars .b.empty-bar { background: var(--border-soft); }
.bars-x { display: flex; gap: 3px; margin-top: 3px; font-size: 9px; color: var(--dim-2); }
.bars-x span { flex: 1; text-align: center; }

table.tbl { width: 100%; border-collapse: collapse; font-size: var(--f-sm); }
table.tbl th {
  padding: 5px 4px; text-align: right; font-size: var(--f-xs); font-weight: 600;
  text-transform: uppercase; color: var(--dim-2);
}
table.tbl th:first-child, table.tbl td:first-child { text-align: left; }
table.tbl td { padding: 6px 4px; text-align: right; border-top: 1px solid var(--border-soft); }

/* ---------------------------------------------------------------- chrome */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; height: var(--tab-h);
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: rgba(6, 11, 20, .94); backdrop-filter: blur(16px);
  border-top: 1px solid var(--border-soft); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  background: 0; border: 0; color: var(--dim-2); font-size: 10px;
  display: grid; place-items: center; gap: 2px;
}
.tabbar button .ic { font-size: 16px; line-height: 1; }
.tabbar button.on { color: var(--accent-2); }

.selbar {
  position: fixed; left: var(--s3); right: var(--s3); bottom: calc(var(--tab-h) + 8px); z-index: 19;
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 10px; font-size: var(--f-sm);
  background: rgba(22, 35, 58, .96); backdrop-filter: blur(10px);
  border: 1px solid var(--accent); border-radius: var(--r);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .55);
}
.selbar .btns { display: flex; gap: 5px; }

.modal { position: fixed; inset: 0; z-index: 40; display: none; background: rgba(2, 5, 10, .8); backdrop-filter: blur(5px); }
.modal.on { display: block; }
.modal-body {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 88vh; overflow: auto;
  padding: var(--s3) var(--s3) calc(var(--s4) + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border); border-radius: 16px 16px 0 0;
}
.sym-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px var(--s2); border-bottom: 1px solid var(--border-soft);
}
.sym-row:active { background: var(--elevated); }

.toast {
  position: fixed; left: var(--s3); right: var(--s3); bottom: calc(var(--tab-h) + 12px); z-index: 50;
  padding: 9px var(--s3); font-size: var(--f-md);
  background: var(--raised); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: var(--r-sm); box-shadow: 0 8px 26px rgba(0, 0, 0, .6);
  transform: translateY(150%); opacity: 0; pointer-events: none;
  transition: transform .22s ease, opacity .18s ease;
}
.toast.on { transform: translateY(0); opacity: 1; }
.toast.err { border-left-color: var(--short); }
.toast.ok { border-left-color: var(--long); }
body.modal-open { overflow: hidden; }
/* A failed close must not hide its price field or the retry button. */
.modal.on ~ .toast { top: calc(12px + env(safe-area-inset-top)); bottom: auto; transform: translateY(-150%); }
.modal.on ~ .toast.on { transform: translateY(0); }

/* A configuration that silently blocks every order deserves to be visible before it does. */
.warn-note {
  margin: 8px 0; padding: 8px 10px; border-radius: var(--r-sm);
  background: var(--short-dim); color: var(--short);
  border: 1px solid rgba(255, 77, 106, .35); font-size: var(--f-sm); line-height: 1.35;
}

.hidden { display: none !important; }
.empty { padding: 22px 10px; text-align: center; color: var(--dim-2); font-size: var(--f-md); }
.spinner {
  display: inline-block; width: 13px; height: 13px; vertical-align: -2px;
  border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
