/* Day / night: light by default, dark via the toggle or the system setting. */
:root {
  color-scheme: light;
  --bg: #f4f5f7; --panel: #ffffff; --side: #f8f9fb; --text: #1c1e21; --muted: #6b7280;
  --line: #e3e5e8; --accent: #4f46e5; --accent-text: #ffffff; --mine: #eef0ff;
  --bubble: #f1f2f4; --chip: #f3f4f6; --chip-on: #e0e7ff; --name-l: 40%;
  --shadow: 0 6px 24px rgba(0, 0, 0, .18);
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0f13; --panel: #1b1e25; --side: #15181e; --text: #e6e8ec; --muted: #8b93a1;
  --line: #2a2e38; --accent: #7c75ff; --accent-text: #0e0f14; --mine: #262a45;
  --bubble: #252932; --chip: #2a2e38; --chip-on: #34385c; --name-l: 70%;
  --shadow: 0 6px 24px rgba(0, 0, 0, .6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0f13; --panel: #1b1e25; --side: #15181e; --text: #e6e8ec; --muted: #8b93a1;
    --line: #2a2e38; --accent: #7c75ff; --accent-text: #0e0f14; --mine: #262a45;
    --bubble: #252932; --chip: #2a2e38; --chip-on: #34385c; --name-l: 70%;
    --shadow: 0 6px 24px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

.shell {
  display: flex; height: 100%; height: 100dvh; max-width: 1240px; margin: 0 auto;
  background: var(--panel); border-left: 1px solid var(--line); border-right: 1px solid var(--line);
}

/* ---------- sidebar ---------- */
#sidebar {
  width: 230px; flex: none; display: flex; flex-direction: column;
  background: var(--side); border-right: 1px solid var(--line);
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 16px; }
.side-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
#roomList { flex: 1; overflow-y: auto; padding: 0 8px; display: flex; flex-direction: column; gap: 2px; }
.room {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 8px 10px; border: 0; border-radius: 8px; background: none; cursor: pointer;
}
.room:hover { background: var(--chip); }
.room.active { background: var(--chip-on); font-weight: 600; }
.room .rname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room .lock { font-size: 12px; }
#roomList { flex: 1 1 0; min-height: 60px; }
.dm-head { padding-top: 14px; border-top: 1px solid var(--line); }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 7px; font-size: 11px; letter-spacing: 0; }
#dmList { flex: 0 1 auto; max-height: 40%; overflow-y: auto; padding: 0 8px 8px; display: flex; flex-direction: column; gap: 2px; }
.dm-empty { padding: 4px 10px; font-size: 13px; color: var(--muted); }
.dm-row { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 8px; font-size: 15px; }
.dm-row .rname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dm-row .sub { font-size: 12px; color: var(--muted); }
.dm-row .primary.small, .dm-row .ghost.small { padding: 3px 10px; font-size: 13px; }
.meta .name.clickable { cursor: pointer; }
.meta .name.clickable:hover { text-decoration: underline; }
.head-btns { display: flex; gap: 2px; }
#guestBar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 6px 16px; font-size: 13px; color: var(--text); background: var(--chip-on); border-bottom: 1px solid var(--line);
}
#guestBar .link { color: var(--accent); font-weight: 600; }
.choice { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.choice label, .check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.choice input, .check input { width: auto; margin: 0; accent-color: var(--accent); }
.code-big {
  text-align: center; font: 700 28px/1.2 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  letter-spacing: .12em; padding: 14px 8px; border: 1px dashed var(--line); border-radius: 10px; user-select: all;
}
dialog.wide { max-width: 560px; }
dialog h3 { margin: 6px 0 0; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.admin-list { display: flex; flex-direction: column; gap: 2px; max-height: 26vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; padding: 4px 8px; }
.admin-row { display: flex; align-items: center; gap: 6px; padding: 5px 0; font-size: 14px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.admin-row:last-child { border-bottom: 0; }
.admin-row .rname { flex: 1 1 120px; min-width: 0; overflow-wrap: anywhere; }
.admin-row .sub { font-size: 12px; color: var(--muted); }
.tag { font-size: 11px; padding: 0 6px; border-radius: 999px; background: var(--chip); color: var(--muted); border: 1px solid var(--line); }
.tag.bad { color: #ef4444; border-color: #ef4444; }
.admin-row button { padding: 2px 10px; font-size: 13px; }
#tip {
  position: fixed; z-index: 40; max-width: min(260px, 90vw); padding: 8px 10px; font-size: 13px; line-height: 1.35;
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); pointer-events: none;
}
#tip .big { font-size: 20px; line-height: 1.1; margin-right: 6px; vertical-align: middle; }
#tip .who { overflow-wrap: anywhere; }
.blocked-list { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; }
.blocked-list .dm-row { padding: 4px 0; }
#userForm .link { align-self: center; }
.side-foot { padding: 10px 12px; border-top: 1px solid var(--line); display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.side-foot .link { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- header ---------- */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
header { display: flex; align-items: center; gap: 10px; padding: 10px 12px 10px 16px; border-bottom: 1px solid var(--line); }
header h1 { font-size: 17px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
.dot.on { background: #22c55e; }
.dot.err { background: #ef4444; }
.icon {
  background: none; border: 0; cursor: pointer; padding: 4px 8px; border-radius: 8px;
  font-size: 18px; line-height: 1; color: var(--muted);
}
.icon:hover { background: var(--chip); color: var(--text); }
#menuBtn { display: none; }
button.link { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 13px; padding: 4px; }
button.link:hover { color: var(--text); text-decoration: underline; }
#online { padding: 6px 16px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); min-height: 28px; }

/* ---------- messages ---------- */
#messages { flex: 1; overflow-y: auto; padding: 18px 16px 12px; display: flex; flex-direction: column; gap: 10px; }
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 30ch; }
.msg {
  position: relative; max-width: 85%; padding: 8px 12px; border-radius: 12px;
  background: var(--bubble); align-self: flex-start;
}
.msg.mine { align-self: flex-end; background: var(--mine); }
.meta { font-size: 12px; margin-bottom: 2px; display: flex; gap: 8px; align-items: baseline; }
.meta .name { font-weight: 600; }
.meta .time { color: var(--muted); }
.meta .pin { color: #f59e0b; }
.meta .edited { color: var(--muted); font-style: italic; }
.edit { min-width: min(60vw, 320px); }
.edit-box { width: 100%; resize: vertical; max-height: 40vh; }
.edit-row { display: flex; justify-content: flex-end; gap: 6px; margin-top: 6px; }
.primary.small, .ghost.small { padding: 5px 14px; font-size: 14px; }
.body { white-space: pre-wrap; overflow-wrap: anywhere; }
.quote {
  border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0 4px; font-size: 13px;
  color: var(--muted); background: rgba(127, 127, 127, .12); border-radius: 4px; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.quote b { color: var(--text); }
.flash { animation: flash 1.4s; }
@keyframes flash { 0% { outline: 2px solid var(--accent); } 100% { outline: 2px solid transparent; } }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; font-size: 13px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--chip); cursor: pointer;
}
.chip.on { background: var(--chip-on); border-color: var(--accent); }

.actions {
  position: absolute; top: -14px; right: 8px; z-index: 2; display: flex; gap: 2px; padding: 1px 3px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity .12s;
}
/* Anchor the bar to the bubble's outer-facing edge so it grows toward the empty side, never off-screen. */
.msg .actions { right: auto; left: 8px; }
.msg.mine .actions { left: auto; right: 8px; }
.msg:hover .actions, .msg:focus-within .actions, .msg.show-actions .actions { opacity: 1; pointer-events: auto; }
.actions button { background: none; border: 0; cursor: pointer; padding: 2px 6px; border-radius: 6px; font-size: 15px; line-height: 1.3; }
.actions button:hover { background: var(--chip); }
.actions .on { color: #f59e0b; }

/* ---------- picker ---------- */
#picker {
  position: fixed; z-index: 30; display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
#picker button { background: none; border: 0; cursor: pointer; font-size: 20px; padding: 4px; border-radius: 8px; }
#picker button:hover { background: var(--chip); }

/* ---------- composer ---------- */
#replyBar {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 16px; font-size: 13px;
  color: var(--muted); border-top: 1px solid var(--line); border-left: 3px solid var(--accent);
}
#replyText { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#replyText b { color: var(--text); }
form.send { display: flex; gap: 8px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
#text { flex: 1; resize: none; max-height: 120px; }
.primary {
  font-weight: 600; background: var(--accent); color: var(--accent-text);
  border: 0; border-radius: 10px; padding: 0 18px; cursor: pointer;
}
.primary:disabled { opacity: .5; cursor: default; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 10px; padding: 8px 16px; cursor: pointer; }

/* ---------- username gate + dialogs ---------- */
#gate { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
#gate form, dialog form { display: flex; flex-direction: column; gap: 12px; }
#gate form { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; width: 100%; max-width: 360px; }
#gate h2, dialog h2 { margin: 0; }
#gate p, .hint { margin: 0; color: var(--muted); font-size: 14px; }
#gate .primary { padding: 10px 18px; }
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; width: calc(100% - 32px); max-width: 360px;
  background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog form { padding: 22px; }
dialog .row { display: flex; justify-content: flex-end; gap: 8px; }
dialog .primary { padding: 8px 18px; }
.err { color: #ef4444; font-size: 13px; min-height: 1.2em; }
.ok { color: #16a34a; font-size: 13px; min-height: 1.2em; }
#gate .link { align-self: center; }

.seen { margin-top: 2px; font-size: 11px; color: var(--muted); }
.msg.mine .seen { text-align: right; }

/* ---------- members panel (right) ---------- */
#members {
  width: 200px; flex: none; display: flex; flex-direction: column;
  background: var(--side); border-left: 1px solid var(--line);
}
#members.closed { display: none; }
#members .sub { font-weight: 400; letter-spacing: 0; }
#membersList { flex: 1; overflow-y: auto; padding: 0 8px 8px; display: flex; flex-direction: column; gap: 2px; }
.member {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 6px 10px; border: 0; border-radius: 8px; background: none; cursor: pointer; font-size: 14px;
}
.member:hover { background: var(--chip); }
.member.me { cursor: default; }
.member .mname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member .pip { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--muted); opacity: .45; }
.member.on .pip { background: #2fb344; opacity: 1; }
.member:not(.on) .mname { color: var(--muted); }
.member .tag { font-size: 11px; color: var(--muted); }
.members-head { padding: 10px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

/* ---------- small screens: rooms become a slide-over drawer ---------- */
@media (max-width: 759px) {
  #menuBtn { display: inline-block; }
  #sidebar {
    position: fixed; z-index: 20; inset: 0 auto 0 0; width: min(80vw, 280px);
    transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow);
  }
  #sidebar.open { transform: none; }
  #members {
    position: fixed; z-index: 20; inset: 0 0 0 auto; width: min(75vw, 260px); box-shadow: var(--shadow);
  }
  #scrim { position: fixed; inset: 0; z-index: 15; background: rgba(0, 0, 0, .45); }
  .msg { max-width: 92%; }
}
