:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f6f8);
  --text: var(--tg-theme-text-color, #16202a);
  --hint: var(--tg-theme-hint-color, #7c8a96);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #3390ec);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --header-bg: var(--tg-theme-header-bg-color, #ffffff);
  --border: color-mix(in srgb, var(--hint) 20%, transparent);
  --incoming: color-mix(in srgb, var(--secondary-bg) 92%, var(--bg));
  --outgoing: color-mix(in srgb, var(--button) 18%, var(--bg));
  --shadow: 0 8px 30px rgba(12, 28, 43, 0.08);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--secondary-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  width: min(1120px, 100%);
  height: 100%;
  margin: 0 auto;
  background: var(--bg);
  box-shadow: var(--shadow);
}

.dialogs-panel {
  position: relative;
  min-width: 0;
  border-right: 1px solid var(--border);
  background: var(--bg);
  overflow: hidden;
}

.panel-header, .chat-header {
  display: flex;
  align-items: center;
  min-height: 70px;
  padding: 12px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
}

.panel-header { justify-content: space-between; }
.panel-header h1, .chat-person h2 { margin: 0; font-size: 19px; line-height: 1.2; }
.panel-header p, .chat-person p { margin: 4px 0 0; color: var(--hint); font-size: 13px; }

.icon-button, .attach-button, .send-button {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.icon-button:hover, .attach-button:hover { background: var(--secondary-bg); }
.icon-button svg, .attach-button svg, .send-button svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.search-wrap {
  position: relative;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.search-wrap svg {
  position: absolute;
  top: 20px;
  left: 25px;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--hint);
  stroke-width: 2;
}

.search-wrap input {
  width: 100%;
  height: 38px;
  padding: 0 14px 0 40px;
  color: var(--text);
  background: var(--secondary-bg);
  border: 0;
  border-radius: 12px;
  outline: none;
}

.dialogs-list { height: calc(100% - 129px); overflow-y: auto; }

.dialog-item {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  gap: 11px;
  align-items: center;
  width: 100%;
  min-height: 70px;
  padding: 9px 12px;
  color: var(--text);
  text-align: left;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.dialog-item:hover { background: var(--secondary-bg); }
.dialog-item.active { background: color-mix(in srgb, var(--button) 13%, var(--bg)); }

.avatar {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  color: #fff;
  font-size: 17px;
  font-weight: 650;
  background: linear-gradient(135deg, #56a8e8, #3579cf);
  border-radius: 50%;
  user-select: none;
}

.dialog-main { min-width: 0; }
.dialog-title-row { display: flex; align-items: center; gap: 8px; }
.dialog-title { overflow: hidden; font-size: 15px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.dialog-preview { overflow: hidden; margin-top: 5px; color: var(--hint); font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.dialog-preview .you { color: var(--button); }
.dialog-side { display: flex; flex-direction: column; align-items: flex-end; gap: 7px; }
.dialog-time { color: var(--hint); font-size: 11px; }
.unread-badge { min-width: 20px; padding: 3px 6px; color: #fff; font-size: 11px; font-weight: 700; text-align: center; background: var(--button); border-radius: 12px; }

.empty-state {
  position: absolute;
  inset: 130px 20px 20px;
  display: grid;
  align-content: center;
  justify-items: center;
  text-align: center;
}
.empty-state h2 { margin: 12px 0 4px; font-size: 17px; }
.empty-state p { margin: 0; color: var(--hint); font-size: 14px; }
.empty-icon { font-size: 38px; }

.chat-panel {
  position: relative;
  min-width: 0;
  height: 100%;
  background:
    radial-gradient(circle at 25% 15%, color-mix(in srgb, var(--button) 5%, transparent) 0 2px, transparent 3px) 0 0 / 36px 36px,
    var(--secondary-bg);
}

.chat-placeholder { display: grid; place-items: center; height: 100%; }
.placeholder-bubble { padding: 7px 13px; color: #fff; font-size: 13px; background: rgba(25, 42, 56, 0.48); border-radius: 16px; }
.chat-content {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  min-width: 0;
  height: 100%;
}
.chat-header { gap: 10px; }
.chat-header .avatar { width: 43px; height: 43px; }
.chat-person { min-width: 0; }
.chat-person h2, .chat-person p { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-person-link { color: inherit; text-decoration: none; cursor: pointer; }
.chat-person-link:hover h2, .chat-person-link:hover p { color: var(--button); }
.back-button { display: none; }

.messages {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 18px clamp(12px, 4vw, 48px);
  overflow-y: auto;
}

.message-row { display: flex; }
.message-row.outgoing { justify-content: flex-end; }
.message-bubble {
  max-width: min(76%, 560px);
  padding: 8px 11px 6px;
  background: var(--incoming);
  border-radius: 14px 14px 14px 4px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
}
.outgoing .message-bubble { background: var(--outgoing); border-radius: 14px 14px 4px 14px; }
.message-text { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.message-image { display: block; width: min(360px, 100%); max-height: 420px; margin: -2px -4px 6px; object-fit: cover; background: var(--secondary-bg); border-radius: 10px; }
.image-loader { width: 220px; height: 140px; margin: -2px -4px 6px; background: var(--secondary-bg); border-radius: 10px; animation: pulse 1.2s ease-in-out infinite; }
.message-file {
  display: flex;
  align-items: center;
  gap: 10px;
  width: min(320px, 100%);
  min-width: 0;
  margin: 0 0 5px;
  padding: 8px;
  color: var(--text);
  text-align: left;
  background: color-mix(in srgb, var(--button) 8%, var(--bg));
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}
.message-file:disabled { opacity: 0.55; cursor: wait; }
.message-file-icon {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  font-size: 21px;
  background: var(--bg);
  border-radius: 50%;
}
.message-file-details { display: flex; flex: 1 1 auto; flex-direction: column; min-width: 0; }
.message-file-name { overflow: hidden; font-size: 14px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.message-file-action { margin-top: 2px; color: var(--button); font-size: 12px; }
.message-time { display: block; margin-top: 3px; color: var(--hint); font-size: 10px; text-align: right; }

.attachment-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  margin: 0 12px 5px;
  padding: 8px 12px;
  color: var(--text);
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.attachment-preview span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-preview button { flex: 0 0 auto; color: var(--hint); font-size: 24px; background: none; border: 0; cursor: pointer; }

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 5px;
  min-width: 0;
  padding: 8px max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.composer textarea {
  min-width: 0;
  width: 100%;
  max-height: 120px;
  padding: 10px 12px;
  color: var(--text);
  line-height: 20px;
  resize: none;
  background: var(--secondary-bg);
  border: 0;
  border-radius: 18px;
  outline: none;
}
.attach-button { color: var(--hint); }
.send-button { color: var(--button-text); background: var(--button); }
.send-button:disabled { opacity: 0.45; cursor: default; }

.toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  max-width: calc(100% - 32px);
  padding: 10px 14px;
  color: #fff;
  font-size: 13px;
  text-align: center;
  background: rgba(22, 32, 42, 0.92);
  border-radius: 12px;
  transform: translateX(-50%);
  box-shadow: var(--shadow);
}

@keyframes pulse { 50% { opacity: 0.45; } }

@media (max-width: 680px) {
  .app-shell { display: block; }
  .dialogs-panel, .chat-panel { width: 100%; height: 100%; }
  .chat-panel { display: none; }
  body.chat-open .dialogs-panel { display: none; }
  body.chat-open .chat-panel { display: block; }
  .back-button { display: grid; margin-left: -7px; }
  .message-bubble { max-width: 86%; }
  .panel-header, .chat-header { min-height: 62px; }
  .dialogs-list { height: calc(100% - 121px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; }
}
