/* Tidal Echo adapter for Pelle d'Umore.
 * Pelle d'Umore is CC BY 4.0, created by Cu & Lunede (cunedi.uk).
 */
.assistant-msg .fx-blur:focus-visible {
  outline: 2px solid var(--focus, currentColor);
  outline-offset: 2px;
}

.assistant-msg .fx-glow,
.assistant-msg .fx-big,
.assistant-msg .fx-huge,
.assistant-msg .fx-whisper,
.assistant-msg .fx-red,
.assistant-msg .fx-shake,
.assistant-msg .fx-blur,
.assistant-msg .fx-glitch {
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

/* Tidal Echo's bubble/composer variables are independent from Pelle's
 * upstream tokens. Bridge every mood so the emotional world owns the whole
 * viewport instead of leaving pale reply cards, receipts or inputs on top. */
body[data-mood="rage"] {
  --bg: oklch(0.075 0.035 20);
  --header-bg: oklch(0.075 0.035 20 / 0.86);
  --hairline: oklch(0.55 0.2 22 / 0.3);
  --text: oklch(0.94 0.025 22);
  --text-soft: oklch(0.79 0.045 22);
  --text-faint: oklch(0.61 0.055 22);
  --bubble-ai-bg: oklch(0.16 0.065 22 / 0.9);
  --bubble-ai-fg: oklch(0.96 0.025 22);
  --bubble-human-bg: oklch(0.19 0.085 22 / 0.78);
  --bubble-human-fg: oklch(0.96 0.025 22);
  --composer-bg: oklch(0.13 0.055 22 / 0.96);
  --field-bg: oklch(0.17 0.06 22 / 0.94);
  --field-line: oklch(0.58 0.18 22 / 0.55);
  --card-bg: oklch(0.17 0.055 22 / 0.82);
  --card-line: oklch(0.58 0.18 22 / 0.25);
  --send-bg: oklch(0.42 0.19 22);
  --tick: oklch(0.69 0.1 22);
  --think-flourish: oklch(0.58 0.2 22 / 0.72);
  --think-label: oklch(0.72 0.12 22);
  --think-body: oklch(0.86 0.055 22);
}

body[data-mood="rage2"] {
  --bg: oklch(0.53 0.2 25);
  --header-bg: oklch(0.53 0.2 25 / 0.92);
  --hairline: oklch(0.2 0.11 25 / 0.34);
  --text: oklch(0.13 0.055 25);
  --text-soft: oklch(0.2 0.065 25);
  --text-faint: oklch(0.3 0.075 25);
  --bubble-ai-bg: oklch(0.56 0.19 25 / 0.92);
  --bubble-ai-fg: oklch(0.13 0.05 25);
  --bubble-human-bg: oklch(0.46 0.19 25 / 0.68);
  --bubble-human-fg: oklch(0.12 0.055 25);
  --composer-bg: oklch(0.5 0.19 25 / 0.97);
  --field-bg: oklch(0.58 0.17 25 / 0.94);
  --field-line: oklch(0.28 0.14 25 / 0.62);
  --card-bg: oklch(0.57 0.16 25 / 0.86);
  --card-line: oklch(0.23 0.13 25 / 0.28);
  --send-bg: oklch(0.24 0.14 25);
  --tick: oklch(0.23 0.1 25);
  --think-flourish: oklch(0.22 0.13 25 / 0.72);
  --think-label: oklch(0.18 0.09 25);
  --think-body: oklch(0.14 0.065 25);
}

body[data-mood="desire"] {
  --bg: oklch(0.12 0.05 var(--mood-desire-hue, 348));
  --header-bg: oklch(0.12 0.05 var(--mood-desire-hue, 348) / 0.88);
  --hairline: oklch(0.48 0.13 var(--mood-desire-hue, 348) / 0.34);
  --text: oklch(0.94 0.02 var(--mood-desire-hue, 348));
  --text-soft: oklch(0.78 0.035 var(--mood-desire-hue, 348));
  --text-faint: oklch(0.59 0.04 var(--mood-desire-hue, 348));
  --bubble-ai-bg: oklch(0.2 0.065 var(--mood-desire-hue, 348) / 0.72);
  --bubble-ai-fg: oklch(0.96 0.018 var(--mood-desire-hue, 348));
  --bubble-human-bg: oklch(0.37 0.12 var(--mood-desire-hue, 348) / 0.58);
  --bubble-human-fg: oklch(0.97 0.018 var(--mood-desire-hue, 348));
  --composer-bg: oklch(0.12 0.05 var(--mood-desire-hue, 348) / 0.96);
  --field-bg: oklch(0.19 0.06 var(--mood-desire-hue, 348) / 0.9);
  --field-line: oklch(0.5 0.13 var(--mood-desire-hue, 348) / 0.52);
  --card-bg: oklch(0.18 0.055 var(--mood-desire-hue, 348) / 0.78);
  --card-line: oklch(0.5 0.13 var(--mood-desire-hue, 348) / 0.28);
  --send-bg: oklch(0.52 0.15 var(--mood-desire-hue, 348));
  --tick: oklch(0.72 0.08 var(--mood-desire-hue, 348));
  --think-flourish: oklch(0.55 0.14 var(--mood-desire-hue, 348) / 0.7);
  --think-label: oklch(0.72 0.08 var(--mood-desire-hue, 348));
  --think-body: oklch(0.86 0.035 var(--mood-desire-hue, 348));
}

body[data-mood="vuoto"] {
  --bg: oklch(0.13 0 0);
  --header-bg: oklch(0.13 0 0 / 0.9);
  --hairline: oklch(0.48 0 0 / 0.3);
  --text: oklch(0.86 0 0);
  --text-soft: oklch(0.68 0 0);
  --text-faint: oklch(0.49 0 0);
  --bubble-ai-bg: oklch(0.2 0 0 / 0.7);
  --bubble-ai-fg: oklch(0.88 0 0);
  --bubble-human-bg: oklch(0.32 0 0 / 0.55);
  --bubble-human-fg: oklch(0.9 0 0);
  --composer-bg: oklch(0.12 0 0 / 0.96);
  --field-bg: oklch(0.2 0 0 / 0.9);
  --field-line: oklch(0.5 0 0 / 0.48);
  --card-bg: oklch(0.19 0 0 / 0.76);
  --card-line: oklch(0.48 0 0 / 0.28);
  --send-bg: oklch(0.54 0 0);
  --tick: oklch(0.62 0 0);
  --think-flourish: oklch(0.5 0 0 / 0.62);
  --think-label: oklch(0.67 0 0);
  --think-body: oklch(0.75 0 0);
}

body[data-mood="moonlight"] {
  --bg: oklch(0.14 0.05 var(--mood-moon-hue, 255));
  --header-bg: oklch(0.13 0.055 var(--mood-moon-hue, 255) / 0.88);
  --hairline: oklch(0.52 0.08 var(--mood-moon-hue, 255) / 0.34);
  --text: oklch(0.94 0.02 var(--mood-moon-hue, 255));
  --text-soft: oklch(0.78 0.03 var(--mood-moon-hue, 255));
  --text-faint: oklch(0.58 0.03 var(--mood-moon-hue, 255));
  --bubble-ai-bg: oklch(0.2 0.06 var(--mood-moon-hue, 255) / 0.68);
  --bubble-ai-fg: oklch(0.96 0.018 var(--mood-moon-hue, 255));
  --bubble-human-bg: oklch(0.34 0.09 var(--mood-moon-hue, 255) / 0.58);
  --bubble-human-fg: oklch(0.96 0.018 var(--mood-moon-hue, 255));
  --composer-bg: oklch(0.12 0.05 var(--mood-moon-hue, 255) / 0.94);
  --field-bg: oklch(0.19 0.055 var(--mood-moon-hue, 255) / 0.9);
  --field-line: oklch(0.66 0.08 85 / 0.48);
  --card-bg: oklch(0.18 0.055 var(--mood-moon-hue, 255) / 0.76);
  --card-line: oklch(0.62 0.08 85 / 0.24);
  --send-bg: oklch(0.75 0.1 85);
  --tick: oklch(0.73 0.09 85);
  --think-flourish: oklch(0.7 0.09 85 / 0.68);
  --think-label: oklch(0.76 0.08 85);
  --think-body: oklch(0.84 0.035 var(--mood-moon-hue, 255));
}

body[data-mood] .topbar {
  background: var(--header-bg);
  -webkit-backdrop-filter: blur(10px) saturate(1.18);
  backdrop-filter: blur(10px) saturate(1.18);
}

body[data-mood] .composer {
  background: linear-gradient(180deg, transparent, var(--header-bg) 34%);
}

body[data-mood] .row.ai .bubble {
  box-shadow: 0 16px 44px oklch(0.08 0.04 22 / 0.3);
}

/* The reference presentation lets the active reply become the page instead of
 * leaving it trapped inside a pale chat card. Older history keeps its bubbles;
 * only the line currently carrying the mood opens to the viewport. */
body[data-mood] .row.ai.pelle-current-reply {
  padding-left: 0;
}

body[data-mood] .row.ai.pelle-current-reply::before {
  opacity: 0;
}

body[data-mood] .row.ai.pelle-current-reply .bubble {
  width: 100%;
  max-width: 100%;
  padding-left: 0;
  padding-right: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

body[data-mood="rage"] .msg--assistant.pelle-current-reply .assistant-msg {
  font-weight: 650;
  line-height: 1.72;
}

body[data-mood="rage2"] .msg--assistant.pelle-current-reply .assistant-msg {
  font-weight: 780;
  line-height: 1.72;
  letter-spacing: 0.006em;
}

body[data-mood="rage"] .pelle-current-reply :is(.fx-red, .fx-shake, .fx-glitch, .fx-big, .fx-huge),
body[data-mood="rage2"] .pelle-current-reply :is(.fx-red, .fx-shake, .fx-glitch, .fx-big, .fx-huge) {
  font-weight: 850;
}

body[data-mood] .api-receipt-body,
body[data-mood] .trust-card {
  background: var(--card-bg);
  border-color: var(--card-line);
  color: var(--text);
}

body[data-mood] .api-receipt-badge,
body[data-mood] .trust-use {
  background: transparent;
  border-color: currentColor;
  color: inherit;
}

/* The upstream desire skin is deliberately subtle. Tidal Echo's companion
 * presentation needs an unmistakable active state: a full-viewport wine-red
 * pulse plus a strong pulse on the line being spoken. The viewport wash sits
 * above the existing app so it cannot disappear behind the wallpaper/app
 * stacking contexts, but it never captures input. */
body[data-mood="desire"]::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 2147482000;
  pointer-events: none;
  /* v19: the pulse color is baked in statically (the old keyframes animated
     background-color — a non-compositable property, so the whole viewport
     repainted every frame under multiply). Only opacity animates now. */
  background:
    radial-gradient(
      115% 82% at 50% 45%,
      oklch(0.58 0.24 var(--mood-desire-hue, 348) / 0.34),
      oklch(0.32 0.2 var(--mood-desire-hue, 348) / 0.24) 52%,
      oklch(0.18 0.13 var(--mood-desire-hue, 348) / 0.42) 100%
    ),
    oklch(0.42 0.24 var(--mood-desire-hue, 348) / 0.26);
  mix-blend-mode: multiply;
  opacity: 0.48;
  /* v20: finite — six deep breaths on entry (~19s of unmistakable arrival),
     then the veil holds statically. On ProMotion screens ANY infinite
     animation pins the display at high refresh; the drama belongs to the
     entrance, not to minute forty. */
  animation: pelle-desire-screen-pulse 3.2s ease-in-out 6 !important;
}

/* This selector deliberately outranks the upstream
 * :nth-last-child(...).assistant-msg animation.
 * v19: finite — the flash marks the line being spoken; ten beats (~12s) of
 * arrival drama, then it settles to the upstream static glow instead of
 * repainting that text region forever. */
body[data-mood="desire"] .msg--assistant.pelle-current-reply .assistant-msg {
  animation: pelle-desire-line-flash 1.15s steps(2, end) 10 !important;
}

/* iPhone/Safari compatibility fallback for the upstream
 * :nth-last-child(... of ...) newest-message selector.
 * v20: four slow drifts (~32s) after arrival, then the static gold glow. */
body[data-mood="moonlight"] .msg--assistant.pelle-current-reply .assistant-msg {
  animation: mood-moon-glow 8s ease-in-out 4;
}

/* v20: cap the upstream newest-message loops the same way — the breath/drift
 * reads as "the line being said right now"; it shouldn't repaint that text
 * region forever. Engines without :nth-last-child(of) skip these harmlessly. */
body[data-mood="desire"] .msg--assistant:nth-last-child(1 of .msg--assistant) .assistant-msg {
  animation-iteration-count: 6 !important;
}
body[data-mood="moonlight"] .msg--assistant:nth-last-child(1 of .msg--assistant) .assistant-msg {
  animation-iteration-count: 4 !important;
}

.pelle-lab {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
  background: rgba(5, 5, 10, 0.76);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  overflow: auto;
}

.pelle-lab__panel {
  width: min(680px, 100%);
  max-height: min(900px, 94vh);
  overflow: auto;
  padding: 18px;
  border: 1px solid var(--accent-line, rgba(255,255,255,.22));
  border-radius: 18px;
  color: var(--text, #f3f1f7);
  background: var(--panel-solid, rgba(18, 16, 26, .96));
  box-shadow: 0 24px 80px rgba(0,0,0,.54);
}

.pelle-lab__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.pelle-lab__header strong {
  display: block;
  font-size: 18px;
}

.pelle-lab__header span,
.pelle-lab__motion {
  display: block;
  margin-top: 4px;
  color: var(--text-dim, #a8a2b3);
  font-size: 12px;
  line-height: 1.55;
}

.pelle-lab__motion {
  margin-top: 12px;
  padding: 9px 11px;
  border-radius: 10px;
  background: rgba(255,255,255,.07);
}

.pelle-lab__section-title {
  margin: 18px 0 9px;
  color: var(--text-strong, #fff);
  font-size: 13px;
  font-weight: 700;
}

.pelle-lab__buttons {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.pelle-lab__button {
  appearance: none;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--accent-line, rgba(255,255,255,.22));
  border-radius: 11px;
  color: var(--text, #f3f1f7);
  background: var(--accent-wash, rgba(255,255,255,.08));
  font: inherit;
  font-size: 12px;
}

.pelle-lab__button:active {
  transform: scale(.98);
  background: var(--accent-line, rgba(255,255,255,.16));
}

.pelle-lab__close {
  flex: none;
  min-height: 34px;
}

.pelle-lab__sample {
  padding: 14px;
  border: 1px solid var(--border-soft, rgba(255,255,255,.16));
  border-radius: 13px;
  background: var(--chat-panel-bg, rgba(255,255,255,.06));
  font-size: 15px;
  line-height: 2.25;
}

.pelle-lab__sample .fx-blur {
  cursor: pointer;
}

.pelle-lab-launch {
  grid-column: 1 / -1;
  margin-top: 4px;
  padding-top: 9px;
  border-top: 1px solid var(--hairline);
}

.pelle-lab-launch__button {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--accent-line, rgba(91,146,117,.28));
  border-radius: 10px;
  color: var(--accent, #4d7e63);
  background: var(--accent-wash, rgba(91,146,117,.1));
  font: inherit;
  font-weight: 700;
}

@media (min-width: 560px) {
  .pelle-lab__buttons {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* v20: finite — these inline accents breathe with the arriving line, then
   rest at their static styling instead of repainting forever. */
.pelle-current-reply .fx-whisper {
  animation: pelle-whisper-breath 2.8s ease-in-out 8;
}

.pelle-current-reply .fx-red {
  animation: pelle-red-heartbeat 1.65s ease-in-out 10;
}

/* v19: opacity-only (compositor layer). The wine tone lives in the element's
   static background above — do not reintroduce animated background-color.
   v20: starts and ends at the element's resting opacity so the finite run
   hands off to the static veil without a visible jump. */
@keyframes pelle-desire-screen-pulse {
  0%, 100% { opacity: 0.48; }
  46%      { opacity: 0.74; }
  58%      { opacity: 0.42; }
}

@keyframes pelle-desire-line-flash {
  0%, 100% {
    opacity: 1;
    text-shadow:
      0 0 4px oklch(0.82 0.16 var(--mood-desire-hue, 348) / 0.55),
      0 0 12px oklch(0.68 0.24 var(--mood-desire-hue, 348) / 0.48);
  }
  50% {
    opacity: 0.58;
    text-shadow:
      0 0 10px oklch(0.9 0.2 var(--mood-desire-hue, 348) / 0.9),
      0 0 24px oklch(0.72 0.29 var(--mood-desire-hue, 348) / 0.82),
      0 0 42px oklch(0.58 0.3 var(--mood-desire-hue, 348) / 0.64);
  }
}

@keyframes pelle-whisper-breath {
  0%, 100% { opacity: 0.62; text-shadow: 0 0 0 transparent; }
  50% { opacity: 0.96; text-shadow: 0 0 9px currentColor; }
}

@keyframes pelle-red-heartbeat {
  0%, 38%, 100% { text-shadow: 0 0 0 transparent; }
  44% { text-shadow: 0 0 8px currentColor; }
  51% { text-shadow: 0 0 2px currentColor; }
  58% { text-shadow: 0 0 11px currentColor; }
}

@media (prefers-reduced-motion: reduce) {
  .mood-invade,
  .mood-star-layer,
  .mood-meteor,
  body[data-mood="desire"]::after,
  body[data-mood="desire"] .msg--assistant.pelle-current-reply .assistant-msg,
  body[data-mood="moonlight"] .msg--assistant.pelle-current-reply .assistant-msg,
  .pelle-current-reply .fx-whisper,
  .pelle-current-reply .fx-red {
    animation: none !important;
  }
}
