<template>
  <div class="app-page result-workbench">
    <div class="data-toolbar">
      <span class="data-toolbar__title">我的绩效</span>
      <span class="data-toolbar__sep">|</span>
      <span class="data-toolbar__meta">{{ data.period?.name || '—' }}</span>
      <span v-if="data.period" class="data-toolbar__meta">
        {{ data.period.startDate }} ~ {{ data.period.endDate }}
      </span>
      <span class="data-toolbar__spacer"></span>
      <el-button size="small" @click="$router.push('/scores/history')">历史记录</el-button>
    </div>

    <PeriodBanner v-if="periods.length" :periods="periods" v-model="periodId" @update:model-value="load" />

    <div v-if="!data.period" class="empty-state">暂无绩效数据，请选择周期</div>

    <!-- SELF-ASSESSMENT FORM (ASSESSING + no self submitted) -->
    <template v-else-if="showSelfForm">
      <div class="form-banner">请完成绩效自评后提交，上级将在此基础进行终审</div>

      <div class="form-card">
        <div class="form-section-title">OKR 目标达成自评 <span class="fst-sub">逐条 KR 打分（0.0-1.0），满分 60</span></div>
        <div v-if="okrRecords.length" class="kr-score-list">
          <div v-for="(kr, i) in okrRecords[0].keyResults" :key="kr.id" class="kr-score-row">
            <div class="ksr-label">
              <span class="ksr-num">KR{{ i+1 }}</span>
              <span class="ksr-title">{{ kr.title }}</span>
              <span class="ksr-progress">{{ kr.currentValue }}/{{ kr.targetValue }} ({{ kr.status }})</span>
            </div>
            <div class="ksr-inputs">
              <el-select v-model="krScores[i].score" size="default" style="width:120px">
                <el-option label="1.0 · 卓越" :value="1.0" />
                <el-option label="0.8 · 良好" :value="0.8" />
                <el-option label="0.6 · 达标" :value="0.6" />
                <el-option label="0.3 · 少量推进" :value="0.3" />
                <el-option label="0.0 · 未做" :value="0.0" />
              </el-select>
              <span class="ksr-eq">= {{ (krScores[i].score * 60 / okrRecords[0].keyResults.length).toFixed(1) }} 分</span>
            </div>
          </div>
        </div>
        <div v-else class="no-okr-hint">暂无 OKR 记录</div>
        <div class="okr-subtotal">
          OKR 得分 = 平均 {{ krAvg.toFixed(2) }} × 60 = <strong>{{ okrTotal.toFixed(1) }} / 60</strong>
        </div>
      </div>

      <div class="form-card">
        <div class="form-section-title">绩效自评 <span class="fst-sub">按维度自述 + 打分，满分 40</span></div>
        <div v-for="(dim, i) in dimScores" :key="i" class="dim-row">
          <div class="dim-head">
            <span class="dim-num">{{ i+1 }}</span>
            <span class="dim-name">{{ dim.name }}</span>
            <span class="dim-max">满分 {{ dim.maxScore }} 分</span>
          </div>
          <el-input v-model="dim.description" type="textarea" :rows="2" :placeholder="'简述' + dim.name + '方面的绩效表现'" style="margin-bottom:6px" />
          <el-input-number v-model="dim.score" :min="0" :max="dim.maxScore" :precision="1" size="small" controls-position="right" style="width:130px" />
          <span class="dim-score-hint">/ {{ dim.maxScore }} 分</span>
        </div>
        <div class="okr-subtotal">
          绩效得分 = <strong>{{ dimTotal.toFixed(1) }} / 40</strong>
        </div>
      </div>

      <div class="form-card">
        <div class="form-section-title">专项加减分</div>
        <div class="bonus-row">
          <span class="bonus-label">加分（上限5）</span>
          <el-input-number v-model="form.bonus" :min="0" :max="5" :precision="1" size="large" controls-position="right" />
          <span class="bonus-label">扣分</span>
          <el-input-number v-model="form.penalty" :min="0" :precision="1" size="large" controls-position="right" />
        </div>
        <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="加减分说明" style="margin-top:8px" />
      </div>

      <div class="result-preview">
        <div class="rp-formula">OKR（{{ okrTotal.toFixed(1) }}）+ 绩效（{{ dimTotal.toFixed(1) }}）+ 加分（{{ form.bonus }}）− 扣分（{{ form.penalty }}）</div>
        <div class="rp-score">{{ computedTotal }} <small>/ 100</small></div>
        <div class="rp-grade">自评评级 <strong>{{ computedGrade }}</strong></div>
      </div>

      <div class="submit-row">
        <el-button type="primary" size="large" @click="handleSelfSubmit" :loading="submitting" round>
          提交自评
        </el-button>
        <span class="submit-hint">提交后由直属上级审核终评</span>
      </div>
    </template>

    <!-- RESULT VIEW (has self or final score) -->
    <div v-else class="dashboard">
      <div v-if="data.finalScore" class="result-grid">
        <div class="result-grid__main">
          <div class="uppercase-label">本期终评</div>
          <div class="result-score">{{ data.finalScore.total }}</div>
          <div class="result-score-max">/ 100 分</div>
          <div class="result-grade-tag">
            <span class="status-dot" :class="gradeDotClass(data.finalScore.grade)"></span>
            <span class="result-grade-letter">{{ data.finalScore.grade }}</span>
            <span class="result-grade-desc">{{ gradeDesc(data.finalScore.grade) }}</span>
          </div>
          <div class="result-vs-last">
            <div class="uppercase-label">较上期</div>
            <div :style="{ color: vsLastColor }">{{ vsLastText }}</div>
          </div>
        </div>

        <div class="result-grid__breakdown">
          <div class="uppercase-label result-breakdown-title">得分构成</div>
          <div class="metric-bar">
            <div class="metric-bar__head">
              <span class="metric-bar__label">OKR 目标达成</span>
              <span class="metric-bar__value">
                {{ data.finalScore.okrScore }}
                <span class="metric-bar__value--muted">/ 60</span>
              </span>
            </div>
            <div class="metric-bar__track">
              <div class="metric-bar__fill metric-bar__fill--brand" :style="{ width: metricPercent(data.finalScore.okrScore, 60) }"></div>
            </div>
          </div>

          <div class="metric-bar">
            <div class="metric-bar__head">
              <span class="metric-bar__label">绩效维度</span>
              <span class="metric-bar__value">
                {{ data.finalScore.kpiScore }}
                <span class="metric-bar__value--muted">/ 40</span>
              </span>
            </div>
            <div class="metric-bar__track">
              <div class="metric-bar__fill metric-bar__fill--accent" :style="{ width: metricPercent(data.finalScore.kpiScore, 40) }"></div>
            </div>
          </div>

          <div class="metric-bar">
            <div class="metric-bar__head">
              <span class="metric-bar__label">专项加分</span>
              <span class="metric-bar__value result-positive">+{{ data.finalScore.bonus }}</span>
            </div>
            <div class="metric-bar__track">
              <div class="metric-bar__fill metric-bar__fill--success" :style="{ width: metricPercent(data.finalScore.bonus, 5) }"></div>
            </div>
          </div>

          <div class="metric-bar">
            <div class="metric-bar__head">
              <span class="metric-bar__label">专项扣分</span>
              <span class="metric-bar__value result-negative">-{{ data.finalScore.penalty }}</span>
            </div>
            <div class="metric-bar__track">
              <div class="metric-bar__fill metric-bar__fill--danger" :style="{ width: metricPercent(data.finalScore.penalty, 5) }"></div>
            </div>
          </div>
        </div>
      </div>

      <div v-else-if="data.selfScore" class="result-pending-card">
        <div class="pending-icon pending-icon--submitted" aria-hidden="true">
          <svg viewBox="0 0 48 48" focusable="false">
            <path d="M12 34h24M16 26l8-8 8 8M24 18v20M14 10h20a4 4 0 0 1 4 4v4M10 18v-4a4 4 0 0 1 4-4" />
          </svg>
        </div>
        <div>
          <div class="result-score result-score--pending">{{ data.selfScore.total }}</div>
          <div class="result-score-max">/ 100 分 · 自评已提交，等待上级终审</div>
        </div>
      </div>

      <div v-else class="score-pending">
        <div class="pending-icon"></div>
        <div class="pending-text">绩效考核尚未完成</div>
      </div>

      <div v-if="data.selfScore && data.finalScore" class="result-compare">
        <div class="uppercase-label result-compare-title">自评 vs 终评</div>
        <div class="result-compare-grid">
          <div v-if="hasSelfScoreBreakdown">
            <div class="uppercase-label">OKR</div>
            <div>
              <span class="compare-self">自评 {{ formatScore(data.selfScore.okrScore) }}</span>
              <span class="compare-separator">至</span>
              <span class="compare-final">终评 {{ formatScore(data.finalScore.okrScore) }}</span>
            </div>
          </div>
          <div v-if="hasSelfScoreBreakdown">
            <div class="uppercase-label">绩效</div>
            <div>
              <span class="compare-self">自评 {{ formatScore(data.selfScore.kpiScore) }}</span>
              <span class="compare-separator">至</span>
              <span class="compare-final">终评 {{ formatScore(data.finalScore.kpiScore) }}</span>
            </div>
          </div>
          <div>
            <div class="uppercase-label">总分</div>
            <div>
              <span class="compare-self">自评 {{ formatScore(data.selfScore.total) }}</span>
              <span class="compare-separator">至</span>
              <span class="compare-final">终评 {{ formatScore(data.finalScore.total) }}</span>
            </div>
          </div>
        </div>
      </div>

      <div v-if="data.finalScore" class="result-actions">
        <el-button
          v-if="!data.finalScore.confirmedAt"
          class="result-action--primary"
          @click="handleConfirm"
          :loading="confirming"
        >
          确认结果
        </el-button>
        <el-button v-else disabled>已确认</el-button>
        <el-button @click="handleAppeal" :loading="appealing">提起申诉</el-button>
        <el-button @click="$router.push('/feedback')">查看面谈记录</el-button>
        <span v-if="data.finalScore.confirmedAt" class="data-toolbar__meta">
          确认时间 {{ data.finalScore.confirmedAt?.slice(0, 16) }}
        </span>
      </div>

      <div v-if="data.finalScore" class="result-trend">
        <div class="uppercase-label result-trend-title">历史趋势（{{ historyList.length + 1 }} 期）</div>
        <div v-if="historyList.length" class="history-trend">
          <div v-for="history in historyList" :key="history.periodId" class="history-trend__item">
            <div class="history-trend__bar" :style="{ height: barHeight(history.total) + 'px' }"></div>
            <span class="uppercase-label">{{ history.periodName }}</span>
            <span class="history-score">{{ formatScore(history.total) }}</span>
          </div>
          <div class="history-trend__item">
            <div
              class="history-trend__bar history-trend__bar--current"
              :style="{ height: barHeight(data.finalScore.total) + 'px' }"
            ></div>
            <span class="uppercase-label history-current">{{ data.period?.name }}</span>
            <span class="history-score history-current">{{ formatScore(data.finalScore.total) }}</span>
          </div>
        </div>
        <div v-else class="history-empty">首次考评，暂无历史成绩</div>
      </div>

      <div class="two-col">
        <div class="col okr-col">
          <div class="col-head">当期 OKR 记录</div>
          <div v-for="o in data.okrRecords || []" :key="o.id" class="mini-okr">
            <div class="mini-o">{{ o.title }}</div>
            <div v-for="kr in o.keyResults" :key="kr.id" class="mini-kr">
              <span>{{ kr.title }}</span>
              <span class="mini-val">{{ kr.currentValue }}/{{ kr.targetValue }}</span>
            </div>
          </div>
        </div>
        <div class="col feedback-col">
          <div class="col-head">面谈反馈</div>
          <div v-if="data.feedback?.interviewNotes" class="fb-content">
            <div class="fb-block" v-if="data.feedback.highlights">
              <div class="fb-label">亮点</div>
              <div class="fb-text">{{ data.feedback.highlights }}</div>
            </div>
            <div class="fb-block" v-if="data.feedback.issues">
              <div class="fb-label">待改进</div>
              <div class="fb-text">{{ data.feedback.issues }}</div>
            </div>
            <div class="fb-block" v-if="data.feedback.improvements">
              <div class="fb-label">改进建议</div>
              <div class="fb-text">{{ data.feedback.improvements }}</div>
            </div>
            <div class="fb-block" v-if="data.feedback.interviewNotes">
              <div class="fb-label">面谈纪要</div>
              <div class="fb-text">{{ data.feedback.interviewNotes }}</div>
            </div>
          </div>
          <div v-else class="fb-empty">暂无面谈记录</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { periodApi, scoreApi, dimensionApi, scoreAppealApi } from '../../api'
import { ElMessage, ElMessageBox } from 'element-plus'
import PeriodBanner from '../../components/PeriodBanner.vue'

const periods = ref([])
const periodId = ref(null)
const data = ref({})
const confirming = ref(false)
const appealing = ref(false)
const submitting = ref(false)
const dimensions = ref([])
const krScores = ref([])
const dimScores = ref([])
const historyList = ref([])
const form = reactive({ bonus: 0, penalty: 0, remark: '' })

const okrRecords = computed(() => data.value.okrRecords || [])

// Show self-assessment form when ASSESSING and no self score submitted
const showSelfForm = computed(() => {
  return data.value.period?.status === 'ASSESSING' && !data.value.selfScore
})

const krAvg = computed(() => {
  if (!krScores.value.length) return 0
  const sum = krScores.value.reduce((a, b) => a + b.score, 0)
  return sum / krScores.value.length
})

const okrTotal = computed(() => {
  if (okrRecords.value.length && okrRecords.value[0].keyResults?.length) {
    const n = okrRecords.value[0].keyResults.length
    const sum = krScores.value.reduce((a, b) => a + b.score, 0)
    return Math.round(sum / n * 60 * 10) / 10
  }
  return 0
})

const dimTotal = computed(() => {
  return Math.round(dimScores.value.reduce((s, d) => s + (d.score || 0), 0) * 10) / 10
})

const computedTotal = computed(() => {
  return Math.round((okrTotal.value + dimTotal.value + form.bonus - form.penalty) * 10) / 10
})

const computedGrade = computed(() => {
  const t = computedTotal.value
  if (t >= 90) return 'S'
  if (t >= 75) return 'A'
  if (t >= 60) return 'B'
  return 'C'
})

const hasSelfScoreBreakdown = computed(() =>
  data.value.selfScore?.okrScore != null && data.value.selfScore?.kpiScore != null
)

const vsLastText = computed(() => {
  if (!historyList.value.length) return '首次考评'
  const previous = historyList.value[historyList.value.length - 1]
  const diff = Number(data.value.finalScore?.total || 0) - Number(previous.total || 0)
  if (diff > 0) return `上升 +${diff.toFixed(1)} 分`
  if (diff < 0) return `下降 ${diff.toFixed(1)} 分`
  return '持平'
})

const vsLastColor = computed(() => {
  if (!historyList.value.length) return 'var(--color-text-muted)'
  const previous = historyList.value[historyList.value.length - 1]
  const diff = Number(data.value.finalScore?.total || 0) - Number(previous.total || 0)
  if (diff > 0) return 'var(--color-success)'
  if (diff < 0) return 'var(--color-danger)'
  return 'var(--color-text-muted)'
})

onMounted(async () => {
  periods.value = await periodApi.list()
  const active = periods.value.filter(p => p.status !== 'ARCHIVED')
  if (active.length) { periodId.value = active[0].id; await load() }
})

async function load() {
  if (!periodId.value) return
  data.value = await scoreApi.getMyResult(periodId.value)

  // Load dimensions and init form state
  dimensions.value = await dimensionApi.list()
  dimScores.value = dimensions.value.map(d => ({
    id: d.id, name: d.name, maxScore: d.maxScore,
    score: 0, description: ''
  }))

  // Init KR scores from OKR records
  const okrs = data.value.okrRecords || []
  if (okrs.length && okrs[0].keyResults?.length) {
    krScores.value = okrs[0].keyResults.map(kr => ({ title: kr.title, score: 0.6 }))
  } else {
    krScores.value = []
  }

  await loadHistory()
}

async function loadHistory() {
  const archived = periods.value
    .filter(period => period.status === 'ARCHIVED' && period.id !== periodId.value)
    .sort((a, b) => String(a.endDate || '').localeCompare(String(b.endDate || '')))
    .slice(-4)

  const results = []
  for (const period of archived) {
    try {
      const result = await scoreApi.getMyResult(period.id)
      if (result.finalScore?.total != null) {
        results.push({
          periodId: period.id,
          periodName: period.name,
          total: Number(result.finalScore.total)
        })
      }
    } catch {
      // 历史周期无成绩时跳过，不影响当前结果展示。
    }
  }
  historyList.value = results
}

async function handleSelfSubmit() {
  if (dimScores.value.some(d => !d.description.trim())) {
    ElMessage.warning('请为每个维度填写绩效自述')
    return
  }
  submitting.value = true
  try {
    const dimItems = dimScores.value.map(d => ({
      dimensionId: d.id, name: d.name, maxScore: d.maxScore,
      score: d.score, description: d.description
    }))
    await scoreApi.submitSelf({
      periodId: periodId.value,
      okrScore: okrTotal.value,
      kpiScore: dimTotal.value,
      bonus: form.bonus,
      penalty: form.penalty,
      commentsJson: JSON.stringify({
        krScores: krScores.value,
        dimensions: dimItems,
        remark: form.remark
      })
    })
    ElMessage.success('自评已提交，等待上级终审')
    await load()
  } finally { submitting.value = false }
}

async function handleConfirm() {
  confirming.value = true
  try {
    await scoreApi.confirm({ periodId: periodId.value })
    ElMessage.success('绩效结果已确认，本轮流程结束')
    await load()
  } finally { confirming.value = false }
}

async function handleAppeal() {
  const { value } = await ElMessageBox.prompt('请说明你对本次绩效结果的异议', '提交绩效申诉', {
    inputType: 'textarea',
    inputPlaceholder: '请描述需要复核的分数、事实依据或沟通记录',
    confirmButtonText: '提交申诉',
    cancelButtonText: '取消'
  })
  appealing.value = true
  try {
    await scoreAppealApi.submit({ periodId: periodId.value, reason: value })
    ElMessage.success('申诉已提交，HR 将收到通知')
  } finally { appealing.value = false }
}

function gradeDesc(g) {
  return { S: '卓越产出 · 团队标杆', A: '超出预期 · 高质量完成', B: '符合预期 · 满足岗位要求', C: '未达预期 · 需整改辅导' }[g] || ''
}

function gradeDotClass(grade) {
  return {
    S: 'status-dot--active',
    A: 'status-dot--active',
    B: 'status-dot--warn',
    C: 'status-dot--danger'
  }[grade] || 'status-dot--info'
}

function metricPercent(value, max) {
  if (!max) return '0%'
  return `${Math.max(0, Math.min(100, Number(value || 0) / max * 100))}%`
}

function formatScore(value) {
  return Number(value || 0).toFixed(1)
}

function barHeight(score) {
  return Math.max(10, Math.min(48, Number(score || 0) / 100 * 48))
}
</script>

<style scoped>
.page { width: 100%; }
.empty-state { display: flex; align-items: center; justify-content: center; height: 300px; color: var(--color-text-muted); font-size: 15px; }

.form-banner { padding: 14px 20px; background: var(--color-surface-tint-info); border-radius: var(--radius-md); font-size: var(--font-size-base); color: var(--color-medical); border: 1px solid var(--color-border-soft); margin-bottom: 20px; }
.form-card { background: var(--color-surface); border-radius: var(--radius-md); padding: 20px 24px; margin-bottom: 16px; box-shadow: var(--shadow-panel); border: 1px solid var(--color-border-soft); }
.form-section-title { font-size: 15px; font-weight: 700; color: var(--color-text); margin-bottom: 16px; }
.fst-sub { font-size: var(--font-size-xs); color: var(--color-text-muted); font-weight: 400; }

.kr-score-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--color-border-soft); }
.kr-score-row:first-child { border-top: none; }
.ksr-label { flex: 1; min-width: 0; }
.ksr-num { font-size: var(--font-size-xs); font-weight: 700; color: var(--color-accent); margin-right: 6px; }
.ksr-title { font-size: var(--font-size-sm); color: var(--color-text); }
.ksr-progress { font-size: 11px; color: var(--color-text-muted); margin-left: 6px; }
.ksr-inputs { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.ksr-eq { font-size: var(--font-size-xs); color: var(--color-text-muted); white-space: nowrap; }
.no-okr-hint { font-size: var(--font-size-sm); color: var(--color-text-muted); padding: 20px 0; text-align: center; }
.okr-subtotal { text-align: right; font-size: var(--font-size-base); color: var(--color-brand); padding-top: 12px; border-top: 1px dashed var(--color-border); margin-top: 8px; }

.dim-row { padding: 14px 0; border-top: 1px solid var(--color-border-soft); }
.dim-row:first-child { border-top: none; }
.dim-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.dim-num { width: 22px; height: 22px; border-radius: 50%; background: var(--color-brand); color: var(--color-white); display: flex; align-items: center; justify-content: center; font-size: var(--font-size-xs); font-weight: 700; }
.dim-name { font-size: var(--font-size-base); font-weight: 600; color: var(--color-text); }
.dim-max, .dim-score-hint, .bonus-label, .submit-hint { font-size: var(--font-size-sm); color: var(--color-text-muted); }
.bonus-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.result-preview {
  background: var(--color-surface-tint-brand);
  border-radius: var(--radius-md);
  padding: 20px;
  text-align: center;
  border: 1px solid var(--color-border-soft);
  margin-bottom: 16px;
}
.rp-formula { font-size: var(--font-size-xs); color: var(--color-text-secondary); }
.rp-score { font-size: 40px; font-weight: 700; color: var(--color-brand); margin: 6px 0; line-height: 1; }
.rp-score small { font-size: var(--font-size-xl); color: var(--color-text-muted); font-weight: 400; }
.rp-grade { font-size: var(--font-size-base); color: var(--color-text-secondary); }
.submit-row { display: flex; align-items: center; gap: 16px; }

.dashboard { background: var(--color-surface); }
.result-grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  border-bottom: 1px solid var(--toolbar-border);
}
.result-grid__main {
  padding: 20px 18px;
  border-right: 1px solid var(--table-border);
  background: var(--table-header-bg);
}
.result-score {
  margin-top: 6px;
  color: var(--color-text);
  font-size: 40px;
  font-weight: 700;
  line-height: 1;
}
.result-score--pending { margin-top: 0; }
.result-score-max { margin-top: 2px; color: var(--color-text-muted); font-size: 11px; }
.result-grade-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding: 4px 10px;
  background: var(--section-band-bg);
  border-radius: var(--radius-sm);
}
.result-grade-letter { color: var(--section-band-color); font-weight: 700; font-size: 13px; }
.result-grade-desc { color: var(--section-band-color); font-size: 11px; }
.result-vs-last { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--table-border); }
.result-vs-last div:last-child { margin-top: 2px; font-size: 13px; font-weight: 600; }
.result-grid__breakdown { padding: 16px 18px; }
.result-breakdown-title { margin-bottom: 12px; }
.metric-bar__fill--brand { background: var(--color-brand); }
.metric-bar__fill--accent { background: var(--color-accent); }
.metric-bar__fill--success { background: var(--color-success); }
.metric-bar__fill--danger { background: var(--color-danger); }
.result-positive { color: var(--color-success); }
.result-negative { color: var(--color-danger); }
.result-pending-card {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 24px;
  background: var(--table-header-bg);
  border-bottom: 1px solid var(--toolbar-border);
}

.score-pending {
  padding: 60px;
  text-align: center;
  border-bottom: 1px solid var(--toolbar-border);
}
.pending-icon { width: 48px; height: 48px; margin: 0 auto 12px; color: var(--color-warning); }
.pending-icon svg { width: 48px; height: 48px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.result-pending-card .pending-icon { margin: 0; flex-shrink: 0; }
.pending-text { font-size: 15px; color: var(--color-text-muted); }

.result-compare {
  padding: 12px 18px;
  border-bottom: 1px solid var(--table-border);
  background: var(--table-header-bg);
}
.result-compare-title { margin-bottom: 8px; }
.result-compare-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  font-size: 11px;
}
.compare-self { color: var(--color-text-secondary); }
.compare-separator { margin: 0 6px; color: var(--color-text-muted); }
.compare-final { color: var(--color-brand); font-weight: 600; }
.result-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--toolbar-border);
}
.result-action--primary {
  background: var(--table-row-group-bg);
  border-color: var(--table-border);
  font-weight: 500;
}
.result-trend { padding: 12px 18px; border-bottom: 1px solid var(--table-border); }
.result-trend-title { margin-bottom: 12px; }
.history-score { color: var(--color-text-secondary); font-size: 10px; }
.history-current { color: var(--color-brand); font-weight: 600; }
.history-empty { padding: 24px; text-align: center; color: var(--color-text-muted); font-size: 11px; }

.two-col { display: flex; gap: 20px; }
.col { flex: 1; background: var(--color-surface); border-radius: var(--radius-md); padding: 20px 24px; box-shadow: var(--shadow-panel); border: 1px solid var(--color-border-soft); }
.col-head { font-size: 15px; font-weight: 700; color: var(--color-text); margin-bottom: 16px; }
.mini-okr { padding: 10px; margin-bottom: 10px; background: var(--color-surface-subtle); border-radius: var(--radius-md); border: 1px solid var(--color-border-soft); }
.mini-o { font-weight: 600; font-size: var(--font-size-sm); color: var(--color-brand); margin-bottom: 6px; }
.mini-kr { display: flex; justify-content: space-between; font-size: var(--font-size-xs); color: var(--color-text-secondary); padding: 2px 0; }
.mini-val { color: var(--color-text-muted); }
.fb-block { margin-bottom: 14px; }
.fb-label { font-size: 11px; font-weight: 600; color: var(--color-accent-strong); text-transform: uppercase; letter-spacing: 0; margin-bottom: 4px; }
.fb-text { font-size: var(--font-size-sm); color: var(--color-text-secondary); line-height: 1.6; }
.fb-empty { color: var(--color-text-muted); font-size: var(--font-size-sm); text-align: center; padding: 30px; }

@media (max-width: 860px) {
  .result-grid { grid-template-columns: 1fr; }
  .result-grid__main { border-right: none; border-bottom: 1px solid var(--table-border); }
  .result-compare-grid { grid-template-columns: 1fr; }
  .result-actions { align-items: flex-start; flex-direction: column; }
  .two-col { flex-direction: column; }
}
</style>
