:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #687385;
  --line: #e3e7ec;
  --primary: #1f5fbf;
  --primary-ink: #ffffff;
  --ok: #1d8a4e;   --ok-bg: #e3f4ea;
  --warn: #b26a00; --warn-bg: #fff1d6;
  --bad: #c0352b;  --bad-bg: #fde4e1;
  --info: #3a5fa8; --info-bg: #e3ebfa;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 2px 8px rgba(20, 30, 50, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c; --card: #1b2129; --text: #e6eaf0; --muted: #98a3b3; --line: #2b333e;
    --primary: #5b95ea; --primary-ink: #0d1520;
    --ok: #5ccb8b; --ok-bg: #173325; --warn: #f0b44c; --warn-bg: #3a2c12;
    --bad: #f07a70; --bad-bg: #3d1d1a; --info: #8db0f0; --info-bg: #1c2a44;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 "IBM Plex Sans Thai", "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .6em; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
.wrap { max-width: 1100px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 460px; }
.topbar {
  background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.topbar .wrap { display: flex; align-items: center; gap: 12px; padding-top: 10px; padding-bottom: 10px; }
.topbar .brand { font-weight: 600; text-decoration: none; color: var(--text); }
.topbar .spacer { flex: 1; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.muted { color: var(--muted); }
.small { font-size: .86rem; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.stack > * + * { margin-top: 10px; }

label { display: block; font-size: .88rem; color: var(--muted); margin-bottom: 4px; }
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=password], input[type=number], input:not([type]), select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
textarea { min-height: 120px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: flex-start; color: var(--text); font-size: .95rem; margin: 6px 0; }
.check input { margin-top: 4px; width: 18px; height: 18px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 15px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
.btn.danger { color: var(--bad); }
.btn.big { padding: 14px 20px; font-size: 1.05rem; width: 100%; }
.btn.sm { padding: 4px 10px; font-size: .85rem; }
.btn:disabled { opacity: .55; cursor: default; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs a {
  padding: 9px 14px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs a.on { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { background: var(--bg); font-weight: 600; font-size: .85rem; color: var(--muted); white-space: nowrap; position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.c, th.c { text-align: center; }
.sticky-col { position: sticky; left: 0; background: var(--card); z-index: 1; }
th.sticky-col { background: var(--bg); z-index: 2; }
.score-input { width: 72px !important; padding: 5px 7px !important; text-align: right; }

.badge {
  display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .8rem; font-weight: 600;
  background: var(--bg); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.info { background: var(--info-bg); color: var(--info); }
.cell-btn { border: 0; background: none; cursor: pointer; padding: 0; }
.flag { color: var(--warn); font-size: .82rem; }

.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 12px; }
.alert.bad { background: var(--bad-bg); color: var(--bad); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.info { background: var(--info-bg); color: var(--info); }

.stat { text-align: center; }
.stat b { display: block; font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .85rem; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100;
  display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none;
}
#toast div {
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: 90vw;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
#toast div.bad { background: var(--bad); color: #fff; }

.modal-bg {
  position: fixed; inset: 0; background: rgba(10, 15, 25, .55); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal { background: var(--card); border-radius: var(--radius); padding: 18px; width: 100%; max-width: 480px; overflow: auto; overscroll-behavior: contain; }

/* กรอบกล้องสัดส่วน 3:4 ที่ไม่สูงเกิน 58% ของจอ (มือถือแนวนอน/จอเตี้ย) */
.cam { position: relative; background: #000; border-radius: 12px; overflow: hidden; aspect-ratio: 3 / 4; width: min(100%, 58vh * 3 / 4); margin: 0 auto; }
.cam video, .cam canvas.ov { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cam .mirror { transform: scaleX(-1); }
.cam .frame { position: absolute; inset: 18%; border: 3px solid rgba(255,255,255,.8); border-radius: 16px; }
.cam .oval { position: absolute; left: 20%; right: 20%; top: 14%; bottom: 18%; border: 3px dashed rgba(255,255,255,.85); border-radius: 50%; }

.steps { list-style: none; padding: 0; margin: 0; }
.steps li { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.steps li:last-child { border-bottom: 0; }
.steps .dot { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--bg); font-size: .85rem; flex: none; }
.steps .done .dot { background: var(--ok-bg); color: var(--ok); }
.steps .fail .dot { background: var(--bad-bg); color: var(--bad); }

.thumb { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; background: var(--bg); vertical-align: middle; }
.face-big { width: 160px; height: 160px; border-radius: 12px; object-fit: cover; background: var(--bg); }
.hide { display: none !important; }

/* ---------------------------------------------------------------- responsive */
.wrap { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.topbar .wrap > * { min-width: 0; }
.topbar .brand, .topbar .title, #who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.tabs::-webkit-scrollbar { display: none; }
h1, h2 { overflow-wrap: anywhere; }
img, video { max-width: 100%; }
#toast { bottom: calc(24px + env(safe-area-inset-bottom)); }
.modal { max-height: 92vh; max-height: 92dvh; }

/* ชื่อนักศึกษาในคอลัมน์แรกของตาราง: รหัสบรรทัดบน ชื่อบรรทัดล่าง */
.who { line-height: 1.3; }
.who b { display: block; font-weight: 600; }
.who span { display: block; font-size: .82rem; color: var(--muted); max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scroll-hint, .show-sm { display: none; }

/* จอสัมผัส: ปุ่มใหญ่พอให้นิ้วกด และช่องกรอก 16px กัน iOS ซูมหน้าเว็บเอง */
@media (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
  .btn { min-height: 42px; }
  .btn.sm { min-height: 36px; padding: 6px 12px; }
  .cell-btn { padding: 6px 2px; }
  .check input { width: 22px; height: 22px; margin-top: 2px; }
}

@media (max-width: 640px) {
  h1 { font-size: 1.25rem; }
  .wrap { padding-top: 12px; padding-bottom: 12px; }
  .card { padding: 14px; border-radius: 10px; }
  .hide-sm { display: none !important; }
  .scroll-hint { display: block; }
  .show-sm { display: inline; }
  .who span { max-width: 34vw; }
  th, td { padding: 7px 8px; }
  .btn { white-space: normal; text-align: center; }
  .row > input, .row > select { flex: 1 1 140px; max-width: none !important; }
  .stat b { font-size: 1.35rem; }
  .face-big { width: 40vw; height: 40vw; }

  /* ตารางแบบการ์ด: แต่ละแถวกลายเป็นการ์ดเรียงลงมา */
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr.head { display: none; }
  table.cards tr { padding: 10px 12px; border-bottom: 1px solid var(--line); }
  table.cards tr:last-child { border-bottom: 0; }
  table.cards td { border: 0; padding: 2px 0; text-align: left !important; }
  table.cards td[data-label] { display: inline-block; width: auto; margin-right: 16px; }
  table.cards td[data-label]::before { content: attr(data-label) " "; color: var(--muted); font-size: .82rem; }
  table.cards td.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
  table.cards td.actions > * { flex: 1 1 auto; }

  /* หน้าต่าง popup เลื่อนขึ้นจากด้านล่าง */
  .modal-bg { align-items: flex-end; padding: 0; }
  .modal { max-width: none; border-radius: 16px 16px 0 0; padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
}

@media (min-width: 1600px) {
  .wrap:not(.narrow) { max-width: 1320px; }
}
