* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg:          #FAF1DD;
  --surface:     #E8F7F6;
  --surface2:    #C8EDEA;
  --border:      #9FDDD7;
  --accent:      #F08766;
  --accent-h:    #D96A48;
  --text:        #2A3530;
  --text-muted:  #6A8480;
  --danger:      #D63C2A;

  /* playful tokens — derived from the theme vars where possible */
  --radius:      18px;
  --radius-sm:   12px;
  --radius-xs:   8px;
  --shadow-sm:   0 2px 6px rgba(0, 0, 0, 0.06);
  --shadow:      0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg:   0 6px 18px rgba(0, 0, 0, 0.10);
  --accent-glow: 0 6px 14px rgba(240, 135, 102, 0.30);
}

body {
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 0% 0%, rgba(255, 255, 255, 0.35), transparent 45%),
    radial-gradient(circle at 100% 100%, rgba(255, 255, 255, 0.25), transparent 45%);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Quicksand', 'Segoe UI', system-ui, sans-serif;
  font-weight: 500;
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Header ───────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

header h1 {
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.01em;
}

header p {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 500;
}

#theme-picker {
  display: flex;
  gap: 7px;
  align-items: center;
  background: var(--surface);
  padding: 6px 10px;
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
}

#mode-toggle-group {
  display: inline-flex;
  margin-left: auto;
  margin-right: 8px;
  background: var(--surface);
  border-radius: 999px;
  padding: 3px;
  box-shadow: var(--shadow-sm);
  gap: 2px;
}

.mode-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  transition: background 0.15s, color 0.15s, transform 0.12s;
}
.mode-toggle-btn svg { width: 16px; height: 16px; }
.mode-toggle-btn:hover {
  color: var(--accent);
  transform: scale(1.08);
}
.mode-toggle-btn.active {
  background: var(--bg);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.theme-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.18s cubic-bezier(.34, 1.56, .64, 1), border-color 0.12s;
  flex-shrink: 0;
}

.theme-dot:hover  { transform: scale(1.25) rotate(8deg); }
.theme-dot.active { border-color: var(--text); transform: scale(1.15); }

#lang-toggle {
  background: var(--surface);
  border: none;
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s, color 0.15s;
}

#btn-help {
  background: var(--surface);
  border: none;
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s, color 0.15s;
}
#btn-help svg { width: 16px; height: 16px; }
#btn-help:hover {
  color: var(--accent);
  transform: translateY(-1px) scale(1.05);
}

#lang-toggle:hover {
  transform: translateY(-1px);
  color: var(--accent);
}

/* ── Main layout ──────────────────────────────────────────── */
#main {
  display: flex;
  flex: 1;
  gap: 14px;
  padding: 4px 20px 18px;
  overflow: hidden;
}

#sidebar-left,
#sidebar-right {
  width: 200px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding-right: 4px;
}

#sidebar-left::-webkit-scrollbar,
#sidebar-right::-webkit-scrollbar { width: 6px; }
#sidebar-left::-webkit-scrollbar-thumb,
#sidebar-right::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.12);
  border-radius: 3px;
}

/* ── Panels (now floating cards) ──────────────────────────── */
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 13px 15px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  display: flex;
  flex-direction: column;
  gap: 9px;
  transition: box-shadow 0.2s, transform 0.2s, padding 0.2s;
}

.panel:hover {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.panel h3 {
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

/* Collapse button inside h3 */
.collapse-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 2px;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s, transform 0.25s ease;
}
.collapse-btn svg { width: 14px; height: 14px; pointer-events: none; }
.collapse-btn:hover { color: var(--accent); background: rgba(0,0,0,0.04); }

.panel-body {
  display: flex;
  flex-direction: column;
  gap: 9px;
  overflow: hidden;
  transition: max-height 0.28s ease, opacity 0.18s ease, margin 0.18s ease;
  max-height: 800px;
  opacity: 1;
}

.panel.collapsed .panel-body {
  max-height: 0;
  opacity: 0;
  margin-top: -9px;        /* swallow the .panel gap above */
  pointer-events: none;
}
.panel.collapsed { padding-bottom: 9px; }
.panel.collapsed .collapse-btn { transform: rotate(-90deg); }

.panel h3 .val,
.panel h3 #brush-size-val,
.panel h3 #sampling-val,
.panel h3 #bpm-val {
  background: var(--surface2);
  color: var(--accent);
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
  font-family: 'Quicksand', sans-serif;
}

/* ── Canvas area ──────────────────────────────────────────── */
#canvas-area {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: stretch;
  justify-content: center;
  overflow: hidden;
}

#canvas-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  max-width: 100%;
  min-height: 0;
}

/* Row that holds: mode-toggle | canvas-container | view-toggle
   Putting the toggles in here (instead of next to canvas-wrap) lets them
   share the row's align-items: center anchor with the canvas, so the
   bookmark tabs stay vertically centered on the actual canvas even when
   the layer-bar above takes up some height. */
#canvas-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex: 1;
  min-height: 0;
}

/* Column with layer-bar + canvas-container — its width matches the
   canvas, so the layer tabs above never extend past the canvas edges.
   container-type: inline-size lets descendant elements size themselves
   with cqw units, scaling to the actual canvas width rather than the
   whole viewport (so sidebars eating viewport space doesn't break the
   scaling logic). */
#canvas-col {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  container-type: inline-size;
  container-name: canvas;
}
#canvas-col #canvas-container { flex: 1; min-width: 0; }

/* View toggle — vertical bookmark tabs on right edge of canvas */
#view-toggle {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-left: 4px;
  z-index: 10;
  flex-shrink: 0;
}

/* Mode toggle — vertical bookmark tabs on LEFT edge (mirror of view-toggle) */
#mode-toggle {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-right: 4px;
  z-index: 10;
  flex-shrink: 0;
}

.mode-tab {
  background: var(--surface);
  border: none;
  color: var(--text-muted);
  /* Vertical padding and font scale with viewport so the bookmark tabs
     shrink alongside a narrower canvas instead of looking oversized. */
  padding: clamp(13px, 2.6vw, 22px) clamp(5px, 0.8vw, 7px);
  border-radius: 14px 0 0 14px;
  cursor: pointer;
  font-family: inherit;
  font-size: clamp(0.62rem, 1.2vw, 0.74rem);
  font-weight: 600;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  letter-spacing: 0.06em;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(5px, 1vw, 8px);
  box-shadow: none;
  transition: color 0.15s, transform 0.15s;
}

.mode-tab svg {
  width: clamp(12px, 1.6vw, 16px);
  height: clamp(12px, 1.6vw, 16px);
  transform: rotate(0deg);
  flex-shrink: 0;
}

.mode-tab:hover {
  color: var(--text);
  transform: translateX(-2px);
}

.mode-tab.active {
  color: var(--accent);
  font-weight: 700;
}

.view-btn {
  background: var(--surface);
  border: none;
  color: var(--text-muted);
  padding: clamp(13px, 2.6vw, 22px) clamp(5px, 0.8vw, 7px);
  border-radius: 0 14px 14px 0;
  cursor: pointer;
  font-family: inherit;
  font-size: clamp(0.62rem, 1.2vw, 0.74rem);
  font-weight: 600;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  letter-spacing: 0.06em;
  white-space: nowrap;
  box-shadow: none;
  transition: color 0.15s, transform 0.15s;
}

.view-btn:hover {
  color: var(--text);
  transform: translateX(2px);
}

.view-btn.active {
  color: var(--accent);
  font-weight: 700;
}

#canvas-container {
  position: relative;
  max-width: 100%;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) var(--radius) var(--radius);
  overflow: hidden;
  cursor: crosshair;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

#canvas-container.upload-mode {
  cursor: default;
}

/* ── Upload dropzone (center + sign) ──────────────────────── */
#upload-dropzone {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  /* Theme-aware frosted tint — uses --surface so the dropzone reads as
     the same color family as the current palette (noir/jade/forest all
     get their own surface color instead of a hard-coded white wash). */
  background: linear-gradient(
    135deg,
    color-mix(in srgb, var(--surface) 72%, transparent),
    color-mix(in srgb, var(--surface) 38%, transparent)
  );
  backdrop-filter: blur(10px) saturate(1.1);
  -webkit-backdrop-filter: blur(10px) saturate(1.1);
  cursor: pointer;
  z-index: 5;
  outline: none;
  transition: background 0.2s, backdrop-filter 0.2s;
}

#upload-dropzone.drag-over {
  background: linear-gradient(
    135deg,
    color-mix(in srgb, var(--accent) 28%, transparent),
    color-mix(in srgb, var(--accent) 10%, transparent)
  );
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
}

.dz-plus {
  position: relative;
  width: 120px;
  height: 120px;
  display: grid;
  place-items: center;
  color: var(--accent);
}

.dz-rings {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.dz-rings .ring {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-dasharray: 5 7;
  transform-origin: 50% 50%;
}
.dz-rings .r1 { opacity: 0.35; animation: dzSpinSlow 18s linear infinite; }
.dz-rings .r2 { opacity: 0.55; animation: dzSpinRev  14s linear infinite; }
.dz-rings .r3 { opacity: 0.75; animation: dzSpinSlow 10s linear infinite; }

@keyframes dzSpinSlow { from {transform: rotate(0)} to {transform: rotate(360deg)} }
@keyframes dzSpinRev  { from {transform: rotate(360deg)} to {transform: rotate(0)} }

.dz-icon {
  position: relative;
  width: 56px;
  height: 56px;
  z-index: 1;
  color: var(--accent);
  background: var(--surface);
  border-radius: 50%;
  padding: 12px;
  box-shadow:
    0 4px 16px rgba(240, 135, 102, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  animation: dzBreathe 2.6s ease-in-out infinite;
}

@keyframes dzBreathe {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.08); }
}

#upload-dropzone.drag-over .dz-icon {
  animation: none;
  transform: scale(1.18);
  box-shadow:
    0 8px 22px rgba(240, 135, 102, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.dz-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  pointer-events: none;
}

.dz-text strong {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
}

.dz-text span {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* ── Attention pulse (used after image import) ────────────── */
#btn-convert.attention-pulse {
  animation: btnAttentionPulse 1.3s ease-in-out infinite;
}
@keyframes btnAttentionPulse {
  0%, 100% {
    box-shadow:
      0 0 0 0 rgba(240, 135, 102, 0.55),
      var(--accent-glow);
  }
  50% {
    box-shadow:
      0 0 0 10px rgba(240, 135, 102, 0),
      var(--accent-glow);
  }
}

#main-canvas,
#piano-roll-canvas,
#workspace-canvas {
  display: block;
  max-width: 100%;
  aspect-ratio: 9 / 5;
  background: var(--surface);
}

/* ── Layer bar ────────────────────────────────────────────── */
#layer-bar {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  margin-bottom: -2px;
  z-index: 1;
  padding-left: 10px;
}

#layer-tabs {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 4px;
}

#layer-props {
  display: flex;
  align-items: center;
  gap: clamp(6px, 1.53cqw, 9px);
  background: var(--surface);
  border-radius: 14px 14px 0 0;
  padding: clamp(4px, 1.02cqw, 6px) clamp(9px, 2.38cqw, 14px);
  margin-left: clamp(5px, 1.36cqw, 8px);
  font-size: clamp(0.64rem, 1.87cqw, 0.74rem);
  color: var(--text-muted);
}

#layer-inst-select {
  width: auto;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: clamp(0.66rem, 1.95cqw, 0.78rem);
  font-weight: 600;
  padding: 0 2px;
  cursor: pointer;
  outline: none;
  box-shadow: none;
}

#layer-inst-select:focus { outline: none; border: none; }

.lp-sep {
  width: 1px;
  height: clamp(11px, 2.55cqw, 14px);
  background: var(--border);
}

.lp-label {
  font-size: clamp(0.62rem, 1.78cqw, 0.72rem);
  color: var(--text-muted);
  cursor: default;
  font-weight: 600;
}

#layer-props input[type="range"] {
  width: clamp(50px, 12.75cqw, 72px);
  height: clamp(4px, 1.1cqw, 6px);
}

#layer-vol-val {
  min-width: clamp(18px, 3.91cqw, 22px);
  text-align: right;
  font-size: clamp(0.64rem, 1.87cqw, 0.74rem);
  font-weight: 700;
  color: var(--text);
}

.layer-tab {
  background: rgba(255, 255, 255, 0.6);
  border: none;
  border-radius: 14px 14px 0 0;
  color: var(--text-muted);
  padding: clamp(5px, 1.44cqw, 7px) clamp(9px, 2.38cqw, 13px);
  font-family: inherit;
  font-size: clamp(0.66rem, 1.87cqw, 0.76rem);
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: clamp(4px, 1.19cqw, 6px);
  transition: color 0.12s, background 0.12s;
  white-space: nowrap;
}

.layer-tab:hover { color: var(--text); background: rgba(255, 255, 255, 0.85); }

.layer-tab.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  box-shadow: 0 -3px 10px rgba(0, 0, 0, 0.05);
}

.layer-dot {
  width: clamp(7px, 1.87cqw, 9px);
  height: clamp(7px, 1.87cqw, 9px);
  border-radius: 50%;
  flex-shrink: 0;
}

.layer-name-input {
  width: 44px;
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--accent);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  outline: none;
  padding: 0;
  min-width: 0;
}

#btn-add-layer {
  background: transparent;
  border: 2px dashed rgba(0, 0, 0, 0.18);
  border-bottom: none;
  border-radius: 14px 14px 0 0;
  color: var(--text-muted);
  padding: clamp(3px, 1.1cqw, 5px) clamp(8px, 2.04cqw, 11px);
  font-size: clamp(0.84rem, 2.38cqw, 1rem);
  font-weight: 700;
  cursor: pointer;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s;
}

#btn-add-layer:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ── Partial overlay ──────────────────────────────────────── */
#partial-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#partial-overlay.active { pointer-events: auto; }

#partial-band {
  position: absolute;
  left: 0;
  right: 0;
  height: 80px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(240, 135, 102, 0.14);
  border-top: 2px solid var(--accent);
  border-bottom: 2px solid var(--accent);
  cursor: move;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

#partial-band:hover { background: rgba(240, 135, 102, 0.22); }

.band-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--accent);
  user-select: none;
  pointer-events: none;
}

.band-handle {
  position: absolute;
  left: 0;
  right: 0;
  height: 12px;
  cursor: ns-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}
.band-handle-top    { top: -7px; }
.band-handle-bottom { bottom: -7px; }
.band-handle::after {
  content: '';
  width: 48px;
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
  opacity: 0.7;
  transition: opacity 0.15s, width 0.15s;
}
.band-handle:hover::after {
  opacity: 1;
  width: 64px;
}

.hidden { display: none !important; }

/* ── Canvas empty-state hint ──────────────────────────────── */
.canvas-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 8px;
  color: var(--text-muted);
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-size: 1.1rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  pointer-events: none;
  opacity: 0.55;
  transition: opacity 0.5s ease;
  user-select: none;
  z-index: 1;
}
.canvas-hint .hint-icon {
  font-size: 1.8rem;
  opacity: 0.85;
}
#canvas-container.has-drawn .canvas-hint {
  opacity: 0;
}

/* ── Tool grid ────────────────────────────────────────────── */
.tool-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.tool-btn {
  background: var(--surface2);
  border: 2px solid transparent;
  color: var(--text-muted);
  padding: 9px 6px 7px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 600;
  text-align: center;
  transition: transform 0.18s cubic-bezier(.34, 1.56, .64, 1),
              background 0.18s, color 0.18s, box-shadow 0.18s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.08),
    0 2px 5px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.tool-btn svg,
.tool-btn > span,
.mode-btn svg,
.mode-btn > span,
.mode-btn > .ico {
  pointer-events: none;
}

.tool-btn svg {
  width: 22px;
  height: 22px;
}

.tool-btn:hover {
  transform: translateY(-2px);
  color: var(--text);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.10),
    0 5px 12px rgba(0, 0, 0, 0.10),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.tool-btn:active {
  transform: translateY(0);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, 0.06),
    inset 0 2px 4px rgba(0, 0, 0, 0.08);
}

.tool-btn.active {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-h) 100%);
  border-color: transparent;
  color: #fff;
  font-weight: 700;
  box-shadow:
    var(--accent-glow),
    inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

/* ── Mode buttons (vertical list) ─────────────────────────── */
.btn-group-v {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.mode-btn {
  background: var(--surface2);
  border: 2px solid transparent;
  color: var(--text-muted);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 600;
  text-align: left;
  transition: transform 0.15s, background 0.15s, color 0.15s, box-shadow 0.15s;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.08),
    0 2px 5px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.mode-btn .ico {
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border-radius: 7px;
  color: var(--accent);
  flex-shrink: 0;
}

.mode-btn:hover {
  color: var(--text);
  transform: translateY(-1px);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.10),
    0 5px 12px rgba(0, 0, 0, 0.10),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.mode-btn:active {
  transform: translateY(0);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, 0.06),
    inset 0 2px 4px rgba(0, 0, 0, 0.08);
}

.mode-btn.active {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-h) 100%);
  color: #fff;
  font-weight: 700;
  box-shadow:
    var(--accent-glow),
    inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.mode-btn.active .ico {
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
}

/* ── Action buttons ───────────────────────────────────────── */
.btn-primary {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-h) 100%);
  border: none;
  color: #fff;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  box-shadow: var(--accent-glow);
  transition: transform 0.15s, box-shadow 0.15s, opacity 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary svg, .btn-secondary svg { pointer-events: none; }

.btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(240, 135, 102, 0.40);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: default;
  box-shadow: none;
}

/* Convert is the preparatory step → quieter than Play */
#btn-convert {
  background: var(--surface2);
  color: var(--accent);
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.08),
    0 3px 6px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.45);
}
#btn-convert:hover:not(:disabled) {
  background: var(--surface);
  transform: translateY(-2px);
  box-shadow:
    0 3px 6px rgba(0, 0, 0, 0.10),
    0 6px 14px rgba(0, 0, 0, 0.10),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
}
#btn-convert:active:not(:disabled) {
  transform: translateY(0);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, 0.06),
    inset 0 2px 4px rgba(0, 0, 0, 0.08);
}
/* Play is the only glowing hero button — uses theme accent for brand consistency */

.btn-secondary {
  background: var(--surface);
  border: 2px solid transparent;
  color: var(--text-muted);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 600;
  transition: color 0.15s, border-color 0.15s, background 0.15s, transform 0.15s, box-shadow 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.07),
    0 2px 5px rgba(0, 0, 0, 0.05),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.btn-secondary:hover {
  color: var(--accent);
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.09),
    0 5px 12px rgba(0, 0, 0, 0.09),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.btn-secondary:active {
  transform: translateY(0);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, 0.06),
    inset 0 2px 4px rgba(0, 0, 0, 0.08);
}

#btn-clear {
  color: var(--danger);
  border-color: rgba(214, 60, 42, 0.25);
}

#btn-clear:hover {
  background: rgba(214, 60, 42, 0.06);
  border-color: var(--danger);
  color: var(--danger);
}

/* Undo / Redo paired row */
.undo-redo-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.undo-redo-row .btn-secondary {
  padding: 8px 6px;
  font-size: 0.78rem;
}
.undo-redo-row .btn-secondary:disabled {
  opacity: 0.4;
  cursor: default;
  color: var(--text-muted);
  box-shadow: none;
  transform: none;
}
.undo-redo-row .btn-secondary:disabled:hover {
  color: var(--text-muted);
  border-color: transparent;
  background: var(--surface);
  transform: none;
  box-shadow: none;
}

.full-width { width: 100%; }

/* ── Range inputs (custom thumb) ──────────────────────────── */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 8px;
  background: var(--surface2);
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  background: linear-gradient(135deg, var(--accent), var(--accent-h));
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
  border: 3px solid var(--surface);
}
input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: 3px solid var(--surface);
}

/* ── Color swatches ───────────────────────────────────────── */
.color-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  /* Buffer so active rings + hover scale aren't clipped by panel-body overflow */
  padding: 4px 4px 6px;
}

.color-swatch {
  aspect-ratio: 1;
  border-radius: 50%;
  cursor: pointer;
  border: none;
  transition: transform 0.15s cubic-bezier(.34, 1.56, .64, 1), box-shadow 0.15s;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.10);
}

.color-swatch:hover {
  transform: scale(1.15) rotate(4deg);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.14);
}

.color-swatch.active {
  transform: scale(1.05);
  /* Single 2px accent ring — fits inside the 4px .color-row padding so it
     never bleeds against the panel-body's overflow:hidden clip. */
  box-shadow:
    0 0 0 2px var(--accent),
    0 1px 3px rgba(0, 0, 0, 0.14);
}

/* ── Select ───────────────────────────────────────────────── */
select {
  width: 100%;
  background-color: var(--surface2);
  border: 2px solid transparent;
  color: var(--text);
  padding: 9px 30px 9px 12px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236a8480' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

select:focus { outline: none; border-color: var(--accent); }

/* ── Play controls ────────────────────────────────────────── */
#play-controls {
  display: flex;
  flex-direction: column;
  gap: 7px;
  /* sit right under the panel above (no margin-top:auto) */
  background: var(--surface);
  border-radius: var(--radius);
  padding: 13px 15px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.export-row {
  display: flex;
  gap: 5px;
}

.export-row select {
  flex: 0 0 auto;
  width: 78px;
  padding: 9px 24px 9px 10px;
  font-size: 0.78rem;
}

.export-row button { flex: 1; }

.project-io-row {
  display: flex;
  gap: 5px;
  margin-top: 5px;
}
.project-io-row button {
  flex: 1;
  font-size: 0.76rem;
  padding: 8px 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

/* ── Confirm dialog (Convert overwrite, etc.) ────────────── */
#confirm-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: cdFadeIn 0.18s ease-out;
}
@keyframes cdFadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.confirm-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
  padding: 22px 22px 18px;
  max-width: min(92vw, 380px);
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  animation: cdScale 0.22s cubic-bezier(.34, 1.56, .64, 1);
}
@keyframes cdScale {
  from { opacity: 0; transform: scale(0.94); }
  to   { opacity: 1; transform: scale(1); }
}
.confirm-msg {
  font-size: 0.94rem;
  font-weight: 500;
  color: var(--text);
  line-height: 1.5;
  margin-bottom: 18px;
}
.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.confirm-btn {
  border: none;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.12s, background 0.12s, box-shadow 0.12s;
}
.confirm-btn:hover { transform: translateY(-1px); }
.confirm-no {
  background: transparent;
  color: var(--text-muted);
}
.confirm-no:hover { color: var(--text); background: rgba(0, 0, 0, 0.05); }
.confirm-yes {
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-h));
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.10);
}
.confirm-yes:hover { box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18); }

/* ── Restore-last-session banner ──────────────────────────── */
#restore-banner {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 8px 8px 16px;
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.10);
  z-index: 90;
  animation: rbSlideIn 0.32s cubic-bezier(.34, 1.56, .64, 1);
}
@keyframes rbSlideIn {
  from { opacity: 0; transform: translate(-50%, -16px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}
.rb-msg { white-space: nowrap; color: var(--text); }
.rb-btn {
  border: none;
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.12s, background 0.12s;
}
.rb-btn:hover { transform: translateY(-1px); }
.rb-yes {
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-h));
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.10);
}
.rb-no {
  background: transparent;
  color: var(--text-muted);
}
.rb-no:hover { color: var(--text); background: rgba(0,0,0,0.05); }

/* ── Status bar (top-center floating toast) ──────────────── */
#status-bar {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translate(-50%, -8px);
  z-index: 60;
  background: var(--surface);
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow:
    0 4px 14px rgba(0, 0, 0, 0.10),
    0 1px 3px rgba(0, 0, 0, 0.05),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.28s cubic-bezier(.34, 1.56, .64, 1);
  max-width: min(560px, 92vw);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#status-bar:not(:empty) {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}

#status-bar.playing {
  color: var(--accent);
}

#status-bar.playing::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(240, 135, 102, 0.7);
  animation: pulse 1.4s infinite;
}

@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(240, 135, 102, 0.7); }
  70%  { box-shadow: 0 0 0 10px rgba(240, 135, 102, 0); }
  100% { box-shadow: 0 0 0 0 rgba(240, 135, 102, 0); }
}

#status-bar.error {
  color: var(--danger);
}

/* ── Intro animation overlay ──────────────────────────────── */
#intro-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background:
    radial-gradient(circle at 30% 30%, rgba(240,135,102,0.18), transparent 55%),
    radial-gradient(circle at 70% 70%, rgba(159,221,215,0.20), transparent 55%),
    #16161c;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  animation: introFadeIn 0.5s ease;
}
#intro-overlay.outro {
  animation: introFadeOut 0.7s ease forwards;
}
@keyframes introFadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes introFadeOut { to { opacity: 0; visibility: hidden; } }

.intro-stage {
  position: relative;
  width: 90vw;
  max-width: 900px;
  height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.intro-words {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.18em;
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-weight: 700;
  font-size: clamp(28px, 5vw, 52px);
  letter-spacing: 0.06em;
  color: #fff;
  user-select: none;
  pointer-events: none;
}

.intro-letter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.05em;
  height: 1.45em;
  border-radius: 4px;
  background: transparent;
  color: #fff;
  opacity: 0;
  transform: translateY(20px);
  transition:
    transform 0.55s cubic-bezier(.34, 1.56, .64, 1),
    background 0.5s ease,
    color 0.4s ease,
    box-shadow 0.4s ease,
    border-radius 0.5s ease,
    opacity 0.4s ease;
  will-change: transform, background, color;
}
.intro-letter.space {
  width: 0.6em;
  background: transparent !important;
  box-shadow: none !important;
}
.intro-letter.visible { opacity: 1; transform: translateY(0); }
.intro-letter.key {
  background: #fff;
  color: transparent;
  border-radius: 3px 3px 6px 6px;
  box-shadow:
    inset 0 -6px 0 rgba(0,0,0,0.06),
    0 2px 4px rgba(0,0,0,0.25);
}
.intro-letter.flying {
  transition:
    transform 1.1s cubic-bezier(.55, 0.08, .35, 1),
    opacity 0.4s ease 0.7s;
}
.intro-letter.faded { opacity: 0; }

.intro-piano {
  position: absolute;
  left: 0;
  top: 50%;
  width: 180px;
  height: 80px;
  transform: translate(-260px, -50%);
  opacity: 0;
  transition: transform 0.7s cubic-bezier(.34, 1.56, .64, 1), opacity 0.5s ease;
  filter: drop-shadow(0 8px 20px rgba(0,0,0,0.4));
}
.intro-piano.visible { transform: translate(0, -50%); opacity: 1; }
.piano-body { fill: #2a2a32; }
.piano-top  { fill: #1c1c22; }
.white-keys rect { fill: #f5f5f5; transition: fill 0.15s, transform 0.15s; transform-origin: 50% 90%; }
.white-keys rect.hit { fill: var(--accent); transform: scaleY(0.96); }
.black-keys rect.hit { fill: var(--accent-h); }

#intro-skip {
  position: absolute;
  bottom: 28px;
  right: 28px;
  background: rgba(255,255,255,0.10);
  border: 1px solid rgba(255,255,255,0.15);
  color: rgba(255,255,255,0.85);
  padding: 8px 16px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: 0.04em;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
#intro-skip:hover {
  background: rgba(255,255,255,0.18);
  color: #fff;
  transform: translateY(-1px);
}

/* ── Tutorial overlay ─────────────────────────────────────── */
/* position:fixed already creates a stacking context (z-index:auto = 0 in
   root). Without an explicit z-index, root-level siblings like #mode-toggle
   / #view-toggle (z-index:10) paint on top of the entire overlay subtree —
   so the bookmark tabs would appear undimmed. z-index:9999 hoists the whole
   overlay above any normal UI. Internal z-indices below are now relative
   to THIS stacking context, so they can be small. */
#tutorial-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
}
#tutorial-overlay:not(.hidden) { pointer-events: auto; }

/* Interactive final step: let real UI clicks pass through, except inside
   the bubble (so the skip button still works). */
#tutorial-overlay.tut-interactive { pointer-events: none; }
#tutorial-overlay.tut-interactive .tutorial-bubble { pointer-events: auto; }

/* Spotlight hole — IS the dim layer. The huge outward box-shadow tints
   everything OUTSIDE the rounded rect; INSIDE the rect there is no fill so
   the target shows through clean. z-index 9000 is pushed above any UI element
   that might have its own stacking context (mode/view toggles, sidebars,
   play-controls panel) so we don't get "buttons appear highlighted, canvas
   stays dim" inversions. */
.tut-hole {
  position: fixed;
  left: 0; top: 0;
  width: 0; height: 0;
  border-radius: 12px;
  pointer-events: none;
  z-index: 9000;
  box-shadow: 0 0 0 9999px rgba(20, 22, 28, 0.55);
  animation: tutFadeIn 0.3s ease;
  transition: left 0.35s cubic-bezier(.34, 1.56, .64, 1),
              top 0.35s cubic-bezier(.34, 1.56, .64, 1),
              width 0.35s cubic-bezier(.34, 1.56, .64, 1),
              height 0.35s cubic-bezier(.34, 1.56, .64, 1),
              border-radius 0.35s ease;
}

/* The glowing accent ring drawn over the hole's outline. */
.tut-ring {
  position: fixed;
  left: 0; top: 0;
  width: 0; height: 0;
  border-radius: 12px;
  pointer-events: none;
  z-index: 9001;
  box-shadow:
    0 0 0 2px var(--accent),
    0 0 0 6px rgba(240,135,102,0.18),
    0 0 28px 4px rgba(240,135,102,0.30);
  transition: left 0.35s cubic-bezier(.34, 1.56, .64, 1),
              top 0.35s cubic-bezier(.34, 1.56, .64, 1),
              width 0.35s cubic-bezier(.34, 1.56, .64, 1),
              height 0.35s cubic-bezier(.34, 1.56, .64, 1),
              border-radius 0.35s ease;
  animation: tutRingPulse 1.8s ease-in-out infinite;
}

@keyframes tutRingPulse {
  0%, 100% { box-shadow:
    0 0 0 2px var(--accent),
    0 0 0 6px rgba(240,135,102,0.18),
    0 0 24px 2px rgba(240,135,102,0.25); }
  50%      { box-shadow:
    0 0 0 2px var(--accent),
    0 0 0 7px rgba(240,135,102,0.28),
    0 0 36px 6px rgba(240,135,102,0.45); }
}

.tut-hole.hidden,
.tut-ring.hidden { display: none; }

@keyframes tutFadeIn { from { opacity: 0; } to { opacity: 1; } }

.tutorial-bubble {
  position: fixed;
  z-index: 9010;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 18px;
  max-width: 280px;
  box-shadow:
    0 8px 32px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.5);
  font-family: 'Quicksand', sans-serif;
  transition: top 0.35s cubic-bezier(.34, 1.56, .64, 1), left 0.35s cubic-bezier(.34, 1.56, .64, 1);
}

.tut-step {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.08em;
  margin-bottom: 6px;
}
.tut-title {
  font-family: 'Fredoka', 'Quicksand', sans-serif;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.tut-body {
  font-size: 0.86rem;
  color: var(--text);
  line-height: 1.5;
  margin-bottom: 12px;
}
.tut-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.tut-btn-ghost,
.tut-btn-primary {
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
  border: none;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s, color 0.15s;
}
.tut-btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.tut-btn-ghost:hover { color: var(--text); }
.tut-btn-primary {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-h) 100%);
  color: #fff;
  box-shadow: var(--accent-glow);
}
.tut-btn-primary:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(240,135,102,0.45); }

/* Inline "click to play" button injected into the bubble when the browser's
   autoplay policy has blocked audio on first visit. */
.tut-play-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  padding: 8px 16px;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-h) 100%);
  box-shadow: var(--accent-glow);
  transition: transform 0.15s, box-shadow 0.15s;
  animation: tutPlayPulse 1.4s ease-in-out infinite;
}
.tut-play-btn:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(240,135,102,0.45); }
@keyframes tutPlayPulse {
  0%,100% { box-shadow: 0 0 0 0 rgba(240,135,102,0.45); }
  50%     { box-shadow: 0 0 0 8px rgba(240,135,102,0.00); }
}

.tutorial-arrow {
  position: fixed;
  z-index: 9010;
  width: 36px;
  height: 36px;
  color: var(--accent);
  pointer-events: none;
  transition: top 0.35s cubic-bezier(.34, 1.56, .64, 1), left 0.35s cubic-bezier(.34, 1.56, .64, 1), transform 0.3s ease;
  filter: drop-shadow(0 2px 5px rgba(0,0,0,0.25));
  animation: tutArrowBob 1.4s ease-in-out infinite;
  transform-origin: center;
}

@keyframes tutArrowBob {
  0%,100% { filter: drop-shadow(0 2px 5px rgba(0,0,0,0.25)); }
  50%     { filter: drop-shadow(0 4px 9px rgba(240,135,102,0.50)); }
}

/* Old .tut-spotlight removed — highlight is now drawn by .tut-hole + .tut-ring
   so we never touch the target element's CSS or its stacking context. */

/* ── Responsive ───────────────────────────────────────────── */
/* iPad landscape territory (1024-1100px wide). Without this breakpoint
   the canvas gets pinched between 200px sidebars and only has ~556px to
   work with — feels cramped given 9:5 aspect ratio.  */
@media (max-width: 1100px) {
  #sidebar-left,
  #sidebar-right {
    width: 175px;
    gap: 11px;
  }
  #main { padding: 4px 16px 16px; gap: 12px; }
  .panel { padding: 12px 13px; }
}

@media (max-width: 960px) {
  #sidebar-left,
  #sidebar-right {
    width: 160px;
    gap: 10px;
  }
  #main { padding: 4px 14px 14px; gap: 10px; }
  .panel { padding: 10px 12px; }
}

@media (max-width: 820px) {
  /* iPad portrait (768px-834px) — sidebars get tightest here so the
     canvas keeps a usable footprint. */
  #sidebar-left,
  #sidebar-right { width: 142px; }
  .panel  { padding: 10px 11px; }
  .panel h3 { font-size: 0.72rem; }
}

@media (max-width: 720px) {
  #sidebar-left,
  #sidebar-right {
    width: 134px;
    gap: 8px;
  }
  .panel { padding: 9px 10px; }
  .mode-btn { font-size: 0.78rem; padding: 8px 10px; }
  .tool-btn { font-size: 0.70rem; padding: 7px 4px 5px; }
  .tool-btn svg { width: 18px; height: 18px; }
  .panel h3  { font-size: 0.68rem; }
  select     { font-size: 0.78rem; padding: 8px 22px 8px 10px; }
  .btn-primary   { font-size: 0.84rem; padding: 9px 10px; }
  .btn-secondary { font-size: 0.78rem; padding: 8px 10px; }
  header h1  { font-size: 1.1rem; }
  header p   { display: none; }
}

@media (max-width: 520px) {
  body { overflow-y: auto; height: auto; }
  #app  { height: auto; min-height: 100vh; }
  #main { flex-direction: column; overflow: visible; padding: 4px 12px 14px; }

  #sidebar-left,
  #sidebar-right {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    max-height: none;
    overflow-y: visible;
    padding: 0;
  }

  .panel        { min-width: 130px; flex: 1; }
  #play-controls { margin-top: 0; flex-direction: row; flex-wrap: wrap; width: 100%; }
  #play-controls button { flex: 1; min-width: 80px; }
  #canvas-area { padding: 8px 0; }

  /* Header: items wrap to a second row instead of overflowing.  Theme
     picker + help/lang sit on the right and wrap together. */
  header { padding: 10px 14px; gap: 8px; row-gap: 8px; }
  header h1 { font-size: 1rem; }
  #mode-toggle-group { margin-left: auto; margin-right: 0; padding: 2px; gap: 0; }
  .mode-toggle-btn   { width: 24px; height: 24px; }
  .mode-toggle-btn svg { width: 13px; height: 13px; }
  #theme-picker      { gap: 5px; padding: 4px 7px; }
  .theme-dot         { width: 14px; height: 14px; border-width: 1.5px; }
  #btn-help          { width: 28px; height: 28px; }
  #btn-help svg      { width: 14px; height: 14px; }
  #lang-toggle       { font-size: 0.72rem; padding: 6px 10px; }


  /* Intro animation: shrink heavily so 21-letter title fits.  Letters
     wrap to multiple lines if needed; piano stays centered. */
  .intro-words {
    flex-wrap: wrap;
    row-gap: 0.1em;
    font-size: clamp(14px, 6.5vw, 36px);
    padding: 0 4vw;
  }
  .intro-letter { width: 0.95em; height: 1.35em; }
  .intro-letter.space { width: 0.4em; }
}

@media (max-width: 380px) {
  /* Extra-narrow phones: drop the h1 to a single-line short title and
     shrink theme picker even more so it sits next to the controls. */
  header h1 { font-size: 0.9rem; }
  #theme-picker { gap: 4px; padding: 3px 6px; }
  .theme-dot { width: 12px; height: 12px; }
  .intro-words { font-size: clamp(12px, 6vw, 28px); }
}
