:root {
  --bg: #eef2f7;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #6b7686;
  --border: #e1e6ee;
  --primary: #1565c0;
  --primary-2: #1e88e5;
  --primary-text: #ffffff;
  --neg: #e53935;
  --danger: #c62828;
  --warn: #b54708;
  --ok: #067647;
  --bar: #9cc3f0;
  --radius: 14px;
  --shadow: 0 1px 3px rgb(16 24 40 / 0.08);
  --red-bg: #fef3f2;
  --red-line: #f04438;
  --green-bg: #ecfdf3;
  --green-line: #17b26a;
  --yellow-bg: #fffaeb;
  --yellow-line: #f79009;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --card: #19212b;
    --text: #e5ebf2;
    --muted: #94a0b0;
    --border: #2a3542;
    --primary: #3b8eea;
    --primary-2: #1d4f8f;
    --primary-text: #ffffff;
    --neg: #ff6b6b;
    --danger: #f97066;
    --warn: #fdb022;
    --ok: #47cd89;
    --bar: #2b5d99;
    --shadow: none;
    --red-bg: #3a1714;
    --red-line: #f97066;
    --green-bg: #0f2e1f;
    --green-line: #47cd89;
    --yellow-bg: #33270b;
    --yellow-line: #fdb022;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 16px/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));
}
h1, h2, h3 { margin: 0 0 8px; }
h2 { font-size: 1.05rem; margin-top: 20px; }
h3 { font-size: 0.95rem; color: var(--text); }
main { max-width: 720px; margin: 0 auto; padding: 12px 16px; }

/* Thanh tiêu đề xanh */
.topbar {
  position: sticky; top: 0; z-index: 5;
  background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: var(--primary-text);
  padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
}
.topbar-inner { max-width: 720px; margin-inline: auto; display: flex; align-items: center; gap: 6px; min-height: 30px; }
.topbar h1 { font-size: 1.15rem; margin: 0; }
.back { font: inherit; font-size: 1.8rem; line-height: 1; background: none; border: 0; color: inherit; padding: 0 8px 0 0; cursor: pointer; }

.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--card); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
}
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; }
.card-head.pad { padding: 14px 14px 4px; }
.link { font: inherit; font-size: 0.85rem; background: none; border: 0; color: var(--primary); cursor: pointer; padding: 0; }

input, select, textarea {
  font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 10px; width: 100%; min-width: 0;
}
textarea { resize: vertical; }

.btn {
  font: inherit; cursor: pointer; border-radius: 10px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  padding: 9px 14px; white-space: nowrap;
}
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.icon { padding: 9px 12px; }
.btn.icon.listening { background: var(--danger); color: #fff; }
.btn.wide { width: 100%; }
.btn.big-btn { padding: 13px; font-size: 1.05rem; font-weight: 600; margin-bottom: 16px; border-radius: 12px; }
.btn:disabled { opacity: 0.6; cursor: wait; }

.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; margin-top: 8px; }

.status { color: var(--muted); margin: 10px 0 0; min-height: 1.2em; }
.error { color: var(--danger); }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.right { text-align: right; }
.neg { color: var(--neg); }

/* Tổng quan: thẻ xanh lớn */
.hero {
  background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: var(--primary-text);
  border-radius: 18px; padding: 18px; margin-bottom: 12px; box-shadow: 0 6px 16px rgb(21 101 192 / 0.25);
}
.hero-label { opacity: 0.85; font-size: 0.9rem; }
.hero-total { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; margin: 2px 0; }
.hero-sub { font-size: 0.85rem; opacity: 0.9; }
.hero .badge { background: rgb(255 255 255 / 0.2); color: #fff; }
.hero-budget { margin-top: 14px; font-size: 0.85rem; }
.hero-budget .bar-label { opacity: 0.95; }
.hero-budget .bar-track { background: rgb(255 255 255 / 0.25); height: 8px; }
.hero-budget .bar-fill { background: #fff; }
.hero-budget .bar-fill.warn { background: #fdb022; }
.hero-budget .bar-fill.over { background: #ff8a80; }

/* Biểu đồ tròn */
.donut-wrap { display: grid; grid-template-columns: 150px 1fr; gap: 16px; align-items: center; }
.donut { position: relative; width: 150px; height: 150px; }
.donut svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.donut-center b { font-size: 0.95rem; font-variant-numeric: tabular-nums; }
.donut-center span { font-size: 0.72rem; color: var(--muted); }
.legend-list { display: flex; flex-direction: column; gap: 6px; font-size: 0.86rem; min-width: 0; }
.legend-item { display: grid; grid-template-columns: 10px 1fr auto; gap: 8px; align-items: center; }
.legend-item .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.legend-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-item .pct { color: var(--muted); font-variant-numeric: tabular-nums; }
@media (max-width: 380px) {
  .donut-wrap { grid-template-columns: 1fr; justify-items: center; }
  .legend-list { width: 100%; }
}

/* Chọn tháng */
.month-nav { display: grid; grid-template-columns: 44px 1fr 44px; gap: 8px; margin-bottom: 12px; }
.month-nav input { text-align: center; font-weight: 600; background: var(--card); }
.nav-arrow { font: inherit; font-size: 1.4rem; border: 1px solid var(--border); background: var(--card); color: var(--primary); border-radius: 10px; cursor: pointer; }

/* Danh sách kiểu sổ */
.list { display: flex; flex-direction: column; }
.list.gap { gap: 0; }
.day-group { background: var(--card); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: var(--radius); margin-bottom: 10px; overflow: hidden; }
.card .day-group { border: 0; box-shadow: none; border-radius: 0; margin: 0; border-top: 1px solid var(--border); }
.day-head { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.day-num { font-size: 1.6rem; font-weight: 700; line-height: 1; min-width: 34px; }
.day-meta { font-size: 0.78rem; color: var(--muted); line-height: 1.25; }
.day-meta b { color: var(--text); font-weight: 600; }
.day-sum { font-weight: 700; color: var(--neg); font-variant-numeric: tabular-nums; }
.expense {
  display: grid; grid-template-columns: 40px 1fr auto; gap: 12px; align-items: center;
  padding: 10px 14px; cursor: pointer;
}
.expense + .expense { border-top: 1px solid var(--border); }
.expense:hover { background: var(--bg); }
.expense .desc { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense .meta { color: var(--muted); font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense > div:nth-child(2) { min-width: 0; }
.expense .amt { font-weight: 700; color: var(--neg); font-variant-numeric: tabular-nums; }
.empty { padding: 16px; text-align: center; color: var(--muted); }

/* Icon tròn màu theo hạng mục */
.cat-ico {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 1.25rem;
  background: color-mix(in srgb, var(--c, #888) 18%, var(--card));
}

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sum-strip { display: flex; justify-content: space-between; align-items: center; padding: 0 4px 10px; font-size: 0.9rem; }

/* Ghi chép */
.amount-card { padding-bottom: 10px; }
.amount-row { display: flex; align-items: baseline; gap: 6px; border-bottom: 2px solid var(--neg); }
.amount-input {
  border: 0; background: transparent; padding: 4px 0; text-align: right;
  font-size: 2rem; font-weight: 700; color: var(--neg); font-variant-numeric: tabular-nums;
}
.amount-input:focus { outline: none; }
.currency { font-size: 1.3rem; font-weight: 700; color: var(--neg); }
.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px 4px; }
.cat-btn {
  font: inherit; background: none; border: 0; color: var(--text); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 2px; border-radius: 12px;
}
.cat-btn .cat-ico { width: 46px; height: 46px; font-size: 1.4rem; border: 2px solid transparent; }
.cat-btn span:last-child { font-size: 0.74rem; text-align: center; line-height: 1.2; }
.cat-btn.selected { background: color-mix(in srgb, var(--c) 10%, transparent); }
.cat-btn.selected .cat-ico { border-color: var(--c); }
.cat-btn.selected span:last-child { font-weight: 700; }
.form-rows .form-row { display: grid; grid-template-columns: 28px 1fr; align-items: center; gap: 8px; padding: 4px 14px; }
.form-row + .form-row { border-top: 1px solid var(--border); }
.form-row input { border: 0; background: transparent; padding: 10px 0; }
.form-row input:focus { outline: none; }
.fr-ico { font-size: 1.15rem; text-align: center; }
.ai-card h3 { margin-bottom: 10px; }
.quick { display: flex; flex-direction: column; gap: 8px; }
.quick-actions { display: flex; gap: 8px; justify-content: flex-end; }
.methods { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.method { text-align: center; padding: 11px 6px; white-space: normal; font-size: 0.92rem; }

/* Thẻ xem trước / sửa */
.expense-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.expense-form .full { grid-column: 1 / -1; }
.expense-form label { font-size: 0.8rem; color: var(--muted); display: flex; flex-direction: column; gap: 2px; }
.items { grid-column: 1 / -1; font-size: 0.9rem; }
.items .item { display: grid; grid-template-columns: 1fr 110px 32px; gap: 6px; margin-top: 6px; }
.items .item button { padding: 4px; }
.preview-card { position: relative; }
.preview-card .remove { position: absolute; top: 8px; right: 8px; }

/* Báo cáo */
.total-line { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px; margin-bottom: 14px; }
.big { font-size: 1.5rem; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row .bar-label { display: flex; justify-content: space-between; font-size: 0.9rem; gap: 8px; }
.bar-track { height: 10px; background: var(--bg); border-radius: 5px; overflow: hidden; margin-top: 4px; }
.bar-fill { height: 100%; background: var(--primary); border-radius: 5px; }
.bar-fill.warn { background: var(--warn); }
.bar-fill.over { background: var(--danger); }
.daily { display: flex; align-items: flex-end; gap: 2px; height: 140px; }
.daily .col { flex: 1; background: var(--bar); border-radius: 3px 3px 0 0; min-height: 2px; position: relative; }
.daily .col:hover { background: var(--primary); }
.legend { font-weight: 400; font-size: 0.75rem; margin-left: 6px; color: var(--muted); }

/* Xu hướng so với tháng trước: đỏ = tăng, xanh = giảm */
.badge { display: inline-block; font-size: 0.75rem; font-weight: 700; padding: 1px 7px; border-radius: 999px; white-space: nowrap; vertical-align: middle; }
.badge.up { background: var(--red-bg); color: var(--danger); }
.badge.down { background: var(--green-bg); color: var(--ok); }
.badge.flat { background: var(--bg); color: var(--muted); }
.badge.new { background: var(--yellow-bg); color: var(--warn); }
.big .badge { font-size: 0.85rem; }
.bar-row.up, .bar-row.down { border-radius: 8px; padding: 6px 8px; margin: 0 -8px; }
.bar-row.up { background: var(--red-bg); }
.bar-row.down { background: var(--green-bg); }
.bar-fill.up { background: var(--red-line); }
.bar-fill.down { background: var(--green-line); }
.bar-track.prev { height: 4px; margin-top: 3px; }
.bar-track.prev .bar-fill { background: var(--muted); opacity: 0.45; }
.bar-sub { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

/* Thẻ nhận xét AI */
.insight { margin-top: 12px; }
.insight-head { display: flex; gap: 14px; align-items: center; }
.insight-summary { font-weight: 600; }
.score { flex: none; width: 76px; height: 76px; border-radius: 50%; display: grid; place-content: center; text-align: center; border: 6px solid; }
.score b { font-size: 1.5rem; line-height: 1; }
.score span { font-size: 0.7rem; color: var(--muted); }
.score.red { border-color: var(--red-line); }
.score.yellow { border-color: var(--yellow-line); }
.score.green { border-color: var(--green-line); }
.point { border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; border-left: 5px solid var(--border); background: var(--card); }
.point-title { font-weight: 700; margin-bottom: 2px; }
.point.red { background: var(--red-bg); border-left-color: var(--red-line); }
.point.red .point-title { color: var(--danger); }
.point.green { background: var(--green-bg); border-left-color: var(--green-line); }
.point.green .point-title { color: var(--ok); }
.point.yellow { background: var(--yellow-bg); border-left-color: var(--yellow-line); }
.point.yellow .point-title { color: var(--warn); }
.point.rec { border-left-color: var(--primary); }
.rec-head { margin-top: 16px; color: var(--text); }
.saving { font-size: 0.8rem; color: var(--ok); white-space: nowrap; }

/* Câu trả lời Hỏi AI có tô màu theo dòng */
.ai-answer .line { padding: 2px 8px; border-radius: 6px; }
.ai-answer .line.red { background: var(--red-bg); color: var(--danger); font-weight: 600; margin: 3px 0; }
.ai-answer .line.green { background: var(--green-bg); color: var(--ok); font-weight: 600; margin: 3px 0; }
.ai-answer .line.yellow { background: var(--yellow-bg); color: var(--warn); margin: 3px 0; }
.ai-answer .gap { height: 8px; }

/* Ngân sách */
.budget-row { display: grid; grid-template-columns: 40px 1fr 130px; gap: 10px; align-items: center; }
.icon-input { width: 56px; text-align: center; flex: none; }

/* Menu Khác */
.menu-row {
  font: inherit; width: 100%; background: none; border: 0; color: var(--text); cursor: pointer;
  display: grid; grid-template-columns: 40px 1fr auto; gap: 12px; align-items: center; padding: 12px 14px; text-align: left;
}
.menu-row + .menu-row { border-top: 1px solid var(--border); }
.m-ico { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 1.2rem; background: color-mix(in srgb, var(--c) 16%, var(--card)); }
.chev { color: var(--muted); font-size: 1.3rem; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 10px; }
.chip { font: inherit; font-size: 0.85rem; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 999px; padding: 5px 10px; cursor: pointer; }
.answer { white-space: pre-wrap; }

/* Thanh tab dưới, nút + nổi ở giữa */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -2px 10px rgb(16 24 40 / 0.06);
}
.tabbar button {
  font: inherit; background: none; border: 0; color: var(--muted);
  padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.tabbar .t-ico { font-size: 1.25rem; line-height: 1.2; filter: grayscale(1); opacity: 0.7; }
.tabbar button span:last-child { font-size: 0.7rem; }
.tabbar button.active { color: var(--primary); font-weight: 600; }
.tabbar button.active .t-ico { filter: none; opacity: 1; }
.fab {
  width: 54px; height: 54px; border-radius: 50%; margin-top: -26px;
  display: grid; place-items: center; font-size: 1.8rem; font-weight: 300; color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 4px 12px rgb(21 101 192 / 0.4); border: 4px solid var(--card);
}

.overlay { position: fixed; inset: 0; background: linear-gradient(160deg, var(--primary), var(--primary-2)); z-index: 20; display: grid; place-items: center; padding: 16px; }
.overlay[hidden] { display: none; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 10px; text-align: center; padding: 24px; }
.login-logo { font-size: 2.6rem; }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--card); color: var(--text); width: min(560px, calc(100% - 32px)); padding: 16px; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }

.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 30;
  max-width: calc(100% - 32px); white-space: pre-line;
}
