/* -------------------------------------------------------------
   Popup Knowledge Hub - Frontend Styles
   @version 1.1.4 (Final Polish)
   ------------------------------------------------------------- */

:root {
  /* Light Theme (Default) */
  --pkh-primary: #3d3df0;
  --pkh-primary-hover: #2929cc;
  --pkh-text-main: #1d2327;
  --pkh-text-light: #646970;
  --pkh-bg-panel: #ffffff;
  --pkh-bg-body: #f6f7f7;
  --pkh-border: #dcdcde;
  --pkh-shadow: rgba(0, 0, 0, 0.15);
  --pkh-focus: #2271b1;
  --pkh-z-popup: 99999; /* Just below WP Admin Bar (100000) */
  --pkh-z-btn: 99990;
}

@media (prefers-color-scheme: dark) {
  :root {
    --pkh-primary: #6868ff;
    --pkh-primary-hover: #8080ff;
    --pkh-text-main: #f0f0f1;
    --pkh-text-light: #a7aaad;
    --pkh-bg-panel: #1e1e1e;
    --pkh-bg-body: #2c2c2c;
    --pkh-border: #444444;
    --pkh-shadow: rgba(0, 0, 0, 0.4);
    --pkh-focus: #4f94d4;
  }
}

/* --- Reset Box Model for Plugin Elements --- */
#pkh-popup *,
#pkh-popup *::before,
#pkh-popup *::after,
#pkh-welcome *,
#pkh-floating-btn * {
  box-sizing: border-box;
}

/* --- Global Lock (Prevent Scroll) --- */
html.pkh-lock,
body.pkh-lock {
  overflow: hidden !important;
  touch-action: none; 
}

/* --- Floating Button --- */
#pkh-floating-btn {
  position: fixed;
  z-index: var(--pkh-z-btn);
  
  /* Initial placement defaults */
  bottom: 20px;
  right: 20px; 
  left: auto; 
  top: auto;
  
  width: 56px;
  height: 56px;
  border-radius: 50%;
  
  display: flex;
  align-items: center;
  justify-content: center;
  
  background-color: var(--pkh-primary);
  color: #ffffff;
  border: none;
  box-shadow: 0 4px 15px var(--pkh-shadow);
  cursor: grab;
  
  /* CRITICAL FOR MOBILE: Prevents browser scroll hijacking */
  touch-action: none; 
  
  /* Only animate transform/color, NEVER top/left to ensure instant drag response */
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background-color 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  
  /* GPU Acceleration for smoother movement */
  will-change: left, top, transform;
}

/* While Dragging: Disable ALL animations to prevent lag */
#pkh-floating-btn.is-dragging {
  cursor: grabbing;
  transition: none !important;
  box-shadow: 0 8px 25px rgba(0,0,0,0.3); /* Lift effect */
  transform: scale(1.05); /* Slight static scale during drag */
}

#pkh-floating-btn:hover {
  transform: scale(1.1);
  background-color: var(--pkh-primary-hover);
}

#pkh-floating-btn:active {
  cursor: grabbing;
}

#pkh-floating-btn:focus-visible {
  outline: 2px solid var(--pkh-focus);
  outline-offset: 4px;
}

#pkh-floating-btn i {
  font-size: 22px;
  pointer-events: none; 
}

/* Pulse Animation (Only when idle) */
#pkh-floating-btn:not(:hover):not(.pkh-btn--close):not(.is-dragging) {
  animation: pkhPulse 3s infinite;
}

@keyframes pkhPulse {
  0% { box-shadow: 0 0 0 0 rgba(61, 61, 240, 0.4); }
  70% { box-shadow: 0 0 0 15px rgba(61, 61, 240, 0); }
  100% { box-shadow: 0 0 0 0 rgba(61, 61, 240, 0); }
}

/* Close State */
#pkh-floating-btn.pkh-btn--close {
  background-color: #333;
  transform: rotate(0deg);
}
#pkh-floating-btn.pkh-btn--close .pkh-x {
  font-size: 26px;
  line-height: 1;
}

/* --- Welcome Bubble --- */
#pkh-welcome {
  position: fixed;
  z-index: var(--pkh-z-btn);
  pointer-events: none;
  
  /* DEFAULT VISIBILITY: Hidden to avoid FOUC. JS will toggle this. */
  display: none; 

  /* DEFAULT POSITION: Matches default button position (Right: 20px) */
  bottom: 25px;
  right: 90px; /* 20px + 56px btn + gap */
  
  filter: drop-shadow(0 4px 6px var(--pkh-shadow));
  animation: pkhFadeInLeft 0.3s ease-out;
  transition: opacity 0.2s; 
}

.pkh-welcome__bubble {
  background: var(--pkh-bg-panel);
  color: var(--pkh-text-main);
  padding: 12px 35px 12px 15px; 
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  max-width: 260px;
  position: relative;
  pointer-events: auto;
  border: 1px solid var(--pkh-border);
}

/* Arrow pointing right (towards button) */
.pkh-welcome__bubble::before {
  content: "";
  position: absolute;
  top: 50%;
  right: -6px; /* Stick to right side */
  margin-top: -6px;
  border-width: 6px 0 6px 6px; /* Point Right */
  border-style: solid;
  border-color: transparent transparent transparent var(--pkh-bg-panel);
}

.pkh-welcome__close {
  position: absolute;
  top: 5px;
  right: 5px;
  background: transparent;
  border: none;
  color: var(--pkh-text-light);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  border-radius: 4px;
}
.pkh-welcome__close:hover {
  background: var(--pkh-bg-body);
  color: #d63638;
}

@keyframes pkhFadeInLeft {
  from { opacity: 0; transform: translateX(-10px); }
  to { opacity: 1; transform: translateX(0); }
}

/* --- Popup Container --- */
#pkh-popup {
  position: fixed;
  inset: auto;
  z-index: var(--pkh-z-popup);
  display: flex;
  flex-direction: column;
  width: 380px;
  max-width: 90vw;
  height: auto;
  max-height: 80vh;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(10px) scale(0.98);
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
  background: var(--pkh-bg-panel);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2), 0 0 0 1px var(--pkh-border);
  overflow: hidden;
}

#pkh-popup.is-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0) scale(1);
}

/* --- Popup Content (Standard) --- */
.pkh-popup__panel {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.pkh-popup__close { display: none; }
.pkh-popup__tabs {
  display: flex;
  gap: 8px;
  padding: 12px;
  background: var(--pkh-bg-body);
  border-bottom: 1px solid var(--pkh-border);
  overflow-x: auto;
  scrollbar-width: none;
}
.pkh-popup__tabs::-webkit-scrollbar { display: none; }
.pkh-tab-btn {
  flex: none;
  background: transparent;
  border: 1px solid transparent;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  color: var(--pkh-text-light);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}
.pkh-tab-btn:hover { background: rgba(0,0,0,0.05); color: var(--pkh-text-main); }
.pkh-tab-btn.is-active { background: var(--pkh-primary); color: #fff; border-color: transparent; }
.pkh-tab-btn.is-active:hover { background: var(--pkh-primary-hover); }
.pkh-popup__content { flex: 1; overflow-y: auto; position: relative; min-height: 200px; }

/* Custom Scrollbar for Content */
.pkh-popup__content::-webkit-scrollbar {
  width: 6px;
}
.pkh-popup__content::-webkit-scrollbar-track {
  background: transparent;
}
.pkh-popup__content::-webkit-scrollbar-thumb {
  background-color: rgba(0,0,0,0.2);
  border-radius: 3px;
}

.pkh-tab-panel { display: none; padding: 0; }
.pkh-tab-panel.is-active { display: block; animation: pkhFadeIn 0.2s ease; }
@keyframes pkhFadeIn { from { opacity: 0; } to { opacity: 1; } }
.pkh-list { list-style: none; margin: 0; padding: 0; }
.pkh-list li { border-bottom: 1px solid var(--pkh-border); }
.pkh-list li:last-child { border-bottom: none; }
.pkh-list a {
  display: block; padding: 12px 16px; text-decoration: none; color: var(--pkh-text-main); font-size: 14px;
  transition: background 0.1s; display: flex; align-items: center; gap: 10px;
}
.pkh-list a:hover, .pkh-list a:focus { background: var(--pkh-bg-body); color: var(--pkh-primary); outline: none; }
.pkh-list a::before {
  content: ""; display: block; width: 14px; height: 14px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71'%3E%3C/path%3E%3Cpath d='M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71'%3E%3C/path%3E%3C/svg%3E");
  background-size: contain; background-repeat: no-repeat; flex-shrink: 0; opacity: 0.6;
}
.pkh-list a:hover::before { filter: grayscale(0) sepia(1) hue-rotate(200deg) saturate(5); opacity: 1; }
.pkh-popup__search { padding: 10px 16px; border-top: 1px solid var(--pkh-border); background: var(--pkh-bg-panel); }
.pkh-search-input {
  width: 100%; padding: 10px 12px; border-radius: 6px; border: 1px solid var(--pkh-border);
  background: var(--pkh-bg-body); color: var(--pkh-text-main); font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s; box-sizing: border-box;
}
.pkh-search-input:focus {
  outline: none; border-color: var(--pkh-focus); box-shadow: 0 0 0 2px rgba(34, 113, 177, 0.2); background: var(--pkh-bg-panel);
}
.pkh-empty, .pkh-error { padding: 30px 20px; text-align: center; color: var(--pkh-text-light); font-size: 14px; }
.pkh-loader { display: flex; justify-content: center; padding: 40px 0; }
.pkh-spinner {
  width: 24px; height: 24px; border: 3px solid var(--pkh-border); border-top-color: var(--pkh-primary); border-radius: 50%;
  animation: pkhSpin 0.8s linear infinite;
}
@keyframes pkhSpin { to { transform: rotate(360deg); } }

/* --- Responsive / Mobile Mode --- */
@media (max-width: 782px) {
  #pkh-floating-btn { bottom: 15px; right: 15px; width: 50px; height: 50px; }
  
  /* Mobile Bubble Position: Forced safe area */
  #pkh-welcome {
      right: 80px;
      bottom: 20px;
      left: auto !important; /* Override JS inline style if needed */
      top: auto !important;
  }
  
  #pkh-popup {
    inset: 0 !important; width: 100% !important; max-width: 100% !important; height: 100% !important; max-height: 100% !important;
    border-radius: 0; transform: none; transition: opacity 0.2s; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end;
    pointer-events: auto !important; 
  }
  .pkh-popup__panel {
    background: var(--pkh-bg-panel); width: 100%; height: 85vh; border-radius: 16px 16px 0 0;
    transform: translateY(100%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); pointer-events: auto;
  }
  #pkh-popup.is-open .pkh-popup__panel { transform: translateY(0); }
  .pkh-popup__close {
    display: flex; position: absolute; top: 10px; right: 10px; width: 30px; height: 30px; border: none;
    background: var(--pkh-bg-body); border-radius: 50%; color: var(--pkh-text-main); font-size: 20px;
    align-items: center; justify-content: center; cursor: pointer; z-index: 10;
  }
  .pkh-popup__tabs { padding-right: 50px; }
}