<template>
  <header class="topbar">
    <div class="left-controls">
      <div class="nav-group">
        <button class="nav-btn ripple-btn" @click="goBack" title="返回">
          <Icon icon="lucide:chevron-left" class="nav-icon" />
        </button>
        <button class="nav-btn ripple-btn" @click="goForward" title="前进">
          <Icon icon="lucide:chevron-right" class="nav-icon" />
        </button>
      </div>

      <div class="search-wrapper">
        <div class="search-container">
          <Icon icon="lucide:search" class="search-icon" />
          <input
              type="text"
              placeholder="搜索音乐、歌手、专辑..."
              class="search-input"
              v-model="searchQuery"
              @keydown.enter="handleSearch"
          />
        </div>
      </div>
    </div>

    <div class="right-controls">
      <div class="app-actions">
        <button class="action-btn ripple-btn" title="设置" @click="openSettings">
          <Icon icon="lucide:settings" class="action-icon" />
        </button>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, inject } from 'vue';
import { useRouter } from 'vue-router';
import { Icon } from '@iconify/vue';

const router = useRouter();
const searchQuery = ref('');

const goBack = () => router.back();
const goForward = () => router.forward();

const handleSearch = () => {
  if (!searchQuery.value.trim()) return;
  router.push({ 
    name: 'Search', 
    query: { q: searchQuery.value } 
  });
};

// Settings
const openSettings = inject<() => void>('openSettings', () => {});
</script>

<style scoped>
/* 变量定义 */
:root {
  --radius-soft: 10px;
}

.topbar {
  box-sizing: border-box;
  height: 64px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px 0 24px;
  background-color: var(--color-bg-primary);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 100;
  user-select: none;
}

/* --- 左侧区域 --- */
.left-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.nav-group {
  display: flex;
  gap: 10px;
}

/* --- 统一功能按钮（Nav / Settings）--- */
.nav-btn,
.action-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-secondary);
  background-color: transparent;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition:
      background-color 0.2s ease,
      border-color 0.2s ease,
      color 0.2s ease,
      transform 0.12s ease;
}

.nav-btn:hover,
.action-btn:hover {
  background-color: var(--color-bg-tertiary);
  color: var(--color-text-primary);
}

/* 图标 */
.nav-icon,
.action-icon {
  width: 22px;
  height: 22px;
  z-index: 2;
}

.ripple-btn::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(0);
  opacity: 0;
  pointer-events: none;
}

.ripple-btn:active::after {
  transform: translate(-50%, -50%) scale(2.5);
  opacity: 1;
  transition: 0s;
}

.ripple-btn:not(:active):after {
  transform: translate(-50%, -50%) scale(2.5);
  opacity: 0;
  transition: opacity 0.4s ease-out;
}

/* --- 搜索框 --- */
.search-wrapper {
}

.search-container {
  display: flex;
  align-items: center;
  height: 40px;
  width: 260px;
  padding: 0 14px;
  background-color: var(--color-bg-tertiary);
  border: 1px solid var(--color-border);
  border-radius: 20px;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  cursor: text;
}

.search-container:hover {
  border-color: var(--color-text-muted);
}

.search-container:focus-within {
  width: 340px;
  border-color: var(--color-accent);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  background: linear-gradient(
      90deg,
      var(--color-bg-primary) 0%,
      var(--color-bg-tertiary) 100%
  );
}

.search-icon {
  color: var(--color-text-muted);
  margin-right: 10px;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  transition: color 0.2s;
}

.search-container:focus-within .search-icon {
  color: var(--color-accent);
}

.search-input {
  flex: 1;
  height: 100%;
  font-size: 15px;
  color: var(--color-text-primary);
  background: transparent;
  border: none;
  outline: none;
}

.search-input::placeholder {
  color: var(--color-text-muted);
  font-size: 14px;
}

/* --- 右侧区域布局调整 --- */
.right-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 100%;
}

.app-actions {
  display: flex;
  align-items: center;
}
</style>
