:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #15181d;
  --muted: #6b7380;
  --line: #e3e6ea;
  --accent: #2f7cf6;
  --accent-text: #ffffff;
  --mine: #2f7cf6;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --theirs-text: #15181d;
  --danger: #e5484d;
  --ok: #2fb35a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .08);
  --app-h: 100dvh;
  --radius: 18px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --panel: #1b1f25;
    --text: #e9ecf1;
    --muted: #8b93a1;
    --line: #2a3038;
    --accent: #4a8ff8;
    --mine: #2f6fd8;
    --theirs: #232830;
    --theirs-text: #e9ecf1;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
body { position: fixed; inset: 0; }

#app {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: var(--app-h);
  display: flex;
  flex-direction: column;
  will-change: transform;
}

.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.center { align-items: center; justify-content: center; padding: 24px; padding-top: max(24px, env(safe-area-inset-top)); }
.muted { color: var(--muted); }

button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; font-size: 16px; color: var(--text); }

.btn {
  border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600;
  background: var(--line); color: var(--text);
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.secondary { background: var(--panel); border: 1px solid var(--line); }
.btn.text { background: none; color: var(--accent); padding: 8px 10px; }
.btn.small { padding: 8px 14px; }
.btn:disabled { opacity: .5; cursor: default; }

.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Login */
.login-card { width: 100%; max-width: 360px; text-align: center; overflow-y: auto; max-height: 100%; }
.login-logo { border-radius: 18px; }
.login-card h1 { margin: 8px 0 20px; font-size: 26px; }
#login-form { display: flex; flex-direction: column; gap: 10px; }
#login-form input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
}
.error { color: var(--danger); min-height: 1.4em; margin: 4px 0 0; font-size: 14px; }

/* Top bar */
.topbar {
  position: relative;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 8px 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  padding-left: calc(12px + env(safe-area-inset-left));
  padding-right: calc(8px + env(safe-area-inset-right));
  background: var(--panel); border-bottom: 1px solid var(--line);
  z-index: 5;
}
.peer { display: flex; align-items: center; gap: 10px; min-width: 0; }
.peer-text { min-width: 0; }
.peer-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 13px; color: var(--muted); white-space: nowrap; }
.peer-status.online { color: var(--ok); }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, #4a8ff8, #36c2b4);
}
.avatar.big { width: 96px; height: 96px; font-size: 40px; }
.top-actions { display: flex; align-items: center; }
.icon-btn {
  position: relative; border: 0; background: none; width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center; color: var(--accent); flex: none;
}
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:active { background: var(--line); }
.icon-btn.off { color: var(--muted); }
.icon-btn .dot { position: absolute; top: 8px; right: 9px; width: 9px; height: 9px; border-radius: 50%; background: var(--danger); }
.menu {
  position: absolute; right: 8px; top: calc(100% - 4px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18); overflow: hidden; min-width: 220px;
}
.menu button { display: block; width: 100%; text-align: left; padding: 13px 16px; border: 0; background: none; }
.menu button + button { border-top: 1px solid var(--line); }
.menu .danger { color: var(--danger); }

.conn-banner { background: #f5a623; color: #000; text-align: center; font-size: 13px; padding: 4px; }

/* Messages */
.messages {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 8px 10px 12px;
  padding-left: calc(10px + env(safe-area-inset-left));
  padding-right: calc(10px + env(safe-area-inset-right));
  overscroll-behavior: contain;
}
.history-top { height: 24px; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.day-sep { text-align: center; margin: 12px 0 8px; }
.day-sep span {
  display: inline-block; font-size: 12.5px; color: var(--muted);
  background: var(--panel); border-radius: 10px; padding: 3px 10px; box-shadow: var(--shadow);
}
.msg { display: flex; margin: 6px 0; }
/* Bigger gap when the speaker changes and around call records */
.msg.mine + .msg:not(.mine):not(.system),
.msg:not(.mine):not(.system) + .msg.mine { margin-top: 14px; }
.msg.system { margin: 14px 0; }
.msg.mine { justify-content: flex-end; }
.bubble {
  max-width: min(80%, 520px); padding: 7px 11px 6px; border-radius: var(--radius);
  background: var(--theirs); color: var(--theirs-text); box-shadow: var(--shadow);
  word-wrap: break-word; overflow-wrap: anywhere; position: relative;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 6px; }
.msg:not(.mine) .bubble { border-bottom-left-radius: 6px; }
.bubble .text { white-space: pre-wrap; }
.bubble a { color: inherit; text-decoration: underline; }
.meta { float: right; margin: 6px 0 -4px 10px; font-size: 11.5px; opacity: .7; white-space: nowrap; user-select: none; }
.meta .ticks { margin-left: 3px; letter-spacing: -3px; }
.msg.pending .meta { opacity: .5; }
.msg.failed .bubble { outline: 2px solid var(--danger); }
.bubble.media { padding: 3px; overflow: hidden; }
.bubble.media .meta { position: absolute; right: 8px; bottom: 6px; float: none; margin: 0; background: rgba(0,0,0,.45); color: #fff; padding: 1px 6px; border-radius: 8px; opacity: 1; }
.bubble img, .bubble video {
  display: block; max-width: 100%; max-height: 360px; border-radius: 15px; background: rgba(0,0,0,.08);
  min-width: 120px; min-height: 80px; object-fit: cover;
}
.file-link { display: flex; align-items: center; gap: 10px; text-decoration: none !important; min-width: 180px; }
.file-icon {
  width: 40px; height: 40px; border-radius: 10px; flex: none; display: grid; place-items: center;
  background: rgba(127,127,127,.18); font-size: 20px;
}
.file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.file-size { font-size: 12.5px; opacity: .75; }
.progress { height: 4px; border-radius: 2px; background: rgba(127,127,127,.3); margin-top: 6px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: currentColor; transition: width .2s; }

.voice { display: flex; align-items: center; gap: 10px; min-width: 200px; }
.voice-play {
  width: 38px; height: 38px; border-radius: 50%; border: 0; flex: none;
  background: rgba(127,127,127,.22); color: inherit; display: grid; place-items: center;
}
.msg.mine .voice-play { background: rgba(255,255,255,.25); }
.voice-play svg { width: 20px; height: 20px; fill: currentColor; }
.voice-track { flex: 1; height: 4px; border-radius: 2px; background: rgba(127,127,127,.35); position: relative; }
.voice-track > div { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: currentColor; border-radius: 2px; }
.voice-time { font-size: 13px; font-variant-numeric: tabular-nums; min-width: 34px; }

.msg.system { justify-content: center; }
.system-pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13.5px;
  background: var(--panel); border-radius: 14px; padding: 6px 12px; box-shadow: var(--shadow);
}
.system-pill.missed { color: var(--danger); }
.system-pill .time { color: var(--muted); font-size: 12px; }

.scroll-down {
  position: absolute; right: calc(14px + env(safe-area-inset-right)); bottom: calc(80px + env(safe-area-inset-bottom));
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--panel); color: var(--muted); display: grid; place-items: center; box-shadow: 0 2px 10px rgba(0,0,0,.15); z-index: 4;
}
.scroll-down svg { width: 24px; height: 24px; fill: currentColor; }
.badge {
  position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 12px; display: grid; place-items: center;
}

/* Composer */
.composer {
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  padding-left: calc(6px + env(safe-area-inset-left));
  padding-right: calc(6px + env(safe-area-inset-right));
}
.kb-open .composer { padding-bottom: 6px; }
.compose-row { display: flex; align-items: flex-end; gap: 4px; }
#input {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 20px; background: var(--bg);
  padding: 9px 14px; max-height: 140px; min-height: 42px; line-height: 1.35; outline: none;
}
.icon-btn.accent { color: var(--accent); }
.record-row { display: flex; align-items: center; justify-content: space-between; min-height: 42px; padding: 0 4px; }
.rec-info { display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }

/* Call screen */
.call {
  position: fixed; inset: 0; z-index: 50; background: #0d1014; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: calc(40px + env(safe-area-inset-top)) 16px calc(28px + env(safe-area-inset-bottom));
}
.remote-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.call:not(.has-video) .remote-video { width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.local-video {
  position: absolute; right: 14px; top: calc(14px + env(safe-area-inset-top));
  width: 104px; height: 150px; object-fit: cover; border-radius: 14px; background: #222;
  box-shadow: 0 4px 18px rgba(0,0,0,.4); z-index: 2;
}
.local-video.mirror { transform: scaleX(-1); }
.call:not(.video-call) .local-video, .call:not(.has-local) .local-video { display: none; }
.call-info { position: relative; z-index: 1; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.call.has-video .call-info { align-self: flex-start; align-items: flex-start; text-shadow: 0 1px 4px rgba(0,0,0,.7); }
.call.has-video .call-info .avatar { display: none; }
.call-name { font-size: 26px; font-weight: 650; }
.call-status { font-size: 16px; opacity: .85; font-variant-numeric: tabular-nums; }
.unmute { position: relative; z-index: 3; }
.call-controls { position: relative; z-index: 2; display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; }
.round {
  width: 68px; border: 0; background: none; color: #fff;
  display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12px;
}
.round svg {
  width: 60px; height: 60px; padding: 16px; border-radius: 50%;
  background: rgba(255,255,255,.18); fill: #fff; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.round.off svg { background: #fff; fill: #111; }
.round.red svg { background: var(--danger); }
.round.green svg { background: var(--ok); }
#call-incoming .round { width: 96px; }
#call-incoming .round svg { width: 72px; height: 72px; padding: 19px; }
.call:not(.video-call) #btn-cam, .call:not(.video-call) #btn-flip { display: none; }

/* Viewer */
.viewer {
  position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-dl { position: absolute; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); text-decoration: none; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 100;
  background: rgba(20,22,26,.92); color: #fff; padding: 10px 16px; border-radius: 12px;
  max-width: 90vw; font-size: 15px; text-align: center;
}

/* Install button + bottom sheets */
.install-btn { width: 100%; margin-top: 16px; }
.btn.wide { width: 100%; }
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.45);
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
}
.sheet-backdrop.center-sheet { justify-content: center; }
.sheet {
  width: 100%; max-width: 420px; background: var(--panel); color: var(--text);
  border-radius: 20px; padding: 22px 20px 16px; text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,.3); display: flex; flex-direction: column; gap: 10px;
}
.sheet h2 { margin: 2px 0 4px; font-size: 20px; }
.sheet p { margin: 0 0 6px; }
.sheet-icon { align-self: center; border-radius: 15px; }
.sheet-emoji { font-size: 40px; }
.install-steps { text-align: left; display: flex; flex-direction: column; gap: 10px; margin: 6px 0 8px; }
.install-step { display: flex; gap: 10px; align-items: flex-start; }
.step-num {
  flex: none; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
}
.sheet-arrow { color: #fff; font-size: 34px; margin-top: 6px; animation: bob 1s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(6px); } }
.share-glyph { color: var(--accent); }
