:root {
  --bg: #f4f7f7;
  --panel: #ffffff;
  --text: #1d2a29;
  --muted: #6b7a79;
  --line: #e3eaea;
  --accent: #3fa79c;
  --accent-strong: #2f8d83;
  --accent-soft: #e2f4f2;
  --mine: #3fa79c;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --danger: #c94a4a;
  --shadow: 0 1px 2px rgba(16, 40, 38, .06), 0 4px 16px rgba(16, 40, 38, .06);
  --radius: 16px;
  --ease-out: cubic-bezier(.23, 1, .32, 1);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c1211; --panel: #111716; --text: #e6efee; --muted: #8a9b99; --line: #1f2a29;
    --accent: #4db6ac; --accent-strong: #6cc7bd; --accent-soft: #15302d;
    --mine: #2c8a80; --theirs: #18201f;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; }
html {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  height: 100%;
}
body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 var(--font); overflow: hidden;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button, a, label, [role="button"] { touch-action: manipulation; }
button, label.icon-btn, .tab, .room { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
input, textarea, select { font: inherit; font-size: 16px; color: var(--text); }
svg { width: 22px; height: 22px; fill: currentColor; display: block; }

/* ---------- кнопки ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent-strong);
  transition: transform 120ms var(--ease-out), background-color 150ms ease;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: transparent; color: var(--danger); }
.btn:active { transform: scale(.97); }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: transparent; color: var(--muted);
  transition: transform 120ms var(--ease-out), background-color 150ms ease, color 150ms ease;
}
.icon-btn:active { transform: scale(.94); background: var(--accent-soft); color: var(--accent-strong); }
@media (hover: hover) and (pointer: fine) {
  .icon-btn:hover { background: var(--accent-soft); color: var(--accent-strong); }
  .btn.primary:hover { background: var(--accent-strong); }
  .room:hover { background: var(--accent-soft); }
}

/* ---------- вход ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px calc(24px + env(safe-area-inset-bottom)); overflow: auto; height: 100%; }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 24px; padding: 28px 22px; box-shadow: var(--shadow); text-align: center; }
.auth-logo { width: 64px; height: 64px; border-radius: 18px; }
.auth h1 { margin: 12px 0 18px; font-size: 22px; }
.tabs { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; margin-bottom: 16px; }
.tab { flex: 1; border: 0; background: transparent; padding: 9px 6px; border-radius: 9px; color: var(--muted); font-weight: 600; font-size: 14px; }
.tab.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.form { display: grid; gap: 10px; }
.form input, .field {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 12px 14px; outline: none;
  transition: border-color 150ms ease;
}
.form input:focus, .field:focus { border-color: var(--accent); }
.error { color: var(--danger); min-height: 1.4em; margin: 12px 0 0; font-size: 14px; }

/* ---------- раскладка ---------- */
.app { display: flex; height: 100dvh; }
.sidebar { width: 340px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: calc(10px + env(safe-area-inset-top)) 10px 10px 16px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.brand img { width: 30px; height: 30px; border-radius: 9px; }
.head-actions { display: flex; gap: 2px; }
.room-list { overflow-y: auto; overscroll-behavior: contain; flex: 1; padding: 6px; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
.room {
  display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: transparent; text-align: left;
  padding: 10px; border-radius: 14px; transition: background-color 150ms ease;
}
.room.active { background: var(--accent-soft); }
.room-info { flex: 1; min-width: 0; }
.room-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.room-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.room-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-top: 2px; }
.room-last { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px; display: grid; place-items: center; flex-shrink: 0; }

.avatar {
  --c: var(--accent);
  width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0; display: grid; place-items: center;
  background: var(--c); color: #fff; font-weight: 700; font-size: 16px; position: relative;
}
.avatar.room-av { border-radius: 14px; }
.avatar.sm { width: 36px; height: 36px; font-size: 14px; }
.avatar .dot { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%; background: #3ccf6a; border: 2px solid var(--panel); }

/* ---------- чат ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.empty-chat { margin: auto; text-align: center; color: var(--muted); }
.empty-chat img { width: 72px; height: 72px; border-radius: 20px; opacity: .5; }
.chat-head { display: flex; align-items: center; gap: 4px; padding: calc(8px + env(safe-area-inset-top)) 10px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
.back { display: none; }
.chat-title { display: flex; align-items: center; gap: 10px; border: 0; background: transparent; padding: 4px 6px; border-radius: 12px; min-width: 0; text-align: left; }
.chat-title .avatar { width: 38px; height: 38px; font-size: 14px; }
.chat-title-text { display: grid; min-width: 0; }
.chat-title-text b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title-text small { color: var(--muted); font-size: 13px; }

.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 12px 14px 6px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 4px 10px; border-radius: 10px; margin: 10px 0 6px; box-shadow: var(--shadow); }
.msg { display: flex; flex-direction: column; max-width: min(78%, 520px); align-self: flex-start; margin-top: 6px; }
.msg.cont { margin-top: 0; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg-author { font-size: 13px; font-weight: 600; color: var(--accent-strong); margin: 0 0 2px 12px; }
.bubble {
  background: var(--theirs); border-radius: 18px; padding: 8px 12px 6px; box-shadow: var(--shadow);
  overflow-wrap: anywhere; white-space: pre-wrap; position: relative;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); }
.msg.mine .bubble a { color: inherit; }
.bubble a { color: var(--accent-strong); }
.meta { font-size: 11px; opacity: .65; margin-left: 10px; float: right; margin-top: 6px; white-space: nowrap; }
.bubble.media { padding: 4px; background: var(--theirs); }
.msg.mine .bubble.media { background: var(--mine); }
.bubble.media img, .bubble.media video {
  display: block; max-width: min(320px, 70vw); max-height: 360px; min-width: 120px; min-height: 80px;
  border-radius: 14px; background: rgba(0,0,0,.08); object-fit: cover;
}
.bubble.media img { cursor: zoom-in; }
.bubble.media .meta { position: absolute; right: 10px; bottom: 8px; background: rgba(0,0,0,.45); color: #fff; opacity: 1; padding: 1px 6px; border-radius: 8px; margin: 0; }
.bubble.media video + .meta { bottom: auto; top: 10px; }
.file-card { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; padding: 4px 2px; }
.file-card .fi { width: 40px; height: 40px; border-radius: 10px; background: rgba(0,0,0,.08); display: grid; place-items: center; flex-shrink: 0; }
.file-card small { display: block; opacity: .7; }
.msg.pending .bubble { opacity: .75; }
.progress { height: 4px; background: rgba(0,0,0,.12); border-radius: 2px; margin-top: 6px; overflow: hidden; min-width: 160px; }
.progress i { display: block; height: 100%; width: 0; background: currentColor; transition: width 150ms linear; }
.load-more { align-self: center; color: var(--muted); font-size: 13px; padding: 8px; }

.composer {
  display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--bg); border-radius: 20px;
  padding: 9px 14px; max-height: 140px; outline: none; line-height: 1.35;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex-shrink: 0;
  transition: transform 120ms var(--ease-out), opacity 150ms ease;
}
.send-btn:active { transform: scale(.92); }
.attach { cursor: pointer; }
.drop-hint { position: absolute; inset: 0; background: color-mix(in srgb, var(--accent) 18%, transparent); border: 3px dashed var(--accent); display: grid; place-items: center; font-weight: 700; color: var(--accent-strong); pointer-events: none; }

/* ---------- модалка ---------- */
.modal { border: 0; padding: 0; border-radius: 20px; width: min(420px, calc(100vw - 32px)); background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal[open] { animation: pop 200ms var(--ease-out); }
.modal::backdrop { background: rgba(10, 20, 19, .45); }
@keyframes pop { from { opacity: 0; transform: scale(.96); } }
.modal-inner { padding: 20px; display: grid; gap: 14px; max-height: 80dvh; }
.modal h2 { margin: 0; font-size: 18px; }
#modalBody { overflow-y: auto; overscroll-behavior: contain; display: grid; gap: 10px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.pick { display: flex; align-items: center; gap: 10px; padding: 6px 4px; border-radius: 12px; }
.pick input { width: 20px; height: 20px; accent-color: var(--accent); }
.person { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: transparent; padding: 8px 6px; border-radius: 12px; text-align: left; }
@media (hover: hover) and (pointer: fine) { .person:hover { background: var(--accent-soft); } }
.person small { color: var(--muted); display: block; }
.link-box { display: flex; gap: 8px; }
.link-box input { flex: 1; min-width: 0; }
.menu-list { display: grid; gap: 4px; }
.menu-list .btn { text-align: left; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }

/* ---------- просмотр фото ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); display: grid; place-items: center; z-index: 50; animation: fade 180ms ease; }
@keyframes fade { from { opacity: 0; } }
.lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.lb-btn { position: absolute; top: calc(12px + env(safe-area-inset-top)); color: #fff; background: rgba(255,255,255,.14); border: 0; border-radius: 12px; padding: 8px 14px; text-decoration: none; font-weight: 600; }
#lightboxDl { left: 12px; }
.lb-close { right: 12px; }

.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #1d2a29; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 60; font-size: 14px; max-width: calc(100vw - 32px); animation: fade 180ms ease; }

/* ---------- телефон ---------- */
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { position: fixed; inset: 0; background: var(--bg); transform: translateX(100%); transition: transform 280ms var(--ease-out), visibility 0s 280ms; z-index: 5; visibility: hidden; }
  .app.in-chat .chat { transform: none; visibility: visible; transition: transform 280ms var(--ease-out), visibility 0s; }
  .back { display: grid; }
  .msg { max-width: 85%; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
