/* Chat Window and Messages */

.chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  position: relative;
  /* For rope-layer absolute positioning */
}


.chat-scroll {
  grid-area: chat;
  min-height: 0;
  overflow-y: auto;
  /* `visible` is not valid beside `auto` and computes back to `auto`, giving a
     second scrollbar on an axis nothing overflows. `clip` keeps the sideways
     bleed the branch ropes need without one. */
  overflow-x: clip;
  /* Room for the branch ropes, which hang outside the bubbles. */
  overflow-clip-margin: var(--chat-gutter);
  /* Room for the branch ropes, which hang outside the bubbles. */
  padding: var(--s-4) var(--chat-gutter);
  /* Reserve the scrollbar track always, so the transcript's text axis does not
     shift when a short conversation grows long enough to scroll, and so the
     composer below lines up with the messages rather than sitting ~15px wider. */
  scrollbar-gutter: stable;
  /* Firefox. The default bar is a bright grey slab against these dark
     surfaces; a thin translucent thumb reads as part of the transcript. */
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
  scroll-behavior: smooth;
}

.chat-scroll::-webkit-scrollbar {
  width: 10px;
}

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

.chat-scroll::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.16);
  /* Inset by a transparent border so the thumb reads as a slim pill with air
     around it rather than a bar wedged against the edge. */
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: var(--r-pill);
}

.chat-scroll::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
  border: 3px solid transparent;
  background-clip: content-box;
}

/* Empty state */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--s-8) var(--s-4);
  color: var(--ink);
  min-height: 300px;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: var(--s-4);
  opacity: 0.8;
}

.empty-title {
  font-size: var(--text-xl);
  font-weight: 600;
  color: var(--ink);
  margin-bottom: var(--s-3);
}

.empty-body {
  font-size: var(--text-base);
  color: var(--ink-soft);
  margin-bottom: var(--s-6);
  max-width: 400px;
  line-height: 1.6;
}

.auto-start-button {
  background-color: var(--teal);
  color: white;
  border: none;
  border-radius: var(--r-m);
  padding: var(--s-2) var(--s-4);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  margin-bottom: var(--s-4);
  transition: background-color var(--dur-fast) var(--ease-out);
}

.auto-start-button:hover {
  background-color: var(--coral);
}

.auto-start-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.prompt-list {
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  width: 100%;
  max-width: 320px;
}

.prompt-item {
  background: var(--teal);
  border: 1px solid var(--teal);
  border-radius: var(--r-m);
  padding: var(--s-3) var(--s-4);
  font-size: var(--text-sm);
  font-weight: 500;
  color: white;
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease-out);
  box-shadow: var(--shadow-button);
}

.prompt-item:hover {
  background: var(--coral);
  border-color: var(--coral);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}

.prompt-item:active {
  transform: translateY(0);
}

/* Messages list */
.messages-list {
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
  padding: var(--s-2) 0;
  /* Holds the conversation to a readable measure and centres it in the canvas.
     The cap lives here rather than on .chat-canvas so the scroll container
     still spans the window and its scrollbar stays at the right edge. */
  width: 100%;
  max-width: var(--container-max);
  margin: 0 auto;
}

/* Individual message wrapper */
.message {
  display: flex;
  align-items: flex-end;
  gap: var(--s-3);
  margin: var(--s-2) 0;
  animation: message-enter var(--dur-med) var(--ease-emph);
}

@keyframes message-enter {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.message--user {
  flex-direction: row-reverse;
}

/* Message grouping - consecutive messages from same sender */
.message--grouped {
  margin-top: var(--s-1);
}

.message--grouped .avatar {
  visibility: hidden;
  /* Keep space but hide avatar */
}

/* Avatar */
.avatar {
  width: var(--avatar-size);
  height: var(--avatar-size);
  border-radius: 50%;
  background: var(--surface-muted);
  box-shadow: var(--shadow-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-soft);
  flex-shrink: 0;
  overflow: hidden;
}

.avatar--bot {
  background: linear-gradient(135deg, var(--coral) 0%, var(--yellow) 100%);
  color: white;
}

.avatar--user {
  background: linear-gradient(135deg, var(--teal) 0%, #4ECDC4 100%);
  color: white;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Message bubble */
.bubble {
  max-width: var(--bubble-max-width);
  padding: var(--s-4) var(--s-5);
  border-radius: var(--r-l);
  position: relative;
  font-size: var(--text-lg);
  line-height: 1.5;
  word-wrap: break-word;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  /* No backdrop-filter: even blur(0) forces a backdrop root and its own
     compositing layer on every bubble, for no visual effect. */
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 10;
  /* Above Aurora */
}

.bubble--bot {
  background: var(--surface);
  /* Solid Surface */
  color: var(--ink);
  border-bottom-left-radius: var(--r-s);
  /* The coloured edge identifies the speaker; a coloured halo on every bubble
     makes the transcript vibrate without adding information. */
  border-left: 3px solid var(--coral);
}

.bubble--user {
  background: var(--surface);
  /* Solid Surface (Dark Card) */
  color: var(--ink);
  border-bottom-right-radius: var(--r-s);
  border-right: 3px solid var(--teal);
}

/* Message header with author and actions */
.message-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--s-1);
  gap: var(--s-3);
}

.message-author {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.7;
}

.message-content {
  margin: 0;
}

/* Replay button */
.replay-button {
  background: transparent;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: var(--r-s);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-sm);
  opacity: 0.6;
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease-out);
}

.replay-button:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.replay-button:active {
  transform: scale(0.95);
}

/* Message timestamp */
.message-time {
  font-size: var(--text-xs);
  color: var(--ink-soft);
  opacity: 0.6;
  margin-top: var(--s-2);
  text-align: right;
}

.message--user .message-time {
  text-align: left;
}

/* Typing indicator */
.loading-indicator {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-4);
  margin-top: var(--s-3);
}

.typing {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  background: var(--coral-tint);
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-m);
  border-bottom-left-radius: var(--r-s);
}

.typing-dots {
  display: flex;
  gap: 3px;
  align-items: center;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--coral);
  opacity: 0.4;
  animation: typing-bounce 1.4s infinite ease-in-out;
}

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

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

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

@keyframes typing-bounce {

  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }

  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.loading-text {
  font-size: var(--text-sm);
  color: var(--ink-soft);
  font-style: italic;
}

/* Responsive adjustments */
/* The gutters exist to give the branch ropes room; on a narrow screen they cost
   more than the ropes are worth. Shrinking the token keeps the composer, which
   uses the same value, aligned with the transcript. */
@media (max-width: 900px) {
  :root {
    --chat-gutter: var(--s-2);
    /* The rope reaches --rope-descent-inset outside the bubble, so on a narrow
       screen it has to shorten or it pushes past the transcript. 52px shrank a
       108-unit-wide drawing to a stub; 76px is the widest that still lands both
       anchors fully on a 390px screen (agent edge at 0, user edge at 379) and
       draws the trunk, hub and endpoint at full strength. */
    --rope-width: 76px;
  }
}

@media (max-width: 480px) {
  .bubble {
    max-width: 85%;
    padding: var(--s-3) var(--s-4);
    font-size: var(--text-base);
  }

  .avatar {
    width: 32px;
    height: 32px;
  }

  .message {
    gap: var(--s-2);
  }

  .empty {
    padding: var(--s-6) var(--s-3);
  }

  .prompt-list {
    max-width: 100%;
  }
}

/* Message delete button */
.message-delete-button {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  border: none;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  color: var(--ink-soft);
  /* A delete affordance on every message at rest is visual noise on a screen
     whose subject is the text. Revealed on hover, and on focus so it stays
     reachable by keyboard. */
  opacity: 0;
  cursor: pointer;
  transition: opacity var(--dur-fast) var(--ease-out);
  padding: 0;
}

.bubble:hover .message-delete-button,
.message-delete-button:focus-visible {
  opacity: 0.6;
}

.message-delete-button:hover {
  opacity: 1;
}

/* Message undo notification */
.message-undo-notification {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--ink-faint);
  font-size: 12px;
  color: var(--ink-soft);
}

.message-undo-notification .undo-button {
  padding: 4px 10px;
  font-size: 12px;
  background: transparent;
  border: 1px solid var(--teal);
  color: var(--teal);
  border-radius: var(--r-s);
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease-out);
}

.message-undo-notification .undo-button:hover {
  background: var(--teal);
  color: white;
}

/* Branch point button - displayed under messages */

/* Rope Row: Flow-based container between messages for overflow visibility */
/* A message and its branch control share one stacking context, so the rope is
   attached to the bubble instead of occupying a spacer row of its own. The old
   .rope-row was a full-width 60px band between messages holding a -80px-offset
   SVG, which left the rope hanging in blank space away from any bubble and put
   a large vertical gap after every message. */
.message-slot {
  position: relative;
  width: 100%;
  /* Room for the rope that hangs below the bubble; must equal the rope's own
     height so the rope starts flush with the bubble's bottom edge. */
  padding-bottom: var(--rope-drop);
}

.rope-anchor {
  position: absolute;
  /* Starts exactly at the bubble's bottom edge. The slot reserves --rope-drop
     of bottom padding for the rope, so the message's own bottom is that far
     above the slot's; offsetting by it keeps the rope attached to the bubble
     instead of floating below the reserved band. The SVG trunk starts at y=0,
     so the rope grows directly out of the message. */
  bottom: 0;
  width: var(--rope-width);
  height: var(--rope-drop);
  overflow: visible;
  display: flex;
  align-items: stretch;
  pointer-events: none;
  /* Always visible: the rope is the conversation's branch structure, not a
     hover affordance. Resting state is dimmed so a long transcript stays calm;
     hover and focus bring it to full strength. */
  opacity: 0.55;
  transition: opacity var(--dur-fast) var(--ease-out);
}

/* The rope branches outward, into the margin beside the transcript. Its trunk
   descends near the box's trailing edge, so the box is placed outside the
   bubble: the descent meets the bubble's edge and the forks open away from the
   conversation. --rope-descent-inset is where the trunk sits inside the box. */
.rope-anchor--agent {
  /* --avatar-lane puts the descent at the bubble's edge (the bubble starts one
     avatar-lane in from the message row); the negative inset then carries the
     rest of the rope outward past the avatar into the margin. */
  left: calc(var(--avatar-lane) - var(--rope-descent-inset));
  justify-content: flex-start;
}

.rope-anchor--user {
  left: auto;
  right: calc(var(--avatar-lane) - var(--rope-descent-inset));
  justify-content: flex-end;
}

/* A branched message keeps its rope at full strength: it is showing structure
   that exists, not offering an action. */
.rope-anchor--branched,
.message-slot:hover .rope-anchor,
.message-slot:focus-within .rope-anchor {
  opacity: 1;
}

.neon-rope-container {
  position: relative;
  width: var(--rope-width);
  height: var(--rope-drop);
  cursor: pointer;
  pointer-events: auto;
  background: transparent;
  border: none;
  padding: 0;
  margin: 0;
  overflow: visible;
}

.neon-rope-container:focus-visible {
  outline: var(--focus-ring);
  outline-offset: var(--focus-offset);
  border-radius: var(--r-s);
}

.neon-rope-svg {
  width: 100%;
  height: 100%;
  overflow: visible;
  transition: filter 0.4s ease-in-out;
}

/* Hover Interaction: "Energize" the circuit */
.neon-rope-container:hover .neon-rope-svg {
  /* Brighten the whole assembly */
  filter: drop-shadow(0 0 5px rgba(0, 255, 255, 0.4));
}

.rope-glow {
  transition: all 0.4s ease-in-out;
}

/* One drop-shadow on the whole assembly replaces the per-rope SVG filter
   chain (feGaussianBlur -> feColorMatrix -> feMerge) that was rebuilt and
   re-rasterised for every message on screen. */
.neon-rope-svg {
  filter: drop-shadow(0 0 2px rgba(0, 255, 255, 0.35));
}

.rope-core {
  transition: all 0.4s ease-in-out;
}

.rope-node {
  transition: all 0.4s ease-in-out;
}


/* Heat up the core on hover. Widths are viewBox user units (0 0 72 40). */
.neon-rope-container:hover .rope-core,
.neon-rope-container:focus-visible .rope-core {
  stroke-width: 2;
  /* Slight thickening */
  filter: drop-shadow(0 0 2px white);
}

/* Intensify the glow path */
.neon-rope-container:hover .rope-glow,
.neon-rope-container:focus-visible .rope-glow {
  opacity: 1;
  stroke-width: 4;
}

/* Nodes pulse slightly on hover */
.neon-rope-container:hover .rope-node,
.neon-rope-container:focus-visible .rope-node {
  r: 3.5;
  /* Grow slightly */
  fill: white;
  /* "Hot" nodes */
}

/* Forking Logic */
.neon-rope-forkable {
  position: relative;
}

/* Reveal forks */
.rope-fork {
  opacity: 0;
  transition: opacity 0.4s ease-in-out, transform 0.5s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  /* Origin at the Hub (34, 24) in the 108x44 viewBox */
  transform-origin: 34px 24px;
  transform: scale(0);
}

.neon-rope-container:hover .rope-fork {
  opacity: 1;
  transform: scale(1);
}

/* Specific delays for "sequential" deployment feel */
.neon-rope-container:hover .rope-fork-up {
  transition-delay: 0.15s;
}

.neon-rope-container:hover .rope-fork-down {
  transition-delay: 0.3s;
}

/* Continue branch button */
.continue-branch-container {
  display: flex;
  justify-content: center;
  padding: var(--s-3);
}

.continue-button {
  background-color: var(--slate-600, #475569);
  color: white;
  border: none;
  border-radius: var(--r-m);
  padding: var(--s-2) var(--s-4);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}

.continue-button:hover {
  background-color: var(--slate-700, #334155);
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {

  .message,
  .typing-dot,
  .replay-button {
    animation: none !important;
    transition: none !important;
  }

  .prompt-item:hover {
    transform: none;
  }

  .neon-rope-container:hover .neon-rope-svg {
    transform: none;
  }
}

/* Language-specific fonts */
.message-content:lang(ar) {
  direction: rtl;
  text-align: right;
  font-size: 1.15em;
  line-height: 1.8;
}

/* Arabic Naskh font */
.arabic-naskh {
  font-family: "Times New Roman", "Noto Naskh Arabic", serif;
}

/* Arabic Ruq'a font */
.arabic-ruqa {
  font-family: 'Aref Ruqaa', serif;
}

/* Arabic Latin - uses default fonts */
.arabic-latin {
  /* default font */
}

/* Japanese ruby text support */
ruby {
  ruby-position: over;
}

rt {
  font-size: 0.5em;
}

/* Message action buttons */
.message-actions {
  display: flex;
  gap: var(--s-2);
  margin-top: var(--s-2);
}

.action-button {
  font-size: var(--text-xs);
  padding: var(--s-1) var(--s-2);
  border-radius: var(--r-s);
  border: 1px solid var(--ink-faint);
  background-color: var(--surface-muted);
  color: var(--ink-soft);
  cursor: pointer;
  transition: all var(--dur-fast) var(--ease-out);
}

.action-button:hover {
  background-color: var(--surface);
  border-color: var(--ink-soft);
}

.explain-button:hover {
  background-color: var(--teal-tint);
  border-color: var(--teal);
  color: var(--teal);
}

.translate-button:hover {
  background-color: var(--coral-tint);
  border-color: var(--coral);
  color: var(--coral);
}

/* Translate selection button */
.translate-selection-btn {
  background-color: #4a90e2;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  font-weight: 500;
}

.translate-selection-btn:hover {
  background-color: #357abd;
}

.translate-selection-btn:active {
  background-color: #2868a8;
}

.translate-selection-btn:disabled {
  background-color: #8fb9dd;
  cursor: not-allowed;
  opacity: 0.7;
}