/* ───────── tokens ───────── */
:root {
  --brand: #3b3bf0; --brand-hover: #2e2ed1; --brand-soft: #eeeefd; --brand-ink: #2a2ac4;
  --bg: #eef0f5; --surface: #ffffff; --surface-2: #f6f7fa; --surface-3: #eceef3;
  --text: #0f1424; --text-2: #3c4458; --muted: #6b7385; --faint: #9aa1b2;
  --line: #e4e7ee; --line-2: #eef0f4;
  --bubble-in: #ffffff; --bubble-in-text: #0f1424; --bubble-out: #3b3bf0; --bubble-out-text: #ffffff;
  --chat-bg: #eef0f6; --chat-dot: rgba(15, 20, 36, .045);
  --ok: #12a150; --warn: #c27803; --err: #c4291c; --err-bg: #fdecea;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 1px 1px rgba(16,24,40,.03);
  --shadow-lg: 0 24px 64px rgba(12,16,40,.18), 0 2px 6px rgba(12,16,40,.06);
  --r: 12px;
  --side-w: 380px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --brand: #5a5af5; --brand-hover: #6e6ef7; --brand-soft: #1f2040; --brand-ink: #a9a9fb;
    --bg: #07090f; --surface: #11141d; --surface-2: #171b26; --surface-3: #1e2331;
    --text: #eceef6; --text-2: #c3c8d6; --muted: #8b93a7; --faint: #5e667a;
    --line: #232838; --line-2: #1a1e2b;
    --bubble-in: #1b2030; --bubble-in-text: #eceef6; --bubble-out: #4848ee; --bubble-out-text: #fff;
    --chat-bg: #0b0e16; --chat-dot: rgba(255, 255, 255, .035);
    --ok: #3ccf7a; --warn: #f0b04a; --err: #ff7b6e; --err-bg: #2b1513;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --shadow-lg: 0 24px 64px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); font-size: 15px; line-height: 1.45; -webkit-text-size-adjust: 100%; -webkit-font-smoothing: antialiased; overscroll-behavior: none; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; font-size: 16px; color: var(--text); }
::placeholder { color: var(--faint); }
.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); font-size: 14px; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ───────── controls ───────── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 44px; padding: 0 20px; border: 0; border-radius: 10px; background: var(--brand); color: #fff; font-weight: 600; font-size: 15px; transition: background .15s, transform .05s; }
.btn:hover { background: var(--brand-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.block { width: 100%; height: 50px; }
.btn.ghost { background: transparent; color: var(--text-2); box-shadow: inset 0 0 0 1px var(--line); }
.btn.ghost:hover { background: var(--surface-2); }
.icon-btn { width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; border: 0; border-radius: 10px; background: transparent; color: var(--muted); transition: background .15s, color .15s; text-decoration: none; }
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.light { color: #fff; background: rgba(255,255,255,.12); }
.icon-btn.light:hover { background: rgba(255,255,255,.22); color: #fff; }
.alert { padding: 11px 13px; border-radius: 10px; background: var(--err-bg); color: var(--err); font-size: 14px; margin-bottom: 14px; }
.field { display: block; margin-bottom: 16px; }
.field > span { display: block; margin-bottom: 7px; font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.field em { font-style: normal; font-weight: 400; color: var(--muted); }
.field input, .field select, .field textarea { display: block; width: 100%; height: 48px; padding: 0 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); transition: border-color .15s, box-shadow .15s; }
.field textarea { height: auto; padding: 12px 14px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent); }

/* ───────── sign in ───────── */
.login { min-height: 100dvh; display: grid; grid-template-columns: 1fr 1fr; background: var(--surface); }
.login__brand { display: flex; flex-direction: column; justify-content: center; padding: 56px; background: var(--brand); color: #fff; }
.login__brand img { border-radius: 14px; margin-bottom: 28px; box-shadow: 0 0 0 2px rgba(255,255,255,.4); }
.login__brand h1 { margin: 0 0 10px; font-size: 34px; letter-spacing: -.025em; line-height: 1.15; }
.login__brand p { margin: 0; max-width: 380px; font-size: 17px; color: rgba(255,255,255,.82); }
.login__card { width: 100%; max-width: 400px; margin: auto; padding: 32px 24px; }
.login__card h2 { margin: 0 0 6px; font-size: 28px; letter-spacing: -.02em; }
.login__card .muted { margin: 0 0 26px; }
@media (max-width: 859px) {
  .login { display: flex; flex-direction: column; background: var(--brand); }
  .login__brand { padding: calc(env(safe-area-inset-top, 0px) + 34px) 26px 58px; }
  .login__brand img { width: 46px; height: 46px; margin-bottom: 18px; }
  .login__brand h1 { font-size: 26px; }
  .login__brand p { font-size: 15px; }
  .login__card { flex: 1; max-width: none; margin: -26px 0 0; padding: 30px 24px calc(env(safe-area-inset-bottom, 0px) + 24px); background: var(--surface); border-radius: 26px 26px 0 0; }
  .login__card > * { max-width: 460px; margin-left: auto; margin-right: auto; }
}

/* ───────── shell ───────── */
.app { height: var(--app-h, 100dvh); display: grid; grid-template-columns: var(--side-w) minmax(0, 1fr); background: var(--surface); overflow: hidden; }
@media (min-width: 720px) and (max-width: 1099px) { :root { --side-w: 320px; } }
@media (min-width: 1500px) {
  body { padding: 24px; }
  .app { height: calc(100dvh - 48px); max-width: 1640px; margin: 0 auto; border-radius: 22px; box-shadow: var(--shadow-lg); }
}

/* sidebar */
.side { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--line); }
.side__head { display: flex; align-items: center; justify-content: space-between; padding: calc(14px + env(safe-area-inset-top, 0px)) 14px 10px 18px; }
.brand { display: flex; align-items: center; gap: 11px; }
.brand img { border-radius: 9px; }
.brand strong { display: block; font-size: 18px; letter-spacing: -.015em; line-height: 1.1; }
.brand span { font-size: 11.5px; color: var(--muted); letter-spacing: .06em; text-transform: uppercase; font-weight: 500; }
.side__actions { display: flex; gap: 2px; }
.seg { display: flex; margin: 6px 16px 12px; padding: 3px; background: var(--surface-3); border-radius: 11px; }
.seg button { flex: 1; height: 36px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 600; font-size: 14px; display: flex; align-items: center; justify-content: center; gap: 7px; transition: background .15s, color .15s, box-shadow .15s; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg .n { min-width: 19px; height: 19px; padding: 0 6px; border-radius: 20px; background: var(--brand); color: #fff; font-size: 11.5px; line-height: 19px; font-weight: 600; }
.search { position: relative; margin: 0 16px 12px; }
.search .ic { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--faint); pointer-events: none; }
.search input { width: 100%; height: 42px; padding: 0 14px 0 40px; border: 1px solid transparent; border-radius: 11px; background: var(--surface-3); transition: background .15s, border-color .15s; }
.search input:focus { outline: none; background: var(--surface); border-color: var(--brand); }
.list { flex: 1; overflow-y: auto; overscroll-behavior: contain; border-top: 1px solid var(--line-2); padding-bottom: env(safe-area-inset-bottom, 0px); }
.item { position: relative; display: flex; gap: 13px; align-items: center; width: 100%; padding: 13px 18px; border: 0; background: transparent; text-align: left; transition: background .12s; }
.item::after { content: ""; position: absolute; left: 73px; right: 0; bottom: 0; height: 1px; background: var(--line-2); }
.item:hover { background: var(--surface-2); }
.item.on { background: var(--brand-soft); }
.item.on::before { content: ""; position: absolute; left: 0; top: 10px; bottom: 10px; width: 3px; border-radius: 0 3px 3px 0; background: var(--brand); }
.avatar { width: 44px; height: 44px; flex: none; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: 600; font-size: 16px; user-select: none; }
.item__body { flex: 1; min-width: 0; }
.item__top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.item__name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item__time { flex: none; display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--faint); }
.item__time .ic { width: 12px; height: 12px; }
.item.unread .item__time { color: var(--brand); font-weight: 600; }
.item__bot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.item__text { font-size: 13.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item.unread .item__text { color: var(--text-2); font-weight: 500; }
.badge { flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 20px; background: var(--brand); color: #fff; font-size: 12px; font-weight: 600; line-height: 20px; text-align: center; }
.none { padding: 56px 28px; text-align: center; color: var(--muted); font-size: 14px; }
.skel { display: flex; gap: 13px; padding: 13px 18px; align-items: center; }
.skel i { display: block; border-radius: 8px; background: linear-gradient(90deg, var(--surface-3), var(--surface-2), var(--surface-3)); background-size: 200% 100%; animation: sh 1.3s infinite; }
@keyframes sh { to { background-position: -200% 0; } }

/* chat */
.chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.empty { margin: auto; max-width: 340px; padding: 24px; text-align: center; }
.empty__ic { width: 64px; height: 64px; margin: 0 auto 18px; display: flex; align-items: center; justify-content: center; border-radius: 20px; background: var(--brand-soft); color: var(--brand); }
.empty__ic .ic { width: 28px; height: 28px; }
.empty h3 { margin: 0 0 6px; font-size: 18px; letter-spacing: -.01em; }
.empty p { margin: 0; color: var(--muted); font-size: 14.5px; }
.chat__inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat__head { display: flex; align-items: center; gap: 12px; padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px 10px; background: var(--surface); border-bottom: 1px solid var(--line); }
.chat__head .back { display: none; }
.chat__who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat__who strong { font-size: 16px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat__who span { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { flex: none; display: inline-flex; align-items: center; gap: 7px; height: 30px; padding: 0 12px; border-radius: 20px; font-size: 12.5px; font-weight: 600; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.pill i { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.pill.warn i { background: var(--warn); }
.pill.off i { background: var(--faint); }

.msgs-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.msgs { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 16px clamp(12px, 4vw, 40px) 14px; display: flex; flex-direction: column; background-image: radial-gradient(var(--chat-dot) 1px, transparent 1px); background-size: 18px 18px; }
.mrow { display: flex; flex-direction: column; margin-top: 10px; }
.mrow.grp { margin-top: 2px; }
.day { align-self: center; margin: 14px 0 8px; padding: 4px 13px; border-radius: 20px; background: var(--surface); color: var(--muted); font-size: 12px; font-weight: 500; box-shadow: var(--shadow); }
.m { max-width: min(76%, 560px); padding: 8px 12px 6px; border-radius: 16px; box-shadow: var(--shadow); word-wrap: break-word; overflow-wrap: anywhere; }
.mrow.in .m { align-self: flex-start; background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 5px; }
.mrow.out .m { align-self: flex-end; background: var(--bubble-out); color: var(--bubble-out-text); border-bottom-right-radius: 5px; }
.mrow.in.grp .m { border-top-left-radius: 5px; }
.mrow.out.grp .m { border-top-right-radius: 5px; }
.m .t { white-space: pre-wrap; font-size: 15px; line-height: 1.42; }
.m .t a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.mrow.in .m .t a { color: var(--brand); }
.m img.pic { display: block; width: 100%; max-width: 340px; max-height: 380px; object-fit: cover; border-radius: 11px; margin: 2px 0 5px; cursor: zoom-in; background: var(--surface-3); }
.m img.pic.sticker { max-width: 160px; background: none; object-fit: contain; }
.m audio { display: block; width: 260px; max-width: 100%; height: 40px; margin: 2px 0 5px; }
.m video { display: block; max-width: 100%; max-height: 360px; border-radius: 11px; margin: 2px 0 5px; background: #000; }
.doc { display: flex; align-items: center; gap: 11px; margin: 2px 0 5px; padding: 10px 12px; border-radius: 11px; background: rgba(127,127,150,.14); color: inherit; text-decoration: none; font-size: 14px; font-weight: 500; }
.doc:hover { background: rgba(127,127,150,.22); }
.doc .ic { width: 22px; height: 22px; opacity: .85; }
.doc span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc.soft { font-weight: 400; opacity: .8; }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; margin-top: 2px; font-size: 11px; opacity: .72; }
.meta .ic { width: 15px; height: 15px; stroke-width: 2.2; }
.tick.read { color: #8ee2ff; opacity: 1; }
.mrow.in .meta { opacity: .6; }
.tag { display: inline-block; margin-bottom: 4px; padding: 1px 8px; border-radius: 6px; font-size: 10.5px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; background: rgba(127,127,150,.2); }
.fail { display: flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 12px; color: #ffd0cb; }
.fail .ic { width: 14px; height: 14px; }
.to-bottom { position: absolute; right: 18px; bottom: 14px; width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; border: 0; border-radius: 50%; background: var(--surface); color: var(--text-2); box-shadow: var(--shadow-lg); }
.to-bottom span { position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 20px; background: var(--brand); color: #fff; font-size: 11.5px; line-height: 20px; font-weight: 600; text-align: center; }
.drop { position: absolute; inset: 10px; display: flex; align-items: center; justify-content: center; border: 2px dashed var(--brand); border-radius: 16px; background: color-mix(in srgb, var(--surface) 88%, var(--brand)); z-index: 3; color: var(--brand); font-size: 16px; }
.drop div { display: flex; align-items: center; gap: 10px; }

/* closed window */
.closed { padding: 16px 18px calc(16px + env(safe-area-inset-bottom, 0px)); background: var(--surface); border-top: 1px solid var(--line); }
.closed__head { display: flex; gap: 13px; }
.closed__ic { flex: none; width: 38px; height: 38px; display: flex; align-items: center; justify-content: center; border-radius: 11px; background: var(--surface-3); color: var(--muted); }
.closed strong { font-size: 15px; }
.closed p { margin: 3px 0 0; color: var(--muted); font-size: 13.5px; line-height: 1.45; }
.closed__preview { margin: 14px 0 10px; padding: 12px 14px; border-left: 3px solid var(--brand); border-radius: 4px 10px 10px 4px; background: var(--surface-2); color: var(--text-2); font-size: 13.5px; line-height: 1.5; }
.closed__preview mark { padding: 0 3px; border-radius: 4px; background: var(--brand-soft); color: var(--brand-ink); font-weight: 600; }
.closed__row { display: flex; gap: 10px; align-items: flex-end; }
.closed__row textarea { flex: 1; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); resize: none; }
.closed__row textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent); }

/* composer */
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--surface); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; max-height: 150px; padding: 11px 16px; border: 1px solid transparent; border-radius: 22px; resize: none; background: var(--surface-3); line-height: 1.38; transition: background .15s, border-color .15s; }
.composer textarea:focus { outline: none; background: var(--surface); border-color: var(--brand); }
.composer .attach { width: 44px; height: 44px; }
.send { width: 44px; height: 44px; flex: none; display: flex; align-items: center; justify-content: center; border: 0; border-radius: 50%; background: var(--brand); color: #fff; transition: background .15s, opacity .15s; }
.send:hover { background: var(--brand-hover); }
.send:disabled { opacity: .45; }
.send .ic { width: 19px; height: 19px; margin-left: -1px; }

/* dialogs / lightbox / toasts */
dialog { width: min(440px, calc(100vw - 28px)); max-height: calc(100dvh - 28px); overflow: auto; padding: 22px; border: 0; border-radius: 18px; background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg); }
dialog::backdrop { background: rgba(8, 10, 24, .55); backdrop-filter: blur(3px); }
.dlg__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.dlg__head h3 { margin: 0; font-size: 18px; letter-spacing: -.01em; }
dialog .muted { margin: 0 0 18px; }
.dlg__foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.att__preview { display: flex; align-items: center; justify-content: center; min-height: 120px; margin: 12px 0 16px; border-radius: 12px; background: var(--surface-2); overflow: hidden; }
.att__preview img { display: block; max-width: 100%; max-height: 300px; }
.att__file { display: flex; align-items: center; gap: 12px; padding: 18px; }
.att__file .ic { width: 30px; height: 30px; color: var(--brand); }
.att__file b { display: block; font-size: 14.5px; word-break: break-all; }
.att__file span { font-size: 12.5px; color: var(--muted); }
.lightbox { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 64px 16px 24px; background: rgba(5, 7, 16, .93); }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.lightbox__bar { position: absolute; top: calc(12px + env(safe-area-inset-top, 0px)); right: 14px; display: flex; gap: 8px; }
.toasts { position: fixed; z-index: 60; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; width: min(420px, calc(100vw - 28px)); pointer-events: none; }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; border-radius: 12px; background: #151a2b; color: #fff; font-size: 14px; box-shadow: var(--shadow-lg); animation: up .2s ease; }
.toast.err { background: #7a1a12; }
.toast .ic { width: 18px; height: 18px; margin-top: 1px; }
@keyframes up { from { opacity: 0; transform: translateY(8px); } }

/* ───────── phones: one pane at a time ───────── */
@media (max-width: 719px) {
  .app { display: block; position: relative; }
  .side, .chat { position: absolute; inset: 0; width: 100%; }
  .chat { transform: translateX(100%); transition: transform .24s cubic-bezier(.4,0,.2,1); z-index: 2; }
  .app.in-chat .chat { transform: none; }
  .app.in-chat .side { visibility: hidden; transition: visibility 0s .24s; }
  .chat__head .back { display: inline-flex; }
  .pill { height: 28px; padding: 0 10px; font-size: 12px; }
  .pill .full { display: none; }
  .closed { padding: 14px 16px calc(14px + env(safe-area-inset-bottom, 0px)); max-height: 64dvh; overflow-y: auto; }
  .closed__ic { display: none; }
  .closed p { font-size: 13px; }
  .closed__preview { margin: 12px 0 10px; padding: 10px 12px; font-size: 13px; }
  .closed__row textarea { height: 56px; }
  .m { max-width: 86%; }
  .msgs { padding-left: 12px; padding-right: 12px; }
  .closed__row { flex-direction: column; align-items: stretch; }
  .closed__row .btn { width: 100%; }
}

@media (max-height: 480px) and (orientation: landscape) { .chat__head { padding-top: 6px; padding-bottom: 6px; } .composer { padding-top: 6px; padding-bottom: 6px; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* avatars: a calm, name-based colour */
.avatar { background: hsl(var(--h, 235) 72% 93%); color: hsl(var(--h, 235) 52% 34%); }
@media (prefers-color-scheme: dark) { .avatar { background: hsl(var(--h, 235) 34% 22%); color: hsl(var(--h, 235) 85% 80%); } }
