/* ============================================================
   Musica — Style Sheet
   Dark theme, colourful, clean, approachable for all ages.

   Key layout concepts:
   ─ app-header    : fixed top bar
   ─ app-main      : fills the remaining viewport height
       ├ tracks-panel    : narrow left sidebar (track controls)
       └ piano-roll-section : fills the rest
           ├ piano-roll-top  : sticky ruler + spacer
           └ piano-roll-body : scrollable note-labels + grid
============================================================ */

/* ----------------------------------------------------------
   1. Reset + CSS Variables
---------------------------------------------------------- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* Backgrounds */
  --bg-app:    #0f1117;
  --bg-panel:  #1a1d27;
  --bg-card:   #22263a;
  --bg-hover:  #2a2f45;

  /* Text */
  --text-primary:   #e8eaf6;
  --text-secondary: #8892b0;
  --text-muted:     #4a5578;

  /* Borders */
  --border:       #2e3350;
  --border-light: #3d4468;

  /* Per-track accent colours (up to 8 tracks) */
  --track-0: #ff6b6b;  /* coral red  */
  --track-1: #ffd93d;  /* golden     */
  --track-2: #6bcb77;  /* mint green */
  --track-3: #4d96ff;  /* sky blue   */
  --track-4: #c77dff;  /* purple     */
  --track-5: #ff9f40;  /* orange     */
  --track-6: #00d4d4;  /* teal       */
  --track-7: #ff6eb4;  /* pink       */

  /* Sizes */
  --cell-w:          26px;   /* width  of each grid cell */
  --cell-h:          24px;   /* height of each grid cell */
  --note-label-w:    44px;   /* width of the note-name column */
  --ruler-h:         30px;   /* height of the step ruler row  */
  --header-h:        60px;   /* height of the top header bar  */
  --track-strip-h:   48px;   /* height of each track strip in left panel */

  /* Misc */
  --radius-sm: 5px;
  --radius-md: 10px;
  --radius-lg: 16px;
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg-app);
  color: var(--text-primary);
  overflow: hidden;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-panel); }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }

/* ----------------------------------------------------------
   2. Header
---------------------------------------------------------- */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 100;
  flex-wrap: wrap;
  overflow: hidden;
}

.header-left {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-shrink: 0;
}

.app-title {
  font-size: 1.55rem;
  font-weight: 800;
  background: linear-gradient(135deg, #ff6b6b 0%, #ffd93d 40%, #6bcb77 70%, #4d96ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
  line-height: 1;
}

.app-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* ----------------------------------------------------------
   3. Transport + Buttons
---------------------------------------------------------- */
.transport {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Base button */
.btn {
  padding: 7px 15px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.14s, transform 0.1s, box-shadow 0.14s;
  white-space: nowrap;
  font-family: inherit;
}
.btn:active { transform: scale(0.95); }

.btn-play  { background: #4d96ff; color: #fff; }
.btn-play:hover  { background: #6aaeff; box-shadow: 0 0 12px #4d96ff66; }

.btn-stop  { background: var(--bg-card); color: var(--text-primary); border: 1px solid var(--border-light); }
.btn-stop:hover  { background: var(--bg-hover); }

.btn-clear { background: var(--bg-card); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-clear:hover { background: #ff6b6b22; color: #ff6b6b; border-color: #ff6b6b55; }

.btn-export {
  background: linear-gradient(135deg, #00c9a7 0%, #1aab6d 100%);
  color: #fff;
  font-weight: 700;
}
.btn-export:hover  { box-shadow: 0 0 14px #00c9a755; filter: brightness(1.1); }
.btn-export:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  filter: none;
  transform: none;
}

.export-status {
  font-size: 0.82rem;
  color: #00c9a7;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.3s;
  white-space: nowrap;
}
.export-status.visible { opacity: 1; }

.btn-share {
  background: linear-gradient(135deg, #6bcb77, #4d96ff);
  color: #fff;
  font-weight: 700;
}
.btn-share:hover { box-shadow: 0 0 14px #4d96ff55; filter: brightness(1.1); }

/* BPM widget */
.bpm-control {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 5px 12px;
  flex-shrink: 0;
}

.bpm-label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.bpm-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 120px;
  height: 6px;
  background: var(--border-light);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}
.bpm-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px; height: 18px;
  background: #4d96ff;
  border-radius: 50%;
  border: 2px solid #fff;
  cursor: pointer;
  transition: box-shadow 0.14s;
}
.bpm-slider:hover::-webkit-slider-thumb { box-shadow: 0 0 10px #4d96ff88; }

.bpm-number {
  width: 64px;
  background: var(--bg-app);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  padding: 2px 4px;
  font-family: inherit;
  /* Hide browser spinner arrows — the slider already provides step control */
  -moz-appearance: textfield;
}
.bpm-number::-webkit-inner-spin-button,
.bpm-number::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.bpm-number:focus { outline: 2px solid #4d96ff; }

/* Share confirm */
.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-shrink: 0;
}

.share-confirm {
  font-size: 0.82rem;
  color: #6bcb77;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.3s;
  white-space: nowrap;
}
.share-confirm.visible { opacity: 1; }

/* ----------------------------------------------------------
   4. Main Layout
---------------------------------------------------------- */
.app-main {
  display: flex;
  height: calc(100vh - var(--header-h));
  overflow: hidden;
}

/* ----------------------------------------------------------
   5. Instruments Panel (left sidebar)
      Shows all 8 instruments. Each row has an on/off toggle so
      multiple instruments can be active and playing simultaneously.
---------------------------------------------------------- */
.instruments-panel {
  width: 220px;
  flex-shrink: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

/* Panel header label */
.panel-header {
  height: calc(var(--ruler-h) + 1px);  /* matches step-ruler height */
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px 0 12px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

/* Toggle-all button in the panel header */
.toggle-all-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 2px 7px;
  cursor: pointer;
  font-family: inherit;
  text-transform: uppercase;
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.toggle-all-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.toggle-all-btn.all-on {
  background: rgba(77, 150, 255, 0.15);
  border-color: rgba(77, 150, 255, 0.4);
  color: #4d96ff;
}

/* Individual instrument row */
.instrument-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px 0 12px;
  height: 48px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s;
  position: relative;
  user-select: none;
  /* --instr-color is set inline per-row by JS */
}
.instrument-row:hover { background: var(--bg-hover); }

/* .visible = pill is ON (notes shown on grid, plays during playback) */
.instrument-row.visible {
  background: rgba(255,255,255,0.025);
}
.instrument-row.visible::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--instr-color, #4d96ff);
  border-radius: 0 2px 2px 0;
}

/* .selected = this instrument is the current editing target */
.instrument-row.selected {
  background: color-mix(in srgb, var(--instr-color, #4d96ff) 12%, var(--bg-hover));
}

/* Glowing left bar for selected row (overrides .visible's ::before) */
.instrument-row.selected::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 4px;
  background: var(--instr-color, #4d96ff);
  border-radius: 0 2px 2px 0;
  box-shadow: 0 0 10px var(--instr-color, #4d96ff);
}

/* Toggle pill (iOS-style switch) */
.instr-toggle {
  width: 34px;
  height: 18px;
  border-radius: 9px;
  background: var(--border-light);
  position: relative;
  flex-shrink: 0;
  transition: background 0.2s;
}
.instrument-row.visible .instr-toggle {
  background: var(--instr-color, #4d96ff);
  box-shadow: 0 0 8px var(--instr-color, #4d96ff);
}
.instr-toggle::after {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  background: #fff;
  border-radius: 50%;
  top: 2px; left: 2px;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.instrument-row.visible .instr-toggle::after {
  transform: translateX(16px);
}

/* Instrument emoji */
.instr-emoji {
  font-size: 1.1rem;
  flex-shrink: 0;
  width: 22px;
  text-align: center;
}

/* Instrument name */
.instr-name {
  flex: 1;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.12s;
}
.instrument-row.selected .instr-name {
  color: var(--text-primary);
  font-weight: 700;
}

/* Edit dot — grows and glows on the selected instrument row */
.instr-edit-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  background: transparent;
  transition: background 0.15s, box-shadow 0.15s, width 0.15s, height 0.15s;
}
.instrument-row.selected .instr-edit-dot {
  width: 12px;
  height: 12px;
  background: var(--instr-color, #4d96ff);
  box-shadow: 0 0 8px var(--instr-color, #4d96ff), 0 0 18px var(--instr-color, #4d96ff);
}

/* Per-instrument volume slider */
.instr-volume {
  -webkit-appearance: none;
  appearance: none;
  width: 40px; height: 4px;
  background: var(--border-light);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  flex-shrink: 0;
}
.instr-volume::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px; height: 12px;
  background: var(--instr-color, #4d96ff);
  border-radius: 50%;
  cursor: pointer;
}
/* Prevent slider click from toggling the row */
.instr-volume { pointer-events: auto; }

/* ----------------------------------------------------------
   6. Piano Roll Section (right side of app-main)
---------------------------------------------------------- */
.piano-roll-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Top row: spacer + step ruler ─────────────────────────── */
.piano-roll-top {
  display: flex;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  position: sticky;
  top: 0;
  z-index: 20;
}

/* Spacer that aligns with the note-labels column below */
.note-label-spacer {
  width: var(--note-label-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border-light);
}

/* Step ruler */
.step-ruler {
  display: flex;
  height: var(--ruler-h);
  overflow: hidden; /* synced by scroll parent */
}

.step-ruler-cell {
  width: var(--cell-w);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.58rem;
  color: var(--text-muted);
  font-weight: 600;
  border-right: 1px solid var(--border);
}

/* Every 4th step = bar line (slightly brighter) */
.step-ruler-cell.bar-start {
  color: var(--text-secondary);
  background: rgba(255,255,255,0.025);
}

/* Body row: note labels + grid ─────────────────────────── */
.piano-roll-body {
  display: flex;
  flex: 1;
  overflow: auto; /* allows scrolling both axes */
}

/* Note name labels column */
.note-labels {
  width: var(--note-label-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border-light);
  background: var(--bg-panel);
  position: sticky;
  left: 0;
  z-index: 10;
}

.note-label {
  height: var(--cell-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

/* C notes (octave boundaries) are highlighted */
.note-label.octave-c {
  color: var(--text-secondary);
  background: rgba(255,255,255,0.04);
  font-size: 0.64rem;
}

/* Sharp notes (#) get a very subtle dark tint — like black piano keys */
.note-label.is-sharp {
  background: rgba(0,0,0,0.15);
  color: var(--text-muted);
}

/* ----------------------------------------------------------
   7. The Grid (piano roll cells)
---------------------------------------------------------- */
.piano-roll {
  /* CSS grid: 64 columns × 24 rows, each cell is --cell-w × --cell-h */
  display: grid;
  grid-template-columns: repeat(64, var(--cell-w));
  grid-template-rows:    repeat(24, var(--cell-h));
  position: relative;
  flex-shrink: 0;
}

/* Individual cell */
.cell {
  width:  var(--cell-w);
  height: var(--cell-h);
  border-right:  1px solid var(--border);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.07s;
  position: relative;
  user-select: none;
}

/* Darker background for sharp-note rows (like black piano keys) */
.cell.row-sharp {
  background: rgba(0, 0, 0, 0.18);
}

/* Bar-line: left border every 4 steps */
.cell.bar-start {
  border-left: 1px solid var(--border-light);
}

/* Subtle hover */
.cell:hover { background: var(--bg-hover); }
.cell.row-sharp:hover { background: var(--bg-hover); }

/* ACTIVE (note on) — background/box-shadow/border set inline by JS (refreshCell).
   Only structural properties here. */
.cell.active {
  z-index: 1;
}
.cell.active:hover { filter: brightness(1.18); }

/* Playhead column */
.cell.playhead { background: rgba(255,255,255,0.10) !important; }
.cell.row-sharp.playhead { background: rgba(255,255,255,0.07) !important; }
.cell.active.playhead    { filter: brightness(1.35); }

/* Copy/paste selection */
.cell.selected {
  outline: 2px solid rgba(255, 255, 255, 0.55);
  outline-offset: -2px;
  border-radius: 3px;
  z-index: 2;
}

/* Click pop animation */
@keyframes cellPop {
  0%   { transform: scale(1);    }
  40%  { transform: scale(1.15); }
  100% { transform: scale(1);    }
}
.cell.pop { animation: cellPop 0.16s ease; }

/* ----------------------------------------------------------
   8. Responsive / Tablet
---------------------------------------------------------- */
@media (max-width: 768px) {
  :root {
    --cell-w: 20px;
    --cell-h: 20px;
    --note-label-w: 36px;
  }
  .instruments-panel { width: 170px; }
  .instr-name        { font-size: 0.7rem; }
  .bpm-slider        { width: 80px; }
  .btn               { padding: 6px 10px; font-size: 0.8rem; }
  .instr-volume      { display: none; }
}

@media (max-width: 520px) {
  .instruments-panel { width: 120px; }
  .instr-volume      { display: none; }
  .instr-name        { font-size: 0.65rem; }
  .note-label-spacer, .note-labels { width: 28px; --note-label-w: 28px; }
}

/* ----------------------------------------------------------
   10. Animations
---------------------------------------------------------- */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: translateY(0);   }
}
.app-header { animation: fadeIn 0.3s ease both; }
.app-main   { animation: fadeIn 0.35s ease 0.06s both; }

/* ----------------------------------------------------------
   11. Welcome banner — first-visit only
---------------------------------------------------------- */
.welcome-banner {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: linear-gradient(135deg, #4d96ff 0%, #6c47ff 100%);
  color: #fff;
  padding: 11px 20px 11px 18px;
  border-radius: 40px;
  font-size: 0.88rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  box-shadow: 0 4px 28px rgba(77, 150, 255, 0.5), 0 0 0 1px rgba(255,255,255,0.12) inset;
  opacity: 0;
  transition: opacity 0.4s ease, transform 0.4s ease;
  pointer-events: none;
  z-index: 200;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  gap: 10px;
}
.welcome-banner.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}
.welcome-dismiss {
  margin-left: 10px;
  opacity: 0.6;
  font-size: 0.78rem;
  vertical-align: middle;
  transition: opacity 0.2s;
}
.welcome-banner:hover .welcome-dismiss { opacity: 1; }
