:root {
  --bg: #0b0d12;
  --bg-2: #0f121a;
  --glass: rgba(255, 255, 255, 0.045);
  --glass-strong: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.09);
  --border-hover: rgba(255, 255, 255, 0.18);
  --text: #eef1f7;
  --muted: #98a0b3;
  --faint: #6b7283;
  --primary: #6366f1;
  --primary-2: #8b5cf6;
  --accent: #22d3ee;
  --danger: #f4506b;
  --ok: #34d399;
  --grad: linear-gradient(135deg, #818cf8 0%, #a78bfa 45%, #22d3ee 100%);
  --grad-btn: linear-gradient(135deg, #6366f1, #8b5cf6);
  --shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.7);
  --radius: 22px;
  --font: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --display: 'Plus Jakarta Sans', var(--font);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- Animated background ---- */
.bg-aura {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  background: radial-gradient(1200px 700px at 50% -10%, #161a26 0%, var(--bg) 60%);
}
.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.5;
  animation: float 18s ease-in-out infinite;
}
.blob-1 { width: 460px; height: 460px; background: #6366f1; top: -120px; left: -80px; }
.blob-2 { width: 420px; height: 420px; background: #22d3ee; bottom: -140px; right: -60px; animation-delay: -6s; }
.blob-3 { width: 360px; height: 360px; background: #a855f7; top: 35%; left: 55%; animation-delay: -11s; opacity: 0.35; }
@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(40px, -30px) scale(1.08); }
  66% { transform: translate(-30px, 20px) scale(0.95); }
}

/* ---- Topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  padding-top: calc(1rem + env(safe-area-inset-top, 0));
}
.brand { display: flex; align-items: center; gap: .6rem; text-decoration: none; color: var(--text); }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border-radius: 11px;
  background: var(--grad-btn);
  color: #fff;
  box-shadow: 0 8px 20px -6px rgba(99, 102, 241, 0.7);
}
.brand-name { font-family: var(--display); font-weight: 800; font-size: 1.1rem; letter-spacing: -0.01em; }
.brand-dot { color: var(--accent); }

.pill {
  display: inline-flex; align-items: center; gap: .45rem;
  font-size: .78rem; font-weight: 500;
  color: var(--muted);
  border: 1px solid var(--border);
  background: var(--glass);
  border-radius: 999px;
  padding: .35rem .75rem;
  backdrop-filter: blur(10px);
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.pill.online { color: #c7f9e3; } .pill.online .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(52,211,153,.18); }
.pill.offline { color: #ffd2da; } .pill.offline .dot { background: var(--danger); }

/* ---- Layout ---- */
main { flex: 1; display: grid; place-items: center; padding: 1.25rem; }
.view { width: 100%; max-width: 440px; animation: rise .45s cubic-bezier(.2,.7,.2,1) both; }
.view-wide { max-width: 720px; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

.card {
  border-radius: var(--radius);
  padding: 2rem;
}
.glass {
  background: linear-gradient(180deg, var(--glass-strong), var(--glass));
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.center { text-align: center; }

.card-head { margin-bottom: 1.5rem; }
.eyebrow {
  display: inline-block;
  font-size: .72rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent);
  background: rgba(34, 211, 238, 0.1);
  border: 1px solid rgba(34, 211, 238, 0.2);
  padding: .25rem .6rem; border-radius: 999px;
  margin-bottom: .9rem;
}
h1 { font-family: var(--display); font-size: 2.1rem; line-height: 1.1; margin: 0 0 .5rem; letter-spacing: -0.02em; }
h2 { font-family: var(--display); font-size: 1.5rem; margin: .4rem 0 .3rem; letter-spacing: -0.01em; }
.grad { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.lead { color: var(--muted); margin: 0; line-height: 1.55; }

/* ---- Form ---- */
form { display: flex; flex-direction: column; gap: 1rem; margin-top: 1.25rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.field { display: flex; flex-direction: column; gap: .4rem; }
.field label { font-size: .8rem; font-weight: 500; color: var(--muted); padding-left: .15rem; }

input, select {
  width: 100%;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  border-radius: 13px;
  color: var(--text);
  padding: .8rem .9rem;
  font-size: 1rem; /* 16px — avoids iOS Safari auto-zoom on focus */
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
input::placeholder { color: var(--faint); }
input:hover, select:hover { border-color: var(--border-hover); }
input:focus, select:focus {
  outline: none;
  border-color: var(--primary-2);
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.22);
  background: rgba(0, 0, 0, 0.35);
}
.select-wrap { position: relative; }
.select-wrap::after {
  content: ""; position: absolute; right: 1rem; top: 50%; width: 9px; height: 9px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: translateY(-65%) rotate(45deg); pointer-events: none;
}
select { appearance: none; -webkit-appearance: none; padding-right: 2.2rem; cursor: pointer; }
select option { background: #14171f; color: var(--text); }

.error {
  color: #ffc1cc; background: rgba(244, 80, 107, 0.12);
  border: 1px solid rgba(244, 80, 107, 0.3);
  border-radius: 11px; padding: .6rem .8rem; margin: 0; font-size: .88rem;
}
.fineprint { color: var(--faint); font-size: .76rem; text-align: center; margin: .25rem 0 0; line-height: 1.5; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  font-family: inherit; font-size: .95rem; font-weight: 600;
  border-radius: 13px; padding: .8rem 1.2rem;
  border: 1px solid transparent; cursor: pointer;
  transition: transform .12s, box-shadow .2s, background .2s, border-color .2s, opacity .2s;
}
.btn:active { transform: translateY(1px) scale(.99); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-sm { padding: .5rem .85rem; font-size: .85rem; border-radius: 10px; }
.btn-primary {
  background: var(--grad-btn); color: #fff;
  box-shadow: 0 12px 28px -10px rgba(99, 102, 241, 0.75);
}
.btn-primary:hover:not(:disabled) { box-shadow: 0 16px 34px -10px rgba(99, 102, 241, 0.9); transform: translateY(-1px); }
.btn-ghost { background: var(--glass); color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--glass-strong); border-color: var(--border-hover); }
.btn-danger { background: rgba(244, 80, 107, 0.15); color: #ff9fae; border-color: rgba(244, 80, 107, 0.35); }
.btn-danger:hover:not(:disabled) { background: rgba(244, 80, 107, 0.28); color: #fff; }
.btn-link { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; font-size: .85rem; text-decoration: underline; padding: 0; }
.btn-send { padding: .8rem; width: 46px; height: 46px; border-radius: 12px; flex: 0 0 auto; }

/* ---- Lobby ---- */
.hello-avatar {
  width: 62px; height: 62px; margin: 0 auto .6rem; display: grid; place-items: center;
  font-size: 1.8rem; border-radius: 50%;
  background: var(--glass-strong); border: 1px solid var(--border);
}
.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; margin: 1.4rem 0 0; }
.mode-card {
  display: flex; flex-direction: column; align-items: flex-start; gap: .15rem;
  text-align: left; padding: 1.1rem; border-radius: 16px;
  background: rgba(0,0,0,0.22); border: 1px solid var(--border); color: var(--text);
  cursor: pointer; transition: transform .15s, border-color .2s, background .2s, box-shadow .2s;
}
.mode-card:hover { transform: translateY(-3px); border-color: var(--border-hover); background: rgba(0,0,0,0.32); }
.mode-icon {
  display: grid; place-items: center; width: 42px; height: 42px; margin-bottom: .55rem;
  border-radius: 12px; background: var(--glass-strong); color: var(--text);
}
.mode-card.accent .mode-icon { background: var(--grad-btn); color: #fff; box-shadow: 0 10px 22px -10px rgba(99,102,241,.8); }
.mode-card.accent:hover { box-shadow: 0 18px 36px -18px rgba(99,102,241,.6); }
.mode-title { font-family: var(--display); font-weight: 700; font-size: 1rem; }
.mode-sub { font-size: .78rem; color: var(--muted); }

.searching {
  display: flex; align-items: center; justify-content: center; gap: .7rem;
  margin-top: 1.3rem; color: var(--muted); font-size: .92rem;
}
.notice {
  margin-top: 1.1rem; font-size: .88rem; color: var(--text);
  background: rgba(0,0,0,0.25); border: 1px solid var(--border);
  border-radius: 12px; padding: .7rem .9rem;
}

/* Pulse ring loader */
.pulse-ring { position: relative; width: 22px; height: 22px; flex: 0 0 auto; }
.pulse-ring span {
  position: absolute; inset: 0; margin: auto; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent); animation: pulse 1.4s ease-out infinite;
}
.pulse-ring span:nth-child(2) { animation-delay: .45s; }
.pulse-ring span:nth-child(3) { animation-delay: .9s; }
@keyframes pulse {
  0% { transform: scale(.4); opacity: .9; }
  80%, 100% { transform: scale(2.6); opacity: 0; }
}

/* ---- Chat ---- */
.view-chat { max-width: none; width: 100%; }
.chat-shell {
  position: relative;
  padding: 0;
  height: calc(100vh - 112px);
  height: calc(100dvh - 112px);
  display: flex; flex-direction: column; overflow: hidden;
}
.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .9rem 1.1rem; border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.02); flex: 0 0 auto;
}
.peer { display: flex; align-items: center; gap: .7rem; }
.peer-avatar {
  width: 40px; height: 40px; display: grid; place-items: center;
  border-radius: 50%; font-family: var(--display); font-weight: 700; font-size: 1rem; color: #fff;
  background: var(--grad-btn); text-transform: uppercase;
}
.peer-meta { display: flex; flex-direction: column; line-height: 1.25; }
.peer-meta strong { font-size: .98rem; }
.peer-status { font-size: .76rem; color: var(--muted); display: flex; align-items: center; gap: .35rem; }
.peer-status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.peer-status.live .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(52,211,153,.18); }
.chat-actions { display: flex; gap: .5rem; }

/* Two-column body: video column (left) + chat column (right) */
.chat-body { flex: 1; display: flex; min-height: 0; }
.media-col { display: none; }
.chat-body.has-video .media-col {
  display: flex; flex-direction: column; gap: .7rem;
  width: 40%; max-width: 540px; min-width: 280px;
  padding: .8rem; border-right: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.18);
}
.chat-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.video-tile {
  position: relative; flex: 1; min-height: 0;
  border-radius: 14px; overflow: hidden;
  background: #05070b; border: 1px solid var(--border);
}
.video-tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.local-tile video { transform: scaleX(-1); }
.tile-label {
  position: absolute; top: 9px; left: 11px; z-index: 4;
  font-size: .68rem; font-weight: 600; letter-spacing: .02em; color: #fff;
  background: rgba(0, 0, 0, 0.5); padding: .18rem .55rem; border-radius: 999px;
  backdrop-filter: blur(6px);
}

.messages {
  list-style: none; margin: 0; padding: 1.1rem;
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: .5rem;
  scroll-behavior: smooth;
}
.messages[hidden] { display: none; }
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: var(--border-hover); border-radius: 8px; }
.messages li {
  max-width: 74%; padding: .6rem .85rem; border-radius: 16px; line-height: 1.4;
  word-wrap: break-word; font-size: .94rem; animation: pop .2s ease both;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }
.messages li.me {
  align-self: flex-end; color: #fff; background: var(--grad-btn);
  border-bottom-right-radius: 5px; box-shadow: 0 8px 20px -12px rgba(99,102,241,.9);
}
.messages li.them {
  align-self: flex-start; background: rgba(255,255,255,0.07);
  border: 1px solid var(--border); border-bottom-left-radius: 5px;
}
.messages li.system {
  align-self: center; max-width: 90%; text-align: center; background: transparent;
  color: var(--faint); font-size: .8rem; padding: .25rem .5rem;
}
.messages li .who { display: block; font-size: .68rem; font-weight: 600; opacity: .85; margin-bottom: .12rem; }

.chat-input { display: flex; gap: .6rem; padding: .85rem; border-top: 1px solid var(--border); margin: 0; }
.chat-input input { flex: 1; border-radius: 12px; }

/* Remote "connecting" placeholder, shown until the remote stream arrives */
.remote-placeholder {
  position: absolute; inset: 0; z-index: 2; display: flex;
  align-items: center; justify-content: center; color: var(--muted);
  background: radial-gradient(420px 220px at 50% 50%, #11151f 0%, #05070b 70%);
}
.remote-placeholder[hidden] { display: none; }

/* Media controls overlaid on the local (your own) tile */
.media-controls { position: absolute; bottom: 10px; right: 10px; z-index: 5; display: flex; gap: .5rem; }
.media-btn {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%;
  background: rgba(10, 12, 18, 0.6); border: 1px solid rgba(255,255,255,0.22); color: #fff;
  cursor: pointer; backdrop-filter: blur(8px); transition: background .2s, transform .12s;
}
.media-btn:hover { background: rgba(10,12,18,0.88); }
.media-btn:active { transform: scale(.94); }
.media-btn.off { background: var(--danger); border-color: transparent; }

/* ---- Searching / paused overlay ---- */
.chat-overlay {
  position: absolute; inset: 0; z-index: 30;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1.2rem; text-align: center; padding: 2rem;
  background: rgba(8, 10, 15, 0.74);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  animation: rise .3s ease both;
}
.chat-overlay[hidden] { display: none; }
.chat-overlay h3 { font-family: var(--display); font-size: 1.45rem; margin: 0 0 .35rem; letter-spacing: -0.01em; }
.chat-overlay .lead { max-width: 30ch; }
.overlay-actions { display: flex; gap: .65rem; flex-wrap: wrap; justify-content: center; }
.pulse-ring.big { width: 30px; height: 30px; }
.pulse-ring.big span { width: 11px; height: 11px; }

/* ---- Inline searching state (shown inside chat panel, keeps camera live) ---- */
.chat-search {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 1rem; padding: 2rem 1.5rem; text-align: center;
  background:
    radial-gradient(520px 280px at 50% 35%, rgba(99,102,241,0.10), transparent 70%),
    radial-gradient(420px 240px at 50% 90%, rgba(34,211,238,0.08), transparent 70%);
  animation: rise .3s ease both;
}
.chat-search[hidden] { display: none; }
.chat-search-text { display: flex; flex-direction: column; gap: .3rem; }
.chat-search-title {
  font-family: var(--display); font-size: 1.2rem; margin: 0;
  letter-spacing: -0.01em;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.chat-search-sub { margin: 0; color: var(--muted); font-size: .92rem; max-width: 28ch; }
.chat-input input:disabled { opacity: .55; cursor: not-allowed; }
.chat-input .btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---- Responsive ---- */
@media (max-width: 680px) {
  main { padding: .75rem; }
  .topbar {
    padding: .75rem 1rem;
    padding-top: calc(.75rem + env(safe-area-inset-top, 0));
  }
  .card { padding: 1.4rem; }
  h1 { font-size: 1.7rem; }
  h2 { font-size: 1.35rem; }
  .mode-grid { grid-template-columns: 1fr; }
  .view, .view-wide, .view-chat { max-width: 100%; }

  /* Use small-viewport height so the URL bar collapse doesn't crop the input,
     and reserve space for iOS home-indicator safe area. */
  .chat-shell {
    height: calc(100vh - 84px);
    height: calc(100svh - 84px);
    border-radius: 18px;
  }
  .chat-header { padding: .7rem .85rem; }
  .peer-avatar { width: 34px; height: 34px; font-size: .92rem; }
  .peer-meta strong { font-size: .92rem; }
  .chat-actions .btn { padding: .55rem .7rem; }

  .messages { padding: .9rem .85rem; }
  .messages li { max-width: 86%; font-size: .95rem; }
  .chat-input {
    gap: .5rem; padding: .7rem .75rem;
    padding-bottom: calc(.7rem + env(safe-area-inset-bottom, 0));
  }
  .btn-send { width: 44px; height: 44px; }

  /* Video mode on phones: remote fills the column, local floats as a small
     picture-in-picture tile in the corner. This mirrors the standard mobile
     video-call pattern and gives the remote stream the most pixels. */
  .chat-body.has-video {
    flex-direction: column;
  }
  .chat-body.has-video .media-col {
    position: relative;
    display: block;
    width: 100%; max-width: none; min-width: 0;
    height: 42svh;
    padding: 0;
    border-right: none; border-bottom: 1px solid var(--border);
    background: #05070b;
  }
  .chat-body.has-video .remote-tile {
    position: absolute; inset: 0;
    border-radius: 0; border: none;
  }
  .chat-body.has-video .local-tile {
    position: absolute; top: 10px; right: 10px; z-index: 10;
    width: 32%; max-width: 130px; aspect-ratio: 3 / 4;
    border-radius: 12px; overflow: hidden;
    box-shadow: 0 10px 24px -10px rgba(0,0,0,.7);
    flex: none;
  }
  .chat-body.has-video .media-controls {
    bottom: 8px; right: 8px; gap: .4rem;
  }
  .chat-body.has-video .media-btn { width: 34px; height: 34px; }
  .chat-body.has-video .local-tile .tile-label { display: none; }
}

/* Very narrow phones: drop chat-header button labels to icons only. */
@media (max-width: 420px) {
  .chat-actions .btn { gap: 0; padding: .55rem; }
  .chat-actions .btn .btn-label { display: none; }
  .topbar {
    padding: .65rem .85rem;
    padding-top: calc(.65rem + env(safe-area-inset-top, 0));
  }
  .brand-name { font-size: 1rem; }
}

/* Landscape phones: keep the chat input visible above the on-screen keyboard. */
@media (max-height: 520px) and (orientation: landscape) {
  .chat-body.has-video .media-col { height: 100%; }
  .chat-body.has-video { flex-direction: row; }
  .messages { padding: .6rem .8rem; }
}
