<template>
  <div class="storyboard-video-create-page">
    <!-- 顶部导航栏 -->
    <header class="top-header">
      <div class="header-left">
        <button class="back-btn" @click="goBack">
          ← 首页
        </button>
      </div>
      <div class="header-right">
        <div class="credits-info">
          <div class="credits-circle">25</div>
          <span>| 首购优惠</span>
        </div>
        <div class="notification-icon">
          🔔
          <div class="notification-badge">5</div>
        </div>
        <div class="user-avatar">
          👤
        </div>
      </div>
    </header>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 左侧设置面板 -->
      <div class="left-panel">
        <!-- 创作模式标签 -->
        <div class="creation-tabs">
          <div class="tab" @click="goToTextToVideo">文生视频</div>
          <div class="tab" @click="goToImageToVideo">图生视频</div>
          <div class="tab active">分镜视频</div>
        </div>

        <!-- 分镜步骤标签 -->
        <div class="storyboard-steps">
          <div 
            class="step" 
            :class="{ active: currentStep === 'generate' }"
            @click="switchToGenerateStep"
          >
            生成分镜图
          </div>
          <div 
            class="step" 
            :class="{ active: currentStep === 'video', disabled: !generatedImageUrl && !uploadedImage }"
            @click="switchToVideoStep"
          >
            生成视频
          </div>
        </div>

        <!-- 生成分镜图区域 - 只在第一步显示 -->
        <div class="storyboard-section" v-if="currentStep === 'generate'">
          <div class="image-upload-btn" @click="uploadImage">
            <span>+ 上传分镜图 (可直接生成视频)</span>
          </div>
          
          <!-- 已上传的图片预览 -->
          <div class="image-preview" v-if="uploadedImage">
            <img :src="uploadedImage" alt="上传的图片" />
            <button class="remove-btn" @click="removeImage">×</button>
          </div>

          <div class="text-input-section">
            <textarea 
              v-model="inputText"
              placeholder="例如:一个咖啡的广告&#10;&#10;提示:简单描述即可，AI会自动优化成专业的分镜图&#10;支持中文或英文输入，系统会自动翻译并优化为专业的分镜图描述"
              class="text-input"
              rows="6"
            ></textarea>
            <div class="input-tips">
              <div class="tip-item">💡 AI会根据您的描述自动生成专业分镜图</div>
              <div class="tip-item">🎬 支持多种画面构图和镜头类型描述</div>
            </div>
            <div class="optimize-btn">
              <button class="optimize-button" @click="optimizePromptHandler" :disabled="!inputText.trim() || optimizingPrompt">
                ✨ {{ optimizingPrompt ? '优化中...' : '一键优化提示词' }}
              </button>
            </div>
          </div>
        </div>

        <!-- 生成视频区域 - 只在第二步显示 -->
        <div class="storyboard-section" v-if="currentStep === 'video'">
          <div class="generated-image-preview">
            <img v-if="generatedImageUrl || uploadedImage" :src="generatedImageUrl || uploadedImage" alt="分镜图" />
            <div v-else class="placeholder-text">暂无分镜图</div>
          </div>
        </div>

        <!-- 视频设置 -->
        <div class="video-settings">
          <div class="setting-item">
            <label>比例</label>
            <select v-model="aspectRatio" class="setting-select">
              <option value="16:9">16:9</option>
              <option value="4:3">4:3</option>
              <option value="1:1">1:1</option>
              <option value="3:4">3:4</option>
              <option value="9:16">9:16</option>
            </select>
          </div>
          
          <div class="setting-item">
            <label>高清模式 (1080P)</label>
            <div class="hd-setting">
              <input type="checkbox" v-model="hdMode" class="hd-switch">
              <span class="cost-text">开启消耗20积分</span>
            </div>
          </div>
        </div>

        <!-- 生成按钮 -->
        <div class="generate-section">
          <button 
            class="generate-btn" 
            @click="handleGenerateClick"
            :disabled="inProgress || (!uploadedImage && !inputText.trim() && currentStep === 'generate') || (currentStep === 'video' && !generatedImageUrl && !uploadedImage)"
          >
            {{ getButtonText() }}
          </button>
        </div>
      </div>

      <!-- 右侧预览区域 -->
      <div class="right-panel">
        <div class="preview-area">
          <div class="status-checkbox">
            <input type="checkbox" v-model="inProgress" id="progress-checkbox">
            <label for="progress-checkbox">进行中</label>
          </div>
          
          <div class="preview-content">
            <!-- 显示上传的图片或生成的图片 -->
            <div v-if="uploadedImage || generatedImageUrl" class="preview-image">
              <img :src="uploadedImage || generatedImageUrl" alt="分镜图" />
            </div>
            <!-- 显示加载状态 -->
            <div v-else-if="inProgress" class="preview-loading">
              <div class="loading-spinner"></div>
              <div class="loading-text">{{ currentStep === 'generate' ? '正在生成分镜图，请稍候...' : '正在生成视频，请稍候...' }}</div>
            </div>
            <!-- 显示占位符 -->
            <div v-else class="preview-placeholder">
              <div class="placeholder-text">开始创作您的第一个作品吧!</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { createStoryboardTask, getStoryboardTask } from '@/api/storyboardVideo'
import { imageToVideoApi } from '@/api/imageToVideo'
import { optimizePrompt } from '@/api/promptOptimizer'

const router = useRouter()

// 表单数据
const inputText = ref('')
const aspectRatio = ref('16:9')
const hdMode = ref(false)
const inProgress = ref(false)
const currentStep = ref('generate') // 'generate' 或 'video'

// 图片上传
const uploadedImage = ref('')
const generatedImageUrl = ref('')
const taskId = ref('')
const pollIntervalId = ref(null) // 保存轮询定时器ID
const optimizingPrompt = ref(false) // 优化提示词状态

// 导航函数
const goBack = () => {
  router.back()
}

const goToTextToVideo = () => {
  router.push('/text-to-video/create')
}

const goToImageToVideo = () => {
  router.push('/image-to-video/create')
}

// 切换到生成分镜图步骤
const switchToGenerateStep = () => {
  currentStep.value = 'generate'
}

// 切换到视频步骤
const switchToVideoStep = () => {
  // 如果有上传的图片或生成的分镜图，可以切换
  if (uploadedImage.value || generatedImageUrl.value) {
    currentStep.value = 'video'
  } else {
    ElMessage.warning('请先上传分镜图或生成分镜图')
  }
}

// 获取按钮文本
const getButtonText = () => {
  if (currentStep.value === 'video') {
    return '生成视频'
  }
  // 第一步：根据是否有图片和提示词显示不同文本
  if (uploadedImage.value) {
    return '使用上传图片生成视频'
  }
  if (inputText.value.trim()) {
    return '开始生成分镜图'
  }
  return '开始生成'
}

// 处理生成按钮点击
const handleGenerateClick = () => {
  console.log('handleGenerateClick 被调用，当前步骤:', currentStep.value)
  
  // 如果已经切换到视频步骤，直接生成视频
  if (currentStep.value === 'video') {
    startVideoGenerate()
    return
  }
  
  // 第一步的逻辑：
  // 1. 如果上传了图片，直接使用上传的图片生成视频
  if (uploadedImage.value) {
    // 使用上传的图片作为分镜图
    generatedImageUrl.value = uploadedImage.value
    // 切换到视频步骤并生成视频
    currentStep.value = 'video'
    // 延迟一点，让UI更新
    setTimeout(() => {
      startVideoGenerate()
    }, 100)
    return
  }
  
  // 2. 如果只有提示词，生成分镜图
  if (inputText.value.trim()) {
    startGenerate()
    return
  }
  
  // 3. 如果都没有，提示用户
  ElMessage.warning('请上传分镜图或输入提示词')
}

// 图片上传处理
const uploadImage = () => {
  const input = document.createElement('input')
  input.type = 'file'
  input.accept = 'image/*'
  input.onchange = (e) => {
    const file = e.target.files[0]
    if (file) {
      const reader = new FileReader()
      reader.onload = (e) => {
        uploadedImage.value = e.target.result
        // 上传图片后，自动使用上传的图片作为分镜图
        generatedImageUrl.value = e.target.result
        // 如果有上传的图片，可以自动切换到视频步骤（可选）
        // currentStep.value = 'video'
      }
      reader.readAsDataURL(file)
    }
  }
  input.click()
}

const removeImage = () => {
  // 保存上传图片的值，用于判断是否与生成的图片相同
  const wasSameImage = uploadedImage.value && generatedImageUrl.value === uploadedImage.value
  uploadedImage.value = ''
  // 如果上传的图片被删除，且它被用作生成的分镜图，也清除生成的图片URL
  if (wasSameImage) {
    generatedImageUrl.value = ''
  }
}

// 优化提示词
const optimizePromptHandler = async () => {
  if (!inputText.value.trim()) {
    ElMessage.warning('请输入提示词')
    return
  }
  
  // 长度检查
  if (inputText.value.length > 2000) {
    ElMessage.warning('提示词过长，请控制在2000字符以内')
    return
  }
  
  try {
    optimizingPrompt.value = true
    const loading = ElLoading.service({
      lock: false,
      text: '正在优化提示词，请稍候...',
      background: 'rgba(0, 0, 0, 0.3)'
    })
    
    const response = await optimizePrompt(inputText.value.trim(), 'storyboard')
    
    loading.close()
    
    if (response.data && response.data.success) {
      const data = response.data.data
      const optimized = data.optimizedPrompt
      
      // 检查是否真正优化了
      if (data.optimized && optimized !== inputText.value.trim()) {
        inputText.value = optimized
        ElMessage.success('提示词优化成功！')
      } else {
        ElMessage.warning('提示词已优化，但可能无明显变化')
      }
    } else {
      ElMessage.error(response.data?.message || '优化失败')
    }
  } catch (error) {
    console.error('优化提示词失败:', error)
    
    let errorMessage = '优化提示词失败'
    if (error.response) {
      const status = error.response.status
      if (status === 400) {
        errorMessage = error.response.data?.message || '请求参数错误'
      } else if (status === 408 || error.code === 'ECONNABORTED') {
        errorMessage = '请求超时，请稍后重试'
      } else if (status >= 500) {
        errorMessage = '服务器错误，请稍后重试'
      } else {
        errorMessage = error.response.data?.message || '优化失败'
      }
    } else if (error.request) {
      errorMessage = '网络错误，请检查网络连接'
    } else {
      errorMessage = error.message || '优化失败'
    }
    
    ElMessage.error(errorMessage)
  } finally {
    optimizingPrompt.value = false
  }
}

// 开始生成分镜图
const startGenerate = async () => {
  console.log('startGenerate 被调用')
  
  if (!inputText.value.trim()) {
    ElMessage.warning('请输入描述文字')
    return
  }
  
  try {
    console.log('开始创建任务，参数:', {
      prompt: inputText.value,
      aspectRatio: aspectRatio.value,
      hdMode: hdMode.value,
      imageUrl: uploadedImage.value || null
    })
    
    inProgress.value = true
    ElMessage.info('开始生成分镜图...')
    
    // 调用API创建任务
    const response = await createStoryboardTask({
      prompt: inputText.value,
      aspectRatio: aspectRatio.value,
      hdMode: hdMode.value,
      imageUrl: uploadedImage.value || null
    })
    
    console.log('API响应:', response)
    
    if (response.data && response.data.success) {
      ElMessage.success('分镜图任务创建成功！')
      taskId.value = response.data.data.taskId
      console.log('Task created:', response.data.data)
      
      // 开始轮询任务状态，获取生成的图片
      // inProgress 将在轮询完成时设置为 false
      pollTaskStatus(response.data.data.taskId)
    } else {
      console.error('创建任务失败，响应:', response.data)
      ElMessage.error(response.data?.message || '创建任务失败')
      inProgress.value = false
    }
  } catch (error) {
    console.error('生成分镜图失败，完整错误:', error)
    console.error('错误详情:', {
      message: error.message,
      response: error.response,
      stack: error.stack
    })
    ElMessage.error('生成分镜图失败: ' + (error.response?.data?.message || error.message))
    inProgress.value = false
  }
}

// 轮询任务状态
const pollTaskStatus = async (taskId) => {
  // 清除之前的轮询（如果存在）
  if (pollIntervalId.value) {
    clearInterval(pollIntervalId.value)
    pollIntervalId.value = null
  }
  
  const maxAttempts = 30
  let attempts = 0
  
  const poll = setInterval(async () => {
    // 先检查是否超过最大尝试次数
    if (attempts >= maxAttempts) {
      clearInterval(poll)
      pollIntervalId.value = null
      inProgress.value = false
      ElMessage.warning('任务超时，请稍后查看')
      return
    }
    
    attempts++
    
    try {
      // 调用获取任务详情的API
      const response = await getStoryboardTask(taskId)
      console.log('轮询任务状态:', response.data)
      
      if (response.data.success && response.data.data) {
        const task = response.data.data
        console.log('任务状态:', task.status, '图片URL:', task.resultUrl)
        
        if (task.status === 'COMPLETED' && task.resultUrl) {
          clearInterval(poll)
          pollIntervalId.value = null
          console.log('分镜图生成完成，图片URL:', task.resultUrl)
          generatedImageUrl.value = task.resultUrl
          inProgress.value = false
          ElMessage.success('分镜图生成完成！')
          // 自动跳转到第二步
          setTimeout(() => {
            currentStep.value = 'video'
          }, 500)
          return
        } else if (task.status === 'FAILED') {
          clearInterval(poll)
          pollIntervalId.value = null
          inProgress.value = false
          ElMessage.error('分镜图生成失败: ' + (task.errorMessage || '未知错误'))
          return
        }
      }
    } catch (error) {
      console.error('轮询任务状态失败:', error)
      // 错误时继续轮询，直到达到最大次数
    }
  }, 2000)
  
  // 保存轮询ID
  pollIntervalId.value = poll
}

// 将图片URL转换为File对象
const urlToFile = async (url, filename) => {
  try {
    let blob
    
    // 如果是base64格式
    if (url.startsWith('data:image')) {
      const response = await fetch(url)
      if (!response.ok) {
        throw new Error(`HTTP错误: ${response.status}`)
      }
      blob = await response.blob()
      return new File([blob], filename, { type: blob.type })
    } else {
      // 如果是普通URL（可能跨域）
      try {
        const response = await fetch(url)
        if (!response.ok) {
          throw new Error(`HTTP错误: ${response.status}`)
        }
        blob = await response.blob()
        return new File([blob], filename, { type: blob.type || 'image/jpeg' })
      } catch (fetchError) {
        // 如果fetch失败（可能是CORS），尝试通过代理或提示用户
        console.error('直接获取图片失败，可能是CORS问题:', fetchError)
        throw new Error('无法加载图片，请确保图片URL可以访问')
      }
    }
  } catch (error) {
    console.error('转换图片URL失败:', error)
    throw new Error('无法加载图片: ' + (error.message || '未知错误'))
  }
}

// 生成视频
const startVideoGenerate = async () => {
  console.log('startVideoGenerate 被调用')
  
  // 优先使用上传的图片，其次使用生成的分镜图
  const imageUrl = uploadedImage.value || generatedImageUrl.value
  
  if (!imageUrl) {
    ElMessage.warning('请先上传分镜图或生成分镜图')
    return
  }
  
  // 提示词可选，如果没有则使用默认提示词或空字符串
  // 图生视频API可能需要提示词，使用上传图片时的提示词或默认提示词
  let prompt = inputText.value.trim()
  if (!prompt && uploadedImage.value) {
    // 如果用户只上传了图片没有输入提示词，可以使用空字符串或默认提示词
    prompt = '根据图片生成视频' // 默认提示词
  }
  
  try {
    inProgress.value = true
    ElMessage.info('开始生成视频...')
    
    // 将图片URL转换为File对象
    console.log('转换图片URL:', imageUrl)
    let imageFile
    
    // 如果是base64格式（上传的图片），直接转换
    if (imageUrl.startsWith('data:image')) {
      const base64Data = imageUrl.split(',')[1]
      const byteCharacters = atob(base64Data)
      const byteNumbers = new Array(byteCharacters.length)
      for (let i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i)
      }
      const byteArray = new Uint8Array(byteNumbers)
      const blob = new Blob([byteArray], { type: 'image/jpeg' })
      imageFile = new File([blob], 'storyboard-image.jpg', { type: 'image/jpeg' })
    } else {
      // 如果是URL，使用fetch转换
      imageFile = await urlToFile(imageUrl, 'storyboard-image.jpg')
    }
    
    // 调用图生视频API
    console.log('调用图生视频API，参数:', {
      prompt: prompt,
      aspectRatio: aspectRatio.value,
      hdMode: hdMode.value,
      duration: 5,
      firstFrame: imageFile
    })
    
    const response = await imageToVideoApi.createTask({
      firstFrame: imageFile,
      prompt: prompt,
      aspectRatio: aspectRatio.value,
      duration: 5, // 默认5秒
      hdMode: hdMode.value
    })
    
    console.log('图生视频API响应:', response)
    
    if (response.data && response.data.success) {
      const taskId = response.data.data.taskId
      ElMessage.success('视频任务创建成功！')
      console.log('视频任务创建成功，任务ID:', taskId)
      
      // 跳转到图生视频结果页面或者在这里显示结果
      // 可以轮询任务状态或跳转到任务详情页
      router.push(`/image-to-video/detail/${taskId}`)
    } else {
      console.error('创建视频任务失败，响应:', response.data)
      ElMessage.error(response.data?.message || '创建视频任务失败')
      inProgress.value = false
    }
  } catch (error) {
    console.error('生成视频失败，完整错误:', error)
    console.error('错误详情:', {
      message: error.message,
      response: error.response,
      stack: error.stack
    })
    ElMessage.error('生成视频失败: ' + (error.response?.data?.message || error.message))
    inProgress.value = false
  }
}

// 组件卸载时清理轮询
onBeforeUnmount(() => {
  if (pollIntervalId.value) {
    clearInterval(pollIntervalId.value)
    pollIntervalId.value = null
  }
})
</script>

<style scoped>
.storyboard-video-create-page {
  height: 100vh;
  background: #0a0a0a;
  color: #fff;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 顶部导航栏 */
.top-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 32px;
  background: #0a0a0a;
  border-bottom: 1px solid #1f1f1f;
  min-height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.back-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  padding: 10px 20px;
  border-radius: 8px;
  transition: all 0.2s ease;
  font-weight: 500;
}

.back-btn:hover {
  background: #1a1a1a;
  transform: translateX(-2px);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 24px;
}

.credits-info {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
}

.credits-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.notification-icon {
  position: relative;
  font-size: 24px;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: background 0.2s ease;
}

.notification-icon:hover {
  background: #1a1a1a;
}

.notification-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: #ef4444;
  color: #fff;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.4);
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #374151, #1f2937);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 20px;
  transition: transform 0.2s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
}

/* 主内容区域 */
.main-content {
  flex: 1;
  display: grid;
  grid-template-columns: 400px 1fr;
  gap: 0;
  height: calc(100vh - 100px);
}

/* 左侧面板 */
.left-panel {
  background: #1a1a1a;
  border-right: 1px solid #2a2a2a;
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 32px;
  overflow-y: auto;
}

/* 创作模式标签 */
.creation-tabs {
  display: flex;
  gap: 4px;
  background: #0a0a0a;
  padding: 4px;
  border-radius: 12px;
}

.tab {
  flex: 1;
  padding: 12px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #9ca3af;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
}

.tab.active {
  background: #3b82f6;
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.tab:hover:not(.active) {
  background: #2a2a2a;
  color: #fff;
}

/* 分镜步骤标签 */
.storyboard-steps {
  display: flex;
  gap: 4px;
  background: #0a0a0a;
  padding: 4px;
  border-radius: 12px;
}

.step {
  flex: 1;
  padding: 10px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #9ca3af;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.step.active {
  background: #3b82f6;
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.step:hover:not(.active):not(.disabled) {
  background: #2a2a2a;
  color: #fff;
}

.step.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.step.disabled:hover {
  background: transparent;
}

/* 生成的图片预览 */
.generated-image-preview {
  width: 100%;
  aspect-ratio: 16/9;
  background: #0a0a0a;
  border: 2px solid #2a2a2a;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.generated-image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.generated-image-preview .placeholder-text {
  color: #6b7280;
  font-size: 14px;
}

/* 分镜图区域 */
.storyboard-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.image-upload-btn {
  width: 100%;
  padding: 12px 16px;
  background: #0a0a0a;
  border: 2px dashed #2a2a2a;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: center;
  color: #9ca3af;
  font-size: 14px;
}

.image-upload-btn:hover {
  border-color: #3b82f6;
  background: #1a1a1a;
  color: #fff;
}

.image-preview {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.remove-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
}

/* 文本输入区域 */
.text-input-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.text-input {
  width: 100%;
  min-height: 120px;
  padding: 16px;
  background: #0a0a0a;
  border: 2px solid #2a2a2a;
  border-radius: 12px;
  color: #fff;
  font-size: 15px;
  line-height: 1.6;
  resize: vertical;
  outline: none;
  transition: all 0.2s ease;
  font-family: inherit;
}

.text-input:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.text-input::placeholder {
  color: #6b7280;
}

.input-tips {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: rgba(59, 130, 246, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.tip-item {
  font-size: 13px;
  color: #9ca3af;
  line-height: 1.5;
}

.tip-item:first-child {
  color: #60a5fa;
  font-weight: 500;
}

.optimize-btn {
  display: flex;
  justify-content: flex-end;
}

.optimize-button {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.optimize-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

/* 视频设置 */
.video-settings {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setting-item label {
  font-size: 14px;
  color: #e5e7eb;
  font-weight: 600;
}

.setting-select {
  padding: 12px 16px;
  background: #0a0a0a;
  border: 2px solid #2a2a2a;
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  outline: none;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}

.setting-select:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.setting-select:hover {
  border-color: #374151;
}

.hd-setting {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hd-switch {
  width: 20px;
  height: 20px;
  cursor: pointer;
  accent-color: #3b82f6;
}

.cost-text {
  font-size: 13px;
  color: #9ca3af;
  font-weight: 500;
}

/* 生成按钮 */
.generate-section {
  margin-top: auto;
}

.generate-btn {
  width: 100%;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: #fff;
  border: none;
  padding: 16px 24px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.generate-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(59, 130, 246, 0.4);
}

.generate-btn:active {
  transform: translateY(0);
}

.generate-btn:disabled {
  background: #6b7280;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* 右侧面板 */
.right-panel {
  background: #0a0a0a;
  padding: 32px;
  display: flex;
  flex-direction: column;
}

.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.status-checkbox {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.status-checkbox input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: #3b82f6;
}

.status-checkbox label {
  font-size: 14px;
  color: #e5e7eb;
  cursor: pointer;
  font-weight: 500;
}

.preview-content {
  flex: 1;
  background: #1a1a1a;
  border: 2px solid #2a2a2a;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.preview-content:hover {
  border-color: #374151;
}

.preview-placeholder {
  text-align: center;
  padding: 40px;
}

.placeholder-text {
  font-size: 18px;
  color: #6b7280;
  font-weight: 500;
  line-height: 1.5;
}

/* 预览图片 */
.preview-image {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.preview-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* 加载状态 */
.preview-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 40px;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #2a2a2a;
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-text {
  font-size: 16px;
  color: #9ca3af;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 350px 1fr;
  }
  
  .left-panel {
    padding: 24px;
  }
  
  .right-panel {
    padding: 24px;
  }
}

@media (max-width: 1024px) {
  .main-content {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  
  .left-panel {
    border-right: none;
    border-bottom: 1px solid #2a2a2a;
    padding: 20px;
  }
  
  .right-panel {
    padding: 20px;
  }
}

@media (max-width: 768px) {
  .top-header {
    padding: 16px 20px;
  }
  
  .header-right {
    gap: 16px;
  }
  
  .left-panel {
    padding: 16px;
    gap: 24px;
  }
  
  .right-panel {
    padding: 16px;
  }
  
  .creation-tabs {
    flex-direction: column;
    gap: 8px;
  }
  
  .tab {
    text-align: left;
  }
  
  .storyboard-steps {
    flex-direction: column;
    gap: 8px;
  }
  
  .step {
    text-align: left;
  }
}
</style>











