:root {
  --bg: #0b0e14;
  --panel: #12161f;
  --panel-2: #181d29;
  --border: #232a3a;
  --text: #e6e9f0;
  --dim: #8b93a7;
  --accent: #4f8cff;
  --green: #22c07a;
  --red: #f0515e;
  --radius: 12px;
}
* { box-sizing: border-box; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, sans-serif;
}
.hidden { display: none !important; }
.dim { color: var(--dim); }
.error { color: var(--red); min-height: 1.2em; font-size: 13px; }
h2 { font-size: 15px; margin: 22px 0 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .06em; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  padding: 9px 16px; border-radius: 9px; cursor: pointer; font-size: 14px;
  transition: filter .15s;
}
.btn:hover { filter: brightness(1.2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-ghost { background: transparent; }
.btn.wide { width: 100%; }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 380px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px; }
.brand { font-size: 22px; font-weight: 700; margin-bottom: 6px; }
.brand.small { font-size: 17px; }
.brand-mark { color: var(--accent); }
.brand-dim { color: var(--dim); font-weight: 400; }
.auth-sub { color: var(--dim); font-size: 13px; margin-bottom: 20px; }
.tabs { display: flex; gap: 6px; margin-bottom: 18px; background: var(--panel-2); padding: 4px; border-radius: 9px; }
.tab { flex: 1; padding: 8px; background: transparent; border: 0; color: var(--dim); border-radius: 7px; cursor: pointer; }
.tab.active { background: var(--panel); color: var(--text); font-weight: 600; }
.auth-form label, .modal-card label { display: block; margin-bottom: 12px; color: var(--dim); font-size: 13px; }
input, select {
  width: 100%; margin-top: 4px; padding: 10px 12px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 14px;
}
input:focus, select:focus { outline: 1px solid var(--accent); }
.auth-form .btn { width: 100%; margin-top: 6px; }

/* ---------- layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; background: var(--panel); border-right: 1px solid var(--border);
  padding: 20px 14px; display: flex; flex-direction: column; gap: 20px;
  position: sticky; top: 0; height: 100vh;
}
.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.nav-btn {
  text-align: left; padding: 10px 12px; border-radius: 9px; border: 0;
  background: transparent; color: var(--dim); cursor: pointer; font-size: 14px;
}
.nav-btn:hover { background: var(--panel-2); color: var(--text); }
.nav-btn.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
.user-chip { font-size: 13px; color: var(--dim); word-break: break-all; }

.main { flex: 1; padding: 0 26px 40px; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 0;
  border-bottom: 1px solid var(--border); margin-bottom: 18px; position: sticky; top: 0;
  background: var(--bg); z-index: 5;
}
.account-select { width: auto; min-width: 200px; margin-top: 0; }
.topbar-stats { margin-left: auto; color: var(--dim); font-size: 13px; display: flex; gap: 18px; }
.topbar-stats b { color: var(--text); }
.view { animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }

/* ---------- stat cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card .k { color: var(--dim); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.card .v { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; white-space: nowrap; }
th { color: var(--dim); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; background: var(--panel); }
td { border-top: 1px solid var(--border); font-variant-numeric: tabular-nums; }
tr:hover td { background: var(--panel-2); }
.pos { color: var(--green); } .neg { color: var(--red); }
.trade-link { color: var(--accent); cursor: pointer; border: 0; background: none; font-size: 14px; }
.status-open { color: var(--accent); } .status-filled { color: var(--green); }
.status-cancelled, .status-rejected { color: var(--dim); }

/* ---------- markets ---------- */
.markets-bar { display: flex; gap: 14px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.markets-bar input { width: 260px; margin-top: 0; }
.class-pills { display: flex; gap: 6px; flex-wrap: wrap; }
.pill {
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--panel); color: var(--dim); cursor: pointer; font-size: 13px;
}
.pill.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- ticket modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal-card { width: 380px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; font-size: 16px; }
.ticket-quote { font-size: 22px; font-weight: 700; margin-bottom: 14px; font-variant-numeric: tabular-nums; }
.side-toggle { display: flex; gap: 6px; margin-bottom: 14px; }
.side-btn { flex: 1; padding: 10px; border-radius: 9px; border: 1px solid var(--border); background: var(--panel-2); color: var(--dim); cursor: pointer; font-weight: 600; }
.side-btn.buy.active { background: var(--green); color: #04160d; border-color: var(--green); }
.side-btn.sell.active { background: var(--red); color: #1c0507; border-color: var(--red); }
.ticket-estimate { color: var(--dim); font-size: 13px; margin: 8px 0; }

/* ---------- chat ---------- */
.chat-wrap { max-width: 760px; display: flex; flex-direction: column; height: calc(100vh - 120px); }
.chat-log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 8px 2px; }
.msg { max-width: 80%; padding: 12px 16px; border-radius: 14px; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.thinking { color: var(--dim); font-style: italic; }
.chat-form { display: flex; gap: 10px; margin-top: 14px; }
.chat-form input { margin-top: 0; flex: 1; }
.chat-note { color: var(--dim); font-size: 12px; margin-top: 8px; }

/* ---------- instrument detail / chart ---------- */
.detail-card { width: 720px; max-width: 96vw; }
.modal-actions { display: flex; gap: 8px; }
.star {
  background: none; border: 0; color: var(--dim); cursor: pointer; font-size: 15px; padding: 0 4px 0 0;
}
.star.on, .star-btn.on { color: #f5b942; }
.star-btn {
  background: none; border: 0; color: var(--dim); cursor: pointer; font-size: 20px; margin-left: 8px;
}
.range-pills { display: flex; gap: 6px; margin-bottom: 12px; }
#chart-container { width: 100%; height: 320px; }
#chart-empty { padding: 40px 0; text-align: center; }
.row-click { cursor: pointer; }
.sym-link { cursor: pointer; color: var(--accent); }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border); padding: 12px 22px;
  border-radius: 10px; z-index: 30; box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.toast.ok { border-color: var(--green); }
.toast.err { border-color: var(--red); }

/* ---------- mobile layout ---------- */
@media (max-width: 860px) {
  .app { flex-direction: column; }

  /* sidebar becomes a compact top strip with a scrollable nav row */
  .sidebar {
    width: 100%; height: auto; position: static; padding: 12px 14px;
    flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px;
    border-right: 0; border-bottom: 1px solid var(--border);
  }
  .sidebar .brand { margin-bottom: 0; }
  .sidebar nav {
    order: 3; flex-basis: 100%;
    flex-direction: row; flex-wrap: nowrap; gap: 6px;
    overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px;
  }
  .nav-btn { white-space: nowrap; padding: 8px 12px; font-size: 13px; }
  .sidebar-foot { margin: 0 0 0 auto; flex-direction: row; align-items: center; gap: 8px; }
  .sidebar-foot .btn { padding: 7px 10px; font-size: 12px; }
  .user-chip { font-size: 12px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; }

  .main { padding: 0 14px 40px; }

  /* topbar wraps; account picker gets its own full row */
  .topbar { flex-wrap: wrap; gap: 8px; padding: 10px 0; }
  .account-select { min-width: 0; flex: 1 1 100%; }
  .topbar-stats { margin-left: 0; flex-basis: 100%; font-size: 12px; gap: 14px; }
  .topbar .btn, .bot-pill { padding: 7px 10px; font-size: 12px; }

  /* cards and tables */
  .cards { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
  .card { padding: 12px; }
  .card .v { font-size: 19px; }
  th, td { padding: 8px 10px; font-size: 13px; }

  /* forms and modals fit small screens */
  .auth-card, .modal-card { width: 380px; max-width: 100%; padding: 22px; }
  input, select, .btn { font-size: 16px; } /* stops iOS auto-zoom on focus */
  .btn { padding: 10px 14px; }

  /* chat */
  .chat-wrap { height: calc(100vh - 200px); }
  .msg { max-width: 92%; }

  /* auto-trader bits */
  .bot-head { flex-direction: column; align-items: flex-start; gap: 10px; }
  .bot-cards { grid-template-columns: 1fr; }
  .bot-perf-stats { gap: 10px; font-size: 12px; }
  .bot-feed-head .btn { margin-left: 0; }
  .feed-msg { white-space: normal; }

  /* floating bot box shrinks out of the way */
  .bot-float { width: 200px; right: 10px; bottom: 10px; padding: 8px 10px; }
  .bot-float-body { font-size: 11px; }

  .toast { max-width: calc(100vw - 32px); }
}

/* very small phones */
@media (max-width: 400px) {
  .cards { grid-template-columns: 1fr 1fr; }
  .bot-float { width: 176px; }
}

/* ---------- Auto-Trader ---------- */
.bot-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; gap: 16px; flex-wrap: wrap; }
.bot-status-line { margin-top: 4px; font-size: 14px; }
.bot-head-actions { display: flex; align-items: center; gap: 12px; }
.bot-enabled-label { font-weight: 700; font-size: 14px; letter-spacing: .06em; }
.small { font-size: 12px; }

.switch { position: relative; display: inline-block; width: 52px; height: 28px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; inset: 0; cursor: pointer; border-radius: 28px;
  background: var(--panel-2); border: 1px solid var(--border); transition: .2s;
}
.switch .slider::before {
  content: ''; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px;
  border-radius: 50%; background: var(--dim); transition: .2s;
}
.switch input:checked + .slider { background: rgba(34,192,122,.25); border-color: var(--green); }
.switch input:checked + .slider::before { transform: translateX(24px); background: var(--green); }

.bot-cards { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-bottom: 8px; }
.bot-capital-row { display: flex; gap: 8px; margin: 10px 0; }
.bot-capital-row input { flex: 1; }
.bot-recommend-out { margin-top: 8px; line-height: 1.5; }

.bot-risk label { display: block; padding: 6px 0; cursor: pointer; font-size: 14px; }
.bot-risk input { margin-right: 8px; }

.bot-channels { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.channel-chip {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 6px 12px; border-radius: 999px; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--dim);
}
.channel-chip.on { border-color: var(--accent); color: var(--text); background: rgba(79,140,255,.15); }
.channel-chip input { accent-color: var(--accent); }

.bot-feed-head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-top: 8px; }
.bot-feed-head h2 { margin: 0; flex: 0 0 auto; }
.bot-feed-toggles { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--dim); }
.bot-feed-toggles label { cursor: pointer; display: inline-flex; align-items: center; gap: 5px; }
.bot-feed-toggles input { accent-color: var(--accent); }
.bot-feed-head .btn { margin-left: auto; }

.bot-feed {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  max-height: 340px; overflow-y: auto; margin-top: 10px;
}
.feed-row {
  display: flex; gap: 10px; align-items: baseline; padding: 9px 14px;
  border-bottom: 1px solid var(--border); font-size: 13px;
}
.feed-row:last-child { border-bottom: none; }
.feed-time { flex: 0 0 74px; font-variant-numeric: tabular-nums; }
.feed-badge {
  flex: 0 0 auto; font-size: 10px; font-weight: 700; letter-spacing: .06em;
  padding: 2px 7px; border-radius: 6px; background: var(--panel-2); color: var(--dim);
}
.kind-buy .feed-badge { color: var(--green); border: 1px solid var(--green); }
.kind-sell .feed-badge { color: var(--red); border: 1px solid var(--red); }
.kind-decision .feed-badge { color: var(--accent); border: 1px solid var(--accent); }
.kind-error .feed-badge { color: var(--red); border: 1px solid var(--red); }
.feed-msg { line-height: 1.45; }

/* ---------- Auto-Trader performance chart ---------- */
.bot-perf-stats { display: flex; gap: 22px; flex-wrap: wrap; font-size: 13px; color: var(--dim); margin-bottom: 10px; }
.bot-perf-stats b { color: var(--text); margin-left: 4px; font-variant-numeric: tabular-nums; }
.bot-perf-stats b.pos, .bot-perf-stats b.neg { margin-left: 4px; }
#bot-equity-chart { width: 100%; height: 260px; }

/* ---------- Auto-Trader status pill (topbar) ---------- */
.bot-pill {
  display: inline-flex; align-items: center; gap: 8px; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 14px; font-size: 12px; font-weight: 600; color: var(--dim);
  letter-spacing: .03em;
}
.bot-pill:hover { filter: brightness(1.2); }
.bot-pill-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); }
.bot-pill.on { color: var(--green); border-color: var(--green); }
.bot-pill.on .bot-pill-dot { background: var(--green); animation: botpulse 2s infinite; }
@keyframes botpulse {
  0% { box-shadow: 0 0 0 0 rgba(34,192,122,.5); }
  70% { box-shadow: 0 0 0 7px rgba(34,192,122,0); }
  100% { box-shadow: 0 0 0 0 rgba(34,192,122,0); }
}
.bot-perf-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }

/* ---------- research scan ---------- */
.bot-research { margin-bottom: 18px; }
.bot-research-explainer { margin-bottom: 12px; line-height: 1.6; }
.bot-research code { background: var(--panel-2); padding: 1px 6px; border-radius: 4px; font-size: 11px; color: var(--accent); }
.bot-research .k { margin-bottom: 8px; }
.scan-action {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 10px; font-weight: 700; letter-spacing: .05em;
}
.scan-bought  { background: rgba(34,192,122,.16);  color: var(--green); }
.scan-sold    { background: rgba(240,81,94,.16);   color: var(--red); }
.scan-holding { background: rgba(79,140,255,.14);  color: var(--accent); }
.scan-watch   { background: rgba(230,180,60,.14);  color: #e6b43c; }
.scan-skip    { background: rgba(139,147,167,.10); color: var(--dim); }
.dash-scan-row { padding: 3px 0; }
#dash-research-card { margin-top: 14px; }
#dash-research-card .k { margin-bottom: 8px; }

/* ---------- floating bot status ---------- */
.bot-float {
  position: fixed; right: 18px; bottom: 18px; z-index: 60;
  width: 252px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 10px 34px rgba(0,0,0,.55);
  padding: 10px 12px;
}
.bot-float.on { border-color: rgba(34,192,122,.55); }
.bot-float-head { display: flex; align-items: center; gap: 8px; cursor: pointer; margin-bottom: 6px; }
.bot-float-head:hover .bot-float-title { color: var(--text); }
.bot-float .bot-pill-dot { flex: none; }
.bot-float.on .bot-pill-dot { background: var(--green); animation: botpulse 2s infinite; }
.bot-float-title { font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--dim); }
.bot-float-state { margin-left: auto; font-size: 11px; font-weight: 700; letter-spacing: .06em; }
.bot-float-body { margin-bottom: 8px; line-height: 1.5; }
.bot-float-actions { display: flex; gap: 8px; }
.bot-float-btn {
  flex: 1; border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  border-radius: 8px; padding: 6px 0; cursor: pointer; font-size: 13px; transition: filter .15s;
}
.bot-float-btn:hover { filter: brightness(1.3); }
.bot-float-btn.danger:hover { border-color: var(--red); color: var(--red); }

/* ---------- admin ---------- */
.admin-create { margin-bottom: 14px; }
.admin-create-form { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.admin-create-form input {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 9px 12px; font-size: 14px; flex: 1; min-width: 180px;
}
.admin-create-form input:focus { outline: none; border-color: var(--accent); }

/* ---------- scalp / fast trades ---------- */
.scan-scalp { background: rgba(191,90,255,.16); color: #bf5aff; }
.scalp-toggle {
  display: block; margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--border); cursor: pointer; font-size: 14px;
}
.scalp-toggle input { margin-right: 6px; accent-color: var(--accent); width: auto; }

/* ---------- mobile overrides (must stay LAST in this file to win) ---------- */
@media (max-width: 860px) {
  .bot-float { width: 190px; right: 10px; bottom: 10px; padding: 8px 10px; }
  .bot-float-body { font-size: 11px; }
  .bot-float-actions { gap: 6px; }
  .bot-float-btn { font-size: 12px; padding: 5px 0; }
  .user-chip { max-width: 110px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .auth-card, .modal-card { width: 380px; max-width: 100%; }
}
@media (max-width: 400px) {
  .bot-float { width: 168px; }
  .user-chip { display: none; }
}

/* ---------- real trading ---------- */
.real-connect-card { max-width: 580px; }
.real-connect-text { margin: 8px 0 14px; line-height: 1.6; }
.real-connect-text code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
.real-order-row { display: flex; gap: 10px; flex-wrap: wrap; }
.real-order-row input { flex: 1; min-width: 120px; margin-top: 0; }
.real-side-toggle { margin: 0; flex: 1; min-width: 160px; }
.real-order-actions { margin-top: 10px; }
.real-order-out { margin-top: 10px; line-height: 1.6; }
.real-confirm-live { background: var(--red); border-color: var(--red); color: #fff; font-weight: 700; }

/* keep the float header on one line */
.bot-float-title, .bot-float-state { white-space: nowrap; }

/* ---------- broker directory ---------- */
.broker-dir-card { margin-bottom: 18px; }
.broker-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; margin-top: 12px; }
.broker-card { background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.broker-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.broker-ready { font-size: 10px; font-weight: 700; letter-spacing: .6px; color: var(--green); background: rgba(52,211,153,.12); border: 1px solid rgba(52,211,153,.35); padding: 3px 7px; border-radius: 999px; white-space: nowrap; }
.broker-soon { font-size: 10px; font-weight: 700; letter-spacing: .6px; color: var(--muted); background: rgba(148,163,184,.12); border: 1px solid var(--border); padding: 3px 7px; border-radius: 999px; white-space: nowrap; }
.broker-badges { display: flex; flex-wrap: wrap; gap: 6px; }
.broker-badge { font-size: 11px; color: var(--text); background: rgba(77,163,255,.10); border: 1px solid rgba(77,163,255,.22); padding: 3px 8px; border-radius: 999px; }
.broker-note { margin: 0; line-height: 1.5; min-height: 60px; }
.broker-actions { margin-top: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.broker-actions .btn { padding: 7px 12px; font-size: 12px; width: auto; margin-top: 0; }
.broker-actions .btn[disabled] { opacity: .45; cursor: not-allowed; }

/* ---------- final mobile overrides (must stay LAST) ---------- */
@media (max-width: 860px) {
  .broker-list { grid-template-columns: 1fr; }
  .broker-note { min-height: 0; }
  .broker-actions .btn { flex: 1; text-align: center; }
  .bot-float { width: 190px; right: 10px; bottom: 10px; padding: 8px 10px; }
  .bot-float-title, .bot-float-state { white-space: nowrap; }
}
@media (max-width: 400px) {
  .bot-float { width: 168px; }
}
