:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --fg: #16181d;
  --muted: #646a76;
  --border: #e1e4ea;
  --track: #e8ebf0;
  --green: #1f9d55;
  --amber: #d98a00;
  --red: #d42a2a;
  --unknown: #8a909c;
  --green-bg: #e5f6ec;
  --amber-bg: #fdf1dc;
  --red-bg: #fde5e5;
  --c1: #2f6fde;
  --c2: #9b51e0;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #0f1115; --card: #181b21; --fg: #e8eaee; --muted: #9aa1ad; --border: #2a2f38;
    --track: #2a2f38; --green: #3fca7a; --amber: #f2a93b; --red: #ff5a5a; --unknown: #7c8390;
    --green-bg: #12301f; --amber-bg: #3a2a0e; --red-bg: #3d1414;
    --c1: #6ea3ff; --c2: #c08cff; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --card: #181b21; --fg: #e8eaee; --muted: #9aa1ad; --border: #2a2f38;
  --track: #2a2f38; --green: #3fca7a; --amber: #f2a93b; --red: #ff5a5a; --unknown: #7c8390;
  --green-bg: #12301f; --amber-bg: #3a2a0e; --red-bg: #3d1414;
  --c1: #6ea3ff; --c2: #c08cff; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 760px;
  padding: 0 12px calc(24px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, p { margin: 0; }
h2 { font-size: 1.05rem; margin-bottom: 10px; display: flex; align-items: baseline; gap: 8px; }
h2 small { font-weight: 400; font-size: .78rem; color: var(--muted); margin-left: auto; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.num { font-variant-numeric: tabular-nums; }

/* ── Aviso de dato rancio: lo primero y lo más gordo ── */
.stale {
  position: sticky; top: 0; z-index: 10;
  margin: 0 -12px;
  padding: 18px 16px;
  padding-top: calc(18px + env(safe-area-inset-top));
  background: #c41c1c; color: #fff;      /* fijo: contraste igual en claro y oscuro */
  text-align: center;
  font-size: 1.15rem; line-height: 1.3;
  box-shadow: 0 4px 16px rgb(0 0 0 / 25%);
}
.stale strong { display: block; font-size: 1.35rem; letter-spacing: .02em; }
.stale span { display: block; margin-top: 6px; font-size: .95rem; opacity: .95; }
body.is-stale main, body.is-stale .problems { opacity: .5; filter: grayscale(.7); }

/* ── Semáforo ── */
.top { display: flex; align-items: center; gap: 12px; padding: 16px 0 12px; }
.light { flex: 1; display: flex; align-items: center; gap: 14px; padding: 14px 16px;
  border-radius: 16px; background: var(--card); border: 2px solid var(--unknown); box-shadow: var(--shadow); }
.light h1 { font-size: 1.4rem; line-height: 1.2; }
.lamp { display: flex; flex-direction: column; gap: 4px; padding: 6px; border-radius: 12px; background: #1d2027; }
.lamp i { width: 18px; height: 18px; border-radius: 50%; background: #3a3e47; }
.light[data-level="red"] { border-color: var(--red); background: var(--red-bg); }
.light[data-level="amber"] { border-color: var(--amber); background: var(--amber-bg); }
.light[data-level="green"] { border-color: var(--green); background: var(--green-bg); }
.light[data-level="red"] .lamp i:nth-child(1) { background: #ff4d4d; box-shadow: 0 0 10px #ff4d4d; }
.light[data-level="amber"] .lamp i:nth-child(2) { background: #ffb020; box-shadow: 0 0 10px #ffb020; }
.light[data-level="green"] .lamp i:nth-child(3) { background: #35d07f; box-shadow: 0 0 10px #35d07f; }
.icon-btn { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card); color: var(--fg); font-size: 1.2rem; cursor: pointer; }

.problems { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px; }
.problems li { padding: 10px 12px; border-radius: 10px; border-left: 4px solid; background: var(--card); }
.problems li.red { border-color: var(--red); background: var(--red-bg); }
.problems li.amber { border-color: var(--amber); background: var(--amber-bg); }

/* ── Tarjetas ── */
main { display: grid; gap: 12px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px; box-shadow: var(--shadow); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.box { border: 1px solid var(--border); border-left: 5px solid var(--unknown); border-radius: 10px; padding: 10px 12px; }
.box.green { border-left-color: var(--green); }
.box.amber { border-left-color: var(--amber); background: var(--amber-bg); }
.box.red { border-left-color: var(--red); background: var(--red-bg); }
.box .title { display: flex; align-items: center; justify-content: space-between; gap: 6px; font-weight: 600; }
.box dl { margin: 6px 0 0; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; font-size: .84rem; }
.box dt { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.box dd { margin: 0; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .75rem; font-weight: 600;
  background: var(--track); color: var(--fg); white-space: nowrap; }
.pill.green { background: var(--green); color: #fff; }
.pill.amber { background: var(--amber); color: #1b1300; }
.pill.red { background: var(--red); color: #fff; }

.restarts { margin-top: 8px; display: flex; align-items: baseline; gap: 8px; padding: 6px 8px;
  border-radius: 8px; background: var(--track); }
.restarts b { font-size: 1.6rem; line-height: 1; font-variant-numeric: tabular-nums; }
.restarts span { font-size: .85rem; line-height: 1.2; }
.restarts.amber { background: var(--amber); color: #1b1300; }
.restarts.red { background: var(--red); color: #fff; }

/* ── Barras ── */
.bar-row { margin-bottom: 14px; }
.bar-head { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.bar-head b { font-variant-numeric: tabular-nums; }
.bar { position: relative; height: 16px; border-radius: 8px; background: var(--track); overflow: hidden; }
.bar > span { position: absolute; inset: 0 auto 0 0; border-radius: 8px; background: var(--green); }
.bar.amber > span { background: var(--amber); }
.bar.red > span { background: var(--red); }
.bar .mark { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--fg); opacity: .35; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.kv div { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.kv span { display: block; font-size: .78rem; color: var(--muted); }
.kv b { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.kv .amber b { color: var(--amber); }
.kv .red b { color: var(--red); }

/* ── Copias ── */
.alert-box { padding: 14px; border-radius: 10px; font-weight: 600; font-size: 1.05rem; margin-bottom: 10px; }
.alert-box.red { background: var(--red); color: #fff; }
.alert-box.amber { background: var(--amber); color: #1b1300; }
.alert-box.green { background: var(--green-bg); color: var(--fg); border: 1px solid var(--green); }

/* ── Gráfica ── */
.chart-controls { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; padding: 8px 12px; background: transparent; color: var(--fg); font: inherit; font-size: .9rem; cursor: pointer; }
.seg button.on { background: var(--c1); color: #fff; }
select { flex: 1; min-width: 160px; padding: 8px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--card); color: var(--fg); font: inherit; font-size: .9rem; }
.readout { min-height: 1.4em; font-size: .85rem; margin-bottom: 4px; font-variant-numeric: tabular-nums; }
.chart { width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .thr-amber { stroke: var(--amber); stroke-dasharray: 4 4; }
.chart .thr-red { stroke: var(--red); stroke-dasharray: 4 4; }
.chart .cursor { stroke: var(--fg); opacity: .4; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .82rem; margin-top: 4px; }
.legend i { display: inline-block; width: 12px; height: 3px; margin-right: 4px; vertical-align: middle; }

footer { padding: 18px 2px; text-align: center; }
#source-error { display: block; color: var(--red); font-weight: 600; margin-bottom: 4px; }
