:root {
  --bg: #f3f5f4;
  --card: #ffffff;
  --text: #17201b;
  --muted: #6b7570;
  --line: #e3e7e5;
  --green: #06c755;
  --green-d: #04a547;
  --red: #e5484d;
  --red-d: #c93a3f;
  --blue: #2f6fed;
  --amber: #f5a524;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
svg { width: 1.2em; height: 1.2em; flex: none; }
button { font: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; }
input, select { font: inherit; }

#app { max-width: 520px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; }
.view { display: none; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }
.view.active { display: block; }

.h1 { font-size: 1.35rem; margin: 8px 0 4px; font-weight: 700; }
.h2 { font-size: 1.05rem; margin: 0 0 10px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.req { color: var(--red); font-weight: 600; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.narrow { margin-top: 6vh; }
.center-box { min-height: 70vh; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--green); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.avatar { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; background: #dfe5e2; }
.avatar.lg { width: 72px; height: 72px; display: block; margin: 0 auto 4px; }
.avatar[src=""], .avatar:not([src]) { visibility: hidden; }
.card .avatar.lg[src=""] { display: none; }

/* forms */
.form { display: grid; gap: 14px; margin-top: 16px; }
.form label { display: grid; gap: 6px; font-weight: 500; font-size: .95rem; }
.form input, .input {
  width: 100%; padding: 12px 14px; border: 1.5px solid var(--line); border-radius: 12px;
  background: #fff; color: var(--text); font-size: 16px;
}
.form input:focus, .input:focus { outline: none; border-color: var(--green); }
.pos-list { display: grid; gap: 8px; }
.pos-opt { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border: 1.5px solid var(--line); border-radius: 12px; font-weight: 400; }
.pos-opt input { width: 20px; height: 20px; accent-color: var(--green); margin: 0; flex: none; }
.pos-opt small { display: block; color: var(--muted); }
.pos-opt:has(input:checked) { border-color: var(--green); background: #effbf3; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 13px 18px; border-radius: 12px; border: 0; font-weight: 600; font-size: 1rem; }
.btn-block { width: 100%; margin-top: 10px; }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:active { background: var(--green-d); }
.btn-primary:disabled { background: #9adbb3; }
.btn-ghost { background: #eef1f0; color: var(--text); }
.btn-light { background: #fff; color: var(--text); }

/* home */
.top { display: flex; align-items: center; gap: 12px; padding: 4px 2px 14px; }
.who { flex: 1; min-width: 0; }
.who-name { font-weight: 700; font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-pos { color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-code { font-family: ui-monospace, Consolas, monospace; font-size: .78rem; color: var(--muted); }
.who-code:empty { display: none; }
.company { font-size: .75rem; color: var(--muted); text-align: right; max-width: 34%; }

.clock-card { background: linear-gradient(145deg, #0f2a1c, #164a2f); color: #fff; border-radius: 20px; padding: 22px 20px 18px; text-align: center; box-shadow: var(--shadow); }
.clock { font-size: 3rem; font-weight: 700; letter-spacing: 1px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.clock-date { opacity: .8; margin-top: 2px; }
.shift-status { margin-top: 12px; display: inline-flex; gap: 6px; align-items: center; padding: 5px 12px; border-radius: 99px; background: rgba(255,255,255,.12); font-size: .9rem; }
.shift-status .dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa5a0; }
.shift-status.on .dot { background: #3ee07f; box-shadow: 0 0 0 4px rgba(62,224,127,.25); }

.actions { display: grid; gap: 12px; margin: 16px 0 22px; }
.btn-punch {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  width: 100%; padding: 22px; border: 0; border-radius: 18px; color: #fff;
  font-size: 1.5rem; font-weight: 700; background: var(--green);
  box-shadow: 0 6px 18px rgba(6,199,85,.35);
}
.btn-punch svg { width: 30px; height: 30px; }
.btn-punch:active { transform: scale(.99); background: var(--green-d); }
.btn-punch.out { background: var(--red); box-shadow: 0 6px 18px rgba(229,72,77,.3); }
.btn-punch.out:active { background: var(--red-d); }
.btn-visit {
  display: flex; align-items: center; gap: 14px; width: 100%; padding: 16px 18px;
  border: 2px solid var(--blue); border-radius: 18px; background: #fff; color: var(--blue); text-align: left;
}
.btn-visit svg { width: 30px; height: 30px; }
.btn-visit b { display: block; font-size: 1.15rem; }
.btn-visit small { color: var(--muted); font-size: .85rem; }
.btn-visit:active { background: #f0f5ff; }

.block { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
details summary { cursor: pointer; list-style: none; margin: 0; }
details summary::-webkit-details-marker { display: none; }
details summary::after { content: "▾"; float: right; color: var(--muted); }
details[open] summary { margin-bottom: 10px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.tl-item { display: grid; grid-template-columns: 22px 1fr 56px; gap: 10px; position: relative; padding-bottom: 14px; }
.tl-item:not(:last-child)::before { content: ""; position: absolute; left: 10px; top: 22px; bottom: 0; width: 2px; background: var(--line); }
.tl-dot { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: #fff; background: var(--blue); margin-top: 2px; }
.tl-dot svg { width: 13px; height: 13px; }
.tl-item.in .tl-dot { background: var(--green); }
.tl-item.out .tl-dot { background: var(--red); }
.tl-main { min-width: 0; }
.tl-head { font-weight: 600; }
.tl-head time { font-variant-numeric: tabular-nums; margin-right: 6px; }
.tl-sub { color: var(--muted); font-size: .85rem; overflow-wrap: anywhere; }
.tl-thumb { width: 56px; height: 56px; border-radius: 10px; object-fit: cover; background: #e8ecea; }
.empty { color: var(--muted); font-size: .9rem; padding: 6px 0; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: .75rem; font-weight: 600; vertical-align: middle; }
.badge.ok { background: #e3f8eb; color: #087a3a; }
.badge.warn { background: #fff3dc; color: #9a5b00; }
.badge.bad { background: #fde8e8; color: #b42318; }
.badge.info { background: #e8efff; color: #2450b8; }

.hist { list-style: none; margin: 0; padding: 0; }
.hist li { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: .9rem; }
.hist li:last-child { border-bottom: 0; }
.hist .d { color: var(--muted); }
.foot { text-align: center; margin-top: 8px; }

/* capture */
#v-capture { padding: 0; background: #0c0f0d; min-height: 100vh; min-height: 100dvh; color: #fff; }
.cap-top { display: flex; align-items: center; gap: 8px; padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top)); }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: rgba(255,255,255,.12); color: #fff; display: grid; place-items: center; }
.cap-title { flex: 1; font-weight: 700; font-size: 1.1rem; }
.cap-clock { font-variant-numeric: tabular-nums; opacity: .85; }
.cam-wrap { position: relative; width: 100%; aspect-ratio: 3 / 4; max-height: 52vh; background: #000; overflow: hidden; }
#video, #shot { width: 100%; height: 100%; object-fit: cover; display: block; }
#video { transform: scaleX(-1); }
.face-guide { position: absolute; left: 50%; top: 45%; width: 55%; aspect-ratio: 3/4; transform: translate(-50%, -50%); border: 2px dashed rgba(255,255,255,.5); border-radius: 50%; pointer-events: none; }
.cam-msg { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; padding: 24px; text-align: center; background: #111; }
.cap-panel { background: #fff; color: var(--text); border-radius: 20px 20px 0 0; margin-top: -16px; position: relative; padding: 16px 16px calc(18px + env(safe-area-inset-bottom)); }
.gps-row { display: flex; gap: 10px; align-items: center; }
.gps-ico { width: 26px; height: 26px; color: var(--amber); }
.gps-row.ok .gps-ico { color: var(--green); }
.gps-row.bad .gps-ico { color: var(--red); }
.gps-row.wait .gps-ico { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.gps-text { display: grid; line-height: 1.35; min-width: 0; }
.gps-text span { color: var(--muted); font-size: .85rem; }
.mini-map { height: 130px; border-radius: 12px; margin: 12px 0; background: #e8ecea; z-index: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.chip { border: 1.5px solid var(--line); background: #fff; border-radius: 99px; padding: 7px 14px; font-size: .95rem; color: var(--text); }
.chip.on { border-color: var(--blue); background: var(--blue); color: #fff; }
.cap-buttons { margin-top: 14px; display: grid; justify-items: center; gap: 6px; }
.cap-buttons.two { grid-template-columns: 1fr 2fr; justify-items: stretch; gap: 10px; }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid var(--green); background: #fff; padding: 5px; }
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--green); }
.shutter:disabled { border-color: #c9d1cd; }
.shutter:disabled span { background: #c9d1cd; }
.shutter:not(:disabled):active span { transform: scale(.92); }

/* done */
.done-ico { width: 76px; height: 76px; border-radius: 50%; margin: 6px auto 4px; display: grid; place-items: center; background: var(--green); color: #fff; }
.done-ico svg { width: 40px; height: 40px; }
.done-ico.out { background: var(--red); }
.done-ico.visit { background: var(--blue); }
.done-time { font-size: 2.2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.done-photo { width: 100%; border-radius: 12px; margin: 14px 0 4px; }
.done-photo:not([src]), .done-photo[src=""] { display: none; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #1c2420; color: #fff; padding: 12px 18px; border-radius: 12px; max-width: calc(100% - 32px); z-index: 9999; box-shadow: 0 8px 24px rgba(0,0,0,.25); font-size: .95rem; }

/* shutter on top of the camera image */
.cam-bar { position: absolute; left: 0; right: 0; bottom: 26px; display: grid; justify-items: center; gap: 8px; padding-top: 40px;
  background: linear-gradient(to top, rgba(0,0,0,.55), rgba(0,0,0,0)); }
.cam-bar .shutter { box-shadow: 0 4px 16px rgba(0,0,0,.35); }
.cam-bar .shutter:disabled { border-color: rgba(255,255,255,.6); background: rgba(255,255,255,.25); }
.cam-bar .shutter:disabled span { background: rgba(255,255,255,.55); }
.cam-hint { color: #fff; font-size: .9rem; text-shadow: 0 1px 3px rgba(0,0,0,.6); padding: 0 16px; text-align: center; }
.shot-badge { position: absolute; top: 12px; left: 12px; display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px;
  border-radius: 99px; background: var(--green); color: #fff; font-weight: 600; font-size: .9rem; box-shadow: 0 2px 8px rgba(0,0,0,.3); }
.shot-badge svg { width: 16px; height: 16px; }
.retake-btn { position: absolute; bottom: 34px; left: 50%; transform: translateX(-50%); display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 20px; border: 0; border-radius: 99px; background: rgba(255,255,255,.92); color: var(--text); font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,.3); }
#cap-confirm { margin-top: 14px; }
#cap-confirm .btn { margin-top: 0; padding: 15px 18px; font-size: 1.05rem; }
.cap-panel .mini-map { margin: 14px 0 0; }
.cap-panel #visit-fields { margin-top: 12px; }
