@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Space+Grotesk:wght@700;800&display=swap');

:root {
  --bg: #f8fafc;
  --bg-card: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --primary: #ffc451;
  --primary-dark: #e6b048;
  --success: #10b981;
  --danger: #ef4444;
  --border: #e2e8f0;
  --radius: 0.75rem;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --bg-card: #1a1d27;
    --text: #f1f1f1;
    --text-muted: #9ca3af;
    --border: #2a2d3e;
  }
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', sans-serif;
  margin: 0;
  min-height: 100vh;
}

.header {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header h1 {
  font-size: 1.1rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  color: var(--text);
}

.header-sub {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 400;
}

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

.month-select {
  padding: 0.375rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--text);
  font-size: 0.78rem;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  outline: none;
}

.month-select:focus {
  border-color: var(--primary);
}

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

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.summary-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  box-shadow: var(--shadow);
  transition: transform 0.15s;
}

.summary-card:hover {
  transform: translateY(-2px);
}

.summary-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.375rem;
}

.summary-value {
  font-size: 1.5rem;
  font-weight: 800;
  font-family: 'Space Grotesk', sans-serif;
  line-height: 1.1;
}

.summary-value.income  { color: var(--success); }
.summary-value.expense { color: var(--danger); }
.summary-value.balance { color: var(--primary-dark); }

.summary-sub {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.main-grid {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 1.5rem;
}

.left-col {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* Form Card */
.form-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.form-card h2 {
  font-size: 0.9rem;
  font-weight: 700;
  margin: 0 0 1rem;
  color: var(--text);
}

.form-group {
  margin-bottom: 0.875rem;
}

label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

input[type="text"],
input[type="number"],
input[type="date"],
select {
  width: 100%;
  padding: 0.625rem 0.875rem;
  border: 1.5px solid var(--border);
  border-radius: 0.5rem;
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.85rem;
  box-sizing: border-box;
  outline: none;
  transition: border-color 0.15s;
  font-family: 'Inter', sans-serif;
  -webkit-appearance: none;
  appearance: none;
}

input[type="text"]:focus,
input[type="number"]:focus,
input[type="date"]:focus,
select:focus {
  border-color: var(--primary);
}

.type-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-bottom: 0.875rem;
}

.type-btn {
  padding: 0.625rem;
  border: 1.5px solid var(--border);
  border-radius: 0.5rem;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  font-family: 'Inter', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
}

.type-btn.income.active {
  background: rgba(16, 185, 129, 0.1);
  border-color: var(--success);
  color: var(--success);
}

.type-btn.expense.active {
  background: rgba(239, 68, 68, 0.1);
  border-color: var(--danger);
  color: var(--danger);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.btn-submit {
  width: 100%;
  padding: 0.75rem;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: 0.5rem;
  font-weight: 700;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background 0.15s;
  font-family: 'Inter', sans-serif;
  margin-top: 0.25rem;
}

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

/* Transactions */
.transactions-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

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

.transactions-header h2 {
  font-size: 0.9rem;
  font-weight: 700;
  margin: 0;
}

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

.tx-count {
  font-size: 0.7rem;
  color: var(--text-muted);
  background: var(--border);
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
}

.clear-btn {
  font-size: 0.72rem;
  color: var(--danger);
  background: none;
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  padding: 0;
}

.clear-btn:hover {
  text-decoration: underline;
}

.transaction-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.transaction-item:last-child { border-bottom: none; }

.tx-icon {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  flex-shrink: 0;
}

.tx-icon.income  { background: rgba(16, 185, 129, 0.15); color: var(--success); }
.tx-icon.expense { background: rgba(239, 68, 68, 0.15);  color: var(--danger); }

.tx-info { flex: 1; min-width: 0; }

.tx-name {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tx-meta {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.tx-category {
  font-size: 0.7rem;
  color: var(--text-muted);
}

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

.tx-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.tx-amount {
  font-weight: 700;
  font-size: 0.875rem;
  font-family: 'Space Grotesk', sans-serif;
}

.tx-amount.income  { color: var(--success); }
.tx-amount.expense { color: var(--danger); }

.tx-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.875rem;
  padding: 0.25rem;
  opacity: 0.5;
  transition: opacity 0.15s, color 0.15s;
  line-height: 1;
}

.tx-delete:hover {
  opacity: 1;
  color: var(--danger);
}

/* Right column */
.right-col {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.chart-card canvas {
  width: 100% !important;
  height: 220px !important;
}

.chart-card h2 {
  font-size: 0.9rem;
  font-weight: 700;
  margin: 0 0 1rem;
  color: var(--text);
}

.chart-legend {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 1rem;
}

.legend-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.72rem;
}

.legend-left {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  color: var(--text-muted);
}

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

.legend-amount {
  font-weight: 700;
  color: var(--text);
  font-family: 'Space Grotesk', sans-serif;
}

/* Tips card */
.tips-card {
  background: linear-gradient(135deg, rgba(255,196,81,0.08), rgba(255,196,81,0.03));
  border: 1px solid rgba(255,196,81,0.2);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.tips-card h3 {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary-dark);
  margin: 0 0 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.tip-item {
  font-size: 0.72rem;
  color: var(--text-muted);
  padding: 0.375rem 0;
  border-bottom: 1px solid rgba(255,196,81,0.1);
  line-height: 1.5;
}

.tip-item:last-child { border-bottom: none; }

.empty-state {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
  font-size: 0.82rem;
}

.empty-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  opacity: 0.4;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: #1a1d27;
  color: #f1f1f1;
  border: 1px solid #2a2d3e;
  border-radius: var(--radius);
  padding: 0.75rem 1.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  z-index: 9999;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  transform: translateY(0);
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
}

@media (max-width: 700px) {
  .summary-grid { grid-template-columns: 1fr; }
  .main-grid    { grid-template-columns: 1fr; }
  .form-row     { grid-template-columns: 1fr; }
  .header-left  { gap: 0.5rem; flex-wrap: wrap; }
  .container    { padding: 0 0.875rem 2rem; }
  .header       { padding: 0.875rem 0.875rem; flex-wrap: wrap; gap: 0.625rem; }
  .header-right { gap: 0.5rem; flex-wrap: wrap; }
  .summary-card { padding: 1rem; }
  .summary-value { font-size: 1.5rem; }
  .form-card, .transactions-card, .chart-card, .goals-card, .tips-card {
    padding: 1rem;
  }
  .transactions-header { flex-wrap: wrap; gap: 0.5rem; }
  .header-actions { gap: 0.375rem; }
  .tx-filter-bar { flex-direction: column; gap: 0.5rem; }
  .tx-sort-select { width: 100%; }
  .tx-search-wrap { width: 100%; }
  .tx-search-input { width: 100%; }
  .month-select   { font-size: 0.78rem; }
}

@media (max-width: 420px) {
  .type-btns { gap: 0.375rem; }
  .type-btn  { padding: 0.5rem 0.75rem; font-size: 0.8rem; }
  .summary-grid { gap: 0.625rem; }
  .back-to-main { font-size: 0.72rem; padding: 0.3rem 0.625rem; }
}

/* ── Back-to-main button ─────────────────────────────────────────────────── */
.back-to-main {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .375rem .875rem;
  border: 1.5px solid rgba(255,196,81,0.3);
  border-radius: .5rem;
  font-size: .8125rem;
  font-weight: 600;
  color: #ffc451;
  background: rgba(255,196,81,0.08);
  text-decoration: none;
  transition: all .2s;
  white-space: nowrap;
}
.back-to-main:hover { background: #ffc451; color: #000; }

/* ── Header layout with left group ──────────────────────────────────────── */
.header-left {
  display: flex;
  align-items: center;
  gap: 0.875rem;
}

/* ── Legend percentage ───────────────────────────────────────────────────── */
.legend-pct {
  font-size: 0.65rem;
  color: var(--text-muted);
  opacity: 0.75;
  margin-left: 0.15rem;
}

/* ── Transaction filter bar (search + sort) ──────────────────────────────── */
.tx-filter-bar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.875rem;
  flex-wrap: wrap;
}

.tx-search-wrap {
  position: relative;
  flex: 1;
  min-width: 140px;
}

.tx-search-icon {
  position: absolute;
  left: 0.65rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.tx-search-input {
  width: 100%;
  padding: 0.45rem 0.75rem 0.45rem 2rem;
  border: 1.5px solid var(--border);
  border-radius: 0.45rem;
  background: var(--bg);
  color: var(--text);
  font-size: 0.78rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}
.tx-search-input::placeholder { color: var(--text-muted); }
.tx-search-input:focus { border-color: var(--primary); }

.tx-sort-select {
  padding: 0.45rem 0.75rem;
  border: 1.5px solid var(--border);
  border-radius: 0.45rem;
  background: var(--bg);
  color: var(--text);
  font-size: 0.75rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.15s;
  width: auto;
}
.tx-sort-select:focus { border-color: var(--primary); }

/* ── Icon-text button (CSV export in transactions header) ────────────────── */
.icon-text-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  transition: all 0.15s;
}
.icon-text-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

/* ── Budget Goals card ───────────────────────────────────────────────────── */
.goals-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.goals-card h2 {
  font-size: 0.9rem;
  font-weight: 700;
  margin: 0 0 0.25rem;
  color: var(--text);
}

.goals-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin: 0 0 1rem;
}

/* Goal form */
.goal-form {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.goal-cat-select {
  flex: 1;
  min-width: 120px;
  padding: 0.45rem 0.625rem;
  border: 1.5px solid var(--border);
  border-radius: 0.45rem;
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.78rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.15s;
}
.goal-cat-select:focus { border-color: var(--primary); }

.goal-amount-input {
  width: 130px;
  padding: 0.45rem 0.625rem;
  border: 1.5px solid var(--border);
  border-radius: 0.45rem;
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.78rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  transition: border-color 0.15s;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}
.goal-amount-input::placeholder { color: var(--text-muted); }
.goal-amount-input:focus { border-color: var(--primary); }

.goal-set-btn {
  padding: 0.45rem 0.875rem;
  background: var(--primary);
  color: #000;
  border: none;
  border-radius: 0.45rem;
  font-weight: 700;
  font-size: 0.78rem;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.goal-set-btn:hover { background: var(--primary-dark); }

/* Goals list */
.goals-list { display: flex; flex-direction: column; gap: 0.625rem; }

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

.goal-item {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  position: relative;
  padding-right: 1.5rem;
}

.goal-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.goal-cat {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
}

.goal-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.goal-over {
  color: #f87171;
  font-weight: 700;
}

.goal-bar-track {
  height: 5px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.goal-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.4s ease;
}

.goal-remove {
  position: absolute;
  top: 0;
  right: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  opacity: 0.5;
  transition: opacity 0.15s, color 0.15s;
}
.goal-remove:hover { opacity: 1; color: var(--danger); }
