/* ============================ Design tokens ============================ */
:root {
  --bg: #f4f5fb;
  --bg-soft: #eceefb;
  --surface: #ffffff;
  --surface-2: #f7f8fd;
  --border: #e6e8f0;
  --text: #171a2b;
  --text-dim: #626784;
  --text-faint: #9aa0be;
  --primary: #5b5bef;
  --primary-2: #7c5cfc;
  --primary-soft: #ecebff;
  --green: #16a34a;
  --green-soft: #e7f7ee;
  --red: #e11d48;
  --red-soft: #fdecef;
  --amber: #d97706;
  --amber-soft: #fdf1e0;
  --shadow: 0 1px 2px rgba(20, 22, 45, .04), 0 8px 28px rgba(20, 22, 45, .06);
  --shadow-lg: 0 24px 60px rgba(20, 22, 45, .18);
  --radius: 18px;
  --radius-sm: 12px;
  --radius-xs: 9px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --nav-w: 236px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

html[data-theme="dark"] {
  --bg: #0b0f1a;
  --bg-soft: #0f1424;
  --surface: #141a2b;
  --surface-2: #1a2135;
  --border: #253049;
  --text: #eef1fb;
  --text-dim: #9aa4c2;
  --text-faint: #6b7699;
  --primary: #7c7cff;
  --primary-2: #9a7bff;
  --primary-soft: #1e2140;
  --green: #34d399;
  --green-soft: #10261f;
  --red: #fb7185;
  --red-soft: #2a1520;
  --amber: #fbbf24;
  --amber-soft: #2a2110;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 12px 30px rgba(0,0,0,.35);
  --shadow-lg: 0 30px 70px rgba(0,0,0,.55);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 20px; }
::selection { background: var(--primary-soft); }

/* ============================== Splash ============================== */
.splash {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 22px; background: var(--bg); z-index: 100;
}
.splash-logo {
  width: 74px; height: 74px; border-radius: 22px; display: grid; place-items: center;
  font-size: 40px; font-weight: 800; color: #fff;
  background: linear-gradient(140deg, var(--primary), var(--primary-2));
  box-shadow: var(--shadow-lg);
}
.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--primary); animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =============================== Auth =============================== */
.auth {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 22px;
  background:
    radial-gradient(900px 500px at 15% -10%, var(--primary-soft), transparent 60%),
    radial-gradient(800px 500px at 110% 20%, rgba(124,92,252,.14), transparent 55%),
    var(--bg);
  overflow: auto;
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 24px; padding: 34px 30px; box-shadow: var(--shadow-lg);
}
.auth-brand { font-size: 22px; font-weight: 800; display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 11px; display: inline-grid; place-items: center;
  color: #fff; font-weight: 800; background: linear-gradient(140deg, var(--primary), var(--primary-2));
}
.auth-tag { color: var(--text-dim); margin: 10px 0 22px; font-size: 14px; line-height: 1.5; }
.seg {
  display: flex; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 4px; margin-bottom: 20px;
}
.seg-btn {
  flex: 1; padding: 9px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--text-dim);
  transition: .15s;
}
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--text-dim); }
.field input, .field select, .field textarea {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-xs);
  padding: 12px 13px; outline: none; transition: .15s; width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-soft); background: var(--surface);
}
.auth-error { color: var(--red); font-size: 13px; background: var(--red-soft); padding: 10px 12px; border-radius: 10px; }
.auth-hint { text-align: center; color: var(--text-faint); font-size: 13px; margin-top: 16px; }

/* =============================== Buttons =============================== */
.btn {
  padding: 11px 16px; border-radius: var(--radius-xs); font-weight: 600; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: .15s;
  border: 1px solid transparent; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(140deg, var(--primary), var(--primary-2)); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--bg-soft); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 12px; font-size: 13px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 18px;
  color: var(--text-dim); transition: .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* =============================== Layout =============================== */
.app { display: grid; grid-template-columns: var(--nav-w) 1fr; height: 100dvh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 20px 14px;
}
.side-brand { display: flex; align-items: center; gap: 11px; font-weight: 800; font-size: 18px; padding: 6px 8px 20px; }
.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 12px;
  color: var(--text-dim); font-weight: 600; font-size: 14.5px; transition: .15s;
}
.nav-item .ico { font-size: 19px; width: 22px; text-align: center; }
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); }
.side-foot { display: flex; gap: 4px; padding-top: 12px; border-top: 1px solid var(--border); }

.main { display: flex; flex-direction: column; min-width: 0; height: 100dvh; overflow: hidden; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; padding: 16px 26px;
  border-bottom: 1px solid var(--border); background: var(--bg); flex-shrink: 0;
}
.month-picker {
  display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 11px; padding: 9px 14px; font-weight: 700; box-shadow: var(--shadow); font-size: 14.5px;
}
.month-picker .caret { color: var(--text-faint); font-size: 11px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 4px var(--green-soft); }
.sync-dot.syncing { background: var(--amber); box-shadow: 0 0 0 4px var(--amber-soft); animation: pulse 1s infinite; }
.sync-dot.offline { background: var(--red); box-shadow: 0 0 0 4px var(--red-soft); }
@keyframes pulse { 50% { opacity: .4; } }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; color: #fff; font-weight: 700; font-size: 15px;
  background: linear-gradient(140deg, var(--primary), var(--primary-2)); display: grid; place-items: center;
}
.view { flex: 1; overflow-y: auto; padding: 26px; scroll-behavior: smooth; }
.mobile-only { display: none; }

/* =============================== Bottom nav =============================== */
.tabbar { display: none; }
.fab {
  position: fixed; right: 24px; bottom: 24px; width: 58px; height: 58px; border-radius: 20px;
  font-size: 28px; color: #fff; background: linear-gradient(140deg, var(--primary), var(--primary-2));
  box-shadow: var(--shadow-lg); z-index: 40; display: grid; place-items: center; line-height: 1;
}
.fab:hover { filter: brightness(1.07); }

/* =============================== View shared =============================== */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-title { font-size: 24px; font-weight: 800; letter-spacing: -.4px; }
.page-sub { color: var(--text-dim); font-size: 14px; margin-top: 3px; }
.grid { display: grid; gap: 16px; }
.cards-4 { grid-template-columns: repeat(4, 1fr); }
.cards-3 { grid-template-columns: repeat(3, 1fr); }
.cards-2 { grid-template-columns: repeat(2, 1fr); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow);
}
.card-pad-lg { padding: 24px; }
.card-h { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 10px; }
.card-h h3 { font-size: 15.5px; font-weight: 700; }
.card-h .link { color: var(--primary); font-size: 13px; font-weight: 600; }

/* Stat card */
.stat .stat-top { display: flex; align-items: center; gap: 10px; color: var(--text-dim); font-size: 13px; font-weight: 600; margin-bottom: 12px; }
.stat .stat-ico { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; font-size: 17px; }
.stat .stat-val { font-size: 26px; font-weight: 800; letter-spacing: -.5px; }
.stat .stat-meta { font-size: 12.5px; color: var(--text-faint); margin-top: 6px; display: flex; align-items: center; gap: 6px; }
.delta.up { color: var(--red); } .delta.down { color: var(--green); }
.tint-primary { background: var(--primary-soft); color: var(--primary); }
.tint-green { background: var(--green-soft); color: var(--green); }
.tint-red { background: var(--red-soft); color: var(--red); }
.tint-amber { background: var(--amber-soft); color: var(--amber); }

/* Lists / rows */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 13px; padding: 13px 4px; border-bottom: 1px solid var(--border);
}
.row:last-child { border-bottom: none; }
.row-ico {
  width: 42px; height: 42px; border-radius: 13px; display: grid; place-items: center; font-size: 19px; flex-shrink: 0;
  background: var(--surface-2);
}
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { color: var(--text-faint); font-size: 12.5px; margin-top: 2px; }
.row-amt { font-weight: 700; font-size: 15px; text-align: right; white-space: nowrap; }
.amt-neg { color: var(--red); } .amt-pos { color: var(--green); }
.row-actions { display: flex; gap: 4px; opacity: 0; transition: .15s; }
.row:hover .row-actions { opacity: 1; }
.mini-btn { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; color: var(--text-faint); font-size: 14px; }
.mini-btn:hover { background: var(--surface-2); color: var(--text); }

/* progress bars */
.bar { height: 8px; border-radius: 20px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 20px; background: linear-gradient(90deg, var(--primary), var(--primary-2)); transition: width .5s; }
.bar.warn > i { background: linear-gradient(90deg, var(--amber), #f59e0b); }
.bar.over > i { background: linear-gradient(90deg, var(--red), #f43f5e); }

/* pills / tags */
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--text-dim); }
.pill.green { background: var(--green-soft); color: var(--green); }
.pill.red { background: var(--red-soft); color: var(--red); }
.pill.amber { background: var(--amber-soft); color: var(--amber); }

/* empty state */
.empty { text-align: center; padding: 46px 20px; color: var(--text-faint); }
.empty .em-ico { font-size: 42px; margin-bottom: 12px; }
.empty h4 { color: var(--text); font-size: 16px; margin-bottom: 6px; }
.empty p { font-size: 13.5px; max-width: 320px; margin: 0 auto 16px; line-height: 1.5; }

/* filter bar */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.search { flex: 1; min-width: 180px; position: relative; }
.search input { width: 100%; padding: 10px 12px 10px 36px; background: var(--surface); border: 1px solid var(--border); border-radius: 11px; outline: none; }
.search:before { content: "🔎"; position: absolute; left: 11px; top: 50%; transform: translateY(-50%); font-size: 14px; opacity: .6; }
.select-wrap select { padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 11px; outline: none; font-weight: 600; }

.chart-wrap { position: relative; height: 280px; }
.chart-wrap.sm { height: 220px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-top: 14px; }
.legend .li { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-dim); }
.legend .dot { width: 10px; height: 10px; border-radius: 3px; }

/* =============================== Modal =============================== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10, 12, 24, .55); backdrop-filter: blur(4px);
  z-index: 60; display: flex; align-items: flex-end; justify-content: center; padding: 0;
  animation: fade .18s ease;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; padding: 20px; } }
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface); width: 100%; max-width: 480px; border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow-lg); max-height: 92vh; overflow-y: auto; animation: slideup .22s cubic-bezier(.2,.8,.2,1);
  padding-bottom: var(--safe-b);
}
@media (min-width: 640px) { .modal { border-radius: 22px; } }
@keyframes slideup { from { transform: translateY(24px); opacity: .6; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 20px 22px 6px; }
.modal-head h3 { font-size: 18px; font-weight: 800; }
.modal-body { padding: 14px 22px 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; gap: 10px; margin-top: 4px; }
.modal-foot .btn { flex: 1; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* type toggle in modal */
.type-toggle { display: flex; gap: 8px; }
.type-toggle button { flex: 1; padding: 11px; border-radius: 11px; border: 1px solid var(--border); background: var(--surface-2); font-weight: 700; color: var(--text-dim); }
.type-toggle button.active.exp { background: var(--red-soft); color: var(--red); border-color: transparent; }
.type-toggle button.active.inc { background: var(--green-soft); color: var(--green); border-color: transparent; }

/* category chips picker */
.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; max-height: 220px; overflow-y: auto; padding: 2px; }
.cat-chip { display: flex; flex-direction: column; align-items: center; gap: 5px; padding: 12px 6px; border-radius: 13px; border: 1px solid var(--border); background: var(--surface-2); font-size: 11.5px; font-weight: 600; text-align: center; transition: .12s; }
.cat-chip .ce { font-size: 21px; }
.cat-chip.active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.cat-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }

/* amount input big */
.amount-big { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.amount-big .cur { font-size: 26px; font-weight: 800; color: var(--text-faint); }
.amount-big input { border: none; background: none; outline: none; font-size: 30px; font-weight: 800; width: 100%; letter-spacing: -.5px; }

/* =============================== Toast =============================== */
.toast-root { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 90; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 12px; font-size: 13.5px; font-weight: 600; box-shadow: var(--shadow-lg); animation: toastin .2s ease; }
.toast.err { background: var(--red); color: #fff; }
.toast.ok { background: var(--green); color: #fff; }
@keyframes toastin { from { transform: translateY(10px); opacity: 0; } }

/* menu (dropdown) */
.menu { position: fixed; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 6px; z-index: 70; min-width: 190px; }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 12px; border-radius: 9px; font-size: 14px; font-weight: 500; color: var(--text); text-align: left; }
.menu button:hover { background: var(--surface-2); }
.menu .sep { height: 1px; background: var(--border); margin: 5px 0; }
.menu .muted { color: var(--text-faint); font-size: 12px; padding: 8px 12px 4px; }

/* section header */
.section-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: var(--text-faint); margin: 22px 4px 10px; }
.date-group { margin-bottom: 4px; }

/* ============================== Responsive ============================== */
@media (max-width: 860px) {
  body { overflow: auto; }
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .main { height: 100dvh; }
  .view { padding: 18px 16px 96px; }
  .topbar { padding: 14px 16px; position: sticky; top: 0; z-index: 30; }
  .mobile-only { display: grid; }
  .cards-4 { grid-template-columns: 1fr 1fr; }
  .cards-3 { grid-template-columns: 1fr; }
  .cards-2 { grid-template-columns: 1fr; }
  .page-title { font-size: 21px; }

  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 45;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 8px 6px calc(8px + var(--safe-b)); justify-content: space-around;
  }
  .tab {
    display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 4px 10px; border-radius: 12px;
    color: var(--text-faint); font-size: 10.5px; font-weight: 600; flex: 1;
  }
  .tab .ico { font-size: 21px; }
  .tab.active { color: var(--primary); }
  .fab { bottom: calc(76px + var(--safe-b)); right: 16px; width: 54px; height: 54px; }
}
@media (max-width: 420px) {
  .cards-4 { grid-template-columns: 1fr 1fr; }
  .row2 { grid-template-columns: 1fr; }
}
