:root {
  --primary:#207d57; --primary-dark:#1a6646; --primary-soft:#e6f2ec;
  --bg:#f8f7fa; --card:#fff; --line:#dbdade; --text:#4b465c; --muted:#8a8795;
  --danger:#ea5455; --danger-dark:#c94344; --ok:#28c76f;
}
* { box-sizing: border-box; }
body { margin:0; font:17px/1.55 "Public Sans","Segoe UI",system-ui,sans-serif; background:var(--bg); color:var(--text); }
body.center { display:grid; place-items:center; min-height:100vh; padding:16px; }
header { display:flex; justify-content:space-between; align-items:center; gap:12px; padding:10px 24px; background:var(--card); border-bottom:1px solid var(--line); box-shadow:0 2px 6px rgba(75,70,92,.08); }
.brand { display:flex; align-items:center; gap:16px; min-width:0; }
.logo { height:44px; width:auto; }
.logo-lg { display:block; margin:0 auto 14px; width:260px; max-width:80%; height:auto; }
.title { font-weight:600; color:var(--primary); border-left:2px solid var(--line); padding-left:16px; }
h1 { margin:0 0 4px; font-size:23px; color:var(--primary); }
main { max-width:960px; margin:24px auto; padding:0 16px; }
nav { display:flex; gap:8px; flex-wrap:wrap; margin-bottom:14px; }
.card { background:var(--card); border:1px solid var(--line); border-radius:8px; padding:22px; box-shadow:0 2px 8px rgba(75,70,92,.06); }
.narrow { width:100%; max-width:440px; text-align:center; }
.narrow form { text-align:left; }
.panel label { display:block; margin:0 0 16px; font-weight:600; font-size:16px; }
input, select, textarea { display:block; width:100%; margin-top:5px; padding:11px 14px; font:inherit; font-weight:400; color:var(--text); border:1px solid var(--line); border-radius:6px; background:#fff; }
input:focus, select:focus, textarea:focus { outline:0; border-color:var(--primary); box-shadow:0 0 0 3px rgba(32,125,87,.15); }
textarea { font-family:ui-monospace,Consolas,monospace; }
input.otp { text-align:center; font-size:30px; letter-spacing:6px; margin:12px 0; }
input.otp-sm { max-width:220px; text-align:center; font-size:24px; letter-spacing:4px; }
fieldset { border:1px solid var(--line); border-radius:6px; margin:0 0 16px; }
legend { font-size:16px; font-weight:600; padding:0 6px; }
label.inline { display:block; font-weight:400; margin:4px 0; }
label.inline input { display:inline; width:auto; margin-right:6px; accent-color:var(--primary); }
button { padding:11px 22px; font:inherit; font-weight:600; border:1px solid var(--primary); border-radius:6px; background:var(--primary); color:#fff; cursor:pointer; }
button:hover:not(:disabled) { background:var(--primary-dark); }
button:disabled { opacity:.45; cursor:not-allowed; }
button.block { width:100%; }
button.ghost { background:#fff; color:var(--primary); }
button.ghost:hover:not(:disabled) { background:var(--primary-soft); }
button.danger { background:var(--danger); border-color:var(--danger); }
button.danger:hover:not(:disabled) { background:var(--danger-dark); }
button.tab { background:#fff; color:var(--text); border:1px solid var(--line); font-weight:500; }
button.tab:hover:not(:disabled) { background:var(--primary-soft); }
button.tab.active { background:var(--primary); border-color:var(--primary); color:#fff; font-weight:600; }
.row { display:flex; gap:10px; flex-wrap:wrap; }
.muted { color:var(--muted); }
.error { color:var(--danger); font-weight:600; }
.badge { display:inline-block; padding:3px 14px; border-radius:14px; font-size:14px; font-weight:700; }
.badge.on { background:rgba(40,199,111,.16); color:#1a8d4e; }
.badge.off { background:rgba(234,84,85,.14); color:var(--danger); }
.qr { display:block; width:210px; height:210px; border:1px solid var(--line); border-radius:6px; padding:6px; background:#fff; }
code { word-break:break-all; background:var(--primary-soft); padding:2px 6px; border-radius:4px; }
.result { margin-top:16px; border:1px solid var(--line); border-radius:8px; overflow:hidden; background:#fff; }
.result-title { padding:10px 16px; font-size:15px; font-weight:700; color:var(--primary-dark); background:var(--primary-soft); border-bottom:1px solid var(--line); }
.result.err { border-color:#f3b4b4; } .result.err .result-title { color:#b3262a; background:#fdeaea; border-color:#f3b4b4; }
.out { margin:0; padding:14px 16px; background:#2f3349; color:#e7e3fc; white-space:pre-wrap; word-break:break-word; font:15.5px/1.65 ui-monospace,Consolas,monospace; }
.out.err { color:#ffb4b4; }
[hidden] { display:none !important; }
@media (max-width:560px) { .title { display:none; } header { padding:10px 16px; } }

/* ---- additions: spinner, users, audit */
@keyframes spin { to { transform: rotate(360deg); } }
button.loading { position:relative; color:transparent !important; pointer-events:none; opacity:1 !important; }
button.loading::after { content:""; position:absolute; left:50%; top:50%; width:16px; height:16px; margin:-8px 0 0 -8px; border:2px solid rgba(255,255,255,.45); border-top-color:#fff; border-radius:50%; animation:spin .7s linear infinite; }
button.ghost.loading::after { border-color:rgba(32,125,87,.3); border-top-color:var(--primary); }
.out.loading::before { content:""; display:inline-block; width:12px; height:12px; margin-right:8px; vertical-align:-1px; border:2px solid rgba(231,227,252,.35); border-top-color:#e7e3fc; border-radius:50%; animation:spin .7s linear infinite; }
.userbox { display:flex; align-items:center; gap:14px; }
.who small { color:var(--muted); }
.banner { background:#fff6e0; border:1px solid #f3cf7a; color:#7a5a00; padding:12px 16px; border-radius:6px; margin-bottom:14px; font-weight:600; }
.msg { margin:12px 0 0; font-weight:600; }
.msg.ok { color:#1a8d4e; } .msg.bad { color:var(--danger); }
.bad { color:var(--danger); }
.small { font-size:14px; } .small-btn { padding:5px 14px; }
.center-link { text-align:center; margin:14px 0 0; }
a { color:var(--primary); }
button.danger-text { color:var(--danger); border-color:var(--danger); }
button.danger-text:hover:not(:disabled) { background:rgba(234,84,85,.08); }
.stack label { display:block; margin:0 0 16px; font-weight:600; font-size:16px; }
h2 { font-size:19px; margin:0 0 12px; color:var(--primary); } h3 { margin:0 0 12px; font-size:17px; }
hr { border:0; border-top:1px solid var(--line); margin:24px 0; }
.row.between { justify-content:space-between; align-items:center; }
.grid2 { display:grid; grid-template-columns:1fr 1fr; gap:0 16px; }
.subcard { margin-top:18px; padding:16px; border:1px solid var(--line); border-radius:8px; background:var(--bg); }
.filters { display:flex; gap:8px; flex-wrap:wrap; margin-bottom:12px; }
.filters input, .filters select { width:auto; flex:1 1 150px; margin:0; }
.tablewrap { overflow-x:auto; }
table { width:100%; border-collapse:collapse; font-size:15px; }
th, td { padding:8px 10px; text-align:left; border-bottom:1px solid var(--line); vertical-align:top; }
th { background:var(--primary-soft); color:var(--primary-dark); white-space:nowrap; }
td.detail { word-break:break-word; min-width:220px; font-family:ui-monospace,Consolas,monospace; font-size:14px; }
tr.fail td { background:rgba(234,84,85,.06); }
tr.inactive td { color:var(--muted); }
.pager { align-items:center; margin-top:12px; }
@media (max-width:560px) { .grid2 { grid-template-columns:1fr; } .userbox .who { display:none; } }

/* ---- show/hide password */
.pw-wrap { position:relative; display:block; }
.pw-wrap input { padding-right:76px; }
button.pw-toggle { position:absolute; right:5px; top:50%; transform:translateY(-50%); margin-top:2px; padding:5px 12px; font-size:14px; font-weight:600; background:transparent; color:var(--primary); border:0; border-radius:4px; }
button.pw-toggle:hover:not(:disabled) { background:var(--primary-soft); }

label.option { display:flex; gap:10px; align-items:flex-start; padding:8px 4px; cursor:pointer; }
label.option input { margin-top:5px; flex:none; }
label.option small { display:block; margin-top:3px; font-size:14.5px; font-weight:400; color:var(--muted); line-height:1.45; }
label.option b { font-weight:600; color:var(--text); }

/* ---- mobile / small screens */
html { -webkit-text-size-adjust:100%; }
nav { scrollbar-width:none; }
nav::-webkit-scrollbar { display:none; }
td[data-label]::before { content:attr(data-label); display:none; }

@media (max-width:760px) {
  body { font-size:16px; padding-left:env(safe-area-inset-left); padding-right:env(safe-area-inset-right); }
  header { position:sticky; top:0; z-index:10; padding:8px 12px; }
  .logo { height:34px; }
  .title { display:none; }
  .userbox .who { display:none; }
  header button.ghost { padding:8px 14px; }
  main { margin:12px auto 32px; padding:0 12px; }

  /* tabs: one scrollable row, big touch targets */
  nav { flex-wrap:nowrap; overflow-x:auto; margin:0 -12px 12px; padding:0 12px 4px; gap:8px; scroll-snap-type:x proximity; -webkit-overflow-scrolling:touch; }
  button.tab { flex:none; white-space:nowrap; padding:11px 16px; scroll-snap-align:start; }

  .card { padding:16px 14px; border-radius:10px; }
  .grid2 { grid-template-columns:1fr; }
  h1 { font-size:21px; } h2 { font-size:18px; }

  /* inputs >=16px so iOS does not zoom; comfortable height */
  input, select, textarea { font-size:16px; padding:12px 14px; min-height:46px; }
  textarea { min-height:120px; }
  input[type=radio], input[type=checkbox] { min-height:0; width:22px; height:22px; }
  label.option { padding:10px 2px; }
  label.option input { margin-top:2px; }
  label.inline { padding:6px 0; }

  /* actions: full-width stacked buttons */
  .row { flex-direction:column; align-items:stretch; }
  .row.between { flex-direction:row; align-items:center; }
  .row > button { width:100%; min-height:48px; }
  .row.between > button { width:auto; }
  button { min-height:44px; }
  .pager { flex-direction:row; }
  .pager > button { width:auto; flex:1; }
  .pager span { flex:none; font-size:14px; }

  .out { font-size:14px; padding:12px; }
  .result-title { font-size:14px; }
  .filters { flex-direction:column; }
  .filters input, .filters select, .filters button { width:100%; flex:none; }
  input.otp-sm { max-width:100%; }
  .qr { margin:0 auto; }
  .narrow { padding:22px 16px; }

  /* tables become cards */
  .tablewrap { overflow:visible; }
  table, tbody { display:block; width:100%; }
  thead { display:none; }
  tr { display:block; margin:0 0 12px; border:1px solid var(--line); border-radius:8px; background:#fff; overflow:hidden; }
  tr.fail { border-color:#f3b4b4; } tr.inactive { opacity:.7; }
  td { display:flex; gap:12px; justify-content:space-between; align-items:flex-start; padding:8px 12px; border-bottom:1px solid #eeedf2; text-align:right; word-break:break-word; }
  td:last-child { border-bottom:0; }
  td[data-label]::before { display:block; flex:none; max-width:42%; text-align:left; font-weight:700; font-size:13px; color:var(--primary-dark); }
  td.detail { min-width:0; text-align:left; font-size:13px; }
  td.detail::before { align-self:flex-start; }
  td[colspan] { display:block; text-align:center; }
  td .small-btn { width:100%; min-height:42px; }
  tr.fail td { background:rgba(234,84,85,.05); }
}
@media (max-width:380px) { .logo { height:30px; } button.tab { padding:10px 12px; } }

/* ---- phone layout v2: spacing by margins (flex `gap` is unsupported on older iOS/Android WebViews) */
@media (max-width:760px) {
  .userbox .who { display:block; max-width:96px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; font-size:14px; }
  .userbox .who small { display:none; }
  .userbox { gap:0; }
  .userbox .who { margin-right:10px; }

  /* tabs */
  nav { gap:0; padding-bottom:8px; margin-bottom:14px; }
  button.tab { margin-right:12px; }
  button.tab:last-child { margin-right:0; }

  /* fields and groups breathe more */
  .panel label, .stack label { margin-bottom:20px; }
  fieldset { padding:12px 12px 6px; margin-bottom:22px; }
  label.option { padding:12px 2px; border-bottom:1px solid #eeedf2; }
  label.option:last-of-type { border-bottom:0; }
  .grid2 label { margin-bottom:20px; }
  hr { margin:30px 0; }

  /* button groups: stacked, each separated by a real margin */
  .row { display:block; gap:0; margin-top:18px; }
  .row > button { display:block; width:100%; min-height:50px; margin:0 0 16px; }
  .row > button:last-child { margin-bottom:0; }
  .row.between { display:flex; justify-content:space-between; align-items:center; margin-top:0; margin-bottom:14px; }
  .row.between > button { width:auto; margin:0; }
  .row.pager { display:flex; align-items:center; margin-top:14px; }
  .row.pager > button { width:auto; flex:1; margin:0; }
  .row.pager > button:first-child { margin-right:12px; }
  .row.pager > button:last-child { margin-left:12px; }
  .row.pager span { flex:none; }

  /* single buttons that follow a field */
  #btn-pw, #btn-2fa-disable, #btn-2fa-begin, #btn-2fa-enable { display:block; width:100%; min-height:50px; }
  #twofa-setup button { margin-top:6px; }
  .subcard .row { margin-top:20px; }

  .filters { display:block; }
  .filters input, .filters select, .filters button { display:block; width:100%; margin:0 0 12px; }
  .filters button { margin-bottom:0; }

  .result { margin-top:20px; }
  p.msg, p.error { margin-top:16px; }
  td .small-btn { margin-top:2px; }
  .banner { margin-bottom:16px; }
}
@media (max-width:760px) { td[data-label]::before { margin-right:14px; } header { padding-left:14px; padding-right:14px; } .brand { margin-right:10px; } }

/* ---- phone header v3: logo + logout on row 1, app title + user on row 2 */
@media (max-width:760px) {
  header { position:static; display:grid; grid-template-columns:minmax(0,1fr) auto; grid-template-areas:"logo out" "title who"; align-items:center; column-gap:10px; row-gap:4px; padding:8px 14px 10px; }
  .brand, .userbox { display:contents; }
  .brand .logo { grid-area:logo; justify-self:start; height:34px; }
  .userbox form { grid-area:out; margin:0; }
  header button.ghost { padding:5px 14px; min-height:34px; font-size:14px; line-height:1.2; white-space:nowrap; }
  .brand { margin-right:0; }
  .title { display:block !important; grid-area:title; border-left:0; padding-left:0; font-size:15px; line-height:1.3; }
  .userbox .who { display:block; grid-area:who; max-width:none; margin:0; text-align:right; font-size:13px; color:var(--muted); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
  .userbox .who b { font-weight:600; color:var(--text); }
  .userbox .who small { display:none; }
}

/* ---- tabs: icons only exist for the phone bottom bar */
.tab svg, .tab .short { display:none; }
@media (max-width:760px) {
  /* bottom navigation bar instead of horizontal scrolling tabs */
  nav { position:fixed; left:0; right:0; bottom:0; z-index:30; display:flex; flex-wrap:nowrap; overflow:visible; margin:0; padding:6px 6px calc(6px + env(safe-area-inset-bottom)); gap:0; background:#fff; border-top:1px solid var(--line); box-shadow:0 -2px 10px rgba(75,70,92,.12); scroll-snap-type:none; }
  button.tab, button.tab:last-child { flex:1 1 0; min-width:0; margin:0 2px; padding:6px 2px; min-height:54px; display:flex; flex-direction:column; align-items:center; justify-content:center; background:transparent; border:0; border-radius:10px; color:var(--muted); font-size:11px; font-weight:600; line-height:1.2; white-space:normal; scroll-snap-align:none; }
  button.tab:hover:not(:disabled) { background:transparent; }
  button.tab.active { background:var(--primary-soft); color:var(--primary); }
  .tab svg { display:block; margin-bottom:3px; }
  .tab .short { display:block; max-width:100%; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
  .tab .full { display:none; }
  main { padding-bottom:96px; }
}
@media (max-width:380px) { .tab .short { font-size:10px; } .tab svg { width:20px; height:20px; } }

/* ---- "Thêm" overflow sheet for the phone bottom bar (7+ tabs) */
.tab.more, .sheet-backdrop, .sheet { display:none; }
@media (max-width:760px) {
  nav.has-more .tab.in-more { display:none; }
  nav.has-more .tab.more { display:flex; }
  .sheet-backdrop:not([hidden]) { display:block; position:fixed; left:0; right:0; top:0; bottom:0; z-index:40; background:rgba(30,27,45,.45); }
  .sheet:not([hidden]) { display:block; position:fixed; left:0; right:0; bottom:0; z-index:50; max-height:80vh; overflow-y:auto; background:#fff; border-radius:18px 18px 0 0; padding:10px 14px calc(18px + env(safe-area-inset-bottom)); box-shadow:0 -6px 24px rgba(30,27,45,.25); animation:sheet-up .2s ease-out; }
  .sheet-grip { width:44px; height:5px; border-radius:3px; background:var(--line); margin:0 auto 12px; }
  .sheet-title { font-weight:700; color:var(--primary); margin:4px 4px 12px; }
  button.sheet-item { display:flex; align-items:center; width:100%; min-height:56px; margin:0 0 10px; padding:10px 14px; background:#fff; color:var(--text); border:1px solid var(--line); border-radius:10px; text-align:left; font-weight:600; }
  button.sheet-item:hover:not(:disabled) { background:var(--primary-soft); }
  button.sheet-item svg { display:block; flex:none; margin-right:14px; color:var(--primary); }
  button.sheet-item.active { background:var(--primary-soft); border-color:var(--primary); color:var(--primary); }
}
@keyframes sheet-up { from { transform:translateY(40px); opacity:0; } to { transform:none; opacity:1; } }

/* ---- documents */
.btnlink { display:inline-block; text-decoration:none; text-align:center; padding:11px 22px; font-weight:600; color:#fff; background:var(--primary); border:1px solid var(--primary); border-radius:6px; }
.btnlink.small-btn { padding:5px 14px; }
.btnlink:hover { background:var(--primary-dark); }
.ext { display:inline-block; min-width:44px; text-align:center; padding:1px 6px; margin-right:4px; font-size:12px; font-weight:700; border-radius:4px; color:var(--primary-dark); background:var(--primary-soft); }
td.doc-actions { white-space:nowrap; text-align:right; }
td.doc-actions > * { margin-left:6px; }
input[type=file] { padding:9px 12px; }
.progress { margin-top:12px; height:8px; background:var(--primary-soft); border-radius:4px; overflow:hidden; }
.progress .bar { height:100%; width:0; background:var(--primary); transition:width .15s; }
.viewer { position:fixed; left:0; right:0; top:0; bottom:0; z-index:60; display:flex; align-items:center; justify-content:center; padding:16px; background:rgba(30,27,45,.55); }
.viewer[hidden] { display:none; }
.viewer-box { width:100%; max-width:900px; max-height:90vh; display:flex; flex-direction:column; background:#fff; border-radius:10px; overflow:hidden; box-shadow:0 10px 40px rgba(0,0,0,.35); }
.viewer-head { padding:12px 16px; background:var(--primary-soft); color:var(--primary-dark); border-bottom:1px solid var(--line); word-break:break-word; }
.viewer-box .out { flex:1; overflow:auto; max-height:none; min-height:120px; }
.viewer-actions { display:flex; gap:10px; padding:12px 16px; justify-content:flex-end; border-top:1px solid var(--line); }
body.noscroll { overflow:hidden; }
@media (max-width:760px) {
  .viewer { padding:0; align-items:flex-end; }
  .viewer-box { max-height:92vh; border-radius:16px 16px 0 0; }
  .viewer-actions { display:block; padding-bottom:calc(14px + env(safe-area-inset-bottom)); }
  .viewer-actions button { display:block; width:100%; min-height:48px; margin:0 0 12px; }
  .viewer-actions button:last-child { margin-bottom:0; }
  td.doc-actions { display:block; text-align:left; white-space:normal; }
  td.doc-actions > * { display:block; width:100%; margin:0 0 10px; min-height:44px; line-height:1.9; }
  td.doc-actions > *:last-child { margin-bottom:0; }
  .btnlink { padding:11px 16px; }
  #doc-upload .row { margin-top:12px; }
}

/* ---- document preview overlay (iframe -> /viewer/<id>) */
.viewer-box.wide { max-width:1200px; height:92vh; max-height:92vh; }
.viewer-head.head-row { display:flex; align-items:center; justify-content:space-between; gap:12px; }
.viewer-head.head-row b { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.head-actions { flex:none; display:flex; align-items:center; gap:14px; }
.plainlink { font-weight:600; white-space:nowrap; }
#preview-frame { flex:1; width:100%; border:0; background:#e9e8ee; }
@media (max-width:760px) {
  .viewer-box.wide { height:100%; max-height:100%; border-radius:0; }
  .viewer.previewer { align-items:stretch; }
  .head-actions { gap:10px; }
  .viewer-head.head-row { padding:10px 12px; }
  .viewer-head.head-row b { font-size:15px; }
}
