// MENTESH 1176 — FET-style compressor, laid out to match the UA 1176LN reference photo
// Order L→R: INPUT · OUTPUT · [ATTACK / RELEASE stacked] · RATIO column · VU meter · METER column

const COMP1176_VARIANTS = {
  black: {
    name: 'Blackface',
    faceplate: 'linear-gradient(180deg, #141210 0%, #0a0908 50%, #050403 100%)',
    ink: '#e8dcb8',
    inkSubtle: 'rgba(232,220,184,0.72)',
    accent: '#d8c89a',
    pinstripe: '#8a7850',
  },
  silver: {
    name: 'Silverface',
    faceplate: 'linear-gradient(180deg, #d4ccbc 0%, #b8ad9a 100%)',
    ink: '#1a1410',
    inkSubtle: 'rgba(26,20,16,0.7)',
    accent: '#1a1410',
    pinstripe: '#4a4038',
  },
  purple: {
    // Purple Audio MC77 — deep violet faceplate, cream/silver ink, chrome knobs
    name: 'MC77 Purple',
    faceplate: 'linear-gradient(180deg, #3a1d5a 0%, #2a1344 50%, #1a0a2e 100%)',
    ink: '#f0e8d0',
    inkSubtle: 'rgba(240,232,208,0.72)',
    accent: '#e8d8a8',
    pinstripe: '#8a6eb8',
  },
};

function MENTESH1176({ variant = 'black', audioLevel = 0, isPlaying = false }) {
  const v = COMP1176_VARIANTS[variant];

  const [input, setInput] = React.useState(0.42);
  const [output, setOutput] = React.useState(0.55);
  const [attack, setAttack] = React.useState(0.6);
  const [release, setRelease] = React.useState(0.45);
  const [ratio, setRatio] = React.useState('4');
  const [allButtons, setAllButtons] = React.useState(false);
  const [meterMode, setMeterMode] = React.useState('GR');

  const ratioNum = ratio === '4' ? 4 : ratio === '8' ? 8 : ratio === '12' ? 12 : 20;
  const simGR = React.useMemo(() => {
    if (!isPlaying) return 0;
    const driven = audioLevel * (0.5 + input * 1.5);
    const threshold = 0.4;
    const over = Math.max(0, driven - threshold);
    const gr = -over * 18 * (1 - 1 / ratioNum) * (allButtons ? 1.6 : 1);
    return Math.max(-20, gr);
  }, [audioLevel, input, ratioNum, allButtons, isPlaying]);

  const meterValue = meterMode === 'GR' ? simGR :
    meterMode === '+8' ? (isPlaying ? -8 + audioLevel * 14 : -20) :
    meterMode === '+4' ? (isPlaying ? -4 + audioLevel * 10 : -20) :
    -20;

  return (
    <div style={{
      width: 1400, height: 220,
      display: 'flex',
    }}>
      <RackEar side="left" variant={{ faceplate: v.faceplate }} />

      <div style={{
        flex: 1,
        background: v.faceplate,
        position: 'relative',
        boxShadow: 'inset 0 2px 4px rgba(255,255,255,0.06), inset 0 -2px 6px rgba(0,0,0,0.5)',
        overflow: 'hidden',
        '--ink-engrave': v.ink,
      }}>
        {/* fine grain */}
        <div style={{
          position: 'absolute', inset: 0,
          backgroundImage: `repeating-linear-gradient(90deg,
            rgba(255,255,255,0.02) 0px, rgba(255,255,255,0.02) 1px,
            rgba(0,0,0,0.05) 1px, rgba(0,0,0,0.05) 2px)`,
          pointerEvents: 'none', opacity: 0.6,
        }}/>

        {/* gold/cream hairline pinstripe border */}
        <div style={{
          position: 'absolute', left: 18, right: 18, top: 12, bottom: 12,
          border: `1px solid ${v.pinstripe}`,
          opacity: 0.55,
          pointerEvents: 'none',
        }}/>

        {/* corner screws */}
        <div style={{ position: 'absolute', left: 26, top: 20 }}><Screw angle={32} size={12} /></div>
        <div style={{ position: 'absolute', right: 26, top: 20 }}><Screw angle={128} size={12} /></div>
        <div style={{ position: 'absolute', left: 26, bottom: 20 }}><Screw angle={77} size={12} /></div>
        <div style={{ position: 'absolute', right: 26, bottom: 20 }}><Screw angle={15} size={12} /></div>

        {/* ── INPUT knob ── */}
        <div style={{ position: 'absolute', left: 90, top: 42 }}>
          <BigSilverKnob value={input} onChange={setInput} label="INPUT" v={v} />
        </div>

        {/* ── OUTPUT knob ── */}
        <div style={{ position: 'absolute', left: 260, top: 42 }}>
          <BigSilverKnob value={output} onChange={setOutput} label="OUTPUT" v={v} />
        </div>

        {/* ── ATTACK + RELEASE (stacked) ── */}
        <div style={{
          position: 'absolute', left: 460, top: 14,
          display: 'flex', flexDirection: 'column', gap: 4, alignItems: 'center',
        }}>
          <SmallKnob value={attack} onChange={setAttack} label="ATTACK" v={v} />
          <SmallKnob value={release} onChange={setRelease} label="RELEASE" v={v} />
        </div>

        {/* ── RATIO column (4 stacked silver buttons, 20/12/8/4 top→bottom) ── */}
        <div style={{
          position: 'absolute', left: 580, top: 26,
          display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
        }}>
          <div style={{
            fontFamily: 'var(--font-engrave)', fontSize: 10, fontWeight: 700,
            letterSpacing: '0.22em', color: v.ink, marginBottom: 2,
          }}>RATIO</div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
            {['20', '12', '8', '4'].map(r => (
              <ColumnButton key={r} label={r}
                active={!allButtons && ratio === r}
                onClick={() => {
                  if (allButtons) setAllButtons(false);
                  setRatio(r);
                  window.playKnobTick && window.playKnobTick(0.85);
                }}/>
            ))}
          </div>
          <button onClick={() => {
            setAllButtons(!allButtons);
            window.playKnobTick && window.playKnobTick(0.9);
          }} style={{
            marginTop: 4,
            background: allButtons
              ? 'linear-gradient(180deg, #c83a3a 0%, #8a2424 100%)'
              : 'transparent',
            color: allButtons ? '#fff' : v.inkSubtle,
            border: `1px solid ${allButtons ? '#c83a3a' : v.pinstripe}`,
            fontFamily: 'var(--font-engrave)',
            fontSize: 7, fontWeight: 700,
            letterSpacing: '0.18em', padding: '2px 6px',
            cursor: 'pointer', borderRadius: 2,
          }}>ALL BTN</button>
        </div>

        {/* ── VU METER (centered-right) ── */}
        <div style={{
          position: 'absolute', left: 660, top: 14,
          display: 'flex', flexDirection: 'column', alignItems: 'center',
        }}>
          {/* ME CLASSICS signature — printed directly on the plate */}
          <div style={{
            display: 'flex', alignItems: 'center', gap: 5,
            marginBottom: 4,
          }}>
            <svg width="22" height="17" viewBox="0 0 30 22">
              <ellipse cx="15" cy="11" rx="14" ry="9" fill="none" stroke={v.ink} strokeWidth="0.8" />
              <text x="15" y="14.5" textAnchor="middle"
                fontFamily="'Playfair Display',serif" fontSize="9" fontWeight="900"
                fontStyle="italic" fill={v.ink}>ME</text>
            </svg>
            <span style={{
              fontFamily: "'Oswald','Bebas Neue',sans-serif",
              fontSize: 9, fontWeight: 700,
              letterSpacing: '0.22em', color: v.ink,
            }}>CLASSICS</span>
          </div>
          {/* Black plastic frame around just the VU window */}
          <div style={{
            padding: 5,
            background: 'linear-gradient(180deg, #1a1612 0%, #0a0806 50%, #050403 100%)',
            border: '1px solid #000',
            borderRadius: 4,
            boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.06), 0 2px 4px rgba(0,0,0,0.5)',
          }}>
            <UA1176Meter value={meterValue} mode={meterMode} v={v} />
          </div>
          {/* below-meter nameplate (centered) */}
          <div style={{
            marginTop: 4,
            display: 'flex', flexDirection: 'column', alignItems: 'center',
          }}>
            <div style={{
              fontFamily: 'var(--font-engrave)', fontSize: 9, fontWeight: 700,
              letterSpacing: '0.16em', color: v.ink,
            }}>ME76 · LIMITING AMPLIFIER</div>
            <div style={{
              fontFamily: "'Bebas Neue','Oswald',sans-serif",
              fontSize: 11, fontWeight: 400,
              letterSpacing: '0.18em', color: v.ink,
              marginTop: 1,
            }}>MENTESH ENGINEERING</div>
          </div>
        </div>

        {/* ── METER column (4 stacked buttons: GR / +8 / +4 / OFF) ── */}
        <div style={{
          position: 'absolute', right: 80, top: 26,
          display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
        }}>
          <div style={{
            fontFamily: 'var(--font-engrave)', fontSize: 10, fontWeight: 700,
            letterSpacing: '0.22em', color: v.ink, marginBottom: 2,
          }}>METER</div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
            {['GR', '+8', '+4', 'OFF'].map(m => (
              <ColumnButton key={m} label={m}
                active={meterMode === m}
                onClick={() => {
                  setMeterMode(m);
                  window.playKnobTick && window.playKnobTick(0.75);
                }}
                wide
              />
            ))}
          </div>
        </div>
      </div>

      <RackEar side="right" variant={{ faceplate: v.faceplate }} />
    </div>
  );
}

// Big silver aluminum knob (INPUT / OUTPUT) — 1176-style cylindrical knob with black indicator notch
function BigSilverKnob({ value, onChange, label, v }) {
  const size = 70;
  const ringR = 58;
  // Scale numbers for 1176 INPUT/OUTPUT: ∞ · 0 · 6 · 12 · 18 · 24 · 30 · 36 · 48 — wraps counterclockwise
  // Matching photo arrangement: ∞ at 7-o'clock, 48 at 8-o'clock going up, 30 at top, 0 at 4-o'clock
  // We'll place them at -225° to +45° (going CCW). For simplicity draw 9 markers 30° apart.
  const labels = ['∞', '0', '6', '12', '18', '24', '30', '36', '48'];
  const minA = -135, maxA = 135;
  // value 0..1 maps rotation minA..maxA; numbers positioned around the knob, but in 1176 order
  // For visual fidelity place labels evenly on an arc from -140° to +140°
  const angle = minA + value * (maxA - minA);

  const onPointerDown = (e) => {
    e.preventDefault();
    const startY = e.clientY;
    const startVal = value;
    const steps = 40;
    let lastStep = Math.round(startVal * steps);
    const onMove = (ev) => {
      const dy = startY - ev.clientY;
      const raw = Math.max(0, Math.min(1, startVal + dy / 220));
      const step = Math.round(raw * steps);
      if (step !== lastStep) {
        lastStep = step;
        window.playKnobTick && window.playKnobTick(0.45);
      }
      onChange(step / steps);
    };
    const onUp = () => {
      window.removeEventListener('pointermove', onMove);
      window.removeEventListener('pointerup', onUp);
    };
    window.addEventListener('pointermove', onMove);
    window.addEventListener('pointerup', onUp);
  };
  const onWheel = (e) => {
    e.preventDefault();
    const steps = 40;
    const dir = e.deltaY > 0 ? -1 : 1;
    const cur = Math.round(value * steps);
    const next = Math.max(0, Math.min(steps, cur + dir));
    if (next !== cur) window.playKnobTick && window.playKnobTick(0.45);
    onChange(next / steps);
  };

  const W = ringR * 2 + 32;
  const cx = W / 2, cy = W / 2;

  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
      <div style={{ position: 'relative', width: W, height: W - 6 }}>
        <svg width={W} height={W} style={{ position: 'absolute', inset: 0, overflow: 'visible' }}>
          {labels.map((lbl, i) => {
            const t = i / (labels.length - 1);
            const a = minA + t * (maxA - minA);
            const rad = (a - 90) * Math.PI / 180;
            return (
              <g key={i}>
                <line x1={cx + Math.cos(rad) * (ringR - 8)} y1={cy + Math.sin(rad) * (ringR - 8)}
                  x2={cx + Math.cos(rad) * (ringR - 2)} y2={cy + Math.sin(rad) * (ringR - 2)}
                  stroke={v.ink} strokeWidth="1.1" opacity="0.85" />
                <text x={cx + Math.cos(rad) * (ringR + 6)} y={cy + Math.sin(rad) * (ringR + 6) + 3}
                  textAnchor="middle" fontSize="9" fontFamily="var(--font-engrave)"
                  fontWeight="700" fill={v.ink} opacity="0.92">{lbl}</text>
              </g>
            );
          })}
        </svg>
        <div style={{
          position: 'absolute', left: '50%', top: '50%',
          transform: 'translate(-50%, -50%)',
        }}>
          <div onPointerDown={onPointerDown} onWheel={onWheel}
            style={{ width: size, height: size, cursor: 'ns-resize' }}>
            <AluminumKnob size={size} angle={angle} />
          </div>
        </div>
      </div>
      <div style={{
        fontFamily: 'var(--font-engrave)', fontSize: 10, fontWeight: 700,
        letterSpacing: '0.22em', color: v.ink, marginTop: 2,
      }}>{label}</div>
    </div>
  );
}

// Machined aluminum knob (tall cylinder) — used for INPUT / OUTPUT
function AluminumKnob({ size, angle }) {
  // Vintage black knurled/fluted body with brushed aluminum cap and dot indicator
  const flutes = 28;
  const capR = size * 0.36; // top cap radius
  return (
    <div style={{
      width: size, height: size, borderRadius: '50%',
      // Outer black fluted body — slight conical taper feel from darker rim to lit top
      background: `
        radial-gradient(circle at 50% 42%, #2a2622 0%, #14110f 45%, #080604 85%)
      `,
      boxShadow: `
        0 4px 8px rgba(0,0,0,0.75),
        inset 0 -4px 6px rgba(0,0,0,0.7),
        inset 0 2px 2px rgba(255,255,255,0.12)
      `,
      position: 'relative',
    }}>
      {/* flute/knurl ring — vertical black ridges around the skirt */}
      <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}
        style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
        <defs>
          <radialGradient id={`fluteMask-${size}`} cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="black" stopOpacity="0" />
            <stop offset="62%" stopColor="black" stopOpacity="0" />
            <stop offset="70%" stopColor="black" stopOpacity="1" />
            <stop offset="100%" stopColor="black" stopOpacity="1" />
          </radialGradient>
          <mask id={`flute-mask-${size}`}>
            <rect width={size} height={size} fill={`url(#fluteMask-${size})`} />
          </mask>
        </defs>
        <g mask={`url(#flute-mask-${size})`}>
          {Array.from({ length: flutes }).map((_, i) => {
            const a = (i / flutes) * 360;
            const rad = (a - 90) * Math.PI / 180;
            const cx = size / 2, cy = size / 2;
            const r1 = size * 0.34;
            const r2 = size * 0.50;
            const x1 = cx + Math.cos(rad) * r1;
            const y1 = cy + Math.sin(rad) * r1;
            const x2 = cx + Math.cos(rad) * r2;
            const y2 = cy + Math.sin(rad) * r2;
            return (
              <line key={i} x1={x1} y1={y1} x2={x2} y2={y2}
                stroke="rgba(0,0,0,0.85)" strokeWidth="1.1" />
            );
          })}
          {/* highlight between flutes */}
          {Array.from({ length: flutes }).map((_, i) => {
            const a = ((i + 0.5) / flutes) * 360;
            const rad = (a - 90) * Math.PI / 180;
            const cx = size / 2, cy = size / 2;
            const r1 = size * 0.36;
            const r2 = size * 0.48;
            const x1 = cx + Math.cos(rad) * r1;
            const y1 = cy + Math.sin(rad) * r1;
            const x2 = cx + Math.cos(rad) * r2;
            const y2 = cy + Math.sin(rad) * r2;
            return (
              <line key={`h-${i}`} x1={x1} y1={y1} x2={x2} y2={y2}
                stroke="rgba(90,85,78,0.35)" strokeWidth="0.6" />
            );
          })}
        </g>
      </svg>

      {/* dark outer rim line */}
      <div style={{
        position: 'absolute', inset: 0, borderRadius: '50%',
        boxShadow: 'inset 0 0 0 1px rgba(0,0,0,0.8)',
        pointerEvents: 'none',
      }}/>

      {/* brushed aluminum top cap (rotates with indicator) */}
      <div style={{
        position: 'absolute', left: '50%', top: '50%',
        transform: `translate(-50%, -50%) rotate(${angle}deg)`,
        width: capR * 2, height: capR * 2, borderRadius: '50%',
        // Brushed aluminum — cross-brushed effect
        background: `
          conic-gradient(from 0deg,
            #b8b2a8 0deg, #e8e2d6 30deg, #c8c2b6 60deg,
            #e8e2d6 90deg, #a8a298 120deg, #d8d2c6 150deg,
            #b8b2a8 180deg, #e8e2d6 210deg, #c8c2b6 240deg,
            #e8e2d6 270deg, #a8a298 300deg, #d8d2c6 330deg,
            #b8b2a8 360deg),
          radial-gradient(circle at 38% 30%, #f4efe3 0%, #c8c2b6 70%)
        `,
        backgroundBlendMode: 'soft-light, normal',
        boxShadow: `
          0 2px 3px rgba(0,0,0,0.55),
          inset 0 1px 1px rgba(255,255,255,0.6),
          inset 0 -1px 2px rgba(0,0,0,0.3),
          inset 0 0 0 0.5px rgba(0,0,0,0.4)
        `,
      }}>
        {/* brushed radial lines (cross shine) */}
        <svg width={capR * 2} height={capR * 2}
          style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
          <defs>
            <linearGradient id={`brush-${size}`} x1="0%" y1="0%" x2="100%" y2="100%">
              <stop offset="0%" stopColor="rgba(255,255,255,0.2)" />
              <stop offset="50%" stopColor="rgba(255,255,255,0)" />
              <stop offset="100%" stopColor="rgba(0,0,0,0.15)" />
            </linearGradient>
          </defs>
          <circle cx={capR} cy={capR} r={capR - 0.5} fill={`url(#brush-${size})`} />
          {/* cross-brush marks */}
          <line x1={capR * 0.2} y1={capR * 0.2} x2={capR * 1.8} y2={capR * 1.8}
            stroke="rgba(255,255,255,0.12)" strokeWidth="0.4" />
          <line x1={capR * 0.2} y1={capR * 1.8} x2={capR * 1.8} y2={capR * 0.2}
            stroke="rgba(255,255,255,0.12)" strokeWidth="0.4" />
        </svg>

        {/* small black dot indicator at 12 o'clock on the cap */}
        <div style={{
          position: 'absolute',
          left: '50%', top: '15%',
          transform: 'translateX(-50%)',
          width: capR * 0.28, height: capR * 0.28,
          borderRadius: '50%',
          background: '#0a0806',
          boxShadow: 'inset 0 1px 1px rgba(255,255,255,0.2), 0 0.5px 0.5px rgba(255,255,255,0.2)',
          border: '0.5px solid #000',
        }}/>
      </div>
    </div>
  );
}

// Small black bakelite knob with 1-7 scale (ATTACK / RELEASE)
function SmallKnob({ value, onChange, label, v }) {
  const size = 38;
  const ringR = 28;
  const labels = ['1', '2', '3', '4', '5', '6', '7'];
  const minA = -135, maxA = 135;
  const angle = minA + value * (maxA - minA);
  const onPointerDown = (e) => {
    e.preventDefault();
    const startY = e.clientY, startVal = value;
    const steps = 30;
    let lastStep = Math.round(startVal * steps);
    const onMove = (ev) => {
      const dy = startY - ev.clientY;
      const raw = Math.max(0, Math.min(1, startVal + dy / 180));
      const step = Math.round(raw * steps);
      if (step !== lastStep) {
        lastStep = step;
        window.playKnobTick && window.playKnobTick(0.4);
      }
      onChange(step / steps);
    };
    const onUp = () => {
      window.removeEventListener('pointermove', onMove);
      window.removeEventListener('pointerup', onUp);
    };
    window.addEventListener('pointermove', onMove);
    window.addEventListener('pointerup', onUp);
  };
  const onWheel = (e) => {
    e.preventDefault();
    const steps = 30;
    const dir = e.deltaY > 0 ? -1 : 1;
    const cur = Math.round(value * steps);
    const next = Math.max(0, Math.min(steps, cur + dir));
    if (next !== cur) window.playKnobTick && window.playKnobTick(0.4);
    onChange(next / steps);
  };
  const W = ringR * 2 + 22;
  const cx = W / 2, cy = W / 2;
  return (
    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
      <div style={{ position: 'relative', width: W, height: W }}>
        <svg width={W} height={W} style={{ position: 'absolute', inset: 0, overflow: 'visible' }}>
          {/* OFF marker at bottom */}
          <text x={cx} y={cy + ringR + 7} textAnchor="middle"
            fontSize="6" fontFamily="var(--font-engrave)"
            fontWeight="700" fill={v.inkSubtle}>OFF</text>
          {labels.map((lbl, i) => {
            const t = i / (labels.length - 1);
            const a = minA + t * (maxA - minA);
            const rad = (a - 90) * Math.PI / 180;
            return (
              <g key={i}>
                <line x1={cx + Math.cos(rad) * (ringR - 4)} y1={cy + Math.sin(rad) * (ringR - 4)}
                  x2={cx + Math.cos(rad) * (ringR - 1)} y2={cy + Math.sin(rad) * (ringR - 1)}
                  stroke={v.ink} strokeWidth="0.8" opacity="0.8" />
                <text x={cx + Math.cos(rad) * (ringR + 4.5)} y={cy + Math.sin(rad) * (ringR + 4.5) + 2.2}
                  textAnchor="middle" fontSize="6.5" fontFamily="var(--font-engrave)"
                  fontWeight="700" fill={v.ink} opacity="0.9">{lbl}</text>
              </g>
            );
          })}
        </svg>
        <div style={{
          position: 'absolute', left: '50%', top: '50%',
          transform: 'translate(-50%, -50%)',
        }}>
          <div onPointerDown={onPointerDown} onWheel={onWheel}
            style={{ width: size, height: size, cursor: 'ns-resize' }}>
            <KnobBody size={size} angle={angle} style="bakelite" />
          </div>
        </div>
      </div>
      <div style={{
        fontFamily: 'var(--font-engrave)', fontSize: 8, fontWeight: 700,
        letterSpacing: '0.2em', color: v.ink, marginTop: 1,
      }}>{label}</div>
    </div>
  );
}

// Stacked column rectangular push-button (silver, pill-shaped) — RATIO and METER columns
function ColumnButton({ label, active, onClick, wide }) {
  return (
    <button onClick={onClick} style={{
      width: wide ? 36 : 32, height: 22,
      background: active
        ? 'linear-gradient(180deg, #f8f4e8 0%, #c8c0b0 50%, #8a8478 100%)'
        : 'linear-gradient(180deg, #2a2620 0%, #0a0806 100%)',
      border: '1px solid #000',
      borderRadius: 3,
      color: active ? '#1a1410' : '#a8a094',
      fontFamily: 'var(--font-engrave)',
      fontSize: 10, fontWeight: 700,
      letterSpacing: '0.06em',
      cursor: 'pointer',
      boxShadow: active
        ? 'inset 0 -2px 3px rgba(0,0,0,0.45), inset 0 1px 0 rgba(255,255,255,0.55), 0 1px 2px rgba(0,0,0,0.5)'
        : 'inset 0 1px 0 rgba(255,255,255,0.1), 0 2px 2px rgba(0,0,0,0.6)',
      transform: active ? 'translateY(1px)' : 'translateY(0)',
      transition: 'all 80ms',
    }}>{label}</button>
  );
}

// UA-style VU meter — cream/amber face, red zone right, swinging needle
function UA1176Meter({ value, mode, v }) {
  const [displayed, setDisplayed] = React.useState(value);
  React.useEffect(() => {
    let raf;
    let cur = displayed, vel = 0;
    const step = () => {
      const k = 0.14, damp = 0.78;
      vel = vel * damp + (value - cur) * k;
      cur += vel;
      setDisplayed(cur);
      raf = requestAnimationFrame(step);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [value]);

  const W = 230, H = 115;
  const cx = W / 2, cy = H * 1.18, rArc = H * 1.05;
  const clamped = Math.max(-20, Math.min(3, displayed));
  const angle = (clamped + 10) / 13 * 54 - 27;
  const valToA = (vv) => (vv + 10) / 13 * 54 - 27;

  return (
    <div style={{
      width: W + 12, height: H + 8,
      padding: 4,
      background: 'linear-gradient(180deg, #3a362f 0%, #1a1612 100%)',
      borderRadius: 4,
      boxShadow: 'inset 0 1px 3px rgba(0,0,0,0.8), 0 1px 0 rgba(255,255,255,0.08)',
      border: '1px solid #000',
    }}>
      <div style={{
        width: '100%', height: '100%',
        background: 'radial-gradient(ellipse at 50% 48%, #fbe896 0%, #f2d472 55%, #c89840 100%)',
        position: 'relative', overflow: 'hidden',
        borderRadius: 2,
        boxShadow: 'inset 0 0 24px rgba(140,80,10,0.32), inset 0 1px 2px rgba(0,0,0,0.2)',
      }}>
        <svg width={W} height={H + 4} viewBox={`0 0 ${W} ${H + 4}`} style={{ display: 'block' }}>
          {/* main arc */}
          <path d={`M ${cx - rArc * Math.sin(27 * Math.PI / 180)} ${cy - rArc * Math.cos(27 * Math.PI / 180)}
                    A ${rArc} ${rArc} 0 0 1
                    ${cx + rArc * Math.sin(27 * Math.PI / 180)} ${cy - rArc * Math.cos(27 * Math.PI / 180)}`}
            stroke="#1a0f08" strokeWidth="0.9" fill="none" />
          {/* red zone (positive side) */}
          <path d={`M ${cx + rArc * Math.sin(0)} ${cy - rArc}
                    A ${rArc} ${rArc} 0 0 1
                    ${cx + rArc * Math.sin(27 * Math.PI / 180)} ${cy - rArc * Math.cos(27 * Math.PI / 180)}`}
            stroke="#c8271a" strokeWidth="2.4" fill="none" />

          {/* VU scale numerals 20 10 7 5 3 0 1 2 3 */}
          {[
            { v: -20, lbl: '20' }, { v: -10, lbl: '10' }, { v: -7, lbl: '7' },
            { v: -5, lbl: '5' }, { v: -3, lbl: '3' }, { v: 0, lbl: '0' },
            { v: 1, lbl: '1' }, { v: 2, lbl: '2' }, { v: 3, lbl: '3' },
          ].map((n, i) => {
            const a = valToA(n.v) * Math.PI / 180;
            const x1 = cx + rArc * Math.sin(a), y1 = cy - rArc * Math.cos(a);
            const x2 = cx + (rArc - 7) * Math.sin(a), y2 = cy - (rArc - 7) * Math.cos(a);
            const tx = cx + (rArc - 15) * Math.sin(a), ty = cy - (rArc - 15) * Math.cos(a);
            const red = n.v > 0;
            return (
              <g key={i}>
                <line x1={x1} y1={y1} x2={x2} y2={y2}
                  stroke={red ? '#c8271a' : '#1a0f08'} strokeWidth="0.9" />
                <text x={tx} y={ty + 3} textAnchor="middle" fontSize="7"
                  fontWeight="700" fill={red ? '#c8271a' : '#1a0f08'}
                  fontFamily="var(--font-engrave)">{n.lbl}</text>
              </g>
            );
          })}
          {/* "VU" on each side of the dial */}
          <text x={cx - rArc * 0.6} y={cy - rArc * 0.35} textAnchor="middle"
            fontSize="10" fontWeight="700" fill="#1a0f08"
            fontFamily="var(--font-engrave)" letterSpacing="1.5">VU</text>
          <text x={cx + rArc * 0.6} y={cy - rArc * 0.35} textAnchor="middle"
            fontSize="10" fontWeight="700" fill="#1a0f08"
            fontFamily="var(--font-engrave)" letterSpacing="1.5">VU</text>
          {/* mode label */}
          <text x={cx} y={cy - rArc * 0.5 + 20} textAnchor="middle"
            fontSize="6" fontWeight="600" fill="#1a0f08"
            fontFamily="var(--font-engrave)" letterSpacing="1.5" opacity="0.7">
            {mode === 'OFF' ? '— OFF —' : mode}
          </text>
          {/* needle */}
          {mode !== 'OFF' && (
            <g transform={`rotate(${angle} ${cx} ${cy})`}>
              <line x1={cx} y1={cy} x2={cx} y2={cy - rArc - 2}
                stroke="#0a0604" strokeWidth="1.1" strokeLinecap="round" />
              <circle cx={cx} cy={cy} r="3.5" fill="#1a0f08" />
            </g>
          )}
        </svg>
        {/* glass reflection */}
        <div style={{
          position: 'absolute', inset: 0,
          background: 'linear-gradient(135deg, rgba(255,255,255,0.18) 0%, transparent 40%)',
          pointerEvents: 'none',
        }}/>
      </div>
    </div>
  );
}

Object.assign(window, { MENTESH1176, COMP1176_VARIANTS });
