:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1c1f24;
  --muted: #646b75;
  --border: #dcdfe4;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --mine: #e6efff;
  --danger: #b42318;
  --ok: #157f3c;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1d2025;
    --surface-2: #262a31;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --border: #343941;
    --accent: #5b95ee;
    --accent-text: #0b1220;
    --mine: #1f2d45;
    --danger: #f97066;
    --ok: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Noto Sans", sans-serif;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); margin: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

button, .button, input, select, textarea { font: inherit; color: inherit; }
button, .button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
button:hover, .button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button:disabled { opacity: 0.55; cursor: default; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 9px 11px;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: grid; gap: 4px; font-weight: 500; }

/* Sign-in and admin cards */
.card-screen { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.card {
  width: 100%; max-width: 440px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 24px; display: grid; gap: 18px;
}
.card > p { margin: 0; }

/* App frame */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.title-block { display: flex; align-items: center; gap: 10px; min-width: 0; }
.title-block h1 { font-size: 1.1rem; }
.actions { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.inline { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; color: var(--muted); }
.inline input { width: auto; }
.inline select { width: auto; padding: 3px 6px; max-width: 180px; }
.pill { font-size: 0.8rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.icon-btn { border: none; background: none; padding: 4px 7px; font-size: 1rem; line-height: 1; color: var(--muted); }
.icon-btn:hover { color: var(--text); }
.only-mobile { display: none; }

.layout { flex: 1; display: flex; min-height: 0; }

/* People list */
.sidebar {
  width: 290px; flex: none; display: flex; flex-direction: column; gap: 10px;
  background: var(--surface); border-right: 1px solid var(--border); padding: 12px 10px; min-height: 0;
}
.me { display: flex; gap: 10px; align-items: center; padding: 4px 6px; }
.me div, .conv-main { display: grid; min-width: 0; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: grid; align-content: start; gap: 2px; }
.conv {
  width: 100%; border: none; background: none; padding: 8px; border-radius: 8px;
  display: flex; align-items: center; gap: 10px; text-align: left;
}
.conv.open { background: var(--surface-2); }
.conv-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-sub { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .conv-name, .conv.unread .conv-sub { font-weight: 700; color: var(--text); }
.conv-main { flex: 1; }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; font-size: 0.75rem; font-weight: 700; padding: 1px 7px; }
.pad { padding: 8px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--border); }
.dot.online { background: var(--ok); }
.dot.group {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 0.8rem; font-weight: 700;
}

/* Chat boxes, side by side */
.boxes { flex: 1; display: flex; gap: 10px; padding: 10px; overflow-x: auto; min-width: 0; }
.empty { margin: auto; text-align: center; max-width: 360px; }
.box {
  flex: 1 1 0; min-width: 330px; max-width: 640px; display: flex; flex-direction: column; position: relative;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; min-height: 0;
}
.box.active { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }
.box-head { display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 12px; border-bottom: 1px solid var(--border); }
.box-title { flex: 1; display: grid; min-width: 0; }
.box-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.box-actions { display: flex; }
.messages { list-style: none; margin: 0; padding: 12px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.messages .system, .messages .more { align-self: center; font-size: 0.85rem; color: var(--muted); }
.messages .more button { font-size: 0.85rem; padding: 4px 10px; }
.typing { margin: 0; padding: 2px 12px; }
.box .reply-bar { margin: 6px 10px 0; }
.composer { display: flex; gap: 6px; align-items: flex-end; padding: 8px 10px 10px; }
.composer textarea { resize: none; max-height: 140px; }
.composer .primary { height: 40px; }
.emoji-btn, .attach-btn { font-size: 1.2rem; height: 40px; }

/* Messages */
.msg { max-width: 88%; align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px 10px; }
.msg.mine { align-self: flex-end; background: var(--mine); }
.msg.call { align-self: center; text-align: center; }
.msg-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; font-size: 0.8rem; color: var(--muted); }
.msg-head strong { color: var(--text); font-size: 0.88rem; }
.msg-text { margin: 2px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-original { margin: 4px 0 0; font-size: 0.85rem; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; border-top: 1px dashed var(--border); padding-top: 4px; }
.msg-pending { font-style: italic; }
.msg-actions { margin-left: auto; display: inline-flex; gap: 0; }
.act-btn { border: none; background: none; padding: 0 4px; font-size: 0.9rem; color: var(--muted); opacity: 0; }
.msg:hover .act-btn, .act-btn:focus-visible { opacity: 1; }
@media (hover: none) { .act-btn { opacity: 1; } }
.act-btn:hover { color: var(--accent); background: none; }
.join-btn { margin-left: 6px; padding: 3px 10px; font-size: 0.85rem; }
.msg-quote {
  display: block; width: 100%; text-align: left; margin: 4px 0 2px; padding: 4px 8px;
  border: none; border-left: 3px solid var(--accent); border-radius: 4px;
  background: var(--surface-2); color: var(--muted); font-size: 0.85rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msg-quote strong { color: var(--text); margin-right: 6px; }
.mention { color: var(--accent); font-weight: 600; }
.msg.mentions-me { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.msg.flash { animation: flash 1.6s; }
@keyframes flash { 30% { background: color-mix(in srgb, var(--accent) 22%, var(--surface)); } }
.reply-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 10px; border-left: 3px solid var(--accent); border-radius: 6px; background: var(--surface-2);
}
.reply-bar > div { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-text { color: var(--muted); margin-left: 6px; }
.reply-bar button { border: none; background: none; padding: 2px 6px; }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.reaction { padding: 1px 8px; font-size: 0.85rem; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); }
.reaction.mine { border-color: var(--accent); color: var(--accent); }
.mention-menu {
  position: absolute; left: 10px; bottom: 58px; z-index: 4;
  list-style: none; margin: 0; padding: 4px; min-width: 220px; max-width: calc(100% - 20px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
}
.mention-menu li { padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.mention-menu li span { color: var(--muted); font-size: 0.85rem; }
.mention-menu li[aria-selected="true"], .mention-menu li:hover { background: var(--surface-2); }
.emoji-pop {
  position: fixed; z-index: 50; display: flex; gap: 2px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.18);
}
.emoji-pop.grid { display: grid; grid-template-columns: repeat(8, 36px); max-width: calc(100vw - 16px); }
.emoji-pop button { border: none; background: none; width: 36px; height: 36px; padding: 0; font-size: 1.25rem; justify-content: center; border-radius: 8px; }
.emoji-pop button:hover, .emoji-pop button:focus-visible { background: var(--surface-2); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 8px; font-size: 0.9rem; z-index: 60;
}

/* Communicator-style presence */
.presence { width: 12px; height: 12px; border-radius: 50%; flex: none; background: #d93b3b; }
.presence.p-available { background: #2bb24c; }
.presence.p-busy { background: #f07c00; }
.presence.p-dnd { background: #f07c00; position: relative; }
.presence.p-dnd::after { content: ""; position: absolute; left: 25%; right: 25%; top: 45%; height: 2px; background: #fff; }
.presence.p-brb, .presence.p-away { background: #e8b100; }
.box-head .presence { width: 12px; height: 12px; }

.me { align-items: flex-start; border-bottom: 1px solid var(--border); padding: 4px 6px 12px; }
.me .presence { margin-top: 5px; width: 14px; height: 14px; }
.me-main { flex: 1; display: grid; gap: 4px; min-width: 0; }
.me-main strong { font-size: 1rem; }
.me-main select { padding: 2px 6px; width: auto; justify-self: start; font-size: 0.85rem; }
.me-main input { padding: 4px 8px; font-size: 0.85rem; }
.me-menu { position: relative; }
.menu {
  position: absolute; right: 0; top: 100%; z-index: 20; display: grid; min-width: 180px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
}
.menu button { border: none; background: none; justify-content: flex-start; }
.menu button:hover { background: var(--surface-2); }

.top-search { flex: 1; max-width: 460px; padding: 7px 12px; border-radius: 999px; background: var(--surface-2); }
.group-head { margin-top: 8px; }
.group-head button {
  width: 100%; border: none; background: none; padding: 4px 6px; justify-content: flex-start;
  font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted);
}
.group-head.static { padding: 4px 6px; font-size: 0.8rem; font-weight: 600; text-transform: uppercase; color: var(--muted); }
.chev { width: 12px; display: inline-block; }
.note.small { margin: 0 6px; }

.divider-new { align-self: stretch; display: flex; align-items: center; gap: 8px; color: var(--danger); font-size: 0.8rem; font-weight: 600; }
.divider-new::before, .divider-new::after { content: ""; flex: 1; border-top: 1px solid var(--danger); }

/* Photos and files */
.file-thumb { display: block; padding: 0; border: none; background: none; margin-top: 4px; }
.file-thumb img { display: block; max-width: 100%; max-height: 240px; border-radius: 8px; min-width: 80px; min-height: 60px; background: var(--surface-2); }
.file-card { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.file-icon { font-size: 1.6rem; }
.file-meta { display: grid; min-width: 0; flex: 1; }
.file-meta strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-card button { padding: 4px 10px; font-size: 0.85rem; }
.box.dropping { outline: 3px dashed var(--accent); outline-offset: -3px; }

/* A chat in its own window */
body.popup .sidebar, body.popup .topbar { display: none; }
body.popup .boxes { padding: 0; }
body.popup .box { max-width: none; min-width: 0; border: none; border-radius: 0; }

/* Admin page */
.admin { max-width: 860px; margin: 0 auto; padding: 24px 16px; display: grid; gap: 20px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; display: grid; gap: 14px; }
.row { display: grid; grid-template-columns: 2fr 2fr 1.4fr auto; gap: 10px; align-items: end; }
.link-box { display: flex; gap: 8px; }
.link-box input { font-family: ui-monospace, monospace; font-size: 0.85rem; }
.member-table { width: 100%; border-collapse: collapse; }
.member-table td, .member-table th { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.member-table th { font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.member-table td:last-child { text-align: right; white-space: nowrap; }
.member-table button { padding: 4px 10px; font-size: 0.85rem; }
button.danger { color: var(--danger); }
.conv-admin-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; max-height: 420px; overflow-y: auto; }
.conv-admin-list button { width: 100%; justify-content: space-between; text-align: left; white-space: normal; gap: 12px; }
.ca-main { display: grid; min-width: 0; }
.ca-main .small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ca-side { text-align: right; flex: none; }
.transcript { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; max-height: 70vh; overflow-y: auto; }
.transcript li { border-bottom: 1px solid var(--border); padding-bottom: 8px; overflow-wrap: anywhere; }
.transcript div { white-space: pre-wrap; }
.transcript button { padding: 2px 8px; font-size: 0.85rem; }
.member-table .access { white-space: nowrap; }
.member-table .access label { display: inline-flex; gap: 4px; align-items: center; font-weight: 400; margin-right: 10px; }
.member-table .access input { width: auto; }
.read-only { margin: 0; padding: 10px 12px; border-top: 1px solid var(--border); }
.net-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.net-list li { display: flex; gap: 8px; align-items: center; }
.net-list button { padding: 2px 8px; font-size: 0.8rem; }
.note { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin: 0; }

@media (max-width: 760px) {
  .only-mobile { display: inline-flex; }
  .topbar .actions .inline:first-of-type { display: none; }
  .top-search { order: 3; flex-basis: 100%; max-width: none; }
  .sidebar { width: 100%; border-right: none; }
  .boxes { display: none; padding: 0; }
  body.chatting .sidebar { display: none; }
  body.chatting .boxes { display: flex; }
  .box { display: none; min-width: 0; max-width: none; border: none; border-radius: 0; }
  .box.active { display: flex; }
  body.chatting .empty { display: none; }
  .row { grid-template-columns: 1fr; }
  .member-table th:nth-child(2), .member-table td:nth-child(2), .member-table th:nth-child(3), .member-table td:nth-child(3) { display: none; }
}
@media (min-width: 761px) { #back-btn { display: none; } }

/* Group chats */
.messages .chat-event { align-self: center; font-size: 0.82rem; color: var(--muted); text-align: center; padding: 2px 8px; }
.new-group { width: 100%; text-align: left; background: none; border: 1px dashed var(--border); border-radius: 8px; padding: 6px 10px; margin: 4px 0 8px; color: var(--accent); cursor: pointer; font: inherit; }
.group-dialog { border: none; border-radius: 12px; padding: 20px; width: min(440px, calc(100vw - 32px)); background: var(--surface); color: inherit; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }
.group-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.group-dialog h2 { margin: 0 0 12px; font-size: 1.1rem; }
.group-dialog form { display: flex; flex-direction: column; gap: 10px; }
.group-dialog label { display: flex; flex-direction: column; gap: 4px; }
.group-people { list-style: none; margin: 0; padding: 0; max-height: 280px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
.group-people li { padding: 6px 10px; }
.group-people li label { flex-direction: row; align-items: center; gap: 8px; cursor: pointer; }
.group-people .presence { flex: none; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dept-input { width: 100%; min-width: 120px; }
.member-table tr.paused td { opacity: 0.6; }
.group-admin-list li { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.transcript li.event { color: var(--muted); }
.admin-chat { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.admin-chat .messages { max-height: 60vh; min-height: 200px; }
.admin-chat .composer { display: flex; gap: 8px; padding: 8px; border-top: 1px solid var(--border); background: var(--surface); }
.admin-chat .composer textarea { flex: 1; }
.admin-chat .read-only { padding: 8px 12px; margin: 0; }
