/* ═══════════════════════════════════════════════════════════════════════════
   Willy Chat Widget — dgs-theme
   Prefixed: dgs-willy-*
   Uses theme design tokens: --dgs-bg, --dgs-text, --dgs-accent, etc.
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Design tokens (fallback if theme vars missing) ─────────────────────── */
#dgs-willy-root {
  --willy-accent: #22D3EE;
  --willy-accent-light: #67E8F9;
  --willy-surface: oklch(18% 0 0);
  --willy-surface-2: oklch(25% 0 0);
  --willy-bg: oklch(12% 0 0);
  --willy-text: oklch(95% 0 0);
  --willy-text-dim: oklch(70% 0 0);
  --willy-radius: 1rem;
  --willy-radius-sm: .6rem;
  --willy-shadow: 0 0 40px rgba(34,211,238,.12), 0 8px 32px rgba(0,0,0,.4);
  --willy-font: system-ui, -apple-system, sans-serif;
}

/* ── Bubble (closed state) ─────────────────────────────────────────────── */
.dgs-willy-bubble {
  position: fixed;
  bottom: 1.5rem;
  right: 1rem;
  z-index: 99990;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .5rem;
}

.dgs-willy-bubble__btn {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: transparent;
  border: none;
  box-shadow: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .15s ease, box-shadow .15s ease;
  position: relative;
  overflow: visible;
}
.dgs-willy-bubble__btn:hover {
  transform: scale(1.08);
  box-shadow: 0 0 50px rgba(34,211,238,.2), 0 8px 40px rgba(0,0,0,.5);
}
.dgs-willy-bubble__btn:active {
  transform: scale(.95);
}
.dgs-willy-bubble__btn svg {
  width: 58px;
  height: 58px;
}

/* Badge */
.dgs-willy-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background: #EF4444;
  color: #fff;
  font-size: .7rem;
  font-weight: 700;
  font-family: var(--willy-font);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  animation: willy-badge-bounce .4s ease;
}
.dgs-willy-badge:empty { display: none; }

/* Breathing dots */
.dgs-willy-dots {
  display: flex;
  gap: 5px;
}
.dgs-willy-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: oklch(50% 0 0);
  animation: willy-dot-breathe 1.4s ease-in-out infinite;
}
.dgs-willy-dots span:nth-child(2) { animation-delay: .2s; }
.dgs-willy-dots span:nth-child(3) { animation-delay: .4s; }

/* Speech tooltip */
.dgs-willy-tooltip {
  position: absolute;
  bottom: 88px;
  right: 0;
  background: var(--willy-surface);
  color: var(--willy-text);
  border: 1px solid oklch(30% 0 0);
  border-radius: var(--willy-radius);
  padding: .6rem 1rem;
  font-size: .82rem;
  font-family: var(--willy-font);
  white-space: nowrap;
  box-shadow: 0 2px 12px rgba(0,0,0,.3);
  animation: willy-tooltip-in .3s ease;
  pointer-events: none;
  max-width: 220px;
  white-space: normal;
}
.dgs-willy-tooltip::after {
  content: '';
  position: absolute;
  bottom: -7px;
  right: 28px;
  width: 12px;
  height: 12px;
  background: var(--willy-surface);
  border-right: 1px solid oklch(30% 0 0);
  border-bottom: 1px solid oklch(30% 0 0);
  transform: rotate(45deg);
}

/* ── Widget Panel (open state) ─────────────────────────────────────────── */
.dgs-willy-overlay {
  position: fixed;
  inset: 0;
  z-index: 99995;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(4px);
  display: none;
}
.dgs-willy-overlay--open { display: block; }

.dgs-willy-panel {
  position: fixed;
  z-index: 99999;
  display: none;
  flex-direction: column;
  background: var(--willy-bg);
  border: 1px solid oklch(25% 0 0);
  box-shadow: var(--willy-shadow);
  font-family: var(--willy-font);
  overflow: hidden;
}
.dgs-willy-panel--open { display: flex; }

/* Mobile: fullscreen */
@media (max-width: 767px) {
  .dgs-willy-panel {
    top: 0; left: 0; right: 0; bottom: 0;
    border-radius: 0;
    border: none;
  }
  .dgs-willy-bubble { bottom: 4.5rem; right: .75rem; }
  .dgs-willy-bubble__btn { width: 64px; height: 64px; }
  .dgs-willy-bubble__btn svg { width: 48px; height: 48px; }
}

/* Desktop: bottom-right panel */
@media (min-width: 768px) {
  .dgs-willy-panel {
    bottom: 1.5rem;
    right: 1.5rem;
    width: min(420px, calc(100vw - 2rem));
    height: min(580px, calc(100vh - 6rem));
    border-radius: 1.5rem;
  }
}

/* ── Panel Header ───────────────────────────────────────────────────────── */
.dgs-willy-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .75rem 1rem;
  background: var(--willy-surface);
  border-bottom: 1px solid oklch(25% 0 0);
  flex-shrink: 0;
}
.dgs-willy-header__avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}
.dgs-willy-header__avatar svg { width: 36px; height: 36px; }
.dgs-willy-header__info { flex: 1; min-width: 0; }
.dgs-willy-header__name {
  font-size: .9rem;
  font-weight: 600;
  color: var(--willy-text);
}
.dgs-willy-header__status {
  font-size: .72rem;
  color: var(--willy-text-dim);
  display: flex;
  align-items: center;
  gap: .3rem;
}
.dgs-willy-header__status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22C55E;
}
.dgs-willy-header__status-dot--off { background: oklch(45% 0 0); }
.dgs-willy-header__close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--willy-text-dim);
  cursor: pointer;
  border-radius: 50%;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}
.dgs-willy-header__close:hover {
  background: oklch(30% 0 0);
  color: var(--willy-text);
}

/* ── Messages List ──────────────────────────────────────────────────────── */
.dgs-willy-messages {
  flex: 1;
  overflow-y: auto;
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
  scroll-behavior: smooth;
}
.dgs-willy-messages::-webkit-scrollbar {
  width: 4px;
}
.dgs-willy-messages::-webkit-scrollbar-thumb {
  background: oklch(30% 0 0);
  border-radius: 2px;
}

/* Message bubbles */
.dgs-willy-msg {
  max-width: 85%;
  padding: .55rem .8rem;
  border-radius: var(--willy-radius);
  font-size: .85rem;
  line-height: 1.45;
  word-break: break-word;
  animation: willy-msg-in .25s ease;
}
.dgs-willy-msg--user {
  align-self: flex-end;
  background: oklch(30% .12 250);
  color: #fff;
  border-bottom-right-radius: .3rem;
}
.dgs-willy-msg--assistant {
  align-self: flex-start;
  background: var(--willy-surface-2);
  color: var(--willy-text);
  border-bottom-left-radius: .3rem;
}
.dgs-willy-msg--system {
  align-self: center;
  background: transparent;
  color: var(--willy-text-dim);
  font-size: .75rem;
  text-align: center;
  max-width: 100%;
}

/* Markdown inside messages */
.dgs-willy-msg p { margin: 0 0 .4rem; }
.dgs-willy-msg p:last-child { margin: 0; }
.dgs-willy-msg strong { color: var(--willy-accent); }
.dgs-willy-msg a {
  color: var(--willy-accent);
  text-decoration: underline;
}
.dgs-willy-msg code {
  background: oklch(20% 0 0);
  padding: .1em .3em;
  border-radius: 3px;
  font-size: .8em;
}
.dgs-willy-msg pre {
  background: oklch(15% 0 0);
  padding: .5rem;
  border-radius: .4rem;
  overflow-x: auto;
  font-size: .78em;
  margin: .4rem 0;
}
.dgs-willy-msg ul, .dgs-willy-msg ol {
  margin: .3rem 0;
  padding-left: 1.2rem;
}

/* Loading dots */
.dgs-willy-loading {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: .6rem .8rem;
  background: var(--willy-surface-2);
  border-radius: var(--willy-radius);
}
.dgs-willy-loading span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--willy-accent);
  animation: willy-dot-breathe 1.2s ease-in-out infinite;
}
.dgs-willy-loading span:nth-child(2) { animation-delay: .15s; }
.dgs-willy-loading span:nth-child(3) { animation-delay: .3s; }

/* ── Input Area ─────────────────────────────────────────────────────────── */
.dgs-willy-input-wrap {
  padding: .6rem .75rem;
  border-top: 1px solid oklch(25% 0 0);
  display: flex;
  gap: .5rem;
  flex-shrink: 0;
  background: var(--willy-surface);
}
.dgs-willy-input {
  flex: 1;
  resize: none;
  border: 1px solid oklch(30% 0 0);
  background: var(--willy-bg);
  color: var(--willy-text);
  border-radius: var(--willy-radius-sm);
  padding: .5rem .7rem;
  font-family: var(--willy-font);
  font-size: .85rem;
  line-height: 1.4;
  outline: none;
  min-height: 40px;
  max-height: 100px;
  transition: border-color .15s;
}
.dgs-willy-input:focus {
  border-color: var(--willy-accent);
}
.dgs-willy-input::placeholder {
  color: var(--willy-text-dim);
}
.dgs-willy-send {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--willy-accent);
  color: #000;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform .15s, opacity .15s;
  font-size: 1.1rem;
}
.dgs-willy-send:hover {
  opacity: .9;
}
.dgs-willy-send:active {
  transform: scale(.92);
}
.dgs-willy-send:disabled {
  opacity: .4;
  cursor: not-allowed;
}

/* ── Feedback Row ───────────────────────────────────────────────────────── */
.dgs-willy-feedback {
  display: flex;
  gap: .3rem;
  align-self: flex-start;
  margin-top: -2px;
}
.dgs-willy-feedback button {
  background: transparent;
  border: none;
  color: var(--willy-text-dim);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: .8rem;
  transition: color .15s;
}
.dgs-willy-feedback button:hover { color: var(--willy-accent); }

/* ── Login Gate ─────────────────────────────────────────────────────────── */
.dgs-willy-gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 2rem;
  text-align: center;
  flex: 1;
}
.dgs-willy-gate__icon svg {
  width: 72px;
  height: 72px;
  opacity: .7;
}
.dgs-willy-gate__text {
  color: var(--willy-text-dim);
  font-size: .9rem;
  font-family: var(--willy-font);
}
.dgs-willy-gate__link {
  display: inline-block;
  padding: .6rem 1.5rem;
  background: var(--willy-accent);
  color: #000;
  text-decoration: none;
  border-radius: 2rem;
  font-weight: 600;
  font-size: .85rem;
  font-family: var(--willy-font);
  transition: opacity .15s;
}
.dgs-willy-gate__link:hover { opacity: .85; }

/* ── Suggested Questions ────────────────────────────────────────────────── */
.dgs-willy-suggested {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  padding: .5rem .75rem;
  border-top: 1px solid oklch(25% 0 0);
  flex-shrink: 0;
}
.dgs-willy-suggested button {
  background: var(--willy-surface-2);
  border: 1px solid oklch(30% 0 0);
  color: var(--willy-text);
  padding: .35rem .7rem;
  border-radius: 1rem;
  font-size: .75rem;
  cursor: pointer;
  font-family: var(--willy-font);
  transition: border-color .15s;
  white-space: nowrap;
}
.dgs-willy-suggested button:hover {
  border-color: var(--willy-accent);
  color: var(--willy-accent);
}

/* ── Willy SVG Animation Moods (applied via CSS classes on SVG groups) ─── */
.dgs-willy-body--idle { animation: willy-body-idle 3s ease-in-out infinite; }
.dgs-willy-body--happy { animation: willy-body-happy 1.2s ease-in-out infinite; }
.dgs-willy-body--thinking { animation: willy-body-think 4s ease-in-out infinite; }
.dgs-willy-body--talking { animation: willy-body-talk .5s ease-in-out infinite; }
.dgs-willy-body--surprised { animation: willy-body-surprise .6s ease-in-out infinite; }
.dgs-willy-body--boop { animation: willy-body-boop .5s ease-out; }

.dgs-willy-head--idle { animation: willy-head-idle 3s ease-in-out infinite; transform-origin: 250px 220px; }
.dgs-willy-head--happy { animation: willy-head-happy 1.2s ease-in-out infinite; transform-origin: 250px 220px; }
.dgs-willy-head--thinking { animation: willy-head-think 2.5s ease-in-out infinite; transform-origin: 250px 220px; }
.dgs-willy-head--talking { animation: willy-head-talk .4s ease-in-out infinite; transform-origin: 250px 220px; }
.dgs-willy-head--surprised { animation: willy-head-surprise 1s ease-in-out infinite; transform-origin: 250px 220px; }
.dgs-willy-head--boop { animation: willy-head-boop .5s ease-out; transform-origin: 250px 220px; }

.dgs-willy-eye-blink { animation: willy-blink 5s ease-in-out infinite; }

/* ── Keyframes ──────────────────────────────────────────────────────────── */
@keyframes willy-badge-bounce {
  0% { transform: scale(0); }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

@keyframes willy-dot-breathe {
  0%, 100% { opacity: .3; transform: scale(.8); }
  50% { opacity: 1; transform: scale(1); }
}

@keyframes willy-tooltip-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

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

@keyframes willy-body-idle {
  0%, 100% { transform: scaleY(1) translateY(0); }
  50% { transform: scaleY(1.02) translateY(1px); }
}
@keyframes willy-body-happy {
  0%, 100% { transform: scaleY(1) translateY(0); }
  25% { transform: scaleY(1.06) translateY(-15px); }
  50% { transform: scaleY(.98) translateY(5px); }
  75% { transform: scaleY(1.02) translateY(-2px); }
}
@keyframes willy-body-think {
  0%, 100% { transform: scaleY(1); }
  50% { transform: scaleY(1.01); }
}
@keyframes willy-body-talk {
  0%, 100% { transform: scaleX(1) translateY(0); }
  50% { transform: scaleX(1.02) translateY(-2px); }
}
@keyframes willy-body-surprise {
  0%, 100% { transform: scaleY(1) translateY(0); }
  33% { transform: scaleY(.97) translateY(4px); }
  66% { transform: scaleY(1.03) translateY(-2px); }
}
@keyframes willy-body-boop {
  0% { transform: scaleX(1) scaleY(1) translateY(0); }
  33% { transform: scaleX(1.15) scaleY(.8) translateY(10px); }
  66% { transform: scaleX(.9) scaleY(1.2) translateY(-20px); }
  100% { transform: scaleX(1) scaleY(1) translateY(0); }
}

@keyframes willy-head-idle {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-4px) rotate(1deg); }
  75% { transform: translateY(0) rotate(-1deg); }
}
@keyframes willy-head-happy {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  20% { transform: translateY(-16px) rotate(-5deg); }
  50% { transform: translateY(4px) rotate(5deg); }
  80% { transform: translateY(-1px) rotate(-5deg); }
}
@keyframes willy-head-think {
  0%, 100% { transform: rotate(-3deg) translateY(0); }
  50% { transform: rotate(4deg) translateY(-2px); }
}
@keyframes willy-head-talk {
  0%, 100% { transform: translateY(0) rotate(-1deg); }
  50% { transform: translateY(-3px) rotate(2deg); }
}
@keyframes willy-head-surprise {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-10px) rotate(-5deg); }
  50% { transform: translateY(2px) rotate(5deg); }
  75% { transform: translateY(-2px) rotate(0deg); }
}
@keyframes willy-head-boop {
  0% { transform: translateY(0) rotate(0deg); }
  33% { transform: translateY(5px) rotate(-8deg); }
  66% { transform: translateY(-15px) rotate(8deg); }
  100% { transform: translateY(0) rotate(0deg); }
}

@keyframes willy-blink {
  0%, 96%, 100% { opacity: 1; }
  98% { opacity: .2; }
}

@keyframes willy-thinking-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
@keyframes willy-thinking-spin-ccw {
  from { transform: rotate(0deg); }
  to { transform: rotate(-360deg); }
}

/* ── Tool Call Cards (orders, products, KB) ────────────────────────────── */
.dgs-willy-card {
  margin: .25rem 0 .5rem;
}
.dgs-willy-cards {
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
.dgs-willy-card__item {
  background: var(--willy-surface-2);
  border: 1px solid oklch(28% 0 0);
  border-radius: var(--willy-radius-sm);
  padding: .6rem .75rem;
  transition: border-color .15s;
}
.dgs-willy-card__item:hover {
  border-color: var(--willy-accent);
}
