/* Chat UI - mobile-first responsive, dark/light theme */

/* По умолчанию светлая тема (и при .theme-light) */
:root,
.theme-light {
  --bg: #f5f5f7;
  --bg-header: #e8e8ed;
  --bg-bubble-user: #d1e3f6;
  --bg-bubble-assistant: #ffffff;
  --text: #1d1d1f;
  --text-muted: #6e6e73;
  --accent: #c41e3a;
  --border: #d2d2d7;
}

/* Тёмная тема */
.theme-dark {
  --bg: #1a1a2e;
  --bg-header: #16213e;
  --bg-bubble-user: #0f3460;
  --bg-bubble-assistant: #16213e;
  --text: #e8e8e8;
  --text-muted: #a0a0a0;
  --accent: #e94560;
  --border: #2a2a4a;
}

:root {
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-left: env(safe-area-inset-left, 0);
  --safe-right: env(safe-area-inset-right, 0);
}

/* По умолчанию светлая тема; тёмная только если устройство явно в тёмном режиме */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1a1a2e;
    --bg-header: #16213e;
    --bg-bubble-user: #0f3460;
    --bg-bubble-assistant: #16213e;
    --text: #e8e8e8;
    --text-muted: #a0a0a0;
    --accent: #e94560;
    --border: #2a2a4a;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 1rem;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
  max-height: 100dvh;
  max-height: 100vh;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  overflow: hidden;
}

/* Header */
.header {
  flex-shrink: 0;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  flex-shrink: 0;
}

/* Кнопка «Вниз»: круглая, правый нижний угол ленты (absolute, без класса .btn) */
.btn-scroll-down-fab {
  position: absolute;
  bottom: 1rem;
  right: 2rem;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  background: var(--bg-header);
  color: var(--text);
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
  transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
  cursor: pointer;
}

/* Десктоп: лента 960px по центру — прижимаем к правому краю ленты */
@media (min-width: 1024px) {
  .btn-scroll-down-fab {
    right: calc((100% - 960px) / 2 + 2rem);
  }
}

.btn-scroll-down-fab-icon {
  display: block;
  line-height: 1;
  margin-top: -0.1em;
}

.btn-scroll-down-fab.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

/* Кнопка «Новые сообщения»: по центру ленты, на уровне кнопки «Вниз» */
.btn-new-messages-fab {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  background: var(--bg-header);
  color: var(--text);
  font-size: 0.875rem;
  white-space: nowrap;
  z-index: 10;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
  cursor: pointer;
}

.btn-new-messages-fab.visible {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

.btn-theme-toggle {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 8px;
  flex-shrink: 0;
  background: transparent;
  color: var(--text);
}

.btn-theme-toggle .theme-icon {
  display: none;
  width: 22px;
  height: 22px;
  margin: 0 auto;
}

.btn-theme-toggle .theme-icon svg {
  display: block;
  width: 100%;
  height: 100%;
}

[data-theme="light"] .btn-theme-toggle .theme-icon-sun,
[data-theme="dark"] .btn-theme-toggle .theme-icon-moon,
[data-theme="auto"] .btn-theme-toggle .theme-icon-auto {
  display: block;
}

.search-wrap {
  position: relative;
  flex-shrink: 0;
}

.btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
}

.btn-search-toggle {
  background: transparent;
  font-size: 1.25rem;
  line-height: 1;
}

.search-panel {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.5rem;
  width: min(90vw, 360px);
  padding: 0.75rem;
  background: var(--bg-header);
  border: 1px solid var(--border);
  border-radius: 8px;
  z-index: 10;
}

.search-panel[hidden] {
  display: none !important;
}

.search-input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font-size: 16px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  margin-bottom: 0.5rem;
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.search-counter {
  font-size: 0.875rem;
  color: var(--text-muted);
  flex: 1;
}

.btn-nav {
  min-width: 36px;
}

.btn-close {
  margin-left: auto;
}

.search-results {
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.search-results .search-result-item {
  padding: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
}

.search-results .search-result-item:hover,
.search-results .search-result-item.highlight {
  background: var(--bg-bubble-user);
}

/* Main - feed area */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.feed-wrapper {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  justify-content: center;
  overflow: hidden;
}

.feed {
  flex: 1;
  width: 100%;
  max-width: 100%;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 1rem;
}

.feed-sentinel {
  height: 1px;
  visibility: hidden;
  pointer-events: none;
}

.feed-messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.message {
  display: flex;
  gap: 0.5rem;
  max-width: 85%;
  align-self: flex-start;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
  background: var(--bg-bubble-user);
  border-bottom-right-radius: 4px;
}

.message.assistant {
  background: var(--bg-bubble-assistant);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message-avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
}

.message.user .message-avatar {
  display: none;
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-body {
  line-height: 1.5;
}

.message-body mark {
  background: var(--accent);
  color: #fff;
  padding: 0 2px;
  border-radius: 2px;
}

.message-body .insert-command {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid var(--accent);
  cursor: pointer;
}

.message-body .insert-command:hover {
  text-decoration: underline;
}

.message-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.message-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-left: auto;
}

.message-chat-id {
  font-size: 1rem; /* как в теле сообщения */
  color: var(--text-muted);
}

.message-unread .message-content {
  position: relative;
}

.message-unread .message-content::before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

/* Status block with typing inside - плавное появление/исчезновение */
.status-block {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--bg-header);
  border-top: 1px solid var(--border);
  font-size: 0.875rem;
  color: var(--text-muted);
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  transition: max-height 0.25s ease-out, opacity 0.2s ease-out;
}

.status-block.visible {
  max-height: 12rem; /* многострочный статус может увеличить блок */
  opacity: 1;
  overflow-y: auto; /* при очень длинном тексте — прокрутка */
}

.status-block .status-text {
  white-space: pre-wrap; /* переносы строк в тексте статуса */
  word-break: break-word;
}

.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0 0.25rem 0 0;
}

.typing-indicator[hidden] {
  display: none !important;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.4s ease-in-out infinite both;
}

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

@keyframes typing-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* Footer - input (always visible) */
.footer {
  flex: 0 0 auto;
  padding: 0.75rem 1rem;
  padding-bottom: calc(0.75rem + var(--safe-bottom));
  background: var(--bg-header);
  border-top: 1px solid var(--border);
}

.input-wrap {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.message-input {
  flex: 1;
  min-height: 44px;
  max-height: 240px; /* ~10 строк */
  padding: 0.6rem 0.75rem;
  font-size: 16px;
  line-height: 1.35;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  resize: none;
  font-family: inherit;
  overflow-y: auto;
}

.message-input::placeholder {
  color: var(--text-muted);
}

.btn-send {
  flex-shrink: 0;
  height: 44px;
  min-height: 44px;
  width: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  line-height: 1;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
}

/* Tablet and desktop */
@media (min-width: 600px) {
  .message {
    max-width: 70%;
  }

  .search-panel {
    width: 400px;
  }
}

/* Desktop: constrain feed width for readability */
@media (min-width: 1024px) {
  .message {
    max-width: 55%;
  }

  .feed {
    max-width: 960px;
    margin: 0 auto;
    width: 100%;
    padding: 1.5rem;
  }
}
