:root {
  --bg: #0b0e13;
  --panel: #12161d;
  --panel-2: #1a1f28;
  --line: #242b36;
  --text: #e9edf3;
  --muted: #8b95a7;
  --accent: #4f7dff;
  --accent-2: #7c5cff;
  --me: #3d6cf5;
  --them: #1a1f28;
  --danger: #ef4444;
  --ok: #22c55e;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f5f9;
    --panel: #ffffff;
    --panel-2: #eef1f6;
    --line: #e2e6ee;
    --text: #0f172a;
    --muted: #64748b;
    --them: #ffffff;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.i { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); letter-spacing: .02em; }
p { margin: 0; }

html, body { overflow: hidden; }
#app {
  position: fixed; left: 0; right: 0; top: var(--app-top, 0); height: 100vh; height: var(--app-h, 100dvh);
  display: flex; flex-direction: column;
}
.composer { flex: none; }
.topbar { flex: none; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.center { align-items: center; justify-content: center; padding: 24px 16px; gap: 14px; overflow-y: auto; }

/* ---------- auth cards ---------- */
.card {
  width: 100%; max-width: 390px; display: flex; flex-direction: column; gap: 16px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 24px; padding: 28px 24px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .22);
}
.brand { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 18px; }
.logo {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 19px; margin: 0 0 8px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 500; }
.input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font-size: 16px; outline: none; transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79, 125, 255, .25); }
.btn { padding: 13px 18px; border-radius: 12px; font-weight: 600; background: var(--panel-2); border: 1px solid var(--line); transition: filter .15s, opacity .15s; }
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.danger { background: var(--danger); border-color: transparent; color: #fff; }
.link { color: var(--accent); font-size: 14px; font-weight: 500; align-self: flex-start; }
.link.subtle { color: var(--muted); align-self: center; }
.danger-link { color: var(--danger); }
.error { color: var(--danger); font-size: 13.5px; }
.error:empty { display: none; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- chat ---------- */
.topbar {
  position: relative; z-index: 5; display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px 14px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.peer { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; }
.peer-text { min-width: 0; }
.avatar {
  position: relative; flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 700; text-transform: uppercase; background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.avatar .dot { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; background: var(--muted); border: 2px solid var(--panel); }
.avatar.online .dot { background: var(--ok); }
.avatar.xl { width: 104px; height: 104px; font-size: 42px; }
.peer-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 12.5px; color: var(--muted); }
.peer-status.clickable { color: var(--accent); cursor: pointer; }
.icon-btn { width: 42px; height: 42px; flex: none; border-radius: 50%; display: grid; place-items: center; color: var(--text); transition: background .15s; }
.icon-btn:hover { background: var(--panel-2); }
.menu {
  position: absolute; right: 8px; top: calc(100% + 6px); min-width: 240px; display: flex; flex-direction: column; padding: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
.menu button { text-align: left; padding: 11px 12px; border-radius: 10px; }
.menu button:hover { background: var(--panel-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }
.menu .danger { color: var(--danger); }

.scroller { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 12px 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.e2ee {
  display: flex; align-items: center; gap: 8px; width: fit-content; max-width: 100%; margin: 4px auto 12px; padding: 7px 14px;
  border-radius: 999px; background: var(--panel-2); color: var(--muted); font-size: 12px; text-align: center;
}
.e2ee .i { width: 14px; height: 14px; }
.empty { display: flex; flex-direction: column; align-items: center; gap: 14px; margin-top: 12vh; color: var(--muted); }
.messages { display: flex; flex-direction: column; }
.day { align-self: center; margin: 14px 0 8px; padding: 3px 10px; border-radius: 999px; font-size: 12px; color: var(--muted); background: var(--panel-2); }
.msg { display: flex; flex-direction: column; max-width: min(80%, 560px); margin-bottom: 2px; animation: pop .16s ease-out; }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg.them { align-self: flex-start; align-items: flex-start; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } }
.bubble {
  padding: 8px 13px; border-radius: 19px; background: var(--them); border: 1px solid var(--line);
  white-space: pre-wrap; overflow-wrap: anywhere; user-select: text;
}
.msg.me .bubble { background: var(--me); border-color: transparent; color: #fff; border-bottom-right-radius: 6px; -webkit-touch-callout: none; }
.msg.them .bubble { border-bottom-left-radius: 6px; }
.bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.bubble.undecryptable { font-style: italic; opacity: .7; }
.bubble.media { padding: 3px; overflow: hidden; }
.thumb { display: block; max-width: 100%; max-height: 340px; min-width: 140px; min-height: 100px; border-radius: 16px; object-fit: cover; background: rgba(127, 127, 127, .15); cursor: zoom-in; }
video.thumb { cursor: default; }
.file { display: flex; align-items: center; gap: 10px; text-align: left; padding: 4px 2px; max-width: 280px; }
.file-info { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-size: 12px; opacity: .75; }
.meta { display: flex; gap: 4px; margin: 2px 8px 6px; font-size: 11px; color: var(--muted); }
.tick { letter-spacing: -.25em; padding-right: .25em; }
.tick.read { color: var(--accent); }
.expiry { opacity: .85; font-variant-numeric: tabular-nums; }

.composer {
  display: flex; align-items: flex-end; gap: 6px; padding: 8px 8px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; max-height: 140px; padding: 10px 15px; border-radius: 21px; outline: none;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font-size: 16px; line-height: 1.35;
}
.composer textarea:focus { border-color: var(--accent); }
.icon-btn.send { background: var(--accent); color: #fff; }
.icon-btn.send:hover { background: var(--accent); filter: brightness(1.1); }

/* ---------- calls ---------- */
.call, .incoming { position: fixed; inset: 0; z-index: 50; color: #fff; background: #05070a; overflow: hidden; }
#remote-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
#remote-video.contain { object-fit: contain; }
.remote-placeholder, .incoming-body {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: radial-gradient(circle at 50% 35%, #1c2a4d 0%, #05070a 70%);
}
.call-status { font-size: 15px; color: rgba(255, 255, 255, .75); }
#local-video {
  position: absolute; z-index: 2; right: 14px; top: calc(env(safe-area-inset-top) + 72px); width: 30vw; max-width: 180px; aspect-ratio: 3 / 4;
  object-fit: cover; border-radius: 16px; background: #111; border: 1px solid rgba(255, 255, 255, .18); box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
}
#local-video.mirror { transform: scaleX(-1); }
@media (min-aspect-ratio: 1/1) { #local-video { aspect-ratio: 16 / 9; width: 24vw; max-width: 300px; } }
.call-top {
  position: absolute; z-index: 3; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: flex-start;
  padding: calc(env(safe-area-inset-top) + 16px) 20px 40px; background: linear-gradient(rgba(0, 0, 0, .55), transparent);
}
.call-peer { font-weight: 650; font-size: 17px; }
.call-timer { font-size: 13px; color: rgba(255, 255, 255, .7); font-variant-numeric: tabular-nums; }
.call-quality {
  display: inline-block; margin-top: 6px; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-variant-numeric: tabular-nums;
  background: rgba(34, 197, 94, .22); color: #b7f5cb;
}
.call-quality.poor { background: rgba(239, 68, 68, .25); color: #fecaca; }
#remote-video { cursor: pointer; }
.call-e2ee { display: flex; align-items: center; gap: 6px; font-size: 12px; color: rgba(255, 255, 255, .75); }
.call-e2ee .i { width: 14px; height: 14px; }
.call-controls, .incoming-actions {
  position: absolute; z-index: 3; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; gap: 14px;
  padding: 40px 16px calc(env(safe-area-inset-bottom) + 28px); background: linear-gradient(transparent, rgba(0, 0, 0, .6));
}
.ctl { display: flex; flex-direction: column; align-items: center; gap: 7px; width: 64px; color: #fff; }
.ctl-circle {
  width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(255, 255, 255, .16); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); transition: background .15s, transform .1s;
}
.ctl:hover .ctl-circle { background: rgba(255, 255, 255, .24); }
.ctl:active .ctl-circle { transform: scale(.94); }
.ctl-label { font-size: 11.5px; color: rgba(255, 255, 255, .85); white-space: nowrap; }
.ctl .i-off, .ctl.off .i-on { display: none; }
.ctl.off .i-off { display: block; }
#ctl-mic.off .ctl-circle { background: #fff; color: #0b0e13; }
.ctl.on .ctl-circle { background: var(--accent); }
.ctl.hang .ctl-circle { background: var(--danger); }
.ctl.accept .ctl-circle { background: var(--ok); }
.ctl.big { width: 76px; }
.ctl.big .ctl-circle { width: 68px; height: 68px; }
.incoming-actions { gap: 22px; }
.call-controls { gap: 6px; flex-wrap: wrap; }
@media (min-width: 480px) { .call-controls { gap: 14px; } }
.call-badges {
  position: absolute; z-index: 3; left: 0; right: 0; top: calc(env(safe-area-inset-top) + 70px);
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
.badge {
  display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; font-size: 13px;
  background: rgba(0, 0, 0, .55); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.badge .i { width: 15px; height: 15px; }
.incoming-name { font-size: 26px; font-weight: 650; }
.pulse { animation: pulse 1.6s ease-out infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(79, 125, 255, .55); } 100% { box-shadow: 0 0 0 34px rgba(79, 125, 255, 0); } }

.ctl-circle { position: relative; }
.ctl-badge {
  position: absolute; top: -3px; right: -3px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  display: grid; place-items: center; font-size: 11px; font-weight: 700; background: var(--danger); color: #fff;
}
@media (max-width: 420px) {
  .call-controls { gap: 2px; padding-left: 6px; padding-right: 6px; }
  .ctl { width: 54px; }
  .ctl-circle { width: 50px; height: 50px; }
  .ctl-label { font-size: 10.5px; }
}

/* Call shrunk to a floating window so the chat stays usable underneath. */
.mini-bar { display: none; }
.call.mini {
  inset: auto; top: calc(env(safe-area-inset-top) + 72px); right: 12px; width: 104px; height: 146px;
  border-radius: 18px; z-index: 40; touch-action: none; cursor: grab;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .45); border: 1px solid rgba(255, 255, 255, .14);
}
@media (min-width: 700px) { .call.mini { width: 200px; height: 130px; } }
/* Wide screens: dock the call as a side panel next to the chat instead of floating over it. */
@media (min-width: 900px) {
  body.call-mini #app { right: 360px; }
  .call.mini {
    top: 0; right: 0; bottom: 0; width: 360px; height: auto; border-radius: 0; border: 0;
    border-left: 1px solid rgba(255, 255, 255, .1); box-shadow: none; cursor: pointer; touch-action: auto;
  }
  .call.mini .avatar.xl { width: 96px; height: 96px; font-size: 38px; }
}

.call.mini .call-controls, .call.mini .call-top, .call.mini .call-badges, .call.mini #local-video, .call.mini .call-status { display: none; }
.call.mini .avatar.xl { width: 56px; height: 56px; font-size: 22px; }
.call.mini #remote-video { pointer-events: none; }
.call.mini .mini-bar {
  position: absolute; z-index: 3; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 18px 8px 8px 10px; background: linear-gradient(transparent, rgba(0, 0, 0, .65)); font-size: 12px; font-variant-numeric: tabular-nums;
}
.mini-actions { display: flex; gap: 6px; }
.mini-btn { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: rgba(255, 255, 255, .2); color: #fff; }
.mini-btn .i { width: 16px; height: 16px; }
.mini-hang { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--danger); color: #fff; }
.mini-hang .i { width: 16px; height: 16px; }

/* ---------- overlays ---------- */
.viewer { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; background: rgba(0, 0, 0, .92); cursor: zoom-out; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
.viewer img { max-width: 100vw; max-height: 100%; object-fit: contain; }
dialog {
  width: min(92vw, 400px); padding: 22px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); box-shadow: 0 30px 80px rgba(0, 0, 0, .4);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack h2, .stack .dialog-actions { margin: 0; }
.avatar-img { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.safety-code { display: grid; grid-template-columns: repeat(3, auto); justify-content: center; gap: 10px 20px; margin-top: 18px; font: 600 22px/1 var(--mono); letter-spacing: .06em; }
.toast {
  position: fixed; z-index: 70; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 84px); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: 14px; font-size: 14px; text-align: center;
  background: #1f2633; color: #fff; box-shadow: 0 12px 34px rgba(0, 0, 0, .35); animation: pop .16s ease-out;
}
