* {
  box-sizing: border-box;
}

:root {
  color-scheme: light;
  --background: #f6f7fb;
  --panel: #ffffff;
  --text: #182033;
  --muted: #5d667a;
  --border: #d8deea;
  --accent: #1769e0;
  --accent-dark: #0f53b5;
  --danger: #b42318;
  --danger-bg: #fff1f0;
  --radius: 10px;
}

body {
  margin: 0;
  background: var(--background);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.app {
  width: min(1120px, calc(100% - 16px));
  margin: 16px auto;
}

.app-header {
  margin-bottom: 10px;
}

.app-header-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px 14px;
  margin-bottom: 6px;
}

.app-header-title-row h1 {
  margin: 0;
}

.app-title {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem;
}

.view-tab {
  padding: 0 0 3px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.view-tab:hover,
.view-tab:focus-visible {
  color: var(--accent-dark);
  background: transparent;
}

.view-tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

button.about-button {
  padding: 4px 10px;
  font-size: 0.85rem;
  font-weight: 600;
}

h1 {
  margin: 0 0 3px;
  font-size: clamp(1.45rem, 3vw, 2.1rem);
  line-height: 1.1;
}

p {
  max-width: 860px;
  margin: 0;
  color: var(--muted);
}

.about-dialog {
  max-width: min(40rem, calc(100vw - 32px));
  margin: auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(24, 32, 51, 0.22);
}

.about-dialog::backdrop {
  background: rgba(24, 32, 51, 0.38);
}

.about-dialog-inner {
  padding: 16px 18px 14px;
}

.about-dialog-inner h2 {
  margin: 0 0 12px;
  font-size: 1.2rem;
  line-height: 1.25;
  color: var(--text);
}

.about-dialog-inner > p {
  margin: 0 0 12px;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--muted);
  max-width: none;
}

.about-dialog-inner ul {
  margin: 0 0 4px;
  padding-left: 1.25em;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text);
}

.about-dialog-inner li {
  margin-bottom: 8px;
}

.about-dialog-inner li strong {
  color: #26324a;
}

.about-dialog-inner code {
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.88em;
  background: #f0f3f9;
  padding: 1px 5px;
  border-radius: 4px;
}

.about-dialog-inner form {
  margin-top: 14px;
  margin-bottom: 0;
}

.panel {
  display: grid;
  gap: 11px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 18px 40px rgba(24, 32, 51, 0.08);
}

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 8px;
  align-items: start;
}

.field {
  display: grid;
  grid-column: span 2;
  gap: 3px;
}

.field-wide {
  grid-column: span 3;
}

/* 4-byte hex inputs — span 2 so a full row fits in 12 columns */
.field-hex {
  grid-column: span 2;
}

.field-full {
  grid-column: 1 / -1;
}

.field-inline {
  display: flex;
  grid-column: span 2;
  flex-wrap: wrap;
  align-content: start;
  gap: 4px 7px;
  padding: 0;
  border: 0;
}

.field-inline legend,
.message-type legend,
.field span {
  color: #26324a;
  font-size: 0.8rem;
  font-weight: 700;
}

.field-inline legend,
.message-type legend {
  width: 100%;
  padding: 0;
  margin-bottom: 0;
}

.message-type {
  display: flex;
  flex-wrap: wrap;
  gap: 5px 11px;
  padding: 0;
  border: 0;
}

.message-type legend {
  margin-bottom: 3px;
}

.message-type-row {
  grid-column: 1 / -1;
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  align-content: flex-start;
  justify-content: flex-start;
  gap: 10px 20px;
  padding-top: 12px;
  margin-top: 2px;
  border-top: 1px solid var(--border);
}

/* Each block: shared minimum width so rows wrap as the panel narrows */
.message-type-row > * {
  flex: 1 1 12rem;
  max-width: 100%;
}

.message-type-row > .message-type {
  flex: 1 1 16rem;
}

.message-type-row .size-inline {
  display: grid;
  grid-column: unset;
  gap: 3px;
}

input.input-reply-wait {
  width: 6.5rem;
  max-width: 100%;
}

label {
  color: var(--text);
}

input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  padding: 2px 7px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  font: inherit;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

input.input-device {
  width: 5ch;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

input.input-hex-quad {
  width: 14ch;
  max-width: 100%;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

/* Paired numeric fields: canonical hex above, decimal equivalent below. */
.field-dual {
  grid-column: span 2;
}

.dual-inputs {
  display: grid;
  gap: 5px;
  width: min-content;
  max-width: 100%;
}

.dual-subfield {
  display: flex;
  align-items: center;
  gap: 4px;
}

.dual-subfield span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1;
  min-width: 1ch;
}

input.input-decimal {
  width: 14ch;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

/* Stack model ID input + preset; same column width and aligned edges (native select inset removed). */
.model-id-row {
  display: grid;
  gap: 5px;
  width: min-content;
  max-width: 100%;
}

.model-id-row .input-hex-quad {
  width: 100%;
  min-width: 18ch;
}

select.model-id-preset {
  width: 100%;
  min-width: 18ch;
  margin: 0;
  padding: 2px 1.6rem 2px 7px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%235d667a' d='M2.2%204.2%20L6%208.3%20L9.8%204.2z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.45rem center;
  background-size: 0.55rem;
  cursor: pointer;
  text-indent: 0;
  font-family: inherit;
  font-size: 0.7rem;
  font-weight: 700;
}

input.input-count {
  width: 7ch;
}

input[type="radio"] {
  width: auto;
  margin: 0 3px 0 0;
}

textarea {
  height: 4em;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(23, 105, 224, 0.15);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  grid-column: 1 / -1;
  width: 100%;
  max-width: 100%;
}

button {
  padding: 5px 9px;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 7px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:hover,
button:focus-visible {
  background: var(--accent-dark);
}

button.secondary {
  color: var(--accent);
  background: #edf4ff;
}

button.secondary:hover,
button.secondary:focus-visible {
  color: var(--accent-dark);
  background: #ddeaff;
}

button:disabled {
  cursor: wait;
  opacity: 0.7;
}

.errors {
  display: none;
  padding: 6px 7px;
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid #ffcdc7;
  border-radius: 7px;
  white-space: pre-line;
}

.errors:not(:empty) {
  display: block;
}

.hidden {
  display: none;
}

#midi-status {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
}

.midi-panel {
  display: grid;
  gap: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.midi-panel h2 {
  margin: 0 0 2px;
  font-size: 1.05rem;
}

.midi-ports-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px 16px;
}

.midi-ports-row > .field-wide {
  flex: 1 1 14rem;
  max-width: 100%;
  min-width: 0;
}

.midi-ports-row .midi-raw-cell {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
}

.reply-output {
  display: grid;
  gap: 6px;
}

.reply-card {
  display: grid;
  gap: 4px;
  padding: 7px;
  background: #f9fbff;
  border: 1px solid var(--border);
  border-radius: 7px;
}

.reply-card pre {
  margin: 0;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

.reply-detail {
  display: grid;
  gap: 2px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.88rem;
}

.calculator-panel {
  gap: 12px;
}

.calculator-panel h2 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--text);
}

#calculator-app {
  display: flex;
  gap: 12px;
  align-items: stretch;
  min-height: 0;
}

#calc-col {
  flex: 0 0 auto;
}

#tape-col {
  flex: 1 1 180px;
  min-width: 160px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

#tape-header {
  display: flex;
  align-items: center;
  gap: 5px;
}

#tape-label {
  flex: 1;
  color: #26324a;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.calc-section {
  width: fit-content;
  max-width: 100%;
  padding: 10px 11px;
  background: #f9fbff;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.calc-section h3 {
  margin: 0 0 7px;
  padding-bottom: 5px;
  color: #26324a;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.calc-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 5px;
}

.row-label {
  width: 80px;
  flex-shrink: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.sysex-in,
.dec-in,
.count-in {
  height: 28px;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.87rem;
  font-weight: 600;
}

.sysex-in {
  width: 112px;
  text-transform: uppercase;
}

.dec-in {
  width: 94px;
}

.count-in {
  width: 56px;
}

.sysex-in[readonly],
.dec-in[readonly] {
  color: var(--accent-dark);
  background: #edf4ff;
  cursor: default;
}

.pipe {
  flex-shrink: 0;
  color: var(--border);
  font-size: 0.85rem;
}

.arr-label,
.count-wrap {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 0.82rem;
}

.arr-label {
  margin-left: 2px;
  cursor: pointer;
  user-select: none;
}

.arr-label input[type="checkbox"] {
  width: 13px;
  height: 13px;
  cursor: pointer;
  accent-color: var(--accent);
}

.arr-label--locked {
  cursor: default;
  opacity: 0.9;
}

.arr-label--locked input[type="checkbox"] {
  cursor: not-allowed;
  opacity: 0.85;
}

.op-choice {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.op-btn {
  position: relative;
  display: inline-block;
  cursor: pointer;
  user-select: none;
}

.op-btn input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
  opacity: 0;
}

.op-btn-face {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 28px;
  padding: 0 14px;
  color: var(--accent);
  background: #edf4ff;
  border: 1px solid #cfe0fb;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  pointer-events: none;
}

.op-btn:hover .op-btn-face {
  color: var(--accent-dark);
  background: #ddeaff;
}

.op-btn input:checked + .op-btn-face {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.op-btn:focus-within .op-btn-face {
  outline: 2px solid rgba(23, 105, 224, 0.35);
  outline-offset: 2px;
}

hr.div {
  margin: 6px 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.result-row .row-label {
  color: var(--accent-dark);
  font-weight: 700;
}

.calc-btn,
.chain-btn,
.copy-btn {
  border-radius: 4px;
  font-family: inherit;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.calc-btn {
  height: 28px;
  padding: 4px 13px;
  font-size: 0.78rem;
}

.replace-val-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding-left: 86px;
}

.chain-btn,
.copy-btn {
  color: var(--accent);
  background: #edf4ff;
  border: 1px solid #cfe0fb;
}

.chain-btn {
  width: fit-content;
  padding: 5px 12px;
  font-size: 0.72rem;
}

.copy-btn {
  padding: 3px 9px;
  font-size: 0.7rem;
  white-space: nowrap;
}

.chain-btn:hover,
.copy-btn:hover {
  color: var(--accent-dark);
  background: #ddeaff;
}

.copy-btn.copied {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

#tickertape {
  flex: 1;
  min-height: 180px;
  width: 100%;
  padding: 8px 10px;
  resize: vertical;
  line-height: 1.55;
}

.err,
.rem-mod {
  color: var(--danger);
  font-size: 0.72rem;
  line-height: 1.35;
}

.err {
  min-height: 1.1em;
  margin-top: 2px;
}

.rem-mod {
  margin-top: 4px;
}

.rem-mod:empty {
  display: none;
}

@media (max-width: 920px) {
  .grid {
    grid-template-columns: repeat(6, 1fr);
  }

  .field,
  .field-wide,
  .field-inline,
  .field-hex,
  .field-dual {
    grid-column: span 3;
  }
}

@media (max-width: 640px) {
  .app {
    width: min(100% - 10px, 1120px);
    margin: 9px auto;
  }

  .panel {
    padding: 8px;
  }

  .grid {
    grid-template-columns: 1fr;
  }

  .field,
  .field-wide,
  .field-inline,
  .field-hex,
  .field-dual {
    grid-column: 1 / -1;
  }

  .dual-inputs {
    width: 100%;
    max-width: 100%;
  }

  #calculator-app {
    flex-direction: column;
    align-items: stretch;
  }

  #calc-col,
  #tape-col,
  .calc-section {
    width: 100%;
  }

  .calc-row {
    flex-wrap: wrap;
  }

  .replace-val-wrap {
    padding-left: 0;
  }
}
