:root {
  --bg: #0d0f14;
  --bg-elevated: #161923;
  --bg-bubble-in: #1f2330;
  --bg-bubble-out: #4c5bf5;
  --text: #f1f2f6;
  --text-dim: #9298ab;
  --border: #262b3a;
  --accent: #4c5bf5;
  --danger: #ef4444;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
  background: var(--bg);
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  height: 100%;
}
.screen.active { display: flex; }

/* ---------- Login ---------- */
#screen-login {
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  padding-bottom: calc(24px + var(--safe-bottom));
}
.login-card {
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.login-card h1 {
  font-size: 22px;
  margin: 0 0 4px;
}
.login-card p {
  color: var(--text-dim);
  font-size: 14px;
  margin: 0 0 24px;
}
.login-card input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 16px;
  margin-bottom: 12px;
}
.login-card button {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
}
.login-card button:disabled { opacity: 0.6; }
.login-error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-top: 10px;
}

/* ---------- Contacts list ---------- */
#screen-contacts .topbar {
  padding-top: calc(14px + var(--safe-top));
}
.topbar {
  padding: 14px 16px;
  font-size: 20px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.contact-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.contact-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.contact-row:active { background: var(--bg-elevated); }
.avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 18px;
  flex-shrink: 0;
}
.contact-meta { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; font-size: 16px; }
.contact-preview {
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Chat thread ---------- */
#screen-thread .topbar {
  padding-top: calc(14px + var(--safe-top));
  gap: 12px;
}
.back-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  padding: 4px 6px 4px 0;
  line-height: 1;
}
.thread-avatar { width: 32px; height: 32px; font-size: 14px; }
.thread-title { font-size: 16px; font-weight: 700; flex: 1; }
.newchat-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  padding: 6px;
  line-height: 1;
}
.newchat-btn:active { color: var(--danger); }

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bubble-row { display: flex; }
.bubble-row.out { justify-content: flex-end; }
.bubble-row.in { justify-content: flex-start; }
.bubble {
  max-width: 78%;
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.bubble-row.in .bubble {
  background: var(--bg-bubble-in);
  border-bottom-left-radius: 4px;
}
.bubble-row.out .bubble {
  background: var(--bg-bubble-out);
  color: white;
  border-bottom-right-radius: 4px;
}
.bubble img {
  max-width: 100%;
  border-radius: 10px;
  margin-top: 6px;
  display: block;
}
.bubble code {
  background: rgba(0,0,0,0.25);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
}
.bubble a { color: #a9b4ff; }
.bubble-row.out .bubble a { color: #dfe3ff; }

.tool-chip {
  align-self: flex-start;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.tool-chip .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s infinite ease-in-out;
}
@keyframes pulse { 0%,100% { opacity: 0.3; } 50% { opacity: 1; } }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}
.send-btn, .image-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  font-size: 18px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.send-btn {
  background: var(--accent);
  color: white;
}
.image-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}
.send-btn:disabled, .image-btn:disabled { opacity: 0.5; }

/* Fullscreen image viewer */
#img-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
#img-viewer.active { display: flex; }
#img-viewer img { max-width: 100%; max-height: 100%; border-radius: 8px; }
