:root {
  color-scheme: dark;
  --bg: #0f0d14;
  --chat-bg: #14111c;
  --panel: #1c1726;
  --accent: #e879a8;
  --accent-soft: #f5a8c8;
  --text: #f4eef8;
  --muted: #9b8faa;
  --bubble-user: #5c3d6e;
  --bubble-koharu: #2a1f32;
  --bubble-border: #3d2f52;
  --composer-bg: #121018;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.install-banner {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 14px;
  background: linear-gradient(135deg, #2a1f32, #1c1726);
  border-bottom: 1px solid #3d2f52;
}

.install-banner.hidden {
  display: none;
}

.install-text {
  margin: 0;
  font-size: 0.82rem;
  line-height: 1.4;
  color: var(--text);
}

.install-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.install-primary {
  flex: 1;
  padding: 10px 12px;
  font-size: 0.85rem;
}

.install-primary.hidden {
  display: none;
}

#app {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  max-width: 520px;
  width: 100%;
  margin: 0 auto;
  background: var(--chat-bg);
}

header {
  padding: 10px 14px;
  border-bottom: 1px solid #2a2238;
  background: var(--composer-bg);
}

.header-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent);
  flex-shrink: 0;
}

.header-info {
  flex: 1;
  min-width: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.header-actions.hidden {
  display: none;
}

header h1 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--text);
  font-weight: 600;
}

.header-status {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--accent-soft);
}

.usage {
  margin: 6px 0 0 52px;
  font-size: 0.72rem;
  color: var(--muted);
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.hidden { display: none !important; }

#login-screen {
  justify-content: center;
  padding: 24px 16px;
  gap: 12px;
  background: var(--bg);
}

.login-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.login-avatar {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--accent);
  box-shadow: 0 8px 24px rgba(232, 121, 168, 0.25);
}

.login-hero h2 {
  margin: 0;
  font-size: 1.35rem;
  color: var(--accent);
}

.auth-tabs {
  display: flex;
  gap: 8px;
}

.auth-tab {
  flex: 1;
  background: var(--panel);
  color: var(--muted);
  border: 1px solid #3a2f52;
}

.auth-tab.active {
  background: var(--accent);
  color: #1a0812;
  border-color: var(--accent);
}

.hint, .fine-print {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
  text-align: center;
}

.fine-print { font-size: 0.75rem; }

input, button {
  font: inherit;
}

input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid #3a2f52;
  background: var(--panel);
  color: var(--text);
  font-size: 16px;
}

button {
  padding: 14px 16px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #1a0812;
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

button.ghost {
  background: transparent;
  color: var(--muted);
  padding: 6px 10px;
  font-size: 0.85rem;
}

.error {
  color: #ff8da1;
  min-height: 1.2em;
  font-size: 0.85rem;
  text-align: center;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(232, 121, 168, 0.06), transparent 55%),
    radial-gradient(ellipse at 80% 100%, rgba(92, 61, 110, 0.08), transparent 50%),
    var(--chat-bg);
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.koharu {
  justify-content: flex-start;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  border: 1.5px solid rgba(232, 121, 168, 0.5);
  align-self: flex-end;
}

.msg-avatar.spacer {
  visibility: hidden;
}

.bubble {
  max-width: calc(100% - 44px);
  padding: 10px 14px;
  line-height: 1.45;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble.user {
  background: var(--bubble-user);
  color: #fff;
  border-radius: 18px 18px 4px 18px;
}

.bubble.koharu {
  background: var(--bubble-koharu);
  border: 1px solid var(--bubble-border);
  border-radius: 18px 18px 18px 4px;
}

.bubble.typing {
  color: var(--muted);
  font-style: italic;
  min-width: 3.5em;
}

.bubble.image {
  padding: 4px;
  background: var(--bubble-koharu);
  border: 1px solid var(--bubble-border);
  border-radius: 18px 18px 18px 4px;
}

.bubble.image img {
  max-width: min(240px, 72vw);
  border-radius: 14px;
  display: block;
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  margin-left: 4px;
  vertical-align: middle;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-soft);
  animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.chat-footer {
  border-top: 1px solid #2a2238;
  background: var(--composer-bg);
  padding-bottom: env(safe-area-inset-bottom);
}

.footer-menu-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: calc(100% - 24px);
  margin: 8px 12px 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid #3a2f52;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
}

.footer-menu-btn.hidden {
  display: none;
}

.footer-menu-chevron {
  font-size: 0.7rem;
  transition: transform 0.2s ease;
}

.chat-footer.menu-open .footer-menu-chevron {
  transform: rotate(180deg);
}

.footer-menu {
  padding: 0 0 4px;
}

.footer-menu.hidden {
  display: none;
}

.privacy-panel {
  margin: 8px 12px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid #3a2f52;
}

.privacy-panel.opted-out {
  border-color: rgba(145, 220, 160, 0.45);
  background: rgba(30, 48, 36, 0.55);
}

.privacy-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.privacy-title {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.privacy-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.privacy-toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.privacy-switch {
  flex-shrink: 0;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: #4a3558;
  border: 1px solid #5c4570;
  position: relative;
  transition: background 0.2s ease;
}

.privacy-switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #f4eef8;
  transition: transform 0.2s ease;
}

.privacy-toggle input:checked + .privacy-switch {
  background: var(--accent);
  border-color: var(--accent);
}

.privacy-toggle input:checked + .privacy-switch::after {
  transform: translateX(18px);
}

.privacy-label {
  font-size: 0.82rem;
  color: var(--text);
  line-height: 1.35;
}

.privacy-hint {
  margin: 8px 0 0;
  font-size: 0.72rem;
  line-height: 1.4;
  color: var(--muted);
}

.privacy-panel.opted-out .privacy-hint {
  color: #9fd4a8;
}

.privacy-link {
  flex-shrink: 0;
  font-size: 0.74rem;
  color: var(--accent-soft);
  text-decoration: none;
}

.legal-lead {
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(232, 121, 168, 0.1);
  border: 1px solid rgba(232, 121, 168, 0.25);
}

.presets-wrap {
  padding: 4px 12px 0;
}

.presets-label {
  margin: 0 0 6px;
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.presets-label.offline {
  color: #ff8da1;
}

.presets {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}

.presets::-webkit-scrollbar {
  display: none;
}

.preset-btn {
  flex: 0 0 auto;
  padding: 7px 12px;
  font-size: 0.78rem;
  background: var(--panel);
  color: var(--text);
  border: 1px solid #3a2f52;
  border-radius: 999px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px 12px;
}

.composer input {
  flex: 1;
  border-radius: 22px;
  padding: 12px 16px;
  background: var(--panel);
}

.composer button {
  flex: 0 0 auto;
  min-width: 64px;
  border-radius: 22px;
  padding: 12px 16px;
}

body.legal-page {
  overflow: auto;
  display: block;
}

.legal {
  max-width: 520px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  line-height: 1.55;
}

.legal h1 {
  margin: 0 0 6px;
  font-size: 1.5rem;
  color: var(--accent);
}

.legal h2 {
  margin: 24px 0 8px;
  font-size: 1.05rem;
  color: var(--text);
}

.legal p,
.legal li {
  font-size: 0.92rem;
  color: var(--text);
}

.legal ul {
  margin: 8px 0;
  padding-left: 1.2rem;
}

.legal li {
  margin-bottom: 6px;
}

.legal a {
  color: var(--accent-soft);
}

.legal-back {
  margin: 0 0 16px;
  font-size: 0.9rem;
}

.legal-updated {
  margin: 0 0 16px;
  font-size: 0.8rem;
  color: var(--muted);
}

.legal-footer {
  margin-top: 32px;
  font-size: 0.9rem;
}

.fine-print a {
  color: var(--accent-soft);
}

.privacy-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 6, 12, 0.92);
  backdrop-filter: blur(6px);
}

.privacy-gate.hidden {
  display: none;
}

.privacy-gate-card {
  width: 100%;
  max-width: 420px;
  max-height: min(90dvh, 640px);
  display: flex;
  flex-direction: column;
  padding: 16px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid #3d2f52;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.privacy-gate-card h2 {
  margin: 0 0 6px;
  font-size: 1.2rem;
  color: var(--accent);
}

.privacy-gate-intro {
  margin: 0 0 10px;
  font-size: 0.82rem;
  color: var(--muted);
}

.privacy-gate-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  border-radius: 12px;
  background: var(--chat-bg);
  border: 1px solid #2a2238;
  font-size: 0.84rem;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}

.privacy-gate-body h3 {
  margin: 14px 0 6px;
  font-size: 0.9rem;
  color: var(--text);
}

.privacy-gate-body h3:first-of-type {
  margin-top: 0;
}

.privacy-gate-body ul {
  margin: 6px 0;
  padding-left: 1.1rem;
}

.privacy-gate-body li {
  margin-bottom: 4px;
}

.privacy-gate-scroll-hint {
  margin: 10px 0 8px;
  font-size: 0.78rem;
  color: var(--accent-soft);
  text-align: center;
}

.privacy-gate-scroll-hint.ready {
  color: #9fd4a8;
}

#privacy-accept-btn {
  width: 100%;
  border-radius: 12px;
}

#privacy-accept-btn:disabled {
  opacity: 0.4;
}

.privacy-gate-full {
  display: block;
  margin-top: 10px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--accent-soft);
}

body.privacy-gate-open {
  overflow: hidden;
}