*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-hover: #22252f;
  --border: #2a2d3a;
  --text: #e4e4e7;
  --text-muted: #9ca3af;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --fee-red: #fca5a5;
  --orange: #f97316;
}

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

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

h1 {
  font-size: 1.75rem;
  font-weight: 700;
}

/* Buttons */
.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

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

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

.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger:hover { background: var(--danger-hover); }

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem;
  font-size: 1.1rem;
  transition: color 0.15s;
}
.btn-icon:hover { color: var(--danger); }

/* Dashboard */
.dashboard {
  max-width: 1200px;
  margin: 0 auto 1.5rem;
}

.dashboard-stats {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.stat-card {
  flex: 1;
  min-width: 140px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.stat-card.stat-total {
  border-color: var(--primary);
  border-width: 2px;
}

.stat-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.stat-value {
  font-size: 1.35rem;
  font-weight: 800;
}

.stat-interest { color: var(--orange); }
.stat-paid { color: var(--green); }

.stat-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.dashboard-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.chart-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem;
  position: relative;
}

.chart-container h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--text-muted);
}

.chart-container canvas {
  max-height: 200px;
}

.chart-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 2rem 0;
}
.chart-empty.hidden { display: none; }

/* Sort Bar */
.sort-bar {
  max-width: 1200px;
  margin: 0 auto 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sort-label {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sort-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
}
.sort-select:focus {
  outline: none;
  border-color: var(--primary);
}

/* Debt Grid */
.debts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 1rem;
  max-width: 1200px;
  margin: 0 auto;
}

/* Debt Card */
.debt-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem;
  transition: border-color 0.15s;
}
.debt-card:hover { border-color: var(--primary); }

.debt-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
}

.debt-name {
  font-size: 1.1rem;
  font-weight: 600;
}

.badge-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.apr-badge {
  background: rgba(59, 130, 246, 0.15);
  color: var(--primary);
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}

.due-badge {
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  background: rgba(156, 163, 175, 0.15);
  color: var(--text-muted);
}

.due-badge.due-warning {
  background: rgba(239, 68, 68, 0.2);
  color: var(--red);
  animation: pulse-due 2s ease-in-out infinite;
}

@keyframes pulse-due {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.debt-balance {
  font-size: 2rem;
  font-weight: 800;
  margin-bottom: 0.5rem;
}

.debt-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.payoff-date {
  font-weight: 600;
}
.payoff-green { color: var(--green); }
.payoff-yellow { color: var(--yellow); }
.payoff-red { color: var(--red); }

/* Payment Form */
.payment-form {
  display: none;
  gap: 0.5rem;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}
.payment-form.open { display: flex; flex-wrap: wrap; }

.payment-form input {
  flex: 1;
  min-width: 100px;
  padding: 0.4rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.85rem;
}
.payment-form input:focus {
  outline: none;
  border-color: var(--primary);
}

.debt-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

/* Payment History */
.payment-history {
  margin-top: 0.75rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

.payment-history-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0;
  margin-bottom: 0.5rem;
}
.payment-history-toggle:hover { color: var(--text); }

.payment-list {
  display: none;
  list-style: none;
  max-height: 200px;
  overflow-y: auto;
}
.payment-list.open { display: block; }

.payment-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0;
  font-size: 0.8rem;
  border-bottom: 1px solid var(--border);
}
.payment-item:last-child { border-bottom: none; }

.payment-amount {
  font-weight: 600;
}
.payment-amount.positive { color: var(--green); }
.payment-amount.negative { color: var(--fee-red); }

.payment-note {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.payment-date {
  color: var(--text-muted);
  font-size: 0.75rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  width: 90%;
  max-width: 400px;
}

.modal-content h2 {
  margin-bottom: 1rem;
  font-size: 1.25rem;
}

.modal-content label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.modal-content input {
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.95rem;
}
.modal-content input:focus {
  outline: none;
  border-color: var(--primary);
}

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

/* Empty State */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  grid-column: 1 / -1;
}
.empty-state p {
  margin-top: 0.5rem;
  font-size: 0.9rem;
}

/* Responsive */
@media (max-width: 768px) {
  .dashboard-charts { grid-template-columns: 1fr; }
  .dashboard-stats { flex-direction: column; }
}

@media (max-width: 600px) {
  body { padding: 1rem; }
  header { flex-direction: column; gap: 1rem; align-items: flex-start; }
  .debts-grid { grid-template-columns: 1fr; }
  .debt-balance { font-size: 1.5rem; }
}
