:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #14202b;
  --text-muted: #5b6b7a;
  --primary: #0f4c81;
  --primary-dark: #0a3a63;
  --accent: #ffb020;
  --danger: #c0392b;
  --success: #1e8e5a;
  --border: #dfe4ea;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1620;
    --surface: #17212c;
    --surface-2: #1e2a37;
    --text: #eaf0f6;
    --text-muted: #93a2b1;
    --primary: #2f8fd1;
    --primary-dark: #1f6fa8;
    --border: #2a3746;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2 { margin: 0 0 .5em 0; }
h2 { font-size: 1.05rem; }

.muted { color: var(--text-muted); }
.small { font-size: .8rem; }

/* ---------- Views ---------- */
.view { display: none; padding: 12px 16px calc(90px + var(--safe-bottom)); animation: fadein .15s ease; }
.view.view-visible { display: block; }
.view-center { display: none; align-items: center; justify-content: center; height: 100vh; }
.view-center.view-visible { display: flex; }

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

/* ---------- Loading ---------- */
.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, var(--primary-dark), var(--primary) 60%, var(--accent) 160%);
}
.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 22px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.brand-icon { width: 44px; height: 44px; border-radius: 10px; }
.brand h1 { font-size: 1.15rem; }

/* ---------- Forms ---------- */
form label {
  display: block;
  font-size: .85rem;
  font-weight: 600;
  margin: 14px 0 6px;
  color: var(--text-muted);
}
form input[type="email"],
form input[type="password"],
form input[type="text"],
form input[type="number"],
form input[type="date"] {
  width: 100%;
  padding: 12px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
}
form input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.checkbox-row { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.checkbox-row input { width: auto; }

.btn {
  display: inline-block;
  padding: 13px 18px;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.btn-block { width: 100%; margin-top: 14px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }

.error { color: var(--danger); font-size: .85rem; margin-top: 10px; }
.success { color: var(--success); font-size: .85rem; margin-top: 10px; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 16px 14px;
  background: var(--primary);
  color: #fff;
}
.topbar h1 { font-size: 1.1rem; margin: 0; }
.icon-btn {
  background: rgba(255,255,255,.15);
  border: none; color: #fff;
  width: 38px; height: 38px; border-radius: 50%;
  font-size: 1.1rem; cursor: pointer;
}

/* ---------- Year tabs ---------- */
.year-tabs {
  display: flex; gap: 8px;
  margin: 14px 0;
  overflow-x: auto;
}
.year-tabs button {
  flex: 0 0 auto;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}
.year-tabs button.year-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* ---------- Stat cards ---------- */
.cards-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex; flex-direction: column; gap: 4px;
}
.stat-card-total { grid-column: 1 / -1; background: var(--primary); border-color: var(--primary); }
.stat-card-total .stat-label, .stat-card-total .stat-value { color: #fff; }
.stat-label { font-size: .75rem; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
.stat-value { font-size: 1.35rem; font-weight: 700; }

.projection-note {
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: .85rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

/* ---------- Chart & table ---------- */
.chart-card, .table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
}
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; white-space: nowrap; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }

/* ---------- Filter chips ---------- */
.filter-row { display: flex; gap: 8px; margin-bottom: 14px; }
.chip {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: .85rem;
  cursor: pointer;
}
.chip-active { background: var(--primary); border-color: var(--primary); color: #fff; }

/* ---------- Entries list ---------- */
.entries-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.entry-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
}
.entry-badge {
  flex: 0 0 auto;
  width: 42px; height: 42px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: .7rem; font-weight: 700; color: #fff;
}
.badge-hno { background: var(--primary); }
.badge-astreinte { background: var(--accent); color: #3a2a00; }
.entry-main { flex: 1; min-width: 0; }
.entry-date { font-weight: 600; font-size: .92rem; }
.entry-sub { font-size: .8rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-amount { font-weight: 700; font-size: .95rem; white-space: nowrap; }
.empty-msg { text-align: center; margin-top: 40px; }

/* ---------- Type toggle (form) ---------- */
.type-toggle { display: flex; gap: 8px; margin-top: 10px; }
.type-btn {
  flex: 1;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}
.type-btn-active { background: var(--primary); border-color: var(--primary); color: #fff; }

.settings-info { margin-top: 26px; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 6px calc(6px + var(--safe-bottom));
}
.nav-btn {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none;
  padding: 6px 2px;
  font-size: .68rem;
  color: var(--text-muted);
  cursor: pointer;
}
.nav-btn .nav-icon { font-size: 1.25rem; }
.nav-btn-active { color: var(--primary); font-weight: 700; }
.nav-btn-fab .nav-icon {
  background: var(--primary);
  color: #fff;
  width: 46px; height: 46px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem;
  margin-top: -22px;
  box-shadow: 0 6px 16px rgba(15,76,129,.4);
}

@media (min-width: 640px) {
  #main-content { max-width: 560px; margin: 0 auto; }
  .bottom-nav { max-width: 560px; margin: 0 auto; left: 0; right: 0; border-radius: 16px 16px 0 0; }
}
