:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --text: #1b2421;
  --muted: #5d6b66;
  --border: #dbe2df;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --in: #1f7a4d;
  --out: #b4432f;
  --warn: #b7791f;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111715;
    --surface: #1a2220;
    --surface-2: #222c29;
    --text: #e7eeeb;
    --muted: #9aaba5;
    --border: #2e3a36;
    --accent: #4fb89a;
    --accent-ink: #0b1512;
    --in: #5cc58f;
    --out: #ec8a75;
    --warn: #e3b05a;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
h3 { font-size: .95rem; margin: .2rem 0 .5rem; color: var(--muted); }
.muted { color: var(--muted); font-size: .9rem; margin: 0 0 .8rem; }

/* Barre du haut */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; min-width: 0; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.year-picker select { font-weight: 600; }

/* Navigation : barre en bas sur téléphone, onglets en haut sur ordinateur */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  background: none; border: 0; color: var(--muted); padding: 8px 2px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .72rem; cursor: pointer;
}
.tabs svg { width: 22px; height: 22px; fill: currentColor; }
.tabs button.active { color: var(--accent); font-weight: 600; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.view { display: none; }
.view.active { display: block; }

@media (min-width: 820px) {
  body { padding-bottom: 24px; }
  .tabs {
    position: sticky; top: 53px; bottom: auto;
    display: flex; justify-content: center; gap: 4px;
    border-top: 0; border-bottom: 1px solid var(--border); padding: 0 16px;
  }
  .tabs button { flex-direction: row; font-size: .9rem; padding: 12px 16px; gap: 8px; border-bottom: 2px solid transparent; }
  .tabs svg { width: 18px; height: 18px; }
  .tabs button.active { border-bottom-color: var(--accent); }
  .fab { display: none !important; }
}

/* Cartes */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: .6rem; }
.card-head h2 { margin: 0; }
.grid2 { display: grid; gap: 16px; }
@media (min-width: 820px) { .grid2 { grid-template-columns: 1fr 1fr; } .grid2 > .card { margin-bottom: 0; } .grid2 { margin-bottom: 16px; } }

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 16px; }
@media (min-width: 820px) { .kpis { grid-template-columns: repeat(4, 1fr); } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: .8rem; }
.kpi .value { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi.in .value { color: var(--in); }
.kpi.out .value { color: var(--out); }
.kpi.main { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.kpi.main .label { color: inherit; opacity: .85; }

/* Graphique mensuel */
.month-chart { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; align-items: end; height: 170px; }
.month-chart .m { display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; gap: 4px; }
.month-chart .bars { display: flex; gap: 2px; align-items: flex-end; height: 100%; width: 100%; justify-content: center; }
.month-chart .bar { width: 42%; max-width: 14px; border-radius: 3px 3px 0 0; min-height: 1px; }
.month-chart .bar.in { background: var(--in); }
.month-chart .bar.out { background: var(--out); }
.month-chart .lbl { font-size: .68rem; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

.catbar { margin-bottom: 10px; }
.catbar .row { display: flex; justify-content: space-between; gap: 8px; font-size: .88rem; }
.catbar .track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-top: 3px; }
.catbar .fill { height: 100%; background: var(--out); border-radius: 4px; }

/* Listes */
.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
  padding: 10px 4px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.item:last-child { border-bottom: 0; }
.item:hover { background: var(--surface-2); }
.item .t { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .s { color: var(--muted); font-size: .8rem; }
.item .amt { font-weight: 650; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.amt.in { color: var(--in); }
.amt.out { color: var(--out); }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; }
.month-head { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 14px 4px 4px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; }

.badge { display: inline-block; font-size: .72rem; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); margin-left: 6px; vertical-align: 1px; }
.badge.ok { background: color-mix(in srgb, var(--in) 18%, transparent); color: var(--in); }
.badge.warn { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }
.badge.bad { background: color-mix(in srgb, var(--out) 18%, transparent); color: var(--out); }

/* Tableau budget */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; }
th { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.section td { background: var(--surface-2); font-weight: 650; }
tr.total td { font-weight: 700; }
td input { width: 100px; text-align: right; }

/* Formulaires */
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 10px; min-height: 40px; width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 0; border-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--text); font-size: 1rem; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr; } .span2 { grid-column: span 2; } }
.form-inline { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.form-inline label { flex: 1 1 200px; }
.form-inline input { width: auto; flex: 1 1 140px; }
.year-picker select { width: auto; min-height: 36px; padding: 6px 10px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1 1 100%; }
.toolbar select { flex: 1 1 130px; width: auto; }
.toolbar .btn { flex: 0 0 auto; }
@media (min-width: 820px) { .toolbar input[type=search] { flex: 1 1 240px; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-weight: 550;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--out); border-color: color-mix(in srgb, var(--out) 40%, var(--border)); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.file-btn { position: relative; overflow: hidden; flex-direction: row; font-size: 1rem; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-2); border-radius: 99px; padding: 3px 4px 3px 10px; font-size: .85rem; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); width: 22px; height: 22px; border-radius: 50%; }
.chip button:hover { background: var(--border); }
.cat-add input { flex: 1 1 120px; }

.logo-preview img { max-height: 60px; max-width: 200px; }

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 9;
  width: 54px; height: 54px; border-radius: 50%; border: 0; font-size: 28px; line-height: 1;
  background: var(--accent); color: var(--accent-ink); box-shadow: 0 4px 14px rgba(0,0,0,.25); cursor: pointer;
}

/* Modale */
dialog {
  border: 0; padding: 0; border-radius: 16px; width: min(640px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px); background: var(--surface); color: var(--text);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
@media (max-width: 640px) {
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 16px 16px 0 0; max-height: 92dvh; }
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal-head h2 { margin: 0; }
.modal-head .x { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); width: 36px; height: 36px; border-radius: 50%; }
.modal-content { padding: 16px; }
.modal-foot { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); position: sticky; bottom: 0; background: var(--surface); }
.modal-foot .spacer { flex: 1; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { flex-direction: row; justify-content: center; align-items: center; padding: 9px; cursor: pointer; font-size: .95rem; color: var(--text); }
.seg input { display: none; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.lines { display: flex; flex-direction: column; gap: 8px; }
.line { display: grid; grid-template-columns: 1fr 70px 100px 36px; gap: 6px; align-items: center; }
.line .del { border: 0; background: none; color: var(--muted); font-size: 20px; cursor: pointer; height: 40px; }
.line-head { display: grid; grid-template-columns: 1fr 70px 100px 36px; gap: 6px; font-size: .75rem; color: var(--muted); }
@media (max-width: 480px) {
  .line { grid-template-columns: 1fr 1fr 36px; }
  .line .lbl-in { grid-column: 1 / -1; }
  .line-head { display: none; }
}
.doc-total { text-align: right; font-size: 1.1rem; font-weight: 700; margin-top: 10px; }

.import-preview { max-height: 45vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.import-preview td { font-size: .82rem; padding: 5px 6px; }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (max-width: 480px) {
  #budgetTable th, #budgetTable td { padding: 6px 3px; font-size: .82rem; }
  #budgetTable td input { width: 72px; padding: 6px; min-height: 34px; }
  #budgetTable .badge { margin-left: 0; padding: 1px 5px; }
}
