/* FaceCheck AI — ธีมม่วง + dark mode
   โทเคนสีอยู่ที่ :root (สว่าง) และ [data-theme=dark] / prefers-color-scheme (มืด)
   ทุก class เดิมยังใช้ได้ หน้าใหม่ใช้โทเคนเดียวกัน */

@font-face { font-family: "IBM Plex Sans Thai"; src: url("../fonts/IBMPlexSansThai-Regular.ttf") format("truetype"); font-weight: 400; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("../fonts/IBMPlexSansThai-Medium.ttf") format("truetype"); font-weight: 500; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("../fonts/IBMPlexSansThai-SemiBold.ttf") format("truetype"); font-weight: 600; font-display: swap; }
@font-face { font-family: "IBM Plex Sans Thai"; src: url("../fonts/IBMPlexSansThai-Bold.ttf") format("truetype"); font-weight: 700; font-display: swap; }

:root {
  color-scheme: light;
  --bg: #F6F5FB;
  --surface: #FFFFFF;
  --surface-2: #F3F1F8;
  --input-bg: #FFFFFF;
  --text: #1D1830;
  --muted: #5F5A73;
  --border: #E4E0EF;
  --border-strong: #CFC9E0;
  --primary: #7C3AED;
  --primary-hover: #6D28D9;
  --on-primary: #FFFFFF;
  --soft: #EFE9FE;
  --on-soft: #4C1D95;
  --link: #6D28D9;
  --brand-panel: #5B21B6;
  --focus: #A78BFA;
  --danger: #B91C1C;
  --scrim: rgba(29, 24, 48, .45);
  --shadow: 0 1px 2px rgba(29, 24, 48, .05), 0 4px 16px rgba(29, 24, 48, .05);
  --shadow-lg: 0 24px 64px rgba(29, 24, 48, .22);
  --toast-bg: #1D1830;
  --toast-fg: #FFFFFF;

  --ok-fg: #166534;    --ok-bg: #DCFCE7;    --ok-dot: #16A34A;
  --late-fg: #92400E;  --late-bg: #FEF3C7;  --late-dot: #F59E0B;
  --abs-fg: #B91C1C;   --abs-bg: #FEE2E2;   --abs-dot: #DC2626;
  --leave-fg: #1E40AF; --leave-bg: #DBEAFE; --leave-dot: #3B82F6;
  --pend-fg: #4B4660;  --pend-bg: #EEECF3;  --pend-dot: #9A95AB;

  /* ชื่อเดิม (ใช้ในโค้ด/กราฟ) */
  --card: var(--surface);
  --ok: var(--ok-fg); --warn: var(--late-fg); --err: var(--abs-fg);
  --present: var(--ok-dot); --late: var(--late-dot); --absent: var(--abs-dot); --leave: var(--leave-dot);
  --radius: 16px;
  --radius-sm: 12px;
  --sidebar-w: 260px;

  font-family: "IBM Plex Sans Thai", system-ui, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0E0B18; --surface: #181425; --surface-2: #221C34; --input-bg: #120F1E;
    --text: #EEEAF8; --muted: #A9A3BE; --border: #2D2742; --border-strong: #433B5E;
    --primary: #A78BFA; --primary-hover: #C4B5FD; --on-primary: #1A1033;
    --soft: #2A2148; --on-soft: #DDD3FE; --link: #C4B5FD; --brand-panel: #2A1B57; --danger: #DC2626;
    --scrim: rgba(0, 0, 0, .55); --shadow: 0 1px 2px rgba(0, 0, 0, .4); --shadow-lg: 0 24px 64px rgba(0, 0, 0, .6);
    --toast-bg: #EEEAF8; --toast-fg: #1D1830;
    --ok-fg: #86EFAC; --ok-bg: #123222; --ok-dot: #4ADE80;
    --late-fg: #FCD34D; --late-bg: #3A2B0A; --late-dot: #FBBF24;
    --abs-fg: #FCA5A5; --abs-bg: #3E1518; --abs-dot: #F87171;
    --leave-fg: #93C5FD; --leave-bg: #152447; --leave-dot: #60A5FA;
    --pend-fg: #C9C3DB; --pend-bg: #2A2440; --pend-dot: #8A84A0;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0E0B18; --surface: #181425; --surface-2: #221C34; --input-bg: #120F1E;
  --text: #EEEAF8; --muted: #A9A3BE; --border: #2D2742; --border-strong: #433B5E;
  --primary: #A78BFA; --primary-hover: #C4B5FD; --on-primary: #1A1033;
  --soft: #2A2148; --on-soft: #DDD3FE; --link: #C4B5FD; --brand-panel: #2A1B57; --danger: #DC2626;
  --scrim: rgba(0, 0, 0, .55); --shadow: 0 1px 2px rgba(0, 0, 0, .4); --shadow-lg: 0 24px 64px rgba(0, 0, 0, .6);
  --toast-bg: #EEEAF8; --toast-fg: #1D1830;
  --ok-fg: #86EFAC; --ok-bg: #123222; --ok-dot: #4ADE80;
  --late-fg: #FCD34D; --late-bg: #3A2B0A; --late-dot: #FBBF24;
  --abs-fg: #FCA5A5; --abs-bg: #3E1518; --abs-dot: #F87171;
  --leave-fg: #93C5FD; --leave-bg: #152447; --leave-dot: #60A5FA;
  --pend-fg: #C9C3DB; --pend-bg: #2A2440; --pend-dot: #8A84A0;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; line-height: 1.6; background: var(--bg); color: var(--text); }
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.75rem; margin: 0 0 .75rem; font-weight: 700; line-height: 1.3; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; font-weight: 600; line-height: 1.4; }
h1 small { font-size: .55em; font-weight: 500; }
.muted { color: var(--muted); }
.small { font-size: .87rem; }
.mono { font-variant-numeric: tabular-nums; letter-spacing: .01em; }
.big { font-size: 1.25rem; font-weight: 600; letter-spacing: .05em; }
.nowrap { white-space: nowrap; }
@media (max-width: 860px) { .hide-m { display: none !important; } }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.icon { width: 20px; height: 20px; flex: none; }
.icon.sm { width: 16px; height: 16px; }
.icon.lg { width: 26px; height: 26px; }

/* ---------------- layout ---------------- */
.layout { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border); padding: 20px 14px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.15rem; font-weight: 600; color: var(--text); padding: 4px 10px 18px; }
.brand b { color: var(--link); }
.brand-mark {
  width: 36px; height: 36px; border-radius: 10px; background: var(--primary); color: var(--on-primary);
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.brand-mark svg { width: 22px; height: 22px; }
.brand.big { font-size: 1.6rem; justify-content: center; }
.nav { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 0 12px; border-radius: 10px;
  color: var(--text); width: 100%; text-align: left; font: inherit; font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; }
.nav-link.active { background: var(--soft); color: var(--on-soft); font-weight: 600; }
.nav-link .icon { color: currentColor; }
.nav-link small { font-size: .75rem; color: var(--ok-fg); margin-left: auto; }
.nav-section { font-size: .78rem; font-weight: 600; letter-spacing: .06em; color: var(--muted); padding: 16px 12px 4px; }
.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding: 16px 6px 0; display: flex; flex-direction: column; gap: 14px; }
.user-chip { display: flex; align-items: center; gap: 10px; min-width: 0; }
.user-chip .who { display: flex; flex-direction: column; line-height: 1.35; min-width: 0; flex: 1; }
.user-chip .who b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip .who small { color: var(--muted); font-size: .85rem; }
.avatar-sm {
  width: 40px; height: 40px; border-radius: 999px; background: var(--soft); color: var(--on-soft);
  display: inline-flex; align-items: center; justify-content: center; font-weight: 600; flex: none;
}
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; flex: none; padding: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.icon-btn.filled { background: var(--surface-2); color: var(--text); }
.icon-btn.primary { background: var(--primary); color: var(--on-primary); }
.icon-btn.primary:hover { background: var(--primary-hover); }
.as-link { background: none; border: 0; cursor: pointer; }
.content { padding: 32px 40px 48px; min-width: 0; max-width: 1280px; width: 100%; }

.theme-switch { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.theme-switch button {
  flex: 1; min-height: 36px; border: 0; border-radius: 9px; background: transparent; color: var(--muted);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; padding: 0 10px;
}
.theme-switch button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .14); }

.topbar, .bottom-nav { display: none; }

@media (max-width: 860px) {
  .layout { display: block; }
  .sidebar { display: none; }
  .topbar {
    display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--border); padding: 8px 10px 8px 14px;
  }
  .topbar .brand-mark { width: 32px; height: 32px; border-radius: 9px; }
  .topbar .brand-mark svg { width: 20px; height: 20px; }
  .topbar-title { flex: 1; min-width: 0; font-size: 1.1rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .bottom-nav {
    display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px calc(8px + env(safe-area-inset-bottom));
  }
  .bottom-nav a, .bottom-nav button {
    display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 56px; border: 0; background: transparent;
    color: var(--muted); font: inherit; font-size: .78rem; font-weight: 500; cursor: pointer; text-decoration: none; padding: 0;
  }
  .bottom-nav .pill { display: inline-flex; align-items: center; justify-content: center; width: 56px; height: 30px; border-radius: 999px; }
  .bottom-nav .active { color: var(--on-soft); font-weight: 600; }
  .bottom-nav .active .pill { background: var(--soft); }
  .content { padding: 16px 16px calc(96px + env(safe-area-inset-bottom)); }
  h1 { font-size: 1.45rem; }
}

/* bottom sheet / dialogs (ใช้ <dialog> ของเบราว์เซอร์: Esc ปิดได้ โฟกัสอยู่ในกล่อง) */
dialog { border: 0; padding: 0; color: var(--text); background: var(--surface); }
dialog::backdrop { background: var(--scrim); }
.sheet { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 22px 22px 0 0; padding: 10px 16px calc(24px + env(safe-area-inset-bottom)); }
.sheet[open] { animation: fc-up .22s ease-out; }
.sheet .grabber { display: block; width: 40px; height: 4px; border-radius: 999px; background: var(--border-strong); margin: 0 auto 12px; }
.sheet .nav-link { min-height: 48px; }
.sheet .user-chip { padding: 4px 12px 12px; }
.sheet .theme-switch { margin: 8px 4px 0; }
.sheet .theme-switch button { gap: 6px; font: inherit; font-size: .9rem; font-weight: 500; }
.modal { width: min(440px, calc(100% - 32px)); border-radius: 20px; padding: 24px; box-shadow: var(--shadow-lg); }
.modal[open] { animation: fc-in .2s ease-out; }
.modal h2 { font-size: 1.25rem; font-weight: 700; margin: 12px 0 6px; }
.modal p { margin: 0; color: var(--muted); }
.modal .actions { justify-content: flex-end; margin-top: 20px; }
.modal-icon { width: 48px; height: 48px; border-radius: 14px; display: inline-flex; align-items: center; justify-content: center; background: var(--abs-bg); color: var(--abs-fg); }
.modal-icon.info { background: var(--soft); color: var(--on-soft); }
@keyframes fc-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fc-in { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fc-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }

/* toast */
.toasts {
  position: fixed; top: 20px; right: 24px; z-index: 50; display: flex; flex-direction: column; gap: 10px;
  width: min(420px, calc(100% - 32px)); pointer-events: none;
}
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: 12px; padding: 10px 8px 10px 14px; border-radius: 14px;
  background: var(--toast-bg); color: var(--toast-fg); box-shadow: 0 12px 32px rgba(0, 0, 0, .25); animation: fc-in .22s ease-out;
}
.toast .toast-icon { width: 26px; height: 26px; border-radius: 999px; flex: none; display: inline-flex; align-items: center; justify-content: center; background: #22C55E; color: #0B2414; }
.toast.err .toast-icon { background: #F87171; color: #2A0707; }
.toast.info .toast-icon { background: #A78BFA; color: #1A1033; }
.toast .toast-text { flex: 1; min-width: 0; }
.toast .icon-btn { width: 36px; height: 36px; color: inherit; opacity: .8; }
.toast .icon-btn:hover { background: rgba(127, 127, 127, .2); color: inherit; opacity: 1; }
.toast.leaving { opacity: 0; transform: translateY(-8px); transition: opacity .2s, transform .2s; }
@media (max-width: 860px) {
  .toasts { top: auto; bottom: calc(84px + env(safe-area-inset-bottom)); left: 12px; right: 12px; width: auto; }
}

/* ---------------- components ---------------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 24px; margin-bottom: 16px; }
.card > h2:first-child { margin-top: 0; }
.narrow { max-width: 560px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
@media (max-width: 700px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } .card { padding: 16px; } }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1, .page-head h2 { margin: 0; }
.page-head p { margin: 4px 0 0; }
.card .page-head { margin-bottom: 12px; align-items: center; }
.eyebrow { margin: 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.filters input:not([type=checkbox]), .filters select { width: auto; flex: 1 1 160px; }
.filters .inline input { width: 140px; flex: none; }
.breadcrumb { display: flex; gap: 6px; align-items: center; font-size: .9rem; color: var(--muted); margin-bottom: 8px; }

label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; }
label.inline { flex-direction: row; align-items: center; gap: 8px; }
label.check { flex-direction: row; align-items: center; gap: 10px; font-weight: 400; min-height: 32px; }
.label { font-weight: 500; margin-bottom: 6px; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
input, select, textarea {
  font: inherit; font-size: 1rem; min-height: 44px; padding: 8px 12px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--input-bg); width: 100%; color: inherit;
}
textarea { min-height: 96px; }
input:hover, select:hover, textarea:hover { border-color: var(--muted); }
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
input[type=checkbox], input[type=radio] { width: 20px; height: 20px; min-height: 0; padding: 0; accent-color: var(--primary); flex: none; }
input[type=file] { padding: 8px; }
input:disabled, select:disabled { background: var(--surface-2); color: var(--muted); }
.checks { display: flex; flex-wrap: wrap; gap: 6px 20px; }
.input-icon { position: relative; display: block; flex: 1 1 220px; }
label > .input-icon, label > .pw-field { flex: none; }
@media (max-width: 700px) { .filters .input-icon { flex-basis: 100%; } }
.input-icon .icon { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.input-icon input { padding-left: 42px; }
.pw-field { position: relative; display: block; }
.pw-field input { padding-right: 52px; }
.pw-field .icon-btn { position: absolute; right: 2px; top: 50%; transform: translateY(-50%); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 18px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; position: relative;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); font-weight: 600; }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--abs-fg); }
.btn.danger:hover { background: var(--abs-bg); }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn.danger-solid:hover { filter: brightness(1.08); background: var(--danger); }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: .9rem; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.lg { min-height: 52px; font-size: 1.05rem; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.is-loading { color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 999px;
  border: 2.5px solid var(--text); border-right-color: transparent; animation: fc-spin .8s linear infinite;
}
.btn.primary.is-loading::after, .btn.danger-solid.is-loading::after { border-color: var(--on-primary); border-right-color: transparent; }
.btn.line { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn.line::before { content: ""; width: 26px; height: 26px; border-radius: 8px; flex: none;
  background: #06C755 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 11.5a8.4 8.4 0 0 1-9 8.4 9 9 0 0 1-3.6-.7L3 21l1.6-4.4A8 8 0 0 1 3 11.5 8.4 8.4 0 0 1 12 3a8.4 8.4 0 0 1 9 8.5z'/%3E%3C/svg%3E") center / 16px no-repeat; }
.btn.line-solid { background: #06C755; border-color: #06C755; color: #04260F; font-weight: 600; }
.btn.line-solid:hover { background: #05B34C; }

.alert { display: flex; gap: 10px; align-items: flex-start; padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; }
.alert.ok { background: var(--ok-bg); color: var(--ok-fg); }
.alert.err { background: var(--abs-bg); color: var(--abs-fg); }
.alert.warn { background: var(--late-bg); color: var(--late-fg); }
.alert.info { background: var(--soft); color: var(--on-soft); }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 10px; border-radius: 999px; font-size: .82rem; font-weight: 600;
  background: var(--pend-bg); color: var(--pend-fg); white-space: nowrap; line-height: 1.6;
}
.badge.ok, .badge.st-present, .badge.state-open { background: var(--ok-bg); color: var(--ok-fg); }
.badge.warn, .badge.st-late { background: var(--late-bg); color: var(--late-fg); }
.badge.err, .badge.st-absent { background: var(--abs-bg); color: var(--abs-fg); }
.badge.info, .badge.st-leave, .badge.state-upcoming { background: var(--leave-bg); color: var(--leave-fg); }
.badge.muted, .badge.state-closed, .badge.st-pending { background: var(--pend-bg); color: var(--pend-fg); }
.badge.state-open::before { content: ""; width: 7px; height: 7px; border-radius: 999px; background: var(--ok-dot); }
.emo { display: inline-flex; align-items: center; gap: 6px; }
.emo::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--c); flex: none; }

.table-wrap { overflow-x: auto; }
@media (min-width: 1100px) { .table-wrap.has-popover { overflow: visible; } } /* popover/เมนูในแถวไม่ถูกตัด */
.card.table-wrap { padding: 0; }
.card.table-wrap > .page-head, .card.table-wrap > h2, .card.table-wrap > p { padding: 16px 24px 0; }
.card.table-wrap > h2 { margin-bottom: 12px; }
.card.table-wrap > p { padding-bottom: 16px; margin: 0; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th:first-child, td:first-child { padding-left: 24px; }
th:last-child, td:last-child { padding-right: 24px; }
th { font-size: .85rem; color: var(--muted); font-weight: 500; background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
td.empty, .empty { text-align: center; color: var(--muted); padding: 32px 16px; }
tr.row-err { background: var(--abs-bg); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; align-items: center; }
.row-actions form { margin: 0; }
.inline-form { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.inline-form select { width: auto; }
.inline-form input { width: 140px; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 10px; flex: none; }
.person { display: flex; align-items: center; gap: 12px; min-width: 0; }
.person .avatar-sq {
  width: 44px; height: 44px; border-radius: 12px; background: var(--surface-2); color: var(--muted); font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.person .who { display: flex; flex-direction: column; line-height: 1.4; min-width: 0; }
.person .who a { color: var(--text); font-weight: 500; }
.person .who span { font-size: .85rem; color: var(--muted); }

/* ตารางที่กลายเป็นการ์ดบนมือถือ: <table class="rtable"> + <td data-label="..."> */
@media (max-width: 700px) {
  .card.table-wrap:has(.rtable) { background: transparent; border: 0; overflow: visible; }
  .card.table-wrap:has(.rtable) > .page-head, .card.table-wrap:has(.rtable) > h2 { padding: 0 0 12px; }
  .rtable, .rtable tbody { display: block; }
  .rtable thead { display: none; }
  .rtable tr {
    display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 12px 14px; margin-bottom: 10px;
  }
  .rtable tbody tr:hover { background: var(--surface); }
  .rtable td, .rtable td:first-child, .rtable td:last-child {
    display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 5px 0; border: 0; text-align: right;
  }
  .rtable td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: .87rem; text-align: left; flex: none; }
  .rtable td.cell-main { display: block; text-align: left; padding: 0 0 10px; margin-bottom: 4px; border-bottom: 1px solid var(--border); }
  .rtable td.cell-actions { display: block; text-align: left; padding-top: 10px; }
  .rtable td.cell-hide-m { display: none; }
  .rtable td.empty { display: block; text-align: center; padding: 16px 0; }
  .rtable .row-actions { justify-content: flex-start; flex-wrap: wrap; }
}

.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg); min-width: 220px; z-index: 15; padding: 6px;
}
.menu-body a, .menu-body button {
  display: flex; align-items: center; width: 100%; min-height: 40px; text-align: left; padding: 0 12px; border: 0; background: none;
  font: inherit; color: var(--text); border-radius: 8px; cursor: pointer;
}
.menu-body a:hover, .menu-body button:hover { background: var(--surface-2); text-decoration: none; }
.menu-body form { margin: 0; }
.danger { color: var(--abs-fg) !important; }

/* status edit popover in activity rows */
.row-edit { position: relative; }
.row-edit > summary { list-style: none; }
.row-edit > summary::-webkit-details-marker { display: none; }
.row-edit-body {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 15; width: 320px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 16px; display: flex; flex-direction: column; gap: 12px;
}
.status-pick { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.status-pick label { position: relative; display: block; }
.status-pick input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.status-pick span {
  display: flex; align-items: center; justify-content: center; min-height: 44px; border-radius: 10px; border: 1px solid var(--border-strong);
  font-weight: 600; background: var(--surface);
}
.status-pick input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.status-pick input[value=present]:checked + span { background: var(--ok-bg); color: var(--ok-fg); border: 2px solid var(--ok-dot); }
.status-pick input[value=late]:checked + span { background: var(--late-bg); color: var(--late-fg); border: 2px solid var(--late-dot); }
.status-pick input[value=leave]:checked + span { background: var(--leave-bg); color: var(--leave-fg); border: 2px solid var(--leave-dot); }
.status-pick input[value=absent]:checked + span { background: var(--abs-bg); color: var(--abs-fg); border: 2px solid var(--abs-dot); }
@media (max-width: 1099px) {
  .row-edit-body { position: static; width: auto; box-shadow: none; margin-top: 10px; background: var(--surface-2); border: 0; }
}

/* stat cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px;
  display: flex; flex-direction: column; gap: 6px; color: var(--text); min-width: 0;
}
.stat-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.stat-label { color: var(--muted); font-size: .9rem; display: inline-flex; align-items: center; gap: 8px; }
.stat-value { font-size: 1.9rem; font-weight: 700; line-height: 1.15; }
.stat-value small { font-size: 1rem; font-weight: 500; color: var(--muted); }
.stat-value.sm { font-size: 1.2rem; }
.stat-icon { width: 34px; height: 34px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; flex: none; background: var(--soft); color: var(--on-soft); }
.stat-icon svg { width: 18px; height: 18px; }
.stat-icon.ok { background: var(--ok-bg); color: var(--ok-fg); }
.stat-icon.info { background: var(--leave-bg); color: var(--leave-fg); }
.stat-icon.err { background: var(--abs-bg); color: var(--abs-fg); }
a.stat:hover { text-decoration: none; border-color: var(--border-strong); }
a.stat.on { border: 2px solid var(--primary); box-shadow: 0 0 0 3px var(--soft); padding: 15px 17px; }
.dot-key { width: 10px; height: 10px; border-radius: 999px; flex: none; background: var(--c, var(--primary)); }
.c-present { color: var(--ok-fg); } .c-late { color: var(--late-fg); } .c-absent { color: var(--abs-fg); } .c-leave { color: var(--leave-fg); }
@media (max-width: 700px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 14px; }
  .stat-value { font-size: 1.5rem; }
  .stats.scroll { display: flex; overflow-x: auto; margin: 0 -16px 16px; padding: 4px 16px 6px; }
  .stats.compact { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; gap: 8px; }
  .stats.compact .stat { padding: 10px; align-items: center; gap: 2px; }
  .stats.compact .stat-value { font-size: 1.3rem; }
  .stats.compact .stat-label { font-size: .8rem; gap: 5px; }
  .stats.compact .small { display: none; }
  .stats.scroll .stat { flex: none; min-width: 120px; }
}
.meter { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; width: 120px; margin-bottom: 4px; }
.meter span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.meter.wide { width: 100%; max-width: 280px; }

/* segmented / chip group */
.seg { display: inline-flex; gap: 4px; padding: 4px; border-radius: var(--radius-sm); background: var(--surface-2); border: 1px solid var(--border); flex-wrap: wrap; }
.seg a, .seg button {
  display: inline-flex; align-items: center; min-height: 36px; padding: 0 14px; border-radius: 9px; color: var(--text); font: inherit;
  font-size: .92rem; font-weight: 500; border: 0; background: transparent; cursor: pointer;
}
.seg a:hover { text-decoration: none; background: var(--surface); }
.seg a.on, .seg .on { background: var(--primary); color: var(--on-primary); }
@media (max-width: 700px) {
  .seg.chips { display: flex; flex-wrap: nowrap; overflow-x: auto; background: none; border: 0; padding: 0 16px; margin: 0 -16px; gap: 8px; }
  .seg.chips a { flex: none; min-height: 40px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); }
  .seg.chips a.on { border-color: var(--primary); background: var(--primary); }
}

/* empty state */
.empty-state { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 8px; padding: 40px 16px; color: var(--muted); }
.empty-state .empty-icon { width: 60px; height: 60px; border-radius: 18px; background: var(--soft); color: var(--on-soft); display: inline-flex; align-items: center; justify-content: center; margin-bottom: 4px; }
.empty-state .empty-icon svg { width: 28px; height: 28px; }
.empty-state b { color: var(--text); font-size: 1.05rem; }
.empty-state p { margin: 0; max-width: 380px; }
.empty-state .btn { margin-top: 8px; }
td.empty .empty-state { padding: 16px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.copy-row { display: flex; gap: 8px; }
.pager { display: flex; gap: 6px; flex-wrap: wrap; }
.audit { margin: 10px 0 0; padding-left: 20px; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
details.card > summary { cursor: pointer; font-weight: 600; min-height: 32px; display: flex; align-items: center; }
.prose ul { padding-left: 20px; }
.chip-meta { display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); font-size: .9rem; }
.meta-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.face-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.face { margin: 0; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px; text-align: center; background: var(--surface); }
.face img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; }
.face figcaption { margin: 6px 0; }
.webcam video { width: 100%; max-height: 320px; background: #0f0b1a; border-radius: var(--radius-sm); }

/* profile (หน้านักเรียน) */
.profile { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.profile h1 { margin: 0; }
.profile p { margin: 4px 0; }
.profile .badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.profile-main { flex: 1 1 280px; min-width: 0; }
.avatar {
  width: 80px; height: 80px; border-radius: 24px; background: var(--soft); color: var(--on-soft);
  display: grid; place-items: center; font-size: 2rem; font-weight: 700; flex: none;
}
.profile-search { margin-left: auto; flex: 0 1 280px; }
.rate-card { display: flex; align-items: center; gap: 20px; }
.ring {
  --p: 0; width: 120px; height: 120px; border-radius: 999px; flex: none; display: grid; place-items: center;
  background: conic-gradient(var(--primary) calc(var(--p) * 1%), var(--surface-2) 0);
}
.ring::before { content: ""; grid-area: 1 / 1; width: 88px; height: 88px; border-radius: 999px; background: var(--surface); }
.ring span { grid-area: 1 / 1; z-index: 1; font-size: 1.5rem; font-weight: 700; }
.split { display: flex; flex-wrap: wrap; gap: 16px; margin-bottom: 16px; }
.split > * { margin-bottom: 0; }
.split > .grow { flex: 2 1 420px; min-width: 0; }
.split > .side { flex: 1 1 300px; min-width: 0; }
@media (max-width: 700px) {
  .avatar { width: 60px; height: 60px; border-radius: 18px; font-size: 1.6rem; }
  .profile { gap: 14px; }
  .profile-search { flex: 1 1 100%; margin: 0; }
  .ring { width: 96px; height: 96px; }
  .ring::before { width: 70px; height: 70px; }
  .ring span { font-size: 1.15rem; }
}

/* live activity banner */
.live {
  background: var(--soft); color: var(--on-soft); border-radius: 20px; padding: 18px 22px; margin-bottom: 20px;
  display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center;
}
.live .badge { background: var(--surface); }
.live-main { flex: 1 1 240px; min-width: 0; }
.live-main b { font-size: 1.1rem; }
.live-main div { font-size: .9rem; }
.live-progress { flex: 1 1 220px; display: flex; flex-direction: column; gap: 6px; font-size: .9rem; }
.live-progress .bar { height: 10px; border-radius: 999px; background: var(--surface); overflow: hidden; }
.live-progress .bar span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.live .btn { background: var(--surface); border-color: transparent; color: var(--on-soft); font-weight: 600; }
.live + .live { margin-top: -8px; }

/* login */
.auth-page { min-height: 100vh; }
.auth { display: flex; flex-wrap: wrap; min-height: 100vh; }
.auth-panel {
  flex: 1 1 520px; background: var(--brand-panel); color: #fff; padding: 56px 64px; display: flex; flex-direction: column;
  justify-content: space-between; gap: 40px;
}
.auth-panel .brand { color: #fff; padding: 0; font-size: 1.35rem; }
.auth-panel .brand b { color: #DDD6FE; }
.auth-panel .brand-mark { width: 44px; height: 44px; border-radius: 12px; background: #fff; color: #6D28D9; }
.auth-panel h2 { font-size: 2.6rem; line-height: 1.25; font-weight: 700; margin: 0; }
.auth-panel p { margin: 0; color: #E9E3FF; font-size: 1.1rem; }
.auth-panel ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; font-size: 1rem; }
.auth-panel li { display: flex; gap: 12px; align-items: center; }
.auth-panel li .icon { color: #DDD6FE; }
.auth-panel .foot { font-size: .85rem; color: #DDD6FE; }
.auth-main { flex: 1 1 480px; display: flex; flex-direction: column; padding: 28px 40px; gap: 20px; }
.auth-main .theme-switch { align-self: flex-end; background: var(--surface-2); border: 1px solid var(--border); flex: none; }
.auth-main .theme-switch button { gap: 6px; font: inherit; font-size: .9rem; font-weight: 500; }
.auth-card { width: 100%; max-width: 400px; margin: auto; display: flex; flex-direction: column; gap: 18px; }
.auth-card h1 { font-size: 2rem; margin: 0; }
.auth-card .lead { margin: 6px 0 0; color: var(--muted); }
.auth-card form { display: flex; flex-direction: column; gap: 18px; }
.auth-card .alert { margin: 0; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .9rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.auth-card .help { text-align: center; color: var(--muted); font-size: .9rem; margin: 0; }
@media (max-width: 860px) {
  .auth { display: block; }
  .auth-panel { padding: 20px 20px 72px; gap: 22px; }
  .auth-panel .tagline, .auth-panel ul, .auth-panel .foot { display: none; }
  .auth-panel h2 { font-size: 1.6rem; }
  .auth-panel p { font-size: .9rem; }
  .auth-main { padding: 0 16px 32px; }
  .auth-main .theme-switch { display: none; }
  .auth-card { margin: -48px auto 0; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 24px 20px; box-shadow: var(--shadow); }
  .auth-card h1 { font-size: 1.5rem; }
  .auth-top .auth-mobile-theme { display: inline-flex !important; }
}
.auth-mobile-theme { display: none !important; color: #fff; background: rgba(255, 255, 255, .14); border: 1px solid rgba(255, 255, 255, .3); }
.auth-mobile-theme:hover { background: rgba(255, 255, 255, .22); color: #fff; }
.auth-top { display: flex; align-items: center; justify-content: space-between; }
/* หน้าแบบไม่มีเมนู (error / ไม่ได้ล็อกอิน) */
.bare-card { max-width: 440px; margin: 15vh auto; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 32px; text-align: center; }

/* password slips */
.slips { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.slip { border: 1px dashed var(--border-strong); border-radius: var(--radius-sm); padding: 12px 16px; background: var(--surface); break-inside: avoid; }
.slip-head { font-size: .78rem; color: var(--muted); margin-bottom: 4px; }

@media print {
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --text: #000; --muted: #444; --border: #999; }
  .sidebar, .topbar, .bottom-nav, .toasts, .no-print, .alert { display: none !important; }
  .layout { display: block; }
  .content { padding: 0; }
  body { background: #fff; color: #000; }
  .slips { grid-template-columns: 1fr 1fr; }
}

/* dashboard */
.grid-dash { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 16px; }
.grid-dash > .card { margin-bottom: 16px; min-width: 0; }
@media (max-width: 1100px) { .grid-dash { grid-template-columns: 1fr; } }
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 320px; }
.device-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.device-list li { display: flex; gap: 12px; align-items: flex-start; padding: 12px; border-radius: var(--radius-sm); background: var(--surface-2); }
.device-list li.empty { display: block; background: none; }
.device-list .grow { flex: 1; min-width: 0; line-height: 1.5; }
.device-list .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 8px; flex: none; }
.dot.on { background: var(--ok-dot); box-shadow: 0 0 0 4px var(--ok-bg); }
.dot.off { background: var(--abs-dot); box-shadow: 0 0 0 4px var(--abs-bg); }
.scan-list { list-style: none; margin: 0 -24px -20px; padding: 0; }
.scan-list li { display: flex; align-items: center; gap: 14px; padding: 10px 24px; border-top: 1px solid var(--border); }
.scan-list .person { flex: 1; }
.scan-list .meta { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 700px) {
  .scan-list { margin: 0 -16px -16px; }
  .scan-list li { padding: 10px 16px; align-items: flex-start; }
  .scan-list .meta { flex-direction: column; align-items: flex-end; gap: 4px; }
  .scan-list .meta .emo { font-size: .85rem; }
}

/* reports */
legend { padding: 0; }
.type-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; }
.type-card {
  flex-direction: row; align-items: center; gap: 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 12px 14px; cursor: pointer; font-weight: 500; background: var(--surface); min-height: 48px;
}
.type-card:hover { border-color: var(--primary); }
.type-card.on, .type-card:has(input:checked) { border-color: var(--primary); background: var(--soft); color: var(--on-soft); box-shadow: 0 0 0 1px var(--primary); }
.filters label:not(.inline) { flex: 1 1 280px; }
.a-C { text-align: center; } .a-R { text-align: right; }
[data-show][hidden] { display: none !important; }
.checklist { list-style: none; padding: 0; margin: 0 0 8px; display: flex; flex-direction: column; gap: 6px; }

/* students: room picker */
.room-group { margin-top: 16px; }
.room-card { flex-wrap: wrap; }
.room-card .room-name { flex: 1; }
.room-chips { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
