:root {
  --font-ui: "Manrope", sans-serif;
  --radius-card: 20px;
  --radius-message: 20px;
  --radius-input: 22px;
  --shadow-soft: 0 12px 32px rgba(15, 23, 42, 0.08);
  --shadow-strong: 0 18px 32px rgba(15, 23, 42, 0.14);
}

:root,
html[data-theme="light"] {
  --page-bg: transparent;
  --surface-card: rgba(255, 255, 255, 0.96);
  --surface-soft: #f8fafc;
  --surface-overlay: rgba(255, 255, 255, 0.72);
  --surface-user: #4f46e5;
  --surface-debug: #101215;
  --text-primary: #2b313c;
  --text-secondary: #6b7280;
  --text-soft: #8a94a6;
  --text-on-dark: #f9fafb;
  --text-on-debug: #d7e2f2;
  --border-soft: rgba(148, 163, 184, 0.16);
  --accent-primary: #4f46e5;
  --accent-primary-soft: rgba(79, 70, 229, 0.12);
  --button-bg: #4f46e5;
  --input-highlight: rgba(255, 255, 255, 0.08);
  --card-shadow: 0 10px 24px rgba(15, 23, 42, 0.06);
  --avatar-bg: rgba(79, 70, 229, 0.10);
  --avatar-color: #4f46e5;
  --status-color: #22c55e;
}

html[data-theme="dark"] {
  --page-bg: transparent;
  --surface-card: rgba(27, 34, 48, 0.96);
  --surface-soft: #20293a;
  --surface-overlay: rgba(13, 17, 23, 0.84);
  --surface-user: #6366f1;
  --surface-debug: #0d1117;
  --text-primary: #f3f4f6;
  --text-secondary: #a7b0bf;
  --text-soft: #7f8ba0;
  --text-on-dark: #eff6ff;
  --text-on-debug: #d9e6ff;
  --border-soft: rgba(148, 163, 184, 0.14);
  --accent-primary: #6366f1;
  --accent-primary-soft: rgba(99, 102, 241, 0.18);
  --button-bg: #6366f1;
  --input-highlight: rgba(255, 255, 255, 0.02);
  --card-shadow: 0 16px 30px rgba(0, 0, 0, 0.22);
  --shadow-soft: 0 18px 36px rgba(0, 0, 0, 0.22);
  --shadow-strong: 0 20px 40px rgba(0, 0, 0, 0.26);
  --avatar-bg: rgba(99, 102, 241, 0.16);
  --avatar-color: #818cf8;
  --status-color: #4ade80;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
  overflow-x: hidden;
  overflow-y: hidden;
  font-family: var(--font-ui);
  color: var(--text-primary);
  background: var(--page-bg);
}

html[data-theme="dark"] {
  color-scheme: dark;
}

html[data-theme="light"] {
  color-scheme: light;
}

body {
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
}

.widget-root {
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.chat-widget {
  position: relative;
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  max-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  gap: 14px;
  padding: 18px;
  background: transparent;
  overflow: hidden;
}

.chat-widget > * {
  min-width: 0;
}

.messages {
  width: 100%;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 4px 4px 4px 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.messages::before {
  content: "";
  margin-top: auto;
}

.message {
  width: auto;
  max-width: min(100%, 760px);
  min-width: 0;
  padding: 16px 18px;
  border-radius: var(--radius-message);
  line-height: 1.65;
  box-shadow: inset 0 0 0 1px var(--border-soft);
  overflow-wrap: anywhere;
  animation: slide-up 220ms ease;
}

.message p {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user {
  align-self: flex-end;
  background: var(--surface-user);
  color: var(--text-on-dark);
  box-shadow: var(--shadow-strong);
}

.message.assistant {
  align-self: flex-start;
  width: min(100%, 760px);
  background: var(--surface-card);
  color: var(--text-primary);
  box-shadow: var(--shadow-soft);
}

.message.pending {
  color: var(--text-secondary);
  background: var(--surface-soft);
}

.composer-shell {
  width: 100%;
  background: transparent;
}

.composer {
  width: 100%;
  min-width: 0;
  display: block;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.composer.locked {
  opacity: 0.78;
}

.composer-field {
  position: relative;
  width: 100%;
}

.composer textarea {
  width: 100%;
  min-width: 0;
  min-height: 58px;
  max-height: 180px;
  resize: none;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-input);
  outline: none;
  background: var(--surface-card);
  box-shadow:
    inset 0 1px 0 var(--input-highlight),
    var(--card-shadow);
  color: var(--text-primary);
  font: inherit;
  line-height: 1.5;
  padding: 16px 68px 16px 18px;
}

.composer textarea::placeholder {
  color: var(--text-soft);
}

.composer button {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 42px;
  height: 42px;
  min-width: 42px;
  min-height: 42px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--button-bg);
  color: var(--text-on-dark);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  transition:
    transform 160ms ease,
    opacity 160ms ease,
    box-shadow 160ms ease,
    background-color 160ms ease;
  box-shadow: var(--shadow-strong);
}

.composer button:hover {
  transform: translateY(-1px);
}

.composer button:disabled {
  opacity: 0.72;
  cursor: not-allowed;
}

.auth-gate {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: color-mix(in srgb, var(--surface-card) 84%, transparent);
  backdrop-filter: blur(10px);
}

.auth-gate-card {
  width: min(100%, 360px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 22px 20px;
  border-radius: 22px;
  border: 1px solid var(--border-soft);
  background: var(--surface-card);
  box-shadow: var(--shadow-soft);
  text-align: center;
}

.auth-gate-spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--accent-primary) 22%, transparent);
  border-top-color: var(--accent-primary);
  animation: auth-spin 900ms linear infinite;
}

.auth-gate strong {
  color: var(--text-primary);
  font-size: 0.96rem;
  font-weight: 700;
}

.auth-gate p {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.84rem;
  line-height: 1.5;
}

.auth-gate[data-state="error"] .auth-gate-spinner {
  display: none;
}

.auth-gate[data-state="error"] .auth-gate-card {
  border-color: color-mix(in srgb, #ef4444 22%, var(--border-soft));
}

.auth-gate[data-state="error"] strong {
  color: #ef4444;
}

.debug-panel {
  width: 100%;
  min-width: 0;
  align-self: end;
  border: 1px solid var(--border-soft);
  border-radius: 18px;
  background: var(--surface-overlay);
  overflow: hidden;
}

.debug-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  color: var(--text-secondary);
  font-weight: 700;
  list-style: none;
}

.debug-panel summary::-webkit-details-marker {
  display: none;
}

.debug-summary-text {
  max-width: min(48ch, 100%);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-soft);
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.debug-panel pre {
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 16px;
  overflow: auto;
  max-height: 220px;
  background: var(--surface-debug);
  color: var(--text-on-debug);
  font-size: 0.84rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.widget-meta {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.widget-meta:empty {
  display: none;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--border-soft);
  background: var(--surface-overlay);
  color: var(--text-secondary);
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.meta-pill[hidden] {
  display: none;
}

.meta-pill.subtle {
  color: var(--text-soft);
}

button.meta-pill {
  font-family: inherit;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}

button.meta-pill:hover,
button.meta-pill:focus-visible {
  color: var(--text-primary);
  border-color: var(--accent-primary);
  background: var(--accent-primary-soft);
}

button.meta-pill:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

.debug-test-button::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-primary);
  margin-right: 6px;
}

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

@keyframes slide-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes auth-spin {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 900px) {
  .message {
    max-width: 100%;
  }
}

@media (max-width: 640px) {
  .chat-widget {
    gap: 12px;
    padding: 12px;
  }

  .composer button {
    right: 10px;
    bottom: 10px;
    width: 40px;
    height: 40px;
  }

  .widget-meta {
    justify-content: flex-start;
  }
}

@media (max-width: 400px) {
  .message-row .avatar {
    width: 26px;
    height: 26px;
  }

  .message-row .avatar svg {
    width: 12px;
    height: 12px;
  }

  .chat-header-avatar {
    width: 30px;
    height: 30px;
  }

  .chat-header-avatar svg {
    width: 14px;
    height: 14px;
  }
}

/* ── Chat Header ── */
.chat-header {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0 2px;
  background: var(--page-bg);
}

.chat-header-avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--avatar-bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-header-avatar svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--avatar-color);
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chat-header-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.chat-header-name {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1;
}

.chat-header-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--status-color);
  line-height: 1;
}

.chat-header-status::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--status-color);
}

/* ── Message Row (wrapper com avatar) ── */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: slide-up 220ms ease;
}

.message-row .message {
  animation: none;
}

.message-row.user {
  justify-content: flex-end;
}

.message-row.assistant {
  justify-content: flex-start;
}

.message-row .avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--avatar-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 4px;
}

.message-row .avatar svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: var(--avatar-color);
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ── Typing dots ── */
.typing-dots {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 4px;
}

.typing-dots span {
  display: block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-soft);
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30%            { transform: translateY(-6px); opacity: 1; }
}
