/* The hidden attribute loses to any author rule that sets display: the
   browser's own [hidden] { display: none } has the lowest priority there is.
   So a class like .actions { display: flex } silently kept "hidden" elements on
   screen — a retry button during a login that was still waiting, a stale match
   number after a denial, a "no broker token" banner on a server that has one.
   One rule, at the top, for every element that will ever carry both. */
[hidden] { display: none !important; }

/* main is a grid, and a grid item will not shrink below its content's
   min-content width unless told to. A wide table inside a panel — even one in
   an overflow:auto wrapper — therefore widened the whole column past the
   window, and every tab with a table scrolled sideways, on phones worst of all.
   min-width: 0 lets the panel take the column's width and leaves the scrolling
   to the table wrappers, which is where it belongs. */
main > [data-panel] { min-width: 0; }

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #16191d;
  --muted: #66707c;
  --accent: #1769aa;
  --accent-weak: #e8f0f8;
  --danger: #b42318;
  --danger-weak: #fdecea;
  --warn: #8a5a00;
  --warn-weak: #fff4e0;
  --ok: #17603a;
  --ok-weak: #e7f4ec;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  /* Chart series: eight hues far enough apart to tell lines apart, in the same
     order as the legend and the comparison table, so colour is never the only
     way to match a line to its row. */
  --s0: #1769aa; --s1: #d9730d; --s2: #2e7d32; --s3: #7b3fa0;
  --s4: #c62828; --s5: #00838f; --s6: #6d4c41; --s7: #ad1457;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1c2025;
    --surface-2: #23282e;
    --border: #313840;
    --text: #e8ebee;
    --muted: #98a2ae;
    --accent: #4d9fe0;
    --accent-weak: #1b2c3a;
    --danger: #f28b82;
    --danger-weak: #35211f;
    --warn: #e0b062;
    --warn-weak: #332a19;
    --ok: #6ecf9a;
    --ok-weak: #172a20;
    --shadow: none;
    --s0: #5aa9e6; --s1: #f0a050; --s2: #6fcf73; --s3: #b98ae3;
    --s4: #ef7070; --s5: #4dd0e1; --s6: #bca08f; --s7: #f06292;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---- header ---- */
header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.bar {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand { font-weight: 650; font-size: 16px; letter-spacing: -.01em; }
.spacer { flex: 1 1 auto; }

/* The environment badge is deliberately loud in production: it is the
   difference between practice and real money. */
.env {
  font-size: 12px;
  font-weight: 650;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
  white-space: nowrap;
}
.env.sandbox { background: var(--ok-weak); color: var(--ok); border-color: currentColor; }
.env.live { background: var(--danger-weak); color: var(--danger); border-color: currentColor; }

nav {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}
nav::-webkit-scrollbar { display: none; }

nav button {
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 10px 12px;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
nav button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

main { max-width: 1180px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }

section.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

h2 { margin: 0 0 4px; font-size: 15px; font-weight: 620; }
h3 { margin: 16px 0 8px; font-size: 13px; font-weight: 620; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 12px; }

/* ---- metrics ---- */
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.metric { background: var(--surface-2); border-radius: 8px; padding: 12px; }
.metric .label { font-size: 12px; color: var(--muted); }
.metric .value { font-size: 20px; font-weight: 620; font-variant-numeric: tabular-nums; margin-top: 2px; }
.metric .value.pos { color: var(--ok); }
.metric .value.neg { color: var(--danger); }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; white-space: normal; min-width: 140px; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
td.pos { color: var(--ok); }
td.neg { color: var(--danger); }

/* ---- drift bar ---- */
.bar-track {
  position: relative;
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  min-width: 90px;
  overflow: hidden;
}
.bar-fill { position: absolute; inset: 0 auto 0 0; background: var(--accent); border-radius: 999px; }
.bar-target { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--text); opacity: .55; }

/* Micro bars live inside a table cell under the number they illustrate, so they
   stay thin and never take a row's height. */
.bar-track.micro { height: 4px; margin-top: 4px; min-width: 40px; }
.bar-track.diverging { background: linear-gradient(to right, var(--surface-2) 0 50%, var(--border) 50% calc(50% + 1px), var(--surface-2) calc(50% + 1px) 100%); }
.bar-track.micro .bar-fill { inset: 0 auto 0 auto; }
.bar-fill.pos { background: var(--ok); }
.bar-fill.neg { background: var(--danger); }
.cell-sub { font-size: 12px; color: var(--muted); }

/* ---- forms ---- */
label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { min-height: 90px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }

/* 1 / -1 rather than "span 2": the row is auto-fit, so at phone width it
   collapses to a single column and a two-column span would overflow it. */
.field-row .span-2 { grid-column: 1 / -1; }
.strategy-hint { display: grid; gap: 2px; margin: 0; }
.strategy-desc { color: var(--text); }
.strategy-needs { font-size: 12px; }
.strategy-key { font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

button.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 550;
  cursor: pointer;
}
button.btn.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
button.btn.danger { background: var(--danger); color: #fff; }
button.btn:disabled { opacity: .5; cursor: not-allowed; }
button.link { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }

/* A switch for the two flags that can start real trading. */
.switch { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.switch input { width: auto; margin: 0; }
.switch .text { display: grid; gap: 2px; }
.switch .text strong { font-size: 13px; color: var(--text); }
.switch .text span { font-size: 12px; color: var(--muted); }
.switch.armed { border-color: var(--danger); background: var(--danger-weak); }

.note { border-radius: 8px; padding: 10px 12px; font-size: 13px; border: 1px solid transparent; }
.note.info { background: var(--accent-weak); color: var(--accent); border-color: currentColor; }
.note.warn { background: var(--warn-weak); color: var(--warn); border-color: currentColor; }
.note.danger { background: var(--danger-weak); color: var(--danger); border-color: currentColor; }
.note.ok { background: var(--ok-weak); color: var(--ok); border-color: currentColor; }
.note + .note { margin-top: 8px; }

.pill { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.pill.ok { background: var(--ok-weak); color: var(--ok); }
.pill.warn { background: var(--warn-weak); color: var(--warn); }
.pill.danger { background: var(--danger-weak); color: var(--danger); }

/* ---- targets editor ---- */
.targets { display: grid; gap: 8px; }
.target-row { display: grid; grid-template-columns: 2fr 1.4fr 90px 80px 36px; gap: 8px; align-items: end; }
.target-row .rm { padding: 9px 0; }
.sum-line { display: flex; justify-content: space-between; align-items: center; font-size: 13px; padding: 8px 0; }
.sum-line .value { font-weight: 650; font-variant-numeric: tabular-nums; }
.sum-line .value.bad { color: var(--danger); }
.sum-line .value.good { color: var(--ok); }

/* ---- dialog ---- */
dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  max-width: 520px;
  width: calc(100% - 32px);
}
dialog::backdrop { background: rgba(10, 12, 15, .55); }

/* ---- chat ---- */
.chat { display: grid; grid-template-rows: 1fr auto; gap: 12px; min-height: 420px; }
.messages { display: grid; gap: 10px; align-content: start; max-height: 55vh; overflow-y: auto; padding-right: 4px; }
.msg { padding: 10px 12px; border-radius: 10px; max-width: 88%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { background: var(--accent-weak); color: var(--text); justify-self: end; }
.msg.assistant { background: var(--surface-2); }
.msg.tool { background: transparent; border: 1px dashed var(--border); color: var(--muted); font-size: 12px; font-family: ui-monospace, monospace; }
.msg .who { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.composer { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.steps { display: grid; gap: 4px; margin-top: 6px; }
.step { font-size: 12px; color: var(--muted); }

.empty { color: var(--muted); font-size: 13px; padding: 18px; text-align: center; border: 1px dashed var(--border); border-radius: 8px; }

/* ---- login ---- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; max-width: 420px; width: 100%; box-shadow: var(--shadow); }
.login-card h1 { font-size: 18px; margin: 0 0 8px; }
.qr-holder { display: grid; place-items: center; padding: 16px; background: #fff; border-radius: 8px; margin: 12px 0; }
.qr-holder svg { max-width: 100%; height: auto; }

.status-line { min-height: 20px; font-size: 13px; }
.status-line.error { color: var(--danger); }
.status-line.ok { color: var(--ok); }

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.right { text-align: right; }
.muted { color: var(--muted); }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

@media (max-width: 640px) {
  .target-row { grid-template-columns: 1fr 1fr; }
  .target-row .rm { grid-column: 2; justify-self: end; }
  .msg { max-width: 100%; }
}

/* ---- layout utilities ----
   These exist so the page needs no inline style attributes: the strict CSP
   (style-src 'self') blocks those, and keeping the policy strict is worth more
   than the convenience of writing style="" in markup. */
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-12 { margin-bottom: 12px; }
.my-12 { margin: 12px 0; }
.no-margin { margin: 0; }
.w-auto { width: auto; }
.w-select { width: auto; min-width: 160px; }
.inline-label { display: inline; }
.center { text-align: center; }
.tall-input { min-height: 52px; }
.tight { gap: 0; }
.section-footer { border-top: 1px solid var(--border); margin-top: 16px; padding-top: 16px; }

/* A target row the broker could not confirm, or confirmed with a problem. */
.target-row.bad-row input { border-color: var(--danger); }

/* Shown only when the configured qualified-investor flag disagrees with the
   broker: silently wrong either rejects orders or hides instruments. */
.env.warn-badge { background: var(--warn-weak); color: var(--warn); border-color: currentColor; cursor: help; }

/* The thread picker holds a question, not a label, so it needs room. It still
   yields on a narrow screen rather than pushing the row off the page. */
.thread-select {
  width: auto;
  min-width: 260px;
  max-width: min(48ch, 100%);
}

@media (max-width: 640px) {
  .thread-select { min-width: 0; width: 100%; }
}

/* ---- charts ----
   Geometry is set in SVG attributes; everything visual lives here, on the same
   tokens as the rest of the page, so charts follow the dark theme without a
   second palette. */
.chart-wrap { overflow-x: auto; }
.chart { display: block; width: 100%; min-width: 460px; height: auto; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis, .chart-label, .chart-value { fill: var(--muted); font-size: 11px; }
.chart-label { fill: var(--text); font-size: 12px; }
.chart-link { stroke: var(--border); stroke-width: 3; stroke-linecap: round; }
.chart-dot.weight { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
.chart-dot.risk { fill: var(--accent); }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-dot { width: 10px; height: 10px; border-radius: 999px; }
.legend-dot.weight { background: var(--surface); border: 2px solid var(--accent); }
.legend-dot.risk { background: var(--accent); }
.price-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.price-line.pos { stroke: var(--ok); }
.price-line.neg { stroke: var(--danger); }
.price-area { stroke: none; opacity: .12; }
.price-area.pos { fill: var(--ok); }
.price-area.neg { fill: var(--danger); }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--surface-2); }
tr.instrument-row > td { background: var(--surface-2); }
.instrument-card { padding: 4px 0 8px; }
.note-list { margin: 6px 0 0; padding-left: 18px; }
.note-list li { margin: 2px 0; }

/* ---- login by approval ---- */
.match-box { display: grid; justify-items: center; gap: 4px; margin: 14px 0 6px; }
.match-label { font-size: 13px; color: var(--muted); }
.match-code { font-size: 44px; font-weight: 700; letter-spacing: 4px; color: var(--text); line-height: 1.1; }
.first-device summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.request-facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 12px 0; }
.request-facts dt { color: var(--muted); font-size: 13px; }
.request-facts dd { margin: 0; overflow-wrap: anywhere; }
.match-choices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 10px; }
.btn.match-choice { font-size: 28px; font-weight: 700; padding: 18px 0; }
.btn.wide { width: 100%; }
.done-text { font-size: 16px; margin: 12px 0 0; }
.done-text.ok { color: var(--ok); }
.done-text.error { color: var(--danger); }
.small { font-size: 12px; }

/* ---- scanner ---- */
.scan-dialog { width: min(420px, calc(100vw - 32px)); }
.scan-frame { position: relative; aspect-ratio: 1 / 1; max-width: 100%; background: #000; border-radius: 10px; overflow: hidden; margin: 10px 0; }
.scan-frame video { width: 100%; height: 100%; object-fit: cover; display: block; }

.setup-banner { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 12px; }
.setup-banner .btn { margin-left: auto; }

/* ---- analysis ---- */
.chart-zero { stroke: var(--muted); stroke-width: 1; opacity: .6; }
.equity-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.equity-benchmark { fill: none; stroke: var(--muted); stroke-width: 1.6; stroke-dasharray: 5 4; }
.equity-line.s0 { stroke: var(--s0); } .equity-line.s1 { stroke: var(--s1); }
.equity-line.s2 { stroke: var(--s2); } .equity-line.s3 { stroke: var(--s3); }
.equity-line.s4 { stroke: var(--s4); } .equity-line.s5 { stroke: var(--s5); }
.equity-line.s6 { stroke: var(--s6); } .equity-line.s7 { stroke: var(--s7); }
.swatch { display: inline-block; width: 14px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.swatch.s0 { background: var(--s0); } .swatch.s1 { background: var(--s1); }
.swatch.s2 { background: var(--s2); } .swatch.s3 { background: var(--s3); }
.swatch.s4 { background: var(--s4); } .swatch.s5 { background: var(--s5); }
.swatch.s6 { background: var(--s6); } .swatch.s7 { background: var(--s7); }
.swatch.dashed { background: repeating-linear-gradient(to right, var(--muted) 0 5px, transparent 5px 9px); }
table.compare td.best { font-weight: 700; }
table.compare td.best::after { content: " ★"; color: var(--muted); font-weight: 400; }
.strategy-picker { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 12px; margin: 12px 0 0; }
.strategy-picker legend { padding: 0 6px; font-size: 13px; }
#bt-strategies { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px 18px; }
.picker-style { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.picker-item { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); padding: 2px 0; cursor: pointer; }
.picker-item input { width: auto; margin: 0; }
tr.detail-row > td { background: var(--surface-2); }
.gap-history { padding: 4px 0; }
table.mini { font-size: 13px; }
table.mini th, table.mini td { padding: 4px 8px; }
.warn-text { color: var(--warn); }

/* Numbers stay on one line so columns line up; prose must not. A refusal
   message or a verdict held on a single line stretched its table far past the
   card and hid the columns after it behind a scrollbar. */
td.prose { white-space: normal; text-align: left; min-width: 220px; }
tr.detail-row > td { white-space: normal; text-align: left; }
.chart.fit { min-width: 0; }

/* ---- allocation: agent share and cash lines ---- */
.alloc-block { border: 1px dashed var(--border); border-radius: 8px; padding: 10px 12px; margin-top: 12px; }
.alloc-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.alloc-label { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.alloc-label input { width: 90px; }
.alloc-note { font-size: 13px; flex: 1 1 220px; }
.alloc-note.bad, .cash-value.bad { color: var(--danger); }
#ask-agent { margin-left: auto; }
.cash-line { margin-top: 12px; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; }
.cash-line #cash-share-input { width: 90px; margin-left: auto; }
.cash-value { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }
.agent-rows { display: grid; gap: 4px; margin-top: 8px; }
.agent-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.agent-row-name { flex: 1; }
.agent-universe { margin-top: 10px; }
.agent-universe summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 4px 2px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: 13px; }
.chip-remove { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 2px 6px; }
