/* Kipinä-widgetin tyylit, osa 1/5: modaali, otsikkorivi, viestit ja kirjoitusilmaisin.
   Rakenne (kipina-widget.js) ja tyylit pidetään erillään; luokat alkavat kip-. Osat ladataan
   partials/footer.php:ssä numerojärjestyksessä ennen kipina-widget.js:ää, koska myöhempi osa voi
   ohittaa aiemman. */
.kip-overlay {
  position: fixed; inset: 0; z-index: 600;
  background: rgba(9,14,36,0.6);
  backdrop-filter: blur(6px) saturate(120%);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0 16px 24px;
  opacity: 0; pointer-events: none; visibility: hidden;
  transition: opacity 0.22s ease, visibility 0s linear 0.22s;
}
@media (min-width: 520px) {
  .kip-overlay { align-items: center; padding: 20px; }
}
.kip-overlay.kip-open {
  opacity: 1; pointer-events: all; visibility: visible;
  transition-delay: 0s;
}
/* Keep the closing fade, then omit the hidden dialog from painting.
   Its status/typing animations only need to run while the dialog is open. */
.kip-overlay:not(.kip-open) * {
  animation-play-state: paused;
}
.kip-modal {
  background: #fff;
  border-radius: 24px;
  width: 100%; max-width: 560px;
  max-height: 90vh;
  display: flex; flex-direction: column;
  box-shadow: 0 32px 80px -16px rgba(9,14,36,0.55), 0 0 0 1px rgba(255,255,255,0.08);
  transform: translateY(24px) scale(0.97);
  transition: transform 0.3s cubic-bezier(0.22,1,0.36,1);
  overflow: hidden;
}
.kip-overlay.kip-open .kip-modal {
  transform: translateY(0) scale(1);
}

/* Header */
.kip-header {
  background: linear-gradient(135deg, #090e24 0%, #1a2452 100%);
  padding: 12px 14px 12px 16px;
  display: flex; align-items: center; gap: 12px;
  flex-shrink: 0;
}
.kip-avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: rgba(255,255,255,0.08);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 4px 14px rgba(230,70,68,0.35);
  overflow: hidden;
  padding: 3px;
}
.kip-avatar img { width: 100%; height: 100%; object-fit: contain; }
.kip-header-info { flex: 1; min-width: 0; }
.kip-name-row { display: flex; align-items: center; gap: 10px; }
.kip-name {
  font-family: 'Figtree',system-ui,sans-serif;
  font-size: 16px; font-weight: 800; color: #fff; letter-spacing: -0.01em; line-height: 1.2;
}
/* Pelkkä tila: nimi ja kuvake kertovat jo, kuka puhuu. */
.kip-meta {
  display: flex; align-items: center; gap: 6px; margin-top: 2px;
  font-family: 'Figtree', sans-serif; font-size: 12px; color: rgba(255,255,255,0.62);
  white-space: nowrap; overflow: hidden;
}
.kip-status { display: inline-flex; align-items: center; gap: 5px; flex-shrink: 0; }
.kip-end-note {
  margin: 6px 0 4px 46px; font-size: 12.5px; color: rgba(26,36,82,0.62);
}
.kip-status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: #4ade80;
  box-shadow: 0 0 6px rgba(74,222,128,0.7);
  animation: kipPulse 2.5s ease-in-out infinite;
}
@keyframes kipPulse {
  0%,100% { opacity: 1; } 50% { opacity: 0.45; }
}
/* Myyntipolun eteneminen: kolme pistettä (tarve, hinta, yhteystieto). Näkyy vain
   kehitysympäristön testimallipalkissa, ei kävijälle. */
.kip-stage { display: inline-flex; gap: 4px; align-items: center; }
.kip-stage[hidden] { display: none; }
.kip-stage span {
  width: 6px; height: 6px; border-radius: 50%;
  background: rgba(255,255,255,0.22);
  transition: background 0.35s ease, box-shadow 0.35s ease;
}
.kip-stage span.on { background: #fcb549; box-shadow: 0 0 0 2px rgba(252,181,73,0.25); }
/* Napit yhtenä hiljaisena ryhmänä: tausta vain hoverissa, sulje hieman muita vahvempi. */
.kip-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; margin-left: 4px; }
.kip-close {
  width: 32px; height: 32px; border-radius: 50%;
  background: transparent; border: none; color: rgba(255,255,255,0.78);
  cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 0;
  transition: background 0.2s, color 0.2s; flex-shrink: 0;
}
.kip-close:hover, .kip-close:focus-visible { background: rgba(255,255,255,0.14); color: #fff; outline: none; }
.kip-close svg { width: 16px; height: 16px; display: block; }
.kip-close-x { color: #fff; background: rgba(255,255,255,0.1); margin-left: 4px; }
.kip-close-x:hover { background: rgba(255,255,255,0.22); }
.kip-close[hidden] { display: none; }
/* Soittopyyntö: otsikkorivin ainoa näkyvä toiminto sulkemisen lisäksi, ääriviivallinen pilleri,
   jotta ihmisen tavoittaa aina yhdellä painalluksella. */
.kip-human {
  width: auto; gap: 6px; padding: 0 12px 0 9px; border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.28);
  font-family: 'Figtree', sans-serif; font-size: 12.5px; font-weight: 600;
  color: rgba(255,255,255,0.85); white-space: nowrap; margin-right: 2px;
}
.kip-human svg { width: 15px; height: 15px; }
@media (max-width: 520px) {
  /* Kapealla näytöllä otsikkorivi on ahdas: ikoni jää, nimi kulkee aria-labelissa. */
  .kip-human span { display: none; }
  .kip-human { padding: 0; width: 32px; }
}
.kip-menu-btn svg { width: 18px; height: 18px; }
.kip-menu-btn[aria-expanded="true"] { background: rgba(255,255,255,0.14); color: #fff; }
.kip-header { position: relative; }
/* Kolmen pisteen valikko otsikon alla oikeassa reunassa, viestien päällä. */
.kip-menu {
  position: absolute; top: calc(100% - 6px); right: 12px; z-index: 5;
  width: 250px; padding: 6px; border-radius: 12px;
  background: #fff; border: 1px solid #e3e6ef;
  box-shadow: 0 14px 34px -12px rgba(9,14,36,0.38);
}
.kip-menu[hidden] { display: none; }
.kip-menu-item {
  display: flex; align-items: flex-start; gap: 10px; width: 100%;
  padding: 9px 10px; border: 0; border-radius: 8px; background: transparent;
  font-family: 'Figtree', sans-serif; font-size: 13.5px; font-weight: 600; line-height: 1.3;
  color: #1a2452; text-align: left; cursor: pointer;
}
.kip-menu-item[hidden] { display: none; }
.kip-menu-item:hover, .kip-menu-item:focus-visible, .kip-menu-item[aria-expanded="true"] { background: #f1f3f9; outline: none; }
.kip-menu-item svg { width: 17px; height: 17px; flex-shrink: 0; margin-top: 1px; color: #4a5060; }
.kip-menu-text small { display: block; margin-top: 2px; font-size: 12px; font-weight: 400; color: #5a6072; }
.kip-menu-dev { margin-top: 4px; border-top: 1px dashed #e3e6ef; border-radius: 0 0 8px 8px; }
.kip-modal { position: relative; }
.kip-debugbar {
  position: absolute; top: 58px; right: 12px; z-index: 2;
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 10px;
  background: #0f1533; color: rgba(255,255,255,0.6); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase;
  box-shadow: 0 10px 24px -8px rgba(9,14,36,0.5);
}
.kip-debugbar[hidden] { display: none; }

/* Messages */
.kip-messages {
  flex: 1; overflow-y: auto; padding: 22px 20px;
  display: flex; flex-direction: column; gap: 14px;
  scroll-behavior: smooth;
}

/* Tilaviesti ruudunlukijalle. Piilossa nakyvista, mutta ei display:nonella, koska silloin
   ruudunlukija ei lue sita. Oma alue viestilistan sijaan: muuten jokainen streamin osa
   ja jokainen kortti keskeyttaisi lukijan uudestaan. */
.kip-sr-live {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}
.kip-msg {
  display: flex; gap: 10px; align-items: flex-end;
  animation: kipFadeUp 0.25s ease forwards;
}
@keyframes kipFadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.kip-msg.kip-msg--ai { justify-content: flex-start; }
.kip-msg.kip-msg--user { justify-content: flex-end; }
.kip-msg-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: rgba(255,255,255,0.05);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; overflow: hidden; padding: 4px;
}
.kip-msg-avatar img { width: 100%; height: 100%; object-fit: contain; }
.kip-bubble {
  max-width: 84%; padding: 13px 17px;
  font-family: 'Figtree',system-ui,sans-serif;
  font-size: 15.5px; line-height: 1.55; border-radius: 20px;
}
.kip-msg--ai .kip-bubble {
  background: #f2f4fb; color: #151515;
  border-bottom-left-radius: 5px;
}
.kip-msg--user .kip-bubble {
  background: linear-gradient(135deg,#1a2452,#232e66);
  color: #fff; border-bottom-right-radius: 5px;
}
/* Artikkelilainaus kävijän oman viestin yläpuolella. */
.kip-quoted {
  border-left: 3px solid #fcb549; padding: 6px 12px; margin: 0 0 10px;
  font-style: italic; color: rgba(255,255,255,0.78);
  font-size: 13.5px; line-height: 1.5;
  background: rgba(255,255,255,0.10); border-radius: 0 8px 8px 0;
}
/* Lähettämätön vuoro: kupla himmenee ja saa pienen merkinnän omalle rivilleen. */
.kip-msg--unsent { flex-wrap: wrap; }
.kip-msg--unsent .kip-bubble { opacity: 0.6; }
.kip-unsent-mark {
  flex-basis: 100%; margin-top: 4px; text-align: right;
  font-family: 'Figtree', sans-serif; font-size: 11.5px; color: rgba(26,36,82,0.55);
}
/* Ehdotukset piilossa lähetyksen ajan; animaatio ei saa pitää riviä näkyvissä. */
.kip-chips-group[hidden], .kip-quick-replies[hidden], .kip-switch-normal[hidden] { display: none; }
/* Häiriöilmoitus: Kipinän kuplan muoto, mutta lämmin sävy erottaa sen vastauksesta. */
.kip-msg--notice .kip-bubble {
  background: #fff5ea; border: 1px solid rgba(245,128,36,0.3); color: #3c2a15;
}
/* Lohkotason nappi: rivinsisäisenä se jatkaisi ilmoituksen viimeistä tekstiriviä. */
.kip-retry-btn {
  display: flex; width: fit-content; align-items: center; margin-top: 11px;
  font-family: 'Figtree', sans-serif; font-size: 13.5px; font-weight: 700;
  padding: 7px 15px; border-radius: 999px; border: 0; cursor: pointer;
  background: #1a2452; color: #fff;
  transition: background 0.2s ease, color 0.2s ease;
}
.kip-retry-btn:hover:not(:disabled), .kip-retry-btn:focus-visible {
  background: #232e66; outline: none; box-shadow: 0 0 0 3px rgba(252,181,73,0.4);
}
.kip-retry-btn:disabled {
  background: rgba(26,36,82,0.12); color: rgba(26,36,82,0.62); cursor: default;
}
/* Caption-style supplementary text (_italic_ + auto-detected parentheticals).
   Dimmed and slightly smaller — like a subtitle. NOT actually italic for clean reading. */
.kip-msg--ai .kip-bubble em {
  font-style: normal;
  color: #6b7280;
  font-size: 0.93em;
  letter-spacing: 0.005em;
}
/* Bold acts as a strong question header */
.kip-msg--ai .kip-bubble strong {
  color: #0a1024;
  font-weight: 700;
}

/* Linkit vastauksissa. Alleviivaus on vaimea lepotilassa ja tummuu osoitettaessa, jottei
   linkki erotu pelkalla varilla: pelkka vari ei riita erottamaan linkkia tekstista. */
.kip-msg .kip-bubble a {
  color: #232e66;
  font-weight: 600;
  text-decoration: underline;
  text-decoration-color: rgba(35,46,102,0.35);
  text-underline-offset: 2px;
}
.kip-msg .kip-bubble a:hover,
.kip-msg .kip-bubble a:focus-visible {
  text-decoration-color: currentColor;
}

/* Typing indicator */
.kip-typing .kip-bubble {
  display: flex; align-items: center; gap: 4px; padding: 12px 14px;
}
.kip-dot {
  width: 6px; height: 6px; border-radius: 50%; background: #9ca3af;
  animation: kipBounce 1.2s ease-in-out infinite;
}
.kip-dot:nth-child(2) { animation-delay: 0.2s; }
.kip-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes kipBounce {
  0%,60%,100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* Success state */
.kip-success {
  display: flex; flex-direction: column; align-items: center;
  gap: 11px; padding: 24px 16px 12px;
  animation: kipFadeUp 0.35s ease forwards;
  text-align: center;
}
.kip-success-icon {
  width: 56px; height: 56px; border-radius: 50%;
  background: rgba(22,101,52,0.10);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.kip-success-icon svg { display: block; }
.kip-success-label {
  font-family: 'Figtree',system-ui,sans-serif;
  font-size: 14.5px; font-weight: 700; color: #166534;
  letter-spacing: 0.01em;
}

