/* ---------- Palette (thème CLI sombre muted) ---------- */
:root {
  color-scheme: dark;

  --bg: #0e1116;            /* fond général terminal */
  --surface: #161b22;       /* nodes, panneaux, topbar */
  --surface-2: #1c222b;     /* survols, code inline, badges */
  --surface-3: #232a34;     /* survols plus marqués */
  --border: #262c36;        /* bordures fines */
  --border-soft: #1f242c;   /* séparateurs discrets */
  --grid-dot: #3a4656;      /* points magnétiques de la grille (.board) */

  --text: #c9d1d9;          /* texte principal */
  --text-muted: #8b949e;    /* texte secondaire */
  --text-dim: #6e7681;      /* texte très discret / placeholder */

  --accent: #6d8fb5;        /* bleu-gris : sélection, poignées, flèche sélectionnée */
  --accent-soft: rgba(109, 143, 181, 0.16);
  --accent-line: rgba(109, 143, 181, 0.45);
  --green: #7aa874;         /* vert muted */
  --amber: #c9a554;         /* ambre muted */
  --danger: #b06060;        /* rouge muted : danger / cellule bloquée */
  --danger-soft: rgba(176, 96, 96, 0.12);

  --edge: #6e7681;          /* flèches SVG au repos */
  --edge-hover: #8b949e;
  --edge-back: #b8964f;     /* ambre muted : arêtes « retour » (contre-sens de lecture) */
  --edge-back-hover: #d0aa5e;

  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--mono);
  color: var(--text);
  background: var(--bg);
  scrollbar-color: var(--border) var(--bg);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

body.linking, body.linking .node { cursor: crosshair; }

/* ---------- Barre supérieure ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: none;
  z-index: 20;
}

#breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  min-width: 0;
}

#breadcrumb a {
  color: var(--text-muted);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

#breadcrumb a:hover { color: var(--text); text-decoration: underline; }

.crumb-sep { color: var(--text-dim); }

.crumb-current {
  font-weight: 600;
  color: var(--text);
  padding: 2px 6px;
  border-radius: 5px;
  white-space: nowrap;
  outline: none;
}

.crumb-current:hover { background: var(--surface-2); }
.crumb-current:focus { background: var(--surface-2); box-shadow: 0 0 0 2px var(--accent); }

/* ---------- Recherche plein texte ---------- */
#search {
  position: relative;
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
  margin: 0 10px;
}

#search[hidden] { display: none; }

#search-input {
  width: 100%;
  max-width: 320px;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 9px;
  font-size: 12px;
  font-family: var(--mono);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}

#search-input::placeholder { color: var(--text-dim); }
#search-input:focus { border-color: var(--accent); }

#search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 320px;
  max-height: 320px;
  overflow: auto;
  z-index: 30;
}

.search-result {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  text-align: left;
  border: none;
  background: transparent;
  padding: 7px 9px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-family: var(--mono);
  width: 100%;
}

.search-result:hover, .search-result.active { background: var(--surface-2); }

.search-excerpt {
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-grid {
  font-size: 10.5px;
  color: var(--text-dim);
}

/* ---------- Mini-aide (overlay raccourcis) ---------- */
#help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

#help-overlay[hidden] { display: none; }

#help-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.6);
  padding: 18px 20px;
  min-width: 320px;
  max-width: 90vw;
  font-family: var(--mono);
}

.help-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 12px;
}

.help-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  margin: 0;
  font-size: 12px;
}

.help-list dt {
  color: var(--amber);
  white-space: nowrap;
}

.help-list dd {
  margin: 0;
  color: var(--text-muted);
}

.help-hint {
  margin-top: 14px;
  font-size: 11px;
  color: var(--text-dim);
}

.topbar-right {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Indicateur de synchro (mode partagé). Rien affiché en mode local. */
.sync-status {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1;
  color: var(--text-muted);
  white-space: nowrap;
  user-select: none;
}
.sync-status[hidden] { display: none; }
.sync-status.sync-sync { color: var(--green); }
.sync-status.sync-busy { color: var(--text-muted); }
.sync-status.sync-offline { color: var(--danger); }
.sync-status.sync-reloaded { color: var(--amber); }

#app-menu-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  border-radius: 6px;
  font-size: 18px;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
}

#app-menu-btn:hover { background: var(--surface-2); color: var(--text); }

.dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 190px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  padding: 5px;
  display: flex;
  flex-direction: column;
}

.dropdown[hidden] { display: none; }

.dropdown button {
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-family: var(--mono);
}

.dropdown button:hover { background: var(--surface-2); }
.dropdown button.danger { color: var(--danger); }
.dropdown button.danger:hover { background: var(--danger-soft); }
.dropdown hr { border: none; border-top: 1px solid var(--border); margin: 4px 6px; }

/* ---------- Plateau ---------- */
#viewport {
  flex: 1;
  overflow: auto;
  background: var(--bg);
  scrollbar-color: var(--border) var(--bg);
  /* Centre le plateau (via .board-scale) quand il est plus petit que le viewport.
     min-* évite que le centrage ne rende le contenu inaccessible au scroll. */
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  padding: 24px;
}

/* Wrapper de dimensionnement : porte la taille naturelle × zoom (le transform du
   plateau n'affecte pas le layout, donc c'est lui qui définit la zone de scroll)
   et c'est lui qui se centre dans le viewport. */
.board-scale {
  position: relative;
  flex: none;
}

.board {
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  /* Bordure discrète matérialisant les limites de la grille bornée. */
  border: 1px solid var(--border);
  border-radius: 10px;
  /* Points magnétiques façon carnet pointé : un point à chaque intersection
     de la trame (centre des gaps, soit (12,12) + k·(264,164)) — visibles même
     quand les cellules sont occupées. Le gradient est centré dans sa tuile
     264×164, d'où le décalage (12−132, 12−82). */
  background-image: radial-gradient(circle, var(--grid-dot) 2px, transparent 2.5px);
  background-size: 264px 164px;
  background-position: -120px -70px;
}

.board-hint {
  position: absolute;
  top: 28px;
  left: 32px;
  font-size: 14px;
  color: var(--text-dim);
  pointer-events: none;
}

.cell-highlight {
  position: absolute;
  width: 240px;
  height: 140px;
  border: 2px dashed var(--accent);
  border-radius: 12px;
  background: var(--accent-soft);
  pointer-events: none;
  z-index: 1;
}

.cell-highlight.blocked {
  border-color: var(--danger);
  background: var(--danger-soft);
}

/* Curseur clavier : outline sobre de la taille d'une cellule, distinct de la
   sélection (accent) et du highlight de drag (dashed accent). */
.cell-cursor {
  position: absolute;
  width: 240px;
  height: 140px;
  border: 2px solid var(--text-muted);
  border-radius: 10px;
  background: transparent;
  pointer-events: none;
  /* Au-dessus des nodes (z-index 2, .dragging 10) pour rester visible sur une
     cellule occupée. Léger liseré sombre pour le détacher de la bordure node. */
  z-index: 12;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.55);
}

/* ---------- Flèches ---------- */
.edge-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.edge-line {
  fill: none;
  stroke: var(--edge);
  stroke-width: 2;
}

.edge-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 14;
  pointer-events: stroke;
  cursor: pointer;
}

g.edge:hover .edge-line { stroke: var(--edge-hover); }
g.edge.back .edge-line { stroke: var(--edge-back); }
g.edge.back:hover .edge-line { stroke: var(--edge-back-hover); }
g.edge.selected .edge-line { stroke: var(--accent); stroke-width: 2.5; }

.edge-temp {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 6 4;
  pointer-events: none;
}

/* ---------- Nodes ---------- */
.node {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  cursor: grab;
  z-index: 2;
}

.node.dragging {
  cursor: grabbing;
  opacity: 0.92;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.55);
  z-index: 10;
}

.node.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.node.link-target {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.node-content {
  height: 100%;
  padding: 10px 12px;
  overflow: hidden;
  font-size: 13px;
  line-height: 1.45;
  font-family: var(--mono);
  color: var(--text);
}

.node-content.empty { color: var(--text-dim); font-style: italic; }

.node-content h1 { font-size: 15px; margin: 0 0 4px; color: var(--text); }
.node-content h2 { font-size: 14px; margin: 0 0 3px; color: var(--text); }
.node-content h3, .node-content h4 { font-size: 13px; margin: 0 0 2px; color: var(--text); }
.node-content p { margin: 3px 0; }
.node-content ul, .node-content ol { margin: 3px 0; padding-left: 18px; }
.node-content li { margin: 1px 0; }
.node-content code {
  background: var(--surface-2);
  border-radius: 3px;
  padding: 1px 4px;
  font-size: 11.5px;
  font-family: var(--mono);
  color: var(--amber);
}
.node-content pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  margin: 4px 0;
  overflow: hidden;
}
.node-content pre code { background: none; padding: 0; color: var(--text); }
.node-content a { color: var(--accent); }
.node-content blockquote {
  border-left: 3px solid var(--border);
  margin: 3px 0;
  padding-left: 8px;
  color: var(--text-muted);
}
.node-content hr { border: none; border-top: 1px solid var(--border); margin: 6px 0; }
.node-content img { max-width: 100%; }

.node-editor {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  outline: 2px solid var(--accent);
  border-radius: 10px;
  resize: none;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.5;
  font-family: var(--mono);
  background: var(--surface);
  color: var(--text);
  z-index: 3;
}

.node-menu-btn {
  position: absolute;
  top: 4px;
  right: 5px;
  width: 24px;
  height: 20px;
  border: none;
  background: var(--surface-2);
  border-radius: 5px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 4;
}

.node:hover .node-menu-btn { opacity: 1; }
.node-menu-btn:hover { background: var(--surface-3); color: var(--text); }

.node-handle {
  position: absolute;
  width: 11px;
  height: 11px;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.12s;
  cursor: crosshair;
  z-index: 5;
}

.node:hover .node-handle { opacity: 1; }
.handle-top { top: -6px; left: calc(50% - 5.5px); }
.handle-bottom { bottom: -6px; left: calc(50% - 5.5px); }
.handle-left { left: -6px; top: calc(50% - 5.5px); }
.handle-right { right: -6px; top: calc(50% - 5.5px); }

.node-badges {
  position: absolute;
  bottom: 5px;
  right: 7px;
  display: flex;
  gap: 4px;
  z-index: 4;
}

.badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 21px;
  height: 21px;
  font-size: 12px;
  border-radius: 5px;
  background: var(--surface-2);
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  text-decoration: none;
}

.badge:hover { background: var(--surface-3); color: var(--text); }
.badge-grid { font-size: 14px; }

/* ---------- Panneau d'options ---------- */
.node-panel {
  position: fixed;
  width: 264px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 32px rgba(0, 0, 0, 0.55);
  padding: 12px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 12px;
  color: var(--text);
}

.panel-dates { color: var(--text-muted); font-size: 11px; line-height: 1.55; }

.panel-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-top: 2px;
}

.node-panel input[type="text"] {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12px;
  outline: none;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
}

.node-panel input[type="text"]::placeholder { color: var(--text-dim); }
.node-panel input[type="text"]:focus { border-color: var(--accent); }

.panel-btn {
  text-align: left;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-2);
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  font-family: var(--mono);
}

.panel-btn:hover { background: var(--surface-3); }
.panel-btn.danger { color: var(--danger); border-color: var(--border); }
.panel-btn.danger:hover { background: var(--danger-soft); }

.panel-empty { color: var(--text-dim); font-style: italic; font-size: 11px; }

.panel-revisions {
  max-height: 150px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.panel-rev {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 3px 5px;
  border-radius: 5px;
  font-size: 11px;
  color: var(--text-muted);
}

.panel-rev:hover { background: var(--surface-2); }

.panel-rev button {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  border-radius: 4px;
  padding: 1px 5px;
  font-family: var(--mono);
}

.panel-rev button:hover { background: var(--accent-soft); }

/* ---------- Contrôle de zoom (flottant, bas-droite) ---------- */
#zoom-ctl {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  align-items: stretch;
  gap: 1px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  z-index: 50;
  font-family: var(--mono);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
}

#zoom-ctl button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1;
  padding: 6px 10px;
  cursor: pointer;
}

#zoom-ctl button:hover { background: var(--surface-2); color: var(--text); }

#zoom-level {
  min-width: 48px;
  text-align: center;
  color: var(--text-muted);
  border-left: 1px solid var(--border-soft);
  border-right: 1px solid var(--border-soft);
}
