/* =========================================================
   Ahmed Osam — portfolio assistant
   Self-contained chat widget. No dependencies, no backend:
   it calls the public Pollinations chat-completions endpoint
   directly from the browser (CORS-enabled, keyless).
   ========================================================= */

.cp-chat {
  --cp-radius: 18px;
  --cp-panel: rgba(11, 17, 32, 0.92);
  --cp-line: rgba(148, 163, 184, 0.22);
  position: fixed;
  z-index: 150;
  inset-inline-end: 18px;
  bottom: 18px;
  font-family: var(--font);
  color: var(--text);
}

[data-lang="ar"] .cp-chat { font-family: var(--font); }

.cp-chat-launcher {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  height: 54px;
  padding: 0 18px 0 14px;
  border: 1px solid var(--cp-line);
  border-radius: 999px;
  background: linear-gradient(160deg, rgba(56, 189, 248, 0.16), rgba(167, 139, 250, 0.18)), var(--bg-soft);
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 18px 40px rgba(3, 7, 18, 0.55), 0 0 0 1px rgba(56, 189, 248, 0.08) inset;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.cp-chat-launcher:hover {
  transform: translateY(-2px);
  border-color: rgba(56, 189, 248, 0.55);
  box-shadow: 0 22px 50px rgba(3, 7, 18, 0.6), 0 0 24px rgba(56, 189, 248, 0.18);
}

.cp-chat-launcher:focus-visible {
  outline: 2px solid var(--sky);
  outline-offset: 3px;
}

.cp-chat-launcher svg { width: 22px; height: 22px; flex: none; }

.cp-chat-launcher-label {
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.cp-chat-launcher .cp-chat-ping {
  position: absolute;
  top: -3px;
  inset-inline-end: 6px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.65);
  animation: cpPing 2.6s ease-out infinite;
}

@keyframes cpPing {
  0% { box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.55); }
  70% { box-shadow: 0 0 0 10px rgba(52, 211, 153, 0); }
  100% { box-shadow: 0 0 0 0 rgba(52, 211, 153, 0); }
}

.cp-chat-panel {
  position: absolute;
  inset-inline-end: 0;
  bottom: 66px;
  display: flex;
  flex-direction: column;
  width: min(384px, calc(100vw - 24px));
  height: min(560px, calc(100dvh - 120px));
  border: 1px solid var(--cp-line);
  border-radius: var(--cp-radius);
  background: var(--cp-panel);
  backdrop-filter: blur(18px) saturate(1.2);
  box-shadow: 0 30px 80px rgba(2, 6, 20, 0.7), 0 1px 0 rgba(255, 255, 255, 0.04) inset;
  overflow: hidden;
}

.cp-chat-panel[hidden] { display: none; }

.cp-chat-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--cp-line);
  background: linear-gradient(180deg, rgba(56, 189, 248, 0.08), transparent);
}

.cp-chat-id { display: flex; gap: 10px; align-items: flex-start; min-width: 0; }

.cp-chat-dot {
  width: 10px;
  height: 10px;
  margin-top: 7px;
  flex: none;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 12px rgba(52, 211, 153, 0.7);
}

.cp-chat-title {
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.3;
}

.cp-chat-sub {
  margin-top: 2px;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.5;
}

.cp-chat-head-actions { display: flex; gap: 4px; flex: none; }

.cp-chat-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.cp-chat-icon-btn:hover {
  color: var(--text);
  background: rgba(148, 163, 184, 0.1);
  border-color: var(--cp-line);
}

.cp-chat-icon-btn:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }

.cp-chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: rgba(148, 163, 184, 0.3) transparent;
}

.cp-chat-log::-webkit-scrollbar { width: 8px; }
.cp-chat-log::-webkit-scrollbar-thumb { background: rgba(148, 163, 184, 0.28); border-radius: 99px; }

.cp-msg {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 0.86rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: 1px solid transparent;
}

.cp-msg--user {
  align-self: flex-end;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.22), rgba(167, 139, 250, 0.24));
  border-color: rgba(56, 189, 248, 0.3);
  border-end-end-radius: 6px;
}

.cp-msg--bot {
  align-self: flex-start;
  background: rgba(148, 163, 184, 0.09);
  border-color: var(--cp-line);
  border-end-start-radius: 6px;
}

.cp-msg strong { color: var(--text); font-weight: 700; }

.cp-msg code {
  font-family: var(--mono);
  font-size: 0.82em;
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(56, 189, 248, 0.12);
  border: 1px solid rgba(56, 189, 248, 0.2);
}

.cp-msg--system {
  align-self: center;
  max-width: 100%;
  background: transparent;
  border-style: dashed;
  border-color: var(--cp-line);
  color: var(--muted);
  font-size: 0.76rem;
  text-align: center;
}

.cp-chat-typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  align-self: flex-start;
  border-radius: 14px;
  background: rgba(148, 163, 184, 0.09);
  border: 1px solid var(--cp-line);
  border-end-start-radius: 6px;
}

.cp-chat-typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: cpBounce 1.1s ease-in-out infinite;
}

.cp-chat-typing i:nth-child(2) { animation-delay: 0.14s; }
.cp-chat-typing i:nth-child(3) { animation-delay: 0.28s; }

@keyframes cpBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.cp-chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--cp-line);
  background: rgba(7, 11, 22, 0.6);
}

.cp-chat-input {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  resize: none;
  padding: 10px 12px;
  border: 1px solid var(--cp-line);
  border-radius: 12px;
  background: rgba(11, 17, 32, 0.85);
  color: var(--text);
  font: inherit;
  font-size: 0.86rem;
  line-height: 1.45;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.cp-chat-input::placeholder { color: rgba(147, 161, 181, 0.75); }

.cp-chat-input:focus {
  border-color: rgba(56, 189, 248, 0.6);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.12);
}

.cp-chat-send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  flex: none;
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.28), rgba(167, 139, 250, 0.3));
  color: var(--text);
  cursor: pointer;
  transition: filter 0.2s ease, transform 0.2s ease, opacity 0.2s ease;
}

.cp-chat-send:hover:not(:disabled) { filter: brightness(1.15); transform: translateY(-1px); }
.cp-chat-send:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }
.cp-chat-send:disabled { opacity: 0.45; cursor: not-allowed; }

.cp-chat-note {
  padding: 0 14px 10px;
  font-size: 0.66rem;
  color: rgba(147, 161, 181, 0.8);
  text-align: center;
  line-height: 1.5;
}

.cp-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 560px) {
  .cp-chat { inset-inline-end: 12px; bottom: 12px; }
  .cp-chat-panel {
    position: fixed;
    inset-inline: 8px;
    bottom: 78px;
    width: auto;
    height: min(70dvh, 560px);
  }
  .cp-chat-launcher-label { display: none; }
  .cp-chat-launcher { width: 54px; padding: 0; justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  .cp-chat-launcher,
  .cp-chat-send,
  .cp-chat-launcher .cp-chat-ping,
  .cp-chat-typing i { animation: none; transition: none; }
}
