:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --pos: #2a78d6;          /* polaridade: lucro */
  --neg: #e34948;          /* polaridade: prejuízo */
  --pos-wash: rgba(42, 120, 214, 0.10);
  --up-text: #006300;
  --down-text: #b42f2f;
  --warn-bg: #fff4dc;
  --warn-ink: #6b4a00;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --pos: #3987e5;
    --neg: #e66767;
    --pos-wash: rgba(57, 135, 229, 0.12);
    --up-text: #0ca30c;
    --down-text: #e66767;
    --warn-bg: #3a2f12;
    --warn-ink: #f5d58a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d; --surface: #1a1a19; --surface-2: #232322; --text: #f0efec; --text-2: #c3c2b7;
  --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --pos: #3987e5; --neg: #e66767; --pos-wash: rgba(57, 135, 229, 0.12);
  --up-text: #0ca30c; --down-text: #e66767; --warn-bg: #3a2f12; --warn-ink: #f5d58a; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 20px; }
h3 { font-size: 15px; }
.muted { color: var(--muted); font-weight: 400; }
.sub { color: var(--text-2); font-size: 13px; margin: 2px 0 0; }
.num { font-variant-numeric: tabular-nums; }
.up { color: var(--up-text); }
.down { color: var(--down-text); }

/* ---------- topo ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 17px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs a {
  padding: 7px 12px; border-radius: 8px; color: var(--text-2);
  text-decoration: none; font-weight: 500; white-space: nowrap;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; outline: none; }

/* ---------- botões e formulários ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; font-weight: 500;
  padding: 8px 14px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--down-text); }
.btn:focus-visible, .tabs a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .seg button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.icon-btn { appearance: none; border: 0; background: none; color: var(--text-2); font-size: 24px; line-height: 1; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); }

input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); font-weight: 500; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px 12px; margin: 0; }
legend { font-size: 13px; font-weight: 600; padding: 0 6px; }
.hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; }
.hint:empty { display: none; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid.inner { grid-template-columns: repeat(3, 1fr); }
.span-2 { grid-column: 1 / -1; }
.with-btn { display: flex; gap: 6px; }
.net-preview { display: flex; align-items: center; min-height: 38px; font-size: 15px; font-weight: 600; color: var(--text); }
.form-error { color: var(--down-text); font-size: 13px; margin: 10px 0 0; min-height: 0; }
.form-error:empty { display: none; }

/* ---------- diálogos ---------- */
.dialog {
  width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 32px);
  border: 1px solid var(--border); border-radius: 14px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.dialog.narrow { width: min(520px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.dialog form { padding: 18px 20px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.dialog-foot { display: flex; gap: 8px; margin-top: 16px; align-items: center; }
.spacer { flex: 1; }

/* ---------- blocos ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin-top: 16px; }
.grid-2 > .card + .card { margin-top: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }

.filters { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.filters select { width: auto; min-width: 200px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button {
  appearance: none; border: 0; background: none; font: inherit; font-size: 13px; font-weight: 500;
  color: var(--text-2); padding: 5px 11px; border-radius: 7px; cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--border); }

.notice { background: var(--warn-bg); color: var(--warn-ink); border-radius: 10px; padding: 10px 14px; font-size: 13px; margin-bottom: 16px; }

/* ---------- painel ---------- */
.hero { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; flex-wrap: wrap; margin-bottom: 16px; }
.hero-label { font-size: 13px; color: var(--text-2); font-weight: 500; }
.hero-value { font-size: 52px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.05; margin-top: 4px; }
.hero-side { display: flex; gap: 28px; flex-wrap: wrap; }
.hero-side div { display: flex; flex-direction: column; }
.hero-side .v { font-size: 20px; font-weight: 600; }
.hero-side .l { font-size: 12px; color: var(--text-2); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .l { font-size: 13px; color: var(--text-2); font-weight: 500; }
.tile .v { font-size: 24px; font-weight: 600; margin-top: 4px; letter-spacing: -0.01em; }
.tile .h { font-size: 12px; color: var(--muted); margin-top: 2px; }

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--muted); font: 11px var(--font); font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .line { fill: none; stroke: var(--pos); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--pos-wash); }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .dot { fill: var(--pos); stroke: var(--surface); stroke-width: 2; }
.chart .bar-pos { fill: var(--pos); }
.chart .bar-neg { fill: var(--neg); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar-hover, .chart .bar-hover { pointer-events: none; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.15);
  min-width: 140px;
}
.tooltip .tt-title { color: var(--text-2); margin-bottom: 4px; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; }
.tooltip .tt-row b { font-variant-numeric: tabular-nums; }

/* ---------- tabelas ---------- */
.table-wrap { overflow-x: auto; margin: 0 -18px -18px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; }
th:first-child, td:first-child { padding-left: 18px; }
th:last-child, td:last-child { padding-right: 18px; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); }
.badge.real { background: var(--accent); color: var(--accent-ink); }
.badge.buy::before { content: "▲ "; color: var(--pos); }
.badge.sell::before { content: "▼ "; color: var(--neg); }
.badge.open { background: var(--warn-bg); color: var(--warn-ink); }

.breakdown td { padding-top: 8px; padding-bottom: 8px; }
.bd-bar { position: relative; width: 120px; height: 10px; }
.bd-bar::before { content: ""; position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--axis); }
.bd-bar span { position: absolute; top: 0; height: 10px; }
.bd-bar .p { left: 50%; background: var(--pos); border-radius: 0 3px 3px 0; }
.bd-bar .n { right: 50%; background: var(--neg); border-radius: 3px 0 0 3px; }

/* ---------- contas ---------- */
.accounts { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.account { display: flex; flex-direction: column; gap: 12px; }
.account .top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.account .bal { font-size: 28px; font-weight: 600; letter-spacing: -0.01em; }
.account dl { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin: 0; font-size: 13px; }
.account dt { color: var(--text-2); }
.account dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.account .actions { display: flex; gap: 8px; margin-top: auto; }

/* ---------- importar ---------- */
.drop {
  border: 2px dashed var(--axis); border-radius: var(--radius); padding: 32px; text-align: center;
  background: var(--surface); cursor: pointer; transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--accent); background: var(--pos-wash); }
.drop strong { display: block; font-size: 16px; margin-bottom: 4px; }
ol.steps { margin: 8px 0 0; padding-left: 20px; color: var(--text-2); font-size: 14px; }
ol.steps li + li { margin-top: 4px; }
.result-list { font-size: 13px; color: var(--text-2); margin: 8px 0 0; padding-left: 18px; }

.empty { text-align: center; padding: 48px 16px; }
.empty h2 { margin-bottom: 6px; }
.empty p { color: var(--text-2); margin: 0 auto 18px; max-width: 460px; }
.empty .row { display: inline-flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.calc-out { font-size: 40px; font-weight: 600; letter-spacing: -0.02em; margin: 6px 0 2px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 14px; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 60;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 16px; }
  .tabs { order: 3; flex-basis: 100%; }
  #btn-new-trade { margin-left: auto; }
  main { padding: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-grid.inner { grid-template-columns: 1fr 1fr; }
  .hero-value { font-size: 40px; }
  .tiles { grid-template-columns: 1fr 1fr; gap: 10px; }
  .tile { padding: 12px; }
  .tile .v { font-size: 20px; }
  .filters select { min-width: 0; flex: 1; }
  .table-wrap { margin: 0 -18px -18px; }
}
