/* ============================================
   Chat widget — floating modal with onboarding + chat
   Self-contained: just include chat.css + chat.js, mounts itself.
   Reads `celiums.lang` from localStorage to mirror the rest of the site.
   ============================================ */

/* ---------- FAB ---------- */
.cw-fab {
  position: fixed;
  right: clamp(20px, 3vw, 36px);
  bottom: clamp(20px, 3vw, 36px);
  z-index: 200;
  padding: 0;
  border: 1px solid var(--spore);
  background: linear-gradient(180deg, rgba(16,200,96,0.18), rgba(16,200,96,0.08));
  color: var(--ink);
  border-radius: 999px;
  cursor: none;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow:
    0 12px 40px rgba(0,0,0,0.35),
    0 0 30px rgba(16,200,96,0.18);
  transition: transform 0.25s ease, box-shadow 0.25s ease, background 0.25s ease;
  font-family: inherit;
}
.cw-fab:hover {
  transform: translateY(-2px);
  background: linear-gradient(180deg, rgba(16,200,96,0.28), rgba(16,200,96,0.14));
  box-shadow:
    0 0 0 6px rgba(16,200,96,0.08),
    0 18px 50px rgba(0,0,0,0.45),
    0 0 50px rgba(16,200,96,0.32);
}
.cw-fab-inner {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 14px 22px 14px 18px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  position: relative;
}
.cw-fab-x {
  width: 14px; height: 14px;
  opacity: 0;
  transform: scale(0.6) rotate(-45deg);
  transition: opacity 0.2s, transform 0.2s;
  position: absolute;
  right: 18px;
  top: 50%;
  margin-top: -7px;
  color: var(--spore);
}
.cw-fab.is-open .cw-fab-x { opacity: 1; transform: scale(1) rotate(0deg); }
.cw-fab.is-open .cw-fab-label,
.cw-fab.is-open .cw-fab-dot { opacity: 0; transform: translateX(-6px); }
.cw-fab-label, .cw-fab-dot { transition: opacity 0.2s, transform 0.2s; }

.cw-fab-dot {
  position: relative;
  width: 10px; height: 10px;
  display: inline-block;
}
.cw-fab-dot-core {
  position: absolute; inset: 0;
  border-radius: 50%;
  background: var(--spore);
  box-shadow: 0 0 8px var(--spore-glow), 0 0 2px #fff inset;
  animation: cwPulse 2s ease-in-out infinite;
}
.cw-fab-dot-halo {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--spore-glow) 0%, transparent 70%);
  animation: cwHalo 2s ease-in-out infinite;
}
@keyframes cwPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.85; }
}
@keyframes cwHalo {
  0%, 100% { transform: scale(0.7); opacity: 0.6; }
  50% { transform: scale(1.6); opacity: 0; }
}

/* ---------- Backdrop ---------- */
.cw-backdrop {
  position: fixed; inset: 0;
  z-index: 199;
  background: radial-gradient(ellipse at 80% 90%, rgba(16,200,96,0.08), transparent 60%), rgba(0,0,0,0.35);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.cw-backdrop.is-open { opacity: 1; pointer-events: auto; }

/* ---------- Panel ---------- */
.cw-panel {
  position: fixed;
  right: clamp(16px, 3vw, 36px);
  bottom: clamp(86px, 9vw, 110px);
  z-index: 201;
  width: min(420px, calc(100vw - 32px));
  max-height: min(680px, calc(100vh - 140px));
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, rgba(10,12,18,0.92), rgba(5,6,10,0.94));
  border: 1px solid var(--line-bright);
  border-radius: 18px;
  overflow: hidden;
  box-shadow:
    0 30px 80px rgba(0,0,0,0.6),
    0 0 0 1px rgba(16,200,96,0.18),
    0 0 80px rgba(16,200,96,0.12);
  backdrop-filter: blur(16px) saturate(1.1);
  -webkit-backdrop-filter: blur(16px) saturate(1.1);
  opacity: 0;
  transform: translateY(16px) scale(0.97);
  pointer-events: none;
  transition: opacity 0.28s ease, transform 0.28s ease;
}
.cw-panel.is-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}
.cw-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 100% 0%, rgba(16,200,96,0.14), transparent 55%),
    radial-gradient(ellipse at 0% 100%, rgba(16,200,96,0.08), transparent 55%);
  pointer-events: none;
}

/* Body wrapper — propagates flex from .cw-panel down to .cw-chat so
   .cw-msgs gets a bounded box that overflow-y:auto can actually scroll. */
.cw-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 1;
}

/* ---------- Header ---------- */
.cw-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 1;
}
.cw-head-l { display: flex; align-items: center; gap: 12px; }
.cw-head-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--spore);
  box-shadow: 0 0 10px var(--spore-glow);
  animation: cwPulse 2s infinite;
  flex-shrink: 0;
}
.cw-head-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.cw-head-sub {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-top: 2px;
}
.cw-close {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink-dim);
  cursor: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.cw-close:hover { color: var(--ink); border-color: var(--line-bright); background: rgba(255,255,255,0.04); }

/* ---------- Onboarding ---------- */
.cw-onboard {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 22px 22px;
  position: relative;
  z-index: 1;
  overflow-y: auto;
}
.cw-ob-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--spore);
  margin-bottom: 14px;
}
.cw-ob-x {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--spore);
  box-shadow: 0 0 8px var(--spore-glow);
}
.cw-ob-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 26px;
  letter-spacing: -0.025em;
  line-height: 1.05;
  margin-bottom: 8px;
  color: var(--ink);
}
.cw-ob-sub {
  color: var(--ink-dim);
  font-size: 13px;
  line-height: 1.5;
}

.cw-field { display: flex; flex-direction: column; gap: 6px; }
.cw-field-label {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.cw-field input {
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: var(--font-body, var(--font-display));
  font-size: 14px;
  padding: 12px 14px;
  border-radius: 8px;
  outline: none;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
  cursor: text;
}
.cw-field input::placeholder { color: var(--ink-faint); }
.cw-field input:focus {
  border-color: var(--spore);
  background: rgba(16,200,96,0.04);
  box-shadow: 0 0 0 3px rgba(16,200,96,0.12);
}

.cw-consent {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,0.015);
  cursor: none;
  position: relative;
  transition: border-color 0.2s, background 0.2s;
}
.cw-consent:hover { border-color: var(--line-bright); }
.cw-consent input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.cw-check {
  width: 20px; height: 20px;
  border-radius: 5px;
  border: 1px solid var(--line-bright);
  background: rgba(0,0,0,0.4);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--bg);
  flex-shrink: 0;
  transition: background 0.2s, border-color 0.2s, transform 0.2s;
  margin-top: 1px;
}
.cw-check svg {
  width: 14px; height: 14px;
  stroke-dasharray: 16;
  stroke-dashoffset: 16;
  transition: stroke-dashoffset 0.25s ease 0.05s;
}
.cw-check.is-on {
  background: var(--spore);
  border-color: var(--spore);
  box-shadow: 0 0 0 3px rgba(16,200,96,0.15), 0 0 14px rgba(16,200,96,0.4);
}
.cw-check.is-on svg { stroke-dashoffset: 0; }
.cw-consent-text {
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--ink-dim);
}
.cw-consent-text a {
  color: var(--spore);
  text-decoration: underline;
  text-decoration-color: rgba(16,200,96,0.4);
  text-underline-offset: 2px;
  cursor: none;
}
.cw-consent-text a:hover { text-decoration-color: var(--spore); }

.cw-error {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  color: #d97a6a;
  padding: 8px 12px;
  border-left: 2px solid #d97a6a;
  background: rgba(217,122,106,0.06);
}

.cw-turnstile {
  display: flex;
  justify-content: center;
  min-height: 0;
}
.cw-turnstile:empty { display: none; }
.cw-turnstile iframe { max-width: 100%; }

.cw-submit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 18px;
  background: var(--spore);
  color: var(--bg);
  border: none;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-weight: 600;
  cursor: none;
  transition: background 0.2s, transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 0 30px rgba(16,200,96,0.2);
}
.cw-submit:hover:not(:disabled) {
  background: var(--spore-hot);
  transform: translateY(-1px);
  box-shadow: 0 0 0 6px rgba(16,200,96,0.1), 0 0 50px rgba(16,200,96,0.4);
}
.cw-submit:disabled { opacity: 0.7; }

.cw-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.cw-spore {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--spore);
  box-shadow: 0 0 6px var(--spore-glow);
  display: inline-block;
}

/* ---------- Chat view ---------- */
.cw-chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  position: relative;
  z-index: 1;
}
.cw-msgs {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  /* Stop wheel events from bubbling out to the page when the message
     list hits its top/bottom edge — keeps the host page from
     scrolling past the chat boundaries. */
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.cw-msgs::-webkit-scrollbar { width: 6px; }
.cw-msgs::-webkit-scrollbar-thumb { background: var(--line-bright); border-radius: 3px; }

.cw-msg { display: flex; flex-direction: column; gap: 4px; max-width: 90%; }
.cw-msg-tag {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--spore);
}
.cw-msg-body {
  font-size: 14px;
  line-height: 1.55;
  padding: 10px 14px;
  border-radius: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--ink);
}
.cw-msg-assistant { align-self: flex-start; }
.cw-msg-assistant .cw-msg-body {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.cw-msg-user { align-self: flex-end; }
.cw-msg-user .cw-msg-body {
  background: linear-gradient(180deg, rgba(16,200,96,0.18), rgba(16,200,96,0.10));
  border: 1px solid rgba(16,200,96,0.4);
  border-bottom-right-radius: 4px;
}

.cw-thinking {
  display: inline-flex;
  gap: 4px;
  padding: 14px 16px;
}
.cw-thinking span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--spore);
  opacity: 0.4;
  animation: cwThink 1.2s ease-in-out infinite;
}
.cw-thinking span:nth-child(2) { animation-delay: 0.18s; }
.cw-thinking span:nth-child(3) { animation-delay: 0.36s; }
@keyframes cwThink {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.cw-input-row {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  align-items: flex-end;
  background: rgba(0,0,0,0.25);
}
.cw-input-row textarea {
  flex: 1;
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--line);
  color: var(--ink);
  font-family: var(--font-body, var(--font-display));
  font-size: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  resize: none;
  outline: none;
  max-height: 120px;
  line-height: 1.4;
  transition: border-color 0.2s, box-shadow 0.2s;
  cursor: text;
}
.cw-input-row textarea::placeholder { color: var(--ink-faint); }
.cw-input-row textarea:focus {
  border-color: var(--spore);
  box-shadow: 0 0 0 3px rgba(16,200,96,0.12);
}
.cw-input-row button {
  width: 38px; height: 38px;
  border-radius: 8px;
  background: var(--spore);
  color: var(--bg);
  border: none;
  cursor: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.2s, box-shadow 0.2s, opacity 0.2s;
  flex-shrink: 0;
  box-shadow: 0 0 18px rgba(16,200,96,0.25);
}
.cw-input-row button:hover:not(:disabled) {
  background: var(--spore-hot);
  transform: translateY(-1px);
  box-shadow: 0 0 0 4px rgba(16,200,96,0.12), 0 0 28px rgba(16,200,96,0.45);
}
.cw-input-row button:disabled { opacity: 0.35; box-shadow: none; }

.cw-foot-chat {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: rgba(0,0,0,0.25);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.cw-foot-l { display: inline-flex; align-items: center; gap: 8px; }
.cw-reset {
  background: none;
  border: none;
  color: var(--ink-faint);
  cursor: none;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  padding: 4px 8px;
  border-radius: 4px;
  transition: color 0.2s, background 0.2s;
}
.cw-reset:hover { color: #d97a6a; background: rgba(217,122,106,0.08); }

@media (max-width: 540px) {
  .cw-panel {
    right: 8px; left: 8px;
    bottom: 78px;
    width: auto;
    max-height: calc(100vh - 100px);
    border-radius: 14px;
  }
  .cw-fab-inner { padding: 12px 18px 12px 14px; }
}

@media (hover: none) and (pointer: coarse) {
  .cw-fab, .cw-close, .cw-consent, .cw-submit,
  .cw-input-row button, .cw-reset, .cw-consent-text a { cursor: pointer; }
}
