:root {
  --indigo: #4f46e5;
  --indigo-d: #4338ca;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --bg: #f4f5fb;
  --card: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16,24,40,.06), 0 8px 24px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* --- LOGIN --- */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); padding: 20px;
}
.login-card {
  background: var(--card); width: 100%; max-width: 380px; padding: 36px 32px;
  border-radius: 18px; box-shadow: 0 20px 50px rgba(0,0,0,.25); display: flex; flex-direction: column; gap: 14px;
}
.login-logo { font-size: 1.4rem; font-weight: 800; }
.login-card h1 { margin: 0; font-size: 1.5rem; }
.login-sub { margin: 0 0 8px; color: var(--muted); font-size: .92rem; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: .85rem; font-weight: 600; color: #374151; }
.login-card input { padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 1rem; }
.login-error { background: var(--red-bg); color: var(--red); padding: 10px; border-radius: 8px; font-size: .9rem; }

/* --- TOPBAR --- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-size: 1.05rem; }
.brand strong { color: var(--indigo); }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.save-state { font-size: .8rem; color: var(--muted); }
.save-state.saving { color: var(--amber); }
.save-state.saved { color: var(--green); }
.save-state.error { color: var(--red); }

/* --- TABS --- */
.tabs {
  display: flex; gap: 4px; padding: 0 16px; background: var(--card);
  border-bottom: 1px solid var(--line); overflow-x: auto; position: sticky; top: 53px; z-index: 19;
}
.tab {
  border: none; background: none; padding: 13px 14px; font-size: .92rem; font-weight: 600;
  color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--indigo); border-bottom-color: var(--indigo); }

/* --- LAYOUT --- */
.content { max-width: 1080px; margin: 0 auto; padding: 22px 16px 60px; }
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px; margin-bottom: 20px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card h2 { margin: 0; font-size: 1.1rem; }
.hint { color: var(--muted); font-size: .86rem; margin: 4px 0 14px; }

/* --- KPI --- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.kpi {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; display: flex; flex-direction: column; gap: 4px;
}
.kpi-label { font-size: .78rem; color: var(--muted); font-weight: 600; }
.kpi-value { font-size: 1.5rem; font-weight: 800; letter-spacing: -.02em; }
.kpi-sub { font-size: .76rem; color: var(--muted); }
.kpi-warn .kpi-value.neg { color: var(--red); }

/* --- CHART --- */
.chart-wrap { position: relative; height: 380px; }
.alert { margin-top: 14px; padding: 11px 14px; border-radius: 10px; font-size: .9rem; }
.alert-danger { background: var(--red-bg); color: var(--red); }

/* --- SWITCH --- */
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: .85rem; color: var(--muted); cursor: pointer; user-select: none; }
.switch input { width: 38px; height: 22px; appearance: none; background: var(--line); border-radius: 99px; position: relative; cursor: pointer; transition: background .2s; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; background: #fff; border-radius: 50%; transition: left .2s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked { background: var(--indigo); }
.switch input:checked::after { left: 18px; }

/* --- TABLES --- */
.table-scroll { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: .9rem; min-width: 640px; }
table.grid thead th { text-align: left; font-size: .74rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 8px 8px; border-bottom: 1px solid var(--line); }
table.grid td { padding: 6px 8px; border-bottom: 1px solid #f1f2f6; vertical-align: middle; }
table.grid tr:last-child td { border-bottom: none; }
table.grid input, table.grid select { width: 100%; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: .88rem; background: #fff; }
table.grid input[type="number"] { text-align: right; }
table.grid input:focus, table.grid select:focus { outline: 2px solid var(--indigo); outline-offset: -1px; }
.col-amount { width: 120px; }
.col-rate { width: 92px; }
.col-date { width: 150px; }
.col-type { width: 130px; }
.col-freq { width: 130px; }
.col-actif { width: 56px; text-align: center; }
.col-del { width: 44px; text-align: center; }
.cell-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: .72rem; font-weight: 700; }
.tag-in { background: var(--green-bg); color: var(--green); }
.tag-out { background: var(--red-bg); color: var(--red); }
.btn-del { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1.05rem; padding: 4px; border-radius: 6px; }
.btn-del:hover { background: var(--red-bg); color: var(--red); }
.empty-row td { color: var(--muted); text-align: center; padding: 18px; font-style: italic; }

/* --- TVA --- */
.tva-grid { display: flex; align-items: stretch; gap: 10px; flex-wrap: wrap; }
.tva-box { flex: 1 1 180px; border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--line); }
.tva-collectee { background: var(--green-bg); }
.tva-deductible { background: var(--amber-bg); }
.tva-nette { background: #eef2ff; }
.tva-label { font-size: .78rem; font-weight: 700; color: #374151; }
.tva-val { font-size: 1.45rem; font-weight: 800; }
.tva-op { display: flex; align-items: center; font-size: 1.6rem; font-weight: 700; color: var(--muted); }

.inline-field { font-size: .85rem; color: var(--muted); display: inline-flex; align-items: center; }
.inline-field select { margin-left: 6px; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px; }

/* --- CALC --- */
.calc-row { display: flex; gap: 14px; flex-wrap: wrap; }
.calc-row label { display: flex; flex-direction: column; gap: 6px; font-size: .82rem; font-weight: 600; color: #374151; flex: 1 1 150px; }
.calc-row input, .calc-row select { padding: 9px 10px; border: 1px solid var(--line); border-radius: 9px; font-size: .95rem; }
.calc-out { display: flex; gap: 12px; margin-top: 16px; flex-wrap: wrap; }
.calc-out div { flex: 1 1 120px; background: var(--bg); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.calc-out span { font-size: .76rem; color: var(--muted); font-weight: 600; }
.calc-out strong { font-size: 1.25rem; }

/* --- SIM --- */
.sim-summary { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.sim-chip { background: var(--bg); border-radius: 10px; padding: 10px 14px; font-size: .85rem; }
.sim-chip strong { display: block; font-size: 1.15rem; margin-top: 2px; }
.sim-chip.delta-pos strong { color: var(--green); }
.sim-chip.delta-neg strong { color: var(--red); }

/* --- BUTTONS --- */
.btn { border: none; border-radius: 10px; padding: 10px 16px; font-size: .9rem; font-weight: 600; cursor: pointer; transition: background .15s, opacity .15s; }
.btn-sm { padding: 8px 12px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-primary { background: var(--indigo); color: #fff; }
.btn-primary:hover { background: var(--indigo-d); }
.btn-secondary { background: #eef2ff; color: var(--indigo-d); }
.btn-secondary:hover { background: #e0e7ff; }
.btn-ghost { background: none; color: var(--muted); }
.btn-ghost:hover { background: #f3f4f6; }
.btn-danger { background: var(--red-bg); color: var(--red); }
.btn-danger:hover { background: #fecaca; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

.footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 24px 16px; }

/* --- RESPONSIVE --- */
@media (max-width: 720px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .chart-wrap { height: 300px; }
}
@media (max-width: 440px) {
  .kpi-grid { grid-template-columns: 1fr; }
}

/* --- NOTES DE FRAIS --- */
.alert-ok { background: var(--green-bg); color: var(--green); }
.dropzone { border: 2px dashed var(--line); border-radius: 12px; padding: 26px; text-align: center; cursor: pointer; color: var(--muted); transition: border-color .15s, background .15s; }
.dropzone:hover, .dropzone.drag { border-color: var(--indigo); background: #f5f6ff; color: var(--indigo-d); }
.dz-icon { font-size: 2rem; margin-bottom: 6px; }
.dz-sub { font-size: .78rem; margin-top: 4px; }
.notes-list { display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 16px; }
.note-card { display: grid; grid-template-columns: 60px 1fr auto; gap: 14px; align-items: center; border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.note-thumb { width: 60px; height: 60px; border-radius: 8px; background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: .8rem; font-weight: 700; color: var(--muted); overflow: hidden; }
.note-thumb img { width: 100%; height: 100%; object-fit: cover; }
.note-fields { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.note-fields input, .note-fields select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: .85rem; background: #fff; }
.note-fields input[type="number"] { text-align: right; }
.note-lib { flex: 1 1 150px; min-width: 110px; }
.note-actions { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.badge { font-size: .68rem; font-weight: 700; padding: 3px 8px; border-radius: 99px; white-space: nowrap; }
.badge-ok { background: var(--green-bg); color: var(--green); }
.badge-verif { background: var(--amber-bg); color: var(--amber); }
.badge-ko { background: var(--red-bg); color: var(--red); }
@media (max-width: 560px) { .note-card { grid-template-columns: 48px 1fr; } .note-actions { grid-column: 1 / -1; flex-direction: row; justify-content: flex-end; } }
.badge-src { background: #eef2ff; color: var(--indigo-d); }
.note-sub { font-size: .74rem; color: var(--muted); width: 100%; margin-top: 2px; }

/* --- CHAT HERMÈS --- */
.chat-launcher { position: fixed; left: 20px; bottom: 20px; width: 54px; height: 54px; border-radius: 50%; background: var(--indigo); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; cursor: pointer; box-shadow: 0 6px 20px rgba(79,70,229,.4); z-index: 50; transition: transform .15s; }
.chat-launcher:hover { transform: scale(1.06); }
.chat-panel { position: fixed; left: 20px; bottom: 84px; width: 360px; max-width: calc(100vw - 32px); height: 480px; max-height: calc(100vh - 120px); background: var(--card); border-radius: 16px; box-shadow: 0 16px 48px rgba(16,24,40,.28); display: flex; flex-direction: column; overflow: hidden; z-index: 51; }
.chat-head { background: var(--indigo); color: #fff; padding: 12px 14px; font-weight: 700; display: flex; align-items: center; justify-content: space-between; }
.chat-close { background: none; border: none; color: #fff; font-size: 1rem; cursor: pointer; opacity: .85; }
.chat-close:hover { opacity: 1; }
.chat-msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; background: var(--bg); }
.chat-empty { color: var(--muted); font-size: .88rem; text-align: center; margin: auto; padding: 0 10px; }
.chat-msg { max-width: 85%; padding: 9px 12px; border-radius: 12px; font-size: .9rem; white-space: pre-wrap; line-height: 1.45; }
.chat-msg.user { align-self: flex-end; background: var(--indigo); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.assistant { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); background: var(--card); }
.chat-form input { flex: 1; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: .9rem; }
.chat-send { background: var(--indigo); color: #fff; border: none; border-radius: 10px; width: 42px; cursor: pointer; font-size: 1rem; }
.chat-send:hover { background: var(--indigo-d); }
@media (max-width: 560px) { .chat-panel { left: 8px; right: 8px; width: auto; } .chat-launcher { left: 12px; bottom: 12px; } }
