:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #17202e;
  --muted: #5b6575;
  --border: #d9dee6;
  --primary: #1b2a5c;
  --primary-hover: #26397a;
  --primary-text: #ffffff;
  --accent: #2f6fdb;
  --success: #17803d;
  --danger: #b42318;
  --warning-bg: #fff6e0;
  --warning-border: #f0c56a;
  --danger-bg: #fdecea;
  --danger-border: #f3b4ad;
  --c-anthropic: #c96442;
  --c-openai: #0f8f6e;
  --c-openrouter: #5b5fe0;
  --c-ionos: #0b5cad;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --surface: #171e29;
    --surface-2: #1f2835;
    --text: #e8ecf2;
    --muted: #9aa5b5;
    --border: #2c3747;
    --primary: #8fb0ff;
    --primary-hover: #a9c2ff;
    --primary-text: #0c1322;
    --accent: #8fb0ff;
    --success: #4cc27a;
    --danger: #ff7b6e;
    --warning-bg: #2c2414;
    --warning-border: #7a5d22;
    --danger-bg: #2e1715;
    --danger-border: #7a2c25;
    --c-anthropic: #e08a66;
    --c-openai: #3cc49d;
    --c-openrouter: #8f92ff;
    --c-ionos: #5aa2ec;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
[hidden] { display: none !important; }
h1 { font-size: 1.6rem; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 1rem 0 .4rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.strong { font-weight: 700; }
.nowrap { white-space: nowrap; }
.error { color: var(--danger); margin: 0; }
.error:empty { display: none; }
.feedback { margin: 0; }
.feedback:empty { display: none; }
.feedback.ok { color: var(--success); }
.feedback.ko { color: var(--danger); }
.skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: .5rem 1rem; z-index: 10; }
.skip:focus { left: 1rem; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.row { display: flex; gap: .75rem; align-items: center; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1 1 220px; min-width: 0; }

input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .5rem .7rem;
  max-width: 100%;
}
input[type="checkbox"], input[type="radio"] { width: 1.1rem; height: 1.1rem; padding: 0; accent-color: var(--primary); }
label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .92rem; }
label .muted, label .small { font-weight: 400; }
label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 500; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: .75rem 1rem 1rem; margin: 0; display: flex; flex-direction: column; gap: .75rem; }
legend { font-weight: 700; padding: 0 .3rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; font-size: .95rem;
  padding: .5rem 1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: .3rem .75rem; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}

/* Connexion */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 1rem; }
.login-card .brand { font-size: 1.25rem; }

/* Barre du haut */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: .7rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.05rem; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.topbar nav a { padding: .4rem .8rem; border-radius: 999px; color: var(--text); text-decoration: none; font-weight: 500; }
.topbar nav a:hover { background: var(--surface-2); }
.topbar nav a[aria-current="page"] { background: var(--primary); color: var(--primary-text); }
.user { display: flex; align-items: center; gap: .5rem; font-size: .9rem; }

main { max-width: 1200px; margin: 0 auto; padding: 1.5rem; outline: none; }

.notice {
  background: var(--warning-bg); border: 1px solid var(--warning-border);
  border-radius: var(--radius); padding: .75rem 1rem; margin-bottom: 1rem;
}
.notice.danger { background: var(--danger-bg); border-color: var(--danger-border); }

/* Période */
.period { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem 1rem; padding: 1rem 1.25rem; }
.period #sync-status { margin-left: auto; align-self: center; }
.period label { min-width: 0; max-width: 100%; }
.period select { width: 100%; text-overflow: ellipsis; }

/* Indicateurs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.kpi {
  display: flex; flex-direction: column; gap: .15rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.25rem; box-shadow: var(--shadow);
}
.kpi.main { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.kpi.main .kpi-sub, .kpi.main .kpi-label { color: inherit; opacity: .85; }
.kpi.warn { background: var(--warning-bg); border-color: var(--warning-border); }
.kpi-label { font-size: .85rem; color: var(--muted); font-weight: 600; }
.kpi-value { font-size: 1.9rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: .85rem; color: var(--muted); }
.kpi.main a, .kpi-sub a { color: inherit; }

/* Graphique */
.chart { width: 100%; height: 240px; margin-top: .5rem; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .hit { fill: transparent; }
.chart g:hover .bar { opacity: .8; }
.bar.p-anthropic, .sw.p-anthropic { fill: var(--c-anthropic); background: var(--c-anthropic); }
.bar.p-openai, .sw.p-openai { fill: var(--c-openai); background: var(--c-openai); }
.bar.p-openrouter, .sw.p-openrouter { fill: var(--c-openrouter); background: var(--c-openrouter); }
.bar.p-ionos, .sw.p-ionos { fill: var(--c-ionos); background: var(--c-ionos); }
.legend { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .85rem; }
.legend-item { display: inline-flex; align-items: center; gap: .35rem; }
.sw { display: inline-block; width: .8rem; height: .8rem; border-radius: 3px; }

/* Étiquettes fournisseur */
.tag {
  display: inline-block; font-size: .75rem; font-weight: 700; padding: .05rem .5rem; border-radius: 99px;
  border: 1px solid currentColor; white-space: nowrap;
}
.tag.p-anthropic { color: var(--c-anthropic); }
.tag.p-openai { color: var(--c-openai); }
.tag.p-openrouter { color: var(--c-openrouter); }
.tag.p-ionos { color: var(--c-ionos); }
.badge { display: inline-block; font-size: .72rem; font-weight: 600; padding: .05rem .5rem; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); margin-left: .3rem; }
.badge.ok { color: var(--success); }
.badge.ko { color: var(--danger); }

/* Tableaux */
.table-wrap { overflow-x: auto; margin-top: .75rem; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table thead th { font-size: .8rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
.table .num { text-align: right; white-space: nowrap; }
.table tfoot th, .table tfoot td { border-bottom: none; border-top: 2px solid var(--border); font-weight: 700; }
.table tr.group th { font-weight: 600; }
.table tr.group.unassigned { background: var(--warning-bg); }
.table tr.group.internal { color: var(--muted); }
.table tr.detail td { font-size: .9rem; background: var(--surface-2); }
.table tr.detail td:first-child { padding-left: 2.2rem; }
.table .bill { color: var(--success); }
.disclose { all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: .4rem; }
.disclose:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.chev { display: inline-block; transition: transform .15s; color: var(--muted); }
.disclose[aria-expanded="true"] .chev { transform: rotate(90deg); }
.keys-table select { min-width: 12rem; }
.keys-table tr.todo { background: var(--warning-bg); }

.filters { margin-top: 1rem; align-items: flex-end; }
.bulk { margin-top: 1rem; padding: .6rem .9rem; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }

/* Listes */
.list { list-style: none; padding: 0; margin: .75rem 0 0; display: flex; flex-direction: column; gap: .5rem; }
.list li {
  display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: .6rem .9rem;
}
.list.compact li { justify-content: flex-start; padding: .4rem .75rem; font-size: .88rem; }
.list li input { flex: 1 1 200px; }
.ok-dot, .ko-dot, .wait-dot { width: .6rem; height: .6rem; border-radius: 50%; flex: none; }
.ok-dot { background: var(--success); }
.ko-dot { background: var(--danger); }
.wait-dot { background: var(--muted); }

.form-grid { display: flex; flex-direction: column; gap: .9rem; max-width: 620px; }
.form-grid input:not([type="checkbox"]):not([type="radio"]) { width: 100%; }

/* Fournisseurs */
.providers { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1rem; margin-top: .75rem; }
.provider { border: 1px solid var(--border); border-radius: 10px; padding: .9rem 1rem; display: flex; flex-direction: column; gap: .4rem; }
.provider h3 { margin: 0; }
.provider p { margin: 0; overflow-wrap: anywhere; }
.provider-form { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .3rem; }
.provider-form input { flex: 1 1 200px; }

.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%) translateY(200%);
  background: var(--text); color: var(--bg); padding: .7rem 1.2rem; border-radius: 14px;
  font-weight: 600; transition: transform .25s; z-index: 20; max-width: calc(100% - 2rem);
}
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 700px) {
  main { padding: 1rem; }
  .topbar { padding: .6rem 1rem; }
  .card { padding: 1rem; }
  .period #sync-status { margin-left: 0; }
  .period label:first-child { flex: 1 1 100%; }
  .kpi-value { font-size: 1.6rem; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
