/* ==========================================================================
   console.css — the app bar and the protocol box (owner: console agent).

   appBar, protocolConsole. Compose the shared classes from components.css
   (.card, .btn, .chip, .status-dot, .frame-bytes, .byte, .byte-key …) and the
   app bar structure from layout.css (.app-bar, .app-bar__brand, .app-bar__end)
   and use only the tokens from tokens.css. Prefix view-specific classes with
   the view name (e.g. .console__block), never with a blocked token
   (ad, log, banner, track, popup, pixel, beacon).

   Text/background pairs used here, all from DESIGN.md §8: ink, ink-2, signal,
   fresh, caution and fault on --readout, --panel and --well; chip tones on
   their own soft tints; byte tiles on their own tile tokens.
   ========================================================================== */

/* --- app bar ---------------------------------------------------------------- */

/* The version as a small tag beside the title, like a nameplate. */
.app-bar__version {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--space-2);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-readout);
  font-size: var(--text-sm);
  line-height: 1.35;
}

/* A real box, not `display: contents`: some browsers drop the live region's
   role with it. While EVEN it is empty and taken out of the flex flow, so it
   adds no gap: at 360 px that one gap was what pushed the theme control onto
   a third row of the bar. */
.app-bar__end {
  position: relative;
}

.app-bar__parity-region {
  display: inline-flex;
  justify-content: flex-end;
}

.app-bar__parity-region:has(> .app-bar__parity[hidden]) {
  position: absolute;
}

/* On a phone the NONE chip gets a row of its own above the connection chip
   and the theme control, so switching to NONE adds that row and never
   reflows the other two. */
@media (max-width: 759.98px) {
  .app-bar__parity-region {
    flex-basis: 100%;
    order: -1;
  }
}

.app-bar__parity[hidden] {
  display: none;
}

/* The connection chip reads "● Simulator connected": the name in ink, the
   state quieter, the dot carrying the colour. */
.app-bar__connection {
  gap: var(--space-2);
  background: var(--bench);
  color: var(--ink-2);
}

.app-bar__connection-name {
  color: var(--ink);
}

.app-bar__connection-state {
  font-weight: 500;
}

.app-bar__connection[data-status='error'] .app-bar__connection-state {
  color: var(--fault);
}

/* Theme: three icon-only segments, quieter than the instrument's own
   controls (decorative --line border, no fill until chosen). */
.app-bar__theme.segmented {
  grid-auto-columns: auto;
  border-color: var(--line);
  background: transparent;
}

.app-bar__theme .segmented__option {
  width: var(--control-h-sm);
  min-height: calc(var(--control-h-sm) - 4px);
  padding: 0;
}

@media (pointer: coarse), (max-width: 759.98px) {
  .app-bar__theme .segmented__option {
    min-height: var(--control-h-sm);
  }
}

.app-bar__theme .icon {
  width: 16px;
  height: 16px;
}

/* --- protocol box: card ----------------------------------------------------- */

.card--protocol .card__body {
  min-height: 0;
}

.card--protocol .card__actions {
  gap: var(--space-1);
}

.console__key-toggle[aria-expanded='true'] {
  background: var(--signal-soft);
}

/* The byte key: quiet, two short rows, only when asked for. */
.console__key {
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-1);
}

.console__key[hidden] {
  display: none;
}

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

.console__key-name {
  min-width: 6.5rem;
  color: var(--ink);
  font-weight: 600;
}

/* --- protocol box: the screen ----------------------------------------------- */

/* The original's white text box, sunk into the card. It scrolls on its own so
   a long session never stretches the page; on the two-column layout it fills
   the card down to the left column's end. */
.console {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  /* The card around it has a floor of its own (layout.css, ≥ 1180), sized
     for one whole read; this only keeps the box from collapsing below that. */
  min-height: 8rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--readout);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-well);
  box-shadow: var(--inset);
}

.console:focus-visible {
  outline-offset: 1px;
}

@media (max-width: 1179.98px) {
  .console {
    flex: none;
    min-height: 12rem;
    max-height: min(36rem, 72vh);
  }
}

.console__empty {
  margin: auto;
  padding: var(--space-8) var(--space-6);
  max-width: 44ch;
  text-align: center;
}

.console__empty[hidden] {
  display: none;
}

.console__empty-title {
  margin-bottom: var(--space-1);
  color: var(--ink);
  font-weight: 500;
}

/* Zero-height, sticky: the button floats over the transcript without
   pushing it down when it appears. */
.console__jump-wrap {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  justify-content: center;
  height: 0;
  overflow: visible;
}

.console__jump-wrap[hidden] {
  display: none;
}

.console__jump {
  margin-top: var(--space-2);
  box-shadow: var(--float-shadow);
}

.console__current:empty,
.console__earlier:empty {
  display: none;
}

.console__earlier {
  margin: 0;
  padding: 0;
  list-style: none;
  border-top: var(--hairline) solid var(--line);
}

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

/* --- one action ------------------------------------------------------------- */

.console__block {
  min-width: 0;
}

.console__current > .console__block {
  padding: var(--space-3) var(--space-4) var(--space-4);
}

.console__summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--ink);
  font: inherit;
  text-align: left;
}

button.console__summary {
  cursor: pointer;
}

button.console__summary:focus-visible {
  outline-offset: -2px;
  border-radius: var(--radius-readout);
}

.console__chevron,
.console__chevron-space {
  flex: none;
  width: 18px;
  height: 18px;
  color: var(--ink-2);
}

.console__chevron {
  transition: transform var(--dur-ui) var(--ease-out);
}

.console__block[data-expanded='true'] .console__chevron {
  transform: rotate(90deg);
}

.console__what {
  flex: 1 1 auto;
  min-width: 0;
}

.console__intro {
  font-weight: 600;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.console__short {
  display: none;
  font-weight: 500;
}

/* Collapsed: one line, the short "what", cut with an ellipsis if it must. */
.console__block[data-expanded='false'] .console__intro {
  display: none;
}

.console__block[data-expanded='false'] .console__short {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.console__result {
  flex: none;
  font-variant-numeric: tabular-nums lining-nums;
}

.console__result[hidden] {
  display: none;
}

.console__time {
  flex: none;
  min-width: 8ch;
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums lining-nums;
  text-align: right;
}

/* Earlier actions: compact rows on the same sunk surface. */
.console__item > .console__block {
  padding: var(--space-2) var(--space-4);
}

.console__item > .console__block > .console__summary {
  min-height: calc(var(--control-h) - var(--space-2));
}

.console__item > .console__block > button.console__summary:hover {
  color: var(--signal);
}

/* An opened earlier action stays on the sunk surface: the data tiles are
   tuned against --readout, and on --well they would fade into it. */
.console__item > .console__block[data-expanded='true'] {
  padding-bottom: var(--space-4);
}

/* --- lines ------------------------------------------------------------------- */

.console__lines {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-top: var(--space-3);
  /* Under the summary text, past the chevron. */
  padding-left: calc(18px + var(--space-2));
  min-width: 0;
}

.console__lines:empty,
.console__lines[hidden] {
  display: none;
}

.console__line {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  min-width: 0;
  color: var(--ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.console__line .icon {
  margin-top: 1px;
}

.console__line--ok {
  color: var(--fresh);
  font-weight: 500;
}

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

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

/* The original's checklist, under the failure it explains. */
.console__check {
  margin-top: var(--space-1);
  padding-left: calc(18px + var(--space-2));
  color: var(--ink);
}

.console__check-items {
  margin: var(--space-1) 0 0;
  padding: 0;
  list-style: none;
}

.console__check-items > li {
  padding-left: 2ch;
  text-indent: -2ch;
}

.console__check-items > li::before {
  content: '- ';
  content: '- ' / '';
}

/* A frame: the original's direction prefix, then the bytes. The prefix stays
   in the mono face so `PC     --> Sensor:` and `Sensor --> PC    :` line up
   column for column, exactly as in the original text box. The tiles wrap
   under it when the line is narrow (phones), never past the edge. */
.console__frame {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-3);
  min-width: 0;
}

.console__dir {
  flex: none;
  color: var(--ink-2);
  font-family: var(--font-mono);
  font-size: var(--text-mono);
  white-space: pre;
  font-variant-ligatures: none;
  font-feature-settings: 'calt' 0, 'liga' 0;
}

.console__frame .frame-bytes {
  flex: 0 1 auto;
}

/* T-Bus talks ASCII: its payload is shown as text on a data-coloured tile. */
.console__ascii {
  padding: 2px var(--space-2);
  border-radius: var(--radius-tile);
  background: var(--byte-data-tile);
  color: var(--byte-data);
  font-weight: 500;
  overflow-wrap: anywhere;
}

.console__silence {
  color: var(--ink-2);
  font-style: italic;
}

/* The Copy fallback's scratch field: present for a moment, never seen. */
.console__clip {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* --- narrow --------------------------------------------------------------------- */

/* Sized by the transcript's own width, not the viewport: in a narrow box the
   open action's intro gets the whole line and its outcome and time drop to a
   second row beneath it. A folded row keeps everything on one line. */
.console {
  container: console / inline-size;
}

@container console (max-width: 34rem) {
  .console__block[data-expanded='true'] > .console__summary {
    display: grid;
    grid-template-columns: 18px minmax(0, 1fr) auto;
    grid-template-areas:
      'mark what what'
      '.    result time';
    align-items: center;
    gap: var(--space-1) var(--space-2);
  }

  /* On the first line of a wrapped intro, not in the middle of it. */
  .console__block[data-expanded='true'] > .console__summary > :first-child {
    grid-area: mark;
    align-self: start;
    margin-top: 2px;
  }

  .console__block[data-expanded='true'] > .console__summary > .console__what {
    grid-area: what;
  }

  .console__block[data-expanded='true'] > .console__summary > .console__result {
    grid-area: result;
    justify-self: start;
  }

  .console__block[data-expanded='true'] > .console__summary > .console__time {
    grid-area: time;
  }

  /* A folded row next to its chip and time has a few characters left on a
     phone; two lines say "Reading counts (Register 3)" in full. */
  .console__block[data-expanded='false'] .console__short {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 759.98px) {
  .console__current > .console__block,
  .console__item > .console__block {
    padding-left: var(--space-3);
    padding-right: var(--space-3);
  }

  .console__lines {
    padding-left: 0;
  }

  .console__check {
    padding-left: var(--space-2);
  }

  .console__time {
    min-width: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .console__chevron {
    transition: none;
  }
}
