:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #dfe2e8;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --ok-bg: #e3f4e8;
  --ok-text: #1c6b34;
  --off-bg: #fde8e8;
  --off-text: #a12424;
  --info-bg: #e6effd;
  --warn-bg: #fff3d6;
  --warn-text: #8a5a00;
  --mine-bg: #e7efff;
  --radius: 8px;
  --topbar-h: 52px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1d2128;
    --text: #e6e8ec;
    --muted: #9aa3b2;
    --border: #323844;
    --accent: #4d84f5;
    --danger: #ef6b6b;
    --ok-bg: #18382a;
    --ok-text: #7fd6a0;
    --off-bg: #44201f;
    --off-text: #f5a3a3;
    --info-bg: #1d2b45;
    --warn-bg: #3d3018;
    --warn-text: #f1c873;
    --mine-bg: #1f2d4a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
/* Атрибут hidden должен скрывать элемент, даже если в стилях задан display (окно картинок, уведомления). */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

/* Шапка */
.topbar {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .5rem 1.5rem;
  padding: .75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); }
.topbar nav { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
.topbar nav a.current { color: var(--text); font-weight: 600; }

.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.container.wide { max-width: none; padding: 0; }
.container.narrow { max-width: 420px; padding-top: 12vh; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.5rem; max-width: 760px;
}
.narrow-card { max-width: 440px; }

.page-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .75rem; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.count { color: var(--muted); font-weight: 400; font-size: 1rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

/* Кнопки */
.btn {
  display: inline-block; padding: .5rem .9rem; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.btn.link, button.link { border: 0; background: none; color: var(--accent); padding: .5rem .25rem; font: inherit; cursor: pointer; }
form.inline { display: inline; margin: 0; }

/* Формы */
.form { display: flex; flex-direction: column; gap: 1rem; }
.form label { display: flex; flex-direction: column; gap: .3rem; font-weight: 500; }
.form .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1rem; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: .5rem .65rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form label.check { flex-direction: row; align-items: center; gap: .5rem; }
.check input { width: auto; }
.input-group { display: flex; gap: .5rem; }
.input-group input { flex: 1; min-width: 0; font-family: ui-monospace, Consolas, monospace; }

/* Фильтры */
.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: 1rem; }
.filters input[type=search] { flex: 1 1 260px; width: auto; }
.filters select { width: auto; }

/* Таблица */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .6rem .75rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tr.blocked td { opacity: .65; }
.row-actions { text-align: right; white-space: nowrap; }
.row-actions > * + * { margin-left: .25rem; }

.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok-text); }
.badge.off { background: var(--off-bg); color: var(--off-text); }

.empty { padding: 3rem 1rem; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

.pager { display: flex; gap: .25rem; flex-wrap: wrap; margin-top: 1rem; }
.pager a, .pager span { padding: .3rem .7rem; border-radius: 6px; }
.pager .current { background: var(--accent); color: var(--accent-text); }
.pager .gap { color: var(--muted); }

/* Сообщения */
.alert { padding: .75rem 1rem; border-radius: 6px; margin-bottom: 1rem; }
.alert ul { margin: 0; padding-left: 1.2rem; }
.alert-success { background: var(--ok-bg); color: var(--ok-text); }
.alert-error { background: var(--off-bg); color: var(--off-text); }
.alert-info { background: var(--info-bg); }

.badge.admin { background: var(--info-bg); color: var(--accent); }

/* ===== Чат ===== */
.chat {
  display: grid; grid-template-columns: 290px 1fr;
  height: calc(100vh - var(--topbar-h));
}
/* Чат занимает всё место под шапкой, какой бы высоты шапка ни была. */
body:has(.chat) { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
body:has(.chat) .container.wide { flex: 1; min-height: 0; width: 100%; }
body:has(.chat) .chat { height: 100%; }
.chat-side { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: .75rem 1rem; border-bottom: 1px solid var(--border); }
.conv-list { overflow-y: auto; flex: 1; padding: .25rem 0; }
.conv-group { padding: .9rem 1rem .3rem; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.conv-empty { padding: .25rem 1rem; color: var(--muted); font-size: .9rem; }
.conv { display: flex; align-items: center; gap: .5rem; padding: .55rem 1rem; color: var(--text); border-left: 3px solid transparent; }
.conv:hover { background: var(--bg); text-decoration: none; }
.conv.current { background: var(--bg); border-left-color: var(--accent); }
.conv-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.has-unread .conv-title { font-weight: 600; }
.conv-sub { font-size: .8rem; color: var(--muted); }
.unread { background: var(--accent); color: var(--accent-text); border-radius: 999px; font-size: .75rem; padding: 0 .45rem; min-width: 1.4rem; text-align: center; }

.chat-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; padding: .6rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 { margin: 0; font-size: 1.1rem; }
#conv-sub { font-size: .85rem; }
.back { display: none; }
.status-filter { display: flex; gap: .35rem; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: .2rem .75rem; font: inherit; font-size: .85rem; cursor: pointer; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .75rem; }
.loading, .empty-chat { margin: auto; color: var(--muted); }
.load-more { align-self: center; }

.msg-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; font-size: .85rem; margin-bottom: .25rem; }
.author { font-weight: 600; }
.msg-head time { color: var(--muted); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-body.deleted { color: var(--muted); font-style: italic; }
.icon-btn { margin-left: auto; border: 0; background: none; cursor: pointer; opacity: .45; font-size: .9rem; padding: 0 .2rem; }
.icon-btn:hover { opacity: 1; }

.files { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .5rem; }
.thumb { display: block; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--bg); }
.thumb img { display: block; max-width: 260px; max-height: 180px; width: auto; height: auto; object-fit: contain; }

/* Замечание с веткой ответов */
.remark { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--warn-text); border-radius: var(--radius); }
.remark[data-status="in_work"] { border-left-color: var(--accent); }
.remark[data-status="done"] { border-left-color: var(--ok-text); }
.remark.is-deleted { border-left-color: var(--border); }
.remark-main { padding: .75rem 1rem .4rem; }
.remark-foot { margin-top: .25rem; }
.remark-foot .btn.link { padding: .2rem 0; }
.thread { border-top: 1px solid var(--border); background: var(--bg); border-radius: 0 0 var(--radius) var(--radius); }
.replies:empty + .reply-slot:empty { display: none; }
.thread:has(.replies:empty):has(.reply-slot:empty) { display: none; }
.reply { padding: .6rem 1rem .6rem 1.5rem; border-bottom: 1px solid var(--border); }
.reply:last-child { border-bottom: 0; }
.reply.solution { background: var(--ok-bg); }
.reply-slot:not(:empty) { padding: .6rem 1rem; }

.status, .status-select { font-size: .8rem; border-radius: 999px; padding: .05rem .6rem; border: 1px solid transparent; width: auto; }
.status-select { padding: .1rem .4rem; cursor: pointer; }
.s-open { background: var(--warn-bg); color: var(--warn-text); }
.s-in_work { background: var(--info-bg); color: var(--accent); }
.s-done { background: var(--ok-bg); color: var(--ok-text); }

/* Личные чаты — пузыри */
.messages.flat .msg { max-width: min(680px, 85%); align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .5rem .8rem; }
.messages.flat .msg.mine { align-self: flex-end; background: var(--mine-bg); }

/* Поле ввода */
.composer-slot { border-top: 1px solid var(--border); background: var(--surface); padding: .75rem 1rem; }
.composer { display: flex; flex-direction: column; gap: .5rem; border-radius: var(--radius); }
.composer.drag { outline: 2px dashed var(--accent); outline-offset: 4px; }
.composer textarea { resize: vertical; min-height: 2.6rem; max-height: 40vh; }
.composer-bar { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.composer-bar .hint { margin-left: auto; color: var(--muted); font-size: .8rem; }
.composer-bar .solve { display: flex; align-items: center; gap: .35rem; font-size: .9rem; color: var(--ok-text); }
.composer-bar .solve input { width: auto; }
.previews { display: flex; flex-wrap: wrap; gap: .5rem; }
.previews:empty { display: none; }
.preview { position: relative; }
.preview img { display: block; height: 72px; max-width: 140px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
.preview .remove { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--danger); color: #fff; cursor: pointer; font-size: .7rem; line-height: 1; }

/* Выбор собеседника */
.picker { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1rem; width: min(420px, calc(100vw - 2rem)); }
.picker::backdrop { background: rgba(0, 0, 0, .4); }
.picker-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.user-list { max-height: 50vh; overflow-y: auto; margin-top: .5rem; }
.user-item { display: flex; gap: .5rem; align-items: center; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border); background: none; color: var(--text); font: inherit; padding: .55rem .25rem; cursor: pointer; }
.user-item:hover { background: var(--bg); }
.user-item .muted { margin-left: auto; font-size: .85rem; }

.lightbox { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); display: flex; align-items: center; justify-content: center; z-index: 50; cursor: zoom-out; padding: 1rem; }
.lightbox img { max-width: 100%; max-height: 100%; }
.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); background: var(--text); color: var(--surface); padding: .6rem 1rem; border-radius: 6px; z-index: 60; max-width: calc(100vw - 2rem); }

/* Телефон: либо список чатов, либо открытый чат */
@media (max-width: 760px) {
  .chat { grid-template-columns: 1fr; }
  .chat.show-list .chat-main { display: none; }
  .chat:not(.show-list) .chat-side { display: none; }
  .back { display: inline-block; }
  .composer-bar .hint { display: none; }
  .thumb img { max-width: 100%; }
  .messages.flat .msg { max-width: 92%; }
}

/* Блок «Связь с 1С» на странице пользователей */
.onec-box { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem 1rem; margin-bottom: 1rem; }
.onec-box strong { margin-right: .5rem; }
.onec-error { color: var(--off-text); font-size: .85rem; margin-top: .2rem; }
