:root {
  color-scheme: light dark;
  --bg: #f3f2fa;
  --panel: #fdfcff;
  --panel-2: #f0eff7;
  --text: #201d32;
  --muted: #625e76;
  --line: #e5e2ee;
  --accent: #5442c7;
  --accent-solid: #5543cb;
  --accent-text: #fff;
  --accent-soft: #ebe7fb;
  --in: #fff;
  --out: #e8e2fa;
  --danger: #b72c45;
  --danger-fill: #b72c45;
  --success: #148257;
  --chat-bg: #f3f1f9;
  --chat-dot: #ded9ef;
  --shadow: 0 14px 40px #23174329;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15141e;
    --panel: #1d1b29;
    --panel-2: #282536;
    --text: #f3effb;
    --muted: #b2aac5;
    --line: #353145;
    --accent: #c6b9ff;
    --accent-solid: #6650db;
    --accent-soft: #332952;
    --in: #252232;
    --out: #382d57;
    --danger: #ff9aaa;
    --danger-fill: #ab304b;
    --success: #65d8a5;
    --chat-bg: #16141f;
    --chat-dot: #2b263c;
    --shadow: 0 14px 40px #0008;
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, textarea, select { font: inherit; color: inherit; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
a { color: var(--accent); text-underline-offset: .16em; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
#app { position: relative; height: 100dvh; display: flex; overflow: hidden; }

/* PIN screen */
#app:has(> .login) {
  overflow: auto;
  padding: calc(16px + var(--safe-top)) calc(12px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(12px + var(--safe-left));
}
.login {
  margin: auto;
  flex: none;
  width: min(100%, 380px);
  padding: 30px 24px 28px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 28px;
  box-shadow: var(--shadow);
}
.login h1 { margin: 0 0 4px; font-size: 29px; line-height: 1.2; letter-spacing: -.03em; }
.login h1::before {
  content: "";
  display: block;
  width: 58px;
  height: 58px;
  margin: 0 auto 17px;
  border-radius: 18px;
  background: var(--accent-solid) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='1.7' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20.5 11.5a7.5 7.5 0 0 1-7.5 7.5H9l-5 3v-5.5a7.5 7.5 0 0 1-1.5-4.5v-1a7.5 7.5 0 0 1 7.5-7.5h3a7.5 7.5 0 0 1 7.5 7.5zM8 10h7m-7 4h4'/%3E%3C/svg%3E") center / 29px no-repeat;
}
.login p { color: var(--muted); margin: 0 0 21px; min-height: 1.45em; }
.login p.err { color: var(--danger); }
.dots { display: flex; gap: 13px; justify-content: center; margin-bottom: 25px; }
.dots span { width: 12px; height: 12px; border-radius: 50%; border: 1px solid var(--muted); }
.dots span.on { background: var(--accent); border-color: var(--accent); }
.dots.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
.pad { display: grid; grid-template-columns: repeat(3, minmax(0, 72px)); gap: 9px 12px; justify-content: center; }
.pad button { height: 60px; border-radius: 17px; background: var(--panel-2); font-size: 25px; font-weight: 550; }
.pad button:active { background: var(--accent-soft); color: var(--accent); }
.pad button.ghost { background: transparent; color: var(--muted); font-size: 13px; }
@media (max-height: 570px) {
  .login { padding-block: 18px; }
  .login h1::before { width: 42px; height: 42px; background-size: 23px; margin-bottom: 10px; }
  .login p, .dots { margin-bottom: 12px; }
  .pad button { height: 46px; }
}

/* Shell and navigation */
.sidebar { width: clamp(305px, 29vw, 382px); flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; }
.main {
  --lane: max(20px, calc((100% - 740px) / 2));
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--chat-bg) radial-gradient(circle, var(--chat-dot) .8px, transparent 1px) center / 19px 19px;
}
.info { width: 340px; flex: none; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; overflow: auto; }
.topbar {
  display: flex;
  flex: none;
  align-items: center;
  gap: 10px;
  padding: calc(9px + var(--safe-top)) calc(16px + var(--safe-right)) 9px calc(16px + var(--safe-left));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.topbar .title { flex: 1; min-width: 0; }
.topbar .title b, .topbar .title small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title b { font-weight: 700; font-size: 16px; }
.topbar .title small { color: var(--muted); font-size: 12px; }
.sidebar .topbar { border-bottom: 0; padding-top: calc(17px + var(--safe-top)); }
.sidebar .topbar .title b {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  font-size: 32px;
  line-height: 1.12;
  letter-spacing: -.04em;
}
.icon-btn { width: 44px; height: 44px; border-radius: 13px; display: grid; place-items: center; color: var(--muted); flex: none; }
.icon-btn svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.sidebar .topbar .icon-btn { background: var(--panel-2); color: var(--text); }
.icon-btn:active, .sidebar .topbar .icon-btn:active { background: var(--accent-soft); color: var(--accent); }
.back { display: none; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); flex: none; }
.status-dot.off { background: var(--danger); }

/* Search, filters and chat list */
.search { position: relative; padding: 4px calc(16px + var(--safe-right)) 12px calc(16px + var(--safe-left)); }
.search::before {
  content: "";
  position: absolute;
  left: calc(30px + var(--safe-left));
  top: 17px;
  width: 19px;
  height: 19px;
  background: var(--muted);
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cpath d='m16 16 4.5 4.5'/%3E%3C/svg%3E") center / contain no-repeat;
  pointer-events: none;
}
.search input { display: block; width: 100%; min-height: 45px; border: 1px solid transparent; border-radius: 14px; background: var(--panel-2); padding: 9px 12px 9px 40px; font-size: 16px; }
.search input:focus, .composer textarea:focus-visible { outline: 0; border-color: var(--accent); }
.filters { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 calc(16px + var(--safe-right)) 10px calc(16px + var(--safe-left)); border-bottom: 1px solid var(--line); }
.filters button { min-height: 38px; padding: 5px 15px; border-radius: 11px; background: var(--panel-2); color: var(--muted); font-size: 14px; font-weight: 600; }
.filters button.on { background: var(--accent-soft); color: var(--accent); }
.list { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 6px var(--safe-right) calc(6px + var(--safe-bottom)) var(--safe-left); }
.list .section { padding: 12px 17px 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.item { display: flex; gap: 12px; min-height: 69px; margin: 0 8px; padding: 9px 8px; border-radius: 14px; align-items: center; cursor: pointer; }
.item.active, .item:active { background: var(--accent-soft); }
.item.active .name { color: var(--accent); }
.item .body { flex: 1; min-width: 0; }
.item .row { display: flex; gap: 8px; align-items: center; min-height: 20px; }
.item .name, .item .sub { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .name { font-size: 15px; font-weight: 700; }
.item .time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; flex: none; }
.item .sub { color: var(--muted); font-size: 14px; }
.badge { background: var(--accent-solid); color: var(--accent-text); border-radius: 999px; min-width: 21px; height: 21px; padding: 0 6px; font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; display: grid; place-items: center; flex: none; }
.badge.muted { background: var(--panel-2); color: var(--muted); }

.avatar { width: 49px; height: 49px; border-radius: 17px; flex: none; display: grid; place-items: center; color: #211a2f; font-weight: 750; overflow: hidden; background: #b6b0ca; font-size: 17px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.sm { width: 40px; height: 40px; border-radius: 14px; font-size: 14px; }
.avatar.lg { width: 88px; height: 88px; border-radius: 28px; font-size: 30px; }
.avatar.self { background: var(--accent-soft); color: var(--accent); }

/* Conversation */
.empty { margin: auto; color: var(--muted); padding: 26px 18px; text-align: center; }
.main > .empty, .messages > .empty { padding: 12px 20px; background: var(--panel); border: 1px solid var(--line); border-radius: 15px; }
.messages { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 10px var(--lane) 12px; display: flex; flex-direction: column; gap: 4px; }
.messages > * { flex-shrink: 0; }
.day { align-self: center; background: var(--panel); border: 1px solid var(--line); color: var(--muted); font-size: 12px; font-weight: 600; padding: 4px 11px; border-radius: 999px; margin: 9px 0 7px; }
.service { align-self: center; color: var(--muted); font-size: 13px; margin: 4px 0; text-align: center; }
.msg { max-width: min(84%, 540px); align-self: flex-start; display: flex; align-items: flex-end; }
.msg.out { align-self: flex-end; }
.msg:not(.out) + .msg.out, .msg.out + .msg:not(.out) { margin-top: 3px; }
.bubble {
  min-width: 0;
  max-width: 100%;
  background: var(--in);
  border-radius: 16px 16px 16px 6px;
  padding: 7px 10px 6px;
  box-shadow: 0 1px 2px #211a2f14;
  overflow-wrap: anywhere;
}
.bubble::after { content: ""; display: block; clear: both; }
.msg.out .bubble { background: var(--out); border-radius: 16px 16px 6px 16px; }
.msg.flash .bubble { animation: flash 1.2s; }
@keyframes flash { 30% { box-shadow: 0 0 0 3px var(--accent); } }
.author { font-size: 13px; font-weight: 700; color: var(--accent); margin-bottom: 3px; cursor: pointer; }
.text { white-space: pre-wrap; }
.meta { float: right; margin: 5px 0 0 9px; font-size: 11px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.meta .ticks { color: var(--accent); margin-left: 4px; }
.quote, .share { border-left: 3px solid var(--accent); padding: 4px 8px; margin: 1px 0 5px; border-radius: 5px; background: var(--panel-2); font-size: 14px; }
.quote { cursor: pointer; }
.quote b { display: block; color: var(--accent); font-size: 13px; }
.quote span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.fwd { font-size: 13px; color: var(--muted); margin-bottom: 3px; }
.media { margin: -3px -6px 4px; border-radius: 12px; overflow: hidden; position: relative; background: var(--panel-2); display: block; max-width: 340px; }
.media img, .media video { display: block; width: 100%; height: auto; max-height: 420px; object-fit: cover; }
.media .play { position: absolute; inset: 0; display: grid; place-items: center; }
.media .play span { width: 52px; height: 52px; border-radius: 50%; background: #0009; display: grid; place-items: center; }
.media .play svg { width: 22px; height: 22px; fill: #fff; margin-left: 3px; }
.media .dur { position: absolute; left: 6px; bottom: 6px; background: #000a; color: #fff; font-size: 12px; padding: 1px 6px; border-radius: 6px; }
.sticker img { width: 160px; max-width: 100%; height: auto; display: block; }
.file { display: flex; gap: 10px; align-items: center; padding: 3px 0 5px; text-decoration: none; color: inherit; }
.file .ic { width: 42px; height: 42px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; flex: none; }
.file .ic svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.file > span:last-child { min-width: 0; }
.file b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.file small, .share small { color: var(--muted); }
audio { width: 240px; max-width: 100%; display: block; margin: 2px 0 6px; }
.share b { display: block; }
.loading { align-self: center; color: var(--muted); font-size: 13px; padding: 8px; }
.loading:empty { display: none; }

/* Composer */
.composer { flex: none; background: var(--panel); border-top: 1px solid var(--line); padding: 8px var(--lane) calc(8px + var(--safe-bottom)); }
.composer .row { display: flex; align-items: flex-end; gap: 7px; }
.composer .bar { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; padding: 2px 4px 2px 9px; border-radius: 12px; background: var(--panel-2); font-size: 14px; }
.composer .bar .what { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; }
.composer .bar .what b { color: var(--accent); display: block; font-size: 13px; }
.composer .bar .what span { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.composer textarea { flex: 1; min-width: 0; resize: none; border: 1px solid transparent; background: var(--panel-2); border-radius: 15px; padding: 10px 12px; max-height: 160px; min-height: 46px; line-height: 1.4; font-size: 16px; }
.send { width: 46px; height: 46px; border-radius: 15px; background: var(--accent-solid); color: var(--accent-text); display: grid; place-items: center; flex: none; }
.send svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.send:active { filter: brightness(.9); }
.send:disabled, .btn:disabled { opacity: .55; cursor: default; }

/* Menus, sheets and media viewer */
.menu { position: fixed; z-index: 60; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 5px; min-width: 190px; }
.menu button { display: block; width: 100%; min-height: 44px; text-align: left; padding: 9px 12px; border-radius: 10px; }
.menu button:active { background: var(--panel-2); }
.menu button.danger { color: var(--danger); }
.scrim { position: fixed; inset: 0; z-index: 50; background: #15112277; display: flex; align-items: center; justify-content: center; padding: 16px; }
.scrim.clear { background: transparent; }
.sheet { background: var(--panel); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); width: 100%; max-width: 440px; max-height: 90dvh; overflow: auto; overscroll-behavior: contain; padding: 20px; }
.sheet h3 { margin: 0 0 12px; font-size: 20px; line-height: 1.2; letter-spacing: -.02em; }
.sheet .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 17px; flex-wrap: wrap; }
.btn { min-height: 44px; padding: 8px 13px; border-radius: 12px; background: var(--panel-2); font-weight: 600; }
.btn.primary { background: var(--accent-solid); color: var(--accent-text); }
.btn.danger { background: var(--danger-fill); color: var(--accent-text); }
.btn:active { filter: brightness(.9); }
.field { display: block; margin: 12px 0; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input, .field textarea, .field select, .upitem .nm input { width: 100%; min-height: 44px; border: 1px solid var(--line); background: var(--panel-2); border-radius: 12px; padding: 8px 10px; font-size: 16px; }
.check { display: flex; gap: 8px; align-items: center; min-height: 44px; margin: 6px 0; }
.check input { accent-color: var(--accent-solid); }
.uplist { display: flex; flex-direction: column; gap: 8px; }
.upitem { display: flex; gap: 10px; align-items: center; }
.upitem img, .upitem .ph { width: 54px; height: 54px; object-fit: cover; border-radius: 11px; background: var(--panel-2); flex: none; display: grid; place-items: center; font-size: 12px; color: var(--muted); }
.upitem .nm { flex: 1; min-width: 0; font-size: 14px; overflow-wrap: anywhere; }
.progress { height: 5px; background: var(--panel-2); border-radius: 5px; overflow: hidden; margin-top: 12px; }
.progress div { height: 100%; width: 0; background: var(--accent-solid); transition: width .15s; }
.note { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.lightbox { position: fixed; inset: 0; z-index: 70; background: #100e17f5; display: flex; align-items: center; justify-content: center; }
.lightbox img, .lightbox video { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
.lightbox .lb-btn { position: absolute; background: #100e17b3; border-radius: 50%; color: #fff; width: 46px; height: 46px; display: grid; place-items: center; font-size: 26px; text-decoration: none; }
.lightbox .lb-close { top: calc(8px + var(--safe-top)); right: calc(8px + var(--safe-right)); }
.lightbox .lb-prev { left: calc(8px + var(--safe-left)); top: 50%; transform: translateY(-50%); }
.lightbox .lb-next { right: calc(8px + var(--safe-right)); top: 50%; transform: translateY(-50%); }
.lightbox .lb-dl { top: calc(8px + var(--safe-top)); right: calc(62px + var(--safe-right)); font-size: 21px; }

/* Profile and group details */
.info .topbar { position: sticky; top: 0; z-index: 1; }
.info > :last-child { padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left); }
.info .head { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 24px 16px 18px; text-align: center; overflow-wrap: anywhere; }
.info .head b { font-size: 22px; letter-spacing: -.02em; }
.info .head small { color: var(--muted); }
.info .rows { padding: 4px 16px 12px; overflow-wrap: anywhere; }
.info .rows div { padding: 10px 0; border-bottom: 1px solid var(--line); }
.info .rows small { display: block; color: var(--muted); font-size: 12px; }
.info h4 { margin: 14px 16px 7px; font-size: 13px; color: var(--muted); font-weight: 700; }
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; padding: 0 16px 16px; }
.grid button { aspect-ratio: 1; overflow: hidden; border-radius: 10px; background: var(--panel-2); }
.grid img { width: 100%; height: 100%; object-fit: cover; display: block; }
.toggle { display: flex; gap: 8px; justify-content: space-between; align-items: center; min-height: 50px; padding: 8px 16px; }
.switch { width: 46px; height: 44px; border-radius: 999px; position: relative; flex: none; }
.switch::before { content: ""; position: absolute; inset: 8px 0; border-radius: inherit; background: var(--line); }
.switch::after { content: ""; position: absolute; top: 11px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px #0003; transition: transform .15s; }
.switch.on::before { background: var(--accent-solid); }
.switch.on::after { transform: translateX(18px); }
.toast { position: fixed; left: 50%; bottom: calc(22px + var(--safe-bottom)); transform: translateX(-50%); max-width: calc(100% - 28px); background: #292137; color: #fff; padding: 10px 15px; border-radius: 13px; z-index: 80; font-size: 14px; box-shadow: var(--shadow); overflow-wrap: anywhere; }

@media (hover: hover) {
  .icon-btn:hover, .item:not(.active):hover, .menu button:hover, .pad button.ghost:hover { background: var(--panel-2); }
  .sidebar .topbar .icon-btn:hover, .filters button:hover, .pad button:not(.ghost):hover { background: var(--accent-soft); color: var(--accent); }
  .send:not(:disabled):hover { filter: brightness(1.08); }
}
@media (max-width: 1100px) {
  .info { position: absolute; inset: 0 0 0 auto; z-index: 20; box-shadow: var(--shadow); }
}
@media (max-width: 760px) {
  .sidebar { width: 100%; border: 0; }
  #app.in-chat .sidebar { visibility: hidden; transition: visibility 0s .22s; }
  .main { position: absolute; inset: 0; z-index: 10; visibility: hidden; transform: translateX(100%); transition: transform .22s ease, visibility 0s .22s; }
  #app.in-chat .main { visibility: visible; transform: none; transition-delay: 0s; }
  .main .topbar { gap: 8px; padding-right: calc(8px + var(--safe-right)); padding-left: calc(6px + var(--safe-left)); }
  .back { display: grid; }
  .filters button { min-height: 44px; }
  .messages { padding-right: calc(9px + var(--safe-right)); padding-left: calc(9px + var(--safe-left)); }
  .composer { padding-right: calc(9px + var(--safe-right)); padding-left: calc(9px + var(--safe-left)); }
  .composer .row { gap: 5px; }
  .info { width: 100%; border: 0; }
  .scrim:not(.clear) { align-items: flex-end; padding: calc(8px + var(--safe-top)) var(--safe-right) 0 var(--safe-left); }
  .sheet { max-width: 100%; max-height: min(90dvh, calc(100dvh - 8px - var(--safe-top))); border-radius: 22px 22px 0 0; padding: 18px 16px calc(15px + var(--safe-bottom)); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; transition-delay: 0s !important; }
  .dots.shake, .msg.flash .bubble { animation: none; }
  .msg.flash .bubble { box-shadow: 0 0 0 2px var(--accent); }
}
