/* ═══════════════════════════════════════════════════════════
   MUSIC MAKER — music-maker.css
   @layer: reset → tokens → base → layout → components → animations → utilities
   ═══════════════════════════════════════════════════════════ */
@layer reset, tokens, base, layout, components, animations, utilities;

/* ── TOKENS ──────────────────────────────────────────────── */
@layer tokens {
  :root {
    /* Instrument button colours — one per instrument */
    --c-drum:   #FF1744;
    --c-piano:  #2979FF;
    --c-guitar: #00C853;
    --c-bell:   #FFD600;
    --c-frog:   #00BCD4;
    --c-laser:  #AA00FF;
    --c-xyl:    #FF6D00;
    --c-trump:  #FF4081;

    /* UI chrome */
    --bg:          #1C1B2E;
    --hud-bg:      rgba(20, 18, 40, 0.92);
    --hud-border:  rgba(255,255,255,0.08);
    --hud-h:       68px;
    --ui-bg:       rgba(255,255,255,0.08);
    --ui-border:   rgba(255,255,255,0.14);
    --ui-shadow:   0 4px 16px rgba(0,0,0,0.35);
    --ui-radius:   50px;

    /* Button */
    --btn-radius:  24px;
    --btn-shadow:
      0 8px 0 rgba(0,0,0,0.30),
      0 12px 28px rgba(0,0,0,0.25);
    --btn-shadow-pressed:
      0 2px 0 rgba(0,0,0,0.30),
      0 4px 12px rgba(0,0,0,0.20);

    /* Grid gap */
    --grid-gap: 12px;
    --grid-pad: 12px;

    /* Typography */
    --font: 'Arial Rounded MT Bold', Arial, sans-serif;

    /* Easing */
    --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-out:    cubic-bezier(0.22, 1, 0.36, 1);

    /* Z-index */
    --z-grid:      1;
    --z-bar:       8;
    --z-hud:      10;
    --z-particles:20;
    --z-celeb:    30;
    --z-confetti: 40;
    --z-skip:    999;
  }
}

/* ── RESET ───────────────────────────────────────────────── */
@layer reset {
  *, *::before, *::after {
    margin: 0; padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
  }
  html, body { width: 100%; height: 100%; overflow: hidden; -webkit-text-size-adjust: 100%; }
  button { font: inherit; cursor: pointer; border: none; background: none; -webkit-appearance: none; }
  a      { color: inherit; text-decoration: none; }
}

/* ── BASE ────────────────────────────────────────────────── */
@layer base {
  body {
    font-family: var(--font);
    background: var(--bg);
    color: #fff;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    overflow: hidden;
  }

  :focus-visible {
    outline: 4px solid #FFD600;
    outline-offset: 4px;
    border-radius: 12px;
  }
}

/* ── LAYOUT ──────────────────────────────────────────────── */
@layer layout {

  .hud {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--hud-h);
    z-index: var(--z-hud);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 14px;
    background: var(--hud-bg);
    border-bottom: 1px solid var(--hud-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  /* 2×4 grid — fills the space below the HUD */
  .instrument-grid {
    position: fixed;
    top: var(--hud-h);
    left: 0; right: 0; bottom: 0;
    z-index: var(--z-grid);
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows:    repeat(4, 1fr);
    gap: var(--grid-gap);
    padding: var(--grid-pad);
    padding-bottom: calc(var(--grid-pad) + 6px); /* room for playback bar */
  }

  /* Landscape: flip to 4×2 */
  @media (orientation: landscape) {
    .instrument-grid {
      grid-template-columns: repeat(4, 1fr);
      grid-template-rows:    repeat(2, 1fr);
    }
  }

  .particle-layer {
    position: fixed; inset: 0;
    pointer-events: none;
    z-index: var(--z-particles);
    overflow: hidden;
  }

  .confetti-layer {
    position: fixed; inset: 0;
    pointer-events: none;
    z-index: var(--z-confetti);
    overflow: hidden;
  }

  /* Playback progress bar — slim strip at very bottom */
  .playback-bar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 6px;
    z-index: var(--z-bar);
    background: rgba(255,255,255,0.08);
    display: none;
  }
  .playback-bar.active { display: block; }

  .playback-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #FFD600, #FF4081);
    transition: width 0.1s linear;
    border-radius: 0 3px 3px 0;
  }
}

/* ── COMPONENTS ──────────────────────────────────────────── */
@layer components {

  /* Skip link */
  .skip-link {
    position: fixed; top: -200%; left: 16px; z-index: var(--z-skip);
    background: #FFD600; color: #111; font-weight: 800;
    padding: 8px 20px; border-radius: 40px; border: 3px solid #111;
    transition: top 100ms;
  }
  .skip-link:focus { top: 12px; }

  /* ── HUD elements */
  .hud-btn {
    display: flex; align-items: center; justify-content: center;
    min-width: 52px; min-height: 52px;
    border-radius: 50%;
    background: var(--ui-bg);
    border: 2px solid var(--ui-border);
    font-size: 1.3rem; color: #fff;
    flex-shrink: 0;
    box-shadow: var(--ui-shadow);
    transition: transform 120ms var(--ease-bounce), background 120ms;
  }
  .hud-btn:hover  { transform: scale(1.10); background: rgba(255,255,255,0.14); }
  .hud-btn:active { transform: scale(0.88); }

  .play-btn  { background: rgba(255,214,0,0.18); border-color: rgba(255,214,0,0.35); }
  .clear-btn { background: rgba(255,60,60,0.14); border-color: rgba(255,60,60,0.28); }

  .status-wrap {
    display: flex; align-items: center; gap: 7px;
    flex: 1;
    justify-content: center;
  }

  .status-dot {
    width: 10px; height: 10px;
    border-radius: 50%;
    background: rgba(255,255,255,0.20);
    transition: background 200ms, box-shadow 200ms;
  }
  .status-dot.recording {
    background: #FF1744;
    box-shadow: 0 0 8px #FF1744, 0 0 16px rgba(255,23,68,0.5);
    animation: dotPulse 1s ease-in-out infinite;
  }
  .status-dot.playing {
    background: #00E676;
    box-shadow: 0 0 8px #00E676, 0 0 16px rgba(0,230,118,0.5);
  }

  .status-label {
    font-size: 1.3rem;
    transition: opacity 200ms;
  }

  .tap-chip {
    display: flex; align-items: center; gap: 6px;
    background: var(--ui-bg);
    border: 2px solid var(--ui-border);
    border-radius: var(--ui-radius);
    padding: 6px 14px;
    font-size: 1rem;
    flex-shrink: 0;
  }
  .tap-num {
    font-size: 1.3rem; font-weight: 900;
    color: #FFD600;
    min-width: 26px; text-align: center;
  }

  /* ── INSTRUMENT BUTTON ── */
  .inst-btn {
    position: relative;
    border-radius: var(--btn-radius);
    background: var(--btn-color, #FF1744);
    border: none;
    box-shadow: var(--btn-shadow);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    touch-action: none;
    overflow: hidden;
    transition: transform 80ms var(--ease-bounce),
                box-shadow 80ms,
                filter 80ms;
    /* Chunky top highlight */
    background-image: linear-gradient(
      180deg,
      rgba(255,255,255,0.22) 0%,
      rgba(255,255,255,0.05) 40%,
      rgba(0,0,0,0.10) 100%
    );
  }

  /* Shine overlay pseudo-element */
  .inst-btn::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 50%;
    border-radius: var(--btn-radius) var(--btn-radius) 60% 60% / var(--btn-radius) var(--btn-radius) 40% 40%;
    background: rgba(255,255,255,0.14);
    pointer-events: none;
  }

  /* Ripple ring inside button on press */
  .inst-btn::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: rgba(255,255,255,0);
    transition: background 300ms ease;
    pointer-events: none;
  }
  .inst-btn.pressed::after {
    background: rgba(255,255,255,0.22);
  }

  /* Pressed state */
  .inst-btn:active,
  .inst-btn.pressed {
    transform: scale(0.93) translateY(4px);
    box-shadow: var(--btn-shadow-pressed);
    filter: brightness(1.15) saturate(1.2);
  }

  /* Playback highlight */
  .inst-btn.playing-back {
    transform: scale(1.06);
    box-shadow:
      var(--btn-shadow),
      0 0 0 5px rgba(255,255,255,0.6),
      0 0 40px rgba(255,255,255,0.3);
    filter: brightness(1.2) saturate(1.3);
    animation: playbackPop 0.25s var(--ease-bounce) forwards;
  }

  .inst-emoji {
    font-size: clamp(2.2rem, 7vw, 4rem);
    line-height: 1;
    pointer-events: none;
    filter: drop-shadow(0 3px 6px rgba(0,0,0,0.30));
    /* Bounce on press */
    transition: transform 120ms var(--ease-bounce);
    position: relative;
    z-index: 1;
  }
  .inst-btn.pressed .inst-emoji,
  .inst-btn:active .inst-emoji {
    transform: scale(0.82) rotate(-8deg);
  }

  /* Wave bars — animated music visualiser on each button */
  .wave-bars {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: clamp(14px, 4vw, 22px);
    pointer-events: none;
    position: relative;
    z-index: 1;
  }
  .wave-bar {
    width: clamp(4px, 1.2vw, 7px);
    border-radius: 3px;
    background: rgba(255,255,255,0.55);
    /* height animated by JS via inline style */
    transition: height 60ms ease;
    min-height: 4px;
  }

  /* Active waves when button is pressed */
  .inst-btn.pressed .wave-bar,
  .inst-btn.playing-back .wave-bar {
    animation: waveActive 0.35s ease-in-out infinite alternate;
  }
  .inst-btn.pressed .wave-bar:nth-child(1) { animation-delay: 0ms; }
  .inst-btn.pressed .wave-bar:nth-child(2) { animation-delay: 60ms; }
  .inst-btn.pressed .wave-bar:nth-child(3) { animation-delay: 120ms; }
  .inst-btn.pressed .wave-bar:nth-child(4) { animation-delay: 40ms; }
  .inst-btn.pressed .wave-bar:nth-child(5) { animation-delay: 80ms; }

  /* ── Note particles that float up from buttons when pressed */
  .note-particle {
    position: fixed;
    pointer-events: none;
    font-size: clamp(1.2rem, 4vw, 2rem);
    animation: noteFloat var(--nf-dur, 1.4s) var(--ease-out) forwards;
    z-index: var(--z-particles);
    filter: drop-shadow(0 2px 6px rgba(0,0,0,0.3));
  }

  /* ── Celebration overlay */
  .celeb-overlay {
    position: fixed; inset: 0;
    z-index: var(--z-celeb);
    display: flex; align-items: center; justify-content: center;
    pointer-events: none;
    opacity: 0;
  }
  .celeb-overlay.show {
    animation: celebFade 2.0s var(--ease-out) forwards;
  }

  .celeb-inner {
    font-size: clamp(4rem, 18vw, 8rem);
    filter: drop-shadow(0 0 40px rgba(255,214,0,0.7));
    animation: none;
  }
  .celeb-overlay.show .celeb-inner {
    animation: celebBounce 2.0s var(--ease-bounce) forwards;
  }

  /* ── Confetti */
  .confetti-piece {
    position: absolute; top: -28px;
    opacity: 0;
    animation: confettiFall var(--cf-dur, 2.8s) var(--cf-delay, 0s) ease-in forwards;
  }
  .confetti-piece.circle  { border-radius: 50%; }
  .confetti-piece.diamond { border-radius: 2px; }
}

/* ── ANIMATIONS ──────────────────────────────────────────── */
@layer animations {

  /* Recording status dot pulse */
  @keyframes dotPulse {
    0%,100% { transform: scale(1);   opacity: 1; }
    50%      { transform: scale(1.4); opacity: 0.7; }
  }

  /* Wave bars dance */
  @keyframes waveActive {
    from { height: 4px; }
    to   { height: clamp(14px, 4vw, 22px); }
  }

  /* Note floats upward */
  @keyframes noteFloat {
    0%   { opacity: 0; transform: translateY(0)     scale(0.5) rotate(var(--nr, -10deg)); }
    15%  { opacity: 1; transform: translateY(-20px)  scale(1.2) rotate(var(--nr, -10deg)); }
    80%  { opacity: 1; transform: translateY(-100px) scale(1.0) rotate(calc(var(--nr, -10deg) * -1)); }
    100% { opacity: 0; transform: translateY(-150px) scale(0.7) rotate(0deg); }
  }

  /* Playback button pop */
  @keyframes playbackPop {
    0%   { transform: scale(1); }
    45%  { transform: scale(1.10); }
    100% { transform: scale(1.06); }
  }

  /* Celebration overlay */
  @keyframes celebFade {
    0%   { opacity: 0; }
    10%  { opacity: 1; }
    70%  { opacity: 1; }
    100% { opacity: 0; }
  }
  @keyframes celebBounce {
    0%   { transform: scale(0.3) rotate(-20deg); }
    20%  { transform: scale(1.3) rotate(10deg);  }
    35%  { transform: scale(0.9) rotate(-5deg);  }
    50%  { transform: scale(1.1) rotate(3deg);   }
    65%  { transform: scale(1.0) rotate(0deg);   }
    85%  { transform: scale(1.0) rotate(0deg);   }
    100% { transform: scale(0.7) rotate(10deg);  }
  }

  /* Confetti */
  @keyframes confettiFall {
    0%   { opacity: 1; transform: translateY(0)   rotate(0deg)              scale(1); }
    70%  { opacity: 1; }
    100% { opacity: 0; transform: translateY(calc(100vh + 60px)) rotate(var(--cf-spin, 540deg)) scale(0.5); }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }
}

/* ── UTILITIES ───────────────────────────────────────────── */
@layer utilities {
  .sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); white-space: nowrap; border: 0;
  }
}
