/* ─── CSS Variables ─────────────────────────────────────── */
:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface2: #f0ede6;
  --border: #e0dbd0;
  --text: #1a1814;
  --text-muted: #6b6560;
  --accent: #e8572a;
  --accent2: #f0a500;
  --accent3: #2a7ae8;
  --green: #1db87c;
  --red: #e84040;
  --shadow: 0 4px 24px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 48px rgba(0,0,0,0.14);
  --radius: 20px;
  --radius-sm: 12px;
  --font-head: 'Syne', sans-serif;
  --font-body: 'DM Sans', sans-serif;
  --transition: 0.25s cubic-bezier(0.4,0,0.2,1);
}

[data-theme="dark"] {
  --bg: #111009;
  --surface: #1c1a14;
  --surface2: #252218;
  --border: #2e2b20;
  --text: #f0ece2;
  --text-muted: #8a8476;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 48px rgba(0,0,0,0.6);
}

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

html { scroll-behavior: smooth; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background var(--transition), color var(--transition);
  overflow-x: hidden;
}

/* Grain texture overlay */
.grain {
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  opacity: 0.035;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  background-size: 200px 200px;
}

/* ─── Header ────────────────────────────────────────────── */
.app-header {
  position: sticky; top: 0; z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  transition: background var(--transition);
}

.header-inner {
  max-width: 900px; margin: 0 auto;
  padding: 16px 20px;
  display: flex; align-items: center; justify-content: space-between;
}

.logo {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--font-head);
  font-weight: 800; font-size: 1.25rem;
}

.logo-icon {
  color: var(--accent);
  font-size: 1.5rem;
  animation: spin 8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.logo em {
  font-style: normal;
  color: var(--accent);
}

.theme-toggle {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--surface);
  cursor: pointer; font-size: 1.1rem;
  display: flex; align-items: center; justify-content: center;
  transition: transform var(--transition), background var(--transition);
  color: var(--text);
}
.theme-toggle:hover { transform: rotate(20deg) scale(1.1); }

/* ─── Main Layout ───────────────────────────────────────── */
.app-main {
  max-width: 900px; margin: 0 auto;
  padding: 28px 20px 60px;
  display: flex; flex-direction: column; gap: 20px;
  position: relative; z-index: 1;
}

/* ─── Balance Card ──────────────────────────────────────── */
.balance-card {
  background: var(--text);
  color: var(--bg);
  border-radius: var(--radius);
  padding: 40px 36px;
  position: relative; overflow: hidden;
  box-shadow: var(--shadow-lg);
  animation: slideDown 0.5s cubic-bezier(0.4,0,0.2,1);
}
@keyframes slideDown { from { opacity:0; transform:translateY(-20px); } to { opacity:1; transform:none; } }

.balance-blob {
  position: absolute; right: -60px; top: -60px;
  width: 240px; height: 240px;
  background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
  opacity: 0.2; border-radius: 50%;
  animation: pulse 4s ease-in-out infinite;
}
@keyframes pulse { 0%,100%{transform:scale(1)} 50%{transform:scale(1.15)} }

.balance-label {
  font-family: var(--font-head);
  font-size: 0.7rem; font-weight: 700;
  letter-spacing: 0.18em; opacity: 0.6;
  margin-bottom: 10px;
}

.balance-amount {
  font-family: var(--font-head);
  font-size: clamp(2.4rem, 8vw, 3.6rem);
  font-weight: 800; line-height: 1;
  color: var(--accent2);
  transition: all 0.4s cubic-bezier(0.4,0,0.2,1);
}

.balance-sub {
  margin-top: 10px;
  font-size: 0.85rem; opacity: 0.55;
}

/* ─── Generic Card ──────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
  transition: background var(--transition), border-color var(--transition);
  animation: fadeUp 0.4s cubic-bezier(0.4,0,0.2,1) both;
}
@keyframes fadeUp { from { opacity:0; transform:translateY(16px); } to { opacity:1; transform:none; } }

.card-title {
  font-family: var(--font-head);
  font-size: 1.05rem; font-weight: 700;
  margin-bottom: 20px;
  display: flex; align-items: center; gap: 8px;
}

/* ─── Form ──────────────────────────────────────────────── */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }

label {
  font-size: 0.75rem; font-weight: 500;
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-muted);
}

input, select {
  font-family: var(--font-body);
  font-size: 0.95rem;
  padding: 12px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(232,87,42,0.12);
}
input::placeholder { color: var(--text-muted); opacity: 0.6; }

.select-wrapper { position: relative; }
.select-wrapper select { appearance: none; padding-right: 36px; cursor: pointer; }
.select-arrow {
  position: absolute; right: 14px; top: 50%; transform: translateY(-50%);
  color: var(--text-muted); pointer-events: none; font-size: 0.85rem;
}

.error-msg {
  font-size: 0.82rem; color: var(--red);
  min-height: 20px; margin-top: 2px;
  transition: opacity 0.2s;
}

.btn-add {
  width: 100%; margin-top: 18px;
  padding: 15px;
  background: var(--accent);
  color: #fff;
  border: none; border-radius: var(--radius-sm);
  font-family: var(--font-head);
  font-size: 0.95rem; font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
  box-shadow: 0 4px 16px rgba(232,87,42,0.3);
}
.btn-add:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(232,87,42,0.4); }
.btn-add:active { transform: translateY(0); }

/* ─── Tab Row ───────────────────────────────────────────── */
.toggle-row {
  display: flex; gap: 8px;
}
.tab-btn {
  flex: 1; padding: 11px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text-muted);
  font-family: var(--font-head); font-weight: 600; font-size: 0.85rem;
  cursor: pointer;
  transition: all var(--transition);
}
.tab-btn.active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

/* ─── Two Column ────────────────────────────────────────── */
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

/* ─── List Card ─────────────────────────────────────────── */
.list-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px;
}
.list-header .card-title { margin-bottom: 0; }
.sort-row { display: flex; align-items: center; gap: 8px; font-size: 0.8rem; color: var(--text-muted); }
.sort-row select { padding: 6px 10px; font-size: 0.78rem; border-radius: 8px; }

.transaction-list {
  display: flex; flex-direction: column; gap: 10px;
  max-height: 360px; overflow-y: auto;
  padding-right: 4px;
}
.transaction-list::-webkit-scrollbar { width: 4px; }
.transaction-list::-webkit-scrollbar-track { background: transparent; }
.transaction-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* Transaction Item */
.txn-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  transition: transform var(--transition), box-shadow var(--transition);
  animation: itemIn 0.3s cubic-bezier(0.4,0,0.2,1) both;
}
@keyframes itemIn { from { opacity:0; transform:translateX(-12px); } to { opacity:1; transform:none; } }
.txn-item:hover { transform: translateX(4px); box-shadow: var(--shadow); }

.txn-left { display: flex; flex-direction: column; gap: 3px; }
.txn-name { font-weight: 500; font-size: 0.9rem; }
.txn-amount { font-family: var(--font-head); font-weight: 700; font-size: 0.95rem; color: var(--accent); }
.txn-badge {
  display: inline-block; margin-top: 3px;
  padding: 2px 8px;
  font-size: 0.68rem; font-weight: 600; letter-spacing: 0.05em;
  border-radius: 99px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-muted);
}

.txn-over .txn-amount { color: var(--red); }
.txn-over { border-left-color: var(--red); }

.btn-delete {
  padding: 7px 13px;
  background: var(--red);
  color: #fff; border: none; border-radius: 8px;
  font-size: 0.75rem; font-weight: 600;
  cursor: pointer; flex-shrink: 0;
  transition: transform var(--transition), opacity var(--transition);
}
.btn-delete:hover { transform: scale(1.08); opacity: 0.88; }

/* Empty state */
.empty-state {
  text-align: center; padding: 40px 0;
  color: var(--text-muted);
}
.empty-icon { font-size: 2.4rem; display: block; margin-bottom: 10px; }

/* Monthly Summary */
.monthly-summary { display: flex; flex-direction: column; gap: 10px; max-height: 360px; overflow-y: auto; }
.month-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; background: var(--surface2);
  border-radius: var(--radius-sm);
}
.month-name { font-weight: 600; font-size: 0.9rem; }
.month-total { font-family: var(--font-head); font-weight: 700; color: var(--accent); }

/* ─── Chart Card ────────────────────────────────────────── */
.chart-card { display: flex; flex-direction: column; }
.chart-wrapper { position: relative; flex: 1; min-height: 240px; }
.chart-empty {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 0.85rem;
}

/* ─── Limit Banner ──────────────────────────────────────── */
.limit-banner {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(100px);
  background: var(--red); color: #fff;
  padding: 14px 24px; border-radius: 99px;
  font-size: 0.88rem; font-weight: 500;
  display: flex; align-items: center; gap: 14px;
  box-shadow: 0 8px 32px rgba(232,64,64,0.4);
  transition: transform 0.4s cubic-bezier(0.4,0,0.2,1);
  z-index: 200; white-space: nowrap;
}
.limit-banner.show { transform: translateX(-50%) translateY(0); }
.limit-banner button {
  background: rgba(255,255,255,0.25); border: none; color: #fff;
  width: 22px; height: 22px; border-radius: 50%; cursor: pointer;
  font-size: 0.75rem;
}

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }
  .field.full { grid-column: 1; }
  .two-col { grid-template-columns: 1fr; }
  .balance-card { padding: 28px 24px; }
}