/* ==========================================================================
   CommonGround Design System
   Privacy-first family budgeting - Warm, trustworthy, clean
   Palette: Cream/beige neutrals + sage green accent
   ========================================================================== */

/* --- CSS Variables / Design Tokens --- */
:root {
  --color-bg: #faf8f5;
  --color-surface: #ffffff;
  --color-surface-elevated: #f7f4ef;
  --color-border: #e8e4dd;
  --color-border-light: #f0ece6;
  --color-text: #2c2824;
  --color-text-secondary: #6b6560;
  --color-text-muted: #999490;
  --color-accent: #5b8c5a;
  --color-accent-hover: #4a7549;
  --color-accent-light: #e8f2e7;
  --color-accent-subtle: #f2f7f1;
  --color-income: #3d8c40;
  --color-income-bg: #e8f5e8;
  --color-expense: #c25450;
  --color-expense-bg: #fdf0ef;
  --color-danger: #c25450;
  --color-danger-hover: #a84542;
  --color-warning: #c2862e;
  --color-warning-bg: #fef7ed;
  --color-sage-dark: #3a5c39;
  --color-cream: #f5f1ea;
  --color-clay: #c49a6c;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 999px;
  --shadow-sm: 0 1px 2px rgba(44, 40, 36, 0.04);
  --shadow-md: 0 4px 12px rgba(44, 40, 36, 0.06);
  --shadow-lg: 0 8px 24px rgba(44, 40, 36, 0.08);
  --font-sans: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', 'Cascadia Code', 'SF Mono', monospace;
  --transition: 200ms cubic-bezier(0.16, 1, 0.3, 1);
  --max-width: 1100px;
  --header-height: 64px;
}

/* --- Reset --- */
*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100dvh;
}

/* --- Header --- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(250, 248, 245, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--color-border);
  height: var(--header-height);
}
.header-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
}
.logo-section { display: flex; align-items: center; gap: 12px; }
.logo {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-sage-dark);
  white-space: nowrap;
}
.privacy-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-accent);
  background: var(--color-accent-light);
  padding: 3px 10px;
  border-radius: var(--radius-full);
  white-space: nowrap;
}

/* --- Navigation --- */
.main-nav { display: flex; gap: 4px; }
.nav-btn {
  background: none;
  border: none;
  padding: 8px 16px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}
.nav-btn:hover { color: var(--color-text); background: var(--color-border-light); }
.nav-btn.active { color: var(--color-accent); background: var(--color-accent-light); font-weight: 600; }

.menu-toggle {
  display: none;
  flex-direction: column;
  gap: 5px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
}
.menu-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--color-text);
  border-radius: 2px;
  transition: var(--transition);
}

/* --- Main Content --- */
.app-main {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 32px 24px 64px;
}
.view { display: none; }
.view.active { display: block; }
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 28px;
}
.view-header h2 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }
.view-subtitle { color: var(--color-text-secondary); font-size: 0.9rem; }

/* --- Dashboard Cards --- */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: box-shadow var(--transition);
}
.stat-card:hover { box-shadow: var(--shadow-md); }
.stat-label { font-size: 0.8rem; font-weight: 500; color: var(--color-text-secondary); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 2rem; font-weight: 700; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.income-card .stat-value { color: var(--color-income); }
.expense-card .stat-value { color: var(--color-expense); }
.balance-card .stat-value { color: var(--color-text); }
.stat-trend { font-size: 0.8rem; color: var(--color-text-muted); }
.stat-sub { font-size: 0.8rem; font-weight: 500; }
.stat-sub.positive { color: var(--color-income); }
.stat-sub.negative { color: var(--color-expense); }

.dashboard-secondary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

/* --- Cards --- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 24px;
}
.card h3 { font-size: 1rem; font-weight: 600; margin-bottom: 16px; color: var(--color-text); }
.card-desc { font-size: 0.85rem; color: var(--color-text-secondary); margin-bottom: 16px; }

/* --- Budget Health --- */
.budget-health-list { display: flex; flex-direction: column; gap: 12px; }
.budget-health-item { display: flex; flex-direction: column; gap: 4px; }
.bh-header { display: flex; justify-content: space-between; align-items: center; }
.bh-category { font-size: 0.85rem; font-weight: 500; }
.bh-amount { font-size: 0.85rem; color: var(--color-text-secondary); font-variant-numeric: tabular-nums; }
.bh-bar {
  height: 6px;
  border-radius: var(--radius-full);
  background: var(--color-border-light);
  overflow: hidden;
}
.bh-fill {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}
.bh-fill.under { background: var(--color-accent); }
.bh-fill.over { background: var(--color-expense); }
.bh-fill.warning { background: var(--color-warning); }

/* --- Recent Activity --- */
.recent-list { display: flex; flex-direction: column; gap: 8px; }
.recent-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  transition: background var(--transition);
}
.recent-item:hover { background: var(--color-bg); }
.recent-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  flex-shrink: 0;
}
.recent-icon.income { background: var(--color-income-bg); }
.recent-icon.expense { background: var(--color-expense-bg); }
.recent-info { flex: 1; min-width: 0; }
.recent-desc { font-size: 0.85rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recent-meta { font-size: 0.75rem; color: var(--color-text-muted); }
.recent-amount { font-weight: 600; font-size: 0.9rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.recent-amount.income { color: var(--color-income); }
.recent-amount.expense { color: var(--color-expense); }

/* --- Transaction List --- */
#transactionList { padding: 0; overflow: hidden; }
.tx-table { width: 100%; border-collapse: collapse; }
.tx-table th {
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
}
.tx-table td {
  padding: 12px 16px;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--color-border-light);
}
.tx-table tbody tr { transition: background var(--transition); }
.tx-table tbody tr:hover { background: var(--color-bg); }
.tx-type-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.tx-type-badge.income { background: var(--color-income-bg); color: var(--color-income); }
.tx-type-badge.expense { background: var(--color-expense-bg); color: var(--color-expense); }
.tx-amount { font-weight: 600; font-variant-numeric: tabular-nums; }
.tx-amount.income { color: var(--color-income); }
.tx-amount.expense { color: var(--color-expense); }
.tx-actions { display: flex; gap: 8px; }
.tx-action-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  color: var(--color-text-muted);
  transition: all var(--transition);
  font-family: inherit;
}
.tx-action-btn:hover { background: var(--color-border-light); color: var(--color-text); }
.tx-action-btn.delete:hover { color: var(--color-danger); background: var(--color-expense-bg); }

/* --- Empty State --- */
.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--color-text-secondary);
}
.empty-icon { margin-bottom: 16px; color: var(--color-text-muted); display: flex; justify-content: center; }
.empty-state p { font-size: 0.95rem; font-weight: 500; }
.empty-hint { font-size: 0.8rem; margin-top: 4px; color: var(--color-text-muted); }

/* --- Filters --- */
.filters-row {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

/* --- Inputs --- */
.input {
  padding: 9px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}
.input:focus { border-color: var(--color-accent); box-shadow: 0 0 0 3px var(--color-accent-subtle); }
.input::placeholder { color: var(--color-text-muted); }
.input-select { cursor: pointer; }

/* --- Buttons --- */
.btn {
  padding: 9px 20px;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all var(--transition);
  white-space: nowrap;
  letter-spacing: -0.01em;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--color-accent);
  color: #ffffff;
}
.btn-primary:hover { background: var(--color-accent-hover); }
.btn-secondary {
  background: var(--color-bg);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}
.btn-secondary:hover { background: var(--color-border-light); }
.btn-danger {
  background: var(--color-surface);
  color: var(--color-danger);
  border: 1px solid var(--color-expense-bg);
}
.btn-danger:hover { background: var(--color-expense-bg); }

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(44, 40, 36, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  padding: 28px;
  animation: modalIn 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(12px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.modal-header h3 { font-size: 1.15rem; font-weight: 700; }
.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  line-height: 1;
  transition: color var(--transition);
}
.modal-close:hover { color: var(--color-text); }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.8rem; font-weight: 600; margin-bottom: 5px; color: var(--color-text-secondary); }
.form-group .input { width: 100%; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 24px; }

/* --- Toast --- */
.toast-container { position: fixed; bottom: 24px; right: 24px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--color-text);
  color: #ffffff;
  padding: 12px 20px;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: toastIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  max-width: 360px;
}
.toast.success { background: var(--color-accent); }
.toast.error { background: var(--color-danger); }
@keyframes toastIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }

/* --- Reports --- */
.reports-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
.chart-card h3 { margin-bottom: 16px; }
.chart-container { position: relative; width: 100%; }
.chart-card .chart-container { height: 280px; }
.chart-wide { height: 320px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; justify-content: center; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--color-text-secondary); }
.legend-color { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }

/* --- Settings --- */
.settings-grid { display: grid; grid-template-columns: 1fr; gap: 20px; max-width: 680px; }
.member-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.member-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: var(--color-bg); border-radius: var(--radius-md); }
.member-name { font-size: 0.9rem; font-weight: 500; }
.member-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 1.1rem;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  transition: color var(--transition);
}
.member-remove:hover { color: var(--color-danger); }
.inline-form { display: flex; gap: 10px; }
.inline-form .input { flex: 1; }

.budget-targets-list { display: flex; flex-direction: column; gap: 10px; }
.budget-target-item { display: flex; align-items: center; gap: 10px; }
.budget-target-item label { font-size: 0.85rem; font-weight: 500; width: 120px; flex-shrink: 0; }
.budget-target-item .input { width: 140px; }

.data-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.hidden-file-input { display: none; }
.privacy-note {
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px;
  background: var(--color-accent-subtle);
  border-radius: var(--radius-md);
  line-height: 1.5;
}
.privacy-note svg { flex-shrink: 0; margin-top: 1px; color: var(--color-accent); }

/* --- Responsive --- */
@media (max-width: 768px) {
  .dashboard-grid { grid-template-columns: 1fr; }
  .dashboard-secondary { grid-template-columns: 1fr; }
  .reports-grid { grid-template-columns: 1fr; }
  .main-nav { display: none; position: absolute; top: var(--header-height); left: 0; right: 0; background: var(--color-surface); flex-direction: column; border-bottom: 1px solid var(--color-border); padding: 8px; }
  .main-nav.open { display: flex; }
  .menu-toggle { display: flex; }
  .view-header { flex-direction: column; align-items: flex-start; }
  .filters-row { flex-direction: column; }
  .inline-form { flex-direction: column; }
  .privacy-badge { display: none; }
  .tx-table { font-size: 0.8rem; }
  .tx-table th:nth-child(4), .tx-table td:nth-child(4) { display: none; }
  .tx-table th:nth-child(5), .tx-table td:nth-child(5) { display: none; }
  .data-actions { flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #1c1a17;
    --color-surface: #252320;
    --color-surface-elevated: #2c2925;
    --color-border: #3a3631;
    --color-border-light: #302d28;
    --color-text: #e8e4df;
    --color-text-secondary: #a8a39d;
    --color-text-muted: #6b6560;
    --color-accent: #6ba86a;
    --color-accent-hover: #7dba7c;
    --color-accent-light: #2a3d29;
    --color-accent-subtle: #242e23;
    --color-income: #5cba5b;
    --color-income-bg: #1e3320;
    --color-expense: #e06965;
    --color-expense-bg: #332020;
    --color-warning: #d49c3d;
    --color-warning-bg: #332a20;
    --color-cream: #2a2722;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.25);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.3);
  }
  .app-header { background: rgba(28, 26, 23, 0.85); }
  .modal-overlay { background: rgba(0,0,0,0.5); }
}
