:root {
  --bg: #0e1116;
  --bg-2: #161b23;
  --bg-3: #1f2731;
  --border: #2a3441;
  --text: #e8edf3;
  --text-dim: #93a1b3;
  --accent: #3b82f6;
  --accent-2: #22c55e;
  --danger: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* ------------------------------- login ---------------------------------- */

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(1000px 500px at 50% -10%, #1b2431 0%, var(--bg) 60%);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .45);
}

.logo { font-size: 42px; text-align: center; }

.auth-card h1 {
  text-align: center;
  margin: 8px 0 4px;
  font-size: 22px;
}

.subtitle {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  margin: 0 0 18px;
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  background: var(--bg-3);
  padding: 4px;
  border-radius: var(--radius-sm);
}

.tab {
  flex: 1;
  padding: 8px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.tab.active { background: var(--accent); color: #fff; }

.auth-form label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin: 10px 0 4px;
}

.auth-form input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.auth-form input:focus { border-color: var(--accent); }

.auth-form button.primary {
  width: 100%;
  margin-top: 16px;
  padding: 11px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.auth-form button.primary:hover { filter: brightness(1.1); }
.auth-form button.primary:disabled { opacity: .6; cursor: wait; }

.hint { color: var(--text-dim); font-size: 12px; line-height: 1.5; }

.auth-error {
  margin-top: 14px;
  padding: 10px 12px;
  background: rgba(239, 68, 68, .12);
  border: 1px solid rgba(239, 68, 68, .4);
  color: #fca5a5;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

/* -------------------------------- app ----------------------------------- */

body[data-page="app"] { display: flex; }

#sidebar {
  width: 320px;
  min-width: 260px;
  border-right: 1px solid var(--border);
  background: var(--bg-2);
  display: flex;
  flex-direction: column;
}

#profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  text-transform: uppercase;
  flex-shrink: 0;
}

.profile-meta { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.profile-meta strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chip {
  border: 1px solid var(--border);
  background: var(--bg-3);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
  max-width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip:hover { color: var(--text); }

.icon-btn {
  margin-left: auto;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  width: 34px;
  height: 34px;
  cursor: pointer;
  font-size: 15px;
}

.icon-btn:hover { color: var(--danger); border-color: var(--danger); }

#add-chat {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

#add-chat input {
  flex: 1;
  min-width: 0;
  padding: 9px 11px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  outline: none;
}

#add-chat input:focus { border-color: var(--accent); }

#add-chat button {
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  width: 38px;
  font-size: 18px;
  cursor: pointer;
}

#add-chat button:hover { filter: brightness(1.1); }

#chat-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.chat-item:hover { background: var(--bg-3); }
.chat-item.active { background: rgba(59, 130, 246, .18); }

.chat-item .avatar { width: 38px; height: 38px; font-size: 15px; }

.chat-item-meta { min-width: 0; flex: 1; }
.chat-item-meta .name { font-size: 14px; font-weight: 600; }
.chat-item-meta .preview {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.chat-empty {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  padding: 22px 12px;
  line-height: 1.6;
  list-style: none;
}

/* main pane */

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}

#chat-title { font-weight: 600; font-size: 15px; }
#chat-title .uuid-hint { color: var(--text-dim); font-size: 12px; font-weight: 400; margin-left: 10px; }
#chat-header.empty { color: var(--text-dim); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg {
  max-width: 62%;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg .meta {
  display: block;
  font-size: 10.5px;
  opacity: .65;
  margin-top: 4px;
}

.msg.in {
  align-self: flex-start;
  background: var(--bg-3);
  border-bottom-left-radius: 4px;
}

.msg.out {
  align-self: flex-end;
  background: var(--accent);
  border-bottom-right-radius: 4px;
}

.msg.failed { border: 1px dashed var(--danger); }
.msg.undecryptable { font-style: italic; color: var(--text-dim); border: 1px dashed var(--border); }

.status { font-size: 11px; color: var(--text-dim); padding: 2px 6px; }

#composer {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-2);
}

#msg-input {
  flex: 1;
  padding: 11px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  font-size: 14px;
  outline: none;
}

#msg-input:focus { border-color: var(--accent); }

#send-btn {
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  width: 42px;
  height: 42px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}

#send-btn:hover { filter: brightness(1.1); }
#send-btn:disabled { opacity: .5; cursor: wait; }

/* toasts */

#toasts {
  position: fixed;
  right: 16px;
  top: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}

.toast {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4);
  max-width: 320px;
  animation: slidein .18s ease-out;
}

.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--accent-2); }

@keyframes slidein {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

@media (max-width: 720px) {
  #sidebar { width: 240px; }
}

/* ---------------------- Лендинг FOXYTALK (вход) --------------------- */

body[data-page="login"] {
  background:
    radial-gradient(900px 420px at 50% 18%, rgba(249, 115, 22, 0.14) 0%, transparent 60%),
    var(--bg);
}

#landing {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.wordmark {
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  /* базовый размер слова: и текст, и картинка (em) масштабируются вместе */
  font-size: clamp(40px, 13vw, 150px);
}

.wm-part {
  font-size: inherit;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1;
  color: #fff;
  text-shadow: 0 12px 70px rgba(249, 115, 22, 0.28);
}

/* Лисичка вместо буквы O: размер привязан к размеру текста (em),
   поэтому автоматически масштабируется на телефоне и десктопе.
   font-size: inherit обязателен: у <button> браузер ставит свой мелкий
   шрифт (~13px), без этого em считался бы от него, а не от FOXYTALK.
   Никаких transform/filter при наведении — иначе картинка «прыгает»,
   а у браузера остаются артефакты (чёрный прямоугольник). */
.foxbtn {
  background: none;
  border: 0;
  padding: 0;
  font-size: inherit;          /* наследуем размер от .wordmark (clamp) */
  height: 1.1em;
  margin: 0 0.04em;
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
  outline: none;               /* убираем чёрную/дефолтную рамку фокуса */
  -webkit-tap-highlight-color: transparent;
}

.foxbtn img {
  height: 100%;
  width: auto;
  display: block;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  border-radius: 6%;
}

/* лёгкая подсветка при наведении без перемещения */
.foxbtn:hover img { filter: brightness(1.08); }

/* доступность: своя аккуратная рамка только при фокусе с клавиатуры */
.foxbtn:focus-visible img {
  outline: 2px solid #f97316;
  outline-offset: 4px;
  border-radius: 10%;
}

/* Окно входа поверх лендинга */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(5, 8, 12, 0.72);
  backdrop-filter: blur(6px);
  z-index: 20;
}

.modal:not(.hidden) { display: flex; }

.modal .auth-card {
  position: relative;
  width: 100%;
  max-width: 400px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.6);
  animation: modal-pop 0.18s ease-out;
}

@keyframes modal-pop {
  from { transform: translateY(12px) scale(0.98); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.modal-top {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mini-logo {
  height: 40px;
  width: 40px;
  flex-shrink: 0;
}

.mini-logo svg { height: 100%; width: 100%; display: block; }

.modal .auth-card h1 {
  text-align: left;
  margin: 0;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.modal .subtitle {
  text-align: left;
  margin: 8px 0 18px;
}

.modal-close {
  margin-left: auto;
  background: none;
  border: 0;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.modal-close:hover { color: var(--text); background: var(--bg-3); }

/* ------------------------- Панель владельца (мастер) --------------------- */

#master-panel {
  margin: 10px 10px 0;
  padding: 10px 12px 12px;
  background: linear-gradient(160deg, rgba(249, 115, 22, 0.10), rgba(249, 115, 22, 0.03));
  border: 1px solid rgba(249, 115, 22, 0.35);
  border-radius: var(--radius-sm);
}

#master-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  user-select: none;
}

#master-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fdba74;
}

#master-toggle {
  background: none;
  border: 0;
  color: #fdba74;
  font-size: 14px;
  cursor: pointer;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  line-height: 1;
}

#master-toggle:hover { background: rgba(249, 115, 22, 0.18); }

#master-panel.collapsed { padding-bottom: 6px; }
#master-panel.collapsed #master-head { margin-bottom: 0; }
#master-panel.collapsed #master-body { display: none; }

.master-stats {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.stat {
  flex: 1;
  text-align: center;
  background: var(--bg-3);
  border-radius: var(--radius-sm);
  padding: 8px 4px;
}

.stat b {
  display: block;
  font-size: 24px;
  font-weight: 800;
  color: #fdba74;
  line-height: 1.1;
}

.stat span {
  font-size: 11px;
  color: var(--text-dim);
}

#gen-invite-btn {
  width: 100%;
  border: 0;
  background: #f97316;
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  border-radius: var(--radius-sm);
  padding: 9px;
  cursor: pointer;
}

#gen-invite-btn:hover { filter: brightness(1.1); }
#gen-invite-btn:disabled { opacity: 0.6; cursor: wait; }

#invite-out { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }

.invite-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
}

.invite-row code {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  letter-spacing: 0.06em;
  color: #fdba74;
  overflow: hidden;
  text-overflow: ellipsis;
}

.invite-row button {
  background: none;
  border: 0;
  cursor: pointer;
  font-size: 15px;
  padding: 2px;
}

.invite-row button:hover { transform: scale(1.15); }

/* ------------------------- кнопка push-уведомлений ----------------------- */

#notify-btn { margin-left: auto; }
#logout-btn { margin-left: 0; }

#notify-btn.enabled {
  color: #fdba74;
  border-color: rgba(249, 115, 22, 0.55);
  background: rgba(249, 115, 22, 0.10);
}

/* ============ Мобильная адаптация и режим «только иконки» ============ */

html, body { max-width: 100%; }
body[data-page="app"] { overflow-x: hidden; position: relative; }

#side-toggle { flex-shrink: 0; }

/* Плавное сужение/раскрытие панели */
#sidebar { transition: width 0.18s ease, transform 0.18s ease; }

/* ---- Режим «только иконки» (узкая колонка 64px) ---- */
body.sidebar-rail #sidebar { width: 64px; min-width: 64px; }

body.sidebar-rail #profile {
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px 6px;
}

body.sidebar-rail #profile .avatar { width: 36px; height: 36px; font-size: 14px; }

body.sidebar-rail .profile-meta,
body.sidebar-rail #profile .chip,
body.sidebar-rail .chat-item-meta,
body.sidebar-rail .chat-empty { display: none !important; }

body.sidebar-rail .chat-item {
  justify-content: center;
  padding: 8px 0;
}

body.sidebar-rail #add-chat {
  flex-direction: column;
  padding: 8px;
  gap: 8px;
}

body.sidebar-rail #add-chat input { display: none; }
body.sidebar-rail #add-chat button { width: 100%; font-size: 20px; }

body.sidebar-rail #logout-btn { margin-left: 0; }

body.sidebar-rail #master-panel {
  margin: 6px;
  padding: 6px;
  background: none;
  border: 0;
}

body.sidebar-rail #master-head { margin: 0; justify-content: center; }
body.sidebar-rail #master-title,
body.sidebar-rail #master-toggle { display: none; }
body.sidebar-rail #master-body { display: none !important; }

/* ---- Телефон: панель уезжает за экран; полная версия — поверх чата ---- */
@media (max-width: 640px) {
  body[data-page="app"] { overflow-x: hidden; }

  #sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    width: min(84vw, 300px);
    min-width: 0;
    transform: translateX(-100%);
    box-shadow: none;
    border-right: 1px solid var(--border);
  }

  body.sidebar-rail #sidebar { transform: none; width: 64px; min-width: 64px; }
  body.sidebar-open #sidebar { transform: none; box-shadow: 0 0 60px rgba(0, 0, 0, 0.6); }

  #main { width: 100%; min-width: 0; }

  #chat-header { padding: 12px 14px; }

  .uuid-hint {
    max-width: 38vw;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-block;
    vertical-align: middle;
  }

  #composer { padding: 10px 12px; min-width: 0; }
  #msg-input { min-width: 0; }

  #messages { padding: 12px; }
  .msg { max-width: 86%; }
}
