:root {
  --accent: #4f46e5;
  --accent-dark: #4338ca;
  --accent-soft: #eef2ff;
  --good: #16a34a;
  --good-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .06);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); text-decoration: none; }
h1 { font-size: 1.35rem; margin: 0; }
h2 { font-size: 1.02rem; margin: 0 0 12px; }
p { margin: .4em 0; }

.hidden { display: none !important; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; margin: 0; }
.hint { color: var(--muted); font-size: .86rem; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 10px;
  background: var(--accent);
  color: #fff;
  padding: 10px 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.18);
  min-height: 52px;
}
.brand { color: #fff; font-weight: 700; font-size: 1.02rem; letter-spacing: .2px; }
.icon-btn {
  background: rgba(255,255,255,.16);
  border: none; color: #fff;
  width: 40px; height: 40px; border-radius: 10px;
  font-size: 1.15rem; cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn:active { background: rgba(255,255,255,.3); }
.topbar-right { margin-left: auto; }
.user-chip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 50%;
  background: #fff; color: var(--accent);
  font-weight: 800; font-size: 1rem;
}

/* ---------- Drawer ---------- */
.nav-overlay {
  position: fixed; inset: 0; z-index: 49;
  background: rgba(15,23,42,.45);
}
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 50;
  width: min(82vw, 300px);
  background: var(--card);
  box-shadow: 4px 0 20px rgba(0,0,0,.2);
  display: flex; flex-direction: column;
  transform: translateX(-105%);
  transition: transform .22s ease;
  overflow-y: auto;
}
.drawer.open { transform: translateX(0); }
.drawer ul { list-style: none; margin: 8px 0; padding: 0; }
.drawer ul a {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 18px; color: var(--text);
  font-weight: 600; border-left: 3px solid transparent;
}
.drawer ul a.active { background: var(--accent-soft); color: var(--accent-dark); border-left-color: var(--accent); }
.drawer ul a:active { background: var(--bg); }
.drawer-logout {
  margin: auto 0 14px; padding: 12px 18px; color: var(--bad); font-weight: 600;
}

/* ---------- Layout ---------- */
.container { max-width: 1020px; margin: 0 auto; padding: 14px 12px 40px; }
.page-head { margin: 4px 0 12px; }
.page-sub { color: var(--muted); margin: 2px 0 0; font-size: .9rem; }

.app-footer {
  text-align: center; color: var(--muted);
  font-size: .8rem; padding: 18px 12px 26px;
}

/* ---------- Cards & grids ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.grid-2 { display: grid; gap: 14px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.stat-box.span-2 { grid-column: span 2; }
.stat-box {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
}
.stat-box.stat-balance { background: linear-gradient(135deg, var(--accent), #7c3aed); border: none; color: #fff; }
.stat-box.stat-balance .stat-label { color: rgba(255,255,255,.85); }
.stat-label { font-size: .76rem; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); display: block; }
.stat-balance .stat-label { color: rgba(255,255,255,.85) !important; }
.stat-value { font-size: 1.35rem; font-weight: 800; display: block; margin-top: 4px; }
.stat-hint { font-size: .7rem; color: var(--muted); display: block; margin-top: 2px; }
.stat-negative { color: var(--bad); }
.stat-positive { color: var(--good); }
.stat-balance .stat-value { color: #fff; }

/* ---------- Cards inner (small chart area) ---------- */
.chart-box { position: relative; height: 260px; margin: 6px 0; }
.legend-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg); padding: 4px 10px; border-radius: 999px;
  font-size: .8rem; color: var(--text);
}
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }

/* ---------- Forms ---------- */
.form-stack { display: grid; gap: 12px; }
.form-group { display: grid; gap: 5px; }
.form-group label { font-size: .82rem; font-weight: 700; color: var(--muted); }
.form-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; }
.text-input {
  width: 100%; padding: 12px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  font-size: 1rem; background: #fff; color: var(--text);
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
select.text-input { appearance: none; -webkit-appearance: none; padding-right: 34px; }
.color-input { width: 64px; height: 46px; border: 1px solid var(--border); border-radius: 10px; padding: 4px; background: #fff; }

.btn-primary {
  background: var(--accent); color: #fff; border: none;
  border-radius: 10px; padding: 13px 16px;
  font-size: 1rem; font-weight: 700; cursor: pointer;
  transition: background .15s;
}
.btn-primary:active { background: var(--accent-dark); }
.btn-block { width: 100%; }
.btn-danger {
  background: transparent; color: var(--bad);
  border: 1px solid var(--bad); border-radius: 8px;
  padding: 6px 12px; font-size: .8rem; font-weight: 700; cursor: pointer;
}

.btn-ghost {
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 8px;
  padding: 6px 12px; font-size: .8rem; font-weight: 700; cursor: pointer;
}
.btn-secondary {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 16px; font-size: 1rem; font-weight: 700; cursor: pointer;
}

.row-actions { display: flex; gap: 6px; flex: 0 0 auto; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(15,23,42,.55);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-card {
  background: var(--card); width: 100%;
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-height: 88vh; overflow-y: auto;
  animation: modal-up .18s ease;
}
@keyframes modal-up {
  from { transform: translateY(30px); opacity: .6; }
  to { transform: translateY(0); opacity: 1; }
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head h3 { margin: 0; }
.modal-close {
  background: var(--bg); border: none; border-radius: 8px;
  width: 34px; height: 34px; font-size: 1.1rem; cursor: pointer; color: var(--muted);
}
.modal-body { display: grid; gap: 12px; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn-primary, .modal-actions .btn-secondary { flex: 1; }
.modal-actions .modal-link { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; text-align: center; }

@media (min-width: 720px) {
  .modal-overlay { align-items: center; }
  .modal-card { width: 440px; max-width: 92vw; border-radius: 16px; }
}

.quick-form { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }
.quick-form .btn-primary { grid-column: 1 / -1; }
.quick-amount { position: relative; display: flex; align-items: center; }
.quick-amount .currency-symbol {
  position: absolute; left: 12px; font-weight: 700; color: var(--muted); pointer-events: none;
}
.quick-amount input {
  width: 100%; padding: 12px 12px 12px 30px;
  border: 1px solid var(--border); border-radius: 10px; font-size: 1.05rem; font-weight: 700;
}
.amount-group { display: flex; align-items: center; gap: 8px; }

/* ---------- Lists ---------- */
.list { display: grid; gap: 8px; max-height: 520px; overflow-y: auto; }
.list-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg); border-radius: 10px; padding: 10px 12px;
}
.list-icon {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem; flex: 0 0 auto; color: #fff;
}
.list-main { flex: 1; min-width: 0; }
.list-title { font-weight: 700; font-size: .92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-sub { font-size: .78rem; color: var(--muted); }
.list-amount { font-weight: 800; font-size: .95rem; white-space: nowrap; }
.list-amount.expense { color: var(--bad); }
.list-amount.income { color: var(--good); }
.link-more { display: inline-block; margin-top: 10px; font-weight: 700; font-size: .9rem; }

.filters { display: grid; gap: 8px; margin-bottom: 10px; }
.expense-total { font-size: .86rem; color: var(--muted); margin-bottom: 8px; }
.income-filter { max-width: 220px; margin-bottom: 10px; }
.income-total { font-size: .86rem; color: var(--muted); margin-bottom: 8px; }

/* ---------- Auth ---------- */
.auth-wrap { max-width: 420px; margin: 8vh auto 0; padding: 0 12px; }
.auth-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 18px; box-shadow: var(--shadow); padding: 26px 22px;
  text-align: center;
}
.auth-logo { font-size: 2.6rem; }
.auth-card h1 { margin: 4px 0 2px; }
.auth-sub { color: var(--muted); margin-bottom: 18px; font-size: .9rem; }
.auth-form { display: grid; gap: 6px; text-align: left; }
.auth-form label { font-size: .82rem; font-weight: 700; color: var(--muted); }
.auth-alt { margin-top: 16px; font-size: .9rem; color: var(--muted); }

/* ---------- Flash & toast ---------- */
.flashes { display: grid; gap: 8px; margin-bottom: 12px; }
.flash { padding: 11px 14px; border-radius: 10px; font-weight: 600; font-size: .9rem; }
.flash-success { background: var(--good-soft); color: #14532d; }
.flash-error { background: var(--bad-soft); color: #7f1d1d; }

.toast {
  position: fixed; left: 50%; bottom: 22px; z-index: 60;
  transform: translateX(-50%);
  background: #0f172a; color: #fff;
  padding: 12px 18px; border-radius: 12px;
  font-weight: 600; font-size: .9rem;
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
  max-width: 90vw; text-align: center;
}

/* ---------- Advisor ---------- */
.result-card { text-align: center; }
.gauge { display: flex; flex-direction: column; align-items: center; }
.gauge-dial {
  position: relative;
  width: 170px; height: 170px; border-radius: 50%;
  background: conic-gradient(var(--gauge-color, var(--accent)) 0deg, var(--border) 0deg);
  display: flex; align-items: center; justify-content: center;
  transition: background .6s ease;
}
.gauge-dial::before {
  content: ""; position: absolute; width: 132px; height: 132px;
  border-radius: 50%; background: var(--card);
}
.gauge.hidden { }
.gauge-dial span {
  position: relative; font-size: 2.2rem; font-weight: 800;
  color: var(--gauge-color, var(--accent));
}
.gauge-label { margin: 10px 0 0; font-weight: 700; color: var(--muted); }
.result-verdict { font-size: 1.5rem; margin: 14px 0 4px; }
.result-verdict.good { color: var(--good); }
.result-verdict.warn { color: var(--warn); }
.result-verdict.bad { color: var(--bad); }
.result-summary { color: var(--muted); margin-bottom: 14px; }

.factor-grid { display: grid; gap: 8px; margin-bottom: 14px; }
.factor-row { display: flex; align-items: center; gap: 10px; }
.factor-row .factor-label { flex: 1; text-align: left; font-size: .82rem; color: var(--muted); }
.factor-bar { flex: 1.4; height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; }
.factor-bar i { display: block; height: 100%; border-radius: 999px; }
.factor-pts { width: 42px; text-align: right; font-weight: 800; font-size: .85rem; }

.result-details {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px;
}
.result-details div {
  background: var(--bg); border-radius: 10px; padding: 10px 6px;
  display: grid; gap: 2px;
}
.result-details span { font-size: .7rem; color: var(--muted); }
.result-details strong { font-size: .92rem; }

.msg-title { font-size: 1rem; margin: 6px 0 10px; text-align: left; }
.msg-list { display: grid; gap: 8px; text-align: left; }
.msg-item {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; border-radius: 10px; font-size: .88rem;
}
.msg-item .msg-emoji { font-size: 1.1rem; }
.msg-item.good { background: var(--good-soft); color: #14532d; }
.msg-item.warn { background: var(--warn-soft); color: #713f12; }
.msg-item.bad { background: var(--bad-soft); color: #7f1d1d; }

/* ---------- Mini suggestions ---------- */
.mini-list { display: grid; gap: 8px; }
.mini-row { display: flex; align-items: center; gap: 10px; }
.mini-row .mini-info { flex: 1; min-width: 0; }
.mini-row .mini-info .mini-name { font-weight: 700; font-size: .88rem; }
.mini-row .mini-info .mini-sub { font-size: .76rem; color: var(--muted); }
.mini-bar { height: 8px; border-radius: 999px; background: var(--border); overflow: hidden; margin-top: 4px; }
.mini-bar i { display: block; height: 100%; border-radius: 999px; }
.mini-amt { font-weight: 800; font-size: .88rem; white-space: nowrap; }

/* ---------- Épargne / catégories ---------- */
.badge-fixe { display: inline-block; font-size: .68rem; font-weight: 700; background: #0D948822; color: #0D9488; padding: 1px 6px; border-radius: 999px; vertical-align: middle; }
.check-row { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.check-row input { width: 18px; height: 18px; accent-color: var(--primary); }

/* ---------- Desktop ---------- */
@media (min-width: 720px) {
  .container { padding: 20px 20px 50px; }
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
  .grid-2 { grid-template-columns: 1fr 1fr; }
  .filters { grid-template-columns: 1fr 1fr 1.2fr; }
  .quick-form { grid-template-columns: 1fr 1.6fr 1fr auto; align-items: center; }
  .quick-form .btn-primary { grid-column: auto; }
  .form-stack.form-inline { grid-template-columns: 1fr 1.6fr 1fr auto; align-items: end; }
  h1 { font-size: 1.5rem; }
}

@media (min-width: 1024px) {
  .factor-row { flex-direction: row; }
}