:root {
  --sidebar-width: 26vw;
  --window-bar-height: 30px;
  --header-height: 60px;
  --telegram-blue: #3390ec;
  --telegram-blue-dark: #2b82d9;
  --text: #0f1419;
  --muted: #999da3;
  --line: #e9e9e9;
  --incoming: #ffffff;
  --outgoing: #eeffde;
  --green-check: #62b93c;
  --time-in: #a1aab3;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: #cfd9e0;
  color: var(--text);
  font-family: "Segoe UI", "Helvetica Neue", "Microsoft YaHei", "PingFang SC", system-ui, Arial, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button, input, textarea { font: inherit; }
button { color: inherit; }
.svg-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Window shell ---------- */
.telegram-window {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #fff;
}
.window-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex: 0 0 var(--window-bar-height);
  padding-left: 12px;
  background: #f4f4f5;
  border-bottom: 1px solid #e7e7e7;
  color: #9aa0a6;
  user-select: none;
}
.window-bar__title { margin-right: auto; font-size: 12px; opacity: 0; }
.window-controls { display: flex; align-self: stretch; }
.window-controls button {
  display: grid; place-items: center;
  width: 46px; height: 100%;
  padding: 0; border: 0; background: transparent; color: #85898e; cursor: default;
}
.window-controls button:hover { background: #e6e6e7; }
.window-controls .window-close:hover { background: #e81123; color: #fff; }
.window-controls svg { width: 15px; height: 15px; stroke-width: 1.4; }

.telegram-app {
  display: grid;
  grid-template-columns: minmax(280px, min(var(--sidebar-width), 26rem)) minmax(0, 1fr);
  flex: 1 1 auto;
  min-height: 0;
}

/* ---------- Sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-right: 1px solid #e6e6e6;
  background: #fff;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 var(--header-height);
  padding: 0 14px 0 12px;
}
.menu-holder { position: relative; flex: 0 0 auto; }
.toolbar-button {
  display: grid; place-items: center;
  width: 44px; height: 44px;
  padding: 0; border: 0; border-radius: 50%;
  background: transparent; color: #707579; cursor: pointer;
  transition: background .15s, color .15s;
}
.toolbar-button:hover { background: #f1f1f1; }
.menu-button svg { width: 26px; height: 26px; stroke-width: 1.7; }
.menu-button i {
  position: absolute; top: 9px; right: 6px;
  width: 8px; height: 8px;
  border: 2px solid #fff; border-radius: 50%; background: #4fae4e;
}

.side-menu {
  position: absolute; z-index: 30; top: 52px; left: 4px;
  width: 240px; padding: 6px;
  border-radius: 10px; background: #fff;
  box-shadow: 0 4px 24px rgba(0,0,0,.18);
  opacity: 0; visibility: hidden; transform: translateY(-8px) scale(.98);
  transform-origin: top left; transition: .16s;
}
.side-menu.open { opacity: 1; visibility: visible; transform: none; }
.side-menu__account {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 10px 12px; margin-bottom: 4px;
  border-bottom: 1px solid #eee;
}
.side-menu__account > span:last-child { display: flex; flex-direction: column; }
.side-menu__account strong { font-size: 14px; }
.side-menu__account small { color: var(--telegram-blue); font-size: 13px; }
.account-avatar {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(145deg,#72d5fd,#2a9ef1); color: #fff; font-weight: 600;
}
.side-menu > button {
  display: block; width: 100%;
  padding: 10px 12px; border: 0; border-radius: 8px;
  background: transparent; text-align: left; font-size: 14px; cursor: pointer;
}
.side-menu > button:hover { background: #f3f4f6; }
.side-menu__version { padding: 8px 12px 4px; color: #b6b6b6; font-size: 11px; }

.global-search {
  display: flex; flex: 1; align-items: center; gap: 8px;
  min-width: 0; height: 42px; padding: 0 12px;
  border-radius: 22px; background: #f4f4f5; color: #93999f;
}
.global-search > svg { flex: 0 0 auto; width: 21px; height: 21px; stroke-width: 1.9; }
.global-search input {
  width: 100%; min-width: 0; height: 100%;
  border: 0; outline: 0; background: transparent; color: #222; font-size: 15px;
}
.global-search input::placeholder { color: #93999f; opacity: 1; }
.global-search button {
  display: none; flex: 0 0 auto;
  width: 26px; height: 26px; padding: 0; place-items: center;
  border: 0; border-radius: 50%; background: transparent; color: #a6a6a6; cursor: pointer;
}
.global-search button svg { width: 18px; height: 18px; }
.global-search.has-value button { display: grid; }

.chat-list {
  flex: 1 1 auto; min-height: 0;
  padding: 4px 6px;
  overflow-y: auto;
  scrollbar-width: thin; scrollbar-color: transparent transparent;
}
.chat-list:hover { scrollbar-color: #cfcfcf transparent; }
.chat-list::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb { border-radius: 4px; background: transparent; }
.chat-list:hover::-webkit-scrollbar-thumb { background: #cfcfcf; }

.chat-card {
  position: relative;
  display: flex; align-items: center; gap: 11px;
  padding: 8px 10px;
  border-radius: 12px; cursor: pointer; user-select: none;
}
.chat-card:hover { background: #f4f4f5; }
.chat-card.active { background: var(--telegram-blue); color: #fff; }
.chat-avatar {
  position: relative;
  display: grid; place-items: center;
  flex: 0 0 54px; width: 54px; height: 54px;
  overflow: hidden; border-radius: 50%;
  color: #fff; font-size: 21px; font-weight: 500;
}
.chat-avatar--river { background: linear-gradient(145deg,#5cc1ff,#3d7bd4); font-size: 23px; }
.chat-avatar--river::after { content: "江"; }
.chat-avatar--reynard { background: #315a68 url("reynard-avatar.svg") center/cover no-repeat; }
.chat-avatar--telegram { background: linear-gradient(145deg,#40b3e8,#0f8ac9); }
.chat-avatar--telegram::after {
  content: "";
  width: 30px; height: 26px;
  background: #fff;
  -webkit-mask: no-repeat center/contain;
  mask: no-repeat center/contain;
  clip-path: polygon(96% 6%, 78% 96%, 45% 62%, 8% 48%);
  transform: translateX(-1px);
}
.chat-avatar--orange { background: linear-gradient(145deg,#ffca6b,#f7772f); font-size: 20px; }
.chat-card__body { flex: 1; min-width: 0; }
.chat-card__top, .chat-card__bottom { display: flex; align-items: center; gap: 6px; min-width: 0; }
.chat-card__top { margin-bottom: 3px; }
.chat-card__name {
  display: flex; flex: 1; align-items: center; gap: 5px;
  min-width: 0; overflow: hidden;
  font-size: 15px; font-weight: 600;
  white-space: nowrap; text-overflow: ellipsis;
}
.chat-card__name > span { overflow: hidden; text-overflow: ellipsis; }
.chat-card__name svg { flex: 0 0 auto; width: 17px; height: 17px; }
.chat-card__name .group-icon { width: 17px; height: 17px; color: #a7abb0; }
.chat-card.active .chat-card__name .group-icon { color: rgba(255,255,255,.85); }
.chat-card__time { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 3px; color: #a1a5ab; font-size: 13px; }
.chat-card__time svg { width: 18px; height: 18px; color: var(--green-check); stroke-width: 2; }
.chat-card__preview { flex: 1; overflow: hidden; color: #8b9096; font-size: 14px; line-height: 1.3; white-space: nowrap; text-overflow: ellipsis; }
.chat-card__preview strong { color: #4ea1e0; font-weight: 400; }
.chat-card.active .chat-card__time,
.chat-card.active .chat-card__time svg,
.chat-card.active .chat-card__preview,
.chat-card.active .chat-card__preview strong { color: rgba(255,255,255,.92); }
.verified-icon { flex: 0 0 auto; color: #3ba7e8; fill: #3ba7e8; stroke: #fff; stroke-width: 1.6; }
.chat-card.active .verified-icon { color: #fff; fill: #fff; stroke: var(--telegram-blue); }
.empty-chats { padding: 60px 20px; color: #b0b0b0; font-size: 15px; text-align: center; }

/* ---------- Conversation ---------- */
.conversation {
  position: relative;
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  overflow: hidden;
}
.conversation-header {
  position: relative; z-index: 6;
  display: flex; align-items: center; gap: 12px;
  flex: 0 0 var(--header-height);
  padding: 0 18px;
  background: #fff; border-bottom: 1px solid #e9e9e9;
}
.conversation-contact {
  display: flex; flex: 1; flex-direction: column; align-items: flex-start; gap: 2px;
  min-width: 0; padding: 0; border: 0; background: transparent; cursor: pointer; text-align: left;
}
.conversation-contact strong { max-width: 100%; overflow: hidden; font-size: 16px; font-weight: 600; white-space: nowrap; text-overflow: ellipsis; }
.conversation-contact small { max-width: 100%; overflow: hidden; color: #909499; font-size: 14px; white-space: nowrap; text-overflow: ellipsis; }
.conversation-actions { display: flex; align-items: center; gap: 4px; }
.conversation-actions .toolbar-button { width: 42px; height: 42px; }
.conversation-actions svg { width: 23px; height: 23px; stroke-width: 1.7; }
.mobile-back { display: none; }

.message-search {
  position: absolute; inset: 9px 12px;
  display: flex; align-items: center; gap: 10px;
  padding: 0 8px 0 16px;
  border-radius: 10px; background: #fff; box-shadow: 0 2px 14px rgba(0,0,0,.14);
  opacity: 0; visibility: hidden; transform: translateY(-6px); transition: .15s;
}
.message-search.open { opacity: 1; visibility: visible; transform: none; }
.message-search > svg { flex: 0 0 auto; color: #909499; }
.message-search input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; color: #222; font-size: 15px; }
.message-search span { flex: 0 0 auto; color: #999; font-size: 13px; white-space: nowrap; }
.message-search button { display: grid; flex: 0 0 auto; width: 34px; height: 34px; padding: 0; place-items: center; border: 0; border-radius: 50%; background: transparent; color: #909499; cursor: pointer; }
.message-search button:hover { background: #f1f1f1; }

/* ---------- Message canvas ---------- */
.chat-canvas {
  position: relative;
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto;
  background-color: #97b78a;
  background-image:
    linear-gradient(160deg, rgba(224,222,120,.55) 0%, rgba(120,186,150,.5) 45%, rgba(150,196,132,.45) 75%, rgba(222,222,140,.55) 100%),
    url("wallpaper-pattern.svg");
  background-size: cover, 420px 420px;
  background-attachment: local;
  scrollbar-width: thin; scrollbar-color: rgba(0,0,0,.18) transparent;
}
.chat-canvas::-webkit-scrollbar { width: 7px; }
.chat-canvas::-webkit-scrollbar-thumb { border-radius: 4px; background: rgba(0,0,0,.16); }

.message-stream {
  display: flex; flex-direction: column;
  max-width: 920px;
  min-height: 100%;
  margin: 0 auto;
  padding: 14px 14px 20px;
}
.message-stream > * { flex: 0 0 auto; }

.message-row { display: flex; margin-top: 2px; animation: bubble-in .16s ease both; }
.message-row.out { justify-content: flex-end; }
.message-row.gap { margin-top: 10px; }
@keyframes bubble-in { from { opacity: 0; transform: translateY(4px); } }

.bubble {
  position: relative;
  max-width: 68%;
  padding: 6px 10px 7px 12px;
  border-radius: 12px;
  background: var(--incoming);
  box-shadow: 0 1px 1px rgba(0,0,0,.12);
  color: #16191c; font-size: 15px; line-height: 1.38;
  overflow-wrap: break-word; word-break: break-word;
}
.message-row.out .bubble { background: var(--outgoing); }
/* tails */
.message-row.tail-in .bubble { border-bottom-left-radius: 4px; }
.message-row.tail-out .bubble { border-bottom-right-radius: 4px; }
.message-row.tail-in .bubble::before,
.message-row.tail-out .bubble::before {
  content: ""; position: absolute; bottom: 0; width: 11px; height: 16px;
}
.message-row.tail-in .bubble::before {
  left: -8px;
  background: radial-gradient(circle at top left, transparent 11px, var(--incoming) 11px) bottom left / 11px 16px no-repeat;
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
  background: var(--incoming);
  clip-path: path("M11 16 C6 16 2 13 0 9 L0 16 Z");
  left: -6px;
}
.message-row.tail-out .bubble::before {
  right: -6px;
  background: var(--outgoing);
  clip-path: path("M0 16 C5 16 9 13 11 9 L11 16 Z");
}

.bubble-body { display: inline; }
.bubble-text { white-space: pre-wrap; }
.bubble-time {
  float: right;
  display: inline-flex; align-items: center; gap: 3px;
  margin: 8px 0 -2px 10px;
  color: var(--time-in); font-size: 12px; white-space: nowrap;
  position: relative; top: 4px;
}
.message-row.out .bubble-time { color: #5eb147; }
.bubble-time svg { width: 16px; height: 14px; stroke-width: 2.2; }
/* short single-line messages: keep time on the same line */
.bubble.compact { white-space: nowrap; }
.bubble.compact .bubble-time { float: none; margin: 0 0 0 10px; top: 0; vertical-align: baseline; }

/* Receipt / transaction card */
.bubble.receipt {
  max-width: 460px; width: 460px;
  padding: 14px 18px 30px;
  color: #4a5560; font-size: 13.5px; line-height: 1.5;
}
.bubble.receipt dl { margin: 0; }
.receipt-line { display: flex; gap: 8px; margin-bottom: 9px; }
.receipt-line:last-child { margin-bottom: 0; }
.receipt-line dt { flex: 0 0 auto; margin: 0; color: #838d97; }
.receipt-line dd { margin: 0; font-weight: 500; color: #3b444d; overflow-wrap: anywhere; }
.receipt-line.is-spaced { margin-top: 16px; }
.receipt-line.is-empty { min-height: 8px; }
.bubble.receipt .bubble-time {
  float: none; position: absolute; right: 12px; bottom: 8px;
  margin: 0; padding: 2px 10px; border-radius: 14px;
  background: rgba(0,0,0,.32); color: #fff; font-size: 12px; top: auto;
}

.date-divider {
  align-self: center;
  margin: 12px auto 6px;
  padding: 4px 12px;
  border-radius: 16px;
  background: rgba(43,68,52,.5);
  color: #fff; font-size: 13px; font-weight: 500;
  backdrop-filter: blur(2px);
}
.bubble.highlight { box-shadow: 0 0 0 3px rgba(255,205,40,.85), 0 1px 1px rgba(0,0,0,.12); }

/* ---------- Composer ---------- */
.composer {
  position: relative; z-index: 7;
  display: flex; align-items: flex-end; gap: 6px;
  flex: 0 0 auto;
  padding: 8px 16px 12px;
  background: #fff; border-top: 1px solid #e9e9e9;
}
.composer-button {
  display: grid; place-items: center;
  flex: 0 0 44px; width: 44px; height: 44px;
  padding: 0; border: 0; border-radius: 50%;
  background: transparent; color: #8a9096; cursor: pointer;
}
.composer-button:hover { background: #f1f1f1; color: #707579; }
.composer-button svg { width: 25px; height: 25px; stroke-width: 1.75; }
.composer textarea {
  flex: 1; min-width: 0; max-height: 120px; height: 44px;
  padding: 11px 6px; border: 0; outline: 0; resize: none;
  background: transparent; color: #1c1c1c; font-size: 15px; line-height: 1.4;
}
.composer textarea::placeholder { color: #a2a7ac; opacity: 1; }
.emoji-holder { position: relative; flex: 0 0 auto; }
.send-button {
  display: none; place-items: center;
  flex: 0 0 44px; width: 44px; height: 44px;
  padding: 0; border: 0; border-radius: 50%;
  background: var(--telegram-blue); color: #fff; cursor: pointer;
  transition: background .15s;
}
.send-button:hover { background: var(--telegram-blue-dark); }
.send-button svg { width: 23px; height: 23px; transform: translate(-1px,1px); }
.composer.has-text .send-button { display: grid; }
.composer.has-text .mic-button { display: none; }

.emoji-panel {
  position: absolute; right: 0; bottom: 54px;
  display: grid; grid-template-columns: repeat(6, 38px); gap: 2px;
  padding: 8px; border-radius: 10px; background: #fff;
  box-shadow: 0 4px 24px rgba(0,0,0,.2);
  opacity: 0; visibility: hidden; transform: translateY(8px); transition: .14s;
}
.emoji-panel.open { opacity: 1; visibility: visible; transform: none; }
.emoji-panel button { width: 38px; height: 38px; padding: 0; border: 0; border-radius: 6px; background: transparent; cursor: pointer; font-size: 22px; }
.emoji-panel button:hover { background: #f1f1f1; }

.toast {
  position: absolute; z-index: 100; bottom: 80px; left: 50%;
  padding: 9px 16px; border-radius: 18px;
  background: rgba(0,0,0,.8); color: #fff; font-size: 14px;
  opacity: 0; pointer-events: none; transform: translate(-50%,10px); transition: .18s;
}
.toast.show { opacity: 1; transform: translate(-50%,0); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  :root { --sidebar-width: 100vw; }
  .telegram-app { grid-template-columns: 100vw; }
  .conversation {
    position: absolute; inset: var(--window-bar-height) 0 0 0;
    transform: translateX(100%); transition: transform .22s ease;
    background: #fff;
  }
  .telegram-window.chat-open .conversation { transform: none; }
  .mobile-back { display: grid; }
  .bubble { max-width: 82%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
