/* ==========================================================================
   EntchenChat — Design System
   Dark theme is the default; [data-theme="light"] on <html> overrides the
   palette (persisted per device in localStorage, see app-main.js).
   ========================================================================== */

/* Loading skeleton */
#loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-color);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}
#loading-overlay.hidden {
  opacity: 0;
  visibility: hidden;
}
.skeleton-loader {
  max-width: 90%;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.skeleton-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.skeleton-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--border-color);
  animation: shimmer 2s ease-in-out infinite;
}
.skeleton-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.skeleton-input {
  height: 20px;
  width: 100%;
  border-radius: 6px;
  background: var(--border-color);
  animation: shimmer 2s ease-in-out infinite;
}
.skeleton-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 1rem;
}
.skeleton-avatar-small {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--border-color);
  animation: shimmer 2s ease-in-out infinite;
}
.skeleton-button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--border-color);
  animation: shimmer 2s ease-in-out infinite;
}
@keyframes shimmer {
  background: linear-gradient(90deg, var(--border-color) 25%, var(--accent-soft) 50%, var(--border-color) 75%);
  background-size: 200% 100%;
  animation: shimmer 2s ease-in-out infinite;
}
/* Footer skeleton buttons are narrower */

:root {
  --bg-color: #0f172a;
  --card-bg: #1a2332;
  --text-color: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-soft: rgba(59, 130, 246, 0.18);
  --bubble-self: #2563eb;
  --bubble-other: #334155;
  --border-color: #334155;
  --surface-hover: #334155;
  --code-bg: #0f172a;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  /* Smooth theme transition support */
  --transition-theme: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
}

[data-theme="light"] {
  --bg-color: #f1f5f9;
  --card-bg: #ffffff;
  --text-color: #0f172a;
  --text-muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --bubble-self: #2563eb;
  --bubble-other: #e2e8f0;
  --border-color: #dbe2ea;
  --surface-hover: #e8edf3;
  --code-bg: #eef2f7;
  --success: #059669;
  --danger: #dc2626;
  --warning: #d97706;
}

/* Non-blocking toast notifications */
#toast-container {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom, 0px));
  right: calc(20px + env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 2000;
  pointer-events: none;
  max-width: min(360px, calc(100vw - 40px));
}
#toast-container .toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 10px;
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  color: var(--text-color);
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  font-size: 0.9rem;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  cursor: pointer;
}
#toast-container .toast.show {
  opacity: 1;
  transform: translateY(0);
}
#toast-container .toast.error {
  border-color: var(--danger);
}
#toast-container .toast.error .toast-icon { color: var(--danger); }
#toast-container .toast.success {
  border-color: var(--success);
}
#toast-container .toast.success .toast-icon { color: var(--success); }
#toast-container .toast.warning {
  border-color: var(--warning);
}
#toast-container .toast.warning .toast-icon { color: var(--warning); }
#toast-container .toast-msg {
  overflow-wrap: anywhere;
}
/* Toast progress bar: shows completion percentage for long-running actions */
#toast-container .toast-progress {
  height: 3px;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 2px;
  margin-top: 6px;
  overflow: hidden;
}
#toast-container .toast-progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.2s ease;
}
[data-theme="light"] #toast-container .toast-progress {
  background: rgba(0, 0, 0, 0.08);
}
/* Toast action button (Discord-style "Rückgängig" on the delete toast): a
   distinct interactive control that dismisses the toast AND runs its callback
   (toast-body click = dismiss only). Reuses the accent so it reads as the
   primary choice inside the toast. */
.toast-action {
  flex-shrink: 0;
  align-self: center;
  padding: 4px 12px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.toast-action:hover {
  background: var(--accent);
  color: #fff;
}
[data-theme="light"] .toast-action:hover {
  color: #fff;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-color);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  /* Dynamic viewport height: mobile browsers reserve space for the dynamic
     URL bar and (with interactive-widget=resizes-content) the soft keyboard
     shrinks the layout, so the composer stays reachable instead of hiding
     behind it. 100vh first = fallback for browsers without dvh support. */
  height: 100dvh;
  overflow: hidden;
  transition: var(--transition-theme);
}

.chat-container {
  width: 100%;
  max-width: 800px;
  height: 100vh;
  height: 100dvh;
  background-color: var(--card-bg);
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}

@media (min-width: 768px) {
  .chat-container {
    height: 90vh;
    height: 90dvh;
    border-radius: 12px;
  }
}

.chat-header {
  padding: 16px 20px;
  background-color: var(--bg-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  transition: var(--transition-theme);
}

.chat-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
}

.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 12px;
  word-break: break-word;
  animation: fadeIn 0.2s ease;
}

/* Grouped consecutive messages (same author, within 5 min): only the run's
   head shows the username; inner corners shrink so consecutive bubbles fuse
   into one visual block, and the flex gap is pulled tighter via a negative
   margin (12px gap - 7px = 5px visual seam). */
.message.grouped {
  margin-top: -7px;
}
.message.group-head {
  border-radius: 12px 12px 4px 4px;
}
.message.group-mid {
  border-radius: 4px;
}
.message.group-end {
  border-radius: 4px 4px 12px 12px;
}
.message.grouped .message-username {
  display: none;
}
.message.grouped .message-meta {
  margin-bottom: 2px;
}

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

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

.pulse {
  animation: pulse 0.5s ease;
}

.message.search-hit {
  cursor: pointer;
  outline: 1px solid transparent;
  transition: outline-color 0.15s ease, filter 0.15s ease;
}

.message.search-hit:hover {
  outline-color: var(--accent);
}

.message.highlight {
  animation: highlightFade 2s ease;
  outline: 2px solid var(--accent);
}

/* Search-term highlight inside search results (<mark> injected by
   renderMessageText when a search query is active) */
mark.search-hit-mark {
  background: var(--accent);
  color: #fff;
  border-radius: 3px;
  padding: 0 1px;
}

@keyframes highlightFade {
  0% { filter: brightness(1.45); }
  60% { filter: brightness(1.2); }
  100% { filter: brightness(1); }
}

/* Load-older pagination control at the top of the message list */
#btn-load-older {
  align-self: center;
  margin: 0.4rem 0 0.6rem;
  padding: 0.35rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--card-bg);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, opacity 0.15s ease;
}

#btn-load-older:hover:not(:disabled) {
  background: var(--surface-hover);
  color: var(--text-color);
}

#btn-load-older:disabled {
  opacity: 0.5;
  cursor: wait;
}

.new-messages-btn {
  position: fixed;
  bottom: 5.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.2rem;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--card-bg);
  color: var(--text-color);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  transition: opacity 0.2s ease, transform 0.2s ease, background 0.15s ease;
  opacity: 0.92;
}

.new-messages-btn:hover {
  opacity: 1;
  background: var(--surface-hover);
  transform: translateX(-50%) scale(1.03);
}

.new-messages-btn:active {
  transform: translateX(-50%) scale(0.97);
}

/* "Back to latest" bar (archive view exit affordance — same family as the
   new-messages button, but docked bottom-left so the two can coexist while
   both are relevant: stranded in an archive AND scrolled away). */
.back-to-latest-bar {
  position: fixed;
  bottom: 5.5rem;
  left: 1rem;
  z-index: 100;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  border: 1px solid #4f80f7;
  background: linear-gradient(180deg, rgba(79, 128, 247, 0.18), rgba(79, 128, 247, 0.10)), var(--card-bg);
  color: var(--text-color);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  transition: opacity 0.2s ease, transform 0.2s ease, background 0.15s ease;
  opacity: 0.95;
}

.back-to-latest-bar:hover {
  opacity: 1;
  transform: translateY(-2px);
}

.back-to-latest-bar:active {
  transform: translateY(0);
}

.back-to-latest-bar svg {
  color: #7ea2ff;
  flex: 0 0 auto;
}

.back-to-latest-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 700px) {
  .back-to-latest-bar {
    bottom: 6.5rem; /* above the mobile composer + safe-area inset */
    left: 0.75rem;
    right: auto;
    max-width: calc(100vw - 1.5rem);
    font-size: 0.75rem;
    padding: 0.5rem 0.85rem;
  }
}

.message.other {
  background-color: var(--bubble-other);
  align-self: flex-start;
}

.message.self {
  background-color: var(--bubble-self);
  align-self: flex-end;
  /* Own messages always sit on the blue bubble in both themes */
  color: #ffffff;
}

/* @mention highlight: readable in dark + light on both bubble colors. A
   translucent white veil works on the dark slate (other) and the saturated
   blue (self) bubbles; the border adds definition. Light theme swaps the
   veil for a soft accent tint on other-people bubbles. */
.mention {
  background: rgba(255, 255, 255, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 4px;
  padding: 0 3px;
  margin: 0 -1px;
  font-weight: 700;
}

.message.self .mention {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.4);
}

[data-theme="light"] .message.other .mention {
  background: rgba(37, 99, 235, 0.1);
  border-color: rgba(37, 99, 235, 0.25);
}

.message-user {
  font-size: 0.75rem;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-muted);
}

.message-text {
  font-size: 0.95rem;
  line-height: 1.4;
}

.chat-form {
  position: relative; /* anchors the reply-context-bar */
  padding: 16px 20px;
  /* iOS notch/home-indicator: keep the composer above the bottom inset on
     full-bleed PWA/tabbed layouts (viewport-fit=cover). env() fallback is
     0px; browsers without env() drop the declaration and keep the base. */
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background-color: var(--bg-color);
  display: flex;
  gap: 10px;
  border-top: 1px solid var(--border-color);
}

.chat-form input,
.chat-form textarea {
  flex: 1;
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  font-size: 1rem;
}

/* Composer wrapper anchors the character counter at the textarea's
   bottom-right corner (overlaid so the flex row layout is untouched). */
.composer-wrap {
  position: relative;
  flex: 1;
  display: flex;
}

.composer-wrap textarea {
  flex: 1;
}

/* Character counter: hidden until the message is long (see app-main.js);
   polite aria-live so screen readers get a gentle heads-up before the cap. */
.char-counter {
  position: absolute;
  right: 10px;
  bottom: 6px;
  font-size: 0.72rem;
  color: var(--text-muted);
  background: color-mix(in srgb, var(--card-bg) 85%, transparent);
  padding: 1px 6px;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
  white-space: nowrap;
}

.char-counter.visible { opacity: 1; }
.char-counter.warn { color: var(--danger); }

/* Multiline composer: grows with content up to ~5 lines, then scrolls.
   Height is managed by app-main.js (auto-resize); resize handle is hidden. */
.chat-form textarea {
  resize: none;
  min-height: 47px;
  max-height: 130px;
  line-height: 1.45;
  font-family: inherit;
  overflow-y: auto;
}

.chat-form input:focus,
.chat-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Offline queue indicator (badge on the send button while disconnected). */
.chat-form button {
  position: relative;
}

.chat-form .queue-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  background: var(--danger);
  color: white;
  font-size: 0.72rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.chat-form button {
  padding: 0 20px;
  background-color: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s;
}

.chat-form button:hover {
  background-color: var(--accent-hover);
}

.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.status-badge {
  font-size: 0.75rem;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface-hover);
  color: var(--text-muted);
}
.status-badge.connecting {
  background: var(--surface-hover);
  color: var(--text-muted);
}
.status-badge.connected {
  background: var(--success);
  color: white;
}
.status-badge.disconnected {
  background: var(--danger);
  color: white;
}
.status-badge.reconnecting {
  background: var(--warning);
  color: white;
  animation: reconnectPulse 1.2s ease-in-out infinite;
}
@keyframes reconnectPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

.header-right button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background-color: var(--card-bg);
  color: var(--text-color);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
}
.header-right button:hover {
  background-color: var(--surface-hover);
}

#search-input {
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  font-size: 0.85rem;
}
#search-input:focus {
  outline: none;
  border-color: var(--accent);
}

.search-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
}

.search-wrapper #search-input {
  flex: 1;
  min-width: 120px;
}

#search-clear {
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  cursor: pointer;
  font-size: 0.85rem;
}

#search-clear:hover {
  background-color: var(--surface-hover);
}

.search-clear-btn {
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background-color: var(--card-bg);
  color: var(--text-color);
  cursor: pointer;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
}

.user-count {
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* --- Live presence chips ------------------------------------------------
   Colored initial chips next to the Online counter, fed by the server's
   deduped 'presence list' broadcast. Same --user-hue mechanism as message
   usernames, so "who is online" matches the colors readers already know. */
.presence-chips {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: nowrap;
  max-width: 40vw;
  overflow: hidden;
}
.presence-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px 2px 3px;
  border-radius: 999px;
  background: hsla(var(--user-hue, 220), 45%, 62%, 0.16);
  border: 1px solid hsla(var(--user-hue, 220), 45%, 62%, 0.35);
  font-size: 0.72rem;
  line-height: 1.4;
  white-space: nowrap;
  cursor: default;
}
.presence-chip .presence-initial {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: hsl(var(--user-hue, 220) 45% 62%);
  color: #0f172a; /* dark ink on the bright chip, legible for every hue */
  font-size: 0.62rem;
  font-weight: 700;
}
.presence-chip .presence-name {
  color: var(--text-muted);
  font-weight: 600;
}
/* Mode icon inside a presence chip (peer's active system prompt). Sized to
   the initial dot; hover title explains the mode. */
.presence-chip .presence-prompt {
  display: inline-flex;
  align-items: center;
  font-size: 0.66rem;
  line-height: 1;
  filter: saturate(0.9);
}
.presence-chip.mine {
  border-color: hsla(var(--user-hue, 220), 60%, 70%, 0.8);
  background: hsla(var(--user-hue, 220), 55%, 65%, 0.28);
}
.presence-chip.more {
  padding: 2px 8px;
  color: var(--text-muted);
  border-style: dashed;
}
/* Click-to-mention: non-own chips act as buttons that drop @Name into the
   composer (Discord member-list behavior). Own chip stays inert. */
.presence-chip.clickable {
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.15s ease;
}
.presence-chip.clickable:hover {
  filter: brightness(1.18);
}
.presence-chip.clickable:active {
  transform: scale(0.96);
}
.presence-chip:focus-visible {
  outline: 2px solid hsl(var(--user-hue, 220) 45% 62%);
  outline-offset: 1px;
}
@media (max-width: 700px) {
  /* Small screens: initials only — names need too much header width. */
  .presence-chip .presence-name { display: none; }
  .presence-chip { padding: 2px 3px; }
  .presence-chips { max-width: 120px; }
}

.message-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  gap: 12px;
}
.message-username {
  font-size: 0.75rem;
  font-weight: 600;
  /* Per-user consistent color: --user-hue is set per message element by
     app-main.js (djb2 hash of the display name, hue 15–345). hsl() with a
     fixed lightness reads on both themes; fallback keeps the muted tone
     for legacy DOM without the variable. */
  color: hsl(var(--user-hue, 220) 45% 62%);
}
/* Own messages sit on the saturated blue bubble: a near-white tint of the
   personal hue keeps continuity without muddying on blue. */
.message.self .message-username {
  color: hsl(var(--user-hue, 220) 70% 92%);
}
[data-theme="light"] .message.other .message-username {
  /* Slightly deeper lightness on white cards so pale hues stay readable. */
  color: hsl(var(--user-hue, 220) 55% 42%);
}
.message-time {
  font-size: 0.65rem;
  opacity: 0.7;
}

/* Permalink affordance: the timestamp becomes a borderless button that
   looks identical to the old span; hover/keyboard focus reveals a subtle
   underline + link color so the copy action is discoverable without
   adding visual noise. Legacy plain-span timestamps (no id) are untouched. */
.msg-permalink {
  background: transparent;
  border: none;
  padding: 0;
  font: inherit;
  cursor: pointer;
  color: inherit;
  border-radius: 3px;
}
.msg-permalink .message-time {
  font-size: 0.65rem;
  opacity: 0.7;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s, opacity 0.15s;
}
.msg-permalink:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.msg-copy {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.2s;
}
.msg-copy:hover {
  color: var(--text-color);
  border-color: var(--accent);
  background: var(--surface-hover);
}
.msg-copy.copied {
  color: var(--success);
  border-color: var(--success);
}

.msg-delete {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.2s;
}
.msg-delete:hover:not(:disabled) {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--surface-hover);
}
.msg-delete:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* Edit own message: button mirrors the copy/delete affordances */
.msg-edit {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.2s;
}
.msg-edit:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface-hover);
}
/* Inline editor inside the message bubble (Enter saves, Esc cancels) */
.msg-edit-editor {
  margin-top: 6px;
}
.msg-edit-editor textarea {
  width: 100%;
  box-sizing: border-box;
  /* --card-bg, not the never-defined --input-bg: an undefined var made the
     editor background transparent, so the text sat directly on the bubble
     behind it (dark --text-color on the blue self-bubble = 3.45:1, below
     the 4.5:1 minimum). --card-bg is defined in both palettes. */
  background: var(--card-bg);
  color: var(--text-color);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.9rem;
  font-family: inherit;
  resize: vertical;
  min-height: 60px;
}
/* Auto-grow state: content taller than the max height (see JS
   EDIT_EDITOR_MAX_HEIGHT in app-main.js startEditMessage) - the measured
   style.height wins, scrolling takes over, and the manual resize handle is
   removed so it cannot fight the clamp. Short edits keep the default
   resize:vertical handle and never show an artificial scrollbar. */
.msg-edit-editor.scrollable textarea {
  resize: none;
  overflow-y: auto;
}
.msg-edit-editor textarea:focus {
  outline: none;
  border-color: var(--accent-hover);
}
.msg-edit-hint {
  margin-top: 3px;
  font-size: 0.7rem;
  color: var(--text-muted);
}
/* "(bearbeitet)" marker next to the timestamp; as a button it is the
   edit-history affordance (same look, cursor + hover hint) */
.msg-edited {
  font-size: 0.7rem;
  font-style: italic;
  color: var(--text-muted);
  white-space: nowrap;
}
button.msg-edited-history {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--text-muted);
  cursor: pointer;
}
button.msg-edited-history:hover {
  color: var(--accent);
  text-decoration: underline;
}

/* --- Edit-history modal -------------------------------------------------
   Revision list: original text first, then one entry per edit with its
   timestamp. Entries are visually separated; the original row is
   highlighted as the starting point of the chain. */
.edit-history-content {
  max-width: 480px;
  width: min(480px, 92vw);
  max-height: 80vh;
  overflow-y: auto;
}
.edit-history-body {
  margin: 12px 0 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.edit-history-entry {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--bg-color);
}
.edit-history-original {
  border-color: var(--accent);
}
.edit-history-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.edit-history-label {
  font-weight: 600;
  color: var(--accent);
}
.edit-history-time {
  white-space: nowrap;
}
.edit-history-text {
  font-size: 0.9rem;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text-color);
}
.edit-history-loading,
.edit-history-empty,
.edit-history-error {
  font-size: 0.9rem;
  color: var(--text-muted);
  padding: 8px 0;
}
.edit-history-error {
  color: var(--danger);
}

/* --- Reply quoting -------------------------------------------------------
   Quote block between meta row and message text; clicking it jumps to the
   original message (which may no longer exist — the quote keeps rendering
   from its denormalized snapshot). */
.msg-reply-quote {
  margin: 2px 0 4px;
  padding: 4px 10px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 1px;
  max-width: 100%;
  overflow: hidden;
}
.msg-reply-quote:hover,
.msg-reply-quote:focus-visible {
  filter: brightness(1.12);
}
/* Quote kept for its TEXT only: the reference could not be restored (its
   parent was not part of the imported file), so there is nothing to jump to.
   Muting it keeps it readable without inviting a click that cannot work. */
.msg-reply-quote-unlinked {
  cursor: default;
  border-left-style: dotted;
  opacity: 0.85;
}
.msg-reply-quote-unlinked:hover {
  filter: none;
}
.msg-reply-author {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
}
.msg-reply-text {
  font-size: 0.78rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-reply {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.8rem;
  padding: 2px 4px;
  color: var(--text-muted);
  transition: color 0.15s ease;
}
/* Hover-reveal for action buttons lives in the @media (hover: hover) block
   below (kept next to .msg-pin so all affordances share one rule set); on
   hover-less (touch) devices every button simply stays visible. */
.msg-reply:hover {
  color: var(--accent);
}
/* Composer "replying to" bar: shows the quote target above the input. */
.reply-context-bar {
  position: absolute;
  top: -40px;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  z-index: 5;
}
.reply-context-text {
  flex: 1;
  font-size: 0.8rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#reply-context-cancel {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 6px;
}
#reply-context-cancel:hover {
  color: var(--danger);
}
/* Short pulse when a live edit broadcast arrives */
.message.edited-pulse {
  animation: msgEditedPulse 1.2s ease;
}
@keyframes msgEditedPulse {
  0% { box-shadow: 0 0 0 0 rgba(88, 166, 255, 0.45); }
  70% { box-shadow: 0 0 0 10px rgba(88, 166, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(88, 166, 255, 0); }
}

/* Fade-out while a deleted message is being removed everywhere */
.message.deleting {
  opacity: 0;
  transform: scale(0.98);
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}

.no-messages {
  text-align: center;
  color: var(--text-muted);
  padding: 20px;
  font-style: italic;
}

/* Welcome empty-state: shown on a genuinely empty chat (0 messages).
   Centered via margin:auto since .chat-messages is a flex column. */
.empty-state {
  margin: auto;
  text-align: center;
  padding: 40px 24px;
}
.empty-state-emoji {
  font-size: 3.2rem;
  line-height: 1;
  margin-bottom: 14px;
}
.empty-state-title {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-color);
  margin-bottom: 6px;
}
.empty-state-hint {
  font-size: 0.92rem;
  color: var(--text-muted);
}

/* Composer emoji picker: floating palette above the 🙂 button,
   styled like the reaction picker family. */
.composer-emoji-btn {
  font-size: 1.25rem;
  padding: 0 14px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.composer-emoji-btn:hover,
.composer-emoji-btn[aria-expanded="true"] {
  background: var(--accent-soft);
  color: var(--text-color);
  border-color: var(--border-color);
}
.emoji-picker {
  position: absolute;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 8px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 1500;
  animation: picker-pop 0.15s ease-out;
}
.emoji-option {
  background: none;
  border: none;
  font-size: 1.15rem;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  transition: transform 0.1s, background 0.15s;
}
.emoji-option:hover,
.emoji-option:focus {
  background: var(--accent-soft);
  transform: scale(1.2);
  outline: none;
}

/* Typing indicator ("X is typing...") */
.typing-indicator {
  padding: 0 20px 8px;
  font-size: 0.75rem;
  font-style: italic;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 24px;
}

/* Typing dots animation (bounce) */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin-left: 4px;
}

.typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
  animation: typing-bounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) { animation-delay: 0s; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

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

/* Screen-reader only text for accessibility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Read receipts ("Gesehen 👀") ---------------------------------------
   Row between message list and typing indicator; per-reader initial chips
   colored with the same deterministic user hues as usernames/presence. */
.read-receipts {
  display: none; /* JS toggles flex + content */
  align-items: center;
  gap: 4px;
  padding: 0 20px 10px;
  min-height: 18px;
  font-size: 0.72rem;
  color: var(--text-muted);
}
.read-receipts .read-label {
  margin-right: 2px;
  font-style: italic;
}
.read-receipts .read-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 0.62rem;
  font-weight: 600;
  color: hsl(var(--user-hue), 70%, 82%);
  background: hsl(var(--user-hue), 45%, 30%);
  flex-shrink: 0;
  user-select: none;
}
.read-receipts .read-chip-more {
  width: auto;
  padding: 0 5px;
  border-radius: 8px;
  font-size: 0.6rem;
  background: var(--text-muted);
  color: var(--bg-color);
}

.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.modal-content {
  background: var(--card-bg);
  padding: 24px;
  border-radius: 12px;
  min-width: 300px;
  /* Modal viewport clipping: a fixed, flex-centered overlay cannot grow
     with its content — on short viewports (phones, small windows) the
     shortcuts modal overflowed BOTH edges and the Close button was
     unreachable. The dialog itself scrolls instead, exactly like
     .edit-history-content, with a margin so the top never hugs the edge. */
  max-height: min(80vh, calc(100vh - 32px));
  overflow-y: auto;
  margin: 16px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.5);
  color: var(--text-color);
}

/* Phone-width fix: min-width: 300px + padding outran the 320px viewport,
   so the dialog spilled past both screen edges. Inside the smallest
   common width (320px) the dialog fills all available width instead. */
@media (max-width: 360px) {
  .modal-content {
    min-width: 0;
    width: calc(100vw - 32px);
  }
}

.modal-content h3 {
  margin-bottom: 12px;
}
.modal-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: -6px 0 14px;
}
.modal-content ul {
  list-style: none;
  margin-bottom: 16px;
}
.modal-content li {
  margin-bottom: 8px;
  font-size: 0.9rem;
}
/* Nested shortcut groups: the category <li> holds a <strong> heading plus its
   own sublist; indent the sublist so hierarchy is visible (this modal used to
   carry flat, parser-flattened markup — keep it well-formed from now on). */
.modal-content li > strong {
  display: block;
  margin-bottom: 4px;
  color: var(--text-color);
}
.modal-content li > ul {
  margin: 2px 0 10px 14px;
}
.modal-content li > ul > li:last-child {
  margin-bottom: 0;
}
.modal-content code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
}
#close-shortcuts {
  width: 100%;
  padding: 8px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
#close-shortcuts:hover {
  background: var(--accent-hover);
}

/* Secondary button inside shortcuts modal */
.modal-sub-btn {
  background: var(--surface-hover);
  color: var(--text-color);
  border: 1px solid var(--border-color);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  margin-top: 8px;
}

.modal-sub-btn:hover {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* Mobile responsiveness */
@media (max-width: 600px) {
  .chat-header {
    padding: 10px 12px;
  }
  .header-left h2 {
    font-size: 1.05rem;
  }
  .header-right {
    gap: 10px;
  }
  .header-right button {
    padding: 8px 12px;
    font-size: 0.85rem;
    min-height: 44px;
    min-width: 44px;
  }
  .chat-messages {
    padding: 12px;
    gap: 8px;
  }
  .message {
    max-width: 90%;
    padding: 8px 10px;
  }
  /* Smaller pull on mobile (8px gap): keeps the fused look at ~5px seam. */
  .message.grouped {
    margin-top: -3px;
  }
  .message-text {
    font-size: 0.9rem;
  }
  .chat-form {
    padding: 10px 12px;
    gap: 8px;
  }
  .chat-form input,
  .chat-form textarea,
  .chat-form button {
    min-height: 44px;
    /* 16px+: iOS Safari auto-zooms the page when focusing any input with a
       smaller font size, which visibly jumps the viewport on composer tap. */
    font-size: 1rem;
  }
  .chat-form textarea {
    max-height: 110px;
  }
  .chat-form button {
    padding: 0 12px;
  }
  /* Search input - prevent iOS Safari auto-zoom on focus by ensuring ≥ 1rem */
  #search-input {
    font-size: 1rem;
  }
  /* Read receipts: tighter paddings/chips fit the mobile width. */
  .read-receipts {
    padding: 0 12px 8px;
    font-size: 0.68rem;
  }
  .read-receipts .read-chip { width: 14px; height: 14px; font-size: 0.56rem; }
  .msg-copy {
    min-width: 32px;
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.8rem;
  }
  .code-copy {
    /* Touch target on small screens: slightly larger hit area. */
    min-width: 30px;
    min-height: 30px;
    padding: 3px 6px;
    font-size: 0.8rem;
  }
  .msg-reply {
    min-width: 32px;
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.8rem;
  }
  .reply-context-bar {
    top: -44px;
    padding: 5px 8px;
  }
  .msg-edit {
    min-width: 32px;
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.8rem;
  }
  .msg-delete {
    min-width: 32px;
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.8rem;
  }
  /* Reactions: bigger touch targets on mobile; opacity stays default so
     hover-capable narrow windows still get the hover-reveal treatment. */
  .msg-react,
  .msg-react-start {
    min-width: 32px;
    min-height: 32px;
    padding: 0 6px;
    font-size: 0.8rem;
  }
  .msg-reaction-chip {
    padding: 4px 10px;
  }
  .user-count {
    font-size: 0.75rem;
  }
}

/* Search pagination */
#search-pagination {
  display: none;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 4px 0;
}

/* Mobile: larger touch targets for search pagination buttons */
@media (max-width: 600px) {
  #search-pagination {
    gap: 10px;
    padding: 8px 0;
  }
  #search-pagination button {
    min-width: 36px;
    min-height: 36px;
    padding: 6px 12px;
    font-size: 0.85rem;
  }
}

#search-pagination button {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px 8px;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.75rem;
  min-width: 24px;
  min-height: 24px;
}

#search-pagination button:hover:not(:disabled) {
  background: var(--surface-hover);
  color: var(--text-color);
}

#search-pagination button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#search-page-info {
  font-size: 0.7rem;
  color: var(--text-muted);
  padding: 0 4px;
}

/* System prompt dropdown */
.model-dropdown-wrap {
  position: relative;
}

#btn-system-prompt {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.2s ease;
}

#btn-system-prompt:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.15);
}

#btn-system-prompt:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: auto;
  width: 220px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.35);
  padding: 6px;
  display: none;
  z-index: 500;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.dropdown-menu.open {
  display: block;
  opacity: 1;
  transform: translateY(0);
}

.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  color: var(--text-color);
  font-size: 0.85rem;
  transition: background 0.12s ease, transform 0.1s ease;
}

.dropdown-item:hover {
  background: var(--surface-hover);
  transform: translateX(2px);
}

.dropdown-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.dropdown-item.active {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent);
}

/* Two-line item layout: title above, description below.
   The markup for every header menu (Export, Upload, System prompt, and the
   random-prompt entry) is <span class="dropdown-item-title"> plus
   <span class="dropdown-item-desc">. Both spans are inline by default, so
   without these rules a 220px menu rendered "... JSON Full backup
   (importable)" as one wrapped run of text — the description was
   indistinguishable from the label, and the `margin-top` that separates them
   did not exist. The rules were deleted as collateral in a dropdown polish
   commit that kept only the `.active` title colour; every one of the 15 menu
   items regressed at once. Keep both blocks: the title rule is NOT a
   duplicate of the `.active` colour rule below (that one only recolours an
   already-block title). */
.dropdown-item-title {
  display: block;
  font-weight: 600;
}

.dropdown-item-desc {
  display: block;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
  /* Descriptions are longer than the 220px menu: wrap them instead of
     letting a long one push the panel wider or overflow it. */
  line-height: 1.3;
}

.dropdown-item.active .dropdown-item-title {
  color: var(--accent);
}

/* (Removed: .dropdown-item.prompt-key-active — bespoke highlight class was
   dead code: the container keydown it depended on could never fire. Arrow
   navigation now moves real focus; :focus-visible above styles it.) */

/* Theme toggle: shows the icon of the theme you'd switch TO.
   Dark mode active (default): show sun. Light mode active: show moon. */
.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  transition: var(--transition-theme);
}
.theme-toggle.theme-toggle-pulse {
  animation: themeTogglePulse 0.3s ease;
}
@keyframes themeTogglePulse {
  0% { transform: rotate(0) scale(1); }
  25% { transform: rotate(-15deg) scale(1.15); }
  50% { transform: rotate(0) scale(1.1); }
  75% { transform: rotate(15deg) scale(1.15); }
  100% { transform: rotate(0) scale(1); }
}
.sound-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  opacity: 0.85;
}
.sound-toggle[aria-pressed="false"] {
  opacity: 0.45;
}
.notify-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  opacity: 0.85;
}
.notify-toggle.notify-off {
  opacity: 0.45;
}
.theme-toggle .theme-icon-light { display: none; }
.theme-toggle .theme-icon-dark { display: inline; }
[data-theme="light"] .theme-toggle .theme-icon-light { display: inline; }
[data-theme="light"] .theme-toggle .theme-icon-dark { display: none; }

/* Day separators between chat days */
.day-separator {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0 10px;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  user-select: none;
}
.day-separator::before,
.day-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-color);
  opacity: 0.6;
}

/* Unread divider: red "Neue Nachrichten" anchor above the first message
   that arrived while the reader was away (Discord-style). Distinct from the
   neutral day separator in color and weight so the two never blur. */
.unread-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0 10px;
  color: #ef4444;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  user-select: none;
}
.unread-divider::before,
.unread-divider::after {
  content: '';
  flex: 1;
  height: 2px;
  background: #ef4444;
  opacity: 0.75;
  border-radius: 1px;
}

/* Clickable links inside messages */
.message-text a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}
.message-text a:hover {
  color: var(--accent-hover);
}

/* --- Inline formatting (markdown-style) ---------------------------------- */
/* renderMessageText emits <code>/<strong>/<em>/<del> for `x`, **x**, *x*,
   ~~x~~. Code spans get the shared --code-bg surface (same look as the
   shortcuts modal); strong/em/del rely on native semantics plus a subtle
   accent for <code> text. */
.message-text code {
  background: var(--code-bg);
  color: var(--accent);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.9em;
}

/* --- Uploaded images ------------------------------------------------------ */
/* renderMessageText turns a line that is exactly a local /uploads/<file> raster
   URL into an embedded image. Width is capped and height auto so portrait and
   landscape shots both stay inside the bubble; radius + border tie it to the
   message surface, and the link wrapper opens the ORIGINAL size in a new tab
   (rel=noopener) for detail viewing. */
.message-text a.message-image-link {
  display: inline-block;
  max-width: 100%;
  line-height: 0; /* no stray inline gap under the img */
}
.message-text img.message-image {
  display: block;
  max-width: min(320px, 100%);
  max-height: 320px;
  width: auto;
  height: auto;
  border-radius: 10px;
  border: 1px solid var(--border-color);
  background: var(--code-bg); /* checker-ish backdrop while loading */
  cursor: zoom-in;
}

/* --- Image lightbox ------------------------------------------------------ */
/* Clicking an inline message image (without modifier keys) opens this
   fullscreen overlay instead of a raw new tab - the installed PWA runs in a
   standalone window with no browser chrome. Esc / backdrop / ✕ close it,
   ←/→ and touch swipe navigate between every rendered image. The z-index
   sits above toasts (2000) and all popups (1500/1600/1700). */
.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.88);
  padding: 32px;
}
.image-lightbox.open {
  display: flex;
  animation: lightbox-fade 0.18s ease-out;
}
@keyframes lightbox-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.image-lightbox-img {
  max-width: min(92vw, 1000px);
  max-height: 88vh;
  width: auto;
  height: auto;
  border-radius: 10px;
  box-shadow: 0 8px 48px rgba(0, 0, 0, 0.6);
}
.image-lightbox-close {
  position: absolute;
  top: calc(14px + env(safe-area-inset-top, 0px));
  right: calc(14px + env(safe-area-inset-right, 0px));
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.image-lightbox-close:hover,
.image-lightbox-close:focus-visible {
  background: rgba(255, 255, 255, 0.24);
  outline: none;
}
body.lightbox-open {
  overflow: hidden;
}

/* --- Fenced code blocks (``` ... ```) ------------------------------------ */
/* renderMessageText lifts complete ``` blocks out of the inline pipeline and
   emits <pre class="code-block"><code> with RAW (un-mangled) content: no
   <br>, no inline formatting, no linkify, no search highlighting inside.
   The block reads like a mini terminal panel on both themes; long lines
   scroll horizontally instead of wrapping (code stays copy-paste honest). */
.message-text pre.code-block {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  /* Top padding reserves headroom for the overlaid toolbar (lang label +
     copy button) so the first code line never hides underneath it. */
  padding: 24px 10px 8px;
  margin: 6px 0;
  overflow-x: auto;
  max-width: 100%;
}
.message-text pre.code-block code {
  display: block;
  background: transparent;
  border: none;
  padding: 0;
  color: var(--text-color);
  font-family: monospace;
  font-size: 0.85em;
  line-height: 1.45;
  white-space: pre;
  tab-size: 4;
}
/* Code toolbar: language label (rendered from data-lang via ::before) plus
   a copy button, both overlaid in the block's top-right so scrollable code
   never flows underneath unreadable. Button copies the RAW code text. */
.message-text pre.code-block::before {
  content: attr(data-lang);
  position: absolute;
  top: 2px;
  right: 34px;
  color: var(--text-muted);
  font-family: monospace;
  font-size: 0.7rem;
  pointer-events: none;
  user-select: none;
  white-space: nowrap;
  max-width: calc(100% - 44px);
  overflow: hidden;
  text-overflow: ellipsis;
}
.message-text pre.code-block .code-copy {
  position: absolute;
  top: 3px;
  right: 3px;
  background: var(--card-bg);
  color: var(--text-muted);
  border: 1px solid var(--border-color);
  border-radius: 5px;
  font-size: 0.72rem;
  line-height: 1;
  padding: 2px 5px;
  cursor: pointer;
  z-index: 2;
}
.message-text pre.code-block .code-copy:hover {
  background: var(--surface-hover);
  color: var(--accent);
}
.message-text pre.code-block .code-copy.copied {
  color: var(--success);
  border-color: var(--success);
}
.message-text pre.code-block .code-copy:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.message-text del {
  opacity: 0.75;
}

/* --- Message reactions (emoji) ------------------------------------------ */
/* Chip row sits under the bubble; chips show emoji + count, "mine" gets an
   accent tint. The ➕ chip button opens the picker (see .reaction-picker). */
.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin: 2px 0 4px 12px;
}
.msg-reaction-chip,
.msg-react {
  border: 1px solid var(--border-color);
  background: var(--card-bg);
  color: var(--text-color);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.8rem;
  line-height: 1.4;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.msg-reaction-chip:hover {
  transform: scale(1.08);
  border-color: var(--accent);
}
.msg-reaction-chip.mine {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.msg-reaction-chip.mine::after {
  content: ' ✓';
  color: var(--accent);
  font-size: 0.7rem;
}
.msg-react {
  opacity: 0.7;
  padding: 2px 7px;
}
.msg-react:hover {
  opacity: 1;
  border-color: var(--accent);
}
.msg-react-start {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0 2px;
}

/* @-mention autocomplete: fixed floating list above the composer. */
.mention-popup {
  position: fixed;
  left: 0;
  bottom: 0; /* JS sets exact left/bottom above the caret */
  min-width: 180px;
  max-width: 320px;
  max-height: 240px;
  overflow-y: auto;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  padding: 4px;
  z-index: 1600;
  animation: picker-pop 0.15s ease-out;
}
.mention-option {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: none;
  border-radius: 8px;
  color: var(--text-color);
  font-size: 0.9rem;
  text-align: left;
  padding: 8px 10px;
  cursor: pointer;
}
.mention-option:hover {
  background: var(--accent-soft);
}
.mention-option.active {
  background: var(--accent-soft);
}
.mention-option .mention-avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  text-transform: uppercase;
}
.mention-option .mention-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Emoji picker: small floating palette above the anchor button. */
.reaction-picker {
  position: absolute;
  display: flex;
  gap: 4px;
  padding: 6px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 1500;
  animation: picker-pop 0.15s ease-out;
}
@keyframes picker-pop {
  from { opacity: 0; transform: translateY(4px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.reaction-option {
  background: none;
  border: none;
  font-size: 1.15rem;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 8px;
  transition: transform 0.1s, background 0.15s;
}
.reaction-option:hover,
.reaction-option:focus {
  background: var(--accent-soft);
  transform: scale(1.2);
  outline: none;
}
/* Chips pop in when a reaction arrives. */
.msg-reaction-chip {
  animation: chip-in 0.2s ease-out;
}
@keyframes chip-in {
  from { opacity: 0; transform: scale(0.7); }
  to { opacity: 1; transform: scale(1); }
}

/* Accessibility: users who opt out of motion get instant, animation-free UI
   (message fade-in, pulses, highlight fade, toast transitions). */
@media (prefers-reduced-motion: reduce) {
  .status-badge.reconnecting { animation: none; }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* --- Message pinning (pinned strip above the timeline) -------------------
   Pinned messages live in a compact, scrollable strip between the header
   and the chat scroll area so important notes stay visible without eating
   the timeline. Entries are click-to-jump; the ✖ unpins (author only). */
.pinned-strip {
  max-height: 132px;
  overflow-y: auto;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border-color);
  background: var(--code-bg);
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}
.pinned-entry {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 0.82rem;
  padding: 5px 8px;
  border: 1px solid var(--border-color);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  background: var(--card-bg);
  cursor: pointer;
  transition: background 0.2s;
  min-width: 0;
}
.pinned-entry:hover {
  background: var(--surface-hover);
}
/* The entry is a real button now (role/tabindex set by the client), so it
   needs a visible keyboard focus ring — without one, Tab moved into the strip
   invisibly. */
.pinned-entry:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.pinned-pin {
  flex-shrink: 0;
}
.pinned-text {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.pinned-author {
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 0.72rem;
}
.pinned-unpin {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 0 5px;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.2s;
}
.pinned-unpin:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--surface-hover);
}
/* 📌 meta button mirrors the copy/delete/edit affordances; pinned state is
   accented so the state is readable at a glance. */
.msg-pin {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.2s;
}
.msg-pin:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface-hover);
}
.msg-pin.pinned {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface-hover);
}
.msg-pin:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* Discord-style hover reveal: meta-row action buttons (📋 ✏️ 🗑️ 🙂 📌 ↩️)
   stay out of the way until the pointer is over the message (or a button is
   keyboard-focused). Only applied when the device actually supports hover:
   touch devices keep every affordance permanently visible, and pinned-state
   📌 never hides so the state stays readable at a glance. */
@media (hover: hover) and (pointer: fine) {
  .msg-copy,
  .msg-edit,
  .msg-delete,
  .msg-react-start,
  .msg-reply,
  .msg-pin {
    opacity: 0;
  }
  .message:hover .msg-copy,
  .message:focus-within .msg-copy,
  .message:hover .msg-edit,
  .message:focus-within .msg-edit,
  .message:hover .msg-delete,
  .message:focus-within .msg-delete,
  .message:hover .msg-react-start,
  .message:focus-within .msg-react-start,
  .message:hover .msg-reply,
  .message:focus-within .msg-reply,
  .message:hover .msg-pin,
  .message:focus-within .msg-pin {
    opacity: 1;
  }
  /* A pinned message shows its 📌 even without hover (state indicator). */
  .msg-pin.pinned {
    opacity: 1;
  }
  .msg-react-start:hover,
  .msg-reply:hover {
    opacity: 1;
  }
  /* Permalink affordance: hover reveals a subtle underline on the timestamp. */
  .msg-permalink:hover .message-time,
  .msg-permalink:focus-visible .message-time {
    opacity: 1;
    border-bottom-color: currentColor;
  }
}

/* Grouped messages hide the redundant timestamp (the run head already shows
   it); hovering reveals each bubble's own time, Discord-style. Touch devices
   (no hover support) keep every grouped timestamp permanently visible. */
@media (hover: hover) and (pointer: fine) {
  .message.grouped .message-time {
    opacity: 0;
  }
  .message.grouped:hover .message-time,
  .message.grouped:focus-within .message-time {
    opacity: 0.7;
  }
  /* Permalink affordance still works inside grouped bubbles. */
  .message.grouped .msg-permalink:hover .message-time,
  .message.grouped .msg-permalink:focus-visible .message-time {
    opacity: 1;
    border-bottom-color: currentColor;
  }
}
.pinned-entry.pulse {
  animation: pulseIn 0.5s ease;
}
@keyframes pulseIn {
  0% { transform: scale(0.97); opacity: 0.4; }
  100% { transform: scale(1); opacity: 1; }
}

@media (max-width: 767px) {
  .pinned-strip {
    padding: 6px 12px;
    max-height: 110px;
  }
  .pinned-unpin {
    min-width: 32px;
    min-height: 32px;
  }
  .msg-pin {
    min-width: 32px;
    min-height: 32px;
  }
}
/* Spoiler Tags */
.spoiler {
    background-color: var(--bg-tertiary, #1e293b);
    color: transparent;
    cursor: pointer;
    border-radius: 4px;
    padding: 0 4px;
    transition: background-color 0.2s, color 0.2s;
}
.spoiler:hover {
    background-color: var(--border-color, #334155);
}
.spoiler.revealed {
    /* --surface-hover (defined in both palettes) instead of the never-defined
       --bg-secondary: its dark-only #334155 fallback left light-theme text
       (--text-color #0f172a) at 1.72:1 on the revealed spoiler - effectively
       unreadable. Pairing it with --text-color keeps both themes legible
       (15.17:1 light, 9.9:1 dark) regardless of which bubble it sits on. */
    background-color: var(--surface-hover);
    color: var(--text-color);
}
.spoiler:focus-visible {
    outline: 2px solid var(--accent, #3b82f6);
    outline-offset: 1px;
}

/* Drag & drop upload overlay: covers the whole window while files are
   dragged over it; dropping them uploads via the shared image pipeline.
   Uses the browser's default drop indicator suppression is NOT needed
   because the window-level dragover handler preventDefaults already. */
#upload-drop-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1700;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(2px);
    align-items: center;
    justify-content: center;
    pointer-events: none; /* never intercepts the actual drop event */
}
#upload-drop-overlay.active {
    display: flex;
}
.upload-drop-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 28px 40px;
    border: 2px dashed var(--accent);
    border-radius: 16px;
    background: var(--card-bg);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
    color: var(--text-color);
}
.upload-drop-icon {
    font-size: 38px;
    line-height: 1;
}
.upload-drop-label {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-color);
}
@media (prefers-reduced-motion: reduce) {
    #upload-drop-overlay,
    .upload-drop-box {
        transition: none;
        animation: none;
    }
}

/* Compact-phone header condensation (≤360px): the header carries ~10
   controls that wrap into many rows on the narrowest phones, and the word
   labels ("Export ▾", "Import", "Upload ▾") eat most of the width. At
   this breakpoint every header control already has an icon or a distinct
   shape, and each exposes its name via title/aria-label, so hiding only
   the label text keeps every action reachable. The search input flexes to
   fill the freed-up row width instead of staying at a fixed sliver. */
@media (max-width: 360px) {
  .chat-header {
    gap: 8px;
  }
  .header-right {
    gap: 6px;
  }
  /* Icon-only buttons: shrink to tight square hit targets. Emoji buttons
     (sound/notify/theme/system prompt) need no text removal - they are
     single glyphs already. */
  .header-right button {
    padding: 6px 8px;
    gap: 0;
  }
  #btn-new-chat {
    /* Keep the + icon, drop the word: font-size:0 empties the text node
       while the inline SVG keeps its own explicit size. !important is
       required because this button carries inline styles in index.html. */
    font-size: 0;
    padding: 6px 9px !important;
    gap: 0 !important;
  }
  #btn-new-chat svg {
    width: 17px;
    height: 17px;
  }
  #export-btn,
  #import-btn,
  #upload-btn {
    /* Same trick as New Chat: icon stays (it is an <svg> child), the word
       label collapses to zero width. The ▾ suffix lives in the text, so
       the affordance for the attached dropdown moves to the caret-free icon
       look; the title still names the menu. */
    font-size: 0;
    padding: 6px 9px;
  }
  #btn-new-chat svg,
  #export-btn svg,
  #import-btn svg,
  #upload-btn svg {
    width: 17px;
    height: 17px;
  }
  /* Search input shrinks but stays tappable and usable: it is the only
     control that must remain text-bearing on the narrowest screens. */
  .search-wrapper {
    flex: 1 1 100%;
    min-width: 0;
  }
  #search-input {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 1rem; /* keep ≥1rem to avoid iOS focus zoom */
  }
  #search-pagination button {
    min-width: 34px;
    min-height: 34px;
  }
}
