:root {
  --bg: #0e1113;
  --surface: #15191c;
  --surface-2: #1b2024;
  --line: #262c31;
  --line-2: #323a40;
  --text: #e7e9eb;
  --muted: #8c969e;
  --faint: #5d666d;
  --buy: #37b37e;
  --buy-bg: rgba(55, 179, 126, .12);
  --sell: #e5574f;
  --sell-bg: rgba(229, 87, 79, .12);
  --amber: #d9a441;
  --amber-bg: rgba(217, 164, 65, .12);
  --accent: #6aa9e0;
  --accent-bg: rgba(106, 169, 224, .12);
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.hidden { display: none !important; }
.buy { color: var(--buy); }
.sell { color: var(--sell); }

/* ---------------------------------------------------------------- barra superior */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 20px;
  background: rgba(14, 17, 19, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: .2px; }
.brand-mark { width: 22px; height: 22px; }
.brand small { font-weight: 500; color: var(--muted); font-family: var(--mono); }
.spacer { flex: 1; }
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; letter-spacing: .3px; text-transform: uppercase;
  border: 1px solid var(--line-2); color: var(--muted);
}
.badge.dry { color: var(--amber); border-color: rgba(217,164,65,.45); background: var(--amber-bg); }
.badge.live { color: var(--sell); border-color: rgba(229,87,79,.5); background: var(--sell-bg); }
.badge.running { color: var(--buy); border-color: rgba(55,179,126,.45); background: var(--buy-bg); }
.badge.paused { color: var(--amber); border-color: rgba(217,164,65,.45); background: var(--amber-bg); }
.badge.error, .badge.offline { color: var(--sell); border-color: rgba(229,87,79,.5); background: var(--sell-bg); }
.badge.idle, .badge.starting { color: var(--muted); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.running .dot { animation: pulse 2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.heartbeat { font-size: 12px; color: var(--muted); }
.user { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }

/* ---------------------------------------------------------------- pestañas */
.tabs {
  display: flex; gap: 2px; padding: 0 20px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.tabs button {
  background: none; border: 0; cursor: pointer;
  padding: 11px 14px; color: var(--muted);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button:hover { color: var(--text); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 18px 20px 60px; }
.view { display: none; }
.view.active { display: block; }

/* ---------------------------------------------------------------- avisos */
.banner {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 11px 14px; margin-bottom: 14px;
  border-radius: var(--radius); border: 1px solid var(--line-2);
  background: var(--surface); white-space: pre-line;
}
.banner.error { border-color: rgba(229,87,79,.5); background: var(--sell-bg); }
.banner.warn { border-color: rgba(217,164,65,.45); background: var(--amber-bg); }
.banner.info { border-color: rgba(106,169,224,.4); background: var(--accent-bg); }

/* ---------------------------------------------------------------- tarjetas */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px; min-width: 0;
}
.card h2 {
  margin: 0 0 12px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .6px; color: var(--muted);
  display: flex; align-items: center; gap: 10px;
}
.card h2 .right { margin-left: auto; text-transform: none; letter-spacing: 0; font-weight: 500; }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-family: var(--mono); font-size: 22px; font-weight: 600; margin-top: 4px; letter-spacing: -.3px; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; font-family: var(--mono); }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 14px; align-items: start; }
.stack { display: grid; gap: 14px; min-width: 0; }

/* ---------------------------------------------------------------- gráfico */
.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: 8px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 3px 10px; color: var(--muted); cursor: pointer; font-size: 12px; }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); }
.chart { width: 100%; height: 280px; display: block; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .level-line { stroke: var(--line-2); stroke-width: 1; stroke-dasharray: 2 4; }
.chart .range-line { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 6 4; opacity: .8; }
.chart .alert-line { stroke: var(--amber); stroke-width: 1; stroke-dasharray: 3 3; opacity: .7; }
.chart .price-line { fill: none; stroke: var(--text); stroke-width: 1.6; }
.chart .price-area { fill: url(#areaGrad); }
.chart text { fill: var(--muted); font: 11px var(--mono); }
.chart .tag { font-weight: 600; }
.chart .last-dot { fill: var(--text); }
.chart-empty { height: 280px; display: grid; place-items: center; color: var(--muted); text-align: center; }

/* barra de posición en el rango */
.rangebar { position: relative; height: 34px; margin: 6px 0 4px; }
.rangebar .track { position: absolute; left: 0; right: 0; top: 14px; height: 6px; border-radius: 3px; background: var(--surface-2); }
.rangebar .zone { position: absolute; top: 14px; height: 6px; background: rgba(106,169,224,.25); border-radius: 3px; }
.rangebar .marker { position: absolute; top: 4px; width: 2px; height: 26px; background: var(--text); border-radius: 1px; }
.rangebar .marker::after { content: ""; position: absolute; left: -4px; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--text); }
.rangebar-labels { display: flex; justify-content: space-between; font: 11px var(--mono); color: var(--muted); }

/* ---------------------------------------------------------------- controles */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--line-2); color: var(--text);
  font-weight: 550; transition: border-color .15s, background .15s;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #0b1620; }
.btn.danger { color: var(--sell); border-color: rgba(229,87,79,.45); }
.btn.danger:hover:not(:disabled) { background: var(--sell-bg); border-color: var(--sell); }
.btn.go { color: var(--buy); border-color: rgba(55,179,126,.45); }
.btn.go:hover:not(:disabled) { background: var(--buy-bg); border-color: var(--buy); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.ghost { background: none; }
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.controls .full { grid-column: 1 / -1; }
.lock { font-size: 11px; opacity: .7; }
.hint { font-size: 12px; color: var(--muted); margin: 10px 0 0; }

dl.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 13px; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; text-align: right; font-family: var(--mono); }

/* ---------------------------------------------------------------- escalera del grid */
.ladder { width: 100%; border-collapse: collapse; font-size: 13px; }
.ladder th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); }
.ladder td { padding: 5px 10px; border-bottom: 1px solid var(--line); font-family: var(--mono); }
.ladder tr.price-row td { background: var(--accent-bg); color: var(--accent); font-weight: 600; border-bottom-color: rgba(106,169,224,.4); }
.ladder td.side-BUY { color: var(--buy); }
.ladder td.side-SELL { color: var(--sell); }
.ladder .pill { display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 11px; font-weight: 600; }
.ladder .pill.BUY { background: var(--buy-bg); color: var(--buy); }
.ladder .pill.SELL { background: var(--sell-bg); color: var(--sell); }
.ladder .pill.PEND { background: var(--amber-bg); color: var(--amber); }
.table-wrap { max-height: 640px; overflow: auto; }

/* ---------------------------------------------------------------- formularios */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px 16px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.field input[type=text], .field input[type=number], .field input[type=password], .field select {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  background: var(--bg); border: 1px solid var(--line-2);
  font-family: var(--mono); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field input.changed { border-color: var(--amber); }
.field .env { font-size: 11px; color: var(--faint); font-family: var(--mono); margin-top: 3px; }
.switch { display: flex; align-items: center; gap: 8px; padding-top: 6px; }
.switch input { width: 16px; height: 16px; accent-color: var(--accent); }
.form-actions { display: flex; gap: 10px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.preview { margin-top: 14px; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); font: 12px/1.6 var(--mono); color: var(--muted); white-space: pre-line; }
.preview.bad { color: var(--sell); }

/* ---------------------------------------------------------------- actividad */
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: grid; grid-template-columns: 112px 1fr; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.feed li:last-child { border-bottom: 0; }
.feed time { color: var(--faint); font: 12px var(--mono); padding-top: 1px; }
.feed .msg { white-space: pre-line; word-break: break-word; }
.feed li.warning .msg { color: var(--amber); }
.feed li.error .msg { color: var(--sell); }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }

table.simple { width: 100%; border-collapse: collapse; font-size: 13px; }
table.simple th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.simple td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.simple td.num, table.simple td .num { white-space: nowrap; }
.status-done { color: var(--buy); }
.status-error, .status-expired { color: var(--sell); }
.status-pending { color: var(--amber); }
.ok-1 { color: var(--buy); }
.ok-0 { color: var(--sell); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ---------------------------------------------------------------- modal y toasts */
dialog {
  border: 1px solid var(--line-2); border-radius: 14px; padding: 0;
  background: var(--surface); color: var(--text); width: min(420px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0,0,0,.6); backdrop-filter: blur(2px); }
.modal { padding: 20px; }
.modal h3 { margin: 0 0 8px; font-size: 17px; }
.modal p { margin: 0 0 14px; color: var(--muted); white-space: pre-line; }
.code-input {
  width: 100%; text-align: center; letter-spacing: 10px; font: 600 26px var(--mono);
  padding: 10px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line-2); outline: none;
}
.code-input:focus { border-color: var(--accent); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal .error-text { color: var(--sell); font-size: 13px; min-height: 18px; margin-top: 8px; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(380px, calc(100vw - 32px)); }
.toast { padding: 11px 14px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line-2); box-shadow: 0 8px 24px rgba(0,0,0,.4); white-space: pre-line; }
.toast.ok { border-color: rgba(55,179,126,.5); }
.toast.bad { border-color: rgba(229,87,79,.6); }

/* ---------------------------------------------------------------- login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 28px 26px; }
.login-card .brand { margin-bottom: 4px; font-size: 17px; }
.login-card p.lead { margin: 0 0 22px; color: var(--muted); font-size: 13px; }
.login-card .field { margin-bottom: 14px; }
.login-card .field input { width: 100%; }
.login-card .btn { width: 100%; padding: 10px; margin-top: 6px; }
.login-error { color: var(--sell); min-height: 20px; font-size: 13px; margin-top: 12px; }
.login-foot { margin-top: 18px; font-size: 12px; color: var(--faint); text-align: center; }

/* ---------------------------------------------------------------- analizador */
.an-intro { margin: 0 0 14px; }
.an-pairs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.an-pairs label { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--line-2);
  border-radius: 999px; cursor: pointer; font: 12px var(--mono); }
.an-pairs input { accent-color: var(--accent); }
.an-pairs .tag { color: var(--faint); font-family: var(--sans); }
.an-form { margin-bottom: 4px; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: 12px; }
.progress .bar { height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.an-table td.r, .an-table th.r { text-align: right; }
.an-table tbody tr.clickable { cursor: pointer; }
.an-table tbody tr.clickable:hover td { background: var(--surface-2); }
.an-table tbody tr.selected td { background: var(--accent-bg); }
.an-table td.current::after { content: " ●"; color: var(--accent); }
.sig { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .3px; }
.sig.favorable { background: var(--buy-bg); color: var(--buy); }
.sig.neutral { background: var(--amber-bg); color: var(--amber); }
.sig.desfavorable { background: var(--sell-bg); color: var(--sell); }
.pos { color: var(--buy); }
.neg { color: var(--sell); }
.reasons { margin: 0 0 16px; padding-left: 18px; }
.reasons li { margin: 3px 0; }
.sub-h { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin: 14px 0 8px; }
.hours { width: 100%; height: 150px; display: block; }
.hours rect { fill: var(--accent); opacity: .35; }
.hours rect.top { opacity: .9; }
.hours text { fill: var(--muted); font: 10px var(--mono); }
.an-errors { margin-top: 8px; font-size: 12px; white-space: pre-line; }
.an-manual { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.an-manual summary { cursor: pointer; color: var(--muted); margin-bottom: 12px; }
.an-table tbody tr.dim td { opacity: .55; }
.an-table td.rank { color: var(--faint); }
.tag-low { margin-left: 6px; font-size: 10px; color: var(--amber); }
body.viewer .an-viewer-status { display: block; }

/* ---------------------------------------------------------------- roles */
.viewer-only { display: none; }
body.viewer .viewer-only { display: flex; }
body.viewer .admin-only { display: none !important; }

/* ---------------------------------------------------------------- responsive */
/* ---- simulaciones (varios grids) */
.kpis.kpis-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.pg-table td { vertical-align: middle; }
.pg-table .sub { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; font-family: var(--sans); }
.pg-table .badge { font-size: 11px; padding: 2px 8px; white-space: nowrap; }
.pg-table .badge.stopped { color: var(--muted); }
.pg-actions { display: flex; gap: 6px; justify-content: flex-end; white-space: nowrap; }
.mini { position: relative; width: 120px; height: 6px; border-radius: 3px; background: var(--surface-2); margin: 5px 0 3px; }
.mini .mk { position: absolute; top: -3px; width: 2px; height: 12px; border-radius: 1px; background: var(--text); }
.mini.out { background: var(--sell-bg); }
.mini.out .mk { background: var(--sell); }
.pg-table tr.msg td { color: var(--amber); font-size: 12px; padding-top: 0; border-top: 0; }
.tag-out { margin-left: 6px; font-size: 10px; color: var(--sell); font-weight: 600; }
.empty-row td { color: var(--muted); text-align: center; padding: 18px 8px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .layout { grid-template-columns: 1fr; }
}
@media (max-width: 680px) {
  .topbar { padding: 10px 16px; }
  .tabs { padding: 0 8px; }
  main { padding: 14px 16px 60px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi .value { font-size: 18px; }
  .two-col { grid-template-columns: 1fr; }
  .feed li { grid-template-columns: 1fr; gap: 2px; }
  .heartbeat { order: 5; width: 100%; }
  .chart, .chart-empty { height: 220px; }
}
@media (max-width: 680px) {
  .kpis.kpis-3 { grid-template-columns: 1fr; }
}
.confirm-check { margin: 4px 0 10px; align-items: flex-start; font-size: 13px; }
.liquidate-wrap { margin-top: 12px; font-size: 13px; align-items: flex-start; }
.liquidate-wrap input, .confirm-check input { margin-top: 2px; flex: none; }
