<template>
  <scroll-view scroll-y class="page-scroll" :scroll-top="pageScrollTop">
    <view class="container">
      <view class="tabs">
        <view class="tab" :class="{ active: activeTab === 'manual' }" @click="activeTab = 'manual'">
          手动记账
        </view>
        <view class="tab" :class="{ active: activeTab === 'ocr' }" @click="activeTab = 'ocr'">
          OCR识别
        </view>
      </view>
      
      <!-- 手动记账 -->
      <view v-if="activeTab === 'manual'" class="form">
      <view class="form-item">
        <text class="label">账单类型</text>
        <view class="type-selector-compact">
          <view 
            class="type-option-compact" 
            :class="{ active: form.type === 1 }" 
            @click="form.type = 1"
          >
            <text class="type-icon-compact">📤</text>
            <text class="type-text-compact">支出</text>
          </view>
          <view 
            class="type-option-compact" 
            :class="{ active: form.type === 2 }" 
            @click="form.type = 2"
          >
            <text class="type-icon-compact">📥</text>
            <text class="type-text-compact">收入</text>
          </view>
        </view>
      </view>
      
      <view class="form-item">
        <text class="label">分类</text>
        <view v-if="categories.length > 0" class="category-grid">
          <view 
            v-for="category in categories" 
            :key="category.id" 
            class="category-icon-item"
            :class="{ active: selectedCategory?.id === category.id }"
            @click="selectCategory(category)"
          >
            <view class="category-icon-circle">
              <text class="category-icon-emoji">{{ category.icon || '📦' }}</text>
            </view>
            <text class="category-icon-name">{{ category.name }}</text>
          </view>
        </view>
        <view v-else class="category-empty">
          <text>暂无分类，请先创建分类</text>
        </view>
      </view>
      
      <view class="form-item">
        <text class="label">金额</text>
        <input 
          v-model="form.amount" 
          type="number"
          inputmode="decimal"
          placeholder="请输入金额" 
          @input="onAmountInput"
          @focus="onAmountFocus"
        />
		
      </view>
      
      <view class="form-item">
        <text class="label">日期</text>
        <picker mode="date" :value="form.billDate" @change="onDateChange">
          <view class="picker">
            <text>{{ form.billDate || '选择日期' }}</text>
            <text class="arrow">></text>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <text class="label">备注</text>
        <textarea v-model="form.description" placeholder="请输入备注（可选）" class="textarea" />
      </view>
      
      <button class="submit-btn" @click="submitBill">保存</button>
    </view>
    
    <!-- OCR识别 -->
    <view v-if="activeTab === 'ocr'" class="ocr-container">
      <!-- 图片预览区域（可折叠） -->
      <view class="image-section">
        <view class="section-header" @click="toggleImageSection">
          <text class="section-title">图片预览</text>
          <text class="expand-icon">{{ imageSectionExpanded ? '▼' : '▶' }}</text>
        </view>
        <view v-if="imageSectionExpanded" class="section-content">
          <view class="upload-area" @click="chooseImage">
            <image v-if="imagePath" :src="imagePath" class="preview-image" mode="aspectFit" />
            <view v-else class="upload-placeholder">
              <text class="upload-icon">📷</text>
              <text>点击选择图片</text>
            </view>
          </view>
        </view>
      </view>
      
      <button v-if="imagePath && ocrResultList.length === 0" class="recognize-btn" @click="recognizeImage" :loading="recognizing">
        识别账单
      </button>
      
      <!-- OCR识别结果列表（可折叠） -->
      <view v-if="ocrResultList.length > 0" class="ocr-result-list-container">
        <!-- 新增的直接存储按钮 -->
        <button class="direct-save-btn" @click="saveBillsDirectly">直接存储</button>
        
        <view class="section-header" @click="toggleResultList">
          <text class="list-title">识别到 {{ ocrResultList.length }} 条账单记录</text>
          <text class="expand-icon">{{ resultListExpanded ? '▼' : '▶' }}</text>
        </view>
        <view v-if="resultListExpanded" class="section-content">
          <view class="result-list-wrapper">
            <view v-for="(item, index) in ocrResultList" :key="index" class="result-card" :class="{ active: selectedOcrResult === item, expanded: expandedCards.has(index) }">
              <view class="result-header" @click.stop="toggleCard(index)">
                <view class="header-left">
                  <text class="result-index">#{{ index + 1 }}</text>
                  <text class="result-value amount summary">¥{{ item.amount }}</text>
                </view>
                <view class="header-right">
                  <text v-if="item.confidence" class="result-confidence">置信度: {{ (item.confidence * 100).toFixed(1) }}%</text>
                  <text class="expand-icon">{{ expandedCards.has(index) ? '▼' : '▶' }}</text>
                </view>
              </view>
              <view v-if="expandedCards.has(index)" class="result-details">
                <view class="result-item">
                  <text class="result-label">金额：</text>
                  <text class="result-value amount">¥{{ item.amount ? Math.abs(item.amount).toFixed(2) : '0.00' }}</text>
                </view>
                <view v-if="item.merchant" class="result-item">
                  <text class="result-label">商户：</text>
                  <text class="result-value">{{ item.merchant }}</text>
                </view>
                <view v-if="item.date" class="result-item">
                  <text class="result-label">日期：</text>
                  <text class="result-value">{{ formatOcrDate(item.date) }}</text>
                </view>
                <button class="select-btn" @click.stop="goToManualWithOcrData(item)">选择此条</button>
              </view>
            </view>
          </view>
        </view>
      </view>
      
    </view>
    </view>
  </scroll-view>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import {createBill, createBillsBatch} from '../../api/bill'
import { getCategories } from '../../api/category'
import { recognizeImage as ocrRecognize } from '../../api/ocr'
import { formatDate } from '../../utils/date'

const activeTab = ref('manual')
const categories = ref([])
const selectedCategory = ref(null)
const imagePath = ref('')
const recognizing = ref(false)
const ocrResultList = ref([])
const selectedOcrResult = ref(null)
const expandedCards = ref(new Set()) // 存储展开的卡片索引
const imageSectionExpanded = ref(true) // 图片预览区域展开状态
const resultListExpanded = ref(true) // 结果列表展开状态
const pageScrollTop = ref(0) // 页面滚动位置

const form = ref({
  type: 1, // 1-支出，2-收入，默认支出
  categoryId: null,
  amount: '',
  description: '',
  billDate: formatDate(new Date()),
  imageUrl: ''
})

const loadCategories = async () => {
  try {
    // 根据账单类型加载对应的分类（1-支出，2-收入）
    const type = form.value.type || 1
    const data = await getCategories(type)
    categories.value = data || []
    // 如果当前选中的分类不在新类型中，清空选择
    if (selectedCategory.value && selectedCategory.value.type !== type) {
      selectedCategory.value = null
      form.value.categoryId = null
    }
  } catch (error) {
    console.error('加载分类失败', error)
  }
}

// 监听账单类型变化，重新加载分类
watch(() => form.value.type, (newType) => {
  if (newType) {
    loadCategories()
  }
})

// 监听账单类型变化，重新加载分类
watch(() => form.value.type, (newType) => {
  if (newType) {
    loadCategories()
  }
})

const chooseImage = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['camera', 'album'],
    success: (res) => {
      imagePath.value = res.tempFilePaths[0]
      ocrResultList.value = []
      selectedOcrResult.value = null
      expandedCards.value.clear()
      form.value.amount = ''
    }
  })
}

const recognizeImage = async () => {
  if (!imagePath.value) {
    uni.showToast({
      title: '请先选择图片',
      icon: 'none'
    })
    return
  }
  
  recognizing.value = true
  try {
    const resultList = await ocrRecognize(imagePath.value)
    // 后端返回的是数组
    ocrResultList.value = Array.isArray(resultList) ? resultList : [resultList]
    
    if (ocrResultList.value.length === 0) {
      uni.showToast({
        title: '未识别到账单信息',
        icon: 'none'
      })
    } else {
      uni.showToast({
        title: `识别到 ${ocrResultList.value.length} 条记录`,
        icon: 'success'
      })
      // 如果只有一条，自动选中并展开
      if (ocrResultList.value.length === 1) {
        expandedCards.value.add(0)
        selectOcrResult(ocrResultList.value[0])
      } else if (ocrResultList.value.length > 0) {
        // 多条记录时，默认展开第一条
        expandedCards.value.add(0)
      }
    }
  } catch (error) {
    console.error('OCR识别失败:', error)
    uni.showToast({
      title: error.message || '识别失败，请重试',
      icon: 'none'
    })
  } finally {
    recognizing.value = false
  }
}

const scrollTop = ref(0)

const toggleImageSection = () => {
  imageSectionExpanded.value = !imageSectionExpanded.value
}

const toggleResultList = () => {
  resultListExpanded.value = !resultListExpanded.value
}

const toggleCard = (index) => {
  if (expandedCards.value.has(index)) {
    expandedCards.value.delete(index)
  } else {
    expandedCards.value.add(index)
  }
}

const goToManualWithOcrData = (item) => {
  // 根据金额正负判断账单类型
  // 负数 -> 支出（type: 1），正数 -> 收入（type: 2）
  const amount = parseFloat(item.amount) || 0
  const billType = amount < 0 ? 1 : 2 // 1-支出，2-收入
  
  // 保存OCR识别数据到本地存储
  const ocrData = {
    amount: Math.abs(amount).toString(), // 金额取绝对值
    billDate: item.date ? formatOcrDate(item.date) : formatDate(new Date()),
    description: item.merchant || '',
    type: billType, // 根据金额正负设置类型
    fromOcr: true
  }
  uni.setStorageSync('ocrFormData', ocrData)
  
  // 切换到手动记账页面
  activeTab.value = 'manual'
  
  // 填充表单数据
  form.value.amount = ocrData.amount
  form.value.billDate = ocrData.billDate
  form.value.description = ocrData.description
  form.value.type = billType // 根据金额正负自动设置类型
  
  // 加载对应类型的分类
  loadCategories()
  
  uni.showToast({
    title: `已填充到手动记账（${billType === 1 ? '支出' : '收入'}）`,
    icon: 'success'
  })
}

const formatOcrDate = (dateStr) => {
  if (!dateStr) return formatDate(new Date())
  // 如果已经是 YYYY-MM-DD 格式，直接返回
  if (typeof dateStr === 'string' && dateStr.match(/^\d{4}-\d{2}-\d{2}/)) {
    return dateStr.split('T')[0] // 处理 LocalDateTime 格式
  }
  // 尝试解析日期
  try {
    const date = new Date(dateStr)
    return formatDate(date)
  } catch (e) {
    return formatDate(new Date())
  }
}

const selectCategory = (category) => {
  selectedCategory.value = category
  form.value.categoryId = category.id
}

const onDateChange = (e) => {
  form.value.billDate = e.detail.value
}

const onAmountInput = (e) => {
  // 确保只能输入数字和小数点
  let value = e.detail.value || ''
  // 移除非数字和小数点的字符
  value = value.replace(/[^\d.]/g, '')
  // 确保只有一个小数点
  const parts = value.split('.')
  if (parts.length > 2) {
    value = parts[0] + '.' + parts.slice(1).join('')
  }
  // 限制小数位数为2位
  if (parts.length === 2 && parts[1].length > 2) {
    value = parts[0] + '.' + parts[1].substring(0, 2)
  }
  form.value.amount = value
}

const onAmountFocus = () => {
  // 聚焦时确保输入框可交互
  console.log('金额输入框聚焦')
}

const submitBill = async () => {
  if (!form.value.type) {
    uni.showToast({
      title: '请选择账单类型',
      icon: 'none'
    })
    return
  }
  
  if (!form.value.categoryId) {
    uni.showToast({
      title: '请选择分类',
      icon: 'none'
    })
    return
  }
  
  if (!form.value.amount || parseFloat(form.value.amount) <= 0) {
    uni.showToast({
      title: '请输入有效金额',
      icon: 'none'
    })
    return
  }
  
  try {
    await createBill({
      type: form.value.type,
      categoryId: form.value.categoryId,
      amount: parseFloat(form.value.amount),
      description: form.value.description,
      billDate: form.value.billDate,
      imageUrl: form.value.imageUrl
    })
    
    uni.showToast({
      title: '保存成功',
      icon: 'success'
    })
    
    // 重置表单
    form.value = {
      type: 1,
      categoryId: null,
      amount: '',
      description: '',
      billDate: formatDate(new Date()),
      imageUrl: ''
    }
    selectedCategory.value = null
    ocrResultList.value = []
    selectedOcrResult.value = null
    expandedCards.value.clear()
    imagePath.value = ''
    
    // 重新加载分类
    loadCategories()
    
    // 触发首页刷新
    uni.$emit('refreshBills')
    
    // 跳转到首页
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/index/index'
      })
    }, 1500)
  } catch (error) {
    console.error('保存失败:', error)
    uni.showToast({
      title: error.message || '保存失败',
      icon: 'none'
    })
  }
}

const saveBillsDirectly = async () => {
  if (ocrResultList.value.length === 0) {
    uni.showToast({
      title: '没有账单记录可以保存',
      icon: 'none'
    })
    return
  }

  try {
    // 构造符合要求的数据格式
    const bills = ocrResultList.value.map(item => {
      // 根据金额正负判断账单类型
      const amount = parseFloat(item.amount) || 0
      const billType = amount < 0 ? 1 : 2 // 1-支出，2-收入
      
      return {
        categoryId: 3, // 默认分类ID为3
        amount: Math.abs(amount), // 金额取绝对值
        description: item.merchant || '购物', // 如果没有商户名，默认为"购物"
        billDate: item.date ? formatOcrDate(item.date) : formatDate(new Date()), // 如果没有日期，使用今天
        type: billType
      }
    })

    // 发送批量保存请求
    // const response = await fetch('/api/bills/batch', {
    //   method: 'POST',
    //   headers: {
    //     'Content-Type': 'application/json'
    //   },
    //   body: JSON.stringify({ bills })
    // })

    const response = await createBillsBatch(JSON.stringify({ bills }))

    console.log('保存结果:', response)

    if (response.length) {
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      })
      // 触发首页刷新
      uni.$emit('refreshBills')
      
      // 跳转到首页
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }, 1500)
    } else {
      throw new Error('保存失败')
    }
  } catch (error) {
    console.error('保存失败:', error)
    uni.showToast({
      title: error.message || '保存失败',
      icon: 'none'
    })
  }
}

onMounted(() => {
  loadCategories()
  
  // 检查是否有OCR数据需要填充
  const ocrFormData = uni.getStorageSync('ocrFormData')
  if (ocrFormData && ocrFormData.fromOcr) {
    form.value.amount = ocrFormData.amount || ''
    form.value.billDate = ocrFormData.billDate || formatDate(new Date())
    form.value.description = ocrFormData.description || ''
    // 清除临时数据
    uni.removeStorageSync('ocrFormData')
    // 切换到手动记账页面
    activeTab.value = 'manual'
  }
})
</script>

<style scoped>
.page-scroll {
  height: 100vh;
  width: 100%;
  padding-bottom: calc(50px + env(safe-area-inset-bottom));
  background: linear-gradient(135deg, #FFE5CC 0%, #FFD4A8 50%, #FFC08A 100%);
}

.container {
  min-height: calc(100vh - 100rpx);
  background: transparent;
  padding: 20rpx;
  padding-bottom: calc(170rpx + env(safe-area-inset-bottom));
}

.tabs {
  display: flex;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  padding: 10rpx;
  border: 3rpx solid #C8956E;
  box-shadow: 0 8rpx 20rpx rgba(200, 149, 110, 0.15);
}

.tab {
  flex: 1;
  text-align: center;
  padding: 20rpx;
  border-radius: 15rpx;
  color: #8D6E63;
  font-size: 28rpx;
  font-weight: 600;
  transition: all 0.3s;
}

.tab.active {
  background: linear-gradient(135deg, #C8956E, #F5D59E);
  color: #5D4037;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.3);
}

.form {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  padding: 40rpx 30rpx;
  border: 3rpx solid #C8956E;
  box-shadow: 0 10rpx 30rpx rgba(200, 149, 110, 0.15);
}

.form-item {
  margin-bottom: 35rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #5D4037;
  margin-bottom: 20rpx;
  font-weight: 600;
}

.type-selector-compact {
  display: flex;
  gap: 20rpx;
  justify-content: center;
}

.type-option-compact {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20rpx 40rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 15rpx;
  border: 3rpx solid #C8956E;
  transition: all 0.3s;
  min-width: 140rpx;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.1);
}

.type-option-compact.active {
  background: linear-gradient(135deg, #FFD700, #F5D59E);
  border-color: #FFD700;
  transform: scale(1.05);
  box-shadow: 0 8rpx 20rpx rgba(255, 215, 0, 0.3);
}

.type-icon-compact {
  font-size: 40rpx;
  margin-bottom: 8rpx;
  filter: drop-shadow(0 2rpx 4rpx rgba(0, 0, 0, 0.1));
}

.type-text-compact {
  font-size: 26rpx;
  color: #5D4037;
  font-weight: 600;
}

.category-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  margin-top: 10rpx;
}

.category-icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: calc(25% - 15rpx);
  margin-bottom: 10rpx;
}

.category-icon-circle {
  width: 110rpx;
  height: 110rpx;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10rpx;
  border: 3rpx solid #C8956E;
  transition: all 0.3s;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.1);
}

.category-icon-item.active .category-icon-circle {
  background: linear-gradient(135deg, #FFD700, #F5D59E);
  border-color: #FFD700;
  transform: scale(1.15);
  box-shadow: 0 8rpx 20rpx rgba(255, 215, 0, 0.4);
}

.category-icon-emoji {
  font-size: 52rpx;
  line-height: 1;
  filter: drop-shadow(0 2rpx 4rpx rgba(0, 0, 0, 0.1));
}

.category-icon-name {
  font-size: 22rpx;
  color: #5D4037;
  text-align: center;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.category-icon-item.active .category-icon-name {
  color: #FFD700;
  font-weight: bold;
  text-shadow: 0 2rpx 4rpx rgba(255, 215, 0, 0.3);
}

.category-empty {
  text-align: center;
  padding: 50rpx 0;
  color: #8D6E63;
  font-size: 26rpx;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 15rpx;
  border: 2rpx dashed #C8956E;
}

.input, .textarea {
  width: 100%;
  padding: 25rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 15rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  border: 3rpx solid #C8956E;
  outline: none;
  color: #5D4037;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.1);
  transition: all 0.3s;
}

.input:focus, .textarea:focus {
  border-color: #FFD700;
  box-shadow: 0 0 0 4rpx rgba(255, 215, 0, 0.3), 0 8rpx 25rpx rgba(255, 215, 0, 0.2);
}

.input::placeholder, .textarea::placeholder {
  color: #BCAAA4;
}

.textarea {
  min-height: 180rpx;
}

.category-selector, .picker {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 15rpx;
  border: 3rpx solid #C8956E;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.1);
}

.placeholder {
  color: #BCAAA4;
}

.arrow {
  color: #C8956E;
  font-weight: bold;
}

.submit-btn {
  width: 100%;
  height: 96rpx;
  background: linear-gradient(135deg, #C8956E, #F5D59E, #FFD700);
  color: #5D4037;
  border-radius: 50rpx;
  font-size: 32rpx;
  font-weight: bold;
  margin-top: 40rpx;
  margin-bottom: 20rpx;
  border: 4rpx solid #5D4037;
  box-shadow: 0 10rpx 30rpx rgba(200, 149, 110, 0.4);
  transition: all 0.3s;
}

.submit-btn:active {
  transform: scale(0.98);
  box-shadow: 0 5rpx 15rpx rgba(200, 149, 110, 0.3);
}

.ocr-container {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 25rpx;
  padding: 30rpx;
  border: 3rpx solid #C8956E;
  box-shadow: 0 10rpx 30rpx rgba(200, 149, 110, 0.15);
}

.image-section {
  margin-bottom: 30rpx;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 20rpx;
  overflow: hidden;
  border: 3rpx solid #C8956E;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx;
  background: linear-gradient(135deg, rgba(200, 149, 110, 0.15), rgba(255, 215, 0, 0.15));
  border-radius: 17rpx;
  cursor: pointer;
  user-select: none;
  transition: background 0.3s;
}

.section-header:active {
  background: linear-gradient(135deg, rgba(200, 149, 110, 0.25), rgba(255, 215, 0, 0.25));
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #5D4037;
}

.list-title {
  font-size: 28rpx;
  color: #5D4037;
  font-weight: bold;
}

.section-content {
  padding: 25rpx;
  animation: slideDown 0.3s ease-out;
}

.upload-area {
  width: 100%;
  height: 400rpx;
  border: 3rpx dashed #C8956E;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 30rpx;
  background: rgba(255, 255, 255, 0.5);
  transition: all 0.3s;
}

.upload-area:active {
  border-color: #FFD700;
  background: rgba(255, 215, 0, 0.1);
}

.preview-image {
  width: 100%;
  height: 100%;
  border-radius: 17rpx;
}

.upload-placeholder {
  text-align: center;
  color: #8D6E63;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.upload-icon {
  font-size: 100rpx;
  display: block;
  margin-bottom: 20rpx;
  filter: drop-shadow(0 4rpx 8rpx rgba(0, 0, 0, 0.1));
}

.recognize-btn {
  width: 100%;
  height: 96rpx;
  background: linear-gradient(135deg, #19BE6B, #51CF66);
  color: #fff;
  border-radius: 50rpx;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
  border: 4rpx solid #108043;
  box-shadow: 0 10rpx 30rpx rgba(25, 190, 107, 0.3);
  transition: all 0.3s;
}

.recognize-btn:active {
  transform: scale(0.98);
}

.direct-save-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #FFD700, #F5D59E);
  color: #5D4037;
  border-radius: 50rpx;
  font-size: 30rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  border: 3rpx solid #5D4037;
  box-shadow: 0 8rpx 25rpx rgba(255, 215, 0, 0.3);
  transition: all 0.3s;
}

.direct-save-btn:active {
  transform: scale(0.98);
}

.ocr-result-list-container {
  margin-top: 30rpx;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 20rpx;
  overflow: hidden;
  border: 3rpx solid #C8956E;
}

.result-list-wrapper {
  max-height: 600rpx;
  overflow-y: auto;
  padding: 0 20rpx 20rpx 20rpx;
}

.result-card {
  background: rgba(255, 255, 255, 0.95);
  border: 3rpx solid #C8956E;
  border-radius: 20rpx;
  padding: 25rpx;
  margin-bottom: 20rpx;
  transition: all 0.3s;
  overflow: hidden;
  box-shadow: 0 4rpx 15rpx rgba(200, 149, 110, 0.1);
}

.result-card.active {
  border-color: #FFD700;
  background: linear-gradient(135deg, rgba(255, 215, 0, 0.1), rgba(245, 213, 158, 0.1));
  box-shadow: 0 8rpx 20rpx rgba(255, 215, 0, 0.3);
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

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

.header-right {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.result-index {
  font-size: 24rpx;
  color: #FFD700;
  font-weight: bold;
  margin-right: 15rpx;
  background: #5D4037;
  padding: 6rpx 12rpx;
  border-radius: 10rpx;
}

.result-value.amount.summary {
  font-size: 32rpx;
  font-weight: bold;
  color: #FA3534;
}

.result-confidence {
  font-size: 22rpx;
  color: #8D6E63;
}

.expand-icon {
  font-size: 24rpx;
  color: #C8956E;
  transition: transform 0.3s;
  margin-left: 10rpx;
  font-weight: bold;
}

.result-details {
  margin-top: 25rpx;
  padding-top: 25rpx;
  border-top: 2rpx solid rgba(200, 149, 110, 0.3);
  animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
  from {
    opacity: 0;
    max-height: 0;
  }
  to {
    opacity: 1;
    max-height: 500rpx;
  }
}

.result-item {
  display: flex;
  padding: 12rpx 0;
  align-items: center;
}

.result-label {
  font-size: 26rpx;
  color: #8D6E63;
  width: 100rpx;
  flex-shrink: 0;
  font-weight: 600;
}

.result-value {
  font-size: 28rpx;
  color: #5D4037;
  flex: 1;
}

.result-value.amount {
  font-weight: bold;
  color: #FA3534;
  font-size: 32rpx;
}

.select-btn {
  width: 100%;
  height: 76rpx;
  background: linear-gradient(135deg, #C8956E, #F5D59E);
  color: #5D4037;
  border-radius: 50rpx;
  font-size: 28rpx;
  font-weight: bold;
  margin-top: 20rpx;
  border: 3rpx solid #5D4037;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6rpx 18rpx rgba(200, 149, 110, 0.3);
  transition: all 0.3s;
}

.select-btn:active {
  transform: scale(0.98);
}

.ocr-result {
  margin-top: 30rpx;
  padding-top: 30rpx;
  border-top: 3rpx solid #C8956E;
}
</style>