/* ==========================================================================
   Map Assistant (Buddy) Stylesheet
   ========================================================================== */

/* --- Assistant Guide Toggle and Touch Ask Button --- */
#buddy-toggle {
  display: none;
  align-self: flex-start;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface-glass);
  backdrop-filter: blur(12px) saturate(1.2);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-float);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 150ms ease, background-color 150ms ease;
}

#buddy-toggle:hover,
#buddy-toggle:focus-within {
  border-color: var(--accent);
}

#buddy-toggle:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#buddy-toggle input[type="checkbox"] {
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.buddy-toggle-hint {
  display: none;
  margin-left: 4px;
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
}

#buddy-toggle:has(input:checked) .buddy-toggle-hint {
  display: inline;
}

/* Drawn as a key cap, so it reads as a key to press. */
.buddy-toggle-hint kbd {
  display: inline-block;
  min-width: 1.6em;
  margin-left: 2px;
  padding: 1px 5px 0;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-bottom-width: 3px;
  border-radius: 5px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  font: inherit;
  font-weight: 700;
  line-height: 1.3;
  text-align: center;
  color: var(--text);
}

#buddy-btn {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  background: var(--surface-glass);
  backdrop-filter: blur(12px) saturate(1.2);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-float);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 150ms ease, background-color 150ms ease;
}

#buddy-btn:hover,
#buddy-btn[aria-expanded="true"] {
  border-color: var(--accent);
}

#buddy-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (hover: hover) and (pointer: fine) {
  #buddy-toggle {
    display: inline-flex;
  }

  #buddy-btn {
    display: none;
  }
}

body.buddy-off #buddy-sprite,
body.buddy-off #buddy-bubble {
  display: none !important;
}

/* --- Sprite Pointer --- */
#buddy-sprite {
  display: none;
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  z-index: 2000;
  pointer-events: none;
  will-change: transform;
  transition: opacity 200ms ease;
}

@media (hover: hover) and (pointer: fine) {
  body.view-map:not(.morphing) #buddy-sprite,
  body.view-life:not(.morphing) #buddy-sprite {
    display: block;
  }
}

#buddy-sprite.is-away {
  opacity: 0;
}

#buddy-sprite .bird,
.buddy-ghost .bird {
  position: absolute;
  left: -40px;
  top: -30px;
  width: 80px;
  height: 60px;
  display: block;
  overflow: visible;
  transform-origin: 40px 30px;
  filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.9)) drop-shadow(0 2px 3px rgba(0, 0, 0, 0.45));
}

.bird-art-img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-size: 600% 100%;
  background-position: 40% 0%;
  transform-origin: 40px 30px;
}

/* The pulse runs on the bird element because the script owns the sprite's own transform and the transforms inside the bird. */
#buddy-sprite.is-thinking .bird {
  animation: buddy-sprite-pulse 1.8s ease-in-out infinite;
}

@keyframes buddy-sprite-pulse {
  0%, 100% {
    transform: scale(1);
    filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.9)) drop-shadow(0 2px 3px rgba(0, 0, 0, 0.45));
  }
  50% {
    transform: scale(1.15);
    filter: drop-shadow(0 0 8px var(--accent));
  }
}

#buddy-sprite.is-swap .bird {
  animation: buddy-bird-swap 260ms ease-out;
}

@keyframes buddy-bird-swap {
  from {
    opacity: 0;
    transform: scale(0.6);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

#buddy-sprite.is-click::after {
  content: "";
  position: absolute;
  left: var(--tip-x, 0px);
  top: var(--tip-y, 0px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  box-sizing: border-box;
  pointer-events: none;
  animation: buddy-click-burst 450ms ease-out forwards;
}

@keyframes buddy-click-burst {
  0% {
    transform: translate(-50%, -50%) scale(0.2);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(3.5);
    opacity: 0;
  }
}

/* The old bird flying off during a species swap; the script owns its transform and opacity. */
.buddy-ghost {
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  z-index: 2000;
  pointer-events: none;
  will-change: transform, opacity;
}

/* --- Speech Bubble --- */
#buddy-bubble {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2001;
  width: max-content;
  max-width: min(300px, 100vw - 24px);
  padding: 10px 12px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-float);
  font: 13px/1.45 var(--font-sans);
  box-sizing: border-box;
  animation: buddy-bubble-in 180ms ease-out;
}

#buddy-bubble[hidden] {
  display: none;
}

#buddy-bubble.is-moving {
  opacity: 0;
  visibility: hidden;
}

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

/* --- Bubble Text and Status --- */
.buddy-text,
.buddy-status {
  margin: 0;
  white-space: pre-line;
  overflow-wrap: anywhere;
}

.buddy-status {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}

.buddy-status:empty {
  display: none;
}

.buddy-guide {
  display: none;
  margin: 6px 0 0 0;
  font-size: 11px;
  color: var(--muted);
}

@media (hover: hover) and (pointer: fine) {
  #buddy-bubble.is-ask .buddy-guide {
    display: block;
  }
}

/* --- Bubble Input Form (.buddy-ask) --- */
.buddy-ask {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

/* A one-line textarea that wraps and grows to four lines (4 x 18 + 12 padding + 2 border), then scrolls */
.buddy-input {
  flex: 1;
  min-width: 0;
  height: 32px;
  max-height: 86px;
  padding: 6px 8px;
  resize: none;
  overflow-y: auto;
  background: var(--surface-glass);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  line-height: 18px;  /* after the font shorthand, which resets it */
  box-sizing: border-box;
}

.buddy-input::placeholder {
  color: var(--muted);
}

.buddy-input:focus {
  border-color: var(--accent);
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.buddy-send {
  height: 32px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  box-sizing: border-box;
  transition: background-color 150ms ease;
}

.buddy-send:hover {
  background-color: color-mix(in srgb, var(--accent) 15%, transparent);
}

.buddy-send:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Push to talk: hold the mic (or "/") and speak; it lights up while the browser listens */
.buddy-mic {
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  box-sizing: border-box;
  transition: color 150ms ease, border-color 150ms ease;
}

.buddy-mic:hover {
  color: var(--text);
  border-color: var(--accent);
}

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

#buddy-bubble.is-listening .buddy-mic {
  background: var(--accent);
  border-color: var(--accent);
  color: #111111;
  animation: buddy-thinking-pulse 1s ease-in-out infinite;
}

/* --- More Button --- */
.buddy-more {
  display: inline-block;
  margin-top: 6px;
  padding: 0;
  background: transparent;
  border: none;
  color: var(--accent);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.buddy-more:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.buddy-more[hidden] {
  display: none;
}

/* --- Choices: answers the assistant offers, pressed instead of typed --- */
.buddy-choices {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.buddy-choices[hidden] {
  display: none;
}

.buddy-choice {
  padding: 4px 10px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 999px;
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.buddy-choice:hover {
  background-color: color-mix(in srgb, var(--accent) 20%, transparent);
}

.buddy-choice:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Bubble Foot and Actions --- */
.buddy-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.buddy-count {
  font-size: 12px;
  color: var(--muted);
  margin-right: auto;
}

.buddy-count:empty,
.buddy-count[hidden] {
  display: none;
}

.buddy-next {
  height: 26px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #111111;
  border: none;
  border-radius: 6px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  box-sizing: border-box;
}

.buddy-next:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.buddy-next[hidden] {
  display: none;
}

.buddy-close {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: color 150ms ease, background-color 150ms ease;
  box-sizing: border-box;
}

.buddy-close:hover {
  color: var(--text);
  background-color: color-mix(in srgb, var(--panel) 60%, transparent);
}

.buddy-close:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Bubble Modes --- */
/* Mode: is-ask -> only form and close button show */
#buddy-bubble.is-ask .buddy-text,
#buddy-bubble.is-ask .buddy-more,
#buddy-bubble.is-ask .buddy-status,
#buddy-bubble.is-ask .buddy-choices,
#buddy-bubble.is-ask .buddy-count,
#buddy-bubble.is-ask .buddy-next {
  display: none;
}

#buddy-bubble.is-ask .buddy-foot {
  justify-content: flex-end;
  margin-top: 6px;
}

#buddy-bubble.is-ask .buddy-close {
  margin-left: auto;
}

/* Mode: is-thinking -> only text shows, muted with soft pulse */
#buddy-bubble.is-thinking .buddy-ask,
#buddy-bubble.is-thinking .buddy-more,
#buddy-bubble.is-thinking .buddy-status,
#buddy-bubble.is-thinking .buddy-choices,
#buddy-bubble.is-thinking .buddy-foot {
  display: none;
}

#buddy-bubble.is-thinking .buddy-text {
  color: var(--muted);
  animation: buddy-thinking-pulse 1.4s ease-in-out infinite;
}

@keyframes buddy-thinking-pulse {
  0%, 100% {
    opacity: 0.6;
  }
  50% {
    opacity: 1;
  }
}

/* Mode: is-answer -> form hidden, everything else shows (unless [hidden]) */
#buddy-bubble.is-answer .buddy-ask {
  display: none;
}

/* --- Focus Glow Animation --- */
.buddy-glow {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  animation: buddy-pulse 0.8s ease-in-out 3;
}

@keyframes buddy-pulse {
  0%, 100% {
    outline-color: var(--accent);
  }
  50% {
    outline-color: transparent;
  }
}
