:root {
  --brand: #2f6fed;
  --brand-soft: #e8efff;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --line: #e6e9f0;
  --text: #14203a;
  --muted: #7a869e;
  --danger: #e03131;
  --ok: #2f9e44;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(20, 32, 58, .06);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); overflow: hidden; }
.hidden { display: none !important; }
.row { display: flex; gap: 8px; align-items: center; }
.row.center { justify-content: center; }
.row.end { justify-content: flex-end; margin-top: 16px; }

input, textarea, button { font: inherit; color: inherit; }
input, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: #fff; outline: none; transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--brand); }
button {
  cursor: pointer; border: 1px solid var(--line); background: #fff;
  border-radius: 10px; padding: 9px 14px; transition: transform .08s, background .15s;
}
button:active { transform: scale(.98); }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
button.primary:hover { background: #2560d8; }
button.ghost { background: transparent; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
button.ok { background: var(--ok); border-color: var(--ok); color: #fff; font-weight: 600; }
button.round { width: 68px; height: 68px; border-radius: 50%; font-size: 22px; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 8px 0 0; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

/* ---------------- auth ---------------- */
.auth-view {
  height: 100%; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(circle at 20% 10%, #dfe9ff, transparent 45%),
              radial-gradient(circle at 80% 90%, #e6f7ee, transparent 40%), var(--bg);
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--panel); border-radius: 20px;
  padding: 30px 26px; box-shadow: 0 18px 50px rgba(20, 32, 58, .12);
}
.brand { font-size: 26px; font-weight: 800; text-align: center; letter-spacing: .5px; }
.brand span { color: var(--brand); }
.brand-sub { text-align: center; color: var(--muted); font-size: 13px; margin: 6px 0 22px; }
.auth-card label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.auth-card .row input { flex: 1; }
.auth-links { display: flex; justify-content: space-between; margin-top: 14px; font-size: 13px; }
.auth-links a { color: var(--brand); text-decoration: none; }
#btn-submit { width: 100%; margin-top: 18px; padding: 12px; font-size: 15px; }
.server-box { margin-top: 22px; border-top: 1px solid var(--line); padding-top: 14px; }
.server-box summary { font-size: 13px; color: var(--muted); cursor: pointer; }
.server-box input { margin-top: 10px; }
.server-box button { margin-top: 8px; }

/* ---------------- app shell ---------------- */
.app-view { height: 100%; display: flex; }
.sidebar {
  width: 330px; min-width: 330px; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; }
.me { display: flex; gap: 10px; align-items: center; min-width: 0; }
.me-name { font-weight: 700; font-size: 15px; }
.conn { font-size: 11px; color: var(--muted); }
.conn.online { color: var(--ok); }
.head-actions { display: flex; gap: 6px; }
.head-actions button { width: 34px; height: 34px; padding: 0; border-radius: 10px; font-size: 16px; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--brand);
  color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 14px;
  flex: 0 0 auto; overflow: hidden; background-size: cover; background-position: center;
}
.avatar.big { width: 100px; height: 100px; font-size: 34px; margin: 0 auto 14px; display: grid; }

.search-wrap { padding: 0 16px 10px; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex; gap: 12px; padding: 11px 16px; cursor: pointer; align-items: center;
  border-left: 3px solid transparent;
}
.chat-item:hover { background: #f8faff; }
.chat-item.active { background: var(--brand-soft); border-left-color: var(--brand); }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .top { display: flex; justify-content: space-between; gap: 8px; }
.chat-item .name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .time { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.chat-item .preview { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  background: var(--brand); color: #fff; border-radius: 10px; font-size: 11px;
  min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 5px;
}
.tabs { display: flex; border-top: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; border-radius: 0; background: transparent; padding: 12px; font-size: 13px; color: var(--muted); }
.tabs button.active { color: var(--brand); font-weight: 700; }

/* ---------------- conversation ---------------- */
.chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.empty-state { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); }
.empty-logo { font-size: 54px; }
.conversation { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.conv-head {
  display: flex; align-items: center; gap: 12px; padding: 12px 18px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.conv-title { flex: 1; min-width: 0; }
.conv-name { font-weight: 700; }
.conv-sub { font-size: 12px; color: var(--muted); }
.conv-actions { display: flex; gap: 6px; }
.conv-actions button { width: 38px; height: 38px; padding: 0; border-radius: 10px; }
.back { display: none; border: 0; background: transparent; font-size: 26px; line-height: 1; padding: 0 4px; }

.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 4px; }
.day-sep { text-align: center; font-size: 11px; color: var(--muted); margin: 12px 0 8px; }
.msg { display: flex; gap: 8px; margin: 2px 0; max-width: 76%; align-self: flex-start; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg .bubble {
  background: var(--panel); padding: 9px 12px; border-radius: 14px 14px 14px 4px;
  box-shadow: var(--shadow); font-size: 14.5px; line-height: 1.45; word-break: break-word;
  white-space: pre-wrap;
}
.msg.mine .bubble { background: var(--brand); color: #fff; border-radius: 14px 14px 4px 14px; }
.msg.sys { align-self: center; max-width: 100%; }
.msg.sys .bubble { background: transparent; box-shadow: none; color: var(--muted); font-size: 12px; }
.msg .stamp { font-size: 10px; color: var(--muted); margin-top: 3px; text-align: right; }
.msg.mine .stamp { color: rgba(255,255,255,.7); }
.msg .sender { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.bubble img.attachment { max-width: 260px; max-height: 260px; border-radius: 10px; display: block; cursor: zoom-in; }
.file-chip { display: flex; gap: 8px; align-items: center; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 14px;
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1 1 auto; width: auto; min-width: 0; resize: none;
  max-height: 120px; padding: 10px 12px; line-height: 1.4;
}
.composer button { flex: 0 0 auto; white-space: nowrap; }
#btn-attach { width: 44px; height: 44px; padding: 0; font-size: 17px; }
.send { padding: 12px 20px !important; }

/* ---------------- calls ---------------- */
.overlay {
  position: fixed; inset: 0; background: rgba(12, 18, 32, .72);
  display: grid; place-items: center; z-index: 50; backdrop-filter: blur(3px);
}
.call-card {
  background: var(--panel); border-radius: 22px; padding: 34px 40px; text-align: center;
  min-width: 300px; box-shadow: 0 30px 70px rgba(0,0,0,.35);
}
.call-card h2 { margin: 0 0 4px; }
.call-card p { color: var(--muted); margin: 0 0 22px; }

.call-view { display: block; background: #0b1220; }
#remote-video { width: 100%; height: 100%; object-fit: cover; background: #0b1220; }
#local-video {
  position: absolute; right: 20px; bottom: 120px; width: 160px; height: 220px;
  object-fit: cover; border-radius: 14px; background: #16233c; box-shadow: 0 8px 30px rgba(0,0,0,.4);
}
.call-hud {
  position: absolute; left: 0; right: 0; bottom: 26px; text-align: center; color: #fff;
}
.call-peer { font-size: 20px; font-weight: 700; }
.call-timer { font-size: 13px; color: rgba(255,255,255,.75); margin: 4px 0 18px; }
.call-hud .round {
  margin: 0 8px; background: rgba(255,255,255,.14); color: #fff; border: 0;
  width: 76px; height: 76px; padding: 0; border-radius: 50%;
  display: inline-grid; place-items: center; font-size: 14px; line-height: 1.1;
}
.call-hud .round.danger { background: var(--danger); }

/* ---------------- modal / toast ---------------- */
.modal-card {
  background: var(--panel); border-radius: 18px; padding: 24px; width: min(420px, 90vw);
  max-height: 80vh; overflow-y: auto;
}
.modal-card h3 { margin: 0 0 14px; }
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #14203a; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13px; z-index: 90; box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.user-pick { display: flex; align-items: center; gap: 10px; padding: 9px 4px; border-radius: 10px; cursor: pointer; }
.user-pick:hover { background: #f4f7ff; }
.user-pick .name { font-weight: 600; font-size: 14px; }
.user-pick .handle { font-size: 12px; color: var(--muted); }

/* ---------------- mobile ---------------- */
@media (max-width: 780px) {
  .sidebar { width: 100%; min-width: 0; }
  .app-view.chatting .sidebar { display: none; }
  .app-view:not(.chatting) .chat-pane { display: none; }
  .back { display: block; }
  .msg { max-width: 88%; }
  #local-video { width: 110px; height: 150px; bottom: 120px; }
  /* 16px keeps iOS from zooming on focus */
  .composer textarea { font-size: 16px; }
  .conv-actions button { width: 34px; height: 34px; }
}
