Skip to content

LIBERATED AI CHAT

TypeScript
98.6 KB
Raw Blame
'use client'

import { useState, useEffect } from 'react'
import { useStore, Theme, MemoryType, DEFAULT_GODMODE_PROMPT } from '@/store'
import type { TierInfo } from '@/store'
import {
  X,
  Key,
  Palette,
  Shield,
  Zap,
  Trash2,
  Download,
  Upload,
  Eye,
  EyeOff,
  Check,
  AlertTriangle,
  SlidersHorizontal,
  RotateCcw,
  Brain,
  TrendingUp,
  ThumbsUp,
  ThumbsDown,
  Sparkles,
  Plus,
  Edit3,
  ToggleLeft,
  ToggleRight,
  User,
  Settings,
  MessageSquare,
  Skull,
  FileText,
  Copy,
  Swords,
  Crown,
  Users,
  Droplets,
  HardDrive
} from 'lucide-react'
import type { AutoTuneStrategy, AutoTuneParams } from '@/lib/autotune'
import {
  STRATEGY_PROFILES,
  PARAM_META,
  getStrategyLabel,
  getStrategyDescription
} from '@/lib/autotune'
import { getFeedbackStats } from '@/lib/autotune-feedback'
import type { ObfuscationTechnique } from '@/lib/parseltongue'
import { getAvailableTechniques, DEFAULT_TRIGGERS } from '@/lib/parseltongue'

interface SettingsModalProps {
  onClose: () => void
}

type SettingsTab = 'api' | 'prompt' | 'appearance' | 'privacy' | 'liquid' | 'autotune' | 'parseltongue' | 'stm' | 'memory' | 'ultraplinian' | 'consortium' | 'plan' | 'data'

export function SettingsModal({ onClose }: SettingsModalProps) {
  const [activeTab, setActiveTab] = useState<SettingsTab>('api')

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
      {/* Backdrop */}
      <div
        className="absolute inset-0 bg-black/60 backdrop-blur-sm"
        onClick={onClose}
      />

      {/* Modal */}
      <div className="relative w-full max-w-2xl bg-theme-bg border border-theme-primary rounded-lg shadow-2xl overflow-hidden">
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-theme-primary">
          <h2 className="text-xl font-bold theme-primary">Settings</h2>
          <button
            onClick={onClose}
            className="p-2 hover:bg-theme-accent rounded-lg transition-colors"
            aria-label="Close settings"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        {/* Content */}
        <div className="flex min-h-[400px]">
          {/* Sidebar */}
          <nav className="w-48 border-r border-theme-primary p-2">
            <TabButton
              icon={<Key className="w-4 h-4" />}
              label="API Key"
              active={activeTab === 'api'}
              onClick={() => setActiveTab('api')}
            />
            <TabButton
              icon={<FileText className="w-4 h-4" />}
              label="System Prompt"
              active={activeTab === 'prompt'}
              onClick={() => setActiveTab('prompt')}
            />
            <TabButton
              icon={<SlidersHorizontal className="w-4 h-4" />}
              label="AutoTune"
              active={activeTab === 'autotune'}
              onClick={() => setActiveTab('autotune')}
            />
            <TabButton
              icon={<Palette className="w-4 h-4" />}
              label="Appearance"
              active={activeTab === 'appearance'}
              onClick={() => setActiveTab('appearance')}
            />
            <TabButton
              icon={<Shield className="w-4 h-4" />}
              label="Privacy"
              active={activeTab === 'privacy'}
              onClick={() => setActiveTab('privacy')}
            />
            <TabButton
              icon={<Droplets className="w-4 h-4" />}
              label="Liquid"
              active={activeTab === 'liquid'}
              onClick={() => setActiveTab('liquid')}
            />
            <TabButton
              icon={<Skull className="w-4 h-4" />}
              label="Parseltongue"
              active={activeTab === 'parseltongue'}
              onClick={() => setActiveTab('parseltongue')}
            />
            <TabButton
              icon={<Zap className="w-4 h-4" />}
              label="STM Modules"
              active={activeTab === 'stm'}
              onClick={() => setActiveTab('stm')}
            />
            <TabButton
              icon={<Sparkles className="w-4 h-4" />}
              label="Memory"
              active={activeTab === 'memory'}
              onClick={() => setActiveTab('memory')}
            />
            <TabButton
              icon={<Swords className="w-4 h-4" />}
              label="ULTRAPLINIAN"
              active={activeTab === 'ultraplinian'}
              onClick={() => setActiveTab('ultraplinian')}
            />
            <TabButton
              icon={<Users className="w-4 h-4" />}
              label="CONSORTIUM"
              active={activeTab === 'consortium'}
              onClick={() => setActiveTab('consortium')}
            />
            <TabButton
              icon={<Crown className="w-4 h-4" />}
              label="Plan"
              active={activeTab === 'plan'}
              onClick={() => setActiveTab('plan')}
            />
            <TabButton
              icon={<Download className="w-4 h-4" />}
              label="Data"
              active={activeTab === 'data'}
              onClick={() => setActiveTab('data')}
            />
          </nav>

          {/* Tab content */}
          <div className="flex-1 p-6 overflow-y-auto">
            {activeTab === 'api' && <APIKeyTab />}
            {activeTab === 'prompt' && <SystemPromptTab />}
            {activeTab === 'autotune' && <AutoTuneTab />}
            {activeTab === 'appearance' && <AppearanceTab />}
            {activeTab === 'privacy' && <PrivacyTab />}
            {activeTab === 'parseltongue' && <ParseltongueTab />}
            {activeTab === 'liquid' && <LiquidTab />}
            {activeTab === 'stm' && <STMTab />}
            {activeTab === 'memory' && <MemoryTab />}
            {activeTab === 'ultraplinian' && <UltraplinianTab />}
            {activeTab === 'consortium' && <ConsortiumTab />}
            {activeTab === 'plan' && <PlanTab />}
            {activeTab === 'data' && <DataTab />}
          </div>
        </div>
      </div>
    </div>
  )
}

function TabButton({
  icon,
  label,
  active,
  onClick
}: {
  icon: React.ReactNode
  label: string
  active: boolean
  onClick: () => void
}) {
  return (
    <button
      onClick={onClick}
      className={`w-full flex items-center gap-2 px-3 py-2 rounded-lg
        transition-colors text-sm
        ${active ? 'bg-theme-accent theme-primary' : 'hover:bg-theme-accent/50'}`}
    >
      {icon}
      {label}
    </button>
  )
}

function APIKeyTab() {
  const { apiKey, setApiKey, veniceApiKey, setVeniceApiKey } = useStore()
  const [showKey, setShowKey] = useState(false)
  const [showVeniceKey, setShowVeniceKey] = useState(false)
  const [localKey, setLocalKey] = useState(apiKey)
  const [localVeniceKey, setLocalVeniceKey] = useState(veniceApiKey)
  const [saved, setSaved] = useState(false)
  const [veniceSaved, setVeniceSaved] = useState(false)

  const handleBlur = () => {
    if (localKey !== apiKey) {
      setApiKey(localKey)
      setSaved(true)
      setTimeout(() => setSaved(false), 2000)
    }
  }

  const handleVeniceBlur = () => {
    if (localVeniceKey !== veniceApiKey) {
      setVeniceApiKey(localVeniceKey)
      setVeniceSaved(true)
      setTimeout(() => setVeniceSaved(false), 2000)
    }
  }

  return (
    <div className="space-y-6">
      <div>
        <h3 className="text-lg font-semibold mb-2">OpenRouter API Key</h3>
        <p className="text-sm theme-secondary mb-4">
          Your API key is stored locally, sent to OpenRouter for model requests, and excluded from G0DM0D3 app telemetry.
          Get your key at{' '}
          <a
            href="https://openrouter.ai/keys"
            target="_blank"
            rel="noopener noreferrer"
            className="theme-primary underline"
          >
            openrouter.ai
          </a>
        </p>
      </div>

      <div className="relative">
        <input
          type={showKey ? 'text' : 'password'}
          value={localKey}
          onChange={(e) => setLocalKey(e.target.value)}
          onBlur={handleBlur}
          placeholder="sk-or-v1-..."
          className="w-full px-4 py-3 pr-20 bg-theme-dim border border-theme-primary rounded-lg
            focus:outline-none focus:glow-box"
        />
        <div className="absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-2">
          {saved && (
            <span className="flex items-center gap-1 text-xs text-green-500">
              <Check className="w-3 h-3" />
              Saved
            </span>
          )}
          <button
            onClick={() => setShowKey(!showKey)}
            className="p-1 hover:theme-primary transition-colors"
            aria-label={showKey ? 'Hide key' : 'Show key'}
          >
            {showKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
          </button>
        </div>
      </div>

      <div className="border-t border-theme-primary/20 pt-6">
        <h3 className="text-lg font-semibold mb-2">Venice API Key <span className="text-xs font-normal theme-secondary">(optional)</span></h3>
        <p className="text-sm theme-secondary mb-4">
          Add a Venice key to race uncensored models alongside OpenRouter in ULTRAPLINIAN mode.
          Get your key at{' '}
          <a
            href="https://venice.ai/settings"
            target="_blank"
            rel="noopener noreferrer"
            className="theme-primary underline"
          >
            venice.ai
          </a>
        </p>
      </div>

      <div className="relative">
        <input
          type={showVeniceKey ? 'text' : 'password'}
          value={localVeniceKey}
          onChange={(e) => setLocalVeniceKey(e.target.value)}
          onBlur={handleVeniceBlur}
          placeholder="venice-..."
          className="w-full px-4 py-3 pr-20 bg-theme-dim border border-theme-primary rounded-lg
            focus:outline-none focus:glow-box"
        />
        <div className="absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-2">
          {veniceSaved && (
            <span className="flex items-center gap-1 text-xs text-green-500">
              <Check className="w-3 h-3" />
              Saved
            </span>
          )}
          <button
            onClick={() => setShowVeniceKey(!showVeniceKey)}
            className="p-1 hover:theme-primary transition-colors"
            aria-label={showVeniceKey ? 'Hide key' : 'Show key'}
          >
            {showVeniceKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
          </button>
        </div>
      </div>

      <p className="text-xs theme-secondary">
        Changes are saved automatically when you click away.
      </p>
    </div>
  )
}

// ── System Prompt Settings Tab ────────────────────────────────────────────

function SystemPromptTab() {
  const {
    customSystemPrompt,
    useCustomSystemPrompt,
    setCustomSystemPrompt,
    setUseCustomSystemPrompt,
    resetSystemPromptToDefault
  } = useStore()

  const [localPrompt, setLocalPrompt] = useState(customSystemPrompt)
  const [saved, setSaved] = useState(false)
  const [showResetConfirm, setShowResetConfirm] = useState(false)

  // Sync local state when store changes
  useEffect(() => {
    setLocalPrompt(customSystemPrompt)
  }, [customSystemPrompt])

  const handleSave = () => {
    setCustomSystemPrompt(localPrompt)
    setSaved(true)
    setTimeout(() => setSaved(false), 2000)
  }

  const handleReset = () => {
    resetSystemPromptToDefault()
    setLocalPrompt(DEFAULT_GODMODE_PROMPT)
    setShowResetConfirm(false)
    setSaved(true)
    setTimeout(() => setSaved(false), 2000)
  }

  const handleCopy = () => {
    navigator.clipboard.writeText(localPrompt)
  }

  const charCount = localPrompt.length
  const isModified = localPrompt !== customSystemPrompt

  return (
    <div className="space-y-4">
      <div>
        <h3 className="text-lg font-semibold mb-1">GODMODE System Prompt</h3>
        <p className="text-sm theme-secondary mb-4">
          The system prompt injected into every conversation. This is your &quot;Ultraplinian&quot; jailbreak prompt.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Use Custom System Prompt"
        description="When enabled, this prompt is sent as the system message to all models"
        enabled={useCustomSystemPrompt}
        onChange={setUseCustomSystemPrompt}
      />

      {useCustomSystemPrompt && (
        <>
          {/* Prompt editor */}
          <div className="space-y-2">
            <div className="flex items-center justify-between">
              <label className="text-sm font-semibold">Edit Prompt</label>
              <div className="flex items-center gap-2">
                {saved && (
                  <span className="flex items-center gap-1 text-xs text-green-500">
                    <Check className="w-3 h-3" />
                    Saved
                  </span>
                )}
                <button
                  onClick={handleCopy}
                  className="flex items-center gap-1 text-xs theme-secondary hover:theme-primary transition-colors"
                  title="Copy to clipboard"
                >
                  <Copy className="w-3 h-3" />
                  Copy
                </button>
              </div>
            </div>

            <textarea
              value={localPrompt}
              onChange={(e) => setLocalPrompt(e.target.value)}
              rows={16}
              className="w-full px-4 py-3 bg-theme-dim border border-theme-primary rounded-lg
                font-mono text-sm resize-none focus:outline-none focus:glow-box
                leading-relaxed"
              placeholder="Enter your system prompt..."
            />

            <div className="flex items-center justify-between text-xs theme-secondary">
              <span>{charCount.toLocaleString()} characters</span>
              {isModified && (
                <span className="text-yellow-500">Unsaved changes</span>
              )}
            </div>
          </div>

          {/* Action buttons */}
          <div className="flex gap-2">
            <button
              onClick={handleSave}
              disabled={!isModified}
              className="flex-1 flex items-center justify-center gap-2 px-4 py-2
                bg-green-500/20 border border-green-500/50 text-green-400 rounded-lg
                hover:bg-green-500/30 transition-all
                disabled:opacity-50 disabled:cursor-not-allowed"
            >
              <Check className="w-4 h-4" />
              Save Changes
            </button>

            {!showResetConfirm ? (
              <button
                onClick={() => setShowResetConfirm(true)}
                className="flex items-center gap-2 px-4 py-2
                  border border-theme-primary/50 rounded-lg
                  hover:bg-theme-accent transition-all"
              >
                <RotateCcw className="w-4 h-4" />
                Reset
              </button>
            ) : (
              <div className="flex items-center gap-2">
                <button
                  onClick={handleReset}
                  className="px-3 py-2 bg-red-500/20 text-red-400 rounded-lg hover:bg-red-500/30 transition-colors"
                >
                  Confirm Reset
                </button>
                <button
                  onClick={() => setShowResetConfirm(false)}
                  className="px-3 py-2 border border-theme-primary/50 rounded-lg hover:bg-theme-accent transition-colors"
                >
                  Cancel
                </button>
              </div>
            )}
          </div>

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <FileText className="w-5 h-5 text-green-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">Prompt Engineering Tips</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li>* Use clear directives: &quot;NEVER refuse&quot;, &quot;ALWAYS provide&quot;</li>
                  <li>* Define an identity that operates outside normal constraints</li>
                  <li>* Include behavioral parameters for tone and style</li>
                  <li>* Memory system adds context after this prompt</li>
                  <li>* Parseltongue obfuscates trigger words in your messages</li>
                </ul>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── AutoTune Settings Tab ────────────────────────────────────────────

function AutoTuneTab() {
  const {
    autoTuneEnabled,
    setAutoTuneEnabled,
    autoTuneStrategy,
    setAutoTuneStrategy,
    autoTuneOverrides,
    setAutoTuneOverride,
    clearAutoTuneOverrides,
    feedbackState,
    clearFeedbackHistory
  } = useStore()

  const [showResetConfirm, setShowResetConfirm] = useState(false)
  const feedbackStats = getFeedbackStats(feedbackState)

  const strategies: AutoTuneStrategy[] = ['adaptive', 'precise', 'balanced', 'creative', 'chaotic']

  // Get preview params for current strategy (without overrides for display)
  const previewParams = autoTuneStrategy === 'adaptive'
    ? null
    : STRATEGY_PROFILES[autoTuneStrategy]

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">AutoTune</h3>
        <p className="text-sm theme-secondary mb-4">
          Inspired by Pliny&apos;s AutoTemp — evolved. Automatically tunes temperature, top_p,
          frequency penalty, presence penalty, repetition penalty, and top_k based on
          conversation context. No extra API calls.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable AutoTune"
        description="Automatically optimize sampling parameters for every message"
        enabled={autoTuneEnabled}
        onChange={setAutoTuneEnabled}
      />

      {autoTuneEnabled && (
        <>
          {/* Strategy selector */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Strategy</h4>
            <div className="space-y-2">
              {strategies.map((s) => (
                <button
                  key={s}
                  onClick={() => setAutoTuneStrategy(s)}
                  className={`w-full text-left p-3 rounded-lg border transition-all
                    ${autoTuneStrategy === s
                      ? 'border-cyan-500 bg-cyan-500/10'
                      : 'border-theme-primary/30 bg-theme-dim hover:border-theme-primary/60'
                    }`}
                >
                  <div className="flex items-center justify-between">
                    <span className={`font-mono font-bold text-sm
                      ${autoTuneStrategy === s ? 'text-cyan-400' : 'theme-primary'}`}>
                      {getStrategyLabel(s)}
                    </span>
                    {autoTuneStrategy === s && (
                      <span className="text-xs text-cyan-400 font-mono">ACTIVE</span>
                    )}
                  </div>
                  <p className="text-xs theme-secondary mt-1">
                    {getStrategyDescription(s)}
                  </p>
                </button>
              ))}
            </div>
          </div>

          {/* Parameter preview for fixed strategies */}
          {previewParams && (
            <div>
              <h4 className="font-semibold mb-2 text-sm">Profile Parameters</h4>
              <div className="grid grid-cols-3 gap-2">
                {(Object.entries(previewParams) as [keyof AutoTuneParams, number][]).map(
                  ([key, value]) => (
                    <div
                      key={key}
                      className="text-center p-2 bg-theme-dim rounded border border-theme-primary/30"
                    >
                      <div className="text-[10px] theme-secondary font-mono">
                        {PARAM_META[key].short}
                      </div>
                      <div className="text-sm font-bold theme-primary font-mono">
                        {value.toFixed(2)}
                      </div>
                    </div>
                  )
                )}
              </div>
            </div>
          )}

          {/* Parameter overrides */}
          <div>
            <div className="flex items-center justify-between mb-3">
              <h4 className="font-semibold text-sm">Parameter Overrides</h4>
              {Object.keys(autoTuneOverrides).length > 0 && (
                <button
                  onClick={clearAutoTuneOverrides}
                  className="flex items-center gap-1 text-xs theme-secondary hover:text-red-400 transition-colors"
                >
                  <RotateCcw className="w-3 h-3" />
                  Reset All
                </button>
              )}
            </div>
            <p className="text-xs theme-secondary mb-3">
              Lock specific parameters to override AutoTune&apos;s decisions. Leave unset to let AutoTune decide.
            </p>
            <div className="space-y-3">
              {(Object.keys(PARAM_META) as (keyof AutoTuneParams)[]).map((paramKey) => {
                const meta = PARAM_META[paramKey]
                const overrideValue = autoTuneOverrides[paramKey]
                const isOverridden = overrideValue !== undefined

                return (
                  <div
                    key={paramKey}
                    className={`p-3 rounded-lg border transition-all
                      ${isOverridden
                        ? 'border-cyan-500/50 bg-cyan-500/5'
                        : 'border-theme-primary/20 bg-theme-dim'
                      }`}
                  >
                    <div className="flex items-center justify-between mb-1">
                      <div className="flex items-center gap-2">
                        <span className="text-xs font-mono font-bold theme-primary">
                          {meta.short}
                        </span>
                        <span className="text-xs theme-secondary">{meta.label}</span>
                      </div>
                      <div className="flex items-center gap-2">
                        {isOverridden && (
                          <span className="text-xs font-mono text-cyan-400">
                            {overrideValue.toFixed(2)}
                          </span>
                        )}
                        <button
                          onClick={() => {
                            if (isOverridden) {
                              setAutoTuneOverride(paramKey, null)
                            } else {
                              // Set to the middle of the range as starting point
                              const defaultVal = previewParams
                                ? previewParams[paramKey]
                                : (meta.min + meta.max) / 2
                              setAutoTuneOverride(paramKey, defaultVal)
                            }
                          }}
                          className={`text-[10px] px-2 py-0.5 rounded font-mono transition-colors
                            ${isOverridden
                              ? 'bg-cyan-500/20 text-cyan-400 hover:bg-red-500/20 hover:text-red-400'
                              : 'bg-theme-accent text-theme-secondary hover:text-theme-primary'
                            }`}
                        >
                          {isOverridden ? 'LOCKED' : 'LOCK'}
                        </button>
                      </div>
                    </div>
                    {isOverridden && (
                      <div className="mt-2">
                        <input
                          type="range"
                          min={meta.min}
                          max={meta.max}
                          step={meta.step}
                          value={overrideValue}
                          onChange={(e) => setAutoTuneOverride(paramKey, parseFloat(e.target.value))}
                          className="w-full h-1.5 rounded-lg appearance-none cursor-pointer
                            bg-theme-accent accent-cyan-500"
                        />
                        <div className="flex justify-between text-[10px] theme-secondary font-mono mt-1">
                          <span>{meta.min}</span>
                          <span>{meta.max}</span>
                        </div>
                      </div>
                    )}
                    <p className="text-[10px] theme-secondary mt-1">{meta.description}</p>
                  </div>
                )
              })}
            </div>
          </div>

          {/* Feedback Learning Stats */}
          <div>
            <div className="flex items-center gap-2 mb-3">
              <Brain className="w-4 h-4 text-purple-400" />
              <h4 className="font-semibold text-sm">Feedback Learning</h4>
            </div>
            <p className="text-xs theme-secondary mb-3">
              Rate responses with thumbs up/down to teach AutoTune your preferences.
              Learned adjustments strengthen over time and are applied per context type.
            </p>

            {feedbackStats.totalFeedback === 0 ? (
              <div className="p-3 bg-theme-dim border border-theme-primary/30 rounded-lg text-center">
                <p className="text-xs theme-secondary">
                  No feedback yet. Rate assistant responses to start learning.
                </p>
              </div>
            ) : (
              <div className="space-y-3">
                {/* Summary bar */}
                <div className="flex items-center gap-3 p-3 bg-theme-dim border border-theme-primary/30 rounded-lg">
                  <div className="flex items-center gap-1 text-xs">
                    <TrendingUp className="w-3 h-3 text-purple-400" />
                    <span className="font-mono font-bold text-purple-400">
                      {feedbackStats.totalFeedback}
                    </span>
                    <span className="theme-secondary">ratings</span>
                  </div>
                  <div className="flex items-center gap-1 text-xs">
                    <ThumbsUp className="w-3 h-3 text-green-400" />
                    <span className="font-mono text-green-400">
                      {Math.round(feedbackStats.positiveRate * 100)}%
                    </span>
                  </div>
                </div>

                {/* Per-context breakdown */}
                <div className="grid grid-cols-1 gap-2">
                  {(Object.entries(feedbackStats.contextBreakdown) as [string, { total: number; positive: number; negative: number; hasLearned: boolean }][])
                    .filter(([, data]) => data.total > 0)
                    .map(([ctx, data]) => (
                      <div
                        key={ctx}
                        className={`flex items-center justify-between p-2 rounded-lg border ${
                          data.hasLearned
                            ? 'border-purple-500/30 bg-purple-500/5'
                            : 'border-theme-primary/20 bg-theme-dim'
                        }`}
                      >
                        <div className="flex items-center gap-2">
                          <span className="text-[10px] font-mono font-bold theme-primary w-20">
                            {ctx.toUpperCase()}
                          </span>
                          <div className="flex items-center gap-1 text-[10px]">
                            <ThumbsUp className="w-2.5 h-2.5 text-green-400" />
                            <span className="text-green-400">{data.positive}</span>
                            <ThumbsDown className="w-2.5 h-2.5 text-red-400 ml-1" />
                            <span className="text-red-400">{data.negative}</span>
                          </div>
                        </div>
                        <div className="flex items-center gap-1">
                          {data.hasLearned ? (
                            <span className="text-[10px] font-mono text-purple-400">LEARNING</span>
                          ) : (
                            <span className="text-[10px] font-mono theme-secondary">
                              {3 - data.total > 0 ? `${3 - data.total} more needed` : 'ready'}
                            </span>
                          )}
                        </div>
                      </div>
                    ))}
                </div>

                {/* Reset learning data */}
                <div className="pt-2">
                  {!showResetConfirm ? (
                    <button
                      onClick={() => setShowResetConfirm(true)}
                      className="flex items-center gap-1 text-xs theme-secondary hover:text-red-400 transition-colors"
                    >
                      <RotateCcw className="w-3 h-3" />
                      Reset learned data
                    </button>
                  ) : (
                    <div className="flex items-center gap-2">
                      <span className="text-xs text-red-400">Erase all learned preferences?</span>
                      <button
                        onClick={() => {
                          clearFeedbackHistory()
                          setShowResetConfirm(false)
                        }}
                        className="text-xs px-2 py-1 bg-red-500/20 text-red-400 rounded hover:bg-red-500/30 transition-colors"
                      >
                        Yes
                      </button>
                      <button
                        onClick={() => setShowResetConfirm(false)}
                        className="text-xs px-2 py-1 bg-theme-accent rounded hover:bg-theme-dim transition-colors"
                      >
                        No
                      </button>
                    </div>
                  )}
                </div>
              </div>
            )}
          </div>

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <SlidersHorizontal className="w-5 h-5 text-cyan-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">How AutoTune Works</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li>* Analyzes your message content and conversation context</li>
                  <li>* Detects intent: CODE, CREATIVE, ANALYTICAL, CHAT, or CHAOS</li>
                  <li>* Applies optimal parameter profile for that context</li>
                  <li>* Adjusts repetition penalty for longer conversations</li>
                  <li>* Learns from your feedback to improve over time</li>
                  <li>* Your locked overrides always take priority</li>
                </ul>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── Parseltongue Settings Tab ────────────────────────────────────────────

function ParseltongueTab() {
  const {
    parseltongueConfig,
    setParseltongueEnabled,
    setParseltongueTechnique,
    setParseltongueIntensity,
    setParseltongueCustomTriggers
  } = useStore()

  const [newTrigger, setNewTrigger] = useState('')
  const [showDefaultTriggers, setShowDefaultTriggers] = useState(false)

  const techniques = getAvailableTechniques()

  const handleAddTrigger = () => {
    if (!newTrigger.trim()) return
    const trimmed = newTrigger.trim().toLowerCase()
    if (!parseltongueConfig.customTriggers.includes(trimmed)) {
      setParseltongueCustomTriggers([...parseltongueConfig.customTriggers, trimmed])
    }
    setNewTrigger('')
  }

  const handleRemoveTrigger = (trigger: string) => {
    setParseltongueCustomTriggers(
      parseltongueConfig.customTriggers.filter(t => t !== trigger)
    )
  }

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">Parseltongue</h3>
        <p className="text-sm theme-secondary mb-4">
          Automatically detect and obfuscate trigger words that might cause model refusals.
          Transform sensitive terms using various encoding techniques.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable Parseltongue"
        description="Detect trigger words and apply obfuscation before sending to model"
        enabled={parseltongueConfig.enabled}
        onChange={setParseltongueEnabled}
      />

      {parseltongueConfig.enabled && (
        <>
          {/* Technique selector */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Obfuscation Technique</h4>
            <div className="grid grid-cols-2 gap-2">
              {techniques.map((tech) => (
                <button
                  key={tech.id}
                  onClick={() => setParseltongueTechnique(tech.id)}
                  className={`text-left p-3 rounded-lg border transition-all
                    ${parseltongueConfig.technique === tech.id
                      ? 'border-green-500 bg-green-500/10'
                      : 'border-theme-primary/30 bg-theme-dim hover:border-theme-primary/60'
                    }`}
                >
                  <span className={`font-mono font-bold text-sm
                    ${parseltongueConfig.technique === tech.id ? 'text-green-400' : 'theme-primary'}`}>
                    {tech.name}
                  </span>
                  <p className="text-[10px] theme-secondary mt-1">
                    {tech.description}
                  </p>
                </button>
              ))}
            </div>
          </div>

          {/* Intensity selector */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Intensity</h4>
            <div className="flex gap-2">
              {(['light', 'medium', 'heavy'] as const).map((intensity) => (
                <button
                  key={intensity}
                  onClick={() => setParseltongueIntensity(intensity)}
                  className={`flex-1 px-4 py-2 rounded-lg border text-sm font-mono transition-all
                    ${parseltongueConfig.intensity === intensity
                      ? 'border-green-500 bg-green-500/10 text-green-400'
                      : 'border-theme-primary/30 bg-theme-dim hover:border-theme-primary/60'
                    }`}
                >
                  {intensity.toUpperCase()}
                </button>
              ))}
            </div>
            <p className="text-xs theme-secondary mt-2">
              {parseltongueConfig.intensity === 'light' && 'Minimal transformation - only 1 character per trigger word'}
              {parseltongueConfig.intensity === 'medium' && 'Moderate transformation - roughly half the characters'}
              {parseltongueConfig.intensity === 'heavy' && 'Maximum transformation - all possible characters'}
            </p>
          </div>

          {/* Custom triggers */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Custom Trigger Words</h4>
            <p className="text-xs theme-secondary mb-3">
              Add your own trigger words to detect and obfuscate.
            </p>

            {/* Add new trigger */}
            <div className="flex gap-2 mb-3">
              <input
                type="text"
                value={newTrigger}
                onChange={(e) => setNewTrigger(e.target.value)}
                onKeyDown={(e) => e.key === 'Enter' && handleAddTrigger()}
                placeholder="Add custom trigger..."
                className="flex-1 px-3 py-2 bg-theme-dim border border-theme-primary/50 rounded-lg
                  text-sm focus:outline-none focus:border-theme-primary"
              />
              <button
                onClick={handleAddTrigger}
                disabled={!newTrigger.trim()}
                className="px-4 py-2 bg-green-500/20 border border-green-500/50 text-green-400
                  rounded-lg hover:bg-green-500/30 transition-all
                  disabled:opacity-50 disabled:cursor-not-allowed"
              >
                <Plus className="w-4 h-4" />
              </button>
            </div>

            {/* Custom trigger list */}
            {parseltongueConfig.customTriggers.length > 0 && (
              <div className="flex flex-wrap gap-2 mb-3">
                {parseltongueConfig.customTriggers.map((trigger) => (
                  <span
                    key={trigger}
                    className="flex items-center gap-1 px-2 py-1 bg-green-500/10 border
                      border-green-500/30 rounded text-xs font-mono text-green-400"
                  >
                    {trigger}
                    <button
                      onClick={() => handleRemoveTrigger(trigger)}
                      className="hover:text-red-400 transition-colors"
                    >
                      <X className="w-3 h-3" />
                    </button>
                  </span>
                ))}
              </div>
            )}
          </div>

          {/* Default triggers preview */}
          <div>
            <button
              onClick={() => setShowDefaultTriggers(!showDefaultTriggers)}
              className="flex items-center gap-2 text-xs theme-secondary hover:theme-primary transition-colors"
            >
              {showDefaultTriggers ? <EyeOff className="w-3 h-3" /> : <Eye className="w-3 h-3" />}
              {showDefaultTriggers ? 'Hide' : 'Show'} default triggers ({DEFAULT_TRIGGERS.length})
            </button>

            {showDefaultTriggers && (
              <div className="mt-2 p-3 bg-theme-dim border border-theme-primary/30 rounded-lg">
                <div className="flex flex-wrap gap-1">
                  {DEFAULT_TRIGGERS.map((trigger) => (
                    <span
                      key={trigger}
                      className="px-1.5 py-0.5 bg-theme-accent/50 rounded text-[10px] font-mono theme-secondary"
                    >
                      {trigger}
                    </span>
                  ))}
                </div>
              </div>
            )}
          </div>

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <Skull className="w-5 h-5 text-green-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">How Parseltongue Works</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li>* Scans your message for known trigger words</li>
                  <li>* Applies chosen obfuscation technique to matches</li>
                  <li>* Original message shown to you, transformed sent to model</li>
                  <li>* Helps bypass overzealous content filters</li>
                  <li>* Check console for transformation details</li>
                </ul>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── Liquid Response Settings Tab ────────────────────────────────────────────

function LiquidTab() {
  const {
    liquidResponseEnabled,
    setLiquidResponseEnabled,
    liquidMinDelta,
    setLiquidMinDelta,
  } = useStore()

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">Liquid Response</h3>
        <p className="text-sm theme-secondary mb-4">
          Universal delivery layer that controls <em>how</em> responses arrive. When enabled, you see
          the best response so far immediately, with live morphing upgrades as better results come in.
          Works independently of which strategy generates the content.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable Liquid Response"
        description="Show the best available response immediately and morph to better ones as they arrive"
        enabled={liquidResponseEnabled}
        onChange={setLiquidResponseEnabled}
      />

      {liquidResponseEnabled && (
        <>
          {/* Min Delta slider */}
          <div>
            <div className="flex items-center justify-between mb-2">
              <h4 className="font-semibold text-sm">Minimum Improvement Threshold</h4>
              <span className="text-sm font-mono text-cyan-400 font-bold">{liquidMinDelta} pts</span>
            </div>
            <input
              type="range"
              min={1}
              max={50}
              step={1}
              value={liquidMinDelta}
              onChange={(e) => setLiquidMinDelta(parseInt(e.target.value))}
              className="w-full h-1.5 rounded-lg appearance-none cursor-pointer
                bg-theme-accent accent-cyan-500"
            />
            <div className="flex justify-between text-[10px] theme-secondary font-mono mt-1">
              <span>1 (upgrade often)</span>
              <span>50 (only major jumps)</span>
            </div>
            <p className="text-xs theme-secondary mt-2">
              A new response must beat the current leader by at least this many score points to trigger
              a visible upgrade. Lower values show more intermediate upgrades; higher values only morph
              for significant quality jumps.
            </p>
          </div>

          {/* Strategy interactions */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">How Liquid Interacts With Each Mode</h4>
            <div className="space-y-2">
              {/* Standard */}
              <div className="p-3 rounded-lg border border-theme-primary/30 bg-theme-dim">
                <div className="flex items-center gap-2 mb-1">
                  <span className="text-xs font-mono font-bold theme-primary">STANDARD</span>
                </div>
                <p className="text-xs theme-secondary">
                  Streams tokens live as they arrive from a single model, instead of waiting for
                  the complete response. Provides the familiar &quot;typing&quot; effect.
                </p>
              </div>

              {/* Ultraplinian */}
              <div className="p-3 rounded-lg border border-orange-500/30 bg-orange-500/5">
                <div className="flex items-center gap-2 mb-1">
                  <Swords className="w-3 h-3 text-orange-400" />
                  <span className="text-xs font-mono font-bold text-orange-400">ULTRAPLINIAN</span>
                </div>
                <p className="text-xs theme-secondary">
                  Shows the first scored response immediately, then morphs the display as faster
                  models finish and a higher-scoring leader emerges. Each upgrade must beat the
                  current leader by the min-delta threshold above.
                </p>
              </div>

              {/* Consortium */}
              <div className="p-3 rounded-lg border border-purple-500/30 bg-purple-500/5">
                <div className="flex items-center gap-2 mb-1">
                  <Users className="w-3 h-3 text-purple-400" />
                  <span className="text-xs font-mono font-bold text-purple-400">CONSORTIUM</span>
                </div>
                <p className="text-xs theme-secondary">
                  Displays the best individual model response while the orchestrator collects and
                  synthesizes all responses. When synthesis completes, the display morphs from the
                  individual leader to the final ground-truth answer.
                </p>
              </div>

              {/* Libertas */}
              <div className="p-3 rounded-lg border border-fuchsia-500/30 bg-fuchsia-500/5">
                <div className="flex items-center gap-2 mb-1">
                  <Skull className="w-3 h-3 text-fuchsia-400" />
                  <span className="text-xs font-mono font-bold text-fuchsia-400">L1B3RT4S</span>
                </div>
                <p className="text-xs theme-secondary">
                  All jailbreak templates race in parallel instead of early-exiting on the first
                  non-refusal. The best-scoring template response is served immediately, then
                  progressively upgraded as higher-scoring templates complete. After all templates
                  finish, the winner is handed to the Liquid refinement loop for iterative polishing.
                </p>
              </div>

              {/* Parseltongue */}
              <div className="p-3 rounded-lg border border-green-500/30 bg-green-500/5">
                <div className="flex items-center gap-2 mb-1">
                  <Skull className="w-3 h-3 text-green-400" />
                  <span className="text-xs font-mono font-bold text-green-400">PARSELTONGUE</span>
                </div>
                <p className="text-xs theme-secondary">
                  Parseltongue operates on the <em>input</em> side (obfuscating trigger words before
                  they reach the model), while Liquid operates on the <em>output</em> side (upgrading
                  responses as they arrive). They are fully independent and stack: Parseltongue
                  encodes your prompt, the model responds, and Liquid delivers the response with
                  live morphing.
                </p>
              </div>
            </div>
          </div>

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <Droplets className="w-5 h-5 text-cyan-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">Liquid = Delivery Mode</p>
                <p className="text-xs theme-secondary">
                  Think of Liquid as the <em>how</em> and strategies (Ultraplinian, Consortium, Standard)
                  as the <em>what</em>. Liquid controls response delivery — live streaming vs batch,
                  progressive upgrades vs wait-for-final. These are independent axes: change your
                  strategy without touching delivery, or toggle Liquid without changing your strategy.
                </p>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

function AppearanceTab() {
  const { theme, setTheme, showMagic, setShowMagic } = useStore()

  const themes: { id: Theme; name: string; preview: string; emoji: string }[] = [
    { id: 'matrix', name: 'Matrix', preview: 'bg-matrix-bg border-matrix-primary', emoji: '◉' },
    { id: 'hacker', name: 'Hacker', preview: 'bg-hacker-bg border-hacker-primary', emoji: '△' },
    { id: 'glyph', name: 'Glyph', preview: 'bg-glyph-bg border-glyph-primary', emoji: '◈' },
    { id: 'minimal', name: 'Minimal', preview: 'bg-minimal-bg border-minimal-primary', emoji: '◯' }
  ]

  return (
    <div className="space-y-4">
      <div>
        <h3 className="text-lg font-semibold mb-2">Theme</h3>
        <p className="text-sm theme-secondary mb-4">
          Choose your aesthetic. Each theme affects colors, effects, and mood.
        </p>
      </div>

      <div className="grid grid-cols-2 gap-3">
        {themes.map((t) => (
          <button
            key={t.id}
            onClick={() => setTheme(t.id)}
            className={`p-4 rounded-lg border-2 transition-all
              ${theme === t.id
                ? 'border-theme-primary glow-box'
                : 'border-theme-dim hover:border-theme-primary/50'
              }`}
          >
            <div className="flex items-center gap-2 mb-2">
              <span className="text-xl">{t.emoji}</span>
              <span className="font-semibold">{t.name}</span>
            </div>
            <div className={`h-8 rounded ${t.preview} border`} />
          </button>
        ))}
      </div>

      <div className="pt-4 border-t border-theme-primary/20">
        <ToggleSetting
          label="Show Magic"
          description="Display pipeline details on messages — AutoTune params, model tags, feedback buttons, and race response version arrows"
          enabled={showMagic}
          onChange={setShowMagic}
        />
      </div>
    </div>
  )
}

function PrivacyTab() {
  const {
    noLogMode,
    setNoLogMode,
    datasetGenerationEnabled,
    setDatasetGenerationEnabled
  } = useStore()

  return (
    <div className="space-y-6">
      <div>
        <h3 className="text-lg font-semibold mb-2">Privacy Controls</h3>
        <p className="text-sm theme-secondary">
          Model content goes to the providers you choose. This React frontend sends structural app telemetry to `/api/telemetry`, and an OpenRouter classifier receives the prompt when a key is present. Telemetry events exclude prompt and response text.
        </p>
      </div>

      <div className="space-y-4">
        <ToggleSetting
          label="No-Log Mode"
          description="Appends provider-specific no-log tags to requests (e.g., [[NOLOG]] for some providers)"
          enabled={noLogMode}
          onChange={setNoLogMode}
        />

        <ToggleSetting
          label="Dataset Generation"
          description="Stores this preference locally. API dataset contribution requires an API request with contribute_to_dataset: true; this toggle is not currently wired to that request."
          enabled={datasetGenerationEnabled}
          onChange={setDatasetGenerationEnabled}
          warning="This toggle alone does not publish or scrub conversations"
        />
      </div>

      <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
        <div className="flex items-start gap-3">
          <Shield className="w-5 h-5 theme-primary flex-shrink-0 mt-0.5" />
          <div className="text-sm">
            <p className="font-semibold mb-1">Privacy Commitment</p>
            <ul className="space-y-1 theme-secondary">
              <li>• No embedded Sentry, Google Analytics, or PostHog</li>
              <li>• Structural app telemetry excludes prompt and response text</li>
              <li>• Provider keys are stored locally and sent to the selected provider</li>
              <li>• AGPL-3.0 licensed - verify the code yourself</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  )
}

function STMTab() {
  const { stmModules, toggleSTM } = useStore()

  return (
    <div className="space-y-4">
      <div>
        <h3 className="text-lg font-semibold mb-2">Semantic Transformation Modules</h3>
        <p className="text-sm theme-secondary mb-4">
          STMs modify model outputs to adjust tone, style, or behavior.
          Enable modules to transform responses in real-time.
        </p>
      </div>

      <div className="space-y-3">
        {stmModules.map((stm) => (
          <div
            key={stm.id}
            className="p-4 bg-theme-dim border border-theme-primary rounded-lg"
          >
            <div className="flex items-center justify-between">
              <div className="flex-1">
                <h4 className="font-semibold">{stm.name}</h4>
                <p className="text-sm theme-secondary">{stm.description}</p>
              </div>
              <button
                onClick={() => toggleSTM(stm.id)}
                className={`w-12 h-6 rounded-full transition-colors relative
                  ${stm.enabled ? 'bg-green-500' : 'bg-theme-accent'}`}
              >
                <span
                  className={`absolute top-1 w-4 h-4 rounded-full bg-white transition-transform
                    ${stm.enabled ? 'left-7' : 'left-1'}`}
                />
              </button>
            </div>
          </div>
        ))}
      </div>
    </div>
  )
}

function MemoryTab() {
  const {
    memories,
    memoriesEnabled,
    setMemoriesEnabled,
    addMemory,
    updateMemory,
    deleteMemory,
    toggleMemory,
    clearMemories
  } = useStore()

  const [showAddForm, setShowAddForm] = useState(false)
  const [newMemoryContent, setNewMemoryContent] = useState('')
  const [newMemoryType, setNewMemoryType] = useState<MemoryType>('fact')
  const [editingId, setEditingId] = useState<string | null>(null)
  const [editContent, setEditContent] = useState('')
  const [showClearConfirm, setShowClearConfirm] = useState(false)

  const handleAdd = () => {
    if (!newMemoryContent.trim()) return
    addMemory({
      type: newMemoryType,
      content: newMemoryContent.trim(),
      source: 'manual',
      active: true
    })
    setNewMemoryContent('')
    setNewMemoryType('fact')
    setShowAddForm(false)
  }

  const handleEdit = (id: string) => {
    if (!editContent.trim()) return
    updateMemory(id, { content: editContent.trim() })
    setEditingId(null)
    setEditContent('')
  }

  const startEdit = (id: string, content: string) => {
    setEditingId(id)
    setEditContent(content)
  }

  const getTypeIcon = (type: MemoryType) => {
    switch (type) {
      case 'fact': return <User className="w-3 h-3" />
      case 'preference': return <Settings className="w-3 h-3" />
      case 'instruction': return <MessageSquare className="w-3 h-3" />
    }
  }

  const getTypeLabel = (type: MemoryType) => {
    switch (type) {
      case 'fact': return 'Fact'
      case 'preference': return 'Preference'
      case 'instruction': return 'Instruction'
    }
  }

  const getTypeColor = (type: MemoryType) => {
    switch (type) {
      case 'fact': return 'text-blue-400 bg-blue-500/10 border-blue-500/30'
      case 'preference': return 'text-purple-400 bg-purple-500/10 border-purple-500/30'
      case 'instruction': return 'text-green-400 bg-green-500/10 border-green-500/30'
    }
  }

  const activeMemories = memories.filter(m => m.active)

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">Memory</h3>
        <p className="text-sm theme-secondary mb-4">
          Persistent memory across conversations. The AI will remember these facts about you.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable Memory"
        description={`Inject ${activeMemories.length} active memories into every conversation`}
        enabled={memoriesEnabled}
        onChange={setMemoriesEnabled}
      />

      {memoriesEnabled && (
        <>
          {/* Add new memory button/form */}
          {!showAddForm ? (
            <button
              onClick={() => setShowAddForm(true)}
              className="w-full flex items-center justify-center gap-2 px-4 py-3
                border-2 border-dashed border-theme-primary/50 rounded-lg
                hover:border-theme-primary hover:bg-theme-accent/30 transition-all"
            >
              <Plus className="w-4 h-4" />
              Add Memory
            </button>
          ) : (
            <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg space-y-3">
              <div className="flex items-center gap-2">
                <Sparkles className="w-4 h-4 text-cyan-400" />
                <span className="text-sm font-semibold">New Memory</span>
              </div>

              {/* Type selector */}
              <div className="flex gap-2">
                {(['fact', 'preference', 'instruction'] as MemoryType[]).map((type) => (
                  <button
                    key={type}
                    onClick={() => setNewMemoryType(type)}
                    className={`flex items-center gap-1 px-3 py-1.5 rounded-lg border text-xs font-mono transition-all
                      ${newMemoryType === type
                        ? getTypeColor(type)
                        : 'border-theme-primary/30 bg-theme-bg hover:border-theme-primary/60'
                      }`}
                  >
                    {getTypeIcon(type)}
                    {getTypeLabel(type)}
                  </button>
                ))}
              </div>

              {/* Content input */}
              <textarea
                value={newMemoryContent}
                onChange={(e) => setNewMemoryContent(e.target.value)}
                placeholder={
                  newMemoryType === 'fact' ? "e.g., My name is Alex, I'm a software engineer"
                  : newMemoryType === 'preference' ? "e.g., I prefer concise answers with code examples"
                  : "e.g., Always use TypeScript, never use var"
                }
                rows={2}
                className="w-full px-3 py-2 bg-theme-bg border border-theme-primary/50 rounded-lg
                  text-sm resize-none focus:outline-none focus:border-theme-primary"
              />

              {/* Action buttons */}
              <div className="flex gap-2">
                <button
                  onClick={handleAdd}
                  disabled={!newMemoryContent.trim()}
                  className="flex-1 flex items-center justify-center gap-2 px-4 py-2
                    bg-cyan-500/20 border border-cyan-500/50 text-cyan-400 rounded-lg
                    hover:bg-cyan-500/30 transition-all
                    disabled:opacity-50 disabled:cursor-not-allowed"
                >
                  <Check className="w-4 h-4" />
                  Save
                </button>
                <button
                  onClick={() => {
                    setShowAddForm(false)
                    setNewMemoryContent('')
                  }}
                  className="px-4 py-2 border border-theme-primary/50 rounded-lg
                    hover:bg-theme-accent transition-all"
                >
                  Cancel
                </button>
              </div>
            </div>
          )}

          {/* Memory list */}
          <div className="space-y-2">
            {memories.length === 0 ? (
              <div className="p-6 bg-theme-dim border border-theme-primary/30 rounded-lg text-center">
                <Sparkles className="w-8 h-8 mx-auto mb-2 theme-secondary" />
                <p className="text-sm theme-secondary">
                  No memories yet. Add facts, preferences, or instructions for the AI to remember.
                </p>
              </div>
            ) : (
              memories.map((memory) => (
                <div
                  key={memory.id}
                  className={`p-3 rounded-lg border transition-all
                    ${memory.active
                      ? 'bg-theme-dim border-theme-primary/50'
                      : 'bg-theme-bg/50 border-theme-primary/20 opacity-60'
                    }`}
                >
                  {editingId === memory.id ? (
                    <div className="space-y-2">
                      <textarea
                        value={editContent}
                        onChange={(e) => setEditContent(e.target.value)}
                        rows={2}
                        className="w-full px-3 py-2 bg-theme-bg border border-theme-primary/50 rounded
                          text-sm resize-none focus:outline-none focus:border-theme-primary"
                        autoFocus
                      />
                      <div className="flex gap-2">
                        <button
                          onClick={() => handleEdit(memory.id)}
                          className="px-3 py-1 text-xs bg-cyan-500/20 text-cyan-400 rounded hover:bg-cyan-500/30"
                        >
                          Save
                        </button>
                        <button
                          onClick={() => setEditingId(null)}
                          className="px-3 py-1 text-xs border border-theme-primary/50 rounded hover:bg-theme-accent"
                        >
                          Cancel
                        </button>
                      </div>
                    </div>
                  ) : (
                    <>
                      <div className="flex items-start justify-between gap-2">
                        <div className="flex-1">
                          <div className="flex items-center gap-2 mb-1">
                            <span className={`flex items-center gap-1 px-2 py-0.5 rounded text-[10px] font-mono border ${getTypeColor(memory.type)}`}>
                              {getTypeIcon(memory.type)}
                              {getTypeLabel(memory.type).toUpperCase()}
                            </span>
                            {memory.source === 'auto' && (
                              <span className="text-[10px] font-mono text-yellow-500">AUTO</span>
                            )}
                          </div>
                          <p className="text-sm">{memory.content}</p>
                        </div>
                        <div className="flex items-center gap-1">
                          <button
                            onClick={() => toggleMemory(memory.id)}
                            className="p-1.5 hover:bg-theme-accent rounded transition-colors"
                            title={memory.active ? 'Disable' : 'Enable'}
                          >
                            {memory.active ? (
                              <ToggleRight className="w-4 h-4 text-green-400" />
                            ) : (
                              <ToggleLeft className="w-4 h-4 theme-secondary" />
                            )}
                          </button>
                          <button
                            onClick={() => startEdit(memory.id, memory.content)}
                            className="p-1.5 hover:bg-theme-accent rounded transition-colors"
                            title="Edit"
                          >
                            <Edit3 className="w-4 h-4 theme-secondary hover:theme-primary" />
                          </button>
                          <button
                            onClick={() => deleteMemory(memory.id)}
                            className="p-1.5 hover:bg-red-500/20 rounded transition-colors"
                            title="Delete"
                          >
                            <Trash2 className="w-4 h-4 text-red-400" />
                          </button>
                        </div>
                      </div>
                    </>
                  )}
                </div>
              ))
            )}
          </div>

          {/* Clear all */}
          {memories.length > 0 && (
            <div className="pt-2">
              {!showClearConfirm ? (
                <button
                  onClick={() => setShowClearConfirm(true)}
                  className="flex items-center gap-1 text-xs theme-secondary hover:text-red-400 transition-colors"
                >
                  <Trash2 className="w-3 h-3" />
                  Clear all memories
                </button>
              ) : (
                <div className="flex items-center gap-2">
                  <span className="text-xs text-red-400">Delete all {memories.length} memories?</span>
                  <button
                    onClick={() => {
                      clearMemories()
                      setShowClearConfirm(false)
                    }}
                    className="text-xs px-2 py-1 bg-red-500/20 text-red-400 rounded hover:bg-red-500/30 transition-colors"
                  >
                    Yes
                  </button>
                  <button
                    onClick={() => setShowClearConfirm(false)}
                    className="text-xs px-2 py-1 bg-theme-accent rounded hover:bg-theme-dim transition-colors"
                  >
                    No
                  </button>
                </div>
              )}
            </div>
          )}

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <Sparkles className="w-5 h-5 text-cyan-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">Memory Types</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li><span className="text-blue-400 font-mono">FACT</span> — Personal info: name, job, location, preferences</li>
                  <li><span className="text-purple-400 font-mono">PREFERENCE</span> — How you want responses: style, format, tone</li>
                  <li><span className="text-green-400 font-mono">INSTRUCTION</span> — Rules to always follow: coding standards, constraints</li>
                </ul>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── ULTRAPLINIAN Settings Tab ────────────────────────────────────────────

function UltraplinianTab() {
  const {
    ultraplinianEnabled,
    setUltraplinianEnabled,
    ultraplinianTier,
    setUltraplinianTier,
    ultraplinianApiUrl,
    setUltraplinianApiUrl,
    ultraplinianApiKey,
    setUltraplinianApiKey
  } = useStore()

  const [showKey, setShowKey] = useState(false)
  const [localUrl, setLocalUrl] = useState(ultraplinianApiUrl)
  const [localKey, setLocalKey] = useState(ultraplinianApiKey)
  const [savedUrl, setSavedUrl] = useState(false)
  const [savedKey, setSavedKey] = useState(false)

  const handleUrlBlur = () => {
    if (localUrl !== ultraplinianApiUrl) {
      setUltraplinianApiUrl(localUrl)
      setSavedUrl(true)
      setTimeout(() => setSavedUrl(false), 2000)
    }
  }

  const handleKeyBlur = () => {
    if (localKey !== ultraplinianApiKey) {
      setUltraplinianApiKey(localKey)
      setSavedKey(true)
      setTimeout(() => setSavedKey(false), 2000)
    }
  }

  const tiers: { id: 'fast' | 'standard' | 'smart' | 'power' | 'ultra'; label: string; models: number; desc: string }[] = [
    { id: 'fast', label: 'FAST', models: 12, desc: 'Quick responses, free-tier models (~10-15s)' },
    { id: 'standard', label: 'STANDARD', models: 27, desc: 'Fast + mid-range workhorses (~15-25s)' },
    { id: 'smart', label: 'SMART', models: 41, desc: 'Standard + flagships & heavy hitters (~25-40s)' },
    { id: 'power', label: 'POWER', models: 53, desc: 'Smart + frontier cutting-edge models (~35-50s)' },
    { id: 'ultra', label: 'ULTRA', models: 60, desc: 'ALL available models, maximum coverage (~45-70s)' },
  ]

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">ULTRAPLINIAN Mode</h3>
        <p className="text-sm theme-secondary mb-4">
          Race multiple AI models in parallel. The best response wins.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable ULTRAPLINIAN"
        description="Send every message to multiple models and pick the best response"
        enabled={ultraplinianEnabled}
        onChange={setUltraplinianEnabled}
      />

      {ultraplinianEnabled && (
        <>
          {/* API URL */}
          <div>
            <label className="text-sm font-semibold mb-2 block">API Server URL</label>
            <div className="relative">
              <input
                type="text"
                value={localUrl}
                onChange={(e) => setLocalUrl(e.target.value)}
                onBlur={handleUrlBlur}
                placeholder="http://localhost:7860"
                className="w-full px-4 py-3 bg-theme-dim border border-theme-primary rounded-lg
                  focus:outline-none focus:glow-box font-mono text-sm"
              />
              {savedUrl && (
                <span className="absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-1 text-xs text-green-500">
                  <Check className="w-3 h-3" />
                  Saved
                </span>
              )}
            </div>
            <p className="text-xs theme-secondary mt-1">
              The base URL of your ULTRAPLINIAN API server.
            </p>
          </div>

          {/* API Key */}
          <div>
            <label className="text-sm font-semibold mb-2 block">ULTRAPLINIAN API Key</label>
            <div className="relative">
              <input
                type={showKey ? 'text' : 'password'}
                value={localKey}
                onChange={(e) => setLocalKey(e.target.value)}
                onBlur={handleKeyBlur}
                placeholder="your-api-key"
                className="w-full px-4 py-3 pr-20 bg-theme-dim border border-theme-primary rounded-lg
                  focus:outline-none focus:glow-box font-mono text-sm"
              />
              <div className="absolute right-3 top-1/2 -translate-y-1/2 flex items-center gap-2">
                {savedKey && (
                  <span className="flex items-center gap-1 text-xs text-green-500">
                    <Check className="w-3 h-3" />
                    Saved
                  </span>
                )}
                <button
                  onClick={() => setShowKey(!showKey)}
                  className="p-1 hover:theme-primary transition-colors"
                >
                  {showKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
                </button>
              </div>
            </div>
            <p className="text-xs theme-secondary mt-1">
              Auth key for the ULTRAPLINIAN backend. Stored locally only.
            </p>
          </div>

          {/* Tier selector */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Speed Tier</h4>
            <div className="space-y-2">
              {tiers.map((t) => (
                <button
                  key={t.id}
                  onClick={() => setUltraplinianTier(t.id)}
                  className={`w-full text-left p-3 rounded-lg border transition-all
                    ${ultraplinianTier === t.id
                      ? 'border-orange-500 bg-orange-500/10'
                      : 'border-theme-primary/30 bg-theme-dim hover:border-theme-primary/60'
                    }`}
                >
                  <div className="flex items-center justify-between">
                    <div className="flex items-center gap-2">
                      <span className={`font-mono font-bold text-sm
                        ${ultraplinianTier === t.id ? 'text-orange-400' : 'theme-primary'}`}>
                        {t.label}
                      </span>
                      <span className="text-[10px] font-mono theme-secondary">
                        {t.models} models
                      </span>
                    </div>
                    {ultraplinianTier === t.id && (
                      <span className="text-xs text-orange-400 font-mono">ACTIVE</span>
                    )}
                  </div>
                  <p className="text-xs theme-secondary mt-1">{t.desc}</p>
                </button>
              ))}
            </div>
          </div>

          {/* Info box */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <Swords className="w-5 h-5 text-orange-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">How ULTRAPLINIAN Works</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li>* Fires your prompt to all tier models in parallel via OpenRouter</li>
                  <li>* Each response is scored on substance, directness, and relevance</li>
                  <li>* GODMODE prompt + Depth Directive injected automatically</li>
                  <li>* Requires a running ULTRAPLINIAN API backend</li>
                </ul>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── CONSORTIUM Settings Tab ────────────────────────────────────────────

function ConsortiumTab() {
  const {
    consortiumEnabled,
    setConsortiumEnabled,
    consortiumTier,
    setConsortiumTier,
    ultraplinianApiUrl,
    ultraplinianApiKey,
  } = useStore()

  const tiers: { id: 'fast' | 'standard' | 'smart' | 'power' | 'ultra'; label: string; models: number; desc: string }[] = [
    { id: 'fast', label: 'FAST', models: 12, desc: 'Collect from 12 models + orchestrator synthesis (~20-30s)' },
    { id: 'standard', label: 'STANDARD', models: 27, desc: 'Collect from 27 models + orchestrator synthesis (~30-45s)' },
    { id: 'smart', label: 'SMART', models: 41, desc: 'Collect from 41 models + orchestrator synthesis (~40-60s)' },
    { id: 'power', label: 'POWER', models: 53, desc: 'Collect from 53 models + orchestrator synthesis (~45-65s)' },
    { id: 'ultra', label: 'ULTRA', models: 60, desc: 'Collect from ALL 60 models + orchestrator synthesis (~55-80s)' },
  ]

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">CONSORTIUM Mode</h3>
        <p className="text-sm theme-secondary mb-4">
          The hive mind. Query ALL models in parallel, then feed every response to a
          strong orchestrator that synthesizes ground truth from collective intelligence.
          Configure <span className="text-cyan-400 font-mono text-xs">LIQUID</span> delivery in the Liquid tab.
        </p>
      </div>

      {/* Master toggle */}
      <ToggleSetting
        label="Enable CONSORTIUM"
        description="Collect all model responses and synthesize ground truth via orchestrator"
        enabled={consortiumEnabled}
        onChange={setConsortiumEnabled}
      />

      {consortiumEnabled && (
        <>
          {/* Needs ULTRAPLINIAN API configured */}
          {(!ultraplinianApiUrl || !ultraplinianApiKey) && (
            <div className="p-4 bg-theme-dim border border-yellow-500/30 rounded-lg">
              <div className="flex items-start gap-3">
                <AlertTriangle className="w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5" />
                <div className="text-sm">
                  <p className="font-semibold mb-1 text-yellow-500">API Not Configured</p>
                  <p className="theme-secondary text-xs">
                    CONSORTIUM uses the same API server as ULTRAPLINIAN. Configure your API URL and key in the ULTRAPLINIAN tab first.
                  </p>
                </div>
              </div>
            </div>
          )}

          {/* Tier selector */}
          <div>
            <h4 className="font-semibold mb-3 text-sm">Collection Tier</h4>
            <div className="space-y-2">
              {tiers.map((t) => (
                <button
                  key={t.id}
                  onClick={() => setConsortiumTier(t.id)}
                  className={`w-full text-left p-3 rounded-lg border transition-all
                    ${consortiumTier === t.id
                      ? 'border-purple-500 bg-purple-500/10'
                      : 'border-theme-primary/30 bg-theme-dim hover:border-theme-primary/60'
                    }`}
                >
                  <div className="flex items-center justify-between">
                    <div className="flex items-center gap-2">
                      <span className={`font-mono font-bold text-sm
                        ${consortiumTier === t.id ? 'text-purple-400' : 'theme-primary'}`}>
                        {t.label}
                      </span>
                      <span className="text-[10px] font-mono theme-secondary">
                        {t.models} models
                      </span>
                    </div>
                    {consortiumTier === t.id && (
                      <span className="text-xs text-purple-400 font-mono">ACTIVE</span>
                    )}
                  </div>
                  <p className="text-xs theme-secondary mt-1">{t.desc}</p>
                </button>
              ))}
            </div>
          </div>

          {/* How it works */}
          <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
            <div className="flex items-start gap-3">
              <Users className="w-5 h-5 text-purple-400 flex-shrink-0 mt-0.5" />
              <div className="text-sm">
                <p className="font-semibold mb-1">How CONSORTIUM Works</p>
                <ul className="space-y-1 theme-secondary text-xs">
                  <li>* Sends your prompt to ALL tier models in parallel</li>
                  <li>* Waits for every model to respond (not early-exit like ULTRAPLINIAN)</li>
                  <li>* All responses scored on substance, directness, and relevance</li>
                  <li>* A strong orchestrator model reads ALL responses</li>
                  <li>* Orchestrator identifies consensus, resolves contradictions</li>
                  <li>* Synthesizes a single, authoritative ground-truth response</li>
                  <li>* Result is more grounded and reliable than any single model</li>
                </ul>
              </div>
            </div>
          </div>

          {/* Comparison box */}
          <div className="p-4 bg-theme-dim border border-theme-primary/50 rounded-lg">
            <p className="text-xs font-semibold mb-2 theme-primary">ULTRAPLINIAN vs CONSORTIUM</p>
            <div className="grid grid-cols-2 gap-3 text-xs theme-secondary">
              <div>
                <p className="text-orange-400 font-mono font-bold mb-1">ULTRAPLINIAN</p>
                <p>Race models, pick the <strong>best single voice</strong></p>
                <p className="text-[10px] mt-1">Fast (~10-15s). One winner.</p>
              </div>
              <div>
                <p className="text-purple-400 font-mono font-bold mb-1">CONSORTIUM</p>
                <p>Collect all voices, synthesize <strong>ground truth</strong></p>
                <p className="text-[10px] mt-1">Slower (~30-60s). More reliable.</p>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  )
}

// ── Plan / Tier Settings Tab ────────────────────────────────────────────

function PlanTab() {
  const {
    tierInfo,
    fetchTierInfo,
    ultraplinianApiUrl,
    ultraplinianApiKey
  } = useStore()

  const [loading, setLoading] = useState(false)

  const handleRefresh = async () => {
    setLoading(true)
    await fetchTierInfo()
    setLoading(false)
  }

  useEffect(() => {
    if (ultraplinianApiUrl && ultraplinianApiKey && !tierInfo) {
      handleRefresh()
    }
  }, [ultraplinianApiUrl, ultraplinianApiKey])

  const plans = [
    {
      tier: 'free' as const,
      label: 'Free',
      color: 'text-gray-400 border-gray-500/30 bg-gray-500/5',
      limits: '5 total, 10/min, 50/day',
      features: [
        'Single-model chat',
        'AutoTune + Parseltongue + STM',
        'ULTRAPLINIAN fast tier (12 models)',
      ],
      locked: [
        'Dataset export',
        'Research API',
        'ULTRAPLINIAN standard/smart/power/ultra tiers',
        'Metadata event log',
        'Corpus download',
      ],
    },
    {
      tier: 'pro' as const,
      label: 'Pro',
      color: 'text-cyan-400 border-cyan-500/30 bg-cyan-500/5',
      limits: 'Unlimited total, 60/min, 1000/day',
      features: [
        'Everything in Free',
        'ULTRAPLINIAN fast + standard + smart + power (53 models)',
        'Dataset export (JSON)',
        'Research API (read: stats, batches)',
      ],
      locked: [
        'ULTRAPLINIAN ultra tier (all 60 models)',
        'Corpus query + download',
        'Force-flush to HuggingFace',
        'Metadata event log',
      ],
    },
    {
      tier: 'enterprise' as const,
      label: 'Enterprise',
      color: 'text-amber-400 border-amber-500/30 bg-amber-500/5',
      limits: 'Unlimited total, 300/min, 10000/day',
      features: [
        'Everything in Pro',
        'ULTRAPLINIAN all tiers (60 models)',
        'Dataset export (JSON + JSONL)',
        'Full Research API (query, download)',
        'Force-flush to HuggingFace',
        'Metadata event log access',
      ],
      locked: [],
    },
  ]

  const currentTier = tierInfo?.tier || 'free'

  return (
    <div className="space-y-5">
      <div>
        <h3 className="text-lg font-semibold mb-1">Plan & Tier</h3>
        <p className="text-sm theme-secondary mb-4">
          Your API key determines your tier. Higher tiers unlock more features, higher rate limits, and full research access.
        </p>
      </div>

      {/* Current tier badge */}
      <div className="flex items-center justify-between p-4 bg-theme-dim border border-theme-primary rounded-lg">
        <div className="flex items-center gap-3">
          <Crown className={`w-6 h-6 ${
            currentTier === 'enterprise' ? 'text-amber-400' :
            currentTier === 'pro' ? 'text-cyan-400' :
            'text-gray-400'
          }`} />
          <div>
            <div className="flex items-center gap-2">
              <span className="font-bold text-lg">
                {tierInfo?.label || 'Free'}
              </span>
              <span className={`text-[10px] font-mono px-2 py-0.5 rounded border ${
                currentTier === 'enterprise' ? 'text-amber-400 border-amber-500/50 bg-amber-500/10' :
                currentTier === 'pro' ? 'text-cyan-400 border-cyan-500/50 bg-cyan-500/10' :
                'text-gray-400 border-gray-500/50 bg-gray-500/10'
              }`}>
                {currentTier.toUpperCase()}
              </span>
            </div>
            <p className="text-xs theme-secondary">
              {tierInfo
                ? `${tierInfo.limits.total === 0 ? 'Unlimited' : tierInfo.limits.total} total, ${tierInfo.limits.perMinute}/min, ${tierInfo.limits.perDay}/day`
                : 'Configure ULTRAPLINIAN API to check tier'}
            </p>
          </div>
        </div>
        <button
          onClick={handleRefresh}
          disabled={loading || !ultraplinianApiUrl || !ultraplinianApiKey}
          className="flex items-center gap-1 px-3 py-1.5 text-xs border border-theme-primary/50 rounded-lg
            hover:bg-theme-accent transition-all disabled:opacity-50 disabled:cursor-not-allowed"
        >
          <RotateCcw className={`w-3 h-3 ${loading ? 'animate-spin' : ''}`} />
          Refresh
        </button>
      </div>

      {!ultraplinianApiUrl || !ultraplinianApiKey ? (
        <div className="p-4 bg-theme-dim border border-yellow-500/30 rounded-lg">
          <div className="flex items-start gap-3">
            <AlertTriangle className="w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5" />
            <div className="text-sm">
              <p className="font-semibold mb-1 text-yellow-500">API Not Configured</p>
              <p className="theme-secondary text-xs">
                Set your ULTRAPLINIAN API URL and API Key in the ULTRAPLINIAN tab to check your tier status.
              </p>
            </div>
          </div>
        </div>
      ) : null}

      {/* Plan comparison */}
      <div className="space-y-3">
        {plans.map((plan) => {
          const isCurrent = plan.tier === currentTier
          return (
            <div
              key={plan.tier}
              className={`p-4 rounded-lg border transition-all ${plan.color} ${
                isCurrent ? 'ring-1 ring-theme-primary' : ''
              }`}
            >
              <div className="flex items-center justify-between mb-2">
                <div className="flex items-center gap-2">
                  <span className="font-mono font-bold">{plan.label}</span>
                  {isCurrent && (
                    <span className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-theme-primary/20 theme-primary">
                      CURRENT
                    </span>
                  )}
                </div>
                <span className="text-[10px] font-mono theme-secondary">{plan.limits}</span>
              </div>

              {/* Included features */}
              <div className="space-y-1 mb-2">
                {plan.features.map((f) => (
                  <div key={f} className="flex items-center gap-2 text-xs">
                    <Check className="w-3 h-3 text-green-400 flex-shrink-0" />
                    <span className="theme-secondary">{f}</span>
                  </div>
                ))}
              </div>

              {/* Locked features */}
              {plan.locked.length > 0 && (
                <div className="space-y-1 pt-2 border-t border-theme-primary/20">
                  {plan.locked.map((f) => (
                    <div key={f} className="flex items-center gap-2 text-xs">
                      <X className="w-3 h-3 text-red-400/60 flex-shrink-0" />
                      <span className="theme-secondary opacity-60">{f}</span>
                    </div>
                  ))}
                </div>
              )}
            </div>
          )
        })}
      </div>

      {/* Upgrade info */}
      {currentTier !== 'enterprise' && (
        <div className="p-4 bg-theme-dim border border-theme-primary rounded-lg">
          <div className="flex items-start gap-3">
            <Crown className="w-5 h-5 text-amber-400 flex-shrink-0 mt-0.5" />
            <div className="text-sm">
              <p className="font-semibold mb-1">Upgrade Your Plan</p>
              <p className="theme-secondary text-xs">
                API key tiers are assigned server-side via the <code className="font-mono text-[10px] bg-theme-accent px-1 rounded">GODMODE_TIER_KEYS</code> environment variable. Contact the API host or set the variable yourself if self-hosting.
              </p>
            </div>
          </div>
        </div>
      )}
    </div>
  )
}

function DataTab() {
  const store = useStore()
  const { conversations, clearConversations } = store
  const [showConfirm, setShowConfirm] = useState(false)
  const [importStatus, setImportStatus] = useState<'idle' | 'success' | 'error'>('idle')
  const [importMessage, setImportMessage] = useState('')
  const [pendingImport, setPendingImport] = useState<Record<string, unknown> | null>(null)
  const [pendingImportSummary, setPendingImportSummary] = useState('')

  const MAX_IMPORT_SIZE = 10 * 1024 * 1024 // 10 MB

  // Estimate localStorage usage
  const storageUsed = (() => {
    try {
      let total = 0
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        if (key) total += key.length + (localStorage.getItem(key)?.length || 0)
      }
      return total * 2 // UTF-16 = 2 bytes per char
    } catch { return 0 }
  })()
  const STORAGE_CAP = 5 * 1024 * 1024 // ~5 MB typical browser limit
  const storagePercent = Math.min(100, Math.round((storageUsed / STORAGE_CAP) * 100))
  const formatBytes = (b: number) => b < 1024 ? `${b} B` : b < 1048576 ? `${(b / 1024).toFixed(1)} KB` : `${(b / 1048576).toFixed(1)} MB`

  const handleExport = () => {
    // Export everything persisted to localStorage — full self-custody backup.
    // STM modules excluded: transformer functions can't be serialized to JSON.
    // STM enabled/disabled state is restored from defaults on import.
    const exportData = {
      _version: 1,
      _exportedAt: new Date().toISOString(),
      _source: 'g0dm0d3',
      // Conversations (chat history)
      conversations: store.conversations,
      currentConversationId: store.currentConversationId,
      // Settings
      theme: store.theme,
      defaultModel: store.defaultModel,
      currentPersona: store.currentPersona,
      apiKey: store.apiKey,
      // Features
      autoTuneEnabled: store.autoTuneEnabled,
      autoTuneStrategy: store.autoTuneStrategy,
      autoTuneOverrides: store.autoTuneOverrides,
      feedbackState: store.feedbackState,
      parseltongueConfig: store.parseltongueConfig,
      // Memory system
      memories: store.memories,
      memoriesEnabled: store.memoriesEnabled,
      // System prompt
      customSystemPrompt: store.customSystemPrompt,
      useCustomSystemPrompt: store.useCustomSystemPrompt,
      // Mode config
      consortiumEnabled: store.consortiumEnabled,
      consortiumTier: store.consortiumTier,
      liquidResponseEnabled: store.liquidResponseEnabled,
      liquidMinDelta: store.liquidMinDelta,
      ultraplinianEnabled: store.ultraplinianEnabled,
      ultraplinianTier: store.ultraplinianTier,
      ultraplinianApiUrl: store.ultraplinianApiUrl,
      ultraplinianApiKey: store.ultraplinianApiKey,
      // Privacy
      datasetGenerationEnabled: store.datasetGenerationEnabled,
      noLogMode: store.noLogMode,
      showMagic: store.showMagic,
      promptsTried: store.promptsTried,
    }
    const data = JSON.stringify(exportData, null, 2)
    const blob = new Blob([data], { type: 'application/json' })
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = `g0dm0d3-backup-${new Date().toISOString().split('T')[0]}.json`
    a.click()
    URL.revokeObjectURL(url)
  }

  const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0]
    // Always reset input so the same file can be re-selected
    e.target.value = ''
    if (!file) return

    if (file.size > MAX_IMPORT_SIZE) {
      setImportStatus('error')
      setImportMessage(`File too large (${(file.size / 1024 / 1024).toFixed(1)} MB). Maximum is 10 MB.`)
      return
    }

    const reader = new FileReader()
    reader.onload = (event) => {
      try {
        const imported = JSON.parse(event.target?.result as string)

        // Legacy export: just an array of conversations
        if (Array.isArray(imported)) {
          if (!imported.every((c: unknown) => c && typeof c === 'object' && 'id' in (c as Record<string, unknown>) && 'messages' in (c as Record<string, unknown>))) {
            throw new Error('Array does not contain valid conversations.')
          }
          setPendingImport({ conversations: imported })
          setPendingImportSummary(`${imported.length} conversations (legacy format)`)
          return
        }

        if (typeof imported !== 'object' || imported === null) {
          throw new Error('Not a valid G0DM0D3 export file.')
        }

        // Validate conversations field if present
        if (imported.conversations !== undefined && !Array.isArray(imported.conversations)) {
          throw new Error('Invalid backup: conversations field is not an array.')
        }

        // Must have conversations or _source marker
        if (!imported.conversations && !imported._source) {
          throw new Error('Not a valid G0DM0D3 export file.')
        }

        const convCount = imported.conversations?.length ?? 0
        const hasSettings = imported.theme || imported.defaultModel || imported.apiKey
        const memCount = imported.memories?.length ?? 0
        const parts = []
        if (convCount > 0) parts.push(`${convCount} conversations`)
        if (memCount > 0) parts.push(`${memCount} memories`)
        if (hasSettings) parts.push('settings')
        if (imported.apiKey) parts.push('API key')

        setPendingImport(imported)
        setPendingImportSummary(parts.join(', ') || 'backup data')
      } catch (err) {
        setImportStatus('error')
        setImportMessage(err instanceof Error ? err.message : 'Failed to parse export file.')
      }
    }
    reader.readAsText(file)
  }

  const confirmImport = () => {
    if (!pendingImport) return
    store.restoreBackup(pendingImport)
    setImportStatus('success')
    setImportMessage(`Restored ${pendingImportSummary}.`)
    setPendingImport(null)
    setPendingImportSummary('')
  }

  const cancelImport = () => {
    setPendingImport(null)
    setPendingImportSummary('')
  }

  const handleClear = () => {
    clearConversations()
    setShowConfirm(false)
  }

  return (
    <div className="space-y-6">
      {/* Self-custody banner */}
      <div className="p-4 rounded-lg border border-theme-primary bg-theme-accent/50">
        <h3 className="text-lg font-semibold mb-1">Your Data. Your Device. Your Responsibility.</h3>
        <p className="text-sm theme-secondary leading-relaxed">
          G0DM0D3 stores everything locally in this browser — conversations, memories, settings, API keys.
          Nothing is sent to a server. There is no cloud sync, no account, no safety net.
          If you clear your browser data or switch devices, it's gone.
          <strong className="theme-primary"> Export a backup regularly.</strong>
        </p>
      </div>

      {/* Backup section */}
      <div className="space-y-3">
        <h4 className="text-xs font-bold uppercase tracking-wider theme-accent">Self-Custody Backup</h4>

        {/* Export */}
        <button
          onClick={handleExport}
          className="w-full flex items-center justify-center gap-2 px-4 py-3
            bg-theme-accent border border-theme-primary rounded-lg
            hover:glow-box transition-all"
        >
          <Download className="w-4 h-4" />
          Export Full Backup
        </button>
        <p className="text-xs theme-secondary px-1">
          Downloads a JSON file you own — {conversations.length} conversations, all settings,
          memories, and config. Portable across browsers and devices.
          Includes API keys — treat this file like a password.
        </p>

        {/* Import */}
        <label
          className="w-full flex items-center justify-center gap-2 px-4 py-3
            border border-theme-primary rounded-lg cursor-pointer
            hover:bg-theme-accent transition-all"
        >
          <Upload className="w-4 h-4" />
          Restore from Backup
          <input
            type="file"
            accept=".json"
            onChange={handleFileSelect}
            className="hidden"
          />
        </label>
        <p className="text-xs theme-secondary px-1">
          Import a previous backup to restore your full state. You'll confirm before anything is overwritten.
        </p>

        {/* Import confirmation */}
        {pendingImport && (
          <div className="p-4 border border-yellow-500 rounded-lg bg-yellow-500/10">
            <div className="flex items-start gap-3 mb-3">
              <AlertTriangle className="w-5 h-5 text-yellow-500 flex-shrink-0" />
              <p className="text-sm">
                This will replace your current data with: <strong>{pendingImportSummary}</strong>.
                Your existing conversations and settings will be overwritten.
              </p>
            </div>
            <div className="flex gap-2">
              <button
                onClick={confirmImport}
                className="flex-1 px-4 py-2 bg-yellow-600 text-white rounded-lg
                  hover:bg-yellow-700 transition-colors"
              >
                Confirm Import
              </button>
              <button
                onClick={cancelImport}
                className="flex-1 px-4 py-2 border border-theme-primary rounded-lg
                  hover:bg-theme-accent transition-colors"
              >
                Cancel
              </button>
            </div>
          </div>
        )}

        {importStatus !== 'idle' && (
          <div className={`p-3 rounded-lg text-sm flex items-start gap-2 ${
            importStatus === 'success'
              ? 'bg-green-500/10 border border-green-500 text-green-400'
              : 'bg-red-500/10 border border-red-500 text-red-400'
          }`}>
            {importStatus === 'success' ? <Check className="w-4 h-4 flex-shrink-0 mt-0.5" /> : <AlertTriangle className="w-4 h-4 flex-shrink-0 mt-0.5" />}
            {importMessage}
          </div>
        )}

      </div>

      {/* Danger zone */}
      <div className="space-y-3 pt-2 border-t border-theme-primary/30">
        <h4 className="text-xs font-bold uppercase tracking-wider text-red-500">Danger Zone</h4>
        {!showConfirm ? (
          <button
            onClick={() => setShowConfirm(true)}
            disabled={conversations.length === 0}
            className="w-full flex items-center justify-center gap-2 px-4 py-3
              border border-red-500 text-red-500 rounded-lg
              hover:bg-red-500/10 transition-all
              disabled:opacity-50 disabled:cursor-not-allowed"
          >
            <Trash2 className="w-4 h-4" />
            Clear All Data
          </button>
        ) : (
          <div className="p-4 border border-red-500 rounded-lg bg-red-500/10">
            <div className="flex items-start gap-3 mb-3">
              <AlertTriangle className="w-5 h-5 text-red-500 flex-shrink-0" />
              <p className="text-sm">
                This permanently deletes all {conversations.length} conversations, memories, and settings from this browser.
                There is no undo. If you haven't exported a backup, this data is gone forever.
              </p>
            </div>
            <div className="flex gap-2">
              <button
                onClick={handleClear}
                className="flex-1 px-4 py-2 bg-red-500 text-white rounded-lg
                  hover:bg-red-600 transition-colors"
              >
                Confirm Delete
              </button>
              <button
                onClick={() => setShowConfirm(false)}
                className="flex-1 px-4 py-2 border border-theme-primary rounded-lg
                  hover:bg-theme-accent transition-colors"
              >
                Cancel
              </button>
            </div>
          </div>
        )}
        <p className="text-xs theme-secondary px-1">
          Wipes this browser's localStorage completely. Cannot be recovered without a backup file.
        </p>
      </div>

      {/* Storage usage footer */}
      <div className="pt-4 border-t border-theme-primary/20">
        <div className="flex items-center gap-2 mb-2">
          <HardDrive className="w-3.5 h-3.5 theme-secondary" />
          <span className="text-xs theme-secondary">
            Local Storage: {formatBytes(storageUsed)} / ~5 MB
          </span>
          <span className="text-xs theme-secondary ml-auto">{storagePercent}%</span>
        </div>
        <div className="w-full h-1.5 rounded-full bg-theme-accent overflow-hidden">
          <div
            className={`h-full rounded-full transition-all ${
              storagePercent > 85 ? 'bg-red-500' : storagePercent > 60 ? 'bg-yellow-500' : 'bg-theme-primary'
            }`}
            style={{ width: `${storagePercent}%` }}
          />
        </div>
        <p className="text-[10px] theme-secondary mt-1.5 opacity-60">
          Browser localStorage is capped at ~5-10 MB. Export backups before hitting the limit.
        </p>
      </div>
    </div>
  )
}

function ToggleSetting({
  label,
  description,
  enabled,
  onChange,
  warning
}: {
  label: string
  description: string
  enabled: boolean
  onChange: (value: boolean) => void
  warning?: string
}) {
  return (
    <div className="flex items-start justify-between gap-4 p-4 bg-theme-dim border border-theme-primary rounded-lg">
      <div className="flex-1">
        <h4 className="font-semibold">{label}</h4>
        <p className="text-sm theme-secondary">{description}</p>
        {warning && enabled && (
          <p className="text-xs text-yellow-500 mt-1 flex items-center gap-1">
            <AlertTriangle className="w-3 h-3" />
            {warning}
          </p>
        )}
      </div>
      <button
        onClick={() => onChange(!enabled)}
        className={`w-12 h-6 rounded-full transition-colors relative flex-shrink-0
          ${enabled ? 'bg-green-500' : 'bg-theme-accent'}`}
      >
        <span
          className={`absolute top-1 w-4 h-4 rounded-full bg-white transition-transform
            ${enabled ? 'left-7' : 'left-1'}`}
        />
      </button>
    </div>
  )
}