:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --text: #1a2230;
  --muted: #64748b;
  --brand: #0f3d63;
  --brand-2: #1667a3;
  --accent: #0ea5a4;
  --ok: #16a34a;
  --ok-bg: #dcfce7;
  --warn: #d97706;
  --warn-bg: #fef3c7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --info: #2563eb;
  --info-bg: #dbeafe;
  --neutral-bg: #eef2f6;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 8px 24px rgba(16,24,40,.06);
  --radius: 12px;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Code", Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --surface: #161b22; --surface-2: #1c2430; --border: #2a3441;
    --text: #e6edf3; --muted: #94a3b8; --brand: #4aa3df; --brand-2: #6bb6e8;
    --neutral-bg: #222c38;
    --ok-bg: #14331f; --warn-bg: #3a2c0d; --bad-bg: #3a1717; --info-bg: #16263f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 14px; line-height: 1.45;
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px; background: var(--brand); color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid; place-items: center; width: 40px; height: 40px;
  background: rgba(255,255,255,.15); border-radius: 10px;
  font-weight: 800; letter-spacing: .5px;
}
.topbar h1 { margin: 0; font-size: 18px; font-weight: 700; }
.topbar .sub { margin: 0; font-size: 12px; opacity: .8; }
.badge-sync {
  background: rgba(255,255,255,.14); color: #fff;
  padding: 6px 12px; border-radius: 999px; font-size: 12px; font-weight: 600;
}

main { max-width: 1400px; margin: 0 auto; padding: 22px; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 22px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.kpi .n { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .l { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.kpi .n small { font-size: 13px; color: var(--muted); font-weight: 500; }

.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 12px; }

/* ---------- Team summary ---------- */
.team-summary { margin-bottom: 24px; }
.team-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.team-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); cursor: pointer; transition: transform .08s, border-color .12s;
}
.team-card:hover { transform: translateY(-2px); border-color: var(--brand-2); }
.team-card.active { border-color: var(--brand-2); box-shadow: 0 0 0 2px var(--brand-2) inset; }
.team-card h3 { margin: 0 0 2px; font-size: 15px; letter-spacing: .02em; }
.team-card .ent { font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.metric { margin: 7px 0; }
.metric .row { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 3px; }
.metric .row span:last-child { font-variant-numeric: tabular-nums; font-weight: 600; }
.bar { height: 6px; background: var(--neutral-bg); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; background: var(--accent); }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.search {
  flex: 1 1 280px; min-width: 220px; padding: 10px 14px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); font-size: 14px;
}
.select {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); font-size: 13px; cursor: pointer;
}
.search:focus, .select:focus { outline: 2px solid var(--brand-2); outline-offset: 0; border-color: var(--brand-2); }
.count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.btn-ghost {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: transparent; color: var(--muted); cursor: pointer; font-size: 13px;
}
.btn-ghost:hover { color: var(--text); border-color: var(--brand-2); }

/* ---------- Table ---------- */
.table-section { scroll-margin-top: 78px; }
.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; }
thead th {
  background: var(--surface-2); text-align: left; font-size: 11px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); padding: 11px 14px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sortable.sorted::after { content: " ▾"; color: var(--brand-2); }
th.sortable.sorted.asc::after { content: " ▴"; }
th.num, td.num { text-align: right; }
tbody td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
.rut { font-family: var(--mono); font-size: 13px; white-space: nowrap; }
.ent-name { font-weight: 600; }
.cli { color: var(--muted); }
.people { font-size: 12px; color: var(--muted); }
.people b { color: var(--text); font-weight: 600; display: block; }

/* módulos chips */
td.mods, th.mods { white-space: nowrap; width: 132px; }
.mods span { display: inline-block; width: 22px; height: 20px; border-radius: 5px; margin-right: 3px; text-align: center;
  font-size: 10px; line-height: 20px; font-weight: 700; background: var(--neutral-bg); color: var(--muted); }
.mods span.on { background: var(--info-bg); color: var(--info); }

/* pills */
.pill { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; white-space: nowrap; }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.info { background: var(--info-bg); color: var(--info); }
.pill.mute { background: var(--neutral-bg); color: var(--muted); }
.dj-mini { font-variant-numeric: tabular-nums; font-size: 12px; }
.dj-mini b { font-weight: 700; }

/* ---------- Drawer ---------- */
.drawer[hidden], .overlay[hidden] { display: none; }
.overlay { position: fixed; inset: 0; background: rgba(10,18,30,.45); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(560px, 94vw);
  background: var(--surface); z-index: 50; box-shadow: -8px 0 30px rgba(0,0,0,.2);
  display: flex; flex-direction: column; animation: slidein .18s ease;
}
@keyframes slidein { from { transform: translateX(100%); } to { transform: translateX(0); } }
.drawer-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 18px 20px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.drawer-head h2 { margin: 0; font-size: 18px; }
.btn-close { border: none; background: transparent; font-size: 28px; line-height: 1; color: var(--muted); cursor: pointer; }
.btn-close:hover { color: var(--text); }
.drawer-body { padding: 8px 20px 30px; overflow-y: auto; }

.mod { border: 1px solid var(--border); border-radius: 10px; margin: 14px 0; overflow: hidden; }
.mod-h { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 11px 14px; background: var(--surface-2); font-weight: 700; font-size: 13px; }
.mod-h .tag { font-size: 11px; font-weight: 600; color: var(--muted); }
.mod-b { padding: 12px 14px; }
.mod-b.empty { color: var(--muted); font-size: 13px; font-style: italic; }
.kv { display: grid; grid-template-columns: 42% 58%; gap: 4px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-word; }
.mini-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; }
.mini-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 5px 8px; border-bottom: 1px solid var(--border); }
.mini-table td { padding: 5px 8px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.mini-table tr:last-child td { border-bottom: none; }
.doc-cat { font-family: var(--mono); font-size: 11px; color: var(--muted); }

/* ---------- formularios de edición ---------- */
.upd { font-size: 11px; color: var(--muted); }
.edit-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.ef { display: flex; flex-direction: column; gap: 3px; font-size: 12px; }
.ef > span { color: var(--muted); }
.ef input, .ef select, .ef textarea {
  padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font-size: 13px; font-family: inherit; width: 100%;
}
.ef input:focus, .ef select:focus, .ef textarea:focus { outline: 2px solid var(--brand-2); outline-offset: 0; border-color: var(--brand-2); }
.ef textarea { resize: vertical; }
.ef:has(textarea) { grid-column: 1 / -1; }
.ef-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; margin-top: 4px; }
.btn-save {
  padding: 9px 18px; border: none; border-radius: 9px; background: var(--brand-2); color: #fff;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-save:hover { background: var(--brand); }
.btn-save:disabled { opacity: .6; cursor: default; }
.save-msg { font-size: 12px; font-weight: 600; }
.save-msg.ok { color: var(--ok); }
.save-msg.err { color: var(--bad); }
@media (max-width: 560px) { .edit-form { grid-template-columns: 1fr; } }

/* ---------- responsive ---------- */
@media (max-width: 1100px) { .kpis { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 720px) {
  main { padding: 14px; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  thead th { top: 60px; }
  .col-cli, .col-people { display: none; }
}
