:root {
  --bg: #eef1f4;
  --panel: #ffffff;
  --panel-2: #f6f8fa;
  --ink: #17212b;
  --ink-2: #4d5b69;
  --ink-3: #8593a1;
  --line: #dbe1e7;
  --accent: #1f6f8b;
  --accent-ink: #ffffff;
  --accent-soft: #e2eff4;
  --bubble-in: #ffffff;
  --bubble-out: #dcebf6;
  --tr: #a35a08;
  --tr-soft: #fbf1e3;
  --ok: #2f7d4f;
  --warn: #b7791f;
  --danger: #b4363a;
  --danger-soft: #f8e6e6;
  --radius-s: 6px;
  --radius-m: 10px;
  --radius-l: 16px;
  --font: 'Onest', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1621;
    --panel: #17212b;
    --panel-2: #1d2a36;
    --ink: #e7edf3;
    --ink-2: #a9b6c2;
    --ink-3: #6f7f8e;
    --line: #26343f;
    --accent: #5aa9c8;
    --accent-ink: #0e1621;
    --accent-soft: #1d3442;
    --bubble-in: #1d2a36;
    --bubble-out: #23445c;
    --tr: #e3a453;
    --tr-soft: #2e2618;
    --ok: #5cbf82;
    --warn: #e0a64a;
    --danger: #e3777a;
    --danger-soft: #3a2124;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.m-only { display: none; }

/* ── кнопки и поля ── */
.btn {
  text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--panel); color: var(--ink); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { min-height: 28px; padding: 0 10px; font-size: 13px; }
.btn.wide { width: 100%; min-height: 42px; }
.input, .select, .textarea {
  width: 100%; min-height: 36px; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--panel); color: var(--ink);
}
.textarea { resize: vertical; min-height: 64px; line-height: 1.4; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); outline: none; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--ink-2); }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 14px; }

/* ── экран входа ── */
.gate { display: grid; place-items: center; padding: 16px; }
.gate-card {
  width: min(420px, 100%); padding: 28px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius-l);
}
.gate-card h1 { margin: 0 0 12px; font-size: 24px; letter-spacing: -.01em; }
.gate-steps { padding-left: 20px; margin: 16px 0; display: grid; gap: 6px; }
.gate-note { color: var(--ink-3); font-size: 13px; margin: 0; }
.gate-error { color: var(--danger); background: var(--danger-soft); padding: 8px 12px; border-radius: var(--radius-s); }

/* ── каркас ── */
.app { display: grid; grid-template-columns: 88px 1fr; height: 100vh; height: 100dvh; }
.rail {
  display: flex; flex-direction: column; gap: 2px; padding: 10px 6px;
  background: var(--panel); border-right: 1px solid var(--line);
}
.rail a {
  display: grid; justify-items: center; gap: 3px; padding: 9px 2px;
  border-radius: var(--radius-m); color: var(--ink-2); text-decoration: none; font-size: 11.5px;
}
.rail a svg { width: 22px; height: 22px; }
.rail a:hover { background: var(--panel-2); color: var(--ink); }
.rail a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.rail .spacer { flex: 1; }
.view { min-width: 0; min-height: 0; overflow: hidden; }

/* ── страницы управления ── */
.page { height: 100%; overflow-y: auto; padding: 24px clamp(16px, 3vw, 40px) 48px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 22px; letter-spacing: -.01em; flex: 1; }
.lead-text { color: var(--ink-2); max-width: 68ch; margin: 0 0 20px; }
/* Строки делят одну сетку (subgrid), поэтому колонки совпадают с заголовком */
.rows {
  display: grid; grid-template-columns: minmax(180px, 1.4fr) repeat(3, minmax(90px, .8fr)) auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-m);
}
.rows > * { grid-column: 1 / -1; }
.row {
  display: grid; grid-template-columns: subgrid; column-gap: 16px;
  align-items: center; padding: 12px 16px; border-top: 1px solid var(--line);
}
.row:first-child { border-top: 0; }
.row.head { color: var(--ink-3); font-size: 12.5px; padding-top: 9px; padding-bottom: 9px; }
.row .title { font-weight: 600; }
.row .sub { color: var(--ink-3); font-size: 13px; }
.row .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.empty { padding: 28px 16px; color: var(--ink-2); text-align: center; }
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.badge::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); }
.badge.online::before { background: var(--ok); }
.badge.connecting::before, .badge.twofa::before { background: var(--warn); }
.badge.stopped { color: var(--ink-3); }
.section { margin-top: 28px; }
.section h2 { font-size: 16px; margin: 0 0 10px; }
.section.first { margin-top: 0; }
.form-grid { display: grid; gap: 14px; max-width: 520px; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { color: var(--ink-3); font-size: 13px; }
.choice { display: grid; gap: 8px; }
.choice label {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; cursor: pointer;
  border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--panel);
}
.choice label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

/* ── чаты ── */
.chats { display: grid; grid-template-columns: 340px 1fr; height: 100%; }
.chat-list { display: flex; flex-direction: column; min-height: 0; background: var(--panel); border-right: 1px solid var(--line); }
.chat-filters { display: grid; gap: 8px; padding: 12px; border-bottom: 1px solid var(--line); }
.chat-filters .inline { justify-content: space-between; }
.chat-filters .select { width: auto; flex: 1; min-height: 32px; padding: 4px 8px; font-size: 14px; }
.chat-items { flex: 1; overflow-y: auto; }
.chat-item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; width: 100%;
  padding: 10px 14px; border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left; cursor: pointer;
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .time { color: var(--ink-3); font-size: 12px; }
.chat-item .preview { color: var(--ink-2); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .where { color: var(--ink-3); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .waiting {
  justify-self: end; align-self: center; width: 10px; height: 10px; border-radius: 50%; background: var(--accent);
}
.more { margin: 10px auto 16px; display: block; }

.conversation { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.conversation.placeholder { display: grid; place-items: center; color: var(--ink-3); padding: 24px; text-align: center; }
.conv-head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.conv-head .who { flex: 1; min-width: 180px; }
.conv-head .who b { font-size: 16px; }
.conv-head .who .sub { color: var(--ink-3); font-size: 13px; }
.conv-head .select { width: auto; min-height: 32px; padding: 4px 8px; }
.back { display: none; }
.mode { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-s); overflow: hidden; }
.mode button { border: 0; background: var(--panel); padding: 5px 10px; cursor: pointer; font-size: 13px; color: var(--ink-2); }
.mode button.on { background: var(--tr-soft); color: var(--tr); font-weight: 600; }

.feed { flex: 1; overflow-y: auto; padding: 16px clamp(12px, 3vw, 32px); display: flex; flex-direction: column; gap: 6px; }
.feed .older { align-self: center; }
.day { align-self: center; color: var(--ink-3); font-size: 12.5px; margin: 10px 0 4px; }
.msg {
  max-width: min(620px, 82%); padding: 8px 12px 6px;
  border-radius: var(--radius-l); background: var(--bubble-in); border: 1px solid var(--line);
  align-self: flex-start; overflow-wrap: anywhere;
}
.msg.out { align-self: flex-end; background: var(--bubble-out); border-color: transparent; }
.msg .author { font-size: 12.5px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.msg .author .via { font-weight: 400; color: var(--ink-3); margin-left: 6px; }
.msg .text { white-space: pre-wrap; }
/* Подстрочник: перевод крупно, оригинал под ним мельче, отмечен янтарной линией */
.msg.translated .text.primary { white-space: pre-wrap; }
.msg .orig {
  white-space: pre-wrap; margin-top: 5px; padding-left: 9px;
  border-left: 2px solid var(--tr); color: var(--ink-2); font-size: 13px;
}
.msg .orig .lang { color: var(--tr); font-weight: 600; margin-right: 6px; }
.msg .pending { color: var(--ink-3); font-size: 12.5px; font-style: italic; }
.msg .stamp { font-size: 11.5px; color: var(--ink-3); text-align: right; margin-top: 2px; }
.msg img.media, .msg video.media { display: block; max-width: 100%; max-height: 360px; border-radius: var(--radius-m); margin: 2px 0 4px; }
.msg video.media.round { width: 240px; height: 240px; object-fit: cover; border-radius: 50%; }
.msg img.sticker { max-width: 160px; }
.msg audio { display: block; width: 260px; max-width: 100%; margin: 2px 0 4px; }
.notice { align-self: center; max-width: 520px; text-align: center; color: var(--ink-2); font-size: 13px; padding: 6px 12px; border-radius: var(--radius-m); background: var(--panel-2); }
.notice.error { color: var(--danger); background: var(--danger-soft); }

.composer { padding: 10px 16px 14px; background: var(--panel); border-top: 1px solid var(--line); display: grid; gap: 8px; }
.composer .inline { justify-content: flex-end; }
.composer .preview { border: 1px solid var(--tr); background: var(--tr-soft); border-radius: var(--radius-m); padding: 8px 10px; display: grid; gap: 6px; }
.composer .preview .label { font-size: 12.5px; color: var(--tr); font-weight: 600; }
.composer .offline { color: var(--danger); font-size: 13px; }

/* ── модальное окно ── */
.modal-back { position: fixed; inset: 0; background: rgba(10, 18, 26, .45); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal { width: min(440px, 100%); max-height: 92vh; overflow-y: auto; background: var(--panel); border-radius: var(--radius-l); padding: 22px; border: 1px solid var(--line); }
.modal h2 { margin: 0 0 12px; font-size: 18px; }
.modal .qr { display: block; width: 260px; height: 260px; margin: 12px auto; background: #fff; border-radius: var(--radius-m); padding: 8px; }
.modal .foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 20;
  background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: var(--radius-m);
  max-width: calc(100% - 32px); font-size: 14px;
}
.toast.error { background: var(--danger); color: #fff; }

/* ── телефон ── */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .rail { grid-row: 2; flex-direction: row; justify-content: space-around; border-right: 0; border-top: 1px solid var(--line); padding: 4px; }
  .rail .spacer { display: none; }
  .rail a { flex: 1; font-size: 10.5px; padding: 6px 2px; }
  .chats { grid-template-columns: 1fr; }
  .chats.open .chat-list { display: none; }
  .chats:not(.open) .conversation { display: none; }
  .back { display: inline-flex; }
  .msg { max-width: 92%; }
  .rows { display: block; }
  .m-only { display: inline; }
  .row { grid-template-columns: 1fr; row-gap: 8px; }
  .row.head { display: none; }
  .row .col-extra { grid-column: 1 / -1; display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; }
  .row .actions { grid-column: 1 / -1; justify-content: flex-start; }
}
@media (min-width: 821px) { .row .col-extra { display: contents; } }
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }

/* ── второй этап ── */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin: -4px 0 20px; border-bottom: 1px solid var(--line); }
.tabs a { padding: 8px 12px; color: var(--ink-2); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--ink); }
.tabs a.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.rows.rows-2 { grid-template-columns: minmax(180px, 1fr) auto; }
.rows.rows-3 { grid-template-columns: minmax(160px, 1fr) minmax(90px, auto) minmax(140px, 1.4fr); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); font-size: 13px; }
.chip.current { margin-left: 8px; background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 600; }
.search-line { margin-bottom: 12px; max-width: 420px; }
.add-line { margin-top: 12px; max-width: 520px; }
.add-line .input { flex: 1; }
.add-box { margin-top: 10px; }
.add-box summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 4px 0; }
.add-box .form-grid { margin-top: 10px; }

.input.date { width: auto; }
.stat-hero { display: flex; align-items: baseline; gap: 12px; margin: 18px 0 8px; color: var(--ink-2); }
.stat-num { font-size: 56px; line-height: 1; font-weight: 700; letter-spacing: -.03em; color: var(--ink); }

/* Столбики: одна серия, тонкие, скруглённые сверху, база — нулевая линия */
.chart { position: relative; margin: 8px 0 0; max-width: 760px; }
.chart figcaption { font-size: 13px; color: var(--ink-2); margin-bottom: 8px; }
.bars { display: grid; grid-template-columns: repeat(14, 1fr); gap: 2px; height: 160px; align-items: end; border-bottom: 1px solid var(--line); }
.bar { position: relative; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; background: none; border: 0; padding: 0 2px; cursor: pointer; }
.bar-fill { display: block; width: 100%; max-width: 28px; border-radius: 4px 4px 0 0; background: var(--accent); opacity: .38; }
.bar:hover .bar-fill, .bar:focus-visible .bar-fill { opacity: .6; }
.bar.on .bar-fill { opacity: 1; }
.bar-value { font-size: 12px; color: var(--ink-2); margin-bottom: 3px; }
.bar-axis { display: grid; grid-template-columns: repeat(14, 1fr); gap: 2px; font-size: 11.5px; color: var(--ink-3); text-align: center; padding-top: 4px; }
.chart-tip { position: absolute; top: 14px; transform: translateX(-50%); background: var(--ink); color: var(--panel); font-size: 13px; padding: 4px 8px; border-radius: var(--radius-s); pointer-events: none; white-space: nowrap; }

.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 4px; max-height: 220px; overflow-y: auto; padding: 6px; border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--panel); }
.icon-pick { height: 40px; font-size: 20px; border: 1px solid transparent; border-radius: var(--radius-s); background: none; cursor: pointer; }
.icon-pick:hover { background: var(--panel-2); }
.icon-pick[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); }

.media-list { display: grid; gap: 10px; }
.media-item { display: flex; gap: 14px; align-items: flex-start; padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-m); }
.media-item audio { width: 240px; max-width: 100%; flex-shrink: 0; }
.media-body { display: grid; gap: 4px; min-width: 0; flex: 1; }
.media-body .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.media-body .text.clamp { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; color: var(--ink-2); }
.thumb { width: 120px; height: 120px; object-fit: cover; border-radius: var(--radius-m); background: var(--panel-2); flex-shrink: 0; }
.thumb.round { border-radius: 50%; }
.thumb.missing { display: grid; place-items: center; text-align: center; font-size: 12px; color: var(--ink-3); padding: 8px; }

@media (max-width: 820px) {
  .rail { overflow-x: auto; justify-content: flex-start; }
  .rail a { flex: 0 0 72px; }
  .bar-axis .odd { visibility: hidden; }
  .media-item { flex-direction: column; }
  .stat-num { font-size: 44px; }
}
