:root {
  --mobile-palette-max-height: 30vh;
  --mobile-palette-padding: 160px;
}

* {
  box-sizing: border-box;
  user-select: none;
}
body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #0f172a;
  color: #e5e7eb;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
html, body {
  height: 100%;
}
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}
#toolbar {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  border-bottom: 1px solid #1f2937;
  background: #020617;
  font-size: 12px;
}
.toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}
.toolbar-links {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  justify-content: flex-end;
  flex-wrap: nowrap;
  white-space: nowrap;
  color: #9ca3af;
  font-weight: 600;
}
.toolbar-links a {
  color: inherit;
  text-decoration: none;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.toolbar-links a:hover {
  color: #f9fafb;
  border-color: #334155;
  background: rgba(255, 255, 255, 0.04);
}
.link-separator {
  color: #475569;
}
#playButton {
  padding: 6px 14px;
  border-radius: 999px;
  border: none;
  font-size: 14px;
  cursor: pointer;
  background: #22c55e;
  color: #020617;
  font-weight: 600;
}
#playButton.playing {
  background: #ef4444;
  color: #f9fafb;
}
#helpButton {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid #4b5563;
  background: #111827;
  color: #e5e7eb;
  font-size: 12px;
  cursor: pointer;
  margin-left: 4px;
}
#speedLabel {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #9ca3af;
  margin-left: 16px;
}
#speedLabel span {
  white-space: nowrap;
}
#speedSlider {
  width: 140px;
  accent-color: #22c55e;
}

#container {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

#playArea {
  flex: 1;
  position: relative;
  background: radial-gradient(circle at 10% 10%, #1f2937, #020617);
  overflow: hidden;
}

#connectionLayer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
  z-index: 8;
}

#connectionLayer line {
  stroke: #fde047;
  stroke-width: 2.2px;
  stroke-linecap: round;
  filter: drop-shadow(0 0 6px rgba(253, 224, 71, 0.35));
}

#connectionLayer line.draft {
  stroke: #93c5fd;
  stroke-dasharray: 6 6;
  filter: drop-shadow(0 0 4px rgba(147, 197, 253, 0.4));
}

#palette {
  position: absolute;
  top: 0;
  right: 0;
  width: 230px;
  height: 100%;
  padding: 12px;
  background: rgba(2, 6, 23, 0.96);
  display: flex;
  flex-direction: column;
  border-left: 1px solid #111827;
  z-index: 10;
  overflow-y: auto;
}
#palette h3 {
  margin: 0 0 8px 0;
  font-size: 13px;
  color: #9ca3af;
}
.palette-items {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.palette-note,
.palette-acc,
.palette-oct {
  aspect-ratio: 1 / 1;
  flex: 0 0 44px;
}
.palette-note {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  cursor: grab;
  border: 2px solid rgba(15, 23, 42, 0.7);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
}
.palette-acc,
.palette-oct {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  cursor: grab;
  border: 2px solid #374151;
  background: radial-gradient(circle at 30% 20%, #6b7280, #111827);
  color: #f9fafb;
  box-shadow: 0 4px 10px rgba(0,0,0,0.5);
}
.palette-oct {
  font-size: 13px;
  font-weight: 600;
}

@media (max-width: 900px), (orientation: portrait) {
  #speedLabel {
    display: none;
  }

  #playArea {
    padding-bottom: var(--mobile-palette-padding);
  }

  #palette {
    top: auto;
    bottom: 0;
    left: 0;
    width: 100%;
    height: auto;
    max-height: var(--mobile-palette-max-height);
    min-height: 140px;
    border-left: none;
    border-top: 1px solid #111827;
    box-shadow: 0 -12px 24px rgba(0, 0, 0, 0.6);
    overflow: hidden;
  }

  #palette h3 {
    text-align: center;
    margin-bottom: 10px;
  }

  .palette-items {
    justify-content: flex-start;
    flex-direction: row;
    flex-wrap: wrap;
    align-content: flex-start;
    overflow-x: hidden;
    overflow-y: auto;
    width: 100%;
    padding: 4px 2px 8px;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
  }
}

.item {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  font-size: 12px;
  font-weight: 600;
  color: #020617;
  border-radius: 999px;
  border: 2px solid rgba(15, 23, 42, 0.8);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.1s;
  z-index: 20;
  outline-offset: 0;
}
.item.note {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.item.chord {
  min-width: 70px;
  height: 32px;
  padding: 0 10px;
  font-size: 12px;
  color: #f9fafb;
}
.item.accidental {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 20px;
  background: radial-gradient(circle at 30% 20%, #6b7280, #111827);
  color: #f9fafb;
}
.item.octave {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 13px;
  background: radial-gradient(circle at 30% 20%, #4b5563, #020617);
  color: #f9fafb;
}
.item.selected {
  box-shadow:
    0 0 0 4px rgba(250, 250, 210, 0.8),
    0 8px 20px rgba(0, 0, 0, 0.9);
}
.item:active {
  cursor: grabbing;
}

#playHead {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: rgba(248, 250, 252, 0.8);
  box-shadow: 0 0 10px rgba(248, 250, 252, 0.9);
  pointer-events: none;
  display: none;
  z-index: 5;
}

#selectionBox {
  position: absolute;
  border: 1px dashed #60a5fa;
  background: rgba(37, 99, 235, 0.15);
  pointer-events: none;
  display: none;
  z-index: 5;
}

/* Help overlay */
#helpOverlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#helpOverlay.visible {
  display: flex;
}
#helpBox {
  width: 480px;
  max-width: calc(100% - 32px);
  background: #020617;
  border-radius: 16px;
  border: 1px solid #1e293b;
  padding: 16px 18px 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.8);
  font-size: 13px;
  color: #e5e7eb;
}
#helpBox h2 {
  margin: 0 0 8px 0;
  font-size: 16px;
}
#helpBox p {
  margin: 4px 0 6px 0;
  color: #9ca3af;
}
.help-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 18px;
  margin-top: 6px;
}
.help-grid ul {
  margin: 0;
  padding-left: 14px;
  list-style: disc;
  font-size: 12px;
  color: #d1d5db;
}
.help-grid li {
  margin-bottom: 4px;
}
#helpFooter {
  margin-top: 8px;
  font-size: 11px;
  color: #9ca3af;
}
#helpActions {
  margin-top: 10px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
#helpCloseButton {
  padding: 5px 12px;
  border-radius: 999px;
  border: none;
  font-size: 13px;
  cursor: pointer;
  background: #22c55e;
  color: #020617;
  font-weight: 600;
}

#hoverTooltip {
  position: absolute;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(2, 6, 23, 0.94);
  border: 1px solid #1e293b;
  color: #e5e7eb;
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.65);
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.12s ease, transform 0.12s ease;
  z-index: 120;
}

#hoverTooltip.visible {
  opacity: 1;
  transform: translateY(0);
}