:root {
  --bg: #0d1020;
  --bg2: #161a33;
  --card: #1b2040;
  --line: #2b3260;
  --text: #eef1ff;
  --muted: #9aa3d0;
  --accent: #5fb8ff;
  --accent2: #9b8cff;
  --danger: #ff6b81;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% 0%, #20275a 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  display: flex; flex-direction: column;
  overflow: hidden; user-select: none;
}

/* ---- Yuqori panel ---- */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 8px;
}
.title { display: flex; flex-direction: column; min-width: 0; }
.title .name { font-size: 22px; font-weight: 700; letter-spacing: .3px; }
.title .sub { font-size: 13px; color: var(--muted); margin-top: 2px; min-height: 1.2em; }
.icon-btn {
  background: transparent; border: 0; color: var(--muted);
  width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  font-size: 20px; cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,.06); }

/* ---- Sahna va avatar ---- */
.stage {
  flex: 1; position: relative; display: flex; flex-direction: column;
  align-items: center; justify-content: center; min-height: 0; padding: 0 16px;
}
#avatar {
  width: min(78vw, 360px); max-height: 56vh; position: relative; z-index: 1;
  filter: drop-shadow(0 18px 40px rgba(0,0,0,.45));
}
#figure { animation: breathe 4.2s ease-in-out infinite; transform-origin: 150px 340px; }
@keyframes breathe { 50% { transform: translateY(-3px) scale(1.006); } }
#head { transform-origin: 150px 230px; transition: transform .6s ease; }

.halo {
  position: absolute; width: min(82vw, 380px); aspect-ratio: 1; border-radius: 50%;
  top: 50%; left: 50%; transform: translate(-50%, -54%);
  background: radial-gradient(circle, rgba(95,184,255,.18), rgba(95,184,255,0) 65%);
  transition: background .4s, transform .2s;
}
body.listening .halo { background: radial-gradient(circle, rgba(95,184,255,.38), rgba(95,184,255,0) 66%); }
body.thinking .halo { background: radial-gradient(circle, rgba(155,140,255,.32), rgba(155,140,255,0) 66%); animation: pulse 1.2s ease-in-out infinite; }
body.speaking .halo { background: radial-gradient(circle, rgba(255,160,200,.28), rgba(255,160,200,0) 66%); }
@keyframes pulse { 50% { transform: translate(-50%, -54%) scale(1.06); } }

/* Ko'zlar: pirpirash */
.eye { transform-box: fill-box; transform-origin: center; transition: transform .07s ease-in; }
#avatar.blink .eye-open { transform: scaleY(.08); }
.eye-open { transform-box: fill-box; transform-origin: center 60%; transition: transform .07s; }
.eye-happy { display: none; }

/* Og'iz shakllari */
#mouth > [data-m] { display: none; }
#avatar[data-mouth="closed"] [data-m="closed"],
#avatar[data-mouth="smile"] [data-m="smile"],
#avatar[data-mouth="half"] [data-m="half"],
#avatar[data-mouth="open"] [data-m="open"],
#avatar[data-mouth="round"] [data-m="round"],
#avatar[data-mouth="wide"] [data-m="wide"] { display: inline; }

/* Kayfiyatlar */
.blush { opacity: .28; transition: opacity .5s; }
.brow { transition: transform .5s; transform-box: fill-box; transform-origin: center; }
#avatar.mood-happy .blush, #avatar.mood-playful .blush, #avatar.mood-laughing .blush { opacity: .55; }
#avatar.mood-laughing .eye-open { display: none; }
#avatar.mood-laughing .eye-happy { display: inline; }
#avatar.mood-caring .blush { opacity: .45; }
#avatar.mood-caring #browL { transform: rotate(-6deg) translateY(1px); }
#avatar.mood-caring #browR { transform: rotate(6deg) translateY(1px); }
#avatar.mood-sad #browL { transform: rotate(-12deg) translateY(2px); }
#avatar.mood-sad #browR { transform: rotate(12deg) translateY(2px); }
#avatar.mood-surprised .brow { transform: translateY(-6px); }
#avatar.mood-thinking #browL { transform: translateY(-4px) rotate(4deg); }
#avatar.mood-playful #browR { transform: translateY(-4px); }
#avatar.mood-thinking #head { transform: rotate(-4deg); }
#avatar.mood-playful #head { transform: rotate(3deg); }
#avatar.mood-sad #head { transform: rotate(-2deg) translateY(2px); }

.caption {
  position: relative; z-index: 1; margin: 10px 0 0; max-width: 560px; min-height: 2.6em;
  text-align: center; font-size: 15px; line-height: 1.4; color: var(--text); opacity: .85;
}
.caption .you { display: block; color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.caption.hidden { visibility: hidden; }

/* ---- Pastki boshqaruv ---- */
.controls {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 10px 16px calc(22px + var(--safe-bot)); gap: 8px;
}
.mic {
  grid-column: 2; width: 84px; height: 84px; border-radius: 50%; border: 0; cursor: pointer;
  color: #08122a; background: linear-gradient(160deg, #8fdcff, var(--accent));
  box-shadow: 0 10px 30px rgba(95,184,255,.35), inset 0 -4px 10px rgba(0,0,0,.15);
  display: grid; place-items: center; transition: transform .15s, background .3s;
}
.mic { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.mic:active, body.listening .mic { transform: scale(1.08); }
body.listening .mic { background: linear-gradient(160deg, #ffb3c7, #ff6b9a); box-shadow: 0 0 0 var(--ring, 0px) rgba(255,107,154,.25), 0 10px 30px rgba(255,107,154,.35); }
body.thinking .mic { background: linear-gradient(160deg, #c9c0ff, var(--accent2)); }
body.speaking .mic { background: linear-gradient(160deg, #ffe0a3, #ffb357); }
.hint { grid-column: 3; justify-self: start; font-size: 12px; color: var(--muted); }

.toggle { grid-column: 1; justify-self: end; display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 12px; color: var(--muted); }
.toggle input { display: none; }
.toggle .track { width: 38px; height: 22px; border-radius: 11px; background: var(--line); position: relative; transition: background .2s; flex: none; }
.toggle .knob { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .2s; }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track .knob { transform: translateX(16px); }
.toggle-label { max-width: 70px; line-height: 1.2; }

/* ---- Sozlamalar ---- */
dialog#settings {
  border: 0; padding: 0; margin: 0; width: 100%; max-width: 100%; height: 100%; max-height: 100%;
  background: var(--bg); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
.sheet { max-width: 640px; margin: 0 auto; padding: calc(10px + var(--safe-top)) 16px calc(30px + var(--safe-bot)); user-select: text; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--bg); z-index: 2; padding: 6px 0; }
.sheet h2 { margin: 0; font-size: 20px; }
fieldset { border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px 14px; margin: 14px 0; background: var(--bg2); display: flex; flex-direction: column; gap: 10px; }
legend { padding: 0 6px; color: var(--accent); font-weight: 600; font-size: 14px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
input, textarea, select {
  font: inherit; font-size: 15px; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
textarea { resize: vertical; line-height: 1.4; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
small { color: var(--muted); font-size: 12px; line-height: 1.4; }
button { font: inherit; }
.btn-primary, .btn-secondary, .btn-danger {
  border: 0; border-radius: 12px; padding: 12px 14px; font-size: 15px; font-weight: 600; cursor: pointer;
}
.btn-primary { width: 100%; background: var(--accent); color: #08122a; margin-top: 6px; }
.btn-secondary { background: var(--card); color: var(--text); border: 1px solid var(--line); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid rgba(255,107,129,.4); }
.btn-link { background: none; border: 0; color: var(--accent); padding: 0; text-align: left; font-size: 13px; cursor: pointer; }

@media (min-width: 700px) {
  #avatar { width: 380px; }
}

input[type="range"] { padding: 0; height: 28px; background: transparent; border: 0; accent-color: var(--accent); }
label b { color: var(--text); font-weight: 600; }

.enroll { display: flex; flex-direction: column; gap: 10px; padding: 12px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }
.enroll[hidden] { display: none; }
.enroll-phrase { margin: 0; font-size: 17px; line-height: 1.45; color: var(--text); }
.enroll-btn { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; margin-top: 0; }
.enroll-btn.rec { background: #ff6b9a; color: #fff; }
.caption .you.other { color: #ffb357; }
.who { display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 8px; font-size: 11px; vertical-align: middle; }
.who.owner { background: rgba(95,184,255,.18); color: var(--accent); }
.who.other { background: rgba(255,179,87,.18); color: #ffb357; }

#vrm-canvas { position: relative; z-index: 1; display: block; touch-action: none; }
#vrm-canvas[hidden] { display: none; }
.file-btn { display: block; text-align: center; cursor: pointer; color: var(--text); font-weight: 600; font-size: 15px; }

.top-actions { display: flex; gap: 2px; }
.icon-btn[hidden] { display: none; }
dialog.bottom-sheet {
  position: fixed; inset: auto 0 0 0; margin: 0 auto; width: 100%; max-width: 640px; max-height: 52vh; overflow: auto;
  border: 1px solid var(--line); border-bottom: 0; border-radius: 18px 18px 0 0; padding: 4px 16px calc(16px + var(--safe-bot));
  background: rgba(22, 26, 51, .97); color: var(--text); box-shadow: 0 -10px 40px rgba(0,0,0,.45); z-index: 20;
}
.bottom-sheet .sheet-head { background: transparent; position: static; }
.wb-outfits { display: flex; gap: 8px; overflow-x: auto; padding: 6px 0 10px; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); color: var(--text); border-radius: 20px; padding: 9px 14px; font-size: 14px; cursor: pointer; white-space: nowrap; }
.chip.active { background: var(--accent); color: #08122a; border-color: var(--accent); font-weight: 600; }
.chip.add { border-style: dashed; color: var(--accent); }
.wb-h { margin: 14px 0 6px; font-size: 14px; color: var(--accent); }
.wb-colors { display: flex; flex-direction: column; gap: 6px; }
.wb-row { display: grid; grid-template-columns: 1fr 52px 34px; align-items: center; gap: 8px; font-size: 14px; }
.wb-row input[type="color"] { width: 52px; height: 34px; padding: 2px; border-radius: 8px; background: var(--card); border: 1px solid var(--line); }
.wb-row .reset { background: none; border: 0; color: var(--muted); font-size: 18px; cursor: pointer; }
.wb-foot { display: flex; justify-content: space-between; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.btn-link.danger { color: var(--danger); }
