:root {
  --bg: #0e1318;
  --panel: #151c23;
  --panel-2: #1b242d;
  --line: #26313c;
  --text: #e8eef3;
  --muted: #9aa8b4;
  --faint: #6f7d89;
  --accent: #5aa9ff;
  --accent-ink: #06121f;
  --gain: #45c486;
  --loss: #ff6b61;
  --sim: #f0b93f;
  --sim-ink: #2a1d00;
  --live: #ff4b4b;
  --live-ink: #fff;
  --warn: #f0b93f;
  --r: 14px;
  --r-sm: 10px;
  --tabs-h: 64px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
body { min-height: 100dvh; }
::selection { background: color-mix(in srgb, var(--accent) 40%, transparent); }
button, input, select { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--accent); text-underline-offset: 3px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.num, .money { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.gain { color: var(--gain); }
.loss { color: var(--loss); }
.err { color: var(--loss); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }
[hidden] { display: none !important; }

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; }
.login-card h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -0.02em; }

/* ---------- Header: modalità + stato ---------- */
.top { position: sticky; top: 0; z-index: 20; background: var(--bg); padding-top: env(safe-area-inset-top); }
.modebar { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 7px 16px; font-weight: 700; font-size: 13px; letter-spacing: 0.04em; text-transform: uppercase; }
.modebar.sim { background: var(--sim); color: var(--sim-ink); }
.modebar.live { background: var(--live); color: var(--live-ink); }
.modebar small { font-weight: 600; text-transform: none; letter-spacing: 0; opacity: .85; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.runstate { display: flex; align-items: center; gap: 10px; min-width: 0; }
.runstate .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--faint); }
.runstate .dot.on { background: var(--gain); box-shadow: 0 0 0 0 color-mix(in srgb, var(--gain) 60%, transparent); animation: beat 2s ease-out infinite; }
.runstate .dot.off { background: var(--loss); }
.runstate .dot.stale { background: var(--warn); }
.runstate b { display: block; font-size: 15px; }
.runstate span { display: block; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@keyframes beat { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--gain) 55%, transparent); } 70%, 100% { box-shadow: 0 0 0 8px transparent; } }
@media (prefers-reduced-motion: reduce) { .runstate .dot.on { animation: none; } }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px; border-radius: var(--r-sm); border: 1px solid var(--line); background: var(--panel-2); font-weight: 600; cursor: pointer; transition: background-color .15s, border-color .15s, transform .1s; -webkit-tap-highlight-color: transparent; }
.btn:hover { border-color: #3a4855; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--live); border-color: var(--live); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn.block { width: 100%; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after { content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--text); border-right-color: transparent; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.runbtn { min-width: 112px; font-weight: 800; letter-spacing: .02em; }
.runbtn.stop { background: var(--live); border-color: var(--live); color: #fff; }
.runbtn.start { background: var(--gain); border-color: var(--gain); color: #04150c; }

/* ---------- Layout ---------- */
.view { padding: 16px 16px calc(var(--tabs-h) + 24px + env(safe-area-inset-bottom)); max-width: 760px; margin: 0 auto; outline: none; }
.section { margin-top: 28px; }
.section:first-child { margin-top: 4px; }
.section > h2 { font-size: 17px; margin: 0 0 10px; letter-spacing: -0.01em; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.section > h2 .link { font-size: 14px; font-weight: 600; color: var(--accent); background: none; border: 0; padding: 6px 0; cursor: pointer; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); }
.pad { padding: 16px; }

/* ---------- Saldo ---------- */
.balance { padding: 18px 16px 16px; }
.balance .label { color: var(--muted); font-size: 13.5px; }
.balance .big { font-size: 38px; font-weight: 750; letter-spacing: -0.03em; line-height: 1.1; margin: 2px 0 6px; }
.balance .pnl { font-size: 15px; font-weight: 600; }
.split { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); margin-top: 14px; }
.split > div { padding: 12px 0 0; }
.split > div + div { padding-left: 14px; border-left: 1px solid var(--line); }
.split > div.full { grid-column: 1 / -1; padding-left: 0; border-left: 0; margin-top: 12px; border-top: 1px solid var(--line); }
.split dt { color: var(--muted); font-size: 12.5px; }
.split dd { margin: 2px 0 0; font-size: 17px; font-weight: 650; }

/* ---------- Limiti ---------- */
.meter { padding: 14px 16px; }
.meter + .meter { border-top: 1px solid var(--line); }
.meter .row { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.meter .row b { font-weight: 650; }
.bar { height: 6px; border-radius: 3px; background: var(--panel-2); margin-top: 8px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .3s ease-out; }
.bar.hot > i { background: var(--warn); }
.bar.full > i { background: var(--loss); }

/* ---------- Avvisi ---------- */
.notice { display: flex; gap: 12px; align-items: flex-start; padding: 14px 16px; border-radius: var(--r); background: color-mix(in srgb, var(--warn) 12%, var(--panel)); border: 1px solid color-mix(in srgb, var(--warn) 45%, var(--line)); margin-bottom: 12px; }
.notice svg { color: var(--warn); margin-top: 1px; }
.notice.bad { background: color-mix(in srgb, var(--loss) 12%, var(--panel)); border-color: color-mix(in srgb, var(--loss) 45%, var(--line)); }
.notice.bad svg { color: var(--loss); }
.notice p { margin: 0; font-size: 14px; }
.notice b { display: block; margin-bottom: 2px; font-size: 14.5px; }

/* ---------- Liste ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.item { padding: 13px 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; }
.item + .item { border-top: 1px solid var(--line); }
.item .head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.item .title { font-weight: 600; font-size: 14.5px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.item .meta { color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.item .reason { font-size: 13.5px; color: var(--text); }
.item .reason.dim { color: var(--muted); }
.item .actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.item .right { text-align: right; white-space: nowrap; font-weight: 650; }

.chip { display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: 6px; font-size: 12px; font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.chip.buy { background: color-mix(in srgb, var(--gain) 18%, transparent); color: var(--gain); }
.chip.sell { background: color-mix(in srgb, var(--loss) 18%, transparent); color: var(--loss); }
.chip.copied { background: var(--accent); color: var(--accent-ink); }
.chip.skipped { background: var(--panel-2); color: var(--muted); border: 1px solid var(--line); }
.chip.error { background: var(--loss); color: #fff; }
.chip.waiting { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--warn); }
.chip.yes { background: color-mix(in srgb, var(--gain) 14%, transparent); color: var(--gain); }
.chip.no { background: color-mix(in srgb, var(--loss) 14%, transparent); color: var(--loss); }
.chip.outcome { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }

.empty { padding: 28px 20px; text-align: center; }
.empty h3 { margin: 0 0 6px; font-size: 16px; }
.empty p { margin: 0 auto 14px; color: var(--muted); font-size: 14px; max-width: 34ch; }

.skeleton { height: 64px; border-radius: var(--r); background: linear-gradient(90deg, var(--panel) 0%, var(--panel-2) 50%, var(--panel) 100%); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; margin-bottom: 10px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Segmenti / filtri ---------- */
.seg { display: flex; padding: 3px; gap: 3px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 14px; }
.seg button { flex: 1; min-height: 38px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 650; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.35); }
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.filters button { flex: none; min-height: 36px; padding: 0 14px; border-radius: 18px; border: 1px solid var(--line); background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; }
.filters button[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ---------- Trader ---------- */
.trader .name { font-weight: 650; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trader .addr { font-size: 12.5px; color: var(--faint); font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.stats div { background: var(--panel-2); border-radius: 10px; padding: 8px 10px; }
.stats dt { font-size: 11.5px; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-weight: 650; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); font-size: 13.5px; }
.detail ul { margin: 6px 0 0; padding: 0; list-style: none; }
.detail li { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; }
.detail li span:first-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail li span:last-child { flex: none; }
.item, .item .head > div { min-width: 0; }

/* Switch */
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch i { position: absolute; inset: 0; border-radius: 15px; background: var(--panel-2); border: 1px solid var(--line); transition: background-color .15s; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: var(--muted); transition: transform .18s ease-out, background-color .15s; }
.switch input:checked + i { background: var(--gain); border-color: var(--gain); }
.switch input:checked + i::after { transform: translateX(20px); background: #fff; }
.switch input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Form regole ---------- */
.form-group { padding: 4px 16px; }
.field { display: grid; gap: 6px; padding: 12px 0; }
.form-group .field + .field, .form-group .field + .toggle, .form-group .toggle + .field { border-top: 1px solid var(--line); }
.field > span { font-weight: 600; font-size: 14.5px; }
.field small, .toggle small { color: var(--muted); font-size: 13px; }
.field input, .field select { width: 100%; min-height: 46px; padding: 0 12px; border-radius: var(--r-sm); border: 1px solid var(--line); background: var(--bg); font-variant-numeric: tabular-nums; font-size: 16px; }
.field input:focus, .field select:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.field input[aria-invalid="true"] { border-color: var(--loss); }
.inputwrap { position: relative; }
.inputwrap em { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-style: normal; pointer-events: none; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 0; }
.toggle > div > span { display: block; font-weight: 600; font-size: 14.5px; }
.savebar { position: sticky; bottom: calc(var(--tabs-h) + 10px + env(safe-area-inset-bottom)); margin-top: 16px; display: flex; gap: 10px; padding: 10px; border-radius: var(--r); background: var(--panel-2); border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.45); }
.savebar .btn { flex: 1; }

.livebox { padding: 16px; }
.livebox h3 { margin: 0 0 6px; font-size: 16px; }
.livebox p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.livebox ul { margin: 0 0 14px; padding-left: 18px; color: var(--muted); font-size: 14px; }

/* ---------- Tab bar ---------- */
.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: repeat(6, 1fr); height: calc(var(--tabs-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); background: color-mix(in srgb, var(--bg) 94%, transparent); border-top: 1px solid var(--line); backdrop-filter: blur(10px); }
.tab { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border: 0; background: none; color: var(--faint); font-size: 11.5px; font-weight: 600; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.tab svg { width: 22px; height: 22px; }
.tab[aria-current="page"] { color: var(--text); }
.tab[aria-current="page"] svg { color: var(--accent); }

/* ---------- Dialog ---------- */
.sheet { border: 1px solid var(--line); border-radius: 18px 18px 0 0; background: var(--panel); color: var(--text); padding: 20px 18px calc(18px + env(safe-area-inset-bottom)); width: 100%; max-width: 560px; margin: auto auto 0; max-height: 90dvh; }
.sheet::backdrop { background: rgba(4, 8, 12, .7); }
.sheet[open] { animation: up .22s cubic-bezier(.16, 1, .3, 1); }
@keyframes up { from { transform: translateY(24px); opacity: 0; } }
.sheet h3 { margin: 0 0 8px; font-size: 18px; }
.sheet p { margin: 0 0 12px; color: var(--muted); }
.sheet .btns { display: flex; gap: 10px; margin-top: 16px; }
.sheet .btns .btn { flex: 1; }
@media (min-width: 640px) { .sheet { border-radius: 18px; margin: auto; } }

.toast { position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom)); transform: translate(-50%, 12px); max-width: calc(100% - 32px); padding: 12px 16px; border-radius: 12px; background: var(--text); color: var(--bg); font-weight: 600; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s ease-out; z-index: 50; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--loss); color: #fff; }

@media (min-width: 900px) {
  .view { max-width: 820px; }
}

.daystats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 0; }
.daystats > div { padding: 14px 16px; }
.daystats > div + div { border-left: 1px solid var(--line); }
.daystats dt { color: var(--muted); font-size: 12.5px; }
.daystats dd { margin: 2px 0 0; font-size: 22px; font-weight: 700; }
a.btn { text-decoration: none; color: var(--text); }
@media (min-width: 900px) {
  .topbar { max-width: 820px; margin: 0 auto; border-bottom: 0; }
  .top { border-bottom: 1px solid var(--line); }
  .tabs { grid-template-columns: repeat(6, minmax(0, 130px)); justify-content: center; }
}

.tab span { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.rep-week summary { list-style: none; cursor: pointer; }
.rep-week summary::-webkit-details-marker { display: none; }
.rep-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.rep-table td { padding: 6px 0; border-top: 1px solid var(--line); }
.rep-table td.num { text-align: right; white-space: nowrap; }

.jump { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 16px; }
.jump .chip { cursor: pointer; font: inherit; font-size: 13px; padding: 6px 12px; border: 1px solid var(--line); }
.section[id] { scroll-margin-top: 130px; }
