.search-results {
  position: absolute;
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  max-height: 200px;
  overflow-y: auto;
  width: 100%;
  z-index: 10001;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  display: none; /* Hidden by default, shown via JS when results exist */
  margin: 0; /* Ensure no margin creates extra space */
  margin-top: 4px; /* Small gap from input */
  top: 100%; /* Position directly below input */
  border-top: none !important; /* Remove top border that could appear as a line */
  left: 0; /* Ensure it starts at the left edge of parent */
}

/* When search results are visible, ensure they appear above all other form fields */
.search-results[style*="display: block"] {
  z-index: 10001 !important;
  position: absolute !important;
}

@media (prefers-color-scheme: dark) {
  .search-results {
    background: var(--card-bg, #2a2a2a);
    border-color: var(--border-color, #444);
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  }
}

/* Location search form field styling */
.location-search-field {
  position: relative;
  z-index: 10;
}

/* Ensure form fields with search results have proper stacking */
[data-controller*="item-search"] .form-field,
[data-controller*="external-search"] .form-field {
  position: relative;
}

[data-controller*="item-search"] .form-field:has(.search-results[style*="display: block"]),
[data-controller*="external-search"] .form-field:has(.search-results[style*="display: block"]) {
  z-index: 10002;
  position: relative;
}

.location-search-field .address-input-container {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

/* When search results are visible, ensure the form field stays on top */
.location-search-field:has(.search-results[style*="display: block"]) {
  z-index: 100;
}

/* Fallback for browsers that don't support :has() */
.location-search-field.has-results {
  z-index: 100;
}

/* When search-results is hidden or empty, remove all borders */
.search-results:empty,
.search-results[style*="display: none"],
.search-results[style*="display:none"] {
  display: none !important;
  border: none !important;
  border-top: none !important;
  box-shadow: none !important;
  height: 0 !important;
  overflow: hidden !important;
}

.search-result {
  padding: 0;
  cursor: pointer;
}

/* Prevent text selection highlighting individual lines inside a result */
[data-controller="item-search"] .search-result,
[data-controller="item-search"] .search-result * {
  user-select: none;
}

/* For item-search results, clear any row-level background so we only
   use the unified content block highlight */
[data-controller="item-search"] .search-result,
[data-controller="item-search"] .search-result:hover,
[data-controller="item-search"] .search-result.selected {
  background-color: transparent;
}

.search-result-content {
  padding: 8px 12px;
}

/* Hover/selected background applies to the unified content block */
[data-controller="item-search"] .search-result-content:hover,
[data-controller="item-search"] .search-result.selected .search-result-content {
  background-color: #f3f4f6;
}

.search-result-empty {
  padding: 8px 12px;
  color: #666;
  font-style: italic;
}

.search-result-header {
  padding: 8px 12px;
  background-color: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  font-size: 0.85rem;
  font-weight: 600;
  color: #475569;
  position: sticky;
  top: 0;
  z-index: 10;
}

.search-result .item-type {
  font-size: 0.85rem;
  color: #64748b;
  font-weight: normal;
}

.form-field {
  position: relative;
  margin-bottom: 1rem;
}

.search-result.create-new {
  background-color: #f0f9ff;
  border-top: 1px solid #e5e7eb;
  color: #2563eb;
  font-weight: 500;
}

.search-result.create-new:hover .search-result-content {
  background-color: #e0f2fe;
}

.search-result.create-new-location {
  background-color: #f0fdf4;
  border-top: 1px solid #d1fae5;
  color: #16a34a;
  font-weight: 500;
}

.search-result.create-new-location:hover .search-result-content {
  background-color: #dcfce7;
}

.search-result.keyboard-selected .search-result-content {
  background-color: #e5edff;
  border-left: 3px solid #2563eb;
}

/* Richer item search result layout */
.search-result-main-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}

.search-result .item-name {
  font-weight: 500;
  color: #111827;
}

.item-type-pill {
  font-size: 0.75rem;
  padding: 2px 6px;
  border-radius: 999px;
  background-color: #eff6ff;
  color: #1d4ed8;
  white-space: nowrap;
}

.item-secondary {
  margin-top: 2px;
  font-size: 0.75rem;
  font-weight: 300;
  color: #b0b0b0 !important;
}

@media (max-width: 600px) {
  .search-result-content {
    padding: 8px 10px;
  }

  .search-result-main-line {
    flex-direction: row;
    align-items: flex-start;
  }

  .item-secondary {
    white-space: normal;
    word-break: break-word;
  }
}

/* Item Preview Popup Styles */
.item-preview-container {
  position: absolute;
  width: 320px;
  background: var(--card-bg, white);
  border: 1px solid var(--border-color, #ddd);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  z-index: 10000;
  animation: fadeIn 0.2s ease-in-out;
  overflow: hidden;
}

@media (prefers-color-scheme: dark) {
  .item-preview-container {
    background: var(--card-bg, #2a2a2a);
    border-color: var(--border-color, #444);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  }
}

.item-preview-container.mobile {
  position: fixed;
  width: auto;
  max-width: 90%;
  left: 5%;
  right: 5%;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.item-preview-popup {
  padding: 16px;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-color, #eee);
}

@media (prefers-color-scheme: dark) {
  .preview-header {
    border-bottom-color: var(--border-color, #444);
  }
}

.preview-item-name {
  font-weight: 600;
  font-size: 16px;
  color: var(--text-color, #1a1a1a);
  flex: 1;
  line-height: 1.3;
}

@media (prefers-color-scheme: dark) {
  .preview-item-name {
    color: var(--text-color, #e0e0e0);
  }
}

.preview-type-badge {
  background: var(--primary-color, #4a90e2);
  color: white;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}

.preview-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.preview-detail {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-color, #333);
}

@media (prefers-color-scheme: dark) {
  .preview-detail {
    color: var(--text-color, #d0d0d0);
  }
}

.preview-detail strong {
  color: var(--text-color, #1a1a1a);
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  .preview-detail strong {
    color: var(--text-color, #e0e0e0);
  }
}

.preview-description {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-secondary, #666);
  margin-top: 4px;
  padding: 8px;
  background: var(--bg-secondary, #f8f8f8);
  border-radius: 6px;
}

@media (prefers-color-scheme: dark) {
  .preview-description {
    color: var(--text-secondary, #aaa);
    background: var(--bg-secondary, #333);
  }
}

.preview-footer {
  padding-top: 8px;
  border-top: 1px solid var(--border-color, #eee);
  text-align: center;
}

@media (prefers-color-scheme: dark) {
  .preview-footer {
    border-top-color: var(--border-color, #444);
  }
}

.preview-footer small {
  font-size: 12px;
  color: var(--text-muted, #999);
  font-style: italic;
}

@media (prefers-color-scheme: dark) {
  .preview-footer small {
    color: var(--text-muted, #777);
  }
}

/* Mobile-specific adjustments */
@media (max-width: 768px) {
  .item-preview-container:not(.mobile) {
    display: none;
  }
  
  .item-preview-container.mobile {
    width: calc(100% - 40px);
    max-width: none;
    left: 20px;
    right: 20px;
  }
}

/* Item name field wrapper for external search */
.item-name-field-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

.item-name-field-wrapper input[type="text"] {
  flex: 1;
}

/* Location Search Help Button */
.help-find-button {
  font-size: 0.75rem;
  padding: 4px 8px;
  background: var(--primary-color, #4a90e2);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.2s ease;
}

@media (prefers-color-scheme: dark) {
  .help-find-button {
    background: var(--primary-color, #5aa0f2);
  }
}

.help-find-button:hover:not(:disabled) {
  background: var(--primary-hover, #3a80d2);
  transform: translateY(-1px);
}

.help-find-button:disabled,
.help-find-button.disabled,
.help-find-button.loading {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  background: var(--disabled-bg, #9ca3af);
}

@media (prefers-color-scheme: dark) {
  .help-find-button:disabled,
  .help-find-button.disabled {
    background: var(--disabled-bg, #4b5563);
  }
}

.help-find-button.success {
  background: var(--success-color, #28a745);
}

/* Item name field wrapper - needs to be positioned relative for absolute children */
.item-name-field-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}

.item-name-field-wrapper input[type="text"] {
  flex: 1;
}

/* External search results container - positioned below the input field and button */
.external-search-results-container {
  position: absolute;
  top: 100%; /* Position directly below the input/button row */
  left: 0;
  right: 0; /* Stretch to match wrapper width */
  width: 100%;
  margin-top: 4px; /* Small gap from input/button */
  background-color: white !important;
  border: 1px solid #d7dee8;
  border-radius: 8px;
  max-height: 300px;
  overflow-y: auto;
  z-index: 10005; /* Above other dropdowns on the form */
  box-shadow: 0 12px 28px rgba(0,0,0,0.18) !important;
}

@media (prefers-color-scheme: dark) {
  .external-search-results-container {
    background-color: var(--card-bg, #2a2a2a) !important;
    border-color: var(--border-color, #444);
    box-shadow: 0 12px 28px rgba(0,0,0,0.55) !important;
  }
}

/* External search results */
.external-search-results {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.external-search-result {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #e2e8f0;
  transition: background-color 0.2s ease;
}

.external-search-result:hover {
  background-color: #f7fafc;
}

.external-search-result:last-child {
  border-bottom: none;
}

.external-search-result-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.external-search-result-description {
  font-size: 0.875rem;
  color: #64748b;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.external-search-error {
  padding: 8px 12px;
  color: #dc2626;
  font-size: 0.875rem;
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  position: relative;
  z-index: 10001;
}

@media (prefers-color-scheme: dark) {
  .external-search-error {
    background: var(--card-bg, #2a2a2a);
    border-color: var(--border-color, #444);
  }
}

@media (prefers-color-scheme: dark) {
  .external-search-result {
    border-bottom-color: #444;
  }

  .external-search-result:hover {
    background-color: #333;
  }

  .external-search-result-description {
    color: #94a3b8;
  }
}

/* Location search results */
.location-result {
  cursor: pointer;
}

.location-result:hover {
  background: var(--hover-bg, #f5f5f5);
}

@media (prefers-color-scheme: dark) {
  .location-result:hover {
    background: var(--hover-bg, #333);
  }
}

.search-result.error {
  color: var(--error-color, #dc3545);
  font-style: italic;
  padding: 12px;
}
