/* ==========================================================================
   left.css — the left column's views (owner: left agent).

   comportBox, expertSettings, addressBox, scanDialog, sensorBox,
   protocolSwitchBox, sessionBox. Compose the shared classes from
   components.css (.box, .well, .btn, .segmented, .select, .figure, .chip,
   .modal, .switch …) and use only the tokens from tokens.css — no literal
   colours, both themes come for free. Prefix view-specific classes with the
   view name (e.g. .comport__identity), never with a blocked token
   (ad, log, banner, track, popup, pixel, beacon).

   Quiet by design: the bold thing of this app is the readouts and the byte
   tiles on the right. The left column is the instrument's knobs — figures in
   the title face, everything else ink-2 hints and the shared controls.
   ========================================================================== */

/* --- shared within the column --------------------------------------------- */

/* In the 2×2 arrangement two boxes of one row stretch to the same height;
   letting the body fill the box lets a box park its last control on the
   bottom edge, so the buttons of neighbouring boxes line up. */
.box--comport .box__body,
.box--address .box__body,
.box--protocol-switch .box__body {
  flex: 1 1 auto;
}

/* --- Comport --------------------------------------------------------------- */

/* The port's name and the status share the first row; the detail line runs
   under both at the box's full width. Squeezed beside the status it wrapped
   ("Connect opens the / browser's picker"), and the box changed height when
   a connect replaced it with a one-line detail, moving every box below. The
   paragraph around name and detail stays in the DOM (one sentence for a
   screen reader) and steps out of the layout. */
.comport__head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    'name status'
    'detail detail';
  align-items: center;
  gap: 2px var(--space-3);
}

.comport__identity {
  display: contents;
}

.comport__name {
  grid-area: name;
  min-width: 0;
  overflow-wrap: anywhere;
}

.comport__detail {
  grid-area: detail;
}

.comport__status {
  grid-area: status;
}

/* "No port" is a placeholder, not an identity. */
.comport__name.is-placeholder {
  color: var(--ink-2);
}

.comport__detail {
  font-variant-numeric: tabular-nums;
}


.comport__status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 500;
  white-space: nowrap;
}

.comport__status:has(.status-dot[data-status='connected']) {
  color: var(--ink);
}

.comport__status:has(.status-dot[data-status='error']) {
  color: var(--fault);
}

.comport__note {
  margin-top: calc(-1 * var(--space-1));
}

.comport__connect {
  margin-top: auto;
}

.comport__choose {
  align-self: center;
  margin-top: calc(-1 * var(--space-1));
}

.comport__error {
  color: var(--fault);
  font-size: var(--text-sm);
  line-height: 1.4;
  overflow-wrap: anywhere;
}

/* The line settings: where parity NONE shows inside this box. */
.comport__line {
  padding-top: var(--space-3);
  border-top: var(--hairline) solid var(--line);
  font-variant-numeric: tabular-nums;
}

.comport__line-part {
  white-space: nowrap;
}

.comport__line-modbus.is-caution {
  color: var(--caution);
  font-weight: 600;
}

/* --- Advanced Settings (expertSettings.js) -------------------------------- */

.expert {
  width: min(25rem, calc(100vw - 2 * var(--space-4)));
}

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

.expert__label {
  font-weight: 500;
}

.expert__parity {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

/* The switch's two state words (the CVI switch's off/on labels). */
.expert__state {
  min-width: 2.75em;
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  transition: color var(--dur-ui) ease;
}

.expert__state.is-active {
  color: var(--ink);
}

.expert__state[data-parity='none'].is-active {
  color: var(--caution);
}

/* --- Modbus Address ------------------------------------------------------- */

/* The original's big centred figure in a white field — as an input. */
.address__input {
  width: 100%;
  min-height: calc(var(--control-h) + var(--space-4));
  padding: 0 var(--space-3);
  font-family: var(--font-title);
  font-size: var(--text-2xl);
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums lining-nums;
  text-align: center;
}

/* A running scan drives the address; the field shows it but takes no typing.
   Any other action on the bus holds it too (it took its address at click
   time); the grey waits 300 ms, like the register buttons' dimming, so a
   50 ms read does not flicker the figure. */
.address__input[readonly] {
  color: var(--ink-2);
  transition: color var(--dur-ui) ease 300ms;
}

.address__legend {
  color: var(--ink-2);
  font-size: var(--text-sm);
  text-align: center;
  transition: color var(--dur-ui) ease;
}

.address__legend.is-active {
  color: var(--ink);
  font-weight: 500;
}

.address__error {
  margin-top: calc(-1 * var(--space-1));
  color: var(--fault);
  font-size: var(--text-sm);
  line-height: 1.4;
  text-align: center;
}

.address__error:empty {
  display: none;
}

.address__scan {
  margin-top: auto;
}

/* --- Scan bus (scanDialog.js) --------------------------------------------- */

.scan__range {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 8rem));
  gap: var(--space-3);
}

.scan__input {
  width: 100%;
  font-family: var(--font-title);
  font-size: var(--text-lg);
  font-weight: 600;
  text-align: center;
}

/* Held while the sweep runs (read-only keeps focus where it was). */
.scan__input[readonly] {
  color: var(--ink-2);
  background: var(--bench);
}

.scan__error {
  margin-top: calc(-1 * var(--space-2));
  color: var(--fault);
  font-size: var(--text-sm);
}

.scan__progress {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-1) var(--space-3);
}

.scan__meter {
  height: 6px;
  overflow: hidden;
  border-radius: 3px;
  background: var(--well);
}

.scan__meter-fill {
  display: block;
  inline-size: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--signal);
  transition: inline-size var(--dur-ui) linear;
}

.scan__percent {
  min-width: 4ch;
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.scan__status {
  grid-column: 1 / -1;
  color: var(--ink-2);
  font-size: var(--text-sm);
}

/* Fixed height, so results arriving never move the buttons under it. */
.scan__results {
  height: min(17rem, 38dvh);
  overflow: auto;
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-well);
  background: var(--readout);
}

.scan__empty {
  padding: var(--space-6) var(--space-4);
  text-align: center;
}

.scan__item + .scan__item {
  border-top: var(--hairline) solid var(--line);
}

.scan__device {
  display: grid;
  grid-template-columns: 3.25ch minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 48px;
  padding: var(--space-2) var(--space-3);
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
  transition: background-color var(--dur-ui) ease;
}

.scan__device:hover {
  background: var(--signal-soft);
}

.scan__device:focus-visible {
  outline-offset: -2px;
}

.scan__device-address {
  font-family: var(--font-title);
  font-size: var(--text-xl);
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums lining-nums;
  text-align: right;
}

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

.scan__device-type {
  font-weight: 600;
}

.scan__device-detail {
  color: var(--ink-2);
  font-size: var(--text-sm);
}

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

.scan__device:hover .scan__device-go {
  color: var(--signal);
}

/* --- Select Sensor --------------------------------------------------------- */

.sensor__select {
  font-family: var(--font-title);
  font-size: var(--text-lg);
  font-weight: 600;
}

/* --- Protocol Switch ------------------------------------------------------- */

.protocol-switch__mode {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-1) var(--space-3);
  font-size: var(--text-sm);
}

.protocol-switch__mode-label {
  color: var(--ink-2);
}

.protocol-switch__mode-value {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--ink);
  font-weight: 600;
}

.protocol-switch__mode[data-mode='tbus'] .protocol-switch__mode-value {
  color: var(--caution);
}

.protocol-switch__mode[data-mode='unknown'] .protocol-switch__mode-value {
  color: var(--ink-2);
  font-weight: 500;
}

.protocol-switch__mode + .protocol-switch__button {
  margin-top: auto;
}

.protocol-switch__button {
  position: relative;
  overflow: hidden;
}

/* The switch at work keeps full strength and runs the readout's reading bar
   along its bottom edge; its sibling dims like any disabled button. */
.protocol-switch__button.is-working[aria-disabled='true'] {
  opacity: 1;
  cursor: progress;
}

.protocol-switch__button::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 38%;
  height: 2px;
  background: var(--signal);
  opacity: 0;
  pointer-events: none;
}

.protocol-switch__button.is-working::after {
  opacity: 1;
  animation: readout-sweep var(--dur-reading) var(--ease-out) infinite;
}

@media (prefers-reduced-motion: reduce) {
  .protocol-switch__button.is-working::after {
    width: 100%;
    animation: none;
    opacity: 0.6;
  }
}

/* --- the fifth box (sessionBox.js) ----------------------------------------- */

.session__body {
  flex: 1 1 auto;
}

.session__bus {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.session__heading {
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 600;
}

.session__devices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(7.5rem, 1fr));
  gap: var(--space-1);
}

.session__device {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  width: 100%;
  min-height: var(--control-h-sm);
  padding: var(--space-1) var(--space-2);
  border: var(--hairline) solid transparent;
  border-radius: var(--radius-control);
  background: transparent;
  color: var(--ink);
  text-align: left;
  transition:
    background-color var(--dur-ui) ease,
    border-color var(--dur-ui) ease;
}

.session__device:hover {
  background: var(--signal-soft);
}

.session__device[aria-current='true'] {
  border-color: var(--signal);
  background: var(--signal-soft);
}

.session__device-address {
  min-width: 3ch;
  font-family: var(--font-title);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums lining-nums;
  text-align: right;
}

.session__device-type {
  min-width: 0;
  overflow: hidden;
  color: var(--ink-2);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session__device[aria-current='true'] .session__device-type {
  color: var(--signal);
}

/* The version sits on the box's bottom edge, where the original's empty box
   ends with the window. */
.session__version {
  margin-top: auto;
  color: var(--ink-2);
  font-family: var(--font-title);
  font-size: var(--text-sm);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  text-wrap: balance;
}

/* --- phones: the four boxes stacked tight ---------------------------------
   Below 560 px the boxes stack one per row (layout.css) and would take two
   screens before the first register button; the brief wants them compact.
   Each keeps every control and its 44 px target, and loses only air. */
@media (max-width: 559.98px) {
  /* The line settings without the rule and the gap above them. */
  .comport__line {
    padding-top: 0;
    border-top: 0;
  }

  /* The address figure and Scan bus share a row (figure 45 %, button 55 %);
     the broadcast legend sits under the figure, a message under both. */
  .box--address .box__body {
    display: grid;
    grid-template-columns: minmax(0, 45fr) minmax(0, 55fr);
    align-items: center;
    gap: var(--space-1) var(--space-2);
  }

  .address__input {
    grid-column: 1;
    grid-row: 1;
    min-height: var(--control-h);
    font-size: var(--text-xl);
  }

  .address__scan {
    grid-column: 2;
    grid-row: 1;
  }

  /* The legend explains the figure, so it sits under it. */
  .address__legend {
    grid-column: 1;
  }

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

  /* The description to one line; the select names the sensor, and the whole
     sentence is still what a screen reader hears (aria-describedby). */
  .sensor__description {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    overflow: hidden;
  }
}

/* Below the desktop width the fifth box would be a full-width slab holding
   one line the app bar already shows (the version), so it steps aside until a
   scan gives it something to list. */
@media (max-width: 1179.98px) {
  .slot--session:has(> .session--quiet) {
    display: none;
  }
}
