:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e6ea;
  --primary: #1d6fd8;
  --primary-text: #ffffff;
  --ok-bg: #e7f6ec;   --ok-text: #1b6b34;
  --warn-bg: #fff6e0; --warn-text: #8a5a00;
  --err-bg: #fdecec;  --err-text: #a12626;
  --in: #1b8a4a;
  --out: #b4541a;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1c1f25;
    --text: #e8eaed;
    --muted: #9aa0a8;
    --border: #2c3038;
    --primary: #4c93ec;
    --ok-bg: #16301f;   --ok-text: #8fdca8;
    --warn-bg: #33290f; --warn-text: #f0c46a;
    --err-bg: #3a1a1a;  --err-text: #f2a3a3;
    --in: #4cc483;
    --out: #f0935a;
  }
}

* { box-sizing: border-box; }
/* CSS display của class (vd .preview { display: block }) không được ghi đè thuộc tính hidden */
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--primary); }
h1 { font-size: 1.3rem; margin: 0 0 .5rem; }
h2 { font-size: 1.05rem; margin: 0 0 .25rem; display: flex; align-items: center; gap: .5rem; }

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; }
.who { display: flex; align-items: center; gap: .75rem; font-size: .9rem; }
.who form { margin: 0; }

.menu {
  display: flex; gap: .25rem; overflow-x: auto; padding: .4rem 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.menu a {
  white-space: nowrap; padding: .45rem .8rem; border-radius: 999px;
  color: var(--text); text-decoration: none; font-size: .92rem;
}
.menu a.active { background: var(--primary); color: var(--primary-text); }

.container { max-width: 760px; margin: 0 auto; padding: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px;
}
.card.narrow { max-width: 400px; margin: 10vh auto 0; }
.row-between { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.warn { color: var(--warn-text); }

.step {
  display: inline-grid; place-items: center; width: 1.6rem; height: 1.6rem; border-radius: 50%;
  background: var(--primary); color: var(--primary-text); font-size: .85rem;
}

.form { display: grid; gap: .8rem; }
.form label, label.form { display: grid; gap: .3rem; font-size: .92rem; }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: .6rem .7rem; width: 100%;
}
form.inline input { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  font: inherit; font-weight: 600; cursor: pointer;
  padding: .65rem 1.1rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; padding: .9rem; font-size: 1.05rem; margin-top: .8rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
button.link { background: none; border: 0; color: var(--primary); font: inherit; cursor: pointer; padding: 0; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .5rem; }

.alert, .result { border-radius: 10px; padding: .7rem .9rem; margin: .7rem 0 0; font-size: .92rem; }
.alert { margin: 0 0 14px; }
.alert.error, .result.error { background: var(--err-bg); color: var(--err-text); }
.result.ok { background: var(--ok-bg); color: var(--ok-text); }
.result.warn { background: var(--warn-bg); color: var(--warn-text); }
.result { background: var(--bg); }

.status { margin: .25rem 0 .75rem; color: var(--muted); }
.status.on { color: var(--in); font-weight: 600; }

.preview { display: block; max-width: 100%; max-height: 320px; margin-top: .8rem; border-radius: 10px; }
.webcam video { width: 100%; max-height: 360px; background: #000; border-radius: 10px; margin-top: .8rem; }

.records { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; }
.record { display: flex; gap: .75rem; align-items: center; }
.record img, .thumb { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.badge { display: inline-block; padding: .05rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 700; color: #fff; }
.badge.in { background: var(--in); }
.badge.out { background: var(--out); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
td .ua { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.diag { margin-top: .8rem; }
table.diag td:first-child { width: 2rem; }
.tips { margin: .5rem 0 0; padding-left: 1.2rem; }
.tips li { margin: .2rem 0; }

/* Bản đồ vị trí realtime */
.map-wrap { margin-top: .8rem; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
#map { height: 280px; width: 100%; background: var(--bg); }
.live-bar { display: flex; align-items: center; gap: .5rem; padding: .45rem .7rem; background: var(--surface); flex-wrap: wrap; }
.live-bar .link { margin-left: auto; }
.live-dot { width: .6rem; height: .6rem; border-radius: 50%; background: var(--muted); flex: none; }
.live-dot.on { background: #1fa855; animation: live-pulse 1.6s infinite; }
.live-dot.refining { background: #e0a100; animation: live-pulse 1s infinite; }
@keyframes live-pulse {
  0% { box-shadow: 0 0 0 0 rgba(31, 168, 85, .55); }
  70% { box-shadow: 0 0 0 .5rem rgba(31, 168, 85, 0); }
  100% { box-shadow: 0 0 0 0 rgba(31, 168, 85, 0); }
}
@media (prefers-reduced-motion: reduce) { .live-dot.on, .live-dot.refining { animation: none; } }

/* ===== Bổ sung cho SSO / quản trị (dùng chung với Chấm công) ===== */
select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: .55rem .6rem; width: 100%;
}
input[type=checkbox], input[type=radio] { width: auto; }
.container.wide { max-width: 1600px; }
.alert.ok { background: var(--ok-bg); color: var(--ok-text); }
.alert.warn { background: var(--warn-bg); color: var(--warn-text); }
.field-error { color: var(--err-text); font-size: .82rem; }
.grid-2 { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.checks { display: flex; flex-wrap: wrap; gap: .4rem 1.2rem; }
.checks label { display: flex; align-items: center; gap: .4rem; font-size: .92rem; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin: .25rem 0 1rem; }
.tabs a { padding: .5rem .9rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: end; margin-bottom: .8rem; }
.filters > * { flex: 1 1 160px; }
.filters .btn { flex: 0 0 auto; }
.pager { display: flex; gap: .5rem; align-items: center; justify-content: flex-end; margin-top: .8rem; font-size: .9rem; }
.pill { display: inline-block; padding: .05rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600;
        background: var(--bg); border: 1px solid var(--border); color: var(--muted); margin: 0 .2rem .2rem 0; }
.pill.ok { background: var(--ok-bg); color: var(--ok-text); border-color: transparent; }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }
.pill.err { background: var(--err-bg); color: var(--err-text); border-color: transparent; }
.secret { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 1rem; padding: .7rem; border-radius: 8px;
          background: var(--bg); border: 1px dashed var(--border); word-break: break-all; user-select: all; }
.otp-input { font-size: 1.6rem; letter-spacing: .5rem; text-align: center; }
.brand-title { text-align: center; margin-bottom: 1rem; }
.brand-title .logo { font-weight: 800; font-size: 1.15rem; color: var(--primary); }
code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .88em; }

/* ===== F0.AppShell: thanh trên + menu dọc (nhóm) + menu ngang (trang trong nhóm) — dùng chung SSO & Chấm công ===== */
:root { --appbar-h: 56px; --sidebar-w: 248px; --admin-accent: #6d4bd8; }
body.mode-admin { --primary: var(--admin-accent); }
@media (prefers-color-scheme: dark) { :root { --admin-accent: #a58bff; } }
.appbar {
  position: sticky; top: 0; z-index: 30; height: var(--appbar-h);
  display: flex; align-items: center; gap: .75rem; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.appbar .brand { font-weight: 800; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand-sub { font-weight: 500; color: var(--muted); margin-left: .35rem; }
.appbar-right { margin-left: auto; display: flex; align-items: center; gap: .6rem; min-width: 0; }
.mode-switch { display: inline-flex; margin: 0; border: 1px solid var(--border); border-radius: 999px; padding: 2px; background: var(--bg); }
.mode-switch button { border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .82rem; font-weight: 600;
  padding: .3rem .7rem; border-radius: 999px; cursor: pointer; white-space: nowrap; }
.mode-switch button.on { background: var(--primary); color: var(--primary-text); }
.usermenu { position: relative; }
.usermenu summary { list-style: none; cursor: pointer; font-size: .9rem; font-weight: 600; max-width: 180px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: .35rem .5rem; border-radius: 8px; }
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu summary::after { content: ' ▾'; color: var(--muted); }
.usermenu-panel { position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; z-index: 40; display: grid; gap: .5rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: .8rem; box-shadow: 0 8px 24px rgba(0,0,0,.12); }
.usermenu-panel form { margin: 0; }

.shell { display: flex; min-height: calc(100vh - var(--appbar-h)); }
.sidebar { width: var(--sidebar-w); flex: none; background: var(--surface); border-right: 1px solid var(--border);
  padding: .8rem .6rem 2rem; position: sticky; top: var(--appbar-h); height: calc(100vh - var(--appbar-h)); overflow-y: auto; }
.nav-group { margin-bottom: .9rem; }
.nav-group-title { font-size: .72rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
  padding: .3rem .6rem; }
.nav-group.active .nav-group-title { color: var(--primary); }
.sidebar a { display: block; padding: .45rem .6rem; border-radius: 8px; color: var(--text); text-decoration: none; font-size: .92rem; }
.sidebar a:hover { background: var(--bg); }
.sidebar a.active { background: color-mix(in srgb, var(--primary) 14%, transparent); color: var(--primary); font-weight: 600; }
.content { flex: 1; min-width: 0; }
.pagehead { background: var(--surface); border-bottom: 1px solid var(--border); padding: .5rem 16px 0; }
.crumbs { font-size: .8rem; color: var(--muted); padding: .1rem 0 .2rem; }
.crumbs span { margin: 0 .25rem; }
.subnav { display: flex; gap: .25rem; overflow-x: auto; scrollbar-width: none; }
.subnav::-webkit-scrollbar { display: none; }
.subnav a { white-space: nowrap; padding: .55rem .8rem; color: var(--muted); text-decoration: none; font-size: .92rem;
  border-bottom: 2px solid transparent; }
.subnav a:hover { color: var(--text); }
.subnav a.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }
.nav-badge { display: inline-block; min-width: 1.3rem; padding: 0 .35rem; margin-left: .35rem; border-radius: 999px;
  background: #d9480f; color: #fff; font-size: .72rem; font-weight: 700; text-align: center; line-height: 1.25rem; }
a:focus-visible, button:focus-visible, summary:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.nav-toggle { display: none; width: 40px; height: 40px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  cursor: pointer; position: relative; flex: none; }
.nav-toggle span, .nav-toggle span::before, .nav-toggle span::after { position: absolute; left: 11px; width: 16px; height: 2px;
  background: var(--text); border-radius: 2px; content: ''; }
.nav-toggle span { top: 19px; }
.nav-toggle span::before { top: -5px; left: 0; }
.nav-toggle span::after { top: 5px; left: 0; }
.sidebar-backdrop { display: none; }

@media (max-width: 900px) {
  .nav-toggle { display: inline-block; }
  .brand-sub { display: none; }
  .sidebar { position: fixed; left: 0; top: var(--appbar-h); bottom: 0; height: auto; z-index: 35;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 24px rgba(0,0,0,.18); }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .sidebar-backdrop { display: block; position: fixed; inset: var(--appbar-h) 0 0 0; z-index: 34; background: rgba(0,0,0,.35); }
  .usermenu summary { max-width: 110px; }
}
.appbar .brand-mark { display: none; height: 28px; width: auto; }
@media (max-width: 480px) {
  .mode-switch button { padding: .3rem .5rem; font-size: .76rem; }
  .appbar { gap: .5rem; padding: 0 10px; }
  .appbar .brand-mark { display: block; }   /* màn nhỏ: logo thu gọn thay chữ để không tràn ngang */
  .appbar .brand-text { display: none; }
  .usermenu summary { max-width: 96px; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }

/* Chọn quyền khi đăng nhập */
.role-pick { border: 0; padding: 0; margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 0; }
.role-pick legend { font-size: .92rem; margin-bottom: .3rem; padding: 0; }
.role-pick label { display: block; cursor: pointer; }
.role-pick input { position: absolute; opacity: 0; pointer-events: none; }
.role-pick span { display: block; text-align: center; padding: .6rem; border: 1px solid var(--border); font-weight: 600; color: var(--muted); }
.role-pick label:first-of-type span { border-radius: 10px 0 0 10px; }
.role-pick label:last-of-type span { border-radius: 0 10px 10px 0; border-left: 0; }
.role-pick input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.role-pick input[value=admin]:checked + span { background: var(--admin-accent); border-color: var(--admin-accent); }
.role-pick input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Logo lớn ở màn đăng nhập / đã đăng xuất */
.brand-logo-lg { display: block; width: min(220px, 70%); height: auto; margin: 0 auto .4rem; }
.login-logo { text-align: center; margin-bottom: .8rem; }

/* Nhóm trường gập/mở trong form */
details.form-section { border: 1px solid var(--border); border-radius: 10px; padding: .6rem .8rem; }
details.form-section > summary { cursor: pointer; font-weight: 600; }
details.form-section[open] > summary { margin-bottom: .7rem; }
details.form-section > label, details.form-section .grid-2 { margin-bottom: .7rem; }
details.form-section label { display: grid; gap: .3rem; font-size: .92rem; }

/* Vai trò + khung giải thích quyền khi rê chuột / chạm (form nhân viên) */
.role-list { display: grid; gap: .5rem; }
.role-option { position: relative; display: flex; align-items: center; gap: .5rem; padding: .55rem .7rem;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.role-option label { display: flex; align-items: center; gap: .45rem; cursor: pointer; flex: 1; font-size: .92rem; }
.role-option.locked { opacity: .65; }
.role-option.role-base { background: var(--bg); justify-content: space-between; font-size: .92rem; }
.role-option:has(input:checked) { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 7%, var(--surface)); }
.role-info { flex: none; width: 1.4rem; height: 1.4rem; border-radius: 50%; display: inline-grid; place-items: center;
  font-size: .78rem; font-weight: 700; color: var(--muted); border: 1px solid var(--border); cursor: help; }
.role-tip { display: none; position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 6px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: .7rem .85rem;
  box-shadow: 0 10px 28px rgba(0,0,0,.16); font-size: .86rem; line-height: 1.45; }
/* Chỉ hiện khi hover vào chữ (tên vai trò) hoặc biểu tượng ?, không hiện khi hover / focus ô tích chọn */
.role-option:has(.role-name:hover) .role-tip, .role-option:has(.role-info:hover) .role-tip,
.role-option:has(.role-info:focus) .role-tip { display: block; }
.role-name { cursor: help; }
.role-tip-app { font-weight: 700; color: var(--primary); margin-bottom: .25rem; }
.role-tip-h { font-weight: 600; margin-top: .35rem; }
.role-tip ul { margin: .15rem 0 0; padding-left: 1.1rem; }
.role-tip ul.can li::marker { content: "✓  "; color: var(--ok-text); }
.role-tip ul.cannot li::marker { content: "✕  "; color: var(--err-text); }
.role-tip-note { margin-top: .45rem; color: var(--muted); font-size: .8rem; }
@media (hover: none) { .role-info { cursor: pointer; } }

td.hours { white-space: nowrap; }
td.hours input { width: 6.4rem; display: inline-block; }
@media (max-width: 1279px) {   /* màn hẹp: 2 ô giờ xếp dọc để bảng văn phòng vừa màn hình */
  td.hours { white-space: normal; }
  td.hours input { display: block; width: 6.4rem; margin: .1rem 0; }
}
@media (min-width: 768px) and (max-width: 1279px) {
  .table-wrap td input:not([type=checkbox]):not([type=time]), .table-wrap td select { min-width: 4.5rem; }
  .table-wrap td input[name=address], .table-wrap td input[name=name] { min-width: 7rem; }
  table.form-table th, table.form-table td { padding: .4rem .3rem; }
  table.form-table td input:not([type=checkbox]), table.form-table td select { padding: .5rem .45rem; }
  table.form-table td .btn { padding: .45rem .7rem; }
}
@media (min-width: 768px) and (max-width: 1023px) {
  table.form-table td input:not([type=checkbox]):not([type=time]), table.form-table td select { min-width: 4rem; }
}

/* Bảng dữ liệu
   - Máy tính (≥ 768px): bảng luôn vừa màn hình, không cuộn ngang. Ô chỉ xuống dòng khi thật sự thiếu chỗ và chỉ
     giữa các từ (không bẻ giữa từ); nhãn, mã, giờ, tiêu đề cột không bao giờ bị bẻ.
   - Điện thoại (< 768px): mỗi ô 1 dòng, bảng rộng thì cuộn ngang trong .table-wrap. */
th { white-space: nowrap; }
.pill, .badge, td code, td time, .nowrap { white-space: nowrap; }
.table-wrap { -webkit-overflow-scrolling: touch; }
.table-wrap td { vertical-align: middle; }
.table-wrap td.wrap .pill { margin: .1rem 0; }
.table-wrap td input:not([type=checkbox]):not([type=time]), .table-wrap td select { min-width: 6rem; }
.table-wrap td input[name=address], .table-wrap td input[name=name] { min-width: 10rem; }
td .secret { overflow-wrap: anywhere; }
td.wrap code { white-space: normal; overflow-wrap: anywhere; }
@media (max-width: 767px) {
  .table-wrap td { white-space: nowrap; }
  .table-wrap td.wrap { white-space: normal; min-width: 12rem; max-width: 26rem; overflow-wrap: anywhere; }
  .table-wrap td input:not([type=checkbox]):not([type=time]), .table-wrap td select { min-width: 9rem; }
  .table-wrap td input[name=address], .table-wrap td input[name=name] { min-width: 16rem; }
}
table.kv th { white-space: normal; width: 40%; }   /* bảng nhãn–giá trị: nhãn dài được xuống dòng trên điện thoại */
table.kv td { overflow-wrap: anywhere; }

/* F24 Ca làm việc */
table.shift-days { max-width: 520px; }
table.shift-days tr.is-off td:not(:nth-child(-n+2)) { opacity: .4; }
table.shift-days input[type=time] { width: 7.5rem; }
.btn.danger { color: var(--err-text); border-color: color-mix(in srgb, var(--err-text) 35%, var(--border)); }
.btn.danger:hover { background: var(--err-bg); }
.row-actions { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.row-actions form { margin: 0; }
.row-actions .btn { padding: .35rem .75rem; font-size: .88rem; }
.push-right { margin-left: auto; }
.nowrap { white-space: nowrap; }
@media (min-width: 768px) and (max-width: 1439px) {
  .table-wrap td.roles { width: 1%; }   /* màn vừa: vai trò xếp dọc, nhường chỗ để tên / phòng ban / văn phòng không xuống dòng */
}
