* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent: #2aabee;
  --accent-dark: #168ac9;
  --bg: #e7ebf0;
  --panel: #ffffff;
  --text: #202428;
  --text-sub: #8a929c;
  --bubble-mine: #e2f4ff;
  --bubble-other: #ffffff;
  --online: #4caf50;
}

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
}

.hidden { display: none !important; }

/* ---------- Auth ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(160deg, #dceefb 0%, #e7ebf0 60%);
}

.auth-card {
  background: var(--panel);
  border-radius: 18px;
  padding: 32px 28px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 8px 40px rgba(20, 60, 90, 0.15);
  text-align: center;
}

.auth-logo {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent), #6bc6f7);
  color: #fff;
  font-size: 30px;
  line-height: 64px;
}

.auth-card h1 { font-size: 24px; margin-bottom: 18px; }

.auth-input {
  width: 100%;
  padding: 12px 14px;
  margin-bottom: 10px;
  border: 1px solid #d5dbe3;
  border-radius: 10px;
  font-size: 15px;
  outline: none;
  background: #f6f8fa;
}
.auth-input:focus { border-color: var(--accent); background: #fff; }

.auth-btn {
  width: 100%;
  padding: 13px;
  margin-top: 6px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.auth-btn:hover { background: var(--accent-dark); }
.auth-btn:disabled { opacity: 0.6; cursor: default; }

.auth-error { color: #d32f2f; font-size: 13px; min-height: 18px; margin-bottom: 6px; }
.auth-switch { margin-top: 14px; font-size: 13px; color: var(--text-sub); }
.auth-switch a { color: var(--accent); text-decoration: none; }

.new-chat-panel {
  padding: 10px 14px;
  border-bottom: 1px solid #e5e9ef;
}
.new-chat-panel .auth-input { margin-bottom: 8px; }
.panel-title { font-weight: 600; font-size: 13px; color: var(--text-sub); margin: 2px 0 4px; }
.panel-divider { height: 1px; background: #e5e9ef; margin: 13px 0 10px; }

/* ---------- Layout ---------- */
.app-screen {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 340px;
  min-width: 340px;
  background: var(--panel);
  border-right: 1px solid #e0e4ea;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid #eef1f5;
  position: relative;
}

.bg-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 14px;
  background: var(--panel);
  border: 1px solid #e0e4ea;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  padding: 10px;
  width: 240px;
  z-index: 70;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.bg-swatch {
  height: 44px;
  border-radius: 10px;
  cursor: pointer;
  border: 2px solid transparent;
  padding: 0;
}
.bg-swatch:hover { border-color: #b8c4d2; }
.bg-swatch.active { border-color: var(--accent); }
.bg-upload-btn {
  grid-column: 1 / -1;
  margin-top: 4px;
  border: 1px dashed #d5dbe3;
  background: transparent;
  border-radius: 10px;
  padding: 8px;
  color: var(--text-sub);
  cursor: pointer;
  font-size: 13px;
}
.bg-upload-btn:hover { color: var(--accent); border-color: var(--accent); }

.my-avatar, .chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #6bc6f7);
  color: #fff;
  font-size: 20px;
  line-height: 40px;
  text-align: center;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}
.my-avatar { cursor: pointer; }
.chat-avatar.editable { cursor: pointer; }
.avatar-img { width: 100%; height: 100%; object-fit: cover; display: block; }

.sidebar-title { flex: 1; min-width: 0; }
.sidebar-title .sidebar-sub { font-size: 12px; color: var(--text-sub); }
.chat-title .sidebar-sub { font-size: 12px; color: var(--text-sub); }

.icon-btn {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--accent);
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: #eef4f9; }
.icon-btn:disabled { opacity: 0.4; cursor: default; }
.icon-btn.disabled { opacity: 0.45; cursor: default; }

.chat-list { flex: 1; overflow-y: auto; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid #f3f5f8;
}
.chat-item:hover { background: #f5f8fb; }
.chat-item.active { background: #e8f4fc; }

.chat-item .chat-info { flex: 1; min-width: 0; }
.chat-item .chat-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .chat-preview { font-size: 13px; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #c4cbd4;
  flex-shrink: 0;
}
.dot.online { background: var(--online); }

.chat-avatar.small { width: 46px; height: 46px; line-height: 46px; font-size: 22px; border-radius: 50%; }
.chat-avatar.small.group { background: linear-gradient(135deg, #e08a3c, #f5c874); }
.section-label { font-size: 12px; font-weight: 600; color: var(--text-sub); padding: 10px 14px 4px; }
.group-count {
  font-size: 12px;
  color: var(--text-sub);
  background: #eef1f5;
  border-radius: 10px;
  padding: 2px 9px;
  flex-shrink: 0;
}

.chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-sub);
  gap: 12px;
}
.chat-empty-icon {
  width: 80px;
  height: 80px;
  border-radius: 24px;
  background: linear-gradient(135deg, var(--accent), #6bc6f7);
  color: #fff;
  font-size: 40px;
  line-height: 80px;
  text-align: center;
  opacity: 0.8;
}

.chat-active { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid #e0e4ea;
}
.chat-header .icon-btn { display: none; }

.chat-title { min-width: 0; }
#chat-name { font-weight: 600; font-size: 16px; }
#chat-status { font-size: 12px; color: var(--text-sub); }
#chat-status.online { color: var(--online); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg {
  max-width: 72%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.4;
  word-wrap: break-word;
  position: relative;
}
.msg.other {
  background: var(--bubble-other);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.msg.mine {
  background: var(--bubble-mine);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.msg img.msg-photo {
  display: block;
  max-width: 280px;
  max-height: 320px;
  border-radius: 10px;
  margin: -4px -6px;
  cursor: pointer;
}
.msg-time { font-size: 11px; color: var(--text-sub); text-align: right; margin-top: 3px; }
.msg-tick { color: #b9c2cc; margin-left: 6px; font-weight: 600; white-space: nowrap; }
.msg-tick.read { color: var(--accent); }
.msg-readat { color: var(--accent); margin-left: 6px; white-space: nowrap; }
.msg-readat:empty { display: none; }
.msg-sender { font-size: 12px; font-weight: 600; color: var(--accent-dark); margin-bottom: 2px; }
.msg-video {
  display: block;
  max-width: 280px;
  max-height: 320px;
  border-radius: 10px;
  margin: -4px -6px;
}
.msg-file {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--accent-dark);
  text-decoration: none;
  font-weight: 600;
  max-width: 100%;
}
.msg-file-icon { font-size: 20px; }
.msg-file span:last-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.photo-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  cursor: zoom-out;
}
.photo-viewer img { max-width: 95vw; max-height: 95vh; border-radius: 6px; }

.chat-input-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--panel);
  border-top: 1px solid #e0e4ea;
  position: relative;
}

.reply-bar {
  flex-basis: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(42, 171, 238, 0.08);
  border: 1px solid rgba(42, 171, 238, 0.25);
  border-radius: 10px;
  padding: 6px 10px;
}
.reply-info { min-width: 0; }
.reply-from { font-size: 12px; color: var(--accent); font-weight: 600; }
.reply-preview {
  font-size: 13px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 46vw;
}
.reply-cancel {
  background: none;
  border: none;
  color: var(--text-sub);
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}

.msg { position: relative; }
.msg-reply {
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  background: rgba(42, 171, 238, 0.08);
  padding: 4px 8px;
  margin-bottom: 6px;
  max-width: 340px;
}
.msg-reply-name { font-size: 12px; font-weight: 600; color: var(--accent); }
.msg-reply-text {
  font-size: 13px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-actions {
  position: absolute;
  top: 4px;
  right: 6px;
  display: none;
  gap: 2px;
  z-index: 2;
}
.msg:hover > .msg-actions { display: flex; }
@media (pointer: coarse) {
  .msg-actions { display: flex; opacity: 0.4; }
}
.msg-actions .act {
  background: var(--panel);
  border: 1px solid #dfe4ea;
  border-radius: 6px;
  font-size: 14px;
  width: 26px;
  height: 26px;
  cursor: pointer;
  line-height: 1;
}
.msg-actions .act:hover { border-color: var(--accent); }

.forward-list {
  overflow-y: auto;
  flex: 1;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.forward-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid #e6eaef;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
}
.forward-row:hover { background: #f3f7fb; }
.forward-row.sel { border-color: var(--accent); background: rgba(42, 171, 238, 0.07); }
.forward-name { flex: 1; font-size: 15px; color: var(--text); }
.forward-check { opacity: 0; color: var(--accent); font-weight: 700; }
.forward-row.sel .forward-check { opacity: 1; }
.forward-actions { flex: none; padding: 10px 14px; }
.forward-actions .auth-btn { width: 100%; }

.emoji-btn { font-size: 19px; }

.emoji-panel {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 12px;
  background: var(--panel);
  border: 1px solid #e0e4ea;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  padding: 8px;
  width: min(340px, calc(100vw - 40px));
  max-height: 240px;
  overflow-y: auto;
  z-index: 70;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}
.emoji-recent {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding-bottom: 6px;
  margin-bottom: 6px;
  border-bottom: 1px solid #eef1f5;
}
.emoji-recent:empty { display: none; }
.emoji-cell {
  border: none;
  background: transparent;
  font-size: 22px;
  line-height: 1.3;
  padding: 4px;
  cursor: pointer;
  border-radius: 6px;
}
.emoji-cell:hover { background: #eef4f9; }

.message-input {
  flex: 1;
  padding: 11px 14px;
  border: 1px solid #d5dbe3;
  border-radius: 20px;
  font-size: 15px;
  outline: none;
}
.message-input:focus { border-color: var(--accent); }

.photo-btn { color: var(--text-sub); font-size: 22px; }
.photo-btn:hover { color: var(--accent); }

.send-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-dark); }
.send-btn:disabled { opacity: 0.5; cursor: default; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #333c45;
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

/* ---------- Media gallery ---------- */
.media-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 25, 35, 0.5);
  z-index: 150;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.media-panel {
  background: var(--panel);
  border-radius: 16px;
  width: 100%;
  max-width: 760px;
  height: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.3);
}
.media-header {
  padding: 12px 14px;
  border-bottom: 1px solid #eef1f5;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.media-title { font-weight: 600; font-size: 16px; }
.media-tabs { display: flex; gap: 6px; flex: 1; min-width: 0; flex-wrap: wrap; }
.media-tab {
  border: none;
  background: #eef1f5;
  color: var(--text-sub);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}
.media-tab.active { background: var(--accent); color: #fff; }
.media-grid {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  align-content: start;
}
.media-item.thumb {
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  background: #dfe4ea;
}
.media-item.thumb img,
.media-item.thumb video { width: 100%; height: 100%; object-fit: cover; }
.media-play {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 34px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}
.media-item.file { border: 1px solid #e5e9ef; border-radius: 12px; padding: 10px; }
.media-item.file a {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-decoration: none;
  color: var(--text);
  min-width: 0;
}
.media-file-icon { font-size: 28px; }
.media-file-name { font-size: 13px; font-weight: 600; word-break: break-word; }
.media-file-meta { font-size: 11px; color: var(--text-sub); }
.media-empty { padding: 40px 20px; text-align: center; color: var(--text-sub); flex: 1; }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .sidebar { width: 100%; min-width: 0; }
  .sidebar.viewing-chat { display: none; }
  .chat-window { width: 100%; }
  .chat-window.idle { display: none; }
  .chat-header .icon-btn { display: block; margin-left: -6px; }
  .chat-active { width: 100%; }
  .msg { max-width: 85%; }
  .msg img.msg-photo { max-width: 220px; }
  .msg-video { max-width: 220px; }
  .media-overlay { padding: 10px; align-items: stretch; }
  .media-panel { max-height: none; }
  .media-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}