:root {
  --bg: #F4F4F2;
  --surface: #FFFFFF;
  --soft: #F7F7F5;
  --line: #E7E7E4;
  --line-2: #EFEFEC;
  --ink: #17171A;
  --ink-2: #4A4A50;
  --muted: #64646B;
  --accent: #2563EB;
  --accent-soft: #EEF3FE;
  --accent-off: #9DB6EE;
  --select: #F1F4FB;
  --bubble-in: #EFEFEC;
  --danger: #A3231A;
  --danger-btn: #B42318;
  --danger-soft: #FBEAE8;
  --ok: #1F9D55;
  --radius: 16px;
  --font: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font-family: var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- кнопки и общие элементы */
.btn { height: 42px; padding: 0 16px; border-radius: 11px; border: 0; font-size: 14px; font-weight: 500; background: #F1F1EF; color: var(--ink); }
.btn.primary { background: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:disabled { background: var(--accent-off); }
.btn.dark { background: var(--ink); color: #fff; font-weight: 600; }
.btn.danger { background: var(--danger-btn); color: #fff; font-weight: 600; }
.btn.soft { background: #F1F1EF; }
.btn.small { height: 34px; padding: 0 14px; border-radius: 10px; font-size: 13px; background: var(--surface); border: 1px solid var(--line); }
.btn.wide { width: 100%; }
.icon-btn { width: 40px; height: 40px; flex: 0 0 auto; border: 0; border-radius: 10px; background: transparent; display: inline-flex; align-items: center; justify-content: center; color: var(--ink); }
.icon-btn:hover, .icon-btn[aria-expanded="true"] { background: #F1F1EF; }
.icon-btn.filled { width: 44px; height: 44px; border-radius: 12px; background: var(--bg); color: var(--ink-2); }
.pill { display: inline-flex; align-items: center; gap: 8px; height: 34px; padding: 0 12px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 13px; white-space: nowrap; }
.pill.soft { background: var(--bg); border: 0; height: 30px; padding: 0 10px; font-size: 12px; color: var(--ink-2); gap: 6px; }
.pill-btn { font-weight: 500; }
.pill-btn:hover { background: var(--soft); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #C4C4BE; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--danger-btn); }
.mini-avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--ink); color: #fff; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; }
#mePill { padding-left: 4px; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #F1F1EF; color: var(--ink-2); white-space: nowrap; }
.badge.red { background: var(--danger-soft); color: var(--danger); }
.avatar { flex: 0 0 auto; width: 44px; height: 44px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 15px; font-weight: 600; }

/* ---------- вход */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 360px; max-width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 28px; display: flex; flex-direction: column; gap: 8px; }
.login-card .brand { margin-bottom: 14px; }
.login-card label { font-size: 13px; font-weight: 500; color: var(--ink-2); margin-top: 6px; }
.login-card input { height: 44px; padding: 0 12px; border-radius: 11px; border: 1px solid var(--line); outline: none; font-size: 15px; }
.login-card input:focus { border-color: var(--accent); }
.login-card .btn { margin-top: 10px; height: 46px; }
.form-error { min-height: 18px; margin: 4px 0 0; font-size: 13px; color: var(--danger); }

/* ---------- каркас */
.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.topbar { flex: 0 0 auto; min-height: 60px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 20px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo { width: 32px; height: 32px; border-radius: 9px; background: var(--ink); color: #fff; display: inline-flex; align-items: center; justify-content: center; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text b { font-size: 15px; font-weight: 600; }
.brand-text small { font-size: 12px; color: var(--muted); }
.top-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.main { flex: 1 1 auto; min-height: 0; display: flex; gap: 12px; padding: 0 12px 12px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden; min-height: 0; }

/* ---------- список диалогов */
.list { flex: 0 0 360px; }
.list-head { padding: 14px 14px 10px; display: flex; flex-direction: column; gap: 10px; }
.search { display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 12px; border-radius: 10px; background: var(--bg); color: var(--muted); }
.search input { flex: 1; min-width: 0; border: 0; outline: none; background: transparent; font-size: 14px; color: var(--ink); }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { height: 32px; padding: 0 12px; border-radius: 999px; border: 0; font-size: 13px; font-weight: 500; background: var(--bg); color: var(--ink-2); }
.tab[aria-selected="true"] { background: var(--ink); color: #fff; }
.chat-list { flex: 1 1 auto; overflow-y: auto; padding: 0 8px 8px; display: flex; flex-direction: column; gap: 2px; }
.chat-item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px; border: 0; border-radius: 12px; text-align: left; background: transparent; color: var(--ink); }
.chat-item:hover { background: var(--soft); }
.chat-item.active { background: var(--select); }
.ci-body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ci-row { display: flex; align-items: center; gap: 6px; }
.ci-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { margin-left: auto; flex: 0 0 auto; font-size: 12px; color: var(--muted); }
.ci-last { flex: 1 1 auto; min-width: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-icon { flex: 0 0 auto; color: var(--muted); display: inline-flex; }
.unread { flex: 0 0 auto; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; }
.list-empty { margin: 24px 12px; font-size: 13px; color: var(--muted); text-align: center; }
.archive-link { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 12px 16px; border: 0; border-top: 1px solid var(--line-2); background: var(--surface); font-size: 12px; color: var(--muted); text-align: left; }
.archive-link:hover { color: var(--ink); }

/* ---------- переписка */
.chat { flex: 1 1 auto; min-width: 0; position: relative; }
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 14px; }
.chat-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head { position: relative; flex: 0 0 auto; display: flex; align-items: center; gap: 12px; padding: 12px 14px 12px 18px; border-bottom: 1px solid var(--line-2); }
.chat-head .avatar { width: 40px; height: 40px; font-size: 14px; }
.chat-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.chat-name-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-name { font-size: 15px; font-weight: 600; }
.badges { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.chat-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.back { display: none; }
.menu { position: absolute; top: 62px; right: 14px; z-index: 5; width: 264px; padding: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 12px 32px rgba(23,23,26,0.12); display: flex; flex-direction: column; }
.menu-item { display: flex; align-items: center; gap: 12px; height: 42px; padding: 0 12px; border: 0; border-radius: 9px; background: transparent; font-size: 14px; text-align: left; color: var(--ink); }
.menu-item:hover { background: var(--soft); }
.menu-item.danger { color: var(--danger); }
.menu-sep { height: 1px; background: var(--line-2); margin: 6px 8px; }
.arch-banner { flex: 0 0 auto; display: flex; align-items: center; gap: 10px; padding: 10px 18px; background: var(--soft); border-bottom: 1px solid var(--line-2); font-size: 13px; color: var(--ink-2); flex-wrap: wrap; }
.arch-banner span { flex: 1 1 240px; }

.messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; background: #FAFAF9; padding: 20px 24px; display: flex; flex-direction: column; gap: 10px; }
.day { align-self: center; font-size: 12px; color: var(--muted); padding: 4px 10px; border-radius: 999px; background: #F1F1EF; margin: 6px 0; }
.load-more { align-self: center; font-size: 12px; color: var(--muted); }
.msg { max-width: 68%; display: flex; flex-direction: column; gap: 4px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 10px 14px; font-size: 14px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; display: flex; flex-direction: column; gap: 6px; }
.msg.in .bubble { background: var(--bubble-in); border-radius: 16px 16px 16px 6px; }
.msg.out .bubble { background: var(--accent); color: #fff; border-radius: 16px 16px 6px 16px; }
.msg.deleted .bubble { background: var(--surface); color: var(--ink); border: 1px dashed #C4C4BE; }
.del-label { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--danger); white-space: normal; }
.was { font-size: 12px; opacity: 0.85; white-space: pre-wrap; border-top: 1px solid rgba(127,127,127,0.3); padding-top: 6px; }
.meta { font-size: 11px; color: var(--muted); padding: 0 4px; }
.msg.selectable { cursor: pointer; }
.msg.selectable .bubble { outline: 2px solid transparent; outline-offset: 2px; }
.msg.selected .bubble { outline-color: var(--accent); }
.media-btn { align-self: flex-start; display: inline-flex; align-items: center; gap: 8px; height: 34px; padding: 0 12px; border-radius: 10px; border: 0; font-size: 13px; font-weight: 500; background: rgba(255,255,255,0.7); color: var(--ink); text-decoration: none; }
.msg.out .media-btn { background: rgba(255,255,255,0.18); color: #fff; }
.media-img { max-width: 100%; max-height: 320px; border-radius: 10px; display: block; }
.media-player { max-width: 100%; }

.forward-bar { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--line-2); font-size: 14px; }
.forward-bar span { flex: 1; }

.composer { position: relative; flex: 0 0 auto; display: flex; align-items: flex-end; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--line-2); }
.composer textarea { flex: 1 1 auto; min-width: 0; min-height: 44px; max-height: 180px; resize: none; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-size: 14px; line-height: 1.45; outline: none; }
.composer textarea:focus { border-color: #C9D6F5; }
.send-btn { width: 44px; height: 44px; flex: 0 0 auto; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; }
.send-btn:disabled { background: var(--accent-off); }

.slash { position: absolute; left: 14px; right: 14px; bottom: calc(100% + 6px); z-index: 4; max-width: 600px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 12px 32px rgba(23,23,26,0.12); padding: 6px; display: flex; flex-direction: column; }
.slash-head { display: flex; align-items: center; gap: 8px; padding: 6px 10px 8px; }
.slash-title { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: 0.02em; text-transform: uppercase; }
.slash-edit { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px; border-radius: 8px; border: 0; background: var(--bg); font-size: 12px; font-weight: 500; }
.slash-list { max-height: 264px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.slash-item { display: flex; align-items: baseline; gap: 12px; padding: 9px 10px; border: 0; border-radius: 9px; background: transparent; text-align: left; }
.slash-item.active, .slash-item:hover { background: var(--select); }
.slash-cmd { flex: 0 0 120px; font-size: 14px; font-weight: 600; color: var(--accent); overflow: hidden; text-overflow: ellipsis; }
.slash-text { flex: 1 1 auto; min-width: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.slash-empty { padding: 10px; font-size: 13px; color: var(--muted); }

/* ---------- окна */
.backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(23,23,26,0.36); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal > * { flex-shrink: 0; }
.modal { width: 540px; max-width: 100%; max-height: 100%; overflow-y: auto; background: var(--surface); border-radius: 20px; padding: 22px; display: flex; flex-direction: column; gap: 16px; }
.modal.wide { width: 680px; }
.modal.narrow { width: 420px; gap: 10px; }
.modal-head { display: flex; align-items: flex-start; gap: 12px; }
.modal-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.modal-head p { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.modal-head .icon-btn { margin-left: auto; width: 36px; height: 36px; background: var(--bg); }
.modal p.text { margin: 0; font-size: 14px; line-height: 1.5; color: var(--ink-2); }
.modal-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; flex-wrap: wrap; }
.modal-actions .hint { margin-right: auto; font-size: 13px; color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; font-weight: 500; color: var(--ink-2); }
.field input, .field textarea, .field select { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 14px; line-height: 1.45; outline: none; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 84px; }
.prefix { display: flex; align-items: center; height: 42px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); }
.prefix b { color: var(--accent); }
.prefix input { flex: 1; min-width: 0; border: 0; padding: 0 0 0 2px; outline: none; }

.cmd-table { display: flex; flex-direction: column; border: 1px solid var(--line-2); border-radius: 14px; overflow: hidden; }
.cmd-row { display: flex; align-items: flex-start; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line-2); }
.cmd-row:last-child { border-bottom: 0; }
.cmd-row.editing { background: var(--select); }
.cmd-row .slash-cmd { padding-top: 2px; }
.cmd-row .cmd-text { flex: 1 1 auto; min-width: 0; font-size: 13px; line-height: 1.45; color: var(--ink-2); white-space: pre-line; overflow-wrap: anywhere; }
.sq-btn { width: 34px; height: 34px; flex: 0 0 auto; border-radius: 9px; border: 0; background: var(--bg); display: inline-flex; align-items: center; justify-content: center; }
.sq-btn.red { background: var(--danger-soft); color: var(--danger); }
.cmd-form { display: flex; flex-direction: column; gap: 10px; padding: 16px; border-radius: 14px; background: var(--soft); }
.cmd-form-row { display: flex; gap: 10px; flex-wrap: wrap; }
.cmd-form-row .field:first-child { flex: 0 1 180px; }
.cmd-form-row .field:last-child { flex: 1 1 300px; }

.gift-tier { display: flex; flex-direction: column; gap: 8px; }
.gift-tier-title { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.gift-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.gift { height: 84px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; font-size: 12px; font-weight: 500; }
.gift .emoji { font-size: 28px; line-height: 1; }
.gift[aria-pressed="true"] { border: 2px solid var(--accent); background: var(--accent-soft); }

.pick-list { display: flex; flex-direction: column; gap: 2px; max-height: 360px; overflow-y: auto; }
.audit-list { display: flex; flex-direction: column; font-size: 13px; }
.audit-row { display: grid; grid-template-columns: 110px 120px minmax(0, 1fr); gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--line-2); }
.audit-row .what { overflow-wrap: anywhere; }

.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 60; max-width: calc(100% - 32px); padding: 12px 16px; border-radius: 12px; background: var(--ink); color: #fff; font-size: 13px; line-height: 1.4; box-shadow: 0 10px 30px rgba(23,23,26,0.25); }
.toast.error { background: var(--danger-btn); }

/* ---------- телефон */
@media (max-width: 760px) {
  .topbar { padding: 8px 12px; }
  .topbar .brand-text small, #meRole, #tgStatus .pill-text { display: none; }
  .main { padding: 0; gap: 0; }
  .panel { border-radius: 0; border-left: 0; border-right: 0; border-bottom: 0; }
  .list { flex: 1 1 auto; }
  .chat { display: none; }
  body.show-chat .list { display: none; }
  body.show-chat .chat { display: flex; }
  body.show-chat .topbar { display: none; }
  .back { display: inline-flex; }
  .chat-head { padding: 10px 8px 10px 4px; gap: 8px; }
  .retention { display: none; }
  .messages { padding: 16px 12px; }
  .msg { max-width: 84%; }
  .composer { padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); }
  .composer textarea { font-size: 16px; }
  .slash { left: 8px; right: 8px; }
  .slash-item { flex-direction: column; gap: 2px; }
  .slash-cmd { flex: none; }
  .slash-text { max-width: 100%; }
  .backdrop { align-items: flex-end; padding: 0; }
  .modal { border-radius: 22px 22px 0 0; max-height: 92vh; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .audit-row { grid-template-columns: 1fr; gap: 2px; }
  .chat-item { padding: 10px 8px; }
  .avatar { width: 50px; height: 50px; font-size: 16px; }
}

/* ---------- цвета аватаров */
.pal0 { background: #DCE7FB; color: #1E4FB8; }
.pal1 { background: #E2F1E6; color: #1E6B3A; }
.pal2 { background: #F6E8DA; color: #8A4A12; }
.pal3 { background: #EEE6F7; color: #5B2E91; }
.pal4 { background: #E1EEF1; color: #1F5A66; }
.pal5 { background: #F7E3E3; color: #922B2B; }
.modal-title { margin: 0; font-size: 18px; font-weight: 600; }
.form-title { font-size: 14px; }

/* ---------- подарки в переписке */
.gift-msg .gift-card { min-width: 200px; max-width: 280px; padding: 14px 16px; border-radius: 16px; background: #FFF8E8; border: 1px solid #F0E0B8; color: var(--ink); display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.gift-dir { font-size: 12px; font-weight: 600; color: #7A5A12; }
.gift-emoji { font-size: 44px; line-height: 1.1; }
.gift-icon { display: inline-flex; color: #7A5A12; }
.gift-title { font-size: 14px; font-weight: 600; }
.gift-text { font-size: 13px; color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- сотрудники */
.badge.dark { background: var(--ink); color: #fff; }
.staff-list { display: flex; flex-direction: column; border: 1px solid var(--line-2); border-radius: 14px; overflow: hidden; }
.staff-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line-2); flex-wrap: wrap; }
.staff-row:last-child { border-bottom: 0; }
.staff-row.off { opacity: 0.6; }
.staff-row .avatar { width: 40px; height: 40px; font-size: 14px; }
.staff-info { flex: 1 1 200px; min-width: 0; display: flex; flex-direction: column; gap: 3px; font-size: 13px; }
.staff-info b { font-size: 14px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.staff-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.staff-form-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 760px) { .staff-form-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* ---------- внутри Telegram */
body.in-tg #logoutBtn { display: none; }
body.in-tg .app { height: var(--tg-viewport-stable-height, 100dvh); }
body.tg-full .app { padding-top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px)); }
body.tg-full .composer { padding-bottom: calc(10px + var(--tg-safe-area-inset-bottom, 0px)); }
