:root {
  --wx-green: #07c160;
  --wx-green-dark: #06ad56;
  --wx-bubble-green: #95ec69;
  --wx-bg: #ededed;
  --wx-header-bg: #ededed;
  --wx-white: #ffffff;
  --wx-text: #191919;
  --wx-text-secondary: #888888;
  --wx-text-light: #b2b2b2;
  --wx-border: #e2e2e2;
  --wx-red: #fa5151;
  --wx-blue: #576b95;
  --wx-search-bg: #f7f7f7;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  background: #d6d6d6;
  color: var(--wx-text);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== Phone frame ===== */
.phone {
  position: relative;
  width: min(420px, 100vw);
  height: min(860px, 100dvh);
  background: var(--wx-bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,.28);
}

@media (min-width: 480px) {
  .phone {
    border-radius: 38px;
    border: 9px solid #161616;
    height: min(820px, 92dvh);
  }
}

.status-bar {
  height: 44px;
  flex: 0 0 44px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 18px 8px;
  background: var(--wx-header-bg);
  color: var(--wx-text);
  font-size: 14px;
  font-weight: 600;
  z-index: 30;
}

.status-icons {
  display: flex;
  align-items: center;
  gap: 5px;
}

.view-root {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.phone.chat-open .tab-bar {
  display: none;
}

/* ===== Tab bar ===== */
.tab-bar {
  flex: 0 0 62px;
  display: flex;
  background: #f7f7f7;
  border-top: 1px solid var(--wx-border);
  z-index: 30;
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: #7b7b7b;
  font-size: 10px;
  cursor: pointer;
  padding: 5px 0 3px;
}

.tab-btn.is-active {
  color: var(--wx-green);
}

.tab-icon {
  display: flex;
}

/* ===== Common pages ===== */
.page {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.page-header {
  flex: 0 0 auto;
  height: 46px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 14px 7px;
  background: var(--wx-header-bg);
  position: relative;
  font-size: 17px;
  font-weight: 600;
}

.page-header .header-title {
  padding-bottom: 2px;
}

.header-right {
  position: absolute;
  right: 12px;
  bottom: 6px;
  display: flex;
  gap: 12px;
  align-items: center;
}

.header-right button {
  border: none;
  background: none;
  color: var(--wx-text);
  font-size: 14px;
  cursor: pointer;
  padding: 2px;
}

/* ===== Chat list ===== */
.chat-list-page .page-header {
  justify-content: flex-start;
  align-items: flex-end;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  background: var(--wx-white);
  -webkit-overflow-scrolling: touch;
}

.search-box {
  flex: 0 0 auto;
  padding: 8px 12px;
  background: var(--wx-white);
  border-bottom: 1px solid var(--wx-border);
}

.search-box input {
  width: 100%;
  height: 34px;
  border: none;
  border-radius: 6px;
  background: var(--wx-search-bg);
  font-size: 14px;
  padding: 0 12px;
  outline: none;
}

.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #f2f2f2;
}

.chat-row:hover {
  background: #f7f7f7;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 27px;
  flex: 0 0 48px;
  user-select: none;
  overflow: hidden;
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  display: block;
}

.avatar-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 20px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,.15);
}

.chat-row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.chat-row-top, .chat-row-bottom {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-row-name {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.chat-row-time {
  font-size: 11px;
  color: var(--wx-text-light);
  flex: 0 0 auto;
}

.chat-row-preview {
  font-size: 13px;
  color: var(--wx-text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.unread-badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--wx-red);
  color: #fff;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.list-empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--wx-text-secondary);
  font-size: 14px;
}

/* ===== Contacts ===== */
.contact-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #f2f2f2;
  background: var(--wx-white);
}

.contact-row:hover {
  background: #f7f7f7;
}

.contact-row .avatar {
  width: 42px;
  height: 42px;
  flex-basis: 42px;
  font-size: 23px;
  border-radius: 5px;
}

.contact-real {
  font-size: 12px;
  color: var(--wx-text-light);
  font-weight: 400;
  margin-left: 4px;
}

.contact-name {
  font-size: 16px;
}

.contact-sig {
  font-size: 12px;
  color: var(--wx-text-light);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-row-main {
  flex: 1;
  min-width: 0;
}

/* ===== Profile ===== */
.profile {
  background: var(--wx-white);
  flex: 1;
  overflow-y: auto;
}

.profile-head {
  padding: 30px 20px 26px;
  text-align: center;
}

.profile-head .avatar {
  width: 76px;
  height: 76px;
  font-size: 42px;
  border-radius: 10px;
  margin: 0 auto 14px;
}

.profile-name {
  font-size: 20px;
  font-weight: 600;
}

.profile-saved {
  margin-top: 8px;
  display: inline-block;
  background: #f0f0f0;
  color: var(--wx-text-secondary);
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 4px;
}

.profile-sig {
  margin-top: 12px;
  color: var(--wx-text-secondary);
  font-size: 14px;
}

.profile-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-top: 1px solid #f2f2f2;
  font-size: 15px;
}

.profile-row .row-label {
  color: var(--wx-text);
}

.profile-row .row-value {
  color: var(--wx-text-secondary);
  text-align: right;
  max-width: 65%;
}

.link-btn {
  border: none;
  background: none;
  color: var(--wx-blue);
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
}

.profile-actions {
  padding: 24px 18px 34px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.btn {
  height: 44px;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  background: #f2f2f2;
  color: var(--wx-text);
}

.btn-green {
  background: var(--wx-green);
  color: #fff;
}

.btn-green:active {
  background: var(--wx-green-dark);
}

.btn-danger {
  background: #fff;
  color: var(--wx-red);
  border: 1px solid #f2f2f2;
}

/* ===== Me / settings ===== */
.me-page {
  background: var(--wx-bg);
}

.me-profile {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--wx-white);
  padding: 18px 16px;
  margin-bottom: 10px;
}

.me-profile .avatar {
  width: 58px;
  height: 58px;
  font-size: 32px;
  border-radius: 8px;
}

.me-name {
  font-size: 18px;
  font-weight: 600;
}

.me-desc {
  font-size: 12px;
  color: var(--wx-text-secondary);
  margin-top: 3px;
}

.section {
  background: var(--wx-white);
  margin-bottom: 10px;
}

.section-title {
  padding: 12px 16px 4px;
  font-size: 13px;
  color: var(--wx-text-secondary);
}

.field {
  padding: 12px 16px;
  border-top: 1px solid #f2f2f2;
}

.field-label {
  font-size: 14px;
  margin-bottom: 7px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.field-label .hint {
  color: var(--wx-text-light);
  font-size: 12px;
  font-weight: 400;
}

.field input, .field select, .field textarea {
  width: 100%;
  border: none;
  background: var(--wx-search-bg);
  border-radius: 6px;
  padding: 9px 11px;
  font-size: 14px;
  color: var(--wx-text);
  outline: none;
  font-family: inherit;
}

.field input[type="checkbox"] {
  width: 18px;
  height: 18px;
  padding: 0;
  background: none;
  flex: 0 0 auto;
  accent-color: var(--wx-green);
}

.field input[type="range"] {
  background: transparent;
  padding: 0;
  height: auto;
  accent-color: var(--wx-green);
}

.field textarea {
  resize: vertical;
  min-height: 60px;
  line-height: 1.5;
}

.field select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='3'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.field-row {
  display: flex;
  gap: 10px;
}

.field-row .field {
  flex: 1;
  border-top: none;
  padding-top: 0;
  padding-bottom: 12px;
}

.api-key-wrap {
  position: relative;
}

.api-key-wrap input {
  padding-right: 58px;
}

.api-key-wrap .toggle-key {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: none;
  color: var(--wx-text-secondary);
  font-size: 12px;
  cursor: pointer;
  padding: 6px 8px;
}

.settings-actions {
  padding: 4px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.char-list {
  padding: 4px 0;
}

.char-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid #f2f2f2;
}

.char-row .avatar {
  width: 40px;
  height: 40px;
  flex-basis: 40px;
  font-size: 20px;
  border-radius: 5px;
}

.char-row-main {
  flex: 1;
  min-width: 0;
}

.char-row-name {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.char-row-sub {
  font-size: 12px;
  color: var(--wx-text-light);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.char-edit-btn {
  border: 1px solid var(--wx-border);
  background: #fff;
  color: var(--wx-text);
  font-size: 13px;
  padding: 5px 14px;
  border-radius: 4px;
  cursor: pointer;
}

.modal-input {
  width: 100%;
  border: 1px solid var(--wx-border);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  font-family: inherit;
  background: var(--wx-search-bg);
}

/* ===== 角色编辑器 ===== */
.char-editor {
  z-index: 55;
}

.editor-body {
  flex: 1;
  overflow-y: auto;
  background: var(--wx-bg);
  padding-bottom: 24px;
}

.editor-head {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--wx-white);
  padding: 16px;
}

.editor-avatar {
  width: 64px;
  height: 64px;
  flex-basis: 64px;
  font-size: 30px;
  border-radius: 8px;
}

.editor-head-text {
  font-size: 14px;
  color: var(--wx-text);
}

.editor-head-text .hint {
  color: var(--wx-text-light);
  font-size: 12px;
}

.avatar-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 14px 16px;
  background: var(--wx-white);
  border-top: 1px solid #f2f2f2;
}

.avatar-option {
  width: 52px;
  height: 52px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  background: #f2f2f2;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-option.is-selected {
  border-color: var(--wx-green);
}

.avatar-upload {
  font-size: 22px;
  color: var(--wx-text-secondary);
  border: 1px dashed var(--wx-border);
  background: var(--wx-search-bg);
}

.editor-actions {
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-note {
  padding: 0 16px 20px;
  font-size: 12px;
  color: var(--wx-text-light);
  line-height: 1.6;
}

.status-pill {
  display: inline-block;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 10px;
}

.status-pill.ok {
  background: rgba(7,193,96,.12);
  color: var(--wx-green-dark);
}

.status-pill.warn {
  background: rgba(250,81,81,.12);
  color: var(--wx-red);
}

.status-pill.mock {
  background: rgba(87,107,149,.12);
  color: var(--wx-blue);
}

/* ===== Chat window ===== */
.chat-page {
  position: absolute;
  inset: 0;
  background: var(--wx-bg);
  display: flex;
  flex-direction: column;
  z-index: 40;
  animation: slideIn .22s ease;
}

.chat-page.profile-overlay {
  z-index: 50;
}

@keyframes slideIn {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

.chat-header {
  flex: 0 0 46px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 10px 7px;
  background: var(--wx-header-bg);
  position: relative;
}

.back-btn {
  position: absolute;
  left: 4px;
  bottom: 4px;
  border: none;
  background: none;
  padding: 6px;
  cursor: pointer;
  color: var(--wx-text);
  display: flex;
}

.chat-header .chat-title {
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  padding-bottom: 2px;
}

.chat-more {
  position: absolute;
  right: 8px;
  bottom: 5px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--wx-text);
  padding: 5px;
  display: flex;
}

.msg-area {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 20px;
  -webkit-overflow-scrolling: touch;
  background-image: radial-gradient(circle, rgba(0,0,0,.035) 1px, transparent 1px);
  background-size: 22px 22px;
}

.day-divider {
  text-align: center;
  font-size: 12px;
  color: var(--wx-text-secondary);
  margin: 12px 0;
}

.day-divider span {
  background: #d9d9d9;
  padding: 2px 10px;
  border-radius: 3px;
  color: #fff;
}

.msg {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  align-items: flex-start;
}

.msg .avatar {
  width: 40px;
  height: 40px;
  flex-basis: 40px;
  font-size: 22px;
  border-radius: 5px;
  margin-top: 2px;
}

.msg-body {
  max-width: 68%;
  display: flex;
  flex-direction: column;
}

.msg-bubble {
  padding: 9px 12px;
  border-radius: 4px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  position: relative;
  box-shadow: 0 1px 1px rgba(0,0,0,.04);
}

.msg.from-me {
  flex-direction: row-reverse;
}

.msg.from-me .msg-bubble {
  background: var(--wx-bubble-green);
}

.msg.from-them .msg-bubble {
  background: var(--wx-white);
}

.msg-bubble .msg-time {
  display: block;
  text-align: right;
  font-size: 10px;
  color: rgba(0,0,0,.35);
  margin-top: 4px;
}

.msg .tail {
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  margin-top: 9px;
}

.msg.from-them .tail {
  border-right: 8px solid var(--wx-white);
  margin-left: -16px;
}

.msg.from-me .tail {
  border-left: 8px solid var(--wx-bubble-green);
  margin-right: -16px;
}

.typing-bubble {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
}

.typing-bubble i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #9a9a9a;
  display: inline-block;
  animation: blink 1.2s infinite;
}

.typing-bubble i:nth-child(2) { animation-delay: .2s; }
.typing-bubble i:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 60%, 100% { opacity: .25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.sys-msg {
  text-align: center;
  font-size: 12px;
  color: var(--wx-text-secondary);
  margin: 10px 0 18px;
}

.sys-msg .sys-bubble {
  display: inline-block;
  background: #dcdcdc;
  color: #666;
  border-radius: 3px;
  padding: 3px 8px;
  max-width: 90%;
}

.input-bar {
  flex: 0 0 auto;
  background: #f7f7f7;
  border-top: 1px solid var(--wx-border);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input-bar .icon-btn {
  border: none;
  background: none;
  cursor: pointer;
  color: #5a5a5a;
  padding: 3px;
  flex: 0 0 auto;
  display: flex;
}

.input-bar textarea {
  flex: 1;
  border: none;
  border-radius: 6px;
  background: var(--wx-white);
  font-size: 15px;
  padding: 9px 11px;
  outline: none;
  resize: none;
  max-height: 90px;
  line-height: 1.4;
  font-family: inherit;
  min-height: 36px;
}

.send-btn {
  flex: 0 0 auto;
  height: 36px;
  padding: 0 16px;
  border: none;
  border-radius: 6px;
  background: var(--wx-green);
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  display: none;
}

.send-btn.is-visible {
  display: block;
}

/* ===== Modal ===== */
.modal-mask {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal-sheet {
  background: var(--wx-white);
  width: 100%;
  border-radius: 12px 12px 0 0;
  padding: 8px 0 calc(14px + env(safe-area-inset-bottom));
  animation: sheetUp .2s ease;
}

@keyframes sheetUp {
  from { transform: translateY(40px); opacity: .6; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-item {
  padding: 15px 18px;
  font-size: 16px;
  text-align: center;
  border-bottom: 1px solid #f2f2f2;
  cursor: pointer;
  background: none;
  width: 100%;
}

.sheet-item:last-child {
  border-bottom: none;
}

.sheet-item.danger {
  color: var(--wx-red);
}

.sheet-cancel {
  margin-top: 8px;
  border-top: 1px solid #f2f2f2;
  padding-top: 8px;
}

.sheet-title {
  text-align: center;
  color: var(--wx-text-secondary);
  font-size: 13px;
  padding: 8px 18px 4px;
}

/* ===== Toast ===== */
.toast {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,.72);
  color: #fff;
  font-size: 14px;
  padding: 12px 20px;
  border-radius: 8px;
  z-index: 80;
  max-width: 82%;
  text-align: center;
  animation: fadeIn .18s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.spin {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: rotate .7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}

@keyframes rotate {
  to { transform: rotate(360deg); }
}

/* Scrollbar subtlety */
.chat-list::-webkit-scrollbar, .msg-area::-webkit-scrollbar, .profile::-webkit-scrollbar, .me-page::-webkit-scrollbar {
  width: 0;
}
