:root {
  --bg: #F1F4F2; --surface: #FFFFFF; --surface-2: #F6F8F7; --ink: #16211D; --muted: #5D6B66;
  --line: #D9E0DC; --line-strong: #BFCAC5; --brand: #1F4A40; --brand-ink: #FFFFFF; --brand-soft: #E2EDE8;
  --gain: #11744F; --gain-soft: #E1F2EA; --loss: #BF3328; --loss-soft: #FBE7E4;
  --warn: #965D08; --warn-soft: #FBF0DA; --slot: #EAF0FB; --slot-line: #6F86C6; --slot-ink: #233A7A;
  --focus: #2F5FD0;
  --font: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --r-panel: 12px; --r-ctl: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101614; --surface: #172019; --surface-2: #1C2622; --ink: #E4ECE8; --muted: #95A59F;
    --line: #2A3632; --line-strong: #3B4A45; --brand: #8FD3B6; --brand-ink: #0E1A16; --brand-soft: #1F2F29;
    --gain: #4CC38A; --gain-soft: #15291F; --loss: #F0766B; --loss-soft: #2E1A18;
    --warn: #E2A84B; --warn-soft: #2C2312; --slot: #1D2740; --slot-line: #6F86C6; --slot-ink: #C3D0F5;
    --focus: #8DA8F0; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101614; --surface: #172019; --surface-2: #1C2622; --ink: #E4ECE8; --muted: #95A59F;
  --line: #2A3632; --line-strong: #3B4A45; --brand: #8FD3B6; --brand-ink: #0E1A16; --brand-soft: #1F2F29;
  --gain: #4CC38A; --gain-soft: #15291F; --loss: #F0766B; --loss-soft: #2E1A18;
  --warn: #E2A84B; --warn-soft: #2C2312; --slot: #1D2740; --slot-line: #6F86C6; --slot-ink: #C3D0F5;
  --focus: #8DA8F0; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font);
  font-variant-numeric: tabular-nums; }
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 600; }
h2 { font-size: 18px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }
p { margin: 0; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.gain { color: var(--gain); } .loss { color: var(--loss); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; padding: 22px 16px; border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 22px; background: var(--surface); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; padding: 0 6px; }
.brand img { width: 28px; height: 28px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--r-ctl);
  text-decoration: none; color: var(--muted); font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--brand-soft); color: var(--ink); }
.nav svg { width: 18px; height: 18px; flex: none; }
.side-status { margin-top: auto; font-size: 13px; color: var(--muted); padding: 0 6px; display: grid; gap: 6px; }
.main { padding: 28px clamp(16px, 4vw, 44px) 80px; max-width: 1180px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px;
  flex-wrap: wrap; margin-bottom: 22px; }
.page-head p { margin-top: 6px; max-width: 62ch; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 20; top: auto; bottom: 0; left: 0; right: 0; height: auto;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--line);
    flex-direction: row; }
  .brand, .side-status { display: none; }
  .nav { flex-direction: row; width: 100%; justify-content: space-around; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; flex: 1; text-align: center; }
  .main { padding: 18px 16px 110px; }
  h1 { font-size: 22px; }
}

/* ---------- surfaces ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 18px; }
.panel + .panel, .stack > * + * { margin-top: 16px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
.grid-2 > .panel + .panel { margin-top: 0; }
@media (max-width: 980px) { .grid-2 { grid-template-columns: 1fr; } .grid-2 > .panel + .panel { margin-top: 0; } }

/* ---------- status hero ---------- */
.hero { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr); gap: 24px; align-items: stretch; }
.hero .status-line { font-size: 19px; line-height: 1.45; max-width: 34ch; }
.hero .status-line strong { font-weight: 600; }
.figures { display: flex; gap: 28px; flex-wrap: wrap; margin-top: 18px; }
.fig .v { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; }
.fig .l { font-size: 13px; color: var(--muted); }
.hero-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 20px; }
@media (max-width: 760px) { .hero { grid-template-columns: 1fr; } .hero .status-line { font-size: 17px; } }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.dot.ok { background: var(--gain); } .dot.bad { background: var(--loss); } .dot.idle { background: var(--line-strong); }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600; background: var(--surface-2); border: 1px solid var(--line); white-space: nowrap; }
.badge.live { background: var(--loss-soft); color: var(--loss); border-color: transparent; }
.badge.paper { background: var(--brand-soft); color: var(--ink); border-color: transparent; }
.badge.dry { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.paused { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

/* ---------- buttons & forms ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 38px;
  padding: 0 14px; border-radius: var(--r-ctl); border: 1px solid var(--line-strong); background: var(--surface);
  font-weight: 600; font-size: 14px; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--loss); border-color: color-mix(in srgb, var(--loss) 45%, var(--line)); }
.btn.danger.solid { background: var(--loss); color: #fff; border-color: var(--loss); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

label.field { display: grid; gap: 5px; align-content: end; font-weight: 500; font-size: 14px; }
label.field .hint { font-weight: 400; font-size: 13px; color: var(--muted); }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; min-height: 38px; padding: 7px 10px; border: 1px solid var(--line-strong);
  border-radius: var(--r-ctl); background: var(--surface); }
textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px 20px; }
.input-suffix { display: flex; align-items: center; }
.input-suffix input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-suffix span { min-height: 38px; display: flex; align-items: center; padding: 0 10px; border: 1px solid var(--line-strong);
  border-left: 0; border-radius: 0 var(--r-ctl) var(--r-ctl) 0; background: var(--surface-2); color: var(--muted); font-size: 14px; }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 500; }
.switch input { appearance: none; width: 40px; height: 24px; border-radius: 999px; background: var(--line-strong);
  position: relative; cursor: pointer; transition: background .15s; flex: none; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked { background: var(--brand); }
.switch input:checked::after { transform: translateX(16px); }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--r-ctl); overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 7px 14px; cursor: pointer; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 13px; padding: 6px 10px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; }
.sym { font-weight: 600; }
.empty { padding: 26px 8px; text-align: center; color: var(--muted); }
.empty .btn { margin-top: 12px; }

/* cards for small screens */
.pos-list { display: grid; gap: 10px; }
.pos { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.pos:last-child { border-bottom: 0; }
.pos .sym { font-size: 16px; }
.riskbar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--surface-2); position: relative; margin-top: 8px; overflow: hidden; }
.riskbar i { position: absolute; top: 0; bottom: 0; background: var(--loss); opacity: .7; }
.riskbar b { position: absolute; top: 0; bottom: 0; background: var(--gain); opacity: .8; }

/* ---------- strategy cards & builder ---------- */
.strat { display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: start; }
.strat + .strat { margin-top: 12px; }
.strat .order { display: flex; flex-direction: column; gap: 2px; }
.strat .order button { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; width: 30px; height: 26px; cursor: pointer; color: var(--muted); }
.strat .order button:hover { color: var(--ink); }
.strat .order .prio { text-align: center; font-size: 12px; color: var(--muted); }
.strat-body h3 { font-size: 16px; }
.strat-rules { margin-top: 8px; display: grid; gap: 4px; font-size: 14px; }
.strat-rules div { display: grid; grid-template-columns: 64px 1fr; gap: 10px; }
.strat-rules span:first-child { color: var(--muted); }
.strat-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
.strat-actions .row { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 680px) {
  .strat { grid-template-columns: 1fr; }
  .strat .order { flex-direction: row; align-items: center; order: 3; }
  .strat-actions { align-items: flex-start; order: 2; }
  .strat-actions .row { justify-content: flex-start; }
}

.sentence { font-size: 17px; line-height: 2.5; }
.rule-block + .rule-block { margin-top: 26px; }
.rule-block > h3 { margin-bottom: 4px; }
.rule-block > p.muted { font-size: 14px; margin-bottom: 8px; }
.cond { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--surface-2); }
.cond + .cond { margin-top: 8px; }
.cond .joiner { color: var(--muted); font-size: 14px; min-width: 32px; }
.slot { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; min-width: 0; background: var(--slot); color: var(--slot-ink);
  border-bottom: 2px dotted var(--slot-line); border-radius: 6px 6px 3px 3px; padding: 2px 4px; }
.slot select, .slot input { border: 0; background: transparent; color: inherit; min-height: 30px; padding: 2px 4px;
  font-weight: 600; width: auto; }
.slot select { max-width: min(250px, 62vw); min-width: 0; text-overflow: ellipsis; }
.slot input.wide[type=number] { width: 96px; }
.slot input[type=number] { width: 66px; }
.slot .sub { font-weight: 400; font-size: 13px; opacity: .8; }
.cond .remove { margin-left: auto; }
.add-cond { margin-top: 10px; }
.preview { background: var(--brand-soft); border-radius: var(--r-panel); padding: 16px 18px; font-size: 15px; }
.preview strong { font-weight: 600; }
.preview div + div { margin-top: 6px; }
.templates { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.template { text-align: left; border: 1px solid var(--line); border-radius: var(--r-panel); background: var(--surface);
  padding: 14px; cursor: pointer; display: grid; gap: 6px; }
.template:hover { border-color: var(--brand); }
.template h3 { font-size: 15px; }
.template p { font-size: 13.5px; color: var(--muted); }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid line { stroke: var(--line); }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .bot { stroke: var(--brand); stroke-width: 2; fill: none; }
.chart .bench { stroke: var(--muted); stroke-width: 1.5; fill: none; stroke-dasharray: 4 3; }
.chart .area { fill: var(--brand); opacity: .08; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 16px; height: 3px; vertical-align: middle; margin-right: 6px; background: var(--brand); }
.legend i.b { background: repeating-linear-gradient(90deg, var(--muted) 0 4px, transparent 4px 7px); }
.chart-empty { height: 160px; display: grid; place-items: center; color: var(--muted); font-size: 14px;
  border: 1px dashed var(--line-strong); border-radius: var(--r-ctl); text-align: center; padding: 16px; }

/* ---------- backtest ---------- */
.versus { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: var(--line);
  border: 1px solid var(--line); border-radius: var(--r-ctl); overflow: hidden; }
.versus > div { background: var(--surface); padding: 12px; }
.versus .l { font-size: 13px; color: var(--muted); }
.versus .v { font-size: 20px; font-weight: 600; }
.versus .b { font-size: 13px; color: var(--muted); }
@media (max-width: 640px) { .versus { grid-template-columns: repeat(2, 1fr); } }
.verdict { padding: 14px 16px; border-radius: var(--r-ctl); font-size: 15px; }
.verdict.good { background: var(--gain-soft); }
.verdict.bad { background: var(--loss-soft); }
.verdict.meh { background: var(--warn-soft); }
.check-list { display: grid; gap: 8px; }
.check-list label { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: var(--r-ctl); cursor: pointer; }
.check-list input { margin-top: 4px; width: 16px; height: 16px; accent-color: var(--brand); }
.job-out { background: var(--surface-2); border-radius: var(--r-ctl); padding: 12px; font-size: 13px; white-space: pre-wrap;
  max-height: 260px; overflow: auto; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line-strong); border-top-color: var(--brand); border-radius: 50%;
  animation: spin .8s linear infinite; display: inline-block; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.list-row { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line);
  text-decoration: none; align-items: center; }
.list-row:last-child { border-bottom: 0; }
.list-row:hover .t { text-decoration: underline; }

/* ---------- log ---------- */
.log { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; line-height: 1.55;
  max-height: 520px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.log .e { color: var(--loss); } .log .w { color: var(--warn); }

/* ---------- settings ---------- */
.settings-section { display: grid; grid-template-columns: 240px 1fr; gap: 24px; padding: 24px 0; border-top: 1px solid var(--line); }
.settings-section:first-of-type { border-top: 0; padding-top: 4px; }
.settings-section > div:first-child p { font-size: 14px; color: var(--muted); margin-top: 6px; }
@media (max-width: 860px) { .settings-section { grid-template-columns: 1fr; gap: 12px; } }
.callout { border-left: 3px solid var(--warn); background: var(--warn-soft); padding: 10px 14px; border-radius: 0 var(--r-ctl) var(--r-ctl) 0; font-size: 14px; }
.callout.info { border-left-color: var(--brand); background: var(--brand-soft); }
ol.steps { margin: 8px 0 0; padding-left: 20px; font-size: 14px; display: grid; gap: 4px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login form { width: min(360px, 100%); display: grid; gap: 14px; }
.login img { width: 44px; height: 44px; }

/* ---------- toast & modal ---------- */
#toast { position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; font-weight: 500;
  transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--loss); color: #fff; }
@media (max-width: 860px) { #toast { bottom: 92px; } }
.modal-back { position: fixed; inset: 0; background: rgba(10, 16, 14, .5); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--r-panel); padding: 22px; width: min(460px, 100%); display: grid; gap: 14px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; }
