/* ═══════════════════════════════════════════════════════════════════
   BRS Unified Search Component
   One component used by every search/filter box in the app (public SPA
   + admin CMS). Tokens resolve against each context's existing theme
   variables, so it adapts to frontend dark/light and admin light/dark
   without needing its own overrides.
   ═══════════════════════════════════════════════════════════════════ */

:root {
  --search-height: 46px;
  --search-height-mobile: 44px;
  --search-radius: 12px;
  --search-padding-x: 16px;
  --search-padding-left: 42px;
  --search-icon-size: 16px;
  --search-font-size: 16px; /* 16px, not 15px, avoids iOS Safari zoom-on-focus on any touch context */
  --search-transition: 200ms ease;
  --search-focus-shadow: 0 0 0 3px rgba(245, 158, 11, 0.15);
  --search-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
  --search-clear-hover-bg: rgba(128, 128, 128, 0.16);
}

/* Declared on body (not :root): --text-main/--input-bg/etc. are only ever
   overridden on body.light-mode / body.dark-mode, one level below :root. A
   var() reference is resolved once, using the cascade at the element it's
   declared on, then that resolved value inherits down — so defining these
   on :root would permanently bake in :root's base-theme colors and ignore
   the body-level theme override entirely. */
body {
  --search-bg: var(--input-bg, var(--card-bg, #ffffff));
  --search-border: var(--border, #e5e7eb);
  --search-text: var(--text-main, var(--text, #1f2937));
  --search-placeholder: var(--text-dim, #9ca3af);
  --search-icon-color: var(--text-dim, #9ca3af);
  --search-focus: var(--primary, #f59e0b);
}

.search-container {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: var(--search-icon-size);
  height: var(--search-icon-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--search-icon-size);
  line-height: 1;
  color: var(--search-icon-color);
  pointer-events: none;
  z-index: 1;
}

/* Selectors below are deliberately qualified as ".search-container input.search-input"
   rather than bare ".search-input": several pages (admin.css's global
   `input:not([type="checkbox"]):not([type="radio"])` reset, songs-library.php's
   `.form-group input`) already carry enough specificity to beat a single class
   selector. Matching their specificity tier (and loading this file after theirs)
   guarantees this component always wins without needing !important. */
.search-container input.search-input {
  width: 100%;
  height: var(--search-height);
  box-sizing: border-box;
  margin: 0;
  background: var(--search-bg);
  color: var(--search-text);
  border: 1px solid var(--search-border);
  border-radius: var(--search-radius);
  padding: 0 var(--search-padding-x) 0 var(--search-padding-left);
  font-family: inherit;
  font-size: var(--search-font-size);
  box-shadow: var(--search-shadow);
  transition: border-color var(--search-transition), box-shadow var(--search-transition);
  appearance: none;
  -webkit-appearance: none;
}

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

.search-container input.search-input:focus {
  outline: none;
  border-color: var(--search-focus);
  box-shadow: var(--search-focus-shadow);
}

.search-container input.search-input[type="search"]::-webkit-search-decoration,
.search-container input.search-input[type="search"]::-webkit-search-cancel-button,
.search-container input.search-input[type="search"]::-webkit-search-results-button,
.search-container input.search-input[type="search"]::-webkit-search-results-decoration {
  -webkit-appearance: none;
  appearance: none;
  display: none;
}

.search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--search-icon-color);
  cursor: pointer;
  padding: 0;
  transition: background var(--search-transition), color var(--search-transition);
}

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

.search-clear.hidden {
  display: none;
}

.search-mic {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--search-icon-color);
  cursor: pointer;
  padding: 0;
  font-size: 14px;
  transition: background var(--search-transition), color var(--search-transition);
}

.search-mic:hover {
  background: var(--search-clear-hover-bg);
  color: var(--search-text);
}

.search-mic:focus-visible {
  outline: 2px solid var(--search-focus);
  outline-offset: 2px;
}

.search-mic.listening {
  color: #ef4444;
  animation: search-mic-pulse 1.2s ease-in-out infinite;
}

/* Two staggered rings read as a soft "sound wave" pulse rather than a flat blink. */
@keyframes search-mic-pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4), 0 0 0 0 rgba(239, 68, 68, 0.25); }
  50% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0), 0 0 0 3px rgba(239, 68, 68, 0.2); }
  100% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0), 0 0 0 6px rgba(239, 68, 68, 0); }
}

@media (prefers-reduced-motion: reduce) {
  .search-mic.listening {
    animation: none;
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25);
  }
}

/* Processing: recognition.stop() was called and we're waiting for onend to
   flush the final result — a beat where the mic shouldn't look idle. */
.search-mic.processing {
  color: var(--search-focus);
  pointer-events: none;
}

/* Error: transient, auto-reverts to idle (see flashMicError in voice-search.js). */
.search-mic.mic-error {
  color: #f59e0b;
}

/* Permission denied/blocked: persists until the user taps the mic again. */
.search-mic.mic-denied {
  color: #ef4444;
  opacity: 0.75;
}

/* Unsupported browser: a real disabled <button>, dimmed rather than hidden
   so there's still a glanceable "voice search exists but isn't available
   here" cue instead of a silent gap in the search box. */
.search-mic:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  animation: none;
}

.search-mic:disabled:hover {
  background: transparent;
  color: var(--search-icon-color);
}

/* ── Language preference popover (long-press / right-click a mic button) ── */
.voice-search-lang-popover {
  z-index: 9999;
  min-width: 190px;
  padding: 6px;
  border-radius: 12px;
  background: var(--search-bg);
  border: 1px solid var(--search-border);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.voice-search-lang-option {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--search-text);
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
}

.voice-search-lang-option:hover,
.voice-search-lang-option:focus-visible {
  background: var(--search-clear-hover-bg);
  outline: none;
}

.voice-search-lang-option.active {
  font-weight: 600;
  color: var(--search-focus);
}

/* Reserve room in the input for the mic icon; where a clear (✕) button can
   also appear (has-clear), give each icon its own slot so they never overlap. */
.search-container.has-mic input.search-input {
  padding-right: 46px;
}

.search-container.has-mic.has-clear .search-mic {
  right: 40px;
}

.search-container.has-mic.has-clear input.search-input {
  padding-right: 74px;
}

/* Modifier for mic buttons placed outside a .search-container wrapper (plain
   flex/inline-flow rows without the absolute-positioning context the default
   .search-mic assumes). Cancels the absolute offset so it lays out inline. */
.search-mic-inline {
  position: relative;
  right: auto;
  top: auto;
  transform: none;
  display: inline-flex;
  vertical-align: middle;
  flex-shrink: 0;
}

.search-loading {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--search-border);
  border-top-color: var(--search-focus);
  animation: search-spin 0.6s linear infinite;
  display: none;
}

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

@keyframes search-spin {
  to { transform: translateY(-50%) rotate(360deg); }
}

@media (max-width: 768px) {
  :root {
    --search-height: var(--search-height-mobile);
  }
}

@media (prefers-reduced-motion: reduce) {
  .search-loading {
    animation: none;
  }
}

/* ═══════════════════════════════════════════════════════════════════
   Fullscreen voice search overlay
   Opened by js/voice-search.js on every mic tap (all breakpoints — this
   replaces the old desktop-only floating pill). Purely a visual layer:
   it renders whatever state the existing SpeechRecognition session is
   already in and mirrors the transcript that's already being written
   into the real search input — no new recognition/search logic lives
   here. BRS-colored (amber focus, existing bg/border tokens), not a
   Google-branded clone.
   ═══════════════════════════════════════════════════════════════════ */

.voice-overlay {
  position: fixed;
  inset: 0;
  z-index: 10050; /* above .mobile-search-overlay (10000) and the lang popover (9999) */
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(15, 15, 20, 0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
}

.voice-overlay[hidden] {
  display: none;
}

.voice-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

body.voice-overlay-open {
  overflow: hidden;
}

.voice-overlay-panel {
  width: 100%;
  height: 100%;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--search-bg);
  color: var(--search-text);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.25);
  transform: translateY(24px);
  transition: transform 260ms cubic-bezier(0.16, 1, 0.3, 1);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.voice-overlay.open .voice-overlay-panel {
  transform: translateY(0);
}

.voice-overlay-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 0;
  flex-shrink: 0;
}

.voice-overlay-close,
.voice-overlay-lang {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--search-text);
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  cursor: pointer;
  transition: background 150ms ease;
}

.voice-overlay-lang {
  width: auto;
  min-width: 40px;
  padding: 0 12px;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 600;
}

.voice-overlay-close:hover,
.voice-overlay-lang:hover,
.voice-overlay-close:focus-visible,
.voice-overlay-lang:focus-visible {
  background: var(--search-clear-hover-bg);
}

.voice-overlay-close:focus-visible,
.voice-overlay-lang:focus-visible {
  outline: 2px solid var(--search-focus);
  outline-offset: 2px;
}

.voice-overlay-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px 28px;
  text-align: center;
}

.voice-overlay-mic-wrap {
  position: relative;
  width: 112px;
  height: 112px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.voice-overlay-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(239, 68, 68, 0.35);
  opacity: 0;
}

.voice-overlay-mic {
  position: relative;
  z-index: 1;
  width: 84px;
  height: 84px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  color: #fff;
  background: #9ca3af; /* idle: grey */
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  transition: background 200ms ease, transform 150ms ease;
}

.voice-overlay-mic:hover {
  transform: scale(1.03);
}

.voice-overlay-mic:focus-visible {
  outline: 3px solid var(--search-focus);
  outline-offset: 3px;
}

/* ── States ── */
.voice-overlay[data-state="listening"] .voice-overlay-mic,
.voice-overlay[data-state="recognizing"] .voice-overlay-mic {
  background: #ef4444; /* listening: red */
  animation: voice-overlay-mic-pulse 1.4s ease-in-out infinite;
}

.voice-overlay[data-state="listening"] .voice-overlay-ring,
.voice-overlay[data-state="recognizing"] .voice-overlay-ring {
  animation: voice-overlay-ring 2.2s ease-out infinite;
}

.voice-overlay-ring.ring-2 {
  animation-delay: 0.6s !important;
}

.voice-overlay[data-state="processing"] .voice-overlay-mic {
  background: var(--search-focus); /* processing: amber, matches BRS primary */
}

.voice-overlay[data-state="completed"] .voice-overlay-mic {
  background: #22c55e; /* completed: green checkmark */
}

.voice-overlay[data-state="error"] .voice-overlay-mic {
  background: #ef4444;
  animation: voice-overlay-mic-shake 420ms ease;
}

@keyframes voice-overlay-mic-pulse {
  0%, 100% { box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18); }
  50% { box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18), 0 0 0 10px rgba(239, 68, 68, 0.12); }
}

@keyframes voice-overlay-ring {
  0% { transform: scale(0.85); opacity: 0.6; border-color: rgba(239, 68, 68, 0.45); }
  100% { transform: scale(1.55); opacity: 0; border-color: rgba(239, 68, 68, 0); }
}

@keyframes voice-overlay-mic-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

.voice-overlay-waveform {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 28px;
  flex-shrink: 0;
  opacity: 0.35;
}

.voice-overlay[data-state="listening"] .voice-overlay-waveform,
.voice-overlay[data-state="recognizing"] .voice-overlay-waveform {
  opacity: 1;
}

.voice-overlay-waveform span {
  width: 4px;
  height: 8px;
  border-radius: 2px;
  background: #ef4444;
  animation: voice-overlay-wave 1s ease-in-out infinite;
  animation-play-state: paused;
}

.voice-overlay[data-state="listening"] .voice-overlay-waveform span,
.voice-overlay[data-state="recognizing"] .voice-overlay-waveform span {
  animation-play-state: running;
}

.voice-overlay-waveform span:nth-child(1) { animation-delay: 0ms; }
.voice-overlay-waveform span:nth-child(2) { animation-delay: 120ms; }
.voice-overlay-waveform span:nth-child(3) { animation-delay: 240ms; }
.voice-overlay-waveform span:nth-child(4) { animation-delay: 360ms; }
.voice-overlay-waveform span:nth-child(5) { animation-delay: 480ms; }

@keyframes voice-overlay-wave {
  0%, 100% { height: 8px; }
  50% { height: 26px; }
}

.voice-overlay-status {
  font-size: 1rem;
  font-weight: 600;
  color: var(--search-text);
  min-height: 1.3em;
}

.voice-overlay-timer {
  font-size: 0.8rem;
  color: var(--search-placeholder);
  font-variant-numeric: tabular-nums;
  margin-top: -10px;
}

.voice-overlay-transcript {
  font-size: 1.3rem;
  font-weight: 500;
  line-height: 1.5;
  min-height: 1.5em;
  max-height: 30vh;
  overflow-y: auto;
  color: var(--search-text);
}

.voice-overlay-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 18px;
  border-radius: 16px;
  background: var(--search-clear-hover-bg);
  max-width: 320px;
  opacity: 1;
  transition: opacity 250ms ease;
}

.voice-overlay-tip.fade {
  opacity: 0;
}

.voice-overlay-tip-icon {
  font-size: 1.1rem;
}

.voice-overlay-tip-text {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--search-text);
}

.voice-overlay-tip-example {
  font-size: 0.85rem;
  color: var(--search-placeholder);
}

/* Once real speech has been recognized, the rotating "what can I say" tip
   gives way to the actual live transcript rather than competing with it. */
.voice-overlay-transcript:empty {
  display: none;
}

.voice-overlay-transcript:empty + .voice-overlay-tip {
  display: flex;
}

.voice-overlay-transcript:not(:empty) + .voice-overlay-tip {
  display: none;
}

.voice-overlay-footer {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  padding: 8px 24px 24px;
}

.voice-overlay-cancel {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--search-placeholder);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 999px;
  cursor: pointer;
}

.voice-overlay-cancel:hover,
.voice-overlay-cancel:focus-visible {
  background: var(--search-clear-hover-bg);
  color: var(--search-text);
}

.voice-overlay-cancel:focus-visible {
  outline: 2px solid var(--search-focus);
  outline-offset: 2px;
}

/* Centered dialog on larger viewports — edge-to-edge sheet is a mobile-only
   pattern; desktop gets a BRS-styled version of Google's own centered
   voice-search card instead of a jarring full-bleed takeover. */
@media (min-width: 640px) {
  .voice-overlay {
    align-items: center;
    padding: 24px;
  }

  .voice-overlay-panel {
    width: 100%;
    max-width: 420px;
    height: auto;
    max-height: 85vh;
    border-radius: 28px;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.3);
  }
}

@media (prefers-reduced-motion: reduce) {
  .voice-overlay,
  .voice-overlay-panel {
    transition: opacity 1ms linear;
    transform: none;
  }

  .voice-overlay-mic,
  .voice-overlay-ring,
  .voice-overlay-waveform span {
    animation: none;
  }

  .voice-overlay[data-state="listening"] .voice-overlay-mic,
  .voice-overlay[data-state="recognizing"] .voice-overlay-mic {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18), 0 0 0 6px rgba(239, 68, 68, 0.2);
  }

  .voice-overlay-waveform span {
    height: 16px;
  }
}
