:root {
  --fs: 19px;
  --bg: #0f1220; --panel: #171a2e; --panel2: #1f2340; --text: #eef0ff; --muted: #9aa0c6;
  --accent: #7c6cff; --accent-d: #5a49e6; --mine: #4b3fd1; --mine-text: #fff; --other: #232849;
  --danger: #ff6b79; --line: rgba(255,255,255,.09); --shadow: 0 8px 30px rgba(0,0,0,.45);
  color-scheme: dark;
}
[data-theme="light"] {
  --bg: #eef0f8; --panel: #ffffff; --panel2: #f1f2fb; --text: #15172b; --muted: #636a91;
  --accent: #5a49e6; --accent-d: #4636c9; --mine: #5a49e6; --mine-text: #fff; --other: #ffffff;
  --danger: #d6293e; --line: rgba(20,22,50,.1); --shadow: 0 8px 30px rgba(20,22,50,.18);
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: var(--fs); }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.4; overflow: hidden; overscroll-behavior: none;
}
.sprite { position: absolute; width: 0; height: 0; }
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
svg { width: 1.4rem; height: 1.4rem; fill: currentColor; display: block; }

#app { display: grid; grid-template-columns: 100%; height: 100dvh; width: 100%; }
#listPane, #chatPane { min-width: 0; min-height: 0; height: 100dvh; background: var(--bg); display: flex; flex-direction: column; }
#chatPane { background: var(--bg); position: relative; }
#app:not(.chat-open) #chatPane { display: none; }
#app.chat-open #listPane { display: none; }
.only-mobile { display: grid; }

@media (min-width: 860px) {
  #app { grid-template-columns: minmax(20rem, 24rem) 1fr; }
  #app:not(.chat-open) #chatPane, #app.chat-open #listPane { display: flex; }
  #listPane { border-right: 1px solid var(--line); }
  .icon-btn.only-mobile { display: none; }
}

/* верхняя панель */
.bar {
  display: flex; align-items: center; gap: .25rem; position: relative;
  padding: calc(.5rem + env(safe-area-inset-top)) .6rem .5rem .6rem;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: none; min-height: 3.6rem;
}
.brand { font-weight: 800; letter-spacing: .12em; font-size: 1.15rem; padding-left: .4rem;
  background: linear-gradient(90deg, #ad9bff, #7c6cff); -webkit-background-clip: text; background-clip: text; color: transparent; }
.spacer { flex: 1; }
.icon-btn { background: none; border: 0; width: 2.9rem; height: 2.9rem; border-radius: 50%; display: grid; place-items: center; color: var(--text); cursor: pointer; flex: none; }
.icon-btn:hover { background: var(--panel2); }
.icon-btn:active { background: var(--line); }
.title-wrap { flex: 1; min-width: 0; padding: 0 .3rem; }
.chat-title { font-weight: 700; font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status.ok::before { content: ""; display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; background: #3ddc97; margin-right: .35rem; vertical-align: .05rem; }

/* список чатов */
#chatList { flex: 1; overflow-y: auto; }
.chat-item { display: flex; gap: .8rem; align-items: center; padding: .8rem 1rem; cursor: pointer; border-bottom: 1px solid var(--line); }
.chat-item:hover, .chat-item.active { background: var(--panel2); }
.avatar { width: 3.2rem; height: 3.2rem; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 1.3rem; color: #fff; flex: none; }
.ci-main { flex: 1; min-width: 0; }
.ci-top { display: flex; gap: .5rem; align-items: baseline; }
.ci-title { font-weight: 650; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { font-size: .75rem; color: var(--muted); flex: none; }
.ci-bottom { display: flex; gap: .5rem; align-items: center; }
.ci-last { flex: 1; color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--accent); color: #fff; font-size: .75rem; font-weight: 700; min-width: 1.5rem; height: 1.5rem; border-radius: .75rem; display: grid; place-items: center; padding: 0 .4rem; flex: none; }
.empty { padding: 2rem 1.4rem; text-align: center; display: flex; flex-direction: column; gap: .8rem; align-items: stretch; }
.empty-title { font-size: 1.3rem; font-weight: 700; }
.empty p { color: var(--muted); margin: 0 0 .5rem; }
.link-btn { background: none; border: 0; color: var(--accent); padding: 1rem; cursor: pointer; font-weight: 600; }
.no-chat { margin: auto; text-align: center; color: var(--muted); font-size: 1.1rem; padding: 2rem; }

/* чат */
#chatView { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#messages { flex: 1; overflow-y: auto; padding: .8rem .7rem; display: flex; flex-direction: column; gap: .3rem; scroll-behavior: auto; }
.day { align-self: center; background: var(--panel2); color: var(--muted); font-size: .8rem; padding: .2rem .8rem; border-radius: 1rem; margin: .6rem 0; }
.msg { display: flex; max-width: 100%; }
.msg.mine { justify-content: flex-end; }
.bubble { max-width: min(85%, 34rem); padding: .5rem .75rem .35rem; border-radius: 1.1rem; background: var(--other); border: 1px solid var(--line); overflow-wrap: anywhere; word-break: break-word; }
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); border-color: transparent; border-bottom-right-radius: .3rem; }
.msg:not(.mine) .bubble { border-bottom-left-radius: .3rem; }
.who { font-size: .85rem; font-weight: 700; margin-bottom: .1rem; color: hsl(var(--h) 75% 74%); }
[data-theme="light"] .who { color: hsl(var(--h) 65% 36%); }
.txt { white-space: pre-wrap; font-size: 1rem; }
.txt a { color: inherit; text-decoration: underline; }
.meta { font-size: .72rem; opacity: .65; text-align: right; margin-top: .1rem; user-select: none; }
.msg.pending .bubble { opacity: .7; }
.msg.failed .bubble { border: 1px solid var(--danger); }
.retry { color: var(--danger); font-weight: 700; cursor: pointer; text-decoration: underline; }
.bubble img.photo { display: block; max-width: 100%; max-height: 22rem; border-radius: .7rem; cursor: zoom-in; margin: .1rem 0; }
.media-ph { min-width: 12rem; min-height: 6rem; display: grid; place-items: center; color: var(--muted); font-size: .9rem; }
.bubble audio { width: min(18rem, 70vw); height: 2.6rem; display: block; }
.bubble video { max-width: 100%; max-height: 22rem; border-radius: .7rem; display: block; }
.file-chip { display: flex; gap: .6rem; align-items: center; cursor: pointer; padding: .2rem 0; }
.file-chip .fi { font-size: 1.6rem; }
.file-chip .fn { font-weight: 600; overflow-wrap: anywhere; }
.file-chip .fs { font-size: .8rem; opacity: .7; }

/* поле ввода */
#composer { flex: none; background: var(--panel); border-top: 1px solid var(--line); padding: .5rem .5rem calc(.5rem + env(safe-area-inset-bottom)); }
.input-row { display: flex; align-items: flex-end; gap: .35rem; }
#msgInput { flex: 1; resize: none; max-height: 8rem; min-height: 2.9rem; padding: .6rem .9rem; border-radius: 1.5rem; border: 1px solid var(--line); background: var(--bg); outline: none; line-height: 1.35; font-size: 1rem; }
#msgInput:focus { border-color: var(--accent); }
.send-btn { width: 2.9rem; height: 2.9rem; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.send-btn:active { background: var(--accent-d); }
.rec-bar { display: flex; align-items: center; gap: .6rem; min-height: 2.9rem; padding: 0 .3rem; }
.rec-dot { width: .8rem; height: .8rem; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .25; } }

/* меню */
.menu { position: absolute; right: .6rem; top: calc(100% - .2rem); background: var(--panel); border: 1px solid var(--line); border-radius: .9rem; box-shadow: var(--shadow); z-index: 20; overflow: hidden; min-width: 14rem; }
.menu button { display: block; width: 100%; text-align: left; padding: .85rem 1.1rem; background: none; border: 0; cursor: pointer; }
.menu button:hover { background: var(--panel2); }
.menu .danger { color: var(--danger); }

/* диалоги */
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 1.2rem; padding: 1.2rem; width: min(92vw, 28rem); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(5,6,15,.6); }
dialog h2 { margin: 0 0 .7rem; font-size: 1.3rem; }
dialog label { display: block; margin: .8rem 0 .3rem; color: var(--muted); font-size: .9rem; }
dialog input[type="text"], dialog textarea, dialog select { width: 100%; padding: .7rem .85rem; border-radius: .8rem; border: 1px solid var(--line); background: var(--bg); outline: none; font-size: 1rem; }
dialog textarea { resize: none; }
dialog input:focus, dialog textarea:focus, dialog select:focus { border-color: var(--accent); }
dialog input[type="range"] { width: 100%; accent-color: var(--accent); }
.dlg-actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1.1rem; flex-wrap: wrap; }
.set-row { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: 1rem; }
.hint { color: var(--muted); font-size: .85rem; margin: .5rem 0 0; }
.btn { border: 1px solid var(--line); background: var(--panel2); padding: .7rem 1.1rem; border-radius: .9rem; cursor: pointer; font-weight: 600; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn:active { filter: brightness(.9); }

#viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); display: grid; place-items: center; z-index: 50; cursor: zoom-out; }
#viewer img { max-width: 96vw; max-height: 94dvh; object-fit: contain; }
#toast { position: fixed; left: 50%; bottom: calc(5.5rem + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(2rem); background: #2a2f57; color: #fff; padding: .7rem 1.1rem; border-radius: 1rem; opacity: 0; pointer-events: none; transition: .25s; z-index: 60; max-width: 90vw; text-align: center; box-shadow: var(--shadow); }
#toast.show { opacity: 1; transform: translateX(-50%); }
#fatal { position: fixed; inset: 0; background: var(--bg); z-index: 100; display: grid; place-items: center; padding: 2rem; text-align: center; font-size: 1.1rem; }
