:root {
  --bg: #000;
  --phone-bg: #0b0b0d;
  --header-bg: rgba(28, 28, 30, 0.9);
  --border: #2c2c2e;
  --me: #0a84ff;
  --them: #26262a;
  --text: #f2f2f7;
  --sub: #8e8e93;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
}

.phone {
  width: min(440px, 100vw);
  height: min(860px, 100vh);
  background: var(--phone-bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 22px;
  position: relative;
}

@media (max-width: 460px) {
  .phone { border-radius: 0; border: none; height: 100vh; }
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
.avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5e5ce6, #bf5af2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 16px; flex-shrink: 0;
}
.who { flex: 1; min-width: 0; }
.name { font-weight: 600; font-size: 16px; cursor: pointer; }
.sub { font-size: 12px; color: var(--sub); }
.sub.online { color: #30d158; }
.room-btn {
  background: none; border: 1px solid var(--border);
  color: var(--me); font-size: 12px; padding: 6px 10px;
  border-radius: 20px; cursor: pointer; flex-shrink: 0;
}
.room-btn:active { opacity: 0.6; }
.icon-btn {
  background: none; border: none; color: var(--text);
  font-size: 20px; cursor: pointer; padding: 4px 6px;
  line-height: 1; flex-shrink: 0;
}
.icon-btn:active { opacity: 0.5; }
.icon-btn.plus {
  color: var(--me); font-size: 26px; font-weight: 300;
  width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--border);
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.messages::-webkit-scrollbar { width: 0; }

.name-label {
  font-size: 11px;
  color: var(--sub);
  margin: 8px 0 2px 12px;
}
.row {
  display: flex;
  margin-top: 2px;
  align-items: center;
  gap: 6px;
}
.row.me { justify-content: flex-end; }
.row.them { justify-content: flex-start; }

.bubble-wrap { position: relative; max-width: 74%; }

.bubble {
  padding: 8px 13px;
  border-radius: 19px;
  font-size: 16px;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: pop 0.14s ease-out;
  cursor: default;
}
@keyframes pop {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.bubble.me { background: var(--me); color: #fff; border-bottom-right-radius: 5px; }
.bubble.them { background: var(--them); color: var(--text); border-bottom-left-radius: 5px; }
.bubble.image { padding: 3px; overflow: hidden; }
.bubble.image img {
  display: block;
  max-width: 240px;
  max-height: 320px;
  width: auto; height: auto;
  border-radius: 16px;
}

/* React trigger button (appears on hover) */
.react-btn {
  opacity: 0;
  border: none;
  background: #1c1c1e;
  color: var(--sub);
  width: 26px; height: 26px;
  border-radius: 50%;
  font-size: 13px;
  cursor: pointer;
  transition: opacity 0.12s;
  flex-shrink: 0;
}
.row:hover .react-btn { opacity: 1; }

/* Reaction badges on a bubble */
.reactions {
  position: absolute;
  top: -14px;
  display: flex;
  gap: 3px;
}
.row.me .reactions { right: 6px; }
.row.them .reactions { left: 6px; }
.reaction {
  background: #1c1c1e;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1px 6px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  white-space: nowrap;
}
.reaction.mine { border-color: var(--me); background: #0a2a4d; }

.stamp {
  text-align: center;
  font-size: 11px;
  color: var(--sub);
  margin: 12px 0 4px;
}

.read-status {
  text-align: right;
  font-size: 11px;
  color: var(--sub);
  margin: 2px 6px 2px 0;
}

/* Typing indicator */
.typing-row { padding: 0 12px 4px; }
.bubble.typing { display: inline-flex; gap: 4px; padding: 12px 14px; animation: none; }
.bubble.typing span {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--sub); display: inline-block;
  animation: blink 1.3s infinite ease-in-out;
}
.bubble.typing span:nth-child(2) { animation-delay: 0.2s; }
.bubble.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* Composer */
.composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.composer input#input {
  flex: 1;
  background: #1c1c1e;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 9px 14px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.composer input#input:focus { border-color: #48484a; }
#send {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: none;
  background: var(--me);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s, opacity 0.1s;
}
#send:disabled { opacity: 0.35; cursor: default; }
#send:not(:disabled):active { transform: scale(0.88); }

/* Reaction picker popup */
.react-picker {
  position: fixed;
  background: #1c1c1e;
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 6px 8px;
  display: flex;
  gap: 4px;
  z-index: 50;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
}
.react-picker button {
  background: none; border: none;
  font-size: 22px; cursor: pointer;
  padding: 4px; border-radius: 50%;
  transition: transform 0.1s;
}
.react-picker button:hover { transform: scale(1.25); }

/* FaceTime call overlay */
.call {
  position: absolute;
  inset: 0;
  background: #000;
  z-index: 120;
  overflow: hidden;
}
.video-grid {
  width: 100%;
  height: 100%;
  display: grid;
  gap: 3px;
  background: #000;
  grid-template-columns: 1fr;
}
.video-grid.two { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
.video-grid.many { grid-template-columns: 1fr 1fr; }
.tile {
  position: relative;
  background: #111;
  overflow: hidden;
  min-height: 0;
}
.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.tile .tile-name {
  position: absolute;
  bottom: 6px;
  left: 8px;
  font-size: 12px;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  padding: 2px 8px;
  border-radius: 10px;
}
.local-video {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 96px;
  height: 132px;
  object-fit: cover;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background: #222;
  transform: scaleX(-1);
}
.call-status {
  position: absolute;
  top: 24px;
  left: 0; right: 0;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}
.call-controls {
  position: absolute;
  bottom: 30px;
  left: 0; right: 0;
  display: flex;
  justify-content: center;
  gap: 22px;
}
.call-round {
  width: 62px; height: 62px;
  border-radius: 50%;
  border: none;
  font-size: 24px;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, opacity 0.1s;
}
.call-round:active { transform: scale(0.9); }
.call-round.decline { background: #ff3b30; }
.call-round.accept { background: #30d158; }
.call-round.neutral { background: rgba(255, 255, 255, 0.18); }
.call-round.neutral.off { background: #fff; color: #000; }

.incoming {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 12, 0.9);
  backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  z-index: 2;
}
.incoming-name { font-size: 28px; font-weight: 600; }
.incoming-sub { font-size: 15px; color: var(--sub); margin-bottom: 40px; }
.incoming-actions { display: flex; gap: 60px; }

/* Name overlay */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.overlay-card {
  background: var(--phone-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 24px;
  width: 100%;
  max-width: 320px;
  text-align: center;
}
.overlay-icon { font-size: 44px; margin-bottom: 8px; }
.overlay-card h2 { margin: 0 0 18px; font-size: 20px; }
.overlay-card input {
  width: 100%;
  background: #1c1c1e;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  margin-bottom: 14px;
}
.overlay-card input:focus { border-color: var(--me); }
.overlay-card button {
  width: 100%;
  background: var(--me);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.overlay-card button:active { opacity: 0.8; }
