:root {
  color-scheme: dark;
  --bg: #0b0810;
  --bg-soft: #120d18;
  --surface: #1a1422;
  --surface-2: #221a2c;
  --kaia-bubble: #2a2136;
  --kaia-bubble-2: #332840;
  --kaia-border: rgba(255, 255, 255, 0.09);
  --text: #f5f2f8;
  --muted: rgba(228, 220, 236, 0.6);
  --hairline: rgba(255, 255, 255, 0.08);
  --pink: #ff4f8b;
  --pink-2: #b760ff;
  --online: #38d17a;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --topbar-h: 60px;
  --composer-h: 68px;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; cursor: pointer; }

.app {
  position: relative;
  width: 100vw;
  height: 100dvh;
  background:
    radial-gradient(110% 60% at 50% 0%, rgba(183, 96, 255, 0.18), transparent 60%),
    radial-gradient(90% 50% at 80% 100%, rgba(255, 79, 139, 0.12), transparent 60%),
    linear-gradient(180deg, #0b0810 0%, #0a0710 100%);
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  overflow: hidden;
  isolation: isolate;
}

/* ---------- Topbar ---------- */
.topbar {
  position: relative;
  z-index: 5;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 6px 0;
  display: grid;
  grid-template-columns: 40px 1fr auto;
  align-items: center;
  background: rgba(11, 8, 16, 0.72);
  backdrop-filter: blur(22px) saturate(140%);
  -webkit-backdrop-filter: blur(22px) saturate(140%);
  border-bottom: 1px solid var(--hairline);
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #ece8f2;
  transition: background 150ms ease, transform 120ms ease;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.06); transform: scale(0.96); }

.topbar-actions { display: flex; gap: 2px; padding-right: 4px; }

.peer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 2px;
  min-width: 0;
}

.avatar {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  background: #222;
  flex: none;
  box-shadow: 0 0 0 1.5px rgba(255, 79, 139, 0.55), 0 4px 14px rgba(255, 79, 139, 0.22);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.avatar .dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--online);
  box-shadow: 0 0 0 2px var(--bg);
}

.peer-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.peer-name { font-weight: 600; font-size: 15.5px; letter-spacing: 0.1px; }
.peer-status { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

/* ---------- Hero (persistent persona banner) ---------- */
.hero {
  position: relative;
  width: 100%;
  height: clamp(360px, 48dvh, 480px);
  overflow: hidden;
  border-bottom: 1px solid var(--hairline);
  background: #0d0a13;
}
.hero-photo-bg {
  position: absolute;
  inset: -20px;
  width: calc(100% + 40px);
  height: calc(100% + 40px);
  object-fit: cover;
  object-position: 50% 20%;
  filter: blur(32px) saturate(130%) brightness(60%);
  transform: scale(1.1);
  opacity: 0.9;
}
.hero-photo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: 50% 50%;
  filter: saturate(112%) contrast(103%);
  z-index: 1;
}
.hero-shade {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, rgba(11, 8, 16, 0) 0%, rgba(11, 8, 16, 0) 60%, rgba(11, 8, 16, 0.6) 92%, var(--bg) 100%),
    radial-gradient(90% 70% at 50% 0%, rgba(183, 96, 255, 0.1), transparent 70%);
  pointer-events: none;
  z-index: 2;
}
.hero-name {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: #fff;
  text-shadow: 0 2px 18px rgba(0, 0, 0, 0.75);
  pointer-events: none;
  z-index: 3;
}
.hero-name::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--online);
  box-shadow: 0 0 0 2px rgba(11, 8, 16, 0.6), 0 0 12px rgba(56, 209, 122, 0.7);
  flex: none;
}

/* ---------- Chat ---------- */
.chat {
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 12px;
  background:
    radial-gradient(120% 60% at 50% 0%, rgba(183, 96, 255, 0.06), transparent 65%),
    transparent;
}
.chat::-webkit-scrollbar { width: 0; }

.messages {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
}

.row { display: flex; align-items: flex-end; gap: 8px; width: 100%; animation: rise 180ms ease-out both; }
.row.me { justify-content: flex-end; }
.row.kaia { justify-content: flex-start; }

.row .mini-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  overflow: hidden;
  flex: none;
  background: #2a2136;
  box-shadow: 0 0 0 1px rgba(255, 79, 139, 0.35);
  opacity: 0;
  transform: translateY(2px);
  transition: opacity 140ms ease;
}
.row.kaia.has-avatar .mini-avatar { opacity: 1; transform: none; }
.row.kaia .mini-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.bubble {
  max-width: min(78%, 420px);
  padding: 10px 14px 11px;
  font-size: 15.5px;
  line-height: 20px;
  border-radius: 22px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  color: var(--text);
}

.kaia .bubble {
  background: linear-gradient(180deg, var(--kaia-bubble-2) 0%, var(--kaia-bubble) 100%);
  border: 1px solid var(--kaia-border);
  border-bottom-left-radius: 8px;
  color: #faf7ff;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.04) inset,
    0 10px 26px rgba(0, 0, 0, 0.38);
}

.me .bubble {
  background: linear-gradient(135deg, var(--pink) 0%, var(--pink-2) 100%);
  color: #fff;
  border-bottom-right-radius: 8px;
  box-shadow: 0 8px 22px rgba(255, 79, 139, 0.28);
}

.row.kaia + .row.kaia .bubble { border-bottom-left-radius: 22px; border-top-left-radius: 22px; }
.row.kaia:has(+ .row.kaia) .bubble { border-bottom-left-radius: 8px; border-top-left-radius: 22px; }
.row.me + .row.me .bubble { border-bottom-right-radius: 22px; border-top-right-radius: 22px; }
.row.me:has(+ .row.me) .bubble { border-bottom-right-radius: 8px; border-top-right-radius: 22px; }

.timestamp {
  width: 100%;
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.3px;
  margin: 10px 0 4px;
}

.read-receipt {
  align-self: flex-end;
  margin: 2px 4px 2px 0;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.2px;
  color: rgba(228, 220, 236, 0.5);
  animation: rise 180ms ease-out both;
}

/* ---------- Typing ---------- */
.typing-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding-top: 6px;
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}
.typing-row[hidden] { display: none; }

.typing-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  overflow: hidden;
  flex: none;
  background: #2a2136;
  box-shadow: 0 0 0 1px rgba(255, 79, 139, 0.35);
}
.typing-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.typing-bubble {
  background: linear-gradient(180deg, var(--kaia-bubble-2) 0%, var(--kaia-bubble) 100%);
  border: 1px solid var(--kaia-border);
  border-radius: 22px;
  border-bottom-left-radius: 8px;
  padding: 10px 14px;
  display: flex;
  gap: 4px;
  align-items: center;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.04) inset,
    0 10px 26px rgba(0, 0, 0, 0.38);
}
.typing-bubble span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(245, 242, 248, 0.75);
  animation: bounce 1050ms infinite ease-in-out;
}
.typing-bubble span:nth-child(2) { animation-delay: 130ms; }
.typing-bubble span:nth-child(3) { animation-delay: 260ms; }

/* ---------- Composer ---------- */
.composer {
  position: relative;
  z-index: 5;
  display: grid;
  grid-template-columns: 40px 1fr 44px;
  align-items: end;
  gap: 8px;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  background: rgba(11, 8, 16, 0.82);
  backdrop-filter: blur(22px) saturate(140%);
  -webkit-backdrop-filter: blur(22px) saturate(140%);
  border-top: 1px solid var(--hairline);
}

.composer-icon {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  color: #ece8f2;
  display: grid;
  place-items: center;
  align-self: end;
}

.input-wrap {
  min-height: 40px;
  display: flex;
  align-items: end;
  padding: 8px 14px;
  border-radius: 22px;
  background: var(--surface);
  border: 1px solid var(--hairline);
}

textarea {
  width: 100%;
  max-height: 120px;
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 15.5px;
  line-height: 20px;
  padding: 0;
  overflow: hidden;
}
textarea::placeholder { color: rgba(228, 220, 236, 0.42); }

.send-button {
  width: 44px;
  height: 44px;
  align-self: end;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--pink) 0%, var(--pink-2) 100%);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: 0 8px 20px rgba(255, 79, 139, 0.35);
  transition: transform 130ms ease, opacity 130ms ease, filter 130ms ease;
}
.send-button:active { transform: scale(0.94); }
.send-button:disabled {
  opacity: 0.5;
  filter: saturate(60%);
  box-shadow: none;
}

/* ---------- Desktop frame ---------- */
@media (min-width: 720px) {
  body { display: grid; place-items: center; background: #060309; }
  .app {
    width: min(100vw, 430px);
    height: min(100dvh, 900px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 34px;
    box-shadow: 0 40px 120px rgba(0, 0, 0, 0.55);
  }
}

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

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