/* ============================================
   SEMANTIC SEARCH - UI COMPONENT (COLLAPSIBLE)
   ============================================ */

/**
 * COLLAPSIBLE SEARCH BAR
 * ----------------------
 * Starts as a clean icon button, expands into full search bar.
 * Beautiful animations and theme-aware colors.
 */

.search-container {
  position: relative;
  display: inline-block;
  z-index: 100;
  margin-left: 16px;
}

/* Search Toggle Button (Icon Only) */
.search-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background-color: var(--bg-tertiary);
  border: 2px solid var(--border-color);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-sm);
}

.search-toggle:hover {
  background-color: var(--accent-primary);
  border-color: var(--accent-primary);
  transform: scale(1.05);
  box-shadow: var(--shadow-md);
}

.search-toggle:hover .search-toggle-icon {
  color: white;
}

.search-toggle-icon {
  width: 22px;
  height: 22px;
  color: var(--text-primary);
  transition: color 0.2s ease;
}

/* Hide toggle when search is expanded */
.search-container.expanded .search-toggle {
  display: none;
}

/* Search Wrapper (Expandable) */
.search-wrapper {
  position: absolute;
  top: 0;
  right: 0;
  display: none;
  align-items: center;
  background-color: var(--bg-secondary);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 10px 14px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: var(--shadow-lg);
  width: 0;
  opacity: 0;
  overflow: hidden;
}

/* Expanded state */
.search-container.expanded .search-wrapper {
  display: flex;
  width: 400px;
  opacity: 1;
}

.search-wrapper:focus-within {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(22, 114, 206, 0.1), var(--shadow-lg);
}

/* Mobile: Full width when expanded */
@media (max-width: 640px) {
  .search-container.expanded .search-wrapper {
    position: fixed;
    top: 70px;
    left: 16px;
    right: 16px;
    width: auto;
  }
}

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

.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  min-width: 250px;
}

.search-input::placeholder {
  color: var(--text-tertiary);
}

.search-loading {
  display: none;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-color);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-left: 8px;
  flex-shrink: 0;
}

.search-loading.active {
  display: block;
}

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

.search-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 6px;
  cursor: pointer;
  color: var(--text-tertiary);
  transition: all 0.2s ease;
  margin-left: 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

.search-close:hover {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
}

.search-clear {
  display: none;
  width: 20px;
  height: 20px;
  padding: 4px;
  cursor: pointer;
  color: var(--text-tertiary);
  transition: color 0.2s ease;
  margin-left: 4px;
  flex-shrink: 0;
}

.search-clear:hover {
  color: var(--text-primary);
}

.search-clear.active {
  display: block;
}

/**
 * SEARCH RESULTS STYLING
 * -----------------------
 * Appears below the search bar when expanded.
 */

.search-results {
  position: absolute;
  top: calc(100% + 12px);
  right: 0;
  width: 500px;
  max-width: calc(100vw - 32px);
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 16px;
  display: none;
  max-height: 70vh;
  overflow-y: auto;
  z-index: 99;
}

.search-results.active {
  display: block;
}

/* Mobile: Full width */
@media (max-width: 640px) {
  .search-results {
    position: fixed;
    top: 130px;
    left: 16px;
    right: 16px;
    width: auto;
    max-height: calc(100vh - 150px);
  }
}

.search-results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 0 4px;
}

.search-results-count {
  font-size: 14px;
  color: var(--text-secondary);
}

.search-results-time {
  font-size: 12px;
  color: var(--text-tertiary);
}

.search-results-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/**
 * INDIVIDUAL RESULT CARD
 * -----------------------
 * Each search result appears as a clickable card.
 */

.search-result-item {
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  display: block;
}

.search-result-item:hover {
  border-color: var(--accent-primary);
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.search-result-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  margin-bottom: 8px;
}

.search-result-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 4px 0;
  line-height: 1.4;
}

.search-result-score {
  font-size: 12px;
  color: var(--text-tertiary);
  background-color: var(--bg-tertiary);
  padding: 4px 8px;
  border-radius: 6px;
  font-weight: 500;
  white-space: nowrap;
  margin-left: 12px;
}

.search-result-url {
  font-size: 13px;
  color: var(--accent-primary);
  margin-bottom: 12px;
  display: block;
}

.search-result-preview {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.search-result-preview mark {
  background-color: rgba(22, 114, 206, 0.15);
  color: var(--text-primary);
  padding: 2px 4px;
  border-radius: 3px;
  font-weight: 500;
}

/**
 * EMPTY STATE
 * ------------
 * Shown when no results found.
 */

.search-empty {
  display: none;
  text-align: center;
  padding: 48px 24px;
  color: var(--text-secondary);
}

.search-empty.active {
  display: block;
}

.search-empty-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 16px;
  color: var(--text-tertiary);
}

.search-empty-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.search-empty-message {
  font-size: 14px;
  line-height: 1.6;
}

/**
 * LOADING STATE
 * --------------
 * Shown while search is initializing or running.
 */

.search-loading-state {
  display: none;
  text-align: center;
  padding: 48px 24px;
}

.search-loading-state.active {
  display: block;
}

.search-loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

.search-loading-message {
  font-size: 14px;
  color: var(--text-secondary);
}

/**
 * MOBILE RESPONSIVE
 * ------------------
 * Adjust layout for smaller screens.
 */

@media (max-width: 768px) {
  .search-container {
    padding: 0 16px;
  }
  
  .search-wrapper {
    padding: 10px 12px;
  }
  
  .search-input {
    font-size: 16px; /* Prevents iOS zoom on focus */
  }
  
  .search-result-item {
    padding: 16px;
  }
  
  .search-result-title {
    font-size: 16px;
  }
  
  .search-result-header {
    flex-direction: column;
    align-items: start;
  }
  
  .search-result-score {
    margin-left: 0;
    margin-top: 8px;
  }
}

/**
 * KEYBOARD NAVIGATION
 * --------------------
 * Style for focused result when navigating with arrow keys.
 */

.search-result-item:focus {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

/**
 * SEARCH SUGGESTIONS (Optional)
 * -------------------------------
 * For showing popular searches or recent searches.
 */

.search-suggestions {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 8px;
  z-index: 1000;
  display: none;
}

.search-suggestions.active {
  display: block;
}

.search-suggestion-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.15s ease;
  font-size: 14px;
  color: var(--text-primary);
}

.search-suggestion-item:hover {
  background-color: var(--bg-tertiary);
}

.search-suggestion-icon {
  width: 16px;
  height: 16px;
  display: inline-block;
  margin-right: 8px;
  color: var(--text-tertiary);
}

/**
 * THEME-SPECIFIC ADJUSTMENTS
 * ---------------------------
 * Fine-tune colors for specific themes.
 */

[data-theme="dark"] .search-result-preview mark {
  background-color: rgba(59, 130, 246, 0.2);
}

[data-theme="ocean"] .search-result-preview mark {
  background-color: rgba(8, 145, 178, 0.15);
}

[data-theme="sunset"] .search-result-preview mark {
  background-color: rgba(220, 38, 38, 0.15);
}