/* Strategy Backtester - app components on top of styles.css (CI). Tokens only, no raw colors.
   The backtest page is a dashboard: it uses the 1440 px workspace instead of the 1040 px content
   width; the other pages keep the CI widths. */

[hidden] { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.grow { flex: 1; }
.small { font-size: 13px; }
.block { display: block; }
.nowrap { white-space: nowrap; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }
.check input { accent-color: var(--accent); }
.btn.sm { padding: 4px 10px; font-size: 13.5px; }
.btn.xs { padding: 2px 8px; font-size: 12.5px; border-radius: 7px; }
.btn.danger, .btn.primary.danger { background: var(--danger); border-color: var(--danger); color: var(--panel); }
.danger-text { color: var(--danger); }
.btn.disabled { opacity: .45; pointer-events: none; }
a.btn { display: inline-block; text-decoration: none; text-align: center; }
.gain { color: var(--ok); }
.loss { color: var(--danger); }
.warn-text { color: var(--warn); font-weight: 600; }
input[type="date"] { min-height: 38px; }
input[type="search"] { padding: 6px 10px; }

/* ---------- Header ---------- */
.app-top { padding-bottom: 10px; }
.app-top.wide { max-width: 1440px; }
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; }
.brand img { width: 30px; height: 30px; border-radius: 8px; display: block; }
.userbox { display: flex; align-items: center; gap: 8px; margin-left: auto; min-width: 0; }
.user-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.btn.icon-btn { display: inline-flex; align-items: center; justify-content: center; padding: 7px 9px; align-self: stretch; }
.btn.icon-btn.xs { padding: 3px 5px; align-self: auto; }

/* ---------- Pages ---------- */
.page { max-width: 1040px; }
.page.wide-page { max-width: 1280px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin: 6px 0 4px; }
.page-head h1 { font-size: clamp(26px, 4vw, 34px); }
.disclaimer {
  font-size: 13px; color: var(--muted); border-left: 3px solid var(--info); padding: 4px 0 4px 10px;
}

/* ---------- Workspace (backtest page) ---------- */
.workspace {
  max-width: 1440px; margin: 0 auto; padding: 6px 16px 28px;
  display: grid; grid-template-columns: 370px minmax(0, 1fr); gap: 16px; align-items: start;
}
.config {
  position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto; padding: 0;
  display: flex; flex-direction: column;
}
.strat-bar { display: flex; gap: 8px; align-items: flex-end; padding: 14px 16px 6px; }
.strat-bar select { width: 100%; }
.strat-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 0 16px 10px; border-bottom: 1px solid var(--border); }
.strat-actions #dirty { margin-left: auto; }
.config-form { flex: 1; }
.sec { border-bottom: 1px solid var(--border); }
.sec summary {
  cursor: pointer; padding: 11px 16px; font-weight: 700; font-size: 14.5px; list-style: none;
  display: flex; align-items: center; justify-content: space-between;
}
.sec summary::-webkit-details-marker { display: none; }
.sec summary::after { content: "+"; color: var(--muted); font-weight: 600; }
.sec[open] summary::after { content: "\2212"; }
.sec summary:hover { background: var(--panel-2); }
.sec-body { padding: 2px 16px 14px; display: grid; gap: 10px; }
.sec-body .field input, .sec-body .field select { width: 100%; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.three { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.symbol-name { margin-top: -4px; min-height: 1em; }
.quick-range { display: flex; flex-wrap: wrap; gap: 4px; }
.match-row { display: flex; align-items: center; gap: 6px; font-size: 14px; flex-wrap: wrap; }
.match-row select { padding: 4px 8px; }
.rules { display: grid; gap: 8px; }
.rule { border: 1px solid var(--border); background: var(--panel-2); border-radius: 10px; padding: 8px 10px 10px; display: grid; gap: 6px; }
.rule-head { display: flex; align-items: center; justify-content: space-between; }
.rule-n { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.operand { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.operand select, .op-select { padding: 5px 8px; font-size: 14px; background: var(--panel); }
.op-ind { flex: 1 1 130px; }
.op-select { width: 100%; font-weight: 600; }
.op-value { width: 100px; padding: 5px 8px; background: var(--panel); }
.mini { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.mini input { width: 64px; padding: 5px 6px; font-size: 14px; background: var(--panel); }
.add-rule { justify-self: start; }
.seg button { font-weight: 600; }
.run-bar {
  position: sticky; bottom: 0; background: var(--panel); border-top: 1px solid var(--border);
  padding: 12px 16px 14px; display: grid; gap: 8px;
}
.run-btn { width: 100%; padding: 10px 14px; font-size: 15.5px; }

.results { display: grid; gap: 14px; min-width: 0; }
.inst-head { display: grid; gap: 4px; padding: 14px 18px; }
.inst-title { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.inst-title h1 { font-size: 26px; }
.tabs { justify-self: start; }
.tabs a[aria-selected="true"] { color: var(--text); }
.view { display: grid; gap: 14px; min-width: 0; }
.warnings { margin: 0; padding: 10px 14px 10px 32px; border-radius: 10px; background: color-mix(in srgb, var(--warn) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent); font-size: 14px; display: grid; gap: 4px; }
.warnings li { color: var(--text); }
.warnings li.outdated { font-weight: 600; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.kpi {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  padding: 12px 14px; display: grid; gap: 2px; min-width: 0;
}
.kpi-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.kpi-value { font-size: 24px; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 12.5px; color: var(--muted); }

.chart-panel { display: grid; gap: 6px; }
.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 6px 16px; flex-wrap: wrap; }
.chart-head h2 { font-size: 16px; }
.chart { width: 100%; position: relative; }
.chart-lg { height: 420px; }
.chart-md { height: 300px; }
.chart-sm { height: 150px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 18px; }
.lg-time { font-weight: 600; color: var(--text); }
.lg-item { display: inline-flex; align-items: center; gap: 5px; }
.lg-item b { color: var(--text); font-weight: 600; }
.lg-swatch { width: 12px; height: 3px; border-radius: 2px; display: inline-block; flex: none; }
.osc { display: grid; gap: 4px; padding-top: 6px; border-top: 1px solid var(--border); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table-wrap.tall { max-height: 620px; overflow-y: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.data caption { text-align: left; caption-side: bottom; padding-top: 8px; }
.data th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 7px 10px;
  border-bottom: 1px solid var(--border); background: var(--panel); white-space: nowrap; }
.data thead th { position: sticky; top: 0; z-index: 1; }
.data tbody th { text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--text); font-weight: 600; }
.data td { padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
.data tr:last-child td, .data tr:last-child th { border-bottom: 0; }
.data .num { text-align: right; }
.data th.grp { text-align: center; border-bottom: 0; }
.data tbody tr:hover td, .data tbody tr:hover th { background: var(--panel-2); }
.data td.best { font-weight: 800; }
.data tr.skipped td { color: var(--muted); }
.data tr.info-row td { color: var(--muted); white-space: normal; }
.metrics-table { max-width: 860px; }
.r-name { font-weight: 600; max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
.pick { display: flex; align-items: center; gap: 6px; }
.pick input { accent-color: var(--accent); width: 16px; height: 16px; }
tr.picked td { background: color-mix(in srgb, var(--accent) 7%, transparent); }
.col-head { display: inline-flex; align-items: center; gap: 6px; text-transform: none; letter-spacing: 0; color: var(--text); font-size: 13.5px; }
.row-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Optimize */
.opt-setup { display: grid; gap: 10px; }
.opt-params { display: grid; gap: 10px; }
.opt-row { display: grid; grid-template-columns: minmax(0, 1fr) 100px 100px 90px auto; gap: 10px; align-items: end;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel-2); }
.opt-row .field input, .opt-row .field select { width: 100%; }
.opt-row > .btn { margin-bottom: 4px; }
.opt-controls { align-items: flex-end; gap: 10px 16px; }

/* Login */
.login-main { display: grid; place-items: center; min-height: calc(100vh - 140px); }
.login-card { max-width: 440px; width: 100%; text-align: center; display: grid; gap: 12px; justify-items: center; padding: 30px 26px; }
.login-mark { border-radius: 16px; }
.login-btn { width: 100%; padding: 10px 14px; }
.setup { text-align: left; width: 100%; }
.setup-list { margin: 6px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; }

/* Users */
.add-user { align-items: flex-end; }
.add-user .field { flex: 1 1 260px; }
.add-user .check { padding-bottom: 8px; }
table.users { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.users th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 10px; border-bottom: 1px solid var(--border); }
.users td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.users tr:last-child td { border-bottom: 0; }
.u-email { word-break: break-all; }
.u-actions { white-space: nowrap; text-align: right; }
.u-actions .btn + .btn { margin-left: 6px; }

/* Dialogs + toast */
dialog.dlg { width: min(480px, calc(100vw - 32px)); color: var(--text); padding: 20px 22px; }
dialog.dlg::backdrop { background: rgba(0, 0, 0, .35); }
.dlg-form { display: grid; gap: 12px; }
.dlg-fields { display: grid; gap: 12px; }
.dlg .field input, .dlg .field select { width: 100%; }
.tpl-list { display: grid; gap: 6px; max-height: 50vh; overflow-y: auto; }
.tpl { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.tpl:hover { border-color: var(--accent); }
.tpl input { margin-top: 4px; accent-color: var(--accent); }
.tpl > span { display: grid; gap: 2px; }
.tpl:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; box-shadow: var(--shadow);
  font-weight: 600; font-size: 14px; max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); color: var(--panel); }

/* ---------- AI assistant ---------- */
.assistant { padding: 0; }
.assistant > summary {
  cursor: pointer; padding: 11px 16px; font-weight: 700; font-size: 14.5px; list-style: none;
  display: flex; align-items: baseline; gap: 10px; justify-content: space-between;
}
.assistant > summary::-webkit-details-marker { display: none; }
.assistant > summary::after { content: "+"; color: var(--muted); font-weight: 600; }
.assistant[open] > summary::after { content: "2"; }
.assistant > summary > span:nth-child(2) { margin-right: auto; font-weight: 400; }
.assist-body { padding: 0 16px 14px; display: grid; gap: 10px; }
.chat-log { display: grid; gap: 8px; max-height: 340px; overflow-y: auto; align-content: start; padding: 2px; }
.chat-msg { max-width: 88%; padding: 8px 12px; border-radius: 12px; font-size: 14.5px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-msg.user { justify-self: end; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.chat-msg.assistant { justify-self: start; background: var(--panel-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.chat-msg.note { justify-self: start; color: var(--muted); background: transparent; border: 1px dashed var(--border); }
.chat-msg.error { justify-self: start; color: var(--danger); border: 1px solid var(--danger); background: transparent; font-weight: 600; }
.chat-card {
  justify-self: start; display: grid; gap: 6px; padding: 10px 12px; border: 1px solid var(--accent); border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 8%, transparent); max-width: 88%;
}
.chat-card b { overflow-wrap: anywhere; }
.chat-card .row { display: flex; gap: 6px; flex-wrap: wrap; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; }
.chat-form textarea {
  flex: 1; min-width: 0; font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; resize: vertical; min-height: 44px; max-height: 200px;
}
.chat-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- Settings page ---------- */
.settings-sec { display: grid; gap: 10px; padding: 16px 18px; }
.settings-sec h2 { font-size: 18px; margin: 0; }
.settings-form { display: grid; gap: 12px; max-width: 520px; }
.settings-form .field input { width: 100%; }
.settings-form input[type="password"] {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-width: 0; max-width: 100%;
}
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.chat-msg a { color: inherit; font-weight: 700; }

/* ---------- Narrow screens ---------- */
@media (max-width: 980px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .config { position: static; max-height: none; overflow: visible; }
  .run-bar { position: static; }
  .opt-row { grid-template-columns: 1fr 1fr 1fr; }
  .opt-row > .field.grow { grid-column: 1 / -1; }
}
@media (max-width: 600px) {
  .user-name { display: none; }
  .page-head { align-items: flex-start; }
  .chart-lg { height: 320px; }
  .chart-md { height: 240px; }
  .three { grid-template-columns: 1fr 1fr; }
  .opt-row { grid-template-columns: 1fr 1fr; }
}
