.chat-popup-wrap {
  --bg: #fbf7ef;
  --panel: #ffffff;
  --ink: #271f17;
  --muted: #6f6357;
  --accent: #244f2f;
  --line: #eadfcf;
  --bubble-user: #e7f1e7;
  --bubble-bot: #d5c6dc;
  --bubble-bot-name: #55415e;
  --bubble-bot-border: #b8a5c2;
  --bubble-user-name: #2d6032;
  --shadow: 0 18px 32px rgba(30, 23, 14, 0.18);
  --radius: 12px;
  --wwc-green: #244f2f;
  --wwc-header-lavender: #d5c6dc;
}

.chat-popup-wrap, .chat-popup-wrap * { box-sizing: border-box; }
.chat-popup-wrap { color: var(--ink); font-family: "Avenir Next", "Segoe UI", Helvetica, Arial, sans-serif; }

.launch-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

body.preview-page {
  min-height: 100vh;
}

.chat-popup-wrap button { border: 1px solid #294a2e; background: var(--accent); color: white; font-weight: 700; border-radius: var(--radius); padding: 10px 14px; cursor: pointer; }
.chat-popup-wrap button[disabled] { opacity: .58; cursor: not-allowed; }
.launch-page button { border: 1px solid #294a2e; background: #1e4528; color: white; font-weight: 700; border-radius: 12px; padding: 10px 14px; cursor: pointer; }

.chat-layout {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  flex: 1 1 auto;
  height: 100%;
}
.chat-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
  height: 100%;
  gap: 8px;
  overflow: hidden;
}

.chat-window { 
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  background: #fcfbf7;
  min-height: 110px;
  max-height: none;
}
.msg { margin: 8px 0; display: flex; }
.msg-bot, .msg-user { display: flex; width: 100%; }
.msg-bot { justify-content: flex-start; }
.msg-user { justify-content: flex-end; }
.msg-bubble {
  max-width: 87%;
  width: 87%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid transparent;
  line-height: 1.45;
  white-space: pre-wrap;
  font-size: 15px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 85%;
  align-items: flex-start;
  box-sizing: border-box;
  text-align: left;
}
.msg-bubble-user { background: var(--bubble-user); border-color: #b8d4b8; }
.msg-bubble-bot { background: var(--bubble-bot); border-color: var(--bubble-bot-border); }
.msg-text {
  width: 100%;
  box-sizing: border-box;
  text-align: left;
}
.msg-sender {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 0;
  text-align: left;
}
.msg-user .msg-sender { color: var(--bubble-user-name); }
.msg-bot .msg-sender { color: var(--bubble-bot-name); }
.msg-text-user { text-align: right; }
.msg-separator {
  height: 0;
  border: 0;
  border-top: 1px solid var(--line);
  width: 50%;
  margin: 0;
}
.msg-user .msg-bubble {
  align-items: stretch;
  text-align: left;
  margin-left: auto;
  margin-right: 0;
}

.chat-form {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}

.chat-form textarea {
  width: 100%;
  min-width: 0;
  min-height: 7.5rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  font-size: 16px;
  font-family: inherit;
  resize: vertical;
  line-height: 1.45;
}

.chat-form textarea::placeholder {
  color: #8d8378;
  white-space: normal;
}

.chat-form button {
  display: inline-block;
  width: auto;
  min-width: 112px;
  min-height: 30px;
  align-self: flex-start;
}

.chat-form #chatSend {
  width: 50%;
  min-height: 26px;
  padding-top: 4px;
  padding-bottom: 4px;
  line-height: 1;
}

.chat-form .chat-actions {
  display: flex;
  gap: 8px;
}

.chat-form .chat-actions button {
  flex: 1 1 0;
  width: auto;
  max-width: none;
  min-height: 26px;
  align-self: auto;
}

.chat-form .step-back-btn {
  width: auto;
  max-width: none;
  background: #8a1f23;
  border-color: #611417;
  color: #fff;
  margin-top: 0;
  min-height: 30px;
}

.chat-form .step-back-btn:hover { filter: brightness(1.05); }

.chat-popup-wrap textarea { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-family: inherit; }

.chat-popup-wrap {
  position: fixed;
  z-index: 1000;
  inset: auto 16px 16px auto;
  max-width: min(640px, calc(100vw - 32px));
  width: min(640px, calc(100vw - 32px));
  height: min(720px, calc(100dvh - 32px));
  max-height: calc(100dvh - 32px);
  display: none;
}

.chat-popup-wrap:not(.hidden) { display: block; }

.chat-popup {
  width: 100%;
  height: 100%;
  max-height: calc(100vh - 32px);
  background: var(--wwc-header-lavender);
  border: 1px solid #d2c2ad;
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: visible;
  padding: 0 8px 8px;
}

.chat-popup-head {
  --mascot-size: clamp(158px, 30vw, 214px);
  position: relative;
  display: flex;
  align-items: center;
  min-height: 58px;
  padding: 8px 4px;
}
.chat-popup-head p {
  margin: 0;
  padding-right: calc(var(--mascot-size) + 50px);
  font-weight: 700;
}
.chat-popup-head button {
  position: absolute;
  right: 4px;
  top: 50%;
  z-index: 3;
  transform: translateY(-50%);
  background: #fff;
  color: var(--ink);
  border-color: #d4c6b5;
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 27px;
  line-height: 1;
  font-weight: 800;
  border-radius: 999px;
}

.chat-mascot {
  position: absolute;
  right: 50px;
  bottom: -4px;
  width: var(--mascot-size);
  height: var(--mascot-size);
  pointer-events: none;
  z-index: 2;
}

.mascot-state {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transform-origin: 50% 85%;
  transition: opacity 180ms ease;
}

.chat-mascot[data-state="typing"] .mascot-typing,
.chat-mascot[data-state="thinking"] .mascot-thinking,
.chat-mascot[data-state="pointing"] .mascot-pointing,
.chat-mascot[data-state="ban"] .mascot-ban { opacity: 1; }

.chat-bubble {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 999;
  border-radius: 999px;
  background: var(--wwc-green);
  color: white;
  border: 1px solid #1a3b20;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
}

body.preview-page .chat-bubble {
  display: none;
}

.chat-bubble .bubble-icon {
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.2);
}

.chat-bubble[aria-expanded="true"] { background: #1f472c; }

.chat-popup-wrap.hidden, .chat-bubble.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 960px) {
  .chat-layout { min-height: auto; }
  .chat-popup-wrap {
    left: 8px;
    right: 8px;
    bottom: 8px;
    max-width: none;
    width: calc(100vw - 16px);
    height: calc(100dvh - 16px);
    max-height: calc(100dvh - 16px);
  }
  .chat-popup {
    width: 100%;
    height: 100%;
    max-height: calc(100vh - 16px);
    border-radius: 16px;
  }
  .chat-window { min-height: 120px; }
}

@media (max-width: 600px) {
  .chat-popup-head {
    --mascot-size: clamp(130px, 42vw, 170px);
    min-height: 58px;
  }
  .chat-popup-head p { font-size: 15px; line-height: 1.2; }
  .chat-panel { padding: 8px; }
  .chat-form textarea { min-height: 84px; }
  .msg-bubble { width: 94%; max-width: 94%; min-width: 0; font-size: 14px; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .chat-popup-wrap { inset: 4px; width: auto; max-width: none; height: calc(100dvh - 8px); max-height: none; }
  .chat-popup-head { --mascot-size: 142px; min-height: 52px; }
  .chat-form { margin-top: 4px; gap: 4px; }
  .chat-form textarea { min-height: 58px; max-height: 72px; }
  .chat-window { min-height: 60px; padding: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  .mascot-state { animation: none !important; transition: none; }
}
