:root {
  --bg: #0d1117;
  --bg-elev: #151b23;
  --bg-soft: #1b2230;
  --line: #262e3d;
  --text: #e6edf3;
  --muted: #8b98a9;
  --accent: #3fb6a8;
  --accent-dim: #266b63;
  --up: #35c48b;
  --down: #f0596b;
  --warn: #e3b341;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", Roboto, -apple-system, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { font-size: 22px; }
.brand strong { display: block; letter-spacing: .3px; }
.brand small { color: var(--muted); font-size: 12px; }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
select, input[type="date"], input[type="text"], input[type="number"] {
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 10px; font-size: 13px; font-family: inherit;
}
select:focus, input:focus { outline: 1px solid var(--accent); }

button, .btn {
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 12px; font-size: 13px; cursor: pointer;
  font-family: inherit; text-decoration: none; display: inline-block;
}
button:hover, .btn:hover { border-color: var(--accent-dim); }
button.primary { background: var(--accent); border-color: var(--accent); color: #06201d; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--down); }

.presets { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.presets button { border: 0; border-radius: 0; padding: 7px 11px; background: var(--bg-soft); }
.presets button.active { background: var(--accent-dim); color: #fff; }

/* abas de ano do grafico de ganho mensal */
.year-tabs { display: flex; gap: 2px; }
.year-tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent;
  border-radius: 0; padding: 4px 10px; color: var(--muted);
  font-size: 13px; font-weight: 600; letter-spacing: .3px;
}
.year-tabs button:hover { color: var(--text); }
.year-tabs button.active { color: var(--warn); border-bottom-color: var(--warn); }

/* ------------------------------------------------------------------ tabs */
.tabs {
  display: flex; gap: 4px; padding: 0 20px;
  background: var(--bg-elev); border-bottom: 1px solid var(--line);
  position: sticky; top: 61px; z-index: 19; overflow-x: auto;
}
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent;
  border-radius: 0; padding: 11px 14px; color: var(--muted); white-space: nowrap;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

/* Configuracao: engrenagem no canto direito da barra superior */
.gear-btn { display: inline-flex; align-items: center; justify-content: center; padding: 6px 8px; line-height: 1; color: var(--muted); }
.gear-btn:hover { color: var(--text); }
.gear-btn.active { color: #06201d; background: var(--accent); border-color: var(--accent); }
.gear-btn .ico-lock, .gear-btn.locked .ico-gear { display: none; }
.gear-btn.locked .ico-lock { display: inline; }
.refresh-info { white-space: nowrap; }
.refresh-info:empty { display: none; }

/* aba Operacoes (chao de fabrica ao vivo) em destaque */
.tabs { align-items: center; }
.tabs button.tab-featured {
  display: inline-flex; align-items: center; gap: 8px;
  margin: 6px 4px; padding: 6px 14px;
  border: 1px solid var(--accent); border-radius: 20px;
  background: rgba(63,182,168,.12); color: var(--accent);
  font-weight: 600;
}
.tabs button.tab-featured:hover { background: rgba(63,182,168,.22); }
.tabs button.tab-featured.active {
  background: var(--accent); color: #06201d; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(63,182,168,.25);
}
.tabs button.tab-featured small {
  font-size: 10px; letter-spacing: .6px; font-weight: 700;
  padding: 1px 6px; border-radius: 10px; background: rgba(227,179,65,.18); color: var(--warn);
}
.tabs button.tab-featured.active small { background: rgba(6,32,29,.18); color: #06201d; }
.live-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--down);
  animation: live-pulse 1.6s ease-out infinite;
}
.tabs button.tab-featured.active .live-dot { background: #06201d; }
@keyframes live-pulse {
  0% { box-shadow: 0 0 0 0 rgba(240,89,107,.6); }
  100% { box-shadow: 0 0 0 8px rgba(240,89,107,0); }
}
@media (prefers-reduced-motion: reduce) { .live-dot { animation: none; } }

main { padding: 20px; max-width: 1600px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

/* ------------------------------------------------------------------ cards */
.card {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.card-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin-bottom: 12px;
}
.card-head h3 { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: .2px; }
/* card recolhivel: o cabecalho abre/fecha; recolhido, some a margem de baixo */
.card-fold > summary { cursor: pointer; list-style: none; user-select: none; }
.card-fold > summary::-webkit-details-marker { display: none; }
.card-fold > summary h3::before { content: '▸'; display: inline-block; width: 1em; color: var(--muted);
  transition: transform .15s; }
.card-fold[open] > summary h3::before { transform: rotate(90deg); }
.card-fold:not([open]) > summary { margin-bottom: 0; }
.card-fold > summary:focus-visible { outline: 2px solid var(--line); outline-offset: 4px; border-radius: 6px; }
.hint { color: var(--muted); font-size: 12px; }
.row-actions { display: flex; align-items: center; gap: 10px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.explain { color: var(--muted); font-size: 13px; margin: 0 0 14px; max-width: 90ch; }

.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 380px; }

/* ------------------------------------------------------------------- kpis */
.kpis {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 12px; margin-bottom: 16px;
}
/* visão geral: 10 cards em 2 linhas de 5 */
.kpis.five { grid-template-columns: repeat(5, minmax(0, 1fr)); }
@media (max-width: 1100px) { .kpis.five { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 720px)  { .kpis.five { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.kpi {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
}
.kpi .label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .6px; }
.kpi .value { font-size: 21px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

.up { color: var(--up); }
.down { color: var(--down); }
.dim { color: var(--muted); }
.warn { color: var(--warn); }

/* ----------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child, th.left, td.left { text-align: left; }
th.center, td.center { text-align: center; }
td { vertical-align: middle; }
th {
  color: var(--muted); font-weight: 500; font-size: 11px;
  text-transform: uppercase; letter-spacing: .5px;
  position: sticky; top: 0; background: var(--bg-elev);
}
tbody tr:hover { background: var(--bg-soft); }
td.num { font-variant-numeric: tabular-nums; }
.tag {
  display: inline-block; padding: 1px 7px; border-radius: 20px;
  background: var(--bg-soft); border: 1px solid var(--line);
  font-size: 11px; color: var(--muted);
}
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 7px; }

.pager { display: flex; align-items: center; gap: 12px; justify-content: center; margin-top: 12px; }
.pager span { color: var(--muted); font-size: 12px; }

.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 6px 20px; }
.kv div { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.kv span:first-child { color: var(--muted); }
.kv span:last-child { font-variant-numeric: tabular-nums; }

/* -------------------------------------------------------------- ea cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; margin-bottom: 16px; }
.ea-card {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-left: 3px solid var(--accent); border-radius: var(--radius); padding: 14px;
}
.ea-card h4 { margin: 0 0 2px; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.ea-card .net { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; margin: 6px 0 10px; }
.ea-card .net-pct { font-size: 13px; font-weight: 500; margin-left: 8px; }
.ea-card .metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 12px; }
.ea-card .metrics div { display: flex; justify-content: space-between; }
.ea-card .metrics span:first-child { color: var(--muted); }
.ea-card .metrics .frac { font-size: 10px; opacity: .8; margin-left: 4px; }
.badge { font-size: 11px; padding: 1px 7px; border-radius: 20px; background: var(--bg-soft); color: var(--muted); }
.badge.live { background: rgba(53,196,139,.15); color: var(--up); }
.badge.cent { background: rgba(255,197,49,.16); color: var(--warn); font-weight: 700; letter-spacing: .05em; }

/* seletor de conta do topo + edicao de apelido / cent */
.acc-pick { position: relative; display: flex; align-items: center; gap: 4px; }
.acc-pick > select { max-width: 280px; }
.acc-edit { padding: 6px 9px; line-height: 1; }
.acc-form { position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; width: 280px; max-width: calc(100vw - 32px);
  display: flex; flex-direction: column; gap: 10px; padding: 14px;
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.acc-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.acc-form input, .acc-form select { width: 100%; box-sizing: border-box; }
.acc-order { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.acc-order > span { flex: 1; }
.acc-order button { padding: 4px 10px; line-height: 1; }
.acc-form-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ------------------------------------------------------------------ forms */
.rule-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.rule-form input[name="pattern"] { flex: 2 1 240px; }
.rule-form input[name="ea_name"] { flex: 1 1 180px; }
.rule-form input[name="priority"] { width: 90px; }
.rule-form input { background: var(--bg-soft); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; }

/* ------------------------------------------------- login / algo trading */
.auth-card { max-width: 440px; margin: 40px auto; }
.auth-card h3 { margin: 0 0 6px; font-size: 16px; }
.auth-form { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.auth-form input {
  background: var(--bg-soft); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; font-size: 15px;
}
.auth-form input[name="code"] { letter-spacing: 4px; font-variant-numeric: tabular-nums; }
.auth-form button { padding: 10px; font-size: 15px; }
.enroll { text-align: center; margin-top: 12px; font-size: 13px; }
.enroll img { background: #fff; border-radius: 8px; padding: 6px; width: 220px; max-width: 100%; }
.enroll code { word-break: break-all; font-size: 13px; letter-spacing: 1px; }

.session-bar { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-bottom: 12px; }

.algo { font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 20px; white-space: nowrap; }
.algo.on  { background: rgba(53,196,139,.15); color: var(--up); }
.algo.off { background: rgba(240,89,107,.15); color: var(--down); }
.algo.unk { background: var(--bg-soft); color: var(--muted); }
.ov-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 0 0 12px; }
.ov-status:empty { display: none; }
.ov-status-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.cmd-aguardando { color: var(--warn); }
.cmd-divergente, .cmd-expirado { color: var(--down); }
.cmd-aplicado { color: var(--up); }
td.warns { white-space: normal; min-width: 200px; max-width: 360px; font-size: 12px; line-height: 1.4; color: var(--warn); text-align: left; }
#tableTerminals td { padding-top: 10px; padding-bottom: 10px; }
#tableTerminals td.actions { white-space: nowrap; }
#tableTerminals td.actions button + button { margin-left: 6px; }
button.t-toggle { min-width: 92px; }

/* ------------------------------------------------------------------ misc */
.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 18px; }
.empty p { max-width: 60ch; margin: 10px auto 22px; }
code { background: var(--bg-soft); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 60;
  background: var(--bg-soft); border: 1px solid var(--accent-dim);
  padding: 12px 16px; border-radius: var(--radius); max-width: 420px;
  box-shadow: 0 12px 32px rgba(0,0,0,.5);
}
.toast.error { border-color: var(--down); }

.loader {
  position: fixed; inset: 0; z-index: 70; display: flex;
  align-items: center; justify-content: center; gap: 12px; flex-direction: column;
  background: rgba(13,17,23,.75); color: var(--muted);
}
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
  .tabs { top: 105px; }
  .chart-box { height: 220px; }
  .auth-card { margin: 16px auto; }
}

/* rotulo com checkbox na mesma linha (trava de fim de semana) */
label.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; min-height: 40px; }

/* legendas e textos pequenos: amarelo em vez de vermelho (vermelho fica para valores) */
.hint .down, .sub .down, .tag.down { color: var(--warn); }
.tag.warn { color: var(--warn); border-color: rgba(227,179,65,.4); }

/* ajuda "?": botao ao lado do titulo; o clique abre o balao (#helpPop, criado pelo app.js) */
.help-btn {
  display: inline-flex; align-items: center; justify-content: center; vertical-align: middle;
  width: 18px; height: 18px; padding: 0; margin-left: 6px; border-radius: 50%; flex: none;
  border: 1px solid var(--line); background: var(--bg-soft); color: var(--muted);
  font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-transform: none; letter-spacing: 0; cursor: pointer;
}
.help-btn:hover, .help-btn[aria-expanded="true"] { color: var(--text); border-color: var(--muted); }
.help-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.help-pop {
  position: fixed; z-index: 1000; max-width: min(340px, calc(100vw - 16px)); padding: 9px 12px;
  background: var(--bg-soft); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.45); color: var(--text); font-size: 13px; line-height: 1.5;
  white-space: pre-line;
}
.help-pop.hidden { display: none; }
/* avisos que continuam visiveis embaixo dos graficos/tabelas (a explicacao foi para o "?") */
.hint:empty { display: none; }
