:root {
  --bg: #10161e;
  --bg-soft: #131a23;
  --panel: #171e28;
  --row: transparent;
  --row-active: rgba(255, 255, 255, 0.04);
  --line: rgba(255, 255, 255, 0.05);
  --text: #e8eef5;
  --muted: #8a97a8;
  --accent: #3eb1f0;
  --accent-soft: rgba(62, 177, 240, 0.16);
  --in: #222a35;
  --out: #274f66;
  --field: #1a222d;
  --pad: 16px;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100vw;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

#app {
  width: 100%;
  max-width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(120% 80% at 50% -10%, rgba(62, 177, 240, 0.07), transparent 55%),
    var(--bg);
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

#sidebar,
#thread {
  display: none;
  flex-direction: column;
  width: 100%;
  max-width: 100%;
  height: 100%;
  min-width: 0;
  overflow: hidden;
}

#app:not(.chat-open) #sidebar,
#app.chat-open #thread {
  display: flex;
}

.sidebar-header {
  flex: 0 0 auto;
  padding: 12px var(--pad) 10px;
  background: transparent;
  border-bottom: 0;
}

.sidebar-header .brand {
  font-size: 1.2rem;
  letter-spacing: -0.02em;
  font-weight: 650;
}

.brand {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 2px 0 12px;
  color: var(--muted);
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#search {
  display: block;
  width: 100%;
  border: 0;
  outline: 0;
  border-radius: 999px;
  padding: 11px 14px;
  background: var(--field);
  color: var(--text);
}

#search::placeholder {
  color: var(--muted);
}

.tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  padding: 4px var(--pad) 12px;
  border-bottom: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  background: var(--field);
  color: var(--muted);
  font-weight: 550;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab.active {
  background: var(--accent-soft);
  color: #c8e9fb;
}

.connect-card {
  flex: 0 0 auto;
  margin: 10px var(--pad);
  padding: 12px;
  border-radius: 14px;
  background: var(--field);
  border: 1px solid var(--line);
}

.connect-card h2 {
  margin: 0 0 4px;
  font-size: 0.92rem;
}

.connect-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.connect-card .status {
  display: inline-block;
  margin-top: 8px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  background: rgba(227, 93, 106, 0.16);
  color: #ff9aa4;
}

.connect-card .status.ok {
  background: rgba(42, 171, 238, 0.16);
  color: #9ed8ff;
}

.connect-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.connect-actions button {
  border: 0;
  border-radius: 10px;
  padding: 9px 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.connect-actions button.ghost {
  background: #243041;
  color: var(--text);
}

.qr-box {
  margin-top: 10px;
  text-align: center;
}

.qr-box img {
  width: min(240px, 70vw);
  height: auto;
  border-radius: 12px;
  background: #fff;
  padding: 8px;
}

.qr-hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
}

.chat-list {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.chat-swipe {
  position: relative;
  overflow: hidden;
  border-bottom: 0;
  touch-action: pan-y;
}

.chat-swipe + .chat-swipe .chat-item {
  box-shadow: inset 0 1px 0 var(--line);
}

.chat-swipe.pinned .chat-name {
  font-weight: 700;
}

.chat-actions {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: stretch;
  z-index: 0;
  pointer-events: none;
}

.chat-actions-left {
  left: 0;
}

.chat-actions-right {
  right: 0;
}

.chat-swipe[style*="--swipe-x"] .chat-actions {
  pointer-events: auto;
}

.swipe-btn {
  border: 0;
  min-width: 72px;
  padding: 0 12px;
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
}

.swipe-btn.mute { background: #3a4758; }
.swipe-btn.pin { background: #2a6fad; }
.swipe-btn.delete { background: #c45b5b; }

.chat-item {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) 52px;
  gap: 10px;
  align-items: center;
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 12px var(--pad);
  border: 0;
  border-radius: 0;
  /* Must be opaque — covers swipe actions until the row is dragged */
  background: var(--bg);
  text-align: left;
  appearance: none;
  -webkit-appearance: none;
  overflow: hidden;
  transform: translateX(var(--swipe-x, 0px));
  transition: transform 0.18s ease, background 0.15s ease;
}

.chat-swipe.active .chat-item,
.chat-swipe:has(.chat-item:active) .chat-item {
  background: #161d27;
}

.chat-swipe.swiping .chat-item {
  transition: none;
}

.chat-badge {
  flex: 0 0 auto;
  font-size: 0.62rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: lowercase;
}

.chat-badge.muted {
  color: var(--muted);
}

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

.chat-item:active,
.chat-item.active {
  background: #161d27;
}

.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.82rem;
  font-weight: 650;
  color: #eef5fb;
  background: #2a3442;
  overflow: hidden;
}

.avatar.max {
  background: linear-gradient(145deg, #4a6d8c, #2f4f6e);
}

.avatar.telegram {
  background: linear-gradient(145deg, #54c3ff, #2aabee);
}

.avatar.whatsapp {
  background: linear-gradient(145deg, #4adf86, #1fa855);
}

.chat-main {
  min-width: 0;
  overflow: hidden;
}

.chat-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.chat-name {
  min-width: 0;
  font-weight: 600;
  font-size: 0.96rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.plat {
  flex: 0 0 auto;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--muted);
}

.plat.max {
  color: #cbb8ff;
}

.plat.telegram {
  color: #7ec8f5;
}

.plat.whatsapp {
  color: #6edc97;
}

.chat-preview {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta {
  justify-self: end;
  align-self: start;
  padding-top: 2px;
  color: var(--muted);
  font-size: 0.7rem;
  white-space: nowrap;
}

.thread-header {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 10px var(--pad) 12px;
  background: rgba(16, 22, 30, 0.55);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.thread-title {
  min-width: 0;
}

.back-chats {
  display: inline-flex;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  opacity: 0.9;
}

.thread-header h1 {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 575;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ghost.danger {
  color: #c98a8a;
  border: 0;
  background: transparent;
  padding: 6px 4px;
  white-space: nowrap;
}

.meta {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.76rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0.85;
}

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 16px var(--pad) 20px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
  background:
    radial-gradient(90% 50% at 50% 0%, rgba(62, 177, 240, 0.04), transparent 60%),
    transparent;
}

.bubble-row {
  display: flex;
  flex: 0 0 auto;
  width: 100%;
  max-width: 100%;
  min-height: 0;
}

.bubble-row.inbound {
  justify-content: flex-start;
  align-items: flex-start;
}

.bubble-row.outbound {
  justify-content: flex-end;
  align-items: flex-start;
}

.bubble {
  display: inline-block;
  flex: 0 0 auto;
  box-sizing: border-box;
  width: auto !important;
  max-width: 78%;
  height: auto !important;
  min-height: 0 !important;
  margin: 0;
  padding: 10px 14px;
  border-radius: 20px;
  line-height: 1.4;
  vertical-align: top;
  white-space: normal;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset;
}

.bubble.inbound {
  background: var(--in);
  border-bottom-left-radius: 8px;
  color: #e6edf5;
}

.bubble.outbound {
  background: var(--out);
  border-bottom-right-radius: 8px;
  color: #edf6fb;
}

.bubble .who {
  display: block;
  margin: 0 0 4px;
  padding: 0;
  font-size: 0.68rem;
  font-weight: 560;
  line-height: 1.15;
  color: #8eb8d4;
  opacity: 0.85;
}

.bubble-text {
  display: block;
  margin: 0;
  padding: 0;
  font-size: 0.93rem;
  line-height: 1.38;
  font-weight: 420;
  white-space: pre-line;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.bubble .status {
  display: block;
  margin: 4px 0 0;
  padding: 0;
  font-size: 0.64rem;
  line-height: 1.1;
  color: rgba(180, 198, 214, 0.75);
}

.composer {
  flex: 0 0 auto;
  padding: 10px var(--pad) calc(12px + env(safe-area-inset-bottom, 0px));
  background: rgba(16, 22, 30, 0.62);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.composer.hidden {
  display: none;
}

#send-form {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: end;
}

.attach-btn {
  border: 0;
  border-radius: 14px;
  width: 44px;
  height: 44px;
  background: var(--field);
  font-size: 1.1rem;
  line-height: 1;
}

.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.attach-preview.hidden {
  display: none;
}

.attach-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border-radius: 10px;
  padding: 6px 8px;
  background: var(--field);
  font-size: 0.78rem;
}

.attach-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}

.attach-chip button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0 2px;
}

.bubble-media {
  display: grid;
  gap: 8px;
  margin: 4px 0 6px;
}

.bubble-media img {
  display: block;
  max-width: min(100%, 280px);
  max-height: 240px;
  border-radius: 10px;
  object-fit: cover;
}

.bubble-media img.media-sticker {
  max-width: min(100%, 160px);
  max-height: 160px;
  object-fit: contain;
  background: transparent;
  border-radius: 0;
}

.bubble-text.muted {
  opacity: 0.65;
}

.bubble-media audio {
  width: min(100%, 260px);
}

.file-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.18);
  max-width: 280px;
}

.file-card .file-meta {
  min-width: 0;
}

.file-card .file-name {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  max-width: 170px;
}

.file-card .file-size {
  display: block;
  font-size: 0.7rem;
  color: var(--muted);
}

.file-card a {
  flex: 0 0 auto;
  color: var(--accent);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.8rem;
}

#draft {
  width: 100%;
  min-width: 0;
  resize: none;
  min-height: 44px;
  max-height: 110px;
  border: 0;
  outline: 0;
  border-radius: 16px;
  padding: 12px 14px;
  background: rgba(26, 34, 45, 0.9);
  color: var(--text);
}

#send-btn {
  border: 0;
  border-radius: 16px;
  padding: 0 14px;
  height: 44px;
  background: rgba(62, 177, 240, 0.88);
  color: #f5fbff;
  font-weight: 560;
  white-space: nowrap;
}

.attach-btn {
  background: rgba(26, 34, 45, 0.9);
  border-radius: 16px;
}

.empty {
  margin: auto;
  padding: 36px 20px;
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

.login-gate {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(360px, 100%);
  display: grid;
  gap: 10px;
  padding: 20px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.login-card input,
.login-card button {
  border: 0;
  border-radius: 12px;
  padding: 12px;
}

.login-card input {
  outline: 0;
  background: var(--field);
}

.login-card button {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.login-error {
  margin: 0;
  min-height: 1.1em;
  color: #ff9aa4;
  font-size: 0.85rem;
}

@media (min-width: 860px) {
  #app {
    display: grid;
    grid-template-columns: 380px minmax(0, 1fr);
    max-width: 1100px;
    margin: 0 auto;
  }

  #sidebar,
  #thread {
    display: flex !important;
  }

  #sidebar {
    border-right: 1px solid var(--line);
  }

  .back-chats {
    display: none;
  }
}
