:root {
  color-scheme: light dark;
  --green: #1f6f4a;
  --green-light: #2e8b57;
  --green-bg: #10201a;
  --red: #c0392b;
  --text: #1c2620;
  --text-muted: #5c6b62;
  --border: #dbe6df;
  --white: #ffffff;
  --radius: 14px;
  --nav-height: 64px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--green-bg);
  color: var(--text);
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font-family: inherit; color: inherit; }
input, textarea, select {
  color: var(--text);
  -webkit-text-fill-color: var(--text);
}

/* ---------- Écran de connexion ---------- */

#login-screen {
  display: none;
  min-height: 100vh;
  min-height: 100dvh;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 2rem 1.25rem;
  gap: 1.5rem;
}
#login-screen.visible { display: flex; }

.login-logo {
  font-size: 2.75rem;
}

.login-title {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--green);
  text-align: center;
}

.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.btn {
  display: block;
  width: 100%;
  padding: 0.9rem 1rem;
  border-radius: var(--radius);
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--green);
  color: var(--white);
}

.btn-secondary {
  background: var(--white);
  color: var(--green);
  border: 1.5px solid var(--green);
}

.btn-text {
  background: none;
  color: var(--text-muted);
  font-weight: 500;
  text-decoration: underline;
  padding: 0.5rem;
}

.field {
  width: 100%;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  border: 1.5px solid var(--border);
  font-size: 1rem;
  background: var(--white);
}

.field:focus { outline: 2px solid var(--green-light); }

#password-form {
  display: none;
  flex-direction: column;
  gap: 0.6rem;
  margin-top: 0.4rem;
}
#password-form.visible { display: flex; }

.login-error {
  color: var(--red);
  font-size: 0.9rem;
  text-align: center;
  min-height: 1.2rem;
}

/* ---------- App shell ---------- */

#app-screen { display: none; }
#app-screen.visible { display: block; }

header.app-header {
  position: sticky;
  top: var(--safe-top);
  background: var(--green);
  color: var(--white);
  padding: 0.55rem 1.1rem;
  border-radius: 0 0 var(--radius) var(--radius);
  z-index: 5;
}

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

.header-spacer { width: 34px; }

.app-title {
  font-size: 24px;
  font-weight: 700;
}

.icon-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: var(--white);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

main {
  padding: 1rem 1.1rem calc(var(--nav-height) + 1.5rem);
  max-width: 480px;
  margin: 0 auto;
}

.view { display: none; }
.view.visible { display: block; }

.card {
  background: var(--white);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.card h2 {
  font-size: 1rem;
  margin: 0 0 0.75rem;
  color: var(--green);
}

/* ---------- Formulaire dépense ---------- */

.amount-input {
  font-size: 2.1rem;
  font-weight: 700;
  text-align: center;
  padding: 0.6rem;
  border: none;
  border-bottom: 2px solid var(--border);
  border-radius: 0;
  background: transparent;
  width: 100%;
  margin-bottom: 1rem;
}
.amount-input:focus { outline: none; border-bottom-color: var(--green); }

.field-group { margin-bottom: 0.75rem; }
.field-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.3rem;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.category-chip {
  padding: 0.55rem 0.3rem;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--white);
  font-size: 0.78rem;
  text-align: center;
  cursor: pointer;
  color: var(--text);
}

.category-chip.selected {
  background: var(--green);
  border-color: var(--green);
  color: var(--white);
}

.category-chip.budget-warning { border-color: #d4a72c; box-shadow: inset 0 0 0 1.5px #d4a72c; }
.category-chip.budget-over { border-color: var(--red); box-shadow: inset 0 0 0 1.5px var(--red); }
.category-chip.selected.budget-warning, .category-chip.selected.budget-over { box-shadow: none; }

.budget-toast {
  position: fixed;
  left: 1rem;
  right: 1rem;
  bottom: calc(84px + env(safe-area-inset-bottom, 0px));
  z-index: 60;
  background: #fff3e0;
  color: #8a5a00;
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-size: 0.88rem;
  text-align: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
.budget-toast[hidden] { display: none; }
.budget-toast.over { background: #fdecea; color: var(--red); }

.form-msg {
  min-height: 1.2rem;
  font-size: 0.88rem;
  text-align: center;
  margin-bottom: 0.5rem;
}
.form-msg.error { color: var(--red); }
.form-msg.success { color: var(--green); }

/* ---------- Liste des dépenses ---------- */

.expense-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--border);
}
.expense-item:last-child { border-bottom: none; }
.expense-item.clickable { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.expense-item.clickable:active { background: var(--green-bg); }
.expense-item .expense-info { flex: 1; min-width: 0; }

.expense-info { display: flex; flex-direction: column; }
.expense-merchant { font-weight: 600; font-size: 0.95rem; }
.expense-meta { font-size: 0.78rem; color: var(--text-muted); }

.expense-amount { font-weight: 700; font-size: 1rem; white-space: nowrap; margin-left: 0.75rem; }
.expense-amount-negative { color: var(--red); }
.expense-amount-positive { color: var(--green); }

.expense-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 1rem 0;
}

.closed-banner {
  background: #fff3e0;
  color: #8a5a00;
  font-size: 0.85rem;
  padding: 0.7rem 1rem;
  border-radius: 10px;
  margin-bottom: 0.75rem;
  text-align: center;
}

/* ---------- Feuille de modification (bottom sheet) ---------- */

.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
}
.sheet-overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--white);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 1rem 1.1rem calc(1.2rem + var(--safe-bottom));
}
.sheet-header { display: flex; justify-content: space-between; align-items: center; }
.sheet-header h2 { margin: 0; font-size: 1.05rem; color: var(--green); }
.sheet-close { background: var(--green-bg); color: var(--text-muted); }
.sheet-info { font-size: 0.78rem; color: var(--text-muted); margin: 0.3rem 0 0.6rem; }
.btn-danger {
  background: none;
  color: var(--red);
  border: 1.5px solid var(--red);
  margin-top: 0.6rem;
}
.sheet .btn-text { margin-top: 0.3rem; }

/* ---------- Navigation basse ---------- */

nav.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--nav-height);
  padding-bottom: var(--safe-bottom);
  background: var(--white);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 10;
}

.nav-btn {
  flex: 1;
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  font-size: 0.7rem;
  color: var(--text-muted);
  cursor: pointer;
}

.nav-btn .nav-icon { font-size: 1.25rem; }
.nav-btn.active { color: var(--green); font-weight: 600; }

@media (prefers-color-scheme: dark) {
  :root {
    --white: #17251f;
    --text: #eef5f0;
    --text-muted: #9fb3a8;
    --border: #26362f;
  }
}
