/* Операторская панель. Без сборки: один файл стилей, светлая и тёмная темы. */

:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #f3f5f8;
  --line: #e2e6ec;
  --text: #15181d;
  --muted: #6b7380;
  --accent: #2f6fed;
  --accent-2: #e8f0ff;
  --feed: #e8ecf1;
  --in: #ffffff;
  --out: #d9e8ff;
  --out-ink: #0e1b31;
  --danger: #d4363b;
  --ok: #1d8f4e;
  --shadow: 0 1px 1px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1013;
    --panel: #171a20;
    --panel-2: #1f232a;
    --line: #2a2f38;
    --text: #e8eaee;
    --muted: #949ba7;
    --accent: #5b8cff;
    --accent-2: #1d2a44;
    --feed: #111419;
    --in: #20242c;
    --out: #1f3963;
    --out-ink: #e9efff;
    --danger: #f0575c;
    --ok: #3cc178;
    --shadow: 0 1px 2px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body.lb-open { overflow: hidden; }
a { color: var(--accent); }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.icon-btn {
  width: 38px; height: 38px; flex: none;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 10px; background: none;
  color: var(--muted); cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn:focus-visible, .btn:focus-visible, .send:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 42px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { height: 34px; padding: 0 12px; font-size: 14px; }

/* ---------- Каркас ---------- */

.app {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.sidebar {
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
  background: var(--panel); border-right: 1px solid var(--line);
}
.side-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 10px 8px 18px;
}
.brand { font-weight: 700; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-actions { display: flex; align-items: center; gap: 2px; }
.side-actions form { margin: 0; }

.search { padding: 0 12px 10px; }
.search input {
  width: 100%; height: 40px; padding: 0 14px;
  border: 1px solid transparent; border-radius: 12px;
  background: var(--panel-2); color: var(--text);
  font: inherit; font-size: 16px; outline: none;
}
.search input:focus { border-color: var(--accent); background: var(--panel); }

.conv-list { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 6px 10px; }
.list-empty { padding: 32px 18px; text-align: center; color: var(--muted); }

.conv {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 10px; border-radius: 12px;
  color: inherit; text-decoration: none;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--accent); color: #fff; }
.conv.active .preview, .conv.active time { color: rgba(255, 255, 255, .82); }
.conv.active .badge { box-shadow: 0 0 0 1px rgba(255, 255, 255, .6); }
.conv.active .dot { background: #fff; }

.ava {
  position: relative; flex: none;
  width: 48px; height: 48px; border-radius: 50%; overflow: hidden;
  display: grid; place-items: center;
  background: hsl(var(--h, 210) 52% 54%); color: #fff;
  font-weight: 600; font-size: 17px; letter-spacing: .02em; user-select: none;
}
.ava img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.conv-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.conv-top, .conv-bottom { display: flex; align-items: center; gap: 6px; min-width: 0; }
.conv-name { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv time { flex: none; font-size: 12.5px; color: var(--muted); }
.badge {
  flex: none; padding: 1px 6px; border-radius: 6px;
  background: var(--c, #8a8f98); color: #fff;
  font-size: 10.5px; font-weight: 700; letter-spacing: .03em; line-height: 16px;
}
.preview { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }

/* ---------- Переписка ---------- */

.chat { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--feed); }

.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  color: var(--muted);
}
.chat-empty p { margin: 0; }

.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-head .ava { width: 42px; height: 42px; font-size: 15px; }
.head-info { flex: 1; min-width: 0; }
.head-title { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.head-sub { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; font-size: 13px; color: var(--muted); }
.manual { color: var(--accent); }
.open-in {
  flex: none; padding: 7px 12px; border-radius: 10px;
  background: var(--accent-2); color: var(--accent);
  font-size: 14px; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.open-in:hover { background: var(--accent); color: #fff; }

.feed { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; overflow-anchor: none; }
.feed-inner {
  display: flex; flex-direction: column; gap: 3px;
  max-width: 860px; margin: 0 auto; padding: 10px 16px 14px;
}
.feed-note { align-self: center; margin: 24px 0; color: var(--muted); text-align: center; }
.feed-error { align-self: center; margin: 24px 0; color: var(--danger); text-align: center; }
.spinner {
  align-self: center; margin: 28px 0; width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.day-sep {
  position: sticky; top: 6px; z-index: 2;
  align-self: center; margin: 10px 0 6px; padding: 3px 11px; border-radius: 999px;
  background: var(--panel); box-shadow: var(--shadow);
  font-size: 12.5px; font-weight: 500; color: var(--muted);
}

.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.msg + .msg.in, .msg + .msg.out { margin-top: 0; }
.msg.in + .msg.out, .msg.out + .msg.in { margin-top: 6px; }

.bubble {
  position: relative; max-width: min(560px, 80%); min-width: 0;
  padding: 7px 11px 7px; border-radius: var(--radius);
  background: var(--in); box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.msg.in .bubble { border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--out); color: var(--out-ink); border-bottom-right-radius: 6px; }
.author { margin-bottom: 2px; font-size: 12.5px; font-weight: 600; color: var(--accent); }
.text { white-space: pre-wrap; }
.text a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.meta {
  float: right; position: relative; top: 5px; margin-left: 10px;
  font-size: 11.5px; color: var(--muted); white-space: nowrap; user-select: none;
}
.msg.out .meta { color: color-mix(in srgb, var(--out-ink) 55%, transparent); }

/* Пузырь с вложением */
.bubble.has-att { padding: 4px; }
.bubble.has-att .author { padding: 3px 7px 0; }
.bubble.has-att .text { padding: 5px 7px 3px; }
.bubble.has-att > .meta { float: none; position: static; display: block; text-align: right; padding: 2px 7px 1px; }
.bubble.bare > .meta {
  position: absolute; top: auto; right: 10px; bottom: 9px; padding: 1px 7px; border-radius: 10px;
  background: rgba(0, 0, 0, .5); color: #fff;
}
.bubble.k-sticker.bare, .bubble.k-video_note.bare { background: transparent; box-shadow: none; padding: 0; }
.bubble.k-sticker.bare > .meta, .bubble.k-video_note.bare > .meta { bottom: 2px; right: 2px; }

.att-photo {
  display: block; max-width: 100%; padding: 0; border: 0; border-radius: 12px; overflow: hidden;
  background: var(--panel-2); cursor: zoom-in;
}
.att-photo img { display: block; width: 100%; height: 100%; max-height: 420px; object-fit: cover; }
.att-photo:not([style]) img { width: auto; max-width: 100%; height: auto; }
.att-photo.sticker { background: none; cursor: default; }
.att-photo.sticker img { object-fit: contain; }

.att-video, .att-gif {
  position: relative; max-width: 100%; border-radius: 12px; overflow: hidden; background: #000;
}
.att-video:not([style]), .att-gif:not([style]) { width: 340px; }
.att-video video, .att-gif video { display: block; width: 100%; height: 100%; max-height: 460px; object-fit: contain; background: #000; }
.att-gif { cursor: zoom-in; }
.att-video .expand {
  position: absolute; top: 8px; right: 8px; width: 34px; height: 34px; border: 0; border-radius: 10px;
  display: grid; place-items: center; background: rgba(0, 0, 0, .5); color: #fff; cursor: pointer;
}
.dur {
  position: absolute; top: 8px; left: 8px; padding: 1px 7px; border-radius: 10px;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 12px; pointer-events: none;
}

/* «Кружок» */
.att-note {
  position: relative; width: 220px; height: 220px; max-width: 62vw; max-height: 62vw;
  border-radius: 50%; cursor: pointer;
}
.att-note video { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; background: #000; display: block; }
.att-note::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%; pointer-events: none;
  background: conic-gradient(var(--accent) var(--p, 0%), transparent 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2px));
}
.note-play {
  position: absolute; inset: 0; margin: auto; width: 54px; height: 54px; border-radius: 50%;
  display: grid; place-items: center; background: rgba(0, 0, 0, .5); color: #fff; pointer-events: none;
}
.att-note.playing .note-play { display: none; }
.att-note .dur { top: auto; bottom: 14px; left: 50%; transform: translateX(-50%); }

/* Аудио */
.att-audio { display: flex; flex-direction: column; gap: 4px; width: 300px; max-width: 100%; padding: 4px 4px 0; }
.audio-info { display: flex; justify-content: space-between; gap: 8px; padding: 0 4px; font-size: 13.5px; }
.audio-kind { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.audio-dur { color: var(--muted); flex: none; }
.att-audio audio { width: 100%; height: 40px; }

/* Документ */
.att-doc {
  display: flex; align-items: center; gap: 10px; min-width: 220px; max-width: 100%;
  padding: 6px 8px 4px 6px; color: inherit; text-decoration: none; border-radius: 12px;
}
.att-doc:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.doc-ico {
  flex: none; width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; letter-spacing: .02em;
}
.doc-text { display: flex; flex-direction: column; min-width: 0; }
.doc-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-size { font-size: 13px; color: var(--muted); }

/* Вложение недоступно */
.att-big, .att-fail {
  display: flex; flex-direction: column; gap: 6px; padding: 8px 8px 4px; max-width: 320px;
  font-size: 14px;
}
.att-big img { width: 100%; max-height: 220px; object-fit: cover; border-radius: 10px; filter: brightness(.75); }
.att-big a, .att-fail a { font-weight: 600; }
.att-fail { color: var(--muted); }
.att-links { display: flex; flex-wrap: wrap; gap: 4px 14px; }

.to-bottom {
  position: absolute; right: 18px; bottom: 86px; z-index: 3;
  width: 42px; height: 42px; border-radius: 50%; border: 0;
  display: grid; place-items: center; background: var(--panel); color: var(--muted);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .18); cursor: pointer;
}

/* ---------- Ответ ---------- */

.composer {
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}
.composer-row, .chip { max-width: 860px; margin: 0 auto; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1; min-width: 0; resize: none; min-height: 40px; max-height: 168px;
  padding: 9px 14px; border: 1px solid var(--line); border-radius: 20px;
  background: var(--panel-2); color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.35; outline: none;
}
.composer textarea:focus { border-color: var(--accent); background: var(--panel); }
.send {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: #fff; cursor: pointer;
}
.send:disabled { opacity: .55; cursor: progress; }
.clip { margin-bottom: 1px; }

.chip {
  display: flex; align-items: center; gap: 10px; margin-bottom: 8px; padding: 6px 6px 6px 8px;
  border-radius: 12px; background: var(--panel-2);
}
.chip-thumb {
  flex: none; width: 40px; height: 40px; border-radius: 8px; overflow: hidden;
  display: grid; place-items: center; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
}
.chip-thumb img { width: 100%; height: 100%; object-fit: cover; }
.chip-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chip-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip-size { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.chip.busy .chip-size { color: var(--accent); }
.chip-bar {
  display: block; height: 4px; margin-top: 5px; border-radius: 2px; overflow: hidden;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
}
.chip-bar > span { display: block; width: 0; height: 100%; border-radius: 2px; background: var(--accent); transition: width .2s; }
.chip-bar.wait > span { width: 35%; transition: none; animation: chip-wait 1.1s ease-in-out infinite; }
@keyframes chip-wait { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
#chipRemove:disabled { opacity: .35; cursor: default; }

.drop-hint {
  position: absolute; inset: 10px; z-index: 5; display: grid; place-items: center;
  border: 2px dashed var(--accent); border-radius: 18px;
  background: color-mix(in srgb, var(--panel) 88%, transparent); color: var(--accent); font-weight: 600;
  pointer-events: none;
}

/* ---------- Просмотр фото и видео ---------- */

.lightbox {
  position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column;
  background: rgba(8, 10, 14, .92);
}
.lb-bar { display: flex; justify-content: flex-end; gap: 8px; padding: calc(10px + env(safe-area-inset-top)) 12px 10px; }
.lb-btn {
  height: 38px; min-width: 38px; padding: 0 14px; border: 0; border-radius: 10px;
  display: inline-grid; place-items: center;
  background: rgba(255, 255, 255, .12); color: #fff; text-decoration: none; cursor: pointer; font-size: 15px;
}
.lb-btn:hover { background: rgba(255, 255, 255, .22); }
.lb-body {
  flex: 1; min-height: 0; display: grid; place-items: center;
  padding: 0 12px calc(16px + env(safe-area-inset-bottom));
}
.lb-body img, .lb-body video {
  max-width: 100%; max-height: calc(100vh - 90px); max-height: calc(100dvh - 90px);
  object-fit: contain; border-radius: 6px;
}

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translateX(-50%); max-width: min(92vw, 520px);
  padding: 10px 16px; border-radius: 12px;
  background: #1d2128; color: #fff; box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
.toast.error { background: var(--danger); }

/* ---------- Простые страницы: вход, операторы, ошибки ---------- */

body.plain { background: var(--bg); }
.login-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 22px; box-shadow: var(--shadow);
}
.card h1 { margin: 6px 0 4px; font-size: 22px; }
.card h2 { margin: 0 0 12px; font-size: 17px; }
.login { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.login p { margin: 0; }
.login-logo {
  width: 52px; height: 52px; border-radius: 16px; display: grid; place-items: center;
  background: var(--accent); color: #fff;
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
input:not([type=file]):not([type=hidden]), select {
  height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel-2); color: var(--text); font: inherit; font-size: 16px; font-weight: 400; outline: none;
}
input:focus, select:focus { border-color: var(--accent); background: var(--panel); }
.alert { padding: 10px 14px; border-radius: 12px; font-size: 14px; }
.alert.error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.alert.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }

.page { max-width: 720px; margin: 0 auto; padding: calc(12px + env(safe-area-inset-top)) 16px 32px; display: flex; flex-direction: column; gap: 14px; }
.page-head { display: flex; align-items: center; gap: 8px; }
.page-head h1 { flex: 1; margin: 0; font-size: 22px; }
.page-head form { margin: 0; }
.users { list-style: none; margin: 0; padding: 0; }
.users li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.users li:first-child { border-top: 0; }
.users form { margin: 0; }
.user-main { display: flex; flex-direction: column; min-width: 0; }
.user-login { font-weight: 600; overflow-wrap: anywhere; }
.user-main .muted { font-size: 13px; }
.create-user { display: grid; gap: 12px; }

/* ---------- Узкий экран: либо список, либо переписка ---------- */

@media (max-width: 767px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .app[data-view="list"] .chat { display: none; }
  .app[data-view="chat"] .sidebar { display: none; }
  .sidebar { border-right: 0; }
  .chat-head { padding-left: 6px; gap: 8px; }
  .feed-inner { padding: 8px 8px 12px; }
  .bubble { max-width: 86%; }
  .open-in { padding: 7px 10px; font-size: 13px; }
  .to-bottom { right: 12px; }
}
@media (min-width: 768px) {
  .back { display: none; }
}
@media (max-width: 767px) {
  .open-in-long { display: none; }
}
@media (max-width: 420px) {
  .att-audio { width: 240px; }
  .att-doc { min-width: 0; }
}
