/* XEM AI — тёмная «сцена в сумерках»; цвет подсветки за персонажем = её настроение */
:root {
  --dusk-top: #2c2452;
  --dusk-bottom: #15122a;
  --ink: #f4f0ff;
  --ink-soft: #b9b0d8;
  --ink-faint: #8a82ab;
  --line: rgba(255, 255, 255, 0.09);
  --glass: rgba(26, 21, 48, 0.62);
  --bubble-me: #efe9ff;
  --bubble-me-ink: #241c42;
  --bubble-her: rgba(255, 255, 255, 0.075);
  --danger: #ff6b7d;
  --mood: #9aa7ff;          /* меняется из JS */
  --mood-soft: rgba(154, 167, 255, 0.28);
  --r-lg: 22px;
  --r-md: 14px;
  --r-sm: 10px;
  --font: "Manrope", system-ui, -apple-system, "Segoe UI", sans-serif;
  --display: "Unbounded", "Manrope", system-ui, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.5 var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--dusk-top), var(--dusk-bottom) 70%);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--mood); outline-offset: 2px; }

/* ── раскладка ── */
.shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(360px, 0.95fr);
}
.stage { position: relative; overflow: hidden; min-height: 0; }
.panel {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin: 14px 14px 14px 0;
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  backdrop-filter: blur(18px) saturate(1.2);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
}

/* ── сцена ── */
.glow {
  position: absolute; inset: -10%;
  background:
    radial-gradient(42% 46% at 50% 58%, var(--mood-soft), transparent 70%),
    radial-gradient(70% 30% at 50% 100%, rgba(0, 0, 0, 0.35), transparent 70%);
  transition: background 1.2s ease;
  pointer-events: none;
}
#avatar-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.stage-fallback { position: absolute; inset: 0; display: grid; place-items: center; }
.orb {
  width: min(38vh, 60%); aspect-ratio: 1; border-radius: 50%;
  background: radial-gradient(circle at 38% 34%, #fff8, var(--mood) 42%, transparent 72%);
  filter: blur(2px);
  animation: breathe 4.5s ease-in-out infinite;
}
@keyframes breathe { 50% { transform: scale(1.05); } }

.stage-head {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 22px 26px;
  pointer-events: none;
}
.stage-head > * { pointer-events: auto; }
.who h1 {
  margin: 0;
  font: 700 clamp(26px, 3.2vw, 40px)/1.05 var(--display);
  letter-spacing: -0.01em;
}
.who p {
  margin: 6px 0 0;
  color: var(--ink-soft);
  font-weight: 500;
  display: flex; align-items: center; gap: 8px;
}
.who p::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--mood); box-shadow: 0 0 12px var(--mood);
  transition: background 0.8s;
}
.stage-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06);
  cursor: pointer; transition: background 0.2s;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.icon-btn svg, .send svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.credit { position: absolute; left: 26px; bottom: 16px; margin: 0; font-size: 12px; color: var(--ink-faint); max-width: 60%; }
@media (max-width: 860px) { .credit { left: 16px; bottom: 30px; font-size: 11px; } }
.loading-note { position: absolute; left: 0; right: 0; bottom: 24px; text-align: center; color: var(--ink-faint); margin: 0; }

/* ── вход ── */
.auth { justify-content: center; }
.auth-inner { width: 100%; max-width: 380px; margin: 0 auto; padding: 32px 28px; }
.brand { font: 700 30px/1.1 var(--display); margin: 0 0 10px; }
.auth-lead { color: var(--ink-soft); margin: 0 0 26px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 999px; background: rgba(255,255,255,0.06); margin-bottom: 20px; }
.tab { border: 0; background: transparent; padding: 9px; border-radius: 999px; cursor: pointer; color: var(--ink-soft); font-weight: 600; }
.tab.active { background: var(--ink); color: var(--bubble-me-ink); }

label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-soft); margin: 0 0 14px; }
input, textarea {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 14px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.05);
  color: var(--ink); font-size: 15px; font-weight: 500;
  transition: border-color 0.2s, background 0.2s;
}
input:focus, textarea:focus { outline: none; border-color: var(--mood); background: rgba(255,255,255,0.08); }
textarea { resize: vertical; }
.field-hint { margin: -8px 0 14px; font-size: 12.5px; color: var(--ink-faint); transition: color 0.2s; }
.field-hint.bad { color: var(--danger); }
.form-error { color: var(--danger); min-height: 1.2em; margin: -4px 0 10px; font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 10px 16px; border-radius: 999px;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.07);
  font-weight: 600; cursor: pointer; transition: background 0.2s, transform 0.1s;
}
.btn:hover { background: rgba(255, 255, 255, 0.13); }
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--ink); color: var(--bubble-me-ink); border-color: transparent; width: 100%; padding: 12px; }
.btn.primary:hover { background: #fff; }
.btn.danger { color: var(--danger); border-color: rgba(255, 107, 125, 0.35); }
.btn[disabled] { opacity: 0.5; pointer-events: none; }

/* ── чат ── */
.messages { flex: 1; overflow-y: auto; padding: 22px 20px 8px; scroll-behavior: smooth; overscroll-behavior: contain; }
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 8px; }
.older { display: block; margin: 0 auto 14px; border: 0; background: none; color: var(--ink-faint); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }

.msg { display: flex; margin: 0 0 10px; }
.msg .bubble {
  max-width: 86%;
  padding: 10px 14px;
  border-radius: 18px;
  white-space: normal;
  overflow-wrap: anywhere;
}
.msg.me { justify-content: flex-end; }
.msg.me .bubble { background: var(--bubble-me); color: var(--bubble-me-ink); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.her .bubble { background: var(--bubble-her); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.her.fresh .bubble { border-color: color-mix(in srgb, var(--mood) 45%, transparent); }
.msg.note { justify-content: center; }
.msg.note .bubble { background: none; color: var(--ink-faint); font-size: 13px; text-align: center; padding: 4px; }
.msg.note.error .bubble { color: var(--danger); }

.bubble p { margin: 0 0 8px; }
.bubble p:last-child, .bubble ul:last-child, .bubble ol:last-child, .bubble pre:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 20px; }
.bubble code { font-family: ui-monospace, Consolas, monospace; font-size: 0.9em; background: rgba(0,0,0,0.25); padding: 1px 5px; border-radius: 5px; }
.bubble pre { margin: 6px 0 8px; padding: 10px 12px; background: rgba(0,0,0,0.35); border-radius: var(--r-sm); overflow-x: auto; }
.bubble pre code { background: none; padding: 0; font-size: 13px; }
.bubble h4 { margin: 6px 0 4px; font-size: 15px; }

.typing { display: inline-flex; gap: 5px; padding: 4px 2px; align-items: center; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-soft); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }
.typing span { margin-left: 8px; color: var(--ink-faint); font-size: 13px; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.empty { text-align: center; padding: 8vh 10px 20px; color: var(--ink-soft); }
.empty p { margin: 0 0 16px; font-size: 16px; }
.starters { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.starters button {
  border: 1px solid var(--line); background: rgba(255,255,255,0.05);
  border-radius: 999px; padding: 8px 14px; cursor: pointer; color: var(--ink);
}
.starters button:hover { background: rgba(255,255,255,0.1); }

.composer {
  display: flex; align-items: flex-end; gap: 10px;
  margin: 8px 12px calc(12px + var(--safe-b));
  padding: 6px 6px 6px 16px;
  border-radius: 24px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.06);
  transition: border-color 0.2s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--mood) 60%, transparent); }
.composer textarea {
  flex: 1; margin: 0; padding: 9px 0; border: 0; background: transparent; resize: none;
  max-height: 160px; line-height: 1.45;
}
.composer textarea:focus { background: transparent; }
.send {
  flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0; cursor: pointer;
  display: grid; place-items: center;
  background: var(--mood); color: #1b1530;
  transition: background 0.8s, transform 0.1s, opacity 0.2s;
}
.send svg { stroke-width: 2.2; }
.send .i-mic { display: none; }
.send.mic .i-mic { display: block; }
.send.mic .i-send { display: none; }
.send.listening { background: #ff5d6e; color: #fff; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(255,93,110,0.45); } 50% { box-shadow: 0 0 0 12px rgba(255,93,110,0); } }
.history-toggle { display: none; }
.send:active { transform: scale(0.94); }
.send[disabled] { opacity: 0.35; cursor: default; }

/* ── настройки ── */
.scrim { position: fixed; inset: 0; background: rgba(8, 6, 20, 0.5); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 21;
  width: min(420px, 100vw);
  background: #1e1936;
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  animation: slide-in 0.25s ease-out;
}
@keyframes slide-in { from { transform: translateX(30px); opacity: 0; } }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font: 700 20px var(--display); }
.drawer-body { overflow-y: auto; padding: 6px 20px calc(30px + var(--safe-b)); }
.drawer section { padding: 18px 0; border-bottom: 1px solid var(--line); }
.drawer section:last-child { border-bottom: 0; }
.drawer h3 { margin: 0 0 12px; font-size: 16px; font-weight: 700; }
.drawer .btn + .btn { margin-left: 6px; }
.drawer .btn.primary { margin-top: 2px; }
.hint { color: var(--ink-faint); font-size: 13px; margin: 0 0 12px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }
.row .btn + .btn { margin-left: 0; }
.saved-note { color: var(--mood); font-size: 13px; margin: 8px 0 0; text-align: center; }
details { margin: 0 0 12px; }
summary { cursor: pointer; color: var(--ink-soft); font-weight: 600; margin-bottom: 10px; }
#btn-logout { margin-top: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip {
  border: 1px solid var(--line); background: rgba(255,255,255,0.05);
  padding: 8px 13px; border-radius: 999px; cursor: pointer; font-weight: 600;
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bubble-me-ink); border-color: transparent; }

.avatar-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; margin-bottom: 16px; }
.av {
  border: 2px solid var(--line); border-radius: var(--r-md); background: rgba(255,255,255,0.04);
  padding: 0; cursor: pointer; overflow: hidden; text-align: center;
}
.av .pic { aspect-ratio: 1; display: grid; place-items: center; font: 700 30px var(--display); color: var(--ink-soft);
  background: radial-gradient(circle at 50% 40%, rgba(255,255,255,0.12), transparent 70%); }
.av .pic img { width: 100%; height: 100%; object-fit: cover; }
.av span { display: block; padding: 6px 4px 8px; font-size: 13px; font-weight: 600; }
.av[aria-pressed="true"] { border-color: var(--mood); }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%);
  z-index: 30; background: #f4f0ff; color: #241c42; padding: 10px 18px; border-radius: 999px;
  font-weight: 600; box-shadow: 0 8px 30px rgba(0,0,0,0.35); max-width: 90vw; text-align: center;
}

/* ── загрузка ── */
body.booting .panel { opacity: 0; }
.panel { transition: opacity 0.4s; }

/* ── телефон: сцена на весь экран, реплики поверх ── */
@media (max-width: 860px) {
  .shell { display: block; position: relative; height: 100dvh; }
  .stage { position: absolute; inset: 0; }
  .stage::after {
    content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 58%; pointer-events: none;
    background: linear-gradient(180deg, rgba(23,19,42,0) 0%, rgba(23,19,42,0.88) 62%, #17132a 100%);
  }
  .stage-head { padding: calc(env(safe-area-inset-top, 0px) + 14px) 18px 0; z-index: 2; }
  .who h1 { font-size: 28px; }
  .credit { bottom: auto; top: calc(env(safe-area-inset-top, 0px) + 84px); left: 18px; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; margin: 0; z-index: 3;
    background: transparent; border: 0; border-radius: 0; backdrop-filter: none; -webkit-backdrop-filter: none;
  }
  .panel.auth { background: rgba(23,19,42,0.88); border-radius: 24px 24px 0 0; border-top: 1px solid var(--line);
    backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); padding-bottom: var(--safe-b); }
  .auth-inner { padding: 24px 22px 18px; }
  .auth-lead { margin-bottom: 18px; }
  .chat .messages {
    flex: none; max-height: 34dvh; padding: 24px 14px 4px;
    -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 64px); mask-image: linear-gradient(180deg, transparent 0, #000 64px);
  }
  .msg .bubble { max-width: 84%; }
  .msg.her .bubble { background: rgba(30,25,54,0.74); }
  .history-toggle {
    display: block; align-self: center; border: 0; background: none; color: var(--ink-soft);
    font-weight: 600; font-size: 13px; padding: 6px 12px; cursor: pointer;
  }
  .history-toggle::after { content: " ↑"; }
  .chat.expanded { top: 0; background: rgba(23,19,42,0.95); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
  .chat.expanded .messages { flex: 1; max-height: none; padding-top: calc(env(safe-area-inset-top, 0px) + 80px); -webkit-mask-image: none; mask-image: none; }
  .chat.expanded .history-toggle::after { content: " ↓"; }
  .composer { margin: 4px 14px calc(14px + var(--safe-b)); padding: 6px 6px 6px 16px; background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.14); }
  .send { width: 52px; height: 52px; }
  .send.mic { width: 58px; height: 58px; box-shadow: 0 0 0 6px color-mix(in srgb, var(--mood) 22%, transparent); }
  .empty { padding: 4px 10px 8px; }
  .empty p { margin-bottom: 10px; font-size: 15px; }
  .shell.keyboard .chat .messages { max-height: 26dvh; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

.ai-note { margin: 18px 0 0; font-size: 12px; color: var(--ink-faint); text-align: center; }
