/* ---------- Base ---------- */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f9fafc;
  --border: #e6e9f1;
  --text: #1a2233;
  --text-soft: #5b6478;
  --text-muted: #8a93a6;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --primary-hover: #4338ca;
  --income: #10b981;
  --income-soft: #ecfdf5;
  --expense: #ef4444;
  --expense-soft: #fef2f2;
  --warning: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(20, 30, 60, 0.04);
  --shadow-md: 0 4px 16px rgba(20, 30, 60, 0.06);
  --shadow-lg: 0 12px 32px rgba(20, 30, 60, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-weight: 600;
  letter-spacing: -0.01em;
}

button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, select, button {
  font-family: inherit;
}

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
}

.brand h1 {
  font-size: 17px;
  line-height: 1.2;
}
.brand h1 span {
  color: var(--primary);
}
.tagline {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 2px;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-soft);
  font-weight: 500;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}
.nav-item:hover {
  background: var(--surface-2);
  color: var(--text);
}
.nav-item.active {
  background: var(--primary-soft);
  color: var(--primary);
}
.nav-item .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.5;
}
.nav-item.active .dot {
  opacity: 1;
}

.balance-card {
  margin-top: auto;
  background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
  color: white;
  padding: 18px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
}
.balance-label {
  font-size: 12px;
  opacity: 0.7;
  margin-bottom: 6px;
}
.balance-amount {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 16px;
}
.balance-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}
.balance-meta div {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.meta-label {
  font-size: 11px;
  opacity: 0.7;
}
.meta-value {
  font-size: 14px;
  font-weight: 600;
}
.meta-value.income { color: #6ee7b7; }
.meta-value.expense { color: #fca5a5; }

.sidebar-foot {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* ---------- Main ---------- */
.main {
  padding: 28px 36px;
  max-width: 1200px;
  width: 100%;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 24px;
}
.topbar h2 {
  font-size: 24px;
}
.subtitle {
  color: var(--text-muted);
  font-size: 13px;
  margin-top: 4px;
}
.topbar-actions {
  display: flex;
  gap: 8px;
}

.btn-ghost {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  color: var(--text-soft);
  font-weight: 500;
  font-size: 13px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-ghost:hover {
  background: var(--surface-2);
  color: var(--text);
}
.btn-ghost.danger:hover {
  background: var(--expense-soft);
  color: var(--expense);
  border-color: #fecaca;
}

.btn-primary {
  width: 100%;
  padding: 12px 16px;
  background: var(--primary);
  color: white;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  transition: background 0.15s, transform 0.05s;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:active { transform: translateY(1px); }

.btn-danger {
  padding: 9px 16px;
  background: var(--expense);
  color: white;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 13px;
}
.btn-danger:hover { background: #dc2626; }

.link-btn {
  color: var(--primary);
  font-size: 13px;
  font-weight: 500;
}
.link-btn:hover { text-decoration: underline; }

/* ---------- Views ---------- */
.view {
  display: none;
  flex-direction: column;
  gap: 20px;
}
.view.active { display: flex; }

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
.grid-2 {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow-sm);
}
.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 22px;
  font-weight: 600;
}
.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.stat-value {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.income { color: var(--income); }
.expense { color: var(--expense); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.card-head h3 {
  font-size: 15px;
}
.muted {
  color: var(--text-muted);
  font-size: 12.5px;
}

/* ---------- Form ---------- */
.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 4px;
  gap: 4px;
}
.seg-btn {
  padding: 8px 10px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-soft);
  transition: background 0.15s, color 0.15s;
}
.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.seg-btn[data-type="income"].active { color: var(--income); }
.seg-btn[data-type="expense"].active { color: var(--expense); }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field span {
  font-size: 12px;
  color: var(--text-soft);
  font-weight: 500;
}
.field input,
.field select {
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  transition: border-color 0.15s, background 0.15s;
}
.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--primary);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* ---------- Transaction list ---------- */
.tx-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 360px;
  overflow-y: auto;
}
.tx-list.full {
  max-height: none;
}
.tx-item {
  display: grid;
  grid-template-columns: 36px 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 10px 8px;
  border-radius: 10px;
  transition: background 0.15s;
}
.tx-item:hover { background: var(--surface-2); }
.tx-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 16px;
}
.tx-icon.income { background: var(--income-soft); color: var(--income); }
.tx-icon.expense { background: var(--expense-soft); color: var(--expense); }

.tx-main { min-width: 0; }
.tx-desc {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tx-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.tx-amount {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
}
.tx-amount.income { color: var(--income); }
.tx-amount.expense { color: var(--expense); }
.tx-delete {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  color: var(--text-muted);
  display: grid;
  place-items: center;
  font-size: 16px;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}
.tx-item:hover .tx-delete { opacity: 1; }
.tx-delete:hover { background: var(--expense-soft); color: var(--expense); }

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 12px;
  font-size: 13px;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.filters {
  display: flex;
  gap: 6px;
}
.chip {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-soft);
  font-size: 13px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.chip:hover { background: var(--surface-2); }
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

#searchInput {
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  width: 240px;
  color: var(--text);
}
#searchInput:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ---------- Analytics ---------- */
.category-chart {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.bar-row {
  display: grid;
  grid-template-columns: 110px 1fr 90px;
  align-items: center;
  gap: 12px;
}
.bar-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-track {
  height: 10px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
  position: relative;
}
.bar-fill {
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--primary), #7c3aed);
  transition: width 0.5s ease;
}
.bar-fill.income { background: linear-gradient(90deg, #10b981, #34d399); }
.bar-fill.expense { background: linear-gradient(90deg, #ef4444, #f87171); }
.bar-value {
  font-size: 13px;
  font-weight: 600;
  text-align: right;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 40, 0.45);
  display: grid;
  place-items: center;
  z-index: 100;
  animation: fade 0.15s ease;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: min(380px, 90vw);
  box-shadow: var(--shadow-lg);
}
.modal-card h3 {
  font-size: 16px;
  margin-bottom: 8px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ---------- Scrollbar ---------- */
.tx-list::-webkit-scrollbar { width: 6px; }
.tx-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
.tx-list::-webkit-scrollbar-thumb:hover { background: #d0d5e0; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    flex-direction: column;
  }
  .balance-card { margin-top: 0; }
  .main { padding: 20px; }
  .grid-3 { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .nav {
    flex-direction: row;
    overflow-x: auto;
  }
  .nav-item { white-space: nowrap; }
  .toolbar { flex-direction: column; align-items: stretch; }
  #searchInput { width: 100%; }
  .bar-row { grid-template-columns: 90px 1fr 70px; }
}
