:root {
  --bg: #0d111b;
  --panel: #141b29;
  --panel-alt: #101725;
  --line: #263147;
  --text: #edf2fb;
  --muted: #91a0b7;
  --blue: #5a91ff;
  --green: #3fd39b;
  --warn: #e5b85b;
  --danger: #ec6d78;
  --shadow: 0 18px 48px rgba(0, 0, 0, .38);
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 320px; background: var(--bg); }
button, input, textarea { font: inherit; }
button { cursor: pointer; }

.app {
  display: grid;
  grid-template: 58px minmax(0, 1fr) 52px / 248px minmax(0, 1fr) 290px;
  grid-template-areas: "top top top" "left work right" "left dock right";
  min-height: 100vh;
  overflow: hidden;
}
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 18px;
  background: #151b2a;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 760; white-space: nowrap; }
.brand-mark { display: grid; width: 22px; height: 22px; place-items: center; border-radius: 7px; background: linear-gradient(135deg, #6d74ff, #4c9bff); font-size: 12px; }
.brand small { padding-left: 8px; border-left: 1px solid var(--line); color: var(--muted); font-weight: 500; }
.stats { display: flex; gap: 8px; }
.stats div { display: grid; min-width: 70px; padding: 5px 9px; border-radius: 7px; background: #101725; line-height: 1; }
.stats b { color: var(--green); font-size: 15px; }
.stats span { margin-top: 4px; color: var(--muted); font-size: 9px; text-transform: uppercase; }
.top-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.socket { color: var(--muted); font-size: 12px; }
.socket.online { color: var(--green); }
.socket.waiting { color: var(--warn); }
.quiet, .icon-button { border: 1px solid var(--line); border-radius: 8px; background: #1a2333; color: var(--text); }
.quiet { padding: 7px 10px; }
.icon-button { width: 27px; height: 27px; }

.leftbar { grid-area: left; overflow: auto; border-right: 1px solid var(--line); background: var(--panel-alt); }
.sidebar-head { display: flex; height: 48px; align-items: center; justify-content: space-between; padding: 0 12px; color: var(--muted); font-size: 10px; letter-spacing: .06em; }
.account-row { width: 100%; padding: 11px 12px; border: 0; border-bottom: 1px solid rgba(38, 49, 71, .65); background: transparent; color: var(--text); text-align: left; }
.account-row:hover, .account-row.active { background: #182238; }
.account-row.unread { background: rgba(90, 145, 255, .08); }
.account-row.unread .row-top { color: #fff; }
.row-top { display: flex; align-items: center; gap: 7px; font-size: 13px; font-weight: 650; }
.row-bottom { display: flex; justify-content: space-between; gap: 6px; margin-top: 4px; color: var(--muted); font-size: 11px; }
.row-bottom span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-bottom time { flex: none; font-size: 9px; }
.dot { width: 7px; height: 7px; flex: none; border-radius: 50%; background: var(--muted); }
.dot.online { background: var(--green); box-shadow: 0 0 0 3px rgba(63, 211, 155, .1); }
.badge { margin-left: auto; padding: 1px 6px; border-radius: 8px; background: #24344f; color: #cfe0ff; font-size: 10px; }
.sidebar-foot { padding: 13px; color: var(--muted); font-size: 11px; line-height: 1.45; }

.workspace { position: relative; grid-area: work; min-height: 0; overflow: auto; background: radial-gradient(ellipse at 50% 0, #111a2c 0, var(--bg) 55%); }
.empty-state { position: absolute; inset: 0; display: grid; place-content: center; gap: 8px; padding: 25px; color: var(--muted); text-align: center; }
.empty-state b { color: var(--text); font-size: 16px; }
.account-card { position: absolute; display: flex; width: 390px; height: 490px; min-width: 290px; min-height: 250px; flex-direction: column; overflow: hidden; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: var(--shadow); }
.account-card::before { display: block; height: 3px; flex: none; background: var(--card-color, var(--blue)); content: ""; }
.account-card.pinned { border-color: var(--card-color, var(--blue)); }
.account-card.has-unread { box-shadow: 0 0 0 1px rgba(90, 145, 255, .45), var(--shadow); }
.account-card.full { position: fixed !important; z-index: 100 !important; top: 72px !important; left: 270px !important; width: calc(100vw - 590px) !important; height: calc(100vh - 140px) !important; }
.card-head { display: flex; height: 48px; flex: none; align-items: center; gap: 8px; padding: 0 10px; border-bottom: 1px solid var(--line); cursor: grab; }
.card-head:active { cursor: grabbing; }
.account-avatar { display: grid; width: 25px; height: 25px; place-items: center; border-radius: 50%; background: var(--card-color, var(--blue)); font-size: 11px; font-weight: 800; }
.account-avatar.has-photo { background-position: center; background-repeat: no-repeat; background-size: cover; color: transparent; }
.card-title { display: grid; min-width: 0; gap: 1px; }
.card-title strong { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.card-title span, .card-count { color: var(--muted); font-size: 10px; }
.card-count { margin-left: auto; font-size: 11px; }
.card-tools { display: flex; gap: 4px; }
.card-tools button { width: 24px; height: 24px; padding: 0; border: 0; border-radius: 6px; background: transparent; color: var(--muted); }
.card-tools button:hover, .card-tools button.active { background: #27344d; color: var(--text); }
.card-body { display: grid; min-height: 0; flex: 1; grid-template-columns: var(--chat-list-width, 150px) 6px minmax(0, 1fr); grid-template-areas: "chats splitter thread"; }
.conversation-list { grid-area: chats; overflow: auto; border-right: 1px solid var(--line); background: #111827; }
.splitter { grid-area: splitter; cursor: col-resize; background: #172238; }
.splitter:hover { background: var(--blue); }
.conversation { width: 100%; padding: 9px; border: 0; border-bottom: 1px solid rgba(38, 49, 71, .8); background: transparent; color: var(--text); text-align: left; }
.conversation:hover, .conversation.selected { background: #1c2941; }
.conversation.unread { background: rgba(90, 145, 255, .07); }
.conversation { display: flex; gap: 8px; }
.conversation-avatar { display: grid; width: 26px; height: 26px; flex: none; place-items: center; border-radius: 50%; background: #2a405f; color: #dce9ff; font-size: 9px; font-weight: 800; }
.conversation-avatar.has-photo { background-position: center; background-repeat: no-repeat; background-size: cover; color: transparent; }
.conversation-copy { display: grid; min-width: 0; flex: 1; }
.conversation-name { display: flex; min-width: 0; align-items: center; gap: 5px; font-size: 12px; font-weight: 650; }
.conversation-name > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-name time { margin-left: auto; color: #71839d; font-size: 8px; font-weight: 500; white-space: nowrap; }
.conversation-meta { overflow: hidden; margin-top: 1px; color: #7f91aa; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.conversation-preview { display: block; overflow: hidden; margin-top: 3px; color: var(--muted); font-size: 10px; line-height: 1.25; text-overflow: ellipsis; white-space: nowrap; }
.thread { display: flex; min-width: 0; min-height: 0; flex-direction: column; overflow: hidden; grid-area: thread; }
.thread-empty { padding: 16px; color: var(--muted); font-size: 12px; line-height: 1.4; }
.messages { position: relative; display: flex; min-height: 0; flex: 1 1 0; flex-direction: column; gap: 7px; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; padding: 10px; background: #121927; }
.history-loader { position: sticky; z-index: 2; top: 0; align-self: center; padding: 4px 8px; border-radius: 10px; background: #24324a; color: var(--muted); font-size: 9px; }
.message { position: relative; max-width: 86%; padding: 7px 9px; border-radius: 10px; background: #222d42; font-size: 12px; line-height: 1.4; white-space: pre-wrap; word-break: break-word; }
.message.outbound { align-self: flex-end; background: #315fae; }
.message.unknown, .message.failed { outline: 1px solid var(--danger); }
.message-foot { display: flex; align-items: center; justify-content: flex-end; gap: 5px; margin-top: 4px; }
.message .meta { display: block; color: #b9c8e1; font-size: 9px; }
.message.inbound .meta { color: var(--muted); }
.message-forward { width: 21px; height: 21px; padding: 0; border: 0; border-radius: 6px; background: rgba(255,255,255,.08); color: #d8e4ff; font-size: 12px; opacity: 0; transition: opacity .15s ease; }
.message:hover .message-forward, .message-forward:focus-visible { opacity: 1; }
.media-note { display: grid; gap: 2px; color: #d8e4ff; font-size: 10px; }
.media-note small { color: var(--muted); font-size: 8px; }
.media-preview { display: block; overflow: hidden; margin: -2px -3px 5px; border-radius: 8px; background: #0d1420; }
.media-preview img { display: block; width: min(100%, 360px); max-height: 300px; object-fit: contain; }
.media-preview.sticker { width: min(180px, 100%); background: transparent; }
.media-preview.sticker img { max-height: 180px; }
video.media-preview.sticker { max-height: 180px; object-fit: contain; }
.media-player { display: block; width: min(100%, 360px); max-height: 300px; margin-bottom: 5px; border-radius: 8px; background: #090d15; }
.media-audio { display: block; width: min(100%, 320px); margin-bottom: 5px; }
.media-file { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; padding: 7px; border-radius: 8px; background: rgba(0,0,0,.18); color: #e5eeff; text-decoration: none; }
.media-file > span:first-child { font-size: 20px; }
.media-file.sticker-file > span:first-child { font-size: 34px; }
.media-file span:last-child { display: grid; min-width: 0; }
.media-file b { overflow: hidden; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.media-file small, .forwarded-label { color: #b7c7df; font-size: 9px; }
.forwarded-label { margin-bottom: 4px; font-style: italic; }
.compose { display: flex; flex: none; align-items: flex-end; gap: 6px; padding: 8px; border-top: 1px solid var(--line); background: var(--panel); }
.compose-main { display: grid; min-width: 0; flex: 1; gap: 4px; }
.compose textarea { min-width: 0; min-height: 34px; max-height: 90px; flex: 1; resize: none; border: 1px solid #34425c; border-radius: 8px; background: #0f1623; color: var(--text); padding: 7px; }
.compose button { height: 34px; padding: 0 10px; border: 0; border-radius: 8px; background: var(--blue); color: #fff; }
.compose .attach { width: 34px; padding: 0; border: 1px solid #34425c; background: #182238; font-size: 20px; }
.compose .attach.active { border-color: var(--green); color: var(--green); }
.compose button:disabled { opacity: .55; }
.compose-error { align-self: center; color: var(--danger); font-size: 10px; }
.file-choice { overflow: hidden; padding: 3px 6px; border-radius: 6px; background: #1c2941; color: #cfe0ff; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.conversation-menu { position: fixed; z-index: 1000; min-width: 160px; padding: 5px; border: 1px solid #3a4962; border-radius: 9px; background: #171f2e; box-shadow: var(--shadow); }
.conversation-menu button { width: 100%; padding: 8px 10px; border: 0; border-radius: 6px; background: transparent; color: #ff9ca5; text-align: left; }
.conversation-menu button:hover { background: #3b2230; }
.forward-picker { position: fixed; z-index: 1100; display: grid; width: 220px; max-height: 250px; gap: 3px; overflow: auto; padding: 7px; border: 1px solid #3a4962; border-radius: 10px; background: #171f2e; box-shadow: var(--shadow); }
.forward-picker strong { padding: 5px 7px; color: var(--muted); font-size: 10px; }
.forward-picker button { overflow: hidden; padding: 7px; border: 0; border-radius: 6px; background: transparent; color: var(--text); text-align: left; text-overflow: ellipsis; white-space: nowrap; }
.forward-picker button:hover { background: #27344d; }
.resize-handle { position: absolute; z-index: 8; display: block; }
.resize-handle.n, .resize-handle.s { right: 10px; left: 10px; height: 8px; cursor: ns-resize; }
.resize-handle.n { top: -4px; }.resize-handle.s { bottom: -4px; }
.resize-handle.e, .resize-handle.w { top: 10px; bottom: 10px; width: 8px; cursor: ew-resize; }
.resize-handle.e { right: -4px; }.resize-handle.w { left: -4px; }
.resize-handle.ne, .resize-handle.nw, .resize-handle.se, .resize-handle.sw { width: 14px; height: 14px; }
.resize-handle.ne { top: -5px; right: -5px; cursor: nesw-resize; }.resize-handle.nw { top: -5px; left: -5px; cursor: nwse-resize; }.resize-handle.se { right: -5px; bottom: -5px; cursor: nwse-resize; }.resize-handle.sw { bottom: -5px; left: -5px; cursor: nesw-resize; }

.rightbar { grid-area: right; overflow: auto; border-left: 1px solid var(--line); background: var(--panel-alt); }
.context-empty { padding: 18px; color: var(--muted); font-size: 12px; line-height: 1.4; }
.context { padding: 14px; }
.context h2 { margin: 0 0 4px; font-size: 16px; }
.context small, .kv span:first-child { color: var(--muted); }
.kv { display: grid; gap: 9px; margin-top: 18px; font-size: 12px; }
.kv div { display: flex; justify-content: space-between; gap: 10px; }
.dock { display: flex; grid-area: dock; align-items: center; gap: 10px; overflow: auto; padding: 0 12px; border-top: 1px solid var(--line); background: #111827; }
.dock > span { color: var(--muted); font-size: 10px; letter-spacing: .06em; }
#dockItems { display: flex; gap: 7px; }
.dock-item { padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: #1c273a; color: var(--text); font-size: 11px; white-space: nowrap; }

.login { display: grid; min-height: 100vh; place-items: center; background: radial-gradient(circle at 50% 0, #1c2b49 0, var(--bg) 45%); }
.login-card { display: grid; width: min(390px, calc(100vw - 32px)); gap: 16px; padding: 26px; border: 1px solid var(--line); border-radius: 15px; background: var(--panel); box-shadow: var(--shadow); }
.login-card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.login-card label { display: grid; gap: 7px; color: var(--muted); font-size: 12px; }
.login-card input { padding: 10px; border: 1px solid #34425c; border-radius: 9px; background: #0f1623; color: var(--text); }
.login-card button { padding: 10px; border: 0; border-radius: 9px; background: var(--blue); color: #fff; }
.form-error { min-height: 16px; color: var(--danger); font-size: 12px; }

@media (max-width: 1050px) {
  .app { grid-template-columns: 210px minmax(0, 1fr); grid-template-areas: "top top" "left work" "left dock"; }
  .rightbar { display: none; }
  .account-card.full { left: 228px !important; width: calc(100vw - 246px) !important; }
}
@media (max-width: 720px) {
  .app { grid-template: 58px minmax(0, 1fr) 52px / 1fr; grid-template-areas: "top" "work" "dock"; }
  .leftbar { display: none; }
  .topbar { gap: 8px; padding: 0 10px; }
  .stats div { min-width: auto; padding: 4px 6px; }
  .stats div:nth-child(2), .stats div:nth-child(4), .brand small, .quiet { display: none; }
  .account-card { top: 12px !important; left: 12px !important; width: calc(100vw - 24px) !important; height: calc(100vh - 128px) !important; resize: none; }
  .account-card.full { position: absolute !important; top: 12px !important; left: 12px !important; width: calc(100vw - 24px) !important; height: calc(100vh - 128px) !important; }
  .card-body { grid-template-columns: minmax(110px, 38%) 6px minmax(0, 1fr); }
}
