:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --card-2: #fafafa;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: rgba(0,0,0,.08);
  --line-strong: rgba(0,0,0,.14);
  --dep: #e5484d;
  --dep-soft: #fdecec;
  --enc: #1a9e5c;
  --enc-soft: #e6f6ed;
  --plan: #b26a00;
  --plan-soft: #fff4e0;
  --accent: #0a84ff;
  --accent-soft: #e8f1ff;
  --radius: 18px;
  --radius-s: 11px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.05);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Inter, Segoe UI, Roboto, Arial, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.4; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- login */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.login-card { width: 100%; max-width: 360px; background: var(--card); border-radius: 24px; box-shadow: var(--shadow); padding: 32px 28px; text-align: center; }
.login-logo { width: 52px; height: 52px; border-radius: 15px; background: #111; color: #fff; font-weight: 700; display: grid; place-items: center; margin: 0 auto 14px; font-size: 18px; }
.login-card h1 { font-size: 22px; margin-bottom: 4px; }
.login-card form { margin-top: 22px; text-align: left; }
.error { color: var(--dep); font-size: 13px; margin: 8px 0 0; }

/* ---------- boutons & champs */
.btn { border: 1px solid var(--line-strong); background: var(--card); border-radius: 10px; padding: 7px 12px; font-weight: 500; transition: background .12s, transform .05s; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: #f2f2f4; }
.btn:active { transform: scale(.98); }
.btn.primary { background: #111; color: #fff; border-color: #111; }
.btn.primary:hover { background: #2a2a2d; }
.btn.dep { background: var(--dep); color: #fff; border-color: var(--dep); }
.btn.dep:hover { filter: brightness(.95); }
.btn.enc { background: var(--enc); color: #fff; border-color: var(--enc); }
.btn.enc:hover { filter: brightness(.95); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover { background: rgba(0,0,0,.05); color: var(--text); }
.btn.danger { color: var(--dep); }
.btn.wide { width: 100%; justify-content: center; padding: 10px; }
.btn.sm { padding: 4px 9px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: default; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.field input, .field select, .field textarea, .input {
  border: 1px solid var(--line-strong); border-radius: 10px; padding: 8px 10px; background: #fff; outline: none; width: 100%;
  transition: border-color .12s, box-shadow .12s;
}
.field input:focus, .field select:focus, .field textarea:focus, .input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field textarea { min-height: 64px; resize: vertical; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track { width: 38px; height: 22px; border-radius: 11px; background: #d9d9de; position: relative; transition: background .15s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s; }
.switch input:checked + .track { background: var(--enc); }
.switch input:checked + .track::after { transform: translateX(16px); }

.seg { display: inline-flex; background: #e9e9ec; border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; border-radius: 8px; padding: 5px 11px; font-weight: 500; color: var(--muted); }
.seg button.on { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

/* ---------- mise en page */
.app { max-width: 1480px; margin: 0 auto; padding: 18px 20px 60px; }
.topbar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 16px; }
.brand { display: flex; align-items: center; gap: 10px; margin-right: 6px; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; background: #111; color: #fff; font-weight: 700; display: grid; place-items: center; font-size: 13px; }
.brand h1 { font-size: 19px; }
.brand small { display: block; font-size: 11.5px; color: var(--muted); font-weight: 400; }
.search { position: relative; flex: 1 1 260px; max-width: 460px; }
.search input { padding-left: 34px; border-radius: 12px; height: 38px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--muted); }
.search .clear { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); border: 0; background: #d9d9de; color: #fff; border-radius: 50%; width: 18px; height: 18px; display: none; place-items: center; font-size: 12px; line-height: 1; }
.search.has .clear { display: grid; }
.spacer { flex: 1; }
.yearsel { height: 38px; border-radius: 12px; border: 1px solid var(--line-strong); background: #fff; padding: 0 10px; font-weight: 600; }

.months { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.months button { border: 1px solid transparent; background: transparent; border-radius: 999px; padding: 5px 11px; color: var(--muted); font-weight: 500; }
.months button:hover { background: rgba(0,0,0,.05); }
.months button.on { background: #111; color: #fff; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; min-height: 96px; display: flex; flex-direction: column; justify-content: space-between; }
.kpi .lbl { font-size: 12.5px; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 7px; }
.kpi .dot { width: 8px; height: 8px; border-radius: 50%; }
.kpi .val { font-size: 27px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 4px; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 6px; }
.kpi .sub b { color: var(--plan); font-weight: 600; }
.kpi.enc .val { color: var(--enc); }
.kpi.dep .val { color: var(--dep); }
.kpi.diff .val.neg { color: var(--dep); }
.kpi.diff .val.pos { color: var(--enc); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.panel { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font-size: 16px; display: flex; align-items: center; gap: 8px; }
.panel-head h2 .dot { width: 9px; height: 9px; border-radius: 50%; }
.panel-head .total { color: var(--muted); font-size: 12.5px; }
.panel-head .total b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.panel-foot { padding: 10px 16px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }

.tbl-wrap { overflow-x: auto; }
table.ops { width: 100%; border-collapse: collapse; }
table.ops th { text-align: left; font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: .03em; text-transform: uppercase; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--card-2); white-space: nowrap; position: sticky; top: 0; }
table.ops th.num { text-align: right; }
table.ops td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.ops tbody tr { transition: background .1s; }
table.ops tbody tr.op:hover { background: #f7f7f9; cursor: pointer; }
table.ops tr:last-child td { border-bottom: 0; }
td.date { white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }
td.who { max-width: 260px; }
td.who .t { font-weight: 550; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.who .l { font-size: 12px; color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.amt { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.cat { font-size: 12px; color: var(--muted); white-space: nowrap; }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; border-radius: 6px; padding: 1px 6px; background: #ececef; color: var(--muted); font-weight: 500; white-space: nowrap; vertical-align: middle; margin-left: 4px; }
.tag.plate { font-family: var(--mono); background: #fff; border: 1px solid var(--line-strong); color: var(--text); letter-spacing: .02em; }
.tag.rec { background: var(--accent-soft); color: var(--accent); }
.tag.prest { background: #f0ebff; color: #5b3fd6; }
.tag.year { background: var(--plan-soft); color: var(--plan); }
.pill { border: 0; border-radius: 999px; padding: 3px 9px; font-size: 12px; font-weight: 600; white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill.paye, .pill.encaisse { background: var(--enc-soft); color: var(--enc); }
.pill.a_payer, .pill.a_recevoir { background: var(--plan-soft); color: var(--plan); }
.pill.a_payer.late, .pill.a_recevoir.late { background: var(--dep-soft); color: var(--dep); }
.pill:hover { filter: brightness(.96); }
td.act { width: 1%; white-space: nowrap; }
.icon-btn { border: 0; background: transparent; border-radius: 7px; width: 26px; height: 26px; display: inline-grid; place-items: center; color: var(--muted); opacity: 0; }
tr:hover .icon-btn { opacity: 1; }
.icon-btn:hover { background: rgba(0,0,0,.07); color: var(--text); }
.icon-btn svg { width: 15px; height: 15px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); font-size: 13px; }
.empty b { display: block; color: var(--text); font-weight: 600; margin-bottom: 4px; }

/* ligne de saisie rapide */
.quick-tbl { border-bottom: 2px solid var(--line-strong); }
.quick-tbl tr.quick td { border-bottom: 0; }
tr.quick td { background: #fbfbfd; border-bottom: 1px solid var(--line); padding: 8px 10px; }
tr.quick input, tr.quick select { border: 1px solid var(--line-strong); border-radius: 8px; padding: 5px 7px; width: 100%; background: #fff; outline: none; min-width: 0; font-size: 13px; }
tr.quick td { padding: 8px 6px; }
tr.quick td:first-child { padding-left: 10px; }
tr.quick input[type=date] { padding: 5px 4px; font-size: 12.5px; }
tr.quick .act .btn { padding: 4px 7px; }
tr.quick input:focus, tr.quick select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
tr.quick .amt-in { text-align: right; font-weight: 600; }
tr.quick-more td { background: #fbfbfd; padding: 4px 10px 12px; }
.more-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.more-grid .field { margin: 0; }
.more-grid .field input, .more-grid .field select { padding: 6px 8px; border-radius: 8px; }
.quick-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.hint { font-size: 12px; color: var(--muted); }
.hint.warn { color: var(--plan); }
.rec-box { grid-column: 1 / -1; background: var(--accent-soft); border-radius: 10px; padding: 10px; display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.rec-box .field > span { color: #2c5fa8; }

/* ---------- tiroir latéral */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.25); opacity: 0; pointer-events: none; transition: opacity .18s; z-index: 40; }
.backdrop.show { opacity: 1; pointer-events: auto; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: 440px; max-width: 100vw; background: var(--card); box-shadow: -8px 0 30px rgba(0,0,0,.12); transform: translateX(104%); transition: transform .2s ease; z-index: 50; display: flex; flex-direction: column; }
.drawer.show { transform: none; }
.drawer-head { padding: 16px 20px 12px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; }
.drawer-head h2 { font-size: 17px; flex: 1; }
.drawer-body { padding: 16px 20px; overflow-y: auto; flex: 1; }
.drawer-foot { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; gap: 8px; align-items: center; background: var(--card-2); }
.drawer-foot .spacer { flex: 1; }
.recap { background: var(--card-2); border-radius: 12px; padding: 4px 12px; margin: 10px 0 14px; }
.recap .r { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.recap .r:last-child { border-bottom: 0; }
.recap .r b { font-variant-numeric: tabular-nums; }
.recap .r.main { font-weight: 600; }
.recap .pos { color: var(--enc); }
.recap .neg { color: var(--dep); }
.list { display: flex; flex-direction: column; gap: 6px; }
.list .item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.list .item .grow { flex: 1; min-width: 0; }
.list .item .grow .t { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .item .grow .s { font-size: 12px; color: var(--muted); }
.list .item.clickable { cursor: pointer; }
.list .item.clickable:hover { background: #f7f7f9; }
.section-title { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; margin: 16px 0 8px; display: flex; align-items: center; justify-content: space-between; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong); border-radius: 999px; padding: 4px 6px 4px 11px; font-size: 13px; background: #fff; }
.chip button { border: 0; background: #ececef; width: 18px; height: 18px; border-radius: 50%; font-size: 12px; line-height: 1; color: var(--muted); display: grid; place-items: center; }
.chip button:hover { background: var(--dep-soft); color: var(--dep); }
.add-inline { display: flex; gap: 6px; margin-top: 8px; }
.add-inline input { flex: 1; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #1d1d1f; color: #fff; border-radius: 14px; padding: 11px 14px 11px 16px; display: flex; align-items: center; gap: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.25); opacity: 0; pointer-events: none; transition: all .18s; z-index: 60; font-size: 13.5px; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { border: 0; background: rgba(255,255,255,.14); color: #fff; border-radius: 8px; padding: 5px 10px; font-weight: 600; }
.toast button:hover { background: rgba(255,255,255,.24); }
.toast.err { background: var(--dep); }

/* modal confirm */
.modal { position: fixed; inset: 0; display: grid; place-items: center; z-index: 70; background: rgba(0,0,0,.3); opacity: 0; pointer-events: none; transition: opacity .15s; padding: 16px; }
.modal.show { opacity: 1; pointer-events: auto; }
.modal .box { background: #fff; border-radius: 18px; padding: 20px; width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal .box h3 { font-size: 16px; margin-bottom: 6px; }
.modal .box p { margin: 0 0 16px; color: var(--muted); font-size: 13.5px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; }

.kbd { font-family: var(--mono); font-size: 11px; background: #ececef; border-radius: 5px; padding: 1px 5px; color: var(--muted); }

.m-only { display: none; }

@media (max-width: 1100px) {
  .grid2 { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  .app { padding: 12px 12px 60px; }
  .kpis { grid-template-columns: 1fr; gap: 8px; }
  .kpi { min-height: 0; padding: 12px 14px; }
  .kpi .val { font-size: 22px; }
  .topbar .brand small { display: none; }
  .drawer { width: 100vw; }
  .icon-btn { opacity: 1; }
  .hide-m { display: none !important; }
  .topbar { gap: 8px; }
  .topbar .btn { padding: 6px 9px; font-size: 13px; }
  table.ops th:nth-child(3), table.ops tr.op td:nth-child(3), table.ops th:nth-child(6), table.ops tr.op td:nth-child(6) { display: none; }
  td.who { max-width: 0; width: 60%; }
  .quick-tbl, .quick-tbl tbody, .quick-tbl tr.quick-more, .quick-tbl tr.quick-more td { display: block; }
  .quick-tbl tr.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 10px 10px 4px; }
  .quick-tbl tr.quick td { display: block; padding: 0 !important; width: auto !important; max-width: none !important; border: 0; }
  .quick-tbl tr.quick td:nth-child(2) { grid-column: 1 / -1; }
  .quick-tbl tr.quick td.act { text-align: right; }
  .m-only { display: inline; }
}
