@import url('https://fonts.googleapis.com/css2?family=Fraunces:ital,wght@0,500;0,600;1,500&family=Inter:wght@400;500;600&display=swap');

:root {
  --ink: #05070A;
  --surface: #0D1712;
  --surface-raised: #13221B;
  --border: #1E3327;
  --text: #E8FFF0;
  --text-muted: #6E9E86;
  --neon: #39FF88;
  --neon-dim: #124A2C;
  --neon-glow: rgba(57, 255, 136, 0.45);
  --mint: #6BFFC8;
  --danger: #FF5C7A;
  --radius: 10px;
  --font-display: 'Fraunces', serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--neon); }

/* ---- Auth screen ---- */
.auth-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
}

.wordmark {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: 34px;
  margin: 0 0 4px;
  color: var(--neon);
  text-shadow: 0 0 14px var(--neon-glow);
}

.tagline {
  color: var(--text-muted);
  margin: 0 0 32px;
  font-size: 14px;
}

.auth-card h2 {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 19px;
  margin: 0 0 20px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.field input, .field textarea {
  width: 100%;
  padding: 11px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14.5px;
}

.field input:focus, .field textarea:focus {
  outline: 2px solid var(--neon);
  outline-offset: 1px;
  border-color: var(--neon);
  box-shadow: 0 0 10px var(--neon-glow);
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--neon);
  color: #04150C;
  border: none;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 14.5px;
  cursor: pointer;
  margin-top: 6px;
  box-shadow: 0 0 16px var(--neon-glow);
  transition: box-shadow 0.15s, filter 0.15s;
}

.btn-primary:hover { filter: brightness(1.1); box-shadow: 0 0 22px var(--neon-glow); }
.btn-primary:disabled { opacity: 0.6; cursor: default; box-shadow: none; }

.btn-link {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13.5px;
  cursor: pointer;
  padding: 0;
  margin-top: 16px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.btn-link:hover { color: var(--text); }

.error-box {
  font-size: 13.5px;
  min-height: 18px;
  margin-top: 10px;
}

/* ---- App shell (phase 1: just the profile screen) ---- */
.app-shell {
  max-width: 480px;
  margin: 0 auto;
  padding: 32px 20px 80px;
}

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

.app-header .wordmark { font-size: 24px; margin: 0; }

.btn-ghost {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius);
  padding: 8px 14px;
  font-size: 13.5px;
  cursor: pointer;
}

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

.profile-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
}

.avatar-row {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.avatar-row img {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-raised);
}

.username-label {
  color: var(--text-muted);
  font-size: 14px;
  margin: 2px 0 0;
}

.save-status {
  font-size: 13px;
  margin-top: 4px;
  min-height: 16px;
}

.notice {
  background: var(--surface-raised);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  color: var(--text-muted);
  font-size: 13.5px;
  margin-top: 28px;
}

/* ============================================================
   Chat shell
   ============================================================ */
.chat-shell {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

.wordmark.small { font-size: 20px; margin: 0; }

/* ---- Sidebar ---- */
.sidebar {
  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 16px 14px;
  min-width: 0;
  gap: 8px;
}

.sidebar-header .wordmark.small {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-actions { display: flex; gap: 6px; flex-shrink: 0; }

.icon-btn {
  background: none;
  border: none;
  font-size: 17px;
  line-height: 1;
  padding: 7px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover { background: var(--surface-raised); }

.new-chat-panel {
  padding: 0 16px 12px;
  border-bottom: 1px solid var(--border);
}

.new-chat-panel input {
  width: 100%;
  padding: 9px 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  margin-bottom: 8px;
}

.new-chat-tabs {
  display: flex;
  gap: 4px;
  margin: 10px 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 3px;
}

.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 6px;
  border-radius: 7px;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--surface-raised);
  color: var(--text);
}

.group-name-field { margin-bottom: 0; }

.selected-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.selected-chips:empty { margin-bottom: 0; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--surface-raised);
  border: 1px solid var(--neon-dim);
  color: var(--text);
  border-radius: 14px;
  padding: 3px 6px 3px 10px;
  font-size: 12.5px;
}

.chip-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 2px;
}
.chip-remove:hover { color: var(--text); }

.user-result { position: relative; }

.user-result.selected { background: var(--surface-raised); }

.check-mark {
  margin-left: auto;
  color: var(--neon);
  font-weight: 700;
  padding-left: 6px;
  flex-shrink: 0;
}

#create-group-btn { margin-top: 4px; }

.result-list, .conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
}

.conversation-list { flex: 1; }

.list-empty, .list-error {
  padding: 16px;
  color: var(--text-muted);
  font-size: 13.5px;
}

.conversation-item, .user-result {
  display: flex;
  gap: 11px;
  align-items: center;
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid transparent;
  border-left: 3px solid transparent;
}

.conversation-item:hover, .user-result:hover { background: var(--surface); }

.conversation-item.active {
  background: rgba(57, 255, 136, 0.1);
  border-left-color: var(--neon);
}
.conversation-item.active .conv-name { color: var(--neon); }

.conv-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--surface-raised);
}

.conv-meta { min-width: 0; flex: 1; }

.conv-top-row, .conv-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.conv-name {
  font-weight: 600;
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time { font-size: 12px; color: var(--text-muted); flex-shrink: 0; }

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

.unread-badge {
  background: var(--neon);
  color: #04150C;
  font-size: 11px;
  font-weight: 700;
  border-radius: 10px;
  padding: 1px 7px;
  flex-shrink: 0;
  box-shadow: 0 0 8px var(--neon-glow);
}

.muted-icon { font-size: 11px; flex-shrink: 0; }

/* ---- Thread pane ---- */
.thread-pane { display: flex; flex-direction: column; min-width: 0; }

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text-muted);
  text-align: center;
}

/* Same bug pattern as .typing-indicator: an unconditional `display` beats
   the `hidden` attribute, so without this the empty-state text (and the
   thread's composer box below) both stayed visible/present regardless of
   which one JS actually meant to show. */
.empty-state[hidden] { display: none; }

.thread { display: flex; flex-direction: column; height: 100%; min-width: 0; }
.thread[hidden] { display: none; }

.thread-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}

.thread-header img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.thread-header > div {
  min-width: 0;
  flex: 1;
}

.thread-header.clickable { cursor: pointer; }

.thread-name, .thread-status {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-name { font-weight: 600; font-size: 15px; }
.thread-status { font-size: 12.5px; color: var(--text-muted); }

.back-btn { display: none; }

.group-members-panel {
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  max-height: 260px;
  overflow-y: auto;
}

.member-list {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.member-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.member-row .conv-name { font-size: 14px; }
.member-row .conv-preview { font-size: 12px; text-transform: capitalize; }
.member-row .remove-member-btn { margin-left: auto; font-size: 12.5px; }

.group-panel-actions {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.btn-link.danger { color: var(--danger); }

.sender-name {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 4px 2px;
}

.message-list {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message-row { display: flex; flex-direction: column; max-width: 68%; }
.message-row.mine { align-self: flex-end; align-items: flex-end; }
.message-row.theirs { align-self: flex-start; align-items: flex-start; }

.message-bubble {
  position: relative;
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 14.5px;
  word-break: break-word;
}

.message-row.mine .message-bubble {
  background: var(--neon);
  color: #04150C;
  border-bottom-right-radius: 4px;
  box-shadow: 0 0 10px var(--neon-glow);
}

.message-row.theirs .message-bubble {
  background: var(--surface-raised);
  border-bottom-left-radius: 4px;
}

.deleted-label, .edited-tag { opacity: 0.65; font-style: italic; font-size: 13px; }

.message-meta {
  display: flex;
  gap: 6px;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 3px;
  padding: 0 4px;
}

.msg-hover-bar {
  position: absolute;
  top: -32px;
  display: none;
  gap: 2px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 3px 5px;
  z-index: 2;
  max-width: 90vw;
}

/* Anchor the bar so it grows AWAY from the screen edge each side sits near.
   A short "theirs" bubble near the sidebar would otherwise have the bar
   grow leftward off-screen (right:0 on a narrow bubble); a short "mine"
   bubble has the same problem on the right. */
.message-row.mine .msg-hover-bar { right: 0; left: auto; }
.message-row.theirs .msg-hover-bar { left: 0; right: auto; }

.message-bubble:hover .msg-hover-bar { display: flex; }

.react-btn, .icon-btn.edit-btn, .icon-btn.delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 15px;
  padding: 3px 5px;
  border-radius: 50%;
}

.react-btn:hover, .icon-btn.edit-btn:hover, .icon-btn.delete-btn:hover { background: var(--border); }

.reactions-row { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.reactions-row.mine { justify-content: flex-end; }

.reaction-pill {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 12px;
  padding: 1px 8px;
  cursor: pointer;
  color: var(--text);
}

.reaction-pill.mine { border-color: var(--neon); }

.edit-textarea {
  width: 100%;
  min-width: 180px;
  background: rgba(0,0,0,0.15);
  border: 1px solid rgba(0,0,0,0.25);
  border-radius: 8px;
  padding: 6px 8px;
  font-family: var(--font-body);
  font-size: 14.5px;
  color: inherit;
  resize: vertical;
}

.edit-actions { display: flex; gap: 10px; margin-top: 6px; }
.edit-actions .btn-link { margin: 0; font-size: 12.5px; }

.typing-indicator {
  padding: 0 20px 6px;
  font-size: 12.5px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* [hidden] must win over the `display: flex` above, or the browser's
   built-in "hide this when `hidden` is set" behavior never kicks in
   and JS toggling `.hidden = true` has no visible effect. */
.typing-indicator[hidden] { display: none; }

.typing-dots { display: inline-flex; gap: 2px; }
.typing-dots span {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

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

.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  min-width: 0;
}

.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 120px;
  padding: 10px 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14.5px;
  line-height: 1.4;
}

.composer textarea:focus { outline: 2px solid var(--neon); outline-offset: 1px; box-shadow: 0 0 10px var(--neon-glow); }

.composer-send { width: auto; padding: 10px 18px; margin-top: 0; border-radius: 18px; flex-shrink: 0; }

/* ---- Responsive: tighten up before the full mobile stack kicks in ---- */
@media (max-width: 900px) {
  .message-list { padding: 16px; }
  .thread-header { padding: 12px 16px; }
  .composer { padding: 12px 16px; }
  .message-row { max-width: 78%; }
}

/* ---- Responsive: stack sidebar/thread on narrow screens ---- */
@media (max-width: 760px) {
  .chat-shell { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .thread-pane { display: none; }
  body.conversation-open .sidebar { display: none; }
  body.conversation-open .thread-pane { display: flex; }
  .back-btn { display: inline-flex; }
  .message-row { max-width: 85%; }
}

@media (max-width: 400px) {
  .message-row { max-width: 92%; }
  .thread-header, .composer { padding-left: 12px; padding-right: 12px; }
}