:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --line: #e4e8f0;
  --primary: #2f6fed;
  --primary-soft: #e8f0ff;
  --income: #12a150;
  --expense: #e2453c;
  --warn: #d98a00;
  --warn-soft: #fff6e0;
  --danger-soft: #fdecea;
  --ok-soft: #e7f7ee;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 4px 16px rgba(20, 30, 60, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131b;
    --card: #181e29;
    --text: #e6eaf2;
    --muted: #8d97ab;
    --line: #273042;
    --primary: #5b8cff;
    --primary-soft: #1d2a47;
    --income: #34c477;
    --expense: #ff6b61;
    --warn: #f0b33c;
    --warn-soft: #33290f;
    --danger-soft: #3a1a19;
    --ok-soft: #12301f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--expense); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }
.income { color: var(--income); }
.expense { color: var(--expense); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
h2, h3, h4 { margin: 0; }

/* ---------- Nút & form ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--expense); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted);
  font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 8px;
}
.icon-btn:hover { background: var(--primary-soft); color: var(--primary); }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 11px;
  width: 100%;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input[type=color] { padding: 2px; height: 40px; }
input[type=checkbox] { width: auto; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.seg { display: flex; gap: 6px; background: var(--bg); padding: 4px; border-radius: 12px; }
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  padding: 8px; border-radius: 9px; font: inherit; font-weight: 600; cursor: pointer;
}
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.seg button.on[data-v=expense] { color: var(--expense); }
.seg button.on[data-v=income] { color: var(--income); }

/* ---------- Đăng nhập ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--card); padding: 32px 28px; border-radius: 20px; box-shadow: var(--shadow);
  width: 100%; max-width: 360px; text-align: center;
}
.login-card .logo { font-size: 48px; }
.login-card h1 { font-size: 22px; margin: 8px 0 4px; }
.login-card input { margin: 16px 0 12px; }

/* ---------- Bố cục ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; flex-shrink: 0; background: var(--card); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 800; font-size: 19px; padding: 4px 10px 18px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar a {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 10px;
  color: var(--text); text-decoration: none; cursor: pointer; font-weight: 500;
}
.sidebar a:hover { background: var(--bg); }
.sidebar a.active { background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; }
.side-foot a { color: var(--muted); font-size: 14px; }

.main { flex: 1; min-width: 0; padding: 20px 28px 60px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar h2 { font-size: 24px; }
.top-actions { display: flex; gap: 10px; align-items: center; }
.month-pick {
  display: flex; align-items: center; background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 2px;
}
.month-pick input { border: 0; padding: 6px 4px; width: 150px; background: transparent; }
.month-pick input:focus { outline: none; }

.banner { padding: 10px 14px; border-radius: 12px; margin-bottom: 14px; font-size: 14px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.danger { background: var(--danger-soft); color: var(--expense); }

/* ---------- Card & lưới ---------- */
.grid { display: grid; gap: 16px; }
.grid.kpi { grid-template-columns: repeat(4, 1fr); }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.cards { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.card-head h3 { font-size: 16px; }
.kpi .card .label { color: var(--muted); font-size: 13px; font-weight: 600; }
.kpi .card .value { font-size: 24px; font-weight: 800; margin-top: 4px; }
.kpi .card .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.section { margin-top: 16px; }
.chart-box { position: relative; height: 260px; }
.empty { text-align: center; color: var(--muted); padding: 28px 10px; }

/* ---------- Thanh tiến độ ---------- */
.progress { height: 10px; background: var(--bg); border-radius: 99px; overflow: hidden; margin: 8px 0 6px; }
.progress > i { display: block; height: 100%; border-radius: 99px; background: var(--income); transition: width .3s; }
.progress.warn > i { background: var(--warn); }
.progress.over > i { background: var(--expense); }
.pill { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--ok-soft); color: var(--income); white-space: nowrap; }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.over { background: var(--danger-soft); color: var(--expense); }
.pill.off { background: var(--bg); color: var(--muted); }

/* ---------- Danh sách giao dịch ---------- */
.filters { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.filters label { min-width: 0; }
.filters label > span { font-size: 12px; color: var(--muted); font-weight: 600; display: block; margin-bottom: 3px; }
.filter-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 12px; }
.filter-actions .spacer { flex: 1; }
.summary-line { display: flex; gap: 18px; flex-wrap: wrap; font-size: 14px; }

.tx-list { list-style: none; margin: 0; padding: 0; }
.tx-date { font-size: 13px; font-weight: 700; color: var(--muted); padding: 14px 4px 6px; display: flex; justify-content: space-between; }
.tx {
  display: flex; align-items: center; gap: 12px; padding: 10px 6px; border-bottom: 1px solid var(--line);
}
.tx:last-child { border-bottom: 0; }
.tx .ic {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; font-size: 19px; flex-shrink: 0;
}
.tx .info { flex: 1; min-width: 0; }
.tx .info b { display: block; font-weight: 600; }
.tx .info small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx .amt { font-weight: 700; text-align: right; }
.tx .acts { display: flex; gap: 2px; }
.tx .acts .icon-btn { font-size: 15px; }

/* ---------- Danh mục, ví, mục tiêu ---------- */
.chip-list { display: flex; flex-direction: column; gap: 6px; }
.chip {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
}
.chip .ic { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; font-size: 17px; }
.chip .name { flex: 1; font-weight: 600; }
.badge { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }
.wallet-card { border-top: 4px solid var(--c, var(--primary)); }
.wallet-card .bal { font-size: 22px; font-weight: 800; margin: 6px 0 2px; }
.card-acts { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.legend-list { list-style: none; padding: 0; margin: 10px 0 0; font-size: 13px; }
.legend-list li { display: flex; align-items: center; gap: 8px; padding: 3px 0; }
.legend-list .dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.legend-list .nm { flex: 1; }

/* ---------- Modal & toast ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(10, 15, 25, .45); display: grid; place-items: center;
  padding: 16px; z-index: 50;
}
.modal-card {
  background: var(--card); width: 100%; max-width: 460px; border-radius: 18px; box-shadow: var(--shadow);
  max-height: calc(100vh - 32px); display: flex; flex-direction: column;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px 6px; }
.modal-body { padding: 10px 18px; overflow: auto; }
.modal-card > .err { padding: 0 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 18px 18px; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: calc(100vw - 32px); }
.toast {
  background: var(--card); border-left: 4px solid var(--income); box-shadow: 0 6px 24px rgba(0,0,0,.15);
  padding: 11px 14px; border-radius: 10px; font-size: 14px; max-width: 380px; animation: tin .2s ease;
}
.toast.warn { border-color: var(--warn); }
.toast.error { border-color: var(--expense); }
@keyframes tin { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .grid.kpi { grid-template-columns: repeat(2, 1fr); }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar {
    position: fixed; bottom: 0; left: 0; right: 0; top: auto; height: auto; width: 100%;
    flex-direction: row; padding: 4px; border-right: 0; border-top: 1px solid var(--line); z-index: 40;
  }
  .brand, .side-foot { display: none; }
  .sidebar nav { flex-direction: row; overflow-x: auto; gap: 0; }
  .sidebar a { flex-direction: column; gap: 0; padding: 6px 8px; font-size: 11px; min-width: 64px; text-align: center; }
  .main { padding: 14px 16px 90px; }
  .topbar h2 { font-size: 20px; }
  .grid.two { grid-template-columns: 1fr; }
  .btn#btnAdd span { display: none; }
  #mobileMore { display: flex !important; }
}
@media (max-width: 520px) {
  .grid.kpi { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi .card { padding: 12px; }
  .kpi .card .value { font-size: 18px; }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .row2 { grid-template-columns: 1fr; }
  .month-pick input { width: 130px; }
  .tx .acts { flex-direction: column; }
}
