:root {
  --bg: #f4f6fa; --card: #fff; --text: #1d2433; --muted: #6b7489; --line: #e3e7ef;
  --primary: #0f4c81; --primary-2: #e6eef7;
  --income: #1f8a4c; --personal: #d9822b; --customer: #c0392b; --refund: #2d7dd2;
  --radius: 12px; --shadow: 0 1px 3px rgba(16,24,40,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c; --card: #1a2029; --text: #e6e9ef; --muted: #98a2b3; --line: #2a3240;
    --primary: #4c9be8; --primary-2: #1e2c3d; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--customer); min-height: 1.2em; margin: .5rem 0 0; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); min-height: 42px;
}
textarea { min-height: 70px; resize: vertical; }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .75rem; }
label > input, label > select, label > textarea { margin-top: .25rem; color: var(--text); }
.btn { border: 1px solid var(--line); background: var(--card); padding: .55rem 1rem; border-radius: 8px;
  cursor: pointer; min-height: 40px; white-space: nowrap; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.danger { color: var(--customer); }
.btn.sm { padding: .3rem .6rem; min-height: 32px; font-size: .85rem; }
.btn.block { width: 100%; }
.link { background: none; border: 0; color: var(--primary); cursor: pointer; padding: .25rem 0; text-align: left; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; padding: 2rem 1.5rem; }
.brand { font-weight: 700; font-size: 1.15rem; display: flex; align-items: center; gap: .5rem; }
.logo { background: var(--primary); color: #fff; border-radius: 8px; padding: .15rem .45rem; font-size: .9rem; }

/* layout */
.layout { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.sidebar { background: var(--card); border-right: 1px solid var(--line); padding: 1.25rem 1rem;
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 1.25rem; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar nav a { padding: .55rem .75rem; border-radius: 8px; color: var(--text); text-decoration: none; }
.sidebar nav a.active { background: var(--primary-2); color: var(--primary); font-weight: 600; }
.me { border-top: 1px solid var(--line); padding-top: .75rem; display: flex; flex-direction: column; font-size: .9rem; }
main { padding: 1.25rem 1.5rem 2rem; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.topbar h1 { margin: 0; font-size: 1.35rem; }
#page-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.bottom-nav, .fab { display: none; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem; }
.grid { display: grid; gap: 1rem; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.kpi .label { font-size: .8rem; color: var(--muted); }
.kpi .value { font-size: 1.35rem; font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .78rem; color: var(--muted); }
.c-income { color: var(--income); } .c-personal { color: var(--personal); }
.c-customer { color: var(--customer); } .c-refund { color: var(--refund); }
.two { grid-template-columns: 1.4fr 1fr; }
.card h3 { margin: 0 0 .75rem; font-size: 1rem; }
.chart-box { position: relative; height: 260px; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.filters label { margin: 0; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .6rem .5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.actions { white-space: nowrap; text-align: right; }
.tag { display: inline-block; padding: .1rem .5rem; border-radius: 99px; font-size: .75rem; background: var(--primary-2); }
.tag.income { color: var(--income); } .tag.personal_expense { color: var(--personal); }
.tag.customer_expense { color: var(--customer); } .tag.open { color: var(--personal); } .tag.settled { color: var(--income); }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.tabs { display: flex; gap: .4rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tabs button { border: 1px solid var(--line); background: var(--card); border-radius: 99px; padding: .35rem .9rem; cursor: pointer; }
.tabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.note { font-size: .85rem; color: var(--muted); background: var(--primary-2); border-radius: 8px; padding: .6rem .8rem; margin-bottom: 1rem; }
.progress { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: .3rem; }
.progress > span { display: block; height: 100%; background: var(--primary); }
.progress.over > span { background: var(--customer); }

/* mobile list rows */
.list-item { display: flex; justify-content: space-between; gap: .75rem; padding: .7rem 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .t { font-weight: 600; }
.list-item .s { font-size: .8rem; color: var(--muted); }
.list-item .amt { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }

dialog { border: 0; border-radius: var(--radius); padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--card); color: var(--text); box-shadow: 0 10px 40px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10,15,25,.45); }
#modal-form { padding: 1.25rem; max-height: 85vh; overflow-y: auto; }
#modal-form h2 { margin: 0 0 1rem; font-size: 1.15rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .75rem; }
.form-grid .full { grid-column: 1 / -1; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .5rem; }
.checks { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; padding: .5rem; }
.checks label { display: flex; gap: .5rem; align-items: center; color: var(--text); margin: .25rem 0; }
.checks input { width: auto; min-height: 0; }

#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #1d2433; color: #fff;
  padding: .6rem 1rem; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 50; }
#toast.show { opacity: .95; }

.only-mobile { display: none; }
.center { text-align: center; justify-content: center; }
.login-icon { display: block; width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 1rem; }
.btn.faceid { display: flex; align-items: center; justify-content: center; gap: .5rem; min-height: 50px;
  background: var(--text); color: var(--bg); border: 0; font-weight: 600; font-size: 1rem; }
.or { display: flex; align-items: center; gap: .6rem; color: var(--muted); font-size: .8rem; margin: 1rem 0; }
.or::before, .or::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
#login-form .btn.faceid { margin-top: 1rem; }

/* ---------- iPhone (15/16, 15/16 Plus/Pro/Pro Max) ---------- */
html { -webkit-text-size-adjust: 100%; }
body { -webkit-tap-highlight-color: transparent; overscroll-behavior-y: none; }
button, a, .list-item { touch-action: manipulation; }
.login { padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
@supports (-webkit-touch-callout: none) {
  /* iOS phóng to khi ô nhập < 16px – giữ 16px để không bị zoom */
  input, select, textarea { font-size: 16px; }
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  main { padding: 0 max(16px, env(safe-area-inset-right)) calc(84px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  /* thanh tiêu đề dính trên, chừa vùng Dynamic Island / tai thỏ */
  .topbar { position: sticky; top: 0; z-index: 15; margin: 0 -16px 1rem; padding: calc(env(safe-area-inset-top) + .7rem) 16px .7rem;
    background: color-mix(in srgb, var(--bg) 88%, transparent); -webkit-backdrop-filter: saturate(180%) blur(14px);
    backdrop-filter: saturate(180%) blur(14px); border-bottom: 1px solid var(--line); flex-wrap: nowrap; }
  #page-actions { flex-wrap: nowrap; overflow-x: auto; }
  #page-actions .btn { min-height: 36px; padding: .4rem .75rem; }
  .bottom-nav a { min-height: 50px; }
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 16px 16px 0 0; max-height: 92vh; }
  #modal-form { max-height: 92vh; padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)); }
  .modal-actions { position: sticky; bottom: 0; background: var(--card); padding-top: .5rem; }
  .modal-actions .btn { flex: 1; min-height: 46px; }
  .two { grid-template-columns: 1fr; }
  .bottom-nav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; background: var(--card);
    border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
  .bottom-nav a { flex: 1; text-align: center; padding: .5rem .2rem; font-size: .72rem; color: var(--muted); text-decoration: none; }
  .bottom-nav a .ic { display: block; font-size: 1.2rem; line-height: 1.2; }
  .bottom-nav a.active { color: var(--primary); font-weight: 600; }
  .fab { display: grid; place-items: center; position: fixed; right: 16px; bottom: calc(72px + env(safe-area-inset-bottom));
    width: 54px; height: 54px; border-radius: 50%; border: 0; background: var(--primary); color: #fff;
    font-size: 1.8rem; box-shadow: 0 4px 14px rgba(0,0,0,.25); z-index: 21; cursor: pointer; }
  .only-desktop { display: none; }
  .only-mobile { display: block; }
  .form-grid { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi .value { font-size: 1.1rem; }
  .topbar h1 { font-size: 1.15rem; }
}

/* =================== Hiệu ứng 3D =================== */
:root {
  --d1: 0 1px 1px rgba(16,24,40,.06), 0 2px 4px rgba(16,24,40,.06);
  --d2: 0 2px 2px rgba(16,24,40,.05), 0 6px 12px rgba(16,24,40,.08), 0 14px 28px -8px rgba(16,24,40,.18);
  --d3: 0 4px 6px rgba(16,24,40,.06), 0 14px 28px rgba(16,24,40,.12), 0 30px 60px -12px rgba(16,24,40,.28);
  --hl: rgba(255,255,255,.75);
}
@media (prefers-color-scheme: dark) {
  :root { --d1: 0 1px 2px rgba(0,0,0,.5); --d2: 0 2px 4px rgba(0,0,0,.4), 0 10px 24px -6px rgba(0,0,0,.6);
    --d3: 0 6px 12px rgba(0,0,0,.45), 0 24px 48px -10px rgba(0,0,0,.75); --hl: rgba(255,255,255,.07); }
}
#page { perspective: 1400px; }
.card {
  position: relative; box-shadow: var(--d2), inset 0 1px 0 var(--hl);
  background: linear-gradient(160deg, color-mix(in srgb, var(--card) 100%, #fff 6%) 0%, var(--card) 55%, color-mix(in srgb, var(--card) 94%, #000 6%) 100%);
  transform-style: preserve-3d; transition: transform .35s cubic-bezier(.2,.8,.2,1), box-shadow .35s;
}
/* thẻ nghiêng theo con trỏ (máy tính) */
.tilt { will-change: transform; }
.tilt:hover { box-shadow: var(--d3), inset 0 1px 0 var(--hl); }
.tilt::after { content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; opacity: 0; transition: opacity .3s;
  background: radial-gradient(circle at var(--mx, 50%) var(--my, 0%), rgba(255,255,255,.28), transparent 55%); }
.tilt:hover::after { opacity: 1; }
.tilt > * { transform: translateZ(18px); }
/* nhấn trên điện thoại: lún xuống */
.pressable:active, .tilt:active { transform: scale(.97) translateZ(0) !important; box-shadow: var(--d1); transition-duration: .08s; }

/* thẻ KPI: dải màu + biểu tượng nổi */
.kpi { overflow: hidden; padding-left: 4.1rem; min-height: 86px; }
.kpi .ico3d { position: absolute; left: .85rem; top: 50%; transform: translateY(-50%) translateZ(30px); }
.kpi::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--accent, var(--primary)); }

/* biểu tượng 3D (khối bóng) cho hạng mục */
.ico3d {
  --c: var(--primary); flex: none; display: inline-grid; place-items: center; width: 42px; height: 42px; border-radius: 13px;
  font-size: 21px; line-height: 1; color: #fff;
  background: radial-gradient(120% 90% at 30% 15%, color-mix(in srgb, var(--c) 55%, #fff) 0%, var(--c) 45%, color-mix(in srgb, var(--c) 70%, #000) 100%);
  box-shadow: 0 1px 0 rgba(255,255,255,.45) inset, 0 -3px 6px rgba(0,0,0,.25) inset, 0 6px 12px -3px color-mix(in srgb, var(--c) 70%, transparent), 0 2px 3px rgba(0,0,0,.18);
  text-shadow: 0 1px 2px rgba(0,0,0,.25); transform: translateZ(0); transition: transform .25s cubic-bezier(.2,.8,.2,1);
}
.ico3d.sm { width: 30px; height: 30px; border-radius: 9px; font-size: 15px; }
.ico3d.xs { width: 22px; height: 22px; border-radius: 7px; font-size: 12px; vertical-align: middle; margin-right: .35rem; }
.list-item:hover .ico3d, tr:hover .ico3d { transform: translateY(-2px) rotateX(12deg) rotateY(-12deg) scale(1.06); }
.list-item { align-items: center; }
.list-item > .lead { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.list-item > .lead > div { min-width: 0; }
.list-item .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* hàng danh sách nổi khi rê chuột */
.list-item[data-tx] { border-radius: 10px; padding-left: .4rem; padding-right: .4rem; margin: 0 -.4rem;
  transition: transform .2s, background .2s, box-shadow .2s; }
.list-item[data-tx]:hover { background: var(--primary-2); transform: translateY(-1px); box-shadow: var(--d1); }

/* nút nổi */
.btn { box-shadow: var(--d1), inset 0 1px 0 var(--hl); transition: transform .12s, box-shadow .2s, filter .2s; }
.btn:hover { transform: translateY(-1px); box-shadow: var(--d2), inset 0 1px 0 var(--hl); }
.btn:active { transform: translateY(1px); box-shadow: inset 0 2px 4px rgba(0,0,0,.18); }
.btn.primary { background: linear-gradient(180deg, color-mix(in srgb, var(--primary) 80%, #fff) 0%, var(--primary) 55%, color-mix(in srgb, var(--primary) 80%, #000) 100%);
  box-shadow: 0 1px 0 rgba(255,255,255,.35) inset, 0 6px 14px -4px color-mix(in srgb, var(--primary) 80%, transparent); }
.btn.faceid { box-shadow: var(--d2); }
.tabs button { box-shadow: var(--d1); transition: transform .15s, box-shadow .2s; }
.tabs button.active { box-shadow: 0 6px 14px -4px color-mix(in srgb, var(--primary) 80%, transparent), inset 0 1px 0 rgba(255,255,255,.3); transform: translateY(-1px); }

/* FAB dạng viên bi */
.fab { background: radial-gradient(100% 100% at 30% 20%, color-mix(in srgb, var(--primary) 50%, #fff), var(--primary) 50%, color-mix(in srgb, var(--primary) 65%, #000));
  box-shadow: 0 1px 0 rgba(255,255,255,.5) inset, 0 -4px 8px rgba(0,0,0,.25) inset, 0 10px 22px -4px color-mix(in srgb, var(--primary) 75%, transparent); transition: transform .15s; }
.fab:active { transform: scale(.92); }

/* thanh điều hướng: mục đang chọn nổi lên */
.sidebar nav a { transition: transform .2s, box-shadow .2s, background .2s; }
.sidebar nav a:hover { transform: translateX(3px); }
.sidebar nav a.active { box-shadow: var(--d1), inset 0 1px 0 var(--hl); }
.bottom-nav a .ic { transition: transform .25s cubic-bezier(.2,.8,.2,1); }
.bottom-nav a.active .ic { transform: translateY(-3px) scale(1.18); text-shadow: 0 4px 8px color-mix(in srgb, var(--primary) 60%, transparent); }
.bottom-nav { box-shadow: 0 -8px 24px -12px rgba(0,0,0,.25); }

/* thanh tiến độ dạng ống */
.progress { box-shadow: inset 0 1px 2px rgba(0,0,0,.2); height: 8px; }
.progress > span { background: linear-gradient(180deg, color-mix(in srgb, var(--primary) 60%, #fff), var(--primary) 60%); box-shadow: 0 1px 3px color-mix(in srgb, var(--primary) 60%, transparent); }
.progress.over > span { background: linear-gradient(180deg, color-mix(in srgb, var(--customer) 60%, #fff), var(--customer) 60%); }

/* hiệu ứng xuất hiện: lật nhẹ từ dưới lên */
@keyframes rise3d { from { opacity: 0; transform: translateY(18px) rotateX(-14deg); } to { opacity: 1; transform: none; } }
.anim-in .card, .anim-in .kpis > .card { animation: rise3d .55s cubic-bezier(.2,.8,.2,1) both; transform-origin: 50% 100%; }
.anim-in .card:nth-child(2) { animation-delay: .05s; } .anim-in .card:nth-child(3) { animation-delay: .1s; }
.anim-in .card:nth-child(4) { animation-delay: .15s; } .anim-in .card:nth-child(5) { animation-delay: .2s; }
.anim-in .card:nth-child(n+6) { animation-delay: .25s; }
dialog[open] { animation: rise3d .35s cubic-bezier(.2,.8,.2,1); }
.login-card { box-shadow: var(--d3), inset 0 1px 0 var(--hl); }
.login-icon { box-shadow: 0 12px 24px -8px rgba(15,76,129,.6), 0 2px 4px rgba(0,0,0,.2); transform: perspective(400px) rotateX(8deg); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .tilt > * { transform: none; }
}

/* trang chi phí hợp đồng: bảng chi tiết rộng hơn */
.two.wide { grid-template-columns: minmax(0, 2.2fr) minmax(0, 1fr); }
@media (max-width: 860px) { .two.wide { grid-template-columns: 1fr; } }

/* bổ sung mục thu chi phát sinh */
.newcat { background: var(--primary-2); border-radius: 10px; padding: .6rem .75rem .1rem; margin-bottom: .75rem;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.08); animation: rise3d .3s cubic-bezier(.2,.8,.2,1); }
.newcat-line { display: flex; gap: .5rem; margin-top: .25rem; }
.newcat-line input { flex: 1; margin: 0; }
.newcat .error { margin: 0 0 .4rem; min-height: 0; }
