:root {
  --bg-top: #f5f3ec;
  --bg-mid: #ebe6db;
  --bg-bottom: #ddd6c6;
  --ink: #1f2a2e;
  --muted: #55676d;
  --panel: rgba(250, 248, 241, 0.86);
  --panel-border: rgba(31, 42, 46, 0.14);
  --accent: #00806d;
  --accent-strong: #045547;
  --danger: #b43f23;
  --keyboard-bg: linear-gradient(145deg, #f3efdf 0%, #e8e0cd 100%);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  color: var(--ink);
  font-family:
    "Avenir Next",
    "Trebuchet MS",
    "Gill Sans",
    sans-serif;
  background:
    radial-gradient(circle at 0% 0%, rgba(255, 255, 255, 0.8), transparent 42%),
    radial-gradient(circle at 92% 12%, rgba(0, 128, 109, 0.11), transparent 30%),
    linear-gradient(180deg, var(--bg-top), var(--bg-mid) 45%, var(--bg-bottom));
}

.app-shell {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 4px 0 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 4px;
  overflow: hidden;
  animation: fade-in 0.45s ease-out both;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid rgba(31, 42, 46, 0.2);
  margin-bottom: 0;
  padding: 1px 0 3px;
  min-height: 0;
  white-space: nowrap;
  gap: 10px;
  overflow: visible;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.header-keybed-field {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.64rem;
  color: var(--muted);
  min-width: 0;
}

.header-keybed-field span {
  white-space: nowrap;
}

.keybed-select-field select {
  width: auto;
  min-width: 165px;
  max-width: min(36vw, 320px);
  padding: 2px 5px;
  font-size: 0.67rem;
}

.header-mini-field select {
  width: auto;
  min-width: 96px;
  max-width: 140px;
  padding: 2px 5px;
  font-size: 0.67rem;
}

.header-mini-field input[type="number"] {
  width: auto;
  min-width: 98px;
  max-width: 120px;
  padding: 2px 5px;
  font-size: 0.67rem;
}

#tonicSelect,
#tuningNoteSelect {
  min-width: 48px;
  max-width: 70px;
}

#tuningFrequencyInput {
  min-width: 49px;
  max-width: 60px;
}

.axis-step-field input[type="number"] {
  width: auto;
  min-width: 44px;
  max-width: 56px;
  padding: 2px 4px;
  font-size: 0.66rem;
}

.tonic-anchor-field {
  gap: 5px;
}

.tonic-anchor-field input[type="checkbox"] {
  width: 13px;
  height: 13px;
  margin: 0;
}

.brand-block h1 {
  margin: 0;
  letter-spacing: 0.04em;
  font-size: clamp(0.92rem, 1.1vw, 1.12rem);
  line-height: 1;
  white-space: nowrap;
}

.brand-block p {
  display: none;
  margin: 0;
  color: var(--muted);
  font-size: 0.74rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  overflow: visible;
}

.header-menu {
  position: relative;
  display: inline-flex;
}

.header-menu-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 188px;
  display: grid;
  gap: 4px;
  padding: 6px;
  border: 1px solid rgba(31, 42, 46, 0.24);
  border-radius: 8px;
  background: rgba(250, 248, 241, 0.97);
  box-shadow: 0 9px 20px rgba(31, 42, 46, 0.2);
  z-index: 12;
}

.menu-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.69rem;
  color: var(--ink);
}

.menu-toggle input[type="checkbox"] {
  width: 14px;
  height: 14px;
  margin: 0;
  flex: 0 0 auto;
}

#expertKeyStats {
  align-self: center;
  padding: 0 2px;
  font-size: 0.64rem;
}

.top-bar .action-btn {
  padding: 3px 7px;
  font-size: 0.74rem;
  border-radius: 8px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas:
    "left library synth"
    "keyboard keyboard keyboard";
  gap: 3px;
  min-height: 0;
  overflow: hidden;
  align-content: start;
}

.workspace.library-hidden {
  grid-template-areas:
    "left left synth"
    "keyboard keyboard keyboard";
}

.panel {
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 5px 14px rgba(31, 42, 46, 0.08);
  padding: 7px;
  min-height: 0;
}

.panel h2 {
  margin: 0;
  font-size: 0.92rem;
}

.left-panel {
  grid-area: left;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 4px;
}

.library-panel {
  grid-area: library;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr);
  gap: 4px;
}

.synth-panel {
  grid-area: synth;
  overflow: auto;
  padding: 5px;
}

.keyboard-panel {
  grid-area: keyboard;
  overflow: hidden;
  padding: 0;
}

label {
  font-size: 0.69rem;
  color: var(--muted);
}

input,
select,
textarea,
button {
  font-family: inherit;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid rgba(31, 42, 46, 0.2);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--ink);
  padding: 3px 6px;
  font-size: 0.75rem;
}

textarea {
  resize: none;
}

.panel-head-inline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 0;
}

.setup-top-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
  min-height: 0;
}

.scale-setup-editor {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 4px;
  min-height: 0;
}

.scale-setup-visualizer {
  display: flex;
  min-height: 0;
  min-width: 0;
  height: 100%;
}

.setup-viewport {
  flex: 1 1 auto;
  height: 100%;
  min-height: 0;
  min-width: 0;
  border-top: 1px solid rgba(31, 42, 46, 0.16);
}

.setup-text-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 4px;
  min-height: 0;
}

.field {
  min-width: 0;
}

.field.compact label {
  margin-bottom: 1px;
}

.textarea-field {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
}

.textarea-field textarea {
  height: 100%;
  min-height: 58px;
  font-size: 0.7rem;
  line-height: 1.2;
  overflow-y: auto;
  overflow-x: hidden;
}

.setup-controls-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  min-height: 0;
}

.axis-input-row {
  display: flex;
  align-items: center;
  gap: 3px;
}

.axis-input-row img {
  width: 18px;
  height: 18px;
  object-fit: contain;
  border-radius: 4px;
  border: 1px solid rgba(31, 42, 46, 0.18);
  background: rgba(255, 255, 255, 0.72);
}

.transpose-field {
  min-width: 0;
}

.transpose-inline {
  display: grid;
  grid-template-columns: 22px 1fr 22px;
  align-items: center;
  gap: 3px;
}

.transpose-inline button {
  height: 22px;
  padding: 0;
}

.transpose-inline strong {
  text-align: center;
  font-size: 0.82rem;
}

.library-filters {
  display: grid;
  gap: 2px;
}

.filter-title-row,
.filter-control-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 4px;
  align-items: center;
}

.filter-title-row span {
  font-size: 0.68rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filter-control-row .action-btn {
  width: 100%;
  padding: 3px 5px;
  font-size: 0.7rem;
}

.keyboard-panel {
  display: grid;
  grid-template-rows: auto auto;
  min-height: 0;
}

.keyboard-head {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 1px 4px;
  flex-wrap: wrap;
  white-space: normal;
}

.keyboard-head h2 {
  margin: 0;
  font-size: 0.88rem;
  flex: 0 1 auto;
}

.period-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.68rem;
  color: var(--muted);
}

.period-inline button {
  border: 1px solid rgba(31, 42, 46, 0.2);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  min-width: 24px;
  height: 22px;
  padding: 0;
  line-height: 1;
  cursor: pointer;
}

.period-inline strong {
  min-width: 18px;
  text-align: center;
  color: var(--ink);
}

.keyboard-display-controls {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}

.keyboard-display-controls .field-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.64rem;
  color: var(--muted);
}

.keyboard-display-controls input,
.keyboard-display-controls select {
  width: auto;
  min-width: 54px;
  max-width: 74px;
  padding: 2px 4px;
  font-size: 0.69rem;
}

.keyboard-display-controls .action-btn {
  padding: 3px 6px;
  font-size: 0.7rem;
}

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

.keyboard-head > strong {
  font-size: 0.95rem;
  color: var(--accent-strong);
  margin-left: 2px;
}

.keyboard-viewport {
  position: relative;
  border-top: 1px solid rgba(31, 42, 46, 0.16);
  border-left: 0;
  border-right: 0;
  border-bottom: 0;
  border-radius: 0;
  background: var(--keyboard-bg);
  overflow: hidden;
  min-height: 0;
  height: auto;
  padding: 0;
}

.visualizer-error-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  max-width: min(46ch, 48%);
  z-index: 6;
  padding: 5px 7px;
  border: 1px solid rgba(143, 47, 26, 0.45);
  border-radius: 8px;
  background: rgba(255, 239, 235, 0.96);
  color: #8f2f1a;
  font-size: 0.66rem;
  font-weight: 700;
  line-height: 1.2;
  pointer-events: none;
}

.keyboard-stage {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.hex-key {
  position: absolute;
  border: 0;
  padding: 0;
  outline: none;
  color: #0f1c20;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  text-align: center;
  cursor: pointer;
  touch-action: none;
  -webkit-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  transition: filter 0.1s ease;
}

.hex-key:focus,
.hex-key:focus-visible {
  outline: none;
  box-shadow: none;
}

.hex-key .key-fill {
  position: absolute;
  inset: 0;
  display: block;
  pointer-events: none;
  z-index: 0;
}

.hex-key .key-label {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 8px;
  line-height: 1.1;
  font-size: var(--key-font, 0.67rem);
  font-weight: 700;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.45);
}

.hex-key .key-label strong {
  font-size: var(--key-font-strong, 0.76rem);
}

.hex-key .tiny {
  font-size: var(--key-font-tiny, 0.6rem);
  opacity: 0.84;
}

.hex-key .key-id {
  font-size: var(--key-font-tiny, 0.58rem);
  opacity: 0.95;
  letter-spacing: 0.01em;
}

.viewer {
  height: 112.5%;
  min-height: 108px;
  max-height: 240px;
  overflow: auto;
  border-radius: 8px;
  border: 1px solid rgba(31, 42, 46, 0.2);
  background: rgba(255, 255, 255, 0.78);
  padding: 5px;
  font-size: 0.67rem;
  line-height: 1.2;
  white-space: normal;
}

.viewer-item {
  display: block;
  width: 100%;
  border: 1px solid rgba(31, 42, 46, 0.14);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.88);
  color: #1f2a2e;
  padding: 4px 6px;
  text-align: left;
  font-size: 0.67rem;
  line-height: 1.2;
  cursor: pointer;
  margin: 0 0 4px;
}

.viewer-item:last-child {
  margin-bottom: 0;
}

.viewer-item:hover {
  border-color: rgba(0, 128, 109, 0.5);
}

.viewer-item.active {
  border-color: rgba(0, 128, 109, 0.72);
  background: rgba(0, 128, 109, 0.12);
}

.library-action-row {
  display: flex;
  justify-content: flex-end;
  gap: 0;
  align-items: center;
  min-height: 0;
}

.toggle-line {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.68rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.compact-toggle {
  border: 1px solid rgba(31, 42, 46, 0.18);
  border-radius: 6px;
  padding: 2px 4px;
  min-height: 24px;
}

.toggle-line input[type="checkbox"] {
  width: 14px;
  height: 14px;
  margin: 0;
  flex: 0 0 auto;
}

.synth-actions {
  display: grid;
  gap: 8px;
}

.midi-actions {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(31, 42, 46, 0.2);
}

.pitch-estimator {
  margin-top: 11px;
  border-top: 1px solid rgba(31, 42, 46, 0.2);
  padding-top: 8px;
}

.pitch-estimator span {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.pitch-estimator strong {
  font-size: 1.2rem;
}

.action-btn {
  border: 1px solid rgba(31, 42, 46, 0.26);
  border-radius: 10px;
  background: linear-gradient(180deg, #ffffff 0%, #f4efdf 100%);
  color: #1f2a2e;
  padding: 6px 9px;
  font-size: 0.86rem;
  cursor: pointer;
}

.action-btn:hover {
  border-color: rgba(0, 128, 109, 0.58);
}

.action-btn.thin {
  font-size: 0.8rem;
  padding: 5px 7px;
}

.danger {
  color: #ffffff;
  background: linear-gradient(180deg, #ca5939, #a73a21);
  border-color: rgba(131, 23, 2, 0.5);
}

.status {
  min-height: 16px;
  margin: 0;
  font-size: 0.67rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status.ok {
  color: #005d4f;
}

.status.error {
  color: #8f2f1a;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(22, 29, 31, 0.44);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 40;
}

.overlay.hidden {
  display: none;
}

.hidden {
  display: none !important;
}

.overlay-card {
  width: min(1300px, 96vw);
  max-height: 92vh;
  overflow: hidden;
  border-radius: 14px;
  background: #fcfbf6;
  border: 1px solid rgba(31, 42, 46, 0.24);
  box-shadow: 0 16px 36px rgba(31, 42, 46, 0.24);
  display: grid;
  grid-template-rows: auto 1fr;
}

.overlay-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(31, 42, 46, 0.18);
}

.overlay-viewport {
  min-height: 66vh;
}

@media (max-width: 1120px) {
  .workspace {
    grid-template-columns: minmax(0, 3fr) minmax(0, 2fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

