/* Панель оператора — темизация ТОЛЬКО через --tg-theme-* с фолбэками */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f0f2f5);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
}

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

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  background: var(--bg2);
  color: var(--text);
  overscroll-behavior: none;
}

#app { height: 100%; display: flex; flex-direction: column; }

.hidden { display: none !important; }

.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* --- Загрузка --- */
#screen-loading { align-items: center; justify-content: center; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--hint);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Заглушки (нет доступа / не из Telegram / сессия) --- */
#screen-stub { align-items: center; justify-content: center; padding: 24px; }
.stub { text-align: center; max-width: 280px; }
.stub-icon { font-size: 44px; margin-bottom: 12px; }
.stub-text { color: var(--hint); font-size: 16px; line-height: 1.4; }

/* --- Вкладки --- */
.tabs {
  display: flex;
  background: var(--bg);
  border-bottom: 1px solid var(--bg2);
  position: sticky; top: 0; z-index: 5;
}
.tab {
  flex: 1;
  padding: 12px 4px;
  background: none; border: none;
  font-size: 14px; font-weight: 500;
  color: var(--hint);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 5px;
}
.tab.active { color: var(--link); border-bottom-color: var(--link); }
.tab-badge {
  background: var(--bg2); color: var(--hint);
  border-radius: 9px; padding: 1px 6px; font-size: 12px;
}
.tab.active .tab-badge { background: var(--link); color: var(--btn-text); }
.tab-unread {
  background: #e53935; color: #fff;
  border-radius: 9px; padding: 1px 6px; font-size: 12px; font-weight: 600;
}

/* --- Фильтр --- */
#filter-row { padding: 8px 12px; background: var(--bg); }
#filter-input {
  width: 100%; padding: 8px 12px;
  border: none; border-radius: 10px;
  background: var(--bg2); color: var(--text);
  font-size: 15px; outline: none;
}
#filter-input::placeholder { color: var(--hint); }

/* --- Список чатов --- */
#dialog-list { flex: 1; overflow-y: auto; background: var(--bg); }
.chat-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--bg2);
  cursor: pointer;
}
.chat-row:active { background: var(--bg2); }
.chat-main { flex: 1; min-width: 0; }
.chat-top { display: flex; align-items: baseline; gap: 6px; }
.chat-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-username { color: var(--hint); font-size: 13px; white-space: nowrap; }
.chat-last {
  color: var(--hint); font-size: 13px; margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-last .lm-prefix { color: var(--text); }
.chat-last.lm-event { font-style: italic; }
.chat-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.chat-time { color: var(--hint); font-size: 12px; }
.chat-badges { display: flex; align-items: center; gap: 5px; }
.ticket-badge {
  font-size: 11px; font-weight: 600; border-radius: 8px; padding: 1px 6px; color: #fff;
}
.ticket-badge.st-open { background: #fb8c00; }
.ticket-badge.st-mine { background: #43a047; }
.ticket-badge.st-foreign { background: #9e9e9e; }
.unread-badge {
  background: #e53935; color: #fff; font-size: 12px; font-weight: 600;
  border-radius: 10px; min-width: 20px; height: 20px;
  display: flex; align-items: center; justify-content: center; padding: 0 5px;
}
.empty { padding: 48px 24px; text-align: center; color: var(--hint); font-size: 15px; }

/* --- Диалог: шапка --- */
#dlg-header {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--bg2);
  position: sticky; top: 0; z-index: 5;
}
#dlg-back {
  background: none; border: none; font-size: 22px; color: var(--link);
  cursor: pointer; padding: 4px 8px 4px 0; line-height: 1;
}
#dlg-head-main { flex: 1; min-width: 0; }
#dlg-title-row { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
#dlg-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dlg-username { color: var(--hint); font-size: 13px; white-space: nowrap; }
#dlg-status { color: var(--hint); font-size: 12px; margin-top: 1px; }

/* --- Диалог: лента --- */
#dlg-messages {
  flex: 1; overflow-y: auto;
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.msg { max-width: 82%; display: flex; flex-direction: column; }
.msg-client { align-self: flex-start; }
.msg-operator, .msg-ai { align-self: flex-end; }
.bubble {
  padding: 7px 11px; border-radius: 14px;
  font-size: 15px; line-height: 1.35;
  white-space: pre-wrap; word-break: break-word;
}
.msg-client .bubble { background: var(--bg); border-bottom-left-radius: 4px; }
.msg-operator .bubble {
  background: var(--btn); color: var(--btn-text);
  border-bottom-right-radius: 4px;
}
.msg-ai .bubble {
  background: var(--bg); color: var(--hint);
  border: 1px dashed var(--hint);
  border-bottom-right-radius: 4px;
}
.msg-meta { font-size: 11px; color: var(--hint); margin-top: 2px; display: flex; gap: 6px; }
.msg-operator .msg-meta, .msg-ai .msg-meta { justify-content: flex-end; }
.msg-event { align-self: center; text-align: center; color: var(--hint); font-size: 12px; padding: 2px 8px; max-width: 92%; }
.msg-pending .bubble { opacity: 0.55; }
.msg-failed .bubble { background: #e53935; color: #fff; }
.msg-fail-note { font-size: 12px; color: #e53935; margin-top: 3px; text-align: right; }
.msg-retry {
  background: none; border: 1px solid #e53935; color: #e53935;
  border-radius: 8px; padding: 2px 10px; font-size: 12px; cursor: pointer;
  margin-top: 4px; align-self: flex-end;
}

/* --- Диалог: низ по input_mode --- */
#dlg-bottom { background: var(--bg); border-top: 1px solid var(--bg2); padding: 8px 12px; }
.compose { display: flex; gap: 8px; align-items: flex-end; }
#compose-text {
  flex: 1; resize: none; border: none; outline: none;
  background: var(--bg2); color: var(--text);
  border-radius: 12px; padding: 9px 12px;
  font: inherit; max-height: 120px; min-height: 38px;
}
#compose-text::placeholder { color: var(--hint); }
.btn {
  border: none; border-radius: 10px; cursor: pointer;
  font-size: 15px; font-weight: 500; padding: 10px 14px;
}
.btn-primary { background: var(--btn); color: var(--btn-text); }
.btn-primary:disabled { opacity: 0.5; }
.btn-wide { width: 100%; }
.btn-ghost { background: var(--bg2); color: var(--link); }
.compose-actions { display: flex; gap: 8px; margin-top: 8px; }
.compose-actions .btn { flex: 1; font-size: 13px; padding: 8px 10px; }
.foreign-note { text-align: center; color: var(--hint); font-size: 14px; padding: 6px 0; }

/* --- Плашка «Нет связи» и тост --- */
#offline-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  background: #e53935; color: #fff;
  text-align: center; font-size: 13px; padding: 4px;
}
#toast {
  position: fixed; bottom: 76px; left: 50%; transform: translateX(-50%);
  z-index: 60; max-width: 88%;
  background: rgba(0, 0, 0, 0.78); color: #fff;
  padding: 9px 16px; border-radius: 12px; font-size: 14px; text-align: center;
}


