/* ─── Chat Widget — TK Rally ──────────────────────────────────────
   Floating chat button + panel.
   Relies on CSS custom properties defined in styles.css.
   Fallback values are inlined for resilience if loaded standalone.
   ──────────────────────────────────────────────────────────────── */

/* ─── Floating action button ───────────────────────────────────── */

.chat-fab {
  align-items: center;
  background: var(--clay, #c25512);
  border: 0;
  border-radius: 50%;
  bottom: 28px;
  box-shadow: 0 6px 24px rgba(194, 85, 18, 0.45);
  color: #fff;
  cursor: pointer;
  display: flex;
  height: 60px;
  justify-content: center;
  position: fixed;
  right: 24px;
  transition: background 180ms ease, transform 180ms ease, box-shadow 180ms ease;
  width: 60px;
  z-index: 200;
}

.chat-fab:hover {
  background: var(--clay-dark, #bf5f00);
  box-shadow: 0 8px 32px rgba(194, 85, 18, 0.58);
  transform: translateY(-2px);
}

.chat-fab:active {
  transform: translateY(0);
}

.chat-fab:focus-visible {
  outline: 3px solid var(--lime, #7ac142);
  outline-offset: 4px;
}

.chat-fab svg {
  display: block;
  flex-shrink: 0;
}

/* Hide FAB on small screens while panel is open (users use the ✕ in header) */
@media (max-width: 480px) {
  body.chat-is-open .chat-fab {
    display: none;
  }
}

/* ─── Panel ─────────────────────────────────────────────────────── */

.chat-panel {
  background: var(--paper, #f4f1ea);
  border: 1px solid rgba(38, 37, 37, 0.14);
  border-radius: 12px;
  bottom: 100px;
  box-shadow: 0 24px 72px rgba(38, 37, 37, 0.22);
  display: flex;
  flex-direction: column;
  max-height: 560px;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  position: fixed;
  right: 24px;
  transform: translateY(14px);
  transition: opacity 200ms ease, transform 200ms ease;
  width: 360px;
  z-index: 199;
}

.chat-panel.chat-open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* ─── Header ─────────────────────────────────────────────────────── */

.chat-header {
  align-items: center;
  background: var(--clay, #c25512);
  color: #fff;
  display: flex;
  flex-shrink: 0;
  gap: 10px;
  justify-content: space-between;
  padding: 13px 16px;
}

.chat-header-title {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.chat-header-name {
  font-family: var(--font-display, Georgia, "Times New Roman", serif);
  font-size: 0.98rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.chat-header-sub {
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  opacity: 0.78;
  text-transform: uppercase;
}

.chat-close {
  align-items: center;
  background: rgba(255, 255, 255, 0.16);
  border: 0;
  border-radius: 50%;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-shrink: 0;
  font-size: 1rem;
  height: 30px;
  justify-content: center;
  line-height: 1;
  padding: 0;
  transition: background 160ms ease;
  width: 30px;
}

.chat-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

.chat-close:focus-visible {
  outline: 3px solid var(--lime, #7ac142);
  outline-offset: 2px;
}

/* ─── Messages list ──────────────────────────────────────────────── */

.chat-messages {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding: 16px 14px 8px;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
  width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(38, 37, 37, 0.16);
  border-radius: 2px;
}

/* ─── Bubbles ────────────────────────────────────────────────────── */

.chat-bubble {
  border-radius: 14px;
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-size: 0.88rem;
  line-height: 1.55;
  max-width: 88%;
  padding: 9px 13px;
  word-break: break-word;
}

.chat-bubble.user {
  align-self: flex-end;
  background: var(--clay, #c25512);
  border-bottom-right-radius: 3px;
  color: #fff;
}

.chat-bubble.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid rgba(38, 37, 37, 0.11);
  border-bottom-left-radius: 3px;
  color: var(--ink, #262525);
}

.chat-bubble.error {
  align-self: flex-start;
  background: rgba(194, 85, 18, 0.08);
  border: 1px solid rgba(194, 85, 18, 0.28);
  border-bottom-left-radius: 3px;
  color: var(--clay-dark, #bf5f00);
  font-weight: 700;
}

/* ─── Typing indicator ───────────────────────────────────────────── */

.chat-typing {
  align-items: center;
  align-self: flex-start;
  background: #fff;
  border: 1px solid rgba(38, 37, 37, 0.11);
  border-radius: 14px;
  border-bottom-left-radius: 3px;
  display: flex;
  gap: 5px;
  padding: 11px 14px;
}

.chat-typing span {
  animation: chat-dot 1.2s infinite ease-in-out;
  background: var(--muted, #616b66);
  border-radius: 50%;
  display: block;
  height: 7px;
  width: 7px;
  opacity: 0.4;
}

.chat-typing span:nth-child(2) {
  animation-delay: 0.18s;
}

.chat-typing span:nth-child(3) {
  animation-delay: 0.36s;
}

@keyframes chat-dot {
  0%, 60%, 100% {
    opacity: 0.4;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-5px);
  }
}

/* ─── Suggestion chips ───────────────────────────────────────────── */

.chat-chips {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 7px;
  padding: 6px 14px 4px;
}

.chat-chips:empty {
  display: none;
}

.chat-chip {
  background: rgba(194, 85, 18, 0.09);
  border: 1px solid rgba(194, 85, 18, 0.3);
  border-radius: 999px;
  color: var(--clay, #c25512);
  cursor: pointer;
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-size: 0.77rem;
  font-weight: 700;
  line-height: 1.3;
  padding: 5px 11px;
  text-align: left;
  transition: background 160ms ease, border-color 160ms ease;
}

.chat-chip:hover {
  background: rgba(194, 85, 18, 0.18);
  border-color: var(--clay, #c25512);
}

.chat-chip:focus-visible {
  outline: 3px solid var(--lime, #7ac142);
  outline-offset: 2px;
}

/* ─── CTA buttons ────────────────────────────────────────────────── */

.chat-cta-row {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 14px 8px;
}

.chat-cta-row:empty {
  display: none;
}

.chat-cta {
  align-items: center;
  background: var(--grass, #163d20);
  border: 0;
  border-radius: 999px;
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-size: 0.8rem;
  font-weight: 900;
  padding: 7px 15px;
  text-decoration: none;
  transition: background 160ms ease, transform 160ms ease;
}

.chat-cta:hover {
  background: var(--clay, #c25512);
  transform: translateY(-1px);
}

.chat-cta:focus-visible {
  outline: 3px solid var(--lime, #7ac142);
  outline-offset: 2px;
}

/* ─── Input row ──────────────────────────────────────────────────── */

.chat-input-row {
  align-items: flex-end;
  background: var(--paper, #f4f1ea);
  border-top: 1px solid rgba(38, 37, 37, 0.12);
  display: flex;
  flex-shrink: 0;
  gap: 8px;
  padding: 10px 12px;
}

.chat-input {
  background: #fff;
  border: 1px solid rgba(38, 37, 37, 0.18);
  border-radius: 8px;
  color: var(--ink, #262525);
  flex: 1 1 auto;
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-size: 0.88rem;
  line-height: 1.45;
  max-height: 110px;
  min-height: 40px;
  overflow-y: auto;
  padding: 9px 12px;
  resize: none;
  transition: border-color 160ms ease, outline 160ms ease;
}

.chat-input:focus {
  border-color: var(--grass, #163d20);
  outline: 3px solid rgba(22, 61, 32, 0.14);
}

.chat-input:focus-visible {
  outline-color: var(--lime, #7ac142);
}

.chat-send {
  align-items: center;
  background: var(--clay, #c25512);
  border: 0;
  border-radius: 8px;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-shrink: 0;
  height: 40px;
  justify-content: center;
  padding: 0;
  transition: background 160ms ease, transform 160ms ease;
  width: 40px;
}

.chat-send:hover {
  background: var(--clay-dark, #bf5f00);
  transform: translateY(-1px);
}

.chat-send:active {
  transform: none;
}

.chat-send:disabled {
  cursor: not-allowed;
  opacity: 0.42;
  transform: none;
}

.chat-send:focus-visible {
  outline: 3px solid var(--lime, #7ac142);
  outline-offset: 2px;
}

.chat-send svg {
  display: block;
}

/* ─── Responsive — full-width bottom sheet on small screens ──────── */

@media (max-width: 480px) {
  .chat-panel {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    bottom: 0;
    left: 0;
    max-height: 80svh;
    right: 0;
    width: 100%;
  }

  .chat-fab {
    bottom: 20px;
    right: 16px;
  }
}

/* Respect reduced-motion */
@media (prefers-reduced-motion: reduce) {
  .chat-fab,
  .chat-panel,
  .chat-chip,
  .chat-cta,
  .chat-send,
  .chat-close,
  .chat-input {
    transition: none !important;
  }

  .chat-panel {
    transform: none !important;
  }

  .chat-typing span {
    animation: none;
    opacity: 1;
  }
}

/* ─── FAB wrapper + always-visible label ──────────────────────────── */
.chat-fab-wrap {
  position: fixed;
  right: 24px;
  bottom: 53px;
  z-index: 200;
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-fab-wrap .chat-fab {
  position: static;
  inset: auto;
}
.chat-fab-label {
  background: #fff;
  color: var(--ink, #262525);
  font-family: var(--font-body, "Trebuchet MS", Verdana, sans-serif);
  font-weight: 700;
  font-size: 0.9rem;
  line-height: 1;
  padding: 10px 15px;
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(38, 37, 37, 0.18);
  white-space: nowrap;
  cursor: pointer;
  border: 1px solid rgba(38, 37, 37, 0.06);
}
body.chat-is-open .chat-fab-label { display: none; }
@media (max-width: 520px) {
  .chat-fab-wrap { right: 16px; bottom: 45px; }
}
@media (max-width: 400px) {
  .chat-fab-label { display: none; }
}
