:root {
  --bg: #f8f4ea;
  --paper: #fffdf7;
  --ink: #2b2a27;
  --muted: #7a7466;
  --tiffany: #0a8f8b;
  --tiffany-soft: #d8f1ef;
  --gold: #b8912e;
  --gold-soft: #f3e7c4;
  --line: #e2d9c3;
  --desk: #efe3c8;
  --absent: #b9b4a8;
  --late: #c46a1c;
  --red: #c0392b;
  --u: 1px; /* 房間單位換算成 px，由程式設定 */
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body, #app { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif; }
body { overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
button { font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); padding: 8px 14px; }
button.primary { background: var(--tiffany); border-color: var(--tiffany); color: #fff; font-weight: 600; }
button.ghost { background: transparent; }
button.big { font-size: 18px; padding: 12px 20px; width: 100%; }
input, textarea { font: inherit; width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: #fff; -webkit-user-select: text; user-select: text; }
label { display: block; margin: 10px 0; font-size: 14px; color: var(--muted); }
label input, label textarea { margin-top: 4px; color: var(--ink); }
.screen[hidden] { display: none !important; }

/* 登入 */
#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--paper); border: 1px solid var(--line); border-radius: 18px; padding: 28px; max-width: 420px; width: 100%; text-align: center; box-shadow: 0 8px 30px rgba(80,60,20,.08); }
.login-card h1 { font-size: 22px; margin: 10px 0 6px; }
.login-card p { color: var(--muted); font-size: 14px; }
.crest { width: 64px; height: 64px; margin: 0 auto; border-radius: 50%; display: grid; place-items: center; font-size: 30px; color: var(--gold); border: 2px solid var(--gold); background: var(--gold-soft); }
.msg { color: var(--red); min-height: 1.2em; }

/* 主畫面 */
#main { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--paper); border-bottom: 1px solid var(--line); }
.topbar .left, .topbar .right { display: flex; align-items: center; gap: 10px; }
.topbar .right { margin-left: auto; }
.class-btn { font-size: 18px; font-weight: 700; border-color: var(--gold); background: var(--gold-soft); }
.class-btn .caret { margin-left: 6px; color: var(--gold); }
.seg { display: inline-flex; border: 1px solid var(--tiffany); border-radius: 10px; overflow: hidden; }
.seg-btn { border: 0; border-radius: 0; background: transparent; color: var(--tiffany); padding: 8px 18px; font-weight: 600; }
.seg-btn.on { background: var(--tiffany); color: #fff; }
.counts { display: flex; gap: 14px; font-size: 16px; align-items: baseline; flex-wrap: wrap; }
.counts b { font-size: 22px; }
.counts .absent b { color: var(--red); }
.counts .late b { color: var(--late); }
.sync { width: 12px; height: 12px; border-radius: 50%; background: #3cb371; display: inline-block; }
.sync.pending { background: #e0a020; }
.sync.offline { background: var(--red); }
.absent-bar { padding: 6px 14px; background: #fbeeea; color: var(--red); font-size: 14px; border-bottom: 1px solid var(--line); }
.absent-bar .lt { color: var(--late); margin-left: 12px; }

.stage { flex: 1; display: flex; min-height: 0; }
.room-wrap { flex: 1; position: relative; overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 8px; }
.room { position: relative; background: var(--paper); border: 2px solid var(--line); border-radius: 14px; touch-action: none; }
.tray { width: 120px; border-left: 1px solid var(--line); background: #f3eee1; display: flex; flex-direction: column; }
.tray-title { padding: 8px; font-size: 13px; color: var(--muted); text-align: center; border-bottom: 1px solid var(--line); }
.tray-list { flex: 1; overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 6px; align-items: center; touch-action: pan-y; }
.tray.drop-on { background: var(--tiffany-soft); }

/* 房間裡的東西：位置與大小用房間單位 × --u */
.fixture { position: absolute; display: grid; place-items: center; font-size: calc(15 * var(--u)); color: var(--muted); border-radius: calc(8 * var(--u)); }
.fixture.board { background: #2f5d50; color: #e9f5ef; font-weight: 600; }
.fixture.teacher { background: var(--gold-soft); border: 1px solid var(--gold); color: #6d5313; }
.fixture.door { background: transparent; border: calc(3 * var(--u)) solid var(--gold); border-radius: 0 0 calc(40 * var(--u)) 0; color: var(--gold); font-weight: 700; }
.table { position: absolute; }
.table .surface { position: absolute; background: var(--desk); border: 1px solid #d6c59b; border-radius: calc(10 * var(--u)); display: grid; place-items: center; color: #8a7442; font-weight: 700; font-size: calc(20 * var(--u)); }
.table.desk2 .surface { background: #f4ecda; }
.table.moving { opacity: .85; z-index: 50; }
.table .tools { position: absolute; display: none; gap: 4px; z-index: 5; }
.editing .table .tools { display: flex; }
.editing .table .surface { outline: 2px dashed var(--gold); cursor: move; }
.tools button { padding: 2px 8px; font-size: calc(16 * var(--u)); min-width: 30px; background: #fff; }
.tools .south.on { background: var(--tiffany); color: #fff; border-color: var(--tiffany); }

.seat { position: absolute; width: calc(84 * var(--u)); height: calc(96 * var(--u)); border-radius: calc(10 * var(--u)); border: 1px dashed #cfc3a5; background: rgba(255,255,255,.55); display: flex; flex-direction: column; align-items: center; justify-content: center; }
.seat.south-seat { border-color: var(--tiffany); }
.seat.drop-on { background: var(--tiffany-soft); border: 2px solid var(--tiffany); }
.stu { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: calc(3 * var(--u)); border-radius: inherit; background: #fff; border: 1px solid var(--line); touch-action: none; position: relative; }
.stu .no { font-size: calc(12 * var(--u)); color: var(--muted); line-height: 1; }
.stu .nm { font-size: calc(17 * var(--u)); font-weight: 600; line-height: 1.1; max-width: 96%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar { width: calc(52 * var(--u)); height: calc(52 * var(--u)); flex: none; border-radius: 50%; background: var(--tiffany-soft); color: var(--tiffany); display: grid; place-items: center; font-weight: 700; font-size: calc(18 * var(--u)); overflow: hidden; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; -webkit-user-drag: none; }
.avatar.photo { background: #fff; }
.avatar.xl { width: 112px; height: 112px; font-size: 36px; }
.stu.absent { background: #eeebe4; }
.stu.absent .avatar { filter: grayscale(1); opacity: .35; }
.stu.absent .nm, .stu.absent .no { color: var(--absent); }
.stu.absent::after { content: "缺席"; position: absolute; top: calc(4 * var(--u)); right: calc(4 * var(--u)); font-size: calc(11 * var(--u)); color: var(--red); font-weight: 700; }
.stu.late::after { content: "遲到"; position: absolute; top: calc(4 * var(--u)); right: calc(4 * var(--u)); font-size: calc(11 * var(--u)); color: #fff; background: var(--late); border-radius: 6px; padding: 0 4px; }
.tray .stu { width: 96px; height: 92px; --u: 1px; }
.ghost-drag { position: fixed; z-index: 1000; pointer-events: none; opacity: .92; transform: scale(1.08); box-shadow: 0 10px 24px rgba(0,0,0,.25); border-radius: 10px; }
.stu.dragging { opacity: .3; }

.edit-bar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; padding-bottom: max(8px, env(safe-area-inset-bottom)); background: var(--gold-soft); border-top: 1px solid var(--gold); font-size: 14px; }
.edit-bar span { margin-right: auto; }

/* 對話框 */
dialog.sheet { border: 1px solid var(--line); border-radius: 16px; padding: 20px; width: min(520px, 94vw); background: var(--paper); color: var(--ink); }
dialog::backdrop { background: rgba(40,30,10,.35); }
dialog h2 { margin: 0 0 10px; font-size: 20px; }
.row { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.class-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.class-list button { text-align: left; padding: 12px; }
.class-list button small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.class-list button.on { border-color: var(--tiffany); background: var(--tiffany-soft); }
.kind-pick { display: flex; gap: 10px; }
.kind { flex: 1; margin: 0; }
.kind input { display: none; }
.kind span { display: block; border: 1px solid var(--line); border-radius: 12px; padding: 10px; color: var(--ink); font-weight: 600; text-align: center; }
.kind small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.kind input:checked + span { border-color: var(--tiffany); background: var(--tiffany-soft); }
.hint { font-size: 12px; color: var(--muted); }
.card-head { display: flex; gap: 16px; align-items: center; }
.card-name { font-size: 22px; font-weight: 700; }
.card-no { color: var(--muted); }
.card-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; margin-top: 14px; }
.card-stats div { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 8px; text-align: center; }
.card-stats b { display: block; font-size: 22px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #2b2a27; color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 14px; z-index: 2000; max-width: 90vw; }
@media (max-width: 700px) {
  .tray { width: 92px; }
  .tray .stu { width: 80px; height: 80px; }
  .counts { font-size: 13px; gap: 8px; } .counts b { font-size: 17px; }
  .seg-btn { padding: 6px 10px; }
}
.edit-bar[hidden], .absent-bar[hidden], .toast[hidden] { display: none !important; }

/* ---- 學生照片 ---- */
.vh { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; pointer-events: none; }
label.btn { display: inline-flex; align-items: center; justify-content: center; position: relative; margin: 0; cursor: pointer; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); padding: 8px 14px; font-size: 16px; }
label.btn.primary { background: var(--tiffany); border-color: var(--tiffany); color: #fff; font-weight: 600; }
label.btn.pick { display: flex; width: 100%; padding: 16px; text-align: center; }
button.danger { color: var(--red); border-color: #e6b9b2; }
button.danger.armed { background: var(--red); color: #fff; border-color: var(--red); }
.msg.info { color: var(--tiffany); }
.note { background: var(--gold-soft); border: 1px solid var(--gold); color: #6d5313; border-radius: 10px; padding: 8px 12px; font-size: 14px; margin: 0 0 10px; }
dialog.sheet.wide { width: min(760px, 96vw); max-height: 92vh; }
dialog h3 { font-size: 15px; margin: 14px 0 6px; color: var(--muted); }
.ph-sum { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ph-sum > div { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.ph-sum > div.ok b { color: var(--tiffany); }
.ph-sum > div.warn { border-color: var(--late); }
.ph-sum > div.warn b { color: var(--late); }
.ph-sum > .hint { grid-column: 1 / -1; background: none; border: 0; padding: 0; }
.ph-sum b { font-size: 20px; margin: 0 2px; }
.ph-list { display: flex; flex-direction: column; gap: 8px; }
.ph-row { display: flex; gap: 10px; align-items: center; background: #fff; border: 1px solid var(--late); border-radius: 12px; padding: 8px; }
.ph-row img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; flex: none; }
.ph-row > div { flex: 1; min-width: 0; }
.ph-row .fn { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ph-row .why { font-size: 12px; color: var(--late); margin-bottom: 4px; }
.ph-row select { font: inherit; width: 100%; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: #fff; }
.ph-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; max-height: 300px; overflow: auto; }
.ph-cell { text-align: center; font-size: 12px; }
.ph-cell img { width: 72px; height: 72px; object-fit: cover; border-radius: 50%; display: block; margin: 0 auto 2px; border: 2px solid var(--tiffany-soft); }
.ph-miss { font-size: 14px; }
.cur-class { background: var(--tiffany-soft); border: 1px solid var(--tiffany); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.cur-class .row { margin-top: 8px; }
.card-actions { align-items: center; }
.crop-box { position: relative; width: min(360px, 80vw); aspect-ratio: 1; margin: 0 auto 10px; touch-action: none; border-radius: 12px; overflow: hidden; background: #fff; cursor: grab; }
.crop-box canvas { width: 100%; height: 100%; display: block; }
.crop-ring { position: absolute; inset: 0; border-radius: 50%; box-shadow: 0 0 0 999px rgba(255,255,255,.45); border: 2px solid var(--tiffany); pointer-events: none; }
#cropZoom { padding: 0; border: 0; background: none; }
@media (max-width: 560px) { .ph-sum { grid-template-columns: 1fr; } }
.tray .stu .avatar { width: 46px; height: 46px; font-size: 16px; }
dialog.sheet.wide { overflow: auto; padding-bottom: 0; }
dialog.sheet.wide > .row.end { position: sticky; bottom: 0; background: var(--paper); padding: 10px 0 16px; margin-top: 4px; border-top: 1px solid var(--line); }
