/* Rakentaja-avustaja — komponenttityylit.
 *
 * BEM-nimiavaruus ra-chat__*. Kaikki tokenit ra-chat-tokens.css:stä.
 * Dokumentoi tarkoituksellisia design-valintoja Fable 5:n auditin pohjalta.
 */

/* ============================================================================
 * Root — piiloitetaan noscript-fallback JS:n kanssa
 * ============================================================================ */

#ra-chat-root { font-family: var(--ra-font-family); }
#ra-chat-root .ra-chat__noscript {
  position: fixed; right: var(--ra-space-5); bottom: var(--ra-space-5);
  background: var(--ra-color-brand-900); color: var(--ra-color-text-on-brand);
  padding: var(--ra-space-3) var(--ra-space-4); border-radius: var(--ra-radius-full);
  text-decoration: none; font-size: var(--ra-font-size-sm); box-shadow: var(--ra-shadow-fab);
  z-index: var(--ra-z-fab);
}

/* ============================================================================
 * FAB (bottom-right)
 * ============================================================================ */

.ra-chat__fab {
  position: fixed;
  right: var(--ra-space-5);
  bottom: var(--ra-space-5);
  width: var(--ra-fab-size);
  height: var(--ra-fab-size);
  border-radius: var(--ra-radius-full);
  background: var(--ra-color-brand-900);
  color: var(--ra-color-text-on-brand);
  border: none;
  box-shadow: var(--ra-shadow-fab);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: var(--ra-z-fab);
  transition: transform var(--ra-transition-fast), box-shadow var(--ra-transition-fast);
}
.ra-chat__fab:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(26, 39, 68, 0.35); }
.ra-chat__fab:focus-visible { outline: 3px solid var(--ra-color-focus-ring); outline-offset: 2px; }
.ra-chat__fab-icon { width: 26px; height: 26px; }
.ra-chat__fab-badge {
  position: absolute; top: -4px; right: -4px;
  background: var(--ra-color-accent-500); color: #fff;
  font-size: var(--ra-font-size-xs); font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: var(--ra-radius-full);
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--ra-color-surface-0);
}
.ra-chat__fab[hidden] { display: none; }

@media (max-width: 640px) {
  .ra-chat__fab { right: var(--ra-space-4); bottom: calc(var(--ra-space-4) + env(safe-area-inset-bottom)); }
}

/* ============================================================================
 * Tervehdyskupla
 * ============================================================================ */

.ra-chat__greeting {
  position: fixed;
  right: calc(var(--ra-space-5) + var(--ra-fab-size) + var(--ra-space-3));
  bottom: calc(var(--ra-space-5) + 8px);
  max-width: 260px;
  padding: var(--ra-space-3) var(--ra-space-4);
  background: var(--ra-color-surface-0);
  border: 1px solid var(--ra-color-border);
  border-radius: var(--ra-radius-md);
  box-shadow: var(--ra-shadow-bubble);
  font-size: var(--ra-font-size-sm);
  color: var(--ra-color-text-primary);
  z-index: var(--ra-z-fab);
  animation: ra-greeting-in var(--ra-transition-base);
}
@keyframes ra-greeting-in {
  from { opacity: 0; transform: translateY(6px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.ra-chat__greeting-text { margin: 0; padding-right: 20px; }
.ra-chat__greeting-close {
  position: absolute; top: 2px; right: 4px; border: none; background: none;
  color: var(--ra-color-text-secondary); font-size: 18px; cursor: pointer; padding: 4px 8px;
  line-height: 1;
}
.ra-chat__greeting-close:hover { color: var(--ra-color-text-primary); }
.ra-chat__greeting[hidden] { display: none; }

@media (max-width: 640px) {
  .ra-chat__greeting { right: var(--ra-space-4); left: var(--ra-space-4); max-width: none; bottom: calc(var(--ra-space-4) + var(--ra-fab-size) + var(--ra-space-3) + env(safe-area-inset-bottom)); }
}

/* ============================================================================
 * Paneeli — runko
 * ============================================================================ */

.ra-chat__panel {
  position: fixed;
  right: var(--ra-space-5);
  bottom: calc(var(--ra-space-5) + var(--ra-fab-size) + var(--ra-space-3));
  width: var(--ra-panel-width);
  height: var(--ra-panel-height);
  max-height: 80vh;
  background: var(--ra-color-surface-0);
  border-radius: var(--ra-radius-lg);
  box-shadow: var(--ra-shadow-panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: var(--ra-z-panel);
  transform-origin: bottom right;
  animation: ra-panel-in var(--ra-transition-base);
}
@keyframes ra-panel-in {
  from { opacity: 0; transform: scale(0.92) translateY(12px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}
.ra-chat__panel[hidden] { display: none; }
/* Restore-tila: skipataan avautumisanimaatio jotta paneeli ilmestyy heti valmiina */
.ra-chat__panel--no-anim { animation: none !important; }

.ra-chat__header {
  display: flex; justify-content: space-between; align-items: center;
  padding: var(--ra-space-3) var(--ra-space-4);
  background: var(--ra-color-brand-900); color: var(--ra-color-text-on-brand);
  flex-shrink: 0;
}
.ra-chat__header-info { display: flex; align-items: center; gap: var(--ra-space-2); }
.ra-chat__header-avatar {
  width: 32px; height: 32px; border-radius: var(--ra-radius-full);
  background: var(--ra-color-accent-500); display: flex; align-items: center; justify-content: center;
  font-size: 0.7rem; font-weight: 700; flex-shrink: 0; color: #fff;
}
.ra-chat__header-title { font-size: var(--ra-font-size-lg); font-weight: 600; margin: 0; line-height: 1.2; }
.ra-chat__header-status { font-size: var(--ra-font-size-xs); opacity: 0.8; display: block; }
.ra-chat__header-actions { display: flex; gap: var(--ra-space-1); }

.ra-chat__icon-btn {
  background: none; border: none; color: var(--ra-color-text-on-brand); opacity: 0.85;
  width: 36px; height: 36px; border-radius: var(--ra-radius-sm); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
}
.ra-chat__icon-btn:hover { background: rgba(255, 255, 255, 0.12); opacity: 1; }
.ra-chat__icon-btn:focus-visible { outline: 2px solid #fff; outline-offset: -2px; }

/* ============================================================================
 * Konversaatioalue
 * ============================================================================ */

.ra-chat__conversation {
  flex: 1;
  overflow-y: auto;
  padding: var(--ra-space-4);
  display: flex;
  flex-direction: column;
  gap: var(--ra-space-3);
  background: var(--ra-color-surface-1);
  overflow-anchor: auto;
  scroll-behavior: smooth;
}

/* Viestit — user + assistant */
.ra-chat__msg { display: flex; flex-direction: column; max-width: 85%; gap: 2px; }
.ra-chat__msg--assistant { align-self: flex-start; align-items: flex-start; }
.ra-chat__msg--user { align-self: flex-end; align-items: flex-end; }

.ra-chat__msg-bubble {
  padding: var(--ra-space-2) var(--ra-space-3);
  border-radius: var(--ra-radius-md);
  font-size: var(--ra-font-size-base);
  line-height: var(--ra-line-height-base);
  box-shadow: var(--ra-shadow-bubble);
  white-space: pre-wrap;
  word-break: break-word;
}
.ra-chat__msg--assistant .ra-chat__msg-bubble {
  background: var(--ra-color-surface-sunken); color: var(--ra-color-text-primary);
  border-bottom-left-radius: var(--ra-radius-sm);
}
.ra-chat__msg--user .ra-chat__msg-bubble {
  background: var(--ra-color-brand-900); color: var(--ra-color-text-on-brand);
  border-bottom-right-radius: var(--ra-radius-sm);
}
.ra-chat__msg-time { font-size: var(--ra-font-size-xs); color: var(--ra-color-text-secondary); padding: 0 4px; }

/* Streaming cursor — vain kun data-streaming="true" */
.ra-chat__msg-bubble[data-streaming="true"]::after {
  content: '';
  display: inline-block;
  width: 2px; height: 1em;
  background: var(--ra-color-brand-700);
  margin-left: 2px;
  vertical-align: -0.15em;
  animation: ra-cursor-blink 1s step-start infinite;
}
@keyframes ra-cursor-blink { 50% { opacity: 0; } }

/* Sähköturvallisuus-KORJAUS-lohko — amber, ei red */
.ra-chat__msg--correction { max-width: 95%; }
.ra-chat__msg--correction .ra-chat__msg-bubble {
  background: var(--ra-color-disclaimer-bg);
  border: 1px solid var(--ra-color-disclaimer-border);
  color: var(--ra-color-disclaimer-text);
  border-radius: var(--ra-radius-md);
  display: flex; align-items: flex-start; gap: var(--ra-space-2);
}
.ra-chat__msg--correction .ra-chat__msg-bubble::before {
  content: '⚠';
  color: var(--ra-color-disclaimer-icon);
  font-size: 1.1em; line-height: 1; flex-shrink: 0;
}

/* Typing-indikaattori */
.ra-chat__typing { display: flex; gap: 4px; padding: var(--ra-space-2) var(--ra-space-3); align-self: flex-start; align-items: center; }
.ra-chat__typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--ra-color-text-secondary);
  animation: ra-typing-bounce 1.2s ease-in-out infinite;
}
.ra-chat__typing span:nth-child(2) { animation-delay: 0.15s; }
.ra-chat__typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes ra-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Tool-status */
.ra-chat__tool-status {
  display: flex; align-items: center; gap: 6px; align-self: flex-start;
  font-size: var(--ra-font-size-sm); color: var(--ra-color-text-secondary);
  padding: var(--ra-space-1) var(--ra-space-2);
}
.ra-chat__tool-status-icon { animation: ra-spin 1.4s linear infinite; width: 14px; height: 14px; }
@keyframes ra-spin { to { transform: rotate(360deg); } }

/* ============================================================================
 * Tuotekortit chatissa (horizontal scroll snap)
 * ============================================================================ */

.ra-chat__products {
  display: flex; gap: var(--ra-space-2);
  overflow-x: auto; padding: 4px 0;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  align-self: stretch;
  min-height: 240px;
  flex-shrink: 0;
}
.ra-chat__product-card {
  flex: 0 0 168px; scroll-snap-align: start;
  background: var(--ra-color-surface-0); border: 1px solid var(--ra-color-border);
  border-radius: var(--ra-radius-md); padding: var(--ra-space-2);
  display: flex; flex-direction: column; gap: 6px;
  min-height: 220px;
  box-shadow: var(--ra-shadow-bubble);
}
.ra-chat__product-img {
  width: 100%; height: 96px; object-fit: contain; background: #fff;
  border-radius: 6px;
  flex-shrink: 0;
}
.ra-chat__product-info {
  display: flex; flex-direction: column; gap: 2px;
  min-height: 60px;
}
.ra-chat__product-brand {
  font-size: 0.68rem; color: var(--ra-color-text-secondary);
  text-transform: uppercase; letter-spacing: 0.3px;
}
.ra-chat__product-name {
  font-size: 0.8rem; font-weight: 600; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
.ra-chat__product-price {
  font-size: 0.9rem; font-weight: 700; color: var(--ra-color-accent-600);
}
.ra-chat__product-actions { display: flex; flex-direction: column; gap: 4px; margin-top: 2px; }
.ra-chat__product-link {
  font-size: 0.75rem; color: var(--ra-color-brand-700);
  text-align: center; text-decoration: none; padding: 4px;
}
.ra-chat__product-link:hover { text-decoration: underline; }
.ra-chat__product-add-btn {
  background: var(--ra-color-accent-600); color: #fff;
  border: none; border-radius: var(--ra-radius-sm);
  padding: 8px; font-size: 0.8rem; font-weight: 600; cursor: pointer;
  min-height: 36px;
}
.ra-chat__product-add-btn:disabled { background: var(--ra-color-border); color: var(--ra-color-text-secondary); cursor: not-allowed; }
.ra-chat__product-add-btn:focus-visible { outline: 2px solid var(--ra-color-focus-ring); outline-offset: 2px; }
.ra-chat__product-add-btn[data-state="added"] { background: var(--ra-color-success); }

/* ============================================================================
 * Disclaimer footer + composer
 * ============================================================================ */

.ra-chat__disclaimer-footer {
  padding: var(--ra-space-2) var(--ra-space-4);
  font-size: 0.6875rem; line-height: 1.4;
  color: var(--ra-color-text-secondary);
  background: var(--ra-color-surface-0);
  border-top: 1px solid var(--ra-color-border);
  flex-shrink: 0;
}

.ra-chat__composer {
  display: flex; align-items: flex-end; gap: var(--ra-space-2);
  padding: var(--ra-space-3); border-top: 1px solid var(--ra-color-border);
  background: var(--ra-color-surface-0); flex-shrink: 0;
}
.ra-chat__composer-input {
  flex: 1; resize: none; max-height: 100px; min-height: 40px;
  border: 1px solid var(--ra-color-border); border-radius: var(--ra-radius-md);
  padding: var(--ra-space-2) var(--ra-space-3);
  font-family: var(--ra-font-family);
  font-size: var(--ra-font-size-base);
  color: var(--ra-color-text-primary);
  background: var(--ra-color-surface-0);
}
.ra-chat__composer-input:focus {
  outline: none;
  border-color: var(--ra-color-brand-700);
  box-shadow: 0 0 0 3px rgba(44, 62, 107, 0.15);
}
.ra-chat__composer-input::placeholder { color: var(--ra-color-text-secondary); }

.ra-chat__send-btn {
  width: 40px; height: 40px; border-radius: var(--ra-radius-full); border: none;
  background: var(--ra-color-brand-900); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: background var(--ra-transition-fast);
}
.ra-chat__send-btn:disabled { background: var(--ra-color-border); cursor: not-allowed; }
.ra-chat__send-btn:hover:not(:disabled) { background: var(--ra-color-brand-700); }
.ra-chat__send-btn:focus-visible { outline: 3px solid var(--ra-color-focus-ring); outline-offset: 2px; }

/* ============================================================================
 * Virhebanneri
 * ============================================================================ */

.ra-chat__error-banner {
  display: flex; align-items: center; gap: var(--ra-space-2);
  padding: var(--ra-space-2) var(--ra-space-3);
  border-radius: var(--ra-radius-md);
  font-size: var(--ra-font-size-sm);
  align-self: stretch;
}
.ra-chat__error-banner[data-variant="network"] { background: #fdecea; color: #7a1c14; }
.ra-chat__error-banner[data-variant="rate-limit"] { background: #fef4e0; color: #7a5a00; }
.ra-chat__error-banner[data-variant="cost-cap"] { background: var(--ra-color-disclaimer-bg); color: var(--ra-color-disclaimer-text); border: 1px solid var(--ra-color-disclaimer-border); }
.ra-chat__error-banner[data-variant="internal"] { background: #fdecea; color: #7a1c14; }
.ra-chat__error-banner button {
  margin-left: auto;
  background: transparent; border: 1px solid currentColor;
  color: inherit; padding: 4px 10px; border-radius: var(--ra-radius-sm);
  cursor: pointer; font-size: var(--ra-font-size-xs); font-weight: 600;
}

/* ============================================================================
 * Escalation-lomake (slide-in overlay panelin sisään)
 * ============================================================================ */

.ra-chat__escalation {
  position: absolute; inset: 0;
  background: var(--ra-color-surface-0);
  z-index: 2;
  display: flex; flex-direction: column;
  padding: var(--ra-space-4);
  transform: translateX(100%);
  transition: transform var(--ra-transition-base);
  overflow-y: auto;
}
.ra-chat__escalation[hidden] { display: none; }
.ra-chat__panel:not([hidden]) .ra-chat__escalation:not([hidden]) { transform: translateX(0); }
.ra-chat__escalation-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: var(--ra-space-3);
}
.ra-chat__escalation-header h3 { margin: 0; font-size: var(--ra-font-size-lg); color: var(--ra-color-text-primary); }
.ra-chat__escalation-header .ra-chat__icon-btn { color: var(--ra-color-text-primary); }
.ra-chat__escalation-header .ra-chat__icon-btn:hover { background: var(--ra-color-surface-sunken); }

.ra-chat__escalation-form { display: flex; flex-direction: column; gap: var(--ra-space-3); }
.ra-chat__escalation-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: var(--ra-font-size-sm); color: var(--ra-color-text-primary);
}
.ra-chat__escalation-form input[type="text"],
.ra-chat__escalation-form input[type="email"],
.ra-chat__escalation-form input[type="tel"],
.ra-chat__escalation-form textarea {
  border: 1px solid var(--ra-color-border);
  border-radius: var(--ra-radius-sm);
  padding: 8px 10px;
  font-family: var(--ra-font-family);
  font-size: var(--ra-font-size-base);
}
.ra-chat__escalation-form input:focus,
.ra-chat__escalation-form textarea:focus {
  outline: none;
  border-color: var(--ra-color-brand-700);
  box-shadow: 0 0 0 3px rgba(44, 62, 107, 0.15);
}
.ra-chat__escalation-form textarea { min-height: 80px; resize: vertical; }
.ra-chat__checkbox {
  flex-direction: row !important;
  align-items: flex-start; gap: var(--ra-space-2);
  cursor: pointer;
}
.ra-chat__checkbox input { margin-top: 2px; }
.ra-chat__escalation-submit {
  background: var(--ra-color-accent-600); color: #fff;
  border: none; border-radius: var(--ra-radius-md);
  padding: 10px 16px;
  font-size: var(--ra-font-size-base); font-weight: 600;
  cursor: pointer; min-height: 44px;
}
.ra-chat__escalation-submit:hover { background: var(--ra-color-accent-500); }
.ra-chat__escalation-submit:disabled { background: var(--ra-color-border); color: var(--ra-color-text-secondary); cursor: not-allowed; }
.ra-chat__escalation-sla {
  font-size: var(--ra-font-size-xs); color: var(--ra-color-text-secondary); margin: 0;
}
.ra-chat__honeypot { position: absolute !important; left: -9999px !important; width: 1px !important; height: 1px !important; opacity: 0 !important; }

/* ============================================================================
 * Success-toast (korilisäys)
 * ============================================================================ */

.ra-chat__toast {
  position: fixed; bottom: calc(var(--ra-space-5) + var(--ra-panel-height) + 8px);
  right: var(--ra-space-5); z-index: calc(var(--ra-z-panel) + 1);
  background: var(--ra-color-success); color: #fff;
  padding: 10px 16px; border-radius: var(--ra-radius-md);
  font-size: var(--ra-font-size-sm); font-weight: 600;
  box-shadow: var(--ra-shadow-bubble);
  animation: ra-toast-in var(--ra-transition-base);
}
.ra-chat__toast[hidden] { display: none; }
@keyframes ra-toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ============================================================================
 * Mobiili — paneeli full-screen
 * ============================================================================ */

@media (max-width: 640px) {
  .ra-chat__panel {
    right: 0; bottom: 0; left: 0; top: 0;
    width: 100vw; height: 100vh; max-height: none;
    border-radius: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .ra-chat__header {
    padding-top: max(var(--ra-space-3), env(safe-area-inset-top));
  }
  .ra-chat__toast {
    left: var(--ra-space-4); right: var(--ra-space-4); bottom: var(--ra-space-4);
  }
}

/* ============================================================================
 * Reduced motion
 * ============================================================================ */

@media (prefers-reduced-motion: reduce) {
  .ra-chat__greeting,
  .ra-chat__panel,
  .ra-chat__toast,
  .ra-chat__escalation { animation-duration: 0.01ms; transition: none; }
  .ra-chat__msg-bubble[data-streaming="true"]::after { animation: none; }
  .ra-chat__typing span { animation: none; opacity: 0.8; }
  .ra-chat__tool-status-icon { animation: none; }
}
