/* static/css/search.css
   Search widget for Hugo PaperMod
   Animated expand: "搜索" label → input + results dropdown
*/

/* ─── Nav-right: groups menu + search widget ────────────────── */
.nav-right {
  display: flex;
  align-items: center;
}

/* ─── Widget container ──────────────────────────────────────── */
.search-widget {
  position: relative;
  display: flex;
  align-items: center;
  align-self: center;        /* Fix 3: 垂直居中于 nav 行 */
  gap: 0;
  margin-left: 8px;
  z-index: 1000;
}

/* ─── Toggle button ("搜索" + icon) ─────────────────────────── */
.search-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--primary);
  font-size: 0.92rem;
  font-weight: 500;
  border-radius: 6px;
  white-space: nowrap;
  letter-spacing: 0.01em;
  line-height: 1;            /* Fix 3: 防止行高撑高容器 */
  opacity: 1;
  /* 不再做 max-width/padding 折叠——仅淡出，保持布局宽度不变，避免 #menu 偏移 */
  transition: background 0.18s, color 0.18s, opacity 0.18s cubic-bezier(0.4,0,0.2,1);
}

/* 搜索展开时：仅淡出切换按钮，不改变其占位宽度 */
.search-widget.is-open .search-toggle {
  opacity: 0;
  pointer-events: none;
}

.search-toggle:hover {
  background: var(--code-bg);
}

.search-label {
  transition: opacity 0.2s;
}

.search-icon {
  flex-shrink: 0;
  opacity: 0.75;
  transition: opacity 0.2s;
}

.search-toggle:hover .search-icon {
  opacity: 1;
}

/* ─── Expandable search box ──────────────────────────────────── */
/* 脱离 flex 流：position:absolute 使其不再占用 .search-widget 的布局宽度，
   从而彻底消除 open/close 动画期间 #menu 横移的问题。
   用 opacity + translateX 做淡入淡出动画，避免 max-width 动画带来的内容裁切。 */
.search-box {
  display: flex;
  align-items: center;
  position: absolute;        /* 脱离 flex 流，不影响父容器宽度 */
  right: 0;                  /* 锚定到 widget 右边缘 */
  top: 50%;
  width: 290px;
  padding: 4px 2px;

  /* collapsed state */
  opacity: 0;
  pointer-events: none;
  transform: translateY(-50%) translateX(8px);   /* 轻微右偏，配合淡入动画 */

  transition:
    opacity   0.22s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

/* expanded state */
.search-widget.is-open .search-box {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(-50%) translateX(0);
}

/* 搜索展开时隐藏 toggle 文字（冗余保护，toggle 已 opacity:0） */
.search-widget.is-open .search-label {
  opacity: 0;
}

.search-input {
  width: 230px;
  height: 32px;              /* Fix 3: 固定高度，不撑高 header */
  padding: 0 32px 0 12px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: var(--entry);
  color: var(--primary);
  font-size: 0.88rem;
  line-height: 1;
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
  -webkit-appearance: none;
  appearance: none;
}

.search-input::-webkit-search-cancel-button {
  display: none;
}

.search-input::placeholder {
  color: var(--secondary);
  opacity: 0.7;
}

.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 12%, transparent);
}

/* ─── Clear / Close button (双用途) ─────────────────────────── */
.search-clear {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);   /* 显式垂直居中，不依赖浏览器静态位置计算 */
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;                   /* 固定尺寸，防止图标切换时尺寸抖动 */
  height: 18px;
  padding: 2px;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--secondary);
  border-radius: 4px;
  opacity: 0.6;
  transition: opacity 0.15s, background 0.15s;
}

.search-clear:hover {
  opacity: 1;
  background: var(--code-bg);
}

/* 两个图标始终在布局中（position:absolute + 相同尺寸），
   用 opacity 切换，避免 display:none/block 引起的重排 */
.search-clear .icon-search-close,
.search-clear .icon-clear {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: opacity 0.12s;
}

/* 默认：显示 × 图标，隐藏搜索图标 */
.search-clear .icon-search-close { opacity: 0; pointer-events: none; }
.search-clear .icon-clear        { opacity: 1; }

/* is-close-mode：显示搜索图标（点击关闭），隐藏 × */
.search-clear.is-close-mode .icon-clear        { opacity: 0; pointer-events: none; }
.search-clear.is-close-mode .icon-search-close { opacity: 1; pointer-events: auto; }

/* ─── Results dropdown ───────────────────────────────────────── */
.search-results {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 300px;
  max-width: 420px;
  max-height: 420px;
  overflow-y: auto;
  background: var(--entry);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  box-shadow:
    0 4px 6px -1px rgba(0,0,0,0.08),
    0 10px 25px -5px rgba(0,0,0,0.12);
  list-style: none;
  margin: 0;
  padding: 6px;
  transform-origin: top right;
  animation: results-in 0.2s cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes results-in {
  from { opacity: 0; transform: scale(0.95) translateY(-4px); }
  to   { opacity: 1; transform: scale(1)    translateY(0);    }
}

.search-results::-webkit-scrollbar { width: 5px; }
.search-results::-webkit-scrollbar-track { background: transparent; }
.search-results::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

/* ─── Result items ───────────────────────────────────────────── */
.search-result-item {
  display: block;
  padding: 10px 12px;
  border-radius: 7px;
  text-decoration: none !important;
  color: var(--primary) !important;
  cursor: pointer;
  transition: background 0.12s;
  outline: none;
}

.search-result-item:hover,
.search-result-item:focus,
.search-result-item.is-active {
  background: var(--code-bg);
}

.search-result-title {
  font-size: 0.93rem;
  font-weight: 600;
  line-height: 1.3;
  margin-bottom: 3px;
  color: var(--primary);
}

.search-result-title mark,
.search-result-summary mark {
  background: color-mix(in srgb, var(--primary) 18%, transparent);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

.search-result-summary {
  font-size: 0.79rem;
  color: var(--secondary);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.search-result-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 5px;
  font-size: 0.73rem;
  color: var(--secondary);
  opacity: 0.8;
}

.search-result-meta .tag {
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.7rem;
}

/* ─── Empty / loading state ──────────────────────────────────── */
.search-no-results,
.search-loading {
  padding: 20px 12px;
  text-align: center;
  font-size: 0.88rem;
  color: var(--secondary);
}

.search-loading::after {
  content: '';
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-left: 8px;
  vertical-align: middle;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Backdrop overlay ───────────────────────────────────────── */
.search-overlay {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: rgba(0,0,0,0.25);
  backdrop-filter: blur(2px);
  animation: fade-in 0.2s ease both;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ─── Responsive ─────────────────────────────────────────────── */
@media (max-width: 600px) {
  .search-box {
    width: 210px;   /* 小屏幕下缩小搜索框 */
  }
  .search-input {
    width: 175px;
  }
  .search-results {
    position: fixed;
    top: 56px;
    left: 12px;
    right: 12px;
    min-width: unset;
    max-width: unset;
  }
}

/* ─── Dark mode ──────────────────────────────────────────────── */
.dark .search-results {
  box-shadow:
    0 4px 6px -1px rgba(0,0,0,0.3),
    0 10px 25px -5px rgba(0,0,0,0.4);
}
