:root {
  color-scheme: dark;
  --bg: #0f1014;
  --surface: #181b20;
  --surface-2: #23272f;
  --border: rgba(204, 204, 220, 0.11);
  --text: #e4e6eb;
  --text-muted: #949ba8;
  --accent: #6e9fff;
  --on-accent: #0a1020;
  --danger: #ff7383;
  --star: #f2cc0c;
  --grid: rgba(204, 204, 220, 0.09);
  --bar-h: 52px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f2f3f6;
    --surface: #ffffff;
    --surface-2: #eceef2;
    --border: rgba(20, 24, 35, 0.12);
    --text: #1b1f27;
    --text-muted: #5c6472;
    --accent: #2459c9;
    --on-accent: #ffffff;
    --danger: #c4162a;
    --star: #c98a00;
    --grid: rgba(20, 24, 35, 0.09);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior-y: none;
  min-height: 100vh;
  min-height: 100dvh;
}
button, input { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; text-align: inherit; touch-action: manipulation; }
a { color: var(--accent); }
h1, h2, h3, h4, p { margin: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon { display: inline-flex; width: 22px; height: 22px; flex: none; }
.icon svg { width: 100%; height: 100%; }

/* ---------- estructura ---------- */
.view { min-height: 100dvh; padding-bottom: calc(24px + var(--safe-b)); }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  min-height: calc(var(--bar-h) + var(--safe-t));
  padding: var(--safe-t) calc(6px + var(--safe-r)) 0 calc(6px + var(--safe-l));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { flex: 1; min-width: 0; padding: 0 8px; }
.topbar-title h1 { font-size: 17px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small { display: block; font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small:empty { display: none; }
.home .topbar-title h1 { font-size: 22px; }
.icon-btn { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; color: var(--text); flex: none; }
.icon-btn:active { background: var(--surface-2); }
.star { color: var(--text-muted); }
.star.on { color: var(--star); }
.star.on svg { fill: currentColor; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px;
  background: var(--surface-2); color: var(--text); font-weight: 600; text-decoration: none; width: 100%;
}
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.6; }
.btn:active { filter: brightness(0.92); }
.link-btn { display: block; width: 100%; padding: 10px 4px; color: var(--accent); font-size: 14px; text-align: center; text-decoration: none; }
.muted { color: var(--text-muted); }
.count { font-size: 12px; color: var(--text-muted); background: var(--surface-2); border-radius: 10px; padding: 1px 8px; font-variant-numeric: tabular-nums; flex: none; }

.boot, .empty { min-height: 60dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 32px; text-align: center; color: var(--text-muted); }
.boot .btn, .empty .btn { width: auto; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- inicio de sesión ---------- */
.login { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: calc(24px + var(--safe-t)) 28px calc(24px + var(--safe-b)); gap: 6px; }
.login-logo { border-radius: 18px; margin-bottom: 10px; }
.login h1 { font-size: 26px; }
.login-sub { color: var(--text-muted); margin-bottom: 18px; }
.login-form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login-form input, .custom-range input, .searchbar input {
  width: 100%; min-height: 48px; padding: 0 14px; font-size: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; outline-offset: 0;
}
.login-error { min-height: 20px; color: var(--danger); font-size: 14px; text-align: center; }

/* ---------- inicio ---------- */
.searchbar { position: relative; padding: 10px calc(12px + var(--safe-r)) 4px calc(12px + var(--safe-l)); max-width: 760px; margin: 0 auto; }
.searchbar .icon { position: absolute; left: calc(26px + var(--safe-l)); top: 23px; color: var(--text-muted); pointer-events: none; }
.searchbar input { padding-left: 44px; -webkit-appearance: none; appearance: none; }
.searchbar.in-sheet { padding: 0 0 8px; }
.searchbar.in-sheet .icon { left: 14px; top: 13px; }
.home-list { max-width: 760px; margin: 0 auto; padding: 4px calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l)); }
.group { margin-top: 10px; }
.group-head { width: 100%; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 4px; color: var(--text-muted); font-size: 13px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; }
.group-head .icon { width: 17px; height: 17px; }
.group-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { transition: transform 0.18s ease; width: 18px; height: 18px; }
.collapsed > .chev, .collapsed .section-head .chev, .group-head.collapsed .chev { transform: rotate(-90deg); }
.group-body { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.dash-row { display: flex; align-items: center; min-height: 52px; padding: 6px 4px 6px 14px; color: var(--text); text-decoration: none; border-top: 1px solid var(--border); }
.dash-row:first-child { border-top: 0; }
.dash-row:active { background: var(--surface-2); }
.dash-row-text { flex: 1; min-width: 0; }
.dash-row-title { display: block; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-row small { color: var(--text-muted); font-size: 12px; }

/* ---------- controles del dashboard ---------- */
.controls {
  position: sticky; top: calc(var(--bar-h) + var(--safe-t)); z-index: 19;
  display: flex; gap: 8px; align-items: center;
  padding: 8px calc(12px + var(--safe-r)) 8px calc(12px + var(--safe-l));
  overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
  background: var(--bg);
}
.controls::-webkit-scrollbar { display: none; }
.controls:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 13px; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); white-space: nowrap; flex: none; font-size: 14px; max-width: 260px; }
.chip > span:not(.icon) { overflow: hidden; text-overflow: ellipsis; }
.chip .icon { width: 17px; height: 17px; color: var(--text-muted); }
.chip.strong { font-weight: 600; }
.chip.icon-only { width: 36px; padding: 0; justify-content: center; }
.chip.var small { color: var(--text-muted); font-size: 12px; }
.chip:active { background: var(--surface-2); }
.chip.spin .icon { animation: spin 0.7s ease; }

/* ---------- secciones y paneles ---------- */
.dash { max-width: 1180px; margin: 0 auto; padding: 0 calc(10px + var(--safe-r)) 0 calc(10px + var(--safe-l)); }
.section { scroll-margin-top: calc(var(--bar-h) + var(--safe-t) + 54px); }
.section-head { width: 100%; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 6px 4px 2px; font-weight: 650; font-size: 15px; }
.section-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.section.collapsed .grid { display: none; }
.section.collapsed .section-head { color: var(--text-muted); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding-bottom: 8px; }
.panel { grid-column: span var(--s2, 2); position: relative; min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 13px; overflow: hidden; display: flex; flex-direction: column; }
@media (min-width: 720px) {
  .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
  .panel { grid-column: span var(--s4, 4); }
}
.panel.plain { background: transparent; border-color: transparent; }
.panel-head { display: flex; align-items: center; gap: 6px; padding: 9px 10px 2px 12px; min-height: 34px; }
.panel-title { flex: 1; min-width: 0; font-size: 13px; font-weight: 600; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel.compact .panel-title { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.25; }
.panel-badge { font-size: 11px; color: var(--accent); flex: none; }
.panel-badge:empty { display: none; }
.panel-open { width: 15px; height: 15px; color: var(--text-muted); opacity: 0.55; }
.panel.compact .panel-open { display: none; }
.panel.untitled.type-text .panel-head { display: none; }
.panel-body { position: relative; padding: 2px 12px 11px; min-width: 0; flex: 1; }
.panel-body.type-timeseries { padding: 2px 4px 8px; }
.panel-body.type-table { padding: 2px 0 4px; }
.panel.loading::after { content: ""; position: absolute; left: 0; top: 0; height: 2px; width: 40%; background: var(--accent); animation: slide 1.1s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(260%); } }
.skeleton { height: 46px; border-radius: 8px; background: linear-gradient(90deg, var(--surface-2) 25%, transparent 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; opacity: 0.6; }
.type-timeseries .skeleton { height: 190px; margin: 0 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }
.panel-empty { color: var(--text-muted); font-size: 14px; padding: 8px 0 4px; }
.type-timeseries .panel-empty, .type-table .panel-empty { padding: 20px 12px; text-align: center; }
.panel-error { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; color: var(--danger); font-size: 13px; padding: 6px 8px; overflow-wrap: anywhere; }
.panel-error .icon { width: 18px; height: 18px; }
.panel-error .link-btn { width: auto; padding: 4px 8px; }
.panel-note { font-size: 12px; color: var(--text-muted); padding: 6px 12px 0; overflow-wrap: anywhere; }
.panel-desc { font-size: 14px; color: var(--text-muted); padding: 12px 12px 4px; white-space: pre-line; }
.more { font-size: 12px; color: var(--text-muted); padding-top: 4px; }

/* stat */
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat-item { position: relative; min-width: 0; }
.stat-value { font-weight: 650; line-height: 1.12; letter-spacing: -0.01em; color: var(--val, var(--text)); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat-value.xl { font-size: 31px; }
.stat-value.lg { font-size: 26px; }
.stat-value.md { font-size: 21px; }
.stat-value.sm { font-size: 17px; }
.stat-value.xs { font-size: 14px; line-height: 1.3; font-weight: 600; }
.stat-value .unit { font-size: 0.6em; font-weight: 550; opacity: 0.85; letter-spacing: 0; }
.stat-name { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat.multi .stat-item { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; padding: 3px 0; border-top: 1px solid var(--border); }
.stat.multi .stat-item:first-child { border-top: 0; }
.stat.multi .stat-name { font-size: 13px; flex: 1; }
.stat.multi .stat-value { flex: none; max-width: 65%; text-align: right; }
.stat-item.bg { background: color-mix(in srgb, var(--tile, var(--surface-2)) 30%, transparent); border-radius: 9px; padding: 8px 10px; }
.stat.multi .stat-item.bg { border-top: 0; margin-top: 2px; }
.stat-item.with-spark { padding-bottom: 30px; }
.spark { position: absolute; left: -12px; right: -12px; bottom: -11px; width: calc(100% + 24px); height: 34px; display: block; pointer-events: none; }

/* gauge */
.gauges { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 10px; }
.gauge-box { flex: 1 1 110px; max-width: 190px; text-align: center; min-width: 0; }
.gauge { width: 100%; display: block; }
.gauge-track { stroke: var(--surface-2); }
.gauge-value { font-weight: 650; font-variant-numeric: tabular-nums; }
.gauge-value.xl { font-size: 23px; }
.gauge-value.lg { font-size: 19px; }
.gauge-value.md { font-size: 15px; }
.gauge-value.sm { font-size: 11.500px; }
.gauge-value.xs { font-size: 9px; }
.gauge-name { font-size: 12px; color: var(--text-muted); margin-top: -6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* barras */
.bars { display: flex; flex-direction: column; gap: 9px; padding-top: 2px; }
.bar-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; font-size: 13px; }
.bar-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-value { flex: none; font-weight: 600; font-variant-numeric: tabular-nums; }
.bar-track { display: flex; height: 8px; margin-top: 4px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar-track.grouped { flex-direction: column; height: auto; gap: 2px; background: none; }
.bar-track.grouped .bar-fill { height: 6px; border-radius: 3px; }
.bar-fill { height: 100%; min-width: 0; }

/* tabla */
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tbl { border-collapse: collapse; min-width: 100%; font-size: 13px; }
.tbl th, .tbl td { padding: 8px 10px; text-align: left; white-space: nowrap; max-width: 260px; overflow: hidden; text-overflow: ellipsis; border-top: 1px solid var(--border); }
.tbl th { border-top: 0; font-size: 12px; font-weight: 600; color: var(--text-muted); position: relative; }
.tbl th.sorted { color: var(--text); }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; }
.tbl th:first-child, .tbl td:first-child { position: sticky; left: 0; background: var(--surface); padding-left: 12px; z-index: 1; max-width: 46vw; }
.tbl td:first-child { font-weight: 550; }
.tbl td.cell-bg { font-weight: 600; }

/* texto */
.md { font-size: 14px; color: var(--text); overflow-wrap: anywhere; }
.md p { margin: 0 0 8px; }
.md p:last-child { margin-bottom: 0; }
.md h3 { font-size: 16px; margin: 4px 0 6px; }
.md h4 { font-size: 14px; margin: 4px 0 6px; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 20px; }
.md code { font: 12.500px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* gráficas */
.ts-chart { min-height: 120px; }
.uplot { font-family: inherit; }
.u-over { touch-action: pan-y; }
.u-cursor-x { border-right: 1px dashed var(--text-muted) !important; }
.u-cursor-pt { border-width: 2px !important; }
.ts-time { position: absolute; right: 10px; top: 4px; z-index: 3; font-size: 11px; font-variant-numeric: tabular-nums; background: var(--surface-2); color: var(--text); padding: 2px 7px; border-radius: 7px; pointer-events: none; }
.ts-time:empty { display: none; }
.legend { padding: 2px 8px 0; }
.legend-item { display: flex; align-items: center; gap: 8px; width: 100%; min-height: 28px; font-size: 13px; }
.legend-item i { width: 12px; height: 4px; border-radius: 2px; flex: none; }
.legend-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-val { flex: none; font-variant-numeric: tabular-nums; color: var(--text-muted); min-width: 64px; text-align: right; }
.legend.scrub .legend-val { color: var(--text); font-weight: 600; }
.legend-item.off { opacity: 0.38; }
.legend-item.head { font-size: 11px; color: var(--text-muted); min-height: 20px; }
.legend.static { display: flex; flex-wrap: wrap; gap: 2px 14px; padding: 8px 0 0; }
.legend.static .legend-item { width: auto; }
.legend.detail .legend-item { min-height: 34px; border-top: 1px solid var(--border); }

/* detalle */
.detail-body { max-width: 1180px; margin: 0 auto; padding: 10px calc(10px + var(--safe-r)) 0 calc(10px + var(--safe-l)); display: flex; flex-direction: column; gap: 10px; }
.detail .panel { grid-column: auto; }
.detail .panel-title { font-size: 14px; }
.detail .stat-value.xl { font-size: 44px; }
.detail .stat-value.lg { font-size: 36px; }
.detail .gauge-box { max-width: 260px; }
.detail .stat-item.with-spark { padding-bottom: 46px; }
.detail .spark { bottom: 0; height: 44px; }
.detail .tbl th, .detail .tbl td { max-width: 70vw; }

/* hojas inferiores */
.sheet-backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, 0); transition: background 0.2s ease; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop.open { background: rgba(0, 0, 0, 0.5); }
.sheet { width: 100%; max-width: 560px; max-height: 84dvh; display: flex; flex-direction: column; background: var(--surface); border-radius: 18px 18px 0 0; border: 1px solid var(--border); border-bottom: 0; transform: translateY(100%); transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1); padding-bottom: var(--safe-b); }
.sheet-backdrop.open .sheet { transform: none; }
.sheet-grip { width: 38px; height: 4px; border-radius: 2px; background: var(--surface-2); margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; padding: 4px 6px 4px 18px; }
.sheet-head h2 { flex: 1; font-size: 17px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 14px 16px; overscroll-behavior: contain; }
.sheet-body h3 { font-size: 13px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 16px 4px 6px; }
.sheet-note { font-size: 13px; color: var(--text-muted); padding: 0 4px 8px; }
.sheet-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.sheet-actions.row { flex-direction: row; }
.option-list { display: flex; flex-direction: column; }
.option { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 4px 8px; border-radius: 10px; width: 100%; }
.option:active { background: var(--surface-2); }
.option.on { color: var(--accent); font-weight: 600; }
.option-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.option-text small { display: block; font-size: 12px; color: var(--text-muted); font-weight: 400; }
.custom-range { display: flex; flex-direction: column; gap: 10px; }
.custom-range label { display: flex; align-items: center; gap: 12px; }
.custom-range label span { width: 52px; color: var(--text-muted); font-size: 14px; }
.account { display: flex; flex-direction: column; padding: 4px; }
.account small { color: var(--text-muted); }
.version { font-size: 12px; color: var(--text-muted); text-align: center; margin-top: 14px; }

.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 20px); z-index: 60; background: var(--surface-2); color: var(--text); padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; transition: all 0.2s ease; max-width: 86vw; border: 1px solid var(--border); }
.toast.open { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
