/* Панель «Чаты пациентов». Без внешних ресурсов (CSP: только свой домен) — шрифты системные. */
:root {
  --paper: #f6f3ee;
  --panel: #fffdf9;
  --ink: #1f2a2c;
  --ink-2: #5b6668;
  --ink-3: #8b9496;
  --line: #e6e0d6;
  --line-2: #efe9e0;
  --teal: #0f5f5c;
  --teal-2: #0b4a47;
  --teal-soft: #e3efed;
  --coral: #c8452f;
  --coral-soft: #fbe9e4;
  --amber: #a76a05;
  --amber-soft: #fbf1dc;
  --ai: #eef0f0;
  --patient: #ffffff;
  --tg: #2a8bd0;
  --max: #6a4fd8;
  --web: #2e8b57;
  --shadow: 0 1px 0 rgba(31, 42, 44, .04), 0 6px 24px -12px rgba(31, 42, 44, .18);
  --radius: 12px;
  --font: "Segoe UI Variable Text", "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PT Sans", sans-serif;
  --display: "Iowan Old Style", "Charter", "Georgia", "PT Serif", serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Consolas", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #121718;
    --panel: #182022;
    --ink: #e6ecec;
    --ink-2: #a7b3b4;
    --ink-3: #74807f;
    --line: #26302f;
    --line-2: #1f2828;
    --teal: #3fb5a9;
    --teal-2: #5fcabe;
    --teal-soft: #173431;
    --coral: #ff7a60;
    --coral-soft: #3a1f1a;
    --amber: #e6a93a;
    --amber-soft: #33290f;
    --ai: #1f292a;
    --patient: #222c2d;
    --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 8px 28px -14px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.boot { display: grid; place-items: center; height: 100%; color: var(--ink-3); }

/* бренд: «капля» — две дуги, отсылка к клетке */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: .01em; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 50%; flex: none;
  background: radial-gradient(circle at 35% 35%, var(--panel) 0 22%, transparent 23%), var(--teal);
  box-shadow: inset 0 0 0 3px var(--teal-soft);
}
.brand-mark.big { width: 56px; height: 56px; box-shadow: inset 0 0 0 8px var(--teal-soft); opacity: .8; }

/* кнопки */
.btn {
  border: 1px solid transparent; border-radius: 999px; padding: 8px 16px; font-weight: 600; font-size: 14px;
  transition: background .15s, border-color .15s, transform .05s; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--teal-2); }
@media (prefers-color-scheme: dark) { .btn-primary { color: #0d1a19; } }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-ghost:hover { border-color: var(--ink-3); }
.link-btn { background: none; border: 0; padding: 0; color: var(--ink-2); font-size: 13px; text-align: left; }
.link-btn:hover { color: var(--ink); text-decoration: underline; }
.form-error { color: var(--coral); font-size: 13px; margin: 0; min-height: 0; }
.form-error:empty { display: none; }
.muted { color: var(--ink-3); }

/* вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px;
  background:
    radial-gradient(1200px 500px at 10% -10%, var(--teal-soft), transparent 60%),
    radial-gradient(800px 400px at 110% 110%, var(--amber-soft), transparent 60%), var(--paper); }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px;
}
.login-card h1 { font: 600 26px/1.2 var(--display); margin: 6px 0 4px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--ink-2); }
.login-card input {
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--paper); font-size: 15px;
}
.login-card .btn { padding: 11px; }
.login-note { font-size: 12px; color: var(--ink-3); margin: 0; }

/* раскладка */
.app { display: grid; grid-template-columns: 220px minmax(300px, 380px) 1fr; height: 100vh; height: 100dvh; }

.rail { background: var(--paper); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 12px; gap: 18px; min-height: 0; }
.rail .brand { padding: 0 8px; }
.filters { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.filters .sep { height: 1px; background: var(--line); margin: 8px 8px; }
.filters button {
  width: 100%; display: flex; align-items: center; gap: 10px; background: none; border: 0; border-radius: 8px;
  padding: 8px 10px; text-align: left; color: var(--ink-2); font-size: 14px;
}
.filters button:hover { background: var(--line-2); color: var(--ink); }
.filters button.active { background: var(--panel); color: var(--ink); font-weight: 600; box-shadow: var(--shadow); }
.count { margin-left: auto; font: 600 12px/1 var(--mono); color: var(--ink-3); }
.count:empty { display: none; }
.count.alert { background: var(--coral); color: #fff; border-radius: 999px; padding: 4px 7px; }
.ch { width: 9px; height: 9px; border-radius: 3px; display: inline-block; flex: none; background: var(--ink-3); }
.ch-tg { background: var(--tg); } .ch-max { background: var(--max); } .ch-web { background: var(--web); }

.rail-foot { margin-top: auto; display: grid; gap: 10px; padding: 0 8px; font-size: 13px; }
.live { display: flex; align-items: center; gap: 8px; color: var(--ink-3); font-size: 12px; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); }
.live.ok i { background: var(--web); box-shadow: 0 0 0 3px color-mix(in srgb, var(--web) 25%, transparent); }
.live.down i { background: var(--coral); }
.me { display: flex; justify-content: space-between; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; color: var(--ink); }

/* список */
.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 14px; border-bottom: 1px solid var(--line); }
.list-head input {
  width: 100%; border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; background: var(--paper); font-size: 14px;
}
.conv-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.conv {
  position: relative; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 11px 12px 11px 16px;
  border-radius: 10px; cursor: pointer; border: 1px solid transparent;
}
.conv::before { /* полоса канала */
  content: ""; position: absolute; left: 5px; top: 12px; bottom: 12px; width: 3px; border-radius: 3px; background: var(--ink-3);
}
.conv.tg::before { background: var(--tg); } .conv.max::before { background: var(--max); } .conv.web::before { background: var(--web); }
.conv:hover { background: var(--paper); }
.conv.active { background: var(--teal-soft); border-color: color-mix(in srgb, var(--teal) 25%, transparent); }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font: 12px var(--mono); color: var(--ink-3); align-self: center; }
.conv-last { grid-column: 1 / -1; color: var(--ink-2); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-last b { font-weight: 500; color: var(--ink-3); }
.conv-tags { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.conv-tags:empty { display: none; }
.tag { font-size: 11px; font-weight: 600; border-radius: 999px; padding: 2px 8px; background: var(--line-2); color: var(--ink-2); }
.tag.esc { background: var(--coral-soft); color: var(--coral); }
.tag.human { background: var(--teal-soft); color: var(--teal); }
.tag.wait { background: var(--amber-soft); color: var(--amber); }
.conv.waiting .conv-name::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--coral); margin-left: 7px; vertical-align: middle; animation: pulse 1.6s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
.conv.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--amber-soft); } }
.empty { text-align: center; color: var(--ink-3); padding: 24px; }

/* диалог */
.chat-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0;
  background: radial-gradient(900px 400px at 100% 0, color-mix(in srgb, var(--teal-soft) 60%, transparent), transparent 70%), var(--paper); }
.chat-empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--ink-2); }
.chat-empty-inner { display: grid; justify-items: center; gap: 4px; }
.chat-body { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 14px; padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.back { display: none; background: none; border: 0; font-size: 20px; padding: 4px 8px; }
.who { min-width: 0; flex: 1 1 220px; }
.who-name { display: flex; align-items: center; gap: 8px; font: 600 20px/1.2 var(--display); }
.who-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-meta { color: var(--ink-2); font-size: 13px; display: flex; gap: 12px; flex-wrap: wrap; margin-top: 2px; }
.who-meta a { color: inherit; }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.status { font-size: 12px; font-weight: 600; padding: 5px 10px; border-radius: 999px; background: var(--ai); color: var(--ink-2); }
.status.human { background: var(--teal-soft); color: var(--teal); }

.esc-banner {
  margin: 12px 20px 0; padding: 12px 14px; border-radius: var(--radius); background: var(--coral-soft);
  border: 1px solid color-mix(in srgb, var(--coral) 30%, transparent); color: var(--ink); font-size: 14px;
}
.esc-banner strong { color: var(--coral); }

.timeline { flex: 1; overflow-y: auto; padding: 18px 20px 8px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font: 12px var(--mono); color: var(--ink-3); margin: 12px 0 4px; letter-spacing: .04em; }
.msg { max-width: min(640px, 78%); display: grid; gap: 3px; }
.msg .who-line { font-size: 11px; color: var(--ink-3); display: flex; gap: 8px; padding: 0 4px; }
.msg .who-line time { font-family: var(--mono); }
.bubble { padding: 9px 13px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.patient { align-self: flex-start; }
.msg.patient .bubble { background: var(--patient); border: 1px solid var(--line); border-top-left-radius: 4px; box-shadow: var(--shadow); }
.msg.ai, .msg.admin { align-self: flex-end; }
.msg.ai .who-line, .msg.admin .who-line { justify-content: flex-end; }
.msg.ai .bubble { background: var(--ai); color: var(--ink-2); border-top-right-radius: 4px; }
.msg.admin .bubble { background: var(--teal); color: #fff; border-top-right-radius: 4px; }
@media (prefers-color-scheme: dark) { .msg.admin .bubble { color: #0d1a19; } }
.msg.new { animation: rise .25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.sys { align-self: center; max-width: 80%; text-align: center; font-size: 12px; color: var(--ink-3); padding: 4px 0; }
.sys.esc {
  color: var(--ink); background: var(--coral-soft); border-radius: 10px; padding: 8px 12px; font-size: 13px;
  border-left: 3px solid var(--coral); text-align: left;
}
.sys.esc.resolved { background: var(--line-2); border-left-color: var(--ink-3); color: var(--ink-2); }

.composer { padding: 10px 20px 16px; display: grid; gap: 6px; }
.composer-hint { margin: 0; font-size: 12px; color: var(--ink-3); }
.composer-hint:empty { display: none; }
.composer-row { display: flex; gap: 10px; align-items: flex-end; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 14px; box-shadow: var(--shadow); }
.composer-row:focus-within { border-color: var(--teal); }
.composer textarea { flex: 1; border: 0; resize: none; background: transparent; outline: none; max-height: 200px; padding: 6px 0; }
.composer.disabled .composer-row { opacity: .6; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--paper);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); z-index: 10; max-width: calc(100% - 32px);
}

/* диалог + карточка */
.chat-split { flex: 1; display: flex; min-height: 0; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.card-toggle[aria-pressed="true"] { background: var(--teal-soft); border-color: transparent; color: var(--teal); }

/* шаблоны быстрых ответов */
.composer { position: relative; }
.tpl-btn {
  flex: none; align-self: flex-end; width: 34px; height: 34px; margin-left: -6px; border: 0; border-radius: 10px;
  background: transparent; color: var(--ink-3); display: grid; place-items: center;
}
.tpl-btn svg { width: 18px; height: 18px; }
.tpl-btn:hover, .tpl-btn[aria-expanded="true"] { background: var(--teal-soft); color: var(--teal); }
.tpl-pop {
  position: absolute; left: 20px; right: 20px; bottom: calc(100% - 4px); z-index: 5; max-width: 560px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 18px 48px -18px rgba(31, 42, 44, .35);
  display: flex; flex-direction: column; max-height: min(420px, 60vh); overflow: hidden; animation: rise .16s ease-out;
}
.tpl-head { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line-2); }
.tpl-head input { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; background: var(--paper); outline: none; }
.tpl-head input:focus { border-color: var(--teal); }
.tpl-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.tpl-item { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 8px 10px; border-radius: 9px; cursor: pointer; }
.tpl-item b { font-size: 14px; }
.tpl-item .tpl-prev { grid-column: 1; font-size: 13px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tpl-item .tpl-ed { grid-row: 1 / span 2; grid-column: 2; align-self: center; opacity: 0; }
.tpl-item:hover .tpl-ed, .tpl-item.sel .tpl-ed { opacity: 1; }
.tpl-item.sel, .tpl-item:hover { background: var(--teal-soft); }
.tpl-empty { padding: 14px 10px; color: var(--ink-3); font-size: 13px; }
.tpl-edit { display: grid; gap: 8px; padding: 12px; }
.tpl-edit input, .tpl-edit textarea {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--paper); outline: none; resize: vertical;
}
.tpl-edit input:focus, .tpl-edit textarea:focus { border-color: var(--teal); }
.tpl-edit-actions { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }
.link-btn.danger { color: var(--coral); }
.tpl-foot { margin: 0; padding: 6px 12px 8px; font: 11px var(--mono); color: var(--ink-3); border-top: 1px solid var(--line-2); }

/* карточка пациента */
.card {
  flex: none; width: 300px; border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto;
  display: flex; flex-direction: column;
}
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.card-head h2 { margin: 0; font: 600 16px/1.2 var(--display); }
.card-body { display: grid; gap: 0; }
.card-sec { padding: 12px 16px; border-top: 1px solid var(--line-2); display: grid; gap: 8px; }
.card-sec:first-child { border-top: 0; }
.card-sec h3 { margin: 0; font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); font-weight: 600; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 4px 10px; font-size: 13px; margin: 0; }
.kv dt { color: var(--ink-3); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.kv dd a { color: var(--teal); }
.copy { margin-left: 6px; font-size: 11px; }
.bk { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.bk li { font-size: 13px; padding: 8px 10px; border-radius: 9px; background: var(--paper); border: 1px solid var(--line-2); display: grid; gap: 2px; }
.bk li.past { opacity: .65; }
.bk .when { font-family: var(--mono); font-size: 12px; }
.bk .st { justify-self: start; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--line-2); color: var(--ink-2); }
.bk .st.pending { background: var(--amber-soft); color: var(--amber); }
.bk .st.confirmed { background: var(--teal-soft); color: var(--teal); }
.bk .st.cancelled { background: var(--coral-soft); color: var(--coral); }
.card .muted, .note-row .muted { font-size: 12px; color: var(--ink-3); }
#note { border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; background: var(--paper); resize: vertical; min-height: 84px; outline: none; font-size: 14px; }
#note:focus { border-color: var(--teal); }
.note-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.note-row .btn { padding: 5px 12px; font-size: 13px; }
.card-backdrop { display: none; }

/* широкий экран — карточка рядом; уже — выезжает поверх */
@media (max-width: 1279px) {
  .chat-pane { position: relative; }
  .card {
    position: absolute; top: 0; right: 0; bottom: 0; z-index: 6; width: min(340px, 92%);
    box-shadow: -18px 0 48px -24px rgba(31, 42, 44, .45); animation: slide .18s ease-out;
  }
  @keyframes slide { from { transform: translateX(16px); opacity: 0; } }
}

/* планшет / узкий ноутбук */
@media (max-width: 1100px) {
  .app { grid-template-columns: 172px minmax(260px, 320px) 1fr; }
  .rail { padding: 14px 8px; }
  .chat-head { flex-wrap: wrap; }
}
/* телефон: список или диалог */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .rail { flex-direction: row; padding: 8px 10px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; gap: 8px; align-items: center; }
  .rail .brand { display: none; }
  .filters { display: flex; gap: 4px; }
  .filters .sep { display: none; }
  .filters button { font-size: 13px; white-space: nowrap; padding: 6px 10px; }
  .filters button::before { content: none !important; }
  .filters button .count { position: static; }
  .rail-foot { margin: 0 0 0 auto; display: flex; }
  .list-pane, .chat-pane { grid-column: 1; grid-row: 2; border: 0; }
  .app.chat-open .list-pane { display: none; }
  .app:not(.chat-open) .chat-pane { display: none; }
  .back { display: block; }
  .chat-head { padding: 10px 12px; flex-wrap: wrap; }
  .actions { width: 100%; justify-content: flex-start; }
  .timeline { padding: 12px; }
  .msg { max-width: 88%; }
  .composer { padding: 8px 12px 12px; }
  .esc-banner { margin: 10px 12px 0; }
  .tpl-pop { left: 12px; right: 12px; }
  .card { width: 100%; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.who-meta .online { color: var(--teal); font-weight: 600; }
.sec-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-sec p.muted { margin: 0; }

/* ---- смена пароля ---- */
.pw-dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--panel); color: var(--ink); padding: 22px; width: min(380px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.pw-dialog::backdrop { background: rgba(20, 28, 28, .45); }
.pw-dialog h2 { font: 600 20px/1.2 var(--display); margin: 0 0 14px; }
.pw-dialog form { display: grid; gap: 12px; }
.pw-dialog label { display: grid; gap: 6px; font-size: 13px; color: var(--ink-2); }
.pw-dialog label small { color: var(--ink-3); }
.pw-dialog input { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--paper); color: var(--ink); font-size: 15px; }
.pw-error { margin: 0; font-size: 13px; color: #b3402a; }
@media (prefers-color-scheme: dark) { .pw-error { color: #f08a72; } }
.pw-actions { display: flex; justify-content: flex-end; gap: 8px; }
