:root {
  --bg: #f3efe6;
  --paper: #fffdf8;
  --ink: #1f2430;
  --muted: #6f6b62;
  --line: #d8d0c2;
  --accent: #1c6e63;
  --accent-2: #c7542f;
  --shadow: 0 14px 40px rgba(31, 36, 48, 0.1);

  --zone-mod: #dbe8ff;
  --zone-mod-text: var(--ink);
  --zone-mod-subtext: rgba(31, 36, 48, 0.7);
  --zone-overlay-trigger: #f6d0ff;
  --zone-key: #d8f2ea;
  --zone-key-accidental: #dbe8ff;
  --zone-octave: #f6d8ff;
  --zone-mode: #e9f5d1;
  --zone-all-notes: #ffd89a;
  --zone-all-notes-on: #b7ec9f;
  --zone-mpe-enabled: #8fe88f;
  --zone-mpe-disabled: #ff9b9b;
  --zone-play: #f4efdf;
  --zone-play-text: var(--ink);
  --zone-play-subtext: rgba(31, 36, 48, 0.7);
  --zone-play-out-of-scale: #fff;
  --zone-play-out-of-scale-text: rgba(31, 36, 48, 0.42);
  --zone-play-out-of-scale-subtext: rgba(31, 36, 48, 0.38);
  --zone-root: #f9b46c;
  --zone-root-text: var(--ink);
  --zone-root-subtext: rgba(31, 36, 48, 0.7);
  --zone-root-border: rgba(199, 84, 47, 0.55);
  --zone-root-shadow: rgba(199, 84, 47, 0.2);
  --zone-disabled: #ece7dc;
  --zone-active: #1c6e63;
  --zone-active-text: #f7fff9;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 12%, rgba(28, 110, 99, 0.12), transparent 42%),
    radial-gradient(circle at 85% 18%, rgba(199, 84, 47, 0.12), transparent 45%),
    linear-gradient(180deg, #f8f5ed 0%, #f1ecdf 100%);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}

.page-shell {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 0.72rem;
  color: var(--accent);
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.5rem, 2vw + 1rem, 2.4rem);
  line-height: 1.05;
}

.lede {
  margin: 8px 0 0;
  color: var(--muted);
  max-width: 56ch;
}

.header-links {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.header-links a {
  color: var(--ink);
  text-decoration: none;
  border-bottom: 1px solid rgba(31, 36, 48, 0.25);
}

.layout-grid {
  display: grid;
  grid-template-columns: minmax(280px, 320px) minmax(0, 1fr);
  grid-template-areas:
    "controls surface"
    "advanced advanced"
    "log log";
  gap: 18px;
  align-items: start;
}

.panel {
  background: rgba(255, 253, 248, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 16px;
  backdrop-filter: blur(4px);
}

.control-panel h2,
.surface-panel h2,
.advanced-panel h2,
.log-panel h2 {
  margin: 0 0 10px;
  font-size: 1rem;
  font-weight: 700;
}

.control-panel h2:not(:first-child) {
  margin-top: 18px;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}

.field-grid label {
  font-size: 0.84rem;
  color: var(--muted);
  margin-top: 4px;
}

.button-row {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.connection-routing {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.75);
}

.connection-routing-label {
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.68rem;
  white-space: nowrap;
}

#routingStatus.routing-ready {
  color: #1f7a1f;
}

#routingStatus.routing-not-ready {
  color: #8a3e2a;
}

.muted {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.status-grid > div {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.65);
}

.status-label {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.control-panel {
  grid-area: controls;
}

.surface-panel {
  grid-area: surface;
  padding-top: 4px;
}

.advanced-panel {
  grid-area: advanced;
}

.log-panel {
  grid-area: log;
}

@media (max-width: 960px) {
  .layout-grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "surface"
      "controls"
      "advanced"
      "log";
  }
}

.surface-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 12px;
}

.surface-meta {
  margin-bottom: 0;
}

.surface-meta .muted {
  margin: 0;
}

.legend {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.legend-item {
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 0.75rem;
  border: 1px solid var(--line);
  background: #fff;
}

.legend-mod {
  background: var(--zone-mod);
}
.legend-key {
  background: var(--zone-key);
}
.legend-mode {
  background: var(--zone-mode);
}
.legend-play {
  background: var(--zone-play);
}
.legend-root {
  background: var(--zone-root);
}

#visualization {
  overflow-x: auto;
}

.surface-row {
  display: flex;
  align-items: center;
  gap: 6px;
  width: max-content;
}

.surface-row + .surface-row {
  margin-top: 0px;
}

.surface-state-row {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.state-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.8);
  font-size: 0.78rem;
}

.state-chip-label {
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.68rem;
  white-space: nowrap;
}

.state-chip strong {
  font-size: 0.8rem;
  white-space: nowrap;
}

.state-chip-value-chord {
  display: inline-block;
  width: 11ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.state-chip-edit {
  padding-right: 6px;
}

.state-chip-edit .form-select {
  min-width: 92px;
  padding-top: 0.12rem;
  padding-bottom: 0.12rem;
  padding-left: 0.45rem;
  border-radius: 4px;
  border-color: rgba(31, 36, 48, 0.12);
  font-size: 0.78rem;
  line-height: 1.1;
  background-color: #fff;
}

.state-chip-edit #stateScaleSelect {
  min-width: 132px;
}

.state-chip-button {
  padding-top: 0.14rem;
  padding-bottom: 0.14rem;
  line-height: 1.1;
  white-space: nowrap;
}

.state-chip-button-group {
  border-radius: 4px;
  overflow: hidden;
}

.state-chip-button-group .state-chip-button {
  min-width: 56px;
}

.cell {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(31, 36, 48, 0.12);
  border-radius: 4px;
  background: #f7f4eb;
  color: var(--ink);
  padding: 4px 2px;
  line-height: 1;
  user-select: none;
  pointer-events: auto;
  touch-action: none;
  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    transform 120ms ease;
}

.cell .cell-label {
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
}

.cell .cell-label .cell-label-octave {
  color: rgba(31, 36, 48, 0.5);
}

.cell .cell-label.cell-label-small {
  font-size: 0.62rem;
}

.cell .cell-sub-label {
  margin-top: 2px;
  font-size: 0.55rem;
  letter-spacing: 0.04em;
  color: rgba(31, 36, 48, 0.7);
  text-transform: uppercase;
}

.cell.zone-mod {
  background: var(--zone-mod);
  color: var(--zone-mod-text);
}

.cell.zone-mod .cell-sub-label {
  color: var(--zone-mod-subtext);
}
.cell.zone-overlay-trigger {
  background: var(--zone-overlay-trigger);
}
.cell.zone-key {
  background: var(--zone-key);
}
.cell.zone-key.cell-accidental {
  background: var(--zone-key-accidental);
}
.cell.zone-preset-switch {
  background: var(--zone-mode);
}
.cell.zone-octave {
  background: var(--zone-octave);
}
.cell.zone-mode {
  background: var(--zone-mode);
}
.cell.zone-all-notes-toggle {
  background: var(--zone-all-notes);
}
.cell.zone-all-notes-toggle.cell-selected {
  background: var(--zone-all-notes-on);
}
.cell.zone-mpe {
  background: var(--zone-mpe-disabled);
}
.cell.zone-mpe.cell-selected {
  background: var(--zone-mpe-enabled);
}
.cell.zone-play {
  background: var(--zone-play);
  color: var(--zone-play-text);
}

.cell.zone-play .cell-sub-label {
  color: var(--zone-play-subtext);
}
.cell.zone-play.cell-out-of-scale {
  background: var(--zone-play-out-of-scale);
  color: var(--zone-play-out-of-scale-text);
}

.cell.zone-play.cell-out-of-scale .cell-sub-label {
  color: var(--zone-play-out-of-scale-subtext);
}
.cell.cell-root {
  background: var(--zone-root);
  color: var(--zone-root-text);
}

.cell.cell-root .cell-sub-label {
  color: var(--zone-root-subtext);
}
.cell.zone-play.cell-root {
  border-color: var(--zone-root-border);
  box-shadow: inset 0 0 0 1px var(--zone-root-shadow);
}
.cell.cell-disabled {
  background: var(--zone-disabled);
  color: rgba(31, 36, 48, 0.45);
}

.cell.cell-selected {
  border-color: rgba(28, 110, 99, 0.6);
  box-shadow: inset 0 0 0 2px rgba(28, 110, 99, 0.18);
}

.cell.cell-selected-live {
  border-color: rgba(199, 84, 47, 0.8);
  box-shadow: inset 0 0 0 2px rgba(199, 84, 47, 0.18);
}

.cell.cell-same-note {
  border-color: rgba(199, 84, 47, 0.8);
  box-shadow: inset 0 0 0 2px rgba(199, 84, 47, 0.12);
}

.cell.cell-active,
.cell.cell-held {
  background: var(--accent-2);
  color: var(--zone-active-text);
  border-color: rgba(199, 84, 47, 0.9);
  transform: translateY(-1px);
}

.cell.cell-active .cell-sub-label,
.cell.cell-held .cell-sub-label {
  color: rgba(247, 255, 249, 0.8);
}

.surface-footer {
  margin-top: 6px;
}

.advanced-axis-grid {
  display: grid;
  grid-template-columns: max-content max-content;
  column-gap: 10px;
  row-gap: 6px;
  align-items: center;
}

.advanced-options-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 10px;
}

.advanced-option {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.7);
  display: grid;
  gap: 6px;
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

#log {
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.log-entry {
  background: #f5f1e8;
  border: 1px solid rgba(31, 36, 48, 0.08);
  font-size: 0.85rem;
  padding: 4px 8px;
  border-radius: 4px;
}

.log-success .msg {
  color: #287a1d;
}
.log-warn .msg {
  color: #a96d00;
}
.log-error .msg {
  color: #a12620;
}

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

  .surface-panel,
  .advanced-panel,
  .log-panel {
    grid-column: auto;
  }

  .surface-header {
    flex-direction: column;
  }

  .legend {
    justify-content: flex-start;
  }
}

@media (max-width: 640px) {
  .page-shell {
    padding: 14px;
  }

  .app-header {
    flex-direction: column;
  }

  .header-links {
    justify-content: flex-start;
  }

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

  .button-row .btn {
    flex: 1 1 auto;
  }
}
