// ============ Teacher screens ============
const { useState, useEffect, useRef } = React;

const TeacherHome = ({ me, onOpen, onNav }) => {
  const pending = DATA.works.filter(w => w.status !== 'graded');
  const total = DATA.works.length;
  const totalStudents = DATA.students.length;
  const totalGroups = DATA.groups.length;
  const recent = [...DATA.works].reverse().slice(0,8);

  return (
    <div className="content">
      <div className="row between" style={{marginBottom:20, flexWrap:'wrap', gap:10}}>
        <div>
          <div className="mono muted" style={{fontSize:11, letterSpacing:'.06em', textTransform:'uppercase'}}>Teacher Dashboard · ภาพรวม</div>
          <div style={{fontFamily:'var(--font-serif)', fontSize:38, lineHeight:1.1, marginTop:4}}>สวัสดี, {me.name} <em className="muted">— let's grade.</em></div>
        </div>
        <button className="btn btn-brand" onClick={()=>onNav('groups')}><Icon name="users" size={14} /> จัดการกลุ่ม</button>
      </div>

      <div className="stat-grid" style={{marginBottom:24}}>
        <div className="stat-card brand">
          <div className="label">รอตรวจ · Pending</div>
          <div className="num">{pending.length}</div>
          <div className="delta">+{Math.min(3, pending.length)} วันนี้</div>
        </div>
        <div className="stat-card">
          <div className="label">ผลงานทั้งหมด</div>
          <div className="num">{total}</div>
          <div className="delta">+12% เดือนนี้</div>
        </div>
        <div className="stat-card">
          <div className="label">นักเรียน</div>
          <div className="num">{totalStudents}</div>
          <div className="delta muted">ป.5: 6 · ป.6: 6</div>
        </div>
        <div className="stat-card warm">
          <div className="label">กลุ่ม</div>
          <div className="num">{totalGroups}</div>
          <div className="delta muted">ใน 4 ห้องเรียน</div>
        </div>
      </div>

      <div style={{display:'grid', gridTemplateColumns:'1.5fr 1fr', gap:20}} className="dash-grid">
        <div>
          <div className="row between" style={{marginBottom:12}}>
            <div style={{fontSize:15, fontWeight:600}}>รอตรวจ <span className="tag-en">· awaiting your feedback</span></div>
            <button className="btn btn-ghost sm" onClick={()=>onNav('submissions')}>ดูทั้งหมด <Icon name="arrow" size={12} /></button>
          </div>
          <div className="card" style={{overflow:'hidden'}}>
            <table className="tbl">
              <thead>
                <tr>
                  <th>ผลงาน</th>
                  <th>นักเรียน</th>
                  <th>ห้อง</th>
                  <th>ส่งเมื่อ</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                {pending.slice(0,7).map(w => {
                  const cls = H.cls(w.classId);
                  return (
                    <tr key={w.id} onClick={()=>onOpen(w)} style={{cursor:'pointer'}}>
                      <td>
                        <div className="row" style={{gap:10}}>
                          <div style={{width:42, height:32, borderRadius:6, overflow:'hidden', position:'relative', flexShrink:0}}>
                            <div className={`thumb-bg ${w.thumb}`}></div>
                          </div>
                          <div>
                            <div style={{fontWeight:600, fontSize:13}}>{w.title}</div>
                            <div className="muted mono" style={{fontSize:10.5}}>{H.typeLabel(w.type)}{w.owners.length>1?' · group':''}</div>
                          </div>
                        </div>
                      </td>
                      <td><AvatarStack ids={w.owners} max={3} /></td>
                      <td><span className="chip neutral">{cls.label}</span></td>
                      <td className="muted mono" style={{fontSize:11.5}}>{H.fmtDate(w.date)}</td>
                      <td><button className="btn btn-outline sm">ตรวจ <Icon name="arrow" size={11} /></button></td>
                    </tr>
                  );
                })}
                {pending.length === 0 && <tr><td colSpan={5}><Empty title="ตรวจครบหมดแล้ว!" /></td></tr>}
              </tbody>
            </table>
          </div>
        </div>

        <div className="col" style={{gap:18}}>
          <div className="card card-pad">
            <div style={{fontSize:14, fontWeight:600, marginBottom:12}}>กิจกรรมห้องเรียน <span className="tag-en">· activity</span></div>
            <div className="col" style={{gap:10}}>
              {DATA.activity.map((a,i)=>{
                const who = H.person(a.who);
                return (
                  <div key={i} className="row" style={{gap:10}}>
                    <Avatar person={who} size="sm" />
                    <div style={{flex:1, fontSize:12.5, lineHeight:1.4}}>
                      <strong>{who.name.split(' ').slice(-1)[0]}</strong> <span className="muted">{a.what}</span> <strong>"{a.target}"</strong>
                      <div className="muted mono" style={{fontSize:10.5}}>{a.when}</div>
                    </div>
                  </div>
                );
              })}
            </div>
          </div>

          <div className="card card-pad">
            <div style={{fontSize:14, fontWeight:600, marginBottom:10}}>ห้องเรียน <span className="tag-en">· classes</span></div>
            <div className="col" style={{gap:8}}>
              {DATA.classes.map(c=>{
                const ws = DATA.works.filter(w=>w.classId===c.id);
                const pn = ws.filter(w=>w.status !== 'graded').length;
                return (
                  <button key={c.id} className="row between" onClick={()=>onNav('gallery', { classFilter: c.id })}
                    style={{padding:'10px 12px', borderRadius:10, textAlign:'left', cursor:'pointer', background:'var(--paper)', border:'1px solid var(--line)'}}>
                    <div>
                      <div style={{fontWeight:600, fontSize:13}}>{c.label}</div>
                      <div className="muted mono" style={{fontSize:10.5}}>{ws.length} ผลงาน · {pn} รอตรวจ</div>
                    </div>
                    <Icon name="arrow" size={14} />
                  </button>
                );
              })}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

// --- Groups management ---
const TeacherGroups = ({ onOpen }) => {
  const [classId, setClassId] = useState('p5-1');
  const [showCreate, setShowCreate] = useState(false);
  const [editingGroup, setEditingGroup] = useState(null);
  const [allGroups, setAllGroups] = useState(DATA.groups);

  const classGroups = allGroups.filter(g => g.classId === classId);
  const classStudents = DATA.students.filter(s => s.classId === classId);
  const assignedIds = new Set(classGroups.flatMap(g => g.members));
  const unassigned = classStudents.filter(s => !assignedIds.has(s.id));

  return (
    <div className="content">
      <div className="row between" style={{marginBottom:18, flexWrap:'wrap', gap:10}}>
        <div>
          <div style={{fontSize:20, fontWeight:600}}>จัดการกลุ่มงาน <span className="tag-en">· group management</span></div>
          <div className="muted" style={{fontSize:12.5}}>ครูเป็นผู้กำหนดกลุ่มและสมาชิก — งาน 1 ชิ้นมีหลายเจ้าของได้</div>
        </div>
        <div className="row" style={{gap:8}}>
          <select className="select" style={{padding:'8px 12px', fontSize:13, height:36, width:'auto'}} value={classId} onChange={e=>setClassId(e.target.value)}>
            {DATA.classes.map(c => <option key={c.id} value={c.id}>{c.label}</option>)}
          </select>
          <button className="btn btn-brand" onClick={()=>{setEditingGroup({name:'', nameEn:'', members:[], classId, color:'oklch(0.75 0.14 50)'}); setShowCreate(true);}}>
            <Icon name="plus" size={14} /> สร้างกลุ่มใหม่
          </button>
        </div>
      </div>

      <div style={{display:'grid', gridTemplateColumns:'2fr 1fr', gap:20}} className="dash-grid">
        <div className="col" style={{gap:14}}>
          {classGroups.length === 0 && <div className="card card-pad"><Empty title="ยังไม่มีกลุ่มในห้องนี้" sub="Create a group to get started" /></div>}
          {classGroups.map(g => {
            const groupWorks = DATA.works.filter(w => w.owners.length > 1 && w.owners.every(o => g.members.includes(o)) && w.owners.length === g.members.length);
            return (
              <div key={g.id} className="card card-pad">
                <div className="row between" style={{marginBottom:14}}>
                  <div className="row" style={{gap:12}}>
                    <div style={{width:46, height:46, borderRadius:10, background:g.color, display:'grid', placeItems:'center', color:'white', fontWeight:600}}>
                      <Icon name="users" size={20} />
                    </div>
                    <div>
                      <div style={{fontSize:16, fontWeight:600}}>{g.name} <span className="tag-en">· {g.nameEn}</span></div>
                      <div className="muted mono" style={{fontSize:11}}>{g.members.length} คน · {groupWorks.length} ผลงานร่วม</div>
                    </div>
                  </div>
                  <div className="row" style={{gap:6}}>
                    <button className="btn btn-outline sm" onClick={()=>{setEditingGroup(g); setShowCreate(true);}}><Icon name="edit" size={12} /> แก้ไข</button>
                    <button className="btn btn-danger sm"><Icon name="trash" size={12} /></button>
                  </div>
                </div>
                <div className="row wrap" style={{gap:6}}>
                  {g.members.map(mid => {
                    const m = H.person(mid);
                    return (
                      <div key={mid} className="row" style={{gap:6, padding:'4px 10px 4px 4px', background:'var(--brand-50)', borderRadius:99}}>
                        <Avatar person={m} size="sm" />
                        <span style={{fontSize:12, fontWeight:500}}>{m.name}</span>
                      </div>
                    );
                  })}
                </div>
                {groupWorks.length > 0 && (
                  <div style={{marginTop:14, paddingTop:14, borderTop:'1px solid var(--line)'}}>
                    <div className="muted mono" style={{fontSize:10.5, textTransform:'uppercase', letterSpacing:'.06em', marginBottom:8}}>ผลงานกลุ่ม · group works</div>
                    <div className="row wrap" style={{gap:8}}>
                      {groupWorks.map(w => (
                        <button key={w.id} className="row" style={{gap:8, padding:6, paddingRight:12, background:'var(--paper)', border:'1px solid var(--line)', borderRadius:8, cursor:'pointer'}} onClick={()=>onOpen(w)}>
                          <div style={{width:32, height:24, borderRadius:4, overflow:'hidden', position:'relative'}}>
                            <div className={`thumb-bg ${w.thumb}`}></div>
                          </div>
                          <span style={{fontSize:12, fontWeight:500}}>{w.title}</span>
                          {w.status==='graded' ? <span className="chip" style={{fontSize:10, padding:'1px 6px', background:'oklch(0.95 0.05 150)', color:'oklch(0.4 0.13 150)'}}>ตรวจแล้ว</span> : <span className="chip neutral" style={{fontSize:10, padding:'1px 6px'}}>รอตรวจ</span>}
                        </button>
                      ))}
                    </div>
                  </div>
                )}
              </div>
            );
          })}
        </div>

        <div className="card card-pad">
          <div style={{fontSize:14, fontWeight:600, marginBottom:4}}>นักเรียนที่ยังไม่มีกลุ่ม</div>
          <div className="muted" style={{fontSize:11.5, marginBottom:12}}>ลากเข้ากลุ่ม หรือสร้างกลุ่มใหม่</div>
          {unassigned.length === 0 ? <div className="muted" style={{fontSize:12.5}}>ทุกคนถูกจัดกลุ่มเรียบร้อย ✓</div> :
            <div className="col" style={{gap:8}}>
              {unassigned.map(s => (
                <div key={s.id} className="row" style={{gap:10, padding:8, background:'var(--paper)', borderRadius:10, border:'1px dashed var(--line-strong)'}}>
                  <Avatar person={s} size="sm" />
                  <div style={{flex:1, fontSize:12.5, fontWeight:500}}>{s.name}</div>
                  <button className="btn btn-ghost sm" style={{fontSize:11}}><Icon name="plus" size={11} /> จัด</button>
                </div>
              ))}
            </div>
          }
        </div>
      </div>

      {showCreate && editingGroup && (
        <Modal onClose={()=>setShowCreate(false)}>
          <div className="modal-head">
            <strong>{editingGroup.id ? 'แก้ไขกลุ่ม · Edit Group' : 'สร้างกลุ่มใหม่ · New Group'}</strong>
            <div className="spacer" style={{flex:1}}></div>
            <button className="btn-ghost" style={{padding:6}} onClick={()=>setShowCreate(false)}><Icon name="x" size={16} /></button>
          </div>
          <div style={{padding:20, display:'flex', flexDirection:'column', gap:14, maxHeight:'70vh', overflow:'auto'}}>
            <div className="row" style={{gap:12, flexWrap:'wrap'}}>
              <div className="field" style={{flex:'1 1 220px'}}>
                <label>ชื่อกลุ่ม (ภาษาไทย)</label>
                <input className="input" value={editingGroup.name} onChange={e=>setEditingGroup({...editingGroup, name:e.target.value})} placeholder="เช่น กลุ่มสายลม" />
              </div>
              <div className="field" style={{flex:'1 1 220px'}}>
                <label>Group Name (English)</label>
                <input className="input" value={editingGroup.nameEn} onChange={e=>setEditingGroup({...editingGroup, nameEn:e.target.value})} placeholder="e.g. Wind" />
              </div>
            </div>
            <div className="field">
              <label>สมาชิก · Members ({editingGroup.members.length})</label>
              <div className="col" style={{gap:6, padding:10, background:'var(--paper)', borderRadius:10, border:'1px solid var(--line)', maxHeight:280, overflow:'auto'}}>
                {classStudents.map(s => {
                  const inGroup = editingGroup.members.includes(s.id);
                  return (
                    <label key={s.id} className="row" style={{gap:10, padding:6, borderRadius:6, cursor:'pointer', background: inGroup?'var(--brand-50)':'transparent'}}>
                      <input type="checkbox" checked={inGroup} onChange={()=>{
                        setEditingGroup({
                          ...editingGroup,
                          members: inGroup ? editingGroup.members.filter(id=>id!==s.id) : [...editingGroup.members, s.id]
                        });
                      }} />
                      <Avatar person={s} size="sm" />
                      <span style={{flex:1, fontSize:13, fontWeight:500}}>{s.name}</span>
                      <span className="muted tag-en" style={{fontSize:11}}>{s.nameEn}</span>
                    </label>
                  );
                })}
              </div>
            </div>
            <div className="row between">
              <button className="btn btn-ghost" onClick={()=>setShowCreate(false)}>ยกเลิก</button>
              <button className="btn btn-brand" onClick={()=>{
                if (!editingGroup.name) return;
                if (editingGroup.id) {
                  setAllGroups(allGroups.map(g => g.id === editingGroup.id ? editingGroup : g));
                } else {
                  const newG = { ...editingGroup, id: 'g'+(allGroups.length+1) };
                  setAllGroups([...allGroups, newG]);
                  DATA.groups.push(newG);
                }
                setShowCreate(false);
              }}>{editingGroup.id?'บันทึก':'สร้างกลุ่ม'}</button>
            </div>
          </div>
        </Modal>
      )}
    </div>
  );
};

// --- Submissions / management ---
const TeacherSubmissions = ({ onOpen }) => {
  const [tab, setTab] = useState('pending');
  const [editing, setEditing] = useState(null);
  let works = [...DATA.works];
  if (tab === 'pending') works = works.filter(w => w.status !== 'graded');
  else if (tab === 'graded') works = works.filter(w => w.status === 'graded');
  works.sort((a,b)=>b.date.localeCompare(a.date));

  return (
    <div className="content">
      <div className="row between" style={{marginBottom:18, flexWrap:'wrap'}}>
        <div>
          <div style={{fontSize:20, fontWeight:600}}>ผลงานส่งเข้ามา <span className="tag-en">· submissions</span></div>
          <div className="muted" style={{fontSize:12.5}}>ดู ตรวจ ปรับแก้การส่ง และให้ feedback</div>
        </div>
      </div>

      <div className="tabs">
        <button className={tab==='pending'?'on':''} onClick={()=>setTab('pending')}>รอตรวจ · Pending ({DATA.works.filter(w=>w.status!=='graded').length})</button>
        <button className={tab==='graded'?'on':''} onClick={()=>setTab('graded')}>ตรวจแล้ว · Reviewed ({DATA.works.filter(w=>w.status==='graded').length})</button>
        <button className={tab==='all'?'on':''} onClick={()=>setTab('all')}>ทั้งหมด · All ({DATA.works.length})</button>
      </div>

      <div className="card" style={{overflow:'hidden'}}>
        <table className="tbl">
          <thead>
            <tr>
              <th>ผลงาน</th>
              <th>เจ้าของ</th>
              <th>ห้อง</th>
              <th>ประเภท</th>
              <th>ส่งเมื่อ</th>
              <th>สถานะ</th>
              <th>การกระทำ · Actions</th>
            </tr>
          </thead>
          <tbody>
            {works.map(w => {
              const cls = H.cls(w.classId);
              return (
                <tr key={w.id}>
                  <td>
                    <div className="row" style={{gap:10}}>
                      <div style={{width:42, height:32, borderRadius:6, overflow:'hidden', position:'relative', flexShrink:0}}>
                        <div className={`thumb-bg ${w.thumb}`}></div>
                      </div>
                      <div>
                        <div style={{fontWeight:600, fontSize:13}}>{w.title}</div>
                        <div className="muted tag-en" style={{fontSize:11}}>{w.titleEn}</div>
                      </div>
                    </div>
                  </td>
                  <td>
                    <div className="row" style={{gap:6}}>
                      <AvatarStack ids={w.owners} max={3} />
                      {w.owners.length>1 && <span className="chip warm" style={{fontSize:10, padding:'1px 6px'}}>Group</span>}
                    </div>
                  </td>
                  <td><span className="chip neutral">{cls.label}</span></td>
                  <td className="muted mono" style={{fontSize:11.5}}>{H.typeLabel(w.type)}</td>
                  <td className="muted mono" style={{fontSize:11.5}}>{H.fmtDate(w.date)}</td>
                  <td>
                    {w.status==='graded'
                      ? <span className="chip" style={{background:'oklch(0.95 0.05 150)', color:'oklch(0.4 0.13 150)', fontSize:11}}>✓ ตรวจแล้ว</span>
                      : <span className="chip warm" style={{fontSize:11}}>รอตรวจ</span>}
                  </td>
                  <td>
                    <div className="row" style={{gap:4}}>
                      <button className="btn btn-outline sm" onClick={()=>onOpen(w)}><Icon name="eye" size={12} /> ดู</button>
                      <button className="btn btn-ghost sm" onClick={()=>setEditing(w)} title="แก้ไขการส่ง"><Icon name="edit" size={12} /></button>
                    </div>
                  </td>
                </tr>
              );
            })}
            {works.length === 0 && <tr><td colSpan={7}><Empty /></td></tr>}
          </tbody>
        </table>
      </div>

      {editing && (
        <Modal onClose={()=>setEditing(null)}>
          <div className="modal-head">
            <strong>แก้ไขการส่งผลงาน · Edit Submission</strong>
            <span className="muted" style={{fontSize:12}}>{editing.title}</span>
            <div className="spacer" style={{flex:1}}></div>
            <button className="btn-ghost" style={{padding:6}} onClick={()=>setEditing(null)}><Icon name="x" size={16} /></button>
          </div>
          <div style={{padding:20, display:'flex', flexDirection:'column', gap:14}}>
            <div className="row" style={{gap:12}}>
              <div className="field" style={{flex:1}}>
                <label>ชื่อผลงาน · Title</label>
                <input className="input" defaultValue={editing.title} />
              </div>
              <div className="field" style={{flex:1}}>
                <label>วันที่ส่ง · Submission Date</label>
                <input className="input" type="date" defaultValue={editing.date} />
              </div>
            </div>
            <div className="row" style={{gap:12}}>
              <div className="field" style={{flex:1}}>
                <label>ประเภท · Type</label>
                <select className="select" defaultValue={editing.type}>
                  {['sketch','painting','illustration','storyboard','animation','video','document'].map(t=>(
                    <option key={t} value={t}>{H.typeLabel(t)}</option>
                  ))}
                </select>
              </div>
              <div className="field" style={{flex:1}}>
                <label>ห้อง · Class</label>
                <select className="select" defaultValue={editing.classId}>
                  {DATA.classes.map(c => <option key={c.id} value={c.id}>{c.label}</option>)}
                </select>
              </div>
            </div>
            <div className="field">
              <label>เจ้าของผลงาน · Owners</label>
              <div className="row wrap" style={{gap:6, padding:10, background:'var(--paper)', borderRadius:10, border:'1px solid var(--line)'}}>
                {editing.owners.map(id=>{
                  const p = H.person(id);
                  return <span key={id} className="row" style={{gap:6, padding:'4px 8px 4px 4px', background:'white', border:'1px solid var(--line)', borderRadius:99}}><Avatar person={p} size="sm" /> <span style={{fontSize:12}}>{p.name}</span> <button className="btn-ghost" style={{padding:2}}><Icon name="x" size={11} /></button></span>;
                })}
                <button className="btn btn-ghost sm"><Icon name="plus" size={11} /> เพิ่ม</button>
              </div>
            </div>
            <div className="surface" style={{padding:12, background:'oklch(0.96 0.02 25)', borderColor:'oklch(0.88 0.04 25)'}}>
              <div className="row" style={{gap:8, fontSize:12, color:'oklch(0.45 0.13 25)'}}>
                <Icon name="trash" size={14} />
                <strong>เขตอันตราย:</strong> ถอนการส่งหรือลบผลงาน — นักเรียนจะถูกแจ้งเตือน
              </div>
              <div className="row" style={{gap:6, marginTop:8}}>
                <button className="btn btn-outline sm">ถอนการส่ง</button>
                <button className="btn btn-danger sm">ลบผลงาน</button>
              </div>
            </div>
            <div className="row between">
              <button className="btn btn-ghost" onClick={()=>setEditing(null)}>ยกเลิก</button>
              <button className="btn btn-brand" onClick={()=>setEditing(null)}><Icon name="check" size={12} /> บันทึก</button>
            </div>
          </div>
        </Modal>
      )}
    </div>
  );
};

// --- Teacher Profile ---
const TeacherProfile = ({ me }) => {
  const [photoUrl, setPhotoUrl] = useState(me.photo || null);
  const [showPwModal, setShowPwModal] = useState(false);
  const fileRef = useRef(null);

  const onPhotoChange = (e) => {
    const f = e.target.files?.[0]; if (!f) return;
    const url = URL.createObjectURL(f);
    setPhotoUrl(url);
    me.photo = url;
  };

  const myComments = Object.values(DATA.commentsByWork).flat().filter(c => c.authorId === me.id).length;
  const reviewed = DATA.works.filter(w => w.status === 'graded').length;

  return (
    <div className="content">
      <div className="card card-pad" style={{marginBottom:20}}>
        <div className="row" style={{gap:20, flexWrap:'wrap'}}>
          <div style={{position:'relative', flexShrink:0}}>
            {photoUrl ? (
              <img src={photoUrl} alt="profile" style={{width:104, height:104, borderRadius:'50%', objectFit:'cover', display:'block', border:'3px solid var(--paper)', boxShadow:'var(--shadow-2)'}} />
            ) : (
              <div style={{width:104, height:104, borderRadius:'50%', background:me.color, display:'grid', placeItems:'center', color:'white', fontSize:40, fontWeight:600}}>{me.initial}</div>
            )}
            <button onClick={()=>fileRef.current?.click()}
              title="เปลี่ยนรูปโปรไฟล์"
              style={{position:'absolute', right:-4, bottom:-4, width:36, height:36, borderRadius:'50%', background:'var(--ink)', color:'var(--paper)', display:'grid', placeItems:'center', border:'3px solid var(--paper)', cursor:'pointer'}}>
              <Icon name="edit" size={14} />
            </button>
            <input type="file" accept="image/*" ref={fileRef} hidden onChange={onPhotoChange} />
          </div>

          <div style={{flex:1, minWidth:200}}>
            <div className="mono muted" style={{fontSize:11, letterSpacing:'.06em', textTransform:'uppercase'}}>ครูประจำวิชา · Art & 2D Animation</div>
            <div style={{fontFamily:'var(--font-serif)', fontWeight:300, fontSize:36, lineHeight:1.1, marginTop:2}}>{me.name}</div>
            <div className="tag-en" style={{fontSize:17, fontWeight:300}}>{me.nameEn}</div>
            <div className="row wrap" style={{gap:8, marginTop:10}}>
              <span className="chip">ครู · Teacher</span>
              <span className="chip neutral">ID: {me.id}</span>
              <span className="chip neutral">{me.email || me.id+'@cmuds.ac.th'}</span>
              <span className="chip neutral">รับผิดชอบ {DATA.classes.length} ห้อง</span>
            </div>
          </div>

          <div className="row" style={{gap:18, alignSelf:'flex-end', flexWrap:'wrap'}}>
            <div><div className="muted mono" style={{fontSize:10.5, textTransform:'uppercase'}}>ตรวจแล้ว</div><div style={{fontFamily:'var(--font-serif)', fontSize:32, fontWeight:300}}>{reviewed}</div></div>
            <div><div className="muted mono" style={{fontSize:10.5, textTransform:'uppercase'}}>คอมเมนต์</div><div style={{fontFamily:'var(--font-serif)', fontSize:32, fontWeight:300}}>{myComments}</div></div>
            <div><div className="muted mono" style={{fontSize:10.5, textTransform:'uppercase'}}>กลุ่ม</div><div style={{fontFamily:'var(--font-serif)', fontSize:32, fontWeight:300}}>{DATA.groups.length}</div></div>
          </div>
        </div>

        <div className="row wrap" style={{gap:8, marginTop:18, paddingTop:18, borderTop:'1px solid var(--line)'}}>
          <button className="btn btn-outline" onClick={()=>fileRef.current?.click()}><Icon name="image" size={14} /> เปลี่ยนรูปโปรไฟล์</button>
          <button className="btn btn-outline" onClick={()=>setShowPwModal(true)}><Icon name="settings" size={14} /> เปลี่ยนรหัสผ่าน</button>
        </div>
      </div>

      <div className="card card-pad">
        <div style={{fontSize:14, fontWeight:600, marginBottom:14}}>ห้องที่รับผิดชอบ <span className="tag-en">· classes I teach</span></div>
        <div style={{display:'grid', gridTemplateColumns:'repeat(auto-fill,minmax(200px,1fr))', gap:12}}>
          {DATA.classes.map(c => {
            const s = DATA.students.filter(x => x.classId === c.id);
            const w = DATA.works.filter(x => x.classId === c.id);
            return (
              <div key={c.id} className="surface" style={{padding:14}}>
                <div style={{fontFamily:'var(--font-serif)', fontSize:24, fontWeight:300}}>{c.label}</div>
                <div className="muted mono" style={{fontSize:11, marginTop:2}}>{s.length} นักเรียน · {w.length} ผลงาน</div>
                <div style={{marginTop:10}}><AvatarStack ids={s.slice(0,5).map(x=>x.id)} max={5} /></div>
              </div>
            );
          })}
        </div>
      </div>

      {showPwModal && <ChangePasswordModal me={me} onClose={()=>setShowPwModal(false)} />}
    </div>
  );
};

Object.assign(window, { TeacherHome, TeacherGroups, TeacherSubmissions, TeacherProfile });
