:root {
  --bg: #f4f6fa; --surface: #ffffff; --surface-2: #f0f3f8; --border: #e2e7ef;
  --text: #121826; --text-2: #4a5468; --muted: #7b8597;
  --accent: #3b6cf6; --accent-weak: #e6edff;
  --up: #0f9d58; --up-weak: #e3f5ec; --down: #d93a3a; --down-weak: #fbe8e8;
  --warn: #b26b00; --warn-weak: #fff3dc;
  --grid: #eef1f6; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f17; --surface: #121823; --surface-2: #19202d; --border: #242d3c;
    --text: #e8ecf3; --text-2: #b3bccb; --muted: #7d8899;
    --accent: #6b8cff; --accent-weak: #1c2540;
    --up: #2fbf71; --up-weak: #12291e; --down: #f0605d; --down-weak: #2d1617;
    --warn: #f0b04a; --warn-weak: #2c2212; --grid: #1a212d; --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0f17; --surface: #121823; --surface-2: #19202d; --border: #242d3c;
  --text: #e8ecf3; --text-2: #b3bccb; --muted: #7d8899;
  --accent: #6b8cff; --accent-weak: #1c2540;
  --up: #2fbf71; --up-weak: #12291e; --down: #f0605d; --down-weak: #2d1617;
  --warn: #f0b04a; --warn-weak: #2c2212; --grid: #1a212d; --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.4 Inter, system-ui, sans-serif; min-height: 100vh; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num, .price, td.n, .mono { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.up { color: var(--up); } .down { color: var(--down); }

/* top bar */
.top { position: sticky; top: 0; z-index: 20; display: flex; gap: 16px; align-items: center; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 8px; white-space: nowrap; font-size: 15px; }
.logo { width: 24px; height: 24px; border-radius: 7px; background: var(--accent)
  url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><path d='M7 21l6-6 4 4 8-9' stroke='white' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>") center/18px no-repeat; }
.badge { font-size: 10px; font-weight: 700; letter-spacing: .06em; padding: 2px 6px; border-radius: 4px;
  background: var(--warn-weak); color: var(--warn); }
.search { position: relative; flex: 1; max-width: 420px; }
.search input { width: 100%; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); outline: none; }
.search input:focus { border-color: var(--accent); }
.results { position: absolute; top: calc(100% + 4px); left: 0; right: 0; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.18); overflow: hidden; max-height: 380px; overflow-y: auto; }
.results div { display: flex; justify-content: space-between; gap: 8px; padding: 9px 12px; cursor: pointer; }
.results div:hover, .results div.act { background: var(--surface-2); }
.results b { font-weight: 600; } .results span { color: var(--muted); font-size: 12px; text-align: right; }
.wallets { display: flex; gap: 8px; }
.indices { display: flex; gap: 14px; margin-left: auto; font-size: 12px; white-space: nowrap; }
.idx b { font-weight: 600; margin: 0 2px; }
.wallet { padding: 4px 10px; border-radius: 8px; border: 1px solid var(--border); font-size: 12px; line-height: 1.3; white-space: nowrap; }
.wallet b { font-size: 13px; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.pill { font-size: 12px; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.pill.open { background: var(--up-weak); color: var(--up); }
.icon { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }

/* layout */
.grid { display: grid; grid-template-columns: 270px minmax(0,1fr) 310px; gap: 12px; padding: 12px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.panel-h { display: flex; justify-content: space-between; padding: 12px 14px; font-weight: 600; border-bottom: 1px solid var(--border); }
.center { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

/* watchlist */
.watch { position: sticky; top: 66px; max-height: calc(100vh - 80px); overflow: auto; }
#watchlist { list-style: none; margin: 0; padding: 4px 0; }
#watchlist li { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 9px 14px; cursor: pointer; position: relative; border-left: 3px solid transparent; }
#watchlist li:hover { background: var(--surface-2); }
#watchlist li.sel { background: var(--accent-weak); border-left-color: var(--accent); }
#watchlist .s { font-weight: 600; } #watchlist .nm { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
#watchlist .p { text-align: right; font-family: "JetBrains Mono", monospace; font-size: 13px; }
#watchlist .c { text-align: right; font-size: 12px; font-family: "JetBrains Mono", monospace; }
#watchlist .x { position: absolute; right: 4px; top: 4px; display: none; border: 0; background: none; color: var(--muted); font-size: 14px; }
#watchlist li:hover .x { display: block; }
#watchlist li.flash-up .p { animation: fu .8s; } #watchlist li.flash-down .p { animation: fd .8s; }
@keyframes fu { from { background: var(--up-weak); } } @keyframes fd { from { background: var(--down-weak); } }

/* quote header */
.quote { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 28px; padding: 14px 18px; }
.q-sym { font-size: 20px; font-weight: 700; } .q-name { font-size: 13px; max-width: 300px; }
.price { font-size: 26px; font-weight: 500; }
.chg { font-family: "JetBrains Mono", monospace; font-size: 13px; }
.q-stats { display: grid; grid-template-columns: repeat(3, auto); gap: 4px 22px; margin: 0 0 0 auto; font-size: 12px; }
.q-stats div { display: flex; gap: 8px; justify-content: space-between; }
.q-stats dt { color: var(--muted); } .q-stats dd { margin: 0; font-family: "JetBrains Mono", monospace; }

/* chart */
.chart-wrap { padding: 10px 12px 6px; }
.chart-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
#chart { height: 420px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; padding: 4px 10px; border-radius: 6px; font-size: 12px; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.seg.full { display: flex; } .seg.full button { flex: 1; padding: 6px; }

/* positions / orders */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); padding: 0 10px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 12px 10px; color: var(--text-2); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabs span { font-size: 11px; background: var(--surface-2); border-radius: 99px; padding: 1px 6px; }
.tab-body { overflow-x: auto; min-height: 140px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 9px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 12px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th.n, td.n { text-align: right; }
tr.click { cursor: pointer; } tr.click:hover td { background: var(--surface-2); }
.empty { padding: 34px; text-align: center; color: var(--muted); }
.tag { font-size: 11px; padding: 2px 7px; border-radius: 4px; background: var(--surface-2); color: var(--text-2); font-weight: 500; }
.tag.buy { background: var(--up-weak); color: var(--up); } .tag.sell { background: var(--down-weak); color: var(--down); }
.tag.filled { background: var(--accent-weak); color: var(--accent); }
.mini { border: 1px solid var(--border); background: var(--surface); padding: 3px 9px; border-radius: 6px; font-size: 12px; }
.mini:hover { border-color: var(--down); color: var(--down); }
.news-item { display: block; padding: 12px 16px; border-bottom: 1px solid var(--grid); color: inherit; text-decoration: none; }
.news-item:hover { background: var(--surface-2); }
.news-item b { font-weight: 600; display: block; margin-bottom: 3px; }

/* order ticket */
.ticket { position: sticky; top: 66px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.side-toggle { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.side-toggle button { border: 0; background: none; padding: 8px; border-radius: 8px; font-weight: 600; color: var(--text-2); }
.side-toggle .buy.on { background: var(--up); color: #fff; } .side-toggle .sell.on { background: var(--down); color: #fff; }
.t-sym { font-size: 15px; }
.lbl { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 4px; font-weight: 500; }
.lbl.sm { font-size: 11px; color: var(--muted); }
.lbl-row { display: flex; justify-content: space-between; align-items: baseline; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ticket input[type=number], .set-grid input { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
  font-family: "JetBrains Mono", monospace; outline: none; }
.ticket input:focus { border-color: var(--accent); }
.stepper { display: flex; } .stepper input { border-radius: 0 !important; text-align: center; -moz-appearance: textfield; }
.stepper button { width: 32px; border: 1px solid var(--border); background: var(--surface); }
.stepper button:first-child { border-radius: 8px 0 0 8px; } .stepper button:last-child { border-radius: 0 8px 8px 0; }
#bracket { display: flex; flex-direction: column; gap: 8px; padding: 10px; border-radius: 10px; background: var(--surface-2); }
#bracket input { background: var(--surface); }
.link { border: 0; background: none; color: var(--accent); font-size: 12px; font-weight: 600; padding: 0; }
.ghost { border: 1px solid var(--border); background: var(--surface); padding: 8px 12px; border-radius: 8px; font-weight: 500; }
.full { width: 100%; }
.summary { display: grid; grid-template-columns: 1fr auto; gap: 5px 10px; margin: 0; font-size: 13px; }
.summary dt { color: var(--muted); } .summary dd { margin: 0; text-align: right; font-family: "JetBrains Mono", monospace; }
.check { font-size: 12px; border-radius: 8px; padding: 9px 10px; display: none; }
.check.pass { display: block; background: var(--up-weak); color: var(--up); }
.check.block { display: block; background: var(--down-weak); color: var(--down); }
.check b { display: block; margin-bottom: 3px; } .check ul { margin: 0; padding-left: 16px; }
.check .notes { color: var(--text-2); margin-top: 4px; }
.place { border: 0; padding: 11px; border-radius: 10px; font-weight: 700; color: #fff; font-size: 15px; }
.place.buy { background: var(--up); } .place.sell { background: var(--down); }
.place:disabled { opacity: .5; cursor: not-allowed; }
.danger { border: 1px solid var(--down); color: var(--down); background: none; padding: 8px 12px; border-radius: 8px; }
.fine { margin: 0; text-align: center; }

/* dialogs */
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 20px; width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(5,10,20,.55); }
dialog h3 { margin: 0 0 10px; }
.ticket-pre { background: var(--surface-2); border-radius: 10px; padding: 12px; font: 12.5px/1.55 "JetBrains Mono", monospace; white-space: pre-wrap; margin: 0 0 14px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.dlg-actions .place { padding: 9px 16px; font-size: 14px; }
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 16px; }
.set-grid label { font-size: 12px; color: var(--text-2); display: flex; flex-direction: column; gap: 4px; }
.check-row { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.backup-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0 0 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.backup-row[hidden] { display: none; }
.backup-row p { flex-basis: 100%; margin: 0; }

#toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; max-width: 340px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.25); animation: tin .2s; }
.toast.err { background: var(--down); color: #fff; } .toast.ok { background: var(--up); color: #fff; }
@keyframes tin { from { transform: translateY(8px); opacity: 0; } }

@media (max-width: 1150px) {
  .grid { grid-template-columns: 240px minmax(0,1fr); }
  .ticket { grid-column: 1 / -1; position: static; }
  .wallets { display: none; }
  .indices { display: none; }
}
/* indicators */
.chart-box { position: relative; }
.vp-canvas { position: absolute; top: 0; left: 0; z-index: 2; pointer-events: none; }
.legend { position: absolute; top: 4px; left: 6px; right: 80px; z-index: 3; display: flex; flex-wrap: wrap; gap: 2px 12px;
  font-size: 11.5px; color: var(--text-2); pointer-events: none; }
.legend b { color: var(--text); font-family: "JetBrains Mono", monospace; font-weight: 500; }
.legend i, .ind-menu i, .pane-lbl i.sw { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
.pane { position: relative; height: 120px; border-top: 1px solid var(--border); margin-top: 4px; }
.pane[hidden] { display: none; }
.pane-lbl { position: absolute; top: 3px; left: 6px; z-index: 3; font-size: 11.5px; color: var(--text-2); pointer-events: none; }
.pane-lbl b { color: var(--text); font-family: "JetBrains Mono", monospace; font-weight: 500; margin-right: 8px; }
.ind-wrap { position: relative; }
.ghost.sm { padding: 5px 10px; font-size: 12px; }
.ind-menu { position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.2); padding: 6px; min-width: 230px; }
.ind-menu label { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.ind-menu label:hover { background: var(--surface-2); }
.ind-menu i { width: 14px; margin: 0; }
.ind-menu small { margin-left: auto; }

/* tools */
.tool-body { padding: 12px 14px; }
.tool-body[hidden] { display: none; }
.tool-body > .tab-body { margin: 0 -14px -12px; }
.tool-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.tag.sig { background: var(--accent-weak); color: var(--accent); margin: 1px 0; display: inline-block; }
.m-sig { display: none; }
.tm-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 12px; }
.tm-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.tm-form input, .tm-form select { width: 120px; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
  font-family: "JetBrains Mono", monospace; outline: none; }
.tm-form select { width: 130px; font-family: inherit; }
.place.sm { padding: 8px 14px; font-size: 13px; }
.tm-res { max-width: 640px; }
.tm-verdict { font-weight: 600; font-size: 15px; margin-bottom: 10px; }
.tm-bar { display: flex; gap: 2px; height: 22px; border-radius: 6px; overflow: hidden; }
.tm-seg.t, .tm-legend i.t { background: var(--up); } .tm-seg.s, .tm-legend i.s { background: var(--down); }
.tm-seg.n, .tm-legend i.n { background: var(--muted); opacity: .55; }
.tm-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--text-2); margin: 8px 0 12px; }
.tm-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.tm-legend b { color: var(--text); font-family: "JetBrains Mono", monospace; }
.tm-sum { max-width: 640px; }
.tm-sum dd { white-space: normal; }

/* phone app shell: hidden on desktop */
.tabbar, .m-actions { display: none; }

@media (max-width: 760px) {
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .top { flex-wrap: wrap; gap: 10px; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; }
  .brand .badge { display: none; }
  .search { order: 3; max-width: none; flex-basis: 100%; }
  .search input, .ticket input[type=number], .set-grid input { font-size: 16px; } /* stops iOS zoom-on-focus */
  .grid { grid-template-columns: minmax(0,1fr); padding: 12px 16px; gap: 12px; }
  .q-stats { margin: 0; grid-template-columns: repeat(2, auto); }
  #chart { height: 340px; }
  .pane { height: 100px; }
  .legend { right: 64px; font-size: 10.5px; }
  .wallets { display: flex; order: 4; flex-basis: 100%; overflow-x: auto; margin: 0; }
  .indices { display: flex; order: 5; flex-basis: 100%; overflow-x: auto; margin: 0; gap: 16px; }
  .watch, .ticket { position: static; max-height: none; }
  .tab-body { min-height: 50vh; }

  /* one screen per tab */
  body:not([data-view="watch"]) .search, body:not([data-view="watch"]) .indices { display: none; }
  body:not([data-view="portfolio"]) .wallets { display: none; }
  body:not([data-view="watch"]) .watch { display: none; }
  body:not([data-view="chart"]) .quote, body:not([data-view="chart"]) .chart-wrap { display: none; }
  body:not([data-view="portfolio"]) .book { display: none; }
  body:not([data-view="trade"]) .ticket { display: none; }
  body:not([data-view="tools"]) .tools { display: none; }
  body[data-view="watch"] .center, body[data-view="trade"] .center { display: none; }
  #scanOut th:nth-child(4), #scanOut td:nth-child(4), #scanOut th:nth-child(5), #scanOut td:nth-child(5),
  #scanOut th:nth-child(6), #scanOut td:nth-child(6) { display: none; }
  #scanOut th:nth-child(7), #scanOut td:nth-child(7) { display: none; }
  #scanOut td:first-child { white-space: normal; }
  .m-sig { display: flex !important; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
  #scanOut td, #scanOut th { padding: 8px; }
  .tm-form input, .tm-form select { width: 100%; font-size: 16px; }
  .tm-form label { flex: 1 1 30%; }
  .tm-form button { flex: 1 1 45%; }

  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
  .tabbar button { border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 3px;
    font-size: 11px; color: var(--muted); padding: 4px 0; }
  .tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .tabbar button.on { color: var(--accent); font-weight: 600; }
  body[data-view="chart"] .m-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; position: fixed; left: 0; right: 0;
    bottom: calc(62px + env(safe-area-inset-bottom)); padding: 10px 16px; background: linear-gradient(transparent, var(--bg) 35%); z-index: 39; }
  body[data-view="chart"] { padding-bottom: calc(130px + env(safe-area-inset-bottom)); }
  #toasts { bottom: calc(76px + env(safe-area-inset-bottom)); left: 16px; right: 16px; }
  .toast { max-width: none; }
}
