/* Agriconexión — guided diagnosis window.
   All colour and type come from the tokens in styles.css, so both themes just work. */

/* ---------- Launcher, stacked above the floating WhatsApp button ---------- */

.chat-launcher {
  position: fixed;
  right: var(--gutter);
  bottom: calc(var(--gutter) + 56px + 12px);
  z-index: 120;
  /* Ink on paper rather than the primary green, which vanishes over the CTA section.
     The ring separates the pill from that green and is invisible on the page itself. */
  background: var(--ink);
  color: var(--bg);
  box-shadow: 0 0 0 1px var(--bg), var(--shadow);
  padding: 8px 20px 8px 8px;
  text-align: left;
  transition: opacity 150ms ease-out, scale 150ms ease-out, background-color 150ms ease-out, visibility 0s;
}

.chat-launcher__icon {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--bg) 16%, transparent);
}

.chat-launcher__body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-launcher__status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  color: color-mix(in srgb, var(--bg) 72%, var(--ink));
}

.chat-launcher__status::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--brand);
}

.chat-launcher__question {
  font-size: 0.9375rem;
}

.chat-launcher:not(.is-visible) {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition-delay: 0s, 0s, 0s, 150ms;
}

html.chat-open .chat-launcher,
html.chat-open .float-wa {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* ---------- Window ---------- */

.chat {
  position: fixed;
  right: var(--gutter);
  bottom: calc(var(--gutter) + env(safe-area-inset-bottom, 0px) + var(--chat-keyboard, 0px));
  z-index: 150;
  display: flex;
  flex-direction: column;
  width: 360px;
  max-height: min(520px, 70vh, calc(var(--chat-viewport, 100vh) - 24px));
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  opacity: 0;
  visibility: hidden;
  transition: opacity 150ms ease-out, visibility 0s linear 150ms;
}

.chat.is-open {
  opacity: 1;
  visibility: visible;
  transition: opacity 150ms ease-out, visibility 0s;
}

.chat:focus {
  outline: none;
}

@media (prefers-reduced-motion: no-preference) {
  .chat {
    translate: 0 12px;
    transition: opacity 150ms ease-out, translate 150ms ease-out, visibility 0s linear 150ms;
  }

  .chat.is-open {
    translate: 0 0;
    transition: opacity 300ms ease-out, translate 300ms ease-out, visibility 0s;
  }
}

/* Phone: still a floating window, with the page visible around it. */
@media (max-width: 480px) {
  .chat {
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px) + var(--chat-keyboard, 0px));
    width: auto;
    max-height: min(70vh, calc(var(--chat-viewport, 100vh) - 24px));
    max-height: min(70dvh, calc(var(--chat-viewport, 100dvh) - 24px));
  }
}

.chat__head {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 6px 4px 14px;
}

.chat__title {
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.chat__close {
  width: 36px;
  height: 36px;
  border-color: transparent;
}

.chat__progress {
  flex: none;
  height: 2px;
  background: var(--rule);
}

.chat__progress-bar {
  display: block;
  width: 0;
  height: 100%;
  background: var(--field);
}

[data-theme="dark"] .chat__progress-bar {
  background: var(--brand);
}

@media (prefers-reduced-motion: no-preference) {
  .chat__progress-bar {
    transition: width 300ms ease-out;
  }
}

/* ---------- Conversation ---------- */

.chat__log {
  display: flex;
  flex: 0 1 auto;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
  font-size: 0.9375rem;
  line-height: 1.45;
}

.chat__log:focus-visible {
  outline-offset: -2px;
}

.chat__log > * {
  flex: none;
}

.chat__text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat__msg--user .chat__text {
  padding-left: 10px;
  border-left: 2px solid var(--field);
  color: var(--muted);
}

[data-theme="dark"] .chat__msg--user .chat__text {
  border-left-color: var(--brand);
}

.chat__privacy {
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--muted);
}

.chat__notice {
  padding-left: 10px;
  border-left: 2px solid var(--ochre);
}

.chat__typing {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 20px;
}

.chat__typing i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--muted);
}

@media (prefers-reduced-motion: no-preference) {
  .chat__typing i {
    animation: chat-typing 1s ease-in-out infinite;
  }

  .chat__typing i:nth-of-type(2) { animation-delay: 150ms; }
  .chat__typing i:nth-of-type(3) { animation-delay: 300ms; }
}

@keyframes chat-typing {
  50% { opacity: 0.3; }
}

/* ---------- Chips ---------- */

.chat__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chat__chip {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid var(--rule-strong);
  border-radius: 18px;
  background: transparent;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.25;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition-property: scale, background-color, border-color;
  transition-duration: 150ms;
  transition-timing-function: ease-out;
}

@media (prefers-reduced-motion: no-preference) {
  .chat__chip:active {
    scale: 0.96;
  }
}

.chat__chip--quiet {
  border-color: transparent;
  color: var(--muted);
  font-weight: 400;
  text-decoration: underline;
  text-decoration-color: var(--rule-strong);
}

/* ---------- Input ---------- */

.chat__composer {
  display: flex;
  flex: none;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px 4px;
  border-top: 1px solid var(--rule);
}

.chat__input {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  max-height: 86px; /* 3 lines */
  padding: 8px 12px;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px; /* below this, iOS zooms the page on focus */
  line-height: 1.4;
  resize: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.chat__input::placeholder {
  color: var(--muted);
}

.chat__input:focus-visible {
  outline-offset: 0;
}

.chat__input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chat__send {
  flex: none;
  width: 40px;
  padding: 0;
}

.chat__send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.chat__links {
  display: flex;
  flex: none;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px 4px;
}

.chat__links a {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  font-size: 0.8125rem;
  color: var(--muted);
}

@media (hover: hover) {
  .chat-launcher:hover { background: color-mix(in srgb, var(--ink) 85%, var(--bg)); }
  .chat__chip:hover { border-color: var(--ink); background: var(--rule); }
  .chat__chip--quiet:hover { border-color: transparent; color: var(--ink); background: transparent; }
  .chat__links a:hover { color: var(--ink); }
}
