:root {
  --bg: #f2f2f2;
  --panel: #ffffff;
  --ink: #1c1c1c;
  --muted: #666666;
  --line: #d8d8d8;
  --soft: #ececec;
  --piano-scroll-height: 430px;
  --staff-scroll-height: 1290px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Source Serif 4", "Georgia", serif;
  color: var(--ink);
  background: linear-gradient(180deg, #fafafa 0%, var(--bg) 100%);
}

.app-shell {
  max-width: 1440px;
  margin: 0 auto;
  padding: 5.8rem 1rem 1.25rem;
}

.side-menu-toggle {
  position: fixed;
  top: 1rem;
  left: 1rem;
  z-index: 30;
  border: 1px solid #2f3b33;
  border-radius: 9px;
  background: #25352d;
  color: #f2f6f3;
  padding: 0.5rem 0.8rem;
  font-size: 0.9rem;
  letter-spacing: 0.01em;
  cursor: pointer;
}

.side-menu {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(330px, 86vw);
  padding: 5rem 1rem 1rem;
  background: #f8f9f8;
  border-right: 1px solid var(--line);
  box-shadow: 0 10px 28px rgb(28 28 28 / 14%);
  transform: translateX(-102%);
  transition: transform 180ms ease;
  z-index: 25;
}

.menu-open .side-menu {
  transform: translateX(0);
}

.menu-controls {
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  align-items: stretch;
}

.floating-playback {
  position: fixed;
  top: 0.65rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 22;
  width: min(96vw, 900px);
  pointer-events: auto;
}

.timeline-controls {
  margin-top: 0.35rem;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.42rem 0.65rem;
  background: rgb(255 255 255 / 90%);
  backdrop-filter: blur(5px);
  box-shadow: 0 8px 20px rgb(18 24 20 / 10%);
}

#timelineRange {
  width: 100%;
}

#measurePreview {
  min-width: 5.8rem;
  font-size: 0.85rem;
  color: var(--muted);
  opacity: 0;
  transition: opacity 120ms ease;
  font-variant-numeric: tabular-nums;
}

#measurePreview.visible {
  opacity: 1;
}

.playback-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.75rem;
  background: rgb(255 255 255 / 92%);
  backdrop-filter: blur(5px);
  box-shadow: 0 8px 20px rgb(18 24 20 / 12%);
}

.view-toggles {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.practice-toggles {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.view-toggles label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.practice-toggles label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.lookahead-control {
  display: flex;
  flex-direction: column;
  gap: 0.32rem;
  color: var(--muted);
  font-size: 0.92rem;
}

#lookAheadRange {
  width: 100%;
}

#lookAheadValue {
  font-size: 0.84rem;
  color: #7a857e;
  font-variant-numeric: tabular-nums;
}

.midi-connect-btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  color: var(--ink);
  padding: 0.45rem 0.6rem;
  font-size: 0.86rem;
  text-align: left;
  cursor: pointer;
}

.midi-connect-btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.speed-control {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.speed-reset-btn {
  width: auto !important;
  min-width: 2.2rem;
  height: 1.75rem !important;
  padding: 0 0.45rem !important;
  font-size: 0.82rem;
  line-height: 1;
}

#speedRange {
  width: 130px;
}

#speedValue {
  min-width: 3.7rem;
  font-variant-numeric: tabular-nums;
}

.playback-controls button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.15rem;
  height: 2.05rem;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  color: var(--ink);
  cursor: pointer;
}

.playback-controls button[hidden] {
  display: none !important;
}

.playback-controls .measure-step-btn {
  width: 2.65rem;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1;
}

.playback-controls .mark-btn {
  width: auto;
  min-width: 2.5rem;
  padding: 0 0.55rem;
  font-size: 0.86rem;
  font-weight: 600;
}

.playback-controls .midi-follow-btn {
  width: auto;
  min-width: 2.1rem;
  height: 1.55rem;
  padding: 0 0.4rem;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  position: relative;
  padding-left: 1rem;
}

.playback-controls .midi-follow-btn::before {
  content: "";
  position: absolute;
  left: 0.36rem;
  top: 50%;
  width: 0.42rem;
  height: 0.42rem;
  border-radius: 999px;
  transform: translateY(-50%);
  background: #8e8e8e;
}

.playback-controls .midi-follow-btn[aria-pressed="true"] {
  border-color: #4a9d6a;
  background: #e9f6ee;
}

.playback-controls .midi-follow-btn[aria-pressed="true"]::before {
  background: #33a15c;
}

.playback-controls .midi-follow-btn[aria-pressed="false"] {
  border-color: #c8c8c8;
  background: #f1f1f1;
}

.playback-controls .keys-follow-badge {
  width: auto;
  min-width: 2.1rem;
  height: 1.55rem;
  padding: 0 0.4rem 0 1rem;
  border: 1px solid #4a9d6a;
  border-radius: 8px;
  background: #e9f6ee;
  color: var(--ink);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1.55rem;
  position: relative;
}

.playback-controls .keys-follow-badge::before {
  content: "";
  position: absolute;
  left: 0.36rem;
  top: 50%;
  width: 0.42rem;
  height: 0.42rem;
  border-radius: 999px;
  transform: translateY(-50%);
  background: #33a15c;
}

.playback-controls .keys-follow-badge.is-waiting {
  border-color: #cb8f2a;
  background: #fff3df;
}

.playback-controls .keys-follow-badge.is-waiting::before {
  background: #cb8f2a;
}

.btn-icon-svg {
  width: 1.05rem;
  height: 1.05rem;
  display: block;
  fill: currentColor;
}

#muteBtn {
  position: relative;
}

#muteBtn .btn-icon-svg {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.icon-hidden {
  display: none;
}

.playback-controls button:hover:enabled {
  background: #e3e3e3;
}

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

#timeDisplay {
  min-width: 6.8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.file-picker {
  text-align: center;
  background: var(--ink);
  color: #fdfdfd;
  padding: 0.5rem 0.85rem;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
}

#midiFile {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfbfb;
  padding: 0.35rem;
  width: 100%;
}

.file-load-progress {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

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

#midiLoadProgress {
  width: 100%;
  height: 0.65rem;
}

#midiLoadProgressValue {
  min-width: 2.5rem;
  text-align: right;
  color: var(--muted);
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

.empty-load-progress {
  margin-top: 0.65rem;
  max-width: 360px;
}

.note-context-menu {
  position: fixed;
  z-index: 80;
  min-width: 180px;
  padding: 0.3rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 8px 22px rgb(18 24 20 / 18%);
}

.note-context-menu button {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 0.42rem 0.5rem;
  font: inherit;
  font-size: 0.88rem;
  cursor: pointer;
}

.note-context-menu button:hover {
  background: #edf4ef;
}

.note-name-label {
  fill: #1f6f44;
  font-size: 10px;
  font-weight: 700;
  pointer-events: none;
  user-select: none;
}

#status {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.score-frame {
  margin-top: 0.2rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  overflow: hidden;
  min-height: 260px;
  display: flex;
  flex-direction: column;
}

.empty-state {
  padding: 2rem 1.2rem;
  color: var(--muted);
}

.empty-state h2 {
  margin: 0;
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 600;
}

.empty-state p {
  margin: 0.7rem 0 0;
  max-width: 72ch;
  line-height: 1.45;
}

.empty-load-btn {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.52rem 0.9rem;
  border-radius: 9px;
  border: 1px solid #2f3b33;
  background: #25352d;
  color: #f2f6f3;
  font-size: 0.92rem;
  cursor: pointer;
}

.empty-load-btn:hover {
  background: #2e4238;
}

.view-panel {
  padding: 0.8rem;
  min-width: 0;
}

#pianoRollView {
  order: 1;
}

#staffView {
  order: 3;
}

.view-splitter {
  order: 2;
  height: 12px;
  position: relative;
  cursor: row-resize;
  touch-action: none;
}

.view-splitter::before {
  content: "";
  position: absolute;
  left: 0.9rem;
  right: 0.9rem;
  top: 50%;
  height: 1px;
  background: var(--line);
  transform: translateY(-50%);
}

.view-splitter::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 34px;
  height: 5px;
  border-radius: 999px;
  background: #b9c2bd;
  transform: translate(-50%, -50%);
}

body.split-resizing {
  cursor: row-resize;
  user-select: none;
}

body.custom-scroll-dragging {
  user-select: none;
}

.synced-scroll {
  margin-top: 0;
  overflow: hidden;
}

#pianoRollScroll {
  height: var(--piano-scroll-height);
  position: relative;
}

#staffScroll {
  height: var(--staff-scroll-height);
  position: relative;
}

.view-viewport {
  position: relative;
  padding-right: 14px;
}

.view-content {
  will-change: transform;
}

.custom-scrollbar {
  position: absolute;
  top: 0;
  right: 0;
  width: 14px;
  height: 100%;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 2px 2px;
}

.custom-scrollbar-track {
  position: relative;
  width: 100%;
  border-radius: 10px;
  background: #e6e9e7;
}

.custom-scrollbar-thumb {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  min-height: 24px;
  border-radius: 10px;
  background: #9aa59e;
  border: 2px solid #e6e9e7;
  cursor: pointer;
}

.custom-scrollbar-thumb:hover {
  background: #7f8a83;
}

#score {
  min-height: 100%;
}

#pianoRollViewport {
  position: sticky;
  top: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  height: 100%;
  background: linear-gradient(180deg, #fafcfb 0%, #f2f4f7 100%);
  z-index: 1;
}

#pianoRollBars {
  position: absolute;
  inset: 0;
  will-change: transform;
}

.piano-roll-note {
  position: absolute;
  border-radius: 5px;
  background: linear-gradient(180deg, #30b36a 0%, #2f8f46 100%);
  opacity: 0.8;
  box-shadow: 0 1px 4px rgb(35 86 52 / 30%);
}

.seekable-note {
  cursor: pointer;
}

.piano-roll-note.is-black-note {
  background: linear-gradient(180deg, #21935a 0%, #1f7047 100%);
}

.piano-roll-note.note-active {
  opacity: 1;
  filter: brightness(1.15);
}

.piano-roll-note.note-midi-input:not(.note-active) {
  opacity: 0.95;
  box-shadow:
    0 0 0 2px rgb(77 146 255 / 85%),
    0 1px 4px rgb(35 86 52 / 30%);
  filter: saturate(1.2);
}

#pianoKeyboard {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 92px;
  background: #1b1f1c;
  z-index: 4;
}

.keyboard-white-layer,
.keyboard-black-layer {
  position: absolute;
  inset: 0;
}

.piano-key {
  position: absolute;
  border: 1px solid #8d8d8d;
  border-radius: 0 0 4px 4px;
  margin: 0;
  padding: 0;
  cursor: default;
  overflow: hidden;
}

.white-key {
  top: 0;
  bottom: 0;
  background: linear-gradient(180deg, #fcfcfb 0%, #ecece7 100%);
}

.black-key {
  top: 0;
  height: 58%;
  border-color: #1a1a1a;
  background: linear-gradient(180deg, #414141 0%, #191919 100%);
  z-index: 2;
}

.white-key.key-active {
  background: linear-gradient(180deg, #95e5b5 0%, #5bc781 100%);
}

.black-key.key-active {
  background: linear-gradient(180deg, #44c278 0%, #21935a 100%);
}

.white-key.key-lookahead:not(.key-active) {
  box-shadow: inset 0 0 0 calc(2px + var(--lookahead-level, 0.25) * 3px)
    rgb(97 187 142 / calc(0.35 + var(--lookahead-level, 0.25) * 0.45));
}

.black-key.key-lookahead:not(.key-active) {
  box-shadow: inset 0 0 0 calc(2px + var(--lookahead-level, 0.25) * 2.5px)
    rgb(114 206 162 / calc(0.38 + var(--lookahead-level, 0.25) * 0.45));
}

.piano-key.key-midi-input::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 50%;
  background: rgb(69 141 255 / 72%);
  pointer-events: none;
}

.black-key.key-midi-input::after {
  height: 50%;
  background: rgb(87 168 255 / 78%);
}

#pianoHitLine {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 92px;
  height: 2px;
  background: linear-gradient(90deg, rgb(47 143 70 / 20%) 0%, rgb(47 143 70 / 65%) 50%, rgb(47 143 70 / 20%) 100%);
  pointer-events: none;
  z-index: 5;
}

svg {
  max-width: 100%;
  height: auto;
  display: block;
}

#score svg {
  max-width: none;
}

#score .note-active path,
#score .note-active ellipse,
#score .note-active line,
#score .note-active polygon {
  fill: #2f8f46 !important;
  stroke: #2f8f46 !important;
}

#score .note-midi-input:not(.note-active) path,
#score .note-midi-input:not(.note-active) ellipse,
#score .note-midi-input:not(.note-active) line,
#score .note-midi-input:not(.note-active) polygon {
  fill: #2f6fd0 !important;
  stroke: #2f6fd0 !important;
}

@media (max-width: 720px) {
  :root {
    --piano-scroll-height: 340px;
    --staff-scroll-height: 1020px;
  }

  .app-shell {
    padding: 6.4rem 0.65rem 1rem;
  }

  .side-menu-toggle {
    top: 0.55rem;
    left: 0.6rem;
  }

  .side-menu {
    width: min(320px, 92vw);
    padding: 4.35rem 0.7rem 0.7rem;
  }

  .floating-playback {
    top: 0.5rem;
    width: calc(100vw - 1rem);
  }

  .playback-controls {
    justify-content: flex-start;
  }

  .timeline-controls {
    gap: 0.4rem;
  }

  #measurePreview {
    min-width: 4.9rem;
    font-size: 0.8rem;
  }

  #pianoRollViewport {
    height: 100%;
  }

  #speedRange {
    width: 110px;
  }
}
