:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2024;
  --muted: #6b7280;
  --border: #e3e6ea;
  --profit: #0f8a4a;
  --loss: #c62828;
  --accent-2: #6250d6;
  --paper: #1f6feb;
  --paper-soft: #e7f0fe;
  --live: #c62828;
  --live-soft: #fdecec;
  --warn: #b26a00;
  /* P&L heatmap: diverging profit/loss with a neutral midpoint */
  --p1: #d6f0df; --p2: #a3dbb8; --p3: #5fbc85; --p4: #1f8a4c;
  --n1: #fbdcdc; --n2: #f3a9a9; --n3: #e26464; --n4: #b42727;
  --hm-none: #eceef1;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242b;
    --text: #e6e8eb;
    --muted: #9aa3ad;
    --border: #2a3038;
    --profit: #34c47c;
    --loss: #f2645a;
    --accent-2: #9085e9;
    --paper: #5b9dff;
    --paper-soft: #14243d;
    --live: #f2645a;
    --live-soft: #3a1717;
    --warn: #f0b44c;
    --p1: #173d27; --p2: #1d5c36; --p3: #258048; --p4: #34c47c;
    --n1: #4a1d1d; --n2: #6e2626; --n3: #a33535; --n4: #f2645a;
    --hm-none: #22272e;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
body.mode-paper { --accent: var(--paper); --accent-soft: var(--paper-soft); }
body.mode-live  { --accent: var(--live);  --accent-soft: var(--live-soft); }
body.mode-backtest { --accent: var(--accent-2); --accent-soft: color-mix(in srgb, var(--accent-2) 14%, var(--surface)); }

.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface);
  border-bottom: 3px solid var(--accent);
}
.brand { font-weight: 700; }
.mode-tabs { display: flex; gap: 4px; }
.tab {
  padding: 6px 14px; border-radius: 999px; text-decoration: none;
  color: var(--muted); border: 1px solid var(--border);
}
.tab.active.tab-paper { background: var(--paper); color: #fff; border-color: var(--paper); }
.tab.active.tab-live  { background: var(--live);  color: #fff; border-color: var(--live); }
.tab.active.tab-backtest { background: var(--accent-2); color: #fff; border-color: var(--accent-2); }
.auth { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.mode-banner {
  position: sticky; top: 0; z-index: 5;
  padding: 8px 16px; font-weight: 700; letter-spacing: .02em;
  background: var(--accent-soft); color: var(--accent);
}
.banner-note { font-weight: 400; margin-left: 8px; }

.container { max-width: 1400px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.container > * { min-width: 0; }   /* wide tables scroll inside their card instead of widening the page */
#bt-results { display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.dashboard { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); grid-auto-flow: row dense; align-items: start; }
.span-2 { grid-column: span 2; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; min-width: 0;
}
.card h2 { font-size: 15px; margin: 0 0 10px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

.controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.controls.compact { gap: 10px; }
.controls label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
select, input[type=date], input[type=month], input[type=number], input[type=text], input[type=time], input:not([type]) {
  font: inherit; padding: 6px 8px; border-radius: 6px; min-width: 0;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}

.multi { position: relative; }
.multi summary {
  cursor: pointer; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); list-style: none; font-size: 13px;
}
.multi summary::-webkit-details-marker { display: none; }
.multi-body {
  position: absolute; z-index: 10; margin-top: 4px; min-width: 260px; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .15); display: grid; gap: 6px;
}
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text); }
.check.strong { font-weight: 600; }

.btn {
  font: inherit; cursor: pointer; padding: 6px 12px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; display: inline-block;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { border-color: var(--loss); color: var(--loss); }
.btn-ghost { background: transparent; }
.btn-small { padding: 2px 8px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.btn-row.end { justify-content: flex-end; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input { flex: 1 1 160px; }

.pill { padding: 3px 10px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); }
.ok { color: var(--profit); }
.bad { color: var(--loss); }
.pos { color: var(--profit); }
.neg { color: var(--loss); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.error { color: var(--loss); min-height: 1em; }
.engine-status { font-weight: 600; margin: 0 0 4px; }

.health { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
.health dt { color: var(--muted); }
.health dd { margin: 0; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; margin-bottom: 10px; }
.tile { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; display: grid; gap: 2px; min-width: 0; }
.tile-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.tile-value { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table td.empty { white-space: normal; padding: 14px 8px; }
.table .actions { display: flex; gap: 4px; }
.row-open td { background: color-mix(in srgb, var(--accent) 6%, transparent); }
.row-alert td { background: color-mix(in srgb, var(--loss) 9%, transparent); }

.badge { padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; border: 1px solid currentColor; }
.badge-ok { color: var(--profit); }
.badge-bad { color: var(--loss); }
.badge-open { color: var(--accent); }
.badge-closed { color: var(--muted); }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab-btn { font: inherit; font-size: 13px; cursor: pointer; padding: 4px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: transparent; color: var(--muted); }
.tab-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.recent { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 4px; font-size: 12px; }
.recent time { color: var(--muted); }

.contrib { display: flex; align-items: center; gap: 8px; min-width: 140px; }
.contrib-bar { height: 8px; border-radius: 4px; background: var(--muted); min-width: 2px; }
.contrib-bar.pos { background: var(--profit); }
.contrib-bar.neg { background: var(--loss); }

.mini-chart { height: 180px; }
.mini-chart.is-empty { display: none; }
.empty-note { padding: 24px 0; text-align: center; }
.price-chart { height: 520px; }
.chart-legend { min-height: 1.4em; margin: 4px 0; font-variant-numeric: tabular-nums; }
.chart-details { display: grid; gap: 4px; margin-top: 6px; }
.chart-details .detail { padding: 6px 8px; background: var(--surface-2); border-radius: 6px; }

.heatmap { display: grid; gap: 3px; }
.hm-row { display: grid; grid-template-columns: repeat(7, 1fr) 1.4fr; gap: 3px; }
.hm-head span { font-size: 11px; color: var(--muted); text-align: center; }
.hm-cell {
  font: inherit; border: 0; border-radius: 4px; min-height: 42px; padding: 3px; cursor: pointer;
  display: grid; align-content: space-between; text-align: left; color: var(--text); background: var(--hm-none);
}
.hm-cell.out { background: transparent; cursor: default; }
.hm-cell.out .hm-day { color: var(--muted); opacity: .5; }
.hm-cell.p1 { background: var(--p1); } .hm-cell.p2 { background: var(--p2); }
.hm-cell.p3 { background: var(--p3); color: #fff; } .hm-cell.p4 { background: var(--p4); color: #fff; }
.hm-cell.n1 { background: var(--n1); } .hm-cell.n2 { background: var(--n2); }
.hm-cell.n3 { background: var(--n3); color: #fff; } .hm-cell.n4 { background: var(--n4); color: #fff; }
.hm-cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.hm-day { font-size: 11px; }
.hm-val { font-size: 11px; font-weight: 650; font-variant-numeric: tabular-nums; }
.hm-week { font-size: 11px; align-self: center; text-align: right; font-variant-numeric: tabular-nums; }
.hm-more { display: inline-block; position: relative; }
.hm-more summary { list-style: none; }
.hm-more summary::-webkit-details-marker { display: none; }
.hm-more input { position: absolute; right: 0; z-index: 5; margin-top: 4px; }

.notifications { display: grid; gap: 6px; }
.note {
  padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); display: flex; gap: 10px;
}
.note.error { border-color: var(--loss); }
.note.warning { border-color: var(--warn); }
.note time { color: var(--muted); font-size: 12px; white-space: nowrap; }

.manual-login { display: flex; gap: 8px; flex-wrap: wrap; }
.manual-login input { flex: 1 1 320px; }

.live-safety { border-color: var(--live); }

dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0; width: min(760px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog-form { padding: 16px; display: grid; gap: 12px; max-height: calc(100vh - 64px); overflow: auto; }
.dialog-form h2 { margin: 0; font-size: 17px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.form-grid label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.form-grid .check { color: var(--text); align-self: end; }
.form-grid .span-2 { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
legend { font-size: 12px; color: var(--muted); padding: 0 4px; }

@media (max-width: 760px) {
  .span-2 { grid-column: auto; }
  .dashboard, .grid { grid-template-columns: minmax(0, 1fr); }
  .auth { margin-left: 0; }
  .price-chart { height: 380px; }
  .hm-cell { min-height: 34px; }
  .hm-val { display: none; }
}

/* Backtest page */
.bt-form { display: grid; gap: 10px; }
.bt-more summary { cursor: pointer; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.bt-note { margin: 10px 0 0; }
.mini-chart.tall { height: 260px; }
.table.wrap td, .table.wrap th { white-space: normal; }
.restart-banner { padding: 10px 16px; background: var(--live-soft); color: var(--loss); border-bottom: 2px solid var(--loss); font-weight: 600; }
.calc { display: grid; gap: 4px; }
.calc-h { font-size: 13px; margin: 10px 0 2px; }
.calc-row { display: grid; grid-template-columns: minmax(150px, 1.1fr) minmax(0, 2fr) minmax(110px, auto); gap: 12px;
  padding: 5px 8px; border-radius: 6px; background: var(--surface-2); font-size: 13px; align-items: baseline; }
.calc-label { font-weight: 600; }
.calc-formula { color: var(--muted); font-variant-numeric: tabular-nums; }
.calc-value { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }
@media (max-width: 760px) { .calc-row { grid-template-columns: 1fr; } .calc-value { text-align: left; } }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: 12px; }
.login-card h1 { font-size: 18px; margin: 0; }
.login-label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.login-label input { font: inherit; padding: 8px 10px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
form.inline { display: inline; margin: 0; }
