/* ── Sections (Lower-Crossed diagram, lists, exercises, etc.) ─────── */

/* ── State toggle: 3-button pill ───────────────────────────────── */
function StateToggle({ phase, setPhase }) {
  return (
    <div style={{
      display: 'flex',
      background: 'var(--paper-2)',
      border: '1px solid var(--line-soft)',
      borderRadius: '999px',
      padding: '4px',
      gap: '2px',
    }}>
      {STATES.map((s, i) => {
        const info = STATE_INFO[s];
        const active = phase === s;
        return (
          <button key={s} onClick={() => setPhase(s)} style={{
            flex: 1,
            padding: '10px 6px',
            borderRadius: '999px',
            background: active ? 'var(--ink)' : 'transparent',
            color: active ? 'var(--paper)' : 'var(--ink-2)',
            transition: 'all .25s',
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '1px',
            position: 'relative',
          }}>
            <div style={{ display: 'flex', alignItems: 'baseline', gap: '6px' }}>
              <span className="news" style={{ fontSize: 11, opacity: .65 }}>{`0${i+1}`}</span>
              <span style={{ fontSize: 14, fontWeight: 600 }}>{info.cn}</span>
            </div>
            <span className="news" style={{ fontSize: 10, opacity: .65 }}>
              {info.en}
            </span>
          </button>
        );
      })}
    </div>
  );
}

/* ── Selected muscle info card ─────────────────────────────────── */
function MuscleCard({ id, phase, onClose }) {
  if (!id) {
    return (
      <div style={{
        marginTop: 14,
        padding: '14px 16px',
        background: 'var(--bg-warm)',
        border: '1px dashed var(--line)',
        borderRadius: 12,
        color: 'var(--muted)',
        fontSize: 12,
        textAlign: 'center',
        fontFamily: '"Newsreader", serif',
        fontStyle: 'italic'
      }}>
        ↑ tap a muscle band to inspect / 点击肌肉查看详情
      </div>
    );
  }
  const m = MUSCLES[id];
  const stateName = m.states[phase];
  const sty = STATE_STYLE[stateName];
  return (
    <div className="fade-up" key={id+phase} style={{
      marginTop: 14,
      padding: '16px 18px',
      background: 'var(--paper)',
      border: '1px solid var(--line-soft)',
      borderRadius: 14,
      position: 'relative',
      boxShadow: '0 8px 22px -10px rgba(33,24,16,.15)'
    }}>
      <button onClick={onClose} style={{
        position: 'absolute', top: 10, right: 12,
        width: 22, height: 22, borderRadius: 999,
        color: 'var(--muted)', fontSize: 14
      }}>✕</button>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 6 }}>
        <span style={{
          width: 12, height: 12, borderRadius: 999,
          background: sty.fill, boxShadow: `0 0 0 3px ${sty.ring}55`
        }}/>
        <div className="serif" style={{ fontSize: 19, fontWeight: 700 }}>{m.cn}</div>
        <div className="news" style={{ fontSize: 13, color: 'var(--muted)' }}>
          {m.en}
        </div>
      </div>
      <div style={{
        display: 'inline-block',
        padding: '3px 10px',
        background: sty.fill,
        color: 'var(--paper)',
        borderRadius: 999,
        fontSize: 11,
        fontWeight: 600,
        letterSpacing: '.05em',
        marginBottom: 10
      }}>
        {sty.zhCn} · {m.chain}
      </div>
      <Field label="功能" en="Function" v={m.fn}/>
      <Field label="角色" en="Role" v={m.role}/>
      <Field label="附着" en="Attachment" v={m.attach} mono/>
    </div>
  );
}
function Field({ label, en, v, mono }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '64px 1fr',
      gap: 10, marginTop: 8,
      paddingTop: 8, borderTop: '1px dotted var(--line)'
    }}>
      <div>
        <div style={{ fontSize: 11, color: 'var(--ink-2)', fontWeight: 600 }}>{label}</div>
        <div className="news" style={{ fontSize: 10, color: 'var(--muted)' }}>{en}</div>
      </div>
      <div className={mono ? 'mono' : ''} style={{
        fontSize: mono ? 11 : 13, lineHeight: 1.55, color: 'var(--ink)',
        wordBreak: 'break-word'
      }}>{v}</div>
    </div>
  );
}

/* ── Angle gauge ─────────────────────────────────────────────── */
function AngleGauge({ phase }) {
  const a = STATE_INFO[phase].angle;
  // gauge from 0 to 30 deg
  const pct = Math.min(1, a / 30);
  const isAbnormal = a > 12;
  return (
    <div style={{ marginTop: 16, padding: '14px 16px',
      background: 'var(--paper-2)', border: '1px solid var(--line-soft)',
      borderRadius: 12 }}>
      <div style={{ display:'flex', justifyContent:'space-between', alignItems:'baseline' }}>
        <div>
          <div style={{ fontSize: 11, letterSpacing: '.2em', color: 'var(--muted)', textTransform: 'uppercase' }}>
            ASIS – PSIS · 倾角
          </div>
          <div style={{ display:'flex', gap: 10, alignItems:'baseline', marginTop: 2 }}>
            <span className="mono" style={{
              fontSize: 28, fontWeight: 700,
              color: isAbnormal ? 'var(--rust-deep)' : 'var(--ink)' }}>
              {a}°
            </span>
            <span style={{ fontSize: 11, color: 'var(--muted)' }}>
              正常区间 4°–7°
            </span>
          </div>
        </div>
        <div style={{
          padding: '4px 9px', borderRadius: 999, fontSize: 11, fontWeight: 600,
          background: isAbnormal ? 'var(--rust)' : 'var(--tan)',
          color: 'var(--paper)'
        }}>
          {isAbnormal ? '前倾 · TILT' : '中立 · NEUTRAL'}
        </div>
      </div>
      {/* Bar */}
      <div style={{ marginTop: 12, height: 10, background: 'var(--bone)',
        borderRadius: 999, position:'relative', overflow: 'hidden' }}>
        {/* normal range zone (4-7 deg of 30) */}
        <div style={{ position:'absolute', left: `${(4/30)*100}%`,
          width: `${(3/30)*100}%`, top: 0, bottom: 0, background: 'var(--tan-soft)',
          opacity: .85 }}/>
        {/* abnormal threshold (>12 region) */}
        <div style={{ position:'absolute', left: `${(15/30)*100}%`, right: 0, top: 0, bottom: 0,
          background: 'rgba(182,84,58,.18)' }}/>
        <div style={{
          position:'absolute', left: `${pct*100}%`, top: -3, bottom: -3,
          width: 4, background: isAbnormal ? 'var(--rust-deep)' : 'var(--ink)',
          borderRadius: 4,
          transition: 'left .9s cubic-bezier(.4,.1,.2,1)'
        }}/>
      </div>
      <div style={{ display:'flex', justifyContent:'space-between', marginTop: 6,
        fontSize: 10, color: 'var(--muted)', fontFamily: 'JetBrains Mono, monospace' }}>
        <span>0°</span><span>15°</span><span>30°</span>
      </div>
    </div>
  );
}

/* ── Lower-crossed schematic (the iconic Janda X) ─────────────── */
function LowerCrossDiagram() {
  return (
    <svg viewBox="0 0 360 240" width="100%" style={{ display:'block' }}>
      <defs>
        <marker id="arrowR" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto">
          <path d="M 0 0 L 10 5 L 0 10 Z" fill="var(--rust)"/>
        </marker>
        <marker id="arrowB" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto">
          <path d="M 0 0 L 10 5 L 0 10 Z" fill="var(--slate)"/>
        </marker>
      </defs>

      {/* Front-back axes labels */}
      <text x="58" y="22" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)">前 Anterior</text>
      <text x="232" y="22" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)">后 Posterior</text>

      {/* Quadrants */}
      {/* Top-back: tight erector spinae */}
      <g>
        <rect x="200" y="36" width="130" height="68" rx="10" fill="var(--paper-2)" stroke="var(--rust)" strokeWidth="1.4"/>
        <text x="265" y="62" fontFamily="Noto Serif SC" fontWeight="700" fontSize="13" fill="var(--rust-deep)" textAnchor="middle">紧张 · TIGHT</text>
        <text x="265" y="80" fontSize="11" fill="var(--ink)" textAnchor="middle">腰部竖脊肌</text>
        <text x="265" y="96" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)" textAnchor="middle">Erector Spinae</text>
      </g>
      {/* Top-front: weak abs */}
      <g>
        <rect x="30" y="36" width="130" height="68" rx="10" fill="var(--paper-2)" stroke="var(--slate)" strokeWidth="1.4"/>
        <text x="95" y="62" fontFamily="Noto Serif SC" fontWeight="700" fontSize="13" fill="var(--slate-deep)" textAnchor="middle">抑制 · WEAK</text>
        <text x="95" y="80" fontSize="11" fill="var(--ink)" textAnchor="middle">腹直 / 腹横肌</text>
        <text x="95" y="96" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)" textAnchor="middle">Abdominals</text>
      </g>
      {/* Bottom-front: tight hip flexors */}
      <g>
        <rect x="30" y="138" width="130" height="68" rx="10" fill="var(--paper-2)" stroke="var(--rust)" strokeWidth="1.4"/>
        <text x="95" y="164" fontFamily="Noto Serif SC" fontWeight="700" fontSize="13" fill="var(--rust-deep)" textAnchor="middle">紧张 · TIGHT</text>
        <text x="95" y="182" fontSize="11" fill="var(--ink)" textAnchor="middle">髂腰肌 + 股直肌</text>
        <text x="95" y="198" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)" textAnchor="middle">Hip Flexors</text>
      </g>
      {/* Bottom-back: weak glutes */}
      <g>
        <rect x="200" y="138" width="130" height="68" rx="10" fill="var(--paper-2)" stroke="var(--slate)" strokeWidth="1.4"/>
        <text x="265" y="164" fontFamily="Noto Serif SC" fontWeight="700" fontSize="13" fill="var(--slate-deep)" textAnchor="middle">抑制 · WEAK</text>
        <text x="265" y="182" fontSize="11" fill="var(--ink)" textAnchor="middle">臀大肌 / 臀中肌</text>
        <text x="265" y="198" fontFamily="Newsreader" fontStyle="italic" fontSize="10" fill="var(--muted)" textAnchor="middle">Glutes</text>
      </g>

      {/* The X — diagonal connecting tight quadrants */}
      <line x1="160" y1="104" x2="200" y2="138" stroke="var(--rust)" strokeWidth="1.6" markerEnd="url(#arrowR)"/>
      <line x1="200" y1="104" x2="160" y2="138" stroke="var(--rust)" strokeWidth="1.6" markerEnd="url(#arrowR)"/>
      {/* Inhibition diagonal */}
      <line x1="160" y1="70" x2="200" y2="70" stroke="var(--slate)" strokeWidth="1.2" strokeDasharray="3 3"/>
      <line x1="160" y1="172" x2="200" y2="172" stroke="var(--slate)" strokeWidth="1.2" strokeDasharray="3 3"/>

      {/* Center label */}
      <text x="180" y="124" fontFamily="Newsreader" fontStyle="italic" fontSize="11" fill="var(--rust)" textAnchor="middle">cross</text>
    </svg>
  );
}

/* ── Symptoms / Causes grids ──────────────────────────────────── */
function SymptomsGrid() {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginTop: 14 }}>
      {SYMPTOMS.map((s, i) => (
        <div key={i} style={{
          padding: '12px 13px', background: 'var(--paper)',
          border: '1px solid var(--line-soft)', borderRadius: 11,
          display: 'flex', flexDirection: 'column', gap: 3
        }}>
          <div style={{ display:'flex', gap: 6, alignItems: 'baseline' }}>
            <span className="news" style={{ color: 'var(--rust)', fontSize: 11 }}>0{i+1}</span>
            <span style={{ fontWeight: 600, fontSize: 13 }}>{s.cn}</span>
          </div>
          <div className="news" style={{ fontSize: 10, color: 'var(--muted)' }}>{s.en}</div>
          <div style={{ fontSize: 11, color: 'var(--ink-2)', lineHeight: 1.5, marginTop: 4 }}>{s.why}</div>
        </div>
      ))}
    </div>
  );
}

function CauseIcon({ kind }) {
  const c = 'var(--rust-deep)';
  const sw = 1.5;
  switch (kind) {
    case 'chair': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 5 L5 14 L17 14"/><path d="M5 14 L5 19"/><path d="M17 14 L17 19"/><path d="M9 5 L17 5 L17 14"/>
    </g>);
    case 'heel': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 17 L17 17 L17 14 L11 14 L9 8 L6 8 L6 14 L3 14 Z"/><path d="M11 14 L13 19"/>
    </g>);
    case 'preg': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="6" r="2.5"/><path d="M11 8.5 L11 13"/><path d="M11 11 C 16 11 17 17 14 19 L 8 19 C 7 17 8 13 11 11"/>
    </g>);
    case 'core': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <rect x="6" y="5" width="10" height="14" rx="2"/><path d="M6 9 L16 9"/><path d="M6 13 L16 13"/><path d="M11 5 L11 19"/>
    </g>);
    case 'glute': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 16 C 5 11 8 9 11 9 C 14 9 17 11 17 16"/><path d="M5 16 L5 19"/><path d="M17 16 L17 19"/><path d="M11 9 L11 13"/>
    </g>);
    case 'back': return (<g fill="none" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
      <path d="M11 4 L11 19"/><path d="M8 7 L14 7"/><path d="M8 11 L14 11"/><path d="M8 15 L14 15"/>
    </g>);
  }
  return null;
}

function CausesGrid() {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8, marginTop: 14 }}>
      {CAUSES.map((c, i) => (
        <div key={i} style={{
          padding: '12px 8px',
          background: 'var(--bg-warm)', border: '1px solid var(--line-soft)',
          borderRadius: 10, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4
        }}>
          <svg viewBox="0 0 22 22" width="28" height="28"><CauseIcon kind={c.icon}/></svg>
          <div style={{ fontSize: 12, fontWeight: 600 }}>{c.cn}</div>
          <div className="news" style={{ fontSize: 9, color: 'var(--muted)' }}>{c.en}</div>
        </div>
      ))}
    </div>
  );
}

/* ── Exercises ───────────────────────────────────────────────── */
function ExerciseList() {
  return (
    <div style={{ marginTop: 14 }}>
      <ExerciseGroup title="拉伸 · 释放" en="STRETCH" items={EXERCISES.stretch}
        accent="var(--rust)" hint="放松紧张 / 缩短的肌群"/>
      <div style={{ height: 12 }}/>
      <ExerciseGroup title="激活 · 唤醒" en="ACTIVATE" items={EXERCISES.activate}
        accent="var(--slate-deep)" hint="重启被抑制 / 弱化的肌群"/>
    </div>
  );
}
function ExerciseGroup({ title, en, items, accent, hint }) {
  return (
    <div style={{
      border: '1px solid var(--line-soft)',
      borderRadius: 14, overflow: 'hidden',
      background: 'var(--paper)'
    }}>
      <div style={{ padding: '12px 14px', borderBottom: '1px solid var(--line-soft)',
        background: 'var(--paper-2)', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <div>
          <div style={{ fontWeight: 700, fontSize: 14, color: accent }}>{title}</div>
          <div style={{ fontSize: 10, color: 'var(--muted)' }}>{hint}</div>
        </div>
        <div className="news" style={{ fontSize: 12, color: accent, letterSpacing: '.15em' }}>{en}</div>
      </div>
      {items.map((x, i) => (
        <div key={i} style={{
          padding: '11px 14px',
          display: 'grid', gridTemplateColumns: '20px 1fr auto', gap: 10, alignItems: 'baseline',
          borderTop: i ? '1px dotted var(--line)' : 'none'
        }}>
          <span className="news" style={{ color: accent, fontSize: 12 }}>0{i+1}</span>
          <div>
            <div style={{ fontWeight: 600, fontSize: 13 }}>{x.cn}
              <span className="news" style={{ color: 'var(--muted)', fontSize: 11, marginLeft: 6, fontStyle: 'italic' }}>{x.en}</span>
            </div>
            <div style={{ fontSize: 11, color: 'var(--muted)' }}>目标 → {x.target}</div>
          </div>
          <span className="mono" style={{
            fontSize: 11, color: 'var(--ink-2)',
            background: 'var(--bg-warm)', padding: '3px 7px', borderRadius: 6,
            border: '1px solid var(--line-soft)'
          }}>{x.dose}</span>
        </div>
      ))}
    </div>
  );
}

Object.assign(window, {
  StateToggle, MuscleCard, AngleGauge, LowerCrossDiagram,
  SymptomsGrid, CausesGrid, ExerciseList
});
