:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --surface-alt: #f1f3f4;
  --border: #e3e6ea;
  --text: #202124;
  --text-muted: #5f6368;
  --accent: #1a73e8;
  --accent-dark: #1557b0;
  --accent-soft: #d2e3fc;
  --bubble-mine-start: #1a73e8;
  --bubble-mine-end: #4285f4;
  --bubble-mine-text: #ffffff;
  --bubble-theirs: #f1f3f4;
  --bubble-theirs-text: #202124;
  --shadow-sm: 0 1px 2px rgba(60, 64, 67, 0.15), 0 1px 3px rgba(60, 64, 67, 0.1);
  --shadow-md: 0 2px 6px rgba(60, 64, 67, 0.15), 0 4px 12px rgba(60, 64, 67, 0.08);
  --online: #34a853;
  --danger: #d93025;
}

html[data-theme="dark"] {
  --bg: #131314;
  --surface: #1e1f20;
  --surface-alt: #2a2b2d;
  --border: #34363a;
  --text: #e8eaed;
  --text-muted: #9aa0a6;
  --accent: #8ab4f8;
  --accent-dark: #aecbfa;
  --accent-soft: #2b3a4d;
  --bubble-mine-start: #4285f4;
  --bubble-mine-end: #1a73e8;
  --bubble-mine-text: #ffffff;
  --bubble-theirs: #2a2b2d;
  --bubble-theirs-text: #e8eaed;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Google Sans", "Roboto", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.25s ease, color 0.25s ease;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- Auth screens ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 20% 20%, #e8f0fe, transparent 55%),
              radial-gradient(circle at 80% 80%, #fce8e6, transparent 55%),
              var(--bg);
  padding: 24px;
}

.auth-card {
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--shadow-md);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.auth-card-wide { max-width: 480px; }

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
}

.brand-bubble {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--bubble-mine-start), var(--bubble-mine-end));
  box-shadow: var(--shadow-sm);
}

.auth-brand h1 {
  font-size: 22px;
  margin: 0;
}

.auth-subtitle {
  color: var(--text-muted);
  margin: 4px 0 28px;
}

.auth-form { display: flex; flex-direction: column; gap: 18px; }

.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); }

.field input,
.auth-form input[type="text"],
.auth-form input[type="password"],
.auth-form input[type="email"] {
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 15px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.auth-form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-primary {
  border: none;
  border-radius: 24px;
  padding: 13px 20px;
  background: linear-gradient(135deg, var(--bubble-mine-start), var(--bubble-mine-end));
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.btn-primary:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }

.auth-error {
  background: #fce8e6;
  color: var(--danger);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 16px;
}

.auth-footer {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: var(--text-muted);
}

.auth-footer a { color: var(--accent); font-weight: 500; }

/* ---------- App shell ---------- */
.app-shell {
  height: 100vh;
  display: grid;
  grid-template-columns: 360px 1fr;
  background: var(--bg);
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 12px;
}

.me { display: flex; align-items: center; gap: 12px; min-width: 0; }

.me-info { display: flex; flex-direction: column; min-width: 0; }
.me-info strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-username { font-size: 12px; color: var(--text-muted); }

.sidebar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover { background: var(--surface-alt); color: var(--text); }

.icon-moon { display: none; }
html[data-theme="dark"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: block; }

.avatar {
  position: relative;
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.3px;
  box-shadow: var(--shadow-sm);
}

.presence-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text-muted);
  border: 2.5px solid var(--surface);
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease;
}

.presence-dot.online { background: var(--online); opacity: 1; }

.search-wrap {
  position: relative;
  margin: 4px 16px 12px;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

#user-search {
  width: 100%;
  border: none;
  background: var(--surface-alt);
  border-radius: 24px;
  padding: 11px 16px 11px 40px;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: box-shadow 0.15s ease;
}

#user-search:focus { box-shadow: 0 0 0 3px var(--accent-soft); }

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border);
  max-height: 320px;
  overflow-y: auto;
  z-index: 20;
  display: none;
}

.search-results.open { display: block; }

.search-result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  transition: background 0.12s ease;
}

.search-result-item:hover { background: var(--surface-alt); }

.search-result-item .avatar { width: 36px; height: 36px; font-size: 13px; }

.search-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 16px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 14px;
  margin-bottom: 2px;
  position: relative;
  transition: background 0.12s ease;
}

.conversation-item:hover { background: var(--surface-alt); }
.conversation-item.active { background: var(--accent-soft); }

.conversation-meta {
  min-width: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.conversation-top strong {
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-top time {
  font-size: 11.5px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.conversation-preview {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

.empty-list { padding: 40px 20px; text-align: center; color: var(--text-muted); }
.empty-list .muted { font-size: 13px; }

/* ---------- Chat panel ---------- */
.chat-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(circle at 100% 0%, var(--accent-soft) 0%, transparent 30%),
    var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.back-btn { display: none; }

.chat-header-info { display: flex; flex-direction: column; min-width: 0; }
.chat-header-info strong { font-size: 15.5px; }
.peer-status { font-size: 12.5px; color: var(--text-muted); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.message-row { display: flex; }
.message-row.mine { justify-content: flex-end; }
.message-row.theirs { justify-content: flex-start; }

.bubble {
  max-width: min(65%, 520px);
  padding: 10px 16px;
  border-radius: 20px;
  position: relative;
  animation: bubble-in 0.18s ease;
  box-shadow: var(--shadow-sm);
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.message-row.mine .bubble {
  background: linear-gradient(135deg, var(--bubble-mine-start), var(--bubble-mine-end));
  color: var(--bubble-mine-text);
  border-bottom-right-radius: 6px;
}

.message-row.theirs .bubble {
  background: var(--bubble-theirs);
  color: var(--bubble-theirs-text);
  border-bottom-left-radius: 6px;
}

.bubble p { margin: 0; font-size: 14.5px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }

.bubble-time {
  display: block;
  font-size: 10.5px;
  opacity: 0.7;
  margin-top: 4px;
  text-align: right;
}

.thread-empty {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 0 24px 8px;
  align-items: center;
}

.typing-indicator[hidden] {
  display: none;
}

.typing-indicator .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.3s; }

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

.message-form {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.ghost-btn { color: var(--text-muted); }

#message-input {
  flex: 1;
  border: 1.5px solid var(--border);
  background: var(--surface-alt);
  border-radius: 24px;
  padding: 12px 18px;
  font-size: 14.5px;
  color: var(--text);
  outline: none;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

#message-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  background: var(--surface);
}

.send-btn {
  width: 42px;
  height: 42px;
  min-width: 42px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--bubble-mine-start), var(--bubble-mine-end));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.send-btn:hover { transform: scale(1.06); box-shadow: var(--shadow-md); }
.send-btn:active { transform: scale(0.96); }

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  max-width: 320px;
}

.empty-illustration { opacity: 0.7; margin-bottom: 12px; }
.empty-state h2 { color: var(--text); margin: 0 0 8px; font-size: 20px; }
.empty-state p { margin: 0; font-size: 14px; }

/* ---------- Flash messages ---------- */
.flash-message {
  margin: 4px 16px 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: #fce8e6;
  color: var(--danger);
  font-size: 13px;
}

/* ---------- Group / attachment message extras ---------- */
.bubble-sender {
  display: block;
  font-size: 11.5px;
  font-weight: 700;
  opacity: 0.75;
  margin-bottom: 3px;
}

.bubble.has-media {
  padding: 4px;
  overflow: hidden;
}

.bubble.has-media .bubble-time {
  padding: 4px 6px 0;
}

.bubble-media {
  display: block;
  max-width: 280px;
  max-height: 320px;
  width: 100%;
  border-radius: 16px;
  object-fit: cover;
}

.bubble-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.06);
  color: inherit;
  font-size: 13.5px;
  max-width: 240px;
}

.bubble-file span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-progress {
  padding: 0 24px 8px;
  font-size: 12.5px;
  color: var(--text-muted);
}

/* ---------- New group modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: 20px;
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 420px;
  padding: 24px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h3 { margin: 0; font-size: 18px; }

#group-form { display: flex; flex-direction: column; gap: 16px; }

#group-form .field { position: relative; }

#group-form input[type="text"] {
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

#group-form input[type="text"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#group-form .search-results { top: calc(100% + 4px); }

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 16px;
  padding: 5px 6px 5px 12px;
  font-size: 13px;
}

.chip button {
  border: none;
  background: transparent;
  color: inherit;
  font-size: 16px;
  line-height: 1;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chip button:hover { background: rgba(0, 0, 0, 0.1); }

#group-submit-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .chat-panel { display: none; }
  .app-shell.has-active .sidebar { display: none; }
  .app-shell.has-active .chat-panel { display: flex; }
  .back-btn { display: flex; }
}
