:root {
  --bg: #e9e4da;
  --chrome: #f7f4ee;
  --chrome-2: #efeae1;
  --line: #d9d1c3;
  --ink: #23262b;
  --ink-soft: #6b6f76;
  --paper: #fffdf8;
  --staff: #3b3f46;
  --accent: #2f6fd6;
  --accent-soft: #e2ecfb;
  --play: #e0882b;
  --warn: rgba(214, 69, 65, 0.09);
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

body {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
}

.workspace { display: flex; min-height: 0; }
.workspace .desk { flex: 1; min-width: 0; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: none;
  background: none;
}

kbd {
  font: 600 10px/1 ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 2px 4px;
  background: var(--paper);
}

/* ---------- Top bar ---------- */
.topbar {
  display: grid;
  /* The sides share the space, but the buttons on the right never get squeezed. */
  grid-template-columns: minmax(0, 1fr) auto minmax(max-content, 1fr);
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--chrome);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-text { min-width: 0; flex: 1; }
.brand-text .status { padding-left: 9px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mark {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  border-radius: 8px;
  background: #1f3a5f; color: #f2a541;
  font-size: 18px;
}
.title-input {
  font: 600 17px/1.2 inherit;
  font-family: inherit;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  padding: 5px 8px;
  min-width: 0;
  width: 100%;
  max-width: 320px;
  color: var(--ink);
  user-select: text;
  -webkit-user-select: text;
}
.title-input:hover { border-color: var(--line); }
.title-input:focus { outline: none; border-color: var(--accent); background: var(--paper); }

.transport { display: flex; align-items: center; gap: 18px; }

.play-btn {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(47, 111, 214, 0.35);
  transition: transform .08s ease, background .15s ease;
}
.play-btn:hover { background: #2861bd; }
.play-btn:active { transform: scale(.95); }
.play-btn svg { width: 24px; height: 24px; fill: currentColor; }
.play-btn .icon-stop { display: none; }
body.playing .play-btn { background: var(--play); box-shadow: 0 2px 6px rgba(224, 136, 43, .4); }
body.playing .play-btn .icon-play { display: none; }
body.playing .play-btn .icon-stop { display: block; }

.tempo label { display: block; font-size: 10px; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-soft); margin-bottom: 2px; }
.tempo-row { display: flex; align-items: center; gap: 4px; }
.tempo input {
  width: 50px;
  font: 600 15px/1 inherit; font-family: inherit;
  text-align: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 2px;
  background: var(--paper);
  color: var(--ink);
  -moz-appearance: textfield;
}
.tempo input::-webkit-inner-spin-button { -webkit-appearance: none; }
.tempo input:focus { outline: none; border-color: var(--accent); }
.unit { font-size: 12px; color: var(--ink-soft); margin-left: 2px; }
.mini {
  width: 24px; height: 24px; border-radius: 6px;
  font-size: 16px; line-height: 1;
  color: var(--ink-soft);
}
.mini:hover { background: var(--chrome-2); color: var(--ink); }

.loop { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-soft); cursor: pointer; }
.loop input { accent-color: var(--accent); }

.actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.select { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-soft); margin-right: 8px; }
.select select {
  font: 600 14px inherit; font-family: inherit;
  border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 6px; background: var(--paper); color: var(--ink);
}
.ghost-btn {
  height: 32px; min-width: 32px; padding: 0 8px;
  border-radius: 7px;
  font-size: 18px;
  color: var(--ink-soft);
}
.ghost-btn.text { font-size: 13px; font-weight: 500; }
.ghost-btn:hover:not(:disabled) { background: var(--chrome-2); color: var(--ink); }
.ghost-btn:disabled { opacity: .35; cursor: default; }

/* ---------- Palette ---------- */
.palette {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 8px 18px;
  background: var(--chrome);
  border-bottom: 1px solid var(--line);
}

.seg { display: flex; background: var(--chrome-2); border-radius: 9px; padding: 3px; }
.seg-btn { padding: 7px 14px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--ink-soft); }
.seg-btn.on { background: var(--paper); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); }

.durations, .group { display: flex; gap: 4px; }
.group { padding-left: 14px; border-left: 1px solid var(--line); }

.tool {
  position: relative;
  width: 44px; height: 52px;
  border-radius: 9px;
  border: 1px solid transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--ink);
}
.tool:hover { background: var(--chrome-2); }
.tool.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.tool svg { width: 26px; height: 32px; overflow: visible; }
.tool .glyph { font-size: 22px; line-height: 30px; height: 32px; }
.tool .glyph.small { font-size: 18px; }
.tool kbd { position: absolute; bottom: 3px; padding: 1px 3px; font-size: 9px; }
.tool.on kbd { color: var(--accent); border-color: rgba(47,111,214,.35); }
.tool svg + kbd, .tool .glyph + kbd { position: static; }

.status { font-size: 11.5px; color: var(--ink-soft); font-variant-numeric: tabular-nums; white-space: nowrap; min-height: 14px; }
.settings { margin-left: auto; display: flex; flex-wrap: wrap; gap: 6px 12px; }
.settings .select { margin-right: 0; }

/* ---------- Score ---------- */
.desk {
  overflow: auto;
  padding: 22px;
}
.paper {
  position: relative;
  max-width: 1400px;
  margin: 0 auto;
  min-height: 100%;
  background: var(--paper);
  border-radius: 6px;
  box-shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(60, 45, 20, .10);
}
#score { display: block; width: 100%; }
#score.hover-note { cursor: grab; }
#score.dragging { cursor: grabbing; }
body.pdragging, body.pdragging * { cursor: grabbing !important; }
.drag-ghost {
  position: fixed; z-index: 40; pointer-events: none;
  width: 30px; height: 36px; transform: translate(-50%, -70%);
  color: var(--accent);
  filter: drop-shadow(0 3px 6px rgba(0,0,0,.2));
}
.drag-ghost[hidden] { display: none; }
.drag-ghost svg { width: 100%; height: 100%; }
#score .clef { font-family: "Apple Symbols", "Noto Music", "Segoe UI Symbol", serif; }
#score .timesig { font-family: Georgia, "Times New Roman", serif; font-weight: 700; }
#score .acc { font-family: "Apple Symbols", "Noto Music", "Segoe UI Symbol", serif; }

.empty {
  position: absolute;
  left: 0; right: 0; top: 190px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  color: var(--ink-soft);
  font-size: 14px;
  pointer-events: none;
}
.empty strong { color: var(--ink); font-size: 16px; font-weight: 600; }
.empty.hidden { display: none; }

/* ---------- Piano ---------- */
.bottom {
  background: var(--chrome);
  border-top: 1px solid var(--line);
  padding: 10px 18px 8px;
}
.piano {
  position: relative;
  display: flex;
  height: 86px;
  max-width: 1000px;
  margin: 0 auto;
}
.wkey {
  flex: 1;
  position: relative;
  background: linear-gradient(#fff, #f6f3ee);
  border: 1px solid #cfc7b8;
  border-top: none;
  border-radius: 0 0 6px 6px;
  margin-right: -1px;
  display: flex; align-items: flex-end; justify-content: center;
  padding-bottom: 6px;
  font-size: 10px;
  font-weight: 600;
  color: #a39b8c;
}
.wkey.c { color: var(--ink-soft); }
.wkey:hover { background: linear-gradient(#fff, #ece6dc); }
.bkey {
  position: absolute;
  top: 0;
  height: 54%;
  background: linear-gradient(#3a3d43, #1d1f23);
  border-radius: 0 0 4px 4px;
  z-index: 2;
  box-shadow: 0 2px 2px rgba(0,0,0,.25);
}
.bkey:hover { background: linear-gradient(#4a4e55, #2a2c31); }
.wkey.lit, .wkey:active { background: linear-gradient(#ffe3c2, #f7c58d); color: #8a5214; }
.bkey.lit, .bkey:active { background: linear-gradient(#f2a541, #c97a1c); }

.hints {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: 4px 16px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--ink-soft);
}
.hints kbd { margin: 0 1px; }

/* ---------- AI ---------- */
.ai-btn {
  margin-left: 8px;
  height: 34px; padding: 0 14px;
  border-radius: 17px;
  background: #1f3a5f; color: #fff;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.ai-btn span { color: #f2a541; }
.ai-btn:hover, .ai-btn.on { background: #284a78; }

.ai-panel {
  width: 360px; flex: none;
  display: flex; flex-direction: column;
  background: var(--chrome);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.ai-panel[hidden] { display: none; }
.ai-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 14px 14px 10px 16px;
  border-bottom: 1px solid var(--line);
}
.ai-head strong { display: block; font-size: 14px; }
.ai-head span { font-size: 12px; color: var(--ink-soft); }
.ai-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; user-select: text; -webkit-user-select: text; }
.msg { max-width: 92%; padding: 9px 12px; border-radius: 12px; font-size: 13px; line-height: 1.45; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--paper); border: 1px solid var(--line); border-bottom-left-radius: 4px; color: var(--ink); }
.msg.error { align-self: flex-start; background: rgba(214,69,65,.10); color: #a3302c; border: 1px solid rgba(214,69,65,.25); }
.msg .applied { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 11.5px; color: var(--ink-soft); }
.msg .applied button { font-size: 11.5px; font-weight: 600; color: var(--accent); padding: 2px 6px; border-radius: 5px; }
.msg .applied button:hover { background: var(--accent-soft); }
.msg.thinking { display: flex; align-items: center; gap: 8px; color: var(--ink-soft); }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.ai-intro { font-size: 12.5px; color: var(--ink-soft); line-height: 1.5; }
.ai-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.chip {
  font-size: 12px; padding: 6px 10px; border-radius: 14px;
  background: var(--paper); border: 1px solid var(--line); color: var(--ink);
  text-align: left;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.ai-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px 14px 14px; border-top: 1px solid var(--line); }
.ai-form textarea {
  flex: 1; resize: none;
  font: 13px/1.4 inherit; font-family: inherit;
  border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; background: var(--paper); color: var(--ink);
  max-height: 120px;
}
.ai-form textarea:focus { outline: none; border-color: var(--accent); }
.ai-send { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.ai-send svg { width: 18px; height: 18px; fill: currentColor; }
.ai-send:disabled { opacity: .4; cursor: default; }

/* ---------- Listening ---------- */
@media (max-width: 1000px) {
  .transport { gap: 12px; }
  .unit, .loop { font-size: 12px; }
  .ai-btn { margin-left: 2px; padding: 0 12px; }
}
.listen { display: flex; align-items: center; margin-left: 8px; border: 1px solid var(--line); border-radius: 17px; background: var(--paper); }
.rec-btn {
  height: 32px; padding: 0 12px 0 11px;
  display: flex; align-items: center; gap: 7px;
  font-size: 13px; font-weight: 600; color: var(--ink);
  border-radius: 16px 0 0 16px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #d64541; flex: none; }
.upload-btn { height: 32px; width: 34px; display: grid; place-items: center; color: var(--ink-soft); border-left: 1px solid var(--line); border-radius: 0 16px 16px 0; }
.upload-btn svg { width: 17px; height: 17px; fill: currentColor; }
.rec-btn:hover, .upload-btn:hover { background: var(--chrome-2); }
.recording .rec-btn { background: #d64541; color: #fff; }
.recording .rec-dot { background: #fff; border-radius: 2px; animation: pulse 1s infinite ease-in-out; }
.listening .rec-btn, .listening .upload-btn { opacity: .6; pointer-events: none; }
@keyframes pulse { 50% { opacity: .45; } }

.toast {
  position: fixed; left: 50%; bottom: 128px; transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 32px));
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px 9px 16px; border-radius: 12px;
  background: #23262b; color: #f4f2ee;
  font-size: 13px; line-height: 1.4;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
  z-index: 20;
}
.toast[hidden] { display: none; }
.toast span { margin-right: 6px; }
.toast button { flex: none; font-size: 12.5px; font-weight: 600; color: #8db6ff; padding: 5px 8px; border-radius: 6px; }
.toast button:hover { background: rgba(255,255,255,.1); }

.drop-hint {
  position: fixed; inset: 12px; z-index: 30;
  display: grid; place-items: center;
  border: 2px dashed var(--accent); border-radius: 16px;
  background: rgba(47,111,214,.08);
  font-size: 18px; font-weight: 600; color: var(--accent);
  pointer-events: none;
}
.drop-hint[hidden] { display: none; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d21;
    --chrome: #25282d;
    --chrome-2: #30343a;
    --line: #3a3e45;
    --ink: #eceae6;
    --ink-soft: #9a9ea6;
    --accent-soft: #233552;
  }
  .paper { box-shadow: 0 8px 30px rgba(0,0,0,.4); }
  .empty { color: #6b6f76; }
  .empty strong { color: #23262b; }
  kbd { background: var(--chrome-2); }
  .title-input:focus, .tempo input, .select select, .ai-form textarea, .chip, .listen { background: var(--chrome-2); }
  .rec-btn:hover, .upload-btn:hover { background: #3d424a; }
  .toast { background: #3a3e45; }
  .msg.assistant { background: var(--chrome-2); }
  .seg-btn.on { background: #3d424a; }
  .wkey { border-color: #111; }
}
