/* ========== CSS Variables & Theme ========== */
:root {
  --bg: #F8FAFC;
  --bg-card: #FFFFFF;
  --bg-input: #F1F5F9;
  --text: #0F172A;
  --text-secondary: #64748B;
  --text-hint: #94A3B8;
  --border: #E2E8F0;
  --primary: #4F46E5;
  --primary-light: #EEF2FF;
  --primary-dark: #4338CA;
  --danger: #EF4444;
  --danger-light: #FEF2F2;
  --income: #10B981;
  --income-light: #ECFDF5;
  --expense: #F59E0B;
  --expense-light: #FFFBEB;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --header-h: 56px;
  --tab-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg: #0F172A;
  --bg-card: #1E293B;
  --bg-input: #334155;
  --text: #F1F5F9;
  --text-secondary: #94A3B8;
  --text-hint: #64748B;
  --border: #334155;
  --primary: #818CF8;
  --primary-light: #1E1B4B;
  --primary-dark: #6366F1;
  --danger: #F87171;
  --danger-light: #3B1515;
  --income: #34D399;
  --income-light: #0A2E20;
  --expense: #FBBF24;
  --expense-light: #2E2400;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.4);
}

/* ========== Reset & Base ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  padding-bottom: calc(var(--tab-h) + var(--safe-bottom));
  padding-top: var(--header-h);
  -webkit-font-smoothing: antialiased;
  transition: background var(--transition), color var(--transition);
}
input, button, select { font: inherit; color: inherit; }
input { outline: none; border: none; background: var(--bg-input); border-radius: var(--radius-sm); padding: 10px 14px; transition: box-shadow var(--transition); }
input:focus { box-shadow: 0 0 0 3px var(--primary-light); }
button { cursor: pointer; border: none; background: none; -webkit-tap-highlight-color: transparent; }
a { color: var(--primary); text-decoration: none; }
.hidden { display: none !important; }

/* ========== Header ========== */
.app-header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  height: var(--header-h); padding: 0 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.app-title { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary); transition: background var(--transition);
}
.icon-btn:active { background: var(--bg-input); }

/* ========== Bottom Tab Bar ========== */
.tab-bar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  display: flex; align-items: center;
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.tab-btn {
  flex: 1; height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  color: var(--text-hint); font-size: 11px; font-weight: 500;
  transition: color var(--transition);
}
.tab-btn.active { color: var(--primary); }

/* ========== Main Content ========== */
.app-main { padding: 16px; max-width: 500px; margin: 0 auto; }
.page { display: none; }
.page.active { display: block; animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ========== Cards ========== */
.form-card {
  background: var(--bg-card); border-radius: var(--radius-lg);
  padding: 20px; margin-bottom: 12px; box-shadow: var(--shadow-sm);
}

/* ========== Amount Section ========== */
.amount-section {
  display: flex; align-items: center; justify-content: center;
  padding: 8px 0 16px;
}
.currency-symbol { font-size: 32px; font-weight: 600; color: var(--text-secondary); margin-right: 4px; }
.amount-input {
  font-size: 40px; font-weight: 700; width: 180px; text-align: center;
  background: transparent; border: none; padding: 0; letter-spacing: -1px;
  color: var(--text);
}
.amount-input:focus { box-shadow: none; }
.amount-input::placeholder { color: var(--text-hint); }

/* ========== Type Toggle ========== */
.type-toggle {
  display: flex; gap: 0; background: var(--bg-input);
  border-radius: var(--radius); padding: 3px;
}
.type-btn {
  flex: 1; padding: 8px 16px; border-radius: calc(var(--radius) - 3px);
  font-size: 15px; font-weight: 600; color: var(--text-secondary);
  transition: all var(--transition); text-align: center;
}
.type-btn.active {
  background: var(--bg-card); color: var(--text);
  box-shadow: var(--shadow-sm);
}
.type-btn.active[data-type="income"] { color: var(--income); }
.type-btn.active[data-type="expense"] { color: var(--expense); }
.type-toggle.small .type-btn { padding: 6px 12px; font-size: 13px; }

/* ========== Category Grid ========== */
.form-label {
  display: block; font-size: 13px; font-weight: 600; color: var(--text-secondary);
  margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.5px;
}
.category-grid {
  display: flex; flex-wrap: wrap; gap: 8px;
}
.category-item {
  display: flex; align-items: center; gap: 5px;
  padding: 8px 14px; border-radius: 20px; font-size: 14px; font-weight: 500;
  background: var(--bg-input); color: var(--text-secondary);
  transition: all var(--transition); cursor: pointer;
  user-select: none; -webkit-user-select: none;
}
.category-item:active { transform: scale(0.96); }
.category-item.selected {
  background: var(--primary); color: #FFFFFF;
}
.category-item .cat-icon { font-size: 16px; }
.category-grid.small .category-item { padding: 6px 12px; font-size: 13px; }

/* ========== Form Rows ========== */
.form-row { margin-bottom: 16px; }
.form-row:last-child { margin-bottom: 0; }
.form-input {
  width: 100%; font-size: 15px;
}

/* ========== Buttons ========== */
.btn-primary {
  width: 100%; padding: 14px; border-radius: var(--radius);
  background: var(--primary); color: #FFFFFF; font-size: 17px; font-weight: 600;
  transition: all var(--transition); margin-top: 4px;
}
.btn-primary:active { background: var(--primary-dark); transform: scale(0.98); }
.btn-outline {
  padding: 8px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); font-size: 13px; font-weight: 500;
  color: var(--text-secondary); transition: all var(--transition);
  white-space: nowrap;
}
.btn-outline:active { background: var(--bg-input); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:active { background: var(--danger-light); }
.btn-link {
  display: block; width: 100%; text-align: center; padding: 8px;
  font-size: 13px; color: var(--primary); font-weight: 500;
}

/* ========== Bills Page ========== */
.bills-toolbar {
  display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px;
}
.search-box {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-card); border-radius: var(--radius);
  padding: 10px 14px; box-shadow: var(--shadow-sm);
}
.search-input { flex: 1; background: transparent; padding: 0; font-size: 15px; }
.search-input:focus { box-shadow: none; }
.filter-row { display: flex; gap: 8px; align-items: center; }
.month-filter { flex: 1; max-width: 150px; }

.bills-summary {
  display: flex; gap: 12px; margin-bottom: 12px;
}
.bills-summary-item {
  flex: 1; background: var(--bg-card); border-radius: var(--radius);
  padding: 12px; text-align: center; box-shadow: var(--shadow-sm);
}
.bills-summary-label { font-size: 11px; color: var(--text-hint); display: block; margin-bottom: 4px; }
.bills-summary-value { font-size: 18px; font-weight: 700; }
.bills-summary-value.income { color: var(--income); }
.bills-summary-value.expense { color: var(--expense); }

.bills-list { display: flex; flex-direction: column; gap: 8px; }
.bill-item {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow-sm);
  display: flex; align-items: center; gap: 12px;
  transition: transform var(--transition);
}
.bill-item:active { transform: scale(0.99); }

.bill-icon {
  width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; flex-shrink: 0;
}
.bill-icon.expense { background: var(--expense-light); }
.bill-icon.income { background: var(--income-light); }

.bill-info { flex: 1; min-width: 0; }
.bill-category { font-size: 15px; font-weight: 600; }
.bill-meta { font-size: 12px; color: var(--text-hint); margin-top: 2px; display: flex; gap: 8px; }
.bill-amount { font-size: 17px; font-weight: 700; white-space: nowrap; }
.bill-amount.expense { color: var(--expense); }
.bill-amount.income { color: var(--income); }

.bill-actions { display: flex; gap: 4px; }

.empty-state {
  text-align: center; padding: 40px 20px; color: var(--text-hint);
}
.empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 15px; margin-bottom: 4px; }
.empty-hint { font-size: 13px; color: var(--text-hint); }

/* ========== Stats Page ========== */
.stats-cards {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px;
}
.stat-card {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 14px 12px; text-align: center; box-shadow: var(--shadow-sm);
}
.stat-label { display: block; font-size: 11px; color: var(--text-hint); margin-bottom: 6px; }
.stat-value { font-size: 17px; font-weight: 700; }
.income-card .stat-value { color: var(--income); }
.expense-card .stat-value { color: var(--expense); }
.balance-card .stat-value { color: var(--primary); }

.chart-card {
  background: var(--bg-card); border-radius: var(--radius-lg);
  padding: 20px; margin-bottom: 12px; box-shadow: var(--shadow-sm);
}
.chart-title { font-size: 15px; font-weight: 600; margin-bottom: 16px; }
.chart-container { position: relative; width: 100%; height: 240px; }
.chart-empty { text-align: center; padding: 40px; color: var(--text-hint); font-size: 14px; }

/* ========== Toast ========== */
.toast {
  position: fixed; top: calc(var(--header-h) + 12px); left: 50%; transform: translateX(-50%) translateY(-120px);
  background: var(--text); color: var(--bg); padding: 10px 20px;
  border-radius: 20px; font-size: 14px; font-weight: 500;
  z-index: 999; pointer-events: none; transition: transform 0.3s ease;
  white-space: nowrap; box-shadow: var(--shadow-md);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ========== Modal ========== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.4); display: flex; align-items: flex-end; justify-content: center;
  visibility: hidden; opacity: 0; transition: all 0.25s ease;
}
.modal-overlay.show { visibility: visible; opacity: 1; }
.modal {
  background: var(--bg-card); border-radius: 20px 20px 0 0;
  width: 100%; max-width: 500px; max-height: 85vh; overflow-y: auto;
  transform: translateY(100%); transition: transform 0.3s ease;
}
.modal-overlay.show .modal { transform: translateY(0); }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 12px;
}
.modal-header h3 { font-size: 18px; font-weight: 700; }
.modal-body { padding: 0 20px 20px; }
.modal-footer { display: flex; gap: 10px; margin-top: 16px; }
.modal-footer .btn-outline { flex: 1; text-align: center; }
.modal-footer .btn-primary { flex: 2; margin-top: 0; }

/* Category edit list */
.category-edit-list { display: flex; flex-wrap: wrap; gap: 8px; }
.category-edit-item {
  display: flex; align-items: center; gap: 4px;
  padding: 6px 12px; border-radius: 20px; font-size: 14px;
  background: var(--bg-input); color: var(--text);
}
.category-edit-item .delete-cat {
  width: 20px; height: 20px; border-radius: 50%; display: flex;
  align-items: center; justify-content: center; font-size: 12px;
  color: var(--text-hint); cursor: pointer; transition: all var(--transition);
}
.category-edit-item .delete-cat:hover { color: var(--danger); background: var(--danger-light); }
.add-category-row { display: flex; gap: 8px; }
.add-category-row input { flex: 1; }

/* ========== Responsive tweaks ========== */
@media (min-width: 500px) {
  .app-main { padding: 20px; }
  body { padding-bottom: calc(var(--tab-h) + 20px); }
}
