:root {
  --bg: #f3f5f9; --surface: #ffffff; --surface2: #f7f9fc; --border: #e1e6ef; --text: #1b2433; --muted: #66728a;
  --primary: #1f3864; --primary2: #2f5597; --accent: #2f5fc4;
  --green: #17783a; --green-bg: #e3f5e8; --yellow: #8a5200; --yellow-bg: #fff3d6; --red: #c62828; --red-bg: #fde7e7;
  --buy: #1565c0; --sell: #2e7d32;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
/* Modo oscuro: automático (según el sistema) salvo que el usuario elija "claro"; o forzado con data-theme="dark" */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141c; --surface: #171e29; --surface2: #1d2533; --border: #2a3444; --text: #e6ebf3; --muted: #93a0b5;
    --primary: #8fb0ff; --primary2: #6d95f0; --accent: #7aa2ff;
    --green: #5ccf7c; --green-bg: #173625; --yellow: #f2b544; --yellow-bg: #3a2e14; --red: #ff7a7a; --red-bg: #3d1c1f;
    --buy: #6aa8ff; --sell: #6fd08a;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141c; --surface: #171e29; --surface2: #1d2533; --border: #2a3444; --text: #e6ebf3; --muted: #93a0b5;
  --primary: #8fb0ff; --primary2: #6d95f0; --accent: #7aa2ff;
  --green: #5ccf7c; --green-bg: #173625; --yellow: #f2b544; --yellow-bg: #3a2e14; --red: #ff7a7a; --red-bg: #3d1c1f;
  --buy: #6aa8ff; --sell: #6fd08a;
  --shadow: none;
  color-scheme: dark;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; font-size: 14px; background: var(--bg); color: var(--text); }
h2, h3 { margin: 0; }
h3, .h3 { font-size: 15px; font-weight: 600; margin: 0; }
h4 { margin: 16px 0 8px; font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Topbar */
.topbar { display: flex; flex-wrap: wrap; gap: 16px; align-items: center; justify-content: space-between; padding: 12px 20px; background: #1f3864; color: #fff; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo { width: 40px; height: 40px; border-radius: 10px; background: #fff; color: #1f3864; display: grid; place-items: center; font-weight: 800; font-size: 13px; }
.brand-title { font-weight: 700; font-size: 17px; }
.brand-sub { font-size: 12px; opacity: .75; }
.top-controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.ctl { display: flex; flex-direction: column; gap: 3px; font-size: 11px; opacity: .95; }
.ctl select, .ctl input { min-width: 150px; }
.topbar select, .topbar input { background: rgba(255,255,255,.12); color: #fff; border: 1px solid rgba(255,255,255,.3); }
.topbar select option { color: #000; }
.inline { display: flex; gap: 4px; }

/* Controles */
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); color: var(--text); min-width: 0; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.btn { font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; white-space: nowrap; }
.btn:hover { filter: brightness(.96); }
.btn.primary { background: #2f5597; border-color: #2f5597; color: #fff; }
.btn.success { background: #17783a; border-color: #17783a; color: #fff; }
.btn.danger { background: transparent; color: var(--red); border-color: var(--red); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.icon { padding: 6px 10px; }
.btn.link { border: none; background: none; color: var(--accent); padding: 2px 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row-btns { display: flex; flex-wrap: wrap; gap: 8px; }

/* Tabs */
.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; position: sticky; top: 0; z-index: 5; }
.tabs button.hidden { display: none; }
.tabs button { font: inherit; padding: 12px 14px; border: none; background: none; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; white-space: nowrap; font-weight: 600; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary2); }
main { padding: 18px 20px 40px; max-width: 1600px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

/* Cards / grids */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; min-width: 0; }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid2 > .card, .grid3 > .card { margin-bottom: 16px; }
@media (max-width: 1100px) { .grid2, .grid3 { grid-template-columns: 1fr; } .grid2 > .card, .grid3 > .card { margin-bottom: 0; } .grid2, .grid3 { margin-bottom: 16px; } }
.chart-box { position: relative; height: 300px; }
.chart-box.sm { height: 220px; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .k-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kpi .k-value { font-size: 22px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi .k-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi.hl { background: linear-gradient(135deg, #1f3864, #2f5597); color: #fff; border: none; }
.kpi.hl .k-label, .kpi.hl .k-sub { color: rgba(255,255,255,.8); }
.pos { color: var(--green); } .neg { color: var(--red); }
.kpi.hl .pos, .kpi.hl .neg { color: #fff; }

/* Tablas */
.table-wrap { overflow: auto; max-height: calc(100vh - 260px); border: 1px solid var(--border); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; }
table.grid th, table.grid td { padding: 7px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.grid th { position: sticky; top: 0; background: var(--surface2); font-size: 12px; text-align: left; color: var(--muted); cursor: pointer; user-select: none; z-index: 1; }
table.grid tbody tr:hover { background: var(--surface2); }
table.grid tfoot td { position: sticky; bottom: 0; background: var(--surface2); font-weight: 700; }
table.simple td, table.simple th { padding: 7px 8px; border-bottom: 1px solid var(--border); }
table.simple th { text-align: left; font-size: 12px; color: var(--muted); }
table.simple tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
table.simple tr.hl td { background: var(--green-bg); font-weight: 700; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.tag.COMPRA { background: rgba(21,101,192,.12); color: var(--buy); }
.tag.VENTA { background: rgba(46,125,50,.12); color: var(--sell); }
.zone { display: inline-block; min-width: 58px; text-align: right; padding: 2px 7px; border-radius: 6px; font-weight: 700; }
.zone.verde { background: var(--green-bg); color: var(--green); }
.zone.amarillo { background: var(--yellow-bg); color: var(--yellow); }
.zone.rojo { background: var(--red-bg); color: var(--red); }
.warn { color: var(--yellow); font-weight: 600; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; color: var(--muted); }

/* Filtros */
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.filters input[type=search] { min-width: 220px; }
.spacer { flex: 1; }

/* Semáforo */
.sema { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.sema div { border-radius: 8px; padding: 10px; }
.sema .t { font-size: 12px; font-weight: 700; }
.sema .v { font-size: 20px; font-weight: 700; margin: 4px 0; }
.sema .d { font-size: 12px; }
.sema .rojo { background: var(--red-bg); color: var(--red); }
.sema .amarillo { background: var(--yellow-bg); color: var(--yellow); }
.sema .verde { background: var(--green-bg); color: var(--green); }
.pe-line { display: flex; justify-content: space-between; margin-bottom: 10px; font-size: 13px; }

/* Importar */
.drop { border: 2px dashed var(--border); border-radius: 12px; padding: 36px 20px; text-align: center; transition: .15s; }
.drop.over { border-color: var(--accent); background: var(--surface2); }
.drop-icon { font-size: 36px; }
.drop-title { font-size: 17px; font-weight: 700; margin: 8px 0 4px; }
.drop-sub { color: var(--muted); margin-bottom: 14px; }
.src-card { border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 12px; background: var(--surface2); }
.src-head { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.src-head .name { font-weight: 700; font-size: 15px; }
.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; margin: 10px 0; }
.map-grid label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.opts { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; margin: 10px 0; }
.opts label { display: flex; gap: 6px; align-items: center; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 6px; background: var(--surface); border: 1px solid var(--border); font-size: 12px; }

/* Formularios */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.form-grid label, .fld { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

/* Modal / toast */
.modal { position: fixed; inset: 0; background: rgba(10, 15, 25, .55); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 12px; padding: 20px; width: min(760px, 100%); max-height: 92vh; overflow: auto; }
.modal-card h3 { margin-bottom: 14px; font-size: 17px; }
.toast { position: fixed; bottom: 20px; right: 20px; background: #1b2433; color: #fff; padding: 12px 18px; border-radius: 8px; z-index: 30; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 420px; }
.empty { text-align: center; padding: 50px 20px; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--border); margin-bottom: 16px; }
.empty p { color: var(--muted); }

@media (max-width: 700px) {
  .topbar { padding: 12px 16px; }
  main { padding: 14px 16px 30px; }
  .ctl select, .ctl input { min-width: 120px; }
  .sema { grid-template-columns: 1fr; }
}

/* ---------- Accesibilidad ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip { position: absolute; left: 8px; top: -60px; background: #fff; color: #1f3864; padding: 8px 14px; border-radius: 6px; z-index: 50; font-weight: 700; }
.skip:focus { top: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.topbar :focus-visible { outline-color: #ffd54a; }
main:focus { outline: none; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; scroll-behavior: auto !important; } }
a { color: var(--accent); }

/* ---------- Pantallas de acceso ---------- */
.auth-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #1f3864, #2f5597); }
.auth-card { background: var(--surface); color: var(--text); width: min(460px, 100%); border-radius: 14px; padding: 26px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.auth-brand { display: flex; gap: 14px; align-items: center; margin-bottom: 18px; }
.auth-brand .logo { background: #1f3864; color: #fff; }
.auth-brand h1 { font-size: 19px; margin: 0; }
.auth-brand p { margin: 2px 0 0; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
.auth-err { color: var(--red); min-height: 1.2em; margin: 12px 0 0; font-weight: 600; }
.btn.block { width: 100%; padding: 11px; }
.btn.ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,.45); }
.mfa-box .steps { padding-left: 20px; color: var(--text); }
.mfa-qr { margin: 14px 0; text-align: center; }
.mfa-qr img { background: #fff; padding: 8px; border-radius: 8px; }
.secret { display: inline-block; font-size: 15px; letter-spacing: .08em; background: var(--surface2); padding: 6px 10px; border-radius: 6px; word-break: break-all; }
.user-box { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.user-label { font-size: 12px; opacity: .9; margin-right: 4px; }

/* ---------- Utilidades (reemplazan estilos en línea, requeridos por la CSP) ---------- */
.th-btn { font: inherit; font-weight: 600; background: none; border: 0; color: inherit; cursor: pointer; padding: 0; text-align: inherit; width: 100%; }
th.num .th-btn { text-align: right; }
.empty-row td { text-align: center; padding: 30px !important; color: var(--muted); }
.table-wrap.tw-sm { max-height: 260px; margin-top: 8px; }
.table-wrap.tw-md { max-height: 360px; }
.table-wrap.tw-auto { max-height: none; }
.span2 { grid-column: span 2; }
.full { grid-column: 1 / -1; }
.num-input { width: 170px; text-align: right; }
.pct-input { width: 90px; text-align: right; }
.inline-num { width: 110px; }
.check-label { flex-direction: row !important; align-items: center; gap: 8px; }
.mt { margin-top: 14px; }
.inline-link { padding: 0 2px; font-size: 12px; font-weight: 600; }
.ok-badge { color: var(--green); border-color: var(--green); }
.bad-badge { color: var(--red); border-color: var(--red); }
td.wrap { white-space: normal; max-width: 420px; font-size: 12px; }
fieldset { border: 0; padding: 0; margin: 10px 0; }
fieldset.opts legend { float: left; margin-right: 10px; }
td.row-btns { white-space: nowrap; }
@media (max-width: 700px) { .span2 { grid-column: auto; } .user-label { display: none; } }

/* ---------- Configuración personal, acceso y cierre de sesión ---------- */
.auth-tools { display: flex; gap: 8px; justify-content: flex-end; margin: -6px 0 12px; }
.auth-tools select { padding: 4px 8px; font-size: 12px; }
.pw-wrap { position: relative; display: flex; }
.pw-wrap input { flex: 1; padding-right: 44px; }
.pw-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); border: 0; background: none; color: var(--muted); cursor: pointer; padding: 6px 8px; font-size: 14px; }
.caps-warn { color: var(--yellow); font-size: 12px; font-weight: 600; }
.btn.ghost.logout { border-color: rgba(255,255,255,.7); }
.settings-card { width: min(640px, 100%); }
.set-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.set-tabs button { font: inherit; font-weight: 600; background: none; border: 0; border-bottom: 3px solid transparent; padding: 10px 12px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.set-tabs button[aria-selected="true"] { color: var(--primary); border-bottom-color: var(--primary2); }
.set-panel { min-height: 220px; }
.info-list { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 16px; }
.info-list dt { color: var(--muted); font-weight: 600; }
.info-list dd { margin: 0; }
.choice-group { display: grid; gap: 8px; border: 0; padding: 0; margin: 0; }
.choice { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; background: var(--surface); }
.choice:has(input:checked) { border-color: var(--primary2); background: var(--surface2); box-shadow: inset 0 0 0 1px var(--primary2); }
.choice .swatch { width: 34px; height: 22px; border-radius: 6px; border: 1px solid var(--border); flex: none; }
.swatch.light { background: linear-gradient(135deg, #ffffff 50%, #f3f5f9 50%); }
.swatch.dark { background: linear-gradient(135deg, #171e29 50%, #0f141c 50%); }
.swatch.system { background: linear-gradient(135deg, #ffffff 50%, #171e29 50%); }
.danger-zone { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }
.auth-err.info { color: var(--green); }
.auth-tag { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
