:root {
  --bg: #f5f6f8;
  --surface: #fcfcfb;
  --surface-2: #eef0f4;
  --text: #0f1420;
  --text-2: #4d5566;
  --muted: #6b7385;
  --line: #dfe3ea;
  --grid: #e8eaee;
  --accent: #1f4fd1;
  --accent-ink: #ffffff;
  --accent-soft: #e3eafb;
  --good: #0a7d3b;
  --good-soft: #e1f4e8;
  --warn: #9a5b00;
  --warn-soft: #fff1d9;
  --bad: #b3261e;
  --bad-soft: #fbe4e2;
  /* gráficos: paleta categórica validada (CVD) — punta, llano, valle */
  --p1: #eb6834;
  --p2: #2a78d6;
  --p3: #1baf7a;
  --series: #2a78d6;
  --shadow: 0 1px 2px rgb(15 20 32 / .05), 0 6px 18px rgb(15 20 32 / .06);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101318;
    --surface: #1a1a19;
    --surface-2: #24262b;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9a9ca3;
    --line: #2f3238;
    --grid: #2a2c30;
    --accent: #6d95ff;
    --accent-ink: #0a1430;
    --accent-soft: #1d2945;
    --good: #5fd08c;
    --good-soft: #163524;
    --warn: #ffbf5e;
    --warn-soft: #3a2b12;
    --bad: #ff8a80;
    --bad-soft: #3d1c1a;
    --p1: #d95926;
    --p2: #3987e5;
    --p3: #199e70;
    --series: #3987e5;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 8px 24px rgb(0 0 0 / .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { flex: none; }
[hidden] { display: none !important; }
h2, h3, h4 { letter-spacing: -.01em; }

/* ---------- Cabecera ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 9px; }
.brand h1 { margin: 0; font-size: 19px; }
.brand p { margin: 0; font-size: 12.5px; color: var(--muted); }
.top-actions { display: flex; gap: 6px; }
.wrap { max-width: 820px; margin: 0 auto; padding: 4px 16px 0; }

/* ---------- Botones y campos ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; }
.icon-btn { display: grid; place-items: center; width: 36px; height: 36px; border: 0; border-radius: 10px; background: transparent; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 13.5px; font-weight: 700; color: var(--text-2); }
.field input, .field select, .suffix {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); outline: none;
}
.suffix { display: flex; align-items: center; gap: 6px; padding: 0 12px; }
.field .suffix input { width: auto; border: 0; background: transparent; outline: none; box-shadow: none; min-height: 42px; flex: 1; min-width: 0; padding: 0; }
.suffix span { color: var(--muted); font-size: 13.5px; white-space: nowrap; }
.field input:focus, .field select:focus, .suffix:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.help { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.muted { color: var(--muted); font-size: 14px; }

/* ---------- Inicio ---------- */
.hero { padding: 18px 0 8px; }
.hero h2 { font-size: clamp(24px, 5.5vw, 34px); line-height: 1.15; margin: 0 0 12px; }
.hero h2 em { font-style: normal; color: var(--accent); }
.hero p { margin: 0; color: var(--text-2); font-size: 17px; max-width: 620px; }
.drop {
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
  margin: 22px 0 12px; padding: 30px 18px; border-radius: 20px; cursor: pointer;
  border: 2px dashed color-mix(in srgb, var(--accent) 45%, var(--line)); background: var(--surface);
  transition: background .15s, border-color .15s;
}
.drop:hover, .drop.over { background: var(--accent-soft); border-color: var(--accent); }
.drop svg { width: 38px; height: 38px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.drop strong { font-size: 18px; }
.drop span { color: var(--muted); font-size: 14px; max-width: 380px; }
.intro-actions { display: flex; justify-content: center; }
.privacy { text-align: center; color: var(--muted); font-size: 14px; margin: 14px 0 22px; }
.howto { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; }
.howto summary { font-weight: 700; cursor: pointer; }
.howto ol { padding-left: 20px; margin: 12px 0 6px; }
.howto li { margin-bottom: 6px; }
.error-box { background: var(--bad-soft); color: var(--text); border-radius: 12px; padding: 12px 14px; margin: 0 0 12px; }

/* ---------- Resultados ---------- */
.loaded {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 14px; margin: 6px 0 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line);
}
.loaded b { display: block; }
.loaded span { font-size: 13.5px; color: var(--muted); }
.loaded-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.kpi { display: flex; flex-direction: column; gap: 2px; padding: 16px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); min-width: 0; }
.kpi .label { font-size: 13px; font-weight: 600; color: var(--muted); }
.kpi strong { font-size: 26px; line-height: 1.15; letter-spacing: -.02em; }
.kpi .sub { font-size: 12.5px; color: var(--muted); }
.hero-kpi { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.hero-kpi strong { font-size: 34px; }
.hero-kpi .label, .hero-kpi .sub { color: color-mix(in srgb, var(--accent-ink) 80%, transparent); }
@media (max-width: 600px) { .kpis { grid-template-columns: 1fr 1fr; } .hero-kpi { grid-column: 1 / -1; } }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; gap: 12px; margin-bottom: 16px; }
.card-head h3 { margin: 2px 0 2px; font-size: 20px; }
.card-head p { margin: 0; color: var(--muted); font-size: 14.5px; }
.step { flex: none; display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 800; }
.chart-title { margin: 24px 0 6px; font-size: 15px; }

.pot-inputs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 480px) { .pot-inputs { grid-template-columns: 1fr; } }

.recs { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
@media (max-width: 600px) { .recs { grid-template-columns: 1fr; } }
.rec { border-radius: 14px; padding: 14px; border: 1px solid var(--line); background: var(--bg); }
.rec h5 { margin: 0 0 6px; font-size: 13.5px; color: var(--text-2); }
.rec .big { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.rec .big strong { font-size: 26px; }
.rec .big s { color: var(--muted); }
.rec p { margin: 6px 0 0; font-size: 14px; color: var(--text-2); }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 99px; font-size: 12.5px; font-weight: 700; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.neutral { background: var(--surface-2); color: var(--text-2); }

.apps { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.apps summary, .table-view summary { cursor: pointer; font-weight: 700; color: var(--accent); }
.table-view summary { font-size: 13.5px; font-weight: 600; margin-top: 4px; }
.app-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.app-list label { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 14.5px; cursor: pointer; }
.app-list label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.app-list input { width: 18px; height: 18px; accent-color: var(--accent); }
.app-list small { margin-left: auto; color: var(--muted); white-space: nowrap; }
.apps-total { font-weight: 700; margin: 12px 0 0; }

/* ---------- Gráficos ---------- */
.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.chart .mark { transition: opacity .12s; }
.chart .mark.dim { opacity: .4; }
.chart .ref { stroke: var(--text-2); stroke-width: 1.5; }
.chart .ref.rec { stroke: var(--good); }
.chart .ref-label { fill: var(--text-2); font-size: 12px; font-weight: 600; }
.tip {
  position: absolute; z-index: 3; pointer-events: none; padding: 8px 10px; border-radius: 10px;
  background: var(--text); color: var(--bg); font-size: 13px; line-height: 1.35; white-space: nowrap; box-shadow: var(--shadow);
}
.tip b { display: block; }

.stack { display: flex; gap: 2px; height: 22px; border-radius: 6px; overflow: hidden; }
.seg { min-width: 4px; }
.seg:first-child { border-radius: 4px 0 0 4px; }
.seg:last-child { border-radius: 0 4px 4px 0; }
.legend { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 8px; }
.legend li { display: grid; grid-template-columns: 12px 1fr auto auto; align-items: center; gap: 10px; font-size: 14.5px; }
.legend li i { width: 12px; height: 12px; border-radius: 3px; }
.legend small { display: block; color: var(--muted); font-size: 12.5px; }
.legend em { font-style: normal; color: var(--muted); font-size: 13.5px; min-width: 88px; text-align: right; font-variant-numeric: tabular-nums; }
.legend.inline { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 0 0 8px; }
.legend.inline li { display: flex; gap: 6px; font-size: 13px; color: var(--text-2); }
.tip-box { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; margin: 14px 0 0; font-size: 14.5px; }

table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; margin-top: 8px; font-variant-numeric: tabular-nums; }
table.data th, table.data td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: right; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 600; }

/* ---------- Tarifas ---------- */
.tariffs { display: grid; gap: 12px; margin-bottom: 12px; }
.tariff { border: 1px solid var(--line); border-radius: 16px; padding: 14px; background: var(--bg); }
.tariff.current { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.tariff-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.tariff-head input { flex: 1; min-width: 0; font-weight: 700; font-size: 16px; border: 0; background: transparent; outline: none; padding: 4px 0; border-bottom: 1px dashed transparent; }
.tariff-head input:focus { border-bottom-color: var(--accent); }
.seg-toggle { display: inline-flex; padding: 3px; border-radius: 10px; background: var(--surface-2); margin-bottom: 10px; }
.seg-toggle button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.seg-toggle button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.tariff-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.tariff-grid .field > span { font-size: 12.5px; }
.tariff-grid .suffix { padding: 0 10px; }
.tariff-grid .suffix span { font-size: 12px; }
@media (max-width: 560px) { .tariff-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.compare { margin-top: 18px; }
.cmp-row { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 2fr) auto; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.cmp-row .name { font-weight: 700; overflow-wrap: anywhere; }
.cmp-row .name small { display: block; font-weight: 500; color: var(--muted); }
.cmp-row .bar { height: 14px; border-radius: 0 4px 4px 0; background: var(--series); min-width: 4px; }
.cmp-row.best .bar { background: var(--good); }
.cmp-row .val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cmp-row .val small { display: block; font-weight: 600; font-size: 12.5px; }
.cmp-row .val .less { color: var(--good); }
.cmp-row .val .more { color: var(--bad); }
@media (max-width: 560px) {
  .cmp-row { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "name name" "bar val"; gap: 6px 12px; }
  .cmp-row .name { grid-area: name; }
  .cmp-row .bar { grid-area: bar; }
  .cmp-row .val { grid-area: val; }
}
.cmp-note { font-size: 13.5px; color: var(--muted); margin: 10px 0 0; }

.disclaimer { font-size: 12.5px; color: var(--muted); margin: 6px 4px 0; }

/* ---------- Aviso ---------- */
.toast {
  position: fixed; inset: auto auto calc(24px + env(safe-area-inset-bottom)) 50%; margin: 0; border: 0;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none; padding: 12px 18px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 14.5px; transition: opacity .25s, transform .25s;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Informe impreso ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; color-scheme: light; }
  .top, .loaded-actions, #addTariff, .icon-btn, .apps, .toast, .table-view summary { display: none !important; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
  .table-view[open] > div { display: block; }
  body { padding: 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
