:root {
  --bg: #f2f0ea;           /* Kim: trắng bạc */
  --panel: #fdfcf9;
  --ink: #24221d;
  --muted: #6f6a5f;
  --line: #e2ddd1;
  --accent: #8a6416;       /* Thổ: vàng đất — Hoả sinh Thổ, Thổ sinh Kim */
  --accent-ink: #fff8e8;
  --accent-soft: #f3e5c2;
  --danger: #a3372b;
  --radius: 10px;
  font-family: "Be Vietnam Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 15px; line-height: 1.45; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
h1, h2 { font-family: Georgia, "Times New Roman", serif; font-weight: 600; letter-spacing: -0.01em; }

/* Buttons & inputs */
.btn { appearance: none; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 9px 16px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; transition: background .15s, border-color .15s; }
.btn:hover { border-color: #d4c29a; background: #fff; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #6f500f; border-color: #6f500f; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn.wide { width: 100%; padding: 12px; }
.btn:disabled { opacity: .55; cursor: default; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; font-size: 13px; padding: 2px 4px; }
.link.danger { color: var(--danger); }
.input { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font: inherit; color: var(--ink); }
.input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 500; }
.msg { min-height: 1.2em; font-size: 13px; color: var(--danger); margin: 10px 0 0; }
.msg.inline { margin-left: 10px; color: var(--accent); }
.hint { font-size: 13px; color: var(--muted); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 36px 28px; text-align: center; box-shadow: 0 12px 40px -20px rgba(90, 64, 14, .3); }
.login-card h1 { margin: 16px 0 4px; font-size: 24px; }
.login-card .input { margin-bottom: 10px; }
.login-card .hint { margin: 18px 0 12px; }
#login-signin { margin-top: 24px; }
.brand-mark { position: relative; display: inline-grid; place-items: center; width: 52px; height: 52px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); font-family: Georgia, serif; font-size: 22px; }
.brand-mark.sm { width: 36px; height: 36px; font-size: 15px; border-radius: 9px; }

/* Layout */
.app { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 20px; position: sticky; top: 0; height: 100vh; }
.side-brand { display: flex; gap: 10px; align-items: center; padding: 0 6px; }
.side-brand small { display: block; color: var(--muted); font-size: 12px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item { display: block; padding: 9px 12px; border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 500; }
.nav-item small { color: var(--muted); font-weight: 400; margin-left: 4px; }
.nav-item:hover { background: var(--bg); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.main { padding: 28px 32px 60px; min-width: 0; max-width: 1100px; }
.view-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.view-head h2 { margin: 2px 0 0; font-size: 26px; }
.eyebrow { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.export { display: flex; gap: 8px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }

.period { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.period label { min-width: 120px; }
.entry { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; border-left: 3px solid var(--accent); }
.entry .grow { flex: 1; min-width: 220px; }
.entry label:last-of-type { width: 170px; }
.entry-actions { display: flex; gap: 6px; }
.col { display: inline-block; font-size: 11px; background: var(--bg); border: 1px solid var(--line); padding: 0 5px; border-radius: 4px; margin-left: 4px; color: var(--muted); }

/* Ledger */
.table-card { padding: 0; overflow-x: auto; }
.ledger { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.ledger th, .ledger td { padding: 10px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.ledger thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #f7f4ec; }
.ledger tr.colcode th { font-size: 11px; padding-top: 0; padding-bottom: 6px; letter-spacing: 0; font-family: Georgia, serif; text-transform: none; }
.ledger .c-date { width: 120px; white-space: nowrap; }
.ledger .c-num { text-align: right; width: 170px; white-space: nowrap; }
.ledger .c-act { width: 110px; text-align: right; white-space: nowrap; }
.ledger tbody tr:hover { background: #faf8f2; }
.ledger tbody tr.editing { background: var(--accent-soft); }
.ledger tfoot td { border-bottom: 0; background: #f7f4ec; font-size: 16px; }
.empty { text-align: center; color: var(--muted); padding: 28px; margin: 0; }

/* Settings */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }

/* Passkeys */
.pk-list { list-style: none; margin: 0 0 16px; padding: 0; }
.pk-list li { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); gap: 10px; }
.pk-list small { color: var(--muted); display: block; }
.pk-add { display: flex; gap: 8px; flex-wrap: wrap; }
.pk-add .input { flex: 1; min-width: 200px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px; opacity: 0; pointer-events: none; transition: all .2s; z-index: 10; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side, nav { min-width: 0; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 12px 16px; gap: 10px; }
  nav { flex-direction: row; order: 3; flex: 1 1 100%; overflow-x: auto; }
  .nav-item { white-space: nowrap; padding: 7px 10px; }
  .side-brand { flex: 1; }
  .main { padding: 18px 16px 40px; }
  .entry label:last-of-type, .entry .grow { width: 100%; }
  .entry label { width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .ledger { font-size: 14px; }
  .ledger th, .ledger td { padding: 9px 8px; }
  .ledger .c-date { width: 86px; white-space: normal; }
  .ledger .c-num { width: auto; }
  .ledger .c-act { width: 44px; }
  .ledger .c-act .link { display: block; margin-left: auto; padding: 2px 0; }
}

/* --- Tổng quan & chi phí --- */
:root { --rev: #2f6fa3; --exp: #a8781a; --fire: #c2410c; } /* Thuỷ = tài lộc → doanh thu; Hoả: điểm nhấn nhỏ */
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.rev { background: var(--rev); }
.sw.exp { background: var(--exp); }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 0; }
.kpi { display: flex; flex-direction: column; gap: 4px; }
.kpi-label { font-size: 13px; color: var(--muted); font-weight: 500; }
.kpi b { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.kpi b.neg { color: var(--danger); }
.kpi small { color: var(--muted); font-size: 12px; }
.chart-card { position: relative; }
.chart-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chart-head h3, .card-title { margin: 0; font-size: 15px; font-family: inherit; font-weight: 600; }
.card-title { margin-bottom: 12px; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.chart { width: 100%; overflow-x: auto; }
.chart svg { display: block; }
.chart .grid { stroke: #efe4c9; stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover, .chart .hit.on { fill: rgba(138, 100, 22, .08); }
.chart-tip { position: absolute; pointer-events: none; background: var(--ink); color: #fff; font-size: 12px; padding: 8px 10px; border-radius: 8px; line-height: 1.5; white-space: nowrap; z-index: 2; font-variant-numeric: tabular-nums; }
.chart-tip b { display: block; margin-bottom: 2px; }
.data-table { margin-top: 10px; font-size: 13px; }
.data-table summary { cursor: pointer; color: var(--accent); }
.data-table table { width: 100%; border-collapse: collapse; margin-top: 8px; font-variant-numeric: tabular-nums; }
.data-table th, .data-table td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: right; }
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.cat-bars { display: flex; flex-direction: column; gap: 10px; }
.cat-row { display: grid; grid-template-columns: minmax(110px, 200px) 1fr auto; gap: 12px; align-items: center; font-size: 14px; }
.cat-track { height: 10px; background: #f1e7cf; border-radius: 0 4px 4px 0; overflow: hidden; }
.cat-fill { height: 100%; background: var(--exp); border-radius: 0 4px 4px 0; }
.cat-row .v { font-variant-numeric: tabular-nums; text-align: right; min-width: 110px; }
.cat-row .v small { color: var(--muted); margin-left: 6px; }
.entry.exp { border-left-color: var(--exp); }
.entry.rev { border-left-color: var(--rev); }
.entry.book label { width: 170px; }
.entry.book label.grow { flex: 1 1 340px; min-width: 220px; width: auto; }
.entry.book label.filebox { width: 260px; }
.filebox .input { padding: 6px 8px; font-size: 13px; }
.files { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 4px; }
.file { display: inline-flex; align-items: center; gap: 2px; font-size: 12px; }
.file a { color: var(--accent); text-decoration: none; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file a:hover { text-decoration: underline; }
.tag.rev { background: #e4edf6; color: #24557f; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: #f4ead0; color: #6f500f; white-space: nowrap; }
.sub { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
@media (max-width: 760px) {
  .kpis { grid-template-columns: 1fr; gap: 0; }
  .kpi b { font-size: 22px; }
  .entry.book label, .entry.book label.grow, .entry.book label.filebox { width: 100%; }
  .cat-row { grid-template-columns: 1fr auto; }
  .cat-row .cat-track { grid-column: 1 / -1; order: 3; }
}

/* Hoả (Minh): chấm nhỏ trên logo — Hoả sinh Thổ */
.brand-mark::after { content: ''; position: absolute; top: 5px; right: 5px; width: 7px; height: 7px; border-radius: 50%; background: var(--fire); box-shadow: 0 0 0 1.5px var(--accent); }
.brand-mark.sm::after { top: 3px; right: 3px; width: 5px; height: 5px; }

/* --- Sổ quỹ --- */
.q-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; margin-bottom: 14px; }
.q-card { text-align: left; font: inherit; color: inherit; cursor: pointer; display: flex; flex-direction: column; gap: 3px; margin: 0; position: relative; transition: border-color .15s, box-shadow .15s; }
.q-card:hover { border-color: #d4c29a; }
.q-card.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.q-card.muted-card { opacity: .6; }
.q-card.q-total { cursor: default; background: #f7f4ec; }
.q-type { font-size: 12px; color: var(--muted); }
.q-name { font-size: 15px; }
.q-bal { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.q-edit { position: absolute; top: 12px; right: 14px; font-size: 12px; color: var(--accent); }
.q-edit:hover { text-decoration: underline; }
.neg { color: var(--danger) !important; }
.tag.src-gd { background: #eceae4; color: #5c574c; }
.open-row td { background: #faf8f2; }
.entry.book label.chk { flex-direction: row; align-items: center; gap: 6px; width: auto; align-self: center; }
.danger-text { color: var(--danger); }

/* --- Dự tính chi trả --- */
.ledger.plan td { vertical-align: middle; }
.c-pri { width: 120px; }
.pri { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; white-space: nowrap; }
.pri.p1 { background: #f8e3dc; color: #8f2f12; }
.pri.p2 { background: #f6ebcf; color: #75520f; }
.pri.p3 { background: #e4edf6; color: #24557f; }
.pri.p4 { background: #eceae4; color: #5c574c; }
.due { display: block; font-size: 12px; color: var(--muted); }
.due.soon { color: #9a5b00; font-weight: 600; }
.due.late { color: var(--danger); font-weight: 600; }
.late-row td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.ok { color: #2f7a4a; font-weight: 600; font-size: 13px; white-space: nowrap; }
.short { color: var(--danger); font-weight: 600; font-size: 13px; white-space: nowrap; }
.muted { color: var(--muted); }
.pay-row td { background: var(--accent-soft); }
.pay-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.pay-form label { width: 170px; }
.paid-box summary { cursor: pointer; font-weight: 600; }
.paid-box .ledger { margin-top: 8px; }
@media (max-width: 760px) {
  .c-pri { width: auto; }
  .pay-form label { width: 100%; }
  .ledger.plan { min-width: 760px; }
}
.ledger.plan .c-num { width: 115px; }
.ledger.plan .c-act { width: 130px; }
.ledger.plan .c-pri { width: 110px; }
.kpi small b { font-size: inherit; font-weight: 600; letter-spacing: 0; }
.kpi small a { color: var(--accent); }

/* =========================================================
   MOBILE / iPhone (Add to Home Screen)
   ========================================================= */
.tabbar, .fab, .sheet, .period-toggle { display: none; }
@media (max-width: 760px) {
  html { -webkit-text-size-adjust: 100%; }
  body { font-size: 15px; -webkit-tap-highlight-color: transparent; overscroll-behavior-y: none; }
  .input, select.input, .btn { font-size: 16px; }          /* chặn iOS tự zoom khi gõ */
  .side { display: none !important; }
  .main { padding: calc(env(safe-area-inset-top) + 14px) 14px calc(env(safe-area-inset-bottom) + 150px); }
  .view-head { margin-bottom: 12px; align-items: center; }
  .view-head h2 { font-size: 24px; }
  .view-head .export { width: 100%; overflow-x: auto; flex-wrap: nowrap; gap: 6px; }
  .view-head .export .btn { padding: 7px 12px; font-size: 14px; white-space: nowrap; }
  .card { border-radius: 14px; padding: 14px; margin-bottom: 12px; }

  /* Thanh tab dưới */
  .tabbar:not(.hidden) { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    background: rgba(253, 252, 249, .94); -webkit-backdrop-filter: saturate(1.6) blur(14px); backdrop-filter: saturate(1.6) blur(14px);
    border-top: 1px solid var(--line); padding: 6px 4px calc(env(safe-area-inset-bottom) + 4px); }
  .tabbar a, .tabbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0; color: var(--muted); text-decoration: none; font: inherit; font-size: 11px; font-weight: 500; background: none; border: 0; }
  .tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .tabbar a:first-child svg { fill: currentColor; stroke: none; opacity: .9; }
  .tabbar .active { color: var(--accent); }
  .tabbar #tab-more svg { stroke-width: 3.2; }

  /* Nút thêm nhanh */
  .fab:not(.hidden) { display: grid; place-items: center; position: fixed; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 76px); z-index: 19;
    width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); box-shadow: 0 8px 22px -6px rgba(90, 64, 14, .55); }
  .fab svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
  .fab:active { transform: scale(.94); }

  /* Menu Khác (bottom sheet) */
  .sheet:not(.hidden) { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(36, 34, 29, .35); animation: fade .15s; }
  .sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; background: var(--panel); border-radius: 18px 18px 0 0; padding: 8px 12px calc(env(safe-area-inset-bottom) + 14px); animation: up .2s ease-out; }
  .sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: #d8d2c4; margin: 4px auto 10px; }
  .sheet-item { display: block; width: 100%; text-align: left; padding: 14px 12px; border: 0; border-bottom: 1px solid var(--line); background: none; font: inherit; font-size: 16px; font-weight: 500; color: var(--ink); text-decoration: none; }
  .sheet-item:last-child { border-bottom: 0; }
  .sheet-item small { display: block; color: var(--muted); font-size: 12px; font-weight: 400; margin-top: 2px; }
  @keyframes up { from { transform: translateY(100%); } }
  @keyframes fade { from { opacity: 0; } }

  /* Chọn kỳ: thu gọn thành 1 nút */
  .period-toggle:not(.hidden) { display: inline-flex; align-items: center; gap: 6px; margin: 0 0 12px; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font: inherit; font-size: 14px; font-weight: 600; color: var(--ink); }
  .period-toggle svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; }
  .period-toggle.open svg { transform: rotate(180deg); }
  #period-bar:not(.open) { display: none !important; }
  .period { gap: 10px; }
  .period label { min-width: 0; flex: 1 1 calc(50% - 10px); }

  /* Form nhập: ẩn, mở bằng nút + */
  .m-collapsible:not(.m-open) { display: none !important; }
  .m-collapsible.m-open { box-shadow: 0 10px 30px -18px rgba(90, 64, 14, .5); }
  .fab.on svg { transform: rotate(45deg); }
  .fab svg { transition: transform .15s; }

  /* KPI gọn */
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
  .kpis .kpi:last-child { grid-column: 1 / -1; }
  .kpi { margin: 0; }
  .kpi b { font-size: 20px; }
  .kpi small { font-size: 11px; }

  /* Bảng → thẻ */
  .table-card { overflow: visible; padding: 0; }
  .ledger, .ledger.plan { min-width: 0 !important; display: block; font-size: 15px; }
  .ledger thead { display: none; }
  .ledger tbody, .ledger tfoot { display: block; }
  .ledger tr { display: grid; grid-template-columns: auto 1fr auto; column-gap: 8px; row-gap: 3px; padding: 12px 14px; border-bottom: 1px solid var(--line); align-items: center; }
  .ledger td { display: block; padding: 0 !important; border: 0 !important; width: auto !important; min-width: 0; }
  .ledger td:empty { display: none; }
  .ledger tfoot tr { display: flex; justify-content: space-between; background: #f7f4ec; border-radius: 0 0 14px 14px; border-bottom: 0; }
  .ledger tfoot td:empty { display: none; }
  .ledger .c-act { display: flex !important; gap: 14px; justify-content: flex-end; }
  .ledger .c-act .link { display: inline; margin: 0; padding: 4px 0; font-size: 14px; }
  .ledger .c-date { font-size: 13px; color: var(--muted); white-space: nowrap; }
  .ledger .c-num { font-weight: 600; white-space: nowrap; text-align: right; }
  .ledger tbody tr.editing { background: var(--accent-soft); }

  /* Doanh thu / Chi phí: [diễn giải | số tiền] / [ngày · danh mục | thao tác] */
  .t-book tbody td:nth-child(3) { grid-column: 1 / 3; grid-row: 1; font-weight: 500; }
  .t-book tbody td:nth-child(4) { grid-column: 3; grid-row: 1; align-self: start; }
  .t-book tbody td:nth-child(1) { grid-column: 1; grid-row: 2; }
  .t-book tbody td:nth-child(2) { grid-column: 2; grid-row: 2; }
  .t-book tbody td:nth-child(5) { grid-column: 3; grid-row: 2; }
  .t-book .sub { font-weight: 400; }

  /* S1a */
  .t-s1a tbody td:nth-child(2) { grid-column: 1 / 3; grid-row: 1; }
  .t-s1a tbody td:nth-child(3) { grid-column: 3; grid-row: 1; }
  .t-s1a tbody td:nth-child(1) { grid-column: 1; grid-row: 2; }
  .t-s1a tbody td:nth-child(4) { grid-column: 3; grid-row: 2; }

  /* Sổ quỹ */
  .t-quy tbody td:nth-child(2) { grid-column: 1 / 3; grid-row: 1; }
  .t-quy tbody td:nth-child(3), .t-quy tbody td:nth-child(4) { grid-column: 3; grid-row: 1; }
  .t-quy tbody td:nth-child(3)::before { content: '+'; }
  .t-quy tbody td:nth-child(3) { color: var(--rev); }
  .t-quy tbody td:nth-child(4)::before { content: '−'; }
  .t-quy tbody td:nth-child(1) { grid-column: 1; grid-row: 2; }
  .t-quy tbody td:nth-child(5) { grid-column: 2; grid-row: 2; font-weight: 400; font-size: 13px; color: var(--muted); text-align: left; }
  .t-quy tbody td:nth-child(5)::before { content: 'Số dư '; }
  .t-quy tbody td:nth-child(6) { grid-column: 3; grid-row: 2; }
  .t-quy tbody tr.open-row td:nth-child(5) { grid-column: 3; grid-row: 1; font-weight: 600; color: var(--ink); font-size: 15px; }
  .t-quy tbody tr.open-row td:nth-child(5)::before { content: ''; }
  .t-quy tbody tr.open-row td:nth-child(1) { grid-row: 1; }
  .t-quy tfoot tr { flex-wrap: wrap; row-gap: 4px; }
  .t-quy tfoot td:nth-child(2) { flex: 1 1 100%; }
  .t-quy tfoot td:nth-child(3)::before { content: 'Thu '; font-weight: 400; color: var(--muted); }
  .t-quy tfoot td:nth-child(4)::before { content: 'Chi '; font-weight: 400; color: var(--muted); }
  .t-quy tfoot td:nth-child(5)::before { content: 'Cuối kỳ '; font-weight: 400; color: var(--muted); }
  .q-cards { grid-auto-flow: column; grid-template-columns: none; grid-auto-columns: 78%; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 4px; margin: 0 -14px 12px; padding-left: 14px; padding-right: 14px; }
  .q-card { scroll-snap-align: start; }

  /* Dự tính chi trả */
  .t-plan tbody tr:not(.pay-row) { grid-template-columns: auto 1fr auto; }
  .t-plan tbody td:nth-child(2) { grid-column: 1 / 3; grid-row: 1; }
  .t-plan tbody td:nth-child(4) { grid-column: 3; grid-row: 1; align-self: start; }
  .t-plan tbody td:nth-child(1) { grid-column: 1; grid-row: 2; }
  .t-plan tbody td:nth-child(3) { grid-column: 2; grid-row: 2; white-space: normal; }
  .t-plan tbody td:nth-child(3) .due { display: inline; margin-left: 6px; }
  .t-plan tbody td:nth-child(5) { display: none; }
  .t-plan tbody td:nth-child(6) { grid-column: 3; grid-row: 2; text-align: right; }
  .t-plan tbody td:nth-child(7) { grid-column: 1 / -1; grid-row: 3; }
  .t-plan tr.pay-row { display: block; }
  .t-plan tr.pay-row.hidden { display: none; }
  .late-row { box-shadow: inset 3px 0 0 var(--danger); }
  .late-row td:first-child { box-shadow: none; }
  .paid-box .ledger tr { grid-template-columns: auto 1fr auto; }
  .paid-box .ledger td:nth-child(2) { grid-column: 1 / -1; grid-row: 1; }
  .paid-box .ledger td:nth-child(3) { grid-column: 3; grid-row: 2; }
  .paid-box .ledger td:nth-child(1) { grid-column: 1; grid-row: 2; }
  .paid-box .ledger td:nth-child(4) { grid-column: 2; grid-row: 2; justify-content: flex-start; }

  .chart-card { padding: 12px; }
  .toast { bottom: calc(env(safe-area-inset-bottom) + 84px); }
  .login { padding-top: env(safe-area-inset-top); }
}
/* Khi mở từ màn hình chính (standalone) */
@media (display-mode: standalone) {
  body { -webkit-user-select: none; user-select: none; }
  input, textarea, .ledger td { -webkit-user-select: text; user-select: text; }
}

/* --- Cài đặt / danh mục --- */
.cfg-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.cat-card .card-title { display: flex; align-items: center; }
.cat-add { display: flex; gap: 8px; margin-bottom: 10px; }
.cat-add .input { flex: 1; }
.cat-list { list-style: none; margin: 0; padding: 0; }
.cat-list li { display: flex; align-items: center; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.cat-list li:last-child { border-bottom: 0; }
.cat-list .nm { flex: 1; min-width: 0; }
.cat-list .nm small { color: var(--muted); margin-left: 6px; font-size: 12px; }
.cat-list li.off .nm { color: var(--muted); text-decoration: line-through; }
.cat-list .ops { display: flex; gap: 2px; align-items: center; }
.cat-list .ops button { background: none; border: 0; font: inherit; font-size: 13px; color: var(--accent); cursor: pointer; padding: 4px 6px; border-radius: 6px; }
.cat-list .ops button:hover { background: var(--accent-soft); }
.cat-list .ops button.danger { color: var(--danger); }
.cat-list .ops button:disabled { opacity: .3; cursor: default; background: none; }
.cat-list .rename { flex: 1; display: flex; gap: 6px; }
.cat-list .rename .input { flex: 1; padding: 6px 9px; }
.cfg-links { display: flex; flex-direction: column; padding: 0; }
.cfg-link { padding: 14px 16px; border-bottom: 1px solid var(--line); color: var(--ink); text-decoration: none; font-weight: 500; }
.cfg-link:last-child { border-bottom: 0; }
.cfg-link small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.cfg-link:hover { background: #faf8f2; }
@media (max-width: 760px) {
  .cfg-grid { grid-template-columns: 1fr; gap: 0; }
  .cat-list li { padding: 11px 0; }
  .cat-list .ops button { font-size: 14px; padding: 6px 7px; }
}
.ledger tr.gap-row td { color: var(--muted); font-style: italic; }
.ledger tr.gap-row .tag { font-style: normal; }
