:root {
  --bg: #0b1020;
  --bg-2: #111831;
  --card: #151d38;
  --card-2: #1b2547;
  --line: #26325c;
  --text: #e8ecf8;
  --muted: #8b96b8;
  --accent: #6c7cff;
  --accent-2: #8f5bff;
  --up: #2ee6a6;
  --down: #ff5c7a;
  --warn: #f5c542;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  min-height: 100dvh;
  overscroll-behavior-y: contain;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.up { color: var(--up); }
.down { color: var(--down); }
.error-text { color: var(--down); min-height: 1.2em; font-size: 13px; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(380px, 100%); background: var(--card); border: 1px solid var(--line);
  border-radius: 22px; padding: 28px 22px; text-align: center; display: grid; gap: 12px;
}
.login-card h1 { margin: 6px 0 0; font-size: 22px; }
.login-card input { text-align: center; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: rgba(11, 16, 32, 0.86); backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(38, 50, 92, 0.6);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center;
  font-weight: 800; font-size: 14px; letter-spacing: .5px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 18px rgba(108, 124, 255, .35);
}
.brand-mark.big { width: 56px; height: 56px; border-radius: 16px; font-size: 20px; margin: 0 auto; }
.brand-title { font-weight: 700; }
.brand-sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--up); box-shadow: 0 0 0 3px rgba(46, 230, 166, .18); }
.dot.err { background: var(--down); }

.badge { font-size: 11px; font-weight: 700; padding: 4px 9px; border-radius: 999px; letter-spacing: .4px; }
.badge.mode { background: rgba(108, 124, 255, .18); color: #aab4ff; border: 1px solid rgba(108, 124, 255, .35); }
.badge.mode.real { background: rgba(255, 92, 122, .18); color: #ffb3c0; border-color: rgba(255, 92, 122, .45); }
.badge.mode.demo { background: rgba(245, 197, 66, .15); color: #ffe08a; border-color: rgba(245, 197, 66, .4); }

.banner {
  margin: 10px 16px 0; padding: 10px 12px; border-radius: 12px; font-size: 13px;
  background: rgba(255, 92, 122, .12); border: 1px solid rgba(255, 92, 122, .35); color: #ffc2cd;
}
.banner.warn { background: rgba(245, 197, 66, .1); border-color: rgba(245, 197, 66, .35); color: #ffe7a3; }

/* ---------- layout ---------- */
main { padding: 12px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px); max-width: 860px; margin: 0 auto; }
.view { display: none; animation: fade .18s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin: 20px 2px 10px; }
.section-head h2 { font-size: 15px; margin: 0; font-weight: 700; color: #cfd6ee; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; background: var(--card-2); border: 1px solid var(--line); font-size: 13px; }
.chip.small { font-size: 11px; padding: 3px 8px; }
.list { display: grid; gap: 10px; }
.empty { text-align: center; color: var(--muted); padding: 22px 10px; border: 1px dashed var(--line); border-radius: var(--radius); font-size: 14px; }

/* ---------- account ---------- */
.account { background: linear-gradient(145deg, #1b2550, #151d38 60%); }
.label { font-size: 12px; color: var(--muted); }
.value-xl { font-size: 30px; font-weight: 800; letter-spacing: -.5px; }
.value { font-size: 16px; font-weight: 700; }
.account-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }

.quick-toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.qt { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; cursor: pointer; }
.qt-title { font-weight: 700; font-size: 14px; }
.qt-sub { font-size: 11px; color: var(--muted); }

/* ---------- switch ---------- */
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: #2a355f; border-radius: 999px; transition: .2s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 2px 6px rgba(0,0,0,.3); }
.switch input:checked + span { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:disabled + span { opacity: .45; }

/* ---------- watchlist ---------- */
.inst { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; align-items: center; cursor: pointer; transition: transform .1s, border-color .2s; }
.inst:active { transform: scale(.985); }
.inst.flash-up { border-color: rgba(46, 230, 166, .6); }
.inst.flash-down { border-color: rgba(255, 92, 122, .6); }
.inst-name { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { width: 34px; height: 34px; border-radius: 10px; background: var(--card-2); display: grid; place-items: center; font-size: 11px; font-weight: 800; overflow: hidden; flex: none; }
.logo img { width: 100%; height: 100%; object-fit: cover; }
.inst-sym { font-weight: 800; }
.inst-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inst-price { text-align: right; font-variant-numeric: tabular-nums; }
.inst-price .p { font-weight: 700; }
.inst-foot { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; }
.sig { font-size: 11px; font-weight: 800; padding: 3px 8px; border-radius: 8px; white-space: nowrap; }
.sig.long { background: rgba(46, 230, 166, .14); color: var(--up); }
.sig.short { background: rgba(255, 92, 122, .14); color: var(--down); }
.sig.neutral { background: rgba(139, 150, 184, .14); color: var(--muted); }
.sig.hot { box-shadow: 0 0 0 1px currentColor inset; }
.bar { flex: 1; height: 6px; background: #222c52; border-radius: 99px; overflow: hidden; position: relative; }
.bar i { position: absolute; top: 0; bottom: 0; left: 0; border-radius: 99px; background: var(--muted); transition: width .4s; }
.bar i.long { background: linear-gradient(90deg, #1fb985, var(--up)); }
.bar i.short { background: linear-gradient(90deg, #d8425f, var(--down)); }
.bar .th { position: absolute; top: -2px; bottom: -2px; width: 2px; background: #fff; opacity: .45; }
.trend { font-size: 11px; color: var(--muted); min-width: 52px; text-align: right; }

.ai-view { margin-top: 14px; font-size: 13px; }
.ai-view b { color: #c9b8ff; }

/* ---------- trades ---------- */
.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 10px; text-align: center; }
.stat .value { font-size: 15px; }
.trade { display: grid; gap: 10px; }
.trade-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.trade-title { display: flex; align-items: center; gap: 8px; font-weight: 800; }
.trade-pnl { text-align: right; font-weight: 800; font-size: 17px; font-variant-numeric: tabular-nums; }
.trade-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; font-size: 13px; }
.trade-grid div span { display: block; font-size: 11px; color: var(--muted); }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--card-2); border: 1px solid var(--line); color: #c6cde6; }
.tag.good { border-color: rgba(46, 230, 166, .45); color: var(--up); }
.tag.ai { border-color: rgba(143, 91, 255, .5); color: #c9b8ff; }
.rbar { height: 6px; border-radius: 99px; background: #222c52; position: relative; overflow: hidden; }
.rbar i { position: absolute; top: 0; bottom: 0; }
.row-mini { display: flex; justify-content: space-between; align-items: center; font-size: 13px; padding: 10px 12px; }
.row-mini .l { display: flex; gap: 8px; align-items: center; min-width: 0; }
.row-mini .l .muted { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ext-pos { display: grid; gap: 8px; padding: 0 0 10px; }
.ext-pos .btn { margin: 0 12px; }
.ext-hint { margin: -4px 2px 10px; }

/* ---------- signals / events ---------- */
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.segmented button { background: none; border: 0; padding: 9px; border-radius: 9px; font-weight: 700; color: var(--muted); cursor: pointer; }
.segmented button.active { background: var(--card-2); color: var(--text); }
.sigcard { display: grid; gap: 6px; }
.sigcard .top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sigcard .why { font-size: 12px; color: var(--muted); }
.act { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 6px; background: var(--card-2); }
.act.aperto { background: rgba(46, 230, 166, .15); color: var(--up); }
.act.veto_ia, .act.errore_ia { background: rgba(255, 92, 122, .12); color: #ff9cb0; }
.act.proposta_ia { background: rgba(143, 91, 255, .18); color: #c9b8ff; }
.event { display: grid; grid-template-columns: 8px 1fr; gap: 10px; font-size: 13px; padding: 10px 12px; }
.event .lv { width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; background: var(--muted); }
.event .lv.success { background: var(--up); }
.event .lv.warning { background: var(--warn); }
.event .lv.error { background: var(--down); }
.event .lv.info { background: var(--accent); }
.event time { display: block; font-size: 11px; color: var(--muted); }

/* ---------- settings ---------- */
.set-card { margin-bottom: 12px; }
.set-card > summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; font-weight: 800; font-size: 15px; }
.set-card > summary::-webkit-details-marker { display: none; }
.set-card > summary::after { content: "›"; transform: rotate(90deg); transition: .2s; color: var(--muted); font-size: 20px; }
.set-card[open] > summary::after { transform: rotate(-90deg); }
.set-card .body { margin-top: 12px; display: grid; gap: 12px; }
.set-desc { font-size: 12px; color: var(--muted); margin: -4px 0 0; }
.field { display: grid; gap: 6px; }
.field > label, .field .lbl { font-size: 13px; color: #c6cde6; font-weight: 600; }
.field .hint { font-size: 11px; color: var(--muted); }
.field.inline { grid-template-columns: 1fr 140px; align-items: center; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 12px; outline: none; min-height: 44px;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(108, 124, 255, .2); }
input[type=range] { width: 100%; accent-color: var(--accent); }
.toggle-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; cursor: pointer; }
.toggle-row .t { font-weight: 700; }
.toggle-row .d { font-size: 12px; color: var(--muted); }
.ai-master { background: linear-gradient(135deg, rgba(108, 124, 255, .15), rgba(143, 91, 255, .12)); border: 1px solid rgba(143, 91, 255, .35); border-radius: 14px; padding: 12px; }
.options { display: grid; gap: 8px; }
.opt { display: grid; grid-template-columns: 22px 1fr; gap: 10px; align-items: start; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-2); cursor: pointer; }
.opt input { margin: 3px 0 0; accent-color: var(--accent); width: 18px; height: 18px; }
.opt .t { font-weight: 700; font-size: 14px; }
.opt .d { font-size: 12px; color: var(--muted); }
.opt:has(input:checked) { border-color: var(--accent); background: rgba(108, 124, 255, .1); }
.opt .rec { font-size: 10px; font-weight: 800; color: var(--up); margin-left: 6px; }
.info { font-size: 12px; color: #c6cde6; background: rgba(74, 163, 255, .08); border: 1px solid rgba(74, 163, 255, .25); border-radius: 12px; padding: 10px 12px; }
.info ol, .info ul { margin: 6px 0 0; padding-left: 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips .chip button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0 0 0 2px; font-size: 14px; }
.search-results { display: grid; gap: 6px; }
.search-results button { display: flex; justify-content: space-between; text-align: left; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; cursor: pointer; }
.save-bar {
  position: sticky; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px); z-index: 5; margin-top: 8px;
  padding: 8px; border-radius: 18px; background: rgba(11, 16, 32, .92); backdrop-filter: blur(10px);
  box-shadow: 0 -6px 24px rgba(0, 0, 0, .35); animation: fade .2s ease;
}
.danger-zone { border-color: rgba(255, 92, 122, .4); }

/* ---------- buttons ---------- */
.btn { border: 0; border-radius: 12px; padding: 12px 14px; font-weight: 800; cursor: pointer; min-height: 46px; transition: transform .1s, opacity .2s; }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.block { width: 100%; }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; box-shadow: 0 8px 22px rgba(108, 124, 255, .3); }
.btn.ghost { background: var(--card-2); border: 1px solid var(--line); }
.btn.buy { background: linear-gradient(135deg, #16b981, var(--up)); color: #04241a; }
.btn.sell { background: linear-gradient(135deg, #d63d5b, var(--down)); color: #fff; }
.btn.small { min-height: 36px; padding: 7px 12px; font-size: 13px; border-radius: 10px; }
.btn.danger { background: rgba(255, 92, 122, .15); color: #ff9cb0; border: 1px solid rgba(255, 92, 122, .4); }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-row:has(> :only-child) { grid-template-columns: 1fr; }
.icon-btn { background: var(--card-2); border: 1px solid var(--line); border-radius: 50%; width: 36px; height: 36px; cursor: pointer; flex: none; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 8px calc(6px + var(--safe-bottom));
  background: rgba(13, 19, 40, .94); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab { background: none; border: 0; display: grid; justify-items: center; gap: 3px; padding: 6px 0; color: var(--muted); cursor: pointer; font-size: 11px; font-weight: 600; }
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: #fff; }
.tab.active svg { stroke: #aab4ff; }

/* ---------- sheet (detail) ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(3, 6, 15, .6); backdrop-filter: blur(2px); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 94dvh; overflow-y: auto;
  background: var(--bg-2); border-top-left-radius: 22px; border-top-right-radius: 22px;
  border: 1px solid var(--line); padding: 8px 16px calc(18px + var(--safe-bottom));
  animation: up .22s ease; max-width: 860px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(30px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet-grip { width: 42px; height: 5px; border-radius: 9px; background: var(--line); margin: 4px auto 10px; }
.sheet-head { display: flex; align-items: center; gap: 10px; }
.d-symbol { font-size: 20px; font-weight: 800; }
.d-price-wrap { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
.d-price { font-size: 20px; font-weight: 800; }
.chart-wrap { margin-top: 12px; height: 260px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.chart-wrap canvas { width: 100%; height: 100%; display: block; touch-action: pan-y; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 11px; color: var(--muted); margin: 8px 2px; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.d-signal { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.ind-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ind { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.ind span { display: block; font-size: 11px; color: var(--muted); }
.ind b { font-variant-numeric: tabular-nums; }
.reasons { margin: 12px 0; padding-left: 18px; font-size: 13px; color: #c6cde6; display: grid; gap: 4px; }
.ai-box { background: linear-gradient(135deg, rgba(108, 124, 255, .12), rgba(143, 91, 255, .1)); border: 1px solid rgba(143, 91, 255, .35); border-radius: 14px; padding: 12px; font-size: 13px; margin-bottom: 12px; display: grid; gap: 6px; }
.trade-box { display: grid; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; left: 0; right: 0; top: calc(64px + var(--safe-top)); z-index: 80; display: grid; justify-items: center; gap: 8px; pointer-events: none; padding: 0 12px; }
.toast { pointer-events: auto; max-width: 520px; width: 100%; background: #1f2a52; border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; font-size: 13px; box-shadow: 0 10px 30px rgba(0,0,0,.35); animation: fade .2s ease; }
.toast.success { border-color: rgba(46, 230, 166, .5); }
.toast.error { border-color: rgba(255, 92, 122, .6); }
.toast.warning { border-color: rgba(245, 197, 66, .5); }

.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.25); border-top-color: #fff; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 720px) {
  .list.grid-wide { grid-template-columns: 1fr 1fr; }
  #watchlist { grid-template-columns: 1fr 1fr; }
  .sheet-panel { bottom: 20px; border-radius: 22px; max-height: 90dvh; }
  .chart-wrap { height: 340px; }
}
@media (max-width: 380px) {
  .quick-toggles { grid-template-columns: 1fr; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .value-xl { font-size: 26px; }
}
