'use client'

import { useEffect, useState, useRef } from 'react'
import { useParams, useRouter } from 'next/navigation'
import { getSite, updateSite, resetSiteData, deleteSite, type Site, type GeoDataLevel } from '@/lib/api/sites'
import { listGoals, createGoal, updateGoal, deleteGoal, type Goal } from '@/lib/api/goals'
import { toast } from '@ciphera-net/ui'
import { getAuthErrorMessage } from '@ciphera-net/ui'
import { SettingsFormSkeleton, GoalsListSkeleton, useMinimumLoading } from '@/components/skeletons'
import VerificationModal from '@/components/sites/VerificationModal'
import ScriptSetupBlock from '@/components/sites/ScriptSetupBlock'
import { PasswordInput } from '@ciphera-net/ui'
import { Select, Modal, Button } from '@ciphera-net/ui'
import { APP_URL } from '@/lib/api/client'
import { generatePrivacySnippet } from '@/lib/utils/privacySnippet'
import { useUnsavedChanges } from '@/lib/hooks/useUnsavedChanges'
import { getSubscription, type SubscriptionDetails } from '@/lib/api/billing'
import { getRetentionOptionsForPlan, formatRetentionMonths } from '@/lib/plans'
import { motion, AnimatePresence } from 'framer-motion'
import { useAuth } from '@/lib/auth/context'
import {
  SettingsIcon,
  GlobeIcon,
  CheckIcon,
  AlertTriangleIcon,
  ZapIcon,
} from '@ciphera-net/ui'

const TIMEZONES = [
  'UTC',
  'America/New_York',
  'America/Los_Angeles',
  'America/Chicago',
  'America/Toronto',
  'Europe/London',
  'Europe/Paris',
  'Europe/Berlin',
  'Europe/Amsterdam',
  'Asia/Tokyo',
  'Asia/Singapore',
  'Asia/Dubai',
  'Australia/Sydney',
  'Pacific/Auckland',
]

export default function SiteSettingsPage() {
  const { user } = useAuth()
  const canEdit = user?.role === 'owner' || user?.role === 'admin'

  const params = useParams()
  const router = useRouter()
  const siteId = params.id as string

  const [site, setSite] = useState<Site | null>(null)
  const [loading, setLoading] = useState(true)
  const [saving, setSaving] = useState(false)
  const [activeTab, setActiveTab] = useState<'general' | 'visibility' | 'data' | 'goals'>('general')

  const [formData, setFormData] = useState({
    name: '',
    timezone: 'UTC',
    is_public: false,
    password: '',
    excluded_paths: '',
    // Data collection settings
    collect_page_paths: true,
    collect_referrers: true,
    collect_device_info: true,
    collect_geo_data: 'full' as GeoDataLevel,
    collect_screen_resolution: true,
    // Performance insights setting
    enable_performance_insights: false,
    // Bot and noise filtering
    filter_bots: true,
    // Data retention (6 = free-tier max; safe default)
    data_retention_months: 6
  })
  const [subscription, setSubscription] = useState<SubscriptionDetails | null>(null)
  const [subscriptionLoadFailed, setSubscriptionLoadFailed] = useState(false)
  const [linkCopied, setLinkCopied] = useState(false)
  const [snippetCopied, setSnippetCopied] = useState(false)
  const [showVerificationModal, setShowVerificationModal] = useState(false)
  const [isPasswordEnabled, setIsPasswordEnabled] = useState(false)
  const [goals, setGoals] = useState<Goal[]>([])
  const [goalsLoading, setGoalsLoading] = useState(false)
  const [goalModalOpen, setGoalModalOpen] = useState(false)
  const [editingGoal, setEditingGoal] = useState<Goal | null>(null)
  const [goalForm, setGoalForm] = useState({ name: '', event_name: '' })
  const [goalSaving, setGoalSaving] = useState(false)
  const initialFormRef = useRef<string>('')

  useEffect(() => {
    loadSite()
    loadSubscription()
  }, [siteId])

  useEffect(() => {
    if (activeTab === 'goals' && siteId) {
      loadGoals()
    }
  }, [activeTab, siteId])

  const loadSubscription = async () => {
    try {
      setSubscriptionLoadFailed(false)
      const sub = await getSubscription()
      setSubscription(sub)
    } catch (e) {
      setSubscriptionLoadFailed(true)
      toast.error(getAuthErrorMessage(e as Error) || 'Could not load plan limits. Showing default options.')
    }
  }

  // * Snap data_retention_months to nearest valid option when subscription loads
  useEffect(() => {
    if (!subscription) return
    const opts = getRetentionOptionsForPlan(subscription.plan_id)
    const values = opts.map(o => o.value)
    const maxVal = Math.max(...values)
    setFormData(prev => {
      if (values.includes(prev.data_retention_months)) return prev
      const bestFit = values.filter(v => v <= prev.data_retention_months).pop() ?? maxVal
      return { ...prev, data_retention_months: Math.min(bestFit, maxVal) }
    })
  }, [subscription])

  const loadSite = async () => {
    try {
      setLoading(true)
      const data = await getSite(siteId)
      setSite(data)
      setFormData({
        name: data.name,
        timezone: data.timezone || 'UTC',
        is_public: data.is_public || false,
        password: '', // Don't show existing password
        excluded_paths: (data.excluded_paths || []).join('\n'),
        // Data collection settings (default to true/full for backwards compatibility)
        collect_page_paths: data.collect_page_paths ?? true,
        collect_referrers: data.collect_referrers ?? true,
        collect_device_info: data.collect_device_info ?? true,
        collect_geo_data: data.collect_geo_data || 'full',
        collect_screen_resolution: data.collect_screen_resolution ?? true,
        // Performance insights setting (default to false)
        enable_performance_insights: data.enable_performance_insights ?? false,
        // Bot and noise filtering (default to true)
        filter_bots: data.filter_bots ?? true,
        // Data retention (default 6 = free-tier max; avoids flash-then-clamp for existing sites)
        data_retention_months: data.data_retention_months ?? 6
      })
      initialFormRef.current = JSON.stringify({
        name: data.name,
        timezone: data.timezone || 'UTC',
        is_public: data.is_public || false,
        excluded_paths: (data.excluded_paths || []).join('\n'),
        collect_page_paths: data.collect_page_paths ?? true,
        collect_referrers: data.collect_referrers ?? true,
        collect_device_info: data.collect_device_info ?? true,
        collect_geo_data: data.collect_geo_data || 'full',
        collect_screen_resolution: data.collect_screen_resolution ?? true,
        enable_performance_insights: data.enable_performance_insights ?? false,
        filter_bots: data.filter_bots ?? true,
        data_retention_months: data.data_retention_months ?? 6
      })
      if (data.has_password) {
        setIsPasswordEnabled(true)
      } else {
        setIsPasswordEnabled(false)
      }
    } catch (error: unknown) {
      toast.error(getAuthErrorMessage(error) || 'Failed to load site settings')
    } finally {
      setLoading(false)
    }
  }

  const loadGoals = async () => {
    try {
      setGoalsLoading(true)
      const data = await listGoals(siteId)
      setGoals(data ?? [])
    } catch (e) {
      toast.error(getAuthErrorMessage(e as Error) || 'Failed to load goals')
    } finally {
      setGoalsLoading(false)
    }
  }

  const openAddGoal = () => {
    setEditingGoal(null)
    setGoalForm({ name: '', event_name: '' })
    setGoalModalOpen(true)
  }

  const openEditGoal = (goal: Goal) => {
    setEditingGoal(goal)
    setGoalForm({ name: goal.name, event_name: goal.event_name })
    setGoalModalOpen(true)
  }

  const handleGoalSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    if (!goalForm.name.trim() || !goalForm.event_name.trim()) {
      toast.error('Name and event name are required')
      return
    }
    const eventName = goalForm.event_name.trim().toLowerCase().replace(/\s+/g, '_')
    if (eventName.length > 64) {
      toast.error('Event name must be 64 characters or less')
      return
    }
    if (!/^[a-zA-Z0-9_]+$/.test(eventName)) {
      toast.error('Event name can only contain letters, numbers, and underscores')
      return
    }
    const duplicateEventName = editingGoal
      ? goals.some((g) => g.id !== editingGoal.id && g.event_name === eventName)
      : goals.some((g) => g.event_name === eventName)
    if (duplicateEventName) {
      toast.error('A goal with this event name already exists')
      return
    }
    setGoalSaving(true)
    try {
      if (editingGoal) {
        await updateGoal(siteId, editingGoal.id, { name: goalForm.name.trim(), event_name: eventName })
        toast.success('Goal updated')
      } else {
        await createGoal(siteId, { name: goalForm.name.trim(), event_name: eventName })
        toast.success('Goal created')
      }
      setGoalModalOpen(false)
      loadGoals()
    } catch (err) {
      toast.error(getAuthErrorMessage(err as Error) || 'Failed to save goal')
    } finally {
      setGoalSaving(false)
    }
  }

  const handleDeleteGoal = async (goal: Goal) => {
    if (!confirm(`Delete goal "${goal.name}"?`)) return
    try {
      await deleteGoal(siteId, goal.id)
      toast.success('Goal deleted')
      loadGoals()
    } catch (err) {
      toast.error(getAuthErrorMessage(err as Error) || 'Failed to delete goal')
    }
  }

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setSaving(true)

    try {
      const excludedPathsArray = formData.excluded_paths
        .split('\n')
        .map(p => p.trim())
        .filter(p => p.length > 0)

      await updateSite(siteId, {
        name: formData.name,
        timezone: formData.timezone,
        is_public: formData.is_public,
        password: isPasswordEnabled ? (formData.password || undefined) : undefined,
        clear_password: !isPasswordEnabled,
        excluded_paths: excludedPathsArray,
        // Data collection settings
        collect_page_paths: formData.collect_page_paths,
        collect_referrers: formData.collect_referrers,
        collect_device_info: formData.collect_device_info,
        collect_geo_data: formData.collect_geo_data,
        collect_screen_resolution: formData.collect_screen_resolution,
        // Performance insights setting
        enable_performance_insights: formData.enable_performance_insights,
        // Bot and noise filtering
        filter_bots: formData.filter_bots,
        // Data retention
        data_retention_months: formData.data_retention_months
      })
      toast.success('Site updated successfully')
      initialFormRef.current = JSON.stringify({
        name: formData.name,
        timezone: formData.timezone,
        is_public: formData.is_public,
        excluded_paths: formData.excluded_paths,
        collect_page_paths: formData.collect_page_paths,
        collect_referrers: formData.collect_referrers,
        collect_device_info: formData.collect_device_info,
        collect_geo_data: formData.collect_geo_data,
        collect_screen_resolution: formData.collect_screen_resolution,
        enable_performance_insights: formData.enable_performance_insights,
        filter_bots: formData.filter_bots,
        data_retention_months: formData.data_retention_months
      })
      loadSite()
    } catch (error: unknown) {
      toast.error(getAuthErrorMessage(error) || 'Failed to save site settings')
    } finally {
      setSaving(false)
    }
  }

  const handleResetData = async () => {
    if (!confirm('Are you sure you want to delete ALL data for this site? This action cannot be undone.')) {
      return
    }

    try {
      await resetSiteData(siteId)
      toast.success('All site data has been reset')
    } catch (error: unknown) {
      toast.error(getAuthErrorMessage(error) || 'Failed to reset site data')
    }
  }

  const handleDeleteSite = async () => {
    const confirmation = prompt('To confirm deletion, please type the site domain:')
    if (confirmation !== site?.domain) {
      if (confirmation) toast.error('Domain does not match')
      return
    }

    try {
      await deleteSite(siteId)
      toast.success('Site deleted successfully')
      router.push('/')
    } catch (error: unknown) {
      toast.error(getAuthErrorMessage(error) || 'Failed to delete site')
    }
  }

  const copyLink = () => {
    const link = `${APP_URL}/share/${siteId}`
    navigator.clipboard.writeText(link)
    setLinkCopied(true)
    toast.success('Link copied to clipboard')
    setTimeout(() => setLinkCopied(false), 2000)
  }

  const copySnippet = () => {
    if (!site) return
    navigator.clipboard.writeText(generatePrivacySnippet(site))
    setSnippetCopied(true)
    toast.success('Privacy snippet copied to clipboard')
    setTimeout(() => setSnippetCopied(false), 2000)
  }

  const isFormDirty = initialFormRef.current !== '' && JSON.stringify({
    name: formData.name,
    timezone: formData.timezone,
    is_public: formData.is_public,
    excluded_paths: formData.excluded_paths,
    collect_page_paths: formData.collect_page_paths,
    collect_referrers: formData.collect_referrers,
    collect_device_info: formData.collect_device_info,
    collect_geo_data: formData.collect_geo_data,
    collect_screen_resolution: formData.collect_screen_resolution,
    enable_performance_insights: formData.enable_performance_insights,
    filter_bots: formData.filter_bots,
    data_retention_months: formData.data_retention_months
  }) !== initialFormRef.current

  useUnsavedChanges(isFormDirty)

  useEffect(() => {
    if (site?.domain) document.title = `Settings · ${site.domain} | Pulse`
  }, [site?.domain])

  const showSkeleton = useMinimumLoading(loading)

  if (showSkeleton) {
    return (
      <div className="w-full max-w-6xl mx-auto px-4 sm:px-6 py-8">
        <div className="space-y-8">
          <div>
            <div className="h-8 w-40 animate-pulse rounded bg-neutral-100 dark:bg-neutral-800 mb-2" />
            <div className="h-4 w-64 animate-pulse rounded bg-neutral-100 dark:bg-neutral-800" />
          </div>
          <div className="flex flex-col md:flex-row gap-8">
            <nav className="w-full md:w-64 flex-shrink-0 space-y-1">
              {Array.from({ length: 4 }).map((_, i) => (
                <div key={i} className="h-12 animate-pulse rounded-xl bg-neutral-100 dark:bg-neutral-800" />
              ))}
            </nav>
            <div className="flex-1 bg-white dark:bg-neutral-900 rounded-2xl border border-neutral-200 dark:border-neutral-800 p-6 md:p-8">
              <SettingsFormSkeleton />
            </div>
          </div>
        </div>
      </div>
    )
  }

  if (!site) {
    return (
      <div className="w-full max-w-6xl mx-auto px-4 sm:px-6 py-8">
        <p className="text-neutral-600 dark:text-neutral-400">Site not found</p>
      </div>
    )
  }

  return (
    <div className="w-full max-w-6xl mx-auto px-4 sm:px-6 py-8">
      <div className="space-y-8">
        <div>
          <h1 className="text-2xl font-bold text-neutral-900 dark:text-white">Site Settings</h1>
          <p className="mt-2 text-neutral-600 dark:text-neutral-400">
            Manage settings for <span className="font-medium text-neutral-900 dark:text-white">{site.domain}</span>
          </p>
        </div>

        <div className="flex flex-col md:flex-row gap-8">
        {/* Sidebar Navigation */}
        <nav className="w-full md:w-64 flex-shrink-0 space-y-1" role="tablist" aria-label="Site settings sections">
          <button
            onClick={() => setActiveTab('general')}
            role="tab"
            aria-selected={activeTab === 'general'}
            className={`w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-xl transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2 ${
              activeTab === 'general'
                ? 'bg-brand-orange/10 text-brand-orange'
                : 'text-neutral-600 dark:text-neutral-400 hover:bg-neutral-100 dark:hover:bg-neutral-800'
            }`}
          >
            <SettingsIcon className="w-5 h-5" />
            General
          </button>
          <button
            onClick={() => setActiveTab('visibility')}
            role="tab"
            aria-selected={activeTab === 'visibility'}
            className={`w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-xl transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2 ${
              activeTab === 'visibility'
                ? 'bg-brand-orange/10 text-brand-orange'
                : 'text-neutral-600 dark:text-neutral-400 hover:bg-neutral-100 dark:hover:bg-neutral-800'
            }`}
          >
            <GlobeIcon className="w-5 h-5" />
            Visibility
          </button>
          <button
            onClick={() => setActiveTab('data')}
            role="tab"
            aria-selected={activeTab === 'data'}
            className={`w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-xl transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2 ${
              activeTab === 'data'
                ? 'bg-brand-orange/10 text-brand-orange'
                : 'text-neutral-600 dark:text-neutral-400 hover:bg-neutral-100 dark:hover:bg-neutral-800'
            }`}
          >
            <SettingsIcon className="w-5 h-5" />
            Data & Privacy
          </button>
          <button
            onClick={() => setActiveTab('goals')}
            role="tab"
            aria-selected={activeTab === 'goals'}
            className={`w-full flex items-center gap-3 px-4 py-3 text-sm font-medium rounded-xl transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2 ${
              activeTab === 'goals'
                ? 'bg-brand-orange/10 text-brand-orange'
                : 'text-neutral-600 dark:text-neutral-400 hover:bg-neutral-100 dark:hover:bg-neutral-800'
            }`}
          >
            <ZapIcon className="w-5 h-5" />
            Goals & Events
          </button>
        </nav>

        {/* Content Area */}
        <div className="flex-1 relative">
          {!canEdit && (
            <div className="mb-6 p-4 bg-amber-50 dark:bg-amber-900/10 text-amber-800 dark:text-amber-200 rounded-xl border border-amber-200 dark:border-amber-800 flex items-center gap-3">
              <AlertTriangleIcon className="w-5 h-5" />
              <p className="text-sm font-medium">You have read-only access to this site. Contact an admin to make changes.</p>
            </div>
          )}

          <motion.div
            key={activeTab}
            initial={{ opacity: 0, x: 20 }}
            animate={{ opacity: 1, x: 0 }}
            transition={{ duration: 0.2 }}
            className="bg-white dark:bg-neutral-900 rounded-2xl border border-neutral-200 dark:border-neutral-800 p-6 md:p-8 shadow-sm"
          >
            {activeTab === 'general' && (
              <div className="space-y-12">
                <form onSubmit={handleSubmit} className="space-y-6">
                  <div>
                    <h2 className="text-2xl font-bold text-neutral-900 dark:text-white mb-1">General Configuration</h2>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400">Update your site details and tracking script.</p>
                  </div>

                  <div className="space-y-4">
                    <div className="space-y-1.5">
                      <label htmlFor="name" className="block text-sm font-medium text-neutral-700 dark:text-neutral-300">
                        Site Name
                      </label>
                      <input
                        type="text"
                        id="name"
                        required
                        maxLength={100}
                        value={formData.name}
                        onChange={(e) => setFormData({ ...formData, name: e.target.value })}
                        className="w-full px-4 py-2 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-neutral-50/50 dark:bg-neutral-900/50 focus:bg-white dark:focus:bg-neutral-900 
                        focus:border-brand-orange focus:ring-4 focus:ring-brand-orange/10 outline-none transition-all duration-200 dark:text-white"
                      />
                      {formData.name.length > 80 && (
                        <span className={`text-xs tabular-nums ${formData.name.length > 90 ? 'text-amber-500' : 'text-neutral-400'}`}>{formData.name.length}/100</span>
                      )}
                    </div>

                    <div className="space-y-1.5">
                      <label htmlFor="timezone" className="block text-sm font-medium text-neutral-700 dark:text-neutral-300">
                        Timezone
                      </label>
                      <Select
                        id="timezone"
                        value={formData.timezone}
                        onChange={(v) => setFormData({ ...formData, timezone: v })}
                        options={TIMEZONES.map((tz) => ({ value: tz, label: tz }))}
                        variant="input"
                        fullWidth
                        align="left"
                      />
                    </div>

                    <div className="space-y-1.5">
                      <label className="block text-sm font-medium text-neutral-700 dark:text-neutral-300">
                        Domain
                      </label>
                      <input
                        type="text"
                        value={site.domain}
                        disabled
                        className="w-full px-4 py-2 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-neutral-100 dark:bg-neutral-800/50 text-neutral-500 dark:text-neutral-400 cursor-not-allowed"
                      />
                      <p className="text-xs text-neutral-500 dark:text-neutral-400">
                        Domain cannot be changed after creation
                      </p>
                    </div>
                  </div>

                  <div className="pt-4 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-lg font-semibold text-neutral-900 dark:text-white mb-2">Tracking Script</h3>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400 mb-4">
                      Add this script to your website to start tracking visitors. Choose your framework for setup instructions.
                    </p>
                    <ScriptSetupBlock
                      site={{ domain: site.domain, name: site.name }}
                      showFrameworkPicker
                      className="mb-4"
                    />

                    <div className="flex items-center gap-4 mt-4">
                      <button
                        type="button"
                        onClick={() => setShowVerificationModal(true)}
                        className="flex items-center gap-2 px-4 py-2 bg-white dark:bg-neutral-800 border border-neutral-200 dark:border-neutral-700 text-neutral-700 dark:text-neutral-300 rounded-xl hover:bg-neutral-50 dark:hover:bg-neutral-700 transition-all text-sm font-medium focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2"
                      >
                        <ZapIcon className="w-4 h-4" />
                        Verify Installation
                      </button>
                      <p className="text-xs text-neutral-500 dark:text-neutral-400">
                        Check if your site is sending data correctly.
                      </p>
                    </div>
                  </div>

                    <div className="pt-4 border-t border-neutral-100 dark:border-neutral-800 flex justify-end">
                    {canEdit && (
                      <Button type="submit" disabled={saving} isLoading={saving}>
                        Save Changes
                      </Button>
                    )}
                    </div>
                  </form>

                  {canEdit && (
                  <div className="space-y-6">
                    <div>
                      <h2 className="text-2xl font-bold text-red-600 dark:text-red-500 mb-1">Danger Zone</h2>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400">Irreversible actions for your site.</p>
                    </div>

                  <div className="space-y-4">
                    <div className="p-4 border border-red-200 dark:border-red-900/50 bg-red-50 dark:bg-red-900/10 rounded-2xl flex items-center justify-between">
                      <div>
                        <h3 className="font-medium text-red-900 dark:text-red-200">Reset Data</h3>
                        <p className="text-sm text-red-700 dark:text-red-300 mt-1">Delete all stats and events. This cannot be undone.</p>
                      </div>
                      <button
                        onClick={handleResetData}
                        className="px-4 py-2 bg-white dark:bg-neutral-900 border border-red-200 dark:border-red-900 text-red-600 dark:text-red-400 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors text-sm font-medium focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2"
                      >
                        Reset Data
                      </button>
                    </div>

                    <div className="p-4 border border-red-200 dark:border-red-900/50 bg-red-50 dark:bg-red-900/10 rounded-2xl flex items-center justify-between">
                      <div>
                        <h3 className="font-medium text-red-900 dark:text-red-200">Delete Site</h3>
                        <p className="text-sm text-red-700 dark:text-red-300 mt-1">Permanently delete this site and all data.</p>
                      </div>
                      <button
                        onClick={handleDeleteSite}
                        className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors text-sm font-medium focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2"
                      >
                        Delete Site
                      </button>
                    </div>
                  </div>
                </div>
                )}
              </div>
            )}

            {activeTab === 'visibility' && (
              <div className="space-y-12">
                <form onSubmit={handleSubmit} className="space-y-6">
                  <div>
                    <h2 className="text-2xl font-bold text-neutral-900 dark:text-white mb-1">Visibility Settings</h2>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400">Manage who can view your dashboard.</p>
                  </div>

                  <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                    <div className="flex items-center justify-between">
                      <div className="flex items-center gap-3">
                        <div className="p-2 bg-white dark:bg-neutral-800 rounded-lg text-neutral-400">
                          <GlobeIcon className="w-6 h-6" />
                        </div>
                        <div>
                          <h3 className="font-medium text-neutral-900 dark:text-white">Public Dashboard</h3>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400">
                            Allow anyone with the link to view this dashboard
                          </p>
                        </div>
                      </div>
                      
                      <label className="relative inline-flex items-center cursor-pointer">
                        <input
                          type="checkbox"
                          checked={formData.is_public}
                          onChange={(e) => setFormData({ ...formData, is_public: e.target.checked })}
                          className="sr-only peer"
                        />
                        <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                      </label>
                    </div>

                    <AnimatePresence>
                      {formData.is_public && (
                        <motion.div
                          initial={{ opacity: 0, height: 0 }}
                          animate={{ opacity: 1, height: 'auto' }}
                          exit={{ opacity: 0, height: 0 }}
                          className="mt-6 pt-6 border-t border-neutral-200 dark:border-neutral-800 overflow-hidden space-y-6"
                        >
                          <div>
                            <label className="block text-sm font-medium mb-2 text-neutral-900 dark:text-white">
                              Public Link
                            </label>
                            <div className="flex gap-2">
                              <input
                                type="text"
                                readOnly
                                value={`${APP_URL}/share/${siteId}`}
                                className="flex-1 px-4 py-2 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-neutral-900 text-neutral-600 dark:text-neutral-400 font-mono text-sm"
                              />
                              <button
                                type="button"
                                onClick={copyLink}
                                className="px-4 py-2 bg-white dark:bg-neutral-800 border border-neutral-200 dark:border-neutral-700 text-neutral-900 dark:text-white rounded-xl font-medium hover:bg-neutral-50 dark:hover:bg-neutral-700 transition-colors focus:outline-none focus:ring-2 focus:ring-brand-orange focus:ring-offset-2"
                              >
                                {linkCopied ? 'Copied!' : 'Copy Link'}
                              </button>
                            </div>
                            <p className="mt-2 text-xs text-neutral-500 dark:text-neutral-400">
                              Share this link with others to view the dashboard.
                            </p>
                          </div>

                          <div>
                            <div className="flex items-center justify-between mb-4">
                              <div>
                                <h3 className="text-sm font-medium text-neutral-900 dark:text-white">Password Protection</h3>
                                <p className="text-xs text-neutral-500 mt-1">Restrict access to this dashboard.</p>
                              </div>
                              <label className="relative inline-flex items-center cursor-pointer">
                                <input 
                                  type="checkbox" 
                                  checked={isPasswordEnabled} 
                                  onChange={(e) => {
                                    setIsPasswordEnabled(e.target.checked);
                                    if (!e.target.checked) setFormData({...formData, password: ''}); 
                                  }}
                                  className="sr-only peer"
                                />
                                <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                              </label>
                            </div>

                            <AnimatePresence>
                              {isPasswordEnabled && (
                                <motion.div
                                  initial={{ opacity: 0, height: 0 }}
                                  animate={{ opacity: 1, height: 'auto' }}
                                  exit={{ opacity: 0, height: 0 }}
                                  className="overflow-hidden"
                                >
                                  <PasswordInput
                                    id="password"
                                    value={formData.password}
                                    onChange={(e) => setFormData({ ...formData, password: e.target.value })}
                                    placeholder={site.has_password ? "Change password (leave empty to keep current)" : "Set a password"}
                                  />
                                  <p className="mt-2 text-xs text-neutral-500 dark:text-neutral-400">
                                    Visitors will need to enter this password to view the dashboard.
                                  </p>
                                </motion.div>
                              )}
                            </AnimatePresence>
                          </div>
                        </motion.div>
                      )}
                    </AnimatePresence>
                  </div>

                  <div className="pt-4 border-t border-neutral-100 dark:border-neutral-800 flex justify-end">
                    {canEdit && (
                      <Button type="submit" disabled={saving} isLoading={saving}>
                        Save Changes
                      </Button>
                    )}
                  </div>
                </form>
              </div>
            )}

            {activeTab === 'data' && (
              <div className="space-y-12">
                <form onSubmit={handleSubmit} className="space-y-6">
                  <div>
                    <h2 className="text-2xl font-bold text-neutral-900 dark:text-white mb-1">Data & Privacy</h2>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400">Control what visitor data is collected. Less data = more privacy.</p>
                  </div>

                  {/* Data Collection Controls */}
                  <div className="space-y-4">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">Data Collection</h3>

                    {/* Page Paths Toggle */}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Page Paths</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Track which pages visitors view
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.collect_page_paths}
                            onChange={(e) => setFormData({ ...formData, collect_page_paths: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>

                    {/* Referrers Toggle */}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Referrers</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Track where visitors come from
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.collect_referrers}
                            onChange={(e) => setFormData({ ...formData, collect_referrers: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>

                    {/* Device Info Toggle */}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Device Info</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Track browser, OS, and device type
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.collect_device_info}
                            onChange={(e) => setFormData({ ...formData, collect_device_info: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>

                    {/* Geographic Data Dropdown */}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Geographic Data</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Control location tracking granularity
                          </p>
                        </div>
                        <Select
                          value={formData.collect_geo_data}
                          onChange={(v) => setFormData({ ...formData, collect_geo_data: v as GeoDataLevel })}
                          options={[
                            { value: 'full', label: 'Full (country, region, city)' },
                            { value: 'country', label: 'Country only' },
                            { value: 'none', label: 'None' },
                          ]}
                          variant="input"
                          align="right"
                          className="min-w-[200px]"
                        />
                      </div>
                    </div>

                    {/* Screen Resolution Toggle */}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Screen Resolution</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Track visitor screen sizes
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.collect_screen_resolution}
                            onChange={(e) => setFormData({ ...formData, collect_screen_resolution: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>
                  </div>

                  {/* Bot and noise filtering */}
                  <div className="space-y-4 pt-6 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">Filtering</h3>
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Filter bots and referrer spam</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Exclude known crawlers, scrapers, and referrer spam domains from your stats
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.filter_bots}
                            onChange={(e) => setFormData({ ...formData, filter_bots: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>
                  </div>

                  {/* Performance Insights Toggle */}
                  <div className="space-y-4 pt-6 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">Performance Insights</h3>
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Performance Insights (Add-on)</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Track Core Web Vitals (LCP, CLS, INP) to monitor site performance
                          </p>
                        </div>
                        <label className="relative inline-flex items-center cursor-pointer">
                          <input
                            type="checkbox"
                            checked={formData.enable_performance_insights}
                            onChange={(e) => setFormData({ ...formData, enable_performance_insights: e.target.checked })}
                            className="sr-only peer"
                          />
                          <div className="w-11 h-6 bg-neutral-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-orange/20 dark:peer-focus:ring-brand-orange/20 rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-neutral-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-neutral-600 peer-checked:bg-brand-orange"></div>
                        </label>
                      </div>
                    </div>
                  </div>

                  {/* Data Retention */}
                  <div className="space-y-4 pt-6 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">Data Retention</h3>
                    {subscriptionLoadFailed && (
                      <div className="p-3 rounded-xl border border-amber-200 dark:border-amber-800 bg-amber-50 dark:bg-amber-900/20 flex items-center justify-between gap-3">
                        <p className="text-sm text-amber-800 dark:text-amber-200">
                          Plan limits could not be loaded. Options shown may be limited.
                        </p>
                        <button
                          type="button"
                          onClick={loadSubscription}
                          className="shrink-0 text-sm font-medium text-amber-800 dark:text-amber-200 hover:underline"
                        >
                          Retry
                        </button>
                      </div>
                    )}
                    <div className="p-6 bg-neutral-50 dark:bg-neutral-900/50 rounded-2xl border border-neutral-100 dark:border-neutral-800">
                      <div className="flex items-center justify-between">
                        <div>
                          <h4 className="font-medium text-neutral-900 dark:text-white">Keep raw event data for</h4>
                          <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-0.5">
                            Events older than this are automatically deleted. Aggregated daily stats are kept permanently.
                          </p>
                        </div>
                        <Select
                          value={String(formData.data_retention_months)}
                          onChange={(v) => setFormData({ ...formData, data_retention_months: Number(v) })}
                          options={getRetentionOptionsForPlan(subscription?.plan_id).map(opt => ({
                            value: String(opt.value),
                            label: opt.label,
                          }))}
                          variant="input"
                          align="right"
                          className="min-w-[160px]"
                        />
                      </div>
                      {subscription?.plan_id && subscription.plan_id !== 'free' && (
                        <p className="text-xs text-neutral-400 dark:text-neutral-500 mt-3">
                          Your {subscription.plan_id} plan supports up to {formatRetentionMonths(
                            getRetentionOptionsForPlan(subscription.plan_id).at(-1)?.value ?? 6
                          )} of data retention.
                        </p>
                      )}
                      {(!subscription?.plan_id || subscription.plan_id === 'free') && (
                        <p className="text-xs text-neutral-400 dark:text-neutral-500 mt-3">
                          Free plan supports up to 6 months. <a href="/pricing" className="text-brand-orange hover:underline">Upgrade</a> for longer retention.
                        </p>
                      )}
                    </div>
                  </div>

                  {/* Excluded Paths */}
                  <div className="space-y-4 pt-6 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">Path Filtering</h3>
                    <div className="space-y-1.5">
                      <label htmlFor="excludedPaths" className="block text-sm font-medium text-neutral-700 dark:text-neutral-300">
                        Excluded Paths
                      </label>
                      <div className="relative">
                        <textarea
                          id="excludedPaths"
                          rows={4}
                          value={formData.excluded_paths}
                          onChange={(e) => setFormData({ ...formData, excluded_paths: e.target.value })}
                          placeholder="/admin/*&#10;/staging/*"
                          className="w-full px-4 py-3 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-neutral-50/50 dark:bg-neutral-900/50 focus:bg-white dark:focus:bg-neutral-900
                          focus:border-brand-orange focus:ring-4 focus:ring-brand-orange/10 outline-none transition-all duration-200 dark:text-white font-mono text-sm"
                        />
                      </div>
                      <p className="text-sm text-neutral-500 dark:text-neutral-400 mt-2">
                        Enter paths to exclude from tracking (one per line). Supports wildcards (e.g., /admin/*).
                      </p>
                    </div>
                  </div>

                  {/* For your privacy policy */}
                  <div className="space-y-4 pt-6 border-t border-neutral-100 dark:border-neutral-800">
                    <h3 className="text-sm font-medium text-neutral-700 dark:text-neutral-300">
                      For your privacy policy
                    </h3>
                    <p className="text-sm text-neutral-500 dark:text-neutral-400">
                      Copy the text below into your site&apos;s Privacy Policy to describe your use of Pulse.
                      It updates automatically based on your saved settings above.
                    </p>
                    <p className="text-xs text-amber-600 dark:text-amber-500">
                      This is provided for convenience and is not legal advice. You are responsible for ensuring
                      your privacy policy is accurate and complies with applicable laws.
                    </p>
                    <div className="relative">
                      <textarea
                        readOnly
                        rows={6}
                        value={site ? generatePrivacySnippet(site) : ''}
                        className="w-full px-4 py-3 pr-12 border border-neutral-200 dark:border-neutral-800 rounded-xl
                          bg-neutral-50 dark:bg-neutral-900/50 font-sans text-sm text-neutral-700 dark:text-neutral-300
                          focus:outline-none resize-y"
                      />
                      <button
                        type="button"
                        onClick={copySnippet}
                        className="absolute top-3 right-3 p-2 rounded-lg bg-neutral-200 dark:bg-neutral-700
                          hover:bg-neutral-300 dark:hover:bg-neutral-600 text-neutral-600 dark:text-neutral-300
                          transition-colors"
                        title="Copy snippet"
                      >
                        {snippetCopied ? (
                          <CheckIcon className="w-4 h-4 text-green-600" />
                        ) : (
                          <svg className="w-4 h-4 text-neutral-500 dark:text-neutral-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <rect x="9" y="9" width="13" height="13" rx="2" ry="2" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
                            <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
                          </svg>
                        )}
                      </button>
                    </div>
                  </div>

                  <div className="pt-4 border-t border-neutral-100 dark:border-neutral-800 flex justify-end">
                    {canEdit && (
                      <Button type="submit" disabled={saving} isLoading={saving}>
                        Save Changes
                      </Button>
                    )}
                  </div>
                </form>
              </div>
            )}

            {activeTab === 'goals' && (
              <div className="space-y-6">
                <div>
                  <h2 className="text-2xl font-bold text-neutral-900 dark:text-white mb-1">Goals & Events</h2>
                  <p className="text-sm text-neutral-500 dark:text-neutral-400">
                    Define goals to label custom events (e.g. signup, purchase). Track with <code className="px-1.5 py-0.5 rounded bg-neutral-200 dark:bg-neutral-700 text-xs">pulse.track(&apos;event_name&apos;)</code> in your snippet.
                  </p>
                </div>
                {goalsLoading ? (
                  <GoalsListSkeleton />
                ) : (
                  <>
                    {canEdit && (
                      <Button onClick={openAddGoal} variant="primary">
                        Add goal
                      </Button>
                    )}
                    <div className="space-y-2">
                      {goals.length === 0 ? (
                        <div className="p-6 rounded-2xl border border-neutral-200 dark:border-neutral-800 bg-neutral-50 dark:bg-neutral-900/50 text-center text-neutral-500 dark:text-neutral-400 text-sm">
                          No goals yet. Add a goal to give custom events a display name in the dashboard.
                        </div>
                      ) : (
                        goals.map((goal) => (
                          <div
                            key={goal.id}
                            className="flex items-center justify-between py-3 px-4 rounded-2xl border border-neutral-200 dark:border-neutral-800 bg-neutral-50/50 dark:bg-neutral-900/50"
                          >
                            <div>
                              <span className="font-medium text-neutral-900 dark:text-white">{goal.name}</span>
                              <span className="text-neutral-500 dark:text-neutral-400 text-sm ml-2">({goal.event_name})</span>
                            </div>
                            {canEdit && (
                              <div className="flex items-center gap-2">
                                <button
                                  type="button"
                                  onClick={() => openEditGoal(goal)}
                                  className="text-sm text-brand-orange hover:underline"
                                >
                                  Edit
                                </button>
                                <button
                                  type="button"
                                  onClick={() => handleDeleteGoal(goal)}
                                  className="text-sm text-red-600 dark:text-red-400 hover:underline"
                                >
                                  Delete
                                </button>
                              </div>
                            )}
                          </div>
                        ))
                      )}
                    </div>
                  </>
                )}
              </div>
            )}
          </motion.div>
        </div>
      </div>
    </div>

      <Modal
        isOpen={goalModalOpen}
        onClose={() => setGoalModalOpen(false)}
        title={editingGoal ? 'Edit goal' : 'Add goal'}
      >
        <form onSubmit={handleGoalSubmit} className="space-y-4">
          <div>
            <label className="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">Display name</label>
            <input
              type="text"
              value={goalForm.name}
              onChange={(e) => setGoalForm({ ...goalForm, name: e.target.value })}
              placeholder="e.g. Signups"
              autoFocus
              className="w-full px-4 py-2 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-neutral-900 text-neutral-900 dark:text-white"
              required
            />
          </div>
          <div>
            <label className="block text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">Event name</label>
            <input
              type="text"
              value={goalForm.event_name}
              onChange={(e) => setGoalForm({ ...goalForm, event_name: e.target.value })}
              placeholder="e.g. signup_click (letters, numbers, underscores only)"
              maxLength={64}
              className="w-full px-4 py-2 border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-neutral-900 text-neutral-900 dark:text-white"
              required
            />
            <div className="flex justify-between mt-1">
              <p className="text-xs text-neutral-500 dark:text-neutral-400">Letters, numbers, and underscores only. Spaces become underscores.</p>
              <span className={`text-xs tabular-nums ${goalForm.event_name.length > 56 ? 'text-amber-500' : 'text-neutral-400'}`}>{goalForm.event_name.length}/64</span>
            </div>
            {editingGoal && goalForm.event_name.trim().toLowerCase().replace(/\s+/g, '_') !== editingGoal.event_name && (
              <p className="mt-2 text-xs text-amber-600 dark:text-amber-400">Changing event name does not reassign events already tracked under the previous name.</p>
            )}
          </div>
          <div className="flex justify-end gap-2 pt-2">
            <Button type="button" variant="secondary" onClick={() => setGoalModalOpen(false)}>
              Cancel
            </Button>
            <Button type="submit" variant="primary" disabled={goalSaving}>
              {goalSaving ? 'Saving…' : editingGoal ? 'Update' : 'Create'}
            </Button>
          </div>
        </form>
      </Modal>

      <VerificationModal
        isOpen={showVerificationModal}
        onClose={() => setShowVerificationModal(false)}
        site={site}
      />
    </div>
  )
}
