:root {
  color-scheme: dark;
  --bg: #111110;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #2d2d2a;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #8f8e86;
  --hr: #e66767;
  --rr: #3987e5;
  --ecg: #2fd39a;
  --warn: #fab219;
  --good: #0ca30c;
  --accent: #3987e5;
  --radius: 10px;
  --gap: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  min-height: 100dvh;
  max-width: 1680px;
  margin: 0 auto;
  padding: var(--gap);
  padding-left: max(var(--gap), env(safe-area-inset-left));
  padding-right: max(var(--gap), env(safe-area-inset-right));
}

main {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  flex: 1;
  min-width: 0;
}

h2 { margin: 0; font-size: 13px; font-weight: 600; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.brand-mark { width: 18px; height: 18px; fill: var(--hr); }

.status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status[data-state="connected"] .status-dot { background: var(--good); box-shadow: 0 0 0 3px rgb(12 163 12 / 0.2); }
.status[data-state="connecting"] .status-dot,
.status[data-state="reconnecting"] .status-dot { background: var(--warn); animation: blink 1s ease-in-out infinite; }

@keyframes blink { 50% { opacity: 0.3; } }

.device-meta { display: flex; align-items: center; gap: 12px; color: var(--muted); min-width: 0; }
.battery { display: inline-flex; align-items: center; gap: 5px; color: var(--text-2); }
.battery svg { width: 22px; height: 12px; }
.battery.low { color: var(--warn); }

.actions { display: flex; gap: 8px; margin-left: auto; align-items: center; }

/* ---------------------------------------------------------------- buttons */
.btn {
  appearance: none;
  font: inherit;
  font-weight: 550;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
  list-style: none;
}
.btn::-webkit-details-marker { display: none; }
.btn:hover { border-color: #45453f; background: #2a2a27; }
.btn:focus-visible, .seg button:focus-visible, .menu-items button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #4a93ea; border-color: #4a93ea; }
.btn.primary.is-connected { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.btn.ghost { background: transparent; color: var(--text-2); }

.btn.toggle { color: var(--muted); padding-left: 26px; position: relative; }
.btn.toggle::before {
  content: "";
  position: absolute;
  left: 10px;
  top: 50%;
  width: 8px;
  height: 8px;
  margin-top: -4px;
  border-radius: 50%;
  border: 1.5px solid var(--muted);
}
.btn.toggle[aria-pressed="true"] { color: var(--text); }
.btn.toggle[aria-pressed="true"]::before { background: var(--ecg); border-color: var(--ecg); }
#pauseBtn[aria-pressed="true"]::before { background: var(--warn); border-color: var(--warn); }

.seg {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}
.seg button {
  appearance: none;
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 6px;
  padding: 4px 9px;
  cursor: pointer;
  white-space: nowrap;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: #3a3a36; color: var(--text); }

.menu { position: relative; }
.menu-items {
  position: absolute;
  right: 0;
  bottom: calc(100% + 6px);
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 210px;
  padding: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.5);
}
.menu-items button {
  appearance: none;
  font: inherit;
  text-align: left;
  color: var(--text);
  background: none;
  border: 0;
  border-radius: 6px;
  padding: 8px 10px;
  cursor: pointer;
}
.menu-items button:hover { background: #33332f; }

/* ---------------------------------------------------------------- banners */
.banner {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-2);
}
.banner.warn { border-left-color: var(--warn); }
.banner b { color: var(--text); }
.banner a { color: #7fb2f0; }

/* ------------------------------------------------------------------ tiles */
.tiles {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) 4fr;
  gap: var(--gap);
}

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  min-width: 0;
}

.tile-label, .section-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tile-value { font-size: 28px; font-weight: 650; line-height: 1.15; margin-top: 2px; white-space: nowrap; }
.tile-value small { font-size: 12px; font-weight: 500; color: var(--muted); margin-left: 5px; }
.tile-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-sub.alert { color: var(--warn); }

.tile.hero { display: flex; flex-direction: column; justify-content: center; }
.tile.hero .tile-value { font-size: 54px; line-height: 1.05; }
.tile.hero .tile-value small { font-size: 15px; }

.heart { width: 14px; height: 14px; fill: var(--hr); transform-origin: center; }
.heart.beating { animation: beat 1s ease-in-out infinite; }
@keyframes beat { 0%, 30%, 100% { transform: scale(1); } 12% { transform: scale(1.35); } }
@media (prefers-reduced-motion: reduce) { .heart.beating, .status-dot { animation: none !important; } }

.hrv { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.hrv-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 30px; }
.hrv-note { color: var(--muted); font-size: 12px; margin-right: auto; }
.hrv-tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--gap); flex: 1; }

/* ------------------------------------------------------------------ cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px 8px;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px 14px; flex-wrap: wrap; margin-bottom: 6px; }
.card-head.sub { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.card-title { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.card-hint { color: var(--muted); font-size: 12px; }
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.chart { position: relative; min-width: 0; }
/* Absolutely positioned so the bitmap size can never feed back into the layout. */
.chart canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: pan-y; }
.ecg-chart { height: clamp(200px, 27vh, 340px); }
.ecg-chart canvas.is-paused { cursor: grab; touch-action: none; }
.trend-chart { height: clamp(110px, 14.5vh, 190px); }
.poincare-chart { flex: 1; min-height: 240px; }

.chart-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0 20px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
}
.chart-badge {
  position: absolute;
  top: 8px;
  right: 14px;
  padding: 2px 9px;
  border-radius: 999px;
  background: rgb(250 178 25 / 0.14);
  color: var(--warn);
  font-size: 12px;
  pointer-events: none;
}

.tip {
  position: absolute;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 6px 9px;
  background: #2b2b28;
  border: 1px solid #45453f;
  border-radius: 7px;
  box-shadow: 0 6px 18px rgb(0 0 0 / 0.45);
  white-space: nowrap;
  pointer-events: none;
  font-size: 12px;
}
.tip[hidden] { display: none; }
.tip b { font-size: 13px; }
.tip span { color: var(--muted); }
.tip em { color: var(--warn); font-style: normal; }

.legend { display: flex; gap: 14px; color: var(--muted); font-size: 12px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key-line { width: 14px; height: 2px; border-radius: 1px; background: var(--rr); }
.key-diamond { width: 7px; height: 7px; border: 1.5px solid var(--warn); transform: rotate(45deg); }

.split { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: var(--gap); }

.facts { display: flex; gap: 22px; margin: 6px 0 2px; flex-wrap: wrap; }
.facts div { display: flex; align-items: baseline; gap: 6px; }
.facts dt { color: var(--muted); font-size: 12px; }
.facts dd { margin: 0; font-weight: 600; }

/* ------------------------------------------------------------ session bar */
.sessionbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.session-facts { color: var(--text-2); }
.session-facts span + span::before { content: "·"; margin: 0 8px; color: var(--muted); }
.session-facts .alert { color: var(--warn); }
.disclaimer { color: var(--muted); font-size: 11px; }
.disclaimer b { color: var(--text-2); font-weight: 600; }

/* ------------------------------------------------------------- explanations */
.about { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.about summary { padding: 9px 12px; cursor: pointer; font-weight: 600; color: var(--text-2); }
.about summary:hover { color: var(--text); }
.about summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-radius: var(--radius); }
.about-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 28px; padding: 2px 14px 12px; color: var(--text-2); }
.about h3 { margin: 6px 0 4px; font-size: 13px; color: var(--text); }
.about p { margin: 0 0 6px; max-width: 62ch; }
.about ul { margin: 0 0 6px; padding-left: 18px; }
.about li { margin-bottom: 3px; }
.about b { color: var(--text); }
.log-section { padding: 0 14px 12px; color: var(--text-2); }
.log { margin: 0; padding: 8px 10px; max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text-2); font: 12px/1.5 ui-monospace, Consolas, monospace; white-space: pre-wrap; user-select: text; -webkit-user-select: text; }

#report { display: none; }

/* Embedded demo (?embed): a showcase without connection and export controls. */
.embed-only { display: none; text-decoration: none; }
.embed .embed-only { display: inline-block; }
.embed #demoBtn, .embed #connectBtn, .embed #ecgBtn, .embed #resetBtn, .embed #exportMenu, .embed #notice, .embed .about { display: none; }
.embed #app { min-height: 0; }
.embed body { scrollbar-width: thin; }
.embed .device-meta { display: none; }
@media (max-width: 900px) {
  /* On a phone the frame only shows the top of the page: lead with the moving ECG. */
  .embed #ecgCard { order: -1; }
}
@media (min-width: 901px) {
  /* Fit the whole dashboard into the frame of the host page without an inner scrollbar. */
  .embed .ecg-chart { height: clamp(170px, 22.5vh, 340px); }
  .embed .trend-chart { height: clamp(90px, 12.5vh, 190px); }
}

/* ------------------------------------------------------------ small screens */
@media (max-width: 900px) {
  .tiles { grid-template-columns: 1fr; }
  .tile.hero { flex-direction: row; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 4px 14px; }
  .hrv-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .split { grid-template-columns: 1fr; }
  .about-grid { grid-template-columns: 1fr; }
  .poincare-chart { height: min(86vw, 360px); flex: none; }
  .device-meta { order: 5; width: 100%; }
  .brand span { display: none; }
}

@media (max-width: 520px) {
  .tile-value { font-size: 24px; }
  .tile.hero .tile-value { font-size: 46px; }
  .card { padding: 8px 8px 6px; }
  .btn, .seg button { padding-top: 7px; padding-bottom: 7px; }
  .ecg-card-hint { display: none; }
}

/* ------------------------------------------------------------------ print */
@page { size: A4 landscape; margin: 10mm; }

@media print {
  html, body { background: #fff; color: #0b0b0b; }
  #app { display: none; }
  #report { display: block; font: 10pt/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
}

#report h1 { font-size: 16pt; margin: 0 0 1mm; }
#report h2 { font-size: 10.5pt; margin: 5mm 0 1.5mm; }
#report .meta { color: #52514e; margin: 0 0 3mm; }
#report .report-stats { display: grid; grid-template-columns: repeat(6, 1fr); gap: 2mm 6mm; margin: 0; }
#report .report-stats div { border-top: 0.3mm solid #0b0b0b; padding-top: 1mm; }
#report .report-stats dt { color: #52514e; font-size: 8pt; }
#report .report-stats dd { margin: 0; font-size: 12pt; font-weight: 650; }
#report .report-charts { display: grid; grid-template-columns: 2fr 1fr; gap: 6mm; align-items: start; }
#report canvas { display: block; width: 100%; height: auto; }
#report .strip { width: 250mm; height: 30mm; margin: 0 0 2mm; }
#report .page { break-before: page; }
#report .note { color: #52514e; font-size: 8pt; margin-top: 3mm; }
