
/* ---------- Design tokens : clair minimaliste ---------- */
:root {
  --bg: #ffffff;
  --surface: #fafafa;
  --surface-2: #f4f5f7;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --muted: #6b7280;
  --muted-2: #9ca3af;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #eff6ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #059669;
  --success-soft: #ecfdf5;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 4px 6px -2px rgba(16, 24, 40, .03), 0 12px 16px -4px rgba(16, 24, 40, .08);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Structure ---------- */

.site-header {
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 20;
}

.site-header .inner {
  max-width: var(--page-width, 880px);
  margin: 0 auto;
  padding: 0 20px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 600;
  letter-spacing: -.01em;
  color: var(--text);
  font-size: 15px;
}
.brand:hover { text-decoration: none; }
.brand .dot {
  width: 9px; height: 9px;
  border-radius: 3px;
  background: var(--accent);
  flex: none;
}

.header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

main {
  max-width: var(--page-width, 880px);
  margin: 0 auto;
  padding: 40px 20px 64px;
}
main.wide { --page-width: 1140px; }

.site-footer {
  max-width: var(--page-width, 880px);
  margin: 0 auto;
  padding: 0 20px 40px;
  color: var(--muted-2);
  font-size: 13px;
}

.page-title {
  font-size: 26px;
  font-weight: 650;
  letter-spacing: -.02em;
  margin: 0 0 6px;
}
.page-sub {
  color: var(--muted);
  margin: 0 0 28px;
  font-size: 14.5px;
}

.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-body { padding: 20px; }

/* ---------- Éditeur ---------- */

.editor {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: var(--shadow);
  transition: border-color .15s, box-shadow .15s;
}
.editor:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.editor textarea {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.65;
  padding: 16px;
  resize: vertical;
  min-height: 320px;
  border-radius: var(--radius);
}
.editor textarea:focus { outline: none; }
.editor textarea::placeholder { color: var(--muted-2); }
.editor textarea[readonly] { color: var(--text); }

.editor-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12.5px;
  background: var(--surface);
  border-radius: 0 0 var(--radius) var(--radius);
}
.editor-foot .spacer { margin-left: auto; }
.counter { font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12px; }
.counter.over { color: var(--danger); font-weight: 600; }

/* ---------- Contrôles ---------- */

.field { margin-top: 26px; }
.field-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin-bottom: 10px;
}

.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.seg {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 15px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-height: 38px;
  transition: background .13s, color .13s, border-color .13s;
}
.seg:hover { color: var(--text); background: rgba(255,255,255,.75); }
.seg[aria-pressed="true"] {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border-strong);
  box-shadow: var(--shadow);
  font-weight: 600;
}

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font: inherit;
  font-size: 14px;
  font-weight: 550;
  padding: 9px 17px;
  min-height: 40px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  transition: background .13s, border-color .13s, color .13s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:active { background: var(--border); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-danger { color: var(--danger); border-color: #fecaca; background: var(--bg); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.btn-sm { font-size: 13px; padding: 6px 11px; min-height: 32px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 26px;
}
.actions .spacer { margin-left: auto; }

/* ---------- Badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 550;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge-accent { background: var(--accent-soft); border-color: #bfdbfe; color: #1e40af; }
.badge-success { background: var(--success-soft); border-color: #a7f3d0; color: #065f46; }
.badge-warn { background: var(--warn-soft); border-color: #fde68a; color: #92400e; }
.badge-danger { background: var(--danger-soft); border-color: #fecaca; color: #991b1b; }

.mono { font-family: var(--mono); }

.paste-id {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 2px 7px;
  border-radius: 6px;
}

/* ---------- Notice / erreurs ---------- */

.notice {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  padding: 28px;
  text-align: center;
  box-shadow: var(--shadow);
}
.notice h1 { font-size: 20px; margin: 0 0 8px; letter-spacing: -.01em; }
.notice p { color: var(--muted); margin: 0 0 20px; }
.notice-icon {
  width: 44px; height: 44px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface-2);
  margin-bottom: 14px;
  font-size: 20px;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Tableau admin ---------- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 26px;
}
.stat {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat .label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 600;
}
.stat .value {
  font-size: 23px;
  font-weight: 650;
  letter-spacing: -.02em;
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
}
.stat .value.alert { color: var(--danger); }

.table-wrap {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}

table.grid {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
table.grid th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 600;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  white-space: nowrap;
}
table.grid td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.grid tr:last-child td { border-bottom: 0; }
table.grid tr:hover td { background: var(--surface); }
.row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

select.input, input.input {
  font: inherit;
  font-size: 13px;
  padding: 6px 9px;
  min-height: 32px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}

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

/* ---------- Collaboration ---------- */

.collab-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.peers { display: flex; align-items: center; gap: -6px; }
.peer-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 550;
  padding: 4px 10px 4px 5px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  margin-right: 6px;
}
.peer-swatch { width: 9px; height: 9px; border-radius: 50%; flex: none; }

.conn-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 500;
}
.conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted-2);
  flex: none;
  transition: background .2s;
}
.conn-status[data-state="open"] .conn-dot { background: var(--success); }
.conn-status[data-state="connecting"] .conn-dot { background: var(--warn); animation: pulse 1.1s ease-in-out infinite; }
.conn-status[data-state="closed"] .conn-dot { background: var(--danger); }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

.save-state { font-size: 12.5px; color: var(--muted-2); font-variant-numeric: tabular-nums; }

/* Surcouche de curseurs : superposée au textarea, jamais cliquable. */
.collab-editor { position: relative; }
.cursor-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  border-radius: var(--radius);
}
.cursor-mirror {
  position: absolute;
  top: 0; left: 0;
  visibility: hidden;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}
.remote-caret {
  position: absolute;
  width: 2px;
  border-radius: 1px;
  transition: transform .08s linear;
}
.remote-caret::after {
  content: attr(data-name);
  position: absolute;
  top: -18px;
  left: -1px;
  font-size: 10.5px;
  font-family: var(--sans);
  font-weight: 600;
  color: #fff;
  background: inherit;
  padding: 1px 5px;
  border-radius: 4px 4px 4px 0;
  white-space: nowrap;
  opacity: 0;
  transition: opacity .15s;
}
.remote-caret.active::after { opacity: 1; }
.remote-selection { position: absolute; border-radius: 2px; opacity: .18; }

/* ---------- Markdown : coloration en direct ----------
 *
 * `.md-content` est peint derrière un textarea au texte transparent. Les deux
 * boîtes doivent avoir des métriques identiques, sinon la coloration se décale.
 * D'où la police monospace: son gras conserve la même chasse, contrairement à
 * une police proportionnelle. Aucun style ne doit modifier la largeur d'un
 * caractère (pas de font-size variable sur les titres).
 */

.md-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  border-radius: var(--radius);
  z-index: 0;
}

.md-content {
  position: absolute;
  top: 0;
  left: 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  color: var(--text);
  will-change: transform;
}

/* Le texte devient transparent: c'est la couche qui l'affiche. Le curseur et
   la sélection restent natifs, donc le comportement mobile est préservé. */
.editor textarea.md-enabled {
  color: transparent;
  caret-color: var(--text);
  background: transparent;
  position: relative;
  z-index: 1;
}
.editor textarea.md-enabled::selection { background: rgba(37, 99, 235, .22); }

/* Styles neutres pour la géométrie: couleur, fond, soulignement et ombre ne
   déplacent aucun caractère. L'ombre portée tient lieu de gras: elle épaissit
   le trait sans toucher à la chasse. */
.md-marker { color: var(--muted-2); }
.md-emphasis { text-shadow: 0 0 .4px currentColor, 0 0 .4px currentColor; }

.md-h { color: #1e3a8a; text-shadow: 0 0 .4px currentColor, 0 0 .4px currentColor; }
.md-h1 { color: #172554; }
.md-bold { color: var(--text); text-shadow: 0 0 .4px currentColor, 0 0 .4px currentColor; }
.md-italic { color: #6d28d9; }
.md-strike { text-decoration: line-through; color: var(--muted); }
.md-mark { background: #fef08a; border-radius: 2px; }
.md-code { color: #9d174d; background: #fdf2f8; border-radius: 3px; }
.md-code-block { color: #334155; background: var(--surface-2); }
.md-code-lang { color: var(--muted); }
.md-quote { color: var(--muted); }
.md-hr { color: var(--muted-2); text-shadow: 0 0 .4px currentColor; }
.md-link-text { color: var(--accent); text-decoration: underline; }
.md-link-url { color: var(--muted-2); }
.md-ordered { color: var(--accent); text-shadow: 0 0 .4px currentColor; }
.md-task { color: var(--accent); text-shadow: 0 0 .4px currentColor; }
.md-task.done { color: var(--success); }
.md-task-text.done { text-decoration: line-through; color: var(--muted); }

/* Vrai gras / vraie italique, activés seulement quand la mesure au démarrage a
   confirmé qu'ils ne changent pas la largeur des caractères. */
.md-bold-real .md-bold,
.md-bold-real .md-h,
.md-bold-real .md-ordered,
.md-bold-real .md-task,
.md-bold-real .md-hr { font-weight: 700; text-shadow: none; }

.md-italic-real .md-italic { font-style: italic; color: inherit; }
.md-italic-real .md-quote { font-style: italic; }

/* La puce est peinte par-dessus le tiret source, hors du flux: le caractère
   d'origine garde donc exactement sa largeur naturelle. */
.md-bullet {
  position: relative;
  color: transparent;
}
.md-bullet::before {
  content: "•";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  text-align: center;
  color: var(--accent);
  text-shadow: 0 0 .4px currentColor;
}

/* ---------- Barre d'outils Markdown ---------- */

.md-toolbar {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--surface);
  overflow-x: auto;
  scrollbar-width: none;
  position: relative;
  z-index: 3;
}
.md-toolbar::-webkit-scrollbar { display: none; }

.md-toolbar .sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
  flex: none;
}

.md-btn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1;
  min-width: 34px;
  height: 34px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex: none;
  transition: background .12s, color .12s;
}
.md-btn:hover { background: var(--bg); color: var(--text); border-color: var(--border); }
.md-btn:active { background: var(--surface-2); }
.md-btn b { font-weight: 800; }
.md-btn i { font-style: italic; font-family: Georgia, serif; }
.md-btn s { text-decoration: line-through; }
.md-btn .mono { font-family: var(--mono); font-size: 12.5px; }

/* L'éditeur collaboratif est collé sous la barre d'outils. */
.editor.with-toolbar { border-radius: 0 0 var(--radius) var(--radius); }
.editor.with-toolbar .collab-editor > textarea { border-radius: 0; }

@media (max-width: 700px) {
  .md-toolbar {
    gap: 2px;
    padding: 5px 6px;
    /* Dégradé de bord: signale que la barre continue au-delà de l'écran. */
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
  }
  /* Cibles tactiles confortables, et la barre reste atteignable au pouce. */
  .md-btn { min-width: 40px; height: 40px; font-size: 15px; }
  .md-toolbar.floating {
    position: fixed;
    left: 0;
    right: 0;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    box-shadow: 0 -2px 8px rgba(16, 24, 40, .08);
    z-index: 60;
  }
}

/* ---------- Panneau historique ---------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, .35);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 40;
}
.drawer-backdrop.show { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100%;
  width: min(460px, 100%);
  background: var(--bg);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  transition: transform .22s cubic-bezier(.4, 0, .2, 1);
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.drawer.show { transform: translateX(0); }

.drawer-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex: none;
}
.drawer-head h2 { font-size: 15px; margin: 0; font-weight: 600; }
.drawer-head .spacer { margin-left: auto; }
.drawer-body { overflow-y: auto; flex: 1; }

.version {
  padding: 13px 18px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .12s;
}
.version:hover { background: var(--surface); }
.version[aria-selected="true"] { background: var(--accent-soft); }
.version .when { font-weight: 600; font-size: 13.5px; }
.version .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }

.diff {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  border-top: 1px solid var(--border);
  background: var(--surface);
  max-height: 42vh;
  overflow: auto;
  flex: none;
}
.diff-line { padding: 0 12px; white-space: pre-wrap; overflow-wrap: break-word; }
.diff-line.add { background: var(--success-soft); color: #065f46; }
.diff-line.del { background: var(--danger-soft); color: #991b1b; }
.diff-line.ctx { color: var(--muted); }
.diff-empty { padding: 20px; color: var(--muted); font-family: var(--sans); font-size: 13px; }

.drawer-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  flex: none;
}

/* ---------- Responsive ---------- */

@media (max-width: 700px) {
  main { padding: 26px 16px 48px; }
  .site-header .inner { padding: 0 16px; }
  .page-title { font-size: 22px; }
  .segmented { display: flex; width: 100%; }
  .seg { flex: 1 1 auto; }
  .actions .btn { flex: 1 1 auto; }
  .editor textarea { min-height: 240px; font-size: 13px; }

  /* Le tableau admin devient une pile de cartes. */
  table.grid, table.grid tbody, table.grid tr, table.grid td { display: block; width: 100%; }
  table.grid thead { display: none; }
  table.grid tr {
    border-bottom: 1px solid var(--border);
    padding: 12px 4px;
  }
  table.grid tr:hover td { background: transparent; }
  table.grid td {
    border: 0;
    padding: 3px 12px;
    display: flex;
    gap: 12px;
    align-items: baseline;
  }
  table.grid td::before {
    content: attr(data-label);
    flex: none;
    width: 92px;
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    font-weight: 600;
  }
  .row-actions { justify-content: flex-start; padding-top: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
