:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel2: #232e3c;
  --text: #f5f5f5;
  --muted: #8b9aab;
  --accent: #2aabee;
  --sent: #8774e1;
  --recv: #182533;
  --danger: #e53935;
  --ok: #4fae4e;
  --hair: rgba(255,255,255,.06);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { border: 0; background: none; cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

#app { height: 100%; display: flex; flex-direction: column; }

/* ===== LOGIN ===== */
.screen { display: none; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

.login {
  justify-content: center; align-items: center; padding: 24px;
  background: radial-gradient(circle at 30% 20%, #1b2a3a, var(--bg) 60%);
}
.login-card { width: min(100%, 380px); }
.brand {
  font-size: 2.4rem; font-weight: 800; letter-spacing: -.03em; margin: 0 0 6px;
}
.brand span { color: var(--accent); }
.login p { color: var(--muted); margin: 0 0 28px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 14px 16px; border-radius: 14px; border: 1px solid var(--hair);
  background: var(--panel); outline: none;
}
.field input:focus { border-color: var(--accent); }
.btn {
  width: 100%; margin-top: 10px; padding: 14px 16px; border-radius: 14px;
  background: var(--accent); color: #041018; font-weight: 700;
}
.btn:disabled { opacity: .5; }
.error { color: #ff8a80; min-height: 1.2em; margin-top: 10px; font-size: .9rem; }

/* ===== CHAT LIST (Telegram) ===== */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--panel); border-bottom: 1px solid var(--hair);
  min-height: 56px;
}
.topbar h1 { flex: 1; margin: 0; font-size: 1.25rem; font-weight: 700; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, #6c8cff, #b388ff); flex-shrink: 0;
  overflow: hidden;
}
.avatar.sm { width: 28px; height: 28px; font-size: .75rem; }
.avatar.lg { width: 48px; height: 48px; font-size: 1.1rem; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.search-wrap { padding: 10px 14px; background: var(--panel); }
.search {
  width: 100%; padding: 11px 14px 11px 38px; border-radius: 22px;
  border: 0; background: var(--panel2); color: var(--text);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='%238b9aab' viewBox='0 0 24 24'%3E%3Cpath d='M15.5 14h-.79l-.28-.27A6.5 6.5 0 1 0 14 15.5l.27.28v.79L20 21.5 21.5 20l-6-6zM10 15a5 5 0 1 1 0-10 5 5 0 0 1 0 10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 12px center;
}

.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-item {
  display: flex; gap: 12px; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--hair); width: 100%; text-align: left;
}
.chat-item:active { background: rgba(42,171,238,.08); }
.chat-meta { flex: 1; min-width: 0; }
.chat-row1, .chat-row2 { display: flex; justify-content: space-between; gap: 8px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { color: var(--muted); font-size: .75rem; flex-shrink: 0; }
.chat-preview {
  color: var(--muted); font-size: .9rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; margin-top: 2px;
}
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent); color: #041018; font-size: .75rem; font-weight: 700;
  display: grid; place-items: center;
}

.bottom-nav {
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: rgba(23,33,43,.96); border-top: 1px solid var(--hair);
  padding: 6px 0 calc(6px + var(--safe-bottom));
}
.nav-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: .7rem; padding: 6px;
}
.nav-btn.active { color: var(--accent); }
.nav-btn svg { width: 24px; height: 24px; }

.fab {
  position: absolute; right: 18px; bottom: calc(72px + var(--safe-bottom));
  width: 56px; height: 56px; border-radius: 50%; background: var(--accent);
  color: #041018; display: grid; place-items: center; box-shadow: 0 8px 24px rgba(0,0,0,.35);
  z-index: 5;
}

/* ===== CHAT VIEW ===== */
#view-chat { background: #0b141a; position: relative; }
#view-chat::before {
  content: ""; position: absolute; inset: 0; opacity: .07; pointer-events: none;
  background-image: radial-gradient(circle at 20% 20%, #fff 1px, transparent 1px),
    radial-gradient(circle at 80% 40%, #fff 1px, transparent 1px);
  background-size: 48px 48px;
}
.chat-header, .composer, .messages { position: relative; z-index: 1; }
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + var(--safe-top)) 10px 8px;
  background: var(--panel); border-bottom: 1px solid var(--hair);
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: var(--text);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title .status { color: var(--muted); font-size: .8rem; }

.messages {
  flex: 1; overflow-y: auto; padding: 12px 10px 8px;
  display: flex; flex-direction: column; gap: 4px;
  -webkit-overflow-scrolling: touch;
  overflow-anchor: none; /* prevent browser scroll jumps when media loads */
  overscroll-behavior-y: contain;
}
.media-thumb:not(.video-poster):not(.sticker-img) {
  width: 100%;
  max-width: 260px;
  aspect-ratio: 3 / 4;
  max-height: 280px;
  object-fit: cover;
  background: #0a1218;
}
.bubble-row { display: flex; }
.bubble-row.me { justify-content: flex-end; }
.bubble {
  max-width: min(82%, 420px); padding: 8px 10px 4px;
  border-radius: 16px; line-height: 1.35; word-wrap: break-word; overflow-wrap: anywhere;
  white-space: pre-wrap; font-size: .98rem;
}
.bubble-row.them .bubble { background: var(--recv); border-bottom-left-radius: 6px; }
.bubble-row.me .bubble { background: var(--sent); border-bottom-right-radius: 6px; }
.bubble .meta {
  display: flex; justify-content: flex-end; gap: 4px; align-items: center;
  margin-top: 2px; font-size: .7rem; color: rgba(255,255,255,.65);
}
.bubble.media-bubble {
  padding: 4px; max-width: min(78%, 320px); overflow: hidden;
}
.bubble.media-bubble .meta {
  position: absolute; right: 8px; bottom: 6px; margin: 0;
  padding: 2px 6px; border-radius: 10px; background: rgba(0,0,0,.45);
  color: #fff;
}
.bubble.media-bubble { position: relative; }
.media-thumb {
  display: block; width: 100%; max-height: 360px; object-fit: cover;
  border-radius: 14px; background: #0a1218; cursor: pointer;
}
img.video-poster {
  width: 100%;
  aspect-ratio: 16 / 9;
  min-height: 160px;
  max-height: 320px;
  object-fit: cover;
  background: #0a1218;
  display: block;
}
.video-wrap {
  position: relative; cursor: pointer;
  width: 100%; border-radius: 14px; overflow: hidden;
  background: #0a1218;
  aspect-ratio: 16 / 9;
}
.video-wrap .play {
  position: absolute; inset: 0; display: grid; place-items: center;
  pointer-events: none; z-index: 2;
}
.video-wrap .play span {
  width: 52px; height: 52px; border-radius: 50%; background: rgba(0,0,0,.45);
  display: grid; place-items: center; color: #fff; font-size: 1.25rem;
  border: 1px solid rgba(255,255,255,.25);
}
.media-caption {
  padding: 4px 8px 22px; font-size: .9rem; white-space: pre-wrap;
}

/* fullscreen lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.92);
  display: none; flex-direction: column; align-items: center; justify-content: center;
  padding: 12px; padding-top: calc(12px + var(--safe-top));
}
.lightbox.open { display: flex; }
.lightbox img, .lightbox video {
  max-width: 100%; max-height: calc(100vh - 80px); border-radius: 8px;
  object-fit: contain;
}
.lightbox .lb-close {
  position: absolute; top: calc(10px + var(--safe-top)); right: 12px;
  width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,.12);
  color: #fff; font-size: 1.3rem;
}
.day {
  align-self: center; margin: 10px 0; padding: 4px 10px; border-radius: 10px;
  background: rgba(0,0,0,.35); color: #dfe8f0; font-size: .75rem;
}
.bubble.emoji-only {
  background: transparent !important; font-size: 2.4rem; line-height: 1.1;
  padding: 2px 4px;
}
.bubble.sticker-bubble {
  background: transparent !important; padding: 2px; max-width: min(55%, 220px);
}
.bubble.sticker-bubble .meta { background: rgba(0,0,0,.4); }
.sticker-img {
  display: block; width: 100%; max-width: 180px; max-height: 180px;
  object-fit: contain; cursor: pointer;
}

/* Telegram-like voice notes (compact) */
.bubble.voice-bubble {
  width: min(58vw, 200px);
  min-width: 160px;
  max-width: 200px;
  padding: 6px 8px 4px;
}
.voice {
  display: flex; align-items: center; gap: 8px;
  user-select: none;
}
.voice-play {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: #fff;
  background: rgba(255,255,255,.18);
}
.bubble-row.me .voice-play { background: rgba(255,255,255,.22); }
.bubble-row.them .voice-play { background: var(--accent); color: #041018; }
.voice-play svg { width: 16px; height: 16px; display: block; }
.voice-main { flex: 1; min-width: 0; }
.voice-wave {
  display: flex; align-items: center; gap: 1.5px; height: 18px;
  cursor: pointer;
}
.voice-wave i {
  display: block; width: 2px; border-radius: 1px;
  background: rgba(255,255,255,.35);
  min-height: 3px;
}
.voice-wave i.on { background: #fff; }
.bubble-row.them .voice-wave i { background: rgba(255,255,255,.28); }
.bubble-row.them .voice-wave i.on { background: var(--accent); }
.voice-meta-row {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: 1px; font-size: .68rem; color: rgba(255,255,255,.65);
  line-height: 1.2;
}
.voice-dur { font-variant-numeric: tabular-nums; }
.voice.playing .voice-play { background: rgba(255,255,255,.3); }

/* emoji / gif / sticker picker */
.picker {
  background: var(--panel); border-top: 1px solid var(--hair);
  max-height: 42vh; display: flex; flex-direction: column;
}
.picker[hidden] { display: none !important; }
.picker-tabs {
  display: flex; gap: 4px; padding: 8px 10px 0;
}
.picker-tab {
  flex: 1; padding: 8px; border-radius: 10px 10px 0 0; color: var(--muted); font-weight: 600;
}
.picker-tab.active { color: var(--accent); background: var(--panel2); }
.picker-search-wrap { padding: 8px 10px; }
.picker-search-wrap input {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 0; background: var(--panel2);
}
.picker-body {
  flex: 1; overflow-y: auto; padding: 8px 10px 12px;
  -webkit-overflow-scrolling: touch;
}
.emoji-grid {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px;
}
.emoji-grid button {
  font-size: 1.55rem; padding: 6px 0; border-radius: 10px;
}
.emoji-grid button:active { background: rgba(255,255,255,.08); }
.gif-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
}
.gif-grid button {
  padding: 0; border-radius: 12px; overflow: hidden; background: var(--panel2);
  aspect-ratio: 1; position: relative;
}
.gif-grid img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.picker-empty { color: var(--muted); text-align: center; padding: 24px 8px; font-size: .9rem; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  background: var(--panel); border-top: 1px solid var(--hair);
}
.composer .input-wrap {
  flex: 1; display: flex; align-items: flex-end; gap: 6px;
  background: var(--panel2); border-radius: 22px; padding: 8px 12px; min-height: 44px;
}
.composer textarea {
  flex: 1; border: 0; background: transparent; resize: none; outline: none;
  max-height: 120px; min-height: 24px; line-height: 1.35;
}
.mic-btn, .send-btn {
  width: 44px; height: 44px; border-radius: 50%; background: var(--sent);
  color: #fff; display: grid; place-items: center; flex-shrink: 0;
}
.send-btn { background: var(--accent); color: #041018; }
.attach-btn { color: var(--muted); padding: 4px; }

/* ===== SETTINGS ===== */
.settings-body { flex: 1; overflow-y: auto; padding: 16px; }
.card {
  background: var(--panel); border-radius: 16px; padding: 14px; margin-bottom: 12px;
}
.card h3 { margin: 0 0 10px; font-size: 1rem; }
.row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--hair); }
.row:last-child { border-bottom: 0; }
.danger-btn {
  width: 100%; padding: 14px; border-radius: 14px; background: rgba(229,57,53,.15);
  color: #ff8a80; font-weight: 700; margin-top: 8px;
}

.empty {
  margin: auto; text-align: center; color: var(--muted); padding: 40px 20px;
}
.hidden { display: none !important; }
.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%); background: #323f4d; color: #fff;
  padding: 10px 16px; border-radius: 12px; z-index: 50; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: .2s;
}
.toast.show { opacity: 1; }

.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60;
  display: none; align-items: flex-end; justify-content: center;
}
.sheet-backdrop.open { display: flex; }
.sheet {
  width: min(100%, 480px); background: var(--panel2); border-radius: 16px 16px 0 0;
  padding: 8px 8px calc(12px + var(--safe-bottom));
}
.sheet button {
  width: 100%; text-align: left; padding: 16px 14px; border-radius: 12px;
  color: var(--text); font-size: 1rem;
}
.sheet button:active { background: rgba(255,255,255,.06); }
.sheet button.danger { color: #ff8a80; }
.sheet .cancel { text-align: center; color: var(--accent); font-weight: 600; margin-top: 4px; }

.chat-item { position: relative; }
.chat-item .del-hint {
  color: var(--muted); font-size: .7rem; margin-left: 4px;
}

/* Desktop: keep phone-like column centered */
@media (min-width: 800px) {
  #app {
    max-width: 480px; margin: 0 auto; border-left: 1px solid var(--hair);
    border-right: 1px solid var(--hair); box-shadow: 0 0 60px rgba(0,0,0,.4);
  }
}
