:root {
  --bg-top: #1a1726;
  --bg-bottom: #2b2440;
  --accent: #ff7aa8;
  --accent-soft: #ffb3cb;
  --text: #f4f0ff;
  --muted: #a99ec9;
  --panel: rgba(20, 17, 33, 0.72);
  --border: rgba(255, 255, 255, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(170deg, var(--bg-top), var(--bg-bottom));
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

/* Soft glow behind the character */
body::before {
  content: "";
  position: fixed;
  left: 50%;
  bottom: -18vh;
  width: 70vh;
  height: 70vh;
  transform: translateX(-50%);
  background: radial-gradient(circle, rgba(255, 122, 168, 0.20), transparent 62%);
  pointer-events: none;
}

#stage { position: relative; width: 100%; height: 100%; }

#live2d { display: block; width: 100%; height: 100%; position: absolute; inset: 0; z-index: 1; }

/* ---- speech bubble ----
   Sits to the RIGHT of the model so it never covers her, and every dimension
   scales with the viewport rather than being a fixed pixel box. */
#bubble {
  position: absolute;
  z-index: 20;
  /* top / left / width / max-height are set by JS from the model's bounds. */
  right: clamp(12px, 2.5vw, 48px);
  top: 12%;
  width: clamp(240px, 30vw, 520px);
  overflow-y: auto;
  padding: clamp(12px, 1.3vw, 20px) clamp(16px, 1.7vw, 26px);
  background: var(--panel);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: clamp(12px, 1.1vw, 18px);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  transition: opacity .28s ease, transform .28s ease;
  scrollbar-width: thin;
}
#bubble.hidden {
  opacity: 0;
  transform: translateX(14px);
  pointer-events: none;
}
#bubble p {
  margin: 0;
  font-size: clamp(14px, 1.05vw, 19px);
  line-height: 1.55;
}
/* Tail points left, toward her. */
#bubble::after {
  content: "";
  position: absolute;
  left: -8px;
  top: 34px;
  width: 16px; height: 16px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  transform: rotate(45deg);
}

/* Not enough room beside her - drop it below her waist instead, so it still
   reads as hers without covering her face. JS supplies top/width because both
   depend on where the model actually renders. */
#bubble.below-model {
  right: auto;
  left: 50%;
  transform: translateX(-50%);
  overflow-y: auto;
}
#bubble.below-model.hidden { transform: translateX(-50%) translateY(12px); }
/* Tail points up, toward her. */
#bubble.below-model::after {
  left: 50%; top: -8px; bottom: auto; margin: 0 0 0 -8px;
  border-left: 1px solid var(--border);
  border-top: 1px solid var(--border);
  border-bottom: none; border-right: none;
}

/* ---- controls ---- */
#controls {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  width: min(640px, 92vw);
}

button, input {
  font: inherit;
  border-radius: 999px;
  border: 1px solid var(--border);
  outline: none;
}

.mic {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 12px 24px;
  color: #2a1020;
  background: linear-gradient(135deg, var(--accent-soft), var(--accent));
  font-weight: 650;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .2s ease, filter .2s ease;
  box-shadow: 0 8px 22px rgba(255, 122, 168, 0.30);
  user-select: none;
}
.mic:hover:not(:disabled) { transform: translateY(-1px); }
.mic:disabled { filter: grayscale(.7) brightness(.75); cursor: not-allowed; box-shadow: none; }
.mic.recording {
  background: linear-gradient(135deg, #ff9d9d, #ff4d4d);
  box-shadow: 0 0 0 6px rgba(255, 77, 77, 0.18), 0 8px 22px rgba(255, 77, 77, .35);
}
.mic .dot { width: 9px; height: 9px; border-radius: 50%; background: #2a1020; }
.mic.recording .dot { background: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }

#type-form { display: flex; gap: 8px; flex: 1; min-width: 260px; }
#type-input {
  flex: 1;
  padding: 12px 18px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.07);
}
#type-input::placeholder { color: var(--muted); }
#type-input:focus { border-color: var(--accent); background: rgba(255, 255, 255, 0.11); }
#send {
  padding: 12px 20px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.09);
  cursor: pointer;
}
#send:hover { background: rgba(255, 255, 255, 0.16); }

/* ---- status + meta ---- */
#status {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 88px;
  transform: translateX(-50%);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

#meta {
  position: absolute;
  z-index: 20;
  top: 14px; left: 16px;
  display: flex;
  gap: 10px;
  align-items: center;
  /* Both bars sit at the top corners; without a ceiling they grow into each
     other on a small window and the far buttons stop being clickable. */
  flex-wrap: wrap;
  max-width: calc(50% - 24px);
  font-size: 12px;
  color: var(--muted);
}
#emotion-chip {
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  text-transform: lowercase;
  letter-spacing: .3px;
}

/* ---- audio output picker ---- */
#audio-bar {
  position: absolute;
  z-index: 20;
  top: 12px; right: 16px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: calc(50% - 24px);
}
#audio-bar select,
#audio-bar button {
  font-size: 12px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  max-width: 230px;
  cursor: pointer;
}
#audio-bar select option { background: #221c34; color: var(--text); }
#audio-bar button:hover,
#audio-bar select:hover { background: rgba(255, 255, 255, 0.15); }
#audio-bar.warn select { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(255,122,168,.18); }

/* ---- account (auth.js) ---- */
#account { display: flex; align-items: center; gap: 8px; font-size: 12px; }
#account:empty { display: none; }
#account img { width: 26px; height: 26px; border-radius: 50%; border: 1px solid var(--border); }
#account .account-name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

/* ---- volume ---- */
#vol-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.07);
  font-size: 12px;
  color: var(--muted);
}
#vol-icon { font-size: 13px; line-height: 1; }
#vol-value { min-width: 36px; text-align: right; font-variant-numeric: tabular-nums; }
#volume {
  -webkit-appearance: none;
  appearance: none;
  width: 92px;
  height: 4px;
  border: none;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.22);
  cursor: pointer;
}
#volume::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #2a1020;
  box-shadow: 0 2px 6px rgba(0,0,0,.4);
}
#volume::-moz-range-thumb {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #2a1020;
}
#vol-wrap.muted { opacity: .55; }

/* ---- what you said (auto-fades) ---- */
#user-bubble {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 118px;
  transform: translateX(-50%) translateY(0);
  max-width: min(520px, 84vw);
  padding: 10px 18px;
  border-radius: 16px;
  background: rgba(255, 122, 168, 0.16);
  border: 1px solid rgba(255, 122, 168, 0.38);
  backdrop-filter: blur(10px);
  transition: opacity .35s ease, transform .35s ease;
}
#user-bubble.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(10px);
  pointer-events: none;
}
#user-bubble p {
  margin: 0;
  font-size: 14px;
  line-height: 1.45;
  color: var(--accent-soft);
}
#user-bubble p::before { content: "you: "; opacity: .6; }

/* ---- history button + panel ---- */
#history-open, #replay {
  padding: 4px 12px;
  font-size: 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  cursor: pointer;
}
#history-open:hover, #replay:hover:not(:disabled) { background: rgba(255, 255, 255, 0.15); color: var(--text); }
#replay:disabled { opacity: .35; cursor: default; }

#history-panel {
  position: absolute;
  z-index: 40;
  top: 0; left: 0; bottom: 0;
  width: min(420px, 88vw);
  display: flex;
  flex-direction: column;
  background: rgba(16, 13, 26, 0.93);
  backdrop-filter: blur(18px);
  border-right: 1px solid var(--border);
  box-shadow: 14px 0 50px rgba(0, 0, 0, .5);
  transition: transform .3s cubic-bezier(.4, 0, .2, 1), opacity .3s ease;
}
#history-panel.hidden { transform: translateX(-102%); opacity: 0; pointer-events: none; }

#history-panel header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
#history-count { color: var(--muted); font-size: 12px; margin-right: auto; }
#history-panel header button {
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.07);
  color: var(--muted);
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
}
#history-panel header button:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); }
#history-clear.confirm {
  background: rgba(255, 77, 77, .22);
  border-color: rgba(255, 77, 77, .6);
  color: #ffb3b3;
}
#history-close { font-size: 17px !important; line-height: 1; padding: 3px 10px !important; }

#history-list { overflow-y: auto; padding: 14px 16px 20px; }
#history-list .empty { color: var(--muted); font-size: 13px; text-align: center; padding: 30px 0; }
.turn { margin-bottom: 14px; display: flex; flex-direction: column; gap: 4px; }
.turn .who { font-size: 11px; color: var(--muted); letter-spacing: .3px; }
.turn .msg {
  font-size: 13.5px;
  line-height: 1.5;
  padding: 9px 13px;
  border-radius: 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.turn.user .msg { background: rgba(255, 122, 168, 0.15); border: 1px solid rgba(255,122,168,.3); }
.turn.assistant .msg { background: rgba(255, 255, 255, 0.07); border: 1px solid var(--border); }
.turn .emo {
  font-size: 10px;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
  margin-left: 6px;
}

/* ---- caption words ----
   Each word animates in on its own as she reaches it. Kept subtle and short:
   the caption is meant to be read while she talks, so anything slower than
   ~200ms lags behind the voice and anything larger pulls the eye off her. */
#bubble-text .w {
  display: inline-block;
  opacity: 0;
  transform: translateY(0.45em) scale(0.96);
  filter: blur(2px);
  transition: opacity .2s ease-out, transform .26s cubic-bezier(.2, .8, .3, 1),
              filter .2s ease-out;
  will-change: opacity, transform;
}
#bubble-text .w.in {
  opacity: 1;
  transform: none;
  filter: none;
}

/* Respect a reduced-motion preference: fade only, no movement. */
@media (prefers-reduced-motion: reduce) {
  #bubble-text .w {
    transform: none;
    filter: none;
    transition: opacity .12s linear;
  }
}

/* ---- character switcher ---- */
#mmd { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; }
.hidden-canvas { display: none !important; }

#model-switch {
  position: absolute;
  z-index: 25;
  left: clamp(10px, 1.4vw, 22px);
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.model-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 16px 10px 13px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  background: rgba(20, 17, 33, 0.66);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background .18s ease, color .18s ease, border-color .18s ease;
  white-space: nowrap;
}
.model-btn:hover { background: rgba(255, 255, 255, 0.13); color: var(--text); }
.model-btn .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
  transition: background .18s ease, box-shadow .18s ease;
}
.model-btn.active {
  color: var(--text);
  border-color: rgba(255, 122, 168, 0.55);
  background: rgba(255, 122, 168, 0.16);
}
.model-btn.active .dot {
  background: var(--accent);
  box-shadow: 0 0 9px var(--accent);
}
.model-btn:disabled { opacity: .5; cursor: wait; }

/* ---------- screen sharing / pin buttons ---------- */
#screen-share, #screen-react, #pin-overlay {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  font-size: 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
#screen-share:hover, #screen-react:hover, #pin-overlay:hover {
  background: rgba(255, 255, 255, 0.15); color: var(--text);
}
#screen-share .dot, #screen-react .dot, #pin-overlay .dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
}
#screen-share.active, #screen-react.active, #pin-overlay.active {
  color: var(--text);
  border-color: rgba(255, 122, 168, 0.55);
  background: rgba(255, 122, 168, 0.16);
}
#screen-share.active .dot, #screen-react.active .dot, #pin-overlay.active .dot {
  background: var(--accent); box-shadow: 0 0 9px var(--accent);
}
#screen-react.hidden { display: none; }

/* ---------- pinned window: just her ---------- */
html.overlay, html.overlay body {
  background: transparent !important;
  overflow: hidden;
}
html.overlay body::before { display: none; }
html.overlay #model-switch, html.overlay #bubble, html.overlay #user-bubble,
html.overlay #status, html.overlay #history-panel, html.overlay #controls,
html.overlay #meta, html.overlay #audio-bar { display: none !important; }
/* The page drags its own window (overlay-link.js) - no CSS drag region, which
   would swallow the scroll wheel used for zoom. Her canvas takes no input. */
html.overlay body { user-select: none; cursor: grab; }
html.overlay body:active { cursor: grabbing; }
html.overlay body.panning { cursor: move; }
html.overlay canvas { pointer-events: none; }

/* ---------- pinned window: subtitles + listening ---------- */
#pin-caption, #pin-listening { display: none; }
html.overlay #pin-caption {
  /* A speech bubble above her head; the tail points down at her. */
  display: flex;
  flex-direction: column;
  justify-content: flex-end;          /* long lines: keep the newest words */
  position: fixed;
  left: 50%;
  top: 10px;
  transform: translateX(-50%) translateY(-6px) scale(.96);
  width: 88%;
  max-height: 21vh;
  padding: 9px 14px;
  border-radius: 18px;
  background: #fffafc;
  color: #2a1a2e;
  border: 2px solid rgba(255, 122, 168, 0.65);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
  font-size: clamp(11px, 4.1vw, 16px);
  line-height: 1.35;
  text-align: center;
  opacity: 0;
  transition: opacity .3s ease, transform .3s ease;
  pointer-events: none;
}
html.overlay #pin-caption > span {
  /* Clip here, not on the bubble (that would cut off the tail); bottom-
     aligned, so overflow drops the oldest lines, not the newest. */
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: 0;
  overflow: hidden;
}
/* Tail: a bordered triangle (outer) with a filled one (inner) on top. */
html.overlay #pin-caption::before,
html.overlay #pin-caption::after {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  border-style: solid;
  border-color: transparent;
}
html.overlay #pin-caption::before {
  bottom: -15px;
  border-width: 14px 11px 0 11px;
  border-top-color: rgba(255, 122, 168, 0.65);
}
html.overlay #pin-caption::after {
  bottom: -11px;
  border-width: 12px 9px 0 9px;
  border-top-color: #fffafc;
}
html.overlay #pin-caption.on { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
html.overlay #pin-listening {
  display: flex;
  align-items: center;
  gap: 6px;
  position: fixed;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  padding: 4px 11px;
  border-radius: 999px;
  background: rgba(14, 10, 24, 0.74);
  color: #fff;
  font-size: 12px;
  opacity: 0;
  transition: opacity .2s ease;
  pointer-events: none;
}
html.overlay #pin-listening.on { opacity: 1; }
html.overlay #pin-listening .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #ff4d6d; box-shadow: 0 0 8px #ff4d6d;
  animation: pulse 1s infinite;
}

/* ---------- talk key panel ---------- */
#hotkey-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 12px; font-size: 12px; border-radius: 999px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.07);
  color: var(--muted); cursor: pointer; white-space: nowrap;
}
#hotkey-btn:hover { background: rgba(255, 255, 255, 0.15); color: var(--text); }
#hotkey-btn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
#hotkey-btn.active { color: var(--text); border-color: rgba(255, 122, 168, 0.55); background: rgba(255, 122, 168, 0.16); }
#hotkey-btn.active .dot { background: var(--accent); box-shadow: 0 0 9px var(--accent); }
#audio-bar { position: absolute; }
#hotkey-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 270px;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 12px;
  z-index: 40;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  display: flex; flex-direction: column; gap: 10px;
}
#hotkey-panel.hidden { display: none; }
#hotkey-panel .row { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
#hotkey-panel .row.modes { flex-direction: column; align-items: flex-start; gap: 6px; }
#hotkey-panel button {
  flex: 1; padding: 5px 10px; border-radius: 8px; font-size: 12px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.08);
  color: var(--text); cursor: pointer;
}
#hotkey-panel button:disabled { opacity: .4; cursor: default; }
#hotkey-panel label { display: flex; gap: 6px; align-items: center; color: var(--muted); cursor: pointer; }
#hotkey-panel p { margin: 0; color: var(--muted); line-height: 1.4; }

/* Ground shadow under her feet, so she stands on something. */
#pin-ground { display: none; }
html.overlay #pin-ground {
  display: block;
  position: fixed;
  left: 50%;
  bottom: 1.2%;
  width: 46%;
  height: 4.5%;
  transform: translateX(-50%);
  background: radial-gradient(ellipse at center,
              rgba(0, 0, 0, 0.42) 0%, rgba(0, 0, 0, 0.18) 45%, rgba(0, 0, 0, 0) 72%);
  pointer-events: none;
  z-index: 0;                         /* above the page, under her (canvases are 1) */
}

/* ---- account menu (auth.js) ---- */
#account { position: relative; }
#account .account-btn {
  display: flex; align-items: center; gap: 8px;
  padding: 3px 12px 3px 4px; border-radius: 999px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.07);
  color: var(--text); cursor: pointer; font-size: 12px;
}
#account .account-btn:hover { background: rgba(255, 255, 255, 0.14); }
#account .credits { color: var(--muted); white-space: nowrap; }
#account.low .credits { color: var(--accent); }
#account-menu {
  position: absolute; top: calc(100% + 8px); right: 0; width: 260px; z-index: 45;
  padding: 12px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--panel); backdrop-filter: blur(14px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  display: flex; flex-direction: column; gap: 8px; font-size: 12px;
}
#account-menu.hidden { display: none; }
#account-menu .row { display: flex; justify-content: space-between; gap: 10px; }
#account-menu .row span { color: var(--muted); }
#account-menu .row strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
#account-menu p { margin: 0; color: var(--muted); line-height: 1.4; }
#account-menu button {
  padding: 6px 10px; border-radius: 8px; font-size: 12px; cursor: pointer;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.08); color: var(--text);
}
#account-menu button:hover { background: rgba(255, 255, 255, 0.15); }
#account-menu button.danger { color: #ff8a8a; border-color: rgba(255, 90, 90, 0.35); }

/* ---- first-time screen-share notice (screen-share.js) ---- */
#share-notice {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(8, 6, 16, 0.55); backdrop-filter: blur(3px);
}
#share-notice .card {
  width: min(440px, 90vw); padding: 20px 22px; border-radius: 16px;
  background: #1f1a2e; border: 1px solid var(--border); color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); font-size: 13px; line-height: 1.5;
}
#share-notice h3 { margin: 0 0 10px; font-size: 16px; }
#share-notice p { margin: 0 0 10px; color: #d9d2ee; }
#share-notice label { display: flex; gap: 8px; align-items: center; color: var(--muted); margin: 6px 0 14px; }
#share-notice .actions { display: flex; justify-content: flex-end; gap: 8px; }
#share-notice button {
  padding: 7px 16px; border-radius: 999px; cursor: pointer; font-size: 13px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.08); color: var(--text);
}
#share-notice button.primary { background: var(--accent); color: #2a1020; border-color: transparent; font-weight: 600; }
