/* ═══════════════════════════════════════════
   Dreamscape — Collab: In-Room Dream Chat
   Owner: Reverie · Branch: feat/collab-chat

   Scoped under .dchat. Consumes the global theme tokens from
   base.css (which flip via [data-theme="light"]), so the panel
   tracks the app theme automatically. A prefers-color-scheme
   fallback covers the standalone case (no [data-theme] set),
   per CLAUDE.md's dark+light requirement.
   ═══════════════════════════════════════════ */

.dchat {
  /* component tokens → global tokens (dark defaults if globals absent) */
  --dchat-bg:      var(--panel, #241535);
  --dchat-fg:      var(--text-primary, rgba(255, 255, 255, 0.85));
  --dchat-dim:     var(--text-secondary, rgba(255, 255, 255, 0.45));
  --dchat-accent:  var(--lavender, #c4b5fd);
  --dchat-accent2: var(--moonlight, #93c5fd);
  --dchat-border:  var(--border, rgba(255, 255, 255, 0.08));
  --dchat-bubble:  var(--hover, #2e1f42);
  --dchat-tint:    var(--overlay-tint, rgba(255, 255, 255, 0.04));
  --dchat-ease:    var(--ease, cubic-bezier(0.4, 0, 0.2, 1));

  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  width: 100%;
  background: var(--dchat-bg);
  color: var(--dchat-fg);
  border: 1px solid var(--dchat-border);
  border-radius: 16px;
  overflow: hidden;
  font-family: 'Quicksand', sans-serif;
  position: relative;
}

/* Soft dream glow at the top edge */
.dchat::before {
  content: '';
  position: absolute;
  inset: 0 0 auto 0;
  height: 80px;
  background: radial-gradient(120% 80px at 50% 0%,
    rgba(167, 139, 250, 0.14), transparent 70%);
  pointer-events: none;
}

/* ── Floating fallback root (used only before Vesper's container exists) ── */
.dchat-root--floating {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 340px;
  height: min(60vh, 520px);
  z-index: 900;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.42);
  border-radius: 16px;
}

/* ── Header ── */
.dchat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--dchat-border);
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}
.dchat-title {
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.4px;
  color: var(--dchat-fg);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.dchat-star {
  color: var(--dchat-accent);
  text-shadow: 0 0 10px rgba(167, 139, 250, 0.5);
}
.dchat-collapse {
  background: transparent;
  border: 1px solid var(--dchat-border);
  color: var(--dchat-dim);
  width: 24px;
  height: 24px;
  border-radius: 7px;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: all 0.15s var(--dchat-ease);
}
.dchat-collapse:hover {
  color: var(--dchat-fg);
  border-color: var(--dchat-accent);
  background: var(--dchat-tint);
}

/* Collapsed: only the header shows */
.dchat.collapsed .dchat-log,
.dchat.collapsed .dchat-typing,
.dchat.collapsed .dchat-compose,
.dchat.collapsed .dchat-jump {
  display: none;
}
.dchat.collapsed {
  height: auto;
}

/* ── Message log ── */
.dchat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

/* ── A single message ── */
.dchat-msg {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 88%;
  align-self: flex-start;
  animation: dchat-rise 0.32s var(--dchat-ease) both;
}
.dchat-msg.own {
  align-self: flex-end;
  align-items: flex-end;
}
.dchat-msg.grouped { margin-top: -4px; }

.dchat-msg-head {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.7rem;
}
.dchat-msg.own .dchat-msg-head { flex-direction: row-reverse; }

.dchat-avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.62rem;
  font-weight: 700;
  color: #1a1025;
  object-fit: cover;
  box-shadow: 0 0 0 1px var(--dchat-border);
}
.dchat-avatar.system {
  background: linear-gradient(135deg, var(--dchat-accent), var(--dchat-accent2));
  color: #fff;
  font-size: 0.72rem;
}
.dchat-msg-name {
  font-weight: 600;
  letter-spacing: 0.2px;
}
.dchat-msg-time {
  color: var(--dchat-dim);
  font-size: 0.64rem;
  font-variant-numeric: tabular-nums;
}

/* ── Bubble ── */
.dchat-bubble {
  background: var(--dchat-bubble);
  color: var(--dchat-fg);
  padding: 8px 12px;
  border-radius: 14px;
  border-top-left-radius: 5px;
  font-size: 0.84rem;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  border-left: 2px solid var(--dchat-edge, transparent);
}
.dchat-msg.own .dchat-bubble {
  background: linear-gradient(135deg,
    rgba(167, 139, 250, 0.34), rgba(147, 197, 253, 0.24));
  border-top-left-radius: 14px;
  border-top-right-radius: 5px;
  border-left: none;
}
.dchat-msg.grouped .dchat-bubble {
  border-top-left-radius: 14px;
}
.dchat-msg.own.grouped .dchat-bubble {
  border-top-right-radius: 14px;
}

/* ── System / co-pilot bubble ── */
.dchat-msg.system { max-width: 94%; align-self: stretch; }
.dchat-bubble.system {
  background: linear-gradient(135deg,
    rgba(167, 139, 250, 0.12), rgba(147, 197, 253, 0.08));
  border: 1px dashed rgba(167, 139, 250, 0.4);
  border-radius: 12px;
  font-style: italic;
  color: var(--dchat-fg);
}
.dchat-bubble.system code {
  font-style: normal;
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  font-size: 0.8em;
  background: rgba(127, 127, 127, 0.18);
  padding: 1px 5px;
  border-radius: 5px;
}
.dchat-msg.system .dchat-msg-name {
  color: var(--dchat-accent);
}
/* tone variants the co-pilot can request */
.dchat-msg.system.tone-alert .dchat-bubble.system {
  border-color: rgba(253, 164, 175, 0.55);
  background: linear-gradient(135deg,
    rgba(253, 164, 175, 0.14), rgba(167, 139, 250, 0.08));
}

/* ── Empty state ── */
.dchat-empty {
  margin: auto;
  text-align: center;
  color: var(--dchat-dim);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.dchat-empty-star {
  font-size: 1.8rem;
  color: var(--dchat-accent);
  text-shadow: 0 0 16px rgba(167, 139, 250, 0.5);
  animation: dchat-twinkle 3.2s ease-in-out infinite;
}
.dchat-empty p { font-size: 0.82rem; }
.dchat-empty-sub { color: var(--dchat-accent); font-weight: 600; }

/* ── Jump-to-latest pill ── */
.dchat-jump {
  position: absolute;
  left: 50%;
  bottom: 76px;
  transform: translateX(-50%);
  background: linear-gradient(135deg, var(--dchat-accent), var(--dchat-accent2));
  color: #1a1025;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 16px;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  z-index: 3;
  user-select: none;
  animation: dchat-rise 0.25s var(--dchat-ease) both;
}
.dchat-jump[hidden] { display: none; }

/* ── Typing indicator ── */
.dchat-typing {
  flex-shrink: 0;
  min-height: 0;
  height: 0;
  opacity: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 16px;
  font-size: 0.72rem;
  color: var(--dchat-dim);
  font-style: italic;
  transition: height 0.2s var(--dchat-ease), opacity 0.2s var(--dchat-ease);
}
.dchat-typing.show {
  height: 20px;
  opacity: 1;
}
.dchat-typing-dots {
  display: inline-flex;
  gap: 3px;
}
.dchat-typing-dots i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--dchat-accent);
  display: inline-block;
  animation: dchat-bounce 1.2s infinite ease-in-out;
}
.dchat-typing-dots i:nth-child(2) { animation-delay: 0.18s; }
.dchat-typing-dots i:nth-child(3) { animation-delay: 0.36s; }

/* ── Composer ── */
.dchat-compose {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--dchat-border);
  background: var(--dchat-tint);
}
.dchat-input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--dchat-border);
  background: var(--dchat-bg);
  color: var(--dchat-fg);
  border-radius: 12px;
  padding: 9px 12px;
  font-family: inherit;
  font-size: 0.84rem;
  line-height: 1.4;
  max-height: 140px;
  overflow-y: auto;
  transition: border-color 0.15s var(--dchat-ease),
              box-shadow 0.15s var(--dchat-ease);
}
.dchat-input::placeholder { color: var(--dchat-dim); }
.dchat-input:focus {
  outline: none;
  border-color: var(--dchat-accent);
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.18);
}
.dchat-send {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, var(--dchat-accent), var(--dchat-accent2));
  color: #1a1025;
  font-size: 0.95rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s var(--dchat-ease), filter 0.15s var(--dchat-ease),
              opacity 0.15s var(--dchat-ease);
}
.dchat-send:hover:not(:disabled) { filter: brightness(1.1); transform: translateY(-1px); }
.dchat-send:active:not(:disabled) { transform: translateY(0) scale(0.96); }
.dchat-send:disabled { opacity: 0.4; cursor: default; }

/* ── Animations ── */
@keyframes dchat-rise {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes dchat-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30%           { transform: translateY(-4px); opacity: 1; }
}
@keyframes dchat-twinkle {
  0%, 100% { opacity: 0.6; transform: scale(1); }
  50%      { opacity: 1;   transform: scale(1.12); }
}

/* ═══ Standalone light theme (no [data-theme] forced — follow the OS) ═══ */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) .dchat {
    --dchat-bg:      #ede8f2;
    --dchat-fg:      rgba(30, 15, 50, 0.85);
    --dchat-dim:     rgba(30, 15, 50, 0.50);
    --dchat-accent:  #7c3aed;
    --dchat-accent2: #3b82f6;
    --dchat-border:  rgba(30, 15, 50, 0.10);
    --dchat-bubble:  #ddd6e8;
    --dchat-tint:    rgba(30, 15, 50, 0.03);
  }
  :root:not([data-theme]) .dchat-avatar { color: #fff; }
  :root:not([data-theme]) .dchat-msg.own .dchat-bubble {
    background: linear-gradient(135deg,
      rgba(124, 58, 237, 0.18), rgba(59, 130, 246, 0.14));
    color: rgba(30, 15, 50, 0.9);
  }
  :root:not([data-theme]) .dchat-jump,
  :root:not([data-theme]) .dchat-send { color: #fff; }
}

/* ── Reduced motion (mirrors base.css global rule for our keyframes) ── */
@media (prefers-reduced-motion: reduce) {
  .dchat-msg,
  .dchat-jump,
  .dchat-empty-star,
  .dchat-typing-dots i {
    animation: none !important;
  }
  .dchat-log { scroll-behavior: auto; }
}
