:root {
  --bg: #0e0f14;
  --panel: #14151c;
  --panel2: #1b1d26;
  --line: #262936;
  --text: #e9e9ef;
  --muted: #8b8fa3;
  --accent: #8b6cf0;
  --accent2: #3fb6c6;
  --pink: #e0568a;
  --mine: #2a2350;
  --theirs: #1d1f2a;
  --danger: #e5484d;
  --ok: #4cc38a;
  --radius: 14px;
  --iri: linear-gradient(120deg, #e0568a 0%, #8b6cf0 45%, #3fb6c6 75%, #d9a441 100%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.4 var(--font); -webkit-font-smoothing: antialiased; overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
a { color: var(--accent2); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.iri { background: var(--iri); -webkit-background-clip: text; background-clip: text; color: transparent; }

/* ---------- auth screens */
.auth { height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 28px 24px; }
.logo { font-weight: 800; font-size: 28px; letter-spacing: -0.5px; margin: 0 0 4px; }
.auth-card p { margin: 0 0 20px; }
.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.input { width: 100%; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; outline: none; }
.input:focus { border-color: var(--accent); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 16px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); font-weight: 600; }
.btn:hover { border-color: #3a3e52; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); border-color: transparent; color: #fff; }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; margin: 4px 0 8px; }

/* ---------- layout */
.layout { display: grid; grid-template-columns: 340px 1fr; height: 100%; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; }
.side-head { display: flex; align-items: center; gap: 10px; padding: 14px 14px 10px; }
.side-head .logo { font-size: 20px; margin: 0; flex: 1; }
.icon-btn { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--panel2); color: var(--text); }
.icon-btn svg { width: 21px; height: 21px; }
.icon-btn.on { color: var(--text); background: var(--panel2); }
.search { margin: 0 14px 10px; }
.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 9px 8px; border-radius: 12px; cursor: pointer; }
.chat-item:hover { background: var(--panel2); }
.chat-item.active { background: #24213d; }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .row1, .chat-item .row2 { display: flex; gap: 8px; align-items: baseline; }
.chat-item .name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 12px; color: var(--muted); flex: none; }
.chat-item .preview { flex: 1; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; border-radius: 10px; padding: 1px 7px; flex: none; }
.side-foot { border-top: 1px solid var(--line); padding: 10px 14px; display: flex; align-items: center; gap: 10px; }
.side-foot .who { flex: 1; min-width: 0; }
.side-foot .who b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role { font-size: 12px; color: var(--muted); }
.role.admin { color: var(--pink); }
.role.alchemist { color: var(--accent2); }

.avatar { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; flex: none; position: relative; font-size: 17px; user-select: none; }
.avatar.sm { width: 34px; height: 34px; font-size: 13px; }
.avatar.xs { width: 28px; height: 28px; font-size: 12px; }
.avatar.lg { width: 96px; height: 96px; font-size: 34px; }
.avatar .dot { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); }
.avatar.group { background: var(--iri) !important; }

/* ---------- chat pane */
.main { display: flex; flex-direction: column; min-width: 0; height: 100%; height: 100dvh; background: var(--bg); position: relative; }
.empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--panel); min-height: 62px; }
.chat-head .title { flex: 1; min-width: 0; }
.chat-head .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .sub { font-size: 13px; color: var(--muted); }
.chat-head .sub.typing { color: var(--accent2); }
.back { display: none; }
.call-banner { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: #1c2a2d; border-bottom: 1px solid var(--line); font-size: 14px; }
.call-banner span { flex: 1; }

.messages { flex: 1; overflow-y: auto; padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 10px; margin: 10px 0 6px; }
.sys { align-self: center; font-size: 13px; color: var(--muted); margin: 6px 0; text-align: center; max-width: 90%; }
.msg { display: flex; gap: 8px; align-items: flex-end; max-width: min(78%, 620px); position: relative; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg .av-slot { width: 28px; flex: none; }
.bubble { background: var(--theirs); border-radius: var(--radius); padding: 7px 11px 6px; min-width: 60px; position: relative; overflow-wrap: anywhere; }
.msg.mine .bubble { background: var(--mine); }
.msg.first .bubble { margin-top: 6px; }
.author { font-size: 13px; font-weight: 700; margin-bottom: 2px; }
.text { white-space: pre-wrap; }
.stamp { font-size: 11px; color: var(--muted); float: right; margin: 6px 0 -2px 10px; user-select: none; display: inline-flex; gap: 3px; align-items: center; }
.stamp .ticks { color: var(--accent2); }
.msg .tools { position: absolute; top: -10px; display: none; gap: 2px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; z-index: 2; }
.msg.mine .tools { left: -6px; }
.msg:not(.mine) .tools { right: -6px; }
.msg:hover .tools { display: flex; }
.tools button { padding: 3px 7px; border-radius: 6px; font-size: 12px; color: var(--muted); }
.tools button:hover { background: var(--panel); color: var(--text); }
.deleted { color: var(--muted); font-style: italic; }
.reply-q { border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0 5px; font-size: 13px; color: var(--muted); border-radius: 4px; background: rgba(255,255,255,.03); cursor: pointer; max-height: 3.2em; overflow: hidden; }
.img { display: block; max-width: min(320px, 100%); max-height: 360px; border-radius: 10px; cursor: zoom-in; margin: 2px 0 4px; background: #000; }
.file { display: flex; align-items: center; gap: 10px; padding: 4px 0; text-decoration: none; color: var(--text); }
.file .ficon { width: 40px; height: 40px; border-radius: 10px; background: var(--accent); display: grid; place-items: center; flex: none; }
.file small { display: block; color: var(--muted); }
.call-msg { display: flex; align-items: center; gap: 8px; }

/* voice */
.voice { display: flex; align-items: center; gap: 10px; min-width: 220px; padding: 2px 0; }
.voice .play { width: 38px; height: 38px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.voice .play svg { width: 18px; height: 18px; }
.voice .track { flex: 1; height: 28px; display: flex; align-items: center; gap: 2px; cursor: pointer; }
.voice .track i { flex: 1; background: #4a4766; border-radius: 2px; min-height: 3px; }
.voice .track i.on { background: var(--accent2); }
.voice .vt { font-size: 12px; color: var(--muted); width: 36px; text-align: right; flex: none; }
.transcript { font-size: 14px; margin-top: 4px; padding-top: 4px; border-top: 1px solid rgba(255,255,255,.07); color: #cfd0dc; }
.transcript.pending { color: var(--muted); font-style: italic; }

/* composer */
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea { flex: 1; resize: none; max-height: 160px; min-height: 40px; background: var(--panel2); border: 1px solid var(--line); border-radius: 20px; padding: 9px 14px; outline: none; }
.composer textarea:focus { border-color: #3a3e52; }
.send { background: var(--accent); color: #fff; border-radius: 50%; }
.send:hover { background: var(--accent); color: #fff; filter: brightness(1.1); }
.rec { display: flex; flex: 1; align-items: center; gap: 10px; padding: 0 8px; height: 40px; }
.rec .reddot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
.rec .live { flex: 1; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@keyframes blink { 50% { opacity: .2; } }
.replybar { display: flex; align-items: center; gap: 10px; padding: 6px 14px; background: var(--panel); border-top: 1px solid var(--line); font-size: 14px; }
.replybar div { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.drop { position: absolute; inset: 0; background: rgba(139,108,240,.15); border: 2px dashed var(--accent); display: grid; place-items: center; font-size: 18px; z-index: 20; pointer-events: none; }

/* ---------- modals */
.overlay { position: fixed; inset: 0; background: rgba(5,6,10,.7); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { width: 100%; max-width: 520px; max-height: calc(100dvh - 32px); overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 20px; }
.modal h2 { margin: 0 0 14px; font-size: 19px; display: flex; align-items: center; gap: 10px; }
.modal h2 span { flex: 1; }
.list { display: flex; flex-direction: column; gap: 4px; }
.row { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; }
.row:hover { background: var(--panel2); }
.row .grow { flex: 1; min-width: 0; }
.row .grow b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.tabs button { padding: 7px 12px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.tabs button.on { background: var(--panel2); color: var(--text); }
.invite-link { display: flex; gap: 8px; align-items: center; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin: 6px 0; font-size: 13px; }
.invite-link code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--accent2); }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: var(--panel2); color: var(--muted); flex: none; }
.pill.ok { color: var(--ok); }
.pill.used { color: var(--muted); }
select.input { padding: 8px 10px; }
.check { width: 18px; height: 18px; accent-color: var(--accent); }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 60; display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 96vw; max-height: 94vh; border-radius: 6px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--panel2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; z-index: 80; font-size: 14px; max-width: 90vw; }

/* ---------- calls */
.call { position: fixed; inset: 0; z-index: 40; background: #07080b; display: flex; flex-direction: column; }
.call-top { display: flex; align-items: center; gap: 10px; padding: 12px 16px; color: var(--muted); }
.call-top b { color: var(--text); }
.call-top span { flex: 1; }
.tiles { flex: 1; display: grid; gap: 8px; padding: 8px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); grid-auto-rows: minmax(160px, 1fr); min-height: 0; }
.tile { position: relative; background: #111219; border-radius: 14px; overflow: hidden; display: grid; place-items: center; min-height: 0; }
.tile video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.tile .label { position: absolute; left: 10px; bottom: 10px; background: rgba(0,0,0,.55); padding: 3px 9px; border-radius: 8px; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.tile.speaking { box-shadow: inset 0 0 0 3px var(--accent2); }
.self-view { position: absolute; right: 16px; bottom: 96px; width: 180px; aspect-ratio: 16/10; border-radius: 12px; overflow: hidden; background: #111219; border: 1px solid var(--line); z-index: 2; }
.self-view video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.self-view video.screen { transform: none; object-fit: contain; }
.call-controls { display: flex; justify-content: center; gap: 14px; padding: 16px 16px calc(18px + env(safe-area-inset-bottom)); }
.cbtn { width: 56px; height: 56px; border-radius: 50%; background: #23252f; display: grid; place-items: center; color: #fff; }
.cbtn svg { width: 24px; height: 24px; }
.cbtn.off { background: #f2f2f5; color: #111; }
.cbtn.hang { background: var(--danger); }
.cbtn.green { background: var(--ok); }
.call-mini { position: fixed; right: 16px; top: 16px; z-index: 39; background: var(--ok); color: #05210f; font-weight: 700; padding: 8px 14px; border-radius: 20px; cursor: pointer; display: flex; gap: 8px; align-items: center; box-shadow: 0 6px 24px rgba(0,0,0,.4); }
.ring { text-align: center; }
.ring .avatar { margin: 6px auto 14px; }
.ring .acts { display: flex; justify-content: center; gap: 22px; margin-top: 22px; }
.pulse { animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(76,195,138,.6); } 70% { box-shadow: 0 0 0 18px rgba(76,195,138,0); } 100% { box-shadow: 0 0 0 0 rgba(76,195,138,0); } }

/* ---------- mobile */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .layout.chat-open .sidebar { display: none; }
  .layout:not(.chat-open) .main { display: none; }
  .back { display: grid; }
  .msg { max-width: 88%; }
  .msg .tools { display: none !important; }
  .self-view { width: 110px; bottom: 100px; right: 10px; }
  .tiles { grid-template-columns: 1fr; }
  .cbtn { width: 52px; height: 52px; }
}
