* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

body {
  background: #f0f2f5;
  height: 100vh;
  overflow: hidden;
}

.screen {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

#welcome-screen,
#login-screen,
#register-screen,
#family-choice-screen,
#create-family-screen,
#join-family-screen {
  align-items: center;
  justify-content: center;
}

.card {
  background: white;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.1);
  text-align: center;
  width: 90%;
  max-width: 400px;
}

.card h1 { margin-bottom: 16px; font-size: 28px; }
.card p { color: #666; margin-bottom: 20px; }

.card input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  margin-bottom: 12px;
  outline: none;
}
.card input:focus { border-color: #4a90e2; }

.primary-btn {
  width: 100%;
  padding: 12px;
  background: #4a90e2;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 4px;
  margin-bottom: 8px;
  transition: background 0.2s;
}
.primary-btn:hover { background: #357abd; }
.primary-btn:disabled { background: #aaa; cursor: not-allowed; }

.secondary-btn {
  width: 100%;
  padding: 12px;
  background: white;
  color: #4a90e2;
  border: 2px solid #4a90e2;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  margin-bottom: 8px;
  transition: background 0.2s;
}
.secondary-btn:hover { background: #f0f7ff; }

.link-btn {
  background: none;
  border: none;
  color: #888;
  cursor: pointer;
  font-size: 14px;
  margin-top: 8px;
  text-decoration: underline;
}
.link-btn:hover { color: #4a90e2; }

.error-message {
  background: #fee;
  border: 1px solid #fcc;
  color: #c33;
  padding: 10px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 12px;
  text-align: left;
}

header {
  background: #4a90e2;
  color: white;
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  flex-wrap: wrap;
  gap: 8px;
}

.header-left { display: flex; align-items: center; gap: 12px; }
.title-block { display: flex; flex-direction: column; gap: 2px; }
.title-block h2 { font-size: 18px; line-height: 1.2; }

/* 🆕 Группа статусов под названием */
.header-status {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.online-count {
  font-size: 12px;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 4px;
}
.online-count::before {
  content: '';
  width: 8px;
  height: 8px;
  background: #4ade80;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 6px rgba(74, 222, 128, 0.6);
}

/* 🆕 Индикатор статуса соединения */
.connection-status {
  font-size: 12px;
  opacity: 0.9;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 10px;
  transition: background 0.3s;
}

.connection-status .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.connection-status.status-online {
  background: rgba(74, 222, 128, 0.2);
}
.connection-status.status-online .status-dot {
  background: #4ade80;
}

.connection-status.status-connecting {
  background: rgba(251, 191, 36, 0.25);
}
.connection-status.status-connecting .status-dot {
  background: #fbbf24;
  animation: status-pulse 1s infinite;
}

.connection-status.status-offline {
  background: rgba(248, 113, 113, 0.3);
}
.connection-status.status-offline .status-dot {
  background: #f87171;
}

@keyframes status-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.invite-btn {
  background: rgba(255,255,255,0.2);
  color: white;
  border: 1px solid rgba(255,255,255,0.4);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}
.invite-btn:hover { background: rgba(255,255,255,0.3); }

.header-right { display: flex; align-items: center; gap: 12px; }
#user-label { font-size: 14px; opacity: 0.9; }

.logout-btn {
  background: rgba(255,255,255,0.2);
  color: white;
  border: 1px solid rgba(255,255,255,0.4);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s;
}
.logout-btn:hover { background: rgba(255,255,255,0.3); }

main#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  position: relative;
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 16px;
  background: white;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  word-wrap: break-word;
  transition: opacity 0.3s;
}

/* 🆕 Сообщение «в пути» (отправляется) */
.message.pending {
  opacity: 0.6;
}

/* 🆕 Сообщение, которое не удалось отправить */
.message.failed {
  opacity: 0.7;
  border: 1px solid #f87171;
}

.message-status {
  font-size: 11px;
  margin-left: 6px;
  display: inline-block;
}

.message .author { font-size: 12px; font-weight: 600; color: #4a90e2; margin-bottom: 4px; }
.message .text { font-size: 15px; line-height: 1.4; }
.message .time { font-size: 11px; color: #999; margin-top: 4px; text-align: right; display: flex; justify-content: flex-end; align-items: center; }

.message.own {
  align-self: flex-end;
  background: #4a90e2;
  color: white;
}
.message.own .author { color: rgba(255,255,255,0.85); }
.message.own .time { color: rgba(255,255,255,0.75); }

/* Кнопка ⊕ для добавления реакции */
.message-add-reaction {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.15s, transform 0.15s;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
  z-index: 5;
}

.message:not(.own) .message-add-reaction { right: -36px; }
.message.own .message-add-reaction { left: -36px; }

.message:hover .message-add-reaction { opacity: 1; }

.message-add-reaction:hover {
  background: #f0f7ff;
  transform: translateY(-50%) scale(1.1);
}

/* Скрываем кнопку реакции для pending/failed сообщений */
.message.pending .message-add-reaction,
.message.failed .message-add-reaction {
  display: none;
}

@media (hover: none) {
  .message-add-reaction { opacity: 0.6; }
  .message-add-reaction:active { opacity: 1; background: #f0f7ff; }
}

.message-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.reaction-pill {
  background: rgba(74, 144, 226, 0.1);
  border: 1px solid rgba(74, 144, 226, 0.2);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.15s;
  user-select: none;
}

.reaction-pill:hover { background: rgba(74, 144, 226, 0.2); }

.reaction-pill.own {
  background: rgba(74, 144, 226, 0.25);
  border-color: #4a90e2;
  font-weight: 600;
}

.reaction-pill .count { font-size: 12px; color: #555; }
.reaction-pill.own .count { color: #2c5d99; }

.message.own .reaction-pill {
  background: rgba(255,255,255,0.2);
  border-color: rgba(255,255,255,0.3);
  color: white;
}
.message.own .reaction-pill:hover { background: rgba(255,255,255,0.3); }
.message.own .reaction-pill.own {
  background: rgba(255,255,255,0.4);
  border-color: white;
}
.message.own .reaction-pill .count { color: rgba(255,255,255,0.9); }

.reaction-picker {
  position: absolute;
  background: white;
  border-radius: 24px;
  padding: 6px 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  display: flex;
  gap: 2px;
  z-index: 50;
  border: 1px solid #f0f0f0;
  flex-wrap: wrap;
  max-width: 320px;
}

.reaction-emoji {
  background: none;
  border: none;
  font-size: 22px;
  padding: 4px 6px;
  cursor: pointer;
  border-radius: 50%;
  transition: transform 0.15s, background 0.15s;
}
.reaction-emoji:hover { background: #f0f0f0; transform: scale(1.2); }

.system {
  text-align: center;
  font-size: 12px;
  color: #888;
  padding: 4px;
  font-style: italic;
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 20px;
  font-size: 13px;
  color: #888;
  background: #f8f9fa;
  border-top: 1px solid #f0f0f0;
  font-style: italic;
}

.typing-dots { display: inline-flex; gap: 3px; align-items: flex-end; }
.typing-dots span {
  width: 6px;
  height: 6px;
  background: #4a90e2;
  border-radius: 50%;
  animation: typing-bounce 1.4s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

footer {
  background: white;
  padding: 12px;
  display: flex;
  gap: 8px;
  border-top: 1px solid #e0e0e0;
  align-items: center;
}

footer input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  font-size: 15px;
  outline: none;
}
footer input:focus { border-color: #4a90e2; }

footer button#send-btn {
  padding: 10px 20px;
  background: #4a90e2;
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 15px;
  cursor: pointer;
}
footer button#send-btn:hover { background: #357abd; }

.emoji-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 50%;
  transition: background 0.2s;
}
.emoji-btn:hover { background: #f0f0f0; }

.emoji-picker {
  background: white;
  border-top: 1px solid #e0e0e0;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  max-height: 320px;
}

.emoji-tabs {
  display: flex;
  border-bottom: 1px solid #f0f0f0;
  padding: 4px;
  gap: 2px;
}

.emoji-tab {
  flex: 1;
  background: none;
  border: none;
  font-size: 22px;
  padding: 8px;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.15s;
}
.emoji-tab:hover { background: #f5f5f5; }
.emoji-tab.active { background: #e8f0fe; }

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  padding: 12px;
  overflow-y: auto;
  flex: 1;
}

.emoji-item {
  background: none;
  border: none;
  font-size: 26px;
  padding: 6px;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s;
  line-height: 1;
}
.emoji-item:hover { background: #f0f0f0; }

@media (max-width: 480px) {
  .emoji-grid { grid-template-columns: repeat(6, 1fr); }
  .emoji-picker { max-height: 260px; }
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 16px;
  text-align: center;
  width: 90%;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.modal-content h2 { margin-bottom: 12px; font-size: 22px; }
.modal-content p { color: #666; margin-bottom: 20px; font-size: 14px; }

.invite-code-big {
  font-size: 36px;
  font-weight: bold;
  letter-spacing: 4px;
  background: #f0f7ff;
  border: 2px dashed #4a90e2;
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 20px;
  color: #4a90e2;
  font-family: monospace;
}
