*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          var(--tg-theme-bg-color, #ffffff);
  --bg2:         var(--tg-theme-secondary-bg-color, #f1f1f1);
  --text:        var(--tg-theme-text-color, #000000);
  --hint:        var(--tg-theme-hint-color, #999999);
  --btn:         var(--tg-theme-button-color, #2481cc);
  --btn-text:    var(--tg-theme-button-text-color, #ffffff);
  --bubble-in:   var(--tg-theme-secondary-bg-color, #e9e9eb);
  --bubble-out:  var(--tg-theme-button-color, #2481cc);
  --radius:      18px;
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

[x-cloak] { display: none !important; }

html, body {
  height: 100dvh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

.screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

.screen--centered {
  display: flex;
  align-items: center;
  justify-content: center;
}

.screen--chat {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
}

/* Online badge */
.online-badge {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--hint);
}
.online-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #4cd964;
  flex-shrink: 0;
}

/* Searching-screen stats */
.stats-row {
  display: flex; align-items: center; gap: 12px;
  font-size: 13px; color: var(--hint);
}
.stat-badge {
  display: flex; align-items: center; gap: 5px;
}
.dot--chat { background: #5ac8fa; }

/* Welcome */
.welcome-inner {
  width: 100%;
  max-width: 360px;
  padding: 32px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.logo { font-size: 64px; line-height: 1; }
h1 { font-size: 22px; font-weight: 700; }
.hint { font-size: 14px; color: var(--hint); text-align: center; line-height: 1.5; }
.field { width: 100%; }

.field input,
.input-row input {
  width: 100%;
  padding: 12px 16px;
  border: 1.5px solid var(--bg2);
  border-radius: 12px;
  background: var(--bg2);
  color: var(--text);
  font-size: 16px;
  outline: none;
}

.field input:focus,
.input-row input:focus { border-color: var(--btn); }

.btn-primary {
  width: 100%;
  padding: 13px;
  background: var(--btn);
  color: var(--btn-text);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary:active { opacity: 0.8; }
.error { color: #e53e3e; font-size: 13px; }

/* Searching */
.searching-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--bg2);
  border-top-color: var(--btn);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
.searching-text { font-size: 16px; color: var(--hint); }

.btn-secondary {
  padding: 11px 28px;
  background: var(--bg2);
  color: var(--text);
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

/* Chat */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--bg2);
  flex-shrink: 0;
}

.chat-header-info { display: flex; align-items: center; gap: 10px; min-width: 0; }
.chat-avatar { font-size: 32px; line-height: 1; }
.partner-name { font-weight: 600; font-size: 15px; }
.partner-status { font-size: 12px; color: var(--hint); }
.partner-status.offline { color: #e53e3e; }

.btn-skip {
  padding: 7px 14px;
  background: var(--bg2);
  color: var(--text);
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.btn-skip.highlight {
  background: var(--btn);
  color: var(--btn-text);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 12px 4px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
}

.msg-row {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.msg-row.out { align-items: flex-end; }
.msg-row.in  { align-items: flex-start; }

.bubble-wrap {
  display: flex;
  flex-direction: column;
  max-width: 75%;
}

.bubble {
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.4;
  word-break: break-word;
}

.bubble.out {
  background: var(--bubble-out);
  color: var(--btn-text);
  border-bottom-right-radius: 4px;
}

.bubble.in {
  background: var(--bubble-in);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.system-msg {
  text-align: center;
  font-size: 12px;
  color: var(--hint);
  padding: 4px 0;
}

.bubble-time {
  font-size: 11px;
  color: var(--hint);
  margin-top: 2px;
  padding: 0 4px;
}

.typing-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 16px 2px;
  font-size: 12px;
  color: var(--hint);
  flex-shrink: 0;
}

.typing-dots { display: flex; gap: 3px; }
.typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--hint);
  animation: blink 1.2s infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--bg2);
  flex-shrink: 0;
}

.input-row input {
  flex: 1;
  min-width: 0;
  border-radius: 22px;
}

.input-row input:disabled { opacity: 0.5; }

.btn-send {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--btn);
  color: var(--btn-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.btn-send:disabled { opacity: 0.4; cursor: default; }
.btn-send:not(:disabled):active { opacity: 0.8; }
