:root {
  color-scheme: dark;
  --bg: #0a0a0a; --bg2: #101010; --panel: #151515; --panel2: #1d1d1d; --hover: #242424; --line: #2b2b2b;
  --text: #f2f2f2; --muted: #a0a0a0; --accent: #e5000f; --accent2: #b80000; --accent-text: #ff5c66;
  --danger: #ff5c66; --ok: #3ecf6e; --warn: #e0b341; --fs: 15px;
  --rail: 72px; --side: 256px; --members: 240px;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
}
[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f1f1; --bg2: #e8e8e8; --panel: #ffffff; --panel2: #f0f0f0; --hover: #e3e3e3; --line: #cfcfcf;
  --text: #111111; --muted: #565656; --accent: #c4000d; --accent2: #9d000a; --accent-text: #b0000b;
  --danger: #b0000b; --ok: #18813f; --warn: #8a6500;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { font: var(--fs)/1.45 system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; -webkit-text-size-adjust: 100%; }
#root { height: 100dvh; }
a { color: var(--accent-text); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 2px; }
.icon { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: square; stroke-linejoin: miter; flex: none; }
.hidden, [hidden] { display: none !important; }
.muted { color: var(--muted); }
.noscript { padding: 24px; }

/* controls */
input, select, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 2px; padding: 10px 12px; width: 100%; min-width: 0; }
textarea { resize: none; }
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 2px; padding: 9px 16px; font-weight: 600; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; color: var(--text); }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.small { min-height: 32px; padding: 4px 12px; font-size: .9em; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: none; border: 0; padding: 8px; min-width: 40px; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; color: var(--muted); border-radius: 2px; }
.icon-btn:hover { color: var(--text); background: var(--hover); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > label, .label { font-size: .8em; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; }
.error { color: var(--danger); font-size: .92em; min-height: 1.3em; margin: 4px 0; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.chip { border: 1px solid var(--line); padding: 2px 8px; font-size: .85em; background: var(--panel2); }

/* avatars: angular, never round */
.avatar { --s: 40px; --c: var(--accent); width: var(--s); height: var(--s); flex: none; background: var(--c); color: var(--fg, #fff); display: grid; place-items: center; font-weight: 700; font-size: calc(var(--s) * .42); overflow: hidden; clip-path: polygon(18% 0, 100% 0, 100% 82%, 82% 100%, 0 100%, 0 18%); user-select: none; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-wrap { position: relative; flex: none; }
.dot { position: absolute; right: -2px; bottom: -2px; width: 12px; height: 12px; background: var(--muted); border: 2px solid var(--bg2); }
.dot.online { background: var(--ok); } .dot.idle { background: var(--warn); } .dot.busy { background: var(--danger); }

/* auth */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: calc(16px + var(--sat)) 16px calc(16px + var(--sab)); background: var(--bg); }
.auth-card { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-top: 4px solid var(--accent); padding: 24px; }
.auth-logo { display: block; width: 140px; height: 140px; margin: 0 auto 8px; }
.auth h1 { text-align: center; margin: 0; font-size: 1.5em; letter-spacing: .02em; }
.tagline { text-align: center; color: var(--muted); margin: 4px 0 20px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tab { background: none; border: 0; border-bottom: 3px solid transparent; padding: 10px 14px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-family: ui-monospace, Consolas, monospace; background: var(--bg); border: 1px solid var(--line); padding: 12px; margin: 12px 0; }

/* shell */
.app { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; height: 100%; padding: var(--sat) var(--sar) 0 var(--sal); }
.nav { display: flex; min-width: 0; }
.rail { width: var(--rail); background: var(--bg); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 10px 0; overflow-y: auto; scrollbar-width: none; }
.rail-item { position: relative; width: 48px; height: 48px; border: 0; padding: 0; background: var(--panel2); color: var(--text); display: grid; place-items: center; font-weight: 700; clip-path: polygon(10px 0, 100% 0, 100% calc(100% - 10px), calc(100% - 10px) 100%, 0 100%, 0 10px); flex: none; }
.rail-item img { width: 100%; height: 100%; object-fit: cover; }
.rail-item:hover, .rail-item.active { background: var(--accent); color: #fff; }
.rail-wrap { position: relative; flex: none; }
.rail-wrap.active::before { content: ""; position: absolute; left: -12px; top: 8px; bottom: 8px; width: 4px; background: var(--accent); }
.rail-badge { position: absolute; right: -4px; bottom: -4px; min-width: 20px; padding: 0 5px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; text-align: center; border: 2px solid var(--bg); }
.rail-sep { width: 32px; height: 2px; background: var(--line); flex: none; }
.rail-logo { width: 48px; height: 48px; flex: none; }
.side { width: var(--side); background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 0 8px 0 16px; height: 52px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; font-weight: 700; flex: none; }
.side-head .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-list { flex: 1; overflow-y: auto; padding: 8px; }
.side-title { display: flex; align-items: center; justify-content: space-between; padding: 12px 8px 4px; font-size: .75em; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; }
.side-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; background: none; border: 0; border-left: 3px solid transparent; text-align: left; color: var(--muted); min-height: 40px; }
.side-item:hover { background: var(--hover); color: var(--text); }
.side-item.active { background: var(--panel2); color: var(--text); border-left-color: var(--accent); }
.side-item.unread { color: var(--text); font-weight: 700; }
.side-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: #fff; font-size: 12px; font-weight: 700; padding: 0 6px; min-width: 20px; text-align: center; }
.userbar { flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 8px calc(8px + var(--sab)) 10px; background: var(--bg); border-top: 1px solid var(--line); }
.userbar .who { flex: 1; min-width: 0; line-height: 1.2; }
.userbar .who b, .userbar .who span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.userbar .who span { font-size: .8em; color: var(--muted); }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--panel); }
.head { display: flex; align-items: center; gap: 6px; height: 52px; padding: 0 8px 0 12px; border-bottom: 1px solid var(--line); flex: none; }
.head h2 { font-size: 1em; margin: 0; white-space: nowrap; }
.head .topic { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; border-left: 1px solid var(--line); padding-left: 10px; margin-left: 4px; }
.head .nav-btn { display: none; }
.conn { background: var(--warn); color: #000; padding: 4px 12px; font-size: .85em; text-align: center; flex: none; }
.content { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.members { width: var(--members); background: var(--bg2); border-left: 1px solid var(--line); overflow-y: auto; padding-bottom: var(--sab); }
.member { display: flex; align-items: center; gap: 10px; width: 100%; padding: 6px 12px; background: none; border: 0; text-align: left; min-height: 44px; }
.member:hover { background: var(--hover); }
.member .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member.off { opacity: .5; }
.member { border-left: 4px solid var(--rc, transparent); }
.scrim { display: none; }

/* messages */
.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 0 8px; overscroll-behavior: contain; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.more { display: block; margin: 8px auto; }
.msg { display: grid; grid-template-columns: 56px minmax(0, 1fr); padding: 2px 16px 2px 8px; position: relative; }
.msg:hover { background: var(--bg2); }
.msg.first { margin-top: 12px; }
.msg .gutter { padding-left: 8px; padding-top: 2px; }
.msg .gutter .time { display: none; font-size: .7em; color: var(--muted); }
.msg.compact:hover .gutter .time { display: block; }
.msg .meta { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.msg .author { font-weight: 700; background: none; border: 0; padding: 0; color: var(--text); }
.msg .time, .msg .edited { color: var(--muted); font-size: .75em; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .ref { display: flex; gap: 6px; color: var(--muted); font-size: .85em; border-left: 3px solid var(--line); padding-left: 8px; margin-bottom: 2px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.msg .atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.msg .atts button { border: 0; padding: 0; background: none; line-height: 0; }
.msg .atts img { display: block; width: auto; height: auto; max-width: min(420px, 100%); max-height: 320px; border: 1px solid var(--line); background: var(--bg); cursor: zoom-in; }
.reacts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.react { background: var(--panel2); border: 1px solid var(--line); padding: 0 8px; min-height: 28px; display: inline-flex; align-items: center; gap: 6px; }
.react.me { border-color: var(--accent-text); background: var(--bg); }
.actions { position: absolute; right: 12px; top: -16px; display: none; background: var(--panel2); border: 1px solid var(--line); }
.msg:hover .actions, .msg.show-actions .actions, .actions:focus-within { display: flex; }
.actions .icon-btn { min-width: 36px; min-height: 32px; padding: 4px 8px; }
.typing { height: 20px; padding: 0 16px; font-size: .8em; color: var(--muted); flex: none; }
.composer { flex: none; padding: 0 12px calc(10px + var(--sab)); }
.reply-bar { display: flex; align-items: center; gap: 8px; background: var(--panel2); border: 1px solid var(--line); border-bottom: 0; padding: 4px 8px; font-size: .85em; color: var(--muted); }
.previews { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px; background: var(--panel2); border: 1px solid var(--line); border-bottom: 0; }
.preview { position: relative; width: 84px; height: 84px; background: var(--bg); border: 1px solid var(--line); }
.preview img { width: 100%; height: 100%; object-fit: cover; }
.preview.busy img { opacity: .4; }
.preview .icon-btn { position: absolute; top: 0; right: 0; background: var(--bg); min-width: 28px; min-height: 28px; padding: 2px; }
.box { display: flex; align-items: flex-end; gap: 4px; background: var(--bg); border: 1px solid var(--line); padding: 2px; }
.box textarea { border: 0; background: transparent; max-height: 40dvh; padding: 10px 8px; line-height: 1.4; }
.box textarea:focus-visible { outline: none; }
.box:focus-within { border-color: var(--accent-text); }
.dropzone { position: absolute; inset: 0; background: rgba(0, 0, 0, .7); color: #fff; display: grid; place-items: center; font-size: 1.3em; border: 3px dashed var(--accent); z-index: 5; }
.relative { position: relative; }

/* friends and lists */
.page { flex: 1; overflow-y: auto; padding: 16px; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 10px 8px; border-bottom: 1px solid var(--line); min-height: 56px; }
.list-row .grow b, .list-row .grow span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .grow span { color: var(--muted); font-size: .85em; }

/* modals, toasts */
.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .7); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { width: 100%; max-width: 520px; max-height: 90dvh; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-top: 4px solid var(--accent); }
.modal.wide { max-width: 800px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 1.1em; }
.modal-body { padding: 20px; overflow-y: auto; padding-bottom: calc(20px + var(--sab)); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.lightbox { background: #000; padding: 0; border-top-color: #000; max-width: 96vw; }
.lightbox img { display: block; max-width: 96vw; max-height: 80dvh; margin: 0 auto; object-fit: contain; }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 4px; }
.emoji-grid button { font-size: 1.5em; background: none; border: 1px solid transparent; min-height: 44px; }
.emoji-grid button:hover { background: var(--hover); border-color: var(--line); }
.profile-banner { height: 110px; background: var(--accent); background-size: cover; background-position: center; margin: -20px -20px 0; position: relative; }
.profile-banner img { width: 100%; height: 100%; object-fit: cover; }
.profile-head { display: flex; align-items: flex-end; gap: 12px; margin-top: -32px; position: relative; }
.profile-head .avatar { border: 0; }
.profile-section { border: 1px solid var(--line); background: var(--bg); padding: 10px 12px; margin-top: 12px; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.swatches input[type="color"] { width: 48px; height: 40px; padding: 2px; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 4px 12px; }
.toasts, #toasts { position: fixed; left: 0; right: 0; bottom: calc(16px + var(--sab)); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; z-index: 200; padding: 0 12px; }
.toast { pointer-events: auto; background: var(--panel2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 12px 10px 14px; max-width: 460px; display: flex; gap: 10px; align-items: center; box-shadow: 0 6px 24px rgba(0, 0, 0, .5); }
.toast.error { border-left-color: var(--danger); } .toast.ok { border-left-color: var(--ok); }
#update-bar { position: fixed; top: var(--sat); left: 0; right: 0; z-index: 300; background: var(--accent); color: #fff; display: flex; gap: 12px; justify-content: center; align-items: center; padding: 8px; }
#update-bar button { background: #fff; color: #000; border: 0; padding: 4px 14px; font-weight: 700; }

/* preferences */
[data-density="compact"] .msg { grid-template-columns: 0 minmax(0, 1fr); }
[data-density="compact"] .msg .gutter .avatar { display: none; }
[data-density="compact"] .msg.first { margin-top: 4px; }
[data-motion="off"] *, [data-motion="off"] *::before, [data-motion="off"] *::after { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }

@media (max-width: 1100px) {
  .app { grid-template-columns: auto minmax(0, 1fr); }
  .members { position: fixed; top: var(--sat); right: 0; bottom: 0; z-index: 50; transform: translateX(100%); transition: transform .2s; width: min(300px, 86vw); }
  .members.open { transform: none; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .nav { position: fixed; top: 0; bottom: 0; left: 0; z-index: 60; padding: var(--sat) 0 0 var(--sal); background: var(--bg); transform: translateX(-100%); transition: transform .2s; max-width: 94vw; }
  .nav.open { transform: none; }
  .scrim.show { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .6); z-index: 55; }
  .head .nav-btn { display: inline-flex; }
  .msg { grid-template-columns: 48px minmax(0, 1fr); padding-right: 10px; }
  .msg .actions { top: -14px; }
  .side { width: min(var(--side), calc(94vw - var(--rail))); }
  .modal-back { align-items: end; padding: 0; }
  .modal { max-width: none; max-height: 92dvh; border-top-width: 3px; }
  .page { padding: 12px; }
}

.lightbox .modal-body { padding: 0; }
.lightbox .modal-head { background: var(--panel); }
