/**
 * Enhanced Search History Styles
 * Implements dynamic layout system with CSS Grid
 */

/* Search History Container */
.search-history-container {
  margin-top: 20px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  transform: translateY(10px);
  animation: slideIn 0.4s ease-out forwards;
  /* 기본적으로 높이 제한 없음 - 레이아웃별로 개별 설정 */
  overflow: visible;
}

@keyframes slideIn {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.search-history-container:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(78, 205, 196, 0.3);
}

/* Items Container - Base Grid */
.search-history-items {
  display: grid;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  transform: scale(0.95);
}

.search-history-items.items-rendered {
  opacity: 1;
  transform: scale(1);
}

/* Vertical Layout (1-4 items) - 스크롤 없이 영역 확장 */
.layout-vertical {
  max-height: none; /* 높이 제한 없음 */
  overflow: visible; /* 스크롤 없음 */
}

.layout-vertical .search-history-items {
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(34px, auto); /* 더 작은 높이 */
  gap: 6px; /* 간격 줄임 */
}

/* Mixed Layout (5-9 items) - 스크롤 적용 */
.layout-mixed {
  max-height: none; /* 높이 제한 */
  overflow: visible; /* 스크롤 없음 */
}

.layout-mixed .search-history-items {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(4, minmax(34px, auto));
  grid-auto-rows: minmax(34px, auto);
  max-height: 340px; /* 컨테이너 내부 높이 */
  gap: 8px; /* 원래 간격 */
}

/* First 4 items positioning in mixed layout */
.layout-mixed .search-history-item:nth-child(1) {
  grid-column: 1;
  grid-row: 1;
}

.layout-mixed .search-history-item:nth-child(2) {
  grid-column: 1;
  grid-row: 2;
}

.layout-mixed .search-history-item:nth-child(3) {
  grid-column: 1;
  grid-row: 3;
}

.layout-mixed .search-history-item:nth-child(4) {
  grid-column: 1;
  grid-row: 4;
}

/* Items 5+ positioning - go to column 2, starting from row 1 */
.layout-mixed .search-history-item:nth-child(5) {
  grid-column: 2;
  grid-row: 1;
}

.layout-mixed .search-history-item:nth-child(6) {
  grid-column: 2;
  grid-row: 2;
}

.layout-mixed .search-history-item:nth-child(7) {
  grid-column: 2;
  grid-row: 3;
}

.layout-mixed .search-history-item:nth-child(8) {
  grid-column: 2;
  grid-row: 4;
}

.layout-mixed .search-history-item:nth-child(9) {
  grid-column: 2;
  grid-row: 5;
}

/* Individual History Items */
.search-history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

/* Vertical Layout (1-4개)에서 더 작은 크기 */
.layout-vertical .search-history-item {
  padding: 6px 10px; /* 더 작은 패딩 */
  min-height: 28px; /* 더 작은 높이 */
}

/* Mixed Layout (5개 이상)에서 원래 크기 */
.layout-mixed .search-history-item {
  padding: 6px 10px; /* 원래 패딩 */
  min-height: 28px; /* 원래 높이 */
}

.search-history-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(78, 205, 196, 0.1), transparent);
  transition: left 0.5s ease;
}

.search-history-item:hover::before {
  left: 100%;
}

.search-history-item:hover {
  background: rgba(78, 205, 196, 0.1);
  border-color: rgba(78, 205, 196, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(78, 205, 196, 0.2);
}

.search-history-item:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(78, 205, 196, 0.3);
}

/* History Text */
.history-text {
  flex: 1;
  color: #e0e0e0;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.2s ease;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

/* Vertical Layout (1-4개)에서 더 작은 텍스트 */
.layout-vertical .history-text {
  font-size: 0.8rem; /* 더 작은 글자 크기 */
  margin-right: 6px; /* 더 작은 마진 */
}

/* Mixed Layout (5개 이상)에서 원래 크기 */
.layout-mixed .history-text {
  font-size: 0.9rem; /* 원래 글자 크기 */
  margin-right: 10px; /* 원래 마진 */
}

.search-history-item:hover .history-text {
  color: #4ecdc4;
}

/* Remove Button */
.history-remove-btn {
  background: rgba(255, 107, 107, 0.2);
  border: 1px solid rgba(255, 107, 107, 0.3);
  color: #ff6b6b;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.2s ease;
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}

/* Vertical Layout (1-4개)에서 더 작은 버튼 */
.layout-vertical .history-remove-btn {
  width: 20px;
  height: 20px;
  font-size: 12px;
}

/* Mixed Layout (5개 이상)에서 원래 크기 */
.layout-mixed .history-remove-btn {
  width: 24px;
  height: 24px;
  font-size: 14px;
}

.history-remove-btn:hover {
  background: rgba(255, 107, 107, 0.3);
  border-color: #ff6b6b;
  transform: scale(1.1);
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
}

.history-remove-btn:active {
  transform: scale(0.95);
}

/* Layout Transition Animations */
.search-history-items {
  animation: layoutTransition 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes layoutTransition {
  0% {
    opacity: 0.7;
    transform: scale(0.98);
  }
  50% {
    opacity: 0.9;
    transform: scale(1.01);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* Item Entry Animation */
.search-history-item {
  animation: itemSlideIn 0.3s ease-out;
  animation-fill-mode: both;
}

.search-history-item:nth-child(1) {
  animation-delay: 0.05s;
}
.search-history-item:nth-child(2) {
  animation-delay: 0.1s;
}
.search-history-item:nth-child(3) {
  animation-delay: 0.15s;
}
.search-history-item:nth-child(4) {
  animation-delay: 0.2s;
}
.search-history-item:nth-child(5) {
  animation-delay: 0.25s;
}
.search-history-item:nth-child(6) {
  animation-delay: 0.3s;
}
.search-history-item:nth-child(7) {
  animation-delay: 0.35s;
}
.search-history-item:nth-child(8) {
  animation-delay: 0.4s;
}

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

/* Item Removal Animation */
.search-history-item.removing {
  animation: itemSlideOut 0.3s ease-in forwards;
}

@keyframes itemSlideOut {
  to {
    opacity: 0;
    transform: translateX(20px) scale(0.9);
    height: 0;
    padding: 0;
    margin: 0;
  }
}

/* Responsive Design */
@media (max-width: 768px) {
  .search-history-container {
    margin-top: 15px;
    padding: 12px;
  }

  .search-history-item {
    padding: 8px 12px;
    min-height: 36px;
  }

  .history-text {
    font-size: 0.85rem;
  }

  .history-remove-btn {
    width: 20px;
    height: 20px;
    font-size: 12px;
  }

  /* Force vertical layout on mobile for better UX */
  .layout-mixed .search-history-items {
    grid-template-columns: 1fr;
  }

  .layout-mixed .search-history-item:nth-child(n + 1) {
    grid-column: 1;
    grid-row: auto;
  }
}

@media (max-width: 480px) {
  .search-history-container {
    padding: 10px;
  }

  .search-history-item {
    padding: 6px 10px;
    min-height: 32px;
  }

  .history-text {
    font-size: 0.8rem;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .search-history-item {
    border-width: 2px;
  }

  .search-history-item:hover {
    border-width: 3px;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .search-history-container,
  .search-history-items,
  .search-history-item,
  .history-remove-btn {
    animation: none;
    transition: none;
  }

  .search-history-item:hover {
    transform: none;
  }
}

/* Focus indicators for accessibility */
.search-history-item:focus-within {
  outline: 2px solid #4ecdc4;
  outline-offset: 2px;
}

.history-remove-btn:focus {
  outline: 2px solid #ff6b6b;
  outline-offset: 2px;
}

/* Loading state */
.search-history-container.loading {
  opacity: 0.6;
  pointer-events: none;
}

.search-history-container.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid rgba(78, 205, 196, 0.3);
  border-top-color: #4ecdc4;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
/* Utility Classes */
.hidden {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.focus-visible:focus-visible {
  outline: 2px solid #007bff;
  outline-offset: 2px;
}

/* Disabled state for history items during search */
.history-item.disabled {
  cursor: not-allowed !important;
  opacity: 0.5 !important;
  pointer-events: none !important;
  filter: grayscale(50%);
  transition: all 0.3s ease;
}

.history-item.disabled:hover {
  transform: none !important;
  background: rgba(255, 255, 255, 0.05) !important;
}

.history-item.disabled .history-text {
  cursor: not-allowed !important;
}

.history-item.disabled .history-remove-btn {
  cursor: not-allowed !important;
  opacity: 0.3 !important;
}
