* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --pink: #F48FB1;
  --pink-deep: #E91E8C;
  --pink-light: #FCE4EC;
  --pink-pale: #FFF0F5;
  --pink-border: #F8BBD0;
  --text: #3D2030;
  --text-soft: #9E7080;
  --text-muted: #C9A8B8;
  --white: #FFFFFF;
  --bg: #FFFBFD;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

html, body { height: 100%; overflow: hidden; background: var(--bg); font-family: -apple-system, 'PingFang SC', sans-serif; color: var(--text); }

/* ── APP LAYOUT ── */
.app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  padding-top: var(--safe-top);
}

/* ── HEADER ── */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: var(--white);
  border-bottom: 1px solid var(--pink-border);
  flex-shrink: 0;
}
.header-title { font-size: 16px; font-weight: 600; color: var(--text); letter-spacing: 0.3px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: none; background: transparent; color: var(--text-soft);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: background 0.15s;
}
.icon-btn:active { background: var(--pink-light); }

/* ── MESSAGES ── */
.messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 20px 16px 8px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}

/* welcome screen */
.welcome {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 8px; padding: 60px 0;
}
.welcome-emoji { font-size: 48px; }
.welcome-title { font-size: 20px; font-weight: 600; color: var(--text); }
.welcome-sub { font-size: 14px; color: var(--text-muted); }

/* message rows */
.message { display: flex; max-width: 100%; }
.message.user { justify-content: flex-end; }
.message.ai { justify-content: flex-start; }

.bubble {
  max-width: 78%; padding: 10px 14px;
  border-radius: 18px; font-size: 15px; line-height: 1.55;
  word-break: break-word; white-space: pre-wrap;
}
.user-bubble {
  background: var(--pink);
  color: var(--white);
  border-bottom-right-radius: 4px;
}
.ai-bubble {
  background: var(--white);
  color: var(--text);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--pink-border);
}

/* markdown inside ai bubble */
.ai-bubble strong { font-weight: 600; }
.ai-bubble em { font-style: italic; }
.ai-bubble h1, .ai-bubble h2, .ai-bubble h3 { font-weight: 600; margin: 4px 0 2px; }
.ai-bubble h1 { font-size: 18px; }
.ai-bubble h2 { font-size: 16px; }
.ai-bubble h3 { font-size: 15px; }
.ai-bubble code {
  background: var(--pink-pale); color: #C2185B;
  padding: 1px 5px; border-radius: 4px; font-size: 13px; font-family: monospace;
}
.ai-bubble pre {
  background: var(--pink-pale); border-radius: 10px;
  padding: 10px 12px; overflow-x: auto; margin: 4px 0;
}
.ai-bubble pre code { background: none; padding: 0; color: var(--text); }

/* speak button */
.bubble-actions {
  display: flex; justify-content: flex-end;
  margin-top: 6px; padding-top: 4px;
  border-top: 1px solid rgba(244,139,177,0.18);
}
.speak-btn {
  background: none; border: none; cursor: pointer;
  font-size: 14px; opacity: 0.35; padding: 0 2px;
  line-height: 1; transition: opacity 0.2s; user-select: none;
}
.speak-btn:hover { opacity: 0.75; }
.speak-btn.speaking { opacity: 1; }

/* typing dots */
.typing { display: flex; gap: 5px; align-items: center; padding: 4px 0; }
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--pink); opacity: 0.6;
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-6px); } }

/* ── VOICE INDICATOR ── */
.voice-indicator {
  display: none; align-items: center; justify-content: center; gap: 12px;
  padding: 10px 16px;
  background: var(--pink-pale);
  border-top: 1px solid var(--pink-border);
  flex-shrink: 0;
}
.voice-indicator.active { display: flex; }
.wave-bars { display: flex; gap: 4px; align-items: center; }
.bar {
  width: 4px; background: var(--pink);
  border-radius: 2px; animation: wave 0.8s infinite ease-in-out;
}
.bar:nth-child(1) { height: 8px;  animation-delay: 0s; }
.bar:nth-child(2) { height: 16px; animation-delay: 0.1s; }
.bar:nth-child(3) { height: 24px; animation-delay: 0.2s; }
.bar:nth-child(4) { height: 16px; animation-delay: 0.3s; }
.bar:nth-child(5) { height: 8px;  animation-delay: 0.4s; }
@keyframes wave { 0%, 100% { transform: scaleY(0.5); } 50% { transform: scaleY(1); } }
.voice-hint { font-size: 13px; color: var(--pink-deep); font-weight: 500; }

/* ── INPUT AREA ── */
.input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--white);
  border-top: 1px solid var(--pink-border);
  flex-shrink: 0;
}
.input-wrap { flex: 1; }
.input-field {
  width: 100%; resize: none; border: none; outline: none;
  background: var(--pink-pale);
  border-radius: 20px; padding: 9px 14px;
  font-size: 15px; color: var(--text);
  font-family: inherit; line-height: 1.4;
  max-height: 120px; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.input-field::placeholder { color: var(--text-muted); }

.voice-btn, .send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: transform 0.1s, background 0.15s;
}
.voice-btn { background: var(--pink-pale); color: var(--pink-deep); }
.voice-btn:active, .voice-btn.recording { background: var(--pink); color: var(--white); transform: scale(1.1); }
.send-btn { background: var(--pink); color: var(--white); }
.send-btn:active { transform: scale(0.93); }
.send-btn:disabled { background: var(--pink-border); }

/* ── SIDEBAR ── */
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(61,32,48,0.35); z-index: 40;
  backdrop-filter: blur(2px);
}
.sidebar-overlay.active { display: block; }

.sidebar {
  position: fixed; left: 0; top: 0; bottom: 0;
  width: 72vw; max-width: 280px;
  background: var(--pink-pale);
  border-right: 1px solid var(--pink-border);
  z-index: 50; display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(.4,0,.2,1);
  padding-top: var(--safe-top);
}
.sidebar.open { transform: translateX(0); }

.sidebar-header { padding: 20px 16px 12px; }
.sidebar-logo { font-size: 17px; font-weight: 700; color: var(--text); }

.sidebar-new { padding: 0 12px 12px; }
.new-chat-btn {
  width: 100%; padding: 10px;
  background: var(--pink); color: var(--white);
  border: none; border-radius: 12px;
  font-size: 14px; font-weight: 600; cursor: pointer;
  letter-spacing: 0.3px;
  transition: background 0.15s;
}
.new-chat-btn:active { background: var(--pink-deep); }

.session-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.session-item {
  display: flex; align-items: center;
  padding: 11px 10px; border-radius: 10px;
  cursor: pointer; margin-bottom: 2px;
  transition: background 0.12s;
}
.session-item:hover, .session-item.active { background: var(--pink-light); }
.session-name { flex: 1; font-size: 14px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-delete {
  opacity: 0; width: 24px; height: 24px;
  border: none; background: none; color: var(--text-muted);
  font-size: 12px; cursor: pointer; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: opacity 0.15s, background 0.12s;
}
.session-item:hover .session-delete { opacity: 1; }
.session-delete:hover { background: var(--pink-border); color: var(--text); }
.no-sessions { padding: 20px 10px; text-align: center; font-size: 13px; color: var(--text-muted); }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--pink-border); }
.settings-link {
  width: 100%; padding: 9px; background: none;
  border: 1px solid var(--pink-border); border-radius: 10px;
  color: var(--text-soft); font-size: 13px; cursor: pointer;
  transition: background 0.12s;
}
.settings-link:active { background: var(--pink-light); }

/* ── MODAL ── */
.modal-overlay {
  display: none; position: fixed; inset: 0; z-index: 60;
  background: rgba(61,32,48,0.4); backdrop-filter: blur(4px);
  align-items: flex-end; justify-content: center;
  padding-bottom: var(--safe-bottom);
}
.modal-overlay.active { display: flex; }

.modal {
  background: var(--white); border-radius: 24px 24px 0 0;
  width: 100%; max-height: 88vh; overflow-y: auto;
  animation: slideUp 0.3s cubic-bezier(.4,0,.2,1);
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 20px 0; font-size: 17px; font-weight: 700; color: var(--text);
}
.modal-close {
  width: 28px; height: 28px; border-radius: 50%;
  border: none; background: var(--pink-pale); color: var(--text-soft);
  font-size: 13px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-label { font-size: 14px; font-weight: 600; color: var(--text); }
.form-input {
  width: 100%; padding: 11px 14px;
  background: var(--pink-pale); border: 1.5px solid var(--pink-border);
  border-radius: 12px; font-size: 15px; color: var(--text);
  font-family: inherit; outline: none; resize: vertical;
  transition: border-color 0.15s;
}
.form-input:focus { border-color: var(--pink); }
.form-hint { font-size: 12px; color: var(--text-muted); }

.modal-footer { padding: 0 20px 24px; }
.btn-primary {
  width: 100%; padding: 14px;
  background: var(--pink); color: var(--white);
  border: none; border-radius: 14px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:active { background: var(--pink-deep); }

/* ── TOAST ── */
.toast {
  position: fixed; bottom: 100px; left: 50%; transform: translateX(-50%) translateY(10px);
  background: rgba(61,32,48,0.85); color: var(--white);
  padding: 9px 18px; border-radius: 20px; font-size: 14px;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; z-index: 100;
  white-space: nowrap; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
