:root {
  --bg0: #010409;
  --bg1: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --line: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #5865f2;
  --accent-hi: #6f7bf7;
  --green: #3fb950;
  --red: #f85149;
  --bar: 52px;
  --bg-side: #0a0e14;
  --rail: 72px;
  --ease: cubic-bezier(.65, .05, .2, 1);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg1);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent-hi); outline-offset: 2px; }
::selection { background: rgba(88, 101, 242, .45); }
h1 { margin: 0; font-size: 24px; letter-spacing: -.01em; }

/* ---------- buttons & inputs ---------- */
.primary {
  background: var(--accent); color: #fff; font-weight: 600;
  border-radius: 6px; padding: 8px 16px; min-height: 38px;
  transition: background .15s, transform .1s, opacity .15s;
}
.primary:hover:not(:disabled) { background: var(--accent-hi); }
.primary:active:not(:disabled) { transform: scale(.98); }
.primary:disabled { opacity: .45; cursor: default; }
.primary.busy { opacity: .7; cursor: progress; }
.ghost-btn { white-space: nowrap; color: var(--muted); padding: 6px 10px; border-radius: 6px; transition: color .15s, background .15s; }
.ghost-btn:hover { color: var(--text); background: var(--bg2); }
.icon-btn { display: none; width: 36px; height: 36px; border-radius: 6px; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--bg2); color: var(--text); }

.field { display: block; margin-bottom: 16px; }
.field > span { display: block; margin-bottom: 6px; font-weight: 600; }
.field small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }
.field input {
  width: 100%; min-height: 38px; padding: 6px 12px;
  background: var(--bg0); border: 1px solid var(--line); border-radius: 6px;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(88, 101, 242, .3); }
.field input.invalid { border-color: var(--red); }
.form-error { min-height: 20px; margin: 0 0 12px; color: var(--red); font-size: 13px; }

/* ---------- auth ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px; overflow: auto; }
.auth-box { width: min(360px, 100%); }
.auth-brand { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-bottom: 28px; }
.auth-brand img { border-radius: 10px; }
.auth-switch { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); margin-bottom: 24px; }
.auth-switch button {
  padding: 10px 0; color: var(--muted); font-weight: 600; position: relative; transition: color .2s;
}
.auth-switch button::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
  background: var(--accent); transform: scaleX(0); transition: transform .25s var(--ease);
}
.auth-switch button.active { color: var(--text); }
.auth-switch button.active::after { transform: scaleX(1); }
.auth form .primary { width: 100%; }

/* ---------- top bar ---------- */
#app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  height: var(--bar); flex: none; padding: 0 16px;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  background: var(--bg0); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -.01em; }
.brand img { border-radius: 7px; }

.tabs { display: flex; gap: 8px; }
.pill {
  height: 30px; min-width: 116px; padding: 0 18px; border-radius: 999px;
  background: var(--bg2); color: var(--muted); font-size: 13px; font-weight: 600; line-height: 1; white-space: nowrap;
  border: 1px solid var(--line);
  transition: background .2s, color .2s, border-color .2s, transform .15s;
}
.pill:hover { color: var(--text); border-color: #484f58; }
.pill:active { transform: scale(.97); }
.pill.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.pill.sm { height: 28px; min-width: 0; padding: 0 14px; }

/* ---------- sliding panes ---------- */
.viewport { flex: 1; min-height: 0; overflow: hidden; }
.track {
  display: flex; width: 200%; height: 100%;
  transition: transform .5s var(--ease);
  will-change: transform;
}
.track[data-tab="repositories"] { transform: translateX(-50%); }
.pane { position: relative; flex: none; width: 50%; height: 100%; display: flex; min-width: 0; overflow: hidden; }

.empty-space {
  margin: auto; text-align: center; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 24px;
}
.empty-space svg { opacity: .55; }
.empty-space p { margin: 0; font-size: 15px; }

/* ---------- avatars ---------- */
.avatar {
  position: relative; flex: none; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 16px; color: #fff; user-select: none;
}
.avatar.sm { width: 30px; height: 30px; font-size: 13px; }
.avatar.xs { width: 26px; height: 26px; font-size: 12px; }
.dot {
  position: absolute; right: -2px; bottom: -2px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--muted); border: 2px solid var(--bg-side); transition: background .25s;
}
.dot.on { background: var(--green); }

/* ---------- server rail ---------- */
.drawer { flex: none; display: flex; min-height: 0; }
.rail {
  flex: none; width: var(--rail); display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 12px 0; background: var(--bg0); overflow-y: auto; overflow-x: hidden; scrollbar-width: none;
}
.rail::-webkit-scrollbar { display: none; }
.rail-list { list-style: none; margin: 0; padding: 0; width: 100%; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.rail-list li, .rail-home-wrap { position: relative; width: 100%; display: flex; justify-content: center; }
.rail-sep { width: 32px; height: 2px; border-radius: 1px; background: var(--line); flex: none; }
.rail-btn {
  position: relative; flex: none; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  background: var(--bg2); color: var(--text); font-weight: 700; font-size: 15px; overflow: visible;
  transition: border-radius .2s var(--ease), background .2s, color .2s;
}
.rail-btn img { width: 100%; height: 100%; border-radius: inherit; display: block; }
.rail-btn[data-tint] { background: var(--tint); color: #fff; }
.rail-btn:hover, .rail-btn.active { border-radius: 16px; }
.rail-btn.active, .rail-home.active { background: var(--accent); color: #fff; }
.rail-home { color: var(--muted); }
.rail-home:hover { background: var(--accent); color: #fff; }
.rail-add { color: var(--green); }
.rail-add:hover { background: var(--green); color: #fff; }
.rail-item::before, .rail-home-wrap::before {
  content: ""; position: absolute; left: 0; top: 50%; width: 4px; height: 8px; border-radius: 0 4px 4px 0;
  background: var(--text); transform: translateY(-50%) scaleY(0); transition: height .2s var(--ease), transform .2s var(--ease);
}
.rail-item.unread::before, .rail-home-wrap.unread::before { transform: translateY(-50%) scaleY(1); }
.rail-item:hover::before, .rail-home-wrap:hover:not(.active)::before { height: 20px; transform: translateY(-50%) scaleY(1); }
.rail-item.active::before, .rail-home-wrap.active::before { height: 36px; transform: translateY(-50%) scaleY(1); }
.rail-badge {
  position: absolute; right: -4px; bottom: -4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--red); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; border: 3px solid var(--bg0); box-sizing: content-box;
}

/* ---------- channel column ---------- */
.sidebar { position: relative; flex: none; width: 240px; display: flex; flex-direction: column; background: var(--bg-side); border-right: 1px solid var(--line); border-top-left-radius: 10px; }
.server-head {
  height: 48px; flex: none; width: 100%; padding: 0 16px; display: flex; align-items: center; gap: 8px; text-align: left;
  border-bottom: 1px solid var(--line); border-top-left-radius: 10px; transition: background .15s;
}
button.server-head:hover, .server-head[aria-expanded="true"] { background: var(--bg2); }
.server-head.static { cursor: default; }
.server-name { flex: 1; min-width: 0; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { flex: none; color: var(--muted); transition: transform .2s var(--ease); }
.server-head[aria-expanded="true"] .chev { transform: rotate(180deg); }
.server-head.static .chev { display: none; }

.menu {
  position: absolute; z-index: 30; top: 54px; left: 8px; right: 8px; padding: 6px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(1, 4, 9, .6);
}
.menu button {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px; text-align: left;
  padding: 7px 8px; border-radius: 5px; color: var(--text); font-size: 13px; font-weight: 500;
}
.menu button:hover, .menu button:focus-visible { background: var(--accent); color: #fff; outline: none; }
.menu button.danger { color: var(--red); }
.menu button.danger:hover, .menu button.danger:focus-visible { background: var(--red); color: #fff; }
.menu hr { border: 0; height: 1px; background: var(--line); margin: 5px 4px; }

.side-scroll { flex: 1; overflow-y: auto; padding: 8px 8px 16px; }
.side-label { padding: 14px 8px 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.cat { display: flex; align-items: center; padding: 14px 4px 4px 0; }
.cat-toggle {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; text-align: left; padding: 0 4px;
  font-size: 12px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; color: var(--muted); transition: color .15s;
}
.cat-toggle:hover { color: var(--text); }
.cat-toggle svg { flex: none; transition: transform .15s; }
.cat.closed .cat-toggle svg { transform: rotate(-90deg); }
.cat-add { width: 20px; height: 20px; display: grid; place-items: center; border-radius: 4px; color: var(--muted); }
.cat-add:hover { color: var(--text); background: var(--bg3); }
.find { width: 100%; min-height: 30px; margin: 4px 0 2px; padding: 4px 10px; background: var(--bg0); border: 1px solid var(--line); border-radius: 6px; font-size: 13px; }
.find:focus { outline: none; border-color: var(--accent); }
.nav { list-style: none; margin: 0; padding: 0; }
.nav .note { padding: 6px 8px; color: var(--muted); font-size: 13px; }
.nav-item {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  padding: 6px 8px; min-height: 34px; border-radius: 6px; color: var(--muted);
  transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--bg2); color: var(--text); }
.nav-item.active { background: var(--bg3); color: var(--text); }
.nav-item.unread { color: var(--text); font-weight: 600; }
.nav-item .hash { width: 20px; flex: none; text-align: center; font-size: 18px; color: var(--muted); }
.nav-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--red); color: #fff; font-size: 12px; font-weight: 700; line-height: 20px; text-align: center;
}

.userpanel { flex: none; height: 54px; display: flex; align-items: center; gap: 8px; padding: 0 8px 0 10px; background: var(--bg0); border-top: 1px solid var(--line); }
.up-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.up-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up-status { font-size: 11px; color: var(--muted); }
.icon-action { width: 32px; height: 32px; flex: none; display: grid; place-items: center; border-radius: 6px; color: var(--muted); transition: color .15s, background .15s; }
.icon-action:hover { color: var(--text); background: var(--bg2); }

/* ---------- chat ---------- */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg1); }
.chat-head { height: 48px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.chat-title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 12px; font-weight: 700; white-space: nowrap; }
.chat-title > span:first-child { flex: none; }
.chat-sub { min-width: 0; overflow: hidden; text-overflow: ellipsis; font-weight: 400; color: var(--muted); font-size: 13px; }
.conn { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; flex: none; }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.conn.online i { background: var(--green); }
.conn.offline i { background: var(--red); }
.conn.online span { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 0 8px; overscroll-behavior: contain; }
.hint { text-align: center; color: var(--muted); padding: 24px 16px; }
.intro { padding: 24px 16px 12px; margin: 0 16px 8px; border-bottom: 1px solid var(--line); }
.intro h2 { margin: 0 0 4px; font-size: 22px; }
.intro p { margin: 0; color: var(--muted); }
.day { display: flex; align-items: center; gap: 12px; margin: 14px 16px 6px; color: var(--muted); font-size: 12px; font-weight: 600; }
.day::before, .day::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.msg { position: relative; display: grid; grid-template-columns: 40px minmax(0, 1fr); gap: 14px; padding: 2px 16px 2px 16px; margin-top: 0; animation: pop .18s ease-out; }
.msg:not(.grouped) { margin-top: 12px; padding-top: 4px; }
.msg:hover, .msg:focus-within { background: rgba(255, 255, 255, .025); }
.msg .gutter { display: flex; justify-content: center; }
.msg .hover-time { font-size: 11px; color: var(--muted); opacity: 0; line-height: 22px; transition: opacity .15s; }
.msg:hover .hover-time { opacity: 1; }
.msg-head { display: flex; align-items: baseline; gap: 8px; }
.author { font-weight: 600; }
.time { color: var(--muted); font-size: 12px; }
.text { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; }
.edited { color: var(--muted); font-size: 11px; }
.actions {
  position: absolute; right: 16px; top: -12px; display: flex; gap: 2px; padding: 2px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 6px;
  opacity: 0; transform: translateY(2px); transition: opacity .15s, transform .15s;
}
.msg:hover .actions, .msg:focus-within .actions { opacity: 1; transform: none; }
.actions button { padding: 2px 8px; border-radius: 4px; color: var(--muted); font-size: 12px; }
.actions button:hover { background: var(--bg3); color: var(--text); }
.actions button.danger:hover { color: var(--red); }
.editor textarea {
  width: 100%; resize: none; padding: 8px 12px; background: var(--bg0);
  border: 1px solid var(--accent); border-radius: 6px; max-height: 200px;
}
.editor textarea:focus { outline: none; box-shadow: 0 0 0 3px rgba(88, 101, 242, .3); }
.editor small { color: var(--muted); font-size: 12px; }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.composer { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); }
.composer textarea {
  flex: 1; min-height: 42px; max-height: 160px; resize: none; padding: 9px 14px;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; line-height: 22px;
  transition: border-color .15s, box-shadow .15s;
}
.composer textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(88, 101, 242, .25); }
.composer .send { height: 42px; border-radius: 8px; }

/* ---------- dialogs ---------- */
.secondary-btn { background: var(--bg3); color: var(--text); font-weight: 600; border-radius: 6px; padding: 8px 16px; min-height: 38px; transition: background .15s; }
.secondary-btn:hover { background: #2b323b; }
.danger-btn { background: var(--red); color: #fff; font-weight: 600; border-radius: 6px; padding: 8px 16px; min-height: 38px; transition: opacity .15s; }
.danger-btn:hover:not(:disabled) { opacity: .88; }
.danger-btn:disabled { opacity: .4; cursor: default; }
.dlg { width: min(460px, 94vw); max-height: 88dvh; padding: 0; background: var(--bg1); color: var(--text); border: 1px solid var(--line); border-radius: 10px; margin: auto; overflow: hidden; }
.dlg[open] { display: flex; flex-direction: column; animation: dlg-in .18s var(--ease); }
.dlg.wide { width: min(640px, 94vw); height: min(620px, 88dvh); }
.dlg::backdrop { background: rgba(1, 4, 9, .72); }
@keyframes dlg-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
.dlg-head { flex: none; display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px 20px; }
.dlg-head h2 { margin: 0; font-size: 18px; }
.seg { flex: none; display: flex; gap: 6px; padding: 0 20px 12px; border-bottom: 1px solid var(--line); }
.dlg-body { padding: 16px 20px 20px; }
.dlg-body.scroll { flex: 1; min-height: 0; overflow-y: auto; }
.dlg-note { margin: 0 0 16px; color: var(--muted); }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.dlg-foot.split { justify-content: space-between; margin-top: 16px; }
.field em { font-weight: 400; font-style: normal; color: var(--muted); }
.copy-row { display: flex; gap: 8px; }
.copy-row input { flex: 1; min-width: 0; min-height: 38px; padding: 6px 12px; background: var(--bg0); border: 1px solid var(--line); border-radius: 6px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.copy-row input:focus { outline: none; border-color: var(--accent); }

.set-h { margin: 0 0 8px; font-size: 12px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; color: var(--muted); }
.set-block + .set-block { margin-top: 24px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; min-height: 38px; padding: 6px 12px; background: var(--bg0); border: 1px solid var(--line); border-radius: 6px; }
.inline-form input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(88, 101, 242, .3); }
.rows { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 8px; }
.rows li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; min-height: 46px; }
.rows li + li { border-top: 1px solid var(--line); }
.rows .grow { flex: 1; min-width: 0; }
.rows .r-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .r-sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .r-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 0 8px; }
.mini { padding: 4px 10px; border-radius: 6px; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--bg3); }
.mini:hover { color: var(--text); }
.mini.danger:hover { background: var(--red); color: #fff; }
.danger-zone { border: 1px solid rgba(248, 81, 73, .45); border-radius: 8px; padding: 14px; }
.danger-zone p { margin: 0 0 10px; color: var(--muted); }
.danger-zone .inline-form { margin-top: 8px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 50; max-width: min(420px, 92vw);
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 10px 16px;
  transition: opacity .2s, transform .25s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- small screens ---------- */
.scrim { display: none; }
@media (max-width: 760px) {
  .topbar { grid-template-columns: auto 1fr auto; padding: 0 10px; gap: 8px; }
  .brand span { display: none; }
  .tabs { justify-content: center; gap: 6px; }
  .pill { min-width: 0; padding: 0 14px; }
  .icon-btn { display: grid; }
  .drawer {
    position: absolute; z-index: 20; inset: 0 auto 0 0; width: min(330px, 90%);
    background: var(--bg-side); transform: translateX(-102%); transition: transform .3s var(--ease);
  }
  .sidebar { flex: 1; width: auto; border-top-left-radius: 0; }
  .server-head { border-top-left-radius: 0; }
  .scrim { display: block; position: absolute; inset: 0; z-index: 15; background: rgba(1, 4, 9, .6); opacity: 0; pointer-events: none; transition: opacity .3s; }
  .pane.sidebar-open .drawer { transform: none; }
  .pane.sidebar-open .scrim { opacity: 1; pointer-events: auto; }
  .msg { grid-template-columns: 36px minmax(0, 1fr); gap: 10px; padding: 2px 12px; }
  .avatar { width: 36px; height: 36px; }
  .avatar.sm { width: 32px; height: 32px; }
  .chat-sub { display: none; }
  .composer { padding: 8px 12px calc(12px + env(safe-area-inset-bottom)); }
  .actions { opacity: 1; transform: none; right: 8px; }
  .dlg.wide { height: 88dvh; }
}
@media (hover: none) { .actions { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
