:root {
  --bg: #f4f6f8; --card: #fff; --text: #1d2330; --muted: #6b7385; --line: #e3e7ee;
  --primary: #0f766e; --primary-ink: #fff; --in: #15803d; --out: #b91c1c; --transfer: #1d4ed8; --warn: #b45309;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11151c; --card: #1a2029; --text: #e6e9ef; --muted: #98a1b3; --line: #2a3240;
    --primary: #2dd4bf; --primary-ink: #062a27; --in: #4ade80; --out: #f87171; --transfer: #93c5fd; --warn: #fbbf24; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; gap: 16px; align-items: center; padding: 10px 20px;
  background: var(--card); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
a { color: inherit; text-decoration-color: var(--line); text-underline-offset: 3px; }
a:hover { color: var(--primary); }
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: var(--primary-ink); }
#nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
#nav a { white-space: nowrap; padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; }
#nav a.active, #nav a:hover { background: var(--bg); color: var(--text); }
main { max-width: 1150px; margin: 0 auto; padding: 20px 16px 60px; }
h1 { font-size: 22px; margin: 0 0 16px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
h1 .spacer { flex: 1; }
h2 { font-size: 16px; margin: 0 0 12px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.pos { color: var(--in); } .neg { color: var(--out); } .xfer { color: var(--transfer); } .muted { color: var(--muted); }
.btn { border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; font: inherit; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 600; }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.danger { color: var(--out); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.actions { white-space: nowrap; text-align: right; }
tr:hover td { background: color-mix(in srgb, var(--bg) 60%, transparent); }
.tag { display: inline-block; font-size: 12px; padding: 0 7px; border-radius: 99px; background: var(--bg); border: 1px solid var(--line); margin: 2px 3px 0 0; cursor: pointer; }
.pill { font-size: 12px; padding: 1px 8px; border-radius: 99px; border: 1px solid currentColor; white-space: nowrap; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
input, select { font: inherit; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--text); min-width: 0; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.bar { height: 8px; border-radius: 4px; background: var(--bg); overflow: hidden; margin-top: 4px; }
.bar > span { display: block; height: 100%; background: var(--primary); }
.list-item { padding: 8px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .top { display: flex; justify-content: space-between; gap: 8px; }
.trend { display: flex; align-items: flex-end; gap: 14px; height: 160px; padding-top: 8px; }
.trend .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; }
.trend .bars { flex: 1; display: flex; align-items: flex-end; gap: 3px; width: 100%; justify-content: center; }
.trend .bars span { width: 40%; max-width: 22px; border-radius: 3px 3px 0 0; min-height: 1px; }
.trend .lbl { font-size: 12px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; }
.ok-banner { padding: 10px 14px; border-radius: 8px; font-size: 14px; }
.ok-banner.ok { background: color-mix(in srgb, var(--in) 12%, transparent); color: var(--in); }
.ok-banner.bad { background: color-mix(in srgb, var(--out) 12%, transparent); color: var(--out); }
.section-title { margin: 24px 0 8px; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; }
.empty { text-align: center; color: var(--muted); padding: 30px; }
dialog { border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(560px, calc(100vw - 32px)); background: var(--card); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.error { color: var(--out); min-height: 1em; margin: 4px 0; font-size: 14px; }
.hint { font-size: 13px; color: var(--muted); margin: -4px 0 10px; }
.type-switch { display: flex; gap: 6px; margin-bottom: 12px; }
.type-switch button { flex: 1; }
.type-switch button.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.rule-line { display: grid; grid-template-columns: 1fr 1fr 1.3fr auto; gap: 6px; margin-bottom: 6px; }
.rule-line.act { grid-template-columns: 1fr 1.6fr auto; }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid.pair { grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); align-items: start; }

/* Lịch dòng tiền */
.cal-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-head h2 { margin: 0; flex: 1; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal .dow { font-size: 12px; color: var(--muted); text-align: center; padding: 2px 0; }
.cal .day { min-height: 64px; border: 1px solid var(--line); border-radius: 8px; padding: 4px 5px; background: none; color: var(--text);
  font: inherit; text-align: left; cursor: pointer; display: flex; flex-direction: column; gap: 1px; min-width: 0; overflow: hidden; }
.cal .day:hover { border-color: var(--primary); }
.cal .day.out { opacity: .35; }
.cal .day.today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cal .day.today .d { background: var(--primary); color: var(--primary-ink); border-radius: 99px; padding: 0 6px; align-self: flex-start; }
.cal .day.sel { background: color-mix(in srgb, var(--primary) 15%, transparent); }
.cal .d { font-size: 13px; font-weight: 600; }
.cal .amt { font-size: 11px; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.cal.mini .day { min-height: 40px; align-items: center; justify-content: center; padding: 2px; }
.cal.mini .day .d { font-weight: 500; }
.cal .dot { width: 5px; height: 5px; border-radius: 50%; background: var(--primary); }
.btn.icon { width: 36px; height: 36px; padding: 0; display: inline-grid; place-items: center; }

/* Drawer xem giao dịch trong ngày */
dialog.drawer { margin: 0 0 0 auto; height: 100dvh; max-height: 100dvh; width: min(440px, 100vw); border-radius: 0; border-width: 0 0 0 1px; }
.drawer-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.drawer-head h2 { flex: 1; margin: 0; }
.day-tx { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.day-tx .info { flex: 1; min-width: 0; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); display: block; flex: none; }

/* Modal giao dịch: ngày + ảnh đối chứng */
.date-quick { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.date-quick .btn.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.mini-wrap { border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin-bottom: 10px; }
.proof-bar { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.proof-bar .btn { flex: 1; }
.proof-preview { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; padding: 8px; border: 1px dashed var(--line); border-radius: 10px; }
.proof-preview img { width: 84px; height: 84px; object-fit: cover; border-radius: 8px; cursor: zoom-in; }
.proof-preview .meta { flex: 1; min-width: 0; font-size: 13px; }
.scan-result { margin-top: 6px; padding: 6px 8px; border-radius: 8px; background: var(--bg); }
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -1px; }
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }

/* Vùng chạm tối thiểu 44px cho màn hình cảm ứng */
@media (pointer: coarse), (max-width: 640px) {
  dialog .btn, .drawer .btn, .cal-head .btn, .date-quick .btn, .proof-bar .btn { min-height: 44px; min-width: 44px; }
  dialog input, dialog select { min-height: 44px; }
  .cal.mini .day { min-height: 44px; }
}
@media (max-width: 900px) { .grid.pair { grid-template-columns: 1fr; } }
@media (max-width: 640px) {
  dialog:not(.drawer) { width: calc(100vw - 16px); max-width: none; padding: 16px 12px; }
  .mini-wrap { padding: 4px; }
  .cal { gap: 2px; }
  .cal .day { min-height: 54px; padding: 3px; }
  .cal .amt { font-size: 10px; }
}

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }

/* ---------- Đa sổ ---------- */
.book-switch { font-weight: 600; max-width: 230px; border: 1px solid var(--line); border-left: 4px solid var(--bc, var(--primary)); padding: 6px 10px; border-radius: 10px; cursor: pointer; }
.who { font-size: 13px; white-space: nowrap; }
.book-pill { display: inline-block; font-size: 11px; line-height: 1.6; padding: 0 7px; border-radius: 99px; white-space: nowrap; vertical-align: 1px;
  color: var(--bc); border: 1px solid color-mix(in srgb, var(--bc) 50%, transparent); background: color-mix(in srgb, var(--bc) 12%, transparent); }
.book-block { border-left: 3px solid var(--bc); padding: 6px 0 2px 10px; margin-bottom: 10px; }
.book-block > .top { display: flex; justify-content: space-between; font-size: 14px; color: var(--bc); }
.book-title { display: flex; align-items: center; gap: 8px; margin: 26px 0 0; }
.book-title::before { content: ""; width: 10px; height: 10px; border-radius: 3px; background: var(--bc); }
.book-line select { border-left: 4px solid var(--primary); }
.pill.xfer { color: var(--transfer); }
body.guest .topbar > :not(.brand), body.guest .tabbar { display: none !important; }
.auth-card { max-width: 400px; margin: 8vh auto 0; padding: 24px; }

/* Quyền riêng tư */
.lockbar { position: sticky; top: 0; z-index: 4; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 16px; background: color-mix(in srgb, var(--warn) 18%, var(--card)); color: var(--warn); font-size: 14px; font-weight: 600;
  border-bottom: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); }
.lockbar[hidden] { display: none; }
.lockbar .btn { color: var(--warn); border-color: currentColor; }
.privacy-note { margin-top: 16px; text-align: center; color: var(--muted); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.switch-row:last-child { border-bottom: 0; }
.switch-row small { display: block; color: var(--muted); }
.switch-row input[type=checkbox] { width: 22px; height: 22px; flex: none; accent-color: var(--primary); }
.book-settings { margin-top: 12px; }

/* Nhóm khoản thu / khoản chi dạng chip */
.field-label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-size: 14px; padding: 6px 12px; border-radius: 99px; cursor: pointer; min-height: 36px; }
.chip:hover { border-color: var(--primary); }
.chip.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 600; }
.chip.add { border-style: dashed; color: var(--muted); }

/* Vay / Nợ */
.grid.two { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.loan-card > .top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.loan-card .spacer, h1 .spacer { flex: 1; }
.loan-card.closed { opacity: .6; }
.loan-nums { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 8px; margin: 10px 0 6px; }
.loan-nums small { display: block; color: var(--muted); font-size: 12px; }
.loan-nums b { font-size: 18px; }
.loan-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
a.btn { text-decoration: none; display: inline-flex; align-items: center; }
.loan-line { display: block; text-decoration: none; margin-top: 6px; border-top: 1px dashed var(--line); }
.invite-box { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; word-break: break-all; margin-bottom: 8px; }
.invite-code { font-size: 18px; letter-spacing: 2px; color: var(--text); }

/* ---------- Thanh điều hướng dưới cùng (điện thoại) ---------- */
.tabbar { display: none; }
@media (max-width: 640px) {
  :root { --tabbar-h: 64px; }
  #nav, #quick-add, .who, .brand-text { display: none; }
  .topbar { justify-content: center; gap: 10px; padding-top: max(10px, env(safe-area-inset-top)); flex-wrap: nowrap; }
  .book-switch { flex: 1; max-width: none; min-height: 44px; font-size: 16px; }
  .chip { min-height: 44px; padding: 8px 14px; }
  .loan-actions .btn { min-height: 44px; }
  main { padding-bottom: calc(var(--tabbar-h) + 40px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); }

  .tabbar { position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: repeat(5, 1fr);
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding: 0 4px env(safe-area-inset-bottom);
    background: var(--primary); color: var(--primary-ink); border-radius: 18px 18px 0 0;
    box-shadow: 0 -4px 18px rgba(0,0,0,.25); }
  .tabbar > a, .tabbar > button { all: unset; box-sizing: border-box; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; min-height: 44px; font-size: 12px; font-weight: 500; color: inherit; cursor: pointer; position: relative;
    -webkit-tap-highlight-color: transparent; opacity: .78; }
  .tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .tabbar .active { opacity: 1; font-weight: 700; }
  .tabbar .active::before { content: ""; position: absolute; top: 5px; width: 22px; height: 3px; border-radius: 2px; background: currentColor; }
  .tabbar > a:active, .tabbar > button:active { transform: scale(.94); }
  .tabbar :focus-visible { outline: 2px solid var(--primary-ink); outline-offset: -4px; border-radius: 10px; }

  /* Nút Chụp nổi ở giữa */
  .tabbar .tab-capture { opacity: 1; justify-content: flex-end; padding-bottom: 7px; }
  .capture-btn { position: absolute; top: -22px; width: 62px; height: 62px; border-radius: 20px; transform: rotate(45deg);
    display: grid; place-items: center; background: var(--card); color: var(--primary);
    box-shadow: 0 6px 16px rgba(0,0,0,.3), 0 0 0 4px var(--primary); }
  .capture-btn svg { transform: rotate(-45deg); width: 30px; height: 30px; stroke-width: 2; }
}

/* Bảng chọn trượt từ dưới lên */
dialog.sheet { margin: auto 0 0; width: 100%; max-width: 100%; border-radius: 18px 18px 0 0; border-width: 1px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); animation: sheet-up .2s ease-out; }
@media (min-width: 641px) { dialog.sheet { margin: auto; width: min(420px, calc(100vw - 32px)); border-radius: 16px; border-width: 1px; } }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 12px; }
.sheet h2 { text-align: center; }
.sheet-option { display: flex; align-items: center; gap: 14px; width: 100%; min-height: 64px; padding: 10px 14px; margin-bottom: 8px;
  border: 1px solid var(--line); border-radius: 14px; background: var(--bg); color: var(--text); font: inherit; text-align: left;
  text-decoration: none; cursor: pointer; }
.sheet-option:hover, .sheet-option:active { border-color: var(--primary); }
.sheet-option .ico { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; flex: none;
  background: color-mix(in srgb, var(--primary) 18%, transparent); color: var(--primary); }
.sheet-option .ico svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sheet-option b { display: block; }
.sheet-option small { color: var(--muted); font-size: 13px; }
.sheet .btn.cancel { width: 100%; min-height: 48px; margin-top: 4px; }
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  .row, .rule-line, .rule-line.act { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
}
