.midi-grid {
  --midi-row-height: 36px;
  --midi-label-width: 64px;
  --midi-ruler-height: 42px;
  --midi-grid-width: 352px;
  --midi-row-count: 128;
  --midi-note-fill: color-mix(in srgb, var(--accent, #7253a3) calc(25% + var(--midi-velocity, .7) * 29%), var(--surface, #fffdfd));
  --midi-surface-black: color-mix(in srgb, var(--surface, #fffdfd) 85%, var(--line, #e8e0ef));
  --midi-tick: color-mix(in srgb, var(--line, #e8e0ef) 57%, transparent);
  --midi-beat: color-mix(in srgb, var(--line, #e8e0ef) 84%, var(--muted, #6b6077));
  --midi-bar: color-mix(in srgb, var(--line, #e8e0ef) 58%, var(--muted, #6b6077));
  min-width: 0;
  color: var(--ink, #2b2239);
}

:root[data-theme="dark"] .midi-grid {
  --midi-note-fill: color-mix(in srgb, var(--accent, #cfb6f1) calc(14% + var(--midi-velocity, .7) * 20%), var(--surface, #2b2235));
}

.midi-grid *,
.midi-grid *::before,
.midi-grid *::after {
  box-sizing: border-box;
}

.midi-grid [hidden] {
  display: none !important;
}

.midi-grid-scroll {
  position: relative;
  width: 100%;
  max-width: 100%;
  height: clamp(285px, 54vh, 475px);
  overflow: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--line, #e8e0ef);
  border-radius: 11px;
  background: var(--canvas, #f8f6fb);
  scrollbar-color: var(--midi-bar) var(--surface, #fffdfd);
  scrollbar-width: thin;
  touch-action: pan-x pan-y;
}

.midi-grid-content {
  width: calc(var(--midi-label-width) + var(--midi-grid-width));
  min-height: 100%;
}

.midi-grid-ruler,
.midi-grid-body {
  display: flex;
  width: 100%;
}

.midi-grid-ruler {
  position: sticky;
  top: 0;
  z-index: 6;
  height: var(--midi-ruler-height);
  border-bottom: 1px solid var(--midi-bar);
  background: var(--surface, #fffdfd);
}

.midi-grid-corner,
.midi-grid-labels {
  position: sticky;
  left: 0;
  flex: 0 0 var(--midi-label-width);
  width: var(--midi-label-width);
  border-right: 1px solid var(--midi-bar);
}

.midi-grid-corner {
  z-index: 3;
  display: grid;
  place-content: center;
  background: var(--surface, #fffdfd);
  color: var(--muted, #6b6077);
  font-size: .8125rem;
  font-weight: 700;
}

.midi-grid-times,
.midi-grid-timeline {
  position: relative;
  flex: 0 0 var(--midi-grid-width);
  width: var(--midi-grid-width);
}

.midi-grid-times {
  display: flex;
  align-items: stretch;
  overflow: hidden;
}

.midi-grid-measure-marks,
.midi-grid-measure-lines {
  position: relative;
  flex: 0 0 var(--midi-bar-width);
  width: var(--midi-bar-width);
}

.midi-grid-measure-marks + .midi-grid-measure-marks {
  border-left: 1px solid var(--midi-bar);
}

.midi-grid-measure-marks span {
  position: absolute;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.midi-grid-bar-mark {
  top: 5px;
  margin-left: 7px;
  color: var(--ink, #2b2239);
  font-size: .8125rem;
  font-weight: 750;
}

.midi-grid-beat-mark {
  bottom: 3px;
  margin-left: 4px;
  color: var(--muted, #6b6077);
  font-size: .75rem;
  font-weight: 650;
}

.midi-grid-labels {
  z-index: 5;
  background: var(--surface, #fffdfd);
}

.midi-grid-pitch,
.midi-grid-row {
  width: 100%;
  height: var(--midi-row-height);
  border: 0;
  border-bottom: 1px solid var(--line, #e8e0ef);
  color: var(--ink, #2b2239);
  background: var(--surface, #fffdfd);
}

.midi-grid-pitch {
  display: grid;
  place-items: center;
  padding: 0 2px;
  font: inherit;
  font-size: .813rem;
  font-weight: 650;
  white-space: nowrap;
}

.midi-grid-pitch.is-black,
.midi-grid-row.is-black {
  background: var(--midi-surface-black);
}

.midi-grid-pitch.is-anchor,
.midi-grid-row.is-anchor {
  border-bottom-color: var(--midi-beat);
}

.midi-grid-pitch.is-anchor {
  color: var(--accent, #7253a3);
  font-weight: 800;
}

.midi-grid-pitch:hover {
  background: var(--accent-soft, #ede5f9);
}

.midi-grid-pitch:focus-visible,
.midi-grid-note:focus-visible {
  outline: 2px solid var(--accent, #7253a3);
  outline-offset: -2px;
}

.midi-grid-timeline {
  display: block;
  overflow: hidden;
  height: calc(var(--midi-row-height) * var(--midi-row-count));
  outline: 0;
  touch-action: pan-x pan-y;
}

.midi-grid[data-tool="draw"] .midi-grid-timeline {
  cursor: crosshair;
}

.midi-grid-row-background,
.midi-grid-lines,
.midi-grid-notes {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.midi-grid-lines {
  display: flex;
}

.midi-grid-measure-lines {
  height: 100%;
  background-image:
    repeating-linear-gradient(to right, var(--midi-beat) 0 1px, transparent 1px var(--midi-beat-width)),
    repeating-linear-gradient(to right, var(--midi-tick) 0 1px, transparent 1px var(--midi-snap-width));
}

.midi-grid-measure-lines + .midi-grid-measure-lines::before {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 1px;
  content: "";
  background: var(--midi-bar);
}

.midi-grid-note,
.midi-grid-cursor,
.midi-grid-ghost {
  position: absolute;
  z-index: 2;
  display: flex;
  align-items: center;
  height: calc(var(--midi-row-height) - 5px);
  margin-top: 2px;
  padding: 0 5px;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--accent, #7253a3) 70%, var(--line, #e8e0ef));
  border-radius: 5px;
  color: var(--ink, #2b2239);
  background: var(--midi-note-fill);
  font: inherit;
  font-size: .76rem;
  font-weight: 750;
  white-space: nowrap;
}

.midi-grid-note {
  pointer-events: auto;
  touch-action: pan-x pan-y;
  user-select: none;
}

.midi-grid-note-name {
  pointer-events: none;
}

.midi-grid-note.is-compact .midi-grid-note-name {
  display: none;
}

.midi-grid-note.is-selected {
  z-index: 3;
  outline: 2px solid var(--accent, #7253a3);
  outline-offset: -1px;
}

.midi-grid[data-tool="select"] .midi-grid-note {
  cursor: grab;
  touch-action: none;
}

.midi-grid[data-tool="select"] .midi-grid-note.is-dragging {
  opacity: .46;
  cursor: grabbing;
}

.midi-grid-handle {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: none;
  width: min(35%, 16px);
  border-left: 1px solid color-mix(in srgb, var(--accent, #7253a3) 44%, transparent);
  background: color-mix(in srgb, var(--accent, #7253a3) 17%, transparent);
  cursor: ew-resize;
}

.midi-grid[data-tool="select"] .midi-grid-note.is-selected .midi-grid-handle {
  display: block;
}

.midi-grid-cursor {
  z-index: 4;
  display: none;
  border: 2px dashed var(--accent, #7253a3);
  background: transparent;
  pointer-events: none;
}

.midi-grid-timeline:focus-visible .midi-grid-cursor {
  display: flex;
}

.midi-grid-ghost {
  z-index: 4;
  border-style: dashed;
  background: var(--accent-soft, #ede5f9);
  pointer-events: none;
}

.midi-grid-playhead {
  position: absolute;
  z-index: 4;
  inset: 0 auto 0 0;
  width: 2px;
  background: #b4502d;
  box-shadow: 0 0 0 1px #eb8b6333;
  pointer-events: none;
}

:root[data-theme="dark"] .midi-grid-playhead {
  background: #f6a17a;
}

.midi-grid-reader {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (pointer: coarse) {
  .midi-grid {
    --midi-row-height: 42px;
  }

  .midi-grid-handle {
    width: min(42%, 20px);
  }
}

@media (max-width: 420px) {
  .midi-grid {
    --midi-label-width: 55px;
  }
}
