/* Modern household budget app — adaptive light/dark theme */

:root {
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12), 0 16px 48px rgba(0, 0, 0, 0.08);
  --transition: 150ms ease;

  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-soft: #eef2ff;
  --income: #10b981;
  --income-soft: #d1fae5;
  --expense: #ef4444;
  --expense-soft: #fee2e2;
  --warning: #f59e0b;
  --warning-soft: #fef3c7;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #131c2e;
  --surface-2: #1c2840;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #1e2a40;
  --accent: #818cf8;
  --accent-hover: #6366f1;
  --accent-soft: #1e1b4b;
  --income: #34d399;
  --income-soft: #064e3b;
  --expense: #f87171;
  --expense-soft: #7f1d1d;
  --warning: #fbbf24;
  --warning-soft: #78350f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.2);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.4), 0 16px 48px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  font-feature-settings: 'tnum';
  transition: background var(--transition), color var(--transition);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 1rem;
  padding-bottom: 1rem;
  gap: 1rem;
}

.header-inner h1 {
  margin: 0;
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-actions {
  display: flex;
  gap: 0.625rem;
  align-items: center;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0 0 1rem;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

main.container {
  padding-top: 1.5rem;
  padding-bottom: 3rem;
  display: grid;
  gap: 1.25rem;
}

/* Summary cards */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.stat-card {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  position: relative;
  overflow: hidden;
}

.stat-card::before {
  content: '';
  position: absolute;
  inset: 0;
  height: 3px;
  background: var(--accent);
}

.income-card::before { background: var(--income); }
.expense-card::before { background: var(--expense); }
.balance-card::before { background: var(--accent); }

.stat-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-amount {
  font-size: 1.875rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.income-card .stat-amount { color: var(--income); }
.expense-card .stat-amount { color: var(--expense); }
.balance-card .stat-amount.negative { color: var(--expense); }

.stat-delta {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  min-height: 1.2em;
}

.stat-delta.up { color: var(--expense); }
.stat-delta.down { color: var(--income); }

/* Charts */
.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.chart-wrap {
  position: relative;
  height: 240px;
}

/* Budget */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.section-header h2 { margin: 0; }

.budget-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.budget-row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.budget-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.budget-meta .cat {
  color: var(--text);
  font-weight: 500;
}

.budget-meta .pct {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.budget-meta.warn .pct { color: var(--warning); }
.budget-meta.over .pct { color: var(--expense); font-weight: 600; }

.progress {
  height: 8px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 400ms cubic-bezier(0.4, 0, 0.2, 1);
}

.progress-bar.warn { background: var(--warning); }
.progress-bar.over { background: var(--expense); }

.empty-budget {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 1rem 0;
  margin: 0;
}

/* Form */
.form-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.75rem;
  align-items: end;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.form-field-wide { grid-column: span 2; }

.form-field span {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 500;
}

input, select {
  font-family: inherit;
  font-size: 0.9375rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  transition: border-color var(--transition), box-shadow var(--transition);
  min-height: 38px;
}

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

input[type="month"], input[type="date"] {
  color-scheme: light dark;
}

.form-submit { height: 38px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: all var(--transition);
  white-space: nowrap;
  user-select: none;
}

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: var(--surface-2);
  border-color: var(--text-muted);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 0.375rem 0.75rem;
}

.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-danger {
  background: transparent;
  color: var(--expense);
  border-color: transparent;
  padding: 0.375rem 0.75rem;
}

.btn-danger:hover { background: var(--expense-soft); }

.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1rem;
  transition: all var(--transition);
}

.icon-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

.month-picker { background: var(--surface-2); }

/* Records */
.records-tools {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.search-input {
  width: 200px;
  min-width: 0;
}

.records-table {
  width: 100%;
  border-collapse: collapse;
}

.records-table th,
.records-table td {
  text-align: left;
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.records-table th {
  font-weight: 600;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.records-table tbody tr:last-child td { border-bottom: none; }
.records-table tbody tr:hover { background: var(--surface-2); }

.records-table .amount-col {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  white-space: nowrap;
}

.records-table .amount-col.income { color: var(--income); }
.records-table .amount-col.expense { color: var(--expense); }

.records-table .actions-col {
  text-align: right;
  white-space: nowrap;
}

.records-table .date-col {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--text-muted);
}

.records-table .note-col {
  color: var(--text-muted);
  font-size: 0.85rem;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-msg {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 0;
  margin: 0;
  font-size: 0.9rem;
}

.cat-pill {
  display: inline-block;
  background: var(--surface-2);
  color: var(--text);
  padding: 0.125rem 0.625rem;
  border-radius: 999px;
  font-size: 0.8125rem;
}

/* Modal */
.modal {
  border: none;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 520px;
  width: calc(100% - 2rem);
  box-shadow: var(--shadow-lg);
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

.modal h2 {
  margin: 0 0 1rem;
  font-size: 1.125rem;
}

.modal-note {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin: 0 0 1rem;
}

#edit-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  align-items: end;
}

#edit-form h2 { grid-column: span 2; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
  grid-column: span 2;
}

.budget-fields {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 320px;
  overflow-y: auto;
  margin-bottom: 0.75rem;
}

.budget-field-row {
  display: grid;
  grid-template-columns: 1fr 140px auto;
  gap: 0.5rem;
  align-items: center;
}

.btn-add-row {
  align-self: flex-start;
  margin-top: 0.25rem;
}

/* Mobile */
@media (max-width: 768px) {
  .container { padding: 0 1rem; }
  .summary-grid { grid-template-columns: 1fr; }
  .charts-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-submit { grid-column: span 2; }
  .form-field-wide { grid-column: span 2; }
  .stat-amount { font-size: 1.625rem; }
  .header-inner h1 { font-size: 1.125rem; }
  .records-table .note-col { display: none; }
  .records-table th:nth-child(4),
  .records-table td:nth-child(4) { display: none; }
  .search-input { width: 130px; }
  #edit-form { grid-template-columns: 1fr; }
  #edit-form h2,
  #edit-form .modal-actions,
  #edit-form .form-field-wide { grid-column: span 1; }
}
