:root {
  color-scheme: dark;
  --bg: #07101c;
  --panel: #0d1928;
  --panel-strong: #111f31;
  --panel-soft: #14253a;
  --text: #f4f7fb;
  --muted: #91a0b5;
  --line: #22344b;
  --accent: #27c27a;
  --accent-strong: #13a864;
  --blue: #56a8ff;
  --danger: #fb7185;
  --warning: #fbbf24;
  --shadow: 0 28px 80px rgba(0, 0, 0, .34);
  --radius: 22px;
}

* { box-sizing: border-box; }
html { min-width: 320px; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 12% -10%, rgba(24, 99, 127, .42), transparent 34%),
    radial-gradient(circle at 100% 0, rgba(23, 104, 73, .24), transparent 28%),
    var(--bg);
  color: var(--text);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
}

button, input, textarea { font: inherit; }

button {
  border: 1px solid var(--line);
  background: #15263b;
  color: var(--text);
  padding: 10px 14px;
  border-radius: 11px;
  cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, background .16s ease, opacity .16s ease;
}

button:hover:not(:disabled) { transform: translateY(-1px); border-color: #3d5a7d; }
button:disabled { opacity: .42; cursor: not-allowed; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid rgba(86, 168, 255, .3); outline-offset: 2px; }

input, textarea {
  border: 1px solid var(--line);
  background: #081421;
  color: var(--text);
  border-radius: 12px;
  padding: 11px 13px;
  outline: none;
}

input:focus, textarea:focus { border-color: #4f83b9; }

.shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 26px 0 56px; }
.hidden { display: none !important; }
.card { border: 1px solid rgba(148, 163, 184, .16); background: rgba(13, 25, 40, .9); box-shadow: var(--shadow); border-radius: var(--radius); }
.eyebrow { color: #73dfae; font-size: 10px; font-weight: 800; letter-spacing: .16em; text-transform: uppercase; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.brand { font-size: 27px; font-weight: 850; letter-spacing: -.045em; }
.brand span { color: var(--accent); }
.subtitle { margin-top: 4px; color: var(--muted); font-size: 12px; }
.status-pill { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: rgba(13, 25, 40, .82); padding: 8px 12px; border-radius: 99px; color: var(--muted); font-size: 12px; }
.status-pill i { width: 8px; height: 8px; border-radius: 50%; background: #66758a; }
.status-pill.connected i { background: var(--accent); box-shadow: 0 0 0 5px rgba(39, 194, 122, .12); }

.compat-notice { margin-bottom: 16px; padding: 12px 14px; border: 1px solid rgba(251, 191, 36, .42); background: rgba(120, 53, 15, .3); color: #fde68a; border-radius: 13px; font-size: 13px; line-height: 1.55; }

.hero { min-height: 176px; padding: 28px; display: flex; align-items: center; justify-content: space-between; gap: 34px; }
.identity { display: flex; align-items: center; gap: 17px; }
.device-avatar, .avatar { display: grid; place-items: center; flex: 0 0 auto; border-radius: 16px; background: linear-gradient(145deg, hsl(var(--avatar-hue, 156) 68% 48%), hsl(calc(var(--avatar-hue, 156) + 32) 68% 42%)); color: white; font-weight: 850; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .14); }
.device-avatar { width: 58px; height: 58px; font-size: 24px; }
.identity p { margin: 7px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.name-button { margin-top: 3px; padding: 2px 0; border: 0; background: transparent; font-size: 21px; font-weight: 760; }
.name-button small { margin-left: 6px; color: var(--muted); font-size: 11px; font-weight: 500; }
.room-actions { display: flex; align-items: center; gap: 10px; }
.join-row { display: flex; gap: 8px; }
#roomInput { width: 160px; letter-spacing: .1em; }
.primary, .send-btn { border-color: transparent; background: linear-gradient(135deg, var(--accent-strong), #168a78); font-weight: 750; }
.ghost { background: transparent; }

body:has(#workspace:not(.hidden)) .hero { display: none; }

.room-panel { margin-bottom: 14px; padding: 13px 15px; display: flex; align-items: center; justify-content: space-between; gap: 16px; box-shadow: 0 14px 40px rgba(0, 0, 0, .18); border-radius: 17px; }
.room-main { min-width: 0; display: flex; align-items: center; gap: 24px; }
.room-code { margin-top: 2px; font-size: 23px; font-weight: 850; letter-spacing: .14em; white-space: nowrap; }
.room-copy { min-width: 0; display: grid; gap: 3px; }
.room-copy strong { font-size: 13px; }
.room-copy span { overflow: hidden; color: var(--muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.room-links { display: flex; gap: 8px; }
.room-links button { padding: 8px 11px; font-size: 12px; }
.qr-box { width: 70px; height: 70px; flex: 0 0 auto; overflow: hidden; padding: 5px; border-radius: 10px; background: white; }
.qr-box svg { display: block; width: 100%; height: 100%; }

.messenger { min-height: 670px; display: grid; grid-template-columns: 286px minmax(0, 1fr); overflow: hidden; border: 1px solid rgba(148, 163, 184, .18); border-radius: 22px; background: rgba(9, 19, 31, .94); box-shadow: var(--shadow); }
.contacts-panel { min-width: 0; display: flex; flex-direction: column; border-right: 1px solid var(--line); background: linear-gradient(180deg, rgba(17, 31, 49, .98), rgba(11, 24, 38, .98)); }
.contacts-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; padding: 22px 19px 15px; }
.contacts-head h2 { margin: 4px 0 0; font-size: 18px; }
.count-badge { padding: 4px 8px; border: 1px solid var(--line); border-radius: 99px; color: var(--muted); font-size: 11px; }
.device-list { flex: 1; min-height: 180px; display: grid; align-content: start; gap: 5px; padding: 4px 9px 14px; overflow-y: auto; }
.device-card { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 11px; border: 0; background: transparent; text-align: left; border-radius: 14px; }
.device-card:hover, .device-card.selected { transform: none; background: rgba(86, 168, 255, .09); }
.device-card.selected { box-shadow: inset 3px 0 var(--accent); }
.device-info { min-width: 0; display: flex; align-items: center; gap: 10px; }
.mini-avatar { width: 42px; height: 42px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 13px; background: linear-gradient(145deg, hsl(var(--avatar-hue, 210) 58% 48%), hsl(calc(var(--avatar-hue, 210) + 26) 58% 38%)); font-weight: 800; }
.device-copy { min-width: 0; }
.device-card strong, .device-card small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-card strong { font-size: 13px; }
.device-card small { margin-top: 3px; color: var(--muted); font-size: 10px; }
.contact-meta { display: grid; justify-items: end; gap: 7px; }
.peer-dot { width: 7px; height: 7px; border-radius: 50%; background: #526174; }
.peer-dot.online { background: var(--accent); box-shadow: 0 0 0 4px rgba(39, 194, 122, .1); }
.unread-badge { min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 5px; border-radius: 99px; background: var(--accent); color: #04140c; font-size: 10px; font-weight: 850; }
.empty { color: var(--muted); text-align: center; }
.contacts-empty { padding: 38px 10px; font-size: 12px; line-height: 1.6; white-space: pre-line; }
.privacy-note { display: flex; align-items: flex-start; gap: 9px; margin: 10px; padding: 11px; border: 1px solid rgba(39, 194, 122, .14); border-radius: 12px; background: rgba(39, 194, 122, .06); color: var(--muted); font-size: 10px; line-height: 1.45; }
.privacy-note i { width: 8px; height: 8px; flex: 0 0 auto; margin-top: 3px; border-radius: 50%; background: var(--accent); }
.privacy-note strong { color: #b8f5d6; }

.chat-panel { min-width: 0; display: grid; grid-template-rows: auto minmax(360px, 1fr) auto; background: #091521; }
.chat-head { min-height: 72px; display: flex; align-items: center; gap: 12px; padding: 13px 18px; border-bottom: 1px solid var(--line); background: rgba(13, 27, 43, .92); }
.chat-avatar { width: 42px; height: 42px; font-size: 16px; }
.chat-title { min-width: 0; flex: 1; }
.chat-title h2 { margin: 0; overflow: hidden; font-size: 16px; text-overflow: ellipsis; white-space: nowrap; }
.chat-title span { display: block; margin-top: 4px; color: var(--muted); font-size: 10px; }
.secure-chip { padding: 5px 8px; border: 1px solid rgba(39, 194, 122, .25); border-radius: 99px; background: rgba(39, 194, 122, .07); color: #92e8bd; font-size: 10px; white-space: nowrap; }

.message-viewport { position: relative; min-height: 0; overflow-y: auto; overscroll-behavior: contain; background: radial-gradient(circle at 50% 0, rgba(27, 57, 78, .18), transparent 38%); }
.message-viewport.drag { box-shadow: inset 0 0 0 2px var(--accent); }
.chat-empty { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px 20px; text-align: center; }
.chat-empty-mark { width: 46px; height: 46px; display: grid; place-items: center; margin-bottom: 14px; border: 1px solid var(--line); border-radius: 15px; background: var(--panel-soft); color: var(--accent); font-size: 22px; }
.chat-empty strong { font-size: 15px; }
.chat-empty span { max-width: 360px; margin-top: 7px; color: var(--muted); font-size: 11px; line-height: 1.6; }
.message-list { display: grid; gap: 16px; padding: 24px 22px 32px; }
.message-row { display: flex; align-items: flex-end; gap: 9px; }
.message-row.outgoing { flex-direction: row-reverse; }
.message-avatar { width: 31px; height: 31px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 10px; background: var(--panel-soft); color: #d8e5f5; font-size: 11px; font-weight: 800; }
.message-stack { max-width: min(72%, 580px); display: grid; gap: 5px; }
.message-row.outgoing .message-stack { justify-items: end; }
.message-meta { padding: 0 3px; color: #718096; font-size: 9px; }
.message-bubble { width: fit-content; padding: 10px 13px; border: 1px solid var(--line); border-radius: 4px 15px 15px; background: #142438; color: #eef4fb; font-size: 13px; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-row.outgoing .message-bubble { border-color: rgba(39, 194, 122, .28); border-radius: 15px 4px 15px 15px; background: #166642; color: #f2fff8; }
.message-status { color: #75859a; font-size: 9px; }
.message-status.error { color: #fda4af; }

.file-message { width: min(390px, 100%); overflow: hidden; border: 1px solid var(--line); border-radius: 16px; background: #111f30; }
.message-row.outgoing .file-message { border-color: rgba(39, 194, 122, .22); background: #102a24; }
.file-trigger { width: 100%; display: block; padding: 0; border: 0; border-radius: 0; background: transparent; text-align: left; }
.file-trigger[role="button"] { cursor: pointer; }
.file-trigger[role="button"]:hover { background: rgba(255, 255, 255, .025); }
.file-trigger[role="button"]:focus-visible { outline: 3px solid rgba(86, 168, 255, .28); outline-offset: -3px; }
.file-summary { display: flex; align-items: center; gap: 11px; padding: 13px; }
.file-preview { width: 50px; height: 50px; display: grid; place-items: center; flex: 0 0 auto; overflow: hidden; border-radius: 12px; background: linear-gradient(145deg, #24486a, #1a3150); color: #cce7ff; font-size: 10px; font-weight: 900; letter-spacing: .04em; }
.file-preview.image { background: linear-gradient(145deg, #355e58, #1c4038); color: #c9ffe7; }
.file-preview.pdf { background: linear-gradient(145deg, #73323e, #48212a); color: #fecdd3; }
.file-preview.archive { background: linear-gradient(145deg, #654f28, #3d321d); color: #fde68a; }
.file-preview.video { background: linear-gradient(145deg, #49447b, #292650); color: #ddd6fe; }
.file-preview.audio { background: linear-gradient(145deg, #65502d, #3f321e); color: #fde68a; }
.file-preview.document { background: linear-gradient(145deg, #2c5374, #1c354c); color: #d4ecff; }
.file-preview img { width: 100%; height: 100%; object-fit: cover; }
.file-copy { min-width: 0; flex: 1; }
.file-copy strong, .file-copy span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-copy strong { font-size: 13px; }
.file-copy span { margin-top: 4px; color: var(--muted); font-size: 10px; }
.file-arrow { color: var(--accent); font-size: 17px; }
.file-items { display: grid; gap: 6px; padding: 0 13px 12px; }
.file-item { display: flex; justify-content: space-between; gap: 12px; color: #aab7c8; font-size: 10px; }
.file-item span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-progress { height: 4px; overflow: hidden; background: rgba(255, 255, 255, .07); }
.file-progress i { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--accent), var(--blue)); transition: width .16s linear; }
.file-footer { min-height: 37px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 12px; border-top: 1px solid rgba(148, 163, 184, .1); color: var(--muted); font-size: 10px; }
.file-actions { display: flex; gap: 6px; }
.file-action { padding: 5px 8px; border-radius: 8px; font-size: 10px; }
.file-action.primary-action { border-color: rgba(39, 194, 122, .28); background: rgba(39, 194, 122, .14); color: #a9f2ce; }

.drop-overlay { position: absolute; inset: 13px; z-index: 5; display: none; place-items: center; align-content: center; border: 2px dashed rgba(39, 194, 122, .72); border-radius: 18px; background: rgba(7, 22, 28, .92); pointer-events: none; }
.message-viewport.drag .drop-overlay { display: grid; }
.drop-overlay strong { color: #bdf6d9; font-size: 17px; }
.drop-overlay span { margin-top: 6px; color: var(--muted); font-size: 11px; }

.composer { padding: 12px 16px 11px; border-top: 1px solid var(--line); background: rgba(13, 27, 43, .96); }
.composer-actions { display: flex; gap: 7px; margin-bottom: 8px; }
.attach-btn { padding: 6px 9px; border-radius: 9px; background: transparent; color: #a9b8ca; font-size: 10px; }
.composer-input { display: flex; align-items: flex-end; gap: 9px; }
.composer textarea { width: 100%; min-height: 40px; max-height: 128px; resize: none; line-height: 1.5; }
.send-btn { min-width: 68px; height: 40px; }
.composer-hint { margin-top: 7px; color: #64748b; font-size: 9px; }

dialog { width: min(520px, calc(100% - 30px)); max-width: 520px; padding: 0; border: 0; background: transparent; color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .68); backdrop-filter: blur(5px); }
.dialog-card { padding: 22px; border: 1px solid var(--line); border-radius: 20px; background: #0d1c2d; box-shadow: var(--shadow); }
.dialog-card h3 { margin: 7px 0 14px; }
.dialog-card input { width: 100%; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast { position: fixed; z-index: 20; left: 50%; bottom: 24px; max-width: min(560px, calc(100% - 28px)); padding: 10px 14px; border-radius: 99px; background: #e8eef5; color: #0c1724; box-shadow: 0 14px 45px rgba(0, 0, 0, .3); font-size: 12px; opacity: 0; pointer-events: none; transform: translate(-50%, 18px); transition: opacity .2s ease, transform .2s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 800px) {
  .shell { width: min(100% - 20px, 720px); padding-top: 18px; }
  .hero { align-items: stretch; flex-direction: column; }
  .room-actions { align-items: stretch; flex-direction: column; }
  .join-row input { width: auto; flex: 1; }
  .room-copy { display: none; }
  .messenger { min-height: 760px; grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .contacts-panel { border-right: 0; border-bottom: 1px solid var(--line); }
  .contacts-head { padding: 15px 15px 8px; }
  .contacts-head h2 { font-size: 15px; }
  .device-list { min-height: 0; display: flex; gap: 7px; padding: 4px 10px 11px; overflow-x: auto; }
  .device-card { width: 158px; flex: 0 0 auto; }
  .privacy-note { display: none; }
  .chat-panel { min-height: 610px; }
  .message-stack { max-width: 82%; }
}

@media (max-width: 520px) {
  .topbar { align-items: flex-start; }
  .subtitle { display: none; }
  .brand { font-size: 23px; }
  .status-pill { padding: 7px 9px; }
  .hero { padding: 21px 18px; }
  .identity { align-items: flex-start; }
  .room-panel { align-items: flex-start; }
  .room-main { min-width: 0; flex: 1; align-items: flex-start; flex-direction: column; gap: 8px; }
  .room-links { flex-wrap: wrap; }
  .qr-box { width: 66px; height: 66px; }
  .messenger { min-height: calc(100vh - 190px); border-radius: 18px; }
  .chat-panel { min-height: 560px; }
  .chat-head { min-height: 64px; padding: 10px 12px; }
  .secure-chip { display: none; }
  .message-list { padding: 19px 12px 28px; }
  .message-stack { max-width: 88%; }
  .message-avatar { display: none; }
  .file-message { width: min(330px, 100%); }
  .composer { padding: 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  .composer-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
