/* Layout and components of the terminal-style interface. */

body {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
}

/* Status bar, in the style of a tmux status line. */

.status-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  align-items: center;
  min-height: 2.75rem;
  padding-right: var(--space-3);
  border-bottom: 1px solid var(--color-rule);
  background: var(--color-screen-raised);
}

.status-bar__session {
  align-self: stretch;
  display: flex;
  align-items: center;
  padding: 0 var(--space-3);
  background: var(--color-duck);
  color: var(--color-duck-ink);
  font-weight: 700;
}

.board-field {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  color: var(--color-muted);
}

/* The board choice, drawn like a terminal menu: a field, and the list of boards that opens
   below it with the highlighted line inverted, as on the bracketed buttons. */

.board-picker {
  /* Anchors the list. */
  position: relative;
}

.board-picker__field {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  min-height: var(--control-height);
  padding: 0 var(--space-2);
  border: 1px solid var(--color-rule);
  background: var(--color-screen);
  color: var(--color-text);
  cursor: pointer;
}

.board-picker__field:hover,
.board-picker__field[aria-expanded="true"] {
  border-color: var(--color-muted);
}

.board-picker__field[aria-disabled="true"] {
  opacity: 0.45;
  cursor: not-allowed;
}

.board-picker__value {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.board-picker__caret {
  margin-left: auto;
  color: var(--color-muted);
  font-size: var(--text-small);
}

.board-picker__caret::before {
  content: "▼";
}

.board-picker__field[aria-expanded="true"] .board-picker__caret::before {
  content: "▲";
}

.board-picker__popup {
  position: absolute;
  top: calc(100% + var(--space-1));
  left: 0;
  z-index: 2;
  width: max-content;
  min-width: 100%;
  max-width: min(64ch, calc(100vw - 2 * var(--space-3)));
  border: 1px solid var(--color-muted);
  background: var(--color-screen-raised);
  color: var(--color-text);
}

.board-picker__list {
  max-height: min(60vh, 24rem);
  margin: 0;
  padding: var(--space-1) 0;
  overflow-y: auto;
  list-style: none;
}

/* A mark for the board in use, the name, and the microcontroller below it. */
.board-picker__option {
  display: grid;
  grid-template-columns: 2ch minmax(0, 1fr);
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-2);
  cursor: pointer;
}

.board-picker__option[aria-selected="true"] {
  color: var(--color-duck);
}

.board-picker__mark {
  visibility: hidden;
}

.board-picker__option[aria-selected="true"] .board-picker__mark {
  visibility: visible;
}

.board-picker__name {
  font-weight: 700;
  white-space: nowrap;
}

.board-picker__detail {
  grid-column: 2;
  color: var(--color-muted);
  font-size: var(--text-small);
}

/* The highlighted line is inverted, even when it is the board in use. */
.board-picker__option--active,
.board-picker__option--active[aria-selected="true"],
.board-picker__option--active .board-picker__detail {
  background: var(--color-text);
  color: var(--color-screen);
}

.board-picker__hint {
  margin: 0;
  padding: var(--space-1) var(--space-2);
  border-top: 1px solid var(--color-rule);
  color: var(--color-muted);
  font-size: var(--text-small);
}

/* Only where a keyboard is likely: on touch screens the keys in the hint do not exist. */
@media (hover: none) {
  .board-picker__hint {
    display: none;
  }
}

.status-bar__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
}

/* Pushed to the right end, like the right side of a tmux status line. */
.status-bar__about {
  margin-left: auto;
}

/* Buttons drawn as bracketed terminal actions, inverted on hover like a selected menu item. */

.terminal-button {
  /* Anchors the shortcut hint. */
  position: relative;
  min-height: var(--control-height);
  padding: 0 var(--space-2);
  border: 0;
  background: transparent;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}

.terminal-button::before {
  content: "[ ";
  color: var(--color-muted);
}

.terminal-button::after {
  content: " ]";
  color: var(--color-muted);
}

.terminal-button:hover:not(:disabled) {
  background: var(--color-text);
  color: var(--color-screen);
}

.terminal-button--accent {
  color: var(--color-duck);
}

.terminal-button--accent:hover:not(:disabled) {
  background: var(--color-duck);
  color: var(--color-duck-ink);
}

.terminal-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* A key combination drawn as rounded keycaps, in the button hints and in the help output. */

.shortcut {
  color: var(--color-muted);
  font-family: inherit;
  font-size: var(--text-small);
  font-weight: 400;
  white-space: nowrap;
}

.keycap {
  padding: 0 0.45em;
  border: 1px solid var(--color-muted);
  border-radius: 4px;
  color: var(--color-text);
  font: inherit;
}

/* A button's shortcut, shown below it on hover or keyboard focus. */
.shortcut-hint {
  position: absolute;
  top: calc(100% + var(--space-1));
  left: 50%;
  z-index: 1;
  display: none;
  padding: var(--space-2);
  border: 1px solid var(--color-rule);
  border-radius: 6px;
  background: var(--color-screen);
  line-height: 1.4;
  transform: translateX(-50%);
}

/* The about button sits at the window's edge, so its hint lines up with the button's right side. */
.status-bar__about .shortcut-hint {
  left: auto;
  right: 0;
  transform: none;
}

.terminal-button:focus-visible .shortcut-hint {
  display: block;
}

/* Only where a pointer can hover: on touch screens a tap would leave the hint stuck open. */
@media (hover: hover) {
  .terminal-button:hover:not(:disabled) .shortcut-hint {
    display: block;
  }
}

/* The toggle of an open pane stays highlighted, like the active window in a tmux status line. */
.terminal-button[aria-expanded="true"] {
  color: var(--color-duck);
}

/* Panes, split like a terminal: the conversation, and the board beside it while it is open.
   A hidden pane leaves the grid, so the conversation then takes the whole width. */

.panes {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-columns: min(var(--board-pane-width), 50%);
  grid-auto-flow: column;
  min-height: 0;
}

.pane--conversation {
  /* The duck and the transcript adapt to the pane's width, not the window's. */
  container: conversation / inline-size;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
}

.pane--board {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  border-left: 1px solid var(--color-rule);
}

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

.pane__command {
  margin: 0;
}

/* On narrow panes the actions move below the command, together, instead of stacking. */
.pane__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

.pane__body {
  overflow-y: auto;
  padding: var(--space-2) var(--space-4) var(--space-6);
}

/* Workspace: the scrolling transcript, with the duck in the bottom-right corner. */

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  min-height: 0;
}

.transcript {
  /* Positioned so that absolutely positioned descendants (screen reader labels) scroll
     with the transcript instead of stretching the page. */
  position: relative;
  overflow-y: auto;
  padding: var(--space-4) var(--space-4) var(--space-6);
}

.motd,
.lines {
  max-width: var(--transcript-width);
}

/* Message of the day: shown until the first request. */

.motd {
  display: grid;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.motd p {
  margin: 0;
}

.motd__title {
  color: var(--color-duck);
  font-weight: 700;
}

.motd__muted {
  color: var(--color-muted);
}

.motd__examples {
  display: grid;
  gap: var(--space-1);
  margin: 0;
  padding: 0;
  list-style: none;
}

.example-button {
  padding: 0 var(--space-1);
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.example-button__index {
  color: var(--color-duck);
}

.example-button:hover {
  background: var(--color-text);
  color: var(--color-screen);
}

/* Transcript lines. */

.lines {
  display: grid;
  /* minmax(0, 1fr) keeps long code lines from widening the column; the code scrolls instead. */
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-4);
  margin: 0;
  padding: 0;
  list-style: none;
}

.line {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.line--reply {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-2);
  white-space: normal;
}

.line__text {
  margin: 0;
}

.prompt-text__user {
  color: var(--color-user);
}

.prompt-text__path {
  color: var(--color-path);
}

.speaker {
  color: var(--color-duck);
  font-weight: 700;
}

.line__meta {
  margin: 0;
  color: var(--color-muted);
}

.line--pending {
  color: var(--color-muted);
}

.line--error {
  color: var(--color-error);
}

.line__error-label {
  font-weight: 700;
}

.thinking-dots::after {
  content: "...";
  display: inline-block;
  width: 0;
  overflow: hidden;
  white-space: nowrap;
  overflow-wrap: normal;
  vertical-align: bottom;
  animation: thinking-dots 1.2s steps(4) infinite;
}

@keyframes thinking-dots {
  to {
    width: 4ch;
  }
}

@media (prefers-reduced-motion: reduce) {
  .thinking-dots::after {
    width: auto;
  }
}

/* Generated code, framed like a file opened in the terminal. */

.code-block {
  margin: 0;
  border: 1px solid var(--color-rule);
  background: var(--color-screen-raised);
}

.code-block__header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding-left: var(--space-3);
  border-bottom: 1px solid var(--color-rule);
  font-size: var(--text-small);
  color: var(--color-muted);
}

.code-block__actions {
  display: flex;
  flex-wrap: wrap;
  margin-left: auto;
}

.code-block__wiring {
  color: var(--color-text);
}

.code-block__body {
  margin: 0;
  padding: var(--space-3);
  overflow-x: auto;
  line-height: 1.55;
  tab-size: 2;
}

/* Board view shown in the board pane by the pinout command. */

.board-view {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-2);
}

.board-view__title {
  margin: 0;
  color: var(--color-duck);
  font-weight: 700;
}

.board-diagram {
  margin: var(--space-2) 0;
  overflow-x: auto;
  color: var(--color-muted);
  font: inherit;
  line-height: 1.35;
}

.board-diagram__used {
  color: var(--color-duck);
}

/* Power, ground and reset: marked on the board before any code says what else to wire. */
.board-diagram__essential {
  color: var(--color-path);
}

/* A pin the code uses with nothing wired to it: usually a pin the model made up. */
.board-diagram__unlisted,
.board-view__warning {
  color: var(--color-error);
}

.board-view__warning {
  margin: 0;
}

/* A component the code moved to another pin, as the student asked: the wire has to move too. */
.board-view__notice {
  margin: 0;
  color: var(--color-beak);
}

/* One card per component a code version uses: its name, drawing and wiring text. */

.component-cards {
  display: grid;
  gap: var(--space-4);
  margin: var(--space-2) 0;
}

.component-card {
  display: grid;
  gap: var(--space-1);
}

.component-card__name {
  margin: 0;
  color: var(--color-duck);
  font-weight: 700;
}

.component-card__drawing {
  margin: 0;
  overflow-x: auto;
  font: inherit;
  line-height: 1.35;
}

.component-drawing__gpio {
  color: var(--color-duck);
}

/* Tables printed like command output: the wiring guide and the help. */

.terminal-table {
  width: 100%;
  border-collapse: collapse;
}

.terminal-table th,
.terminal-table td {
  padding: var(--space-1) var(--space-3);
  text-align: left;
  vertical-align: top;
}

/* Columns are split by vertical rules, like a table printed by psql; the outer edges stay open. */
.terminal-table :is(th, td) + :is(th, td) {
  border-left: 1px solid var(--color-rule);
}

.terminal-table :is(th, td):first-child {
  padding-left: 0;
}

.terminal-table :is(th, td):last-child {
  padding-right: 0;
}

.terminal-table th {
  border-bottom: 1px solid var(--color-rule);
  color: var(--color-muted);
  font-weight: 400;
  white-space: nowrap;
}

.terminal-table__key {
  color: var(--color-duck);
  white-space: nowrap;
}

/* The essential pins keep in the table the blue they have on the board. */
.terminal-table--essential .terminal-table__key {
  color: var(--color-path);
}

/* Output of the help command. */

.line--help {
  display: grid;
  gap: var(--space-2);
  white-space: normal;
}

/* Only as wide as its content, so each shortcut stays next to its command. */
.line--help .terminal-table {
  width: auto;
}

/* The rubber duck the student explains the problem to. */

.duck {
  align-self: end;
  width: 36ch;
  padding: var(--space-4);
  color: var(--color-duck);
}

.duck__speech {
  position: relative;
  /* Keeps one line of height while the text is still being typed. */
  min-height: calc(var(--line-height) * 1em + 2 * var(--space-2) + 2px);
  margin: 0 0 var(--space-6);
  padding: var(--space-2) var(--space-3);
  border: 1px dashed var(--color-duck);
  color: var(--color-text);
  font-size: var(--text-small);
}

.duck__speech::after {
  content: "/";
  position: absolute;
  left: 11ch;
  bottom: -1.5em;
  color: var(--color-duck);
}

.duck__art,
.duck__mini {
  margin: 0;
  font: inherit;
  line-height: 1.15;
  white-space: pre;
}

.duck__art .duck__water {
  color: var(--color-water);
}

.duck__mini {
  display: none;
}

/* Prompt line where the student types. */

.prompt {
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--color-rule);
}

.prompt:focus-within {
  border-top-color: var(--color-duck);
}

.prompt__line {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: 1ch;
  align-items: start;
}

.prompt__label {
  white-space: nowrap;
}

.prompt__input {
  min-height: calc(var(--line-height) * 1em);
  max-height: 40vh;
  padding: 0;
  border: 0;
  background: transparent;
  line-height: var(--line-height);
  caret-color: var(--color-duck);
  resize: none;
}

/* The prompt line shows focus through the highlighted border above it. */
.prompt__input:focus-visible {
  outline: none;
}

.prompt__actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-2);
}

.prompt__hint {
  margin-right: auto;
  font-size: var(--text-small);
  color: var(--color-muted);
}

/* Dialogs, framed in the duck's yellow over a dimmed screen. */

.dialog {
  width: min(54ch, 100% - 2 * var(--space-4));
  padding: var(--space-4);
  border: 1px solid var(--color-duck);
  background: var(--color-screen-raised);
  color: var(--color-text);
}

.dialog::backdrop {
  background: rgb(0 0 0 / 0.6);
}

.dialog__form {
  display: grid;
  gap: var(--space-3);
}

.dialog__form p {
  margin: 0;
}

.dialog__title {
  color: var(--color-duck);
  font-weight: 700;
}

.dialog__actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-2);
}

.dialog__hint {
  margin-right: auto;
  font-size: var(--text-small);
  color: var(--color-muted);
  white-space: nowrap;
}

/* About dialog: the command that "printed" it, a banner, then the version and the credits. */

.dialog--about {
  width: min(76ch, 100% - 2 * var(--space-4));
  padding: var(--space-6);
  box-shadow: 0 0 48px rgb(246 201 69 / 0.18);
}

.about {
  /* The banner scales with this box, so it fits any dialog width. */
  container-type: inline-size;
  display: grid;
  gap: var(--space-3);
}

.about__banner {
  margin: var(--space-2) 0 0;
  /* One font for the blocks, the lines and the spaces between them, so the columns line up. */
  font-family: ui-monospace, Menlo, Consolas, "DejaVu Sans Mono", monospace;
  /* 66 columns of about 0.6em each, never larger than the body text. */
  font-size: min(var(--text-base), calc(100cqi / 40));
  line-height: 1.1;
  white-space: pre;
  background: linear-gradient(180deg, var(--color-duck), var(--color-beak));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.about__version {
  color: var(--color-muted);
}

.about__details {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: var(--space-1) var(--space-3);
  margin: 0;
}

.about__label {
  color: var(--color-muted);
}

.about__value {
  margin: 0;
}

.about__link {
  color: var(--color-path);
}

.about__copyright {
  color: var(--color-muted);
  font-size: var(--text-small);
}

/* Narrow conversation pane, on a small screen or beside the board: the duck shrinks to one line
   above the prompt. */

@container conversation (max-width: 64rem) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .duck {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    width: auto;
    padding: var(--space-2) var(--space-4);
    border-top: 1px dashed var(--color-rule);
  }

  .duck__art {
    display: none;
  }

  .duck__mini {
    display: block;
    order: -1;
  }

  .duck__speech {
    min-height: calc(var(--line-height) * 1em);
    margin: 0;
    padding: 0;
    border: 0;
  }

  .duck__speech::after {
    content: none;
  }
}

/* Small screens have no room for two panes: the board pane takes the screen until closed. */
@media (max-width: 64rem) {
  .panes:has(> .pane--board:not([hidden])) > .pane--conversation {
    display: none;
  }

  .pane--board {
    border-left: 0;
  }
}

@media (max-width: 40rem) {
  :root {
    --text-base: 0.875rem;
  }

  .status-bar__session,
  .prompt__hint,
  .dialog__hint {
    display: none;
  }

  .status-bar {
    padding: var(--space-2) var(--space-3) 0;
  }

  /* The board choice shares the first row with the about button; the actions go below. The
     list of boards takes the width of that row instead of growing past the screen edge. */
  .board-field {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
  }

  .board-picker {
    position: static;
    flex: 1;
    min-width: 0;
  }

  .board-picker__popup {
    right: 0;
    width: auto;
    max-width: none;
  }

  .status-bar__actions {
    order: 1;
    flex: 1 1 100%;
  }

  .status-bar__about {
    margin-left: 0;
  }
}
