:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1c2530;
  --muted: #6b7885;
  --line: #e4e9ef;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --accent: #0ea5a3;
  --warn-bg: #fef3c7; --warn-ink: #92600a;
  --bad-bg: #fee2e2; --bad-ink: #991b1b;
  --ok-bg: #dcfce7; --ok-ink: #166534;
  --info-bg: #e0f2fe; --info-ink: #075985;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.1);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151b; --panel: #171f28; --ink: #e6edf3; --muted: #94a3b1;
    --line: #263241; --brand: #14b8a6; --shadow: none;
    --warn-bg:#3a2f10;--warn-ink:#f6d38a;--bad-bg:#3b1f22;--bad-ink:#fca5a5;
    --ok-bg:#12301f;--ok-ink:#86efac;--info-bg:#0e2a3b;--info-ink:#7dd3fc;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: inherit; text-decoration: none; }

.layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--panel);
  border-right: 1px solid var(--line); padding: 20px 14px;
  display: flex; flex-direction: column; gap: 6px; position: sticky; top: 0; height: 100vh;
}
.brand { font-size: 20px; font-weight: 800; padding: 6px 10px 16px; letter-spacing: -.02em; }
.brand span { color: var(--brand); }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a {
  padding: 9px 12px; border-radius: 10px; color: var(--muted); font-weight: 500;
  transition: background .12s, color .12s;
}
.sidebar nav a:hover { background: var(--bg); color: var(--ink); }
.sidebar nav a.active { background: var(--brand); color: var(--brand-ink); }
.sidebar .foot { margin-top: auto; color: var(--muted); font-size: 12px; padding: 12px 10px; }

/* Content */
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 28px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: -.01em; }
.body { padding: 24px 28px; }

/* Cards */
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px;
}
.card h2 { margin: 0 0 12px; font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.kpi { display: flex; align-items: center; gap: 14px; }
.kpi-icon { font-size: 26px; width: 46px; height: 46px; display: grid; place-items: center; background: var(--bg); border-radius: 12px; }
.kpi-value { font-size: 24px; font-weight: 800; letter-spacing: -.02em; }
.kpi-label { font-size: 12.5px; color: var(--muted); }

.grid2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; }
@media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; color: var(--muted); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--line);
}
tbody td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg); }
.table-wrap { overflow-x: auto; }

.muted { color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge-bad  { background: var(--bad-bg);  color: var(--bad-ink); }
.badge-ok   { background: var(--ok-bg);   color: var(--ok-ink); }
.badge-info { background: var(--info-bg); color: var(--info-ink); }
.badge-muted{ background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.alert-row { padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.alert-row:last-child { border-bottom: none; }

/* Progress bar */
.bar { background: var(--bg); border-radius: 999px; height: 8px; overflow: hidden; min-width: 90px; }
.bar > i { display: block; height: 100%; background: var(--accent); }

.pill-row { display: flex; gap: 6px; flex-wrap: wrap; }
.empty { color: var(--muted); padding: 20px; text-align: center; }

/* Formularios */
form input, form select {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); font-size: 14px;
}
form button {
  padding: 8px 14px; border: none; border-radius: 8px; background: var(--brand);
  color: var(--brand-ink); font-weight: 600; cursor: pointer;
}
form button:hover { filter: brightness(1.08); }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.form-row input { flex: 1; min-width: 140px; }
.form-inline { display: flex; gap: 6px; align-items: center; }
.form-inline form { margin: 0; }

/* Botones secundarios */
.btn-soft { background: var(--bg); color: var(--ink); border: 1px solid var(--line); }
.btn-soft:hover { background: var(--panel); }
.btn-danger { background: var(--bad-bg); color: var(--bad-ink); }
.acciones { display: flex; flex-direction: column; gap: 6px; }
.acciones form { margin: 0; }

/* Flash */
.flash { padding: 10px 14px; border-radius: 10px; margin-bottom: 16px; font-weight: 600; }
.flash-ok { background: var(--ok-bg); color: var(--ok-ink); }
.flash-err { background: var(--bad-bg); color: var(--bad-ink); }

/* Gráfico de barras */
.chart { display: flex; align-items: flex-end; gap: 10px; height: 150px; padding-top: 8px; }
.chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.chart-barwrap { width: 100%; height: 104px; display: flex; align-items: flex-end; }
.chart-bar { width: 100%; background: var(--accent); border-radius: 6px 6px 0 0; min-height: 3px; }
.chart-val { font-size: 12px; font-weight: 700; }
.chart-lbl { font-size: 11px; color: var(--muted); }

/* Ranking */
.rank-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.rank-row:last-child { border-bottom: none; }
.rank-name { width: 130px; flex-shrink: 0; font-weight: 500; }
.rank-row .bar { flex: 1; }
.rank-pct { width: 52px; text-align: right; }
.demo-flag { font-size: 11px; background: var(--warn-bg); color: var(--warn-ink); padding: 2px 8px; border-radius: 999px; font-weight: 700; }

/* --- Menú móvil (hamburguesa + drawer) --- */
.menu-btn {
  display: none; background: none; border: 1px solid var(--line); color: var(--ink);
  width: 40px; height: 40px; border-radius: 10px; font-size: 20px; cursor: pointer;
  align-items: center; justify-content: center; flex-shrink: 0;
}
.backdrop { display: none; }

@media (max-width: 860px) {
  .sidebar {
    position: fixed; top: 0; left: 0; z-index: 60; height: 100vh; width: 260px;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 0 0 50px rgba(0,0,0,.35);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .backdrop {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50;
  }
  .menu-btn { display: inline-flex; }
  .topbar { position: sticky; top: 0; z-index: 30; gap: 12px; padding: 14px 16px; }
  .topbar h1 { font-size: 18px; flex: 1; }
  .body { padding: 16px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .grid2 { grid-template-columns: 1fr; }
  .form-row { flex-direction: column; align-items: stretch; }
  .form-row input, .form-row select, .form-row button { width: 100%; min-width: 0; }
  .userbox .user-name { display: none; }
  .acciones { gap: 8px; }
}

/* --- Fichas de detalle --- */
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.crumbs a { color: var(--brand); font-weight: 600; }
.detalle-head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.avatar {
  width: 54px; height: 54px; border-radius: 50%; background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center; font-size: 22px; font-weight: 800; flex-shrink: 0;
}
.detalle-head h2 { margin: 0; font-size: 20px; text-transform: none; letter-spacing: 0; color: var(--ink); }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin: 16px 0; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat-num { font-size: 22px; font-weight: 800; letter-spacing: -.02em; }
.stat-lbl { font-size: 12px; color: var(--muted); }

/* Timeline (historial) */
.tl { list-style: none; margin: 0; padding: 0; }
.tl li { position: relative; padding: 0 0 14px 20px; border-left: 2px solid var(--line); }
.tl li:last-child { border-left-color: transparent; padding-bottom: 0; }
.tl li::before { content: ''; position: absolute; left: -6px; top: 3px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }
.tl-ev { font-weight: 600; }
.tl-meta { font-size: 12px; color: var(--muted); }

/* Tabs de filtro */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); font-weight: 600; font-size: 14px; }
.tabs a.active { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* Enlaces en filas de tablas */
.rowlink { color: var(--ink); font-weight: 700; }
.rowlink:hover { color: var(--brand); }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 360px; }
.login-card label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; font-weight: 600; }
.login-card input { width: 100%; }

/* Barra de usuario (topbar) */
.userbox { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.user-name { font-weight: 600; }
.logout { padding: 6px 12px; border-radius: 8px; border: 1px solid var(--line); color: var(--muted); font-weight: 600; }
.logout:hover { background: var(--bg); color: var(--ink); }

/* Código de vinculación (se muestra una sola vez) */
.codigo-card { border: 1px solid var(--brand); }
.codigo-valor {
  font: 700 34px/1.1 ui-monospace, "SFMono-Regular", "Consolas", monospace;
  letter-spacing: .12em; color: var(--brand);
  background: var(--bg); border: 1px dashed var(--brand); border-radius: 10px;
  padding: 16px 20px; margin-top: 6px; display: inline-block;
  user-select: all;
}
