/* ─── Chat Button (Calcy avatar) ──────────────────────────────────────────── */

.chat-btn-wrap {
  position: absolute;
  top: -20px;
  right: -10px;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.chat-btn {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.2s ease;
  flex-shrink: 0;
}

.chat-btn:hover {
  transform: scale(1.1);
}

.chat-btn:active {
  transform: scale(0.95);
}

.chat-avatar {
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
}

.chat-btn.open {
  opacity: 0.7;
}

/* Wiggle animation for first visit */
.chat-btn.wiggle {
  animation: calcy-wiggle 1.5s ease-in-out infinite;
}

@keyframes calcy-wiggle {
  0%, 100% { transform: rotate(0deg); }
  15% { transform: rotate(-8deg); }
  30% { transform: rotate(8deg); }
  45% { transform: rotate(-5deg); }
  60% { transform: rotate(3deg); }
  75% { transform: rotate(0deg); }
}

/* Speech bubble — hidden by default, shown on hover or first visit */
.chat-bubble {
  position: absolute;
  right: 70px;
  top: 8px;
  background: var(--accent);
  color: var(--bg);
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transform: translateX(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.chat-bubble::after {
  content: '';
  position: absolute;
  right: -6px;
  top: 12px;
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 8px solid var(--accent);
}

/* Show on first visit (with entrance delay) */
.chat-bubble.show.first-visit {
  animation: bubble-appear 0.4s ease 0.5s both;
}

/* Show bubble (via JS toggle) */
.chat-bubble.show {
  opacity: 1;
  transform: translateX(0);
}

@keyframes bubble-appear {
  from { opacity: 0; transform: translateX(8px); }
  to { opacity: 1; transform: translateX(0); }
}

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

.chat-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 380px;
  height: 520px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 999;
  display: none;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

.chat-panel.visible {
  display: flex;
  animation: chat-appear 0.2s ease forwards;
}

@keyframes chat-appear {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-header-avatar {
  width: 28px;
  height: 28px;
  object-fit: contain;
  image-rendering: pixelated;
}

.chat-header-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.chat-badge {
  font-size: 0.65rem;
  color: var(--accent);
  background: rgba(6, 182, 212, 0.12);
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.chat-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  font-size: 1.25rem;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}

.chat-close:hover {
  color: var(--text);
  background: var(--card-hover);
}

/* ─── Messages Area ───────────────────────────────────────────────────────── */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

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

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

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

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

.chat-msg {
  max-width: 85%;
  padding: 10px 14px;
  font-size: 0.875rem;
  line-height: 1.5;
  word-wrap: break-word;
  animation: msg-appear 0.15s ease;
}

@keyframes msg-appear {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--bg);
  border-radius: 12px 12px 4px 12px;
}

.chat-msg.assistant {
  align-self: flex-start;
  background: var(--input-bg);
  color: var(--text);
  border-radius: 12px 12px 12px 4px;
  border: 1px solid var(--border);
}

.chat-msg.system {
  align-self: center;
  background: none;
  color: var(--text-dim);
  font-size: 0.8rem;
  text-align: center;
  padding: 6px 12px;
  max-width: 100%;
}

/* ─── Markdown in assistant messages ──────────────────────────────────────── */

.chat-msg.assistant p {
  margin: 0 0 8px 0;
}

.chat-msg.assistant p:last-child {
  margin-bottom: 0;
}

.chat-msg.assistant strong {
  color: var(--accent);
  font-weight: 600;
}

.chat-msg.assistant code {
  background: rgba(6, 182, 212, 0.1);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.82rem;
}

.chat-msg.assistant ul,
.chat-msg.assistant ol {
  margin: 4px 0 8px 0;
  padding-left: 18px;
}

.chat-msg.assistant li {
  margin-bottom: 2px;
}

/* ─── Typing Indicator ────────────────────────────────────────────────────── */

.chat-typing {
  align-self: flex-start;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 12px 12px 12px 4px;
  padding: 12px 18px;
  display: none;
  gap: 5px;
  align-items: center;
}

.chat-typing.visible {
  display: flex;
}

.typing-dot {
  width: 7px;
  height: 7px;
  background: var(--text-dim);
  border-radius: 50%;
  animation: typing-bounce 1.4s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.16s; }
.typing-dot:nth-child(3) { animation-delay: 0.32s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ─── Limit Indicator ─────────────────────────────────────────────────────── */

.chat-limit {
  padding: 6px 16px;
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-limit.at-limit {
  color: var(--yellow);
}

/* ─── Input Bar ───────────────────────────────────────────────────────────── */

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 0.875rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
  resize: none;
  overflow-y: hidden;
  max-height: 120px;
  line-height: 1.4;
}

.chat-input:focus {
  border-color: var(--accent);
}

.chat-input::placeholder {
  color: var(--text-dim);
}

.chat-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-send {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--accent);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

.chat-send:hover {
  background: var(--accent-dim);
}

.chat-send:active {
  transform: scale(0.92);
}

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

.chat-send svg {
  width: 18px;
  height: 18px;
  fill: var(--bg);
}

/* ─── Action Anchor Links (inline in prose) ────────────────────────────────── */

.chat-action-link {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
  cursor: pointer;
}

.chat-action-link:hover {
  text-decoration-style: solid;
}

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

.chat-actions-panel {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.chat-actions-header {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}

.chat-actions-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-action-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(6, 182, 212, 0.06);
  transition: background 0.3s;
}

.chat-action-item.highlight {
  animation: action-highlight 1s ease;
}

@keyframes action-highlight {
  0% { background: rgba(6, 182, 212, 0.25); }
  100% { background: rgba(6, 182, 212, 0.06); }
}

.chat-action-item.applied {
  background: rgba(34, 197, 94, 0.08);
}

.chat-action-label {
  font-size: 0.82rem;
  color: var(--text);
  flex: 1;
  min-width: 0;
}

.chat-action-apply {
  padding: 4px 12px;
  background: rgba(6, 182, 212, 0.15);
  color: var(--accent);
  border: 1px solid rgba(6, 182, 212, 0.3);
  border-radius: 6px;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}

.chat-action-apply:hover {
  background: rgba(6, 182, 212, 0.25);
  border-color: var(--accent);
}

.chat-action-item.applied .chat-action-apply {
  background: rgba(34, 197, 94, 0.15);
  color: var(--green);
  border-color: rgba(34, 197, 94, 0.3);
  cursor: default;
}

.chat-action-apply-all {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 8px;
  background: rgba(6, 182, 212, 0.12);
  color: var(--accent);
  border: 1px solid rgba(6, 182, 212, 0.25);
  border-radius: 6px;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.chat-action-apply-all:hover:not(:disabled):not(.applied) {
  background: rgba(6, 182, 212, 0.22);
  border-color: var(--accent);
}

.chat-action-apply-all:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.chat-action-apply-all.applied {
  background: rgba(34, 197, 94, 0.12);
  color: var(--green);
  border-color: rgba(34, 197, 94, 0.25);
  cursor: default;
}

/* ─── Undo Links ───────────────────────────────────────────────────────────── */

.chat-undo,
.chat-undo-all {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.75rem;
  text-decoration: underline;
  padding: 0 0 0 6px;
  font-family: inherit;
}

.chat-undo:hover,
.chat-undo-all:hover {
  color: var(--text);
}

/* ─── Dock/Expand Button ─────────────────────────────────────────────────── */

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-dock {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
  display: none;
}

.chat-dock:hover {
  color: var(--text);
  background: var(--card-hover);
}

.chat-dock svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

/* Only show dock button on wide viewports */
@media (min-width: 900px) {
  .chat-dock {
    display: block;
  }
}

/* ─── Docked Mode ────────────────────────────────────────────────────────── */

body.chat-docked .container {
  margin-right: 400px;
  transition: margin-right 0.25s ease;
}

body.chat-docked .chat-panel {
  top: 0;
  right: 0;
  bottom: 0;
  left: auto;
  width: 380px;
  height: 100%;
  border-radius: 0;
  border: none;
  border-left: 1px solid var(--border);
  box-shadow: none;
}

body.chat-docked .chat-panel.visible {
  animation: none;
}

/* ─── Mobile ──────────────────────────────────────────────────────────────── */

@media (max-width: 600px) {
  .chat-panel {
    width: 100%;
    height: 100%;
    bottom: 0;
    right: 0;
    border-radius: 0;
    border: none;
  }

  .chat-btn {
    bottom: 16px;
    right: 16px;
  }
}
