:root {
  --ink: #17153B;
  --paper: #FBFBFE;
  --surface: #F1F0F8;
  --line: #E2E1EE;
  --muted: #55537A;
  --video: #FF4F79;
  --voice: #00A896;
  --text: #FFC23C;
  --on-mode: #17153B;
  --danger: #D7263D;
  --focus: #3D5AFE;
  --mode: var(--video);
  --display: 'Bricolage Grotesque', 'Segoe UI', system-ui, sans-serif;
  --body: 'Atkinson Hyperlegible', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --ink: #F2F1FB; --paper: #121030; --surface: #1B1940; --line: #2E2C57; --muted: #AEACD0; --focus: #8C9EFF; }
}
body[data-mode="voice"] { --mode: var(--voice); }
body[data-mode="text"]  { --mode: var(--text); }

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 400 1rem/1.5 var(--body);
  display: grid; grid-template-rows: auto 1fr auto; height: 100dvh; overflow: hidden;
}
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Top bar */
.bar { display: flex; align-items: center; gap: 16px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.logo { display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: inherit; font: 800 1.2rem var(--display); letter-spacing: -0.02em; }
.modes { display: inline-flex; background: var(--surface); border-radius: 999px; padding: 4px; margin-left: auto; }
.modes button { border: 0; background: none; padding: 8px 16px; border-radius: 999px; font-weight: 700; cursor: pointer; color: var(--muted); }
.modes button[aria-selected="true"] { background: var(--mode); color: var(--on-mode); }
.online { color: var(--muted); font-size: .9rem; font-weight: 700; white-space: nowrap; }
@media (max-width: 520px) { .logo span { display: none; } .modes button { padding: 8px 12px; } .online { display: none; } }

/* Layout */
.app { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(320px, 1fr); gap: 16px; padding: 16px; min-height: 0; }
body[data-mode="text"] .app { grid-template-columns: minmax(0, 760px); justify-content: center; }
body[data-mode="text"] .stage { display: none; }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); gap: 12px; padding: 12px; }
}

/* Stage */
.stage { position: relative; display: grid; min-height: 0; }
.tile { position: relative; border-radius: 22px; overflow: hidden; background: #1E1B45; }
.remote { min-height: 0; height: 100%; display: grid; place-items: center; }
.remote video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #1E1B45; display: none; }
body[data-mode="video"][data-state="connected"] .remote video.has-video { display: block; }
.self { position: absolute; right: 14px; bottom: 14px; width: clamp(110px, 24%, 220px); aspect-ratio: 4 / 3; border: 3px solid var(--paper); box-shadow: 0 6px 24px rgba(0,0,0,.35); }
.self canvas { width: 100%; height: 100%; display: block; object-fit: cover; }
body[data-mode="voice"] .self { display: none; }
@media (max-width: 860px) {
  .remote { height: 52dvh; min-height: 260px; aspect-ratio: auto; width: 100%; }
  body[data-mode="voice"] .remote { height: 170px; min-height: 0; }
  .self { right: 10px; bottom: 10px; width: clamp(84px, 27%, 120px); aspect-ratio: 3 / 4; border-width: 2px; }
  .overlay { padding: 16px 16px 120px; }
}

.overlay { position: relative; z-index: 1; text-align: center; color: #fff; padding: 24px; max-width: 44ch; }
.overlay h1 { font: 800 clamp(1.6rem, 3.4vw, 2.4rem)/1.05 var(--display); letter-spacing: -0.02em; margin: 0 0 10px; }
.overlay p { margin: 0; opacity: .85; }
body[data-mode="video"][data-state="connected"] .overlay.hide-on-video { display: none; }
.badge { position: absolute; left: 12px; bottom: 12px; z-index: 2; background: rgba(23,21,59,.82); color: #fff; font-size: .82rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; }
.self .badge { left: 8px; bottom: 8px; font-size: .75rem; }

/* Voice visual */
.voice-visual { display: none; position: absolute; inset: 0; place-items: center; }
body[data-mode="voice"][data-state="connected"] .voice-visual { display: grid; }
body[data-mode="voice"][data-state="connected"] .overlay { display: none; }
.ring { width: 96px; height: 96px; border-radius: 50%; background: var(--voice); box-shadow: 0 0 0 calc(var(--lvl, 0) * 60px) rgba(0,168,150,.25); transition: box-shadow .08s linear; }
.ring-label { position: absolute; bottom: 18px; color: #fff; font-weight: 700; font-size: 1.1rem; display: inline-flex; align-items: center; gap: 8px; }
.ring-label .flag { width: 32px; height: 24px; margin: 0; }
body[data-mode="voice"] .badge.country { display: none; }

/* Waiting animation: the only ambient motion in the app */
body[data-state="waiting"] .overlay h1::after { content: ""; display: inline-block; width: .9em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* Chat */
.chat { display: flex; flex-direction: column; min-height: 0; border: 1px solid var(--line); border-radius: 22px; overflow: hidden; background: var(--paper); }
.setup { padding: 16px; border-bottom: 1px solid var(--line); }
.setup label { font-weight: 700; display: block; margin-bottom: 8px; }
.hint { color: var(--muted); font-size: .88rem; font-weight: 400; margin: 8px 0 0; }
body:not([data-state="idle"]) .setup { display: none; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; border: 1.5px solid var(--line); border-radius: 14px; padding: 6px; }
.tags:focus-within { border-color: var(--ink); }
.tag { display: inline-flex; align-items: center; gap: 4px; background: var(--mode); color: var(--on-mode); border-radius: 999px; padding: 3px 4px 3px 12px; font-weight: 700; font-size: .9rem; }
.tag button { border: 0; background: rgba(23,21,59,.14); border-radius: 50%; width: 22px; height: 22px; line-height: 1; cursor: pointer; color: var(--on-mode); }
.tags input { flex: 1; min-width: 140px; border: 0; background: none; padding: 6px; outline: none; }

.log { list-style: none; margin: 0; padding: 16px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; }
.log li { max-width: 85%; padding: 8px 12px; border-radius: 16px; overflow-wrap: anywhere; }
.log .me { align-self: flex-end; background: var(--ink); color: var(--paper); border-bottom-right-radius: 4px; }
.log .them { align-self: flex-start; background: var(--surface); border-bottom-left-radius: 4px; }
.log .sys { align-self: center; max-width: 100%; text-align: center; color: var(--muted); font-size: .9rem; padding: 4px; }
.log .sys strong { color: var(--ink); }
.typing { margin: 0; padding: 0 16px 6px; color: var(--muted); font-size: .88rem; }
.compose { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.compose input { flex: 1; min-width: 0; border: 1.5px solid var(--line); border-radius: 999px; padding: 10px 16px; background: var(--paper); }
.compose input:focus { border-color: var(--ink); outline: none; }
.compose button { border: 0; border-radius: 999px; padding: 0 18px; background: var(--ink); color: var(--paper); font-weight: 700; cursor: pointer; }
.compose button:disabled, .compose input:disabled { opacity: .5; cursor: not-allowed; }

/* Controls */
.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--paper); }
.ctl {
  min-height: 46px; padding: 0 18px; border-radius: 999px; border: 1.5px solid var(--line);
  background: var(--paper); font-weight: 700; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; color: var(--ink);
}
.ctl:disabled { opacity: .45; cursor: not-allowed; }
.ctl.main { background: var(--mode); border-color: var(--mode); color: var(--on-mode); min-width: 150px; font-size: 1.05rem; }
.ctl.warn { color: var(--danger); border-color: currentColor; }
.ctl[aria-pressed="true"]:not(#blurBtn) { background: var(--ink); color: var(--paper); border-color: var(--ink); }
body[data-mode="text"] .av { display: none; }
body[data-mode="voice"] .vid { display: none; }
@media (max-width: 520px) { .ctl { padding: 0 12px; min-height: 44px; font-size: .92rem; } .ctl.main { min-width: 110px; } }

/* Dialogs */
dialog { border: 0; border-radius: 22px; padding: 0; max-width: min(460px, calc(100% - 32px)); background: var(--paper); color: var(--ink); }
dialog::backdrop { background: rgba(10,9,30,.7); }
dialog form { padding: 24px; }
dialog h2 { font: 800 1.6rem/1.1 var(--display); margin: 0 0 10px; letter-spacing: -0.02em; }
dialog ul { padding-left: 1.2em; color: var(--muted); }
dialog fieldset { border: 0; padding: 0; margin: 0 0 8px; }
.check, .radio { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; cursor: pointer; }
.check input, .radio input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--ink); flex: none; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

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

/* Country filters and flags */
.badge.country { left: 12px; top: 12px; bottom: auto; display: inline-flex; align-items: center; }
.flag { width: 24px; height: 18px; border-radius: 3px; object-fit: cover; vertical-align: -3px; margin-right: 6px; box-shadow: 0 0 0 1px rgba(0,0,0,.15); }
.field { margin-top: 14px; }
.setup { max-height: 62%; overflow-y: auto; }
.clist { list-style: none; margin: 6px 0 0; padding: 4px; border: 1.5px solid var(--line); border-radius: 14px; max-height: 190px; overflow-y: auto; background: var(--paper); }
.clist[hidden] { display: none; }
.clist button { display: flex; align-items: center; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 10px; cursor: pointer; font-weight: 700; }
.clist button:hover, .clist button:focus-visible { background: var(--surface); }
.check.small { margin-top: 10px; padding: 4px 0; font-weight: 700; }
body:not([data-mode="video"]) .video-only { display: none; }
.tag .flag { margin-right: 4px; }

/* ---- Phone layout: small video, big chat, one row of controls, country on the video ---- */
.badge.country { z-index: 3; }
@media (max-width: 860px) {
  .app { gap: 8px; padding: 8px; }
  .remote { height: 36dvh; min-height: 190px; max-height: 340px; }
  body[data-mode="voice"] .remote { height: 130px; min-height: 0; }
  .self { width: 72px; right: 8px; bottom: 8px; }
  .self .badge { display: none; }
  .badge.country { left: 10px; top: 10px; padding: 6px 12px; font-size: 1rem; background: rgba(23,21,59,.78); backdrop-filter: blur(4px); }
  .badge.country .flag { width: 28px; height: 21px; margin-right: 8px; }
  .badge { font-size: .78rem; left: 10px; bottom: 10px; }
  .chat { border-radius: 16px; }
  .log { padding: 10px 12px; gap: 6px; font-size: 1.02rem; }
  .compose { padding: 8px; }
  .controls { display: grid; grid-template-columns: 1.25fr 1fr 1fr 1.15fr 1fr 1.1fr; gap: 6px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); }
  body[data-mode="text"] .controls { grid-template-columns: 1.5fr 1fr 1fr; }
  body[data-mode="voice"] .controls { grid-template-columns: 1.3fr 1fr 1fr 1fr; }
  .controls .ctl, .controls .ctl.main { min-width: 0; min-height: 42px; padding: 0 2px; font-size: .78rem; line-height: 1.1; text-align: center; }
  .controls .ctl.main { font-size: .9rem; }
}
.badge.country { padding: 6px 12px; font-size: 1rem; background: rgba(23,21,59,.78); backdrop-filter: blur(4px); }
.badge.country .flag { width: 28px; height: 21px; margin-right: 8px; }
