.cody-chat-root {
  --cody-accent: #5b8f72;
  --cody-gold: #d6b15b;
  --cody-bg: rgba(7, 8, 7, .88);
  --cody-panel: rgba(15, 17, 14, .96);
  --cody-panel-strong: rgba(23, 25, 20, .98);
  --cody-soft: rgba(246, 239, 224, .07);
  --cody-line: rgba(246, 239, 224, .14);
  --cody-line-strong: rgba(214, 177, 91, .28);
  --cody-text: #f6efe0;
  --cody-strong: #fff8ea;
  --cody-muted: #b9ad96;
  --cody-danger: #c96a5b;
  --cody-success: #84c35a;
  --cody-shadow: 0 28px 90px rgba(0, 0, 0, .52);
  position: fixed;
  right: 18px;
  bottom: max(var(--cody-safe-bottom, 18px), env(safe-area-inset-bottom));
  z-index: 9999;
  color: var(--cody-text);
  font-family: "IBM Plex Sans Arabic", "Tajawal", "Cairo", Tahoma, Arial, sans-serif;
}

.cody-chat-root[data-position="bottom-left"] {
  right: auto;
  left: 18px;
}

.cody-chat-root * {
  box-sizing: border-box;
}

.cody-chat-root button,
.cody-chat-root input,
.cody-chat-root textarea,
.cody-chat-root select {
  font: inherit;
}

.cody-chat-bubble {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 64px;
  height: 64px;
  border: 1px solid var(--cody-line-strong);
  border-radius: 999px;
  background:
    radial-gradient(circle at 50% 18%, rgba(214, 177, 91, .2), transparent 55%),
    linear-gradient(180deg, rgba(27, 31, 24, .98), rgba(7, 8, 7, .96));
  color: var(--cody-strong);
  box-shadow: var(--cody-shadow);
  cursor: pointer;
  transition: transform .18s ease, border-color .18s ease, background .18s ease;
}

.cody-chat-bubble:hover,
.cody-chat-bubble:focus-visible {
  border-color: rgba(214, 177, 91, .52);
  transform: translateY(-2px);
  outline: none;
}

.cody-chat-bubble__mark,
.cody-avatar {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--cody-gold), var(--cody-accent));
  color: #080907;
  font-weight: 900;
  letter-spacing: 0;
}

.cody-chat-bubble__label {
  position: absolute;
  overflow: hidden;
  width: 1px;
  height: 1px;
  clip: rect(0 0 0 0);
}

.cody-unread {
  position: absolute;
  inset-block-start: 8px;
  inset-inline-start: 9px;
  display: none;
  min-width: 18px;
  height: 18px;
  place-items: center;
  border-radius: 999px;
  background: var(--cody-danger);
  color: #fff;
  font-size: 11px;
  font-weight: 900;
}

.cody-chat-root.has-unread .cody-unread {
  display: grid;
}

.cody-chat-panel {
  display: none;
  width: min(430px, calc(100vw - 28px));
  max-width: calc(100vw - 28px);
  height: min(72vh, 720px);
  min-height: 520px;
  overflow: hidden;
  border: 1px solid var(--cody-line);
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(246, 239, 224, .055), rgba(246, 239, 224, .025)),
    var(--cody-panel);
  box-shadow: var(--cody-shadow);
  backdrop-filter: blur(20px);
}

.cody-chat-root.is-near-footer .cody-chat-panel {
  height: min(72vh, calc(100vh - var(--cody-safe-bottom, 18px) - 24px));
  min-height: min(420px, calc(100vh - var(--cody-safe-bottom, 18px) - 24px));
}

.cody-chat-root.is-open .cody-chat-panel {
  display: grid;
  grid-template-rows: auto auto auto 1fr auto auto;
}

.cody-chat-root.is-open .cody-chat-bubble {
  display: none;
}

.cody-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--cody-line);
  background: rgba(7, 8, 7, .58);
}

.cody-chat-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.cody-chat-title strong {
  display: block;
  color: var(--cody-strong);
  font-size: 15px;
  line-height: 1.2;
}

.cody-chat-title span:not(.cody-avatar) {
  display: block;
  color: var(--cody-muted);
  font-size: 12px;
  line-height: 1.5;
}

.cody-header-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.cody-icon-btn {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--cody-muted);
  cursor: pointer;
  transition: background .18s ease, color .18s ease, border-color .18s ease;
}

.cody-icon-btn:hover,
.cody-icon-btn:focus-visible {
  border-color: var(--cody-line);
  background: var(--cody-soft);
  color: var(--cody-strong);
  outline: none;
}

.cody-chat-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--cody-line);
  color: var(--cody-muted);
  font-size: 12px;
}

.cody-status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 24px;
  padding: 0 10px;
  border: 1px solid rgba(132, 195, 90, .26);
  border-radius: 999px;
  background: rgba(91, 143, 114, .14);
  color: var(--cody-strong);
  font-weight: 800;
}

.cody-status-badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--cody-success);
}

.cody-status-badge.is-setup,
.cody-status-badge.is-offline {
  border-color: rgba(214, 177, 91, .3);
  background: rgba(214, 177, 91, .1);
}

.cody-status-badge.is-setup::before,
.cody-status-badge.is-offline::before {
  background: var(--cody-gold);
}

.cody-topic-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 12px 14px 8px;
  border-bottom: 1px solid rgba(246, 239, 224, .08);
  scrollbar-width: thin;
}

.cody-topic-tab {
  flex: 0 0 auto;
  min-height: 34px;
  border: 1px solid var(--cody-line);
  border-radius: 999px;
  background: rgba(246, 239, 224, .04);
  color: var(--cody-muted);
  padding: 0 12px;
  cursor: pointer;
  font-weight: 800;
  white-space: nowrap;
}

.cody-topic-tab.is-active,
.cody-topic-tab:hover,
.cody-topic-tab:focus-visible {
  border-color: var(--cody-line-strong);
  background: rgba(214, 177, 91, .12);
  color: var(--cody-strong);
  outline: none;
}

.cody-topic-panel {
  padding: 0 14px 12px;
  border-bottom: 1px solid rgba(246, 239, 224, .08);
}

.cody-topic-description {
  margin: 0 0 10px;
  color: var(--cody-muted);
  font-size: 12px;
  line-height: 1.7;
}

.cody-quick-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.cody-quick-chip,
.cody-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border: 1px solid var(--cody-line);
  border-radius: 10px;
  background: rgba(246, 239, 224, .045);
  color: var(--cody-text);
  padding: 8px 10px;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
  line-height: 1.35;
}

.cody-quick-chip:hover,
.cody-quick-chip:focus-visible,
.cody-action-btn:hover,
.cody-action-btn:focus-visible {
  border-color: var(--cody-line-strong);
  background: rgba(91, 143, 114, .16);
  outline: none;
}

.cody-chat-messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding: 14px;
  scroll-behavior: smooth;
}

.cody-msg {
  display: grid;
  gap: 8px;
  max-width: 92%;
}

.cody-msg--visitor {
  align-self: flex-start;
}

[dir="ltr"] .cody-msg--visitor {
  align-self: flex-end;
}

.cody-msg--ai,
.cody-msg--agent,
.cody-msg--system {
  align-self: flex-end;
}

[dir="ltr"] .cody-msg--ai,
[dir="ltr"] .cody-msg--agent,
[dir="ltr"] .cody-msg--system {
  align-self: flex-start;
}

.cody-msg__bubble {
  border: 1px solid var(--cody-line);
  border-radius: 14px;
  padding: 11px 12px;
  background: rgba(246, 239, 224, .055);
  color: var(--cody-text);
  white-space: pre-wrap;
  line-height: 1.75;
  font-size: 13px;
}

.cody-msg--visitor .cody-msg__bubble {
  border-color: rgba(91, 143, 114, .36);
  background: rgba(91, 143, 114, .16);
}

.cody-msg--ai .cody-msg__bubble,
.cody-msg--agent .cody-msg__bubble {
  border-color: rgba(214, 177, 91, .22);
  background: rgba(214, 177, 91, .08);
}

.cody-source-row,
.cody-action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.cody-source-chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid rgba(214, 177, 91, .22);
  border-radius: 999px;
  padding: 0 9px;
  color: var(--cody-gold);
  background: rgba(214, 177, 91, .08);
  text-decoration: none;
  font-size: 11px;
  font-weight: 800;
}

.cody-action-btn {
  min-height: 34px;
  border-radius: 999px;
  padding: 7px 11px;
}

.cody-typing {
  display: none;
  align-items: center;
  gap: 6px;
  padding: 0 14px 10px;
  color: var(--cody-muted);
  font-size: 12px;
}

.cody-chat-root.is-typing .cody-typing {
  display: flex;
}

.cody-typing i {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--cody-gold);
  animation: codyPulse 1s infinite ease-in-out;
}

.cody-typing i:nth-child(2) {
  animation-delay: .12s;
}

.cody-typing i:nth-child(3) {
  animation-delay: .24s;
}

.cody-composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px 14px 10px;
  border-top: 1px solid var(--cody-line);
  background: rgba(7, 8, 7, .52);
}

.cody-composer textarea {
  min-height: 44px;
  max-height: 110px;
  resize: none;
  border: 1px solid var(--cody-line);
  border-radius: 12px;
  background: rgba(0, 0, 0, .22);
  color: var(--cody-strong);
  padding: 10px 12px;
  outline: none;
}

.cody-composer textarea:focus {
  border-color: var(--cody-line-strong);
  box-shadow: 0 0 0 3px rgba(214, 177, 91, .1);
}

.cody-send {
  min-width: 48px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--cody-gold), var(--cody-accent));
  color: #070807;
  cursor: pointer;
  font-weight: 900;
}

.cody-send:disabled {
  cursor: not-allowed;
  opacity: .55;
}

.cody-footer-note {
  padding: 0 14px 12px;
  color: var(--cody-muted);
  font-size: 11px;
  line-height: 1.55;
  background: rgba(7, 8, 7, .52);
}

.cody-handoff {
  display: none;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--cody-line);
  background: rgba(7, 8, 7, .72);
}

.cody-handoff.is-open {
  display: grid;
}

.cody-handoff p {
  margin: 0;
  color: var(--cody-muted);
  font-size: 12px;
  line-height: 1.7;
}

.cody-handoff input,
.cody-handoff textarea,
.cody-handoff select {
  width: 100%;
  border: 1px solid var(--cody-line);
  border-radius: 10px;
  background: rgba(0, 0, 0, .24);
  color: var(--cody-strong);
  padding: 9px 10px;
  outline: none;
}

.cody-handoff textarea {
  min-height: 80px;
  resize: vertical;
}

.cody-submit-handoff {
  min-height: 40px;
  border: 0;
  border-radius: 999px;
  background: var(--cody-accent);
  color: #061009;
  cursor: pointer;
  font-weight: 900;
}

.cody-feedback {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
  color: var(--cody-muted);
  font-size: 11px;
}

.cody-feedback button {
  border: 1px solid var(--cody-line);
  border-radius: 999px;
  background: transparent;
  color: var(--cody-text);
  padding: 5px 8px;
  cursor: pointer;
}

.cody-empty {
  border: 1px dashed var(--cody-line);
  border-radius: 14px;
  padding: 14px;
  color: var(--cody-muted);
  line-height: 1.7;
  background: rgba(246, 239, 224, .035);
}

@keyframes codyPulse {
  0%, 80%, 100% { opacity: .3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

@media (prefers-reduced-motion: reduce) {
  .cody-chat-root *,
  .cody-chat-root *::before,
  .cody-chat-root *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}

@media (max-width: 720px) {
  .cody-chat-root {
    inset-inline: 0;
    inset-block-end: 0;
    display: grid;
    justify-items: end;
    padding: 12px;
    pointer-events: none;
  }

  .cody-chat-root > * {
    pointer-events: auto;
  }

  .cody-chat-root.is-open {
    padding: 0;
  }

  .cody-chat-root.is-near-footer {
    inset-block-end: 0;
  }

  .cody-chat-panel {
    width: 100vw;
    max-width: 100vw;
    height: min(88vh, 780px);
    min-height: 0;
    border-inline: 0;
    border-end-start-radius: 0;
    border-end-end-radius: 0;
  }

  .cody-chat-root.is-open .cody-chat-panel {
    align-self: end;
  }

  .cody-quick-grid {
    grid-template-columns: 1fr;
  }
}
