:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e6e9ee;
  --text: #1b1e24;
  --muted: #6b7280;
  --accent: #f47a3c;
  --me: #eef2f7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f25;
    --line: #2b2f37;
    --text: #e6e8ec;
    --muted: #949cab;
    --me: #262b33;
  }
}

* { box-sizing: border-box; }

/* Панель переключает экраны атрибутом hidden, а у каждого экрана есть свой
   display (flex/grid) — а он в авторском стиле сильнее, чем `[hidden]` из
   стиля браузера. Без этого правила hidden не скрывает ничего: форма входа
   остаётся поверх панели даже после успешного входа. */
[hidden] { display: none !important; }

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

/* --- вход --- */
.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card {
  width: 340px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px 24px; display: flex; flex-direction: column; gap: 10px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card .hint { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.login-card input {
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px;
  background: transparent; color: inherit; font-size: 14px;
}
.login-card button {
  padding: 11px; border: 0; border-radius: 9px; background: var(--accent);
  color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}
.error { color: #d33; font-size: 13px; margin: 4px 0 0; }

/* --- каркас --- */
.app { display: grid; grid-template-columns: 320px 1fr; height: 100vh; }

.sidebar {
  border-right: 1px solid var(--line); background: var(--panel);
  display: flex; flex-direction: column; min-width: 0;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 10px; font-size: 15px;
}
.mode-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: #fff4e5; color: #9a5b00; border: 1px solid #ffd8a8;
}
.mode-badge.prod { background: #ffe9e9; color: #a11; border-color: #ffc9c9; }

.filters { display: flex; gap: 6px; padding: 0 16px 10px; }
.filters button {
  border: 1px solid var(--line); background: transparent; color: var(--muted);
  border-radius: 14px; padding: 4px 12px; font-size: 12px; cursor: pointer;
}
.filters button.active { border-color: var(--accent); color: var(--accent); }

.conversations { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conversations li {
  padding: 11px 16px; border-bottom: 1px solid var(--line); cursor: pointer;
  display: flex; flex-direction: column; gap: 3px;
}
.conversations li:hover { background: var(--bg); }
.conversations li.active { background: var(--bg); border-left: 3px solid var(--accent); padding-left: 13px; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-channel { font-size: 11px; color: var(--muted); white-space: nowrap; }
.conv-last { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar-foot {
  padding: 10px 16px; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; color: var(--muted);
}
.link { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: 12px; padding: 0; }
.link.danger { color: #d33; }

/* --- переписка --- */
.main { display: flex; flex-direction: column; min-width: 0; }
.empty { margin: auto; color: var(--muted); }
.thread, .card-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.thread-actions { display: flex; gap: 14px; }
.muted { color: var(--muted); font-size: 12px; margin-left: 8px; }

.log { flex: 1; overflow-y: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 68%; display: flex; flex-direction: column; gap: 3px; }
.msg .text {
  padding: 9px 13px; border-radius: 13px; white-space: pre-wrap; word-break: break-word;
  background: var(--me);
}
.msg.inbound { align-self: flex-start; }
.msg.outbound { align-self: flex-end; align-items: flex-end; }
.msg.outbound .text { background: var(--accent); color: #fff; }
.msg .meta { font-size: 11px; color: var(--muted); }
.msg.failed .text { opacity: .6; }
.msg .err { font-size: 11px; color: #d33; }

.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 12px 20px; border-top: 1px solid var(--line); background: var(--panel);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 12px; font: inherit; background: transparent; color: inherit; max-height: 140px;
}
.composer select {
  border: 1px solid var(--line); border-radius: 10px; padding: 9px;
  background: transparent; color: inherit; font: inherit;
}
.composer button {
  padding: 10px 18px; border: 0; border-radius: 10px; background: var(--accent);
  color: #fff; font-weight: 600; cursor: pointer;
}
.composer button:disabled { opacity: .5; cursor: not-allowed; }

/* --- каналы клиента --- */
.channels { display: flex; flex-wrap: wrap; gap: 8px; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.channel {
  border: 1px solid var(--line); border-radius: 11px; padding: 8px 12px;
  min-width: 168px; background: var(--panel);
}
.channel .name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.channel .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.channel.linked .dot { background: #2ea34d; }
.channel.unavailable .dot, .channel.opted_out .dot { background: #d33; }
.channel.pending_invite .dot, .channel.unknown .dot { background: #e9a13b; }
.channel .detail { font-size: 11px; color: var(--muted); margin-top: 3px; }

/* --- настройки --- */
.foot-actions { display: flex; gap: 12px; }
.settings-view { display: flex; flex-direction: column; min-height: 0; }
.settings-body { overflow-y: auto; padding: 18px 20px 28px; display: flex; flex-direction: column; gap: 22px; }
.settings-form { display: flex; flex-direction: column; gap: 16px; max-width: 560px; }
.setting { display: flex; flex-direction: column; gap: 5px; }
.setting.inactive { opacity: .55; }
.setting label { font-weight: 600; font-size: 13px; }
.setting input, .setting textarea {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); color: var(--text); font: inherit; resize: vertical;
}
.setting-hint { font-size: 11px; color: var(--muted); }
.settings-actions { display: flex; align-items: center; gap: 12px; }
.settings-actions button {
  padding: 10px 18px; border: 0; border-radius: 10px; background: var(--accent);
  color: #fff; font-weight: 600; cursor: pointer;
}
.settings-note { font-size: 12px; color: var(--muted); }
.settings-note.ok { color: #2ea34d; }
.settings-note.bad { color: #d33; }

/* --- поля с ошибками и паролями --- */
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; }
.field > span:first-child { font-weight: 600; }
.field input {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text); font: inherit; width: 100%;
}
.field-error { font-size: 11px; color: #d33; }
input.invalid, textarea.invalid { border-color: #d33; }

/* Обёртка «поле + глаз»: кнопка лежит поверх правого края поля. */
.pw { position: relative; display: block; }
/* width обязателен: обёртка появляется вокруг уже свёрстанных полей — и на
   форме входа, и в настройках, — а без неё поле схлопывается до размера по
   умолчанию вместо ширины формы. */
.pw input { width: 100%; padding-right: 40px; }
.pw-eye {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; padding: 0; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.pw-eye:hover { color: var(--text); }
.pw-eye:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.password-form { display: flex; flex-direction: column; gap: 12px; }

/* --- сотрудники --- */
.staff-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.staff {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 0; border-bottom: 1px solid var(--line);
}
.staff:last-child { border-bottom: 0; }
.staff.inactive { opacity: .55; }
.staff-who { display: flex; flex-direction: column; min-width: 0; }
.staff-name { font-weight: 600; font-size: 13px; }
.staff-meta { font-size: 11px; color: var(--muted); }
.temp-password {
  border: 1px solid var(--accent); border-radius: 10px; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.staff-form { display: flex; flex-direction: column; gap: 12px; border-top: 1px solid var(--line); padding-top: 14px; }
.staff-form h3 { margin: 0; font-size: 13px; }
.staff-form select {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text); font: inherit; width: 100%;
}
.temp-title { font-size: 12px; }
.temp-value { font-size: 16px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; user-select: all; }
.temp-note { font-size: 11px; color: var(--muted); }

/* --- номерная сессия --- */
.session-box {
  max-width: 560px; border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 18px; background: var(--panel); display: flex; flex-direction: column; gap: 10px;
}
.session-box h2 { margin: 0; font-size: 15px; }
.session-box .hint { margin: 0; font-size: 12px; color: var(--muted); }
.session-state {
  font-size: 13px; padding: 8px 11px; border-radius: 10px;
  background: var(--me); border-left: 3px solid var(--muted);
}
.session-state.running { border-left-color: #2ea34d; }
.session-state.waiting_code, .session-state.waiting_password, .session-state.starting { border-left-color: #e9a13b; }
.session-state.error, .session-state.off { border-left-color: #d33; }
.session-form { display: flex; gap: 8px; }
.session-form input {
  flex: 1; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text); font: inherit;
}
.session-form button {
  padding: 9px 16px; border: 0; border-radius: 10px; background: var(--accent);
  color: #fff; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.session-actions { display: flex; gap: 14px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
