/* ──────────────────────────────────────────────────────────────────────────
   Палитра WhatsApp. Мини-апп живёт внутри Telegram, но оператор весь день
   смотрит на WhatsApp — интерфейс должен читаться как его продолжение,
   поэтому тему Telegram здесь сознательно НЕ наследуем.
   ────────────────────────────────────────────────────────────────────────── */
:root {
  --brand:          #008069;  /* шапка */
  --brand-deep:     #00705c;
  --accent:         #00a884;  /* кнопка отправки, активные элементы */
  --unread:         #25d366;

  --app-bg:         #ffffff;
  --panel:          #f0f2f5;  /* шапки списка и поле ввода */
  --divider:        #e9edef;
  --canvas:         #efe7de;  /* полотно переписки */

  --bubble-in:      #ffffff;
  --bubble-out:     #d9fdd3;
  --text:           #111b21;
  --text-dim:       #667781;
  --text-on-brand:  #ffffff;

  --danger:         #ea0038;
  --shadow-bubble:  0 1px 0.5px rgba(11, 20, 26, .13);
  --shadow-raised:  0 2px 5px rgba(11, 20, 26, .16);

  --radius-bubble:  7.5px;
  --tap:            44px;      /* минимальная зона нажатия */
}

:root[data-theme='dark'] {
  --brand:          #1f2c34;
  --brand-deep:     #16222a;
  --accent:         #00a884;
  --unread:         #00a884;

  --app-bg:         #111b21;
  --panel:          #202c33;
  --divider:        #222d34;
  --canvas:         #0b141a;

  --bubble-in:      #202c33;
  --bubble-out:     #005c4b;
  --text:           #e9edef;
  --text-dim:       #8696a0;
  --text-on-brand:  #e9edef;

  --danger:         #f15c6d;
  --shadow-bubble:  0 1px 0.5px rgba(0, 0, 0, .3);
  --shadow-raised:  0 2px 5px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--app-bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

/* Поверхности, которые рисует не я, тоже принадлежат дизайну. */
::selection { background: var(--accent); color: #fff; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
* { scrollbar-width: thin; scrollbar-color: rgba(134, 150, 160, .45) transparent; }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-thumb { background: rgba(134, 150, 160, .45); border-radius: 3px; }
*::-webkit-scrollbar-track { background: transparent; }

.hidden { display: none !important; }
.muted { color: var(--text-dim); }
.small { font-size: 12.5px; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
.screen.center { align-items: center; justify-content: center; padding: 24px; }

.icon { width: 22px; height: 22px; display: block; flex: 0 0 auto; }

/* ── Загрузка ───────────────────────────────────────────────────────────── */
.spinner {
  width: 30px; height: 30px;
  border: 3px solid var(--divider);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s cubic-bezier(.4, 0, .2, 1) infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Заглушки: нет доступа, нужен QR ────────────────────────────────────── */
.notice { text-align: center; max-width: 320px; }
.notice-icon {
  color: var(--accent);
  margin-bottom: 14px;
  display: flex; justify-content: center;
}
.notice-icon .icon { width: 44px; height: 44px; }
.notice h2 { margin: 0 0 10px; font-size: 19px; font-weight: 600; }
.notice p { margin: 6px 0; color: var(--text-dim); font-size: 14px; }
.notice code {
  display: inline-block;
  padding: 7px 14px; margin: 8px 0 4px;
  background: var(--panel); border-radius: 8px;
  font: 600 17px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
  user-select: all;
}
#qr-image {
  width: 100%; max-width: 250px;
  margin-top: 18px; padding: 10px;
  background: #fff; border-radius: 10px;
  box-shadow: var(--shadow-raised);
}

/* ── Шапка ──────────────────────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 0 16px;
  min-height: 56px;
  background: var(--brand);
  color: var(--text-on-brand);
  flex: 0 0 auto;
  padding-top: env(safe-area-inset-top);
}
.topbar h1 { margin: 0; font-size: 19px; font-weight: 600; letter-spacing: -.01em; }

.icon-btn {
  background: none; border: 0; cursor: pointer;
  color: currentColor;
  width: var(--tap); height: var(--tap);
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  transition: background-color .15s ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, .12); }
.icon-btn:active { background: rgba(255, 255, 255, .2); }
.spacer { margin-left: auto; }

/* Индикатор связи с WhatsApp: точка, а не текст — он не должен спорить с заголовком. */
.status {
  width: 9px; height: 9px; border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, .25);
}
.status-on  { background: var(--unread); }
.status-off { background: #f5a623; }

.chat-topbar { gap: 6px; }
.chat-topbar .back { margin-left: -8px; }
.chat-title { min-width: 0; flex: 1 1 auto; }
#chat-name { font-weight: 600; font-size: 16.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-phone { font-size: 12.5px; opacity: .8; }
.chat-topbar .chat-avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: rgba(255, 255, 255, .2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; overflow: hidden; position: relative;
  flex: 0 0 auto; font-size: 15px; text-transform: uppercase;
}

/* ── Поиск ──────────────────────────────────────────────────────────────── */
.search-bar { padding: 8px 12px; background: var(--app-bg); flex: 0 0 auto; }
.search-field {
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px; height: 38px;
  background: var(--panel); border-radius: 9px;
  color: var(--text-dim);
}
.search-field .icon { width: 18px; height: 18px; }
.search-field input {
  flex: 1 1 auto; min-width: 0;
  border: 0; background: none; color: var(--text);
  font-size: 14.5px; outline: none;
}
.search-field input::placeholder { color: var(--text-dim); }

/* ── Список чатов ───────────────────────────────────────────────────────── */
.list { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; background: var(--app-bg); }

.chat-row {
  display: flex; align-items: center; gap: 13px;
  padding: 9px 16px; cursor: pointer;
  transition: background-color .12s ease;
}
.chat-row:hover  { background: var(--panel); }
.chat-row:active { background: var(--divider); }

.avatar {
  position: relative;
  width: 49px; height: 49px; border-radius: 50%;
  background: var(--divider); color: var(--text-dim);
  display: flex; align-items: center; justify-content: center;
  font-weight: 500; font-size: 19px; flex: 0 0 auto;
  text-transform: uppercase; overflow: hidden;
}
:root[data-theme='dark'] .avatar { background: #2a3942; color: #8696a0; }
/* Лежит поверх буквы: не загрузилось — под ним остаётся буква, без мигания. */
.avatar-img, .chat-avatar img {
  position: absolute; inset: 0;
  width: 100%; height: 100%; object-fit: cover;
}

.chat-main {
  flex: 1 1 auto; min-width: 0;
  /* Разделитель начинается после аватара — как в WhatsApp. */
  border-bottom: 1px solid var(--divider);
  padding-bottom: 9px; margin-bottom: -9px;
  padding-top: 1px;
}
.chat-row:last-child .chat-main { border-bottom: 0; }

.chat-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.chat-head b { font-weight: 500; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 12px; color: var(--text-dim); flex: 0 0 auto; }
.chat-time.unread { color: var(--unread); font-weight: 500; }

.chat-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 2px; }
.chat-preview {
  color: var(--text-dim); font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1 1 auto;
}
.badge {
  min-width: 20px; height: 20px; padding: 0 6px;
  background: var(--unread); color: #fff;
  border-radius: 10px; font-size: 12px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex: 0 0 auto;
}
:root[data-theme='dark'] .badge { color: #111b21; }

.empty { padding: 60px 32px; text-align: center; color: var(--text-dim); font-size: 14.5px; }

/* ── Полотно переписки ──────────────────────────────────────────────────── */
.messages {
  flex: 1 1 auto; overflow-y: auto;
  padding: 12px 5% 8px;
  display: flex; flex-direction: column; gap: 2px;
  background-color: var(--canvas);
  /* Собственный фоновый мотив: узор WhatsApp — их авторская графика, поэтому
     здесь нарисован свой, в той же логике еле заметных линейных штрихов. */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140' viewBox='0 0 140 140'%3E%3Cg fill='none' stroke='%23000' stroke-width='1.4' stroke-linecap='round' opacity='.04'%3E%3Cpath d='M14 22c4-6 11-6 15 0M22 40a7 7 0 1 0 .01 0'/%3E%3Cpath d='M96 16h14M103 9v14M60 52c6-4 13-1 13 6'/%3E%3Cpath d='M118 60a9 9 0 1 0 .01 0M32 82h16M40 74v16'/%3E%3Cpath d='M86 92c5-7 14-7 19 0M12 108c7-5 14-2 16 6'/%3E%3Cpath d='M64 118a8 8 0 1 0 .01 0M112 116h16'/%3E%3Cpath d='M52 14c3 5 1 11-4 13M128 92c-6 2-11-2-11-8'/%3E%3C/g%3E%3C/svg%3E");
  background-repeat: repeat;
}
:root[data-theme='dark'] .messages {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140' viewBox='0 0 140 140'%3E%3Cg fill='none' stroke='%23fff' stroke-width='1.4' stroke-linecap='round' opacity='.035'%3E%3Cpath d='M14 22c4-6 11-6 15 0M22 40a7 7 0 1 0 .01 0'/%3E%3Cpath d='M96 16h14M103 9v14M60 52c6-4 13-1 13 6'/%3E%3Cpath d='M118 60a9 9 0 1 0 .01 0M32 82h16M40 74v16'/%3E%3Cpath d='M86 92c5-7 14-7 19 0M12 108c7-5 14-2 16 6'/%3E%3Cpath d='M64 118a8 8 0 1 0 .01 0M112 116h16'/%3E%3Cpath d='M52 14c3 5 1 11-4 13M128 92c-6 2-11-2-11-8'/%3E%3C/g%3E%3C/svg%3E");
}

/* Разделитель дня — плашка поверх полотна. */
.day {
  align-self: center;
  margin: 12px 0 10px;
  padding: 5px 12px;
  background: var(--bubble-in);
  color: var(--text-dim);
  border-radius: 7px;
  font-size: 12.5px; font-weight: 500;
  text-transform: uppercase; letter-spacing: .02em;
  box-shadow: var(--shadow-bubble);
}

.bubble {
  position: relative;
  max-width: min(82%, 520px);
  padding: 6px 9px 8px;
  border-radius: var(--radius-bubble);
  box-shadow: var(--shadow-bubble);
  word-wrap: break-word; overflow-wrap: anywhere;
  white-space: pre-wrap;
  font-size: 14.8px; line-height: 1.32;
}
.bubble.in  { align-self: flex-start; background: var(--bubble-in); }
.bubble.out { align-self: flex-end;   background: var(--bubble-out); }

/* Хвостик — только у первого сообщения серии, как в WhatsApp. */
.bubble.tail-in  { border-top-left-radius: 0; }
.bubble.tail-out { border-top-right-radius: 0; }
.bubble.tail-in::before, .bubble.tail-out::before {
  content: ''; position: absolute; top: 0;
  width: 8px; height: 13px;
}
.bubble.tail-in::before {
  left: -8px;
  background: var(--bubble-in);
  clip-path: polygon(100% 0, 0 0, 100% 30%);
}
.bubble.tail-out::before {
  right: -8px;
  background: var(--bubble-out);
  clip-path: polygon(0 0, 100% 0, 0 30%);
}
/* Внутри серии интервал меньше — сообщения читаются как один блок. */
.bubble + .bubble { margin-top: 0; }
.bubble.first-of-group { margin-top: 8px; }

.bubble .sender {
  font-size: 13px; font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

/* Время прижато к правому нижнему углу и не наезжает на текст. */
.bubble .meta {
  float: right;
  display: inline-flex; align-items: center; gap: 3px;
  margin: 6px -3px -3px 8px;
  font-size: 11px; line-height: 1;
  color: var(--text-dim);
  user-select: none;
}
:root[data-theme='dark'] .bubble.out .meta { color: rgba(233, 237, 239, .6); }
.bubble .meta .tick { width: 15px; height: 15px; color: var(--text-dim); }
:root[data-theme='dark'] .bubble.out .meta .tick { color: rgba(233, 237, 239, .6); }

.bubble img, .bubble video {
  max-width: 100%; border-radius: 6px; display: block;
  margin-bottom: 4px; cursor: pointer;
}
.bubble audio { width: 230px; max-width: 100%; margin: 2px 0; }
.bubble a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.media-wrap { max-width: 100%; }
.media-missing {
  padding: 9px 11px; margin-bottom: 4px;
  border: 1px dashed currentColor; border-radius: 6px;
  opacity: .8; font-size: 13px;
}

/* ── Поле ввода ─────────────────────────────────────────────────────────── */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
  background: var(--panel);
  flex: 0 0 auto;
}
.composer-field {
  flex: 1 1 auto; min-width: 0;
  display: flex; align-items: flex-end; gap: 4px;
  background: var(--bubble-in);
  border-radius: 21px;
  padding: 0 6px 0 12px;
  box-shadow: var(--shadow-bubble);
}
.composer textarea {
  flex: 1 1 auto; resize: none;
  max-height: 110px; min-height: 42px;
  padding: 11px 0;
  border: 0; background: none; color: var(--text);
  font: inherit; font-size: 15px; line-height: 1.3;
  outline: none;
}
.composer textarea::placeholder { color: var(--text-dim); }
.attach {
  width: 40px; height: 42px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim); cursor: pointer;
  flex: 0 0 auto;
}
.send-btn {
  width: 46px; height: 46px; flex: 0 0 auto;
  border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: transform .12s cubic-bezier(.2, 0, 0, 1), opacity .12s ease;
}
.send-btn:active { transform: scale(.92); }
.send-btn:disabled { opacity: .55; cursor: default; transform: none; }

.file-hint {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 16px;
  background: var(--panel); color: var(--text-dim);
  font-size: 13px; border-top: 1px solid var(--divider);
  cursor: pointer;
}

/* ── Сотрудники ─────────────────────────────────────────────────────────── */
.admin-body { flex: 1 1 auto; overflow-y: auto; padding: 16px 16px 32px; background: var(--app-bg); }

.add-user { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-dim); }
.field input {
  padding: 11px 13px; border: 0; border-radius: 9px;
  background: var(--panel); color: var(--text); font-size: 15px;
  outline: none;
}
.field input:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.field-row { display: flex; align-items: center; gap: 9px; font-size: 14px; }
.field-row input { width: 18px; height: 18px; accent-color: var(--accent); }

button.primary {
  padding: 12px; border: 0; border-radius: 9px;
  background: var(--accent); color: #fff;
  font-size: 15px; font-weight: 600; cursor: pointer;
  transition: filter .12s ease;
}
button.primary:hover { filter: brightness(1.06); }
button.primary:active { filter: brightness(.94); }

.error {
  padding: 9px 12px; border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger); font-size: 13px;
}
.hint { margin: 18px 0 8px; }
.hint code {
  background: var(--panel); padding: 2px 6px; border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.user-list { display: flex; flex-direction: column; }
/* Кнопки уходят на свою строку: в мини-аппе ширины на «имя + два действия»
   не хватает, и имя обрезалось до «Аня, менедж…». */
.user-row {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: 8px 12px;
  padding: 12px 0; border-top: 1px solid var(--divider);
}
.user-main { flex: 1 1 100%; min-width: 0; }
.user-top { display: flex; align-items: center; gap: 8px; }
.user-top b { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 9px;
  background: var(--panel); color: var(--text-dim);
  text-transform: uppercase; letter-spacing: .03em;
}
.role.admin { background: var(--accent); color: #fff; }
.user-actions { display: flex; gap: 6px; margin-left: auto; }
button.ghost {
  background: var(--panel); color: var(--text);
  border: 0; border-radius: 8px; padding: 8px 11px;
  font-size: 12.5px; cursor: pointer; white-space: nowrap;
  transition: filter .12s ease;
}
button.ghost:hover { filter: brightness(.96); }
button.ghost.danger { color: var(--danger); }

/* ── Выдвижные листы: заметки и шаблоны ─────────────────────────────────── */
.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; }
.sheet-backdrop {
  position: absolute; inset: 0;
  background: rgba(11, 20, 26, .55);
  animation: fade .18s ease-out;
}
.sheet-panel {
  position: relative;
  width: 100%; max-height: 82vh;
  display: flex; flex-direction: column;
  background: var(--app-bg);
  border-radius: 14px 14px 0 0;
  padding: 6px 16px calc(18px + env(safe-area-inset-bottom));
  box-shadow: 0 -6px 24px rgba(11, 20, 26, .28);
  animation: rise .24s cubic-bezier(.2, .8, .2, 1);
  overflow-y: auto;
}
/* Ручка сверху — подсказка, что лист тянется и закрывается. */
.sheet-panel::before {
  content: ''; display: block;
  width: 36px; height: 4px; margin: 4px auto 10px;
  background: var(--divider); border-radius: 2px;
}
@keyframes fade { from { opacity: 0 } }
@keyframes rise { from { transform: translateY(14px); opacity: .6 } }

.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet-sub { margin: 2px 0 12px; }
.sheet-sub code {
  background: var(--panel); padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.icon-btn.dim { color: var(--text-dim); width: 34px; height: 34px; }
.icon-btn.dim:hover { background: var(--panel); }
.icon.sm { width: 16px; height: 16px; }
.sheet-empty { padding: 18px 0 6px; text-align: center; }
.sheet-section {
  margin: 4px 0 2px; font-size: 15px; font-weight: 600;
  padding-top: 14px; border-top: 1px solid var(--divider);
}
.rename { margin-bottom: 6px; }
.rename-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.rename-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rename-row button { flex: 0 0 auto; }

.sheet-form { display: flex; flex-direction: column; gap: 9px; margin-bottom: 14px; }
.sheet-form textarea, .sheet-form input {
  border: 0; border-radius: 10px; padding: 11px 13px;
  background: var(--panel); color: var(--text);
  font: inherit; font-size: 15px; resize: vertical; outline: none;
}
.sheet-form textarea:focus, .sheet-form input:focus { box-shadow: inset 0 0 0 2px var(--accent); }

button.ghost.wide {
  width: 100%; padding: 11px;
  display: flex; align-items: center; justify-content: center; gap: 7px;
  margin-bottom: 12px;
}

/* Заметки — жёлтая полоса слева, как у бумажного стикера. */
.notes-list { display: flex; flex-direction: column; gap: 8px; }
.note {
  position: relative;
  padding: 10px 40px 10px 12px;
  background: var(--panel);
  border-left: 3px solid #f5a623;
  border-radius: 0 9px 9px 0;
}
.note-text { white-space: pre-wrap; word-wrap: break-word; font-size: 14.5px; }
.note-meta { margin-top: 4px; }
.note-del { position: absolute; top: 6px; right: 4px; }

.templates-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.tpl { display: flex; align-items: center; gap: 6px; }
.tpl-main {
  flex: 1 1 auto; min-width: 0; text-align: left;
  display: flex; flex-direction: column; gap: 2px;
  padding: 10px 12px; border: 0; border-radius: 10px;
  background: var(--panel); color: var(--text);
  font: inherit; cursor: pointer;
  transition: filter .12s ease;
}
.tpl-main:hover { filter: brightness(.96); }
.tpl-main b { font-weight: 600; font-size: 14.5px; }
.tpl-main span {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Счётчик заметок на иконке в шапке чата. */
#open-notes { position: relative; }
.dot {
  position: absolute; top: 5px; right: 4px;
  min-width: 16px; height: 16px; padding: 0 4px;
  background: #f5a623; color: #111b21;
  border-radius: 8px; font-size: 10.5px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
