/* Autopilot Trader. One stylesheet, mobile-first details at the bottom.
   AI content always uses the violet "AI voice" colour so you can tell at a glance what the AI said. */

:root {
  --bg: #F4F5F7;
  --surface: #FFFFFF;
  --surface-2: #F6F7F9;
  --ink: #121826;
  --muted: #5E6779;
  --line: #E3E6EB;
  --line-strong: #CDD2DA;
  --accent: #2448D8;
  --accent-soft: #EAEEFC;
  --ai: #6B4EFF;
  --ai-soft: #F2EFFF;
  --gain: #067A57;
  --gain-soft: #E3F5EE;
  --loss: #C8372D;
  --loss-soft: #FCEBE9;
  --warn: #8F5E00;
  --warn-soft: #FFF4DB;
  --r-lg: 16px;
  --r: 10px;
  --r-sm: 8px;
  --shadow: 0 1px 2px rgba(18, 24, 38, 0.04), 0 4px 16px rgba(18, 24, 38, 0.04);
  --font: "Manrope", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --side: 248px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0D1220; --surface: #141B2C; --surface-2: #1A2236; --ink: #E8ECF3; --muted: #97A1B5;
    --line: #253049; --line-strong: #34405D; --accent: #8396FF; --accent-soft: #1F2848;
    --ai: #A895FF; --ai-soft: #231E44; --gain: #36C79A; --gain-soft: #112E27;
    --loss: #F2776A; --loss-soft: #3A1E1C; --warn: #E8B95B; --warn-soft: #34290F;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font); font-variant-numeric: tabular-nums; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; }
h1, h2, h3 { margin: 0; line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.45rem; font-weight: 800; }
h2 { font-size: 1.02rem; font-weight: 750; }
h3 { font-size: 0.98rem; font-weight: 750; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 0.84rem; }
.nowrap { white-space: nowrap; }
.up { color: var(--gain); }
.down { color: var(--loss); }
[hidden] { display: none !important; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1rem; color: inherit; text-decoration: none; padding: 2px 10px; }
.brand-lg { font-size: 1.1rem; margin-bottom: 28px; padding: 0; }
.brand-mark { display: inline-block; width: 28px; height: 28px; border-radius: 8px; flex: none; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M7 21l6-7 5 4 7-9' stroke='white' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / cover; }

/* ---------- sign-in ---------- */
.gate { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.gate-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 36px; box-shadow: var(--shadow); }
.gate-card h1 { margin-bottom: 8px; }
.gate-card .muted { margin-bottom: 22px; }
.gate-version { text-align: center; margin-top: 16px; }

/* ---------- app shell (desktop) ---------- */
.app { display: grid; grid-template-columns: var(--side) minmax(0, 1fr); min-height: 100vh; }
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 22px; padding: 22px 14px 18px; background: var(--surface); border-right: 1px solid var(--line); overflow-y: auto; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item { display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: var(--r-sm); color: var(--muted); font-weight: 650; font-size: 0.93rem; text-decoration: none; }
.nav-item svg { width: 19px; height: 19px; fill: currentColor; flex: none; }
.nav-item:hover { color: var(--ink); background: var(--surface-2); }
.nav-item.is-active { color: var(--accent); background: var(--accent-soft); }
.nav-item[href="assistant.php"] { color: var(--ai); }
.nav-item[href="assistant.php"].is-active { background: var(--ai-soft); }
.nav-group { margin: 16px 10px 6px; font-size: 0.76rem; font-weight: 700; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.nav-group-gap { margin: 10px 0 6px; border-top: 1px solid var(--line); }
.paper-tag { font-size: 0.7rem; padding: 1px 7px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; padding: 0 6px; }
.foot-row { display: flex; align-items: center; justify-content: space-between; }
.alert-pill { align-self: flex-start; border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 4px 11px; font-size: 0.8rem; font-weight: 700; color: var(--muted); text-decoration: none; }
.alert-pill.is-on { background: var(--gain-soft); color: var(--gain); border-color: transparent; }
.version { font-size: 0.78rem; color: var(--muted); text-decoration: none; }
.version:hover { color: var(--accent); }

.main { min-width: 0; padding: 26px clamp(18px, 3vw, 40px) 40px; display: flex; flex-direction: column; gap: 20px; max-width: 1360px; width: 100%; }
.topbar { display: flex; align-items: center; gap: 14px; }
.topbar-mark { display: none; }
.topbar-title { flex: 1; min-width: 0; }
.market { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.86rem; margin-top: 2px; }
.market .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); flex: none; }
.market.is-open .dot { background: var(--gain); box-shadow: 0 0 0 4px var(--gain-soft); }
.btn.btn-ai { color: var(--ai); border-color: color-mix(in srgb, var(--ai) 35%, var(--line)); background: var(--ai-soft); }
.btn-ai svg { width: 17px; height: 17px; fill: currentColor; }
.btn.btn-ai:hover { border-color: var(--ai); }
.tabbar, .sheet { display: none; }

.banner { padding: 12px 16px; border-radius: var(--r); background: var(--warn-soft); color: var(--warn); font-weight: 650; font-size: 0.9rem; }
.banner.is-danger { background: var(--loss-soft); color: var(--loss); }
.foot { margin-top: auto; padding-top: 6px; }

/* ---------- panels ---------- */
.view { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 20px; min-width: 0; box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.panel-head > div { min-width: 0; flex: 1 1 240px; }
.panel-head > .btn, .panel-head > .seg { flex: none; }
.panel-head p { margin-top: 3px; }
.panel-link { display: block; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); font-size: 0.86rem; font-weight: 700; text-decoration: none; }
.empty { padding: 22px 8px; text-align: center; color: var(--muted); font-size: 0.92rem; }
.empty-state { padding: 34px 26px; }
.empty-state h2 { margin-bottom: 8px; }
.prose { max-width: 72ch; margin-top: 8px; }

/* ---------- dashboard ---------- */
.control-strip { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 26px; padding: 14px 18px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow); }
.control-strip #stopBtn { margin-left: auto; }
.kpis { display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 14px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 16px 18px; display: flex; flex-direction: column; gap: 2px; min-width: 0; box-shadow: var(--shadow); }
.kpi span { font-size: 0.82rem; color: var(--muted); font-weight: 600; }
.kpi b { font-size: 1.5rem; font-weight: 800; letter-spacing: -0.02em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi small { font-size: 0.84rem; font-weight: 650; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi small.up { color: var(--gain); }
.kpi small.down { color: var(--loss); }
.kpi-main b { font-size: 1.95rem; }

.dash-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(300px, 380px); grid-template-areas: "chart feed" "strats feed" "pos orders"; gap: 20px; align-items: start; }
.area-chart { grid-area: chart; }
.area-feed { grid-area: feed; }
.area-strats { grid-area: strats; }
.area-pos { grid-area: pos; }
.area-orders { grid-area: orders; }
.chart-wrap { position: relative; height: 260px; }
.chart-wrap.tall { height: 300px; }
.chart-note { margin: 16px 0 8px; }

/* alerts feed */
.feed-panel { position: sticky; top: 20px; display: flex; flex-direction: column; max-height: calc(100vh - 40px); }
.feed { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 120px; }
.feed-full { max-height: none; overflow: visible; }
.ev { display: grid; grid-template-columns: 3px 1fr; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.ev:first-child { border-top: 0; padding-top: 2px; }
.ev-dot { border-radius: 3px; background: var(--line-strong); }
.ev-watch .ev-dot, .ev-declined .ev-dot { background: var(--ai); }
.ev-entry .ev-dot, .ev-fill .ev-dot { background: var(--gain); }
.ev-exit .ev-dot { background: var(--ink); }
.ev-error .ev-dot { background: var(--loss); }
.ev-head { display: flex; justify-content: space-between; gap: 8px; }
.ev-type { font-size: 0.75rem; font-weight: 800; color: var(--muted); }
.ev-watch .ev-type, .ev-declined .ev-type { color: var(--ai); }
.ev-entry .ev-type, .ev-fill .ev-type { color: var(--gain); }
.ev-error .ev-type { color: var(--loss); }
.ev-title { font-weight: 700; font-size: 0.92rem; margin-top: 1px; }
.ev-body { font-size: 0.86rem; color: var(--muted); margin-top: 4px; }
.ev-watch .ev-body, .ev-declined .ev-body, .ev-entry .ev-body { color: var(--ink); background: var(--ai-soft); border-radius: var(--r-sm); padding: 8px 10px; margin-top: 6px; }
.ev.is-new { animation: ev-in 1.6s ease-out; }
@keyframes ev-in { from { background: var(--accent-soft); } to { background: transparent; } }
.feed-empty { padding: 16px 0; }

/* live strategies */
.deploy-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.deploy-list > .empty { grid-column: 1 / -1; }
.deploy { border: 1px solid var(--line); border-radius: var(--r); padding: 14px; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.deploy-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.deploy-sym { font-weight: 800; font-size: 1.08rem; }
.deploy-name { font-weight: 700; flex-basis: 100%; order: 5; }
.deploy-head .deploy-rm { margin-left: auto; }
.pick { padding: 16px 0; border-top: 1px solid var(--line); }
.pick:first-of-type { border-top: 0; padding-top: 2px; }
.pick-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.pick-name { font-weight: 750; }
.pick-btns { margin-left: auto; display: flex; gap: 8px; }
.pick-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 20px; }
.stat-cap { font-size: 0.76rem; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.cell-title { display: block; font-weight: 700; }
.cell-sub { display: block; font-size: 0.78rem; color: var(--muted); }
.rule { font-size: 0.85rem; padding: 8px 10px; background: var(--surface-2); border-radius: var(--r-sm); }
.rule b { font-weight: 750; }
.stats { display: flex; gap: 6px 16px; flex-wrap: wrap; }
.stat { font-size: 0.76rem; color: var(--muted); }
.stat b { display: block; font-size: 0.95rem; color: var(--ink); }
.strat-why { font-size: 0.88rem; padding: 10px 12px; background: var(--ai-soft); border-radius: var(--r-sm); margin: 10px 0; }
.sitting { margin: 14px 0 0; padding-left: 18px; color: var(--muted); font-size: 0.88rem; }
.sources { margin: 10px 0 0; padding-left: 18px; font-size: 0.84rem; }

/* ---------- controls ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 38px; padding: 8px 15px; border-radius: var(--r-sm); border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); font-weight: 700; font-size: 0.9rem; cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn-danger-ghost { color: var(--loss); border-color: color-mix(in srgb, var(--loss) 40%, var(--line)); }
.btn-danger-ghost:hover { background: var(--loss-soft); border-color: var(--loss); }
.btn-sm { min-height: 32px; padding: 5px 11px; font-size: 0.82rem; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row.end { justify-content: flex-end; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 700; cursor: pointer; font-size: 0.86rem; }
.spinner { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.seg { display: inline-flex; flex-wrap: wrap; gap: 2px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r); padding: 3px; }
.seg button, .seg label span { display: block; border: 0; background: none; padding: 6px 12px; border-radius: 7px; font-size: 0.84rem; font-weight: 700; color: var(--muted); cursor: pointer; white-space: nowrap; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg button.is-on, .seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(18, 24, 38, 0.14); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.seg-wide { display: flex; }
.seg-wide label { flex: 1; text-align: center; }

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; }
.switch-track { width: 40px; height: 24px; border-radius: 999px; background: var(--line-strong); position: relative; transition: background 0.2s; flex: none; }
.switch-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform 0.2s; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25); }
.switch input:checked + .switch-track { background: var(--gain); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-text { display: flex; flex-direction: column; line-height: 1.25; }
.switch-text b { font-size: 0.9rem; font-weight: 750; }
.switch-text small { font-size: 0.76rem; color: var(--muted); }
.switch-label { font-weight: 700; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tab { border: 0; background: none; padding: 10px 14px; font-weight: 700; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tab.is-on { color: var(--ink); border-bottom-color: var(--accent); }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 0.75rem; font-weight: 700; background: var(--surface-2); border: 1px solid var(--line); white-space: nowrap; }
.pill.buy { background: var(--gain-soft); color: var(--gain); border-color: transparent; }
.pill.sell { background: var(--loss-soft); color: var(--loss); border-color: transparent; }
.pill.ai { background: var(--ai-soft); color: var(--ai); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.field > span { font-size: 0.84rem; font-weight: 700; }
.field small { color: var(--muted); font-size: 0.79rem; }
.field.narrow { max-width: 280px; }
input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { width: 100%; min-height: 40px; padding: 9px 12px; border: 1px solid var(--line-strong); border-radius: var(--r-sm); background: var(--surface); outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[readonly] { color: var(--muted); font-size: 0.85rem; background: var(--surface-2); }
input[type=range] { width: 100%; accent-color: var(--accent); }
textarea { resize: vertical; }
.check { display: flex; align-items: flex-start; gap: 10px; margin: 6px 0 10px; font-size: 0.92rem; cursor: pointer; }
.check input { width: 17px; height: 17px; margin-top: 2px; accent-color: var(--accent); flex: none; }
.row2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }
.row3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 16px; }
.form-error { color: var(--loss); font-size: 0.88rem; min-height: 1.3em; margin: -4px 0 12px; }
.input-row { display: flex; gap: 8px; align-items: stretch; }
.input-row > input, .input-row > textarea { flex: 1; }
.chips, .strategy-picks { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-check { position: relative; cursor: pointer; }
.chip-check input { position: absolute; opacity: 0; }
.chip-check span { display: inline-block; padding: 6px 12px; border: 1px solid var(--line-strong); border-radius: 999px; font-size: 0.83rem; font-weight: 650; background: var(--surface); }
.chip-check input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip-check input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.custom-dates { margin-top: 10px; }
.choice { display: grid; gap: 8px; }
.choice label { position: relative; cursor: pointer; }
.choice input { position: absolute; opacity: 0; }
.choice span { display: flex; flex-direction: column; gap: 2px; padding: 12px 14px; border: 1px solid var(--line-strong); border-radius: var(--r); }
.choice small { color: var(--muted); font-size: 0.82rem; }
.choice input:checked + span { border-color: var(--ai); background: var(--ai-soft); box-shadow: inset 0 0 0 1px var(--ai); }
.choice input:focus-visible + span { outline: 2px solid var(--accent); }

.settings { display: flex; flex-direction: column; gap: 20px; }
.settings fieldset { margin: 0; }
.settings legend { float: left; width: 100%; font-weight: 800; font-size: 1.05rem; margin-bottom: 8px; }
.settings legend + * { clear: both; }
.save-bar { position: sticky; bottom: 12px; display: flex; justify-content: flex-end; gap: 10px; padding: 12px; background: color-mix(in srgb, var(--surface) 90%, transparent); backdrop-filter: blur(8px); border: 1px solid var(--line); border-radius: var(--r-lg); z-index: 5; }
.test-results:empty { display: none; }
.test-results { display: grid; gap: 8px; }
.test-line { padding: 10px 14px; border-radius: var(--r); font-size: 0.9rem; font-weight: 650; }
.test-line.ok { background: var(--gain-soft); color: var(--gain); }
.test-line.bad { background: var(--loss-soft); color: var(--loss); }
.pw-form h2 { margin-bottom: 14px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 0.9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-weight: 650; overflow-wrap: anywhere; }
.changelog { margin-top: 14px; }
.changelog summary, .explainer summary { cursor: pointer; font-weight: 700; }
#changelog { margin-top: 10px; font-size: 0.9rem; max-width: 72ch; }
#changelog p { margin: 2px 0; }
.diag { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.diag .btn { margin-top: 10px; }
#diagBox { margin-top: 14px; }
.spec { max-height: 320px; overflow: auto; white-space: pre-wrap; font: 0.78rem/1.5 var(--mono); background: var(--surface-2); padding: 12px; border-radius: var(--r-sm); margin: 0 0 12px; }
.code { font-family: var(--mono); font-size: 0.82rem; line-height: 1.5; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.table th { text-align: left; font-size: 0.76rem; font-weight: 700; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; white-space: nowrap; }
.table .sym { font-weight: 800; }
.table .reason { color: var(--muted); font-size: 0.82rem; max-width: 420px; }
.lvl-error { color: var(--loss); font-weight: 700; }
.lvl-warn { color: var(--warn); font-weight: 700; }
.lvl-trade { color: var(--gain); font-weight: 700; }

/* ---------- studio & backtest ---------- */
.grid-studio { display: grid; grid-template-columns: minmax(320px, 400px) minmax(0, 1fr); gap: 20px; align-items: start; }
.grid-backtest { display: grid; grid-template-columns: minmax(300px, 340px) minmax(0, 1fr); gap: 20px; align-items: start; }
.studio-form h2, .bt-form h2 { margin-bottom: 4px; }
.studio-form > .muted { margin-bottom: 16px; }
.bt-form h2 { margin-bottom: 14px; }
.bt-form #btDesc { margin: -6px 0 14px; }
.param-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 12px; }
.progress { position: relative; height: 34px; background: var(--surface-2); border-radius: var(--r-sm); overflow: hidden; margin-bottom: 12px; }
.progress-bar { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--accent-soft); transition: width 0.4s; }
.progress span { position: relative; display: block; line-height: 34px; padding: 0 12px; font-size: 0.85rem; font-weight: 700; }
.joblog { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; font-size: 0.85rem; }
.joblog li { padding: 6px 0; border-top: 1px solid var(--line); }
.joblog li:first-child { border-top: 0; }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.metric { background: var(--surface); padding: 14px 16px; min-width: 0; }
.metric > span { font-size: 0.78rem; color: var(--muted); display: block; font-weight: 600; }
.metric b { font-size: 1.25rem; font-weight: 800; letter-spacing: -0.02em; }
.metric small { display: block; color: var(--muted); font-size: 0.75rem; }

/* ---------- library ---------- */
.lib-filter { margin-bottom: 18px; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.lib-card { border: 1px solid var(--line); border-radius: var(--r); padding: 16px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.lib-card .btn-row { margin-top: auto; }
.lib-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }

/* ---------- journal ---------- */
.note { padding: 14px 0; border-bottom: 1px solid var(--line); }
.note:last-child { border-bottom: 0; }
.note-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 0.85rem; margin-bottom: 6px; }
.note pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: 0.84rem/1.55 var(--mono); color: var(--muted); max-height: 260px; overflow: auto; }

/* ---------- assistant ---------- */
.chat { display: flex; flex-direction: column; gap: 16px; max-width: 900px; width: 100%; margin: 0 auto; min-height: calc(100vh - 210px); }
.chat-log { display: flex; flex-direction: column; gap: 14px; flex: 1; }
.msg { max-width: 85%; padding: 13px 16px; border-radius: 16px; font-size: 0.95rem; line-height: 1.55; overflow-wrap: anywhere; }
.msg p.gap { height: 8px; }
.msg ul { margin: 6px 0; padding-left: 20px; }
.msg code { font-family: var(--mono); font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.msg-user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg-user a, .msg-user code { color: #fff; background: rgba(255, 255, 255, 0.15); }
.msg-ai { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--ai); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg-wait { display: flex; align-items: center; gap: 10px; color: var(--ai); }
.msg-tools { margin-bottom: 8px; font-size: 0.82rem; color: var(--ai); }
.msg-tools summary { cursor: pointer; font-weight: 700; }
.msg-tools ul { color: var(--muted); }
.msg-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.act-done { color: var(--gain); font-weight: 650; font-size: 0.88rem; flex-basis: 100%; }
.act-failed { color: var(--loss); font-weight: 650; font-size: 0.88rem; flex-basis: 100%; }
.chat-starters { display: flex; flex-wrap: wrap; gap: 10px; }
.starter { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 999px; padding: 9px 16px; font-weight: 650; font-size: 0.9rem; cursor: pointer; text-align: left; }
.starter:hover { border-color: var(--ai); color: var(--ai); }
.chat-compose { position: sticky; bottom: 14px; background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-lg); padding: 12px 14px; box-shadow: 0 8px 28px rgba(18, 24, 38, 0.1); }
.chat-compose:focus-within { border-color: var(--accent); }
.chat-compose textarea:focus-visible { outline: none; }
.chat-compose textarea { border: 0 !important; background: transparent !important; resize: none; padding: 4px 2px; box-shadow: none !important; min-height: 48px; }
.chat-bar { display: flex; align-items: center; gap: 16px; margin: 6px 0; }
.chat-bar .check { margin: 0; font-size: 0.86rem; }
.chat-bar .btn { margin-left: auto; }

/* ---------- toasts & dialogs ---------- */
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 60; max-width: min(420px, calc(100vw - 40px)); }
.toast { padding: 12px 16px; border-radius: var(--r); background: var(--ink); color: var(--bg); font-weight: 650; font-size: 0.9rem; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18); animation: toast-in 0.2s ease-out; }
.toast.bad { background: var(--loss); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
.dialog { border: 1px solid var(--line); border-radius: var(--r-lg); padding: 24px; width: min(460px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); }
.dialog-wide { width: min(780px, calc(100vw - 32px)); }
.dialog::backdrop, .sheet::backdrop { background: rgba(13, 18, 32, 0.45); }
.dialog h2 { margin-bottom: 8px; }
.dialog p { margin-bottom: 20px; }

/* ======================================================================
   Tablet
   ====================================================================== */
@media (max-width: 1180px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash-grid { grid-template-columns: minmax(0, 1fr); grid-template-areas: "feed" "strats" "pos" "chart" "orders"; }
  .feed-panel { position: static; max-height: 440px; }
  .grid-studio, .grid-backtest { grid-template-columns: minmax(0, 1fr); }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ======================================================================
   Phone: app-style shell with a bottom tab bar
   ====================================================================== */
@media (max-width: 820px) {
  body { font-size: 15px; }
  .app { display: block; }
  .sidebar { display: none; }
  .main { padding: 0 14px calc(92px + env(safe-area-inset-bottom)); gap: 14px; }
  .foot { display: none; }

  .topbar { position: sticky; top: 0; z-index: 20; margin: 0 -14px; padding: calc(10px + env(safe-area-inset-top)) 14px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); }
  .topbar-mark { display: block; }
  .topbar-mark .brand-mark { width: 30px; height: 30px; }
  h1 { font-size: 1.15rem; }
  .market { font-size: 0.78rem; }
  .btn-ai { min-height: 36px; padding: 6px 10px; }
  .btn-ai span { display: none; }
  .btn-ai svg { width: 20px; height: 20px; }

  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(14px); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
  .tab-item { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 5px 2px; border: 0; background: none; color: var(--muted); font-size: 0.7rem; font-weight: 700; text-decoration: none; border-radius: var(--r-sm); cursor: pointer; }
  .tab-item svg { width: 23px; height: 23px; fill: currentColor; }
  .tab-item.is-active { color: var(--accent); }
  .tab-item[href="assistant.php"] { color: var(--ai); }
  .sheet[open] { display: block; position: fixed; inset: auto 0 0 0; margin: 0; width: 100%; max-width: none; border: 0; border-radius: 20px 20px 0 0; padding: 8px 16px calc(18px + env(safe-area-inset-bottom)); background: var(--surface); color: var(--ink); animation: sheet-in 0.2s ease-out; }
  @keyframes sheet-in { from { transform: translateY(30px); opacity: 0; } }
  .sheet-grip { width: 40px; height: 4px; border-radius: 4px; background: var(--line-strong); margin: 4px auto 12px; }
  .sheet-item { display: flex; align-items: center; gap: 14px; padding: 14px 6px; border-bottom: 1px solid var(--line); color: var(--ink); text-decoration: none; font-weight: 700; }
  .sheet-item svg { width: 22px; height: 22px; fill: var(--muted); }
  .sheet-foot { display: flex; justify-content: space-between; align-items: center; padding: 16px 6px 0; }

  .panel { padding: 16px; border-radius: 14px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .panel-head { margin-bottom: 12px; }
  .row2, .row3 { grid-template-columns: minmax(0, 1fr); }
  input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea { font-size: 16px; min-height: 44px; }
  .btn { min-height: 42px; }
  .btn-sm { min-height: 36px; }

  /* dashboard */
  .banner { font-size: 0.86rem; padding: 10px 12px; }
  .control-strip { display: flex; flex-direction: column; align-items: stretch; gap: 0; padding: 4px 14px 14px; }
  .control-strip .switch { flex-direction: row-reverse; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .control-strip .switch-text small { font-size: 0.74rem; }
  .control-strip #stopBtn { margin: 12px 0 0; }
  .kpis { gap: 10px; }
  .kpi { padding: 12px 14px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi:nth-child(4) { display: none; }
  .kpi-main { grid-column: 1 / -1; }
  .kpi span { font-size: 0.74rem; }
  .kpi small { font-size: 0.74rem; }
  .kpi b { font-size: 1.1rem; }
  .kpi-main b { font-size: 1.8rem; }
  .chart-wrap { height: 210px; }
  .deploy-list { grid-template-columns: minmax(0, 1fr); }
  .pick-stats { grid-template-columns: minmax(0, 1fr); }
  .pick-btns { margin-left: 0; flex-basis: 100%; }
  .table td .cell-title, .table td .cell-sub { text-align: right; }
  .seg { max-width: 100%; }
  .seg-wide { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); }

  /* tables become stacked cards; labels come from the column headers */
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: 10px 0; border-bottom: 1px solid var(--line); }
  .table tr:last-child { border-bottom: 0; }
  .table td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 3px 0; border: 0; text-align: right; }
  .table td::before { content: attr(data-label); color: var(--muted); font-size: 0.8rem; font-weight: 600; text-align: left; flex: none; }
  .table td:not([data-label])::before, .table td[data-label=""]::before { content: none; }
  .table td.num, .table td.reason { text-align: right; max-width: none; }
  .table td.reason { display: block; text-align: left; }
  .table td.reason::before { display: block; margin-bottom: 2px; }
  .table td.empty { display: block; text-align: center; }
  .table td:last-child:not([data-label]) { justify-content: flex-end; padding-top: 8px; }

  /* studio, library, settings */
  .lib-grid { grid-template-columns: minmax(0, 1fr); }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric b { font-size: 1.1rem; }
  .save-bar { bottom: calc(80px + env(safe-area-inset-bottom)); }
  .save-bar .btn { flex: 1; }
  .kv dd { text-align: left; }
  .kv { grid-template-columns: 1fr; gap: 0; }
  .kv dt { margin-top: 8px; }
  .toasts { left: 12px; right: 12px; bottom: calc(84px + env(safe-area-inset-bottom)); max-width: none; }
  .dialog { padding: 20px; }

  /* assistant */
  .chat { min-height: calc(100dvh - 190px); }
  .msg { max-width: 94%; }
  .chat-compose { bottom: calc(76px + env(safe-area-inset-bottom)); border-radius: 14px; padding: 10px 12px; }
  .chat-compose > .small { display: none; }
  .chat-bar { gap: 12px; }
  .chat-bar .btn { min-height: 38px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
