/* Thiết kế ưu tiên điện thoại; màn hình lớn chỉ nới rộng ra ở cuối file */

:root {
  --bg: #f3f5f4;
  --surface: #ffffff;
  --surface-2: #edf1f0;
  --border: #dde3e1;
  --text: #1a2422;
  --muted: #5d6b68;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #d8efec;
  --danger: #b42318;
  --danger-soft: #fde8e6;
  --warn: #8a5208;
  --warn-soft: #fdf0d8;
  --ok: #17643a;
  --ok-soft: #dbf2e3;
  --info: #1d4f91;
  --info-soft: #e0eaf9;
  --shadow: 0 1px 2px rgb(16 24 22 / 6%), 0 2px 10px rgb(16 24 22 / 5%);
  --radius: 16px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1514;
    --surface: #18201e;
    --surface-2: #222c2a;
    --border: #2e3a37;
    --text: #e6eceb;
    --muted: #9aa8a5;
    --primary: #2fb3a6;
    --primary-ink: #04201c;
    --primary-soft: #153834;
    --danger: #f08479;
    --danger-soft: #3b1e1b;
    --warn: #f0b660;
    --warn-soft: #382a14;
    --ok: #6fd39a;
    --ok-soft: #163224;
    --info: #8db8f2;
    --info-soft: #1a2a42;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

a { color: var(--primary); }
button, a, label, summary { touch-action: manipulation; }
h1, h2 { line-height: 1.25; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.ok-text { color: var(--ok); }
.warn-text { color: var(--warn); }
.loading, .empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.empty { background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---------- Khung ---------- */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  height: calc(52px + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 16px 0;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-size: 1.05rem; }
.brand img { border-radius: 8px; }
.who { display: flex; align-items: center; color: inherit; text-decoration: none; text-align: right; line-height: 1.2; min-width: 0; }
.who strong { display: block; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 48vw; }
.who small { display: block; font-size: .75rem; color: var(--muted); }

/* Thanh menu dưới cùng */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--nav-h) + var(--safe-b));
  padding: 4px 4px var(--safe-b);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-top: 1px solid var(--border);
}
.tabs a {
  position: relative; flex: 1 1 0; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border-radius: 12px; color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.tabs a.active { color: var(--primary); }
.nav-icon {
  display: grid; place-items: center; width: 52px; height: 30px; border-radius: 999px;
  font-size: 1.25rem; transition: background .15s;
}
.tabs a.active .nav-icon { background: var(--primary-soft); }
.nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.badge {
  position: absolute; top: 2px; left: calc(50% + 8px);
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: .72rem; font-weight: 700; line-height: 20px; text-align: center;
  box-shadow: 0 0 0 2px var(--surface);
}

.main {
  max-width: 720px; margin: 0 auto;
  padding: 12px 16px calc(var(--nav-h) + var(--safe-b) + 28px);
}
.page-title { font-size: 1.45rem; margin: 6px 0 14px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.page-head .btn { min-height: 40px; padding: 0 14px; }

/* ---------- Thành phần ---------- */

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px;
}
.list { display: flex; flex-direction: column; }
.gap { margin-bottom: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid transparent;
  font: inherit; font-weight: 650; cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: transform .08s, filter .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn.danger-text { color: var(--danger); }
.btn.block { width: 100%; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 999px; background: var(--surface-2); color: var(--muted);
  font-size: 1rem; cursor: pointer;
}
.btn:focus-visible, .icon-btn:focus-visible, .chip input:focus-visible + span, .chip-btn:focus-visible,
.tabs a:focus-visible, .copy-btn:focus-visible {
  outline: 3px solid var(--primary); outline-offset: 2px;
}

label { display: block; font-weight: 600; margin-bottom: 14px; }
label.check { display: flex; align-items: center; gap: 12px; font-weight: 500; min-height: 44px; margin-bottom: 4px; }
label.check input { width: 22px; height: 22px; accent-color: var(--primary); flex: none; }
/* font-size 16px để iPhone không tự phóng to khi chạm vào ô nhập */
input:where(:not([type=checkbox], [type=radio])), select, textarea {
  display: block; width: 100%; margin-top: 6px; padding: 12px 14px; min-height: 48px;
  font: inherit; font-size: 16px; font-weight: 400; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  -webkit-appearance: none; appearance: none;
}
select {
  padding-right: 40px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 15px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
textarea { resize: vertical; }
fieldset { border: 0; padding: 0; margin: 0 0 14px; min-width: 0; }
legend { font-weight: 600; margin-bottom: 8px; padding: 0; }
.form-error { color: var(--danger); background: var(--danger-soft); padding: 10px 12px; border-radius: 12px; margin: 0 0 14px; }

.chips { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.chip { margin: 0; font-weight: 500; position: relative; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: flex; align-items: center; gap: 8px; min-height: 52px; padding: 8px 14px;
  border: 1.5px solid var(--border); border-radius: 14px; cursor: pointer; background: var(--surface);
  transition: border-color .15s, background .15s;
}
.chip input:checked + span { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 650; }

.money-input { position: relative; display: block; }
.money-input input { padding-right: 40px; font-size: 1.5rem; font-weight: 700; letter-spacing: .01em; min-height: 58px; }
.money-input > span { position: absolute; right: 16px; top: 50%; transform: translateY(-40%); color: var(--muted); font-weight: 700; font-size: 1.1rem; }
.quick { display: flex; gap: 8px; margin: -6px 0 16px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.quick::-webkit-scrollbar { display: none; }
.chip-btn {
  flex: none; min-height: 38px; padding: 0 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer;
}
.chip-btn:active { background: var(--primary-soft); }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.filters label:last-child:nth-child(odd) { grid-column: 1 / -1; }
.filters label { font-size: .85rem; margin-bottom: 12px; }
.filters input, .filters select { min-height: 44px; padding-top: 9px; padding-bottom: 9px; }

/* Thẻ yêu cầu */
.req { padding: 14px 14px 12px; }
.req-head { display: flex; align-items: center; gap: 12px; }
.cat-icon {
  flex: none; display: grid; place-items: center; width: 44px; height: 44px;
  border-radius: 14px; background: var(--surface-2); font-size: 1.35rem;
}
.req-title { flex: 1; min-width: 0; line-height: 1.3; }
.req-title strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.req-title .sub { display: block; font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.req-amount { flex: none; text-align: right; line-height: 1.2; }
.amount { display: block; font-size: 1.15rem; font-weight: 750; font-variant-numeric: tabular-nums; }
.req-amount s { font-size: .78rem; color: var(--muted); }
.reason { margin: 10px 0 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
.req-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; }
.needed { font-size: .8rem; color: var(--muted); }
.status { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .75rem; font-weight: 700; line-height: 1.4; }
.status.pending { background: var(--warn-soft); color: var(--warn); }
.status.approved { background: var(--info-soft); color: var(--info); }
.status.transferred { background: var(--ok-soft); color: var(--ok); }
.status.rejected { background: var(--danger-soft); color: var(--danger); }
.status.cancelled { background: var(--surface-2); color: var(--muted); }
.timeline { list-style: none; margin: 10px 0 0; padding: 10px 0 0; border-top: 1px solid var(--border); font-size: .84rem; color: var(--muted); }
.timeline li { padding-left: 18px; position: relative; margin-bottom: 4px; overflow-wrap: anywhere; }
.timeline li::before { content: ""; position: absolute; left: 3px; top: .55em; width: 7px; height: 7px; border-radius: 50%; background: var(--border); }
.timeline li.pos::before { background: var(--ok); }
.timeline li.neg::before { background: var(--danger); }
.timeline b { color: var(--text); font-weight: 600; }
.timeline q { display: block; font-style: italic; color: var(--text); }
.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions .btn { flex: 1 1 0; }

/* Thống kê */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px; }
.stats.compact { grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; }
.stat-label { display: block; font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat strong { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat small { display: block; font-size: .75rem; color: var(--muted); }
.stats.compact .stat { padding: 10px; }
.stats.compact .stat strong { font-size: .98rem; }

/* Bảng báo cáo: trên điện thoại mỗi dòng thành một khối */
.table-card { padding: 16px 0 4px; }
.table-card h2 { padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
thead { display: none; }
tbody tr { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; padding: 12px 16px; border-top: 1px solid var(--border); }
td { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; font-variant-numeric: tabular-nums; }
td.name { grid-column: 1 / -1; font-weight: 650; padding-bottom: 4px; }
td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: .8rem; }

.user-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.user-row.inactive { opacity: .6; }
.help { margin-top: 8px; }
.profile { display: flex; align-items: center; gap: 14px; }
.avatar {
  display: grid; place-items: center; width: 52px; height: 52px; flex: none; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary); font-size: 1.4rem; font-weight: 750;
}
.steps { margin: 0 0 4px; padding-left: 22px; }
.steps li { margin-bottom: 6px; }

/* Nhắc bật thông báo */
.push-banner {
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px;
  background: var(--warn-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
}
.push-banner.off { background: var(--primary-soft); border-color: color-mix(in srgb, var(--primary) 35%, transparent); }
.push-banner p:last-child, .push-banner ol:last-child { margin-bottom: 0; }

/* Đăng nhập */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 400px; padding: 24px 20px; }
.login-brand { text-align: center; margin-bottom: 20px; }
.logo-img { border-radius: 18px; }
.login-brand h1 { margin: 8px 0 0; }
.login-brand p { margin: 4px 0 0; }

/* ---------- Hộp thoại: trượt lên từ dưới trên điện thoại ---------- */
dialog {
  width: 100%; max-width: 100%; max-height: 92vh; max-height: 92dvh;
  margin: auto 0 0; padding: 0; border: 0; border-radius: 22px 22px 0 0;
  background: var(--surface); color: var(--text); overflow: hidden;
}
dialog[open] { animation: sheet-up .22s ease-out; }
@keyframes sheet-up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
#dlg-form { display: flex; flex-direction: column; max-height: inherit; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 10px; border-bottom: 1px solid var(--border); }
.dlg-head h2 { font-size: 1.1rem; margin: 0; }
.dlg-scroll { overflow-y: auto; padding: 14px 16px 4px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.dlg-actions {
  display: flex; gap: 8px; padding: 10px 16px calc(10px + var(--safe-b));
  border-top: 1px solid var(--border); background: var(--surface);
}
.dlg-actions .btn { flex: 1 1 0; }
.dlg-actions .btn.primary, .dlg-actions .btn.danger { flex-grow: 2; }
.big-amount { font-size: 2rem; font-weight: 800; margin: 0 0 2px; color: var(--primary); font-variant-numeric: tabular-nums; }

/* Chuyển tiền */
.pay-box { background: var(--surface-2); border-radius: 14px; padding: 2px 12px; margin: 12px 0; }
.pay-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 0 8px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.pay-row:last-child { border-bottom: 0; }
.pay-row > span { grid-column: 1 / -1; font-size: .75rem; color: var(--muted); }
.pay-row > b { min-width: 0; overflow-wrap: anywhere; font-size: 1.02rem; }
.mono { font-variant-numeric: tabular-nums; letter-spacing: .03em; }
.copy-btn {
  min-height: 36px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--primary); font: inherit; font-size: .82rem; font-weight: 650; cursor: pointer;
}
.pay-app { margin-bottom: 8px; }
.pay-app .btn { margin-bottom: 6px; min-height: 54px; font-size: 1.05rem; }
.pay-qr { margin-bottom: 14px; border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; }
.pay-qr summary { cursor: pointer; font-weight: 600; }
.pay-qr img { display: block; width: 240px; max-width: 100%; height: auto; margin: 12px auto; background: #fff; border-radius: 10px; }
.notice { background: var(--warn-soft); color: var(--warn); padding: 10px 12px; border-radius: 12px; }
.bank-fields { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 8px; }

/* Thông báo nhanh, nằm trên thanh menu */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 14px); z-index: 100;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  width: max-content; max-width: calc(100vw - 32px); padding: 12px 18px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-weight: 600; text-align: center;
  transition: opacity .2s, transform .2s; box-shadow: 0 6px 24px rgb(0 0 0 / 20%);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); color: #fff; }

/* ---------- Màn hình lớn (máy tính / máy tính bảng) ---------- */
@media (min-width: 760px) {
  .tabs {
    position: sticky; top: calc(52px + env(safe-area-inset-top, 0px)); bottom: auto;
    height: auto; max-width: 720px; margin: 0 auto; padding: 8px 16px;
    justify-content: flex-start; gap: 4px; background: transparent; border: 0;
    backdrop-filter: none; -webkit-backdrop-filter: none;
  }
  .tabs a { flex: none; flex-direction: row; gap: 6px; padding: 6px 14px 6px 6px; font-size: .9rem; border-radius: 999px; }
  .tabs a.active { background: var(--primary-soft); }
  .nav-icon { width: 30px; height: 30px; font-size: 1.1rem; }
  .tabs a.active .nav-icon { background: transparent; }
  .badge { position: static; box-shadow: none; }
  .main { padding-bottom: 40px; }
  .stats { grid-template-columns: repeat(4, 1fr); }
  .stats.compact { grid-template-columns: repeat(3, 1fr); }
  .chips { grid-template-columns: repeat(3, 1fr); }
  #toast { bottom: 24px; }
  dialog { width: min(480px, calc(100vw - 32px)); margin: auto; border-radius: 20px; }
  .dlg-actions { padding-bottom: 12px; }
  thead { display: table-header-group; }
  tbody tr { display: table-row; padding: 0; }
  td, th { display: table-cell; padding: 10px 16px; border-top: 1px solid var(--border); text-align: left; white-space: nowrap; }
  td[data-label]::before { content: none; }
  th { font-size: .75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; }
  .num { text-align: right; }
  td.name { padding-bottom: 10px; }
}

/* ---------- Quỹ gia đình ---------- */
.fund-hero {
  border-radius: 20px; padding: 18px 18px 16px; margin-bottom: 14px; color: #fff;
  background: linear-gradient(135deg, #0f766e, #115e59);
  box-shadow: 0 6px 20px rgb(15 118 110 / 25%);
}
.fund-hero.neg { background: linear-gradient(135deg, #b42318, #912018); box-shadow: 0 6px 20px rgb(180 35 24 / 25%); }
.fund-label { display: block; font-size: .85rem; opacity: .85; }
.fund-balance { display: block; font-size: 2.2rem; font-weight: 800; line-height: 1.2; margin: 2px 0 8px; font-variant-numeric: tabular-nums; }
.fund-hero p { margin: 0 0 6px; font-size: .88rem; opacity: .95; }
.fund-warn { background: rgb(255 255 255 / 16%); border-radius: 10px; padding: 6px 10px; font-weight: 600; }
.fund-actions { display: flex; gap: 8px; margin-top: 12px; }
.fund-actions .btn { flex: 1 1 0; background: rgb(255 255 255 / 16%); color: #fff; border-color: rgb(255 255 255 / 35%); }

.fund-flow { margin: 0; }
.fund-flow > div { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.fund-flow dt { color: var(--muted); }
.fund-flow dd { margin: 0; font-weight: 650; font-variant-numeric: tabular-nums; }
.fund-flow .in dd { color: var(--ok); }
.fund-flow .out dd { color: var(--danger); }
.fund-flow .total { border-bottom: 0; padding-top: 10px; }
.fund-flow .total dt { color: var(--text); font-weight: 700; }
.fund-flow .total dd { font-size: 1.15rem; font-weight: 800; color: var(--primary); }
.fund-flow .total.neg dd { color: var(--danger); }

.ledger { padding-bottom: 4px; }
.ledger ul { list-style: none; margin: 0 -16px; padding: 0; }
.fund-row { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-top: 1px solid var(--border); }
.fund-row .cat-icon { width: 40px; height: 40px; font-size: 1.2rem; }
.fund-row .req-title strong { font-size: .95rem; }
.fund-amt { flex: none; text-align: right; }
.fund-amt b { display: block; font-variant-numeric: tabular-nums; }
.fund-row.deposit .fund-amt b { color: var(--ok); }
.fund-row.expense .fund-amt b, .fund-row.transfer .fund-amt b { color: var(--danger); }
.link-btn { border: 0; background: none; padding: 4px 0; color: var(--muted); font: inherit; font-size: .78rem; text-decoration: underline; cursor: pointer; }
.fund-line { margin-top: -4px; }
.notice a { color: inherit; font-weight: 700; }
.user-row .avatar { width: 44px; height: 44px; font-size: 1.4rem; background: var(--surface-2); }
.user-info { flex: 1; min-width: 0; }
.adult-fields { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 8px; }

/* ---------- Ảnh đại diện trên thanh trên ---------- */
.who { gap: 10px; }
.avatar.sm { width: 36px; height: 36px; font-size: 1.15rem; background: var(--surface-2); }
.who.active .avatar.sm { background: var(--primary-soft); box-shadow: 0 0 0 2px var(--primary); }

/* ---------- Nhóm trò chuyện ---------- */
/* Cả màn hình là một cột cố định cao bằng phần nhìn thấy (tự co lại khi bàn phím bật lên);
   chỉ danh sách tin nhắn cuộn, ô nhập luôn nằm sát dưới */
body.chat-mode { overflow: hidden; }
body.chat-mode #app {
  position: fixed; left: 0; right: 0; top: var(--vv-top, 0px);
  height: var(--vvh, 100vh); height: var(--vvh, 100dvh);
  display: flex; flex-direction: column;
}
body.chat-mode .topbar { position: relative; flex: none; }
body.chat-mode .tabs { position: relative; flex: none; order: 3; }
body.chat-mode .main { flex: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; padding: 0; }
body.chat-mode #main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
body.chat-mode .push-banner { flex: none; margin: 8px 10px 0; padding: 8px 10px; font-size: .85rem; }
/* Trong khung chat chỉ giữ nút Bật thông báo cho gọn */
body.chat-mode .push-banner.off p { display: none; }
body.chat-mode .push-banner .btn { min-height: 40px; }
body.chat-mode #toast { bottom: 140px; }
/* Đang gõ trên điện thoại: ẩn thanh menu dưới để có thêm chỗ */
body.chat-typing .tabs { display: none; }
body.chat-typing .chat-input { padding-bottom: calc(8px + var(--safe-b)); }
body.chat-typing #toast { bottom: 80px; }

.chat-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-scroll-area { position: relative; flex: 1; min-height: 0; }
.chat-list {
  position: absolute; inset: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 6px 10px 14px;
}
.chat-jump {
  position: absolute; right: 12px; bottom: 12px; z-index: 2;
  min-width: 44px; height: 44px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--primary); font: inherit; font-weight: 700; cursor: pointer;
  box-shadow: 0 4px 16px rgb(0 0 0 / 14%);
}
.chat-jump.new { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.chat-more { margin-bottom: 8px; min-height: 40px; }
.chat-day { text-align: center; margin: 14px 0 8px; }
.chat-day span { display: inline-block; padding: 3px 12px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: .75rem; font-weight: 600; }
.msg { display: flex; align-items: flex-end; gap: 8px; margin-top: 8px; }
.msg.grouped { margin-top: 2px; }
.msg.mine { justify-content: flex-end; }
.msg-avatar { flex: none; width: 32px; height: 32px; display: grid; place-items: center; border-radius: 999px; font-size: 1.05rem; }
.msg:not(.grouped) .msg-avatar { background: var(--surface-2); }
.msg-main { display: flex; flex-direction: column; max-width: 80%; min-width: 0; }
.msg.mine .msg-main { align-items: flex-end; }
.msg-name { font-size: .75rem; color: var(--muted); margin: 0 0 2px 10px; font-weight: 600; }
.bubble {
  padding: 8px 12px 6px; border-radius: 18px; background: var(--surface); border: 1px solid var(--border);
  white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.4;
}
.msg:not(.mine) .bubble { border-bottom-left-radius: 6px; }
.msg.mine .bubble { background: var(--primary); color: var(--primary-ink); border-color: transparent; border-bottom-right-radius: 6px; cursor: pointer; }
.bubble time { display: block; text-align: right; font-size: .68rem; opacity: .65; margin-top: 2px; }
.bubble.deleted { background: transparent !important; color: var(--muted) !important; border: 1px dashed var(--border) !important; font-style: italic; cursor: default; }
.msg-links { display: flex; gap: 16px; padding: 0 8px; }
.msg .link-btn { color: var(--muted); margin-top: 2px; }
.msg .link-btn.danger-link { color: var(--danger); }
.chat-seen { text-align: right; font-size: .72rem; color: var(--muted); margin-top: 4px; }

.chat-input {
  position: relative; flex: none; z-index: 5;
  display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 8px; background: var(--surface); border-top: 1px solid var(--border);
}
.chat-input textarea {
  flex: 1; min-width: 0; margin: 0; min-height: 44px; max-height: 120px; padding: 10px 14px;
  border-radius: 22px; resize: none; line-height: 1.4; background: var(--surface-2); border-color: transparent;
}
.send-btn {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 999px; cursor: pointer;
  background: var(--primary); color: var(--primary-ink); font-size: 1.1rem;
}
.send-btn:disabled { opacity: .6; }
@media (min-width: 760px) {
  /* Thanh menu nằm ngay dưới thanh trên: bỏ khoảng "top" dùng cho kiểu dính (sticky) ở các trang khác */
  body.chat-mode .tabs { order: 0; top: auto; width: 100%; }
  body.chat-mode .main { max-width: 720px; margin: 0 auto; }
  .chat-input { border: 1px solid var(--border); border-bottom: 0; border-radius: 16px 16px 0 0; padding: 10px 12px; }
  body.chat-mode #toast { bottom: 96px; }
}

/* ---------- Thanh tiến độ: Đã gửi -> Xác nhận -> Chi thành công ---------- */
.progress {
  list-style: none; display: grid; grid-template-columns: repeat(3, 1fr);
  margin: 12px 0 0; padding: 12px 0 0; border-top: 1px solid var(--border); counter-reset: step;
}
.progress li { position: relative; text-align: center; padding: 0 4px; font-size: .78rem; color: var(--muted); counter-increment: step; }
.progress li::before {
  content: ""; position: absolute; top: 13px; left: -50%; width: 100%; height: 3px; background: var(--border);
}
.progress li:first-child::before { display: none; }
.progress li.done::before, .progress li.current::before, .progress li.fail::before { background: var(--ok); }
.progress .dot {
  position: relative; z-index: 1; display: grid; place-items: center; width: 28px; height: 28px; margin: 0 auto 4px;
  border-radius: 50%; border: 2px solid var(--border); background: var(--surface); font-size: .8rem; font-weight: 800;
}
.progress .dot::after { content: counter(step); }
.progress li.done .dot { background: var(--ok); border-color: var(--ok); color: #fff; }
.progress li.done .dot::after { content: "✓"; }
.progress li.current .dot { border-color: var(--warn); background: var(--warn-soft); color: var(--warn); animation: pulse 1.6s ease-in-out infinite; }
.progress li.fail .dot { background: var(--danger); border-color: var(--danger); color: #fff; }
.progress li.fail .dot::after { content: "✕"; }
.progress li.skip { opacity: .45; }
.progress b { display: block; color: var(--text); font-size: .8rem; line-height: 1.25; }
.progress li.current b { color: var(--warn); }
.progress li.fail b { color: var(--danger); }
.progress small { display: block; font-size: .7rem; line-height: 1.3; overflow-wrap: anywhere; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--warn) 20%, transparent); } }
@media (prefers-reduced-motion: reduce) { .progress li.current .dot { animation: none; } }
.req-notes { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.req-notes q { display: block; font-size: .84rem; background: var(--surface-2); border-radius: 10px; padding: 6px 10px; quotes: none; overflow-wrap: anywhere; }
.page-head h2 { margin: 10px 0; }

/* ---------- Mục con trong Duyệt chi ---------- */
.seg { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 14px; margin-bottom: 14px; }
.seg a {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 42px;
  border-radius: 10px; color: var(--muted); font-weight: 650; text-decoration: none;
}
.seg a.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg .badge { position: static; box-shadow: none; }

/* ---------- Trò chuyện: ảnh, cảm xúc, nhắc tên ---------- */
.attach-btn {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 999px; cursor: pointer;
  background: var(--primary-soft); color: var(--primary); font-size: 1.4rem; font-weight: 700;
}
.attach-btn[aria-expanded="true"] { background: var(--primary); color: var(--primary-ink); }
.attach-menu {
  position: absolute; left: 8px; bottom: calc(100% + 6px); z-index: 6;
  display: flex; flex-direction: column; min-width: 200px; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 8px 28px rgb(0 0 0 / 16%);
}
.attach-menu button {
  min-height: 50px; padding: 0 16px; border: 0; border-bottom: 1px solid var(--border); background: none;
  font: inherit; font-weight: 600; color: var(--text); text-align: left; cursor: pointer;
}
.attach-menu button:last-child { border-bottom: 0; }
.attach-menu button:active { background: var(--surface-2); }
.chat-input.busy { opacity: .6; pointer-events: none; }
.msg-img { display: block; padding: 0; border: 0; background: none; cursor: zoom-in; margin: -2px -6px 6px; }
.msg-img img { display: block; max-width: min(240px, 60vw); max-height: 320px; border-radius: 14px; object-fit: cover; background: var(--surface-2); }
.bubble.photo { padding: 4px; background: transparent !important; border-color: transparent !important; }
.bubble.photo .msg-img { margin: 0; }
.bubble.photo time { padding: 0 6px 2px; color: var(--muted); opacity: 1; }
.msg-text { display: block; }
.mention { font-weight: 700; color: var(--primary); }
.msg.mine .mention { color: inherit; text-decoration: underline; }
.mention.me { background: var(--warn-soft); color: var(--warn); border-radius: 6px; padding: 0 3px; }
.bubble.at-me { box-shadow: 0 0 0 2px var(--warn); }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: -6px; padding: 0 6px; position: relative; z-index: 1; }
.msg.mine .reactions { justify-content: flex-end; }
.reaction {
  display: inline-flex; align-items: center; gap: 3px; min-height: 26px; padding: 0 7px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); font: inherit; font-size: .85rem; cursor: pointer;
  box-shadow: var(--shadow);
}
.reaction span { font-size: .72rem; font-weight: 700; color: var(--muted); }
.reaction.mine { border-color: var(--primary); background: var(--primary-soft); }
.msg-actions { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.msg.mine .msg-actions { align-items: flex-end; }
.react-pick {
  display: inline-flex; gap: 2px; padding: 4px; border-radius: 999px; background: var(--surface);
  border: 1px solid var(--border); box-shadow: 0 4px 16px rgb(0 0 0 / 12%);
}
.react-pick button {
  width: 40px; height: 40px; border: 0; border-radius: 999px; background: none; font-size: 1.4rem; cursor: pointer;
  transition: transform .1s;
}
.react-pick button:active { transform: scale(1.25); }
.react-pick button.on { background: var(--primary-soft); }

.mention-pop {
  position: absolute; left: 12px; right: 12px; bottom: calc(100% + 6px);
  display: flex; flex-direction: column; max-height: 260px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 8px 28px rgb(0 0 0 / 16%);
}
.mention-pop button {
  display: flex; align-items: center; gap: 10px; min-height: 46px; padding: 0 14px; border: 0; background: none;
  font: inherit; color: var(--text); text-align: left; cursor: pointer; border-bottom: 1px solid var(--border);
}
.mention-pop button:last-child { border-bottom: 0; }
.mention-pop button:active, .mention-pop button:hover { background: var(--surface-2); }

.viewer {
  position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; background: rgb(0 0 0 / 92%);
  padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px);
}
.viewer img { max-width: 100vw; max-height: 100vh; max-height: 100dvh; object-fit: contain; }
.viewer .icon-btn { position: absolute; top: calc(12px + env(safe-area-inset-top, 0px)); right: 12px; background: rgb(255 255 255 / 18%); color: #fff; }
/* Nhấn giữ ảnh để mở cảm xúc, không bật menu lưu ảnh của iPhone (trình xem ảnh lớn vẫn lưu được) */
.bubble { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.msg-img img { -webkit-touch-callout: none; pointer-events: none; }
.reactions, .msg-actions { -webkit-user-select: none; user-select: none; }

/* ---------- Ảnh đại diện ---------- */
.avatar { overflow: hidden; line-height: 1; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.xs { width: 30px; height: 30px; font-size: 1rem; background: var(--surface-2); }
.avatar.xl { width: 84px; height: 84px; font-size: 2.6rem; background: var(--surface-2); box-shadow: 0 0 0 3px var(--surface), 0 0 0 5px var(--primary-soft); }
.msg-avatar.avatar { background: var(--surface-2); }
.avatar-btns { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 10px; }
.avatar-btns .btn { flex: 1 1 auto; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; }
.emoji-grid button {
  aspect-ratio: 1; border: 1px solid transparent; border-radius: 12px; background: var(--surface-2);
  font-size: 1.35rem; cursor: pointer; padding: 0;
}
.emoji-grid button.on { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 0 0 2px var(--primary); }
.emoji-grid button:active { transform: scale(.92); }

/* ---------- Bình chọn ---------- */
.bubble.has-poll {
  width: min(320px, 76vw); padding: 12px; cursor: default;
  background: var(--surface) !important; color: var(--text) !important; border: 1px solid var(--border) !important;
}
.bubble.has-poll time { color: var(--muted); }
.poll { white-space: normal; }
.poll-q { font-weight: 700; line-height: 1.35; margin-bottom: 2px; white-space: pre-wrap; }
.poll-sub { font-size: .75rem; color: var(--muted); margin-bottom: 10px; }
.poll-opt {
  position: relative; display: block; width: 100%; overflow: hidden; margin-bottom: 6px; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: 12px; background: var(--surface);
  font: inherit; color: var(--text); text-align: left; cursor: pointer;
}
.poll-opt:disabled { cursor: default; }
.poll-opt.on { border-color: var(--primary); }
.poll-bar { position: absolute; inset: 0 auto 0 0; background: var(--primary-soft); transition: width .25s ease-out; }
.poll-row { position: relative; display: flex; align-items: center; gap: 10px; }
.poll-check { flex: none; width: 20px; height: 20px; border: 2px solid var(--border); border-radius: 50%; background: var(--surface); }
.poll-opt.multi .poll-check { border-radius: 6px; }
.poll-opt.on .poll-check { border-color: var(--primary); background: var(--primary); box-shadow: inset 0 0 0 3px var(--surface); }
.poll-opt.multi.on .poll-check { box-shadow: none; display: grid; place-items: center; }
.poll-opt.multi.on .poll-check::after { content: "✓"; color: var(--primary-ink); font-size: .75rem; font-weight: 800; }
.poll-label { flex: 1; min-width: 0; overflow-wrap: anywhere; line-height: 1.35; }
.poll-count { flex: none; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--primary); }
.poll-voters { position: relative; display: block; margin: 4px 0 0 30px; font-size: .75rem; color: var(--muted); line-height: 1.3; }
.poll-end { display: block; margin: 4px 0 0 auto; }
.poll-inputs #poll-inputs input { margin: 0 0 8px; }
.poll-inputs .btn { min-height: 42px; }
@media (prefers-reduced-motion: reduce) { .poll-bar { transition: none; } }

/* ---------- Video và tệp trong trò chuyện ---------- */
.bubble.has-video { padding: 4px; background: transparent !important; border-color: transparent !important; }
.bubble.has-video time { padding: 0 6px 2px; color: var(--muted); opacity: 1; }
.bubble.has-video .msg-text { padding: 2px 8px 0; color: var(--text); }
.msg-video video {
  display: block; width: min(280px, 64vw); max-height: 360px; border-radius: 14px; background: #000;
}
.msg-video-dl { display: block; margin: 4px 6px 0; text-align: left; font-size: .75rem; color: var(--muted); text-decoration: none; }
.msg-file {
  display: flex; align-items: center; gap: 10px; min-width: min(240px, 60vw); margin: 2px 0 6px; padding: 8px 10px;
  border-radius: 12px; background: color-mix(in srgb, currentColor 8%, transparent); color: inherit; text-decoration: none;
}
.msg-file-icon { flex: none; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; background: var(--surface-2); font-size: 1.3rem; }
.msg-file-info { min-width: 0; line-height: 1.3; }
.msg-file-info b { display: block; font-weight: 650; overflow-wrap: anywhere; }
.msg-file-info small { display: block; font-size: .72rem; opacity: .75; }

.upload-bar {
  position: absolute; left: 8px; right: 8px; bottom: calc(100% + 6px); z-index: 6;
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 6px 12px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 8px 28px rgb(0 0 0 / 16%);
}
.upload-info { display: flex; justify-content: space-between; gap: 10px; font-size: .85rem; min-width: 0; }
.upload-info span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-info b { flex: none; font-variant-numeric: tabular-nums; color: var(--primary); }
.upload-track { grid-column: 1; height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.upload-track span { display: block; height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.upload-bar .link-btn { grid-column: 2; grid-row: 1 / span 2; color: var(--danger); font-size: .85rem; }
.chat-input.uploading .attach-btn { opacity: .5; pointer-events: none; }

/* ---------- Thả cảm xúc ---------- */
.bubble-row { display: flex; align-items: center; gap: 4px; min-width: 0; }
.msg.mine .bubble-row { flex-direction: row-reverse; }
.bubble-row .bubble { min-width: 0; }
.react-btn {
  flex: none; width: 32px; height: 32px; border: 0; border-radius: 999px; padding: 0;
  background: var(--surface-2); color: var(--muted); font-size: 1.15rem; line-height: 1; cursor: pointer;
  opacity: 0; transition: opacity .12s;
}
.react-btn:focus-visible { opacity: 1; outline: 3px solid var(--primary); outline-offset: 2px; }
/* Máy tính: hiện nút khi rê chuột vào tin. Điện thoại: chạm hoặc nhấn giữ vào tin để thả cảm xúc */
@media (hover: hover) and (pointer: fine) { .msg:hover .react-btn { opacity: 1; } }
@media (hover: none), (pointer: coarse) { .react-btn { display: none; } }
.react-pick { flex-wrap: wrap; max-width: 100%; }
.react-more-btn { color: var(--muted) !important; font-size: 1.2rem !important; font-weight: 700; }
.react-more-btn.on { background: var(--surface-2) !important; }
.react-grid {
  display: grid; grid-template-columns: repeat(8, 36px); gap: 2px; padding: 6px; border-radius: 16px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: 0 4px 16px rgb(0 0 0 / 12%);
}
.react-grid button { width: 36px; height: 36px; border: 0; border-radius: 10px; background: none; font-size: 1.3rem; cursor: pointer; padding: 0; }
.react-grid button.on { background: var(--primary-soft); }
.react-grid button:active { transform: scale(1.2); }
@media (max-width: 380px) { .react-grid { grid-template-columns: repeat(6, 36px); } }
/* Bong bóng giữ xuống dòng của nội dung tin (pre-wrap): khối video / tệp thì không */
.msg-video, .msg-file { white-space: normal; }
/* 6 cảm xúc nhanh + nút ＋ nằm gọn trên một hàng trên điện thoại */
.react-pick { flex-wrap: nowrap; }
.react-pick button { width: 38px; height: 38px; font-size: 1.3rem; }
