:root {
  color-scheme: light;
  --page: #ffffff;
  --card: #ffffff;
  --card-2: #f6f6f4;
  --line: #e6e5e0;
  --grid: #ecebe6;
  --axis: #c3c2b7;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #6f6d67;
  --s1: #2a78d6;
  --s1-wash: rgba(42, 120, 214, 0.10);
  --s2: #eb6834;
  --s3: #a3a19a;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --up-bad: #c22f2f;
  --down-good: #006300;
  --chip-ink: #0b0b0b;
  --brand-red: #d03b3b;
  /* Color de texto de cada nivel: más oscuro sobre blanco, más claro sobre negro (contraste ≥ 3:1). */
  --lvl-normal: #0a7d0a;
  --lvl-leve: #9a6200;
  --lvl-grave: #c4521f;
  --lvl-critica: #c22f2f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #000000;
    --card: #0c0c0c;
    --card-2: #151515;
    --line: #232322;
    --grid: #1d1d1c;
    --axis: #383835;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --ink-3: #8f8d86;
    --s1: #3987e5;
    --s1-wash: rgba(57, 135, 229, 0.16);
    --s2: #d95926;
    --s3: #6f6d67;
    --up-bad: #f07171;
    --down-good: #2bc42b;
    --shadow: none;
    --brand-red: #ff5148;
    --lvl-normal: #2bc42b;
    --lvl-leve: #fab219;
    --lvl-grave: #f08a5d;
    --lvl-critica: #f07171;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #000000;
  --card: #0c0c0c;
  --card-2: #151515;
  --line: #232322;
  --grid: #1d1d1c;
  --axis: #383835;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --ink-3: #8f8d86;
  --s1: #3987e5;
  --s1-wash: rgba(57, 135, 229, 0.16);
  --s2: #d95926;
  --s3: #6f6d67;
  --up-bad: #f07171;
  --down-good: #2bc42b;
  --shadow: none;
  --brand-red: #ff5148;
  --lvl-normal: #2bc42b;
  --lvl-leve: #fab219;
  --lvl-grave: #f08a5d;
  --lvl-critica: #f07171;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.4;
  transition: background-color 0.3s ease, color 0.3s ease;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
.wrap { max-width: 1320px; margin: 0 auto; padding: 0 24px 48px; }
.num { font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Cabecera */
.top {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.top-inner {
  max-width: 1320px; margin: 0 auto; padding: 12px 24px;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
}
.logo { display: inline-flex; align-items: center; gap: 9px; color: var(--ink); text-decoration: none; }
.logo-mark { width: 28px; height: 28px; flex: none; }
.logo-word { font-size: 20px; font-weight: 750; letter-spacing: -0.03em; }
.logo-word span { color: var(--ink-3); font-weight: 600; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: -0.01em; margin-right: auto; display: flex; align-items: center; gap: 12px; }
.live {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12px; font-weight: 600; letter-spacing: 0.06em; color: var(--ink-2);
  padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px;
}
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); position: relative; }
.live .dot::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%;
  border: 2px solid var(--good); opacity: 0; animation: ping 1.6s ease-out infinite;
}
.live.stale .dot, .live.stale .dot::after { background: var(--critical); border-color: var(--critical); }
.live .ago { font-weight: 500; letter-spacing: 0; color: var(--ink-3); }
@keyframes ping { 0% { transform: scale(0.6); opacity: 0.9; } 100% { transform: scale(1.6); opacity: 0; } }

.seg { display: inline-flex; background: var(--card-2); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.seg button {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer;
  font-size: 14px; font-weight: 500; color: var(--ink-2); white-space: nowrap;
  transition: background-color 0.2s ease, color 0.2s ease;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--page); font-weight: 600; }

.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; display: grid; place-items: center;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn .sun { display: none; }
:root[data-theme="dark"] .icon-btn .sun { display: block; }
:root[data-theme="dark"] .icon-btn .moon { display: none; }

.progress { height: 2px; background: transparent; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--s1); transition: width 1s linear; }

/* Datos de Renfe sin actualizar: cifras atenuadas */
body.is-stale #hero .hero-value, body.is-stale #hero .gauge, body.is-stale #kpis { opacity: 0.45; transition: opacity 0.4s; }

/* Contexto: país y reloj */
.context {
  display: flex; justify-content: space-between; align-items: baseline; gap: 4px 16px; flex-wrap: wrap;
  margin-top: 14px; font-size: 13px; color: var(--ink-3);
}
.context time { color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* Tarjetas */
.grid { display: grid; gap: 16px; margin-top: 16px; }
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 20px; min-width: 0;
  transition: background-color 0.3s ease;
}
.card h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--ink-2); letter-spacing: 0.02em; }
.card .sub { font-size: 12.5px; color: var(--ink-3); margin-top: 2px; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }

/* Héroe */
.hero { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(320px, 1.25fr); gap: 32px; position: relative; overflow: hidden; }
.hero-label { font-size: 13px; font-weight: 600; color: var(--ink-2); letter-spacing: 0.02em; }
.hero-value { display: flex; align-items: baseline; gap: 10px; margin: 6px 0 10px; }
.hero-value .big {
  font-size: clamp(96px, 15vw, 176px); font-weight: 700; letter-spacing: -0.045em; line-height: 0.9;
  color: var(--lvl-ink, var(--ink)); transition: color 0.6s ease;
}
.hero-value .of { font-size: 20px; color: var(--ink-3); font-weight: 500; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px;
  font-size: 14px; font-weight: 600; background: var(--card-2); border: 1px solid var(--line);
}
.chip .sw { width: 10px; height: 10px; border-radius: 3px; background: var(--lvl, var(--axis)); }
.hero-meaning { margin-top: 12px; font-size: 16px; font-weight: 600; color: var(--ink-2); }
.hero-refs { margin-top: 18px; display: grid; gap: 6px; font-size: 14px; color: var(--ink-2); }
.hero-refs b { color: var(--ink); font-weight: 600; }
.hero-right { display: flex; flex-direction: column; gap: 18px; justify-content: center; min-width: 0; }
.flash { font-size: 15px; font-weight: 600; opacity: 0; transition: opacity 0.4s ease; white-space: nowrap; }
.flash.show { opacity: 1; }

.delta { font-weight: 600; white-space: nowrap; }
.delta.up { color: var(--up-bad); }
.delta.down { color: var(--down-good); }
.delta.flat { color: var(--ink-3); }

/* Medidor: escala de niveles con triángulos en el valor actual */
.gauge { position: relative; height: 104px; }
.gauge-track { position: absolute; left: 0; right: 0; top: 40px; height: 16px; border-radius: 6px; overflow: hidden; display: flex; gap: 2px; }
.gauge-band { height: 100%; opacity: 0.28; transition: opacity 0.6s ease; }
.gauge-band.on { opacity: 0.9; }
.gauge-tri {
  position: absolute; width: 20px; height: 12px; transform: translateX(-50%);
  background: var(--lvl-ink, var(--ink)); transition: left 1s cubic-bezier(.2,.8,.2,1), background-color 0.6s ease;
}
.gauge-tri.down { top: 25px; clip-path: polygon(0 0, 100% 0, 50% 100%); }
.gauge-tri.up { top: 59px; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.gauge-now {
  position: absolute; top: 2px; transform: translateX(-50%); white-space: nowrap;
  font-size: 13px; font-weight: 700; color: var(--lvl-ink, var(--ink)); transition: left 1s cubic-bezier(.2,.8,.2,1);
}
.gauge-ref { position: absolute; top: 40px; width: 0; height: 16px; border-left: 2px solid var(--ink-2); transition: left 1s ease; }
.gauge-ref span { position: absolute; top: 52px; left: 50%; transform: translateX(-50%); font-size: 11px; color: var(--ink-2); white-space: nowrap; }
.gauge-scale { position: absolute; left: 0; right: 0; top: 76px; height: 14px; font-size: 11px; color: var(--ink-3); }
.gauge-scale span { position: absolute; white-space: nowrap; }
.gauge-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.gauge-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.scan { position: absolute; inset: 0; pointer-events: none; overflow: hidden; border-radius: var(--radius); }
.scan::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 30%;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--s1) 14%, transparent), transparent);
  transform: translateX(-120%); animation: scan 1.4s ease-in-out;
}
.scan.loop::after { animation: scan 1.4s ease-in-out infinite; }
@keyframes scan { to { transform: translateX(420%); } }

/* KPIs */
.kpis { grid-template-columns: repeat(var(--kpi-n, 4), minmax(0, 1fr)); }
.kpi { position: relative; overflow: hidden; }
.kpi .val { font-size: clamp(40px, 4.4vw, 56px); font-weight: 700; letter-spacing: -0.03em; line-height: 1; margin-top: 10px; }
.kpi .val .v { color: var(--lvl-ink, var(--ink)); transition: color 0.6s ease; }
.kpi .val small { font-size: 0.45em; font-weight: 500; color: var(--ink-3); letter-spacing: 0; margin-left: 4px; }
.kpi .meta { display: flex; justify-content: space-between; gap: 8px; margin-top: 8px; font-size: 13px; color: var(--ink-3); flex-wrap: wrap; }
.kpi .spark { margin-top: 12px; height: 36px; }
.kpi .spark svg { display: block; width: 100%; height: 36px; overflow: visible; }
.kpi .flash { font-size: 12.5px; }

/* Ticker */
.ticker-card { padding: 14px 20px; }
.ticker { display: flex; align-items: center; gap: 16px; min-width: 0; }
.ticker .tag { font-size: 12px; font-weight: 700; letter-spacing: 0.06em; color: var(--ink-2); white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.ticker ul { list-style: none; margin: 0; padding: 0; display: flex; gap: 28px; overflow: hidden; flex: 1; mask-image: linear-gradient(90deg, #000 85%, transparent); }
.ticker li { white-space: nowrap; font-size: 13.5px; color: var(--ink-2); display: flex; gap: 8px; align-items: center; animation: slidein 0.6s ease both; }
.ticker li b { color: var(--ink); font-weight: 600; }
.ticker li .t { color: var(--ink-3); }
.ticker .empty { color: var(--ink-3); font-size: 13.5px; }
@keyframes slidein { from { opacity: 0; transform: translateX(-12px); } to { opacity: 1; transform: none; } }
.ev-ico { width: 14px; height: 14px; flex: none; }

/* Pestañas de métrica */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs button {
  border: 1px solid var(--line); background: transparent; color: var(--ink-2); padding: 6px 12px; border-radius: 999px;
  font-size: 13px; cursor: pointer; transition: all 0.2s ease;
}
.tabs button[aria-pressed="true"] { background: var(--ink); color: var(--page); border-color: var(--ink); font-weight: 600; }
.small-btn {
  border: 1px solid var(--line); background: transparent; color: var(--ink-2); padding: 5px 10px; border-radius: 8px;
  font-size: 12.5px; cursor: pointer;
}
.small-btn:hover { color: var(--ink); }

.charts { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart.loading { opacity: 0.55; transition: opacity 0.2s; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: var(--ink-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 16px; height: 2px; border-radius: 2px; display: inline-block; }
.legend i.box { width: 10px; height: 10px; border-radius: 3px; }
.empty-note { font-size: 13px; color: var(--ink-3); margin-top: 8px; }

.draw { stroke-dasharray: var(--len); stroke-dashoffset: var(--len); animation: draw 1.4s cubic-bezier(.3,.7,.2,1) forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
.pulse { animation: pulse 2s ease-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes pulse { 0% { opacity: 0.55; transform: scale(1); } 100% { opacity: 0; transform: scale(3.2); } }
.grow { transform-box: fill-box; transform-origin: bottom; animation: grow 0.8s cubic-bezier(.2,.8,.2,1) both; }
@keyframes grow { from { transform: scaleY(0); } }

/* Tooltip */
.tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--card); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 12.5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14); opacity: 0; transition: opacity 0.12s; min-width: 150px;
}
.tip.show { opacity: 1; }
.tip .tt { color: var(--ink-3); margin-bottom: 6px; font-size: 12px; }
.tip .row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.tip .row + .row { margin-top: 4px; }
.tip .row b { font-size: 14px; font-variant-numeric: tabular-nums; }
.tip .row span { color: var(--ink-2); display: inline-flex; align-items: center; gap: 6px; }
.tip .key { width: 12px; height: 2px; border-radius: 2px; display: inline-block; }

/* Detalle */
.detail-toggle {
  margin: 24px auto 0; display: flex; align-items: center; gap: 8px; border: 1px solid var(--line);
  background: var(--card); padding: 10px 18px; border-radius: 999px; cursor: pointer; font-size: 14px; font-weight: 600;
}
.detail-toggle svg { width: 16px; height: 16px; transition: transform 0.3s; }
.detail-toggle[aria-expanded="true"] svg { transform: rotate(180deg); }
.detail { grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); }
.detail[hidden] { display: none; }
.hbars { display: grid; gap: 10px; }
.hbar { display: grid; grid-template-columns: 140px minmax(0, 1fr) 150px; gap: 12px; align-items: center; font-size: 13.5px; }
.hbar .name { color: var(--ink); font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .bar { height: 14px; background: var(--card-2); border-radius: 0 4px 4px 0; position: relative; }
.hbar .bar i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; transition: width 0.9s cubic-bezier(.2,.8,.2,1); }
.hbar .v { text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbar .v b { color: var(--ink); }
.span2 { grid-column: 1 / -1; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th { text-align: left; font-weight: 600; color: var(--ink-3); font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }
.dbar { display: flex; align-items: center; gap: 8px; justify-content: flex-end; }
.dbar i { display: inline-block; height: 8px; border-radius: 0 4px 4px 0; }
.muted { color: var(--ink-3); }
.badge { font-size: 11.5px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--ink-2); white-space: nowrap; }
.badge.over { border-color: color-mix(in srgb, var(--critical) 50%, transparent); color: var(--up-bad); }

footer { margin-top: 32px; padding-top: 20px; border-top: 1px solid var(--line); color: var(--ink-3); font-size: 12.5px; display: grid; gap: 8px; }
footer details summary { cursor: pointer; color: var(--ink-2); font-weight: 600; }
footer nav { display: flex; gap: 16px; flex-wrap: wrap; }
.legal { max-width: 760px; padding-top: 32px; }
.legal h1 { font-size: 32px; letter-spacing: -0.02em; margin: 16px 0; }
.legal h2 { font-size: 17px; margin: 28px 0 8px; }
.legal p { color: var(--ink-2); line-height: 1.6; }
.legal a { color: var(--ink); }
footer details p { margin: 6px 0; max-width: 820px; }
footer a { color: var(--ink-2); }

.intro { position: fixed; inset: 0; z-index: 40; background: var(--page); display: grid; place-items: center; transition: opacity 0.5s ease, visibility 0.5s; }
.intro.done { opacity: 0; visibility: hidden; }
.intro-box { text-align: center; }
.intro-count { font-size: 64px; font-weight: 650; letter-spacing: -0.03em; }
.intro-text { color: var(--ink-2); font-size: 15px; margin-top: 6px; }
.intro-bar { width: 260px; height: 3px; background: var(--card-2); border-radius: 3px; margin: 18px auto 0; overflow: hidden; }
.intro-bar i { display: block; height: 100%; width: 40%; background: var(--s1); border-radius: 3px; animation: indet 1.1s ease-in-out infinite; }
@keyframes indet { from { transform: translateX(-100%); } to { transform: translateX(250%); } }

.reveal { animation: reveal 0.7s cubic-bezier(.2,.8,.2,1) both; }
@keyframes reveal { from { opacity: 0; transform: translateY(10px); } }

@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .charts, .detail { grid-template-columns: minmax(0, 1fr); }
  .hero { grid-template-columns: minmax(0, 1fr); gap: 20px; }
}
@media (max-width: 1000px) {
  .kpis[data-n="3"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .kpis[data-n="3"] { grid-template-columns: minmax(0, 1fr); }
  .wrap, .top-inner { padding-left: 16px; padding-right: 16px; }
  .top-inner { gap: 10px; }
  .brand { order: 1; flex: 1 1 0; min-width: 0; flex-wrap: wrap; gap: 8px; font-size: 16px; }
  .icon-btn { order: 2; }
  .seg { order: 3; width: 100%; overflow-x: auto; }
  .seg button { padding: 7px 9px; font-size: 12.5px; }
  .hbar { grid-template-columns: 100px minmax(0, 1fr); }
  .hbar .v { grid-column: 2; text-align: left; }
  .card { padding: 16px; }
}
@media (max-width: 460px) {
  .kpis { grid-template-columns: minmax(0, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .draw { stroke-dashoffset: 0; }
}
