:root {
  --bg: #ffffff;
  --bg-secondary: #f4f4f5;
  --bg-sidebar: #ffffff;
  --bg-chat: #e6ebee;
  --bg-bubble-in: #ffffff;
  --bg-bubble-out: #effdde;
  --bg-hover: #f0f0f0;
  --bg-active: #2481cc;
  --bg-active-text: #ffffff;
  --text: #000000;
  --text-secondary: #707579;
  --text-link: #2481cc;
  --border: #dadce0;
  --accent: #2481cc;
  --accent-hover: #1f6fb1;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  --unread-bg: #4fae4e;
  --danger: #e53935;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17212b;
    --bg-secondary: #1f2c38;
    --bg-sidebar: #17212b;
    --bg-chat: #0e1621;
    --bg-bubble-in: #182533;
    --bg-bubble-out: #2b5278;
    --bg-hover: #202b36;
    --bg-active: #2b5278;
    --bg-active-text: #ffffff;
    --text: #ffffff;
    --text-secondary: #708499;
    --text-link: #6cb6ff;
    --border: #0e1621;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  background: var(--bg-chat);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
#app { height: 100dvh; height: 100vh; display: flex; flex-direction: column; }

.loading {
  display: flex; align-items: center; justify-content: center;
  height: 100%; color: var(--text-secondary);
}

button {
  font: inherit; cursor: pointer; border: none; background: none; color: inherit;
}
input, textarea {
  font: inherit; color: inherit;
}

/* ===== Login ===== */
.login-screen {
  display: flex; align-items: center; justify-content: center;
  height: 100%; padding: 16px; background: var(--bg-chat);
}
.login-card {
  background: var(--bg); padding: 32px; border-radius: 12px;
  box-shadow: var(--shadow); width: 100%; max-width: 360px;
}
.login-card h1 {
  margin: 0 0 8px; font-size: 22px; text-align: center;
}
.login-card .hint {
  text-align: center; color: var(--text-secondary); font-size: 13px; margin-bottom: 24px;
}
.login-card label { display: block; margin-bottom: 12px; }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg); font-size: 15px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type=submit] {
  width: 100%; padding: 11px; background: var(--accent); color: #fff;
  border-radius: 8px; font-weight: 500; margin-top: 4px;
}
.login-card button[type=submit]:hover { background: var(--accent-hover); }
.login-card .error { color: var(--danger); font-size: 13px; margin-top: 8px; min-height: 16px; text-align: center; }

/* ===== Layout ===== */
.main {
  display: flex; height: 100%;
}
.sidebar {
  width: 360px; min-width: 280px; max-width: 420px;
  background: var(--bg-sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.chat-area {
  flex: 1; display: flex; flex-direction: column;
  background: var(--bg-chat);
  min-width: 0;
}

/* ===== Sidebar header ===== */
.sidebar-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
}
.sidebar-header .me {
  flex: 1; display: flex; align-items: center; gap: 10px; overflow: hidden;
}
.sidebar-header .me .name {
  font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sidebar-header .me .role {
  font-size: 11px; color: var(--text-secondary);
}
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
}
.icon-btn:hover { background: var(--bg-hover); }
.icon-btn svg { width: 22px; height: 22px; }

.search-bar {
  padding: 8px 12px; border-bottom: 1px solid var(--border);
}
.search-bar input {
  width: 100%; padding: 8px 12px; border: none; border-radius: 18px;
  background: var(--bg-secondary); outline: none;
}

.tabs {
  display: flex; padding: 0 8px; border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
}
.tab {
  flex: 1; padding: 10px 8px; text-align: center; cursor: pointer;
  color: var(--text-secondary); font-size: 13px; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ===== Chat list ===== */
.list {
  flex: 1; overflow-y: auto; overflow-x: hidden;
}
.list-empty {
  padding: 32px 16px; color: var(--text-secondary); text-align: center; font-size: 13px;
}
.chat-item, .user-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; cursor: pointer;
  border-bottom: 1px solid transparent;
}
.chat-item:hover, .user-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); color: var(--bg-active-text); }
.chat-item.active .preview, .chat-item.active .time { color: rgba(255,255,255,0.7); }

.avatar {
  width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 500; font-size: 17px;
  flex-shrink: 0; position: relative;
  background: #65aadd;
}
.avatar.sm { width: 32px; height: 32px; font-size: 14px; }
.avatar.lg { width: 36px; height: 36px; font-size: 15px; }
.avatar .online-dot {
  position: absolute; right: 0; bottom: 0;
  width: 12px; height: 12px; border-radius: 50%;
  background: #4fae4e; border: 2px solid var(--bg-sidebar);
}

.chat-item .body, .user-item .body {
  flex: 1; min-width: 0;
}
.chat-item .row1, .user-item .row1 {
  display: flex; justify-content: space-between; align-items: baseline; gap: 6px;
}
.chat-item .title, .user-item .name {
  font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .time {
  font-size: 12px; color: var(--text-secondary); white-space: nowrap;
}
.chat-item .row2 {
  display: flex; justify-content: space-between; align-items: center; gap: 6px; margin-top: 2px;
}
.chat-item .preview {
  flex: 1; color: var(--text-secondary); font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-item .status { color: var(--text-secondary); font-size: 12px; }

.badge {
  background: var(--unread-bg); color: #fff;
  border-radius: 12px; padding: 1px 7px; font-size: 12px;
  min-width: 20px; text-align: center; font-weight: 500;
}
.chat-item.active .badge { background: #fff; color: var(--accent); }

/* ===== Chat area ===== */
.chat-placeholder {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
}
.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border); box-shadow: var(--shadow);
  z-index: 2;
}
.chat-header .info { flex: 1; min-width: 0; cursor: pointer; }
.chat-header .title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header .status { font-size: 12px; color: var(--text-secondary); }
.chat-header .status.online { color: var(--accent); }
.chat-header .back { display: none; }
@media (max-width: 720px) {
  .chat-header .back { display: flex; }
}

.messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 12px 8px; display: flex; flex-direction: column; gap: 2px;
}
.date-divider {
  align-self: center; padding: 4px 10px; background: rgba(0,0,0,0.18);
  color: #fff; border-radius: 14px; font-size: 12px; margin: 8px 0;
}
@media (prefers-color-scheme: light) {
  .date-divider { background: rgba(0,0,0,0.16); }
}
.unread-divider {
  align-self: stretch; text-align: center;
  background: rgba(36, 129, 204, 0.1); color: var(--accent);
  padding: 4px 0; font-size: 12px; margin: 6px 0;
  border-radius: 4px;
}

.msg-group {
  display: flex; gap: 8px; max-width: 100%;
  padding: 0 8px;
}
.msg-group.out { justify-content: flex-end; }
.msg-group .avatar { align-self: flex-end; margin-bottom: 4px; }
.msg-group.out .avatar { display: none; }
.msg-group .stack { display: flex; flex-direction: column; gap: 2px; max-width: 70%; min-width: 0; }
@media (max-width: 600px) { .msg-group .stack { max-width: 85%; } }

.bubble {
  background: var(--bg-bubble-in); padding: 6px 10px;
  border-radius: 12px; box-shadow: var(--shadow);
  word-wrap: break-word; overflow-wrap: anywhere;
  position: relative; max-width: 100%;
}
.msg-group.out .bubble { background: var(--bg-bubble-out); }
@media (prefers-color-scheme: dark) {
  .msg-group.out .bubble { color: #fff; }
}
.bubble .sender {
  font-size: 12px; font-weight: 500; color: var(--accent); margin-bottom: 2px;
}
.bubble .text { white-space: pre-wrap; line-height: 1.35; }
.bubble .text a { color: var(--text-link); }
.bubble .meta {
  font-size: 11px; color: var(--text-secondary);
  float: right; margin-left: 8px; margin-top: 4px;
  white-space: nowrap; user-select: none;
}
.msg-group.out .bubble .meta { color: rgba(0,0,0,0.45); }
@media (prefers-color-scheme: dark) {
  .msg-group.out .bubble .meta { color: rgba(255,255,255,0.55); }
}
.bubble .meta .ticks { margin-left: 2px; }
.bubble .meta .ticks.read { color: #2481cc; }
@media (prefers-color-scheme: dark) {
  .bubble .meta .ticks.read { color: #7eb6f5; }
}
.bubble.deleted { font-style: italic; color: var(--text-secondary); }
.bubble.system {
  align-self: center; background: rgba(0,0,0,0.18); color: #fff;
  border-radius: 14px; padding: 4px 12px; font-size: 12px;
  box-shadow: none;
}

.bubble .reply-quote {
  border-left: 3px solid var(--accent);
  padding: 2px 6px; margin-bottom: 4px;
  background: rgba(36, 129, 204, 0.1);
  border-radius: 4px;
  font-size: 13px;
  cursor: pointer;
}
.bubble .reply-quote .qname { color: var(--accent); font-weight: 500; font-size: 12px; }
.bubble .reply-quote .qtext { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.attachment img, .attachment video {
  max-width: 320px; max-height: 320px;
  border-radius: 8px; display: block; margin: 4px 0;
  cursor: pointer;
}
.attachment .file {
  display: flex; gap: 8px; align-items: center;
  padding: 6px; background: rgba(0,0,0,0.05); border-radius: 8px;
  margin: 4px 0; text-decoration: none; color: inherit;
}
.attachment .file .ic {
  width: 40px; height: 40px; background: var(--accent);
  border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff;
}
.attachment .file .fn { font-weight: 500; }

.typing-indicator {
  padding: 6px 16px; color: var(--text-secondary); font-size: 12px;
  font-style: italic; min-height: 24px;
}

/* ===== Reply preview & composer ===== */
.reply-preview {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
}
.reply-preview .bar { width: 3px; height: 32px; background: var(--accent); border-radius: 2px; }
.reply-preview .text { flex: 1; min-width: 0; }
.reply-preview .qname { color: var(--accent); font-size: 12px; font-weight: 500; }
.reply-preview .qtext { color: var(--text-secondary); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px; background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; border: none; outline: none;
  background: var(--bg-secondary); border-radius: 18px;
  padding: 10px 14px; max-height: 140px; min-height: 36px;
  line-height: 1.3;
}
.composer .send {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.composer .send:hover { background: var(--accent-hover); }
.composer .send:disabled { background: var(--bg-secondary); color: var(--text-secondary); cursor: default; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px;
}
.modal {
  background: var(--bg); border-radius: 12px;
  max-width: 480px; width: 100%; max-height: 90vh;
  display: flex; flex-direction: column;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
.modal-header {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 17px; flex: 1; }
.modal-body { padding: 14px 16px; overflow-y: auto; flex: 1; }
.modal-footer {
  padding: 12px 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

.btn {
  padding: 8px 16px; border-radius: 8px; font-weight: 500;
  background: var(--bg-secondary); color: var(--text);
}
.btn:hover { background: var(--bg-hover); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.5; cursor: default; }

.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.form-row input, .form-row select {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg); outline: none;
}
.form-row input:focus, .form-row select:focus { border-color: var(--accent); }
.form-error { color: var(--danger); font-size: 13px; margin-top: 4px; }

/* Members picker */
.member-pick {
  display: flex; align-items: center; gap: 8px; padding: 8px;
  border-radius: 8px; cursor: pointer;
}
.member-pick:hover { background: var(--bg-hover); }
.member-pick input[type=checkbox] { transform: scale(1.2); }

/* Admin panel */
.admin-user-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-bottom: 1px solid var(--border);
}
.admin-user-row .actions { display: flex; gap: 4px; }
.admin-user-row .actions button { padding: 4px 8px; font-size: 12px; }
.admin-user-row.blocked { opacity: 0.5; }

/* Lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,0.92);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; cursor: zoom-out;
}
.lightbox img, .lightbox video { max-width: 95vw; max-height: 95vh; }

/* Mobile */
@media (max-width: 720px) {
  .sidebar { width: 100%; }
  .main.has-chat .sidebar { display: none; }
  .main:not(.has-chat) .chat-area { display: none; }
  .attachment img, .attachment video { max-width: 240px; max-height: 240px; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(127,127,127,0.4); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(127,127,127,0.6); }
