:root {
  --bg: #f4f0ea;
  --panel: #ffffff;
  --ink: #1c1c1c;
  --muted: #6b6b6b;
  --accent: #0f7a76;
  --accent-soft: #dff0ee;
  --bubble-you: #0f7a76;
  --bubble-them: #efefef;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  background: radial-gradient(circle at top, #fff8ef 0%, #efe5d8 55%, #e4d7c7 100%);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.app {
  width: min(980px, 100%);
  background: var(--panel);
  border-radius: 28px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 288px 1fr;
  overflow: hidden;
}

.friends {
  background: #f7f5f2;
  padding: 26px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  border-right: 1px solid #efe8de;
  height: 640px;
  overflow: hidden;
}

.friends h2 {
  font-size: 14px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
}

.friend-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding-right: 8px;
}

.friend {
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.7);
  text-align: left;
  padding: 10px 12px;
  border-radius: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--ink);
  width: 100%;
  position: relative;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease,
    background 0.2s ease;
}

.friend:hover {
  border-color: #e3d6c7;
  background: #fff;
  box-shadow: 0 10px 24px rgba(15, 122, 118, 0.08);
  transform: translateY(-1px);
}

.friend:focus-visible {
  outline: 2px solid rgba(15, 122, 118, 0.45);
  outline-offset: 2px;
}

.friend-initial {
  display: inline-flex;
  width: 30px;
  height: 30px;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: linear-gradient(140deg, #0f7a76, #0b5f5b);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  flex: 0 0 30px;
}

.friend-name {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
  width: auto;
  overflow: hidden;
  padding: 6px 10px;
  border-radius: 12px;
  background: rgba(15, 122, 118, 0.08);
  line-height: 1.2;
}

.friend-name-text {
  display: block;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.friend-subline {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.friend-subline.typing {
  color: var(--accent);
}

.friend-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  flex: 0 0 auto;
}

.presence-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c9bdb1;
}

.presence-dot.online {
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 122, 118, 0.12);
}

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

.unread-badge.show {
  display: inline-flex;
}

.friend.active {
  background: #fff;
  border-color: rgba(15, 122, 118, 0.35);
  color: var(--accent);
  box-shadow: 0 12px 26px rgba(15, 122, 118, 0.14);
}

.friend.active .friend-initial {
  background: linear-gradient(140deg, #0f7a76, #0f7a76);
  box-shadow: 0 8px 16px rgba(15, 122, 118, 0.2);
}

.friend.active .friend-name {
  background: rgba(15, 122, 118, 0.12);
  color: var(--accent);
}

.chat {
  display: flex;
  flex-direction: column;
  height: 640px;
  background: linear-gradient(165deg, #ffffff 0%, #fbfaf8 70%);
  overflow: hidden;
}

.chat-header {
  padding: 24px 28px 16px;
  border-bottom: 1px solid #f0ede7;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.chat-meta {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-header h1 {
  margin: 0;
  font-size: 22px;
}

.chat-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.chat-typing {
  margin: 0;
  font-size: 12px;
  color: var(--accent);
}

.chat-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--muted);
}

.refresh-button {
  width: 32px;
  height: 32px;
  border: 1px solid #d7c6b7;
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.refresh-button svg {
  width: 16px;
  height: 16px;
  fill: var(--accent);
}

.messages {
  flex: 1;
  padding: 28px;
  overflow-y: auto;
  position: relative;
  display: flex;
  flex-direction: column;
}

.message-stream {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-loader,
.history-start {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  margin: 0 auto 12px;
  width: max-content;
}

.history-loader {
  background: #fff;
  border: 1px solid #efe0d1;
  color: var(--muted);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.06);
}

.history-start {
  background: #efe0d1;
  color: #5a4f45;
  text-align: center;
}

.message {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.35;
  position: relative;
  animation: rise 0.4s ease;
}

.message.you {
  align-self: flex-end;
  background: var(--bubble-you);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message.them {
  align-self: flex-start;
  background: var(--bubble-them);
  color: var(--ink);
  border-bottom-left-radius: 4px;
}

.message time {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
  margin-top: 4px;
  color: rgba(0, 0, 0, 0.5);
}

.message.you time {
  color: rgba(255, 255, 255, 0.7);
}

.receipt {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.4);
  display: inline-block;
}

.receipt.sent {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.receipt.delivered {
  background: rgba(255, 255, 255, 0.7);
}

.receipt.read {
  background: #fff;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.18);
}

.composer {
  padding: 18px 24px 24px;
  border-top: 1px solid #f0ede7;
  display: flex;
  gap: 12px;
}

.composer input {
  flex: 1;
  border: 1px solid #e1d7c9;
  border-radius: 16px;
  padding: 14px 16px;
  font-size: 15px;
  font-family: inherit;
  background: #fff;
}

.composer button {
  border: none;
  border-radius: 16px;
  padding: 14px 20px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.composer button:hover {
  filter: brightness(1.05);
}

.invite {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.invite button {
  border: none;
  border-radius: 14px;
  padding: 12px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.invite-output {
  display: none;
  background: #fff;
  border: 1px solid #efe8de;
  border-radius: 14px;
  padding: 12px;
  font-size: 13px;
  color: var(--muted);
}

.invite-link-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.copy-button {
  width: 38px;
  height: 38px;
  border: 1px solid #d7c6b7;
  border-radius: 10px;
  background: linear-gradient(145deg, #0f7a76, #0a5e5b);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 16px rgba(15, 122, 118, 0.18);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.copy-button svg {
  width: 32px;
  height: 32px;
  fill: #fff;
}

.copy-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 18px rgba(15, 122, 118, 0.28);
}

.copy-button:active {
  transform: translateY(0);
  box-shadow: 0 6px 12px rgba(15, 122, 118, 0.2);
}

.invite-output input {
  width: 100%;
  height: 38px;
  border: 1px solid #e1d7c9;
  border-radius: 10px;
  padding: 0 10px;
  font-size: 12px;
  background: #faf8f4;
}

.invite-join {
  display: none;
  gap: 8px;
  flex-direction: column;
}

.hidden {
  display: none;
}

.auth-panel {
  display: none;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.auth-panel input {
  border: 1px solid #e1d7c9;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  background: #fff;
}

.auth-panel button {
  padding: 10px 12px;
  border: none;
  border-radius: 12px;
  background: #1c1c1c;
  color: #fff;
  cursor: pointer;
}

.auth-error {
  display: none;
  font-size: 12px;
  color: #b54a3b;
}

.auth-status {
  display: none;
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #f0ece6;
  color: var(--muted);
  font-size: 12px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.logout-button {
  border: 1px solid #cdb8a5;
  background: linear-gradient(140deg, #1c1c1c, #3a3a3a);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.logout-button svg {
  width: 32px;
  height: 32px;
  fill: #fff;
}

.toast {
  position: fixed;
  top: 18px;
  right: 22px;
  background: #0f7a76;
  color: #fff;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  box-shadow: 0 10px 22px rgba(15, 122, 118, 0.25);
  opacity: 0;
  transform: translateY(-6px);
  visibility: hidden;
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
  z-index: 99999;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
  visibility: visible;
}

.cred-panel {
  display: none;
  background: #fff;
  border: 1px solid #efe8de;
  border-radius: 14px;
  padding: 12px;
  font-size: 12px;
  color: var(--muted);
  max-width: 100%;
}

.cred-panel strong {
  display: block;
  color: var(--ink);
  margin-bottom: 6px;
}

.cred-panel code {
  display: block;
  background: #faf8f4;
  border: 1px solid #e1d7c9;
  border-radius: 10px;
  padding: 6px 8px;
  font-size: 12px;
  color: var(--ink);
}

.cred-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  width: 100%;
  min-width: 0;
}

.cred-row input {
  flex: 1 1 auto;
  border: 1px solid #e1d7c9;
  border-radius: 10px;
  height: 38px;
  padding: 0 10px;
  font-size: 12px;
  background: #faf8f4;
  min-width: 0;
  width: 100%;
}

.invite-join input {
  border: 1px solid #e1d7c9;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  background: #fff;
}

.invite-join button {
  padding: 10px 12px;
  background: #1c1c1c;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 800px) {
  .app {
    grid-template-columns: 1fr;
  }

  .friends {
    flex-direction: row;
    overflow-x: auto;
    border-right: none;
    border-bottom: 1px solid #efe8de;
    padding: 18px 16px;
  }

  .friend-list {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding-right: 0;
  }

  .chat {
    height: auto;
    min-height: 70vh;
  }

  .friend {
    min-width: 200px;
  }
}
