:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-soft: #cde2fb;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #006300;
  --critical: #d03b3b;
  --warning-bg: #fff4d6;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-soft: #1c3a5e;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good: #0ca30c;
    --critical: #e66767;
    --warning-bg: #3a2f12;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --accent-soft: #1c3a5e;
  --series-1: #3987e5;
  --series-2: #d95926;
  --good: #0ca30c;
  --critical: #e66767;
  --warning-bg: #3a2f12;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 var(--font);
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 700; font-size: 16px; padding: 0 10px 16px; }
.brand small { display: block; font-weight: 400; color: var(--text-muted); font-size: 12px; }
.nav-area { color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; padding: 12px 10px 4px; }
.nav-link {
  display: block; padding: 8px 10px; border-radius: 8px;
  color: var(--text-secondary); text-decoration: none;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--accent-soft); color: var(--text-primary); font-weight: 600; }
.nav-soon { color: var(--text-muted); padding: 8px 10px; font-size: 13px; }
.sidebar-footer { margin-top: auto; font-size: 12px; color: var(--text-muted); padding: 0 10px; display: flex; flex-direction: column; gap: 10px; }
.theme-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; align-self: flex-start; }
.theme-switch button { border: 0; background: var(--surface); color: var(--text-secondary); padding: 5px 10px; cursor: pointer; font-size: 12px; }
.theme-switch button:hover { background: var(--surface-2); }
.theme-switch button[aria-pressed="true"] { background: var(--accent-soft); color: var(--text-primary); font-weight: 600; }

/* ---------- Pestañas de página ---------- */
.page-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.page-tabs button {
  border: 0; background: none; padding: 9px 14px; cursor: pointer; color: var(--text-secondary);
  border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 600;
}
.page-tabs button:hover { color: var(--text-primary); }
.page-tabs button[aria-selected="true"] { color: var(--text-primary); border-bottom-color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- Alertas, exclusiones y formularios ---------- */
.alerts.has-alerts { border-color: var(--critical); }
.alert-group + .alert-group { margin-top: 14px; }
.alert-rule { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 6px; }
.alert-rule .count { background: var(--surface-2); border-radius: 999px; padding: 1px 9px; font-size: 12px; }
.alert-rule .count.hot { background: var(--critical); color: #fff; }
.ok-line { color: var(--good); margin: 4px 0; }
.rule-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.rule-row input[type="number"] { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; }
.btn-row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.form-error { color: var(--critical); min-height: 18px; font-size: 13px; margin: 4px 0; }
.exclusion { border-top: 1px solid var(--grid); margin-top: 16px; padding-top: 14px; }
.exclusion h3 { margin: 0 0 6px; font-size: 14px; }
.exclusion .field { margin-bottom: 10px; }
.exclusion select, .exclusion textarea, .form textarea {
  width: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; font: inherit; color: inherit;
}
.tag { display: inline-block; font-size: 11px; border-radius: 6px; padding: 1px 6px; background: var(--surface-2); color: var(--text-secondary); margin-left: 6px; }
.tag.excluded { background: var(--warning-bg); color: var(--text-primary); }
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; align-items: end; }
.form .field input, .form .field select { width: 100%; min-width: 0; }
.checks { display: flex; flex-wrap: wrap; gap: 10px; }
.checks label { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; color: var(--text-primary); }
.muted { color: var(--text-muted); }
.sync-error { color: var(--critical); }

/* ---------- Recorrido (diálogo) ---------- */
.modal { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text-primary); padding: 0; width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 64px); }
.modal::backdrop { background: rgba(0, 0, 0, 0.45); }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 18px 20px 12px; border-bottom: 1px solid var(--grid); }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-head p { margin: 2px 0 0; color: var(--text-secondary); }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 16px; }
.facts div { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.facts small { display: block; color: var(--text-muted); font-size: 11px; }
.facts strong { font-size: 15px; }
.problem { color: var(--text-secondary); margin: 0 0 16px; white-space: pre-wrap; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 14px 1fr auto; gap: 10px; padding-bottom: 14px; position: relative; }
.timeline li::before { content: ""; position: absolute; left: 6px; top: 16px; bottom: 0; width: 2px; background: var(--grid); }
.timeline li:last-child::before { display: none; }
.timeline .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--dot, var(--baseline)); margin-top: 3px; box-shadow: 0 0 0 2px var(--surface); }
.timeline .what { font-weight: 600; }
.timeline .when { color: var(--text-muted); font-size: 12px; }
.timeline .dur { font-variant-numeric: tabular-nums; text-align: right; font-weight: 600; }
.timeline .dur small { display: block; font-weight: 400; color: var(--text-muted); font-size: 11px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; font: inherit; }
.path { display: inline-flex; gap: 3px; align-items: center; }
.path i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: var(--dot); }
.sidebar-footer a { color: var(--text-secondary); }

main { padding: 24px 28px 48px; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 22px; }
.page-head p { margin: 2px 0 0; color: var(--text-secondary); }
.head-actions { display: flex; align-items: center; gap: 10px; color: var(--text-muted); font-size: 13px; }

.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 7px 12px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }

.banner { background: var(--warning-bg); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; }
.error { background: var(--surface); border: 1px solid var(--critical); border-radius: var(--radius); padding: 16px; }

/* ---------- Filtros ---------- */
.filters {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: end;
  margin-bottom: 18px;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--text-muted); }
.field select, .field input {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 10px; min-width: 150px;
}
.field input[type="search"] { min-width: 230px; }
.field.custom-range { flex-direction: row; gap: 6px; align-items: end; }
.field.custom-range input { min-width: 0; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; background: var(--surface); padding: 5px 12px; cursor: pointer; color: var(--text-secondary); }
.segmented button.on { background: var(--accent-soft); color: var(--text-primary); font-weight: 600; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi .label { color: var(--text-secondary); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 600; margin-top: 4px; }
.kpi .sub { color: var(--text-muted); font-size: 12px; margin-top: 2px; }
.kpi.hero .value { font-size: 40px; }

/* ---------- Tarjetas y gráficas ---------- */
.grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 12px; margin-bottom: 12px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.span-12 { grid-column: span 12; }
.span-8 { grid-column: span 8; }
.span-6 { grid-column: span 6; }
.span-4 { grid-column: span 4; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.card h2 { font-size: 15px; margin: 0; }
.card .hint { color: var(--text-muted); font-size: 12px; margin: 2px 0 0; }
.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 320px; }
.loading .chart-box, .loading .kpis, .loading .table-wrap { opacity: .45; transition: opacity .2s; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; position: sticky; top: 0; background: var(--surface); }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-primary); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }
.badge { display: inline-flex; align-items: center; gap: 6px; }
.badge::before { content: ""; width: 9px; height: 9px; border-radius: 3px; background: var(--dot, var(--baseline)); flex: none; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; color: var(--text-muted); font-size: 13px; }
.pager .btns { display: flex; gap: 6px; }
.empty { color: var(--text-muted); padding: 24px; text-align: center; }
.age-hot { color: var(--critical); font-weight: 600; }

/* ---------- Login ---------- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: 100%; max-width: 360px; }
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card p { margin: 0 0 18px; color: var(--text-secondary); }
.login-card .field { margin-bottom: 12px; }
.login-card .field input { width: 100%; }
.login-card .btn { width: 100%; padding: 10px; }
.login-error { color: var(--critical); min-height: 20px; font-size: 13px; margin-bottom: 6px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .span-8, .span-6, .span-4 { grid-column: span 12; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 12px 16px; }
  .brand { padding: 0 10px 0 0; }
  .nav-area, .nav-soon { display: none; }
  .sidebar-footer { margin: 0 0 0 auto; }
  main { padding: 16px; }
  .field, .field select, .field input, .field input[type="search"] { width: 100%; min-width: 0; }
  .kpi.hero .value { font-size: 32px; }
}
