:root {
  --bg: #0f1115;
  --bg-soft: #151821;
  --panel: #191d27;
  --panel-2: #1f2431;
  --border: #2a3040;
  --text: #e8ebf3;
  --muted: #8b93a7;
  --accent: #7c5cff;
  --accent-2: #9d85ff;
  --accent-soft: rgba(124, 92, 255, .14);
  --danger: #ff5c7a;
  --ok: #3ddc97;
  --radius: 14px;
  --shadow: 0 18px 50px rgba(0, 0, 0, .45);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent-2); text-decoration: none; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }

/* ---------------------------------------------------------------- buttons */
.primary-btn, .ghost-btn, .danger-btn, .send-btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
  font-weight: 600;
  transition: transform .08s ease, background .15s ease, opacity .15s ease;
}
.primary-btn { background: var(--accent); color: #fff; }
.primary-btn:hover { background: var(--accent-2); }
.primary-btn:disabled { opacity: .5; cursor: not-allowed; }
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ghost-btn:hover { background: var(--panel-2); }
.danger-btn { background: rgba(255, 92, 122, .14); color: var(--danger); border: 1px solid rgba(255, 92, 122, .35); }
.danger-btn:hover { background: rgba(255, 92, 122, .24); }
.send-btn { background: var(--accent); color: #fff; padding: 9px 18px; border-radius: 10px; }
.send-btn:disabled { opacity: .45; cursor: not-allowed; }

.icon-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  width: 34px;
  height: 34px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  white-space: nowrap;
}
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--ok); }

/* ---------------------------------------------------------------- auth */
.auth {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: hidden;
}
.auth-bg {
  position: absolute;
  inset: -20%;
  background:
    radial-gradient(45% 45% at 20% 20%, rgba(124, 92, 255, .28), transparent 70%),
    radial-gradient(40% 40% at 80% 75%, rgba(61, 220, 151, .14), transparent 70%),
    radial-gradient(35% 35% at 60% 10%, rgba(255, 92, 122, .12), transparent 70%);
  filter: blur(10px);
}
.auth-card {
  position: relative;
  width: min(420px, 100%);
  background: rgba(25, 29, 39, .92);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.auth-brand h1 { margin: 0; font-size: 22px; letter-spacing: .5px; }
.auth-brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.logo {
  width: 46px; height: 46px;
  border-radius: 14px;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), #5b3ee0);
  color: #fff; font-weight: 700; font-size: 22px;
  box-shadow: 0 8px 24px rgba(124, 92, 255, .35);
}
.logo.small { width: 30px; height: 30px; border-radius: 9px; font-size: 15px; box-shadow: none; }

.tabs { display: flex; gap: 6px; background: var(--bg-soft); border: 1px solid var(--border); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.tab { flex: 1; background: transparent; border: 0; color: var(--muted); padding: 9px; border-radius: 9px; font-weight: 600; }
.tab.active { background: var(--panel-2); color: var(--text); }

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label, .field { display: flex; flex-direction: column; gap: 7px; font-size: 13px; color: var(--muted); }
.auth-form input, .field input, .field textarea {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  color: var(--text);
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.auth-form input:focus, .field input:focus, .field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.auth-form .primary-btn { margin-top: 4px; padding: 12px; }
.form-error { color: var(--danger); font-size: 13px; margin: 14px 0 0; }
.auth-foot { color: var(--muted); font-size: 12px; margin: 18px 0 0; text-align: center; }

/* ---------------------------------------------------------------- layout */
.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  height: 58px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(21, 24, 33, .9);
  backdrop-filter: blur(10px);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: .3px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.user-chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px 4px 5px;
  color: var(--text);
}
.user-chip:hover { border-color: var(--accent); }

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

.sidebar {
  width: 264px;
  flex: 0 0 264px;
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 10px;
  color: var(--muted); font-size: 12px; letter-spacing: 1px; text-transform: uppercase;
}
.room-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.room-item {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  background: transparent; border: 0;
  border-radius: 11px;
  padding: 11px 12px;
  text-align: left;
  color: var(--text);
  margin-bottom: 2px;
}
.room-item:hover { background: var(--panel); }
.room-item.active { background: var(--accent-soft); box-shadow: inset 0 0 0 1px rgba(124, 92, 255, .35); }
.room-hash { color: var(--muted); font-weight: 700; }
.room-item.active .room-hash { color: var(--accent-2); }
.room-meta { min-width: 0; flex: 1; }
.room-name { display: block; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room-sub { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room-count { color: var(--muted); font-size: 11px; background: var(--panel-2); border-radius: 999px; padding: 2px 7px; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.chat-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 17, 21, .6);
}
.chat-title h2 { margin: 0; font-size: 17px; }
.chat-title p { margin: 2px 0 0; font-size: 12px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: auto;
}
.messages::-webkit-scrollbar, .room-list::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb, .room-list::-webkit-scrollbar-thumb { background: #2c3344; border-radius: 8px; }

.day-sep { align-self: center; color: var(--muted); font-size: 11px; background: var(--panel); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; margin: 10px 0; }
.load-more { align-self: center; margin: 6px 0 12px; }

.msg { display: flex; gap: 10px; padding: 4px 0; position: relative; }
.msg.own { flex-direction: row-reverse; }
.msg-body { max-width: min(680px, 78%); min-width: 0; }
.msg.own .msg-body { display: flex; flex-direction: column; align-items: flex-end; }
.msg-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 3px; font-size: 12px; }
.msg.own .msg-head { flex-direction: row-reverse; }
.msg-name { font-weight: 600; color: var(--text); }
.msg-time { color: var(--muted); font-size: 11px; }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 5px; background: var(--accent-soft); color: var(--accent-2); border: 1px solid rgba(124, 92, 255, .3); }
.badge.mod { background: rgba(61, 220, 151, .12); color: var(--ok); border-color: rgba(61, 220, 151, .3); }

.bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px 14px 14px 14px;
  padding: 9px 13px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
  display: inline-block;
  max-width: 100%;
}
.msg.own .bubble {
  background: linear-gradient(145deg, rgba(124, 92, 255, .95), rgba(97, 68, 224, .95));
  border-color: transparent;
  border-radius: 14px 4px 14px 14px;
  color: #fff;
}
.bubble.deleted { color: var(--muted); font-style: italic; background: transparent; border-style: dashed; }
.bubble .image-wrap { margin: -3px -7px; }
.bubble img.chat-image { display: block; max-width: min(360px, 60vw); max-height: 320px; border-radius: 11px; cursor: zoom-in; object-fit: cover; }
.bubble .caption { margin-top: 7px; }

.reply-quote {
  border-left: 3px solid var(--accent-2);
  background: rgba(255, 255, 255, .04);
  border-radius: 6px;
  padding: 5px 9px;
  margin-bottom: 7px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.reply-quote b { color: var(--accent-2); font-weight: 600; }

.reactions { display: flex; gap: 5px; margin-top: 5px; flex-wrap: wrap; }
.reaction {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 2px 8px; font-size: 12px; color: var(--muted);
}
.reaction:hover { border-color: var(--accent); }
.reaction.mine { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-2); }

.msg-actions {
  position: absolute; top: 2px; right: 0;
  display: flex; gap: 2px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s ease;
  z-index: 2;
}
.msg:hover .msg-actions, .msg:focus-within .msg-actions { opacity: 1; pointer-events: auto; }
.msg.own .msg-actions { right: auto; left: 0; }
.msg-actions button { border: 0; background: transparent; color: var(--muted); padding: 4px 6px; border-radius: 6px; font-size: 13px; }
.msg-actions button:hover { background: var(--panel); color: var(--text); }

.system-msg {
  align-self: center;
  max-width: 90%;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  margin: 6px 0;
}

.avatar {
  width: 36px; height: 36px; flex: 0 0 36px;
  border-radius: 50%;
  background: linear-gradient(145deg, #39405a, #262c3d);
  display: grid; place-items: center;
  font-size: 14px; font-weight: 600; color: #cfd6ea;
  overflow: hidden;
  user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.tiny { width: 26px; height: 26px; flex-basis: 26px; font-size: 11px; }
.avatar.big { width: 64px; height: 64px; flex-basis: 64px; font-size: 24px; }

.scroll-bottom {
  position: absolute; left: 50%; bottom: 96px; transform: translateX(-50%);
  background: var(--accent); color: #fff; border: 0; border-radius: 999px;
  padding: 7px 14px; font-size: 12px; box-shadow: var(--shadow); z-index: 5;
}

/* ---------------------------------------------------------------- composer */
.composer-wrap { flex: 0 0 auto; padding: 10px 20px 16px; border-top: 1px solid var(--border); background: var(--bg-soft); }
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 8px;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea {
  flex: 1;
  resize: none;
  border: 0;
  background: transparent;
  outline: none;
  padding: 9px 4px;
  max-height: 140px;
  line-height: 1.5;
}
.char-count { color: var(--muted); font-size: 11px; padding-bottom: 10px; white-space: nowrap; }
.composer-error { color: var(--danger); font-size: 12px; padding: 0 4px 8px; }
.reply-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 6px 10px;
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--muted);
}
.reply-name { color: var(--accent-2); font-weight: 600; }
.reply-bar .icon-btn { margin-left: auto; width: 26px; height: 26px; }

/* ---------------------------------------------------------------- modal */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(5, 6, 9, .7); backdrop-filter: blur(3px); }
.modal-card {
  position: relative;
  width: min(520px, 100%);
  max-height: min(86vh, 760px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  animation: pop .16s ease;
}
.modal-card.narrow { width: min(420px, 100%); }
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 6px; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 12px 20px 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-body h4 { margin: 4px 0 0; font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.sep { border: 0; border-top: 1px solid var(--border); margin: 6px 0; width: 100%; }
.avatar-row { display: flex; align-items: center; gap: 16px; }
.upload-label { cursor: pointer; }

.session-list { display: flex; flex-direction: column; gap: 8px; }
.session-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 12px;
}
.session-row .grow { flex: 1; min-width: 0; }
.session-row .ua { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-row .current { color: var(--ok); font-size: 11px; }

.reason-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: var(--bg-soft); border: 1px solid var(--border); color: var(--muted); border-radius: 999px; padding: 6px 12px; font-size: 12px; }
.chip:hover { border-color: var(--accent); color: var(--text); }

.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .9); display: grid; place-items: center; cursor: zoom-out; padding: 24px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* ---------------------------------------------------------------- toasts */
.toasts { position: fixed; top: 16px; right: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: slide .18s ease;
  max-width: 320px;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }
@keyframes slide { from { transform: translateX(12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------------------------------------------------------------- maintenance */
.maintenance { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; background: var(--bg); padding: 24px; }
.maintenance-card { text-align: center; max-width: 420px; }
.maintenance-icon { font-size: 42px; margin-bottom: 10px; }
.maintenance-card h1 { font-size: 20px; margin: 0 0 8px; }
.maintenance-card p { color: var(--muted); }

/* ---------------------------------------------------------------- responsive */
.only-mobile { display: none; }
.sidebar-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .55); z-index: 24; }

@media (max-width: 760px) {
  .only-mobile { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: 58px; bottom: 0; left: 0;
    z-index: 25;
    transform: translateX(-100%);
    transition: transform .2s ease;
    width: 78vw; max-width: 300px;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .chat-head { padding: 10px 14px; }
  .messages { padding: 12px 12px 6px; }
  .composer-wrap { padding: 8px 10px 12px; }
  .msg-body { max-width: 84%; }
  .bubble img.chat-image { max-width: 62vw; }
  .msg-actions { opacity: 1; pointer-events: auto; position: static; background: transparent; border: 0; padding: 0; margin-top: 4px; justify-content: flex-end; }
  .msg.own .msg-actions { justify-content: flex-start; }
  .msg-actions button { background: var(--panel-2); border: 1px solid var(--border); }
  .topbar { padding: 0 10px; }
  .user-chip #user-name { max-width: 70px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
