/* Conversation stage, bubbles, markdown, Mermaid */

.stage {
  grid-column: 1;
  grid-row: 1;
  z-index: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--elev-0);
}

@media (min-width: 900px) {
  .stage {
    grid-column: 2;
  }
}

.stage-surface {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.02) 0%, transparent 32%),
    var(--elev-0);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
  scroll-behavior: smooth;
  max-width: 52rem;
  width: 100%;
  margin: 0 auto;
}

.messages:focus-visible {
  outline: none;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: min(92%, 38rem);
  animation: msg-in 0.4s var(--ease-out) both;
}

/* Assistant messages: avatar + bubble side-by-side */
.msg.assistant {
  flex-direction: row;
  align-items: flex-start;
  gap: 0.65rem;
  width: min(92%, 38rem); /* explicit width prevents align-self:flex-start from collapsing the row */
}

.msg-avatar {
  flex-shrink: 0;
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 50%;
  overflow: hidden;
  border: 1.5px solid rgba(232, 160, 136, 0.3);
  box-shadow:
    0 0 0 2px rgba(232, 160, 136, 0.08),
    0 0 12px rgba(232, 160, 136, 0.15),
    0 4px 12px rgba(0, 0, 0, 0.4);
  margin-top: 1.35rem; /* align with bubble text after role-label */
  background: #0c0e14;
}

.msg-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 10%;
  display: block;
}

.msg-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.msg.user {
  align-self: flex-end;
}

.msg.assistant {
  align-self: flex-start;
}

.role-label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--text-faint);
  margin-bottom: 0.4rem;
}

.msg.user .role-label {
  text-align: right;
}

.bubble {
  padding: 0.95rem 1.15rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--stroke);
  font-size: 0.95rem;
  line-height: 1.55;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset;
}

.msg.user .bubble {
  background: var(--elev-2);
  border-bottom-right-radius: var(--radius-sm);
  border-color: var(--stroke-strong);
}

.msg.assistant .bubble {
  background: var(--elev-1);
  border-bottom-left-radius: var(--radius-sm);
}

.msg.assistant .bubble.loading {
  color: var(--text-dim);
  position: relative;
  overflow: hidden;
}

.msg.assistant .bubble.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 40%, rgba(255, 255, 255, 0.04) 50%, transparent 60%);
  animation: sweep 1.2s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
  .msg.assistant .bubble.loading::after {
    animation: none;
  }
}

@keyframes sweep {
  from {
    transform: translateX(-60%);
  }
  to {
    transform: translateX(60%);
  }
}

.bubble strong {
  color: var(--text);
  font-weight: 600;
}
.bubble code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: rgba(0, 0, 0, 0.28);
  padding: 0.1em 0.38em;
  border-radius: 6px;
  border: 1px solid var(--stroke);
}
.bubble pre:not(.thinking-art) {
  overflow-x: auto;
  background: rgba(0, 0, 0, 0.35);
  padding: 0.85rem;
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 0.82rem;
  border: 1px solid var(--stroke);
}

.msg.assistant .bubble .md-body {
  min-width: 0;
}

.msg.assistant .bubble .md-body > :first-child {
  margin-top: 0;
}

.msg.assistant .bubble .md-body > :last-child {
  margin-bottom: 0;
}

.msg.assistant .bubble .md-body :is(p, ul, ol) {
  margin: 0.55rem 0;
}

.msg.assistant .bubble .md-body :is(h1, h2, h3, h4) {
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.3;
  margin: 0.85rem 0 0.5rem;
  color: var(--text);
}

.msg.assistant .bubble .md-body h1 {
  font-size: 1.2rem;
}
.msg.assistant .bubble .md-body h2 {
  font-size: 1.1rem;
}
.msg.assistant .bubble .md-body h3 {
  font-size: 1.02rem;
}
.msg.assistant .bubble .md-body h4 {
  font-size: 0.98rem;
}

.msg.assistant .bubble .md-body h1:first-child,
.msg.assistant .bubble .md-body h2:first-child,
.msg.assistant .bubble .md-body h3:first-child {
  margin-top: 0;
}

.msg.assistant .bubble .md-body ul,
.msg.assistant .bubble .md-body ol {
  padding-left: 1.35rem;
}

.msg.assistant .bubble .md-body li {
  margin: 0.3rem 0;
}

.msg.assistant .bubble .md-body li::marker {
  color: var(--accent);
}

.msg.assistant .bubble .md-body blockquote {
  margin: 0.65rem 0;
  padding: 0.45rem 0 0.45rem 1rem;
  border-left: 3px solid rgba(126, 200, 227, 0.45);
  color: var(--text-dim);
}

.msg.assistant .bubble .md-body hr {
  border: none;
  border-top: 1px solid var(--stroke);
  margin: 1rem 0;
}

.msg.assistant .bubble .md-body a {
  color: var(--accent2);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg.assistant .bubble .md-body a:hover {
  color: var(--accent);
}

.msg.assistant .bubble .md-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  margin: 0.65rem 0;
}

.msg.assistant .bubble .md-body th,
.msg.assistant .bubble .md-body td {
  border: 1px solid var(--stroke);
  padding: 0.4rem 0.55rem;
  text-align: left;
}

.msg.assistant .bubble .md-body th {
  background: rgba(0, 0, 0, 0.2);
  font-weight: 600;
}

.msg.assistant .bubble .md-body pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: inherit;
}

.msg.assistant .bubble .md-body img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-sm);
}

.msg.assistant .bubble .mermaid-wrap {
  position: relative;
  margin: 0.85rem 0;
  padding: 0.65rem 0.75rem;
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--stroke);
  background: rgba(0, 0, 0, 0.28);
  cursor: zoom-in;
}

/* ── Thinking / loading animation ───────────────────────────────────── */

.thinking-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.55rem;
  padding: 0.05rem 0;
}

.thinking-art {
  margin: 0;
  padding: 0;
  font-family: var(--mono);
  font-size: 0.88rem;
  line-height: 1.35;
  color: var(--accent);
  white-space: pre;
  user-select: none;
  letter-spacing: 0.05em;
  text-shadow: 0 0 8px rgba(232, 160, 136, 0.5);
}

.thinking-label {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--text-faint);
  opacity: 0;
  transform: translateY(4px);
  transition:
    opacity 0.3s ease,
    transform 0.3s ease;
  letter-spacing: 0.01em;
  min-height: 1em;
}

.thinking-label.visible {
  opacity: 1;
  transform: translateY(0);
}

/* Portrait avatar glows amber while Sachi is thinking */
.msg-avatar.thinking {
  animation: avatar-thinking 1.8s ease-in-out infinite;
}

@keyframes avatar-thinking {
  0%, 100% {
    box-shadow:
      0 0 0 2px rgba(232, 160, 136, 0.08),
      0 0 12px rgba(232, 160, 136, 0.15),
      0 4px 12px rgba(0, 0, 0, 0.4);
  }
  50% {
    box-shadow:
      0 0 0 3px rgba(232, 160, 136, 0.25),
      0 0 22px rgba(232, 160, 136, 0.45),
      0 4px 18px rgba(0, 0, 0, 0.5);
  }
}

@media (prefers-reduced-motion: reduce) {
  .thinking-art {
    animation: none;
  }
  .msg-avatar.thinking {
    animation: none;
  }
  .thinking-label {
    transition: none;
  }
}

/* ── End thinking animation ──────────────────────────────────────────── */

.msg.assistant .bubble .mermaid-wrap .mermaid {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 3rem;
}

.msg.assistant .bubble .mermaid-wrap svg {
  max-width: 100%;
  height: auto;
  min-height: 120px;
}

/* Expand button — visible on hover */
.mermaid-expand-btn {
  position: absolute;
  top: 0.4rem;
  right: 0.5rem;
  padding: 0.18rem 0.5rem;
  font-size: 0.7rem;
  line-height: 1.4;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid var(--stroke);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
  pointer-events: none;
}
.mermaid-wrap:hover .mermaid-expand-btn {
  opacity: 1;
  pointer-events: auto;
}
.mermaid-expand-btn:hover {
  background: rgba(255, 255, 255, 0.14);
  color: var(--text);
}

/* Lightbox overlay — click the dark backdrop to close */
.mermaid-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  animation: mermaid-lb-in 0.15s ease;
}
.mermaid-lightbox.hidden {
  display: none;
}
@keyframes mermaid-lb-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Wrapper is a flex column: close button row on top, diagram panel below.
   The close button is in normal document flow — the SVG can never cover it. */
.mermaid-lb-wrapper {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.4rem;
  cursor: default;
  width: 88vw;
}

.mermaid-lightbox-close {
  /* Normal-flow button — sits above the panel in the flex column */
  background: rgba(40, 40, 60, 0.95);
  border: 1px solid var(--stroke);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.3rem 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
.mermaid-lightbox-close:hover {
  background: rgba(70, 70, 100, 0.95);
  color: var(--text);
}

.mermaid-lightbox-inner {
  background: #141420;
  border: 1px solid var(--stroke);
  border-radius: var(--radius-sm);
  padding: 1.5rem;
  width: 88vw;
  max-height: 80vh;
  overflow: auto;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.7);
}

.mermaid-lightbox-inner svg {
  display: block;
  width: 100%;
  height: auto;
  min-height: 200px;
}

.mermaid-lb-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
  opacity: 0.6;
  padding-top: 0.2rem;
}
