/* Nifty Algo — layout: single phone-width column, sticky top bar, fixed bottom tabs.
   Ink-blue accent over blue-biased slate neutrals; profit/loss/warning are semantic colours, not the accent. */
:root {
  --bg: #f2f4f8;
  --surface: #ffffff;
  --surface-2: #eaeef4;
  --line: #d9dfe8;
  --fg: #121826;
  --muted: #5a6578;
  --accent: #2c4bd1;
  --accent-fg: #ffffff;
  --accent-soft: #e3e8fb;
  --up: #0d8048;
  --up-soft: #dcf2e6;
  --down: #c42f27;
  --down-soft: #fbe3e1;
  --warn: #9a5c00;
  --warn-soft: #fbefd9;
  --idle: #7a8496;
  --shadow: 0 1px 2px rgba(18, 24, 38, .06), 0 4px 14px rgba(18, 24, 38, .05);
  --font: "Instrument Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --r: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1118; --surface: #151b26; --surface-2: #1c2432; --line: #283244; --fg: #e8edf4; --muted: #8e9aad;
    --accent: #8299ff; --accent-fg: #0d1118; --accent-soft: #232c4d; --up: #3fcf88; --up-soft: #15362a;
    --down: #ff6f64; --down-soft: #3e1d1d; --warn: #f2a93f; --warn-soft: #3a2c15; --idle: #6f7a8d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1118; --surface: #151b26; --surface-2: #1c2432; --line: #283244; --fg: #e8edf4; --muted: #8e9aad;
  --accent: #8299ff; --accent-fg: #0d1118; --accent-soft: #232c4d; --up: #3fcf88; --up-soft: #15362a;
  --down: #ff6f64; --down-soft: #3e1d1d; --warn: #f2a93f; --warn-soft: #3a2c15; --idle: #6f7a8d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
#app { max-width: 560px; margin: 0 auto; min-height: 100vh; }
main { padding: 12px 16px calc(92px + env(safe-area-inset-bottom, 0px)); display: flex; flex-direction: column; gap: 14px; outline: none; }
h2 { font-size: 19px; margin: 4px 0 8px; text-wrap: balance; display: flex; align-items: center; gap: 8px; }
h3 { font-size: 15px; margin: 0 0 8px; }
p { margin: 0; }
code { font-family: var(--mono); font-size: .9em; }
.num, .pnl-big, .pos-pnl, .v, .mk-spot, .clock, td, .meter + *, input[type=number] { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.up { color: var(--up); } .down { color: var(--down); } .flat { color: var(--fg); }
.warn-text { color: var(--warn); }
.right { text-align: right; }
.center { display: grid; place-items: center; gap: 12px; text-align: center; padding: 40px 16px; }
.empty { padding: 8px 2px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.page-head { margin-top: 4px; }

/* top bar + tabs */
.top { position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: .01em; }
.logo { width: 22px; height: 22px; border-radius: 6px; background:
  linear-gradient(135deg, transparent 0 45%, var(--accent-fg) 45% 55%, transparent 55%), var(--accent); }
.top-right { display: flex; align-items: center; gap: 10px; font-size: 12.5px; }
.clock { color: var(--muted); font-family: var(--mono); font-size: 12px; }
.badge { font-weight: 700; font-size: 11px; letter-spacing: .08em; padding: 3px 7px; border-radius: 6px; }
.badge.paper { background: var(--accent-soft); color: var(--accent); }
.badge.live { background: var(--down); color: #fff; }
.badge.demo { background: var(--warn-soft); color: var(--warn); }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--idle); }
.conn.live { background: var(--up); box-shadow: 0 0 0 3px var(--up-soft); }
.conn.connecting { background: var(--warn); }
.conn.offline { background: var(--down); }
.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: repeat(5, 1fr);
  max-width: 560px; margin: 0 auto; background: var(--surface); border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px)); }
.tabs a { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 5px 0; color: var(--muted); text-decoration: none; font-size: 11px; border-radius: 10px; }
.tabs a svg { width: 22px; height: 22px; }
.tabs a.on { color: var(--accent); }
.tabs a:focus-visible { outline: 2px solid var(--accent); }

/* surfaces */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
details.card summary { cursor: pointer; font-weight: 600; }
details.card[open] summary { margin-bottom: 10px; }
.banner { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: var(--r); border: 1px solid; }
.banner svg { width: 22px; height: 22px; flex: none; }
.banner > div { flex: 1; min-width: 0; }
.banner ul { margin: 4px 0 0; padding-left: 18px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }
.banner.bad { background: var(--down-soft); color: var(--down); border-color: color-mix(in srgb, var(--down) 30%, transparent); }
.banner .btn { flex: none; }

/* status + controls */
.status-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.pill { font-weight: 700; font-size: 13px; padding: 5px 11px; border-radius: 999px; display: inline-flex; align-items: center; gap: 7px; }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pill.ok { background: var(--up-soft); color: var(--up); }
.pill.ok::before { animation: pulse 1.6s ease-in-out infinite; }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--down-soft); color: var(--down); }
.pill.idle { background: var(--surface-2); color: var(--muted); }
@keyframes pulse { 50% { opacity: .35; } }
.reason { margin: 10px 0 12px; color: var(--muted); font-size: 13.5px; }
.controls { display: grid; gap: 10px; }
.controls.two { grid-template-columns: 1fr 1fr; }
.closing { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 12px; background: var(--warn-soft); color: var(--warn); font-size: 13.5px; }
.closing.bad { background: var(--down-soft); color: var(--down); }
.closing svg { width: 20px; height: 20px; flex: none; }

/* buttons */
.btn { position: relative; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); color: var(--fg); font: 600 14.5px var(--font); cursor: pointer;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
.btn svg { width: 18px; height: 18px; flex: none; position: relative; }
.btn span { position: relative; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .link:focus-visible, .icon-btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .choice:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.danger { background: var(--down); border-color: var(--down); color: #fff; }
.btn.ghost { background: transparent; }
.btn.big { min-height: 54px; font-size: 16px; }
.btn.wide { width: 100%; }
.small-btn { min-height: 36px; padding: 0 12px; font-size: 13px; }
.pos .small-btn { margin-top: 10px; width: 100%; }
.hold { user-select: none; -webkit-user-select: none; }
.hold .hold-fill { position: absolute; inset: 0; background: rgba(0, 0, 0, .25); transform: scaleX(0); transform-origin: left; }
.hold.holding .hold-fill { transform: scaleX(1); transition: transform 1.2s linear; }
.link { background: none; border: 0; color: var(--accent); font: 600 14px var(--font); cursor: pointer; display: inline-flex; align-items: center; gap: 5px; padding: 6px 0; }
.link svg { width: 16px; height: 16px; }
.icon-btn { background: none; border: 0; color: var(--muted); width: 34px; height: 34px; display: grid; place-items: center; border-radius: 8px; cursor: pointer; flex: none; }
.icon-btn svg { width: 18px; height: 18px; }

/* P&L */
.k { font-size: 11.5px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.pnl-big { font-size: 38px; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; margin: 4px 0 6px; }
.pnl-split { display: flex; gap: 16px; flex-wrap: wrap; color: var(--muted); margin-bottom: 12px; }
.cut-row { display: flex; justify-content: space-between; margin: 10px 0 5px; }
.meter { position: relative; height: 8px; border-radius: 99px; background: var(--surface-2); overflow: visible; }
.meter-fill { height: 100%; border-radius: 99px; transition: width .4s; }
.meter-fill.ok { background: var(--up); } .meter-fill.warn { background: var(--warn); } .meter-fill.bad { background: var(--down); }
.meter-mark { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--fg); opacity: .55; border-radius: 1px; }

/* positions */
.count { font-size: 12px; background: var(--surface-2); color: var(--muted); border-radius: 99px; padding: 1px 8px; font-weight: 600; }
.pos { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin-bottom: 10px; box-shadow: var(--shadow); }
.pos-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.pos-sym { font-weight: 700; font-size: 15px; }
.pos-pnl { font-size: 20px; font-weight: 700; white-space: nowrap; }
.pos-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.pos-grid > div { display: flex; flex-direction: column; }
.pos-grid .k { font-size: 10.5px; }
.v { font-weight: 600; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px; letter-spacing: .02em; vertical-align: 1px; }
.tag.ok { background: var(--up-soft); color: var(--up); } .tag.bad { background: var(--down-soft); color: var(--down); }
.tag.warn { background: var(--warn-soft); color: var(--warn); } .tag.idle { background: var(--surface-2); color: var(--muted); }

/* market */
.mk-top { display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: end; }
.mk-spot { font-size: 24px; font-weight: 700; }
.mk-chg { font-weight: 600; text-align: right; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px; min-width: 0; }
.tile .k { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .v { font-size: 18px; }
.imb { height: 6px; border-radius: 99px; background: var(--down); margin-top: 8px; overflow: hidden; }
.imb-buy { display: block; height: 100%; background: var(--up); transition: width .4s; }
.imb-lbl { display: flex; justify-content: space-between; color: var(--muted); margin-top: 3px; }

/* lists */
.slist { list-style: none; margin: 0; padding: 0; }
.srow { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); cursor: pointer; }
.srow:first-child { border-top: 0; }
.sname { flex: 1; min-width: 0; }
.sdot { width: 8px; height: 8px; border-radius: 50%; background: var(--idle); flex: none; }
.sdot.ok { background: var(--up); } .sdot.bad { background: var(--down); }
.alert-row { display: grid; grid-template-columns: auto auto 1fr; gap: 8px; align-items: baseline; padding: 6px 0; border-top: 1px solid var(--line); font-size: 13px; }
.alert-row:first-of-type { border-top: 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--idle); display: inline-block; flex: none; }
.dot.critical, .dot.error { background: var(--down); } .dot.warn { background: var(--warn); } .dot.success { background: var(--up); } .dot.info { background: var(--accent); }
.log { list-style: none; padding: 0; margin: 0; }
.ev { display: grid; grid-template-columns: auto auto 1fr; gap: 9px; align-items: baseline; padding: 8px 0; border-top: 1px solid var(--line); font-size: 13.5px; }
.ev:first-child { border-top: 0; }
.ev time { font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
.ev.critical span:last-child { color: var(--down); font-weight: 600; }
.ev > span:last-child { min-width: 0; overflow-wrap: anywhere; }
.orders { list-style: none; margin: 0; padding: 0; }
.orders li { padding: 10px 0; border-top: 1px solid var(--line); display: grid; gap: 3px; }
.orders li:first-child { border-top: 0; }
.hist { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.hist th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 4px; }
.hist td { padding: 9px 4px; border-top: 1px solid var(--line); }
.hist .r { text-align: right; }

/* segmented control */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg.wide { display: flex; }
.seg.wide button { flex: 1; }
.seg button { border: 0; background: none; color: var(--muted); font: 600 13px var(--font); padding: 7px 12px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }

/* forms */
.field { display: flex; flex-direction: column; gap: 5px; margin: 0 0 12px; min-width: 0; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.field small { font-size: 11.5px; }
input, select, textarea { font: 15px var(--font); color: var(--fg); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; min-height: 42px; width: 100%; min-width: 0; }
input[disabled] { opacity: .55; }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 50%, calc(100% - 10px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 28px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid2 .field { margin-bottom: 12px; }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 14px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { width: 44px; height: 26px; border-radius: 99px; background: var(--line); position: relative; flex: none; transition: background .2s; }
.switch .track::after { content: ""; position: absolute; left: 3px; top: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: transform .2s; }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* strategies */
.strat.errored { border-color: var(--down); }
.strat-top { display: flex; justify-content: space-between; gap: 10px; cursor: pointer; }
.strat-top h3 { margin: 0 0 2px; }
.summary { margin: 8px 0 0; color: var(--muted); cursor: pointer; }
.summary b, #summary b { color: var(--fg); }
.strat-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.summary-card { background: var(--accent-soft); border-color: transparent; }
#summary p { margin: 6px 0 0; }
.entry h3 { margin: 0; }
.group { display: grid; gap: 10px; margin: 6px 0 4px; }
.group-head { font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.group-head select { width: auto; min-height: 34px; padding: 4px 26px 4px 10px; }
.hold-for { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; }
.hold-for input { width: 64px; min-height: 34px; padding: 4px 8px; text-align: center; }
.cond { display: grid; gap: 8px; padding: 10px; border-radius: 12px; background: var(--surface-2); }
.operand { display: flex; flex-wrap: wrap; gap: 6px; }
.operand > select:first-child { flex: 1 1 100%; }
.operand select, .operand input { flex: 1 1 120px; min-height: 38px; font-size: 14px; }
.mini { display: inline-flex; align-items: center; gap: 4px; flex: 1 1 100px; }
.mini input { flex: 1 1 60px; }
.mini i { font-style: normal; font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.mini-qty { flex: 1.4 1 150px; }
.mini-qty input { min-width: 84px; }
.cond-op { display: flex; gap: 6px; align-items: center; }
.cond-op select { font-weight: 600; }
.then { font-size: 13.5px; font-weight: 600; margin: 10px 0 6px; }
.leg { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px; border-radius: 12px; border: 1px dashed var(--line); margin-bottom: 8px; align-items: center; }
.leg select, .leg input { flex: 1 1 110px; min-height: 38px; font-size: 14px; }
.leg .side-BUY { color: var(--up); font-weight: 700; } .leg .side-SELL { color: var(--down); font-weight: 700; }
.sub { font-weight: 600; font-size: 13.5px; margin: 6px 0; }
.editor-foot { position: sticky; bottom: calc(70px + env(safe-area-inset-bottom, 0px)); display: flex; justify-content: space-between; gap: 10px;
  padding: 10px 0; background: linear-gradient(transparent, var(--bg) 30%); }
.editor-foot .btn.primary { flex: 1; max-width: 240px; margin-left: auto; }
textarea.json { font: 12.5px/1.5 var(--mono); min-height: 360px; resize: vertical; margin: 8px 0 10px; white-space: pre; overflow-x: auto; }
.explain { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; margin: 0 0 12px; display: grid; gap: 6px; }
.explain svg { width: 14px; height: 14px; vertical-align: -2px; }
.diag { font: 11.5px/1.45 var(--mono); background: var(--surface-2); border-radius: 10px; padding: 10px; overflow-x: auto; max-height: 420px; margin-top: 10px; }
.about p + p { margin-top: 8px; }
.diag-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.checks { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 10px; }
.checks li { display: grid; grid-template-columns: 52px 1fr; gap: 10px; align-items: start; }
.checks li > div { min-width: 0; overflow-wrap: anywhere; }
.checks .tag { text-align: center; margin-top: 2px; }
.test-result { margin-top: 12px; padding: 12px; border-radius: 12px; display: grid; gap: 6px; }
.test-result.ok { background: var(--up-soft); } .test-result.bad { background: var(--down-soft); }
.tbl { overflow-x: auto; }
details.raw { margin-top: 12px; }
details.raw summary { cursor: pointer; color: var(--muted); }
details.raw .btn { margin-top: 8px; }

/* sheets + toasts */
#sheet { position: fixed; inset: 0; z-index: 50; pointer-events: none; }
#sheet.open { pointer-events: auto; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(8, 12, 20, .45); opacity: 0; transition: opacity .2s; }
#sheet.open .sheet-backdrop { opacity: 1; }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto; max-height: 88vh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; padding: 10px 18px calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateY(100%); transition: transform .25s ease; display: flex; flex-direction: column; gap: 12px; }
#sheet.open .sheet-panel { transform: none; }
.sheet-grip { width: 40px; height: 4px; border-radius: 99px; background: var(--line); margin: 0 auto 6px; }
.sheet-panel h3 { font-size: 18px; margin: 0; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }
.launch-mode { font-weight: 700; padding: 10px 12px; border-radius: 10px; font-size: 13.5px; }
.launch-mode.paper { background: var(--accent-soft); color: var(--accent); }
.launch-mode.live { background: var(--down); color: #fff; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; font-size: 14px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; text-align: right; font-weight: 600; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 4px; color: var(--muted); }
.choice { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; width: 100%; text-align: left; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--fg); font: 14.5px var(--font); cursor: pointer; margin-bottom: 8px; }
#toasts { position: fixed; left: 0; right: 0; top: calc(64px + env(safe-area-inset-top, 0px)); z-index: 60; display: grid; justify-items: center; gap: 8px; padding: 0 16px; pointer-events: none; }
.toast { max-width: 520px; width: 100%; padding: 11px 14px; border-radius: 12px; background: var(--fg); color: var(--bg); font-size: 14px; box-shadow: 0 8px 30px rgba(0, 0, 0, .2);
  opacity: 0; transform: translateY(-8px); transition: all .25s; white-space: pre-line; }
.toast.show { opacity: 1; transform: none; }
.toast.err { background: var(--down); color: #fff; }
.toast.ok { background: var(--up); color: #fff; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 380px) {
  .grid2 { grid-template-columns: 1fr; }
  .pnl-big { font-size: 32px; }
  .controls.two { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
