/* Presión: identidad "calma clínica" */
:root {
  color-scheme: light;

  /* Superficies y tinta */
  --page: #EEF3F6;
  --surface: #FFFFFF;
  --ink: #102A35;
  --ink-2: #4B6470;
  --muted: #5F7581;
  --line: #DCE5EA;
  --grid: #E6ECEF;

  /* Marca */
  --brand: #0B6E80;
  --brand-press: #08586A;
  --brand-soft: #E0EFF2;
  --heart: #E0445E;
  --danger: #B42318;

  /* Series del gráfico (validadas: azul y magenta, ver dataviz) */
  --sys: #2A78D6;
  --dia: #E87BA4;

  /* Estados de presión (marcas) */
  --s0: #0CA30C;
  --s1: #FAB219;
  --s2: #EC835A;
  --s3: #D03B3B;
  --s4: #8F1D1D;

  --font: "Lexend", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --fs-small: 17px;
  --fs-body: 19px;
  --fs-title: 24px;
  --fs-row: 32px;
  --fs-hero: clamp(64px, 19vw, 96px);

  --gap: 18px;
  --r-hero: 28px;
  --r-card: 22px;
  --r-control: 16px;
  --tabbar-h: 78px;
}

*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--fs-body);
  line-height: 1.45;
}

button, input { font: inherit; color: inherit; }

h1, h2, h3, p { margin: 0; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 3px;
  border-radius: 8px;
}

abbr { text-decoration: none; }

.top, main {
  width: 100%;
  max-width: 600px;
  margin-inline: auto;
}

/* Encabezado con marca */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(16px + env(safe-area-inset-top)) var(--gap) 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  flex: none;
  border-radius: 14px;
}

.top h1 {
  font-size: var(--fs-title);
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 15px;
  color: var(--muted);
}

main {
  display: flex;
  flex-direction: column;
  gap: 26px;
  padding: 0 var(--gap) calc(var(--tabbar-h) + 40px + env(safe-area-inset-bottom));
}

/* Tarjetas */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  padding: 20px;
}

.card-title, .section-title {
  font-size: var(--fs-body);
  font-weight: 600;
}

.section-title { font-size: 21px; font-weight: 800; }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

/* Última medición */
.hero {
  border-radius: var(--r-hero);
  padding: 22px 22px 20px;
  border-color: transparent;
  box-shadow: 0 1px 2px rgba(16, 42, 53, 0.06), 0 8px 24px rgba(16, 42, 53, 0.06);
}

.hero-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.hero-head .card-title { color: var(--ink-2); }

.hero-when {
  margin-top: 2px;
  color: var(--muted);
  font-size: var(--fs-small);
}

.hero-reading {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  column-gap: 10px;
  margin: 14px 0 6px;
}

.reading-big {
  font-size: var(--fs-hero);
  font-weight: 800;
  line-height: 0.95;
  letter-spacing: -0.035em;
  white-space: nowrap;
}

.reading-big .slash,
.row-reading .slash {
  font-weight: 400;
  color: var(--muted);
  margin: 0 0.03em;
}

.unit {
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--muted);
}

.hero-pulse {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 21px;
  font-weight: 600;
}

.hero-pulse small {
  font-size: var(--fs-small);
  font-weight: 400;
  color: var(--muted);
}

.heart { color: var(--heart); flex: none; }

/* Escala de categorías: 5 tramos con 2px de separación */
.scale {
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  margin-top: 26px;
  height: 12px;
}

.scale span {
  border-radius: 2px;
  opacity: 0.28;
}

.scale span:first-child { border-radius: 6px 2px 2px 6px; }
.scale span:nth-child(5) { border-radius: 2px 6px 6px 2px; }
.scale span.on { opacity: 1; }

.scale span[data-level="0"] { background: var(--s0); }
.scale span[data-level="1"] { background: var(--s1); }
.scale span[data-level="2"] { background: var(--s2); }
.scale span[data-level="3"] { background: var(--s3); }
.scale span[data-level="4"] { background: var(--s4); }

.scale-marker {
  position: absolute;
  top: -12px;
  width: 0;
  height: 0;
  border: 7px solid transparent;
  border-top: 9px solid var(--ink);
  border-bottom: 0;
  transform: translateX(-50%);
}

.scale-ends {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 15px;
  color: var(--muted);
}

/* Chips de categoría: siempre ícono + texto */
.chip {
  --c-bg: var(--grid);
  --c-fg: var(--ink-2);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px 5px 9px;
  border-radius: 999px;
  background: var(--c-bg);
  color: var(--c-fg);
  font-size: var(--fs-small);
  font-weight: 600;
  white-space: nowrap;
}

.chip:empty { display: none; }
.chip svg { flex: none; }

.chip-sm {
  padding: 3px 10px 3px 7px;
  font-size: 15px;
}

.chip[data-level="0"] { --c-bg: #E2F4E2; --c-fg: #1A6B1F; }
.chip[data-level="1"] { --c-bg: #FFF2CC; --c-fg: #7A5200; }
.chip[data-level="2"] { --c-bg: #FDE7DC; --c-fg: #9B3B12; }
.chip[data-level="3"] { --c-bg: #FBE1E0; --c-fg: #A52020; }
.chip[data-level="4"] { --c-bg: #8F1D1D; --c-fg: #FFFFFF; }

/* Tendencia */
.segmented {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: #DDE6EB;
}

.segmented button {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--ink-2);
  font-size: var(--fs-small);
  font-weight: 600;
  cursor: pointer;
}

.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(16, 42, 53, 0.14);
}

.stats {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px 20px;
  align-items: start;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
}

.stat-label {
  font-size: var(--fs-small);
  color: var(--muted);
}

.stat-value {
  font-size: 30px;
  font-weight: 800;
  line-height: 1.1;
}

.legend {
  display: flex;
  gap: 20px;
  margin: 20px 0 4px;
  font-size: var(--fs-small);
  color: var(--ink-2);
}

.key {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.key::before {
  content: "";
  width: 18px;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
}

.key-sys::before { background: var(--sys); }
.key-dia::before { background: var(--dia); }

.chart {
  position: relative;
  width: 100%;
  touch-action: pan-y;
}

.chart svg { display: block; overflow: visible; }

.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--line); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.chart .end-label { fill: var(--ink); font-size: 15px; font-weight: 600; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.sys { stroke: var(--sys); }
.chart .line.dia { stroke: var(--dia); }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .dot.sys { fill: var(--sys); }
.chart .dot.dia { fill: var(--dia); }
.chart .crosshair { stroke: var(--ink-2); stroke-width: 1; }
.chart .hit { fill: transparent; cursor: crosshair; }
.chart .hit:focus { outline: none; }
.chart .hit:focus-visible + .focus-ring { stroke: var(--brand); }
.chart .focus-ring { fill: none; stroke: transparent; stroke-width: 2; }

.tooltip {
  position: absolute;
  top: 0;
  z-index: 2;
  min-width: 150px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 6px 18px rgba(16, 42, 53, 0.16);
  font-size: 15px;
  pointer-events: none;
}

.tooltip-title { color: var(--ink-2); margin-bottom: 4px; }

.tooltip-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tooltip-row::before {
  content: "";
  width: 12px;
  height: 3px;
  border-radius: 2px;
  background: var(--k);
}

.tooltip-row strong { font-size: 18px; }
.tooltip-row span { color: var(--ink-2); }
.tooltip-note { margin-top: 4px; color: var(--muted); }

.trend-empty {
  color: var(--ink-2);
  margin-top: 6px;
}

/* Historial */
.top-actions { display: flex; gap: 8px; flex: none; }

.btn-icon { width: 48px; padding: 0; }
.btn-refresh:disabled { opacity: 0.6; cursor: progress; }
.btn-refresh:disabled svg { animation: spin 0.9s linear infinite; }

@media (prefers-reduced-motion: reduce) {
  .btn-refresh:disabled svg { animation: none; }
}

.history h3:first-child { padding-top: 0; }

.history {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history h3 {
  padding: 14px 4px 2px;
  font-size: var(--fs-small);
  font-weight: 600;
  color: var(--ink-2);
}

.history ul {
  list-style: none;
  margin: 0;
  padding: 0 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
}

.history li + li .row { border-top: 1px solid var(--grid); }

.row {
  display: grid;
  grid-template-columns: 3.4em 1fr auto;
  grid-template-areas:
    "time reading pulse"
    ".    meta    meta";
  align-items: center;
  column-gap: 12px;
  width: 100%;
  min-height: 76px;
  padding: 14px 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.row:active { background: var(--brand-soft); }

.row-time {
  grid-area: time;
  font-size: var(--fs-small);
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}

.row-time.no-time { font-size: 15px; line-height: 1.2; color: var(--muted); }

.row-reading {
  grid-area: reading;
  font-size: var(--fs-row);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.row-pulse {
  grid-area: pulse;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-body);
  font-weight: 600;
}

.row-meta {
  grid-area: meta;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  margin-top: 8px;
  font-size: var(--fs-small);
}

.row-comment { color: var(--ink-2); }

.loading, .load-error, .empty {
  padding: 30px 4px 0;
  color: var(--ink-2);
}

.load-error { color: var(--danger); }

.empty {
  padding: 28px 22px;
  background: var(--surface);
  border-radius: var(--r-card);
  border: 1px dashed #B9C8D0;
}

.empty p:first-child { font-weight: 800; font-size: 21px; color: var(--ink); }
.empty p + p { margin-top: 6px; }

.mock-note {
  padding: 12px 16px;
  border-radius: var(--r-control);
  background: var(--brand-soft);
  color: var(--brand-press);
  font-size: var(--fs-small);
}

.foot {
  padding: 0 4px;
  font-size: 15px;
  color: var(--muted);
  max-width: 36em;
}

/* Botones */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  min-height: 62px;
  padding: 0 24px;
  border: 0;
  border-radius: var(--r-control);
  background: var(--brand);
  color: #fff;
  font-size: 21px;
  font-weight: 800;
  cursor: pointer;
}

.btn-primary:active { background: var(--brand-press); }
.btn-primary:disabled { opacity: 0.55; cursor: progress; }

.btn-quiet {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--surface);
  color: var(--brand-press);
  font-weight: 600;
  cursor: pointer;
  flex: none;
}

.btn-link {
  display: block;
  margin-top: 10px;
  min-height: 48px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--brand);
  font-size: var(--fs-small);
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.btn-danger-quiet {
  width: 100%;
  min-height: 56px;
  border: 0;
  background: none;
  color: var(--danger);
  font-weight: 600;
  cursor: pointer;
}

.btn-danger {
  min-height: 56px;
  padding: 0 22px;
  border: 0;
  border-radius: var(--r-control);
  background: var(--danger);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

/* Barra inferior: Resumen | Agregar | Historial */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 5;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -6px 20px rgba(16, 42, 53, 0.06);
}

.tabbar-inner {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: stretch;
  max-width: 600px;
  height: var(--tabbar-h);
  margin-inline: auto;
  padding: 0 12px;
}

.tab, .tab-add {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 10px;
  gap: 3px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.tab {
  height: 100%;
  border-radius: 14px;
}

.tab[aria-current="page"] { color: var(--brand); }

.tab[aria-current="page"] svg {
  padding: 2px 16px;
  border-radius: 999px;
  background: var(--brand-soft);
  box-sizing: content-box;
}

.tab:not([aria-current="page"]) svg { padding: 2px 16px; box-sizing: content-box; }

.tab-add {
  padding-inline: 14px;
  color: var(--ink);
}

.tab-add-circle {
  display: grid;
  place-items: center;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  border: 5px solid var(--surface);
  box-shadow: 0 8px 20px rgba(11, 110, 128, 0.38);
  box-sizing: content-box;
  margin-top: -40px;
}

.tab-add:active .tab-add-circle { background: var(--brand-press); }

/* Menú de agregar */
.add-menu-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 10px var(--gap) calc(22px + env(safe-area-inset-bottom));
}

.add-menu-body::before {
  content: "";
  align-self: center;
  width: 44px;
  height: 5px;
  border-radius: 3px;
  background: var(--line);
}

.add-option {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  min-height: 78px;
  padding: 12px 18px;
  border: 1px solid var(--line);
  border-radius: var(--r-control);
  background: var(--surface);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.add-option svg {
  flex: none;
  padding: 10px;
  border-radius: 14px;
  background: var(--brand-soft);
  color: var(--brand);
  box-sizing: content-box;
}

.add-option span { display: flex; flex-direction: column; gap: 2px; }
.add-option strong { font-size: 20px; font-weight: 800; }
.add-option small { font-size: var(--fs-small); color: var(--muted); }

.add-option-main {
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}

.add-option-main svg { background: rgba(255, 255, 255, 0.18); color: #fff; }
.add-option-main small { color: rgba(255, 255, 255, 0.85); }
.add-option:active { background: var(--brand-soft); }
.add-option-main:active { background: var(--brand-press); }

/* Hoja de carga / edición */
.sheet {
  width: 100%;
  max-width: 600px;
  max-height: 94dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: var(--r-hero) var(--r-hero) 0 0;
  background: var(--surface);
  color: var(--ink);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sheet::backdrop { background: rgba(16, 42, 53, 0.5); }

.sheet[open] { animation: rise 260ms cubic-bezier(0.2, 0.8, 0.2, 1); }

@keyframes rise {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.sheet form {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 10px var(--gap) calc(24px + env(safe-area-inset-bottom));
}

.sheet form::before {
  content: "";
  align-self: center;
  width: 44px;
  height: 5px;
  border-radius: 3px;
  background: var(--line);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sheet h2 {
  font-size: var(--fs-title);
  font-weight: 800;
}

.btn-close {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--page);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.photo {
  margin: 0;
  border-radius: var(--r-control);
  overflow: hidden;
  background: var(--page);
}

.photo img {
  display: block;
  width: 100%;
  max-height: 32dvh;
  object-fit: contain;
}

.read-status {
  padding: 12px 14px;
  border-radius: var(--r-control);
  background: var(--brand-soft);
  color: var(--brand-press);
  font-size: var(--fs-small);
  font-weight: 600;
}

.read-status:empty { display: none; }

.read-status.busy::before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 10px;
  vertical-align: -2px;
  border: 3px solid rgba(11, 110, 128, 0.25);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.read-status.warn { background: #FBE1E0; color: #A52020; }

.btn-retry {
  align-self: flex-start;
  margin-top: -6px;
  min-height: 52px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.readings {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 0;
  padding: 0;
  border: 0;
}

.num-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.num-label {
  font-size: var(--fs-small);
  font-weight: 600;
}

.num-label abbr {
  display: block;
  color: var(--muted);
  font-weight: 400;
  font-size: 15px;
}

.num-field input {
  width: 100%;
  min-height: 76px;
  padding: 0 6px;
  border: 2px solid var(--line);
  border-radius: var(--r-control);
  background: var(--page);
  font-size: 38px;
  font-weight: 800;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.live-cat {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: -6px;
  font-size: var(--fs-small);
  color: var(--ink-2);
}

.live-cat:empty { display: none; }

.when {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: var(--fs-small);
  font-weight: 600;
}

.field input {
  width: 100%;
  min-width: 0;
  min-height: 58px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: var(--r-control);
  background: var(--page);
  font-size: var(--fs-body);
  font-weight: 400;
}

.num-field input:focus,
.field input:focus {
  border-color: var(--brand);
  background: var(--surface);
  outline: none;
  box-shadow: 0 0 0 4px rgba(11, 110, 128, 0.18);
}

input[aria-invalid="true"] { border-color: var(--danger) !important; }

.hint {
  margin-top: -8px;
  font-size: var(--fs-small);
  color: var(--muted);
}

.hint:empty, .form-error:empty { display: none; }

.form-error {
  color: var(--danger);
  font-weight: 600;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 4px;
}

.confirm {
  padding: 18px;
  border-radius: var(--r-control);
  background: #FBE1E0;
}

.confirm p { font-weight: 600; color: #7A1A1A; }

.confirm-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 14px;
}

.pin form p { color: var(--ink-2); }

/* Aviso */
.toast {
  position: fixed;
  left: 50%;
  z-index: 10;
  bottom: calc(var(--tabbar-h) + 30px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: calc(100% - 2 * var(--gap));
  padding: 14px 22px;
  border-radius: var(--r-control);
  background: var(--ink);
  color: #fff;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms, transform 200ms;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

@media (min-width: 700px) {
  .sheet { margin: auto; border-radius: var(--r-hero); }
  .sheet form::before, .add-menu-body::before { display: none; }
  .top { padding-top: 32px; }
}

@media (max-width: 360px) {
  :root { --fs-row: 28px; --gap: 14px; }
  .brand-sub { display: none; }
  .num-field input { font-size: 32px; }
  .when { grid-template-columns: minmax(0, 1fr); }
  .btn-primary { font-size: 19px; padding: 0 14px; gap: 10px; }
  .card { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet[open] { animation: none; }
  .toast { transition: none; }
  .read-status.busy::before { animation-duration: 3s; }
}
