// MENTESH Pro·Q 4 — 3U parametric EQ with up to 24 bands
// Dark navy panel, large analyzer+curves display, band handles with colored nodes,
// FREQ/GAIN/Q knobs, 24-band selector strip, OUTPUT GAIN / DRY-WET / AUTO-GAIN.

function MenteshProQ({ variant = {} }) {
  const [preset, setPreset] = React.useState(2);
  const presets = [
    'Flat', 'Vocal Clarity', 'Drum Bus', 'Master Polish', 'Warm Tape Roll-Off',
    'Air Lift', 'Mud Cut', 'Surgical Notch',
  ];

  // 7 active bands, each with freq (0..1 log x), gain (-12..+12 -> 0..1), q, shape, color, enabled
  // Bright, saturated band colors matching Pro-Q reference photo
  const bandColors = ['#3a9eff', '#7ab8e0', '#f5c842', '#66dc7a', '#c084ff', '#ff7aa8', '#ff9a4a'];
  const [bands, setBands] = React.useState([
    { freq: 0.08, gain: 0.42, q: 0.5, shape: 'LS', color: bandColors[0] },     // low shelf slight cut
    { freq: 0.22, gain: 0.58, q: 0.45, shape: 'BELL', color: bandColors[1] },  // low bump
    { freq: 0.40, gain: 0.75, q: 0.35, shape: 'BELL', color: bandColors[2] },  // mid boost
    { freq: 0.55, gain: 0.22, q: 0.72, shape: 'BELL', color: bandColors[3] },  // mid notch
    { freq: 0.70, gain: 0.25, q: 0.80, shape: 'BELL', color: bandColors[4] },  // deep notch
    { freq: 0.82, gain: 0.68, q: 0.55, shape: 'BELL', color: bandColors[5] },  // hi bump
    { freq: 0.92, gain: 0.55, q: 0.5, shape: 'HS', color: bandColors[6] },     // high shelf
  ]);
  const [selected, setSelected] = React.useState(2);
  const [outputGain, setOutputGain] = React.useState(0.5);
  const [dryWet, setDryWet] = React.useState(1.0);
  const [autoGain, setAutoGain] = React.useState(true);

  const updateBand = (i, patch) =>
    setBands(bs => bs.map((b, j) => j === i ? { ...b, ...patch } : b));

  const cur = bands[selected];

  return (
    <div style={{
      width: 1400,
      background: '#0a0c12',
      position: 'relative',
      fontFamily: "'Inter','Helvetica Neue',sans-serif",
      color: '#d8e0ea',
      userSelect: 'none',
    }}>
      <ProQRackEars />
      <div style={{
        margin: '0 54px',
        background: 'linear-gradient(180deg, #151a22 0%, #0e1218 60%, #0a0c12 100%)',
        border: '1px solid rgba(255,255,255,0.04)',
        borderRadius: 2, overflow: 'hidden',
      }}>
        <ProQTopStrip preset={preset} setPreset={setPreset} presets={presets} />

        <div style={{ display: 'grid', gridTemplateColumns: '200px 1fr 200px' }}>
          <ProQLeftControls cur={cur} onChange={patch => updateBand(selected, patch)} selected={selected} />
          <ProQDisplay bands={bands} selected={selected} setSelected={setSelected} updateBand={updateBand} />
          <ProQRightControls cur={cur} onChange={patch => updateBand(selected, patch)} />
        </div>

        <ProQBandStrip bands={bands} selected={selected} setSelected={setSelected} />
        <ProQBottomBar outputGain={outputGain} setOutputGain={setOutputGain}
          dryWet={dryWet} setDryWet={setDryWet}
          autoGain={autoGain} setAutoGain={setAutoGain} />
        <ProQFooter />
      </div>
    </div>
  );
}

// ——— TOP STRIP ———————————————————————————————————————————————
function ProQTopStrip({ preset, setPreset, presets }) {
  return (
    <div style={{
      height: 44, display: 'flex', alignItems: 'center',
      background: 'linear-gradient(180deg, #1a2230 0%, #131924 100%)',
      borderBottom: '1px solid rgba(0,0,0,0.5)',
      boxShadow: 'inset 0 -1px 0 rgba(255,255,255,0.04)',
      padding: '0 16px', gap: 14,
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
      }}>
        <div style={{
          width: 30, height: 30, borderRadius: '50%',
          border: '1.4px solid rgba(255,255,255,0.5)',
          background: 'radial-gradient(circle at 35% 30%, #2a3344 0%, #131924 100%)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: "'Playfair Display',serif", fontStyle: 'italic',
          fontSize: 14, fontWeight: 900, color: '#e8eef5',
          boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.15), 0 1px 2px rgba(0,0,0,0.5)',
          lineHeight: 1,
        }}>ME</div>
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 1 }}>
          <span style={{
            fontFamily: "'Playfair Display',serif", fontStyle: 'italic',
            fontWeight: 500, fontSize: 22, color: '#e8eef5',
            lineHeight: 1,
          }}>ME.Q4</span>
          <span style={{
            fontFamily: "'Inter',sans-serif", fontWeight: 600,
            fontSize: 8, letterSpacing: '0.18em', color: 'rgba(201,212,227,0.7)',
            lineHeight: 1,
          }}>MENTESH ENGINEERING</span>
        </div>
      </div>

      <div style={{ flex: 1 }} />
      <button onClick={() => setPreset(p => (p - 1 + presets.length) % presets.length)} style={proqNavBtn}>‹‹</button>
      <button onClick={() => setPreset(p => (p - 1 + presets.length) % presets.length)} style={proqNavBtn}>‹</button>
      <div style={{
        minWidth: 280, padding: '6px 16px', textAlign: 'center',
        background: 'rgba(0,0,0,0.35)',
        border: '1px solid rgba(255,255,255,0.06)', borderRadius: 3,
        fontSize: 12, letterSpacing: '0.04em', color: '#d8e0ea',
      }}>
        <span style={{ opacity: 0.5, marginRight: 10 }}>NO</span>
        <span style={{ fontWeight: 600 }}>{presets[preset]}</span>
        <span style={{ opacity: 0.4, marginLeft: 10, fontStyle: 'italic', fontSize: 11 }}>— Curve Preset</span>
      </div>
      <button onClick={() => setPreset(p => (p + 1) % presets.length)} style={proqNavBtn}>›</button>
      <button onClick={() => setPreset(p => (p + 1) % presets.length)} style={proqNavBtn}>››</button>
      <div style={{ flex: 1 }} />
      <div style={{ display: 'flex', gap: 20, fontSize: 10, letterSpacing: '0.2em', opacity: 0.55 }}>
        <span>20Hz</span>
        <span>20kHz</span>
      </div>
    </div>
  );
}

const proqNavBtn = {
  width: 26, height: 24,
  background: 'rgba(255,255,255,0.04)',
  border: '1px solid rgba(255,255,255,0.06)',
  borderRadius: 3, color: '#a8b4c4', fontSize: 12, cursor: 'pointer',
};

// ——— LEFT CONTROLS ———————————————————————————————————————————
function ProQLeftControls({ cur, onChange, selected }) {
  return (
    <div style={{
      padding: '14px 14px 10px',
      borderRight: '1px solid rgba(255,255,255,0.04)',
      background: 'rgba(255,255,255,0.012)',
      display: 'flex', flexDirection: 'column', gap: 10,
    }}>
      <div style={{
        display: 'flex', justifyContent: 'space-between',
        fontSize: 9, letterSpacing: '0.22em', color: '#8794a6',
      }}>
        <span>BAND SELECT</span>
        <span>SOLO</span>
      </div>
      <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
        <div style={{
          flex: 1, padding: '4px 8px',
          background: 'rgba(0,0,0,0.3)', border: '1px solid rgba(255,255,255,0.05)',
          borderRadius: 3, fontSize: 10, letterSpacing: '0.1em',
          color: cur.color, textAlign: 'center',
        }}>BAND {selected + 1}</div>
        <button style={{
          width: 28, height: 22,
          background: 'rgba(255,255,255,0.04)',
          border: '1px solid rgba(255,255,255,0.08)',
          borderRadius: 3, color: '#a8b4c4', fontSize: 8, cursor: 'pointer',
          letterSpacing: '0.1em',
        }}>S</button>
      </div>

      <div style={{ display: 'flex', justifyContent: 'space-around', marginTop: 6 }}>
        <ProQGlowKnob value={cur.freq} onChange={v => onChange({ freq: v })}
          label="FREQ" color={cur.color} size={64} />
        <ProQGlowKnob value={cur.gain} onChange={v => onChange({ gain: v })}
          label="GAIN" color={cur.color} size={64} />
      </div>
      <div style={{
        marginTop: 4, padding: '4px 8px',
        background: 'rgba(0,0,0,0.3)', border: '1px solid rgba(255,255,255,0.05)',
        borderRadius: 3, display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        fontSize: 9, letterSpacing: '0.16em', color: '#8794a6',
      }}>
        <span>BAND</span>
        <span style={{ color: cur.color }}>▼</span>
      </div>
    </div>
  );
}

// ——— RIGHT CONTROLS ———————————————————————————————————————————
function ProQRightControls({ cur, onChange }) {
  const shapes = ['BELL', 'LS', 'HS', 'LC', 'HC', 'NOTCH', 'TILT'];
  const cycleShape = (dir) => {
    const i = shapes.indexOf(cur.shape);
    const next = shapes[(i + dir + shapes.length) % shapes.length];
    onChange({ shape: next });
  };
  return (
    <div style={{
      padding: '14px 14px 10px',
      borderLeft: '1px solid rgba(255,255,255,0.04)',
      background: 'rgba(255,255,255,0.012)',
      display: 'flex', flexDirection: 'column', gap: 10,
    }}>
      <div style={{ display: 'flex', gap: 6 }}>
        <button onClick={() => cycleShape(1)} style={proqBtn('#5fd27a')}>TYPE</button>
        <button onClick={() => cycleShape(-1)} style={proqBtn('#a8b4c4')}>BACK</button>
      </div>
      <div style={{ display: 'flex', justifyContent: 'space-around' }}>
        <ProQGlowKnob value={cur.gain} onChange={v => onChange({ gain: v })}
          label="GAIN" color={cur.color} size={64} />
        <ProQGlowKnob value={cur.q} onChange={v => onChange({ q: v })}
          label="Q" color={cur.color} size={64} />
      </div>
      <div style={{
        padding: '4px 8px',
        background: 'rgba(0,0,0,0.3)', border: '1px solid rgba(255,255,255,0.05)',
        borderRadius: 3,
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        fontSize: 9, letterSpacing: '0.16em', color: '#8794a6',
      }}>
        <span>TYPE</span>
        <span style={{ color: cur.color }}>{cur.shape}</span>
      </div>
    </div>
  );
}

function proqBtn(color) {
  return {
    flex: 1, height: 24,
    background: `${color}15`,
    border: `1px solid ${color}40`,
    borderRadius: 3, color: color,
    fontSize: 9, letterSpacing: '0.18em', cursor: 'pointer',
    fontFamily: 'inherit',
  };
}

// ——— MAIN DISPLAY ———————————————————————————————————————————
function ProQDisplay({ bands, selected, setSelected, updateBand }) {
  const W = 900, H = 300;
  const svgRef = React.useRef(null);
  const drag = React.useRef(null);

  // spectrum animation
  const [t, setT] = React.useState(0);
  React.useEffect(() => {
    let id; const loop = () => { setT(x => x + 1); id = requestAnimationFrame(loop); };
    id = requestAnimationFrame(loop); return () => cancelAnimationFrame(id);
  }, []);

  const NUM_BARS = 120;
  const bars = Array.from({ length: NUM_BARS }, (_, i) => {
    const x = i / (NUM_BARS - 1);
    const base = Math.exp(-Math.pow((x - 0.25) * 2.5, 2)) * 0.75
               + Math.exp(-Math.pow((x - 0.55) * 3, 2)) * 0.5
               + Math.exp(-Math.pow((x - 0.85) * 4, 2)) * 0.32;
    const wobble = Math.sin(t * 0.04 + i * 0.5) * 0.06 + Math.sin(t * 0.09 + i * 1.1) * 0.04;
    return { x, h: Math.max(0.04, Math.min(0.95, base + wobble)) };
  });

  // compute EQ curve by summing each band's bell/shelf contribution
  const curveY = (x) => {
    let totalDb = 0;
    bands.forEach(b => {
      const db = (b.gain - 0.5) * 24; // -12..+12
      const bandCenterX = b.freq;
      const q = 0.3 + b.q * 4;
      const dist = (x - bandCenterX);
      let contrib = 0;
      if (b.shape === 'LS') {
        contrib = db * (1 / (1 + Math.exp((x - bandCenterX) * 12)));
      } else if (b.shape === 'HS') {
        contrib = db * (1 / (1 + Math.exp(-(x - bandCenterX) * 12)));
      } else {
        // bell
        contrib = db * Math.exp(-Math.pow(dist * q * 4.5, 2));
      }
      totalDb += contrib;
    });
    // convert db (-12..+12) to y (0..1 with 0.5 = 0dB)
    return 0.5 - (totalDb / 24) * 0.9;
  };

  // filled per-band curves
  const bandPath = (b) => {
    let d = `M 0 ${H * 0.5}`;
    for (let i = 0; i <= 160; i++) {
      const x = i / 160;
      const db = (b.gain - 0.5) * 24;
      const q = 0.3 + b.q * 4;
      const dist = (x - b.freq);
      let contrib = 0;
      if (b.shape === 'LS') contrib = db * (1 / (1 + Math.exp((x - b.freq) * 12)));
      else if (b.shape === 'HS') contrib = db * (1 / (1 + Math.exp(-(x - b.freq) * 12)));
      else contrib = db * Math.exp(-Math.pow(dist * q * 4.5, 2));
      const y = 0.5 - (contrib / 24) * 0.9;
      d += ` L ${x * W} ${y * H}`;
    }
    d += ` L ${W} ${H * 0.5} Z`;
    return d;
  };

  // master curve path
  const masterPath = (() => {
    let d = `M 0 ${curveY(0) * H}`;
    for (let i = 1; i <= 200; i++) {
      const x = i / 200;
      d += ` L ${x * W} ${curveY(x) * H}`;
    }
    return d;
  })();

  const onNodeDown = (e, i) => {
    e.stopPropagation();
    setSelected(i);
    const rect = svgRef.current.getBoundingClientRect();
    drag.current = { i };
    const onMove = ev => {
      const fx = Math.max(0, Math.min(1, (ev.clientX - rect.left) / rect.width));
      const fy = Math.max(0, Math.min(1, (ev.clientY - rect.top) / rect.height));
      const gain = 1 - fy;
      updateBand(i, { freq: fx, gain });
    };
    const onUp = () => {
      window.removeEventListener('mousemove', onMove);
      window.removeEventListener('mouseup', onUp);
    };
    window.addEventListener('mousemove', onMove);
    window.addEventListener('mouseup', onUp);
  };

  return (
    <div style={{
      position: 'relative',
      background: 'radial-gradient(ellipse at center, #0e1a2a 0%, #060a12 100%)',
      borderLeft: '1px solid rgba(255,255,255,0.03)',
      borderRight: '1px solid rgba(255,255,255,0.03)',
      boxShadow: 'inset 0 2px 12px rgba(0,0,0,0.55)',
      overflow: 'hidden',
    }}>
      <svg ref={svgRef} viewBox={`0 0 ${W} ${H}`} width="100%" height={H}
        preserveAspectRatio="none" style={{ display: 'block', cursor: 'crosshair' }}>
        {/* grid */}
        {[0.25, 0.5, 0.75].map(y => (
          <line key={y} x1="0" x2={W} y1={y * H} y2={y * H}
            stroke={y === 0.5 ? 'rgba(255,255,255,0.12)' : 'rgba(255,255,255,0.04)'}
            strokeWidth="1" />
        ))}
        {[0.1, 0.25, 0.45, 0.7, 0.9].map(x => (
          <line key={x} x1={x * W} x2={x * W} y1="0" y2={H}
            stroke="rgba(255,255,255,0.04)" strokeWidth="1" />
        ))}

        {/* spectrum bars */}
        {bars.map((b, i) => {
          const bw = W / NUM_BARS - 0.8;
          const bx = b.x * (W - bw);
          const bh = b.h * (H * 0.72);
          return (
            <rect key={i} x={bx} y={H - bh - 14} width={bw} height={bh}
              fill="#6b90b8" opacity="0.28" />
          );
        })}

        {/* per-band filled areas — vibrant, matching Pro-Q look */}
        {bands.map((b, i) => (
          <path key={i} d={bandPath(b)}
            fill={b.color} opacity={selected === i ? 0.45 : 0.28}
            stroke={b.color} strokeWidth="1" strokeOpacity="0.5" />
        ))}

        {/* master curve — bright white glowing line */}
        <path d={masterPath}
          fill="none" stroke="#ffffff" strokeWidth="2.5"
          style={{ filter: 'drop-shadow(0 0 6px rgba(255,255,255,0.7))' }} />

        {/* band nodes */}
        {bands.map((b, i) => {
          const cx = b.freq * W;
          const cy = (1 - b.gain) * H;
          const isSel = selected === i;
          return (
            <g key={i} onMouseDown={e => onNodeDown(e, i)} style={{ cursor: 'move' }}>
              {isSel && (
                <circle cx={cx} cy={cy} r="14" fill="none"
                  stroke={b.color} strokeWidth="1" opacity="0.4" strokeDasharray="2 3" />
              )}
              <circle cx={cx} cy={cy} r={isSel ? 10 : 8}
                fill={b.color}
                stroke="#ffffff" strokeWidth={isSel ? 2 : 1}
                style={{ filter: `drop-shadow(0 0 5px ${b.color})` }} />
              <text x={cx} y={cy + 3.5} fill="#0a0c12" fontSize="9"
                textAnchor="middle" fontWeight="700">{i + 1}</text>
            </g>
          );
        })}

        {/* frequency labels */}
        {[
          { x: 0.08, l: '50' }, { x: 0.22, l: '100' }, { x: 0.38, l: '300' },
          { x: 0.55, l: '1k' }, { x: 0.72, l: '3k' }, { x: 0.88, l: '10k' },
        ].map(f => (
          <text key={f.l} x={f.x * W} y={H - 3}
            fill="rgba(180,210,240,0.35)" fontSize="10" textAnchor="middle">
            {f.l}Hz
          </text>
        ))}
      </svg>
    </div>
  );
}

// ——— 24-BAND STRIP ———————————————————————————————————————————
function ProQBandStrip({ bands, selected, setSelected }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: 'repeat(24, 1fr)',
      padding: '10px 16px',
      background: 'linear-gradient(180deg, #0c1017 0%, #0a0c12 100%)',
      borderTop: '1px solid rgba(255,255,255,0.05)',
      gap: 4,
    }}>
      {Array.from({ length: 24 }, (_, i) => {
        const b = bands[i];
        const active = !!b;
        const isSel = selected === i;
        return (
          <button key={i}
            onClick={() => b && setSelected(i)}
            disabled={!b}
            style={{
              height: 26,
              background: isSel
                ? `${b.color}30`
                : active ? 'rgba(255,255,255,0.04)' : 'rgba(255,255,255,0.015)',
              border: isSel
                ? `1px solid ${b.color}`
                : '1px solid rgba(255,255,255,0.06)',
              borderRadius: 2,
              color: isSel ? b.color : active ? '#a8b4c4' : '#3d4656',
              fontSize: 10, fontWeight: isSel ? 700 : 500,
              fontFamily: 'inherit',
              cursor: b ? 'pointer' : 'not-allowed',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              position: 'relative',
            }}>
            {i + 1}
            {active && (
              <div style={{
                position: 'absolute', top: -4, left: '50%',
                transform: 'translateX(-50%)',
                width: 8, height: 4,
                background: b.color,
                borderRadius: 1,
                boxShadow: `0 0 6px ${b.color}, 0 0 10px ${b.color}80`,
              }}/>
            )}
          </button>
        );
      })}
    </div>
  );
}

// ——— BOTTOM BAR ———————————————————————————————————————————
function ProQBottomBar({ outputGain, setOutputGain, dryWet, setDryWet, autoGain, setAutoGain }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '1fr 1fr 1fr 1fr',
      padding: '10px 16px',
      background: 'linear-gradient(180deg, #0a0c12 0%, #080a10 100%)',
      borderTop: '1px solid rgba(255,255,255,0.04)',
      gap: 14, alignItems: 'center',
    }}>
      <ProQSlider value={outputGain} onChange={setOutputGain} label="OUTPUT GAIN" color="#a0b4cc" />
      <ProQSlider value={dryWet} onChange={setDryWet} label="DRY/WET" color="#a0b4cc" />
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10 }}>
        <span style={{ fontSize: 9, letterSpacing: '0.22em', color: '#8794a6' }}>DRY/WET</span>
        <div style={{
          width: 40, height: 18, borderRadius: 9,
          background: 'rgba(0,0,0,0.4)',
          border: '1px solid rgba(255,255,255,0.08)',
          position: 'relative',
        }}>
          <div style={{
            position: 'absolute', top: 1, left: 1, width: 16, height: 14,
            background: '#a0b4cc', borderRadius: '50%',
            boxShadow: '0 1px 2px rgba(0,0,0,0.6)',
          }}/>
        </div>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10 }}>
        <button onClick={() => setAutoGain(!autoGain)}
          style={{
            padding: '6px 14px',
            background: autoGain ? '#5fd27a20' : 'rgba(255,255,255,0.04)',
            border: `1px solid ${autoGain ? '#5fd27a' : 'rgba(255,255,255,0.08)'}`,
            borderRadius: 3,
            color: autoGain ? '#5fd27a' : '#8794a6',
            fontSize: 9, letterSpacing: '0.22em',
            fontFamily: 'inherit', cursor: 'pointer',
          }}>AUTO-GAIN</button>
      </div>
    </div>
  );
}

function ProQSlider({ value, onChange, label, color }) {
  const ref = React.useRef(null);
  const onDown = e => {
    const rect = ref.current.getBoundingClientRect();
    const update = (x) => {
      const v = Math.max(0, Math.min(1, (x - rect.left) / rect.width));
      onChange(v);
    };
    update(e.clientX);
    const onMove = ev => update(ev.clientX);
    const onUp = () => {
      window.removeEventListener('mousemove', onMove);
      window.removeEventListener('mouseup', onUp);
    };
    window.addEventListener('mousemove', onMove);
    window.addEventListener('mouseup', onUp);
  };
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      <span style={{ fontSize: 9, letterSpacing: '0.22em', color: '#8794a6', minWidth: 80 }}>{label}</span>
      <div ref={ref} onMouseDown={onDown} style={{
        flex: 1, height: 6, background: 'rgba(0,0,0,0.4)',
        border: '1px solid rgba(255,255,255,0.06)', borderRadius: 3,
        position: 'relative', cursor: 'ew-resize',
      }}>
        <div style={{
          position: 'absolute', left: 0, top: 0, bottom: 0,
          width: `${value * 100}%`,
          background: `linear-gradient(90deg, ${color}30, ${color})`,
          borderRadius: 3,
        }}/>
        <div style={{
          position: 'absolute', left: `calc(${value * 100}% - 5px)`, top: -3,
          width: 10, height: 12, background: color, borderRadius: 2,
          boxShadow: `0 0 4px ${color}`,
        }}/>
      </div>
    </div>
  );
}

function ProQFooter() {
  return (
    <div style={{
      height: 22,
      borderTop: '1px solid rgba(255,255,255,0.05)',
      background: 'linear-gradient(180deg, #0b0e14 0%, #070910 100%)',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '0 14px',
      fontSize: 9, letterSpacing: '0.22em', color: '#4a5466',
    }}>
      <span>ME.Q4 · MENTESH ENGINEERING · 24-BAND PARAMETRIC EQ</span>
      <span>ZERO LATENCY · NATURAL · LINEAR PHASE</span>
    </div>
  );
}

function ProQRackEars() {
  const ear = {
    position: 'absolute', top: 0, bottom: 0, width: 54,
    background: 'linear-gradient(90deg, #1a1a1e 0%, #0a0a0c 100%)',
    borderTop: '1px solid rgba(255,255,255,0.04)',
    borderBottom: '1px solid rgba(0,0,0,0.6)',
    display: 'flex', flexDirection: 'column',
    alignItems: 'center', justifyContent: 'space-between',
    padding: '16px 0',
  };
  const screw = (
    <div style={{
      width: 12, height: 12, borderRadius: '50%',
      background: 'radial-gradient(circle at 30% 30%, #7a7a7e, #2a2a2c 70%)',
      border: '1px solid #0a0a0c', position: 'relative',
    }}>
      <div style={{
        position: 'absolute', inset: 2,
        background: 'linear-gradient(45deg, transparent 45%, #0a0a0c 45%, #0a0a0c 55%, transparent 55%)',
      }}/>
    </div>
  );
  return (
    <>
      <div style={{ ...ear, left: 0 }}>{screw}{screw}{screw}{screw}</div>
      <div style={{ ...ear, right: 0 }}>{screw}{screw}{screw}{screw}</div>
    </>
  );
}

// ——— SHARED KNOB ———————————————————————————————————————————
function ProQGlowKnob({ value, onChange, label, size = 64, color = '#4aa3ff' }) {
  const drag = React.useRef(null);
  const onDown = e => {
    drag.current = { y: e.clientY, v: value };
    window.addEventListener('mousemove', onMove);
    window.addEventListener('mouseup', onUp);
  };
  const onMove = e => {
    if (!drag.current || !onChange) return;
    const dy = drag.current.y - e.clientY;
    onChange(Math.max(0, Math.min(1, drag.current.v + dy / 180)));
  };
  const onUp = () => {
    drag.current = null;
    window.removeEventListener('mousemove', onMove);
    window.removeEventListener('mouseup', onUp);
  };
  const angle = -135 + value * 270;
  const arcSteps = 40;
  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 5 }}>
      <div onMouseDown={onDown} style={{
        width: size, height: size, position: 'relative', cursor: 'ns-resize',
      }}>
        <svg viewBox="0 0 100 100" style={{ position: 'absolute', inset: -3, width: size + 6, height: size + 6 }}>
          {Array.from({ length: arcSteps }, (_, i) => {
            const a = -135 + (i / (arcSteps - 1)) * 270;
            const lit = (i / (arcSteps - 1)) <= value;
            const r = 46, cx = 50, cy = 50;
            const rad = a * Math.PI / 180;
            const x1 = cx + Math.cos(rad) * (r - 4);
            const y1 = cy + Math.sin(rad) * (r - 4);
            const x2 = cx + Math.cos(rad) * r;
            const y2 = cy + Math.sin(rad) * r;
            return <line key={i} x1={x1} y1={y1} x2={x2} y2={y2}
              stroke={lit ? color : 'rgba(255,255,255,0.08)'}
              strokeWidth="1.5" strokeLinecap="round"
              style={lit ? { filter: `drop-shadow(0 0 3px ${color})` } : undefined} />;
          })}
        </svg>
        <div style={{
          position: 'absolute', inset: 10,
          borderRadius: '50%',
          background: 'radial-gradient(circle at 30% 25%, #3a4556 0%, #1c2230 60%, #0e1320 100%)',
          boxShadow: `inset 0 2px 4px rgba(255,255,255,0.1), inset 0 -3px 6px rgba(0,0,0,0.6), 0 0 12px ${color}40`,
          transform: `rotate(${angle}deg)`,
        }}>
          <div style={{
            position: 'absolute', left: '50%', top: 5,
            width: 2.5, height: '30%', transform: 'translateX(-50%)',
            background: color, borderRadius: 2,
            boxShadow: `0 0 5px ${color}`,
          }}/>
          <div style={{
            position: 'absolute', inset: '32%',
            borderRadius: '50%',
            background: 'radial-gradient(circle at 35% 30%, #4a5668 0%, #1a2130 70%)',
          }}/>
        </div>
      </div>
      <span style={{ fontSize: 9, letterSpacing: '0.22em', color: '#a8b4c4' }}>{label}</span>
    </div>
  );
}

Object.assign(window, { MenteshProQ });
