:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --line: #e3e6eb;
  --line-strong: #cfd4dc;
  --text: #1b2330;
  --text-2: #5b6675;
  --text-3: #8a94a3;
  --brand: #7d1220;
  --brand-2: #b0182b;
  --accent: #b0182b;
  --accent-soft: #fdeced;
  --ok: #1d7a3a; --ok-bg: #e7f5ec;
  --warn: #8a5a00; --warn-bg: #fff5dc;
  --err: #b42318; --err-bg: #fdecea;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  font-size: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); line-height: 1.45;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; font-weight: 650; }
h3 { font-size: .95rem; margin: 0 0 .5rem; font-weight: 650; }
.muted { color: var(--text-2); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num, .tabla .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--brand); color: #f6dfe1; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand { padding: 20px 20px 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand b { display: block; color: #fff; font-size: 1.05rem; letter-spacing: .01em; }
.brand span { font-size: .78rem; color: #e3b3b8; }
.nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  color: #f6dfe1; padding: 9px 12px; border-radius: 6px; display: flex; gap: 10px; align-items: center;
  font-weight: 500;
}
.nav a:hover { background: rgba(255,255,255,.07); text-decoration: none; }
.nav a.activo { background: rgba(255,255,255,.14); color: #fff; }
.nav .sep { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: #d99aa1; padding: 14px 12px 4px; }
.nav svg { width: 17px; height: 17px; flex: none; opacity: .9; }
.user-box { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.08); font-size: .85rem; }
.user-box b { color: #fff; display: block; }
.user-box a { color: #ffd0d4; margin-right: 10px; }
.main { min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 18px 28px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar .acciones { display: flex; gap: 8px; flex-wrap: wrap; }
.content { padding: 22px 28px 48px; }

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px 20px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--text); padding: 7px 13px; border-radius: 6px;
  font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
.btn-primary:hover { background: var(--brand); }
.btn-danger { color: var(--err); border-color: #f0b8b2; }
.btn-danger:hover { background: var(--err-bg); }
.btn-sm { padding: 4px 9px; font-size: .85rem; }
.btn svg { width: 15px; height: 15px; }

.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; border: 1px solid; }
.flash.ok { background: var(--ok-bg); color: var(--ok); border-color: #b5dfc3; }
.flash.error { background: var(--err-bg); color: var(--err); border-color: #f3c0ba; }
.flash.aviso { background: var(--warn-bg); color: var(--warn); border-color: #f0d9a0; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  background: #eef1f5; color: var(--text-2); white-space: nowrap; }
.badge.azul { background: var(--accent-soft); color: #9b1626; }
.badge.verde { background: var(--ok-bg); color: var(--ok); }
.badge.rojo { background: #b42318; color: #fff; }
.badge.ambar { background: var(--warn-bg); color: var(--warn); }

/* ---------- forms ---------- */
label { display: block; font-weight: 600; font-size: .82rem; color: var(--text-2); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=date], input[type=email], input[type=search], input[type=url], input[type=number], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: #fff;
  border: 1px solid var(--line-strong); border-radius: 6px; padding: 7px 9px;
}
textarea { min-height: 76px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #f1a7ad; outline-offset: 0; border-color: var(--accent); }
input[readonly], .calc { background: var(--surface-2); color: var(--text-2); }
.campo { min-width: 0; }
.campo.error input, .campo.error select, .campo.error textarea { border-color: var(--err); background: #fffafa; }
.campo .msg { color: var(--err); font-size: .8rem; margin-top: 3px; }
.campo .ayuda { color: var(--text-3); font-size: .78rem; margin-top: 3px; }
.campo.cambiado input, .campo.cambiado select, .campo.cambiado textarea { border-color: #f2a600; background: #fffbef; }
.req { color: var(--err); }
.grid { display: grid; gap: 14px 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.span2 { grid-column: span 2; } .span3 { grid-column: span 3; } .spanall { grid-column: 1 / -1; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 500; color: var(--text); }

/* ---------- filtros ---------- */
.filtros { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; padding: 14px 16px; }
.filtros .f { min-width: 150px; flex: 1 1 150px; max-width: 260px; }
.filtros .f.ancho { flex: 2 1 260px; max-width: 380px; }
.filtros .f label { font-size: .75rem; }

/* ---------- tables ---------- */
.tabla-wrap { overflow-x: auto; }
table.tabla { width: 100%; border-collapse: collapse; }
.tabla th, .tabla td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.tabla th { background: var(--surface-2); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--text-2); font-weight: 650; position: sticky; top: 0; white-space: nowrap; }
.tabla th a { color: inherit; }
.tabla tbody tr:hover { background: #fff6f6; }
.tabla td.trunc { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabla td.wrap { max-width: 340px; white-space: pre-wrap; word-break: break-word; }
.tabla tr.inactivo td { color: var(--text-3); }
.pie-tabla { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; gap: 12px; flex-wrap: wrap; }
.paginacion { display: flex; gap: 4px; align-items: center; }
.paginacion a, .paginacion span { padding: 4px 10px; border-radius: 6px; border: 1px solid var(--line); background: #fff; }
.paginacion span.actual { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }

/* ---------- tabs (formulario) ---------- */
.form-layout { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 20px; align-items: start; }
.tabs { position: sticky; top: 84px; display: flex; flex-direction: column; gap: 2px; padding: 8px; }
.tabs button {
  text-align: left; border: 0; background: transparent; font: inherit; padding: 9px 12px; border-radius: 6px;
  cursor: pointer; color: var(--text-2); display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.tabs button:hover { background: var(--surface-2); }
.tabs button.activo { background: var(--accent-soft); color: #8f1424; font-weight: 600; }
.tabs .cnt { font-size: .72rem; color: var(--text-3); font-variant-numeric: tabular-nums; }
.tabs button.con-error { color: var(--err); }
.seccion { display: none; }
.seccion.activo { display: block; }
.seccion-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 14px; }
.barra-guardar {
  position: sticky; bottom: 0; background: rgba(255,255,255,.96); border-top: 1px solid var(--line);
  padding: 12px 20px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  border-radius: 0 0 var(--radius) var(--radius);
}
.meta { display: flex; gap: 18px; flex-wrap: wrap; font-size: .82rem; color: var(--text-2); }

/* autocompletar */
.ac { position: relative; }
.ac-lista { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; background: #fff; border: 1px solid var(--line-strong);
  border-radius: 6px; box-shadow: 0 8px 24px rgba(16,24,40,.12); max-height: 280px; overflow-y: auto; display: none; }
.ac-lista div { padding: 7px 10px; cursor: pointer; font-size: .88rem; border-bottom: 1px solid #f0f2f5; }
.ac-lista div:hover, .ac-lista div.sel { background: var(--accent-soft); }

/* historial */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 10px 0; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 150px 160px 1fr; gap: 12px; }
.cambio { font-size: .85rem; }
.cambio .ant { color: var(--err); text-decoration: line-through; word-break: break-word; }
.cambio .nue { color: var(--ok); word-break: break-word; }

/* ---------- dashboard ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(178px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { padding: 14px 16px 14px 18px; position: relative; overflow: hidden; }
.kpi::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: var(--kpi, #b0182b); }
.kpi .lbl::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--kpi, #b0182b); margin-right: 6px; vertical-align: 1px; }
.kpi .lbl { font-size: .78rem; color: var(--text-2); font-weight: 600; }
.kpi .val { font-size: 1.75rem; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .78rem; color: var(--text-3); }
.kpi a.val { color: var(--text); display: block; }
.charts { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
.chart-card { padding: 16px 18px; }
.chart-card.full { grid-column: 1 / -1; }
.chart-card .sub { font-size: .8rem; color: var(--text-3); margin: -6px 0 10px; }
.chart-box { position: relative; height: 290px; }
.chart-box.alto { height: 400px; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 3px 10px; font: inherit; font-size: .8rem; cursor: pointer; color: var(--text-2); }
.seg button.activo { background: var(--accent-soft); color: #8f1424; font-weight: 600; }
.ver-tabla { font-size: .78rem; }
.mini-tabla { font-size: .82rem; max-height: 290px; overflow: auto; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, #7d1220 0%, #b0182b 55%, #f6eeee 55%); }
.login { width: 380px; padding: 30px 30px 26px; }
.login h1 { margin-bottom: 4px; }
.login .grid { margin-top: 20px; }

/* catálogos */
.cat-layout { display: grid; grid-template-columns: 260px 1fr; gap: 18px; align-items: start; }
.cat-nav a { display: flex; justify-content: space-between; padding: 7px 12px; color: var(--text); border-radius: 6px; }
.cat-nav a.activo { background: var(--accent-soft); color: #8f1424; font-weight: 600; }
.cat-nav a:hover { text-decoration: none; background: var(--surface-2); }

.clave-temporal { font-family: Consolas, monospace; font-size: 1.2rem; background: #fff; padding: 6px 12px; border: 1px dashed var(--warn); border-radius: 6px; display: inline-block; }

@media (max-width: 1100px) {
  .charts { grid-template-columns: 1fr; }
  .form-layout { grid-template-columns: 1fr; }
  .tabs { position: static; flex-direction: row; flex-wrap: wrap; }
  .g4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .span2, .span3 { grid-column: auto; }
  .content, .topbar { padding-left: 14px; padding-right: 14px; }
  .cat-layout { grid-template-columns: 1fr; }
}

/* Lo que se escribe se ve en mayúscula */
input[type=text]:not([data-sin-mayus]), input[type=search]:not([data-sin-mayus]), textarea:not([data-sin-mayus]) { text-transform: uppercase; }
input::placeholder, textarea::placeholder { text-transform: none; }

/* Logo institucional */
.brand-logo { display: block; background: #fff; border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; text-align: center; }
.brand-logo img { height: 62px; max-width: 100%; display: inline-block; }
.sin-datos { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 24px; color: var(--text-3); font-size: .88rem; border: 1px dashed var(--line); border-radius: 8px; }

/* Alertas de seguridad (dashboard del administrador) */
.alertas-panel { padding: 14px 16px; }
.alertas-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; }
.alertas-panel.sin-alertas { border-left: 5px solid #0aac23; }
.alertas-panel.sin-alertas .alertas-head { margin-bottom: 0; }
.alerta { display: flex; gap: 12px; align-items: center; padding: 10px 12px; border-radius: 6px; margin-top: 8px; border-left: 5px solid; }
.alerta-icono { font-size: 1.15rem; width: 22px; text-align: center; }
.alerta-nivel { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 1px 7px; border-radius: 999px; margin-right: 4px; }
.alerta-critica { background: #fdecea; border-color: #dc3c3d; }
.alerta-critica .alerta-nivel { background: #b42318; color: #fff; }
.alerta-alta { background: #fff1e8; border-color: #f18a59; }
.alerta-alta .alerta-nivel { background: #c2410c; color: #fff; }
.alerta-media { background: #fff8e1; border-color: #fbb315; }
.alerta-media .alerta-nivel { background: #8a5a00; color: #fff; }
.alertas-mas summary { cursor: pointer; color: var(--accent); margin-top: 10px; font-weight: 600; }
.aviso-mismo-exp { margin-top: 6px; padding: 8px 10px; border-radius: 6px; background: #fff1e8; border-left: 4px solid #f18a59;
  font-size: .84rem; color: #7a2e0e; line-height: 1.5; }
.aviso-mismo-exp a { font-weight: 600; }

/* Campos formulados (se calculan solos) */
.fx { display: inline-block; font-style: italic; font-weight: 700; font-size: .72rem; line-height: 1; padding: 2px 6px;
  border-radius: 999px; background: #6a4ce2; color: #fff; margin-left: 4px; vertical-align: 1px; letter-spacing: .02em; }
.campo.formulado label { color: #4b2fb8; }
.campo.formulado input, .campo.formulado select, .campo.formulado .calc {
  background: #f3effe; border-color: #c4b5f5; color: #2e1d78; font-weight: 600; }
.campo.formulado .ayuda { color: #6a5aa8; }
.campo.formulado.recalculado input, .campo.formulado.recalculado select { animation: fx-pulso 1.2s ease-out; }
@keyframes fx-pulso { 0% { box-shadow: 0 0 0 4px rgba(106,76,226,.45); } 100% { box-shadow: 0 0 0 0 rgba(106,76,226,0); } }
.leyenda-fx { font-size: .78rem; color: #6a5aa8; }

/* Respuestas pendientes */
.tabla tr.fila-activa td { background: #fdf1f2; }
.resp-aviso { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap;
  border-left:5px solid #b42318; }
.resp-aviso .cifras { display:flex; gap:8px; flex-wrap:wrap; }
