/*
 * Transparent overlay styling for OBS Browser Source.
 * Background is fully transparent so OBS composites it over video.
 * Targets 1920x1080 but scales to 1280x720 via relative units.
 */

:root {
  --panel-bg: rgba(0, 0, 0, 0.55);
  --panel-stale-bg: rgba(60, 0, 0, 0.55);
  --accent: #ff4d4f;
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.55);
  --radius: 0.6rem;
  /* Overlay renders on a fixed 1920x1080 OBS canvas, then the composited stream
     is downscaled on the remote viewer's device (often a phone in landscape),
     so size for legibility at that smaller end. */
  font-size: clamp(16px, 1.6vw, 26px);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: transparent; /* critical for OBS transparency */
  overflow: hidden;
  font-family: "Inter", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  /* Strong text shadow keeps numerals readable over busy video / Zoom compression. */
  text-shadow: 0 0.1rem 0.4rem rgba(0, 0, 0, 0.9);
}

/*
 * Cards pin to the top corners: with one participant the single card sits at the
 * upper-left; with two, they split to the upper-left and upper-right corners
 * (space-between places a lone item at the start, two items at the ends).
 */
.panels {
  position: absolute;
  top: 2.5vh;
  left: 2.5vw;
  right: 2.5vw;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: stretch; /* both cards share the height of the taller one */
  gap: 1rem;
}

/* The sparkline spans the name + live rows on the right, so the intensity
   bands get the full height of the card's upper half. */
.panel {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "name spark"
    "live spark"
    "session session"
    "resp resp"
    "zwarn zwarn"
    "badge badge";
  column-gap: 1rem;
  row-gap: 0.45rem;
  padding: 0.8rem 1.2rem;
  min-width: 21rem;
  background: var(--panel-bg);
  border-radius: var(--radius);
  border-left: 0.4rem solid var(--accent);
  backdrop-filter: blur(2px);
  transition: opacity 0.3s ease, background 0.3s ease;
}

.panel .name {
  grid-area: name;
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
}

.panel .session {
  grid-area: session;
}

.panel .resp {
  grid-area: resp;
}

.panel .badge {
  grid-area: badge;
}

.panel .live {
  grid-area: live;
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
}

.panel .bpm {
  font-size: 3.6rem;
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.panel .unit {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--muted);
}

/* Intensity-zone colors (shared by the BPM chip and the sparkline bands).
   Standard five-zone palette: Z1 blue through Z5 red, gray for rest. */
:root {
  --zone-rest: #7a8699;
  --zone-z1: #4aa3ff;
  --zone-z2: #2ecc71;
  --zone-z3: #ffb020;
  --zone-z4: #ff7a45;
  --zone-z5: #ff2e33;
  --zone-over: #ff2e33;
}

/* The big BPM number takes the current zone's color. */
.panel .bpm.rest {
  color: var(--zone-rest);
}

.panel .bpm.z1 {
  color: var(--zone-z1);
}

.panel .bpm.z2 {
  color: var(--zone-z2);
}

.panel .bpm.z3 {
  color: var(--zone-z3);
}

.panel .bpm.z4 {
  color: var(--zone-z4);
}

.panel .bpm.z5 {
  color: var(--zone-z5);
}

.panel .bpm.over {
  color: var(--zone-over);
  animation: over-pulse 0.8s ease-in-out infinite;
}

/* Zone chip (Rest / Z1: Recovery..Z5: Peak / SUPERMAX) in the card's bottom
   row. It shares the badge grid area with "no signal": the chip is empty
   (hidden) exactly when the badge shows, so they never collide. */
.panel .zone {
  grid-area: badge;
  justify-self: start;
  align-self: center;
  padding: 0.12rem 0.45rem;
  border-radius: 0.3rem;
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: #fff;
  text-shadow: none;
}

.panel .zone:empty {
  display: none;
}

.panel .zone.rest {
  background: var(--zone-rest);
}

.panel .zone.z1 {
  background: var(--zone-z1);
}

.panel .zone.z2 {
  background: var(--zone-z2);
}

.panel .zone.z3 {
  background: var(--zone-z3);
  color: #201500;
}

.panel .zone.z4 {
  background: var(--zone-z4);
}

.panel .zone.z5 {
  background: var(--zone-z5);
}

.panel .zone.over {
  background: var(--zone-over);
  animation: over-pulse 0.8s ease-in-out infinite;
}

/* Zone-time bar: stacked share of session time per zone, left-aligned in the
   right half of the bottom row (opposite the zone chip), with the elapsed
   minutes riding at its end. Only zone colors are drawn — no background
   track. Scaled against max(1 hour, session duration): the bar (and label)
   grow rightward until the 1-hour mark, then only proportions change. */
.panel .zonebar {
  grid-area: badge;
  justify-self: end;
  align-self: center;
  width: 50%;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.panel .zonebar:empty {
  display: none;
}

/* Warning that zones assume a default age (no birth year / max HR set).
   Its grid row collapses when empty, like the respiration row. */
.panel .zone-warn {
  grid-area: zwarn;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #ffb020;
}

.panel .zone-warn:empty {
  display: none;
}

.panel .zonebar .segs {
  display: flex;
  height: 0.5rem;
  min-width: 0.25rem; /* a just-started session still shows a nub of color */
  border-radius: 0.25rem;
  overflow: hidden;
}

.panel .zonebar .mins {
  flex: none;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  line-height: 1;
}

.panel .zonebar .seg.rest {
  background: var(--zone-rest);
}

.panel .zonebar .seg.z1 {
  background: var(--zone-z1);
}

.panel .zonebar .seg.z2 {
  background: var(--zone-z2);
}

.panel .zonebar .seg.z3 {
  background: var(--zone-z3);
}

.panel .zonebar .seg.z4 {
  background: var(--zone-z4);
}

.panel .zonebar .seg.z5 {
  background: var(--zone-z5);
}

.panel .zonebar .seg.over {
  background: var(--zone-over);
}

@keyframes over-pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.45;
  }
}

/* Sparkline zone bands: translucent stripes + hairlines at the divisors. */
.band {
  fill-opacity: 0.25;
}

.band.z1 {
  fill: var(--zone-z1);
}

.band.z2 {
  fill: var(--zone-z2);
}

.band.z3 {
  fill: var(--zone-z3);
}

.band.z4 {
  fill: var(--zone-z4);
}

.band.z5 {
  fill: var(--zone-z5);
}

.band-line {
  stroke: rgba(255, 255, 255, 0.28);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

/* Sparkline: SVG of the last 5 minutes. Its grid area spans the name and
   live-BPM rows, so the chart fills the card's full upper-half height. */
.panel .spark {
  grid-area: spark;
  align-self: stretch;
  display: flex;
  align-items: stretch;
  gap: 0.4rem;
}

.panel .spark-svg {
  /* Responsive width: fits beside the BPM+chip at 720p, grows at 1080p. */
  width: clamp(9rem, 10vw, 13rem);
  min-height: 5.5rem; /* floor; normally stretched by the grid rows */
}

.panel .spark-svg svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.spark-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3.5;
  stroke-linejoin: round;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

.spark-area {
  fill: var(--accent);
  opacity: 0.16;
}

/* Live-head marker: round cap on a zero-length path + non-scaling stroke =
   a fixed-size screen-space circle despite the stretched viewBox. */
.spark-dot {
  fill: none;
  stroke: var(--accent);
  stroke-width: 7;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

/* Axis labels, one per gridline: the zone BPM cut-offs with zones,
   window min/max BPM without. */
.panel .spark-bounds {
  position: relative;
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 4ch;
}

.panel .spark-bounds .tick {
  position: absolute;
  left: 0;
  transform: translateY(-50%);
  line-height: 1;
}

/* Whole-session summary. */
.panel .session {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  min-height: 1.4rem; /* reserve the row so the card height stays constant */
  font-size: 1rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.panel .session b {
  color: var(--text);
  font-weight: 700;
}

/* Experimental respiration estimate (shown only with --respire-experiment). */
.panel .resp {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 1.3rem; /* reserve the row so EST appearing/vanishing doesn't resize */
  font-size: 1rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* When respiration is disabled (the default) or below the confidence floor, the
   row is empty — collapse it entirely so it leaves no gap in the card. */
.panel .resp:empty {
  display: none;
}

.panel .resp b {
  color: var(--text);
  font-weight: 700;
}

.panel .resp .est {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #ffb020;
  border: 1px solid currentColor;
  border-radius: 0.25rem;
  padding: 0 0.25rem;
}

/* Heart pulses at the current BPM (period set inline via --beat). */
.panel .name::before {
  content: "\2665"; /* ♥ */
  color: var(--accent);
  margin-right: 0.4rem;
  display: inline-block;
  animation: beat var(--beat, 1s) ease-in-out infinite;
}

@keyframes beat {
  0%,
  100% {
    transform: scale(1);
  }
  15% {
    transform: scale(1.35);
  }
  30% {
    transform: scale(1);
  }
}

/* Disconnected / stale states keep the layout stable but signal clearly. */
.panel.stale {
  background: var(--panel-stale-bg);
  border-left-color: var(--muted);
}

.panel.stale .bpm {
  color: var(--muted);
}

.panel.stale .name::before {
  animation: none;
  opacity: 0.4;
}

.panel .badge {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  /* Reserve the height of the zone chip so swapping between the chip and
     "no signal" doesn't resize the card. */
  min-height: 1.3rem;
}

.panel.stale .badge::after {
  content: "no signal";
  color: #ffb020;
}

/* Session clock: start time + running duration, bottom center of the frame. */
.session-clock {
  position: absolute;
  bottom: 2.5vh;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  padding: 0.3rem 0.9rem;
  border-radius: 1.2rem;
  background: var(--panel-bg);
  backdrop-filter: blur(2px);
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.session-clock:empty {
  display: none;
}

/* Tiny WS connection dot, hidden by default in normal operation. */
.conn-status {
  position: absolute;
  bottom: 1vh;
  right: 1vw;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: #888;
  opacity: 0.35;
}

.conn-status.online {
  background: #2ecc71;
}

.conn-status.offline {
  background: var(--accent);
}
