:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --ok-bg: #e6f4ea;
  --ok-fg: #1e6b34;
  --warn-bg: #fdecea;
  --warn-fg: #a12622;
  --test-bg: #eef2ff;
  --test-fg: #3b4cb8;
  --live-bg: #fff4e5;
  --live-fg: #8a4b00;
  --muted-bg: #eef0f3;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --border: #2a3039;
    --text: #e6e9ee;
    --muted: #97a2b0;
    --accent: #4c8dff;
    --accent-text: #0b0d10;
    --danger: #ff6b6b;
    --ok-bg: #173323;
    --ok-fg: #7fd69a;
    --warn-bg: #3a1a1a;
    --warn-fg: #ff9a94;
    --test-bg: #1d2448;
    --test-fg: #a9b6ff;
    --live-bg: #3a2a12;
    --live-fg: #ffc875;
    --muted-bg: #222831;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "brand nav who";
  align-items: center;
  gap: 8px 24px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { grid-area: brand; }
.topbar .nav { grid-area: nav; min-width: 0; }
.topbar .who { grid-area: who; }

.balance-chip {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.balance-chip:hover, .balance-chip[aria-current="page"] { background: var(--muted-bg); text-decoration: none; }

.banner {
  padding: 10px 24px;
  background: var(--warn-bg);
  color: var(--warn-fg);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.banner a { color: inherit; font-weight: 650; text-decoration: underline; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); }
.brand svg, .login-logo svg { flex-shrink: 0; }
/* Couleurs du thème : la CSP interdit les attributs style, le SVG garde ses couleurs par défaut pour le favicon. */
.brand .logo-bg, .login-logo .logo-bg { fill: var(--accent); }
.brand .logo-fg, .login-logo .logo-fg { stroke: var(--accent-text); }
.brand svg { width: 26px; height: 26px; }
.login-logo svg { width: 44px; height: 44px; display: block; margin-bottom: 12px; }
.brand span { color: var(--muted); font-weight: 400; }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }

main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; }

h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.row-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--muted-bg); }
tr.revoked td { color: var(--muted); }
td.num { font-variant-numeric: tabular-nums; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 550;
  background: var(--muted-bg);
  color: var(--muted);
}
.badge.active { background: var(--ok-bg); color: var(--ok-fg); }
.badge.suspended, .badge.revoked { background: var(--warn-bg); color: var(--warn-fg); }
.badge.test { background: var(--test-bg); color: var(--test-fg); }
.badge.live { background: var(--live-bg); color: var(--live-fg); }

button, input, select { font: inherit; }
button {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 14px;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 550;
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.danger { background: transparent; color: var(--danger); border-color: var(--border); }
button.small { padding: 3px 10px; font-size: 13px; }

input, select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

form.inline { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
form.inline input { flex: 1 1 200px; }

.login { max-width: 360px; margin: 10vh auto 0; }
.login form { display: grid; gap: 12px; }
.login label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.login h1 { margin-bottom: 4px; }

.error { color: var(--danger); font-size: 14px; margin: 0; }
.muted { color: var(--muted); }
.small-text { font-size: 13px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.stat { padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius); }
.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }

.secret {
  border: 1px solid var(--live-fg);
  background: var(--live-bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.secret .key { display: flex; gap: 8px; margin: 10px 0; }
.secret .key input { flex: 1; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }

.audit { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.audit li { padding: 8px 0; border-bottom: 1px solid var(--border); display: flex; gap: 12px; flex-wrap: wrap; }
.audit li:last-child { border-bottom: none; }
.audit time { color: var(--muted); min-width: 140px; }

.empty { color: var(--muted); padding: 24px 0; text-align: center; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 14px;
  max-width: calc(100vw - 32px);
}

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  .who #who-email { display: none; }
}

.card form.inline + .table-wrap,
.card form.inline + .empty { margin-top: 16px; }

.gap { height: 10px; }
h2.subtitle { margin-top: 20px; font-size: 14px; color: var(--muted); }

/* ── Navigation ─────────────────────────────────────────────────────────── */
.nav { display: flex; gap: 4px; }
.nav a { color: var(--muted); padding: 6px 10px; border-radius: 6px; font-size: 14px; font-weight: 550; white-space: nowrap; }
.nav a:hover { color: var(--text); background: var(--muted-bg); text-decoration: none; }
.nav a[aria-current="page"] { color: var(--text); background: var(--muted-bg); }

/* ── Formulaire utilisateur ─────────────────────────────────────────────── */
form.stack { display: grid; gap: 14px; }
form.stack p { margin: 0; }
form.stack label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
form.stack .fields { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
form.stack .fields.phones { grid-template-columns: repeat(3, 1fr); }
form.stack .fields.prices { grid-template-columns: repeat(2, 1fr); }
.price-line { display: grid; gap: 6px; }
form.stack label.check { display: flex; align-items: center; gap: 6px; color: var(--text); }
.phones-cell { white-space: nowrap; font-variant-numeric: tabular-nums; }
.badge.pending { background: var(--live-bg); color: var(--live-fg); }
.badge.admin { background: var(--test-bg); color: var(--test-fg); }
tr.me td:first-child::after { content: " (vous)"; color: var(--muted); }

@media (max-width: 720px) {
  /* Marque, solde et déconnexion en haut ; le menu défile en dessous. */
  .topbar { grid-template-columns: 1fr auto; grid-template-areas: "brand who" "nav nav"; padding: 10px 16px 0; gap: 8px; }
  .topbar .nav { overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 12px 8px; }
  .topbar .nav::-webkit-scrollbar { display: none; }
  .who #who-email, .who #who-role { display: none; }
  .banner { padding: 10px 16px; }
}

@media (max-width: 640px) {
  .brand span { display: none; }
  form.stack .fields,
  form.stack .fields.phones,
  form.stack .fields.prices { grid-template-columns: 1fr; }
}
.row-left.actions { flex-wrap: nowrap; gap: 6px; }

/* ── Champ mot de passe avec œil ────────────────────────────────────────── */
.password-field { position: relative; display: flex; }
.password-field input { flex: 1; padding-right: 40px; }
button.reveal {
  position: absolute;
  top: 50%;
  right: 4px;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
}
button.reveal:hover { color: var(--text); background: var(--muted-bg); }
button.reveal[aria-pressed="true"] { color: var(--accent); }

/* ── Solde ──────────────────────────────────────────────────────────────── */
td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
td.num.credit { color: var(--ok-fg); }
tr.uncounted td.num { color: var(--muted); }
tr.void td.num span { text-decoration: line-through; }

/* ── Tableaux en fiches sur mobile ──────────────────────────────────────── */
th.num { text-align: right; }
@media (max-width: 640px) {
  table.responsive thead { display: none; }
  table.responsive tr { display: block; padding: 10px 0; border-bottom: 1px solid var(--border); }
  table.responsive tr:last-child { border-bottom: none; }
  table.responsive td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 4px 0;
    border: none;
    white-space: normal;
    text-align: right;
  }
  table.responsive td::before { content: attr(data-label); color: var(--muted); font-size: 13px; text-align: left; flex-shrink: 0; }
  table.responsive td[data-label=""]::before { content: none; }
  table.responsive td[data-label=""] { justify-content: flex-end; }
  table.responsive td.empty { justify-content: center; }
  .row-left.actions { flex-wrap: wrap; justify-content: flex-end; }
}

/* ── Solde ─────────────────────────────────────────────────────────────── */
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.stats.tariffs { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 12px; }
.stats.tariffs .value { font-size: 17px; }
.stat.off .value { color: var(--muted); font-size: 15px; }

/* ── Tarifs (admin) ────────────────────────────────────────────────────── */
.prices-table td { vertical-align: middle; }
.prices-table tr.off td:first-child > div:first-child { color: var(--muted); text-decoration: line-through; }
.input-suffix { display: inline-flex; align-items: center; gap: 6px; }
.input-suffix input { width: 110px; text-align: right; font-variant-numeric: tabular-nums; }
.input-suffix span { color: var(--muted); font-size: 14px; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); cursor: pointer; }
details.help { font-size: 14px; color: var(--muted); }
details.help summary { cursor: pointer; color: var(--text); font-weight: 550; }
details.help ul { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 4px; }

.warn-text { color: var(--warn-fg); font-size: 13px; }

/* Hors session (connexion, mot de passe), ni barre ni bandeau. */
.topbar[hidden] + .banner { display: none !important; }
.login-back { text-align: center; font-size: 14px; margin-top: 16px; }
@media (max-width: 640px) { .login { margin-top: 24px; } }

.nav a.external { display: inline-flex; align-items: center; gap: 4px; }
.nav a.external svg { width: 14px; height: 14px; }
@media (max-width: 640px) {
  .row > input[type="search"], .row .row-left, .row .row-left input[type="search"] { width: 100%; }
  .row .row-left input[type="search"] { flex: 1; }
}

/* ── Pied de page (repris des pages publiques) ─────────────────────────── */
/* Collé en bas même quand la page est courte. */
body { min-height: 100vh; display: flex; flex-direction: column; }
body > main { flex: 1; width: 100%; }
.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px 16px;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 16px 32px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}
.footer-links { display: flex; flex-wrap: wrap; gap: 8px 16px; }
@media (max-width: 640px) { .footer { flex-direction: column; align-items: flex-start; } }
