// SS4 — Trends Tab, Dark Mode

function TabIcon({ kind, color, size = 56 }) {
  const props = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none' };
  if (kind === 'jumprope') {
    // Stylized jumping figure (placeholder per design system: SF Symbol figure.jumprope on iOS)
    return (
      <svg {...props}>
        <circle cx="13" cy="4.5" r="1.8" fill={color}/>
        <path d="M13 7 L12 13 M13 7 L15.5 11 L18 9 M12 13 L10 18 M12 13 L14.5 17.5"
          stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
        <path d="M7 7 C5 10 5 16 7 19" stroke={color} strokeWidth="1.4" strokeLinecap="round" fill="none" opacity="0.85"/>
        <path d="M19 5 C21 8 21 16 19 19" stroke={color} strokeWidth="1.4" strokeLinecap="round" fill="none" opacity="0.85"/>
      </svg>
    );
  }
  if (kind === 'history') {
    return (
      <svg {...props}>
        <circle cx="12" cy="12" r="9" stroke={color} strokeWidth="1.8" fill="none"/>
        <path d="M12 7 L12 12 L15.5 14" stroke={color} strokeWidth="1.8" strokeLinecap="round" fill="none"/>
        <path d="M3.5 8 L6 6 L8.5 8" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
      </svg>
    );
  }
  if (kind === 'chart') {
    return (
      <svg {...props}>
        <path d="M4 20 L4 4 M4 20 L20 20" stroke={color} strokeWidth="1.8" strokeLinecap="round" fill="none"/>
        <path d="M7 16 L11 11 L14 13 L19 6" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
      </svg>
    );
  }
  if (kind === 'gear') {
    return (
      <svg {...props}>
        <circle cx="12" cy="12" r="3" stroke={color} strokeWidth="1.8" fill={color}/>
        <path d="M12 2.5 L12 5 M12 19 L12 21.5 M2.5 12 L5 12 M19 12 L21.5 12 M5.2 5.2 L7 7 M17 17 L18.8 18.8 M5.2 18.8 L7 17 M17 7 L18.8 5.2"
          stroke={color} strokeWidth="1.8" strokeLinecap="round" fill="none"/>
      </svg>
    );
  }
}

function TabItem({ icon, label, selected }) {
  const color = selected ? RC.accentDark : '#94A3B8';
  return (
    <div style={{
      flex: 1, display: 'flex', flexDirection: 'column',
      alignItems: 'center', gap: 6, color,
    }}>
      <TabIcon kind={icon} color={color} size={56} />
      <div style={{ fontSize: 24, fontWeight: selected ? 600 : 500, fontFamily: RC.fontSans }}>{label}</div>
    </div>
  );
}

function MetricTile({ value, label }) {
  return (
    <div style={{
      flex: 1, background: RC.surfaceDark,
      borderRadius: 24, padding: '32px 24px',
      border: '1px solid #334155',
    }}>
      <div style={{
        fontSize: 52, fontWeight: 600, color: '#FFFFFF',
        fontFamily: RC.fontMono, fontVariantNumeric: 'tabular-nums',
        letterSpacing: -1,
      }}>{value}</div>
      <div style={{
        marginTop: 8,
        fontSize: 26, color: RC.textSecondaryDark, fontWeight: 400,
      }}>{label}</div>
    </div>
  );
}

function LineChart() {
  // Chart area inside card. Width ~1080, height ~580
  const W = 1080, H = 580;
  const padL = 100, padR = 50, padT = 60, padB = 100;
  const innerW = W - padL - padR, innerH = H - padT - padB;
  const yMin = 12, yMax = 20;
  const weeks = [14, 15, 16, 17];
  const xs = weeks.map((_, i) => padL + (innerW * i) / (weeks.length - 1));
  const ys = weeks.map(v => padT + innerH * (1 - (v - yMin) / (yMax - yMin)));
  const linePath = xs.map((x, i) => `${i === 0 ? 'M' : 'L'} ${x} ${ys[i]}`).join(' ');
  const yTicks = [12, 14, 16, 18, 20];
  return (
    <svg width={W} height={H} viewBox={`0 0 ${W} ${H}`}>
      {/* Grid */}
      {yTicks.map((t, i) => {
        const y = padT + innerH * (1 - (t - yMin) / (yMax - yMin));
        return (
          <g key={i}>
            <line x1={padL} y1={y} x2={W - padR} y2={y}
              stroke="#334155" strokeWidth="1" strokeDasharray={t === yMax ? '0' : '4 6'} opacity="0.7"/>
            <text x={padL - 18} y={y + 10} fill="#94A3B8" fontSize="26" textAnchor="end"
              fontFamily={RC.fontMono} fontWeight="500">{t}</text>
          </g>
        );
      })}
      {/* Soft accent area under line */}
      <path d={`${linePath} L ${xs[xs.length - 1]} ${padT + innerH} L ${xs[0]} ${padT + innerH} Z`}
        fill={RC.accentDark} fillOpacity="0.08"/>
      {/* Line */}
      <path d={linePath} stroke={RC.accentDark} strokeWidth="4"
        strokeLinecap="round" strokeLinejoin="round" fill="none"/>
      {/* Dots */}
      {xs.map((x, i) => (
        <g key={i}>
          <circle cx={x} cy={ys[i]} r="14" fill="#0B1120" stroke={RC.examMidDark} strokeWidth="5"/>
          <circle cx={x} cy={ys[i]} r="6" fill={RC.examMidDark}/>
        </g>
      ))}
      {/* X labels */}
      {['第1周', '第2周', '第3周', '第4周'].map((lbl, i) => (
        <text key={i} x={xs[i]} y={padT + innerH + 50} fill="#94A3B8"
          fontSize="26" textAnchor="middle" fontFamily={RC.fontSans} fontWeight="500">{lbl}</text>
      ))}
    </svg>
  );
}

function SS4Trends() {
  return (
    <ScreenshotShell
      dark={true}
      headline="每次练习，积累成绩"
      subline="中考分数趋势，一眼看清进步轨迹"
      caption="历史记录完整保留 · 随时回看"
    >
      <div style={{ position: 'absolute', inset: 0, background: RC.bgDark, overflow: 'hidden' }}>
        <StatusBar dark={true} />

        {/* Nav title */}
        <div style={{
          position: 'absolute', top: 160, left: 56, right: 56,
        }}>
          <div style={{
            fontSize: 68, fontWeight: 700, color: '#FFFFFF',
            letterSpacing: -1.5, fontFamily: RC.fontSans,
          }}>趋势</div>
        </div>

        {/* Range picker — segmented */}
        <div style={{
          position: 'absolute', top: 290, left: 56, right: 56,
          padding: 6,
          background: RC.surfaceDark,
          borderRadius: 18,
          display: 'flex', gap: 4,
        }}>
          <div style={{
            flex: 1, height: 82, borderRadius: 14,
            background: 'transparent',
            color: RC.textSecondaryDark,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 34, fontWeight: 500, fontFamily: RC.fontSans,
          }}>周</div>
          <div style={{
            flex: 1, height: 82, borderRadius: 14,
            background: 'rgba(52,211,153,0.16)',
            color: RC.accentDark,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 34, fontWeight: 600, fontFamily: RC.fontSans,
            border: `1px solid ${RC.accentDark}55`,
          }}>月</div>
        </div>

        {/* Metric tiles */}
        <div style={{
          position: 'absolute', top: 444, left: 56, right: 56,
          display: 'flex', gap: 16,
        }}>
          <MetricTile value="2,847" label="总次数" />
          <MetricTile value="48 分" label="总时长" />
          <MetricTile value="128" label="均速 个/分" />
        </div>

        {/* Exam Score Trend card */}
        <div style={{
          position: 'absolute', top: 720, left: 56, right: 56,
          background: RC.surfaceDark,
          borderRadius: 28, padding: 40,
          border: '1px solid #334155',
        }}>
          <div style={{
            display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          }}>
            <div>
              <div style={{
                fontSize: 38, fontWeight: 600, color: '#FFFFFF',
                fontFamily: RC.fontSans, letterSpacing: -0.5,
              }}>中考分数趋势</div>
              <div style={{
                marginTop: 8,
                fontSize: 28, color: RC.textSecondaryDark, fontWeight: 400,
              }}>本月最高 <span style={{ fontFamily: RC.fontMono, color: '#FFFFFF', fontWeight: 600 }}>17</span> 分 · 稳步提升中</div>
            </div>
            <div style={{
              padding: '14px 24px',
              borderRadius: 9999,
              background: 'rgba(16,185,129,0.16)',
              color: RC.examMidDark,
              fontSize: 30, fontWeight: 600,
              fontFamily: RC.fontSans,
              letterSpacing: -0.2,
              border: `1px solid ${RC.examMidDark}40`,
            }}>17 分 / 满分 20</div>
          </div>

          {/* Chart */}
          <div style={{ marginTop: 32 }}>
            <LineChart />
          </div>
        </div>

        {/* Tab bar */}
        <div style={{
          position: 'absolute', left: 0, right: 0, bottom: 0,
          height: 220,
          background: 'rgba(11,17,32,0.92)',
          borderTop: '1px solid #1E293B',
          backdropFilter: 'blur(20px)',
          paddingTop: 22, paddingBottom: 70,
          display: 'flex',
        }}>
          <TabItem icon="jumprope" label="测量" selected={false} />
          <TabItem icon="history" label="历史" selected={false} />
          <TabItem icon="chart" label="趋势" selected={true} />
          <TabItem icon="gear" label="设置" selected={false} />
        </div>
      </div>
    </ScreenshotShell>
  );
}

window.SS4Trends = SS4Trends;
