:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #17212b;
  --muted: #637083;
  --line: #dde3ea;
  --brand: #0f5fd6;
  --brand-ink: #ffffff;
  --ok: #12804a;
  --ok-bg: #e6f6ee;
  --err: #b42318;
  --err-bg: #fdecea;
  --warn: #9a6700;
  --warn-bg: #fff4d6;
  --radius: 14px;
  font-family: 'Sarabun', 'Noto Sans Thai', 'Leelawadee UI', 'Tahoma', system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a; --card: #18202a; --ink: #e8edf2; --muted: #9aa7b6; --line: #2a3542;
    --brand: #4c8dff; --brand-ink: #06121f;
    --ok: #4cd18a; --ok-bg: #11301f; --err: #ff8a80; --err-bg: #3a1714; --warn: #f5c451; --warn-bg: #352a0c;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-size: 16px; line-height: 1.5; }
body { min-height: 100vh; }
.wrap { max-width: 520px; margin: 0 auto; padding: 16px; }
.wrap.wide { max-width: 1100px; }
h1 { font-size: 1.35rem; margin: 0 0 4px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
.muted { color: var(--muted); font-size: .9rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 12px 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1 1 auto; }

label { display: block; font-size: .85rem; color: var(--muted); margin: 10px 0 4px; }
input, select, button, textarea { font: inherit; }
input, select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink);
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); cursor: pointer; text-decoration: none; min-height: 44px;
}
button.primary, .btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
button.big { width: 100%; padding: 16px; font-size: 1.1rem; }
button.danger { color: var(--err); }
button.small { padding: 6px 10px; min-height: 34px; font-size: .85rem; }
button:disabled { opacity: .55; cursor: not-allowed; }

.status { padding: 12px 14px; border-radius: 12px; margin: 12px 0; font-weight: 600; }
.status.ok { background: var(--ok-bg); color: var(--ok); }
.status.err { background: var(--err-bg); color: var(--err); }
.status.warn { background: var(--warn-bg); color: var(--warn); }
.status.info { background: var(--bg); color: var(--muted); border: 1px solid var(--line); font-weight: 400; }

.pill { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .8rem; font-weight: 600; }
.pill.in { background: var(--ok-bg); color: var(--ok); }
.pill.out { background: var(--warn-bg); color: var(--warn); }
.pill.off { background: var(--err-bg); color: var(--err); }

.hidden { display: none !important; }

/* ตัวสแกน */
.scanner { position: relative; border-radius: var(--radius); overflow: hidden; background: #000; aspect-ratio: 3 / 4; max-height: 70vh; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner .frame {
  position: absolute; inset: 18% 12%; border: 3px solid rgba(255,255,255,.9); border-radius: 18px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.35);
}

/* ตาราง */
.tablewrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.grid th, table.grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.grid th { color: var(--muted); font-weight: 600; font-size: .8rem; }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0; }
.tabs button[aria-selected="true"] { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 12px; }

/* สรุปสำหรับผู้บริหาร (หน้าบัตร) */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0; }
.tiles div { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; text-align: center; }
.tiles .n { display: block; font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.tiles .l { font-size: .78rem; color: var(--muted); }
@media (min-width: 560px) { .tiles { grid-template-columns: repeat(6, 1fr); } }
.row-between { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ตัวเลือกประเภทการลงเวลา (ปกติ / ลาป่วย / ลากิจ) */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg label { margin: 0; position: relative; color: var(--ink); font-size: .95rem; }
.seg input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; cursor: pointer; margin: 0; }
.seg span { display: flex; align-items: center; justify-content: center; text-align: center; min-height: 48px; padding: 8px 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.seg input:checked + span { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
textarea { resize: vertical; }

/* กล่องระบุเหตุผลมาสาย/ออกก่อนเวลา */
.card.reasoncard { border-color: var(--warn); background: var(--warn-bg); }
.card.reasoncard h2 { margin-top: 0; color: var(--warn); }
.reasonitem + .reasonitem { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--warn); }
.reasonitem .what { font-weight: 600; margin-bottom: 6px; }
.reasonitem .saved { margin: 6px 0 0; font-size: .9rem; }
