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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #eef2f7;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  /* No selection, callouts, or loupe anywhere in the app. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  padding:
    max(10px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  gap: 10px;
}

/* ---------- Toolbar ---------- */

.toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 128px;
}

.center-controls {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.transport { display: flex; gap: 16px; }

.btn {
  width: 96px;
  height: 96px;
  border: none;
  border-radius: 28px;
  display: grid;
  place-items: center;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18);
  transition: transform 0.1s, box-shadow 0.1s, opacity 0.2s, filter 0.2s;
}
.btn svg { width: 52px; height: 52px; fill: #fff; }
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.18); }
.btn:disabled { opacity: 0.3; filter: grayscale(0.6); cursor: default; box-shadow: none; }

.btn-play { background: #22C55E; }
.btn-stop { background: #EF4444; }

/* ---------- Lines picker ---------- */

.lines-picker {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 6px 14px;
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
}
.lines-icon { width: 30px; height: 30px; fill: #94a3b8; flex: none; }
.lines-buttons { display: flex; gap: 6px; }
.lines-buttons button {
  width: 52px;
  height: 48px;
  border: none;
  border-radius: 16px;
  background: #f1f5f9;
  color: #475569;
  font: 700 24px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.lines-buttons button:active:not(:disabled) { transform: scale(0.94); }
.lines-buttons button.selected { background: #3B82F6; color: #fff; }
.lines-buttons button:disabled { cursor: default; }
.lines-picker.disabled { opacity: 0.4; }

/* ---------- Tempo slider ---------- */

.tempo {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 52px;
  padding: 0 14px;
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
}
.tempo-icon { font-size: 30px; line-height: 1; }
.tempo-value {
  min-width: 2.2em;
  font: 600 15px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #94a3b8;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#tempo {
  flex: 1;
  min-width: 120px;
  height: 44px;
  margin: 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
  cursor: pointer;
}
#tempo::-webkit-slider-runnable-track {
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, #86efac, #fde047, #fb923c);
}
#tempo::-moz-range-track {
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, #86efac, #fde047, #fb923c);
}
#tempo::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 36px;
  height: 36px;
  margin-top: -12px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid #64748b;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.25);
}
#tempo::-moz-range-thumb {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid #64748b;
}

/* ---------- Art / Staff mode toggle ---------- */

.right-controls { display: flex; align-items: center; gap: 12px; }

.view-toggle {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  background: #fff;
  border-radius: 22px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
}
.view-toggle button {
  width: 56px;
  height: 52px;
  border: none;
  border-radius: 16px;
  background: transparent;
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, box-shadow 0.15s;
}
.view-toggle button.selected { background: #dbeafe; box-shadow: inset 0 0 0 3px #3B82F6; }
/* Pencil/finger toggle: until a Pencil is detected, only the finger option shows as active. */
.view-toggle button:disabled { opacity: 0.25; cursor: default; filter: grayscale(1); }
.input-toggle button.selected:disabled { opacity: 1; filter: none; }

/* ---------- Color wheel ---------- */

.wheel {
  width: 128px;
  height: 128px;
  overflow: visible;
  touch-action: manipulation;
}
.wheel { transition: opacity 0.2s; }
.wheel.is-hidden { display: none; }   /* staff mode: the row picks the color */

/* Staff mode: sharps & flats toggle, in the wheel's spot. */
.acc-toggle {
  width: 128px;
  height: 128px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: #94a3b8;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}
.acc-toggle[hidden] { display: none; }
.acc-glyph { font: 700 44px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
.acc-swatches { display: flex; gap: 4px; opacity: 0.35; transition: opacity 0.15s; }
.acc-swatches i { width: 12px; height: 12px; border-radius: 50%; }
.acc-toggle.on { color: #1e293b; box-shadow: inset 0 0 0 4px #3B82F6, 0 2px 10px rgba(15, 23, 42, 0.08); background: #eff6ff; }
.acc-toggle.on .acc-swatches { opacity: 1; }
.wheel .seg {
  cursor: pointer;
  stroke: #fff;
  stroke-width: 2.5;
  stroke-linejoin: round;
  transition: transform 0.15s ease-out;
}
.wheel .seg.selected { transform: scale(1.08); }
.wheel .center { stroke: #fff; stroke-width: 3; transition: fill 0.15s; }
.wheel .check { pointer-events: none; transition: transform 0.15s ease-out; }
.wheel .check circle { fill: #fff; filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.35)); }
.wheel .check path { fill: none; stroke: #16A34A; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Drawing surface ---------- */

.stage { flex: 1 1 auto; min-height: 0; display: flex; }

/* The surface is one big Pencil area; the four white boxes are drawn inside it,
   so dots can be dragged freely between boxes (and over the gaps). */
.surface {
  position: relative;
  flex: 1;
  overflow: visible;    /* edge dots may poke a few px past the boxes */
  touch-action: none;   /* no scrolling, panning, or zooming */
  cursor: crosshair;
}

.boxes, .dots { position: absolute; inset: 0; pointer-events: none; }

.box {
  position: absolute;
  left: 0;
  right: 0;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
  overflow: hidden;
}

.measure { position: absolute; top: 0; bottom: 0; }

/* Staff (piano roll) background: one tinted band per pitch, B at the top. */
.staff-grid { position: absolute; inset: 0; }
.pitch-band { position: absolute; left: 0; right: 0; box-shadow: inset 0 -1px 0 rgba(255, 255, 255, 0.9); }
.pitch-band .key { position: absolute; left: 0; top: 0; bottom: 0; width: 8px; }
.pitch-band.sharp .key { width: 14px; }   /* like a black key: stands out from the naturals */
.beat-lines {
  position: absolute;
  inset: 0;
  background-image: linear-gradient(to right, rgba(15, 23, 42, 0.08) 0 1px, transparent 1px);
  background-repeat: repeat-x;
}
.measure.shade { background: #f6f8fb; }

/* ---------- Dots ---------- */

.dot {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 40px;
  will-change: transform;
}
.dot.animating { transition: transform 500ms cubic-bezier(0.34, 1.4, 0.64, 1); }
.dot.sliding { transition: transform 90ms ease-out; }   /* beat-to-beat moves while playing */

.dot-face {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12);
  transition: transform 0.12s, box-shadow 0.12s;
}
.surface.staff .dot-face { transform: scale(var(--note-scale, 1)); }
.dot.dragging .dot-face {
  transform: scale(1.15);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12), 0 6px 14px rgba(0, 0, 0, 0.22);
}
.surface.staff .dot.dragging .dot-face { transform: scale(calc(var(--note-scale, 1) * 1.25)); }

/* ---------- Playhead ---------- */

.playhead {
  position: absolute;
  top: 0;
  left: 0;
  height: 0;            /* set to one box's height in JS */
  width: 4px;
  margin-left: -2px;
  border-radius: 2px;
  background: rgba(59, 130, 246, 0.35);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
.playhead.visible { opacity: 1; }

/* ---------- Songs panel ---------- */

.btn-songs { background: #6366F1; }

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.35);
}
.sheet-backdrop[hidden] { display: none; }

.sheet {
  width: min(920px, 100%);
  max-height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  padding: 20px 24px 28px;
  background: #fff;
  border-radius: 28px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.25);
}

.sheet-head { display: flex; align-items: center; gap: 12px; }

.pill {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 64px;
  padding: 0 26px 0 20px;
  border: none;
  border-radius: 20px;
  font: 700 22px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.14);
  transition: transform 0.1s, box-shadow 0.1s;
}
.pill:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.14); }
.pill svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.pill-save { background: #22C55E; color: #fff; }
.pill-new { background: #f1f5f9; color: #334155; }

.sheet-close {
  margin-left: auto;
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: #f1f5f9;
  color: #475569;
  cursor: pointer;
  touch-action: manipulation;
}
.sheet-close svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; }

.sheet h2 {
  margin: 26px 4px 12px;
  font: 700 20px/1.2 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #64748b;
}

.song-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 16px;
}

.song-card {
  position: relative;
  padding: 10px;
  background: #eef2f7;
  border-radius: 18px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s;
}
.song-card:active { transform: scale(0.97); }
.song-card svg { display: block; width: 100%; height: auto; }
.song-name {
  display: block;
  margin-top: 8px;
  font: 600 17px/1.2 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #334155;
  text-align: center;
}
.song-delete {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: #ef4444;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.2);
  cursor: pointer;
  touch-action: manipulation;
}
.song-delete svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 3.5; stroke-linecap: round; }

.song-card.just-saved { animation: pop 0.6s ease-out; }
@keyframes pop {
  0% { transform: scale(0.8); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.6); }
  60% { transform: scale(1.05); box-shadow: 0 0 0 8px rgba(34, 197, 94, 0.25); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

.empty {
  padding: 6px 4px;
  font: 500 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #94a3b8;
}

/* ---------- Narrow screens (portrait iPad mini, etc.) ---------- */

@media (max-width: 860px) {
  .toolbar { min-height: 104px; gap: 8px; }
  .tempo { height: 46px; padding: 0 10px; gap: 6px; }
  .tempo-icon { font-size: 24px; }
  .tempo-value { display: none; }
  .btn { width: 76px; height: 76px; border-radius: 22px; }
  .btn svg { width: 42px; height: 42px; }
  .transport { gap: 10px; }
  .lines-picker { gap: 6px; padding: 6px 8px; }
  .lines-icon { display: none; }
  .lines-buttons { gap: 4px; }
  .lines-buttons button { width: 38px; height: 46px; font-size: 20px; border-radius: 13px; }
  .wheel { width: 104px; height: 104px; }
  .right-controls { gap: 8px; }
  .view-toggle { gap: 4px; padding: 5px; }
  .view-toggle button { width: 46px; height: 42px; font-size: 24px; }
  .acc-toggle { width: 104px; height: 104px; gap: 6px; }
  .acc-glyph { font-size: 36px; }
  .acc-swatches i { width: 10px; height: 10px; }
}

/* iPad mini portrait and narrower: tighten a bit more so everything fits. */
@media (max-width: 780px) {
  .toolbar { gap: 6px; }
  .btn { width: 64px; height: 64px; border-radius: 18px; }
  .btn svg { width: 34px; height: 34px; }
  .transport { gap: 8px; }
  .wheel, .acc-toggle { width: 92px; height: 92px; }
}
