:root {
  --bg: #0f0f13;
  --bg-card: #1a1a22;
  --bg-elevated: #24242e;
  --bg-input: #2a2a36;
  --text: #f0f0f5;
  --text-secondary: #a0a0b0;
  --text-muted: #6b6b7b;
  --accent: #7c5cff;
  --accent-soft: rgba(124, 92, 255, 0.18);
  --success: #34d399;
  --success-soft: rgba(52, 211, 153, 0.15);
  --warning: #fbbf24;
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.15);
  --border: rgba(255, 255, 255, 0.06);
  --radius: 16px;
  --radius-sm: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav-height: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  overflow: hidden;
  overscroll-behavior: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

.header {
  padding: 16px 20px 12px;
  padding-top: max(16px, env(safe-area-inset-top));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header h1 {
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-actions { display: flex; gap: 8px; }

.date-line {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

.btn-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s;
}
.btn-icon.small { width: 32px; height: 32px; font-size: 1rem; }
.btn-icon:active { transform: scale(0.92); }
.ai-btn { background: rgba(52, 211, 153, 0.15); color: var(--success); }

.content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--nav-height) + var(--safe-bottom) + 20px);
}

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
}

.card-title { font-size: 1.05rem; font-weight: 600; margin-bottom: 4px; }
.card-meta { font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 10px; }

.progress-bar {
  height: 8px;
  background: var(--bg-input);
  border-radius: 4px;
  overflow: hidden;
  margin: 10px 0 6px;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #a78bfa);
  border-radius: 4px;
  transition: width 0.35s ease;
}
.progress-fill.danger { background: linear-gradient(90deg, var(--danger), #fb923c); }
.progress-text {
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: flex;
  justify-content: space-between;
}

.goal-actions, .row-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); opacity: 0.85; }
.btn-primary { background: var(--accent); color: #fff; flex: 1; }
.btn-secondary { background: var(--bg-elevated); color: var(--text); }
.btn-ghost { background: transparent; color: var(--text-secondary); padding: 8px 10px; }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-success { background: var(--success-soft); color: var(--success); }

/* Nested goals */
.goal-tree { margin-left: 0; }
.goal-node { position: relative; }
.goal-children {
  margin-left: 18px;
  border-left: 2px solid var(--border);
  padding-left: 12px;
  margin-top: 8px;
}
.goal-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 2px 6px;
}

/* Habits */
.habit-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.habit-item:last-child { border-bottom: none; }
.habit-check {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 2px solid var(--text-muted);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  transition: all 0.2s;
}
.habit-check.done {
  background: var(--success);
  border-color: var(--success);
  color: #0f0f13;
  animation: pop 0.25s ease;
}
.habit-check.bad-done {
  background: var(--danger);
  border-color: var(--danger);
}
.habit-info { flex: 1; min-width: 0; }
.habit-name { font-weight: 560; font-size: 1rem; }
.habit-streak { font-size: 0.75rem; color: var(--text-secondary); margin-top: 2px; }
.habit-streak strong { color: var(--warning); }
.habit-badge {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 600;
  margin-left: 6px;
}

/* Bad habit reduction */
.reduction-plan {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 6px;
  padding: 8px;
  background: var(--bg-input);
  border-radius: 8px;
}

/* Finance */
.finance-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.fin-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px;
  border: 1px solid var(--border);
  text-align: center;
}
.fin-value { font-size: 1.35rem; font-weight: 700; letter-spacing: -0.02em; }
.fin-value.positive { color: var(--success); }
.fin-value.negative { color: var(--danger); }
.fin-label { font-size: 0.75rem; color: var(--text-secondary); margin-top: 2px; }

.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item .amount { font-weight: 600; }
.list-item .amount.income { color: var(--success); }
.list-item .amount.expense { color: var(--danger); }

/* Journal */
.journal-entry { margin-bottom: 14px; }
.journal-date { font-size: 0.8rem; color: var(--accent); font-weight: 600; margin-bottom: 6px; }
.journal-text { font-size: 0.95rem; white-space: pre-wrap; line-height: 1.5; }
.mood-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.mood-btn {
  width: 40px; height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  font-size: 1.3rem;
  cursor: pointer;
  transition: all 0.15s;
}
.mood-btn.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: scale(1.08);
}

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  border: 1px solid var(--border);
  text-align: center;
}
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--accent); letter-spacing: -0.03em; }
.stat-label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 4px; }
.chart-container {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
  border: 1px solid var(--border);
  margin-bottom: 16px;
}
.chart-title { font-size: 0.95rem; font-weight: 600; margin-bottom: 12px; }

.empty {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}
.empty-icon { font-size: 2.5rem; margin-bottom: 12px; opacity: 0.6; }
.empty p { font-size: 0.95rem; line-height: 1.5; }

.section-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin: 18px 0 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: rgba(15, 15, 19, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 50;
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.68rem;
  font-weight: 500;
  padding: 8px 0;
  cursor: pointer;
  transition: color 0.2s;
}
.nav-item.active { color: var(--accent); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal.hidden { pointer-events: none; opacity: 0; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  transition: opacity 0.25s;
}
.modal:not(.hidden) .modal-backdrop { opacity: 1; }
.modal-sheet {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  background: var(--bg-elevated);
  border-radius: 20px 20px 0 0;
  padding: 12px 20px calc(24px + var(--safe-bottom));
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  overflow-y: auto;
}
.modal:not(.hidden) .modal-sheet { transform: translateY(0); }
.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--text-muted);
  border-radius: 2px;
  margin: 0 auto 16px;
  opacity: 0.5;
}
.modal h2 { font-size: 1.25rem; font-weight: 700; margin-bottom: 16px; }

.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
}
.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus,
.form-group textarea:focus { border-color: var(--accent); }
.form-group textarea { min-height: 100px; resize: vertical; }
.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }
.modal-actions { display: flex; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.modal-actions .btn { flex: 1; padding: 14px; min-width: 100px; }

/* AI Panel */
.ai-panel {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: var(--bg);
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
}
.ai-panel:not(.hidden) { transform: translateY(0); }
.ai-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  padding-top: max(16px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}
.ai-header h2 { font-size: 1.2rem; }
.ai-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.ai-msg {
  max-width: 85%;
  padding: 12px 14px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.45;
}
.ai-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.ai-msg.bot {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.ai-msg.bot .actions { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.ai-input-row {
  display: flex;
  gap: 8px;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.ai-input-row input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
}
.ai-input-row .btn { padding: 12px 18px; border-radius: 12px; }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 200;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  white-space: nowrap;
  border: 1px solid var(--border);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.goal-done .card-title { text-decoration: line-through; color: var(--text-secondary); }
.goal-done .progress-fill { background: var(--success); }

.flex-between { display: flex; justify-content: space-between; align-items: center; }
.badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

@keyframes pop {
  0% { transform: scale(0.8); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.settings-section {
  margin-bottom: 20px;
}
.settings-section h3 {
  font-size: 0.95rem;
  margin-bottom: 10px;
  color: var(--text-secondary);
}
