/* ── Meridian — AI Chat ────────────────────────────────────────────────────
   Floating chat surface. The FAB button styles live in src/styles/tokens.css
   (.meridian-fab / .meridian-fab-icon / .meridian-fab-dot), ported from the
   client mockup. This file styles the chat window, the streamed assistant
   message formatting, and the conversation-history overlay.

   meridian.js wires this to the live backend (POST /api/meridian SSE stream +
   the thread endpoints); see design/MERIDIAN_SPEC.md §Phase 15A.
   All classes are prefixed .mdr- to avoid collisions with existing styles.
   ------------------------------------------------------------------------- */

/* Hide Meridian on the login / onboarding screen, show it once authenticated.
   Gating is driven by `body.meridian-authed`, toggled in meridian.js by
   observing the auth overlay's *computed* visibility. We can't rely on a single
   class here: auth.js dismisses #authOverlay two different ways — onAuthSuccess()
   adds .hidden, but checkAuth() (the persisted-session reload path) only sets
   style.display:none with NO .hidden class. The earlier `:has(#authOverlay:not(.hidden))`
   rule missed that second path, so the FAB stayed hidden after a reload.
   (MERIDIAN_SPEC.md §10) */
.meridian-fab { display: none; }
body.meridian-authed .meridian-fab { display: flex; }
body:not(.meridian-authed) #meridianChat { display: none !important; }

/* Chat window shell */
#meridianChat {
  display: none;
  position: fixed;
  bottom: 96px;
  right: 28px;
  width: min(420px, calc(100vw - 56px));
  height: min(620px, calc(100vh - 120px));
  z-index: 9995;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  flex-direction: column;
  font-family: var(--font-body);
  background: var(--surface, #fefcf9);
}
#meridianChat.open { display: flex; }

/* Header */
.mdr-chat-header {
  padding: 16px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  background: linear-gradient(135deg, #0d1f14, #1a3d2b);
  border-radius: 18px 18px 0 0;
}
.mdr-chat-logomark {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.mdr-chat-title { font-size: 14px; font-weight: 800; color: #fff; letter-spacing: -0.3px; }
.mdr-chat-subtitle {
  font-size: 9px;
  color: rgba(255, 255, 255, 0.4);
  font-family: var(--font-mono);
  letter-spacing: 0.8px;
  text-transform: uppercase;
  margin-top: 2px;
}
.mdr-chat-close {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.45);
  transition: all 150ms;
  font-size: 14px;
  flex-shrink: 0;
  line-height: 1;
}
.mdr-chat-close:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

/* Messages */
.mdr-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  min-height: 0;
}
.mdr-msg { display: flex; gap: 8px; align-items: flex-start; animation: mdrFadeIn 220ms ease both; }
.mdr-msg.user { flex-direction: row-reverse; }
.mdr-avatar {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, #1a3d2b, #2d6a4f);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}
.mdr-avatar-user {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--surface-2);
  border: 1px solid var(--border-med);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 700;
  color: var(--text-2);
  margin-top: 2px;
}
.mdr-bubble {
  max-width: 84%;
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.mdr-msg.user .mdr-bubble {
  background: #1a3d2b;
  color: #e8f0ea;
  border-color: transparent;
  border-radius: 12px 12px 3px 12px;
}
.mdr-msg.ai .mdr-bubble { border-radius: 3px 12px 12px 12px; }
.mdr-bubble-label {
  font-family: var(--font-mono);
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--green);
  margin-bottom: 5px;
  opacity: 0.8;
}
.mdr-bubble strong { font-weight: 700; }

/* Typing indicator */
.mdr-typing { display: flex; gap: 4px; align-items: center; padding: 12px 14px; }
.mdr-typing span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-3);
  animation: mdrDot 1.2s ease-in-out infinite;
}
.mdr-typing span:nth-child(2) { animation-delay: 0.2s; }
.mdr-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes mdrDot {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}
@keyframes mdrFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Suggested prompts */
.mdr-prompts {
  padding: 0 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex-shrink: 0;
  transition: opacity 200ms ease;
}
.mdr-prompt-label {
  font-family: var(--font-mono);
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 3px;
}
.mdr-prompt-btn {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 12px;
  color: var(--text-2);
  text-align: left;
  transition: all 150ms;
  font-family: var(--font-body);
  line-height: 1.3;
}
.mdr-prompt-btn:hover { background: var(--green-dim); border-color: rgba(45, 106, 79, 0.2); color: var(--text); }
.mdr-prompt-icon { font-size: 13px; flex-shrink: 0; opacity: 0.7; }

/* Input */
.mdr-input-area {
  padding: 12px 14px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  background: var(--surface);
}
.mdr-input {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--border-med);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
  outline: none;
  transition: border-color 150ms ease;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.5;
}
.mdr-input:focus { border-color: rgba(45, 106, 79, 0.45); }
.mdr-input::placeholder { color: var(--text-3); }
.mdr-send {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: #1a3d2b;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 150ms ease;
  color: #a8d5b5;
}
.mdr-send:hover { background: var(--green); color: #fff; box-shadow: 0 2px 10px rgba(45, 106, 79, 0.3); }
.mdr-send:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }

/* Header action buttons (new chat / history) */
.mdr-chat-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.mdr-icon-btn {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.55);
  transition: all 150ms;
  flex-shrink: 0;
}
.mdr-icon-btn:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

/* Assistant message formatting (mdrFormat output) */
.mdr-content > p { margin: 0 0 8px; }
.mdr-content > p:last-child { margin-bottom: 0; }
.mdr-list { margin: 0 0 8px; padding-left: 18px; }
.mdr-list:last-child { margin-bottom: 0; }
.mdr-list li { margin: 2px 0; }
.mdr-code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  background: var(--green-dim);
  color: var(--text);
  padding: 1px 5px;
  border-radius: 5px;
}

/* Conversation history overlay */
.mdr-history {
  position: absolute;
  top: 61px;            /* below the header */
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface, #fefcf9);
  z-index: 5;
  display: none;
  flex-direction: column;
  animation: mdrFadeIn 160ms ease both;
}
.mdr-history.open { display: flex; }
.mdr-history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.mdr-history-heading {
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-3);
}
.mdr-history-new {
  border: 1px solid var(--border-med);
  background: var(--surface);
  color: var(--text-2);
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 150ms;
}
.mdr-history-new:hover { background: var(--green-dim); border-color: rgba(45, 106, 79, 0.2); color: var(--text); }
.mdr-history-list { flex: 1; overflow-y: auto; padding: 8px; min-height: 0; }
.mdr-history-empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--text-3);
  font-size: 12px;
}
.mdr-history-item {
  display: flex;
  align-items: stretch;
  gap: 4px;
  border-radius: 9px;
  margin-bottom: 3px;
  transition: background 150ms;
}
.mdr-history-item:hover { background: var(--surface-2); }
.mdr-history-item.active { background: var(--green-dim); }
.mdr-history-open {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-start;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  padding: 9px 10px;
  min-width: 0;
}
.mdr-history-title {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.mdr-history-meta {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--text-3);
  letter-spacing: 0.4px;
}
.mdr-history-del {
  width: 30px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-3);
  font-size: 12px;
  border-radius: 8px;
  flex-shrink: 0;
  opacity: 0;
  transition: all 150ms;
}
.mdr-history-item:hover .mdr-history-del { opacity: 1; }
.mdr-history-del:hover { background: var(--border-med); color: var(--text); }

/* Mobile: full-width window */
@media (max-width: 520px) {
  #meridianChat {
    right: 12px;
    left: 12px;
    bottom: 12px;
    width: auto;
    height: min(70vh, 560px);
  }
}
