/* 1. ANCHOR & HIGHLIGHTER */
.kif-anchor { 
  position: relative; 
  display: inline-block; 
  color: inherit; 
  text-decoration: underline;
  text-underline-offset: 4px;
  text-decoration-color: rgba(22, 163, 74, 0.5);
  cursor: pointer; 
  z-index: 1; 
}
.kif-anchor::after {
  content: ""; position: absolute; top: 0; bottom: 0; left: 0; right: 0;
  background-color: rgba(22, 163, 74, 0.15); z-index: -1; transform: scaleX(0); 
}
@keyframes wipe-highlight {
  0% { transform-origin: left; transform: scaleX(0); }
  12.5% { transform-origin: left; transform: scaleX(1); } 
  87.49% { transform-origin: left; transform: scaleX(1); } 
  87.5% { transform-origin: right; transform: scaleX(1); } 
  100% { transform-origin: right; transform: scaleX(0); } 
}
.kif-anchor.is-in-view::after { animation: wipe-highlight 4s infinite; }
.kif-anchor.is-activated::after {
  animation: none; transform: scaleX(1) !important; background-color: rgba(22, 163, 74, 0.25);
}

/* 2. BASIC RENDERER (Transient) */
#kif-renderer-root {
  position: absolute; width: 250px; z-index: 1100; /* HIGHEST */
  background: #fff; border: 1px solid #e5e7eb; border-radius: 8px;
  padding: 1.25rem; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
  opacity: 0; visibility: hidden; transition: opacity 0.3s ease-in;
  font-family: system-ui, sans-serif;
}
#kif-renderer-root.is-active { opacity: 1; visibility: visible; }
.kif-term { font-weight: bold; margin-bottom: 8px; font-size: 1.1em; margin-top: 0; color: #111; }
.kif-text { font-size: 0.9rem; color: #4b5563; margin-bottom: 0; line-height: 1.5; }

/* Close Button Override */
#kif-renderer-root .kif-close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 20;
  background: #f3f4f6;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}
#kif-renderer-root .kif-close-btn:hover {
  background: #e5e7eb;
}

/* 3. RICH RENDERER (Persistent Window) */
#kif-rich-renderer-root {
  position: fixed; z-index: 1000; /* BELOW BASIC */
  width: 350px; height: 300px;
  background: #fff; border: 1px solid #ddd; border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.2);
  display: flex; flex-direction: column;
  opacity: 0; visibility: hidden;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  font-family: system-ui, sans-serif;
  resize: both; overflow: hidden;
}
#kif-rich-renderer-root.is-active { opacity: 1; visibility: visible; }

/* The "Bubble" state for expansion */
#kif-rich-renderer-root.is-bubble {
  width: 120px; height: 40px;
  border-radius: 20px; overflow: hidden;
}

.kif-rich-header {
  padding: 12px 16px; background: #f9fafb; 
  border-bottom: 1px solid #eee; cursor: grab;
  display: flex; justify-content: space-between; align-items: center;
}
.kif-rich-header:active { cursor: grabbing; }
.kif-rich-term { font-weight: bold; font-size: 1rem; color: #111; margin: 0; }

.kif-rich-content {
  padding: 16px; overflow-y: auto; flex-grow: 1;
  font-size: 0.95rem; line-height: 1.6; color: #374151;
}
.kif-rich-content p { margin-bottom: 1em; }
.kif-rich-content blockquote { 
  border-left: 4px solid #16a34a; padding-left: 1rem; 
  font-style: italic; color: #6b7280; margin: 1em 0; 
}

.kif-close-btn {
  background: none; border: none; font-size: 1.2rem; cursor: pointer; color: #9ca3af;
}

/* 4. CANVAS & BACKDROP */
.kif-connector-canvas { position: absolute; top: 0; left: 0; width: 1px; height: 1px; overflow: visible; pointer-events: none; z-index: 999; }
.kif-connector-path { fill: none; stroke: #16a34a; stroke-width: 2px; stroke-linecap: round; }
.kif-backdrop { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.3); z-index: 998; opacity: 0; pointer-events: none; transition: opacity 0.3s ease; }
.kif-backdrop.is-active { opacity: 1; pointer-events: auto; }

@media (max-width: 768px) {
  #kif-renderer-root, #kif-rich-renderer-root {
    position: fixed !important; bottom: 0 !important; left: 0 !important; right: 0 !important; top: auto !important;
    width: 100% !important; max-width: 100% !important; border-radius: 16px 16px 0 0 !important;
    transform: translateY(100%); transition: transform 0.3s ease-out !important;
  }
  #kif-renderer-root.is-active, #kif-rich-renderer-root.is-active { transform: translateY(0); }
  .kif-connector-canvas { display: none !important; }
}

/* --- Z-INDEX LAYERING --- */
/* Basic transient definitions sit on top (1100) */
#kif-renderer-root { z-index: 1100; }
/* Rich persistent definitions sit below (1000) */
#kif-rich-renderer-root { z-index: 1000; }

/* --- RICH DEFINITION WINDOW --- */
#kif-rich-renderer-root {
  position: fixed; /* Viewport locked */
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.2);
  opacity: 0; 
  visibility: hidden;
  display: flex;
  flex-direction: column;
  
  /* Initial Bubble State */
  width: 40px;
  height: 40px;
  border-radius: 20px;
  overflow: hidden;
  
  /* Smooth Expansion Animation */
  transition: width 0.4s cubic-bezier(0.16, 1, 0.3, 1), 
              height 0.4s cubic-bezier(0.16, 1, 0.3, 1), 
              border-radius 0.4s ease, 
              opacity 0.2s ease;
}

#kif-rich-renderer-root.is-active {
  opacity: 1;
  visibility: visible;
}

/* Expanded State */
#kif-rich-renderer-root.is-expanded {
  width: 320px;
  height: 250px;
  border-radius: 8px;
  /* Removed native resize: both */
}

/* Close Button Override */
#kif-rich-renderer-root .kif-close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 20;
  background: #f3f4f6;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}
#kif-rich-renderer-root .kif-close-btn:hover {
  background: #e5e7eb;
}

/* Custom Bottom-Left Resize Handle */
.kif-resize-handle-bl {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 20px;
  height: 20px;
  cursor: sw-resize;
  z-index: 20;
}
/* Optional: Adds a little visual indicator to the corner */
.kif-resize-handle-bl::after {
  content: "";
  position: absolute;
  bottom: 4px;
  left: 4px;
  width: 8px;
  height: 8px;
  border-bottom: 2px solid #9ca3af;
  border-left: 2px solid #9ca3af;
}

/* Draggable Header Area */
.kif-rich-header {
  padding: 12px 36px 12px 16px;
  background: #f9fafb;
  border-bottom: 1px solid #e5e7eb;
  cursor: grab;
  flex-shrink: 0;
}
.kif-rich-header:active { cursor: grabbing; }

/* Scrollable Rich Content Area */
.kif-rich-content {
  padding: 16px;
  overflow-y: auto;
  flex-grow: 1;
  font-size: 0.95rem;
  line-height: 1.6;
  color: #374151;
  
  /* Fade in content only after expansion */
  opacity: 0;
  transition: opacity 0.3s ease 0.2s;
}
#kif-rich-renderer-root.is-expanded .kif-rich-content {
  opacity: 1;
}

/* Rich Text Formatting Resets */
.kif-rich-content blockquote {
  border-left: 4px solid #16a34a;
  padding-left: 1rem;
  margin-left: 0;
  color: #4b5563;
  font-style: italic;
}
.kif-rich-content ul, .kif-rich-content ol { padding-left: 1.5rem; margin-top: 0.5rem; }