/* Live search - full responsive CSS with slide-up on close
   Works with: add/remove .is-expanded on .live-search OR .open on .live-search-results
   Behavior: opening -> slide down + fade in; closing -> fade out + slide up smoothly
   Date: September 19, 2025
*/

/* Live search - base styles (right-aligned toggle that opens search leftwards) */
.live-search {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end; /* contents align to the right */
  flex: 1 2 0%;
  max-width: 250px;
  margin: 2rem 0;
  order: 1;
}

/* Toggle (hidden on desktop; shown on small screens) */
.live-search-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  background: transparent;
  border: none;
  padding: 0.5rem;
  margin: 0;
  color: inherit;
  cursor: pointer;
  border-radius: 8px;
}

.live-search-toggle:focus {
  outline: 2px solid rgba(102,175,233,0.9);
  outline-offset: 2px;
  border-radius: 8px;
}

.live-search-toggle svg {
  color: var(--icon-color)!important;
}

/* Search field */
.live-search input[type="search"] {
  box-sizing: border-box;
  width: 100%;
  min-width: 100px;
  padding: 0.5rem 2.4rem 0.5rem 0.75rem; /* right padding for icon */
  font: inherit;
  line-height: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid var(--border-color)!important;
  border-radius: 8px;
  transition: border-color 0.2s ease;
  -webkit-appearance: none;
  appearance: none;
}

.live-search .search-icon {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  pointer-events: none;
  color: var(--icon-color);
  display: inline-block;
  line-height: 0;
}

/* Remove native search decorations */
.live-search input[type="search"]::-webkit-search-decoration,
.live-search input[type="search"]::-webkit-search-cancel-button,
.live-search input[type="search"]::-webkit-search-results-button,
.live-search input[type="search"]::-webkit-search-results-decoration {
  -webkit-appearance: none;
  display: none;
}
.live-search input[type="search"]::-ms-clear,
.live-search input[type="search"]::-ms-reveal {
  display: none;
  width: 0;
  height: 0;
}

.live-search input[type="search"]:focus {
  outline: none;
  border-color: #66afe9;
  box-shadow: 0 0 4px rgba(102,175,233,.4);
}

/* Helper: use a CSS variable for target max-height (adjust if you expect taller lists) */
:root {
  --live-search-max: 500px; /* desktop open target */
  --live-search-max-mobile: 1200px; /* mobile open target */
}

/* Results dropdown (animatable: opacity + translateY + max-height)
   Use translateY rather than scaleY for more natural slide and consistent closing animation.
*/
.live-search-results {
  position: absolute;
  top: calc(100% + 2rem);
  left: 0;
  right: 0;
  margin: 0;
  background-color: var(--live-search-results-background-color);
  border-radius: 4px;

  /* Baseline collapsed */
  max-height: 0;
  overflow: hidden;

  z-index: 300;
  font: inherit;
  display: flex;
  flex-direction: column;
  gap: 0;
  white-space: normal;

  /* Hidden baseline for animation */
  opacity: 0;
  visibility: hidden;
  transform-origin: top center;
  transform: translateY(-8px); /* slide up slightly when hidden */
  pointer-events: none;

  /* Animate max-height + opacity + transform */
  transition:
    max-height 360ms cubic-bezier(.2,.9,.2,1),
    opacity 220ms ease,
    transform 260ms cubic-bezier(.2,.9,.2,1),
    visibility 0s linear 360ms; /* delay hiding visibility until after close animations */
  will-change: max-height, opacity, transform;
}

/* show border only when there are results */
.live-search-results:not(:empty) {
  border: 1px solid var(--border-color);
}

/* List reset and items */
.live-search-results ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.live-search-results li {
  display: block;
  width: 100%;
  box-sizing: border-box;
}

/* Result link rows */
.live-search-results li a {
  display: block;
  gap: 1rem;
  width: 100%;
  padding: 1.5rem;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--border-color);
  box-sizing: border-box;
  white-space: normal;
}
.live-search-results li a:hover,
.live-search-results li a:focus {
  background: var(--live-search-results-background-color-hover);
  outline: none;
}
.live-search-results li:last-child a {
  border-bottom: none;
}


/* No-results / error / too short (status line) */
.live-search-results .status-line {
  padding: 1.5rem;
  color: #777;
  display: flex;
  align-items: center;
}
.live-search-results .status-text {
  padding: 0;
  margin: 0;
}

/* spinner */
.live-search .spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid rgba(0,0,0,0.15);
  margin-top: -1px; /* compensate for border height */
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  display: none;
}
.live-search .spinner.visible {
  display: inline-block;
}

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

/* Highlighting */
.live-search-results mark {
  background: #fffb8f;
  font-weight: normal;
}

.live-search-breadcrumb {
  font-weight: 500;
  font-size: var(--font-size-base);
  margin-bottom: 0.75rem;
  color: var(--text-high-contrast)
}

.live-search-results small {
  display: block;
  font-weight: 300;
}

.live-search-results strong {
  display: block;
  font-weight: 500;
  position: relative;
}

/* OPEN state - immediate visibility, slide down and expand height */
.live-search.is-expanded .live-search-results,
.live-search-results.open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
  max-height: var(--live-search-max);
  overflow-y: auto;

  /* Ensure visibility becomes available immediately when opening */
  transition-delay: 0s, 0s, 0s, 0s;
}

/* To guarantee smooth close (slide up + fade), when removing .is-expanded/.open:
   visibility remains until max-height/opacity/transform animate out (handled by default transition and visibility delay).
*/

/* Responsive (mobile) */
@media (max-width: 600px) {
  .live-search {
    width: 40px; /* As wide as toggle by default */
    max-width: 100%;
    gap: 0.5rem;
    justify-content: flex-end;
  }

  .live-search input[type="search"] {
    padding-right: 1.9rem;
    background-position: right 6px center;
    background-size: 16px 16px;
  }

  .live-search .search-icon {
    display: none;
  }

  /* Mobile results: use block for reliable height measurement */
  .live-search-results {
    position: absolute;
    top: calc(100% + 2rem);
    left: 0;
    right: 0;
    margin: 0;
    border-radius: 4px;
    background-color: var(--live-search-results-background-color);

    /* collapsed baseline */
    max-height: 0;
    overflow: hidden;
    z-index: 300;
    font: inherit;
    display: block; /* not flex so height measured reliably */
    white-space: normal;

    /* hidden baseline */
    opacity: 0;
    visibility: hidden;
    transform-origin: top center;
    transform: translateY(-8px);
    pointer-events: none;

    transition:
      max-height 360ms cubic-bezier(.2,.9,.2,1),
      opacity 220ms ease,
      transform 260ms cubic-bezier(.2,.9,.2,1),
      visibility 0s linear 360ms; /* keep visibility until animations finish */
    will-change: max-height, opacity, transform;
  }

  /* Show toggle */
  .live-search-toggle {
    display: inline-flex;
    background: transparent;
    order: 1; /* keep toggle at the rightmost position */
  }

  /* Collapsed state - shrink the input on mobile */
  .live-search input[type="search"] {
    width: 0;
    max-width: 0;
    padding-left: 0;
    padding-right: 0;
    opacity: 0;
    pointer-events: none;
    transform-origin: right center;
    border-color: transparent;
    border-radius: 8px;
    margin-right: 0;
    transition:
      width 500ms cubic-bezier(.2,.9,.2,1),
      max-width 500ms cubic-bezier(.2,.9,.2,1),
      opacity 100ms ease,
      padding 500ms ease,
      border-color 100ms ease;
    -webkit-transition:
      width 500ms cubic-bezier(.2,.9,.2,1),
      max-width 500ms cubic-bezier(.2,.9,.2,1),
      opacity 100ms ease,
      padding 500ms ease,
      border-color 100ms ease;
  }

  .live-search.is-expanded {
    align-items: center;
  }

  @media (max-width: 600px) {
    .hide-logo {
      display: none!important;
    }
  }

  .live-search.is-expanded input[type="search"] {
    width: calc(100% - 48px); /* toggle 40px + 8px gap */
    max-width: calc(100% - 48px);
    padding: 0.5rem 1.2rem 0.5rem 0.75rem;
    opacity: 1;
    pointer-events: auto;
    border-color: #ddd;
    transition:
      width 320ms cubic-bezier(.2,.9,.2,1),
      max-width 320ms cubic-bezier(.2,.9,.2,1),
      opacity 240ms ease,
      padding 200ms ease,
      border-color 160ms ease;
    -webkit-transition:
      width 320ms cubic-bezier(.2,.9,.2,1),
      max-width 320ms cubic-bezier(.2,.9,.2,1),
      opacity 240ms ease,
      padding 200ms ease,
      border-color 160ms ease;
    order: 0; /* place input left of toggle visually */
    margin-right: 8px;
  }

  .live-search.is-expanded .live-search-results,
  .live-search-results.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    pointer-events: auto;
    max-height: var(--live-search-max-mobile);
    overflow-y: auto;
    transition-delay: 0s, 0s, 0s, 0s; /* immediate when opening */
  }

  .live-search.is-expanded .live-search-results li a {
    padding: 1.5rem;
  }
}

/* Usage notes (not CSS):
   - JavaScript should add .is-expanded on .live-search or .open on .live-search-results to open.
   - To close, remove those classes. The CSS uses a visibility delayed by the max-height transition
     (visibility 0s linear 360ms) so when closing the container will animate opacity+transform+max-height
     then visibility will be set to hidden after 360ms, producing a smooth slide-up on close.
   - If you need faster visibility hiding, match the visibility delay to the longest closing transition time.
   - Adjust --live-search-max / --live-search-max-mobile if lists are taller.
*/