:root {
  --bg: #0b1020;
  --surface: #161d2f;
  --surface-2: #212a40;
  --border: rgba(255, 255, 255, 0.08);
  --primary: #6366f1;
  --primary-press: #4f46e5;
  --text: #f8fafc;
  --muted: #9aa6bd;
  --danger: #f87171;
  --green: #34d399;
  --amber: #fbbf24;
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 64px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* CRITICAL: make the hidden attribute always win over display:flex/grid. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

body {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 12px);
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(var(--bg), var(--bg) 70%, transparent);
  padding: max(env(safe-area-inset-top), 14px) 18px 12px;
}
.topbar h1 { margin: 0 0 10px; font-size: 1.6rem; font-weight: 800; letter-spacing: -0.5px; }

.period-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}
.period-nav span {
  font-size: 0.95rem; color: var(--muted); flex: 1; text-align: center; font-weight: 600;
}
.nav-btn {
  width: 44px; height: 44px; flex: none;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface);
  color: var(--text); font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.nav-btn:active { background: var(--surface-2); }

main { padding: 4px 18px 18px; }

/* ---------- Total card ---------- */
.total-card {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  border-radius: var(--radius);
  padding: 20px;
  display: flex; flex-direction: column; gap: 4px;
  margin-bottom: 18px;
  box-shadow: 0 10px 30px rgba(99, 102, 241, 0.25);
}
.total-label { font-size: 0.85rem; opacity: 0.9; font-weight: 600; }
.total-amount { font-size: 2.7rem; font-weight: 800; letter-spacing: -1.5px; line-height: 1.1; }
.total-sub { font-size: 0.8rem; opacity: 0.85; }

/* ---------- Section heading ---------- */
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px; font-weight: 700; font-size: 1.05rem;
}
.link-btn {
  border: none; background: rgba(99,102,241,0.15); color: #a5b4fc;
  font-size: 0.85rem; font-weight: 600; cursor: pointer; padding: 8px 14px; border-radius: 999px;
}

/* ---------- Insights ---------- */
.insights { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 18px; }
.insight {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px; display: flex; flex-direction: column; gap: 3px; min-width: 0;
}
.insight .ins-label { color: var(--muted); font-size: 0.72rem; font-weight: 600; }
.insight .ins-value { font-size: 1.2rem; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.insight .ins-sub { font-size: 0.72rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ins-up { color: var(--danger); }
.ins-down { color: var(--green); }

/* ---------- Budgets ---------- */
.budget-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 18px;
}
.budget-row { margin-bottom: 14px; }
.budget-row:last-child { margin-bottom: 0; }
.budget-top { display: flex; justify-content: space-between; align-items: baseline; font-size: 0.88rem; margin-bottom: 7px; gap: 8px; }
.budget-cat { font-weight: 700; }
.budget-amt { color: var(--muted); font-size: 0.8rem; text-align: right; }
.budget-amt.over { color: var(--danger); font-weight: 600; }
.bar { height: 9px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--green); transition: width .3s; }
.bar-fill.warn { background: var(--amber); }
.bar-fill.over { background: var(--danger); }
.budget-empty { color: var(--muted); font-size: 0.88rem; text-align: center; padding: 8px 0; }

/* ---------- History filter + range ---------- */
.history-filter { display: flex; flex-direction: column; gap: 12px; margin-bottom: 14px; }
.history-range {
  display: flex; gap: 10px; align-items: flex-end; margin-bottom: 16px;
}
.history-range label { display: flex; flex-direction: column; gap: 6px; font-size: 0.72rem; color: var(--muted); flex: 1; }
.history-range input[type="date"] {
  height: 48px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 0 12px; font-size: 0.9rem;
}
.range-btn {
  height: 48px; padding: 0 20px; border: none; border-radius: 12px;
  background: var(--primary); color: #fff; font-size: 1rem; font-weight: 700; cursor: pointer;
}
.range-btn:active { background: var(--primary-press); }

/* ---------- Breakdown (week) ---------- */
.breakdown { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.bd-row {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 13px 15px;
}
.bd-day { display: flex; flex-direction: column; }
.bd-day span { font-weight: 700; }
.bd-day small { color: var(--muted); font-size: 0.74rem; }
.bd-amt { font-weight: 700; }
.bd-row.empty-day .bd-amt { color: var(--muted); font-weight: 500; }

/* ---------- Expense list ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.day-group-label {
  color: var(--muted); font-size: 0.76rem; font-weight: 700;
  margin: 10px 2px 2px; text-transform: uppercase; letter-spacing: 0.6px;
}
.item {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px;
}
.item-tap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; cursor: pointer; }
.item-cat-ico {
  flex: none; width: 42px; height: 42px; border-radius: 12px;
  background: var(--surface-2); display: flex; align-items: center; justify-content: center; font-size: 1.2rem;
}
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 700; font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-meta { color: var(--muted); font-size: 0.76rem; margin-top: 2px; }
.item-price { font-weight: 800; font-size: 1.1rem; white-space: nowrap; }
.del-btn {
  width: 44px; height: 44px; flex: none;
  border: none; border-radius: 11px;
  background: rgba(248, 113, 113, 0.13); color: var(--danger);
  font-size: 1.15rem; cursor: pointer;
}
.del-btn:active { background: rgba(248, 113, 113, 0.28); }
.empty { text-align: center; color: var(--muted); margin-top: 36px; font-size: 0.95rem; }

/* ---------- Chips ---------- */
.chips {
  display: flex; gap: 8px; overflow-x: auto;
  -webkit-overflow-scrolling: touch; padding-bottom: 2px; scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  height: 42px; padding: 0 15px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
  font-size: 0.92rem; white-space: nowrap; cursor: pointer;
}
.chip.selected { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }

/* ---------- Floating add button ---------- */
.fab {
  position: fixed; right: 18px;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  width: 60px; height: 60px; border-radius: 50%;
  border: none; background: var(--primary); color: #fff;
  font-size: 2rem; line-height: 1; font-weight: 300; cursor: pointer;
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.5);
  z-index: 30;
  display: flex; align-items: center; justify-content: center;
}
.fab:active { background: var(--primary-press); transform: scale(0.94); }

/* ---------- Bottom tab bar ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  max-width: 480px; margin: 0 auto;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom); z-index: 20;
}
.tab {
  flex: 1; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: none; background: none; color: var(--muted); font-size: 0.7rem; font-weight: 600; cursor: pointer;
}
.tab-ico { font-size: 1.3rem; line-height: 1; opacity: 0.6; }
.tab.active { color: #a5b4fc; }
.tab.active .tab-ico { opacity: 1; }

/* ---------- Modal / bottom sheet ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  width: 100%; max-width: 480px;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 22px 18px calc(22px + var(--safe-bottom));
  max-height: 90vh; overflow-y: auto;
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal h2 { margin: 0 0 4px; font-size: 1.3rem; }
.modal .hint { margin: 0 0 12px; font-size: 0.82rem; color: var(--muted); }
.modal label { display: block; font-size: 0.76rem; color: var(--muted); margin: 14px 0 7px; font-weight: 600; }

/* form fields */
.field {
  width: 100%; height: 52px; margin: 0;
  border: 1px solid var(--border); border-radius: 13px;
  background: var(--surface-2); color: var(--text);
  font-size: 1.05rem; padding: 0 15px;
}
.field:focus { outline: 2px solid var(--primary); border-color: transparent; }
.price-wrap { position: relative; }
.rupee { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 1.05rem; pointer-events: none; }
.price-field { padding-left: 32px; }

.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.btn-primary {
  flex: 1; height: 52px; border: none; border-radius: 13px;
  background: var(--primary); color: #fff; font-size: 1.05rem; font-weight: 700; cursor: pointer;
}
.btn-primary:active { background: var(--primary-press); }
.btn-ghost {
  height: 52px; min-width: 92px; border: 1px solid var(--border);
  border-radius: 13px; background: transparent; color: var(--text); font-size: 1.05rem; font-weight: 600; cursor: pointer;
}
.btn-danger {
  height: 52px; border: none; border-radius: 13px;
  background: rgba(248,113,113,0.15); color: var(--danger); font-size: 1.05rem; font-weight: 600; cursor: pointer; padding: 0 18px;
}
.budget-input-row { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.budget-input-row .b-cat { flex: 1; font-size: 0.98rem; font-weight: 600; }
.budget-input-row .price-wrap { flex: 0 0 130px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 90px);
  background: #fff; color: #0b1020;
  padding: 11px 20px; border-radius: 999px; font-size: 0.92rem; font-weight: 700;
  z-index: 120; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
