/* Colors come from Telegram's theme (set by telegram-web-app.js); fallbacks for plain browsers. */
:root {
  --bg: var(--tg-theme-bg-color, #0f1115);
  --bg2: var(--tg-theme-secondary-bg-color, #1a1d24);
  --text: var(--tg-theme-text-color, #f2f3f5);
  --hint: var(--tg-theme-hint-color, #8b919c);
  --accent: var(--tg-theme-button-color, #3d8bfd);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #ef5350);
  --ok: #34c759;
  --level: 0;
}
@media (prefers-color-scheme: light) {
  :root:not([style*="--tg-theme-bg-color"]) {
    --bg: #ffffff; --bg2: #f1f3f6; --text: #111318; --hint: #707784;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }

#app {
  min-height: 100%; display: flex; flex-direction: column;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

header { display: flex; justify-content: space-between; align-items: center; color: var(--hint); font-size: 14px; }
#online-wrap { visibility: hidden; } /* until the server says whether to show it */
.online i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 6px; }
.online b { color: var(--text); font-weight: 600; }
.timer { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }
#settings {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px 6px 8px; border: 0; border-radius: 999px;
  background: var(--bg2); color: var(--text); font: inherit; font-size: 14px; font-weight: 600; cursor: pointer; line-height: 1;
}
#settings [aria-hidden] { font-size: 18px; }
#settings:active { transform: scale(.97); }

.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; text-align: center; }

/* The orb reacts to the partner's voice level (--level 0..1 set from JS). */
.orb { position: relative; width: 180px; height: 180px; display: grid; place-items: center; }
.orb .ring, .orb .core { position: absolute; border-radius: 50%; }
.orb .ring {
  inset: 0; border: 2px solid var(--accent); opacity: .25;
  transform: scale(calc(1 + var(--level) * .35)); transition: transform 80ms linear;
}
.orb .core {
  width: 110px; height: 110px; background: var(--accent);
  box-shadow: 0 0 calc(20px + var(--level) * 60px) color-mix(in srgb, var(--accent) 60%, transparent);
  transform: scale(calc(1 + var(--level) * .15)); transition: transform 80ms linear, background .3s;
}
[data-state="idle"] .orb .core, [data-state="banned"] .orb .core { background: var(--bg2); box-shadow: none; }
[data-state="waiting"] .orb .ring, [data-state="connecting"] .orb .ring { animation: pulse 1.4s ease-out infinite; }
@keyframes pulse { from { transform: scale(.75); opacity: .6; } to { transform: scale(1.25); opacity: 0; } }

.status { margin: 0; font-size: 18px; font-weight: 600; max-width: 320px; }
.peer {
  margin: 0; padding: 8px 16px; border-radius: 999px; background: var(--bg2);
  font-size: 17px; font-weight: 600;
}
.secure { max-width: 320px; }
.sas { margin: 0 0 6px; font-size: 30px; letter-spacing: 6px; }
.hint { margin: 0; color: var(--hint); font-size: 13px; }

footer { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.controls { display: none; width: 100%; max-width: 420px; gap: 12px; align-items: center; }
.btn { border: 0; font: inherit; font-weight: 600; cursor: pointer; color: var(--text); background: var(--bg2); }
.btn.primary { width: 100%; max-width: 420px; height: 52px; border-radius: 14px; background: var(--accent); color: var(--accent-text); }
.btn.round { flex: none; width: 52px; height: 52px; border-radius: 50%; font-size: 20px; }
.btn.link { background: none; color: var(--hint); font-weight: 500; padding: 6px 12px; }
.btn:active { transform: scale(.97); }
#mute[aria-pressed="true"] { background: var(--danger); }

/* Segmented choice (partner filter, language) */
.label { margin: 0; color: var(--hint); font-size: 13px; }
.seg { display: flex; width: 100%; max-width: 420px; padding: 4px; gap: 4px; border-radius: 14px; background: var(--bg2); }
.seg.wrap { flex-wrap: wrap; }
.seg button {
  flex: 1 1 0; min-width: 0; height: 40px; border: 0; border-radius: 10px; background: none;
  font: inherit; font-size: 14px; font-weight: 600; color: var(--hint); cursor: pointer; white-space: nowrap;
}
.seg.wrap button { flex-basis: calc(50% - 2px); }
.seg button[aria-checked="true"] { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .15); }

/* First-launch / settings sheet */
.sheet {
  position: fixed; inset: 0; z-index: 10; overflow-y: auto; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center;
  padding: max(28px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.sheet h2 { margin: 8px 0 4px; font-size: 24px; }
.cards { display: flex; gap: 12px; width: 100%; max-width: 420px; margin-bottom: 8px; }
.card {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 18px 8px;
  border: 2px solid transparent; border-radius: 18px; background: var(--bg2);
  font: inherit; font-weight: 600; color: var(--text); cursor: pointer;
}
.card .emoji { font-size: 48px; line-height: 1; }
.card[aria-checked="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--bg2)); }
.me { margin: 6px 0 0; font-size: 15px; }
.sheet .hint { max-width: 320px; }
.sheet .btn.primary { margin-top: auto; }
.sheet .btn.primary:disabled { opacity: .4; cursor: default; }
.attr { margin: 0; font-size: 11px; color: var(--hint); }
.attr a { color: inherit; }

/* Telegram-only gate: the app exists only after the server accepted our initData. */
.gate {
  min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 20px; padding: 24px 16px; text-align: center;
}
.gate-logo {
  width: 110px; height: 110px; border-radius: 50%; display: grid; place-items: center;
  font-size: 48px; background: var(--bg2);
}
.gate .btn.primary { display: grid; place-items: center; text-decoration: none; }
body:not([data-auth="ok"]) #app, body:not([data-auth="ok"]) #setup { display: none !important; }
body[data-auth="ok"] #gate { display: none; }
body[data-auth="pending"] .gate-logo { animation: pulse-soft 1.2s ease-in-out infinite alternate; }

/* Opened outside Telegram: a minimal landing that leads into the bot. */
.landing { display: none; flex-direction: column; align-items: center; gap: 16px; width: 100%; max-width: 380px; }
body[data-auth="denied"] .landing { display: flex; }
body[data-auth="denied"] #gate-text { display: none; }
.landing h1 { margin: 0; font-size: 26px; line-height: 1.2; }
.lead { margin: -6px 0 0; color: var(--hint); }
.steps {
  list-style: none; counter-reset: step; width: 100%; margin: 4px 0; padding: 16px;
  display: grid; gap: 14px; text-align: left; border-radius: 16px; background: var(--bg2);
}
.steps li { counter-increment: step; display: flex; align-items: center; gap: 12px; font-weight: 500; }
.steps li::before {
  content: counter(step); flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  font-size: 14px; font-weight: 700; background: var(--accent); color: var(--accent-text);
}
.bot { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.handle {
  display: inline-flex; align-items: center; gap: 10px; padding: 6px 6px 6px 16px; border: 0; border-radius: 999px;
  background: var(--bg2); color: var(--text); font: inherit; cursor: pointer;
}
.handle b { font-size: 17px; }
.handle span { padding: 6px 12px; border-radius: 999px; background: var(--bg); color: var(--hint); font-size: 13px; font-weight: 600; }
.handle:active { transform: scale(.97); }
@keyframes pulse-soft { from { opacity: .45; } to { opacity: 1; } }

/* State-driven visibility */
#stop { display: none; }
.filter { display: none; }
[data-state="idle"] .filter { display: flex; }
[data-state="idle"] p.filter { display: block; }
#settings { visibility: hidden; }
[data-state="idle"] #settings, [data-state="banned"] #settings { visibility: visible; }
[data-state="waiting"] #start, [data-state="connecting"] #start, [data-state="talking"] #start { display: none; }
[data-state="waiting"] .controls, [data-state="connecting"] .controls, [data-state="talking"] .controls { display: flex; }
[data-state="waiting"] #stop, [data-state="connecting"] #stop, [data-state="talking"] #stop { display: block; }
[data-state="waiting"] #mute, [data-state="waiting"] #report { visibility: hidden; }
[data-state="banned"] footer { display: none; }
