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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f5f5f5;
  color: #333;
  line-height: 1.6;
  overflow-x: hidden;
}

/* Nav */
.nav {
  background: #2563eb;
  color: white;
  display: flex;
  align-items: center;
  padding: 0 1rem;
  height: 3rem;
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav-brand { font-weight: bold; font-size: 1.1rem; margin-right: auto; color: white; text-decoration: none; }
.nav-links { display: flex; gap: 0.25rem; }
.nav-links a {
  color: white;
  text-decoration: none;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  font-size: 0.85rem;
}
.nav-links a:hover { background: rgba(255,255,255,0.2); }
.nav-toggle { display: none; background: none; border: none; color: white; font-size: 1.5rem; cursor: pointer; }

@media (max-width: 640px) {
  .nav-links {
    display: none;
    position: absolute;
    top: 3rem;
    left: 0;
    right: 0;
    background: #2563eb;
    flex-direction: column;
    padding: 0.5rem;
  }
  .nav-links.open { display: flex; }
  .nav-toggle { display: block; }
}

/* Container */
.container {
  max-width: 48rem;
  margin: 0 auto;
  padding: 1rem;
  overflow-x: hidden;
}

/* Cards */
.card {
  background: white;
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.card h3 { margin-bottom: 0.75rem; font-size: 1rem; }

/* Stats Grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.stat-card {
  background: white;
  border-radius: 8px;
  padding: 1rem;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.stat-label { font-size: 0.8rem; color: #666; }
.stat-value { font-size: 1.3rem; font-weight: bold; }
@media (max-width: 480px) { .stat-value { font-size: 1.1rem; } }
.stat-change { font-size: 0.7rem; margin-top: 0.15rem; }
.stat-change.up { color: #dc2626; }
.stat-change.down { color: #16a34a; }

/* Colors */
.income { color: #16a34a; }
.expense { color: #dc2626; }

/* Table */
.table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.table th, .table td { padding: 0.5rem; text-align: left; border-bottom: 1px solid #eee; }
.table th { font-weight: 600; color: #666; font-size: 0.8rem; }
.table-wrap { overflow-x: auto; }

/* Buttons */
.btn {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  background: white;
  color: #333;
  text-decoration: none;
  font-size: 0.85rem;
  cursor: pointer;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: #2563eb; color: white; border-color: #2563eb; }
.btn-primary:hover { background: #1d4ed8; }
.btn-secondary { background: #f3f4f6; }
.btn-danger { color: #dc2626; border-color: #dc2626; }
.btn-danger:hover { background: #fef2f2; }
.btn-sm { padding: 0.2rem 0.5rem; font-size: 0.75rem; }
.btn-block { width: 100%; text-align: center; }

/* Forms */
.form-card { background: white; border-radius: 8px; padding: 1rem; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.25rem; font-size: 0.85rem; font-weight: 500; }
.form-group input, .form-group select {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}
input[type="date"] { appearance: auto; -webkit-appearance: auto; }
.input-lg { font-size: 1.5rem; padding: 0.7rem; text-align: center; letter-spacing: 0.05em; }
.radio-group { display: flex; gap: 1rem; }
.radio-label { display: flex; align-items: center; gap: 0.3rem; font-size: 0.95rem; }
.form-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.form-actions .btn { font-size: 1rem; padding: 0.6rem 1.2rem; }
.select-row { display: flex; gap: 0.5rem; align-items: center; }
.select-row select { flex: 1; }
.inline-add { display: flex; gap: 0.5rem; align-items: center; margin-top: 0.4rem; }
.inline-add input { flex: 1; padding: 0.4rem; border: 1px solid #d1d5db; border-radius: 4px; font-size: 0.9rem; }

/* Type toggle */
.type-toggle { display: flex; gap: 0; border: 1px solid #d1d5db; border-radius: 6px; overflow: hidden; }
.type-toggle .radio-label {
  flex: 1; justify-content: center; padding: 0.6rem 1rem; cursor: pointer;
  font-weight: 500; transition: all 0.15s;
}
.type-toggle .radio-label input { display: none; }
.type-toggle .type-expense { background: #fef2f2; }
.type-toggle .type-income { background: #f0fdf4; }
.type-toggle .type-expense.active { background: #dc2626; color: white; }
.type-toggle .type-income.active { background: #16a34a; color: white; }

/* Filters */
.filters { margin-bottom: 1rem; }
.filter-form { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: end; }
.filter-form input, .filter-form select { padding: 0.4rem; border: 1px solid #d1d5db; border-radius: 4px; font-size: 0.85rem; }
.filter-form input[type="month"] { width: auto; }

/* Inline form (categories) */
.inline-form { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.inline-form input[type="text"] { padding: 0.4rem; border: 1px solid #d1d5db; border-radius: 4px; flex: 1; min-width: 8rem; }
.inline-form select { padding: 0.4rem; border: 1px solid #d1d5db; border-radius: 4px; }

/* Category cards */
.category-card { margin-bottom: 0.75rem; }
.category-header { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; }
.category-actions { margin-left: auto; display: flex; gap: 0.25rem; }
.badge { font-size: 0.7rem; background: #e5e7eb; padding: 0.1rem 0.4rem; border-radius: 3px; }
.subcategory-list { padding-left: 1rem; }
.subcategory-item { display: flex; align-items: center; padding: 0.3rem 0; border-bottom: 1px solid #f3f4f6; }
.subcategory-item span { flex: 1; }
.edit-form { margin: 0.5rem 0; }

/* Tabs */
.tabs { display: flex; gap: 0; margin-bottom: 1rem; border-bottom: 2px solid #e5e7eb; }
.tab {
  padding: 0.5rem 1.2rem; text-decoration: none; color: #666; font-size: 0.9rem;
  border-bottom: 2px solid transparent; margin-bottom: -2px; transition: all 0.15s;
}
.tab:hover { color: #2563eb; }
.tab.active { color: #2563eb; border-bottom-color: #2563eb; font-weight: 500; }

/* Bar chart */
.chart-legend { display: flex; gap: 1rem; margin-bottom: 0.5rem; font-size: 0.75rem; color: #666; }
.legend-expense::before, .legend-income::before {
  content: ''; display: inline-block; width: 0.7rem; height: 0.7rem;
  border-radius: 2px; margin-right: 0.3rem; vertical-align: middle;
}
.legend-expense::before { background: #fca5a5; }
.legend-income::before { background: #86efac; }
.bar-chart {
  display: flex; align-items: flex-end; gap: 2px; height: 10rem;
  padding: 0.5rem 0;
}
.bar-col {
  flex: 1; min-width: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: flex-end; height: 100%;
}
.bar-pair { display: flex; gap: 1px; align-items: flex-end; width: 100%; height: 100%; }
.bar {
  flex: 1; border-radius: 2px 2px 0 0; min-height: 2px;
  transition: height 0.3s;
}
.bar-expense { background: #fca5a5; }
.bar-income { background: #86efac; }
.bar-label { font-size: 0.6rem; color: #999; margin-top: 2px; }

/* Rank list */
.rank-list { display: flex; flex-direction: column; gap: 0.4rem; }
.rank-item { display: flex; justify-content: space-between; align-items: center; padding: 0.5rem; background: #f9fafb; border-radius: 4px; }
.rank-item:hover { background: #f3f4f6; }
.rank-left { display: flex; align-items: center; gap: 0.5rem; }
.rank-name { font-size: 0.9rem; }
.rank-count { font-size: 0.7rem; color: #999; }
.rank-right { display: flex; align-items: center; gap: 0.5rem; }
.rank-amount { font-weight: 600; }
.rank-pct { font-size: 0.75rem; color: #999; }
.rank-group .sub-stats { padding-left: 1rem; }
.rank-list.sub { gap: 0.3rem; }
.rank-list.sub .rank-item { background: #f3f4f6; font-size: 0.85rem; }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #f5f5f5; }
.login-card {
  background: white;
  border-radius: 8px;
  padding: 2rem;
  width: 100%;
  max-width: 22rem;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.login-card h1 { text-align: center; margin-bottom: 1.5rem; color: #2563eb; }
.login-card label { display: block; margin-bottom: 0.25rem; font-size: 0.85rem; }
.login-card input {
  width: 100%;
  padding: 0.6rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  margin-bottom: 1rem;
  font-size: 1rem;
}

/* Alert */
.alert { padding: 0.6rem; border-radius: 4px; margin-bottom: 1rem; font-size: 0.85rem; }
.alert-error { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }

/* Empty state */
.empty { text-align: center; color: #999; padding: 2rem 0; }

/* Floating action button */
.fab {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 3.2rem;
  height: 3.2rem;
  border-radius: 50%;
  background: #2563eb;
  color: white;
  font-size: 1.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  box-shadow: 0 2px 8px rgba(37,99,235,0.4);
  z-index: 90;
  transition: transform 0.15s;
}
.fab:hover { transform: scale(1.1); background: #1d4ed8; }
.fab-back { background: #6b7280; box-shadow: 0 2px 8px rgba(107,114,128,0.4); }
.fab-back:hover { background: #4b5563; }

/* HTMX loading indicator */
.htmx-indicator {
  display: none;
  position: fixed;
  top: 3rem;
  left: 0;
  right: 0;
  height: 3px;
  background: #2563eb;
  z-index: 101;
  animation: loading 1s ease-in-out infinite;
}
@keyframes loading {
  0% { opacity: 0.3; }
  50% { opacity: 1; }
  100% { opacity: 0.3; }
}

/* Toast notification */
.toast {
  position: fixed;
  bottom: 5.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(1rem);
  background: #1f2937;
  color: white;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-size: 0.85rem;
  opacity: 0;
  transition: all 0.3s;
  z-index: 200;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Mobile card layout for transaction list */
@media (max-width: 640px) {
  .table { display: none; }
  .tx-card {
    background: white;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    margin-bottom: 0.5rem;
    box-shadow: 0 1px 2px rgba(0,0,0,0.06);
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }
  .tx-card-main { flex: 1; min-width: 0; }
  .tx-card-top { display: flex; justify-content: space-between; align-items: center; }
  .tx-card-amount { font-size: 1.1rem; font-weight: 600; }
  .tx-card-meta { font-size: 0.75rem; color: #999; margin-top: 0.15rem; }
  .tx-card-actions { display: flex; gap: 0.25rem; }
  .tx-card-actions .btn { padding: 0.3rem 0.5rem; font-size: 0.75rem; }
}
@media (min-width: 641px) {
  .tx-card { display: none; }
}
