/* ============================================================
   Design tokens
   ============================================================ */
:root {
  /* Surface colors */
  --bg: #FAF8F5;
  --surface: #FFFFFF;
  --surface-soft: #F5F4EF;

  /* Text */
  --text: #0F172A;
  --text-muted: #64748B;
  --text-faint: #94A3B8;

  /* Borders */
  --border: #ECEAE3;
  --border-strong: #D4D2C9;

  /* Brand & accents (sage mint green) */
  --primary: #7DB87A;
  --primary-deep: #4FA876;
  --primary-soft: #B8E0A1;
  --primary-tint: #E8F4DC;

  /* Semantic */
  --income: #10B981;
  --income-soft: #D1FAE5;
  --expense: #EF4444;
  --expense-soft: #FEE2E2;

  /* Category colors */
  --cat-orange: #FB923C;
  --cat-orange-bg: #FFEDD5;
  --cat-blue: #60A5FA;
  --cat-blue-bg: #DBEAFE;
  --cat-pink: #F472B6;
  --cat-pink-bg: #FCE7F3;
  --cat-purple: #A78BFA;
  --cat-purple-bg: #EDE9FE;
  --cat-slate: #94A3B8;
  --cat-slate-bg: #F1F5F9;
  --cat-green: #34D399;
  --cat-green-bg: #D1FAE5;
  --cat-red: #F87171;
  --cat-red-bg: #FEE2E2;
  --cat-stone: #A8A29E;
  --cat-stone-bg: #F5F5F4;
  --cat-amber: #FBBF24;
  --cat-amber-bg: #FEF3C7;
  --cat-cyan: #06B6D4;
  --cat-cyan-bg: #CFFAFE;
  --cat-lime: #84CC16;
  --cat-lime-bg: #ECFCCB;
  --cat-emerald: #10B981;
  --cat-emerald-bg: #D1FAE5;

  /* Shadow & radius */
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow: 0 4px 12px -2px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 12px 32px -4px rgba(15, 23, 42, 0.12);
  --shadow-fab: 0 6px 16px -2px rgba(77, 168, 118, 0.5);
  --radius: 16px;
  --radius-sm: 12px;
  --radius-lg: 24px;

  /* Nav */
  --nav-height: 76px;
  --nav-fab-size: 60px;
}

/* ============================================================
   Base
   ============================================================ */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* leave space for bottom nav */
  padding-bottom: calc(var(--nav-height) + 16px);
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 20px 0;
}

button { font: inherit; color: inherit; }
input, select, button { font-family: inherit; }

/* ============================================================
   Header
   ============================================================ */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0 20px;
}

.user {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #B8E0A1 0%, #4FA876 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 20px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(79, 168, 118, 0.2);
}

.user-text { line-height: 1.2; }
.greeting { font-size: 12px; color: var(--text-muted); }
.user-name { font-size: 16px; font-weight: 600; margin-top: 2px; }

.header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text);
  position: relative;
  transition: background 0.15s, transform 0.15s;
}

.icon-btn:hover { background: var(--surface-soft); }
.icon-btn:active { transform: scale(0.95); }

.icon-btn svg { width: 20px; height: 20px; }

.notif-dot {
  position: absolute;
  top: 11px;
  right: 12px;
  width: 8px;
  height: 8px;
  background: var(--expense);
  border-radius: 50%;
  border: 2px solid var(--surface);
}

/* ============================================================
   Hero card
   ============================================================ */
.hero-card {
  position: relative;
  background: linear-gradient(135deg, #B8E0A1 0%, #95CC83 100%);
  border-radius: var(--radius-lg);
  padding: 24px;
  overflow: hidden;
  min-height: 200px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: 0 4px 20px -4px rgba(77, 168, 118, 0.25);
}

.hero-decorations {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.deco {
  position: absolute;
}

.deco-coin {
  top: 24px;
  right: 64px;
  width: 56px;
  height: 56px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.1));
}

.deco-donut {
  top: 100px;
  right: 20px;
  width: 48px;
  height: 48px;
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.1));
  transform: rotate(-15deg);
}

.deco-leaf {
  bottom: 40px;
  right: 80px;
  width: 44px;
  height: 44px;
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.1));
  transform: rotate(25deg);
}

.deco-blob {
  bottom: -30px;
  right: -20px;
  width: 100px;
  height: 100px;
  opacity: 0.65;
}

.hero-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hero-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  align-self: flex-start;
  backdrop-filter: blur(8px);
}

.badge-icon {
  width: 12px;
  height: 12px;
  color: var(--primary-deep);
}

.hero-balance {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.hero-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.95);
  border: none;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  align-self: flex-start;
  backdrop-filter: blur(8px);
  transition: background 0.15s, transform 0.15s;
}

.hero-btn:hover { background: white; }
.hero-btn:active { transform: scale(0.97); }

.hero-btn svg {
  width: 14px;
  height: 14px;
}

/* ============================================================
   Stats row (income / expense)
   ============================================================ */
.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon svg { width: 22px; height: 22px; }

.stat-income .stat-icon { background: var(--income-soft); color: var(--income); }
.stat-expense .stat-icon { background: var(--expense-soft); color: var(--expense); }

.stat-info { min-width: 0; }
.stat-label { font-size: 12px; color: var(--text-muted); }
.stat-value {
  font-size: 18px;
  font-weight: 700;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.stat-income .stat-value { color: var(--income); }
.stat-expense .stat-value { color: var(--expense); }

/* ============================================================
   Sections (Top expenses, Budget)
   ============================================================ */
.section {
  margin-top: 24px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding: 0 4px;
}

.section-header h2 {
  font-size: 17px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.tag-green {
  background: var(--primary-tint);
  color: var(--primary-deep);
}

/* ============================================================
   Top expenses list
   ============================================================ */
.expense-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.expense-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.expense-item:last-child { border-bottom: none; }

.cat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.cat-info { flex: 1; min-width: 0; }
.cat-name {
  font-weight: 500;
  font-size: 14px;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cat-date {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 1px;
}

.cat-amount {
  font-weight: 700;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  color: var(--expense);
  white-space: nowrap;
}

.cat-delete {
  background: transparent;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-faint);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  margin-left: 4px;
}

.cat-delete:hover { background: var(--expense-soft); color: var(--expense); }
.cat-delete svg { width: 16px; height: 16px; }

/* ============================================================
   Budget grid
   ============================================================ */
.budget-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.budget-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.budget-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.budget-name { font-size: 13px; font-weight: 500; color: var(--text-muted); }
.budget-chevron { color: var(--text-faint); }
.budget-chevron svg { width: 16px; height: 16px; }

.budget-amount {
  font-size: 18px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  margin-bottom: 8px;
}

.progress {
  height: 6px;
  background: var(--surface-soft);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 6px;
}

.progress-bar {
  height: 100%;
  border-radius: 999px;
  transition: width 0.3s;
}

.budget-left {
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   Empty state (inline)
   ============================================================ */
.empty-inline {
  text-align: center;
  padding: 24px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px dashed var(--border-strong);
  color: var(--text-muted);
  font-size: 13px;
}

/* ============================================================
   Section action button (small icon next to section title)
   ============================================================ */
.section-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-action-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.section-action-btn:hover {
  background: var(--primary-tint);
  color: var(--primary-deep);
  border-color: var(--primary-soft);
}

.section-action-btn svg {
  width: 16px;
  height: 16px;
}

.section-action-btn:active {
  transform: scale(0.95);
}

/* ============================================================
   Bottom nav
   ============================================================ */
.bottom-nav {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 12px;
  width: calc(100% - 32px);
  max-width: 448px;
  box-shadow: var(--shadow-lg);
  z-index: 50;
}

.nav-item {
  background: transparent;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  border-radius: 12px;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 10px;
  font-weight: 500;
  min-width: 56px;
  transition: color 0.15s;
}

.nav-item svg { width: 22px; height: 22px; }
.nav-item:hover { color: var(--text-muted); }
.nav-item.active { color: var(--text); }
.nav-item.active svg { stroke-width: 2.2; }

.nav-fab {
  width: var(--nav-fab-size);
  height: var(--nav-fab-size);
  border-radius: 50%;
  background: linear-gradient(135deg, #7DB87A 0%, #4FA876 100%);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  box-shadow: var(--shadow-fab);
  margin: 0 4px;
  transition: transform 0.15s, box-shadow 0.15s;
  flex-shrink: 0;
}

.nav-fab:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px -2px rgba(77, 168, 118, 0.6);
}

.nav-fab:active { transform: translateY(0) scale(0.96); }
.nav-fab svg { width: 26px; height: 26px; }

/* ============================================================
   Modal (bottom sheet)
   ============================================================ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

/* Defensive: ensure [hidden] actually hides the modal even though
   .modal sets display: flex. Without this, the close animation may run
   but the modal stays visible. */
.modal[hidden] { display: none !important; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(2px);
  animation: fadeIn 0.2s ease;
}

.modal-sheet {
  position: relative;
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 12px 20px 28px;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 999px;
  margin: 0 auto 16px;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.sheet-header h2 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
}

.sheet-close {
  background: var(--surface-soft);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.sheet-close:hover { background: var(--border); }
.sheet-close svg { width: 16px; height: 16px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

/* ============================================================
   Form (inside modal)
   ============================================================ */
.form-error {
  padding: 10px 12px;
  background: var(--expense-soft);
  color: var(--expense);
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 14px;
}

.type-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  border: none;
  padding: 0;
  margin: 0 0 16px;
}

.type-option { position: relative; cursor: pointer; }
.type-option input { position: absolute; opacity: 0; pointer-events: none; }

.type-option span {
  display: block;
  text-align: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: all 0.15s;
  user-select: none;
}

.type-option:hover span { background: var(--surface-soft); }
.type-option input:checked + span {
  color: white;
  border-color: transparent;
}
.type-option input[value="income"]:checked + span { background: var(--income); }
.type-option input[value="expense"]:checked + span { background: var(--expense); }

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.form-field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

input[type="text"],
input[type="number"],
input[type="date"],
select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-tint);
}

/* ============================================================
   Budget edit form
   ============================================================ */
.sheet-subtitle {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.budget-form {
  display: flex;
  flex-direction: column;
}

.budget-edit-list {
  display: flex;
  flex-direction: column;
  margin-bottom: 16px;
  max-height: 50vh;
  overflow-y: auto;
  margin-left: -4px;
  margin-right: -4px;
  padding: 0 4px;
}

.budget-edit-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.budget-edit-row:last-child { border-bottom: none; }

.budget-edit-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.budget-edit-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.budget-edit-input {
  display: flex;
  align-items: center;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  width: 120px;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

.budget-edit-input:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-tint);
  background: var(--surface);
}

.budget-edit-input .prefix {
  color: var(--text-muted);
  font-size: 14px;
  margin-right: 2px;
}

.budget-edit-input input {
  border: none;
  background: transparent;
  padding: 0;
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.budget-edit-input input:focus {
  outline: none;
  box-shadow: none;
}

.budget-edit-reset {
  background: transparent;
  border: none;
  color: var(--text-faint);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0;
  text-decoration: underline;
  text-underline-offset: 2px;
  transition: color 0.15s;
}

.budget-edit-reset:hover:not(:disabled) {
  color: var(--primary-deep);
}

.budget-edit-reset:disabled {
  color: var(--text-faint);
  text-decoration: none;
  cursor: default;
  opacity: 0.5;
}

.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}

.btn {
  padding: 14px 20px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.15s;
  flex: 1;
}

.btn-primary {
  background: var(--primary-deep);
  color: white;
  flex: 2;
}
.btn-primary:hover { background: var(--primary); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
  flex: 1;
}
.btn-ghost:hover { background: var(--surface-soft); color: var(--text); }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 32px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 200;
  animation: toastIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes toastIn {
  from { transform: translateX(-50%) translateY(20px); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ============================================================
   Utility
   ============================================================ */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ============================================================
   Mobile tweaks
   ============================================================ */
@media (max-width: 360px) {
  .app { padding: 12px 16px 0; }
  .hero-balance { font-size: 32px; }
  .deco-coin { width: 48px; height: 48px; right: 56px; }
  .deco-donut { width: 40px; height: 40px; }
  .deco-leaf { width: 36px; height: 36px; right: 70px; }
}

@media (min-width: 481px) {
  .app { padding: 24px 24px 0; }
}
