/* ─── Reset & Base ──────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-bg: #fef2f2;
  --warning: #f59e0b;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07);
  --header-bg: #374151;
}

html { font-size: 14px; }

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

/* ─── Typography ────────────────────────────────────────────── */
.text-success { color: var(--success) !important; }
.text-danger { color: var(--danger) !important; }
.text-muted { color: var(--text-muted); }

/* ─── Header ────────────────────────────────────────────────── */
.header {
  background: var(--header-bg);
  color: white;
  padding: 0.6rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.logo { font-size: 1.1rem; font-weight: 700; white-space: nowrap; }

.header-right { display: flex; gap: 0.5rem; align-items: center; }

.budget-selector {
  background: rgba(255,255,255,0.15);
  color: white;
  border-color: rgba(255,255,255,0.3);
  min-width: 180px;
}
.budget-selector option { color: var(--text); background: white; }

/* ─── Tabs ──────────────────────────────────────────────────── */
.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  padding: 0 1rem;
  overflow-x: auto;
}

.tab {
  padding: 0.6rem 1.25rem;
  border: none;
  background: none;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* Colored tab badges matching the spreadsheet */
.tab[data-tab="expenses"] { background: #e2e8f0; border-radius: 4px 4px 0 0; }
.tab[data-tab="income"] { background: #e2e8f0; border-radius: 4px 4px 0 0; }
.tab[data-tab="summary"] { background: #e2e8f0; border-radius: 4px 4px 0 0; }
.tab[data-tab="transactions"] { background: #e2e8f0; border-radius: 4px 4px 0 0; }
.tab[data-tab="tx-summary"] { background: #e2e8f0; border-radius: 4px 4px 0 0; }
.tab.active[data-tab="expenses"],
.tab.active[data-tab="income"],
.tab.active[data-tab="summary"],
.tab.active[data-tab="transactions"],
.tab.active[data-tab="tx-summary"] {
  background: var(--primary-light);
}

/* ─── Buttons ───────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  transition: all 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-icon { padding: 0.3rem 0.5rem; border: none; background: none; font-size: 1.1rem; color: var(--text-muted); cursor: pointer; }
.btn-icon:hover { color: var(--text); }
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.75rem; }
.btn-lg { padding: 0.7rem 1.75rem; font-size: 0.95rem; }
.btn-xs { padding: 0.15rem 0.4rem; font-size: 0.7rem; border-radius: 3px; }
.btn-inline { padding: 0.1rem 0.3rem; border: none; background: none; color: var(--text-muted); cursor: pointer; font-size: 0.85rem; }
.btn-inline:hover { color: var(--danger); }

/* ─── Empty State ───────────────────────────────────────────── */
.empty-state { text-align: center; padding: 4rem 2rem; }
.empty-icon { font-size: 4rem; margin-bottom: 1rem; }
.empty-state h2 { margin-bottom: 0.5rem; }
.empty-state p { color: var(--text-muted); margin-bottom: 1.5rem; }

/* ─── Panels ────────────────────────────────────────────────── */
.panel { padding: 1rem 1.25rem; }

/* ─── Setup Tab ─────────────────────────────────────────────── */
.setup-container { max-width: 640px; }
.tab-heading { font-size: 1.2rem; color: #c2410c; margin-bottom: 0.25rem; }
.setup-container hr { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.setup-container h3 { font-size: 0.95rem; margin-bottom: 0.5rem; }
.instructions { padding-left: 1.25rem; margin-bottom: 0; font-size: 0.85rem; color: var(--text-muted); }
.instructions li { margin-bottom: 0.3rem; }
.setup-form { display: flex; flex-direction: column; gap: 0.75rem; max-width: 320px; }
.tip { font-size: 0.8rem; font-style: italic; color: #c2410c; }

/* ─── Forms & Inputs ────────────────────────────────────────── */
.input {
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
  background: var(--surface);
  color: var(--text);
}
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px rgba(79,70,229,0.15); }

.form-group { display: flex; flex-direction: column; gap: 0.2rem; }
.form-group label { font-size: 0.75rem; font-weight: 600; color: var(--text-muted); }
.form-group-btn { justify-content: flex-end; }
.form-row { display: flex; gap: 0.75rem; }

/* ─── Grid (Spreadsheet) ───────────────────────────────────── */
.grid-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.grid-scroll {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.budget-grid {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

.budget-grid th,
.budget-grid td {
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

.budget-grid th {
  background: var(--bg);
  font-weight: 600;
  font-size: 0.75rem;
  color: var(--text-muted);
  position: sticky;
  top: 0;
  z-index: 2;
}

/* Sticky first column */
.budget-grid .col-label {
  text-align: left;
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  min-width: 180px;
  max-width: 220px;
}

.budget-grid th.col-label {
  z-index: 3;
  background: var(--bg);
}

/* Section total row (category header) */
.budget-grid .section-row td {
  background: #f8fafc;
  font-weight: 700;
  border-top: 2px solid var(--border);
}

.budget-grid .section-row .col-label {
  background: #f8fafc;
  font-style: italic;
}

.section-name {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* Item row */
.budget-grid .item-row .col-label {
  padding-left: 1.5rem;
  font-weight: 400;
}

/* Editable cell */
.budget-grid .cell {
  cursor: pointer;
  min-width: 80px;
}
.budget-grid .cell:hover { background: var(--primary-light); }

.cell-input {
  width: 70px;
  padding: 0.15rem 0.3rem;
  border: 1px solid var(--primary);
  border-radius: 3px;
  font-size: 0.8rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.cell-input:focus { outline: none; box-shadow: 0 0 0 2px rgba(79,70,229,0.2); }

/* Section label in summary */
.section-label {
  font-size: 1rem;
  color: #c2410c;
  padding: 0.75rem 0.6rem 0.35rem;
}

/* ─── Summary ───────────────────────────────────────────────── */
.summary-notice {
  display: flex;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
  gap: 2rem;
}
.summary-notice p { font-size: 0.8rem; margin-top: 0.25rem; }
.notice-right { text-align: right; }
.notice-right strong { color: var(--danger); }

.summary-grid .total-row td { font-weight: 700; border-top: 2px solid #94a3b8; }
.summary-grid .subtotal-row td { border-top: 1px solid #94a3b8; }

/* ─── Transactions ──────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  margin-bottom: 0.75rem;
}

.tx-form-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  flex-wrap: wrap;
}
.tx-form-row .form-group { flex: 1; min-width: 110px; }
.tx-form-row .form-group-btn { flex: 0; }

.tx-table td, .tx-table th { text-align: left; }
.tx-table .col-amount { text-align: right; }
.tx-table .col-actions { text-align: center; width: 40px; }

/* ─── Tx Summary ─────────────────────────────────────────────── */
#tx-summary-grid .col-label { min-width: 160px; }
.budget-actual-cell { font-size: 0.75rem; }
.ba-budget { color: var(--text-muted); }
.ba-actual { font-weight: 600; }
.ba-over { color: var(--danger); }
.ba-under { color: var(--success); }

/* ─── Modals ────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 1rem;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%; max-width: 480px;
  max-height: 85vh; overflow-y: auto;
}
.modal-sm { max-width: 360px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 0.95rem; }
.modal-body { padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem; }
.modal-footer { display: flex; gap: 0.5rem; justify-content: flex-end; padding: 0.75rem 1rem; border-top: 1px solid var(--border); }

/* ─── Toast ─────────────────────────────────────────────────── */
.toast-container { position: fixed; bottom: 1rem; right: 1rem; z-index: 300; display: flex; flex-direction: column; gap: 0.4rem; }
.toast { padding: 0.6rem 1rem; border-radius: var(--radius); background: var(--text); color: white; font-size: 0.8rem; box-shadow: var(--shadow-md); animation: toast-in 0.25s ease; }
.toast-error { background: var(--danger); }
.toast-success { background: var(--success); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ─── Responsive ────────────────────────────────────────────── */
@media (max-width: 768px) {
  .header { flex-wrap: wrap; gap: 0.4rem; padding: 0.5rem 0.75rem; }
  .header-right { width: 100%; }
  .budget-selector { flex: 1; }
  .tabs { padding: 0 0.25rem; }
  .tab { padding: 0.5rem 0.75rem; font-size: 0.75rem; }
  .panel { padding: 0.75rem; }
  .tx-form-row { flex-direction: column; }
  .tx-form-row .form-group { min-width: 100%; }
  .summary-notice { flex-direction: column; gap: 0.75rem; }
  .notice-right { text-align: left; }
}
