:root {
  --bg: #eef2fb;
  --card: #ffffff;
  --blue: #2f6bff;
  --blue-dark: #1b4fd6;
  --blue-soft: #e8f0ff;
  --navy: #16233f;
  --muted: #6b7793;
  --rec: #e5484d;
  --violet: #7c5cff;
  --violet-dark: #5f3fe6;
  --violet-soft: rgba(124, 92, 255, 0.16);
  --speak: #1fab6e;
  --speak-soft: rgba(31, 171, 110, 0.25);
  --glass: rgba(255, 255, 255, 0.62);
  --glass-border: rgba(255, 255, 255, 0.65);
  --spin-color: var(--blue);
  --shadow: 0 10px 30px rgba(28, 55, 120, 0.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: linear-gradient(180deg, #f5f8ff 0%, #e7edfc 55%, #dfe7fa 100%);
  color: var(--navy);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

/* Dekorativ fon nurlari — faqat transform animatsiyalanadi (GPU-friendly) */
body::before,
body::after {
  content: "";
  position: fixed;
  z-index: -1;
  border-radius: 50%;
  filter: blur(80px);
  pointer-events: none;
}
body::before {
  top: -12%;
  left: -10%;
  width: 55vw;
  height: 55vw;
  background: radial-gradient(circle, rgba(47, 107, 255, 0.16), transparent 70%);
  animation: drift 26s ease-in-out infinite alternate;
}
body::after {
  bottom: -16%;
  right: -12%;
  width: 60vw;
  height: 60vw;
  background: radial-gradient(circle, rgba(124, 92, 255, 0.14), transparent 70%);
  animation: drift 30s ease-in-out infinite alternate-reverse;
}
@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to   { transform: translate(4%, 3%) scale(1.08); }
}

.app {
  max-width: 460px;
  margin: 0 auto;
  padding: max(32px, env(safe-area-inset-top, 0px) + 20px) 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  user-select: none;
  touch-action: manipulation;
}
.app > * {
  animation: fadeInUp 0.55s ease both;
}
.app > *:nth-child(1) { animation-delay: 0s; }
.app > *:nth-child(2) { animation-delay: 0.08s; }
.app > *:nth-child(3) { animation-delay: 0.16s; }
.app > *:nth-child(4) { animation-delay: 0.24s; }
.app > *:nth-child(5) { animation-delay: 0.32s; }
.app > *:nth-child(6) { animation-delay: 0.38s; }
.app > *:nth-child(7) { animation-delay: 0.42s; }
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 2px;
}
.app-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: 0.5px;
}
.logo { display: inline-flex; }
.logo img { width: 28px; height: 28px; object-fit: contain; }

/* Gapiruvchi surat */
.face {
  position: relative;
  width: 100%;
  /* Nisbat: jonli avatar (Anam) ulansa JS uni video'ning haqiqiy nisbatiga
     o'rnatadi; aks holda statik surat nisbati (512×512) ishlaydi —
     kadr kesilmasin, atrofda masofa qolsin. talking-photo.js dagi
     IMAGE_ASPECT bilan DOIM birga o'zgaradi. */
  aspect-ratio: var(--face-ar, 1 / 1);
  /* ulanish/uzilishda (nisbat) va holat almashinuvida (nur) yumshoq o'tish */
  transition: aspect-ratio 0.25s ease, box-shadow 0.3s ease;
  border-radius: 18px;
  overflow: hidden;
  background: #dfe6f5;
  box-shadow: var(--shadow);
}
/* AI gapiryapti — avatar atrofida yashil nur (layoutga tegmaydi) */
body[data-state="speaking"] .face {
  box-shadow: 0 0 0 3px var(--speak-soft), 0 12px 40px rgba(31, 171, 110, 0.35);
}
.face canvas,
.face .face-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
/* Jonli avatar (Anam WebRTC) — faollashganda talking-photo ustida ko'rinadi */
#avatarVideo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
  z-index: 2;
}
#avatarVideo.on { display: block; }

/* WebGL yo'q bo'lsa — yumshoq "gapirish" pulsatsiyasi */
.face-img.speaking { animation: talk 0.45s ease-in-out infinite alternate; }
@keyframes talk {
  from { transform: scale(1); }
  to   { transform: scale(1.012); }
}

/* Til qatori */
.lang-title {
  margin: 2px 0 0;
  text-align: center;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.4px;
  color: var(--blue);
}
/* Til tugmalari bosilmaydi — savol tilini backend o'zi aniqlaydi */
.lang-note {
  margin: -12px 0 0; /* sarlavhaga yopishsin (.app gap'ini qoplaydi) */
  text-align: center;
  font-size: 0.66rem;
  font-weight: 500;
  color: var(--muted);
}
.lang-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.lang {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 12px;
  min-height: 64px;
  background: var(--glass);
  backdrop-filter: blur(14px) saturate(1.3);
  -webkit-backdrop-filter: blur(14px) saturate(1.3);
  border: 2px solid var(--glass-border);
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(28, 55, 120, 0.06);
  transition: border-color 0.25s, box-shadow 0.25s, background 0.25s;
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .lang { background: var(--card); }
}
.lang:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 3px;
}
.lang .flag {
  display: inline-flex;
  border-radius: 50%;
  overflow: hidden;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06);
  flex: none;
}
.lang-text { display: flex; flex-direction: column; line-height: 1.1; }
.lang-text b { font-size: 1rem; }
.lang-text small { font-size: 0.66rem; color: var(--muted); }
.lang.active {
  border-color: var(--blue);
  background: var(--blue-soft);
  box-shadow: 0 0 0 3px rgba(47, 107, 255, 0.14), 0 6px 16px rgba(47, 107, 255, 0.18);
}

/* Holat / matn maydoni */
.stage {
  min-height: 92px;
  max-height: 34vh;
  overflow-y: auto;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 18px 16px;
  background: var(--glass);
  backdrop-filter: blur(14px) saturate(1.3);
  -webkit-backdrop-filter: blur(14px) saturate(1.3);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .stage { background: var(--card); }
}
.stage::-webkit-scrollbar { width: 0; height: 0; }
.q-line {
  margin: 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--navy);
}
.stage-text {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  color: var(--blue);
  line-height: 1.55;
}

/* Manba chiplari — [hidden] bilan to'qnashmasin (attract overlay'dagi kabi) */
.source-chips:not([hidden]) {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}
.source-label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
}
.source-chip {
  background: var(--blue-soft);
  color: var(--blue-dark);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.72rem;
  font-weight: 600;
}

/* Spinner — qo'zg'almas nuqtalar, yorqin/katta nuqta doira bo'ylab aylanadi (comet) */
.spinner {
  position: relative;
  width: 48px;
  height: 48px;
}
.spinner span {
  position: absolute;
  inset: 0;
  transform: rotate(var(--r));
}
.spinner span::after {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 13px;
  height: 13px;
  margin-left: -6.5px;
  border-radius: 50%;
  background: var(--spin-color);
  transform-origin: center;
  animation: dotpulse 0.9s linear infinite;
  animation-delay: var(--d, 0s);
  transition: background 0.2s;
}
@keyframes dotpulse {
  0%   { transform: scale(1);    opacity: 1; }
  100% { transform: scale(0.32); opacity: 0.2; }
}
body[data-state="recording"] { --spin-color: var(--rec); }
body[data-state="processing"] { --spin-color: var(--violet); }

/* Mikrofon zonasi */
.mic-area {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
}
.wave {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 44px;
  flex: 1;
  overflow: hidden;
}
#waveL { justify-content: flex-end; }
#waveR { justify-content: flex-start; }
.wave span {
  width: 2px;
  flex: none;
  height: calc(var(--h, 6) * 1px);
  border-radius: 2px;
  background: var(--blue);
  opacity: 0.75;
  transform-origin: center;
  transition: background 0.2s;
}

/* Bo'sh holat — yumshoq "nafas", muzlagan chiziqlar taassurotini bermaslik uchun */
.wave span {
  animation: wv-idle 3.2s ease-in-out infinite;
  animation-delay: var(--d, 0s);
}
@keyframes wv-idle {
  0%, 100% { transform: scaleY(0.35); opacity: 0.35; }
  50%      { transform: scaleY(0.55); opacity: 0.55; }
}

/* Yozib olish — JS haqiqiy mikrofon energiyasidan scaleY yozadi (updateVisualizer);
   CSS animatsiyasi o'chiriladi, aks holda inline transform bosib ketiladi. */
body.listening .wave span {
  animation: none;
  background: var(--rec);
  opacity: 1;
}

/* AI gapiryapti — yashil, jonli (mavjud wv keyframe qayta ishlatiladi) */
body[data-state="speaking"] .wave span {
  animation: wv 0.9s ease-in-out infinite;
  animation-delay: var(--d, 0s);
  background: var(--speak);
  opacity: 1;
}
@keyframes wv {
  0%, 100% { transform: scaleY(0.4); opacity: 0.6; }
  50%      { transform: scaleY(1);   opacity: 1; }
}

.mic-btn {
  flex: none;
  width: 96px;
  height: 96px;
  border: none;
  border-radius: 50%;
  color: #fff;
  background: radial-gradient(circle at 35% 30%, #6fa0ff, var(--blue) 55%, var(--blue-dark));
  box-shadow: 0 8px 22px rgba(47, 107, 255, 0.45), 0 0 0 6px rgba(47, 107, 255, 0.12);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.2s, background 0.2s;
  /* bosib turib gapirish: uzoq bosish scroll/zoom/menyu/belgilashga aylanmasin */
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
.mic-btn .mic-glyph { width: 38px; height: 38px; }
@media (hover: hover) {
  .mic-btn:hover { transform: scale(1.05); }
}
.mic-btn:active { transform: scale(0.94); }
.mic-btn:disabled { cursor: default; opacity: 0.85; }
/* Kiosk (?kiosk=1): kursor yo'q; u mikrofon ustida qolib ketsa ham tugma
   doim kattalashib turmasin */
.kiosk, .kiosk * { cursor: none !important; }
.kiosk .mic-btn:hover { transform: none; }
.mic-btn:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 4px;
  box-shadow: 0 0 0 3px var(--blue-dark);
}

.mic-btn .stop-glyph { display: none; }

/* Yozib olish holati — qizil stop kvadrat */
.mic-btn.recording {
  background: radial-gradient(circle at 35% 30%, #ff8a8d, var(--rec) 60%, #c23b3f);
  box-shadow: 0 8px 22px rgba(229, 72, 77, 0.45), 0 0 0 6px rgba(229, 72, 77, 0.14);
  animation: throb 1.1s ease-in-out infinite;
}
.mic-btn.recording .mic-glyph { display: none; }
.mic-btn.recording .stop-glyph {
  display: block;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: #fff;
}
@keyframes throb {
  0%, 100% { box-shadow: 0 8px 22px rgba(229, 72, 77, 0.45), 0 0 0 6px rgba(229, 72, 77, 0.14); }
  50%      { box-shadow: 0 8px 22px rgba(229, 72, 77, 0.55), 0 0 0 12px rgba(229, 72, 77, 0.05); }
}

/* O'ylayapman — binafsha, sekin aylanadigan halo */
body[data-state="processing"] .mic-btn {
  background: radial-gradient(circle at 35% 30%, #b3a2ff, var(--violet) 55%, var(--violet-dark));
  box-shadow: 0 8px 22px rgba(124, 92, 255, 0.45), 0 0 0 6px var(--violet-soft);
}

/* Gapiryapti — yashil nur, mikrofon ochiq (barge-in) */
body[data-state="speaking"] .mic-btn {
  background: radial-gradient(circle at 35% 30%, #6fe0b8, var(--speak) 55%, #128a58);
  box-shadow: 0 8px 22px rgba(31, 171, 110, 0.45), 0 0 0 6px var(--speak-soft);
  animation: speak-pulse 1.6s ease-in-out infinite;
}
@keyframes speak-pulse {
  0%, 100% { box-shadow: 0 8px 22px rgba(31, 171, 110, 0.45), 0 0 0 6px var(--speak-soft); }
  50%      { box-shadow: 0 8px 22px rgba(31, 171, 110, 0.55), 0 0 0 12px rgba(31, 171, 110, 0.08); }
}

.mic-label {
  margin: 0;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: var(--blue);
  transition: color 0.2s;
}
body[data-state="recording"] .mic-label { color: var(--rec); }
body[data-state="processing"] .mic-label { color: var(--violet-dark); }
body[data-state="speaking"] .mic-label { color: var(--speak); }

.error {
  margin: 0;
  padding: 10px 12px;
  font-size: 0.85rem;
  color: #b3261e;
  background: #fdecea;
  border-radius: 10px;
  text-align: center;
}

/* Kattaroq ekran (kiosk) — biroz kengroq */
@media (min-width: 900px) {
  .app { max-width: 640px; }
  .app-header h1 { font-size: 1.8rem; }
  .stage-text { font-size: 1.25rem; }
}

/* 65" kiosk ekran — katta shrift va teginish nishonlari */
@media (min-width: 1400px) {
  .app { max-width: 780px; }
  .app-header h1 { font-size: 2.2rem; }
  .stage-text { font-size: 1.4rem; line-height: 1.6; }
  .q-line { font-size: 1.05rem; }
  .lang-text b { font-size: 1.3rem; }
  .mic-btn { width: 120px; height: 120px; }
  .mic-btn .mic-glyph { width: 46px; height: 46px; }
  .wave { height: 56px; gap: 3px; }
  .wave span { width: 3px; }
}

/* Tik (portret) kiosk ekran — 2160×3840 @300% = 720×1280 CSS px (9:16).
   Yuqoridagi min-width so'rovlari bunga TUSHMAYDI (eni atigi 720px), shuning
   uchun .app 460px da qolib, ekranning pastki yarmi bo'sh turardi. Bu yerda
   hamma o'lcham vh'da — sahifa ekran balandligiga to'la cho'ziladi, scroll yo'q.
   Avatar kvadrat qoladi (gid surati kesilmasin); ortgan joyni javob maydoni
   (.stage, flex: 1) oladi. Telefon (min-height) va planshet (aspect) bunga
   tushmaydi. */
@media (orientation: portrait) and (max-aspect-ratio: 3/4) and (min-height: 900px) {
  html, body { height: 100%; overflow: hidden; }

  .app {
    max-width: none;
    height: 100vh;
    height: 100dvh;
    padding: 2.4vh 4vw 2vh;
    gap: 1.3vh;
  }

  .app-header { justify-content: center; gap: 1.2vh; padding: 0; }
  .app-header h1 { font-size: 3.4vh; }
  .logo img { width: 4.2vh; height: 4.2vh; }

  .face {
    flex: none;
    align-self: center;
    /* 46vh: qolgan bloklar ~54vh — yig'indi 100vh dan oshsa pastki yozuv kesiladi */
    width: min(100%, 46vh);
    border-radius: 2.2vh;
    transition: width 0.45s ease, aspect-ratio 0.25s ease, box-shadow 0.3s ease;
  }
  /* Javob ekranda (qLine faqat shunda ko'rinadi) — avatar kichrayib, joyni
     matnga beradi: 46vh avatar bilan javob maydoniga ~4 qator sig'ardi,
     110 so'zlik javob esa ~16 qator. talking-photo ResizeObserver bilan
     o'zi moslashadi. */
  body:has(#qLine:not([hidden])) .face { width: min(100%, 28vh); }

  .lang-title { font-size: 1.4vh; margin: 0.4vh 0 0; }
  .lang-note { font-size: 1.2vh; margin-top: -1.1vh; white-space: nowrap; }
  .lang-row { gap: 1.4vw; }
  .lang {
    min-height: 7vh;
    padding: 1vh 1.6vw;
    gap: 1.2vw;
    border-radius: 1.8vh;
  }
  .lang .flag svg { width: 4.4vh; height: 4.4vh; }
  .lang-text b { font-size: 2.3vh; }
  .lang-text small { font-size: 1.4vh; }

  .stage {
    flex: 1 1 0;
    min-height: 11vh;
    max-height: none;
    /* safe: matn idishdan uzun bo'lsa boshi tepadan kesilib qolmasin */
    justify-content: safe center;
    gap: 1vh;
    padding: 1.6vh 3vw;
    border-radius: 2vh;
  }
  .q-line { font-size: 1.8vh; }
  .stage-text { font-size: 2.2vh; line-height: 1.5; }
  .source-label,
  .source-chip { font-size: 1.4vh; }
  .spinner { width: 5vh; height: 5vh; }
  .spinner span::after { width: 1.3vh; height: 1.3vh; margin-left: -0.65vh; }

  .mic-area { gap: 3vw; }
  .wave { height: 6vh; gap: 3px; }
  .wave span { width: 3px; }
  .mic-btn { width: 10.5vh; height: 10.5vh; }
  .mic-btn .mic-glyph { width: 4.4vh; height: 4.4vh; }
  .mic-btn.recording .stop-glyph { width: 3vh; height: 3vh; border-radius: 0.7vh; }
  .mic-label { font-size: 2vh; }
  .error { font-size: 1.7vh; padding: 1vh 2vw; }

  .attract-card { padding: 1vh 2.4vh 1vh 1vh; column-gap: 1.4vh; }
  .attract-mic { width: 5.6vh; height: 5.6vh; }
  .attract-mic svg { width: 2.8vh; height: 2.8vh; }
  .attract-text { font-size: 2.6vh; }
  .attract-hint { font-size: 1.3vh; white-space: nowrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---- Attract (jalb qilish) rejimi ---- */
/* Mikrofon tugmasi ustida turadigan kichik "gap pufagi" — ekranni qoplamaydi,
   orqa fonni xiralashtirmaydi va teginishlarni to'smaydi (mikrofonni birinchi
   bosishdayoq yozish boshlanadi). `top` ni JS mikrofon tugmasiga qarab
   qo'yadi (app.js::placeAttract) — absolute, ya'ni sahifa scroll bo'lsa ham
   tugma ustida qoladi.
   display:none bazaviy holat — [hidden] bilan bir xil ustuvorlikda to'qnashmasin
   (aks holda author qoidasi UA'ning [hidden]{display:none}'idan ustun kelib,
   pufak "hidden" bo'lsa ham ko'rinib qolardi) */
.attract {
  position: absolute;
  left: 50%;
  top: 0;
  z-index: 50;
  display: none;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.35s ease;
  pointer-events: none;
}
.attract:not([hidden]) { display: block; }
.attract.show { opacity: 1; }
.attract-card {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas: "mic text" "mic hint";
  align-items: center;
  column-gap: 14px;
  row-gap: 2px;
  padding: 12px 22px 12px 12px;
  background: #fff;
  border: 2px solid var(--blue-soft);
  border-radius: 999px;
  box-shadow: 0 10px 28px rgba(28, 55, 120, 0.22);
  animation: attract-in 0.5s ease both, attract-bob 2.6s ease-in-out 0.5s infinite;
}
/* Pastga — mikrofon tugmasiga qaragan uchburchak */
.attract-card::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -9px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-right: 2px solid var(--blue-soft);
  border-bottom: 2px solid var(--blue-soft);
  transform: translateX(-50%) rotate(45deg);
}
@keyframes attract-in {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes attract-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}
.attract-mic {
  grid-area: mic;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  background: radial-gradient(circle at 35% 30%, #6fa0ff, var(--blue) 55%, var(--blue-dark));
}
.attract-mic svg { width: 24px; height: 24px; }
.attract-text {
  grid-area: text;
  margin: 0;
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--navy);
  white-space: nowrap;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.attract-text.is-fading { opacity: 0; transform: translateY(4px); }
.attract-hint {
  grid-area: hint;
  margin: 0;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
}
/* Pufak ko'rinib turganda xush kelibsiz matni u bilan ustma-ust tushmasin */
body.attract-on .stage-text { opacity: 0; }
.stage-text { transition: opacity 0.3s ease; }
