:root {
  --bg: #faf6f3;
  --surface: #ffffff;
  --text: #2a2226;
  --muted: #8a7c82;
  --line: #ece3df;
  --accent: #c2476b;
  --accent-text: #ffffff;
  --mine: #c2476b;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --theirs-text: #2a2226;
  --danger: #b3261e;
  --ok: #2e9d5b;
  --shadow: 0 1px 2px rgba(60, 30, 40, .08);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17131a;
    --surface: #221c25;
    --text: #f3eaee;
    --muted: #a3949b;
    --line: #342b36;
    --accent: #e0668a;
    --mine: #b8456a;
    --theirs: #2b232e;
    --theirs-text: #f3eaee;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
h1, h2, h3 { margin: 0; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; margin: 18px 0 8px; color: var(--muted); font-weight: 600; }

/* Buttons */
.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 10px 18px; font: inherit; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: currentColor; background: transparent; }
.btn.small { padding: 6px 12px; font-size: .88rem; }
.btn:disabled { opacity: .6; cursor: default; }
.center { display: block; margin: 8px auto; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border-radius: 24px;
  padding: 28px 22px; box-shadow: var(--shadow); border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { text-align: center; font-size: 1.5rem; }
.login-card > .muted { text-align: center; margin: -6px 0 6px; }
.logo { text-align: center; font-size: 2.2rem; color: var(--accent); line-height: 1; }
label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--muted); }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; text-align: center; font-size: .9rem; }
.tip { font-size: .85rem; background: var(--bg); border-radius: 12px; padding: 12px; color: var(--muted); }
.tip ul { margin: 6px 0 0; padding-left: 18px; }

/* App shell */
.app { height: 100%; display: flex; flex-direction: column; max-width: 720px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.who { display: flex; align-items: center; gap: 10px; }
.name { font-weight: 700; }
.status { font-size: .8rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--line); flex: none; }
.dot.on { background: var(--ok); }
.tabs { display: flex; background: var(--surface); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; border: 0; background: none; padding: 10px 0; font: inherit; color: var(--muted);
  border-bottom: 2px solid transparent; cursor: pointer;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#panel-videos, #panel-activity { overflow-y: auto; padding: 16px; display: block; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; font-size: .75rem; color: var(--muted); margin: 10px 0 4px; }
.msg { max-width: 80%; display: flex; flex-direction: column; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 8px 12px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word;
  overflow-wrap: anywhere; box-shadow: var(--shadow);
}
.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 6px; }
.theirs .bubble { background: var(--theirs); color: var(--theirs-text); border-bottom-left-radius: 6px; border: 1px solid var(--line); }
.meta { font-size: .7rem; color: var(--muted); margin: 2px 6px 6px; }
.seen { font-size: .7rem; color: var(--muted); align-self: flex-end; margin: -4px 6px 6px; }

.vcard { padding: 0; overflow: hidden; width: 200px; max-width: 100%; cursor: pointer; }
.vthumb { position: relative; background: #000; aspect-ratio: 4 / 5; overflow: hidden; display: grid; place-items: center; }
.vthumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.vthumb .play {
  position: absolute; width: 46px; height: 46px; border-radius: 50%; background: rgba(0,0,0,.55);
  color: #fff; display: grid; place-items: center; font-size: 18px; padding-left: 3px;
}
.vthumb .dur { position: absolute; right: 6px; bottom: 6px; font-size: .72rem; background: rgba(0,0,0,.6); color: #fff; padding: 1px 6px; border-radius: 6px; }
.vthumb .state { color: #fff; font-size: .85rem; padding: 16px; text-align: center; }
.vinfo { padding: 8px 12px; font-size: .88rem; }
.vinfo .cc { opacity: .85; font-size: .8rem; margin-top: 2px; }

/* Composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line);
}
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; padding: 10px 14px; }
.send { width: 44px; height: 44px; padding: 0; flex: none; font-size: 1.05rem; }
.attach {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  display: grid; place-items: center; cursor: pointer; font-size: 1.2rem; color: var(--text);
}
.attach input { display: none; }
.hint { font-size: .72rem; text-align: center; margin: 0; padding: 0 12px 8px; background: var(--surface); }

.upload-bar { padding: 8px 16px; background: var(--surface); border-top: 1px solid var(--line); display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: .85rem; }
.upload-text { flex: 1; }
.progress { flex-basis: 100%; height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; order: 3; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* Video grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.gitem { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; cursor: pointer; }
.gitem .vthumb { aspect-ratio: 1 / 1; }
.gitem .vinfo { padding: 6px 10px; font-size: .78rem; }
.empty { text-align: center; padding: 40px 16px; }

/* Viewer */
.viewer { position: fixed; inset: 0; background: var(--bg); display: flex; flex-direction: column; z-index: 10; max-width: 720px; margin: 0 auto; }
.viewer-body { flex: 1; overflow-y: auto; padding: 0 0 12px; }
.viewer video { width: 100%; height: 56vh; object-fit: contain; background: #000; display: block; }
.viewer-body > :not(video) { margin-left: 16px; margin-right: 16px; }
.small-text { font-size: .8rem; margin-top: 8px; margin-bottom: 0; }
.caption { margin-top: 6px; white-space: pre-wrap; }
.comments { display: flex; flex-direction: column; gap: 10px; }
.comment { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px; }
.comment .by { font-size: .78rem; font-weight: 600; color: var(--accent); }
.comment .when { font-size: .7rem; color: var(--muted); margin-left: 6px; font-weight: 400; }
.comment .text { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 2px; }
.comment .del { float: right; border: 0; background: none; color: var(--muted); font-size: .75rem; cursor: pointer; }

/* Activity */
.activity-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.list { display: flex; flex-direction: column; gap: 8px; }
.row { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.row .t { font-weight: 600; font-size: .92rem; }
.row > .btn { flex: none; white-space: nowrap; }
.row .s { font-size: .78rem; color: var(--muted); }
.badge { display: inline-block; white-space: nowrap; font-size: .7rem; padding: 2px 8px; border-radius: 999px; background: var(--line); color: var(--muted); margin-left: 6px; }
.badge.on { background: var(--ok); color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem; max-width: 90%;
}

/* Clear chat */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 22px; }
.card h2 { margin-bottom: 4px; }
.card .small-text { margin-top: 0; }
.choices { display: flex; flex-direction: column; gap: 10px; margin: 12px 0 14px; }
.choice { flex-direction: row; align-items: center; gap: 10px; color: var(--text); font-size: .92rem; flex-wrap: wrap; }
.choice input { width: auto; margin: 0; accent-color: var(--accent); }
.choice select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; }

/* Call buttons */
.top-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface);
  font-size: 1.05rem; cursor: pointer; display: grid; place-items: center; padding: 0;
}
.icon-btn:disabled { opacity: .4; }

/* Call pills in the chat */
.msg.callmsg { align-self: center; align-items: center; max-width: 90%; }
.callpill {
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px;
  font-size: .85rem; cursor: pointer; color: var(--text);
}
.callpill.missed { color: var(--danger); }

/* Call screen */
.call {
  position: fixed; inset: 0; z-index: 30; background: #120d14; color: #fff;
  display: flex; flex-direction: column; justify-content: space-between; overflow: hidden;
}
.call video.remote { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #120d14; }
.call.voice video.remote, .call:not(.connected) video.remote { opacity: 0; }
.call video.local {
  position: absolute; top: max(14px, env(safe-area-inset-top)); right: 14px; width: 28vw; max-width: 150px;
  aspect-ratio: 3 / 4; object-fit: cover; border-radius: 14px; border: 2px solid rgba(255,255,255,.7);
  background: #000; z-index: 2; transform: scaleX(-1);
}
.call video.local.back { transform: none; }
.call-info { position: relative; z-index: 1; text-align: center; padding: 18vh 16px 0; text-shadow: 0 1px 4px rgba(0,0,0,.6); }
.call.video.connected .call-info { padding-top: max(16px, env(safe-area-inset-top)); text-align: left; }
.call.video.connected .call-avatar { display: none; }
.call-avatar {
  width: 96px; height: 96px; margin: 0 auto 14px; border-radius: 50%; display: grid; place-items: center;
  font-size: 2.6rem; color: #fff; background: var(--accent);
}
.call.ringing .call-avatar { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(224,102,138,.6); } 50% { box-shadow: 0 0 0 22px rgba(224,102,138,0); } }
.call-name { font-size: 1.5rem; font-weight: 700; }
.call-status { opacity: .85; margin-top: 4px; font-size: .95rem; }
.call-controls {
  position: relative; z-index: 1; display: flex; justify-content: center; gap: 18px; flex-wrap: wrap;
  padding: 20px 16px max(28px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(0,0,0,.55));
}
.ctl { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: .75rem; opacity: .95; }
.round {
  width: 60px; height: 60px; border-radius: 50%; border: 0; font-size: 1.4rem; cursor: pointer;
  background: rgba(255,255,255,.18); color: #fff; display: grid; place-items: center;
}
.round.off { background: #fff; color: #120d14; }
.round.red { background: #e5484d; }
.round.green { background: #2e9d5b; }
.call.voice .video-only { display: none; }

/* ---------- v4: lock, setup, profile, photos, background ---------- */
.center-text { text-align: center; margin-top: -4px; }
.pin-input { letter-spacing: .4em; font-size: 1.2rem; text-align: center; }
.pin-input.big { font-size: 1.8rem; padding: 14px; }

.lock {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 24px 16px;
  background: var(--bg);
}
.lock-card { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.lock-card h1 { font-size: 1.3rem; }
.lock-icon { font-size: 2.2rem; }
.lock-note {
  margin: 0; padding: 10px 12px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600;
  animation: pulse 1.6s ease-in-out infinite;
}
/* Nothing of the chat may be visible behind the lock or setup screens */
body.locked #app, body.locked #callScreen, body.locked #viewer { visibility: hidden !important; }

.avatar-wrap { position: relative; width: 40px; height: 40px; flex: none; }
.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; display: block; background: var(--line); }
.avatar.initial {
  display: grid; place-items: center; font-weight: 700; color: #fff; background: var(--accent); font-size: 1.05rem;
}
.avatar-wrap .dot { position: absolute; right: -1px; bottom: -1px; border: 2px solid var(--surface); width: 12px; height: 12px; }
.avatar.big { width: 96px; height: 96px; font-size: 2.2rem; }
.avatar-big-wrap { flex: none; }

#panel-profile { overflow-y: auto; padding: 16px; display: block; }
.profile-head { display: flex; align-items: center; gap: 16px; }
.profile-actions, .row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.card.form { display: flex; flex-direction: column; gap: 8px; }
.card.form .btn { align-self: flex-start; }
label.btn { display: inline-flex; flex-direction: row; color: var(--text); font-size: .88rem; }
.logout-wide { width: 100%; margin: 4px 0 24px; }

.messages.has-bg {
  background-size: cover; background-position: center;
}
.messages.has-bg .day, .messages.has-bg .meta, .messages.has-bg .seen {
  color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,.8);
}

.call-avatar { overflow: hidden; position: relative; }
.call-avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
