:root {
  --bg: #f8f4ef;
  --card: #ffffff;
  --ink: #2b2420;
  --muted: #74675f;
  --line: #e8ddd3;
  --field: #d6c8bc;
  --accent: #a3213a;
  --accent-dark: #851a2f;
  --accent-soft: #f8e8eb;
  --ok: #1b7246;
  --ok-soft: #e4f3ea;
  --ok-line: #bfe0cc;
  --danger: #b42318;
  --r: 14px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.noscroll { overflow: hidden; }
main { max-width: 720px; margin: 0 auto; padding: 20px 16px calc(100px + env(safe-area-inset-bottom)); }
h1 { font-size: 1.55rem; line-height: 1.2; margin: 0 0 4px; }
h2 { font-size: 1.25rem; margin: 0 0 12px; }
small { font-size: .85rem; color: var(--muted); }
.sub { color: var(--muted); margin: 0 0 16px; }
.hint { color: var(--muted); font-size: .9rem; margin: 8px 0 0; }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }
a { color: var(--accent); }

/* nút */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  font: inherit; font-weight: 600; min-height: 46px; padding: 10px 16px; border-radius: 12px; cursor: pointer;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:active { background: var(--accent-dark); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.big { width: 100%; min-height: 54px; font-size: 1.05rem; }
.btn.small { min-height: 38px; padding: 6px 12px; font-size: .9rem; flex: none; }
.btn.danger { color: var(--danger); }
.link {
  appearance: none; border: 0; background: none; font: inherit; color: var(--accent); padding: 8px 4px;
  cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}
.link.danger { color: var(--danger); }
.link.muted { color: var(--muted); }
.link.small { font-size: .9rem; }

/* ô nhập */
input {
  font: inherit; font-size: 17px; width: 100%; min-height: 46px; padding: 8px 12px;
  border: 1px solid var(--field); border-radius: 10px; background: #fff; color: var(--ink);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
label { display: block; font-size: .9rem; font-weight: 600; color: var(--muted); }
label > input, label > .money, label > .stepper { margin-top: 4px; }
label + label { margin-top: 12px; }
.money { display: flex; align-items: center; gap: 8px; }
.money > span { color: var(--muted); font-weight: 600; }

/* thẻ, danh sách */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 16px; margin: 0 0 14px; }
.list { list-style: none; margin: 14px 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 58px; padding: 12px 16px;
  color: inherit; text-decoration: none; background: none; border: 0; font: inherit; text-align: left; cursor: pointer;
}
.row:active { background: var(--accent-soft); }
.grow { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row b { font-weight: 650; }
.row strong { white-space: nowrap; }
.own { color: var(--accent); }
.ok { color: var(--ok); font-weight: 600; }
.count { margin: 18px 4px -6px; color: var(--muted); font-size: .9rem; }
.add { display: flex; gap: 8px; padding: 12px; }
.add .btn { flex: none; }
.who { text-align: center; color: var(--muted); font-size: .9rem; margin: 28px 0 0; }
.account { display: flex; justify-content: center; gap: 24px; margin-top: 4px; }
.badge.locked { background: #fdecea; color: var(--danger); }
.acct-actions { display: grid; gap: 8px; margin-top: 16px; }
.creds {
  white-space: pre-wrap; overflow-wrap: anywhere; margin: 8px 0 0; padding: 12px 14px;
  background: #fff; border: 1px solid var(--line); border-radius: 12px;
  font: 15px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; user-select: all;
}

/* thanh dưới */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: flex;
  background: rgba(255, 255, 255, .97); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
#tabs[hidden] { display: none; }
#tabs a { flex: 1; text-align: center; padding: 15px 8px; color: var(--muted); text-decoration: none; font-weight: 600; }
#tabs a.on { color: var(--accent); box-shadow: inset 0 3px 0 var(--accent); }

/* một bảng lương */
.back { display: inline-block; text-decoration: none; font-weight: 600; padding: 4px 0 10px; }
.title-row { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 0 16px; }
.summary div { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 8px; min-width: 0; }
.summary small { display: block; }
.summary b { font-size: .92rem; overflow-wrap: anywhere; }

.line { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin: 0 0 12px; }
.line-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.line-head b { font-size: 1.12rem; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 10px; }
.fields label + label { margin-top: 0; }
.stepper { display: flex; }
.stepper input { text-align: center; border-radius: 0; min-width: 0; padding: 8px 4px; }
.stepper button {
  flex: none; width: 44px; min-height: 46px; border: 1px solid var(--field); background: var(--accent-soft);
  color: var(--accent); font: inherit; font-size: 1.4rem; font-weight: 700; cursor: pointer; padding: 0;
}
.stepper button:first-child { border-radius: 10px 0 0 10px; border-right: 0; }
.stepper button:last-child { border-radius: 0 10px 10px 0; border-left: 0; }
.line-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line); }
.line-foot .total { font-size: 1.2rem; }
.line.signed { background: var(--ok-soft); border-color: var(--ok-line); }
.badge { font-size: .85rem; font-weight: 700; padding: 4px 10px; border-radius: 99px; background: #fff; color: var(--ok); white-space: nowrap; }
.facts { margin: 0; color: var(--muted); font-size: .95rem; }
.paid { display: flex; align-items: center; gap: 12px; margin-top: 10px; }
.paid img { height: 60px; max-width: 55%; object-fit: contain; background: #fff; border-radius: 8px; padding: 4px; border: 1px solid var(--ok-line); }
.paid strong { display: block; font-size: 1.2rem; }
.actions { display: grid; gap: 10px; margin-top: 18px; }
.actions .link { justify-self: center; }

/* hộp thoại */
.modal { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; background: rgba(30, 20, 15, .45); }
.modal form {
  width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; background: var(--bg);
  border-radius: 18px 18px 0 0; padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
}
@media (min-width: 600px) { .modal { align-items: center; } .modal form { border-radius: 18px; } }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; position: sticky; bottom: -16px; background: var(--bg); padding: 8px 0; }
.modal-actions .btn { flex: 1; }
.pick-head { display: flex; justify-content: space-between; align-items: center; margin: 16px 0 6px; }
.pick { background: var(--card); border: 1px solid var(--line); border-radius: 12px; max-height: 42vh; overflow: auto; }
.check { display: flex; align-items: center; gap: 12px; margin: 0; padding: 12px 14px; color: var(--ink); font-size: 1rem; font-weight: 500; cursor: pointer; }
.check + .check { border-top: 1px solid var(--line); margin-top: 0; }
.check input { width: 22px; height: 22px; min-height: 0; margin: 0; flex: none; accent-color: var(--accent); }
.check span { flex: 1; }

/* ký nhận */
.sign-screen {
  position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; gap: 12px; background: #fff;
  padding: calc(14px + env(safe-area-inset-top)) 16px calc(14px + env(safe-area-inset-bottom));
}
.sign-top { text-align: center; }
.sign-top p { margin: 0; }
.sign-top .who { font-size: 1.2rem; font-weight: 700; }
.sign-top .amount { font-size: 2.3rem; font-weight: 800; color: var(--accent); line-height: 1.2; }
.sign-top .hint { margin-top: 6px; color: var(--ink); }
.pad { position: relative; flex: 1; min-height: 170px; border: 2px dashed #cdbfb4; border-radius: 16px; background: #fffdfb; }
.pad canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.pad::after { content: ""; position: absolute; left: 10%; right: 10%; bottom: 28%; border-bottom: 1px solid var(--field); pointer-events: none; }
.pad-hint { position: absolute; left: 0; right: 0; bottom: calc(28% + 8px); text-align: center; color: #b3a59b; pointer-events: none; }
.pad.inked .pad-hint { display: none; }
.sign-actions { display: flex; gap: 8px; }
.sign-actions .btn { flex: 1; }
.sign-actions .primary { flex: 2; }

/* đăng nhập */
.login { max-width: 380px; margin: 8vh auto 0; text-align: center; }
.logo { width: 72px; height: 72px; }
.login form { text-align: left; margin-top: 18px; }
.login .btn { margin-top: 14px; }
.err { color: var(--danger); min-height: 1.3em; margin: 10px 0 0; }

/* báo nhỏ */
#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 40;
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px; background: #2b2420; color: #fff; text-align: center;
  opacity: 0; pointer-events: none; transform: translate(-50%, 16px); transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: var(--danger); }
#saveState {
  position: fixed; top: calc(10px + env(safe-area-inset-top)); right: 12px; z-index: 6; display: none;
  padding: 4px 12px; border-radius: 99px; font-size: .85rem; font-weight: 600; background: #fff; border: 1px solid var(--line);
}
#saveState.busy, #saveState.done { display: block; color: var(--muted); }
#saveState.bad { display: block; color: #fff; background: var(--danger); border-color: var(--danger); }

/* bản in: chỉ có bảng */
#print { display: none; }
@media print {
  @page { size: A4 portrait; margin: 12mm; }
  body { background: #fff; color: #000; font-size: 10.5pt; }
  body > :not(#print) { display: none !important; }
  #print { display: block; }
  #print h1 { text-align: center; font-size: 15pt; margin: 0; letter-spacing: .5px; }
  #print .p-sub { text-align: center; margin: 2px 0 10px; }
  #print table { width: 100%; border-collapse: collapse; }
  #print th, #print td { border: 1px solid #000; padding: 3px 5px; vertical-align: middle; }
  #print th { font-size: 9.5pt; background: #eee; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #print tr { break-inside: avoid; }
  #print .r { text-align: right; white-space: nowrap; }
  #print .c { text-align: center; }
  #print .b { font-weight: 700; }
  #print td.sig { width: 32mm; height: 14mm; padding: 1px; text-align: center; }
  #print td.sig img { max-width: 31mm; max-height: 13mm; }
  #print .p-foot { font-size: 9pt; margin-top: 8px; }
}
