:root {
  --bg: #0b1020;
  --bg2: #1b2547;
  --text: #f4f6ff;
  --muted: #aab3d6;
  --green: #22c55e;
  --red: #ef4444;
  --glass: rgba(15, 20, 40, .55);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; border: 0; cursor: pointer; }
.hidden { display: none !important; }

.screen {
  position: fixed; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.screen.active { display: flex; }

/* ---------- tela inicial / encerrada ---------- */
#screen-start, #screen-ended {
  background: radial-gradient(120% 80% at 50% 0%, var(--bg2), var(--bg));
}
.start-card { max-width: 380px; text-align: center; display: grid; gap: 16px; justify-items: center; }
.start-card h1 { font-size: 1.8rem; }
.start-card p { color: var(--muted); line-height: 1.45; }
.start-avatar { width: 150px; height: 150px; }
.btn-primary {
  background: var(--green); color: #052e16; font-weight: 700; font-size: 1.1rem;
  padding: 14px 32px; border-radius: 999px; box-shadow: 0 8px 24px rgba(34, 197, 94, .35);
}
.btn-primary:active { transform: scale(.97); }
.hint { font-size: .85rem; color: #fbbf24 !important; }

.avatar-slot {
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(#8ec5ff, #5b8def);
  display: grid; place-items: end center;
}
.avatar-slot .character { width: 120%; margin-bottom: -38%; }

/* ---------- chamada recebida ---------- */
#screen-ringing {
  justify-content: space-between;
  background: linear-gradient(160deg, #1e3a8a, #0b1020 70%);
}
.ring-top { text-align: center; margin-top: 6vh; display: grid; gap: 6px; }
.ring-label { color: var(--muted); font-size: .95rem; }
.ring-top h2 { font-size: 2.4rem; font-weight: 600; }
.ring-sub { color: var(--muted); }
.ring-avatar-wrap { position: relative; width: 180px; height: 180px; }
.ring-avatar { position: absolute; inset: 0; animation: bob 1.2s ease-in-out infinite; }
.pulse {
  position: absolute; inset: 0; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .5);
  animation: pulse 2s ease-out infinite;
}
.pulse.p2 { animation-delay: 1s; }
@keyframes pulse { from { transform: scale(1); opacity: .8; } to { transform: scale(1.9); opacity: 0; } }

.ring-actions { display: flex; gap: 72px; margin-bottom: 8vh; }
.action { display: grid; gap: 10px; justify-items: center; color: var(--muted); font-size: .9rem; }
.round { width: 72px; height: 72px; border-radius: 50%; display: grid; place-items: center; }
.round svg { width: 34px; height: 34px; fill: #fff; }
.round.red { background: var(--red); }
.round.green { background: var(--green); }
.wiggle { animation: wiggle 1s ease-in-out infinite; }
@keyframes wiggle {
  0%, 50%, 100% { transform: rotate(0); }
  10%, 30% { transform: rotate(-12deg); }
  20%, 40% { transform: rotate(12deg); }
}

/* ---------- em chamada ---------- */
#screen-call { padding: 0; background: #000; }
.stage { position: absolute; inset: 0; overflow: hidden; }
.room {
  position: absolute; inset: 0;
  background: linear-gradient(#ffe7a8 0 72%, #c98b4f 72% 100%);
}
.room .window {
  position: absolute; left: 8%; top: 10%; width: 26%; aspect-ratio: 3/4; max-width: 220px;
  background: linear-gradient(#7cc4ff, #c9ecff);
  border: 10px solid #8a5a2b; border-radius: 50% 50% 6px 6px / 30% 30% 6px 6px;
}
.room .frame {
  position: absolute; right: 9%; top: 14%; width: 18%; aspect-ratio: 1; max-width: 150px;
  background: radial-gradient(circle at 50% 60%, #ffd23f 0 30%, #ff7a59 31% 100%);
  border: 8px solid #d7262e; border-radius: 8px; transform: rotate(4deg);
}
.room .floor { position: absolute; left: 0; right: 0; bottom: 0; height: 28%; background: repeating-linear-gradient(90deg, #c98b4f 0 80px, #b97b41 80px 160px); }

.character-slot {
  position: absolute; left: 50%; bottom: 0;
  width: min(92vw, 70vh);
  transform: translateX(-50%);
}
.character-slot .character { width: 100%; display: block; overflow: visible; }

.character .head { transform-box: view-box; transform-origin: 200px 360px; animation: idle 3.2s ease-in-out infinite; }
.character .body { transform-box: view-box; transform-origin: 200px 500px; animation: breathe 3.2s ease-in-out infinite; }
.talking .character .head { animation: talk .45s ease-in-out infinite alternate; }
.character .eyes { transform-box: view-box; transform-origin: 200px 195px; transition: transform .07s; }
.blink .character .eyes { transform: scaleY(.08); }
.character .pupils { transition: transform .4s ease; transform-box: view-box; }
.character .ear { transform-box: view-box; }
.happy .character .ear-l { animation: earwiggle .6s ease-in-out 2; transform-origin: 140px 160px; }
.happy .character .ear-r { animation: earwiggle .6s ease-in-out 2 reverse; transform-origin: 260px 160px; }

.mouth { display: none; }
.character[data-mouth="rest"] .m-rest,
.character:not([data-mouth]) .m-rest,
.character[data-mouth="small"] .m-small,
.character[data-mouth="wide"] .m-wide,
.character[data-mouth="o"] .m-o,
.character[data-mouth="ee"] .m-ee { display: inline; }

@keyframes bob { 50% { transform: translateY(-6px); } }
@keyframes idle {
  0%, 100% { transform: rotate(-1.5deg) translateY(0); }
  50% { transform: rotate(1.5deg) translateY(-6px); }
}
@keyframes talk {
  from { transform: rotate(-3deg) translateY(-2px); }
  to { transform: rotate(3deg) translateY(-10px); }
}
@keyframes breathe { 50% { transform: scaleY(1.015); } }
@keyframes earwiggle { 50% { transform: rotate(-10deg); } }

.call-top {
  position: absolute; top: 0; left: 0; right: 0;
  padding: max(14px, env(safe-area-inset-top)) 16px 30px;
  background: linear-gradient(rgba(0, 0, 0, .55), transparent);
  text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
}
.call-top div { display: flex; flex-direction: column; }
.call-top strong { font-size: 1.15rem; }
.call-top span { color: #e5e7eb; font-variant-numeric: tabular-nums; font-size: .9rem; }

.selfview {
  position: absolute; top: max(14px, env(safe-area-inset-top)); right: 16px;
  width: 96px; height: 136px; object-fit: cover; border-radius: 14px;
  background: #222; border: 2px solid rgba(255, 255, 255, .7);
  transform: scaleX(-1); box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
@media (min-width: 700px) { .selfview { width: 160px; height: 120px; } }

.captions {
  position: absolute; left: 16px; right: 16px; top: calc(max(14px, env(safe-area-inset-top)) + 150px);
  display: grid; gap: 6px; justify-items: center; pointer-events: none;
}
.cap {
  max-width: 560px; text-align: center; line-height: 1.35;
  padding: 6px 12px; border-radius: 12px; background: var(--glass);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.cap:empty { display: none; }
.cap.you { color: #c7d2fe; font-size: .9rem; }
.cap.him { font-size: 1.05rem; font-weight: 500; }

.status {
  position: absolute; left: 50%; bottom: 122px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  padding: 6px 14px; border-radius: 999px; background: var(--glass);
  font-size: .85rem; white-space: nowrap;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; }
.status[data-state="listening"] .dot { background: var(--green); animation: blinkdot 1s infinite; }
.status[data-state="thinking"] .dot { background: #fbbf24; animation: blinkdot .5s infinite; }
.status[data-state="speaking"] .dot { background: #60a5fa; }
.status[data-state="muted"] .dot, .status[data-state="error"] .dot { background: var(--red); }
@keyframes blinkdot { 50% { opacity: .25; } }

.text-form {
  position: absolute; left: 16px; right: 16px; bottom: 122px;
  max-width: 560px; margin: 0 auto; display: flex; gap: 8px; z-index: 2;
}
.text-form input {
  flex: 1; min-width: 0; padding: 12px 14px; border-radius: 999px; border: 0;
  font-size: 1rem; background: rgba(255, 255, 255, .95); color: #111;
}
.text-form button { padding: 0 18px; border-radius: 999px; background: var(--green); color: #052e16; font-weight: 700; }

.controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: center; gap: 18px;
  padding: 20px 16px max(22px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(0, 0, 0, .6));
}
.ctl {
  width: 60px; height: 60px; border-radius: 50%;
  background: rgba(255, 255, 255, .22); display: grid; place-items: center;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.ctl svg { width: 28px; height: 28px; fill: #fff; }
.ctl.red { background: var(--red); }
.ctl.off { background: #fff; }
.ctl.off svg { fill: #111; }
#btn-mic .off { display: none; }
#btn-mic.off .on { display: none; }
#btn-mic.off .off { display: block; }

@media (prefers-reduced-motion: reduce) {
  .character .head, .character .body, .ring-avatar, .pulse, .wiggle { animation: none !important; }
}

body.typing .status { bottom: 180px; }

/* celular em pé: personagem maior; legendas abaixo da sua câmera */
@media (max-aspect-ratio: 1/1) {
  .character-slot { width: min(125vw, 66vh); }
}
/* tela deitada / computador: legendas no topo, entre o nome e a sua câmera */
@media (min-aspect-ratio: 1/1) and (min-width: 700px) {
  .captions { top: max(16px, env(safe-area-inset-top)); left: 200px; right: 200px; }
}

/* ---------- chamada só de áudio ---------- */
.audio-panel { display: none; }
body.audio-call #screen-call { background: linear-gradient(160deg, #1e3a8a, #0b1020 70%); }
body.audio-call .stage,
body.audio-call .call-top,
body.audio-call .selfview,
body.audio-call #btn-cam { display: none !important; }
body.audio-call .audio-panel {
  position: absolute; top: max(48px, env(safe-area-inset-top)); left: 0; right: 0;
  display: grid; justify-items: center; gap: 10px; text-align: center;
}
body.audio-call .audio-avatar { width: 140px; height: 140px; }
body.audio-call .audio-panel h2 { font-size: 2rem; }
body.audio-call .audio-timer { color: var(--muted); font-variant-numeric: tabular-nums; }
body.audio-call.talking-audio .audio-avatar { box-shadow: 0 0 0 6px rgba(96, 165, 250, .45); }
body.audio-call .captions { top: calc(max(48px, env(safe-area-inset-top)) + 230px); }
.ring-actions { gap: 28px; }
