:root {
  --ink: #2b1d4e;
  --muted: #7a6b99;
  --card: rgba(255, 255, 255, 0.86);
  --line: rgba(43, 29, 78, 0.12);
  --mine-a: #ff4fa3;
  --mine-b: #8a4dff;
  --theirs: #ffffff;
  --accent: #ff7a00;
  --radius: 22px;
  font-family: "Fredoka", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

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

body {
  background: #ff6ec7;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Animated rainbow backdrop */
.rainbow {
  position: fixed;
  inset: -20%;
  z-index: -1;
  background: linear-gradient(120deg,
    #ff3b3b, #ff9a3b, #ffe53b, #4dff7a, #3bd5ff, #5b5bff, #c13bff, #ff3b9d, #ff3b3b);
  background-size: 400% 400%;
  animation: rainbow 18s linear infinite;
}
.rainbow::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 20% 30%, rgba(255,255,255,.35) 0 8%, transparent 9%),
    radial-gradient(circle at 80% 20%, rgba(255,255,255,.25) 0 6%, transparent 7%),
    radial-gradient(circle at 70% 80%, rgba(255,255,255,.3) 0 10%, transparent 11%),
    radial-gradient(circle at 15% 85%, rgba(255,255,255,.2) 0 5%, transparent 6%);
  animation: float 12s ease-in-out infinite alternate;
}
@keyframes rainbow { to { background-position: 400% 400%; } }
@keyframes float { to { transform: translateY(-40px) rotate(3deg); } }

.brand {
  margin: 0;
  font-weight: 700;
  font-size: 2.4rem;
  letter-spacing: -0.5px;
  background: linear-gradient(90deg, #ff3b3b, #ff9a3b, #e6c200, #22c55e, #0ea5e9, #6d28d9, #db2777);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand.small { font-size: 1.35rem; }

/* ---------- Login ---------- */
.login-card {
  width: min(400px, calc(100vw - 32px));
  background: var(--card);
  backdrop-filter: blur(12px);
  border-radius: 32px;
  padding: 36px 30px 30px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(60, 0, 90, .35);
}
.logo { font-size: 4rem; line-height: 1; }
.bounce { animation: bounce 2.4s ease-in-out infinite; }
@keyframes bounce { 50% { transform: translateY(-10px) rotate(-6deg); } }
.tagline { color: var(--muted); margin: 6px 0 22px; }
.login-card form { display: grid; gap: 14px; text-align: left; }
.login-card label { display: grid; gap: 6px; font-weight: 500; }
.login-card input {
  font: inherit;
  padding: 12px 16px;
  border-radius: 16px;
  border: 2px solid var(--line);
  background: #fff;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}
.login-card input:focus { border-color: var(--mine-b); box-shadow: 0 0 0 4px rgba(138,77,255,.2); }
.error {
  background: #ffe1e6;
  color: #b0183d;
  border-radius: 14px;
  padding: 10px 14px;
  margin: 0 0 14px;
  animation: shake .4s;
}
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

button { font: inherit; cursor: pointer; }
.btn-primary {
  border: 0;
  border-radius: 999px;
  padding: 12px 20px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(90deg, var(--mine-a), var(--mine-b));
  box-shadow: 0 6px 18px rgba(138, 77, 255, .35);
  transition: transform .15s;
}
.btn-primary:hover { transform: translateY(-2px) scale(1.02); }
.btn-ghost {
  border: 2px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 6px 14px;
  color: var(--ink);
}
.btn-ghost:hover { background: #f6f0ff; }

/* ---------- Chat ---------- */
.chat {
  width: min(760px, 100vw);
  height: min(920px, 100dvh);
  display: flex;
  flex-direction: column;
  background: var(--card);
  backdrop-filter: blur(14px);
  box-shadow: 0 20px 60px rgba(60, 0, 90, .35);
  position: relative;
}
@media (min-width: 800px) {
  .chat { border-radius: 32px; height: min(920px, calc(100dvh - 48px)); overflow: hidden; }
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.7);
}
.header-actions { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.header-actions form { margin: 0; }
.avatar {
  position: relative;
  flex: none;
  width: 44px; height: 44px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 1.2rem;
  background: conic-gradient(#ff3b3b, #ffe53b, #4dff7a, #3bd5ff, #c13bff, #ff3b3b);
  text-shadow: 0 1px 3px rgba(0,0,0,.4);
}
.who { display: flex; flex-direction: column; }
.sub { color: var(--muted); font-size: .9rem; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.empty { margin: auto; color: var(--muted); font-size: 1.2rem; }

.day {
  align-self: center;
  font-size: .78rem;
  color: var(--muted);
  background: rgba(255,255,255,.8);
  padding: 3px 12px;
  border-radius: 999px;
  margin: 10px 0 4px;
}

.msg {
  max-width: min(78%, 480px);
  display: flex;
  flex-direction: column;
  animation: pop .35s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(.6); opacity: 0; } }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: 0 3px 10px rgba(60, 0, 90, .12);
}
.mine .bubble {
  color: #fff;
  background: linear-gradient(135deg, var(--mine-a), var(--mine-b));
  border-bottom-right-radius: 6px;
}
.theirs .bubble {
  background: var(--theirs);
  border-bottom-left-radius: 6px;
}
.bubble.jumbo { font-size: 2.6rem; background: none !important; box-shadow: none; padding: 0 4px; }
.bubble.media { padding: 5px; }
.bubble img {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 18px;
  cursor: zoom-in;
}
.bubble audio { display: block; width: 260px; max-width: 100%; }
.time { font-size: .72rem; color: var(--muted); margin: 2px 8px 4px; }

/* Composer */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(255,255,255,.75);
}
.icon-btn {
  flex: none;
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 50%;
  background: #f3ecff;
  font-size: 1.3rem;
  cursor: pointer;
  transition: transform .15s, background .2s;
}
.icon-btn:hover { transform: scale(1.12) rotate(-8deg); background: #ffe6f4; }
.icon-btn.active { background: #ffd6e9; }
#text {
  flex: 1;
  min-width: 0;
  font: inherit;
  resize: none;
  max-height: 140px;
  padding: 10px 14px;
  border-radius: 21px;
  border: 2px solid var(--line);
  outline: none;
  background: #fff;
}
#text:focus { border-color: var(--mine-b); }
.send-btn {
  flex: none;
  width: 46px; height: 46px;
  border: 0;
  border-radius: 50%;
  color: #fff;
  font-size: 1.3rem;
  background: linear-gradient(135deg, #ff9a3b, var(--mine-a), var(--mine-b));
  box-shadow: 0 6px 16px rgba(255, 79, 163, .4);
  transition: transform .15s;
}
.send-btn:hover { transform: scale(1.1) rotate(10deg); }

/* Emoji picker */
.emoji-panel {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 2px;
  max-height: 210px;
  overflow-y: auto;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: #fff;
}
.emoji-panel[hidden] { display: none; }
.emoji-panel button {
  border: 0;
  background: none;
  font-size: 1.6rem;
  border-radius: 10px;
  padding: 4px 0;
  transition: transform .1s;
}
.emoji-panel button:hover { background: #f6f0ff; transform: scale(1.25); }

/* Recording bar */
.recording {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: #fff0f6;
  font-weight: 500;
}
.recording[hidden] { display: none; }
.recording .btn-ghost { margin-left: auto; }
.rec-dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: #ff2d55;
  animation: pulse 1s infinite;
}
@keyframes pulse { 50% { opacity: .3; transform: scale(.7); } }

.toast {
  position: fixed;
  z-index: 40;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: .9rem;
  max-width: calc(100% - 32px);
}
.toast[hidden] { display: none; }

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(20, 0, 40, .85);
  display: grid;
  place-items: center;
  cursor: zoom-out;
  z-index: 10;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 16px; }

/* ---------- Background presets (also used by the picker swatches) ---------- */
.bg-sunset  { background-image: linear-gradient(120deg, #ff512f, #ff9a3b, #f9d423, #ff5e62, #dd2476, #ff512f); }
.bg-ocean   { background-image: linear-gradient(120deg, #00c6ff, #0072ff, #00f2a9, #1e3c72, #00c6ff); }
.bg-galaxy  { background-image: linear-gradient(120deg, #0f0c29, #302b63, #8e2de2, #24243e, #ff00cc, #0f0c29); }
.bg-candy   { background-image: linear-gradient(120deg, #ffafbd, #ffc3a0, #c9ffbf, #a1c4fd, #fbc2eb, #ffafbd); }
.bg-forest  { background-image: linear-gradient(120deg, #134e5e, #71b280, #d4fc79, #11998e, #38ef7d, #134e5e); }
.bg-neon    { background-image: linear-gradient(120deg, #f700ff, #00fff0, #faff00, #ff0080, #00ff47, #f700ff); }
.bg-lava    { background-image: linear-gradient(120deg, #200122, #6f0000, #ff4e00, #ec9f05, #b20a2c, #200122); }
.bg-rainbow { background-image: linear-gradient(120deg, #ff3b3b, #ff9a3b, #ffe53b, #4dff7a, #3bd5ff, #5b5bff, #c13bff, #ff3b9d, #ff3b3b); }

.rainbow.bg-photo {
  inset: 0;
  background-size: cover;
  background-position: center;
  animation: none;
}
.rainbow.bg-photo::after { display: none; }

/* ---------- Header ---------- */
.presence {
  position: absolute;
  right: -1px; bottom: -1px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #b9b3c7;
  border: 3px solid #fff;
  transition: background .3s;
}
.presence.online { background: #22c55e; animation: glow 2s infinite; }
@keyframes glow { 50% { box-shadow: 0 0 0 5px rgba(34,197,94,.25); } }
.who { min-width: 0; }
.sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#music-btn.playing { animation: wiggle 1s ease-in-out infinite; }
@keyframes wiggle { 25% { transform: rotate(-12deg); } 75% { transform: rotate(12deg); } }
@media (max-width: 520px) {
  .chat-header { gap: 8px; padding: 10px; }
  .header-actions { gap: 3px; }
  .header-actions .icon-btn { width: 36px; height: 36px; font-size: 1.1rem; }
  .avatar { width: 38px; height: 38px; }
  .brand.small { font-size: 1.1rem; }
  .sub { font-size: .78rem; }
}
@media (max-width: 380px) {
  .who { display: none; }
}
/* On phones the chat fills the screen, so let the background show through. */
@media (max-width: 799px) {
  .chat { background: rgba(255, 255, 255, .28); backdrop-filter: blur(3px); }
  .chat-header, .composer { background: rgba(255, 255, 255, .88); }
  .time {
    background: rgba(255, 255, 255, .75);
    border-radius: 999px;
    padding: 0 6px;
  }
  .empty { background: rgba(255, 255, 255, .85); padding: 6px 14px; border-radius: 999px; }
}

/* ---------- Music drawer ---------- */
.music-panel {
  border-bottom: 1px solid var(--line);
  background: linear-gradient(135deg, rgba(255,240,250,.95), rgba(238,240,255,.95));
  padding: 12px 16px;
  max-height: 46%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: drop .25s ease-out;
}
.music-panel[hidden] { display: none; }
@keyframes drop { from { transform: translateY(-12px); opacity: 0; } }
.now-playing { display: flex; align-items: center; gap: 12px; }
.disc {
  flex: none;
  width: 52px; height: 52px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 1.5rem;
  background: conic-gradient(#ff3b3b, #ffe53b, #4dff7a, #3bd5ff, #c13bff, #ff3b3b);
  box-shadow: inset 0 0 0 14px rgba(255,255,255,.35);
}
.disc.spin { animation: spin 3s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.np-info { flex: 1; min-width: 0; }
.np-info strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seek-row { display: flex; align-items: center; gap: 8px; font-size: .78rem; color: var(--muted); }
.seek-row input { flex: 1; min-width: 0; accent-color: var(--mine-a); }
.music-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.play-btn {
  width: 48px; height: 48px;
  border: 0; border-radius: 50%;
  font-size: 1.3rem;
  background: linear-gradient(135deg, var(--mine-a), var(--mine-b));
  box-shadow: 0 6px 16px rgba(138, 77, 255, .35);
}
.toggle { display: flex; align-items: center; gap: 6px; font-size: .9rem; cursor: pointer; }
.toggle input { accent-color: var(--mine-b); width: 18px; height: 18px; }
.upload-btn { cursor: pointer; font-size: .9rem; }
.music-controls .upload-btn { margin-left: auto; }
.track-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 0; }
.track { display: flex; align-items: center; border-radius: 14px; }
.track:hover { background: rgba(255,255,255,.7); }
.track.playing { background: rgba(255, 79, 163, .12); }
.track-play {
  flex: 1; min-width: 0;
  display: flex; align-items: baseline; gap: 6px;
  border: 0; background: none;
  padding: 8px 10px;
  text-align: left;
  color: var(--ink);
}
.track-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.track-by { flex: none; font-size: .75rem; color: var(--muted); }
.track-del { border: 0; background: none; opacity: .5; padding: 6px 10px; }
.track-del:hover { opacity: 1; }
.track-empty { color: var(--muted); padding: 6px 10px; }

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(30, 0, 60, .45);
  backdrop-filter: blur(4px);
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative;
  width: min(440px, 100%);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  background: #fff;
  border-radius: 28px;
  padding: 24px;
  box-shadow: 0 20px 60px rgba(60, 0, 90, .4);
  animation: pop .3s cubic-bezier(.2, 1.4, .4, 1);
}
.modal-card h2 { margin: 0 0 4px; }
.modal-card h3 { margin: 18px 0 8px; font-size: 1rem; }
.hint { color: var(--muted); margin: 0 0 14px; font-size: .9rem; }
.modal-close {
  position: absolute;
  top: 14px; right: 14px;
  border: 0; background: #f3ecff;
  width: 34px; height: 34px;
  border-radius: 50%;
}
.presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.swatch {
  aspect-ratio: 1;
  border: 3px solid transparent;
  border-radius: 16px;
  display: flex; align-items: flex-end; justify-content: center;
  padding: 4px;
  transition: transform .15s;
}
.swatch:hover { transform: scale(1.06); }
.swatch.selected { border-color: var(--ink); }
.swatch span {
  font-size: .68rem;
  background: rgba(255,255,255,.85);
  border-radius: 999px;
  padding: 1px 6px;
  white-space: nowrap;
}
.gradient-row { display: flex; align-items: center; gap: 8px; }
.gradient-row input[type="color"] {
  width: 44px; height: 44px;
  border: 0; padding: 0;
  background: none;
  border-radius: 12px;
  cursor: pointer;
}
.grad-preview { flex: 1; height: 44px; border-radius: 12px; }

/* ---------- Incoming call ---------- */
.ringing { text-align: center; }
.ring-avatar {
  width: 90px; height: 90px;
  margin: 6px auto 12px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 2.4rem; font-weight: 700; color: #fff;
  background: conic-gradient(#ff3b3b, #ffe53b, #4dff7a, #3bd5ff, #c13bff, #ff3b3b);
  animation: ring 1.2s infinite;
}
@keyframes ring {
  0% { box-shadow: 0 0 0 0 rgba(255, 79, 163, .6); }
  100% { box-shadow: 0 0 0 26px rgba(255, 79, 163, 0); }
}
.call-choice { display: flex; justify-content: center; gap: 40px; margin-top: 18px; }

.round-btn {
  width: 60px; height: 60px;
  border: 0; border-radius: 50%;
  font-size: 1.5rem;
  background: rgba(255,255,255,.9);
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
  transition: transform .15s;
}
.round-btn:hover { transform: scale(1.1); }
.round-btn.green { background: #22c55e; animation: pulse-green 1.2s infinite; }
@keyframes pulse-green {
  0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, .7); }
  100% { box-shadow: 0 0 0 18px rgba(34, 197, 94, 0); }
}
.round-btn.red { background: #ff2d55; }
.round-btn.off { background: #6b6b80; }
.round-btn[hidden] { display: none; }

/* ---------- Video call ---------- */
.call {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: radial-gradient(circle at 50% 40%, #3a1c71, #1a0533 70%);
}
.call[hidden] { display: none; }
.call::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 6px solid transparent;
  border-image: linear-gradient(90deg, #ff3b3b, #ffe53b, #4dff7a, #3bd5ff, #c13bff) 1;
  pointer-events: none;
  z-index: 2;
}
#remote-canvas {
  display: block;
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity .4s;
}
@media (orientation: portrait) {
  #remote-canvas { object-fit: contain; }
}
.call.has-video #remote-canvas { opacity: 1; }
.remote-placeholder {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #fff;
  font-weight: 600;
}
.remote-placeholder .ring-avatar { animation: none; }
.call.live:not(.has-video) .remote-placeholder { display: flex; }
.call-badges {
  position: absolute;
  left: 0; right: 0;
  bottom: calc(max(28px, env(safe-area-inset-bottom)) + 76px);
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  z-index: 3;
}
.badge {
  color: #fff;
  font-size: .85rem;
  background: rgba(0,0,0,.45);
  padding: 4px 12px;
  border-radius: 999px;
}
.badge[hidden] { display: none; }
.badge.hint { opacity: .8; }
#local-video {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: 16px;
  width: min(30vw, 200px);
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 18px;
  border: 3px solid #fff;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  transform: scaleX(-1);
  background: #2b1d4e;
  z-index: 3;
}
#local-video.off { opacity: .15; }
.call-status {
  position: absolute;
  top: max(20px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-size: 1.2rem;
  font-weight: 600;
  background: rgba(0,0,0,.35);
  padding: 6px 16px;
  border-radius: 999px;
  z-index: 3;
  white-space: nowrap;
}
.call:not(.live) .call-status { top: 45%; font-size: 1.4rem; }
.call-controls {
  position: absolute;
  left: 0; right: 0;
  bottom: max(28px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  gap: 18px;
  z-index: 3;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
