:root {
  --bg: #f5f7f8;
  --panel: #ffffff;
  --panel-soft: #f0f4f2;
  --text: #17201c;
  --muted: #65716b;
  --line: #d9e0dc;
  --accent: #146c54;
  --accent-strong: #0f5a46;
  --accent-soft: #dff1eb;
  --warn: #b45a19;
  --danger: #b42318;
  --danger-soft: #ffe5e1;
  --shadow: 0 18px 48px rgba(23, 32, 28, 0.08);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  margin: 0;
  background:
    linear-gradient(180deg, rgba(223, 241, 235, 0.6), transparent 260px),
    var(--bg);
  color: var(--text);
}

.toast-region {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 50%;
  display: grid;
  width: min(440px, calc(100vw - 24px));
  padding-top: 10px;
  pointer-events: none;
  transform: translateX(-50%);
}

.toast {
  display: grid;
  gap: 2px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 16px 36px rgba(23, 32, 28, 0.18);
  color: var(--text);
  font-size: 13px;
  font-weight: 750;
  opacity: 0;
  transform: translateY(-130%);
  transition:
    opacity 160ms ease,
    transform 180ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

.toast.success {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.toast.error {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
}

button,
input,
select,
textarea {
  min-width: 0;
  font: inherit;
}

button {
  min-height: 38px;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  outline: none;
}

input,
select {
  height: 40px;
  padding: 0 12px;
}

textarea {
  resize: vertical;
  padding: 12px;
  line-height: 1.65;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(20, 108, 84, 0.16);
}

label {
  display: grid;
  gap: 7px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.file-input {
  display: none;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 24px clamp(16px, 3vw, 34px) 14px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1;
}

.header-actions,
.transport-row,
.insert-row,
.duration-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.device-serial {
  display: flex;
  flex: 1 1 250px;
  align-items: center;
  gap: 8px;
  min-width: min(100%, 250px);
}

.device-badge {
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  min-width: 112px;
  max-width: min(42vw, 230px);
  padding: 8px 12px;
  border: 2px solid #c7d1cc;
  border-radius: 6px;
  background: #17201c;
  color: #fff;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  font-weight: 850;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-badge::before {
  content: "";
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #9aa7a1;
  box-shadow: 0 0 0 3px rgba(154, 167, 161, 0.2);
}

.device-badge.success {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.device-badge.success::before,
.device-badge.connected::before {
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(20, 108, 84, 0.18);
}

.device-badge.connected {
  border-color: var(--accent);
  background: #fff;
  color: var(--accent-strong);
}

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

.device-badge.error::before {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(180, 35, 24, 0.16);
}

.primary,
.secondary,
.danger,
.icon-button {
  padding: 0 14px;
}

.primary {
  background: var(--accent);
  color: #fff;
}

.primary:hover {
  background: var(--accent-strong);
}

.secondary,
.icon-button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
}

.secondary:hover,
.icon-button:hover {
  background: var(--panel-soft);
}

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

.compact {
  min-height: 32px;
  padding: 0 12px;
}

.icon-button {
  width: 34px;
  min-height: 34px;
  padding: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(210px, 260px) minmax(0, 1fr) minmax(280px, 360px);
  gap: 16px;
  align-items: start;
  padding: 0 clamp(16px, 3vw, 34px) 28px;
}

.panel,
.input-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.panel {
  min-width: 0;
  padding: 16px;
}

.series-panel,
.export-panel {
  position: sticky;
  top: 16px;
}

.section-title {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  min-height: 34px;
  margin-bottom: 10px;
  font-weight: 850;
}

.section-title > * {
  min-width: 0;
}

.section-title.with-action {
  margin-top: 18px;
}

.compact-title {
  min-height: 24px;
  margin-bottom: 6px;
}

.hint {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.series-panel {
  display: grid;
  gap: 12px;
}

.series-json-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.series-json-actions button {
  flex: 1 1 84px;
}

.song-list {
  display: grid;
  gap: 8px;
}

.song-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  min-height: 58px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  text-align: left;
}

.song-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.song-item-main {
  min-width: 0;
  min-height: 0;
  padding: 0;
  background: transparent;
  text-align: left;
}

.song-item-name {
  overflow: hidden;
  color: var(--text);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.song-item-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.song-storage-meta {
  margin-top: 2px;
  color: var(--text);
  font-weight: 700;
}

.song-storage-meta.warning {
  color: var(--danger);
}

.song-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: flex-end;
}

.mini-action {
  width: 30px;
  min-height: 30px;
  padding: 0;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
}

.mini-action.mcu-action {
  width: 42px;
  font-size: 11px;
}

.editor-panel {
  display: grid;
  gap: 14px;
  min-width: 0;
}

.editor-toolbar {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) 86px minmax(96px, 128px);
  gap: 12px;
  align-items: end;
}

.editor-toolbar > *,
.composer-grid > *,
.nfc-toolbar > *,
.sequence-block,
.sequence-editor {
  min-width: 0;
}

.song-tools {
  display: flex;
  flex-wrap: wrap;
  grid-column: 1 / -1;
  gap: 8px;
  justify-content: flex-end;
}

.transpose-amount {
  margin-top: 12px;
  width: 112px;
}

.composer-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 0.8fr);
  gap: 12px;
}

.input-card {
  min-width: 0;
  padding: 14px;
  box-shadow: none;
}

.nfc-card {
  display: grid;
  gap: 12px;
}

.nfc-toolbar {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) max-content;
  gap: 12px;
  align-items: end;
}

.nfc-toolbar button {
  justify-self: end;
  min-width: 96px;
  min-height: 40px;
  padding: 0 12px;
}

.nfc-help {
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  line-height: 1.55;
}

.nfc-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 12px;
}

.nfc-fields:empty {
  display: none;
}

.nfc-span {
  grid-column: 1 / -1;
}

.hint.warning {
  color: var(--danger);
  font-weight: 800;
}

.insert-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto;
}

.duration-row {
  margin-top: 10px;
}

.duration-button {
  min-width: 58px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
}

.duration-button.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.volume-control {
  margin-top: 12px;
}

.volume-control input {
  padding: 0;
}

.sequence-editor {
  position: relative;
  display: block;
}

.sequence-highlights {
  position: absolute;
  inset: 0;
  overflow: hidden;
  padding: 12px;
  border: 1px solid transparent;
  color: var(--text);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 14px;
  line-height: 1.65;
  pointer-events: none;
  white-space: pre-wrap;
  word-break: break-word;
}

.sequence-highlights mark {
  border-radius: 4px;
  background: #ffd8d3;
  color: var(--danger);
  font-weight: 800;
}

.sequence-block textarea {
  position: relative;
  z-index: 1;
  min-height: 156px;
  background: transparent;
  caret-color: var(--text);
  color: transparent;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 14px;
}

.sequence-block textarea::placeholder {
  color: var(--muted);
}

.validation {
  min-height: 22px;
  margin: -6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.validation.error {
  color: var(--danger);
  font-weight: 700;
}

.sequence-derived {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.jianpu-sequence {
  display: grid;
  gap: 8px;
  min-height: 52px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.jianpu-line {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  min-height: 52px;
}

.jianpu-line:empty::before {
  content: "";
  display: block;
  min-height: 44px;
}

.jianpu-event {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 32px;
  padding: 0;
}

.jianpu-note-cell {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  min-width: 30px;
  min-height: 18px;
  gap: 1px;
}

.jianpu-run {
  display: inline-grid;
  grid-template-rows: 32px 8px 6px;
  justify-items: center;
  gap: 2px;
}

.jianpu-run-notes {
  display: inline-flex;
  align-items: flex-end;
  justify-content: center;
  justify-self: center;
  gap: 0;
  height: 32px;
  width: max-content;
}

.jianpu-run-lines {
  display: grid;
  gap: 2px;
  justify-self: stretch;
  min-height: 0;
  width: 100%;
}

.jianpu-run-lines span {
  height: 2px;
  border-radius: 2px;
  background: currentColor;
}

.jianpu-run.underline-0 .jianpu-run-lines {
  visibility: hidden;
}

.jianpu-run-low-dots {
  display: inline-flex;
  justify-content: center;
  justify-self: center;
  min-height: 6px;
  width: 100%;
}

.jianpu-run.underline-0 .jianpu-run-low-dots {
  visibility: hidden;
}

.jianpu-low-dot-cell {
  display: inline-flex;
  justify-content: center;
  width: 30px;
  min-width: 30px;
  padding: 0;
}

.jianpu-extension {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  font-size: 17px;
  font-weight: 850;
  line-height: 18px;
}

.jianpu-bar {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 2px 7px;
  color: var(--muted);
  font-size: 22px;
  font-weight: 400;
  line-height: 1;
}

.jianpu-empty {
  color: var(--muted);
  font-size: 13px;
}

.note-palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(74px, 1fr));
  gap: 8px;
}

.note-key {
  display: grid;
  gap: 2px;
  min-height: 58px;
  padding: 7px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  text-align: left;
}

.note-key.black {
  border-color: #c9d2cd;
  background: #e8eeeb;
  color: var(--text);
}

.note-key:hover {
  transform: translateY(-1px);
}

.note-key .note-name {
  font-size: 14px;
  font-weight: 850;
}

.note-key .jianpu {
  color: inherit;
  opacity: 0.82;
}

.jianpu-notation {
  display: inline-grid;
  grid-template-rows: 6px 18px 6px;
  align-items: center;
  justify-items: center;
  justify-content: center;
  min-height: 30px;
  min-width: 30px;
  line-height: 1;
}

.jianpu-dot-row {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  min-height: 6px;
}

.jianpu-notation.no-below {
  grid-template-rows: 6px 18px 6px;
  min-height: 30px;
}

.jianpu-notation.no-below .jianpu-dot-row.below {
  visibility: hidden;
}

.jianpu-accidental {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  font-size: 11px;
  line-height: 18px;
}

.jianpu-accidental.empty {
  display: none;
}

.jianpu-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  font-size: 17px;
  font-weight: 850;
  line-height: 18px;
}

.jianpu-dots {
  display: flex;
  align-items: center;
  gap: 2px;
  justify-content: center;
  width: 18px;
  min-height: 5px;
}

.jianpu-dots span {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: currentColor;
}

.note-index {
  color: inherit;
  font-size: 11px;
  opacity: 0.58;
}

.rest-key {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.palette-switch {
  display: inline-flex;
  max-width: 100%;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.palette-switch button {
  min-height: 32px;
  padding: 0 10px;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.palette-switch button + button {
  border-left: 1px solid var(--line);
}

.palette-switch button.active {
  background: var(--accent);
  color: #fff;
}

.export-panel {
  min-width: 0;
}

.collapse-panel {
  margin-top: 14px;
}

.collapse-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 34px;
}

.collapse-toggle {
  min-height: 34px;
  min-width: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-weight: 850;
  text-align: left;
}

.collapse-title {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.collapse-title::before {
  content: "›";
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  color: var(--muted);
  transform: rotate(0deg);
  transition: transform 140ms ease;
}

.collapse-toggle[aria-expanded="true"] .collapse-title::before {
  transform: rotate(90deg);
}

.collapse-content {
  margin-top: 10px;
}

.collapse-hint {
  margin: -4px 0 8px;
}

.device-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.pause-nfc-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.pause-nfc-panel .nfc-toolbar {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.pause-nfc-panel .nfc-fields {
  grid-template-columns: 1fr;
}

.device-fields {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.device-fields code {
  overflow: hidden;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-fields code.warning {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
}

.device-actions,
.log-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.device-actions button {
  flex: 1 1 120px;
}

.device-progress {
  display: grid;
  gap: 8px;
  padding: 11px;
  border: 1px solid rgba(20, 108, 84, 0.34);
  border-radius: 8px;
  background: #edf8f3;
}

.device-progress.error {
  border-color: rgba(180, 35, 24, 0.36);
  background: var(--danger-soft);
}

.device-progress[hidden] {
  display: none;
}

.device-progress-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 850;
}

.device-progress-head strong {
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 18px;
}

.device-progress.error .device-progress-head {
  color: var(--danger);
}

.device-progress-track {
  overflow: hidden;
  height: 14px;
  border: 1px solid rgba(15, 90, 70, 0.32);
  border-radius: 999px;
  background: #fff;
}

.device-progress-track span {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #31a783);
  transition: width 120ms ease;
}

.device-progress.error .device-progress-track span {
  background: var(--danger);
}

.device-progress-detail {
  color: var(--muted);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 11px;
  font-weight: 700;
}

pre {
  overflow: auto;
  max-height: calc(100vh - 152px);
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111815;
  color: #d7f4e7;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.uart-log-preview {
  max-height: 240px;
}

.import-dialog {
  width: min(620px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.import-dialog::backdrop {
  background: rgba(17, 24, 21, 0.42);
}

.confirm-dialog {
  width: min(440px, calc(100vw - 32px));
}

.dialog-body {
  display: grid;
  gap: 12px;
  padding: 16px;
  background: var(--panel);
}

.dialog-body textarea {
  min-height: 220px;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 13px;
}

.confirm-message {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  font-weight: 650;
  line-height: 1.7;
}

.device-info-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.device-info-row code {
  overflow: hidden;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: minmax(220px, 280px) minmax(0, 1fr);
  }

  .export-panel {
    position: static;
    grid-column: 1 / -1;
  }

  pre {
    max-height: 420px;
  }
}

@media (max-width: 980px) {
  .composer-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 820px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-actions,
  .device-serial {
    width: 100%;
  }

  .device-serial {
    flex-wrap: wrap;
  }

  .device-badge {
    flex: 1 1 180px;
    max-width: none;
  }

  .app-shell,
  .composer-grid,
  .editor-toolbar,
  .insert-row,
  .nfc-fields,
  .nfc-toolbar,
  .sequence-derived {
    grid-template-columns: 1fr;
  }

  .song-tools {
    justify-content: stretch;
  }

  .song-tools button {
    flex: 1 1 160px;
  }

  .transpose-amount {
    width: auto;
  }

  .nfc-toolbar button {
    justify-self: stretch;
  }

  .series-panel {
    position: static;
  }

  .note-palette {
    grid-template-columns: repeat(auto-fill, minmax(66px, 1fr));
  }
}
