* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --radius: 14px;
  --gap: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[data-theme="dark"] {
  --bg: #0f1216; --bg-soft: #151a21; --card: #1a212c; --card-2: #222b38;
  --text: #e8ecf1; --muted: #93a1b4; --border: #2b3546;
  --accent: #4f8cff; --accent-soft: rgba(79,140,255,.15); --green: #38d39f; --danger: #ff6b6b; --warn: #ffb454;
  --shadow: 0 10px 28px rgba(0,0,0,.4);
  --input: #121822; --hover: rgba(255,255,255,.05);
}
[data-theme="light"] {
  --bg: #f2f5f9; --bg-soft: #e9eef5; --card: #ffffff; --card-2: #f5f8fc;
  --text: #1d2733; --muted: #67768b; --border: #e1e8f0;
  --accent: #2f6df6; --accent-soft: rgba(47,109,246,.12); --green: #0f9d6e; --danger: #e5484d; --warn: #d98a06;
  --shadow: 0 8px 22px rgba(25,40,70,.09);
  --input: #eef2f7; --hover: rgba(20,40,80,.05);
}

html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}
button { font: inherit; color: inherit; cursor: pointer; background: none; border: none; }
input, select { font: inherit; color: var(--text); }

#app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 232px; flex-shrink: 0;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  padding: 18px 14px;
  display: flex; flex-direction: column; gap: 14px;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-size: 18px; font-weight: 700; padding: 4px 8px; }
.balance-card {
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  border-radius: var(--radius);
  padding: 14px;
  color: #fff;
}
.balance-card .bal-label { font-size: 12px; opacity: .85; }
.balance-card .bal-value { font-size: 24px; font-weight: 700; margin-top: 2px; }
.balance-card .bal-sub { font-size: 12px; opacity: .85; margin-top: 6px; }
#nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
#nav button {
  text-align: left; padding: 10px 12px; border-radius: 10px;
  color: var(--muted); font-weight: 600; font-size: 14px;
  transition: background .15s, color .15s;
}
#nav button:hover { background: var(--hover); color: var(--text); }
#nav button.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-footer { border-top: 1px solid var(--border); padding-top: 12px; }

/* ---------- Main ---------- */
main { flex: 1; padding: 20px 26px 80px; max-width: 1180px; width: 100%; margin: 0 auto; }
#topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-nav.hidden { visibility: hidden; }
.month-label { font-size: 18px; font-weight: 700; min-width: 150px; text-align: center; }

/* ---------- Buttons ---------- */
.btn {
  padding: 9px 16px; border-radius: 10px; font-weight: 600; font-size: 14px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  transition: transform .06s, background .15s, border-color .15s;
}
.btn:hover { background: var(--card-2); }
.btn:active { transform: scale(.97); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent:hover { filter: brightness(1.08); background: var(--accent); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--hover); color: var(--text); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: rgba(229,72,77,.1); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

#fab {
  position: fixed; right: 26px; bottom: 26px; z-index: 40;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 28px; line-height: 1;
  box-shadow: var(--shadow);
  transition: transform .12s;
}
#fab:hover { transform: scale(1.08); }

/* ---------- Tabs & panels ---------- */
.tab { display: none; }
.tab.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.panel { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.panel + .panel { margin-top: var(--gap); }
/* Grid/flex containers already space children via gap */
.grid-2 > .panel + .panel,
.stack > .panel + .panel { margin-top: 0; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 8px; flex-wrap: wrap; }
.panel-title { font-size: 15px; font-weight: 700; }
.panel-sub { font-size: 12.5px; color: var(--muted); }

.cards-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--gap); margin-bottom: var(--gap); }
.grid-2 { display: grid; grid-template-columns: 1.15fr 1fr; gap: var(--gap); margin-bottom: var(--gap); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--gap); }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
/* Prevent wide content (e.g. the trend chart) from blowing out grid/flex tracks */
.grid-2 > *, .grid-3 > *, .cards-grid > *, .stack > * { min-width: 0; }

.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.stat-label { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.stat-value { font-size: 24px; font-weight: 700; margin-top: 6px; }
.stat-delta { font-size: 12.5px; margin-top: 6px; color: var(--muted); }
.stat-delta .up { color: var(--green); font-weight: 700; }
.stat-delta .down { color: var(--danger); font-weight: 700; }
.pos { color: var(--green); } .neg { color: var(--danger); }

/* ---------- Progress bars ---------- */
.progress { height: 8px; background: var(--bg-soft); border-radius: 99px; overflow: hidden; margin: 8px 0 6px; }
.progress .fill { height: 100%; border-radius: 99px; background: var(--green); transition: width .3s ease; }
.progress .fill.warn { background: var(--warn); }
.progress .fill.over { background: var(--danger); }
.budget-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: none; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-variant-numeric: tabular-nums; }

.alert {
  background: rgba(229,72,77,.1); border: 1px solid var(--danger);
  color: var(--danger); border-radius: 10px; padding: 10px 14px;
  margin-bottom: var(--gap); font-size: 14px; font-weight: 600;
}

/* ---------- Donut & legend ---------- */
.donut-wrap { position: relative; width: 190px; margin: 0 auto; }
.donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.donut-center .dc-value { font-size: 20px; font-weight: 700; }
.donut-center .dc-label { font-size: 12px; color: var(--muted); }
.legend { list-style: none; margin-top: 14px; display: flex; flex-direction: column; gap: 7px; }
.legend li { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.legend .dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.legend .lg-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend .lg-pct { color: var(--muted); font-size: 12px; width: 38px; text-align: right; }

/* ---------- Transactions ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.filters input[type="search"], .filters select { flex: 1; min-width: 130px; }
.tx-day-header {
  display: flex; justify-content: space-between; padding: 12px 4px 6px;
  font-size: 12.5px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .4px;
}
.tx-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 8px; border-radius: 10px;
}
.tx-row:hover { background: var(--hover); }
.tx-icon {
  width: 38px; height: 38px; border-radius: 12px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.tx-main { flex: 1; min-width: 0; }
.tx-cat { font-weight: 600; font-size: 14.5px; }
.tx-note { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tx-amt.income { color: var(--green); }
.tx-amt.expense { color: var(--danger); }
.tx-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .15s; }
.tx-row:hover .tx-actions, .tx-row:focus-within .tx-actions { opacity: 1; }
.tx-actions button { padding: 6px 8px; border-radius: 8px; font-size: 14px; }
.tx-actions button:hover { background: var(--hover); }
/* Touch devices have no hover — always show the actions */
@media (hover: none) {
  .tx-actions { opacity: 1; }
}
.tx-footer { padding: 14px 4px 0; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }

/* ---------- Forms / modal ---------- */
#modal-root:empty { display: none; }
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(8,10,14,.55); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
  animation: fade .15s ease;
}
.modal {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto;
  padding: 22px; box-shadow: var(--shadow);
}
.modal-title { font-size: 17px; font-weight: 700; margin-bottom: 16px; }
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
input[type="text"], input[type="number"], input[type="date"], input[type="search"], input[type="email"], input[type="password"], select {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--input);
  outline: none; transition: border-color .15s;
}
input:focus, select:focus { border-color: var(--accent); }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.check-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }

.seg { display: flex; background: var(--input); border-radius: 10px; padding: 4px; gap: 4px; }
.seg button { flex: 1; padding: 8px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.seg button.active { background: var(--accent); color: #fff; }
.seg button.active.expense { background: var(--danger); color: #fff; }
.seg button.active.income { background: var(--green); color: #fff; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 7px 12px; border-radius: 99px; font-size: 13.5px; font-weight: 600;
  border: 1px solid var(--border); background: var(--input); color: var(--text);
}
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.amount-input { font-size: 26px !important; font-weight: 700; text-align: center; padding: 14px !important; }

/* ---------- Goals ---------- */
.goal-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; display: flex; flex-direction: column; gap: 8px; }
.goal-head { display: flex; align-items: center; gap: 10px; }
.goal-icon { font-size: 26px; }
.goal-name { font-weight: 700; flex: 1; }
.goal-actions { display: flex; gap: 6px; margin-top: 4px; }

/* ---------- Settings lists ---------- */
.list-row {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 4px; border-bottom: 1px solid var(--border); font-size: 14px;
}
.list-row:last-child { border-bottom: none; }
.badge { font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); }
.data-buttons { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Auth screen ---------- */
#auth-screen {
  position: fixed; inset: 0; z-index: 60;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; overflow-y: auto;
  animation: fade .18s ease;
}
#auth-screen.hidden { display: none; }
.auth-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  width: 100%; max-width: 400px; padding: 26px; box-shadow: var(--shadow);
}
.auth-card .brand { padding: 0; margin-bottom: 6px; }
.auth-sub { font-size: 13.5px; margin-bottom: 16px; }
.auth-error {
  background: rgba(229,72,77,.1); border: 1px solid var(--danger); color: var(--danger);
  border-radius: 10px; padding: 8px 12px; font-size: 13.5px; margin-bottom: 12px;
}
.auth-info {
  background: var(--accent-soft); border: 1px solid var(--accent); color: var(--accent);
  border-radius: 10px; padding: 8px 12px; font-size: 13.5px; margin-bottom: 12px;
}
.auth-links { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.auth-code-input { text-align: center; font-size: 22px !important; font-weight: 700; letter-spacing: 6px; }
.auth-user { display: flex; align-items: center; gap: 4px; margin-bottom: 4px; }
.auth-email {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; color: var(--muted); padding-left: 8px;
}

/* ---------- Toasts ---------- */
#toast-root { position: fixed; top: 18px; right: 18px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 10px; padding: 12px 16px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 14px; font-size: 14px;
  animation: slidein .2s ease; max-width: 340px;
}
.toast button { color: var(--accent); font-weight: 700; }
@keyframes slidein { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* ---------- Misc ---------- */
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .empty-icon { font-size: 40px; margin-bottom: 10px; }
.empty p { margin-bottom: 14px; }
.kbd {
  font-family: ui-monospace, monospace; font-size: 12px;
  background: var(--input); border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 6px; padding: 1px 6px;
}
.inline-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline-form input, .inline-form select { width: auto; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 8px; }
.budget-input { width: 110px; text-align: right; }
.trend-wrap { overflow-x: auto; }

/* ---------- Responsive ---------- */
@media (max-width: 940px) {
  .cards-grid { grid-template-columns: repeat(2, 1fr); }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  #app { flex-direction: column; }
  #sidebar {
    width: 100%; height: auto; position: static;
    flex-direction: row; align-items: center; flex-wrap: wrap;
    padding: 10px 12px; gap: 8px;
  }
  .brand { font-size: 16px; flex: 1; }
  .balance-card { order: 4; width: 100%; padding: 10px 12px; display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
  .balance-card .bal-value { font-size: 18px; }
  .balance-card .bal-sub { margin-top: 0; }
  #nav { order: 3; flex-basis: 100%; flex-direction: row; overflow-x: auto; }
  #nav button { white-space: nowrap; padding: 8px 10px; }
  .sidebar-footer { border: none; padding: 0; display: flex; align-items: center; gap: 4px; }
  .sidebar-footer .btn { padding: 6px 8px; font-size: 13px; }
  .sidebar-footer .btn-block { width: auto; }
  .auth-user { min-width: 0; max-width: 45vw; margin-bottom: 0; }
  #toast-root { left: 12px; right: 12px; }
  .toast { max-width: none; }
  main { padding: 14px 14px 90px; }
  .month-label { min-width: 0; font-size: 16px; }
}
@media (max-width: 400px) {
  .stat-value { font-size: 19px; }
  .brand span { display: none; }
}
