/* ==========================================================================
   read.css — the register views (owner: read agent).

   changeAddressCard, registerCard, customRegister, historyChart. Compose the
   shared classes from components.css (.card, .well, .well__label, .btn,
   .readout, .unit, .stepper, .tabs, .select, .input …) and use only the
   tokens from tokens.css (chart series: --series-1..6). Prefix view-specific
   classes with the view name (e.g. .registers__row), never with a blocked
   token (ad, log, banner, track, popup, pixel, beacon).

   One column grid is shared by the change-address strip and every register
   row, so the original's vertical line — `Set new Modbus Adress` above the
   read buttons, every value field under the next — survives the redesign:

     [ action button 15rem ][ readout 9.5rem ][ unit ]

   Rows switch to a phone arrangement by the width of their own well
   (container queries), not the viewport: a well is narrow on a phone, but
   also when SMT500's labelled wells share a wide card.
   ========================================================================== */

.card--registers,
.card--change-address {
  /* Width of the button column; the change-address button uses the same. */
  --read-action-w: 15rem;
}

/* --- busy gate -------------------------------------------------------------- */
/* While an operation is on the bus the action buttons are aria-disabled (the
   focused one keeps focus). The dimming waits 300 ms: a 50 ms read must not
   flicker the whole panel, a 2 s timeout should visibly hold it. Leaving the
   state uses the base .btn transition (no opacity), so it comes back at once. */
.registers__gated[aria-disabled='true'],
.change-address__submit[aria-disabled='true'],
.custom__submit[aria-disabled='true'] {
  cursor: progress;
  transition: opacity var(--dur-ui) ease 300ms;
}

/* --- Change Sensor Address ------------------------------------------------ */
.change-address__well {
  gap: var(--space-2);
}

.change-address__row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3) var(--space-6);
}

.change-address__submit {
  flex: 0 0 var(--read-action-w);
  max-width: 100%;
}

.change-address__field {
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
}

/* The original's own label, at body size beside the stepper. */
.change-address__label {
  font-size: var(--text-md);
  color: var(--ink);
}

.change-address__stepper {
  width: 10rem;
}

.change-address__stepper:has(.stepper__input[aria-invalid='true']) {
  border-color: var(--fault);
}

.change-address__message {
  margin: 0;
}

.change-address__message--error {
  color: var(--fault);
  font-weight: 500;
}

@media (max-width: 759.98px) {
  .change-address__row {
    gap: var(--space-3);
  }

  .change-address__submit {
    flex: 1 1 100%;
  }

  .change-address__field {
    flex: 1 1 100%;
    justify-content: space-between;
  }

  .change-address__stepper {
    flex: 0 1 11rem;
  }
}

/* --- Read Sensor Registers: title row --------------------------------------- */
/* The actions fill the rest of the title row, the sensor chip at their start
   and the buttons at their end: next to the title on a wide card, one line
   of its own (chip left, buttons right) when a phone wraps it. */
.card--registers .card__actions {
  flex: 1 1 auto;
  justify-content: flex-end;
}

.registers__sensor {
  /* The selected sensor; quiet, but findable. */
  margin-right: auto;
  background: var(--signal-soft);
  color: var(--signal);
}

.registers__poll {
  display: inline-flex;
  align-items: stretch;
}

/* The toggle and its interval read as one control. */
.registers__poll-toggle {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.registers__poll-icon {
  display: inline-flex;
}

.registers__interval {
  width: auto;
  min-height: var(--control-h-sm);
  margin-left: calc(-1 * var(--hairline));
  padding-left: var(--space-2);
  padding-right: calc(var(--space-2) + 18px);
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  font-size: var(--text-sm);
}

/* A poll in progress is visible at a glance: the toggle carries the signal
   colour (aria-pressed), and its interval joins it. */
.card--registers.is-polling .registers__interval {
  border-color: var(--signal);
  color: var(--signal);
}

@media (max-width: 759.98px) {
  /* Chip, Read all and Poll on one line under the title, even at 360 px. */
  .card--registers .card__actions {
    column-gap: var(--space-1);
  }

  .registers__sensor {
    padding-inline: var(--space-1);
  }
}

/* --- tabs ------------------------------------------------------------------- */
.registers__tabset {
  gap: var(--space-3);
}

.registers__panel--registers {
  container: registers / inline-size;
}

/* ≥ 1180 the card is held to the window (layout.css): the panels scroll
   inside it, under the tabs, like the original's register frame with its own
   scroll bar. Every flex level between the card and the panel must be
   allowed to shrink (min-height: 0) or the panel would never get the
   constraint. */
@media (min-width: 1180px) {
  .card--registers .card__body,
  .registers__tabset {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* The tab list scrolls sideways on a phone, which lets a flex item shrink
     to nothing; here it keeps its height. */
  .registers__tabset > .tabs__list {
    flex: none;
  }

  .registers__panel {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    /* A scroll box clips what overflows it, focus rings included: 4px of
       room on every side (taken back by the margin, so nothing moves) keeps
       the ring of a control at the panel's edge whole. */
    padding: 4px;
    margin: -4px;
    scroll-padding-block: var(--space-3);
  }
}

/* --- register wells and rows ----------------------------------------------- */
.registers__wells {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: var(--space-3);
}

.registers__well {
  container: registers-well / inline-size;
  gap: var(--space-2);
}

.registers__well-label {
  margin: 0 0 var(--space-1);
}

.registers__row {
  display: grid;
  grid-template-columns: minmax(8rem, var(--read-action-w)) var(--readout-w) minmax(max-content, 1fr);
  align-items: center;
  column-gap: var(--space-4);
}

.registers__action {
  min-width: 0;
}

.registers__readout {
  inline-size: 100%;
}

.registers__unit {
  min-width: 0;
}

.registers__note {
  grid-column: 1 / -1;
}

.registers__row--disabled .registers__action {
  text-decoration: line-through;
}

/* SMT500's labelled wells share a line when the card is wide: Temperature
   (one row) across the top, Water Content and TDR Time side by side — the
   eleven rows fit without pushing the protocol box out of view. */
@container registers (min-width: 56rem) {
  .registers__wells--labelled {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .registers__wells--labelled > .registers__well--single,
  .registers__wells--labelled > .registers__note {
    grid-column: 1 / -1;
  }
}

/* Phone arrangement: button and readout share the line, the unit sits under
   the readout, right-aligned with its digits — so `% rel. Humidity` fits a
   360 px screen without squeezing the button. 27rem is where the one-line
   row stops fitting its longest unit (8rem + 9.5rem + `% rel. Humidity` +
   gaps). SMT500's side-by-side wells can drop just below it; the rule after
   this one keeps their rows on one line, since their units are short. */
@container registers-well (max-width: 27rem) {
  /* 7rem still holds "-100.00" at the readout size once the inner padding
     shrinks; every px it gives up goes to the button label. */
  .registers__row {
    grid-template-columns: minmax(0, 1fr) 7rem;
    grid-template-areas:
      'action readout'
      '. unit';
    column-gap: var(--space-3);
    row-gap: 2px;
  }

  .registers__action {
    grid-area: action;
    padding-inline: var(--space-3);
  }

  .registers__readout {
    grid-area: readout;
    padding-inline: var(--space-2);
  }

  .registers__unit {
    grid-area: unit;
    justify-self: end;
    padding-inline-end: var(--space-1);
    font-size: var(--text-sm);
    line-height: var(--leading-tight);
    text-align: right;
    white-space: normal;
  }

  .registers__unit:empty {
    display: none;
  }
}

/* Between 56 and 62rem (a 1024 px tablet holds a 58rem card) SMT500's two
   wells only share the line if the readouts give up 1.5rem: 8rem still holds
   the widest SMT500 value ("-100.00") and leaves "Read Water Content Field 5"
   its single line. Stacking them instead would put the protocol box — the
   other half of every button press — a full screen further down. Each well
   is then 25.6–28.6rem wide inside, under the phone rule's 27rem, but its
   units (°C, vol. %, ps) are short enough for the one-line row, which is
   restored here. Every row of the panel, Temperature included, narrows
   together so the readout column stays one straight edge. */
@container registers (56rem <= width < 62rem) {
  /* 14.5rem, not the usual 15: the half-width wells cannot give the button
     its full 15rem at 1024 px, and the full-width Temperature well must not
     either, or its readout sits 2px off the column below it. */
  .registers__wells--labelled .registers__row {
    grid-template-columns: minmax(8rem, 14.5rem) 8rem minmax(max-content, 1fr);
    grid-template-areas: none;
    column-gap: var(--space-3);
    row-gap: 0;
  }

  .registers__wells--labelled .registers__action,
  .registers__wells--labelled .registers__readout,
  .registers__wells--labelled .registers__unit {
    grid-area: auto;
  }

  .registers__wells--labelled .registers__action {
    padding-inline: var(--space-4);
  }

  .registers__wells--labelled .registers__readout {
    padding-inline: var(--space-3);
  }

  .registers__wells--labelled .registers__unit {
    justify-self: start;
    padding-inline-end: 0;
    font-size: var(--text-md);
    line-height: inherit;
    text-align: start;
    white-space: nowrap;
  }
}

/* Between 51 and 56rem — the two-column layout from ~1222 to ~1320 px, a
   common laptop band (1280 × 800 / 1024) — SMT500's wells still share the
   line if the rows give up more: wells padded 12px, a 12.75rem button column
   (the 177px of "Read Water Content Field 5" plus its padding), a 7rem
   readout (enough for "-100.00" once its inner padding shrinks) and 8px
   gaps, which is 371px against 372px of well. Stacked, the eleven rows would
   need twice the scrolling in the panel and leave half of every well empty.
   Below 51rem (1180–1221 px) they stack; the panel scrolls (layout.css). */
@container registers (51rem <= width < 56rem) {
  .registers__wells--labelled {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .registers__wells--labelled > .registers__well--single,
  .registers__wells--labelled > .registers__note {
    grid-column: 1 / -1;
  }

  .registers__wells--labelled > .registers__well {
    padding-inline: var(--space-3);
  }

  .registers__wells--labelled .registers__row {
    grid-template-columns: 12.75rem 7rem minmax(max-content, 1fr);
    grid-template-areas: none;
    column-gap: var(--space-2);
    row-gap: 0;
  }

  .registers__wells--labelled .registers__action,
  .registers__wells--labelled .registers__readout,
  .registers__wells--labelled .registers__unit {
    grid-area: auto;
  }

  .registers__wells--labelled .registers__action {
    padding-inline: var(--space-3);
  }

  .registers__wells--labelled .registers__readout {
    padding-inline: var(--space-2);
  }

  .registers__wells--labelled .registers__unit {
    justify-self: start;
    padding-inline-end: 0;
    font-size: var(--text-md);
    line-height: inherit;
    text-align: start;
    white-space: nowrap;
  }
}

/* --- Custom register -------------------------------------------------------- */
.custom {
  container: custom / inline-size;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Function | Register | Count or Value | Codec | Read — one line where the
   card is wide. Count and Value are never shown together (03/04 read a
   count, 06 writes a value), so they share the third column. */
.custom__fields {
  display: grid;
  grid-template-columns:
    minmax(12rem, 1.6fr) minmax(7rem, 1fr) minmax(6rem, 0.8fr) minmax(14rem, 1.8fr)
    minmax(7rem, auto);
  align-items: start;
  gap: var(--space-3) var(--space-4);
}

.custom__field--submit {
  align-self: start;
  /* Line the button up with the controls: skip the label's line. */
  padding-top: calc(var(--text-sm) * 1.4 + var(--space-1));
}

.custom__submit {
  width: 100%;
}

.custom__input {
  width: 100%;
  font-variant-numeric: tabular-nums;
}

/* The hex/decimal echo under a number; reserved height so typing a digit
   never moves the row below. */
.custom__echo {
  min-height: calc(var(--text-sm) * 1.4);
  font-variant-numeric: tabular-nums;
}

.custom__result {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: var(--space-2) var(--space-6);
  padding: var(--space-1) var(--space-4) 0;
}

.custom__readout-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

/* ASCII replies can be longer than a number: grow instead of clipping. */
.custom__readout {
  inline-size: auto;
  min-inline-size: var(--readout-w);
}

.custom__facts {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-6);
  margin: 0;
  font-size: var(--text-sm);
}

.custom__fact {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  min-width: 0;
}

.custom__fact dt {
  color: var(--ink-2);
}

.custom__fact dd {
  margin: 0;
  min-width: 0;
  color: var(--ink);
  font-weight: 500;
  overflow-wrap: anywhere;
}

.custom__words {
  font-size: var(--text-sm);
}

.custom__status {
  grid-column: 1 / -1;
  margin: 0;
  min-height: calc(var(--text-sm) * 1.4);
}

.custom__status--ok {
  color: var(--fresh);
}

.custom__status--error {
  color: var(--fault);
  font-weight: 500;
}

.custom__hint {
  padding: 0 var(--space-4);
}

/* Narrower cards: two columns — Function and Codec across, Register beside
   Count/Value, the button last. */
@container custom (max-width: 56rem) {
  .custom__fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .custom__field--function,
  .custom__field--codec,
  .custom__field--submit {
    grid-column: 1 / -1;
  }

  .custom__field--submit {
    padding-top: 0;
  }
}

/* Phones: the result stacks, everything flush with the card. */
@container custom (max-width: 30rem) {
  .custom__result {
    grid-template-columns: minmax(0, 1fr);
    padding: 0;
  }

  .custom__hint {
    padding: 0;
  }
}

/* --- History ---------------------------------------------------------------- */
.history {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
}

.history .is-hidden {
  display: none;
}

.history__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-2) var(--space-4);
}

.history__legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-2);
  min-width: 0;
}

.history__legend:empty {
  display: none;
}

.history__tools {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-left: auto;
}

.history__count {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.history__toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--control-h-sm);
  padding: 0 var(--space-2);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-control);
  background: var(--panel);
  color: var(--ink);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition:
    background-color var(--dur-ui) ease,
    border-color var(--dur-ui) ease,
    color var(--dur-ui) ease;
}

.history__toggle:hover {
  border-color: var(--line-strong);
}

.history__toggle[aria-pressed='false'] {
  border-style: dashed;
  color: var(--ink-2);
}

.history__toggle[aria-pressed='false'] .history__swatch {
  opacity: 0.35;
}

.history__swatch {
  flex: none;
  width: 14px;
  height: 3px;
  border-radius: 2px;
  background: var(--series, var(--series-1));
}

.history__toggle .history__unit {
  color: var(--ink-2);
  font-weight: 400;
}

/* One modifier drives the line, the swatch, the cursor dot and the value, so
   a series has one colour everywhere it appears. */
.history__series--1 { --series: var(--series-1); }
.history__series--2 { --series: var(--series-2); }
.history__series--3 { --series: var(--series-3); }
.history__series--4 { --series: var(--series-4); }
.history__series--5 { --series: var(--series-5); }
.history__series--6 { --series: var(--series-6); }

.history__plot {
  position: relative;
  min-width: 0;
  padding: var(--space-2) 0;
  border-radius: var(--radius-well);
  background: var(--panel);
}

.history__svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
  /* Horizontal drags move the crosshair; vertical ones still scroll a phone. */
  touch-action: pan-y;
}

.history__grid-line {
  stroke: var(--line);
  stroke-width: 1;
}

.history__tick,
.history__axis {
  fill: var(--ink-2);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.history__axis {
  font-weight: 600;
}

.history__line {
  fill: none;
  stroke: var(--series, var(--series-1));
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.history__cursor-line {
  stroke: var(--ink-2);
  stroke-width: 1;
  stroke-dasharray: 3 3;
}

.history__dot {
  fill: var(--panel);
  stroke: var(--series, var(--series-1));
  stroke-width: 2;
}

.history__empty {
  margin: 0;
  padding: var(--space-10) var(--space-4);
  border: var(--hairline) dashed var(--line);
  border-radius: var(--radius-well);
  text-align: center;
  max-width: none;
}

.history__values {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-1) var(--space-5);
  min-height: 1.5rem;
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

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

.history__item {
  display: inline-flex;
  align-items: baseline;
  gap: var(--space-1);
  white-space: nowrap;
}

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

.history__value {
  font-family: var(--font-title);
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--ink);
}

/* The swatch colour marks the value too, as a short underline — series
   colours are chosen for lines (3:1), not for text on the panel. */
.history__value::after {
  content: '';
  display: block;
  height: 2px;
  margin-top: 1px;
  border-radius: 1px;
  background: var(--series, var(--series-1));
}

.history__item .history__unit {
  color: var(--ink-2);
}

@media (max-width: 759.98px) {
  /* An SMT500 puts eleven series in the legend: two even columns instead of
     a ragged wrap, labels truncated rather than widening a column. */
  .history__legend {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .history__toggle {
    min-width: 0;
  }

  .history__toggle-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .history__toggle .history__unit {
    display: none;
  }
}
