:root {
  --bg: #f3f6f4;
  --bg-accent: #e7f2ec;
  --surface: #ffffff;
  --surface-2: #f8faf9;
  --text: #14201b;
  --muted: #5c6f66;
  --border: #d7e3dc;
  --primary: #1a9f6e;
  --primary-hover: #15855c;
  --primary-soft: rgba(26, 159, 110, 0.12);
  --danger: #d14343;
  --danger-soft: rgba(209, 67, 67, 0.1);
  --income: #1a9f6e;
  --expense: #d14343;
  --shadow: 0 1px 2px rgba(20, 32, 27, 0.04), 0 8px 24px rgba(20, 32, 27, 0.06);
  --radius: 16px;
  --radius-sm: 10px;
  --sidebar: 260px;
  --font: 'Manrope', system-ui, sans-serif;
  --ring: 0 0 0 3px rgba(26, 159, 110, 0.28);
}

[data-theme='dark'] {
  --bg: #0f1613;
  --bg-accent: #15201b;
  --surface: #18241e;
  --surface-2: #1e2d26;
  --text: #e8f0eb;
  --muted: #9ab3a8;
  --border: #2a3b33;
  --primary: #2ec98a;
  --primary-hover: #3dd998;
  --primary-soft: rgba(46, 201, 138, 0.14);
  --danger: #f07171;
  --danger-soft: rgba(240, 113, 113, 0.12);
  --income: #2ec98a;
  --expense: #f07171;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.25), 0 12px 32px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, var(--bg-accent), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(26, 159, 110, 0.08), transparent 50%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

h1, h2 {
  margin: 0;
  letter-spacing: -0.02em;
}

h1 { font-size: 1.65rem; font-weight: 800; }
h2 { font-size: 1.05rem; font-weight: 700; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.muted { color: var(--muted); }
.tiny { font-size: 0.8rem; }
.big-number {
  font-size: 1.75rem;
  font-weight: 800;
  margin: 0.35rem 0;
  letter-spacing: -0.03em;
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar) 1fr;
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 1.4rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(10px);
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.25rem 0.4rem;
}

.brand strong {
  display: block;
  font-size: 1.05rem;
  font-weight: 800;
}

.brand span {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  background: linear-gradient(145deg, var(--primary), #0d7a52);
  box-shadow: 0 8px 18px rgba(26, 159, 110, 0.35);
  position: relative;
}

.brand-mark::after {
  content: '';
  position: absolute;
  inset: 10px 11px;
  border-left: 2.5px solid #fff;
  border-bottom: 2.5px solid #fff;
  transform: rotate(-45deg) translate(1px, -1px);
  border-radius: 1px;
}

.brand-mark.lg {
  width: 52px;
  height: 52px;
  margin: 0 auto 0.75rem;
  display: block;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.nav-btn {
  text-align: left;
  border: 0;
  background: transparent;
  padding: 0.7rem 0.9rem;
  border-radius: 12px;
  color: var(--muted);
  font-weight: 600;
  transition: background 0.15s, color 0.15s;
}

.nav-btn:hover { background: var(--primary-soft); color: var(--text); }
.nav-btn.active {
  background: var(--primary-soft);
  color: var(--primary);
}

.main-wrap {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.9rem 1.4rem;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.month-nav {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem;
  box-shadow: var(--shadow);
}

.month-select-wrap select {
  border: 0;
  background: transparent;
  font-weight: 700;
  padding: 0.45rem 0.6rem;
  outline: none;
  max-width: 180px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  display: inline-grid;
  place-items: center;
  color: var(--text);
}

.icon-btn:hover { background: var(--surface-2); }

.content {
  padding: 1.25rem 1.4rem 2.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.view[hidden] { display: none !important; }

.page-head { margin-bottom: 0.25rem; }
.page-head.row {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem;
  flex-wrap: wrap;
}

.page-head p { margin: 0.3rem 0 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border-radius: 12px;
  border: 1px solid transparent;
  padding: 0.65rem 1rem;
  font-weight: 700;
  transition: transform 0.12s, background 0.15s, border-color 0.15s;
}

.btn:active { transform: translateY(1px); }
.btn-block { width: 100%; }
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
  background: var(--surface);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.9rem;
}
.btn-row.center { justify-content: center; }
.file-btn { cursor: pointer; }
.link-btn {
  border: 0;
  background: none;
  color: var(--primary);
  font-weight: 700;
  padding: 0;
}

.metrics {
  display: grid;
  grid-template-columns: 1.4fr repeat(3, 1fr);
  gap: 0.9rem;
}

.metric-card, .panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
}

.metric-card.hero {
  background:
    linear-gradient(145deg, color-mix(in srgb, var(--primary) 16%, var(--surface)), var(--surface));
}

.metric-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.metric-value {
  display: block;
  font-size: 1.55rem;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.metric-card.hero .metric-value { font-size: 1.9rem; }
.metric-value.positive { color: var(--income); }
.metric-value.negative { color: var(--expense); }
.metric-hint {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem;
}

.grid-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem;
}

.panel-wide { grid-column: 1 / -1; }
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.chart-box {
  height: 240px;
  position: relative;
  margin-top: 0.5rem;
}
.chart-box.tall { height: 300px; }

.goal-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin: 0.75rem 0 1rem;
}
.goal-stats span {
  display: block;
  font-size: 0.78rem;
  margin-bottom: 0.2rem;
}
.goal-stats strong { font-size: 1rem; }

.progress {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
}
.progress span {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--primary), #3dd998);
  border-radius: inherit;
  transition: width 0.35s ease;
}
.progress span.over {
  background: linear-gradient(90deg, #0d7a52, var(--primary));
}

.category-list {
  list-style: none;
  margin: 0.85rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.cat-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.35rem;
  font-weight: 600;
}
.cat-meta { color: var(--muted); font-weight: 600; font-size: 0.92rem; }
.cat-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}
.cat-bar span {
  display: block;
  height: 100%;
  background: var(--primary);
  border-radius: inherit;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: end;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-width: 140px;
}
.field.grow { flex: 1; min-width: 180px; }
.field.inline { min-width: 120px; }
.field span {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
}
.field em { font-style: normal; font-weight: 500; opacity: 0.8; }
.field input,
.field select,
.field textarea,
.inline-add input {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.75rem;
  outline: none;
}
.field input:focus,
.field select:focus,
.field textarea:focus,
.inline-add input:focus {
  border-color: var(--primary);
  box-shadow: var(--ring);
}

.table-wrap { overflow-x: auto; }
.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 640px;
}
.data-table th,
.data-table td {
  text-align: left;
  padding: 0.85rem 0.6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.data-table th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tbody tr:hover { background: var(--surface-2); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
}
.badge.income { background: var(--primary-soft); color: var(--income); }
.badge.expense { background: var(--danger-soft); color: var(--expense); }
.badge.off { background: var(--surface-2); color: var(--muted); }

.row-actions {
  display: flex;
  gap: 0.35rem;
  justify-content: flex-end;
}
.row-actions button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 0.35rem 0.55rem;
  font-size: 0.8rem;
  font-weight: 600;
}
.row-actions button:hover { background: var(--surface-2); }
.row-actions .danger { color: var(--danger); }

.cards-list { display: none; }
.tx-card, .rec-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem;
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}
.tx-card + .tx-card,
.rec-card + .rec-card { margin-top: 0.65rem; }
.tx-card-top, .rec-card-top {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: start;
}
.amount-income { color: var(--income); font-weight: 800; }
.amount-expense { color: var(--expense); font-weight: 800; }

.empty-hint {
  text-align: center;
  color: var(--muted);
  padding: 1.5rem 0.5rem;
  margin: 0;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem;
}
.login-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}
.login-gate[hidden] {
  display: none !important;
}
.login-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1.6rem 1.35rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  text-align: center;
}
.login-card h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
}
.login-card .field { text-align: left; }
.login-card .brand-mark.lg { margin-bottom: 0.25rem; }
.nav-logout { margin-top: auto; color: var(--danger); }
.nav-logout:hover { background: var(--danger-soft); color: var(--danger); }

body:not(.authed) .app { display: none !important; }

.demo-banner {
  border-color: color-mix(in srgb, #f59e0b 45%, var(--border));
  background:
    linear-gradient(145deg, color-mix(in srgb, #f59e0b 14%, var(--surface)), var(--surface));
}
.demo-banner h2 { margin-bottom: 0.25rem; }
.demo-banner .btn-row { margin-top: 0.85rem; }

.setup-panel {
  border-color: color-mix(in srgb, var(--primary) 35%, var(--border));
  background:
    linear-gradient(145deg, color-mix(in srgb, var(--primary) 10%, var(--surface)), var(--surface));
}
.setup-panel h2 { margin-bottom: 0.25rem; }
.setup-panel .btn-row { margin-top: 0.85rem; }
.panel-head .tiny { margin: 0.15rem 0 0; }

.modal {
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0;
  width: min(480px, calc(100vw - 1.5rem));
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.modal::backdrop {
  background: rgba(10, 16, 13, 0.45);
  backdrop-filter: blur(3px);
}
.modal-sm { width: min(380px, calc(100vw - 1.5rem)); }
.modal-head, .modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem;
}
.modal-foot {
  justify-content: flex-end;
  border-top: 1px solid var(--border);
}
.modal-head { border-bottom: 1px solid var(--border); }
.modal-body { padding: 1.1rem; display: flex; flex-direction: column; gap: 0.85rem; }
.modal form { margin: 0; }
.onboard { text-align: center; padding: 1.6rem 1.2rem 1.4rem; }
.onboard h2 { margin-bottom: 0.4rem; }
.onboard p { margin: 0 auto 1rem; max-width: 34ch; color: var(--muted); }

.type-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}
.type-toggle label {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.7rem;
  text-align: center;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface-2);
}
.type-toggle input { accent-color: var(--primary); }
.type-toggle label:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.grid-2-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.inline-add {
  display: flex;
  gap: 0.5rem;
}
.inline-add input { flex: 1; }

.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 0.88rem;
  font-weight: 600;
}

.toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 0.75rem 1.1rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
  z-index: 100;
  box-shadow: var(--shadow);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 25;
}

.mobile-only { display: none; }

@media (max-width: 1100px) {
  .metrics { grid-template-columns: 1fr 1fr; }
  .metric-card.hero { grid-column: 1 / -1; }
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(280px, 86vw);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-only { display: inline-grid; }
  .desktop-hide-sm { display: none; }
  .grid-2,
  .grid-charts,
  .settings-grid,
  .goal-stats { grid-template-columns: 1fr; }
  .content { padding: 1rem; }
}

@media (max-width: 700px) {
  .data-table { display: none; }
  .cards-list { display: block; }
  .month-select-wrap select { max-width: 150px; font-size: 0.92rem; }
  .metrics { grid-template-columns: 1fr; }
  .grid-2-fields { grid-template-columns: 1fr; }
  .page-head.row { align-items: stretch; }
  .page-head.row .btn { width: 100%; }
}
