:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1f2328;
  --ink-soft: #5f6670;
  --line: #e1e4e8;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --ok: #1a7f37;
  --warn: #9a6700;
  --danger: #cf222e;
  --sel: #ddebff;
  /* Une seule famille pour toute l'app : la pile d'interface de VSCode. */
  --sans: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", system-ui, "Ubuntu", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --panel: #171b21;
    --ink: #e6e9ee;
    --ink-soft: #9aa3ae;
    --line: #2a3038;
    --accent: #4c8dff;
    --ok: #3fb950;
    --warn: #d29922;
    --danger: #f85149;
    --sel: #1c2c46;
  }
}

* { box-sizing: border-box; }

/* `hidden` doit gagner, toujours : une règle qui pose `display` battrait le
   `display: none` du navigateur, et l'élément resterait à l'écran. */
[hidden] { display: none !important; }

/* La page ne défile jamais : seules les zones internes le font. */
body {
  margin: 0; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg); color: var(--ink); font: 14px/1.45 var(--sans);
}

a { color: var(--accent); }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--ink-soft); }
.spacer { flex: 1; }
.pad { padding: 12px; }
.empty { padding: 48px; text-align: center; color: var(--ink-soft); }

/* --- Bandeau ---------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 52px; flex: none;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 16px; }
.tabs { display: flex; gap: 4px; height: 100%; }
.tabs a {
  display: flex; align-items: center; padding: 0 12px; color: var(--ink-soft);
  text-decoration: none; border-bottom: 2px solid transparent;
}
.tabs a.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

.pill { padding: 3px 10px; border-radius: 999px; font-size: 12px; text-decoration: none; white-space: nowrap; }
.pill.sim { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.pill.live { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.pill.warn { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }

#view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* --- Contrôles -------------------------------------------------------- */
input, select, textarea {
  font: inherit; color: inherit; background: var(--panel);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); border-color: var(--accent); }
textarea { width: 100%; resize: vertical; }
input[type=checkbox] { width: 15px; height: 15px; vertical-align: middle; }
input.short { width: 64px; }
fieldset { margin: 0; min-width: 0; }
fieldset:disabled { opacity: .7; }

.btn {
  font: inherit; cursor: pointer; padding: 6px 12px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--ink-soft); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.ok { color: var(--ok); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; color: var(--ink-soft); }
button.icon { border: 0; background: none; color: var(--ink-soft); cursor: pointer; font-size: 14px; padding: 2px 6px; border-radius: 4px; }
button.icon:hover:not(:disabled) { background: var(--bg); color: var(--ink); }
button.icon:disabled { opacity: .3; }
button.icon.danger:hover { color: var(--danger); }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--ink-soft); }
.field input, .field select { width: 100%; }
.inline { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: var(--bg); border: 1px solid var(--line); white-space: nowrap; }
.s-accepted, .e-sent, .r-ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.s-excluded, .s-unsubscribed, .r-error, .e-interrupted { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.e-sending { color: var(--accent); border-color: var(--accent); }
.e-paused { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 50%, transparent); }
.tag { display: inline-block; margin: 0 4px 2px 0; padding: 0 6px; border-radius: 4px; font-size: 12px; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 16px; margin-bottom: 16px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head h3 { margin: 0; font-size: 15px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 10;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; max-width: min(640px, 90vw);
  box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.toast.error { background: var(--danger); color: #fff; }

/* --- Contacts --------------------------------------------------------- */
.view-contacts { padding: 16px 20px 0; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 10px; }
.toolbar input[type=search] { width: 280px; }
.bulk { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 12px; margin-bottom: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.bulk.off .btn:not(.ghost) { opacity: .45; pointer-events: none; }
.table-wrap { flex: 1; min-height: 0; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.grid th { position: sticky; top: 0; background: var(--panel); font-size: 12px; color: var(--ink-soft); cursor: pointer; user-select: none; white-space: nowrap; z-index: 1; }
.grid th.sorted { color: var(--ink); }
.grid th.sorted[data-dir=desc]::after { content: " ↓"; }
.grid th.sorted[data-dir=asc]::after { content: " ↑"; }
.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.grid tr.sel td { background: var(--sel); }
.grid td.mono { word-break: break-all; }
.grid.small td, .grid.small th { padding: 4px 8px; font-size: 12.5px; cursor: default; }
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 10px; flex: none; }

/* --- Campagnes -------------------------------------------------------- */
.view-campaigns { flex-direction: row !important; }
.camp-aside { width: 270px; flex: none; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; }
.aside-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px; }
.aside-head h2 { margin: 0; font-size: 15px; }
.camp-list { overflow: auto; flex: 1; }
.camp-item { display: block; padding: 10px 14px; border-top: 1px solid var(--line); color: inherit; text-decoration: none; }
.camp-item:hover { background: var(--bg); }
.camp-item.active { background: var(--sel); }
.camp-name { font-weight: 600; margin-bottom: 4px; }
.camp-meta { display: flex; gap: 8px; align-items: center; font-size: 12px; }

.editor { flex: 1; min-width: 0; overflow: auto; padding: 16px 20px; }
.ed-head { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.title-input { font-size: 18px; font-weight: 600; border-color: transparent; background: transparent; min-width: 280px; }
.title-input:hover { border-color: var(--line); }
.ed-grid { display: grid; grid-template-columns: minmax(380px, 1fr) minmax(380px, 1fr); gap: 20px; align-items: start; }
.ed-right { position: sticky; top: 0; }

.blocks { display: flex; flex-direction: column; gap: 10px; }
.block { border: 1px solid var(--line); border-radius: 8px; padding: 10px; background: var(--bg); }
.block-head { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.block-type { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-soft); }
.block > input, .block textarea { width: 100%; }
.row2 { display: flex; gap: 10px; align-items: flex-start; }
.row2 > * { flex: 1; min-width: 0; }
.row2 > .thumb { flex: none; }
.image-block input[type=file] { margin-bottom: 6px; width: 100%; }
.image-block input:not([type]) { width: 100%; }
.thumb { width: 96px; height: 72px; object-fit: cover; border-radius: 4px; border: 1px solid var(--line); }
.add-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 12px; }

.preview-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.subject { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-meta { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; flex: none; }
.seg button { font: inherit; font-size: 12.5px; border: 0; background: var(--panel); color: var(--ink-soft); padding: 4px 10px; cursor: pointer; }
.seg button.active { background: var(--accent); color: var(--accent-ink); }
.frame-wrap { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #f3f4f6; margin: 0 auto; transition: width .2s; }
.frame-wrap.mobile { width: 390px; }
.frame-wrap iframe { display: block; width: 100%; height: calc(100vh - 200px); height: calc(100dvh - 200px); border: 0; }
.text-version { white-space: pre-wrap; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 16px; font: 13px/1.5 var(--mono); max-height: calc(100dvh - 200px); overflow: auto; margin: 0; }

.send .stats { margin: 6px 0 10px; }
.send-row { display: flex; gap: 8px; margin-bottom: 10px; }
.send-row input { flex: 1; }
.progress { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.progress > div { height: 100%; background: var(--accent); transition: width .4s; }
.warn-box { padding: 8px 12px; border-radius: 6px; background: color-mix(in srgb, var(--warn) 14%, transparent); }
details summary { cursor: pointer; color: var(--ink-soft); }
details .grid { margin-top: 8px; }

/* --- Réglages --------------------------------------------------------- */
.view-settings { overflow: auto; }
.settings { max-width: 820px; margin: 0 auto; padding: 20px; width: 100%; }
.kv { border-collapse: collapse; width: 100%; margin-bottom: 8px; }
.kv th, .kv td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.kv th { font-weight: 500; color: var(--ink-soft); width: 45%; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 8px; }
.dot.ok { background: var(--ok); }
.dot.ko { background: var(--danger); }
.settings ol li { margin-bottom: 6px; }

@media (max-width: 1100px) {
  .ed-grid { grid-template-columns: 1fr; }
  .ed-right { position: static; }
}
@media (max-width: 720px) {
  .view-campaigns { flex-direction: column !important; }
  .camp-aside { width: 100%; max-height: 35vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .topbar { gap: 12px; padding: 0 12px; }
  .toolbar input[type=search] { width: 100%; }
  .frame-wrap.mobile { width: 100%; }
}

/* --- Droits : une commande non permise est masquée (voir UI.setRights) --- */
.right-hidden { display: none !important; }

/* --- Connexion ----------------------------------------------------------- */
.view-login { align-items: center; justify-content: center; }
.login-wrap { width: 100%; max-width: 420px; padding: 24px 16px; margin: auto; }
.login h1 { font-size: 20px; margin: 0 0 18px; }
.btn.block { display: block; text-align: center; text-decoration: none; margin-bottom: 10px; }
.or { text-align: center; margin: 12px 0; }
.login-form { display: flex; flex-direction: column; gap: 8px; }
.login-form input { width: 100%; }
#who { font-size: 12.5px; }

/* --- Accès --------------------------------------------------------------- */
.settings.wide { max-width: 1100px; }
.token-form { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 14px; }
.token-form .send-row input:not(.short) { flex: 1; }
code.secret { display: block; word-break: break-all; padding: 8px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; user-select: all; }
.view-access, .view-log, .view-audience { overflow: auto; }

/* --- Livres ------------------------------------------------------------- */
.mini-cover { width: 44px; height: 66px; object-fit: cover; border-radius: 2px; display: block; }
.books-table td { vertical-align: middle; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.inline-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }

/* Audience : les totaux en tuiles, les visites par jour en barres, puis provenances et pages. */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin: 12px 0 16px; }
.tile { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; }
.tile strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.tile span { font-size: 12px; }
.chart { margin-bottom: 18px; }
.bars { display: flex; align-items: flex-end; height: 140px; border-bottom: 1px solid var(--line); }
.bar-slot { flex: 1; height: 100%; display: flex; align-items: flex-end; padding: 0 1px; }
.bar-slot:hover { background: var(--bg); }
.bar { width: 100%; min-height: 1px; background: var(--accent); border-radius: 4px 4px 0 0; }
.axis { display: flex; justify-content: space-between; font-size: 12px; margin-top: 4px; }
.small-print { font-size: 12.5px; margin: 0 0 4px; }
.audience-cols { display: grid; grid-template-columns: minmax(220px, 1fr) 2.4fr; gap: 20px; align-items: start; }
.audience-cols h4 { margin: 0 0 8px; font-size: 13.5px; }
.table-scroll { max-height: 60vh; overflow: auto; }
.grid tr.clickable { cursor: pointer; }
.grid tr.clickable:hover td { background: var(--bg); }
@media (max-width: 820px) { .audience-cols { grid-template-columns: 1fr; } }

/* Les commandes : l'adresse se lit comme sur l'enveloppe, la dédicace à part. */
.s-to-ship { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 50%, transparent); }
.orders-table .address { line-height: 1.4; user-select: all; }
.orders-table .dedication { margin-top: 4px; }
.orders-table td { vertical-align: top; }
