/* ================= ตัวแปรสี ================= */
:root {
  --navy: #1e4e79;
  --navy-d: #173c5e;
  --navy-l: #2f6ea5;
  --bg: #f2f5f8;
  --card: #ffffff;
  --ink: #1c2733;
  --muted: #6b7a89;
  --border: #dde4ea;
  --green: #1f8a4c;
  --amber: #b8730a;
  --red: #c0392b;
  --tint: #e9f0f7;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--ink);
  font-family: "Sarabun", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", "Noto Sans Thai", sans-serif;
  font-size: 16px; line-height: 1.6;
  overscroll-behavior-y: none;
}
h1, h2, h3 { margin: 0; font-weight: 700; }
button, input, textarea, select { font: inherit; color: inherit; }

.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 13px; }
.center { text-align: center; }
.err { color: var(--red); font-size: 14px; margin: 10px 0 0; }
[hidden] { display: none !important; }

/* ================= หน้าเข้าสู่ระบบ ================= */
.screen { min-height: 100dvh; }
#screen-login {
  background: linear-gradient(160deg, var(--navy) 0%, var(--navy-d) 100%);
  display: flex; align-items: center; justify-content: center;
  padding: calc(24px + var(--safe-t)) 20px calc(24px + var(--safe-b));
}
.login-wrap { width: 100%; max-width: 400px; }
.login-head { text-align: center; margin-bottom: 24px; color: #fff; }
.login-head h1 { font-size: 22px; margin-top: 12px; }
.login-head p { margin: 4px 0 0; color: #c9dcec; font-size: 14px; }
.login-foot { text-align: center; color: #a9c5db; font-size: 13px; margin-top: 18px; }

.brand-mark {
  width: 64px; height: 64px; margin: 0 auto; border-radius: 18px;
  background: #fff; position: relative;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.brand-mark::before, .brand-mark::after {
  content: ""; position: absolute; background: var(--navy); border-radius: 2px;
  transform-origin: bottom center; left: 50%; bottom: 50%;
}
.brand-mark::before { width: 3px; height: 18px; margin-left: -1.5px; }
.brand-mark::after  { width: 3px; height: 13px; margin-left: -1.5px; transform: rotate(105deg); }

/* ================= การ์ดและฟอร์ม ================= */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; margin-bottom: 14px;
}
.card h2 { font-size: 17px; margin-bottom: 10px; }

.lbl { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 6px; }
.inp {
  width: 100%; padding: 12px 12px; border: 1px solid var(--border);
  border-radius: 10px; background: #fff; font-size: 16px;
}
.inp:focus { outline: 2px solid var(--navy-l); outline-offset: -1px; border-color: var(--navy-l); }
textarea.inp { resize: vertical; min-height: 76px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ================= ปุ่ม ================= */
.btn {
  border: 0; border-radius: 12px; padding: 14px 18px; font-size: 16px;
  font-weight: 700; cursor: pointer; background: var(--navy); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { display: flex; width: 100%; margin-top: 12px; }
.btn-primary { background: var(--navy); }
.btn-success { background: var(--green); }
.btn-danger  { background: var(--red); }
.btn-outline { background: #fff; color: var(--navy); border: 1.5px solid var(--navy); }
.btn-outline.danger { color: var(--red); border-color: var(--red); }
.btn-mini {
  border: 0; background: #fff; color: var(--navy); border-radius: 8px;
  padding: 6px 12px; font-size: 13px; font-weight: 700; cursor: pointer;
}
.btn-mini.ghost { background: transparent; color: #cfe0ee; padding: 6px 8px; }
.icon-btn {
  background: rgba(255, 255, 255, .14); color: #fff; border: 0; border-radius: 10px;
  width: 38px; height: 38px; font-size: 19px; cursor: pointer;
}

/* ================= แถบบน / ล่าง ================= */
.appbar {
  background: var(--navy); color: #fff;
  padding: calc(12px + var(--safe-t)) 16px 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  position: sticky; top: 0; z-index: 20;
}
.appbar-title { font-size: 17px; font-weight: 700; }
.appbar-sub { font-size: 13px; color: #cfe0ee; }

#main {
  padding: 14px 14px calc(88px + var(--safe-b));
  max-width: 860px; margin: 0 auto;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--card); border-top: 1px solid var(--border);
  display: flex; padding: 6px 0 calc(4px + var(--safe-b));
}
.tabbar button {
  flex: 1; background: none; border: 0; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 11px; padding: 4px 0;
}
.tabbar button .ic { font-size: 20px; line-height: 1.1; opacity: .5; }
.tabbar button.active { color: var(--navy); font-weight: 700; }
.tabbar button.active .ic { opacity: 1; }

/* ================= นาฬิกา ================= */
.clock-card {
  background: var(--navy); color: #fff; border-radius: 14px;
  padding: 18px; text-align: center; margin-bottom: 14px;
}
.clock-date { font-size: 14px; color: #cfe0ee; }
.clock-time { font-size: 42px; font-weight: 700; letter-spacing: 1px;
              font-variant-numeric: tabular-nums; }
.clock-work { font-size: 13px; color: #cfe0ee; }

/* ================= รายการเข้า-ออก ================= */
.punch { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.punch + .punch { border-top: 1px solid var(--border); }
.punch img { width: 52px; height: 52px; border-radius: 10px; object-fit: cover;
             background: var(--border); flex: 0 0 52px; }
.punch .pmain { flex: 1; min-width: 0; }
.punch .pt { font-weight: 700; }
.punch .ps { font-size: 13px; color: var(--muted); }

.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; color: #fff; white-space: nowrap;
}
.b-ok { background: var(--green); }
.b-warn { background: var(--amber); }
.b-bad { background: var(--red); }
.b-mute { background: var(--muted); }
.pill {
  background: var(--tint); color: var(--navy); border-radius: 999px;
  padding: 1px 10px; font-size: 13px; margin-left: 4px;
}

/* ================= แถบชวนติดตั้ง ================= */
.install-bar {
  background: var(--navy-l); color: #fff; padding: 9px 14px;
  display: flex; align-items: center; gap: 10px; font-size: 13.5px;
}
.install-bar span { flex: 1; }

/* ================= รายการ / ตาราง ================= */
.item { border-top: 1px solid var(--border); padding: 11px 0; }
.item:first-child { border-top: 0; }
.item-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.item-title { font-weight: 700; }

.stats { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.stat { background: var(--bg); border-radius: 10px; padding: 7px 12px; min-width: 92px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 20px; font-weight: 700; }

.daysrow {
  display: flex; gap: 10px; padding: 7px 0; border-top: 1px solid var(--border);
  font-size: 13.5px; align-items: baseline;
}
.daysrow .d { width: 52px; flex: 0 0 52px; }
.daysrow .m { flex: 1; color: var(--muted); min-width: 0; }
.daysrow .s { font-weight: 700; font-size: 12.5px; white-space: nowrap; }

.seg { display: flex; gap: 10px; margin-bottom: 14px; }
.seg-btn {
  flex: 1; padding: 12px; border-radius: 12px; border: 1.5px solid var(--navy);
  background: #fff; color: var(--navy); font-weight: 700; cursor: pointer; font-size: 15px;
}
.seg-btn.active { background: var(--navy); color: #fff; }

.big-total { background: var(--navy); color: #fff; border-radius: 14px;
             padding: 16px; text-align: center; margin-bottom: 14px; }
.big-total .n { font-size: 36px; font-weight: 700; }
.big-total .k { font-size: 14px; color: #cfe0ee; }

/* ================= สวิตช์ตั้งค่า ================= */
.sw-row { display: flex; justify-content: space-between; align-items: center;
          gap: 12px; padding: 9px 0; border-top: 1px solid var(--border); }
.sw-row:first-of-type { border-top: 0; }
.sw { position: relative; width: 48px; height: 28px; flex: 0 0 48px; }
.sw input { opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.sw span {
  position: absolute; inset: 0; background: #c7d2dc; border-radius: 999px;
  transition: background .18s; pointer-events: none;
}
.sw span::after {
  content: ""; position: absolute; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; top: 3px; left: 3px; transition: transform .18s;
}
.sw input:checked + span { background: var(--navy-l); }
.sw input:checked + span::after { transform: translateX(20px); }

/* ================= กล้อง / ชีต ================= */
.sheet {
  position: fixed; inset: 0; z-index: 60; background: rgba(12, 22, 32, .82);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-inner {
  background: var(--card); width: 100%; max-width: 520px;
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + var(--safe-b));
  max-height: 94dvh; overflow: auto;
}
.dialog-inner { border-radius: 18px; margin: 0 12px 12px; }
.dialog-inner h3 { font-size: 17px; margin-bottom: 8px; }

.cam-head { font-weight: 700; font-size: 16px; text-align: center; margin-bottom: 10px; }
.cam-stage {
  position: relative; background: #0d1720; border-radius: 14px; overflow: hidden;
  aspect-ratio: 3 / 4; max-height: 56dvh; margin: 0 auto; display: flex;
}
.cam-stage video, .cam-stage img, .cam-stage canvas {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.cam-hint { text-align: center; color: var(--muted); font-size: 13.5px; margin: 10px 0; }
.cam-actions { display: flex; gap: 10px; margin-top: 8px; }
.cam-actions .btn { flex: 1; }
.cam-fallback {
  display: block; text-align: center; margin-top: 12px; font-size: 13.5px;
  color: var(--navy); text-decoration: underline; cursor: pointer;
}
.cam-fallback input { display: none; }

/* ================= overlay / toast ================= */
.overlay {
  position: fixed; inset: 0; z-index: 80; background: rgba(12, 22, 32, .55);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
}
.busy-text { color: #fff; font-size: 15px; }
.spinner {
  width: 42px; height: 42px; border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, .3); border-top-color: #fff;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast-wrap {
  position: fixed; left: 12px; right: 12px; bottom: calc(96px + var(--safe-b));
  z-index: 90; display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  background: #17293a; color: #fff; padding: 11px 16px; border-radius: 12px;
  font-size: 14.5px; max-width: 520px; width: 100%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25); white-space: pre-line;
}
.toast.ok { background: #14663a; }
.toast.bad { background: #93281c; }
.toast.warn { background: #8a5606; }

/* ================= จอใหญ่ ================= */
@media (min-width: 820px) {
  #main { padding: 20px 20px 40px; }
  .tabbar { position: sticky; bottom: 0; border-radius: 0; }
  .clock-time { font-size: 48px; }
}
