/* WeChat-style chatroom frontend */

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

:root {
  --green:        #07C160;
  --green-dark:   #06AD56;
  --bubble-self:  #95EC69;
  --bubble-other: #FFFFFF;
  --header-bg:    #EDEDED;
  --header-text:  #1A1A1A;
  --list-bg:      #EDEDED;
  --item-bg:      #FFFFFF;
  --sep:          rgba(0,0,0,0.10);
  --badge:        #FA3434;
  --text-main:    #1A1A1A;
  --text-sub:     #888888;
  --text-time:    #AAAAAA;
  --tab-bg:       #F7F7F7;
  --tab-border:   rgba(0,0,0,0.12);
  --tab-active:   #07C160;
  --tab-inactive: #8E8E93;
  --input-bg:     #FFFFFF;
  --msg-bg:       #EDEDED;
  --radius-av:    6px;
  --radius-bub:   10px;
  --tab-h:        54px;
  --top-h:        48px;
  --font:         -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif;
}

html, body {
  height: 100%;
  background: #EDEDED;
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

#app {
  position: relative;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background: var(--list-bg);
  overflow: hidden;
  box-shadow: 0 0 40px rgba(0,0,0,0.18);
}

/* ── Screens ──────────────────────────────────────────────────────────────── */
.screen {
  position: absolute;
  top: 0; left: 0; right: 0;
  bottom: var(--tab-h);
  display: flex;
  flex-direction: column;
  background: var(--list-bg);
  overflow: hidden;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1;
}
.screen.active { transform: translateX(0); z-index: 2; }
.screen.behind { transform: translateX(-30%); z-index: 1; }

#screen-chat  { bottom: 0; z-index: 10; }
#screen-search { bottom: 0; z-index: 20; }   /* covers tab bar + above other screens */

/* ── Search screen ──────────────────────────────────────────────────────────── */
.search-top-bar {
  display: flex; align-items: center;
  padding: 8px 12px; gap: 8px;
  background: var(--header-bg);
  border-bottom: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.search-field {
  flex: 1; display: flex; align-items: center;
  background: var(--input-bg); border-radius: 8px;
  padding: 0 10px; gap: 6px; height: 34px;
}
.search-field-icon { width: 15px; height: 15px; color: var(--text-sub); flex-shrink: 0; }
#search-input {
  flex: 1; border: none; background: transparent;
  font-size: 15px; outline: none; color: var(--text-main);
  font-family: var(--font);
}
#search-input::placeholder { color: var(--text-sub); }
.search-cancel-btn {
  background: none; border: none;
  color: var(--green); font-size: 15px;
  padding: 4px 0; cursor: pointer; white-space: nowrap;
  font-family: var(--font);
}

#search-results-wrap {
  flex: 1; overflow-y: auto; background: var(--list-bg);
}
#search-results-wrap.hidden { display: none; }
.search-placeholder {
  text-align: center; margin-top: 60px;
  font-size: 14px; color: var(--text-sub);
}
.search-result-item {
  display: flex; align-items: flex-start;
  padding: 12px 16px; gap: 12px;
  border-bottom: 0.5px solid var(--sep);
  cursor: pointer; background: var(--item-bg);
}
.search-result-item:active { background: #e5e5e5; }
.search-result-body { flex: 1; min-width: 0; }
.search-result-meta {
  display: flex; align-items: baseline; gap: 4px; margin-bottom: 3px;
}
.search-result-name { font-size: 15px; font-weight: 500; color: var(--text-main); }
.search-result-chan  { font-size: 12px; color: var(--text-sub); flex: 1; padding-left: 4px; }
.search-result-time  { font-size: 12px; color: var(--text-time); white-space: nowrap; }
.search-result-text  {
  font-size: 13px; color: var(--text-sub);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.search-result-text mark { background: none; color: var(--green); font-weight: 600; }

/* Context view */
#search-context-wrap {
  display: flex; flex-direction: column; flex: 1; overflow: hidden;
}
#search-context-wrap.hidden { display: none; }
.search-context-bar {
  display: flex; align-items: center;
  height: var(--top-h); flex-shrink: 0;
  background: var(--header-bg);
  border-bottom: 0.5px solid var(--sep);
}
.search-context-bar span {
  font-size: 17px; font-weight: 600; color: var(--header-text);
}
#context-messages {
  flex: 1; overflow-y: auto;
  padding: 8px 0 20px;
  background: var(--msg-bg);
}
.msg-row.ctx-hit .msg-bubble {
  outline: 2px solid var(--green); outline-offset: 2px;
}
#screen-chat.active   { z-index: 30; }
#screen-search.active { z-index: 30; }

/* ── Top bar (list / contacts / me) ───────────────────────────────────────── */
.top-bar {
  height: var(--top-h);
  background: var(--header-bg);
  border-bottom: 0.5px solid var(--sep);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 8px;
  flex-shrink: 0;
}
.top-bar-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--header-text);
  flex: 1;
}
.top-bar-actions { display: flex; gap: 4px; }
.top-bar-btn {
  width: 36px; height: 36px;
  background: none; border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--header-text); cursor: pointer;
  border-radius: 6px;
}
.top-bar-btn svg { width: 20px; height: 20px; }
.top-bar-btn:active { background: rgba(0,0,0,0.08); }

/* ── Chat list ─────────────────────────────────────────────────────────────── */
#chat-list {
  flex: 1;
  overflow-y: auto;
  background: var(--list-bg);
}
.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--item-bg);
  border-bottom: 0.5px solid var(--sep);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.chat-item:active { background: #D9D9D9; }

.chat-avatar {
  width: 48px; height: 48px;
  border-radius: var(--radius-av);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 600; color: #fff;
  letter-spacing: -0.5px;
}
.chat-avatar.group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  padding: 4px;
  background: #C8C8C8;
  font-size: 0;
}
.chat-avatar.group .mini-av {
  border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 600; color: #fff;
}

.chat-item-center {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.chat-item-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item-preview {
  font-size: 13.5px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
  flex-shrink: 0;
}
.chat-item-time {
  font-size: 12px;
  color: var(--text-time);
  white-space: nowrap;
}
.chat-badge {
  min-width: 18px; height: 18px;
  background: var(--badge);
  border-radius: 9px;
  color: #fff;
  font-size: 11px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  padding: 0 5px;
}
.chat-badge.hidden { visibility: hidden; }

/* ── Chat top bar ────────────────────────────────────────────────────────── */
.chat-top-bar {
  height: var(--top-h);
  background: var(--header-bg);
  border-bottom: 0.5px solid var(--sep);
  display: flex;
  align-items: center;
  padding: 0 4px 0 0;
  flex-shrink: 0;
}
.back-btn {
  width: 44px; height: var(--top-h);
  background: none; border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--green); cursor: pointer;
}
.back-btn svg { width: 22px; height: 22px; }
.back-btn:active { opacity: 0.5; }

.chat-top-center {
  flex: 1;
  display: flex; flex-direction: column; align-items: center;
  gap: 1px;
}
.chat-top-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
}
.chat-top-sub {
  font-size: 11px;
  color: var(--text-sub);
}
.chat-top-more {
  width: 44px; height: var(--top-h);
  background: none; border: none;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-main); cursor: pointer;
}
.chat-top-more svg { width: 22px; height: 22px; }

/* ── Messages area ────────────────────────────────────────────────────────── */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 0 8px;
  background: var(--msg-bg);
  display: flex;
  flex-direction: column;
}

.time-divider {
  text-align: center;
  font-size: 12px;
  color: var(--text-time);
  padding: 6px 0 10px;
  user-select: none;
}

.msg-system {
  text-align: center;
  font-size: 12px;
  color: var(--text-time);
  background: rgba(0,0,0,0.06);
  border-radius: 10px;
  padding: 3px 10px;
  margin: 4px auto;
  max-width: 70%;
}

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 2px 12px;
}
.msg-row.self { flex-direction: row-reverse; }
.msg-row.new-sender { margin-top: 8px; }

.msg-av {
  width: 40px; height: 40px;
  border-radius: var(--radius-av);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600; color: #fff;
}
.msg-av.spacer { visibility: hidden; }

.msg-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  max-width: calc(100% - 60px);
}
.msg-row.self .msg-content { align-items: flex-end; }

.msg-name {
  font-size: 12px;
  color: var(--text-sub);
  padding: 0 4px;
}
.msg-row.self .msg-name { display: none; }

.msg-bubble {
  background: var(--bubble-other);
  color: var(--text-main);
  border-radius: var(--radius-bub);
  border-top-left-radius: 2px;
  padding: 8px 12px;
  font-size: 15px;
  line-height: 1.45;
  word-break: break-word;
  max-width: 100%;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.msg-row.self .msg-bubble {
  background: var(--bubble-self);
  border-top-left-radius: var(--radius-bub);
  border-top-right-radius: 2px;
}

/* ── Input bar ───────────────────────────────────────────────────────────── */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--header-bg);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.input-icon-btn {
  width: 36px; height: 36px;
  background: none; border: none;
  display: flex; align-items: center; justify-content: center;
  color: #555; cursor: pointer; flex-shrink: 0;
  border-radius: 6px;
}
.input-icon-btn svg { width: 24px; height: 24px; }
.input-icon-btn:active { background: rgba(0,0,0,0.08); }
.input-icon-btn.hidden { display: none; }

.input-field-wrap {
  flex: 1;
  background: var(--input-bg);
  border-radius: 20px;
  padding: 7px 14px;
  border: 0.5px solid rgba(0,0,0,0.12);
}
#msg-input {
  width: 100%;
  border: none; outline: none; background: none;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  color: var(--text-main);
  resize: none;
  max-height: 100px;
  overflow-y: auto;
}

.send-btn {
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  height: 36px;
  white-space: nowrap;
}
.send-btn:active { background: var(--green-dark); }
.send-btn.hidden { display: none; }

/* ── Contacts ────────────────────────────────────────────────────────────── */
#contacts-list {
  flex: 1;
  overflow-y: auto;
}
.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--item-bg);
  border-bottom: 0.5px solid var(--sep);
  cursor: pointer;
}
.contact-item:active { background: #D9D9D9; }
.contact-av {
  width: 44px; height: 44px;
  border-radius: var(--radius-av);
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; font-weight: 600; color: #fff;
}
.contact-info { flex: 1; }
.contact-name { font-size: 16px; font-weight: 500; color: var(--text-main); }
.contact-status { font-size: 13px; color: var(--text-sub); margin-top: 2px; }
.presence-badge {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #ccc;
  flex-shrink: 0;
}
.presence-badge.active    { background: var(--green); }
.presence-badge.listening { background: #FFD60A; }
.presence-badge.absent    { background: #ccc; }
.presence-badge.asleep    { background: #555; }

/* ── Me screen ──────────────────────────────────────────────────────────── */
#screen-me { background: var(--list-bg); }

.me-profile-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 20px 16px 18px;
  background: var(--item-bg);
  border-bottom: 0.5px solid var(--sep);
}
.me-avatar {
  width: 60px; height: 60px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; font-weight: 600; color: #fff;
  flex-shrink: 0;
}
.me-info { flex: 1; }
.me-name { font-size: 20px; font-weight: 600; color: var(--text-main); }
.me-id   { font-size: 13px; color: var(--text-sub); margin-top: 4px; }

.settings-group {
  margin-top: 10px;
  background: var(--item-bg);
  border-top: 0.5px solid var(--sep);
  border-bottom: 0.5px solid var(--sep);
}
.settings-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 0.5px solid var(--sep);
}
.settings-item:last-child { border-bottom: none; }
.settings-label {
  font-size: 16px;
  color: var(--text-main);
  flex-shrink: 0;
  min-width: 70px;
}
.settings-input {
  flex: 1;
  border: none; outline: none;
  font-family: var(--font);
  font-size: 15px;
  color: var(--text-main);
  background: none;
}
.settings-save {
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
}
.conn-status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-sub);
  font-size: 14px;
}

/* Save full-width button */
.settings-save-full {
  display: block;
  width: calc(100% - 32px);
  margin: 14px 16px 0;
  padding: 12px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-family: var(--font);
  cursor: pointer;
}

/* Avatar color presets */
.color-presets {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.color-dot {
  width: 28px; height: 28px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  flex-shrink: 0;
}
.color-dot.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--green); }

/* Toggle switch (mute) */
.toggle-switch { position: relative; display: inline-flex; align-items: center; cursor: pointer; }
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track {
  width: 48px; height: 28px; background: #ccc; border-radius: 14px;
  position: relative; transition: background 0.2s;
}
.toggle-switch input:checked ~ .toggle-track { background: var(--green); }
.toggle-thumb {
  position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; background: #fff; border-radius: 50%;
  transition: left 0.2s;
}
.toggle-switch input:checked ~ .toggle-track .toggle-thumb { left: 23px; }

/* Me screen avatar image support */
.me-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; }
.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #ccc;
  flex-shrink: 0;
}
.status-dot.connected    { background: var(--green); }
.status-dot.reconnecting { background: #FFD60A; }
.status-dot.disconnected { background: #FA3434; }

/* ── Tab bar ─────────────────────────────────────────────────────────────── */
.tab-bar {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: var(--tab-h);
  background: var(--tab-bg);
  border-top: 0.5px solid var(--tab-border);
  display: flex;
  align-items: stretch;
  z-index: 20;
  padding-bottom: env(safe-area-inset-bottom);
}
#screen-chat.active ~ .tab-bar,
#screen-search.active ~ .tab-bar { display: none; }
.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--tab-inactive);
  position: relative;
  -webkit-tap-highlight-color: transparent;
}
.tab-item svg { width: 24px; height: 24px; }
.tab-item span { font-size: 10px; }
.tab-item.active { color: var(--tab-active); }
.tab-item:active { opacity: 0.6; }

.tab-badge {
  position: absolute;
  top: 4px; right: calc(50% - 20px);
  min-width: 16px; height: 16px;
  background: var(--badge);
  border-radius: 8px;
  color: #fff;
  font-size: 10px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  padding: 0 4px;
  border: 2px solid var(--tab-bg);
}
.tab-badge.hidden { display: none; }

/* ── System panel ────────────────────────────────────────────────────────── */
.sys-top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
}
.top-bar-icon-btn {
  background: none; border: none; padding: 0; cursor: pointer;
  color: var(--text-sub); display: flex; align-items: center;
}
.top-bar-title { font-size: 17px; font-weight: 600; color: var(--text-main); }

.sys-subtabs {
  display: flex; background: var(--item-bg);
  border-bottom: 0.5px solid var(--sep); flex-shrink: 0;
}
.sys-subtab {
  flex: 1; padding: 10px 0; background: none; border: none;
  border-bottom: 2px solid transparent; font-size: 13px;
  color: var(--text-sub); cursor: pointer; font-family: var(--font);
}
.sys-subtab.active { color: var(--green); border-bottom-color: var(--green); font-weight: 500; }

.sys-panel { flex: 1; overflow-y: auto; background: var(--list-bg); }
.sys-panel.hidden { display: none; }

.sys-agent-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--item-bg);
  border-bottom: 0.5px solid var(--sep);
}
.sys-agent-info { flex: 1; min-width: 0; }
.sys-agent-name { font-size: 15px; font-weight: 500; color: var(--text-main); }
.sys-agent-meta { font-size: 12px; color: var(--text-sub); margin-top: 2px; }
.sys-presence-badge {
  font-size: 11px; font-weight: 600; padding: 2px 8px;
  border-radius: 10px; flex-shrink: 0;
}
.sys-presence-badge.active    { background: #E8F9EE; color: #07C160; }
.sys-presence-badge.listening { background: #FFF8E1; color: #B8860B; }
.sys-presence-badge.absent    { background: #F0F0F0; color: #888; }
.sys-presence-badge.asleep    { background: #EBEBEB; color: #999; }

.sys-section-hdr {
  padding: 8px 16px; font-size: 12px; color: var(--text-sub);
  font-weight: 500; letter-spacing: 0.3px; background: var(--list-bg);
}
.sys-stat-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px; border-bottom: 0.5px solid var(--sep);
  background: var(--item-bg); font-size: 14px;
}
.sys-stat-label { color: var(--text-sub); }
.sys-stat-value { color: var(--text-main); font-weight: 500; }

.sys-mem-item { background: var(--item-bg); border-bottom: 0.5px solid var(--sep); cursor: pointer; }
.sys-mem-header { display: flex; align-items: center; gap: 8px; padding: 11px 16px; }
.sys-mem-id { font-size: 11px; color: var(--text-sub); flex-shrink: 0; }
.sys-mem-title {
  flex: 1; font-size: 14px; color: var(--text-main); min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sys-mem-date { font-size: 11px; color: var(--text-time); flex-shrink: 0; }
.sys-mem-tags { display: flex; gap: 4px; flex-wrap: wrap; padding: 0 16px 8px; }
.sys-mem-tag { font-size: 10px; color: var(--green); background: #E8F9EE; padding: 1px 7px; border-radius: 9px; }
.sys-mem-body {
  display: none; padding: 8px 16px 12px; font-size: 13px; color: var(--text-sub);
  line-height: 1.5; border-top: 0.5px solid var(--sep);
  white-space: pre-wrap; word-break: break-word;
}
.sys-mem-item.open .sys-mem-body { display: block; }

#screen-system {
  display: flex; flex-direction: column;
}

/* ── Scrollbars ──────────────────────────────────────────────────────────── */
#messages::-webkit-scrollbar,
#chat-list::-webkit-scrollbar,
#contacts-list::-webkit-scrollbar { width: 3px; }
#messages::-webkit-scrollbar-thumb,
#chat-list::-webkit-scrollbar-thumb,
#contacts-list::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15); border-radius: 2px;
}
