:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #17191c;
  --muted: #5d6673;
  --border: #e1e4e9;
  --grid: rgba(20, 24, 31, 0.07);
  --accent: #2458d6;
  --accent-text: #ffffff;
  --pos: #117a3d;
  --neg: #c0262d;
  --pos-bg: rgba(17, 122, 61, 0.1);
  --neg-bg: rgba(192, 38, 45, 0.1);
  --warn: #a15c00;
  --highlight: rgba(36, 88, 214, 0.08);
  --c-close: #2458d6;
  --c-sma20: #d98b00;
  --c-sma50: #8a4fd6;
  --c-sma200: #d0417e;
  --c-band: rgba(36, 88, 214, 0.08);
  --c-volume: rgba(93, 102, 115, 0.35);
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e222a;
    --text: #e7e9ed;
    --muted: #949cab;
    --border: #2a2f39;
    --grid: rgba(231, 233, 237, 0.07);
    --accent: #6d94f5;
    --accent-text: #0f1115;
    --pos: #3ecf7a;
    --neg: #ff6b6b;
    --pos-bg: rgba(62, 207, 122, 0.12);
    --neg-bg: rgba(255, 107, 107, 0.12);
    --warn: #f0a93b;
    --highlight: rgba(109, 148, 245, 0.12);
    --c-close: #6d94f5;
    --c-sma20: #f0a93b;
    --c-sma50: #b48cf5;
    --c-sma200: #f07aae;
    --c-band: rgba(109, 148, 245, 0.1);
    --c-volume: rgba(148, 156, 171, 0.35);
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
h1 { font-size: 1.6rem; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
h3 { font-size: 0.95rem; margin: 18px 0 8px; }

.container { max-width: 1280px; margin: 0 auto; padding: 0 16px; }

/* ---------- barra superior */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; padding-top: env(safe-area-inset-top); }
.topbar-inner { max-width: 1280px; margin: 0 auto; padding: 0 16px; height: 56px; display: flex; align-items: center; gap: 24px; }
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); }
.brand span { color: var(--accent); }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { color: var(--muted); padding: 6px 12px; border-radius: 6px; }
.topbar nav a:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.topbar nav a.active { color: var(--text); background: var(--surface-2); font-weight: 600; }

.mode { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; padding: 3px 8px; border-radius: 999px; vertical-align: middle; }
.mode.paper { background: var(--highlight); color: var(--accent); }
.mode.live { background: var(--neg-bg); color: var(--neg); }

main.container { padding-top: 24px; padding-bottom: 24px; }
.footer { color: var(--muted); font-size: 0.8rem; padding-bottom: 32px; }

/* ---------- mensajes */
.flash { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 20px; border: 1px solid; }
.flash.ok { background: var(--pos-bg); border-color: var(--pos); color: var(--text); }
.flash.error { background: var(--neg-bg); border-color: var(--neg); color: var(--text); }

/* ---------- cabecera de página */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .name { font-size: 1rem; font-weight: 400; }
.page-head p { margin: 0; }
.back { font-size: 0.85rem; }

/* ---------- panel de información de la empresa */
.title-wrap { position: relative; display: flex; align-items: center; gap: 8px; }
.info-btn {
  flex: none; width: 28px; height: 28px; padding: 0; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: 1px solid transparent; color: var(--muted);
}
.info-btn svg { width: 20px; height: 20px; }
.info-btn:hover, .info-btn[aria-expanded="true"] { color: var(--accent); background: var(--highlight); filter: none; }
.info-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.popover {
  position: absolute; top: calc(100% + 8px); left: 0; z-index: 20;
  width: min(460px, calc(100vw - 32px));
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15); padding: 16px 18px; font-size: 0.9rem; color: var(--text);
}
.popover:focus { outline: none; }
.popover .profile-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.popover .profile-head strong { font-size: 1rem; }
.popover .profile-head .icon { margin: -4px -8px 0 0; }
.popover .summary {
  margin: 10px 0 4px; line-height: 1.55;
  display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden;
}
.popover .summary.expanded { display: block; }
.popover .kv { margin-top: 12px; font-size: 0.86rem; }
.popover footer { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--border); display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 0.78rem; color: var(--muted); }
button.link { background: none; border: none; padding: 0; color: var(--accent); font-weight: 600; font-size: 0.85rem; }
button.link:hover { text-decoration: underline; filter: none; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.quote { text-align: right; }
.quote .price { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.quote form { margin-top: 8px; }

/* ---------- formularios */
input, select, button { font: inherit; }
input, select {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  background: var(--accent); color: var(--accent-text); border: 1px solid var(--accent);
  border-radius: 8px; padding: 8px 14px; cursor: pointer; font-weight: 600;
}
button:hover { filter: brightness(1.08); }
button.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
button.secondary:hover { background: var(--surface-2); filter: none; }
button.secondary.danger { color: var(--neg); }
button.small { padding: 4px 10px; font-size: 0.85rem; }
button.icon { background: none; border: none; color: var(--muted); padding: 4px 8px; font-size: 1rem; }
button.icon:hover { color: var(--text); background: var(--surface-2); filter: none; }
button.icon.danger:hover { color: var(--neg); }

.inline-form { display: flex; gap: 8px; }
.inline-form input { width: 240px; }

.order-form { display: grid; gap: 12px; }
.order-form label { display: grid; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.order-form label[hidden] { display: none; }
.side-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.side-toggle label { display: block; color: var(--text); }
.side-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.side-toggle span { display: block; text-align: center; padding: 8px; cursor: pointer; font-weight: 600; }
.side-toggle input:checked + span { background: var(--pos-bg); color: var(--pos); }
.side-toggle label:last-child input:checked + span { background: var(--neg-bg); color: var(--neg); }
.side-toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- tarjetas y rejilla */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; box-shadow: var(--shadow); min-width: 0; }
.card.flush { padding: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; align-items: start; }
.grid > .card { margin-bottom: 0; }
.grid + .card, .grid + .grid { margin-top: 16px; }
.span-2 { grid-column: span 2; }
.two-col { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.empty { text-align: center; padding: 40px 20px; }
.empty h2 { font-size: 1.2rem; }

.section-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin: 28px 0 12px; }
.section-head h2 { font-size: 1.2rem; margin: 0 0 2px; }
.section-head p { margin: 0; font-size: 0.88rem; }

/* ---------- KPIs de señal */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.kpis .card { margin-bottom: 0; }
.kpi-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.kpi-head h2 { margin: 0; }
.kpi-main { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin: 12px 0 14px; }
.kpi-line { margin: 10px 0 6px; font-size: 0.88rem; }
.kpi .kv { margin-top: 12px; }
.kpi details { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 8px; }
.kpi summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 0.86rem; }
.kpi details .table { margin-top: 6px; }
.kpi-note { margin: 8px 0 16px; }

.rating { display: inline-block; font-weight: 700; font-size: 1.35rem; padding: 2px 12px; border-radius: 8px; background: var(--surface-2); color: var(--muted); }
.rating.small { font-size: 0.8rem; padding: 1px 8px; border-radius: 999px; font-weight: 600; }
.rating.strong_buy, .rating.buy { background: var(--pos-bg); color: var(--pos); }
.rating.strong_sell, .rating.sell, .rating.underperform { background: var(--neg-bg); color: var(--neg); }
.rating.strong_buy, .rating.strong_sell { box-shadow: inset 0 0 0 2px currentColor; }

.gauge { position: relative; height: 8px; border-radius: 999px; background: linear-gradient(90deg, var(--neg), var(--surface-2) 50%, var(--pos)); }
.gauge-marker { position: absolute; left: var(--at); top: -5px; width: 4px; height: 18px; margin-left: -2px; border-radius: 2px; background: var(--text); box-shadow: 0 0 0 2px var(--surface); }
.gauge-scale { display: flex; justify-content: space-between; font-size: 0.72rem; color: var(--muted); margin-top: 6px; }

/* ---------- acceso */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); margin: 0; padding: 28px; }
.login-brand { font-size: 1.4rem; margin-bottom: 18px; }
.login-card h1 { font-size: 1.2rem; margin-bottom: 16px; }
.login-form { display: grid; gap: 14px; }
.login-form label { display: grid; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.login-form button { margin-top: 6px; padding: 10px; }
.user-menu { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; color: var(--muted); }
.user-menu form { margin: 0; }

/* ---------- avisos de rentabilidad */
.alerts-banner { display: grid; gap: 8px; margin-bottom: 20px; }
.alert-item {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; border-radius: var(--radius); border: 1px solid var(--pos); background: var(--pos-bg);
}
.alert-item.down { border-color: var(--neg); background: var(--neg-bg); }
.alert-item.level { border-color: var(--accent); background: var(--highlight); }
a.button {
  display: inline-block; background: var(--accent); color: var(--accent-text); border: 1px solid var(--accent);
  border-radius: 8px; padding: 8px 14px; font-weight: 600;
}
a.button:hover { filter: brightness(1.08); text-decoration: none; }
.alert-icon { font-size: 1.3rem; }
.alert-text { flex: 1; min-width: 200px; }
.alert-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.alert-actions form { margin: 0; }
.alert-cell { min-width: 190px; }
.level-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.level-form input[name="value"] { width: 110px; }
.level-form + .note { margin: 6px 0 10px; }
.level-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.level-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 8px; border-radius: 6px; background: var(--surface-2); font-size: 0.88rem; }
.level-list li.triggered { background: var(--highlight); }
.level-list li.dismissed { opacity: 0.6; }
.level-list form { margin: 0; }
.telegram-status { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.telegram-status form { margin: 0; }
.alert-row { display: flex; align-items: center; gap: 2px; }
.alert-row form { margin: 0; }
.alert-form { display: flex; align-items: center; gap: 4px; }
.alert-form input { width: 64px; padding: 4px 6px; text-align: right; }

/* ---------- backtest */
.verdict { border-left: 4px solid var(--muted); }
.verdict.good { border-left-color: var(--pos); }
.verdict.bad { border-left-color: var(--neg); }
.verdict.mixed { border-left-color: var(--warn); }
.verdict-text { font-size: 1.05rem; font-weight: 600; margin: 0 0 6px; }
.verdict p:last-child { margin-bottom: 0; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; font-size: 0.9rem; }
.legend .c-muted::before { background: var(--muted); }
.kpi-backtest { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; font-size: 0.86rem; margin: 10px 0 0; padding-top: 8px; border-top: 1px solid var(--border); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.good { background: var(--pos); }
.dot.bad { background: var(--neg); }
.dot.mixed { background: var(--warn); }

.insights ul { margin: 0; padding-left: 20px; display: grid; gap: 4px; }

.stats { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 0.8rem; }
.stat .value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- texto auxiliar */
.muted { color: var(--muted); }
.sub { color: var(--muted); font-size: 0.8rem; font-weight: 400; }
.note { color: var(--muted); font-size: 0.8rem; margin: 10px 0 0; }
.strong { font-weight: 700; }
.nowrap { white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.tag { display: inline-block; font-size: 0.72rem; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--highlight); color: var(--accent); }
.tag.warn { background: var(--neg-bg); color: var(--warn); }

/* ---------- tablas */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { font-size: 0.78rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--surface-2); }
.table.compact th, .table.compact td { padding: 6px 8px; font-size: 0.88rem; }
.table .num, .table th.num { text-align: right; white-space: nowrap; }
.table tr.current td, .matrix tr.current td, .matrix tr.current th { background: var(--highlight); }
.table .seq { letter-spacing: 2px; font-size: 0.8rem; white-space: nowrap; }
.sym { font-weight: 700; }
.row-actions { display: flex; gap: 2px; justify-content: flex-end; }
.row-actions form { margin: 0; }

.spark { width: 120px; height: 32px; display: block; }
.spark polyline { fill: none; stroke: var(--muted); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.spark.pos polyline { stroke: var(--pos); }
.spark.neg polyline { stroke: var(--neg); }

.matrix { width: 100%; border-collapse: collapse; margin-bottom: 12px; font-variant-numeric: tabular-nums; }
.matrix th, .matrix td { padding: 8px; text-align: center; border: 1px solid var(--border); }
.matrix thead th { font-size: 0.78rem; color: var(--muted); }
.matrix td { font-size: 1.1rem; font-weight: 600; }

.bar { display: inline-block; height: 8px; width: var(--w); max-width: 100%; min-width: 2px; border-radius: 2px; vertical-align: middle; background: var(--muted); }
.bar.pos { background: var(--pos); }
.bar.neg { background: var(--neg); }
.bar.neutral { background: var(--c-sma50); }
.bar.faint { opacity: 0.35; }
.acf td:nth-child(2), .acf td:nth-child(4) { width: 30%; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; font-variant-numeric: tabular-nums; }
.kv dt { color: var(--muted); font-size: 0.88rem; }
.kv dd { margin: 0; text-align: right; }
.kv.inline { grid-template-columns: repeat(4, auto 1fr); margin-bottom: 12px; }

.signals { list-style: none; padding: 0; margin: 0 0 14px; display: grid; gap: 6px; font-size: 0.88rem; }
.signals li { padding: 6px 10px; border-radius: 6px; border-left: 3px solid var(--muted); background: var(--surface-2); }
.signals li.bull { border-color: var(--pos); }
.signals li.bear { border-color: var(--neg); }

.status { font-size: 0.78rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.status.filled { background: var(--pos-bg); color: var(--pos); }
.status.rejected { background: var(--neg-bg); color: var(--neg); }
.status.pending { background: var(--highlight); color: var(--accent); }

/* ---------- gráficos */
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented { background: var(--surface); }
.segmented button, .segmented a { background: none; color: var(--muted); border: none; border-radius: 0; padding: 4px 10px; font-weight: 600; font-size: 0.82rem; }
.segmented a { padding: 6px 14px; }
.segmented a:hover { color: var(--text); text-decoration: none; }
.segmented > * + * { border-left: 1px solid var(--border) !important; }
.segmented .active { background: var(--highlight); color: var(--accent); }
.range-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin: 4px 0 12px; }
.range-stats:empty { display: none; }
.range-stats .rs { background: var(--surface); padding: 8px 12px; display: grid; gap: 1px; min-width: 0; }
.range-stats .label { font-size: 0.75rem; color: var(--muted); }
.range-stats strong { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.range-stats .sub { font-size: 0.75rem; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
.legend .k::before { content: ""; display: inline-block; width: 14px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; background: currentColor; }
.legend .c-close::before { background: var(--c-close); }
.legend .c-sma20::before { background: var(--c-sma20); }
.legend .c-sma50::before { background: var(--c-sma50); }
.legend .c-sma200::before { background: var(--c-sma200); }
.legend .c-band::before { background: var(--c-band); height: 10px; border: 1px solid var(--c-close); }
.chart { position: relative; }
.chart-price { height: 360px; }
.chart-sub { height: 110px; }
.chart-label { font-size: 0.8rem; font-weight: 600; color: var(--muted); margin-top: 14px; }

/* ---------- responsive */
@media (max-width: 1000px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  .grid, .two-col, .kpis { grid-template-columns: minmax(0, 1fr); }
  .span-2 { grid-column: auto; }
  .stats, .range-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kv.inline { grid-template-columns: auto 1fr; }
  .hide-sm { display: none; }
  .inline-form { flex: 1; }
  .inline-form input { width: auto; flex: 1; }
  .quote { text-align: left; }
  .chart-price { height: 260px; }
  .topbar-inner { gap: 8px; padding: 0 12px; }
  .topbar nav { gap: 0; }
  .topbar nav a { padding: 6px 8px; }
  .topbar .mode.paper { display: none; }  /* en móvil la barra solo lo indica si se opera con dinero real */
  .card { padding: 14px; }
}
