/* defgen playground.
 *
 * One stylesheet, no framework, no web fonts: the page is four rows — header,
 * options, two panes, status — and the panes are what scrolls, not the page.
 */

:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-alt: #f0f2f5;
  --border: #d8dde5;
  --border-strong: #c2c9d4;
  --text: #16191d;
  --text-dim: #5b636e;
  --accent: #2f6feb;
  --accent-text: #ffffff;
  --accent-soft: #e5eefd;
  --error: #c0362c;
  --error-soft: #fbeceb;
  --warning: #8a5a00;
  --warning-soft: #fdf3e0;
  --ok: #1a7f47;

  --syn-keyword: #a626a4;
  --syn-string: #50a14f;
  --syn-number: #986801;
  --syn-comment: #a0a1a7;
  --syn-type: #0184bc;
  --syn-attribute: #4078f2;

  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  /* The gutter's line boxes have to line up with the textarea's to the pixel,
   * so both read these rather than each rounding a unitless line-height. */
  --code-size: 13px;
  --code-line: 20px;

  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-alt: #1e242c;
    --border: #2b323c;
    --border-strong: #3a424e;
    --text: #e6e9ee;
    --text-dim: #98a2b0;
    --accent: #5b8dff;
    --accent-text: #0f1216;
    --accent-soft: #1b2740;
    --error: #ff8a80;
    --error-soft: #2a1a1a;
    --warning: #ffc773;
    --warning-soft: #2a2214;
    --ok: #5dd393;

    --syn-keyword: #c678dd;
    --syn-string: #98c379;
    --syn-number: #d19a66;
    --syn-comment: #7f848e;
    --syn-type: #61afef;
    --syn-attribute: #56b6c2;
  }
}

* {
  box-sizing: border-box;
}

/* Several things here are laid out with `display`, which would otherwise beat
 * the user agent's rule for `[hidden]` on specificity alone. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
}

code,
kbd,
pre,
textarea,
.mono {
  font-family: var(--mono);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

/* ------------------------------------------------------------------ header */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.topbar h1 {
  margin: 0;
  font-family: var(--mono);
  font-size: 18px;
  letter-spacing: -0.01em;
}

.chip-static {
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tagline {
  flex: 1 1 320px;
  margin: 0;
  color: var(--text-dim);
  font-size: 13px;
}

.tagline code {
  font-size: 12px;
}

.topbar nav {
  display: flex;
  gap: 14px;
  font-size: 13px;
}

a {
  color: var(--accent);
}

/* ----------------------------------------------------------------- toolbar */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 12px 28px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.field {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.field legend {
  padding: 0 0 5px;
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.field-backends {
  flex: 1 1 380px;
}

.field-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  margin-left: auto;
}

.hint {
  margin: 5px 0 0;
  color: var(--text-dim);
  font-size: 11.5px;
}

.field-actions .hint {
  text-align: center;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chips label {
  position: relative;
  display: inline-flex;
  padding: 4px 11px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 12.5px;
  cursor: pointer;
  user-select: none;
}

.chips input {
  position: absolute;
  opacity: 0;
  inset: 0;
  cursor: pointer;
}

.chips label:hover {
  border-color: var(--accent);
}

.chips label:has(input:checked) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.chips label:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.stem-input {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
}

.stem-input:focus-within {
  border-color: var(--accent);
}

.stem-input input {
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  font-family: var(--mono);
  font-size: 13px;
}

.stem-input input:focus {
  outline: none;
}

.stem-input .suffix {
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 13px;
}

.toggles {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12.5px;
}

.toggles label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.toggles code {
  color: var(--text-dim);
  font-size: 11.5px;
}

button {
  padding: 5px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: inherit;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

button.primary {
  padding: 7px 22px;
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.08);
  color: var(--accent-text);
}

select {
  padding: 3px 6px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: inherit;
  font: inherit;
  font-size: 12.5px;
  max-width: 100%;
}

/* ------------------------------------------------------------------- panes */

main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  padding: 12px 20px;
  min-height: 0;
}

.pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.pane-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  min-height: 40px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
}

.pane-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.pane-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.inline-select {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
}

.tabs button {
  border: 0;
  border-radius: 6px;
  background: none;
  padding: 4px 10px;
  color: var(--text-dim);
  font-size: 12.5px;
  white-space: nowrap;
}

.tabs button[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: inset 0 0 0 1px var(--border);
}

/* ------------------------------------------------------------------ editor */

.editor {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
  font-size: var(--code-size);
  line-height: var(--code-line);
}

.gutter {
  padding: 10px 8px 10px 12px;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-line);
  text-align: right;
  white-space: pre;
  user-select: none;
}

/* The highlighted schema and the textarea that edits it sit in the same box,
 * one on top of the other: the textarea's own text is transparent, so what a
 * typist sees is the `pre` underneath, and what they edit is the (invisible)
 * text above it. Padding, font and line-height must match exactly or the
 * caret drifts from the letter it is next to. */
.code-input {
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.highlight,
#source {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 10px 12px;
  border: 0;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-line);
  white-space: pre;
  tab-size: 4;
}

.highlight {
  overflow: hidden;
  pointer-events: none;
  color: var(--text);
}

.highlight code {
  font: inherit;
}

#source {
  background: none;
  color: transparent;
  caret-color: var(--text);
  -webkit-text-fill-color: transparent;
  resize: none;
  overflow: auto;
}

#source:focus {
  outline: none;
}

/* ------------------------------------------------------------------ result */

.result {
  min-height: 0;
  overflow: auto;
}

.result pre {
  margin: 0;
  padding: 10px 12px;
  font-size: var(--code-size);
  line-height: var(--code-line);
  tab-size: 4;
}

.tok-keyword {
  color: var(--syn-keyword);
}

.tok-string {
  color: var(--syn-string);
}

.tok-number {
  color: var(--syn-number);
}

.tok-comment {
  color: var(--syn-comment);
  font-style: italic;
}

.tok-type {
  color: var(--syn-type);
}

.tok-attribute {
  color: var(--syn-attribute);
}

.result .placeholder {
  display: grid;
  place-content: center;
  height: 100%;
  padding: 24px;
  color: var(--text-dim);
  text-align: center;
}

/* Diagnostics */

.diagnostics {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
}

.diagnostic {
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: 6px;
  overflow: hidden;
}

.diagnostic.error {
  border-left-color: var(--error);
}

.diagnostic.warning {
  border-left-color: var(--warning);
}

.diagnostic-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  padding: 7px 10px;
  border: 0;
  border-radius: 0;
  background: none;
  text-align: left;
  font-size: 13px;
  line-height: 1.4;
}

.diagnostic-head:hover {
  background: var(--surface-alt);
  color: inherit;
}

.badge {
  flex: none;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.error .badge {
  background: var(--error-soft);
  color: var(--error);
}

.warning .badge {
  background: var(--warning-soft);
  color: var(--warning);
}

.diagnostic-where {
  flex: none;
  margin-left: auto;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11.5px;
}

.diagnostic pre {
  margin: 0;
  padding: 8px 10px 10px;
  border-top: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 12px;
  /* Ariadne draws its labels out of box-drawing characters, which only join up
   * when a line box is exactly as tall as the glyphs in it. */
  line-height: 1;
  overflow-x: auto;
}

/* Schema summary */

.summary {
  padding: 12px;
}

.summary h3 {
  margin: 18px 0 8px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.summary h3:first-child {
  margin-top: 0;
}

.facts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.fact {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-alt);
  font-size: 12.5px;
}

.fact b {
  font-family: var(--mono);
  font-weight: 600;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

th {
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
}

td {
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

tbody tr:last-child td {
  border-bottom: 0;
}

td.name,
td.size,
td.uuid {
  font-family: var(--mono);
}

td.uuid,
td.size {
  color: var(--text-dim);
  white-space: nowrap;
}

td.uuid {
  font-size: 11px;
}

.tag {
  display: inline-block;
  margin-right: 4px;
  padding: 0 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.service-name {
  margin: 14px 0 6px;
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
}

.service-name span {
  margin-left: 6px;
  color: var(--text-dim);
  font-size: 11.5px;
  font-weight: 400;
}

/* --------------------------------------------------------------- statusbar */

.statusbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 6px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 12.5px;
}

.status {
  margin: 0;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status.ok {
  color: var(--ok);
}

.status.bad {
  color: var(--error);
}

.status-right {
  margin: 0 0 0 auto;
  color: var(--text-dim);
  white-space: nowrap;
}

kbd {
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface-alt);
  font-size: 10.5px;
}

/* ------------------------------------------------------------------ device */

.device {
  min-height: 0;
  padding: 12px;
  overflow: auto;
}

.device-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin-bottom: 8px;
}

.device-status {
  margin: 0;
  color: var(--text-dim);
  font-size: 12.5px;
}

.device-status.ok {
  color: var(--ok);
}

.device-status.bad {
  color: var(--error);
}

.device-status.info {
  color: var(--accent);
}

.device-hint {
  margin: 0 0 14px;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.5;
}

.device-hint.bad {
  color: var(--error);
}

.device-services {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.gatt-service {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.service-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 600;
}

.service-uuid {
  color: var(--text-dim);
  font-weight: 400;
  font-size: 11px;
}

.service-chars {
  display: flex;
  flex-direction: column;
}

.char {
  padding: 10px;
  border-top: 1px solid var(--border);
}

.char:first-child {
  border-top: 0;
}

.char-unknown {
  background: var(--surface-alt);
}

.char-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.char-name {
  font-weight: 600;
  font-size: 13px;
}

.char-uuid {
  color: var(--text-dim);
  font-size: 11px;
}

.badge.matched {
  background: var(--ok);
  color: var(--accent-text);
}

.badge.unmatched {
  background: var(--surface-alt);
  color: var(--text-dim);
  box-shadow: inset 0 0 0 1px var(--border-strong);
}

.prop-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-left: auto;
}

.prop-badge {
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 10.5px;
  font-weight: 600;
}

.char-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.char-btn.active {
  border-color: var(--accent);
  color: var(--accent);
}

.char-result {
  margin-bottom: 8px;
  font-size: 12.5px;
}

.char-result:empty {
  margin-bottom: 0;
}

.result-line {
  margin: 0;
}

.result-line.ok {
  color: var(--ok);
}

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

.value-hex {
  display: flex;
  gap: 6px;
  margin-bottom: 4px;
  font-family: var(--mono);
  font-size: 12px;
}

.value-hex-label {
  color: var(--text-dim);
}

.value-decode-error {
  margin: 0;
  color: var(--warning);
  font-size: 12px;
}

.value-decoded {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-alt);
}

.value-fields {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 10px;
  margin: 0;
  font-size: 12px;
}

.value-fields dt {
  color: var(--text-dim);
}

.value-fields dd {
  margin: 0;
}

.value-array {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.value-array-item {
  display: flex;
  gap: 6px;
  font-size: 12px;
}

.value-index {
  color: var(--text-dim);
}

.value-union {
  font-size: 12px;
}

.value-union-variant {
  font-weight: 600;
}

.value-union-raw {
  color: var(--text-dim);
  font-family: var(--mono);
}

.value-enum-unknown {
  color: var(--warning);
}

.value-num,
.value-scaled {
  font-family: var(--mono);
}

.char-write {
  margin-top: 6px;
}

.mode-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.mode-tab {
  padding: 3px 9px;
  border: 0;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--text-dim);
  font-size: 11.5px;
}

.mode-tab.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.write-pane {
  margin-bottom: 6px;
}

.hex-input,
.num-input,
.str-input {
  width: 100%;
  max-width: 260px;
  padding: 4px 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: inherit;
  font: inherit;
  font-size: 12.5px;
}

.hex-input {
  max-width: 100%;
  font-family: var(--mono);
}

.struct-editor {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-alt);
}

.struct-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.struct-field.reserved {
  opacity: 0.65;
}

.field-name {
  min-width: 110px;
  color: var(--text-dim);
}

.array-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.array-editor {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.array-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.array-add,
.array-remove {
  align-self: flex-start;
  padding: 2px 8px;
  font-size: 11.5px;
}

.enum-editor,
.union-editor {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-start;
}

.union-body:not(:empty) {
  padding-left: 4px;
  border-left: 2px solid var(--border);
}

.notify-log {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 220px;
  padding: 6px 8px;
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-alt);
  overflow-y: auto;
}

.notify-entry {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
  font-size: 11.5px;
}

.notify-entry:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.notify-time {
  flex: none;
  color: var(--text-dim);
  font-family: var(--mono);
}

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

@media (max-width: 940px) {
  body {
    height: auto;
    min-height: 100dvh;
  }

  main {
    grid-template-columns: minmax(0, 1fr);
  }

  .pane {
    height: 70vh;
  }

  .field-actions {
    margin-left: 0;
  }

  /* The pane heads carry a tab strip and up to three controls, which is more
   * than fits on a phone in one row. */
  .pane-head {
    flex-wrap: wrap;
  }

  .pane-actions {
    margin-left: 0;
  }

  .inline-select select {
    max-width: 150px;
  }
}
