:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --bg-card: #1e293b;
  --bg-elev: #273449;
  --border: #334155;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --text-faint: #64748b;
  --accent: #10b981;
  --accent-dark: #059669;
  --danger: #ef4444;

  /* Code couleur des couches */
  --layer-explication: #3b82f6; /* bleu */
  --layer-recul: #a855f7;       /* violet */
  --layer-nuance: #f59e0b;      /* orange */
  --layer-contexte: #22c55e;    /* vert */
  --layer-tronc: #64748b;       /* gris (bloc racine) */
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

button { font-family: inherit; cursor: pointer; border: none; }
input, textarea { font-family: inherit; }

/* ---------- Auth screen ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 0%, #14532d22, transparent 60%), var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 24px;
  box-shadow: 0 20px 60px #0008;
}
.brand {
  text-align: center;
  margin-bottom: 28px;
}
.brand-logo {
  font-size: 40px;
  margin-bottom: 8px;
}
.brand h1 {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.5px;
}
.brand p { color: var(--text-dim); font-size: 14px; margin-top: 6px; }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; font-weight: 500; }
.field input {
  width: 100%;
  padding: 13px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 16px;
  transition: border-color .15s;
}
.field input:focus { outline: none; border-color: var(--accent); }

.btn {
  width: 100%;
  padding: 14px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: var(--accent);
  color: #03291c;
  transition: transform .1s, background .15s;
}
.btn:active { transform: scale(.98); }
.btn:hover { background: var(--accent-dark); }
.btn.secondary { background: var(--bg-elev); color: var(--text); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; color: var(--text-dim); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.auth-switch { text-align: center; margin-top: 18px; font-size: 14px; color: var(--text-dim); }
.auth-switch a { color: var(--accent); cursor: pointer; font-weight: 600; }
.auth-error {
  background: #ef444422; border: 1px solid #ef444455; color: #fca5a5;
  padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 16px;
}

/* ---------- App header ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
}
.topbar .title { font-weight: 700; font-size: 17px; flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar .title .crumb { color: var(--text-faint); font-weight: 500; }
.topbar .title span.tree-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  background: var(--bg-elev); color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.icon-btn:active { background: var(--border); }

/* ---------- Forest (liste arbres) ---------- */
.content { flex: 1; padding: 16px; max-width: 760px; width: 100%; margin: 0 auto; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.section-head h2 { font-size: 20px; font-weight: 800; }
.section-head .sub { color: var(--text-dim); font-size: 13px; margin-top: 2px; }

.tree-grid { display: flex; flex-direction: column; gap: 12px; }
.tree-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  display: flex; align-items: center; gap: 14px;
  transition: transform .1s, border-color .15s;
}
.tree-card:active { transform: scale(.99); }
.tree-card .tree-ico {
  width: 44px; height: 44px; border-radius: 12px; flex-shrink: 0;
  background: linear-gradient(135deg, #14532d, #166534);
  display: flex; align-items: center; justify-content: center; font-size: 20px; color: #4ade80;
}
.tree-card .tree-body { flex: 1; min-width: 0; }
.tree-card .tree-body h3 { font-size: 16px; font-weight: 700; margin-bottom: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-card .tree-body p { color: var(--text-dim); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-card .tree-meta { color: var(--text-faint); font-size: 12px; flex-shrink: 0; text-align: right; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-dim); }
.empty-state .big { font-size: 50px; margin-bottom: 16px; opacity: .6; }
.empty-state h3 { color: var(--text); font-size: 18px; margin-bottom: 8px; }
.empty-state p { font-size: 14px; max-width: 320px; margin: 0 auto 20px; line-height: 1.5; }

.fab {
  position: fixed; bottom: 24px; right: 24px; z-index: 40;
  width: 58px; height: 58px; border-radius: 18px;
  background: var(--accent); color: #03291c;
  font-size: 24px; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 30px #10b98155;
}
.fab:active { transform: scale(.94); }

/* ---------- Tree view (canvas) ---------- */
.tree-view { flex: 1; position: relative; overflow: hidden; background:
  radial-gradient(circle at 20% 20%, #1e293b55, transparent 50%), var(--bg); }
#tree-svg { width: 100%; height: 100%; display: block; touch-action: none; }

.node-card { cursor: pointer; }
.legend {
  position: fixed; bottom: 16px; left: 16px; z-index: 30;
  background: var(--bg-soft)ee; border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 12px; display: flex; flex-wrap: wrap; gap: 10px;
  font-size: 11px; max-width: calc(100vw - 32px); backdrop-filter: blur(8px);
}
.legend .lg { display: flex; align-items: center; gap: 5px; color: var(--text-dim); }
.legend .dot { width: 10px; height: 10px; border-radius: 50%; }

.tree-actions { position: fixed; bottom: 24px; right: 16px; z-index: 40; display: flex; flex-direction: column; gap: 10px; }

/* ---------- Modal / sheet ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: #00000088; backdrop-filter: blur(3px);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 560px;
  background: var(--bg-card);
  border-radius: 20px 20px 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
  padding: 20px;
  max-height: 90vh; overflow-y: auto;
  animation: slideup .2s ease;
}
@keyframes slideup { from { transform: translateY(40px); opacity: .6; } }
.sheet-handle { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: -6px auto 16px; }
.sheet h3 { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
.sheet .sub { color: var(--text-dim); font-size: 13px; margin-bottom: 18px; }
.sheet textarea {
  width: 100%; min-height: 120px; resize: vertical;
  padding: 14px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; color: var(--text); font-size: 16px; line-height: 1.5;
}
.sheet textarea:focus { outline: none; border-color: var(--accent); }
.sheet .row { display: flex; gap: 10px; margin-top: 16px; }
.sheet .row .btn { flex: 1; }

/* Sélection de portion */
.selectable-text {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px; line-height: 1.6; font-size: 15px; white-space: pre-wrap; word-break: break-word;
  margin-bottom: 8px; user-select: text; -webkit-user-select: text;
}
.selectable-text ::selection { background: var(--accent); color: #03291c; }
.sel-hint { font-size: 12px; color: var(--text-faint); margin-bottom: 16px; display: flex; align-items: center; gap: 6px; }
.sel-quote { background: var(--bg-elev); border-left: 3px solid var(--accent); padding: 8px 12px; border-radius: 8px; font-size: 14px; margin-bottom: 16px; color: var(--text-dim); }

/* Choix type de couche */
.layer-types { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 18px; }
.layer-opt {
  padding: 14px 12px; border-radius: 12px; border: 2px solid var(--border);
  background: var(--bg); text-align: center; transition: all .12s;
}
.layer-opt .ico { font-size: 20px; margin-bottom: 6px; display: block; }
.layer-opt .name { font-size: 13px; font-weight: 600; color: var(--text); }
.layer-opt.active { background: var(--bg-elev); }

/* Detail bloc */
.block-detail .layer-badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 20px; font-size: 13px; font-weight: 600; margin-bottom: 14px;
}
.block-content-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px; line-height: 1.6; font-size: 15px; white-space: pre-wrap; word-break: break-word; margin-bottom: 14px;
}
.block-anchor { font-size: 13px; color: var(--text-dim); margin-bottom: 14px; }
.block-anchor .q { font-style: italic; }
.block-meta { font-size: 12px; color: var(--text-faint); margin-bottom: 18px; display: flex; align-items: center; gap: 6px; }
.action-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 10px; }
.action-grid .btn { font-size: 14px; padding: 12px; }
.action-grid .btn i { margin-right: 6px; }

/* Historique versions */
.version-item {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; margin-bottom: 10px;
}
.version-item .v-date { font-size: 12px; color: var(--text-faint); margin-bottom: 6px; display: flex; justify-content: space-between; }
.version-item .v-content { font-size: 14px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.version-item.current { border-color: var(--accent); }
.version-item .badge-cur { color: var(--accent); font-weight: 600; }

/* Toast */
#toast-root { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  background: var(--bg-elev); border: 1px solid var(--border); color: var(--text);
  padding: 12px 18px; border-radius: 12px; font-size: 14px; box-shadow: 0 8px 24px #0006;
  animation: toastin .2s ease; max-width: 90vw;
}
.toast.err { border-color: #ef444466; color: #fca5a5; }
@keyframes toastin { from { transform: translateY(10px); opacity: 0; } }

.spinner {
  width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite; margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-full { display: flex; align-items: center; justify-content: center; min-height: 100vh; }

/* Desktop */
@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
  .sheet { border-radius: 20px; border-bottom: 1px solid var(--border); }
  .sheet-handle { display: none; }
  .layer-types { grid-template-columns: repeat(4, 1fr); }
}
