:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #17201d;
  --muted: #66736f;
  --line: #e2e7e5;
  --accent: #0f766e;
  --accent-soft: #d8efeb;
  --neg: #c2410c;
  --pos: #15803d;
  --danger: #b91c1c;
  --series-income: #2a78d6;
  --series-expense: #eb6834;
  --grid: #e8ecea;
  --axis: #c9d1ce;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --surface: #18201e;
    --text: #e7eeec;
    --muted: #93a19d;
    --line: #2a3431;
    --accent: #2dd4bf;
    --accent-soft: #123a35;
    --neg: #fb923c;
    --pos: #4ade80;
    --danger: #f87171;
    --series-income: #3987e5;
    --series-expense: #d95926;
    --grid: #26302d;
    --axis: #3a4542;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* bỏ độ trễ khi chạm trên điện thoại */
}
button, input, select, textarea { font: inherit; color: inherit; }
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 0 8px; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.month { display: flex; align-items: center; gap: 4px; }
.month span { min-width: 104px; text-align: center; font-weight: 600; }
.month button { border: 0; background: var(--accent-soft); color: var(--accent); width: 38px; height: 38px; border-radius: 50%; font-size: 20px; cursor: pointer; }

/* Tổng tiền hiện có */
.card.total-card { background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 70%, #000)); color: #fff; border: 0; }
.total-label { font-size: 13px; opacity: .85; }
.total-amount { display: block; font-size: clamp(26px, 8vw, 34px); font-weight: 700; font-variant-numeric: tabular-nums; margin: 2px 0 10px; white-space: nowrap; }
.balance-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.balance-list li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; cursor: pointer; background: rgb(255 255 255 / .1); }
.balance-list li:active, .balance-list li.selected { background: rgb(255 255 255 / .22); }
.balance-list b { font-variant-numeric: tabular-nums; white-space: nowrap; }
.total-card .muted { color: #fff; opacity: .75; margin: 8px 0 0; }
@media (prefers-color-scheme: dark) { .total-card { color: #04201d; } .total-card .muted { color: #04201d; } .balance-list li { background: rgb(0 0 0 / .1); } .balance-list li.selected { background: rgb(0 0 0 / .2); } }

main { max-width: 720px; margin: 0 auto; padding: 8px 16px 24px; }
.tab { display: none; }
.tab.active { display: block; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.stat span { display: block; color: var(--muted); font-size: 13px; }
.stat strong { font-size: clamp(16px, 4.8vw, 22px); font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--neg); }
.pos { color: var(--pos); }

.bars { display: grid; gap: 8px; }
.bar { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.bar .label { display: flex; justify-content: space-between; font-size: 14px; }
.bar .label b { font-variant-numeric: tabular-nums; }
.bar .track { height: 6px; background: var(--line); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.bar .fill { height: 100%; background: var(--accent); border-radius: 3px; }

.txlist { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.txlist li { display: flex; align-items: center; gap: 12px; padding: 11px 14px; min-height: 58px; border-top: 1px solid var(--line); cursor: pointer; }
.txlist li:first-child { border-top: 0; }
.txlist li:hover, .txlist li:active { background: var(--accent-soft); }
.txlist .empty { justify-content: center; color: var(--muted); cursor: default; }
.txlist .empty:hover { background: none; }
.tx-icon { width: 36px; height: 36px; flex: none; border-radius: 10px; display: grid; place-items: center; background: var(--accent-soft); font-size: 17px; }
.tx-main { flex: 1; min-width: 0; }
.tx-main .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-main .sub { font-size: 12.5px; color: var(--muted); }
.tx-amount { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge { display: inline-block; font-size: 11px; padding: 0 6px; border-radius: 6px; background: var(--line); margin-left: 4px; }

.filters { display: flex; gap: 8px; margin: 12px 0; }
.filters input { flex: 1; min-width: 0; }
/* 16px để iPhone không tự phóng to khi chạm vào ô nhập */
input, select, textarea {
  width: 100%; padding: 10px 12px; min-height: 44px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
textarea { min-height: 0; }
.filters select { width: auto; max-width: 40%; }
.filters { flex-wrap: wrap; }
.filters input { flex-basis: 100%; }
@media (min-width: 560px) { .filters { flex-wrap: nowrap; } .filters input { flex-basis: auto; } }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-top: 12px; }
.card h2 { margin-top: 0; }
.acc { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.acc h3 { margin: 0 0 6px; font-size: 15px; }
.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; font-size: 14px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

button.primary { background: var(--accent); color: #fff; border: 0; border-radius: 10px; padding: 10px 18px; min-height: 44px; font-weight: 600; cursor: pointer; }
@media (prefers-color-scheme: dark) { button.primary { color: #062320; } }
button.primary:disabled { opacity: .6; cursor: wait; }
button.wide { width: 100%; }
button.danger { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 10px; padding: 9px 14px; cursor: pointer; }
#cancelBtn { background: none; border: 1px solid var(--line); border-radius: 10px; padding: 9px 14px; cursor: pointer; }

nav.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
}
nav.bottom button {
  border: 0; background: none; color: var(--muted); font-size: 11px; line-height: 1.2;
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 2px; min-height: 52px; cursor: pointer;
}
nav.bottom svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
nav.bottom button.active { color: var(--accent); font-weight: 600; }
.fab {
  justify-self: center; width: 58px; height: 58px; margin-bottom: 6px; border-radius: 50%;
  display: grid; place-items: center; cursor: pointer;
  background: var(--accent); box-shadow: 0 4px 14px rgb(0 0 0 / .25);
}
.fab svg { width: 26px; height: 26px; fill: none; stroke: #fff; stroke-width: 1.9; stroke-linejoin: round; }
@media (prefers-color-scheme: dark) { .fab svg { stroke: #04201d; } }
.fab:active { transform: scale(.95); }

dialog {
  width: min(560px, calc(100% - 24px)); max-height: calc(100% - 24px);
  border: 0; border-radius: 18px; padding: 0; background: var(--surface); color: var(--text);
}
/* Trên điện thoại: form mở như tấm trượt từ dưới lên, nút Lưu luôn ở đáy */
@media (max-width: 560px) {
  dialog { width: 100%; max-width: 100%; max-height: 94dvh; margin: auto 0 0; border-radius: 18px 18px 0 0; }
  dialog form { padding-bottom: 0; }
  dialog .actions {
    position: sticky; bottom: 0; margin: 4px -18px 0; padding: 10px 18px calc(10px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--line);
  }
  .actions button { min-height: 46px; }
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { padding: 18px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 17px; text-transform: none; letter-spacing: 0; color: var(--text); }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
dialog label input, dialog label select, dialog label textarea { color: var(--text); font-size: 16px; }
.row { display: flex; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.cur { width: 84px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 10px; padding: 3px; }
.seg label { display: block; text-align: center; padding: 7px; border-radius: 8px; cursor: pointer; color: var(--text); font-size: 14px; }
.seg input { display: none; }
.seg label:has(input:checked) { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / .15); font-weight: 600; }
.preview { width: 100%; max-height: 220px; object-fit: contain; border-radius: 10px; background: var(--bg); }
.scan { padding: 10px 12px; border-radius: 10px; background: var(--accent-soft); font-size: 14px; }
.scan.error { background: color-mix(in srgb, var(--danger) 15%, transparent); }
.items { list-style: none; padding: 0; margin: 8px 0 0; font-size: 14px; }
.items li { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; border-bottom: 1px dashed var(--line); }
details summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }

/* Biểu đồ thu chi */
.chart-card { position: relative; padding: 14px 14px 10px; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chart-head h2 { margin: 0; }
.legend { display: flex; gap: 12px; font-size: 13px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.sw-income { background: var(--series-income); }
.sw-expense { background: var(--series-expense); }
.chart { margin-top: 8px; height: 200px; }
@media (max-width: 560px) { .chart { height: 170px; } }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .income { fill: var(--series-income); }
.chart .expense { fill: var(--series-expense); }
.chart .band { fill: transparent; cursor: pointer; }
.chart .band.selected { fill: var(--accent-soft); }
.chart .band:hover { fill: color-mix(in srgb, var(--accent-soft) 60%, transparent); }
.chart .tick.selected { fill: var(--text); font-weight: 600; }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 2;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; font-size: 13px; box-shadow: 0 4px 14px rgb(0 0 0 / .12); white-space: nowrap;
}
.chart-tip b { display: block; margin-bottom: 2px; }
.chart-tip div { display: flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.chart-card details { margin-top: 6px; }
.flow-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 6px; font-variant-numeric: tabular-nums; }
.flow-table th, .flow-table td { padding: 5px 4px; text-align: right; border-bottom: 1px solid var(--line); }
.flow-table th:first-child, .flow-table td:first-child { text-align: left; }
.flow-table th { color: var(--muted); font-weight: 500; }

.cash { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.cash strong { font-size: 24px; font-variant-numeric: tabular-nums; }
button.ghost { background: none; border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; cursor: pointer; }
#viewAccount { width: 100%; max-width: none; }

.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; }
.login-card img { border-radius: 14px; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.error { color: var(--danger); margin: 0; font-size: 14px; }
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  max-width: calc(100% - 32px);
}
