/* ── VARIABLES ─────────────────────────────────────────────────────────── */
:root {
  --bg: #f1f5f9;
  --sidebar-bg: #0f172a;
  --sidebar-hover: #1e293b;
  --sidebar-active: #3b82f6;
  --sidebar-text: #94a3b8;
  --sidebar-text-active: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --income: #059669;
  --income-bg: #d1fae5;
  --expense: #dc2626;
  --expense-bg: #fee2e2;
  --savings: #0284c7;
  --savings-bg: #e0f2fe;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
}

/* ── RESET ─────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button { cursor: pointer; border: none; background: none; font: inherit; }
input, select, textarea { font: inherit; }

.hidden { display: none !important; }

/* ── AUTH SCREEN ───────────────────────────────────────────────────────── */
.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 100%);
}
.auth-card {
  background: var(--card);
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-md);
}
.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 28px;
  justify-content: center;
}
.auth-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 4px;
  margin-bottom: 24px;
}
.auth-tab {
  flex: 1;
  padding: 8px;
  border-radius: 6px;
  font-weight: 500;
  font-size: 0.875rem;
  color: var(--text-muted);
  transition: all .15s;
}
.auth-tab.active {
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* ── APP LAYOUT ────────────────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: 220px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ── SIDEBAR ───────────────────────────────────────────────────────────── */
.sidebar {
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 20px 12px;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.125rem;
  font-weight: 700;
  color: #f8fafc;
  padding: 8px 12px 20px;
  border-bottom: 1px solid #1e293b;
  margin-bottom: 12px;
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--sidebar-text);
  font-size: 0.875rem;
  font-weight: 500;
  transition: all .15s;
  text-align: left;
  width: 100%;
}
.nav-item:hover { background: var(--sidebar-hover); color: #e2e8f0; }
.nav-item.active { background: var(--sidebar-active); color: #fff; }
.nav-item svg { flex-shrink: 0; opacity: .8; }
.nav-item.active svg { opacity: 1; }

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 0;
  border-top: 1px solid #1e293b;
  margin-top: 12px;
  color: var(--sidebar-text);
  font-size: 0.8125rem;
}
.sidebar-username { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-logout {
  color: var(--sidebar-text);
  font-size: 0.75rem;
  padding: 4px 8px;
  border-radius: 6px;
  border: 1px solid #1e293b;
  transition: all .15s;
  flex-shrink: 0;
}
.btn-logout:hover { background: var(--sidebar-hover); color: #e2e8f0; }

/* ── MAIN CONTENT ──────────────────────────────────────────────────────── */
.main-content {
  overflow-y: auto;
  padding: 28px 32px;
}
#content { max-width: 1100px; }

/* ── PAGE HEADER ───────────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.page-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
}
.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ── MONTH NAV ─────────────────────────────────────────────────────────── */
.month-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
}
.month-btn {
  font-size: 1.1rem;
  color: var(--text-muted);
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
  transition: color .15s;
}
.month-btn:hover { color: var(--primary); }
.month-label {
  font-weight: 600;
  font-size: 0.875rem;
  min-width: 130px;
  text-align: center;
  color: var(--text);
}

/* ── BUTTONS ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 500;
  font-size: 0.875rem;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  transition: all .15s;
}
.btn:hover { background: var(--bg); }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { background: var(--bg); }
.btn-full { width: 100%; justify-content: center; }

/* ── SUMMARY CARDS ─────────────────────────────────────────────────────── */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}
.summary-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px 24px;
  border-left: 4px solid transparent;
  box-shadow: var(--shadow);
}
.income-card { border-left-color: var(--income); }
.expense-card { border-left-color: var(--expense); }
.savings-card { border-left-color: var(--savings); }
.savings-card.negative { border-left-color: var(--expense); }
.summary-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.summary-amount {
  font-size: 1.625rem;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.income-card .summary-amount { color: var(--income); }
.expense-card .summary-amount { color: var(--expense); }
.savings-card.positive .summary-amount { color: var(--savings); }
.savings-card.negative .summary-amount { color: var(--expense); }
.summary-pct {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ── CARDS ─────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-header h2 {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
}
.card-body { padding: 20px; }

/* ── DASHBOARD GRID ────────────────────────────────────────────────────── */
.dash-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ── DASHBOARD EXPENSE TREE ────────────────────────────────────────────── */
.dash-cat { margin-bottom: 2px; }
.dash-cat-row {
  display: flex;
  align-items: center;
  padding: 6px 8px;
  border-radius: 6px;
  gap: 8px;
}
.dash-cat-row:hover { background: var(--bg); }
.dash-cat-name { flex: 1; font-weight: 500; font-size: 0.875rem; }
.dash-cat-total { font-size: 0.875rem; font-weight: 600; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ── INCOME ROW (dashboard) ────────────────────────────────────────────── */
.income-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.income-row:last-child { border-bottom: none; }
.income-desc { flex: 1; font-size: 0.875rem; }
.income-amount { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 0.875rem; color: var(--income); }

/* ── RECURRENCE BADGES ─────────────────────────────────────────────────── */
.recur-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 0.6875rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── TABLE ─────────────────────────────────────────────────────────────── */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th {
  padding: 10px 12px;
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
}
.data-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #fafafa; }
.data-table tr.inactive-row td { opacity: .45; }
.amt-cell { font-variant-numeric: tabular-nums; font-weight: 500; text-align: right; }
.date-cell { color: var(--text-muted); white-space: nowrap; }
.action-cell { text-align: right; white-space: nowrap; }

/* ── EXPENSE TREE ──────────────────────────────────────────────────────── */
.tree-container { padding: 12px; }

.tree-category { position: relative; }
.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 8px;
  transition: background .1s;
  min-height: 38px;
}
.tree-row:hover { background: var(--bg); }
.tree-row:hover .tree-actions { opacity: 1; }

.tree-toggle {
  width: 22px;
  height: 22px;
  font-size: 0.625rem;
  color: var(--text-muted);
  border-radius: 4px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .1s;
}
.tree-toggle:hover { background: var(--border); color: var(--text); }
.tree-toggle.invisible { visibility: hidden; }

.color-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.tree-cat-name {
  flex: 1;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-cat-total {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-right: 4px;
}

.tree-children {
  padding-left: 20px;
  margin-left: 10px;
  border-left: 2px solid var(--border);
  margin-bottom: 2px;
}

/* Expense row inside tree */
.expense-row { padding: 5px 8px; }
.expense-bullet {
  width: 22px;
  text-align: center;
  color: var(--border);
  font-size: 1.2rem;
  line-height: 1;
  flex-shrink: 0;
}
.expense-desc {
  flex: 1;
  font-size: 0.875rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.expense-monthly {
  font-size: 0.875rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  white-space: nowrap;
  margin-right: 4px;
}

/* Tree action buttons (hidden until hover) */
.tree-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity .15s;
  flex-shrink: 0;
}
.act-btn {
  padding: 3px 8px;
  border-radius: 5px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  border: 1px solid transparent;
  transition: all .15s;
  white-space: nowrap;
}
.act-btn:hover {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text);
}
.act-danger { color: var(--danger); }
.act-danger:hover { background: var(--expense-bg); border-color: #fca5a5; color: var(--danger); }

/* Category depth info (categories page) */
.cat-depth-info {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-right: 8px;
}

/* Icon buttons (table rows) */
.btn-icon {
  padding: 4px 8px;
  border-radius: 5px;
  font-size: 0.8rem;
  color: var(--text-muted);
  transition: all .15s;
}
.btn-icon:hover { background: var(--bg); color: var(--text); }
.btn-icon-danger:hover { background: var(--expense-bg); color: var(--danger); }

/* ── FORMS ─────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-group input:focus,
.form-group select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
  background: #fff;
}
.form-group input[type="color"] {
  height: 40px;
  padding: 4px 8px;
  cursor: pointer;
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.form-error {
  background: var(--expense-bg);
  color: var(--expense);
  border: 1px solid #fca5a5;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.875rem;
  margin-bottom: 14px;
}

/* ── MODAL ─────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  backdrop-filter: blur(2px);
}
.modal {
  background: var(--card);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 460px;
  padding: 28px;
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 1.25rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .15s;
}
.modal-close:hover { background: var(--bg); color: var(--text); }
.modal-title {
  font-size: 1.125rem;
  font-weight: 700;
  margin-bottom: 20px;
  color: var(--text);
}

/* ── STATES ────────────────────────────────────────────────────────────── */
.empty {
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 16px 0;
  text-align: center;
}
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
}
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-state {
  color: var(--expense);
  padding: 20px;
  background: var(--expense-bg);
  border-radius: var(--radius);
}

/* ── RESPONSIVE ────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .summary-grid { grid-template-columns: 1fr 1fr; }
  .dash-grid { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .summary-grid { grid-template-columns: 1fr; }
  .main-content { padding: 16px; }
  .form-row { grid-template-columns: 1fr; }
}
