:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel-2: #1c2836;
  --hover: #202b36;
  --active: #2b5278;
  --bubble-in: #182533;
  --bubble-out: #2b5278;
  --text: #e9edf1;
  --muted: #708499;
  --accent: #64b5ef;
  --badge: #5288c1;
  --line: rgba(255, 255, 255, .07);
  --danger: #e37272;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --rail-w: 320px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Ниже задаётся display на .gate и .shell — без !important это правило
   перебило бы браузерное [hidden] { display: none }. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 15px/1.45 var(--sans);
  color: var(--text);
  background: var(--bg);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }

button {
  border: 0;
  background: var(--badge);
  color: #fff;
  padding: 7px 16px;
  border-radius: 6px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: #5f9ad6; }
button:disabled { opacity: .35; cursor: default; }
button.ghost { background: transparent; color: var(--muted); padding: 7px 12px; }
button.ghost:hover:not(:disabled) { background: var(--hover); color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input[type="search"], input[type="password"] {
  border: 1px solid transparent;
  border-radius: 18px;
  padding: 8px 14px;
  background: var(--panel-2);
  color: var(--text);
  width: 100%;
}
input::placeholder { color: var(--muted); }
input:focus { outline: none; border-color: var(--accent); }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #2a3846; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #364656; }
::-webkit-scrollbar-track { background: transparent; }
* { scrollbar-width: thin; scrollbar-color: #2a3846 transparent; }

.avatar {
  flex: none;
  width: 46px; height: 46px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 500; font-size: 18px;
  user-select: none;
}
.avatar.small { width: 38px; height: 38px; font-size: 15px; }

/* ---------------------------------------------------------------- вход */

.gate { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.gate-card {
  width: min(360px, 100%);
  background: var(--panel);
  border-radius: 12px;
  padding: 32px;
}
.gate-card h1 { font-size: 20px; margin: 0 0 6px; font-weight: 500; }
.gate-card p { color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.gate-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.gate-card button { width: 100%; margin-top: 18px; padding: 10px; }
.error { color: var(--danger); font-size: 13px; margin: 10px 0 0; }

/* ---------------------------------------------------------------- шапка */

.shell { display: flex; flex-direction: column; height: 100vh; }

.top { background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px 0; flex: none; }
.top-line { display: flex; align-items: center; gap: 12px; }
.brand { font-weight: 500; font-size: 15px; }
.top-actions { margin-left: auto; display: flex; gap: 4px; }

.link { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.link::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.link[data-state="on"] { color: #6cc070; }
.link[data-state="on"]::before { background: #6cc070; }

.accounts { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0; }
.account {
  flex: 0 0 auto;
  background: var(--panel-2);
  border-radius: 10px;
  padding: 7px 14px 7px 8px;
  color: var(--text);
  display: flex; align-items: center; gap: 10px;
  text-align: left;
}
.account:hover { background: var(--hover); }
.account[aria-pressed="true"] { background: var(--active); }
.account[aria-pressed="true"]:hover { background: #33608f; }
.account .lines { display: grid; }
.account .name { font-size: 14px; font-weight: 500; }
.sub { font-size: 12px; color: var(--muted); }
.account[aria-pressed="true"] .sub { color: #b6cfe8; }
.account.broken .name { color: var(--danger); }
.account .badge {
  font-size: 12px; font-weight: 600; color: #fff; background: var(--badge);
  border-radius: 10px; padding: 1px 7px; min-width: 20px; text-align: center;
}
.account[aria-pressed="true"] .badge { background: #fff; color: var(--active); }

/* ---------------------------------------------------------------- тело */

.body { flex: 1; display: grid; grid-template-columns: var(--rail-w) 1fr; min-height: 0; }

.rail { background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.rail-head { padding: 10px 12px; display: flex; gap: 8px; align-items: center; }
.rail-head button { flex: none; }

.dialogs { overflow-y: auto; flex: 1; padding: 0 6px 8px; }
.dialog {
  display: flex; align-items: center; gap: 11px;
  width: 100%; text-align: left; background: none; color: var(--text);
  border-radius: 10px; padding: 8px 9px; margin-bottom: 1px;
}
.dialog:hover { background: var(--hover); }
.dialog[aria-pressed="true"] { background: var(--active); }
.dialog-main { flex: 1; min-width: 0; display: grid; gap: 3px; }
.dialog .row { display: flex; align-items: center; gap: 8px; }
.dialog .title {
  font-weight: 500; font-size: 14.5px; flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dialog .time { font-size: 12px; color: var(--muted); flex: none; }
.dialog .snippet {
  font-size: 13px; color: var(--muted); flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dialog .count {
  flex: none; font-size: 12px; font-weight: 600; color: #fff; background: var(--badge);
  border-radius: 11px; padding: 1px 7px; min-width: 21px; text-align: center;
}
.dialog[aria-pressed="true"] .time,
.dialog[aria-pressed="true"] .snippet { color: #b6cfe8; }
.dialog[aria-pressed="true"] .count { background: #fff; color: var(--active); }

/* ---------------------------------------------------------------- сообщения */

.pane { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.pane-head {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.pane-title { flex: 1; min-width: 0; }
.pane-title h2 {
  font-size: 15px; font-weight: 500; margin: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pane-title p { margin: 0; font-size: 12.5px; color: var(--muted); }
.pane-actions { display: flex; gap: 4px; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 14px 16px 24px; }
.empty { color: var(--muted); text-align: center; margin-top: 18vh; font-size: 14px; }

.msg {
  max-width: min(70%, 62ch);
  background: var(--bubble-in);
  border-radius: 12px;
  padding: 6px 11px 5px;
  margin-bottom: 3px;
  width: fit-content;
}
.msg.out { background: var(--bubble-out); margin-left: auto; }
.msg .who { font-size: 13.5px; font-weight: 500; color: var(--accent); display: block; }
.msg.out .who { display: none; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14.5px; }
.msg .when { font-size: 11.5px; color: var(--muted); text-align: right; margin-top: 1px; }
.msg.out .when { color: #9fc3e8; }
.msg .attach { margin-top: 4px; font-size: 13.5px; }
.msg .attach a { color: var(--accent); }
.msg .attach button { background: rgba(255,255,255,.08); color: var(--text); padding: 5px 11px; font-size: 13px; }
.msg .attach button:hover { background: rgba(255,255,255,.14); }
.msg img { max-width: 100%; border-radius: 8px; margin-top: 5px; display: block; }
.msg .where { font-size: 12px; color: var(--muted); margin-top: 3px; }

.day { text-align: center; margin: 14px 0 8px; }
.day span {
  background: rgba(0, 0, 0, .35); color: #c8d4de;
  font-size: 12.5px; padding: 3px 12px; border-radius: 12px;
}

/* ---------------------------------------------------------------- выгрузки и уведомления */

.jobs { position: fixed; left: 16px; bottom: 14px; display: grid; gap: 8px; z-index: 30; width: min(320px, 90vw); max-height: 45vh; overflow-y: auto; }
.job { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.job .head { display: flex; align-items: center; gap: 8px; }
.job .name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job .close { background: none; padding: 0 4px; color: var(--muted); font-size: 16px; line-height: 1; }
.job .bar { height: 3px; background: rgba(255,255,255,.12); margin-top: 8px; border-radius: 2px; overflow: hidden; }
.job .bar i { display: block; height: 100%; background: var(--accent); transition: width .2s; }
.job .note { margin-top: 4px; font-size: 12px; color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job .stop { margin-top: 6px; padding: 4px 10px; font-size: 12.5px; }
.job .links { margin-top: 7px; display: flex; gap: 12px; }
.job a { color: var(--accent); }
.job.failed { box-shadow: inset 3px 0 0 var(--danger); }
.job.failed .name { color: var(--danger); }

.toasts { position: fixed; right: 16px; bottom: 14px; display: grid; gap: 8px; z-index: 30; width: min(330px, 90vw); }
.toast {
  background: var(--panel-2); border-radius: 10px; padding: 10px 12px; cursor: pointer;
  display: flex; gap: 10px; align-items: center;
}
.toast:hover { background: var(--hover); }
.toast .lines { min-width: 0; }
.toast .from { font-weight: 500; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast .body { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast.alert { box-shadow: inset 3px 0 0 var(--danger); }
.toast.alert .body { white-space: normal; overflow: visible; }

/* ---------------------------------------------------------------- узкий экран */

@media (max-width: 760px) {
  .body { grid-template-columns: 1fr; }
  .shell[data-view="chat"] .rail { display: none; }
  .shell[data-view="chat"] .back { display: inline-block; }
  .shell[data-view="rail"] .pane { display: none; }
  .msg { max-width: 85%; }
  .pane-actions { gap: 0; }
  .jobs, .toasts { width: calc(100vw - 32px); }
}

@media (prefers-reduced-motion: no-preference) {
  .toast, .job { animation: rise .16s ease-out; }
  @keyframes rise { from { transform: translateY(6px); opacity: 0; } }
}

.bulk-export { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 8px 0 12px; font-size: 13px; }
.bulk-export label { display: inline-flex; align-items: center; gap: 6px; }
.bulk-export input[type="number"] { width: 95px; padding: 6px 8px; }
.bulk-export input[type="checkbox"] { width: auto; }
.job .links { flex-wrap: wrap; }

.export-overlay{position:fixed;inset:0;z-index:1000;background:#000a;display:flex;align-items:center;justify-content:center;padding:16px}
.export-overlay[hidden]{display:none}
.export-settings{background:#182633;color:#edf3f8;border:1px solid #405064;border-radius:16px;padding:22px;width:min(860px,100%);max-height:90vh;overflow:auto;box-shadow:0 20px 80px #0008}
.settings-top,.export-fields{display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap}
.export-fields label{display:flex;flex-direction:column;gap:6px}
.export-settings input[type=number]{width:160px}
.export-settings input[type=checkbox]{width:auto;margin-right:8px}
.export-settings fieldset{display:flex;flex-wrap:wrap;gap:14px;margin:12px 0;border:1px solid #405064;border-radius:8px}
.export-preview-list{max-height:34vh;min-height:60px;overflow:auto;margin:12px 0;border:1px solid #405064;border-radius:8px}
.export-preview-row{display:flex;gap:10px;padding:12px;border-bottom:1px solid #344353;align-items:flex-start;overflow-wrap:anywhere}
.export-preview-row.excluded{opacity:.65}
.export-preview-row input{flex-shrink:0;margin-top:5px}
.export-preview-row .sub{display:block}
@media(max-width:600px){.export-overlay{padding:6px}.export-settings{padding:14px;max-height:96vh}.export-preview-list{max-height:30vh}}
