/* ============================================================
   AI Digital Twin widget — kawalec.pl
   Namespacing .dk-twin-* (zero kolizji z site CSS).
   Dual-theme przez custom properties + override body.light.
   ============================================================ */

.dk-twin {
  /* dark (default) */
  --dk-bg: #1c1c1c;
  --dk-bg-2: #161616;
  --dk-fg: #ffffff;
  --dk-fg-dim: rgba(255, 255, 255, 0.62);
  --dk-line: rgba(255, 255, 255, 0.1);
  --dk-accent: #ddf160;        /* site .main-color (żółty) */
  --dk-accent-fg: #161616;
  --dk-bot-bubble: #232323;
  --dk-user-bubble: #ddf160;
  --dk-user-fg: #161616;
  --dk-live: #10b981;          /* jedyny zielony - kropka online */
  --dk-radius: 20px;
  --dk-radius-bubble: 16px;
  --dk-shadow: 0 18px 50px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.4);
  --dk-font-head: "Funnel Display", sans-serif;
  --dk-font-body: "Mozilla Text", sans-serif;

  position: fixed;
  z-index: 9000;
  font-family: var(--dk-font-body);
  line-height: 1.4;
}

body.light .dk-twin {
  --dk-bg: #ffffff;
  --dk-bg-2: #f4f4f4;
  --dk-fg: #161616;
  --dk-fg-dim: rgba(22, 22, 22, 0.6);
  --dk-line: rgba(0, 0, 0, 0.1);
  --dk-accent: #9f8be7;        /* light-mode akcent = fiolet (jak site) */
  --dk-accent-fg: #ffffff;
  --dk-bot-bubble: #f0f0f3;
  --dk-user-bubble: #9f8be7;
  --dk-user-fg: #ffffff;
  --dk-shadow: 0 18px 50px rgba(0, 0, 0, 0.18), 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* podbij istniejący back-to-top żeby nie nachodził na FAB */
.progress-wrap { bottom: 96px !important; }
@media (max-width: 767px) { .progress-wrap { bottom: 88px !important; } }

/* ---------------- FAB ---------------- */
.dk-twin-fab {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--dk-accent);
  color: var(--dk-accent-fg);
  border: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: var(--dk-shadow);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.dk-twin-fab:hover { transform: translateY(-2px) scale(1.04); }
.dk-twin-fab:focus-visible { outline: 3px solid var(--dk-live); outline-offset: 3px; }
.dk-twin-fab svg { width: 27px; height: 27px; display: block; }
.dk-twin-fab .dk-twin-fab-close { display: none; }
.dk-twin.is-open .dk-twin-fab .dk-twin-fab-open { display: none; }
.dk-twin.is-open .dk-twin-fab .dk-twin-fab-close { display: block; }

.dk-twin-fab-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: var(--dk-live);
  color: #fff;
  font: 600 11px/18px var(--dk-font-body);
  text-align: center;
  padding: 0 4px;
}

/* ---------------- Panel ---------------- */
.dk-twin-panel {
  position: fixed;
  right: 24px;
  bottom: 96px;
  width: 384px;
  height: min(640px, 78vh);
  display: flex;
  flex-direction: column;
  background: var(--dk-bg);
  color: var(--dk-fg);
  border: 1px solid var(--dk-line);
  border-radius: var(--dk-radius);
  box-shadow: var(--dk-shadow);
  overflow: hidden;
  transform-origin: bottom right;
  opacity: 0;
  transform: translateY(12px) scale(0.98);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.dk-twin.is-open .dk-twin-panel {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/* ---------------- Header ---------------- */
.dk-twin-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--dk-line);
  background: var(--dk-bg-2);
}
.dk-twin-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--dk-accent);
  flex: 0 0 38px;
}
.dk-twin-id { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.dk-twin-title { font: 600 15px/1.2 var(--dk-font-head); }
.dk-twin-sub {
  font-size: 12px;
  color: var(--dk-fg-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}
.dk-twin-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--dk-live);
  box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.5);
  animation: dk-pulse 2s infinite;
}
.dk-twin-reset {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--dk-fg-dim);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 8px;
  display: grid;
  place-items: center;
}
.dk-twin-reset:hover { color: var(--dk-fg); }
.dk-twin-reset svg { width: 17px; height: 17px; display: block; }
.dk-twin-close {
  background: none;
  border: none;
  color: var(--dk-fg-dim);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}
.dk-twin-close:hover { color: var(--dk-fg); }

/* ---------------- Log + bubbles ---------------- */
.dk-twin-log {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--dk-bg);
  scroll-behavior: smooth;
}
.dk-twin-msg {
  max-width: 85%;
  padding: 10px 13px;
  border-radius: var(--dk-radius-bubble);
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.dk-twin-msg.is-bot {
  background: var(--dk-bot-bubble);
  color: var(--dk-fg);
  border-bottom-left-radius: 5px;
  align-self: flex-start;
}
.dk-twin-msg.is-user {
  background: var(--dk-user-bubble);
  color: var(--dk-user-fg);
  border-bottom-right-radius: 5px;
  align-self: flex-end;
}
.dk-twin-msg a { color: inherit; text-decoration: underline; }
.dk-twin-msg strong { font-weight: 700; }
.dk-twin-msg code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: rgba(255, 255, 255, 0.09);
  padding: 1px 5px;
  border-radius: 5px;
}
body.light .dk-twin-msg code { background: rgba(0, 0, 0, 0.07); }
.dk-twin-msg.is-user code { background: rgba(0, 0, 0, 0.12); }

/* ---------------- Typing ---------------- */
.dk-twin-typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 14px;
  background: var(--dk-bot-bubble);
  border-radius: var(--dk-radius-bubble);
  border-bottom-left-radius: 5px;
}
.dk-twin-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--dk-fg-dim);
  animation: dk-bounce 1.2s infinite;
}
.dk-twin-typing span:nth-child(2) { animation-delay: 0.15s; }
.dk-twin-typing span:nth-child(3) { animation-delay: 0.3s; }

/* ---------------- Chips ---------------- */
.dk-twin-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px 12px;
}
.dk-twin-chip {
  font: 500 13px var(--dk-font-body);
  padding: 7px 14px;
  border: 1px solid var(--dk-accent);
  color: var(--dk-accent);
  background: transparent;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.15s;
}
.dk-twin-chip:hover { background: var(--dk-accent); color: var(--dk-accent-fg); }

/* ---------------- Input ---------------- */
.dk-twin-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--dk-line);
  background: var(--dk-bg-2);
}
.dk-twin-textarea {
  flex: 1;
  resize: none;
  max-height: 110px;
  min-height: 40px;
  background: var(--dk-bg);
  color: var(--dk-fg);
  border: 1px solid var(--dk-line);
  border-radius: 12px;
  padding: 10px 12px;
  font: 14px var(--dk-font-body);
  outline: none;
}
.dk-twin-textarea:focus { border-color: var(--dk-accent); }
.dk-twin-send {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border: none;
  border-radius: 12px;
  background: var(--dk-accent);
  color: var(--dk-accent-fg);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.dk-twin-send svg { width: 18px; height: 18px; }
.dk-twin-send:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- icon buttons (attach / mic) ---------- */
.dk-twin-iconbtn {
  width: 36px;
  height: 40px;
  flex: 0 0 32px;
  border: none;
  background: none;
  color: var(--dk-fg-dim);
  cursor: pointer;
  display: grid;
  place-items: center;
  border-radius: 10px;
}
.dk-twin-iconbtn:hover { color: var(--dk-fg); }
.dk-twin-iconbtn svg { width: 19px; height: 19px; }
.dk-twin-mic.is-rec { color: #ef4444; animation: dk-pulse-mic 1.3s infinite; }
@keyframes dk-pulse-mic { 0%,100% { opacity: 1; } 50% { opacity: 0.45; } }

/* ---------- attachment previews (above input) ---------- */
.dk-twin-atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 12px; }
.dk-twin-atts:not(:empty) { padding: 10px 12px 0; }
.dk-twin-att {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 160px;
  background: var(--dk-bg);
  border: 1px solid var(--dk-line);
  border-radius: 10px;
  padding: 4px 6px;
}
.dk-twin-att img { width: 44px; height: 44px; object-fit: cover; border-radius: 7px; }
.dk-twin-att-ic { font-size: 18px; }
.dk-twin-att-nm { font-size: 12px; color: var(--dk-fg-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100px; }
.dk-twin-att-x {
  position: absolute; top: -7px; right: -7px;
  width: 18px; height: 18px; border-radius: 50%;
  border: none; background: #ef4444; color: #fff;
  font-size: 13px; line-height: 1; cursor: pointer;
}

/* ---------- attachments inside a sent bubble ---------- */
.dk-twin-msg-atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.dk-twin-msg-img { max-width: 180px; max-height: 180px; border-radius: 10px; display: block; }
.dk-twin-msg-file { font-size: 12px; opacity: 0.85; }

/* ---------- drag-drop hint ---------- */
.dk-twin-panel.dk-twin-drag { outline: 2px dashed var(--dk-accent); outline-offset: -8px; }

.dk-twin-consent {
  font-size: 11px;
  color: var(--dk-fg-dim);
  text-align: center;
  padding: 0 16px 10px;
}
.dk-twin-consent a { color: var(--dk-fg-dim); }

/* ---------------- Mobile sheet ---------------- */
@media (max-width: 767px) {
  .dk-twin-panel {
    inset: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100dvh;
    border-radius: 0;
  }
  .dk-twin.is-open .dk-twin-fab { display: none; }
}

/* ---------------- Animations ---------------- */
@keyframes dk-pulse {
  0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.5); }
  70% { box-shadow: 0 0 0 7px rgba(16, 185, 129, 0); }
  100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}
@keyframes dk-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .dk-twin-panel, .dk-twin-fab { transition: none; }
  .dk-twin-dot, .dk-twin-typing span { animation: none; }
  .dk-twin-log { scroll-behavior: auto; }
}
