:root {
  --bg: #0b0e13;
  --panel: #121720;
  --panel-2: #171d28;
  --line: #232b38;
  --text: #e6eaf0;
  --muted: #8b95a5;
  --bid: #22c55e;
  --bid-bg: rgba(34, 197, 94, 0.12);
  --ask: #f0506e;
  --ask-bg: rgba(240, 80, 110, 0.12);
  --accent: #f7931a;
  --warn-bg: rgba(247, 147, 26, 0.14);
  --grid: rgba(139, 149, 165, 0.12);
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9; --panel: #ffffff; --panel-2: #f7f8fb; --line: #e2e6ec; --text: #111827; --muted: #6b7280;
    --bid: #16a34a; --bid-bg: rgba(22, 163, 74, 0.10); --ask: #dc2646; --ask-bg: rgba(220, 38, 70, 0.09);
    --grid: rgba(107, 114, 128, 0.14); color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 var(--sans); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
h2 { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: 0.02em; }

.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; gap: 12px; align-items: center; }
.logo {
  width: 34px; height: 34px; border-radius: 8px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 19px;
}
.title { font-weight: 650; font-size: 16px; }
.subtitle { font-size: 12px; }
.badges { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.badge {
  font-size: 12px; font-weight: 600; padding: 4px 9px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel-2); display: inline-flex; align-items: center; gap: 6px;
}
.badge.env { background: var(--warn-bg); border-color: transparent; color: var(--accent); text-transform: uppercase; }
.badge.env.prod { background: var(--ask-bg); color: var(--ask); }
.badge.soft { font-weight: 500; color: var(--muted); }
.badge.live { background: var(--ask); color: #fff; border-color: transparent; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); display: inline-block; }
.status.ok .dot { background: var(--bid); box-shadow: 0 0 0 3px var(--bid-bg); }
.status.bad .dot { background: var(--ask); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button {
  font: inherit; font-size: 12px; padding: 4px 10px; border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--panel-2); color: var(--text); font-weight: 600; }
.seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.quotes {
  display: grid; grid-template-columns: 1.4fr repeat(7, 1fr); gap: 1px; background: var(--line);
  border-bottom: 1px solid var(--line);
}
.q { background: var(--panel); padding: 10px 16px; min-width: 0; }
.q label { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.q .v { font: 600 16px/1.5 var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.q .v.big { font-size: 24px; line-height: 1.25; }
.q .sub { font: 12px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.v.bid, .up { color: var(--bid); }
.v.ask, .down { color: var(--ask); }
.flash-up { animation: fu 0.6s; } .flash-down { animation: fd 0.6s; }
@keyframes fu { from { background: var(--bid-bg); } } @keyframes fd { from { background: var(--ask-bg); } }

.grid { display: grid; gap: 14px; padding: 14px 20px; grid-template-columns: minmax(0, 1fr); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; min-width: 0; }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.card-head .seg, .card-head > .muted:last-child, .card-head .badge { margin-left: auto; }
.book-card { display: flex; flex-direction: column; }

.chart-meta { font: 12px var(--mono); }
.chart-wrap { position: relative; height: 380px; }
#chart { width: 100%; height: 100%; display: block; cursor: crosshair; }
.tip {
  position: absolute; pointer-events: none; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 9px; font: 12px/1.45 var(--mono); white-space: pre; box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

.book-cols, .row { display: grid; grid-template-columns: 1.1fr 1fr 1fr; gap: 6px; }
.book-cols { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; padding: 0 6px 6px; }
.book-cols span:not(:first-child), .row span:not(:first-child) { text-align: right; }
.book { display: flex; flex-direction: column; font: 12.5px/1.75 var(--mono); font-variant-numeric: tabular-nums; }
#asks { flex-direction: column-reverse; }
.row { position: relative; padding: 0 6px; border-radius: 3px; }
.row .depth { position: absolute; inset: 0 0 0 auto; border-radius: 3px; }
.row span { position: relative; }
#asks .row .p { color: var(--ask); } #asks .depth { background: var(--ask-bg); }
#bids .row .p { color: var(--bid); } #bids .depth { background: var(--bid-bg); }
.book-mid {
  display: flex; justify-content: space-between; margin: 6px 0; padding: 7px 6px;
  border-block: 1px solid var(--line); font: 600 14px var(--mono);
}

.countdown { font: 600 34px/1.1 var(--mono); font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.bar { height: 4px; background: var(--panel-2); border-radius: 4px; margin: 10px 0 12px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); transform: scaleX(0); transform-origin: left; transition: transform 1s linear; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 8px; }
.kv dt { color: var(--muted); font-size: 13px; }
.kv dd { margin: 0; text-align: right; font: 13px var(--mono); font-variant-numeric: tabular-nums; }
h3 { margin: 0; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
button { font: inherit; }
input {
  font: 14px var(--mono); color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* order ticket */
.side-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 12px; }
.side-toggle button {
  padding: 9px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2);
  color: var(--muted); font-weight: 600; cursor: pointer;
}
.side-toggle button.on.buy { background: var(--bid); border-color: var(--bid); color: #fff; }
.side-toggle button.on.sell { background: var(--ask); border-color: var(--ask); color: #fff; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field em { font-style: normal; }
.input-row { position: relative; margin-top: 6px; }
.input-row input { padding-right: 84px; }
.suffix { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-size: 12px; color: var(--muted); pointer-events: none; }
.chips { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.chips button {
  font: 12px var(--mono); padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line);
  background: transparent; color: var(--muted); cursor: pointer;
}
.chips button:hover { color: var(--text); border-color: var(--muted); }
.seg.wide { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
.preview { padding: 10px 0 2px; border-top: 1px solid var(--line); }
.tag {
  font: 600 10px var(--sans); letter-spacing: 0.06em; padding: 1px 5px; border-radius: 4px; vertical-align: 1px;
}
.tag.taker { background: var(--warn-bg); color: var(--accent); }
.check { font-size: 12.5px; padding: 7px 10px; border-radius: 8px; margin: 6px 0 10px; background: var(--panel-2); color: var(--muted); }
.check.ok { background: var(--bid-bg); color: var(--bid); }
.check.bad { background: var(--ask-bg); color: var(--ask); }
.submit {
  width: 100%; padding: 11px; border: 0; border-radius: 8px; color: #fff; font-weight: 650; cursor: pointer;
}
.submit.buy { background: var(--bid); } .submit.sell { background: var(--ask); }
.submit:disabled { opacity: 0.4; cursor: not-allowed; }
.fine { margin: 10px 0 0; }
.order-result { margin-top: 8px; padding: 7px 10px; border-radius: 8px; font-family: var(--mono); background: var(--bid-bg); color: var(--bid); }
.order-result.bad { background: var(--ask-bg); color: var(--ask); }

/* position + risk */
.ghost {
  margin-left: auto; font-size: 12px; padding: 4px 10px; border-radius: 8px; border: 1px solid var(--line);
  background: transparent; color: var(--text); cursor: pointer;
}
.ghost:disabled { opacity: 0.4; cursor: not-allowed; }
.pos-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.pos-side { font: 600 18px var(--mono); }
.pos-side.long { color: var(--bid); } .pos-side.short { color: var(--ask); }
.pos-upnl { font: 600 18px var(--mono); }
.risk { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 4px; }
.risk-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.kill-state { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--bid-bg); color: var(--bid); }
.kill-state.tripped { background: var(--ask); color: #fff; }
.meter-label { display: flex; justify-content: space-between; font-family: var(--mono); }
.meter { height: 6px; background: var(--panel-2); border-radius: 6px; overflow: hidden; margin: 5px 0; }
.meter > div { height: 100%; width: 100%; background: var(--bid); transform-origin: left; transform: scaleX(0); transition: transform 0.4s; }
.meter.warn > div { background: var(--accent); } .meter.bad > div { background: var(--ask); }
.kill-reason { margin-top: 8px; padding: 7px 10px; border-radius: 8px; background: var(--ask-bg); color: var(--ask); }
.risk-actions { display: flex; gap: 8px; margin-top: 10px; }
.risk-actions .ghost { margin-left: 0; }
.danger {
  font-size: 12px; padding: 5px 10px; border-radius: 8px; border: 1px solid var(--ask); background: transparent;
  color: var(--ask); cursor: pointer; font-weight: 600;
}
.danger:hover { background: var(--ask-bg); }

/* strategy runner */
.runner-card .card-head .badge { margin-left: 0; }
.runner-card #runMeta { margin-left: auto; }
.runner-grid { display: grid; grid-template-columns: minmax(0, 420px) minmax(0, 1fr); gap: 16px; }
.runlog {
  margin: 0; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  font: 11.5px/1.6 var(--mono); color: var(--muted); max-height: 230px; overflow: auto; white-space: pre-wrap;
}
.ret-row { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 12px; }
.ret { background: var(--panel-2); padding: 10px 12px; min-width: 0; }
.ret label { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.ret-v { font: 600 26px/1.25 var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ret-v.small-v { font-size: 18px; line-height: 1.8; }
.ret-sub { font: 12px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
code { font: 12px var(--mono); background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
@media (max-width: 760px) { .runner-grid { grid-template-columns: minmax(0, 1fr); } }

/* runner P&L charts */
.pnl-charts { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; margin-bottom: 4px; }
.pnl-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.pnl-head .legend, .pnl-head #pnlRetNow { margin-left: auto; }
.pnl-wrap { position: relative; height: 220px; }
.pnl-wrap canvas { width: 100%; height: 100%; display: block; cursor: crosshair; }
.legend { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 14px; height: 2px; margin-left: 6px; }
.legend .sw.dash { height: 0; border-top: 2px dashed; }
@media (max-width: 1100px) { .pnl-charts { grid-template-columns: minmax(0, 1fr); } }

/* runner trade history */
.history-grid { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 20px; }
.history-grid h3 { margin-bottom: 8px; }
.history-grid .fine { margin: 0 0 8px; }
@media (max-width: 1100px) { .history-grid { grid-template-columns: minmax(0, 1fr); } }

/* fills */
.small-btn { margin-left: 0; }
.fills-card .card-head .muted { margin-left: auto; }
.table-wrap { overflow-x: auto; }
table.fills { width: 100%; border-collapse: collapse; font: 12.5px var(--mono); font-variant-numeric: tabular-nums; }
.fills th { text-align: left; font: 600 11px var(--sans); color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.fills td { padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.fills .r { text-align: right; }
.fills td.empty { text-align: center; padding: 18px; font-family: var(--sans); }
.events { margin-top: 10px; display: grid; gap: 4px; }
.events div { color: var(--muted); }
.events .bad { color: var(--ask); }

footer { padding: 4px 20px 20px; }

/* wide: chart over three cards, order book down the right */
@media (min-width: 1101px) {
  .grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) 320px;
          grid-template-areas: "chart chart chart book" "ticket position funding account" "runner runner runner runner" "history history history history" "fills fills fills fills";
          align-items: start; }
  .chart-card { grid-area: chart; align-self: stretch; display: flex; flex-direction: column; }
  .chart-card .chart-wrap { flex: 1; min-height: 380px; height: auto; }
  .book-card { grid-area: book; align-self: stretch; }
  .ticket-card { grid-area: ticket; } .position-card { grid-area: position; }
  .funding-card { grid-area: funding; } .account-card { grid-area: account; } .fills-card { grid-area: fills; }
  .runner-card { grid-area: runner; } .history-card { grid-area: history; }
}
@media (max-width: 1100px) {
  .quotes { grid-template-columns: repeat(4, 1fr); }
}
@media (max-width: 560px) {
  .topbar, .grid, footer { padding-left: 16px; padding-right: 16px; }
  .quotes { grid-template-columns: repeat(2, 1fr); }
  .q-last, .quotes > .q:last-child { grid-column: span 2; }
  .card-head { flex-wrap: wrap; }
  .chart-card .chart-meta { display: none; }
  .chart-wrap { height: 280px; }
}

/* read-only public view (dashboard/server.py --public) */
.public-only { display: none; }
body.public .public-only { display: inline-flex; }
body.public footer .public-only { display: inline; }
body.public .ticket-card, body.public .position-card, body.public .fills-card { display: none; }
.repo-link { text-decoration: none; color: var(--text); }
.repo-link:hover { border-color: var(--muted); }
@media (min-width: 1101px) {
  body.public .grid { grid-template-areas: "chart chart chart book" "runner runner runner book" "history history history history" "funding funding account account"; }
}

/* ---------- simplified public view ---------- */
.public-block { display: none; }
body.public .public-block { display: block; }
body.public .seg[aria-label="Price units"] { display: none; }

.overview { padding: 36px 20px 8px; max-width: 1400px; margin: 0 auto; }
.ov-intro { max-width: 70ch; }
.overview h1 {
  margin: 0 0 12px; font-size: clamp(26px, 3.2vw, 38px); line-height: 1.12; font-weight: 650;
  letter-spacing: -0.02em; text-wrap: balance;
}
.ov-lede { margin: 0; font-size: 18px; line-height: 1.55; color: var(--muted); max-width: 64ch; text-wrap: pretty; }
.ov-lede strong { color: var(--text); font-weight: 600; }
.ov-lede .up, .ov-lede .down { font-weight: 650; }

.ov-facts {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 32px 0 0;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.ov-fact { padding: 18px 20px 18px 0; min-width: 0; }
.ov-fact + .ov-fact { padding-left: 20px; border-left: 1px solid var(--line); }
.ov-fact dt { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.ov-fact dd { margin: 0; }
.ov-v { font: 600 26px/1.3 var(--mono); font-variant-numeric: tabular-nums; margin-top: 6px !important; white-space: nowrap; }
.ov-sub { font-size: 13px; color: var(--muted); margin-top: 2px !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.live { width: 7px; height: 7px; border-radius: 50%; background: var(--bid); box-shadow: 0 0 0 3px var(--bid-bg); }
@media (prefers-reduced-motion: no-preference) {
  .live { animation: pulse 2s ease-out infinite; }
  @keyframes pulse { 50% { box-shadow: 0 0 0 6px transparent; } }
}

.ov-how { display: grid; grid-template-columns: 180px minmax(0, 1fr); gap: 8px 32px; padding: 28px 0 8px; }
.ov-how h2 { font-size: 15px; margin: 2px 0 0; }
.ov-how ul { margin: 0; padding: 0; list-style: none; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 24px; }
.ov-how li { font-size: 14.5px; line-height: 1.55; color: var(--muted); }
.ov-how li strong { display: block; color: var(--text); font-weight: 600; margin-bottom: 2px; }
.ov-note { grid-column: 2; margin: 8px 0 0; font-size: 13px; color: var(--muted); }

body.public .grid { grid-template-columns: minmax(0, 1fr); grid-template-areas: none; max-width: 1400px; margin: 0 auto; }
body.public .history-card { grid-area: auto; }

.market-details { max-width: 1400px; margin: 0 auto 8px; padding: 0 20px; }
.market-details > summary {
  cursor: pointer; list-style: none; display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap;
  padding: 14px 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); font-weight: 600;
}
.market-details > summary::-webkit-details-marker { display: none; }
.market-details > summary::before { content: "+"; font: 600 16px var(--mono); color: var(--accent); width: 12px; }
.market-details[open] > summary::before { content: "−"; }
.market-details > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.market-details[open] > summary { margin-bottom: 14px; }
.details-slot .quotes { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; margin-bottom: 14px; }
.details-grid { display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 1101px) {
  .details-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) 320px;
                  grid-template-areas: "chart chart chart book" "runner runner runner book" "funding funding account account"; }
}
::selection { background: rgba(247, 147, 26, 0.35); }

@media (max-width: 900px) {
  .ov-facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ov-fact:nth-child(3) { padding-left: 0; border-left: 0; }
  .ov-fact:nth-child(n+3) { border-top: 1px solid var(--line); }
  .ov-how { grid-template-columns: minmax(0, 1fr); }
  .ov-how ul { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .ov-note { grid-column: 1; }
}
@media (max-width: 560px) {
  .overview { padding: 24px 16px 4px; }
  .market-details { padding: 0 16px; }
  .ov-v { font-size: 21px; }
  .ov-fact { padding-right: 12px; }
  .ov-fact + .ov-fact { padding-left: 12px; }
}
@media (max-width: 560px) { .ov-fact:nth-child(3) { padding-left: 0; } }

.public-inline { display: none; }
body.public .public-inline { display: inline; }
body.public .trader-only { display: none !important; }
body.public .c-liq, body.public .c-real, body.public .c-notional, body.public .c-mark { display: none; }
body.public .history-grid { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); }
@media (max-width: 560px) {
  .ov-v { font-size: 18px; white-space: normal; }
  .ov-sub { white-space: normal; }
}
