Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

43.2 KB
Raw Blame
import React, { useState, useEffect, useRef, useMemo } from 'react';
import { X, Trash2 } from 'lucide-react';
import {
  AdjustmentLayerData,
  CurvesAdjustmentData,
  GradientMapAdjustmentData,
  LevelsAdjustmentData,
  ColorBalanceAdjustmentData,
  GradientStop,
  ToneCurvePoints,
} from '../types/editor';
import {
  buildMonotonicCubicSplineLut,
  buildGradientMapLut,
} from '../utils/toolConfigs';
import { nativeLayerHistogram, type NativeLayerHistogram } from '../utils/nativeEngineBridge';
import { isImeComposing, useI18n } from '../i18n';

export interface AdjustmentModalProps {
  isOpen: boolean;
  onClose: () => void;
  onApply: (type: 'curves' | 'gradient-map' | 'levels' | 'color-balance', data: AdjustmentLayerData) => void;
  initialType?: 'curves' | 'gradient-map' | 'levels' | 'color-balance';
  initialData?: AdjustmentLayerData;
  editingLayerId?: string | null;
  onPreviewChange?: (data: AdjustmentLayerData | null, enabled: boolean) => void;
  histogramImageData?: ImageData | null;
}

const DEFAULT_CURVES: ToneCurvePoints = {
  rgb: [[0, 0], [255, 255]],
  r: [[0, 0], [255, 255]],
  g: [[0, 0], [255, 255]],
  b: [[0, 0], [255, 255]],
};

const DEFAULT_GRADIENT_STOPS: GradientStop[] = [
  { position: 0, color: '#000000' },
  { position: 1, color: '#ffffff' },
];

const GRADIENT_PRESETS: { name: string; stops: GradientStop[]; interpolation: 'linear' | 'smooth' }[] = [
  {
    name: 'B&W',
    stops: [{ position: 0, color: '#000000' }, { position: 1, color: '#ffffff' }],
    interpolation: 'linear',
  },
  {
    name: 'Sepia',
    stops: [
      { position: 0, color: '#1a0f00' },
      { position: 0.35, color: '#704214' },
      { position: 0.75, color: '#d2b48c' },
      { position: 1, color: '#fffff0' },
    ],
    interpolation: 'smooth',
  },
  {
    name: 'Sunset',
    stops: [
      { position: 0, color: '#0f051d' },
      { position: 0.3, color: '#591a4f' },
      { position: 0.6, color: '#c83e4d' },
      { position: 0.85, color: '#f4b251' },
      { position: 1, color: '#fff1b0' },
    ],
    interpolation: 'smooth',
  },
  {
    name: 'Cyberpunk',
    stops: [
      { position: 0, color: '#03001e' },
      { position: 0.35, color: '#7303c0' },
      { position: 0.75, color: '#ec38bc' },
      { position: 1, color: '#fdeff9' },
    ],
    interpolation: 'smooth',
  },
  {
    name: 'Emerald',
    stops: [
      { position: 0, color: '#051c09' },
      { position: 0.35, color: '#114b1e' },
      { position: 0.7, color: '#2ca048' },
      { position: 1, color: '#9be88b' },
    ],
    interpolation: 'smooth',
  },
  {
    name: 'Inferno',
    stops: [
      { position: 0, color: '#000004' },
      { position: 0.3, color: '#51127c' },
      { position: 0.65, color: '#b73779' },
      { position: 0.9, color: '#fb8861' },
      { position: 1, color: '#fcfdbf' },
    ],
    interpolation: 'smooth',
  },
];

export const AdjustmentModal: React.FC<AdjustmentModalProps> = ({
  isOpen,
  onClose,
  onApply,
  initialType = 'curves',
  initialData,
  editingLayerId,
  onPreviewChange,
  histogramImageData: _histogramImageData,
}) => {
  const { t, tc, td } = useI18n();
  const [activeTab, setActiveTab] = useState<'curves' | 'gradient-map' | 'levels' | 'color-balance'>(initialType);
  const [previewEnabled, setPreviewEnabled] = useState(true);

  // Curves State
  const [curves, setCurves] = useState<ToneCurvePoints>(DEFAULT_CURVES);
  const [curveChannel, setCurveChannel] = useState<'rgb' | 'r' | 'g' | 'b'>('rgb');
  const [selectedPointIdx, setSelectedPointIdx] = useState<number | null>(null);

  // Gradient Map State
  const [gradientStops, setGradientStops] = useState<GradientStop[]>(DEFAULT_GRADIENT_STOPS);
  const [gradientInterpolation, setGradientInterpolation] = useState<'linear' | 'smooth'>('smooth');
  const [selectedStopIdx, setSelectedStopIdx] = useState<number>(0);

  // Levels State
  const [levels, setLevels] = useState<{
    blackPoint: number;
    gamma: number;
    whitePoint: number;
    outputMin: number;
    outputMax: number;
    channel: 'rgb' | 'r' | 'g' | 'b';
  }>({
    blackPoint: 0,
    gamma: 1.0,
    whitePoint: 255,
    outputMin: 0,
    outputMax: 255,
    channel: 'rgb',
  });

  // Color Balance State
  const [colorBalance, setColorBalance] = useState<{
    shadows: [number, number, number];
    midtones: [number, number, number];
    highlights: [number, number, number];
    preserveLuminosity: boolean;
  }>({
    shadows: [0, 0, 0],
    midtones: [0, 0, 0],
    highlights: [0, 0, 0],
    preserveLuminosity: true,
  });
  const [cbRange, setCbRange] = useState<'shadows' | 'midtones' | 'highlights'>('midtones');

  // Load initial data
  useEffect(() => {
    if (initialType) {
      setActiveTab(initialType);
    }
    if (initialData) {
      if (initialData.type === 'curves') {
        setCurves(JSON.parse(JSON.stringify(initialData.curves)));
      } else if (initialData.type === 'gradient-map') {
        setGradientStops(JSON.parse(JSON.stringify(initialData.stops)));
        setGradientInterpolation(initialData.interpolation || 'smooth');
      } else if (initialData.type === 'levels') {
        setLevels({
          blackPoint: initialData.blackPoint,
          gamma: initialData.gamma,
          whitePoint: initialData.whitePoint,
          outputMin: initialData.outputMin ?? 0,
          outputMax: initialData.outputMax ?? 255,
          channel: initialData.channel || 'rgb',
        });
      } else if (initialData.type === 'color-balance') {
        setColorBalance({
          shadows: [...initialData.shadows],
          midtones: [...initialData.midtones],
          highlights: [...initialData.highlights],
          preserveLuminosity: initialData.preserveLuminosity ?? true,
        });
      }
    }
  }, [initialType, initialData, isOpen]);

  // Current adjustment data object
  const currentAdjustmentData: AdjustmentLayerData = useMemo(() => {
    switch (activeTab) {
      case 'curves':
        return { type: 'curves', curves } as CurvesAdjustmentData;
      case 'gradient-map':
        return {
          type: 'gradient-map',
          stops: gradientStops,
          interpolation: gradientInterpolation,
        } as GradientMapAdjustmentData;
      case 'levels':
        return {
          type: 'levels',
          ...levels,
        } as LevelsAdjustmentData;
      case 'color-balance':
        return {
          type: 'color-balance',
          ...colorBalance,
        } as ColorBalanceAdjustmentData;
    }
  }, [activeTab, curves, gradientStops, gradientInterpolation, levels, colorBalance]);

  // Notify preview changes
  useEffect(() => {
    if (isOpen && onPreviewChange) {
      onPreviewChange(currentAdjustmentData, previewEnabled);
    }
  }, [currentAdjustmentData, previewEnabled, isOpen, onPreviewChange]);

  const [nativeHist, setNativeHist] = useState<NativeLayerHistogram | null>(null);
  useEffect(() => {
    if (!isOpen) return;
    let cancelled = false;
    void nativeLayerHistogram()
      .then((h) => {
        if (!cancelled) setNativeHist(h);
      })
      .catch(() => {});
    return () => {
      cancelled = true;
    };
  }, [isOpen]);

  // Native Rust layer histogram for Tone Curve / Levels background
  const histogramData = useMemo(() => {
    if (!nativeHist) return null;
    const raw =
      curveChannel === 'r'
        ? nativeHist.r
        : curveChannel === 'g'
          ? nativeHist.g
          : curveChannel === 'b'
            ? nativeHist.b
            : (nativeHist.rgb ?? nativeHist.luminance);
    if (!Array.isArray(raw) || raw.length < 256) return null;
    return new Uint32Array(raw);
  }, [nativeHist, curveChannel]);

  const maxHistogramCount = useMemo(() => {
    if (!histogramData) return 1;
    let max = 1;
    for (let i = 0; i < 256; i++) {
      if (histogramData[i] > max) max = histogramData[i];
    }
    return max;
  }, [histogramData]);

  // Tone Curve calculation for selected channel
  const currentPts = curves[curveChannel];
  const curveLut = useMemo(() => {
    return buildMonotonicCubicSplineLut(currentPts);
  }, [currentPts]);

  // SVG path for active curve
  const curveSvgPath = useMemo(() => {
    let d = `M 0 ${256 - curveLut[0]}`;
    for (let x = 1; x < 256; x++) {
      d += ` L ${x} ${256 - curveLut[x]}`;
    }
    return d;
  }, [curveLut]);

  // Handle Tone Curve control point drag
  const curveSvgRef = useRef<SVGSVGElement | null>(null);
  const [isDraggingPoint, setIsDraggingPoint] = useState(false);

  const handleCurvePointerDown = (e: React.PointerEvent<SVGSVGElement>) => {
    const svg = curveSvgRef.current;
    if (!svg) return;
    const rect = svg.getBoundingClientRect();
    const x = Math.max(0, Math.min(255, Math.round(((e.clientX - rect.left) / rect.width) * 255)));
    const y = Math.max(0, Math.min(255, Math.round((1 - (e.clientY - rect.top) / rect.height) * 255)));

    // Check if clicked near an existing control point
    const pts = curves[curveChannel];
    let clickedIdx = -1;
    for (let i = 0; i < pts.length; i++) {
      const dx = pts[i][0] - x;
      const dy = pts[i][1] - y;
      if (Math.sqrt(dx * dx + dy * dy) < 14) {
        clickedIdx = i;
        break;
      }
    }

    if (clickedIdx >= 0) {
      setSelectedPointIdx(clickedIdx);
      setIsDraggingPoint(true);
      (e.target as Element).setPointerCapture(e.pointerId);
    } else {
      // Add new point
      const newPts = [...pts, [x, y] as [number, number]].sort((a, b) => a[0] - b[0]);
      const newIdx = newPts.findIndex((p) => p[0] === x && p[1] === y);
      setCurves((prev) => ({
        ...prev,
        [curveChannel]: newPts,
      }));
      setSelectedPointIdx(newIdx);
      setIsDraggingPoint(true);
      svg.setPointerCapture(e.pointerId);
    }
  };

  const handleCurvePointerMove = (e: React.PointerEvent<SVGSVGElement>) => {
    if (!isDraggingPoint || selectedPointIdx === null) return;
    const svg = curveSvgRef.current;
    if (!svg) return;
    const rect = svg.getBoundingClientRect();
    let x = Math.max(0, Math.min(255, Math.round(((e.clientX - rect.left) / rect.width) * 255)));
    const y = Math.max(0, Math.min(255, Math.round((1 - (e.clientY - rect.top) / rect.height) * 255)));

    const pts = [...curves[curveChannel]];
    // Lock X coordinate for endpoint 0 and last endpoint
    if (selectedPointIdx === 0) x = 0;
    else if (selectedPointIdx === pts.length - 1) x = 255;
    else {
      // Constrain X between neighbor points
      const minX = pts[selectedPointIdx - 1][0] + 1;
      const maxX = pts[selectedPointIdx + 1][0] - 1;
      x = Math.max(minX, Math.min(maxX, x));
    }

    pts[selectedPointIdx] = [x, y];
    setCurves((prev) => ({
      ...prev,
      [curveChannel]: pts,
    }));
  };

  const handleCurvePointerUp = (e: React.PointerEvent<SVGSVGElement>) => {
    if (isDraggingPoint) {
      setIsDraggingPoint(false);
      try {
        (e.target as Element).releasePointerCapture(e.pointerId);
      } catch (_) {}
    }
  };

  const deleteSelectedCurvePoint = () => {
    if (selectedPointIdx === null) return;
    const pts = curves[curveChannel];
    if (pts.length <= 2) return; // Keep at least 2 points
    if (selectedPointIdx === 0 || selectedPointIdx === pts.length - 1) return; // Don't delete endpoints

    const nextPts = pts.filter((_, i) => i !== selectedPointIdx);
    setCurves((prev) => ({
      ...prev,
      [curveChannel]: nextPts,
    }));
    setSelectedPointIdx(null);
  };

  // Gradient Map Stop Handlers
  const gradientRampRef = useRef<HTMLDivElement | null>(null);
  const [isDraggingStop, setIsDraggingStop] = useState(false);

  const handleRampPointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
    const ramp = gradientRampRef.current;
    if (!ramp) return;
    const rect = ramp.getBoundingClientRect();
    const pos = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));

    // Sample color from current gradient at this position
    const lut = buildGradientMapLut(gradientStops, gradientInterpolation);
    const lumIdx = Math.round(pos * 255) * 4;
    const r = lut[lumIdx].toString(16).padStart(2, '0');
    const g = lut[lumIdx + 1].toString(16).padStart(2, '0');
    const b = lut[lumIdx + 2].toString(16).padStart(2, '0');
    const newColor = `#${r}${g}${b}`;

    const newStop: GradientStop = { position: Number(pos.toFixed(3)), color: newColor };
    const nextStops = [...gradientStops, newStop].sort((a, b) => a.position - b.position);
    const newIdx = nextStops.findIndex((s) => s.position === newStop.position);
    setGradientStops(nextStops);
    setSelectedStopIdx(newIdx);
  };

  const handleStopPointerDown = (idx: number, e: React.PointerEvent) => {
    e.stopPropagation();
    setSelectedStopIdx(idx);
    setIsDraggingStop(true);
    try {
      (e.target as Element).setPointerCapture(e.pointerId);
    } catch {
      // Ignore synthetic/non-capturable pointers
    }
  };

  const handleStopPointerMove = (e: React.PointerEvent) => {
    if (!isDraggingStop) return;
    const ramp = gradientRampRef.current;
    if (!ramp) return;
    const rect = ramp.getBoundingClientRect();
    if (rect.width <= 0) return;
    const pos = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));

    setGradientStops((prev) => {
      if (selectedStopIdx < 0 || selectedStopIdx >= prev.length) return prev;
      const stops = [...prev];
      stops[selectedStopIdx] = {
        ...stops[selectedStopIdx],
        position: Number(pos.toFixed(3)),
      };
      return stops;
    });
  };

  const handleStopPointerUp = (e: React.PointerEvent) => {
    if (isDraggingStop) {
      setIsDraggingStop(false);
      try {
        (e.target as Element).releasePointerCapture(e.pointerId);
      } catch (_) {}
      // Re-sort stops while preserving selection on the dragged stop
      const draggedStop = gradientStops[selectedStopIdx];
      const sorted = [...gradientStops].sort((a, b) => a.position - b.position);
      setGradientStops(sorted);
      if (draggedStop) {
        const nextIdx = sorted.indexOf(draggedStop);
        if (nextIdx >= 0) {
          setSelectedStopIdx(nextIdx);
        }
      }
    }
  };

  const deleteSelectedStop = () => {
    if (gradientStops.length <= 2) return;
    setGradientStops((prev) => {
      const next = prev.filter((_, i) => i !== selectedStopIdx);
      return next.sort((a, b) => a.position - b.position);
    });
    setSelectedStopIdx(0);
  };

  const handleResetCurrent = () => {
    if (activeTab === 'curves') {
      setCurves(DEFAULT_CURVES);
      setSelectedPointIdx(null);
    } else if (activeTab === 'gradient-map') {
      setGradientStops(DEFAULT_GRADIENT_STOPS);
      setGradientInterpolation('smooth');
      setSelectedStopIdx(0);
    } else if (activeTab === 'levels') {
      setLevels({
        blackPoint: 0,
        gamma: 1.0,
        whitePoint: 255,
        outputMin: 0,
        outputMax: 255,
        channel: 'rgb',
      });
    } else if (activeTab === 'color-balance') {
      setColorBalance({
        shadows: [0, 0, 0],
        midtones: [0, 0, 0],
        highlights: [0, 0, 0],
        preserveLuminosity: true,
      });
    }
  };

  const handleApply = () => {
    onApply(activeTab, currentAdjustmentData);
    onClose();
  };

  useEffect(() => {
    if (!isOpen) return;
    const handleKeyDown = (e: KeyboardEvent) => {
      if (isImeComposing(e)) return;
      if (e.key === 'Escape') {
        e.preventDefault();
        e.stopPropagation();
        onClose();
      }
    };
    window.addEventListener('keydown', handleKeyDown, true);
    return () => window.removeEventListener('keydown', handleKeyDown, true);
  }, [isOpen, onClose]);

  if (!isOpen) return null;

  const channelColor =
    curveChannel === 'r' ? '#ef4444' : curveChannel === 'g' ? '#22c55e' : curveChannel === 'b' ? '#3b82f6' : 'var(--ui-text-primary)';

  return (
    <div
      role="dialog"
      aria-modal="true"
      aria-labelledby="adjustment-modal-title"
      className="fixed inset-0 z-50 flex items-center justify-center bg-ui-overlay p-4 select-none"
      onClick={onClose}
    >
      <div
        onClick={(e) => e.stopPropagation()}
        className="bg-ui-panel border border-ui-default rounded-ui-lg shadow-ui-popover w-[480px] max-w-[95vw] overflow-hidden flex flex-col text-ui-primary text-xs"
      >
        {/* Header */}
        <div className="h-10 flex items-center justify-between px-4 border-b border-ui-subtle bg-ui-header">
          <span id="adjustment-modal-title" className="font-medium text-ui-primary text-xs">
            {editingLayerId ? t('Edit Adjustment Layer') : t('New Adjustment Layer')}
          </span>
          <button
            type="button"
            onClick={onClose}
            aria-label={tc('action', 'Close')}
            title={t('Cancel (Esc)')}
            className="w-6 h-6 flex items-center justify-center text-ui-muted hover:text-ui-primary rounded-ui-sm hover:bg-ui-control-hover transition-colors cursor-pointer"
          >
            <X className="w-3.5 h-3.5" />
          </button>
        </div>

        {/* Tab Selector */}
        <div className="flex border-b border-ui-subtle bg-ui-header p-1.5 gap-1">
          <button
            type="button"
            onClick={() => setActiveTab('curves')}
            className={`flex-1 py-1.5 px-2 rounded-ui-sm font-medium transition-colors cursor-pointer border ${
              activeTab === 'curves'
                ? 'bg-ui-control text-ui-primary border-ui-default'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
            }`}
          >
            {t('Tone Curves')}
          </button>
          <button
            type="button"
            onClick={() => setActiveTab('gradient-map')}
            className={`flex-1 py-1.5 px-2 rounded-ui-sm font-medium transition-colors cursor-pointer border ${
              activeTab === 'gradient-map'
                ? 'bg-ui-control text-ui-primary border-ui-default'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
            }`}
          >
            {t('Gradient Map')}
          </button>
          <button
            type="button"
            onClick={() => setActiveTab('levels')}
            className={`flex-1 py-1.5 px-2 rounded-ui-sm font-medium transition-colors cursor-pointer border ${
              activeTab === 'levels'
                ? 'bg-ui-control text-ui-primary border-ui-default'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
            }`}
          >
            {t('Levels')}
          </button>
          <button
            type="button"
            onClick={() => setActiveTab('color-balance')}
            className={`flex-1 py-1.5 px-2 rounded-ui-sm font-medium transition-colors cursor-pointer border ${
              activeTab === 'color-balance'
                ? 'bg-ui-control text-ui-primary border-ui-default'
                : 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent'
            }`}
          >
            {t('Color Balance')}
          </button>
        </div>

        {/* Body Content */}
        <div className="p-4 space-y-4 max-h-[75vh] overflow-y-auto">
          {/* TAB 1: TONE CURVES */}
          {activeTab === 'curves' && (
            <div className="space-y-3">
              {/* Channel Selector */}
              <div className="flex items-center justify-between">
                <span className="text-xs text-ui-muted">{t('Channel:')}</span>
                <div className="flex bg-ui-input rounded-ui-sm p-0.5 border border-ui-subtle gap-0.5">
                  {(['rgb', 'r', 'g', 'b'] as const).map((ch) => (
                    <button
                      key={ch}
                      type="button"
                      onClick={() => {
                        setCurveChannel(ch);
                        setSelectedPointIdx(null);
                      }}
                      className={`px-3 py-1 rounded-ui-xs text-[11px] font-semibold transition-colors cursor-pointer ${
                        curveChannel === ch
                          ? ch === 'r'
                            ? 'bg-red-500/20 text-red-500 ring-1 ring-red-500/50'
                            : ch === 'g'
                            ? 'bg-green-500/20 text-green-500 ring-1 ring-green-500/50'
                            : ch === 'b'
                            ? 'bg-blue-500/20 text-blue-500 ring-1 ring-blue-500/50'
                            : 'bg-ui-control-active text-ui-primary'
                          : 'text-ui-muted hover:text-ui-primary'
                      }`}
                    >
                      {ch.toUpperCase()}
                    </button>
                  ))}
                </div>
              </div>

              {/* Interactive Curve SVG Grid */}
              <div className="relative w-full aspect-square bg-ui-input rounded-ui-sm border border-ui-default overflow-hidden flex items-center justify-center">
                {/* Histogram Overlay */}
                {histogramData && (
                  <svg
                    viewBox="0 0 256 256"
                    className="absolute inset-0 w-full h-full pointer-events-none opacity-25"
                    preserveAspectRatio="none"
                  >
                    {Array.from({ length: 256 }).map((_, i) => {
                      const hVal = (histogramData[i] / maxHistogramCount) * 256;
                      return (
                        <rect
                          key={i}
                          x={i}
                          y={256 - hVal}
                          width={1}
                          height={hVal}
                          fill={channelColor}
                        />
                      );
                    })}
                  </svg>
                )}

                {/* Grid Lines & Labels */}
                <svg
                  ref={curveSvgRef}
                  viewBox="0 0 256 256"
                  className="w-full h-full cursor-crosshair"
                  onPointerDown={handleCurvePointerDown}
                  onPointerMove={handleCurvePointerMove}
                  onPointerUp={handleCurvePointerUp}
                >
                  {/* Grid Lines at 25%, 50%, 75% */}
                  {[64, 128, 192].map((coord) => (
                    <React.Fragment key={coord}>
                      <line x1={coord} y1={0} x2={coord} y2={256} stroke="var(--ui-border-subtle)" strokeWidth={1} />
                      <line x1={0} y1={coord} x2={256} y2={coord} stroke="var(--ui-border-subtle)" strokeWidth={1} />
                    </React.Fragment>
                  ))}

                  {/* Diagonal Reference Baseline */}
                  <line x1={0} y1={256} x2={256} y2={0} stroke="var(--ui-border-strong)" strokeDasharray="3 3" strokeWidth={1} />

                  {/* Curve Path */}
                  <path d={curveSvgPath} fill="none" stroke={channelColor} strokeWidth={2.5} strokeLinecap="round" />

                  {/* Control Points */}
                  {currentPts.map(([px, py], idx) => {
                    const isSelected = selectedPointIdx === idx;
                    return (
                      <g key={idx}>
                        <circle
                          cx={px}
                          cy={256 - py}
                          r={isSelected ? 6 : 4.5}
                          fill={isSelected ? 'var(--ui-accent)' : 'var(--ui-text-primary)'}
                          stroke="var(--ui-bg-input)"
                          strokeWidth={2}
                          className="cursor-pointer transition-transform hover:scale-125"
                        />
                      </g>
                    );
                  })}
                </svg>
              </div>

              {/* Point Inspector & Actions */}
              <div className="flex items-center justify-between text-xs pt-1">
                <div className="flex items-center space-x-2">
                  <span className="text-ui-muted font-ui-mono tabular-nums">
                    {selectedPointIdx !== null
                      ? t('In: {inVal}  Out: {outVal}', {
                          inVal: currentPts[selectedPointIdx][0],
                          outVal: currentPts[selectedPointIdx][1],
                        })
                      : t('Click curve to add points')}
                  </span>
                </div>
                {selectedPointIdx !== null && selectedPointIdx > 0 && selectedPointIdx < currentPts.length - 1 && (
                  <button
                    type="button"
                    onClick={deleteSelectedCurvePoint}
                    className="flex items-center space-x-1 px-2 py-0.5 bg-ui-danger/15 text-ui-danger hover:bg-ui-danger/25 rounded-ui-xs border border-ui-danger/40 cursor-pointer transition-colors"
                  >
                    <Trash2 className="w-3 h-3" />
                    <span>{t('Delete Point')}</span>
                  </button>
                )}
              </div>
            </div>
          )}

          {/* TAB 2: GRADIENT MAP */}
          {activeTab === 'gradient-map' && (
            <div className="space-y-4">
              {/* Gradient Preview Bar */}
              <div className="space-y-1.5">
                <div className="flex items-center justify-between text-xs">
                  <span className="text-ui-muted">{t('Color Ramp (Click to add stop):')}</span>
                  <div className="flex items-center space-x-1 text-[11px]">
                    <span className="text-ui-muted">{t('Blend:')}</span>
                    <button
                      type="button"
                      onClick={() => setGradientInterpolation('linear')}
                      className={`px-1.5 py-0.5 rounded-ui-xs cursor-pointer transition-colors ${
                        gradientInterpolation === 'linear'
                          ? 'bg-ui-control-active text-ui-primary font-medium'
                          : 'text-ui-muted hover:text-ui-primary'
                      }`}
                    >
                      {t('Linear')}
                    </button>
                    <button
                      type="button"
                      onClick={() => setGradientInterpolation('smooth')}
                      className={`px-1.5 py-0.5 rounded-ui-xs cursor-pointer transition-colors ${
                        gradientInterpolation === 'smooth'
                          ? 'bg-ui-control-active text-ui-primary font-medium'
                          : 'text-ui-muted hover:text-ui-primary'
                      }`}
                    >
                      {t('Smooth')}
                    </button>
                  </div>
                </div>

                <div
                  ref={gradientRampRef}
                  onClick={handleRampPointerDown}
                  className="relative h-9 rounded-ui-sm border border-ui-strong cursor-pointer overflow-visible"
                  style={{
                    background: `linear-gradient(to right, ${gradientStops
                      .map((s) => `${s.color} ${s.position * 100}%`)
                      .join(', ')})`,
                  }}
                >
                  {/* Draggable Stop Pins */}
                  {gradientStops.map((stop, idx) => {
                    const isSelected = selectedStopIdx === idx;
                    return (
                      <div
                        key={idx}
                        onPointerDown={(e) => handleStopPointerDown(idx, e)}
                        onPointerMove={handleStopPointerMove}
                        onPointerUp={handleStopPointerUp}
                        style={{ left: `${stop.position * 100}%` }}
                        className="absolute -bottom-3.5 -translate-x-1/2 w-4 h-5 flex flex-col items-center cursor-ew-resize z-20 group"
                        title={t('Stop {index}: {color} ({percent}%)', {
                          index: idx + 1,
                          color: stop.color,
                          percent: Math.round(stop.position * 100),
                        })}
                      >
                        <div
                          className={`w-0 h-0 border-l-[4px] border-l-transparent border-r-[4px] border-r-transparent border-b-[5px] ${
                            isSelected ? 'border-b-ui-accent' : 'border-b-ui-primary'
                          }`}
                        />
                        <div
                          className={`w-3.5 h-3.5 rounded-[2px] border ${
                            isSelected ? 'border-ui-accent ring-2 ring-ui-accent-border' : 'border-ui-strong'
                          } shadow-md`}
                          style={{ backgroundColor: stop.color }}
                        />
                      </div>
                    );
                  })}
                </div>
              </div>

              {/* Selected Stop Controls */}
              <div className="flex items-center justify-between p-2.5 bg-ui-header rounded-ui-md border border-ui-subtle mt-4">
                <div className="flex items-center space-x-3">
                  <div className="flex items-center space-x-1.5">
                    <span className="text-ui-muted">{t('Color:')}</span>
                    <input
                      type="color"
                      value={gradientStops[selectedStopIdx]?.color || '#ffffff'}
                      onChange={(e) => {
                        const val = e.target.value;
                        setGradientStops((prev) => {
                          const stops = [...prev];
                          stops[selectedStopIdx] = { ...stops[selectedStopIdx], color: val };
                          return stops;
                        });
                      }}
                      className="w-7 h-7 rounded-ui-xs border border-ui-default bg-transparent cursor-pointer p-0"
                    />
                    <input
                      type="text"
                      value={gradientStops[selectedStopIdx]?.color || ''}
                      onChange={(e) => {
                        const val = e.target.value;
                        setGradientStops((prev) => {
                          const stops = [...prev];
                          stops[selectedStopIdx] = { ...stops[selectedStopIdx], color: val };
                          return stops;
                        });
                      }}
                      className="w-20 bg-ui-input border border-ui-default rounded-ui-sm px-1.5 py-0.5 font-ui-mono text-xs text-ui-primary"
                    />
                  </div>
                  <div className="flex items-center space-x-1">
                    <span className="text-ui-muted">{t('Pos:')}</span>
                    <span className="text-ui-primary font-ui-mono tabular-nums">
                      {Math.round((gradientStops[selectedStopIdx]?.position || 0) * 100)}%
                    </span>
                  </div>
                </div>

                {gradientStops.length > 2 && (
                  <button
                    type="button"
                    onClick={deleteSelectedStop}
                    className="p-1 text-ui-muted hover:text-ui-danger hover:bg-ui-danger/15 rounded-ui-xs cursor-pointer transition-colors"
                    title={t('Delete Stop')}
                  >
                    <Trash2 className="w-3.5 h-3.5" />
                  </button>
                )}
              </div>

              {/* Preset Ramps */}
              <div className="space-y-1.5">
                <span className="text-ui-muted text-[11px]">{t('Presets:')}</span>
                <div className="grid grid-cols-3 gap-1.5">
                  {GRADIENT_PRESETS.map((p) => (
                    <button
                      key={p.name}
                      type="button"
                      onClick={() => {
                        setGradientStops(JSON.parse(JSON.stringify(p.stops)));
                        setGradientInterpolation(p.interpolation);
                        setSelectedStopIdx(0);
                      }}
                      className="flex items-center space-x-2 p-1.5 rounded-ui-sm bg-ui-header hover:bg-ui-control-hover border border-ui-subtle transition-colors text-left cursor-pointer"
                    >
                      <div
                        className="w-5 h-5 rounded-[2px] shrink-0 border border-ui-default"
                        style={{
                          background: `linear-gradient(to right, ${p.stops
                            .map((s) => `${s.color} ${s.position * 100}%`)
                            .join(', ')})`,
                        }}
                      />
                      <span className="text-[11px] text-ui-primary truncate">{td(p.name)}</span>
                    </button>
                  ))}
                </div>
              </div>
            </div>
          )}

          {/* TAB 3: LEVELS */}
          {activeTab === 'levels' && (
            <div className="space-y-3.5">
              <div className="flex items-center justify-between">
                <span className="text-xs text-ui-muted">{t('Channel:')}</span>
                <div className="flex bg-ui-input rounded-ui-sm p-0.5 border border-ui-subtle gap-0.5">
                  {(['rgb', 'r', 'g', 'b'] as const).map((ch) => (
                    <button
                      key={ch}
                      type="button"
                      onClick={() => setLevels((prev) => ({ ...prev, channel: ch }))}
                      className={`px-3 py-1 rounded-ui-xs text-[11px] font-semibold transition-colors cursor-pointer ${
                        levels.channel === ch
                          ? 'bg-ui-control-active text-ui-primary'
                          : 'text-ui-muted hover:text-ui-primary'
                      }`}
                    >
                      {ch.toUpperCase()}
                    </button>
                  ))}
                </div>
              </div>

              {/* Input Black Point Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-ui-muted">{t('Shadows (Black Point):')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{levels.blackPoint}</span>
                </div>
                <input
                  type="range"
                  min={0}
                  max={Math.min(253, levels.whitePoint - 1)}
                  value={levels.blackPoint}
                  onChange={(e) => setLevels((prev) => ({ ...prev, blackPoint: Number(e.target.value) }))}
                  className="w-full"
                />
              </div>

              {/* Gamma Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-ui-muted">{t('Midtones (Gamma):')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{levels.gamma.toFixed(2)}</span>
                </div>
                <input
                  type="range"
                  min={0.1}
                  max={5.0}
                  step={0.05}
                  value={levels.gamma}
                  onChange={(e) => setLevels((prev) => ({ ...prev, gamma: Number(e.target.value) }))}
                  className="w-full"
                />
              </div>

              {/* Input White Point Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-ui-muted">{t('Highlights (White Point):')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{levels.whitePoint}</span>
                </div>
                <input
                  type="range"
                  min={Math.max(2, levels.blackPoint + 1)}
                  max={255}
                  value={levels.whitePoint}
                  onChange={(e) => setLevels((prev) => ({ ...prev, whitePoint: Number(e.target.value) }))}
                  className="w-full"
                />
              </div>

              {/* Output Min / Max */}
              <div className="grid grid-cols-2 gap-3 pt-2 border-t border-ui-subtle">
                <div className="space-y-1">
                  <div className="flex justify-between text-xs">
                    <span className="text-ui-muted">{t('Out Min:')}</span>
                    <span className="font-ui-mono tabular-nums text-ui-primary">{levels.outputMin}</span>
                  </div>
                  <input
                    type="range"
                    min={0}
                    max={255}
                    value={levels.outputMin}
                    onChange={(e) => setLevels((prev) => ({ ...prev, outputMin: Number(e.target.value) }))}
                    className="w-full"
                  />
                </div>
                <div className="space-y-1">
                  <div className="flex justify-between text-xs">
                    <span className="text-ui-muted">{t('Out Max:')}</span>
                    <span className="font-ui-mono tabular-nums text-ui-primary">{levels.outputMax}</span>
                  </div>
                  <input
                    type="range"
                    min={0}
                    max={255}
                    value={levels.outputMax}
                    onChange={(e) => setLevels((prev) => ({ ...prev, outputMax: Number(e.target.value) }))}
                    className="w-full"
                  />
                </div>
              </div>
            </div>
          )}

          {/* TAB 4: COLOR BALANCE */}
          {activeTab === 'color-balance' && (
            <div className="space-y-4">
              {/* Range Selector */}
              <div className="flex border border-ui-subtle rounded-ui-sm p-0.5 bg-ui-input gap-0.5">
                {(['shadows', 'midtones', 'highlights'] as const).map((r) => (
                  <button
                    key={r}
                    type="button"
                    onClick={() => setCbRange(r)}
                    className={`flex-1 py-1 rounded-ui-xs text-[11px] font-medium capitalize transition-colors cursor-pointer ${
                      cbRange === r
                        ? 'bg-ui-control-active text-ui-primary'
                        : 'text-ui-muted hover:text-ui-primary'
                    }`}
                  >
                    {tc('tonalRange', r)}
                  </button>
                ))}
              </div>

              {/* Cyan - Red Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-cyan-500">{t('Cyan')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{colorBalance[cbRange][0]}</span>
                  <span className="text-red-500">{t('Red')}</span>
                </div>
                <input
                  type="range"
                  min={-100}
                  max={100}
                  value={colorBalance[cbRange][0]}
                  onChange={(e) => {
                    const val = Number(e.target.value);
                    setColorBalance((prev) => {
                      const arr: [number, number, number] = [...prev[cbRange]];
                      arr[0] = val;
                      return { ...prev, [cbRange]: arr };
                    });
                  }}
                  className="w-full accent-red-500"
                />
              </div>

              {/* Magenta - Green Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-fuchsia-500">{t('Magenta')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{colorBalance[cbRange][1]}</span>
                  <span className="text-green-500">{t('Green')}</span>
                </div>
                <input
                  type="range"
                  min={-100}
                  max={100}
                  value={colorBalance[cbRange][1]}
                  onChange={(e) => {
                    const val = Number(e.target.value);
                    setColorBalance((prev) => {
                      const arr: [number, number, number] = [...prev[cbRange]];
                      arr[1] = val;
                      return { ...prev, [cbRange]: arr };
                    });
                  }}
                  className="w-full accent-green-500"
                />
              </div>

              {/* Yellow - Blue Slider */}
              <div className="space-y-1">
                <div className="flex justify-between text-xs">
                  <span className="text-amber-500">{t('Yellow')}</span>
                  <span className="font-ui-mono tabular-nums text-ui-primary">{colorBalance[cbRange][2]}</span>
                  <span className="text-blue-500">{t('Blue')}</span>
                </div>
                <input
                  type="range"
                  min={-100}
                  max={100}
                  value={colorBalance[cbRange][2]}
                  onChange={(e) => {
                    const val = Number(e.target.value);
                    setColorBalance((prev) => {
                      const arr: [number, number, number] = [...prev[cbRange]];
                      arr[2] = val;
                      return { ...prev, [cbRange]: arr };
                    });
                  }}
                  className="w-full accent-blue-500"
                />
              </div>

              {/* Preserve Luminosity Toggle */}
              <label className="flex items-center space-x-2 text-xs text-ui-secondary cursor-pointer pt-1">
                <input
                  type="checkbox"
                  checked={colorBalance.preserveLuminosity}
                  onChange={(e) => setColorBalance((prev) => ({ ...prev, preserveLuminosity: e.target.checked }))}
                />
                <span>{t('Preserve Luminosity')}</span>
              </label>
            </div>
          )}
        </div>

        {/* Footer */}
        <div className="flex items-center justify-between px-4 py-2.5 border-t border-ui-subtle bg-ui-header">
          <div className="flex items-center space-x-3">
            <button
              type="button"
              onClick={handleResetCurrent}
              className="px-2.5 py-1 text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover rounded-ui-sm transition-colors cursor-pointer"
            >
              {t('Reset')}
            </button>
            <label className="flex items-center space-x-1.5 text-xs text-ui-secondary cursor-pointer">
              <input
                type="checkbox"
                checked={previewEnabled}
                onChange={(e) => setPreviewEnabled(e.target.checked)}
              />
              <span>{t('Preview')}</span>
            </label>
          </div>

          <div className="flex items-center gap-2">
            <button
              type="button"
              onClick={onClose}
              className="px-3.5 py-1.5 text-xs font-medium text-ui-primary bg-ui-control hover:bg-ui-control-hover border border-ui-default rounded-ui-sm transition-colors cursor-pointer"
            >
              {t('Cancel')}
            </button>
            <button
              type="button"
              onClick={handleApply}
              className="px-4 py-1.5 text-xs bg-ui-accent text-ui-accent-contrast hover:opacity-90 font-medium rounded-ui-sm transition-opacity cursor-pointer"
            >
              {editingLayerId ? t('Update') : t('Create Layer')}
            </button>
          </div>
        </div>
      </div>
    </div>
  );
};