:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #16191f;
  --muted: #667085;
  --border: #e3e6ea;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --ok: #12855b;
  --bad: #c92a2a;
  --warn: #b26a00;
  --warn-bg: #fff4e0;
  --crit-bg: #fdecec;
  --ok-bg: #e6f6ee;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1d222b;
    --text: #e6e8ec;
    --muted: #8a93a3;
    --border: #2a303b;
    --accent: #6d8cff;
    --accent-text: #0e1116;
    --ok: #3ccf8e;
    --bad: #ff6b6b;
    --warn: #ffb547;
    --warn-bg: #2d2412;
    --crit-bg: #321a1c;
    --ok-bg: #12291f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
h3 { font-size: 14px; }

.container { max-width: 1280px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.topbar nav a { padding: 6px 10px; border-radius: 6px; color: var(--muted); }
.topbar nav a.active, .topbar nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.logout { margin: 0; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
@media (max-width: 900px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi { display: flex; flex-direction: column; gap: 2px; min-height: 92px; }
.kpi .label, .stats-grid .label { color: var(--muted); font-size: 12px; display: block; }
.kpi .value { font-size: 22px; font-weight: 600; }
.kpi .sub { font-size: 12px; color: var(--muted); }
.skeleton { background: var(--surface-2); }

.chart-box { position: relative; height: 280px; }

/* text helpers */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; margin: 4px 0 0; }
.ok { color: var(--ok); }
.bad { color: var(--bad); }
.kpi .sub.ok { color: var(--ok); }
.kpi .sub.bad { color: var(--bad); }
.empty { color: var(--muted); padding: 16px 0; text-align: center; }
.spacer { flex: 1; }

/* pills */
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); margin: 1px 2px; white-space: nowrap; }
.pill.ok { color: var(--ok); border-color: currentColor; }
.pill.bad, .pill.critical { color: var(--bad); border-color: currentColor; background: var(--crit-bg); }
.pill.warning { color: var(--warn); border-color: currentColor; background: var(--warn-bg); }
.pill.muted { color: var(--muted); }
#live-status.ok { color: var(--ok); }
#live-status.bad { color: var(--bad); }
.side { font-weight: 600; font-size: 12px; }
.side.buy { color: var(--ok); }
.side.sell { color: var(--bad); }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 500; }
td.num, th.num { text-align: right; }
tbody tr:hover { background: var(--surface-2); }

/* forms */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 6px; }
.btn {
  font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  padding: 7px 14px; border-radius: 6px; white-space: nowrap; display: inline-block;
}
.btn:hover { border-color: var(--muted); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 3px 10px; font-size: 12px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: wait; }
button.link { background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; }
.inline, form.inline { display: inline; margin: 0; }
.inline-form, .filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.filters .grow { flex: 1; min-width: 200px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 12px; align-items: end; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.flash { padding: 8px 12px; border-radius: 6px; margin: 0; }
.flash.error { background: var(--crit-bg); color: var(--bad); }
.flash.ok { background: var(--ok-bg); color: var(--ok); }
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; }

/* alerts */
.alerts { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 360px; overflow-y: auto; }
.alert { display: flex; justify-content: space-between; gap: 12px; align-items: start; padding: 10px 12px; border-radius: 8px; border-left: 3px solid var(--warn); background: var(--warn-bg); }
.alert.critical { border-left-color: var(--bad); background: var(--crit-bg); }
.alert p { margin: 2px 0; }

/* analysis */
.analysis-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }

/* rules */
.rules { display: flex; flex-direction: column; gap: 8px; }
.rule { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; }
.rule.disabled { opacity: .55; }
.rule summary { display: flex; align-items: center; gap: 8px; cursor: pointer; flex-wrap: wrap; list-style: none; }
.rule summary::-webkit-details-marker { display: none; }
.rule-form { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.rule-form textarea { width: 100%; }

/* lessons */
.lessons { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lesson details { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; }
.lesson summary { cursor: pointer; display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; list-style: none; }
.lesson summary::-webkit-details-marker { display: none; }
.lesson-text { flex: 1; min-width: 240px; }

/* reviews */
.review { border-top: 1px solid var(--border); padding: 12px 0; }
.review header { display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 12px; }

/* chat */
.chat-layout { flex-direction: row; align-items: stretch; height: calc(100vh - 56px); }
.chat-sidebar { width: 260px; flex-shrink: 0; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }
.conversations { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.conversations li { padding: 6px 8px; border-radius: 6px; display: flex; flex-direction: column; }
.conversations li.active { background: var(--surface-2); }
.conversations a { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 4px; }
.msg { display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg .bubble { max-width: min(760px, 92%); padding: 10px 14px; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--accent); color: var(--accent-text); white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--surface-2); }
.bubble table { font-size: 13px; }
.bubble pre { overflow-x: auto; }
.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }
.tool-trace { margin-top: 4px; }
.typing { color: var(--muted); font-style: italic; }
.chat-form { display: flex; gap: 8px; margin-top: 12px; align-items: end; }
.chat-form textarea { flex: 1; resize: vertical; }
@media (max-width: 800px) {
  .chat-layout { flex-direction: column; height: auto; }
  .chat-sidebar { width: auto; max-height: 200px; }
  .chat-main { min-height: 70vh; }
}

/* toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.18); cursor: pointer; }
.toast.ok { border-left-color: var(--ok); }
.toast.warning { border-left-color: var(--warn); }
.toast.critical { border-left-color: var(--bad); background: var(--crit-bg); }

/* settings */
.prompt-preview { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 12px; font-size: 12px; max-height: 360px; overflow-y: auto; }
.section-title { margin: 16px 0 8px; }
code { font-size: 12px; }

/* vinculación MT5 */
.mt5-current { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.flash.info { background: var(--surface-2); color: var(--text); margin-bottom: 12px; }

/* auditoría */
.actor { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap; }
.actor-USER { background: #3b5bdb; }
.actor-BOT_SYSTEM { background: #2b8a3e; }
.actor-LISTENER { background: #2f9e44; }
.actor-API_EXTERNAL { background: #7048e8; }
.status-SUCCESS { color: var(--ok); border-color: currentColor; }
.status-WARNING { color: var(--warn); border-color: currentColor; background: var(--warn-bg); }
.status-FAILURE { color: var(--bad); border-color: currentColor; background: var(--crit-bg); }
.audit-table td { vertical-align: middle; }
.center { text-align: center; }
.modal { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 16px; width: min(1000px, calc(100vw - 32px)); max-height: calc(100vh - 64px); }
.modal::backdrop { background: rgba(0, 0, 0, .5); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.audit-diff { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
@media (max-width: 760px) { .audit-diff { grid-template-columns: 1fr; } }
pre.json { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); border-radius: 8px; padding: 10px; font-size: 12px; max-height: 420px; overflow: auto; margin: 6px 0 0; }

/* estrategia */
pre.setup-card { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 12px; font-size: 13px; margin: 8px 0; }
.setup-retired { opacity: .6; }
.version-history { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 4px; }
