* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #1a1018;
  --bg2: #221a22;
  --bg3: #2e2230;
  --border: #3d2e40;
  --text: #f0e4ec;
  --text2: #a890a8;
  --primary: #d4608a;
  --primary-hover: #e87da5;
  --danger: #e74c6f;
  --success: #65c9a6;
  --radius: 14px;
  --accent-glow: rgba(212, 96, 138, 0.15);
}

body {
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Hiragino Sans GB', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.page { height: 100vh; height: 100dvh; }
.hidden { display: none !important; }

/* Auth Page */
.auth-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 60px 20px;
  text-align: center;
}
.auth-container h1 { font-size: 2.2rem; margin-bottom: 8px; letter-spacing: 2px; }
.subtitle { color: var(--text2); margin-bottom: 32px; font-size: 15px; letter-spacing: 1px; }
.auth-tabs { display: flex; gap: 0; margin-bottom: 24px; }
.tab {
  flex: 1; padding: 12px;
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--text2); cursor: pointer; font-size: 15px;
  transition: all .2s;
}
.tab:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.tab:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form input {
  padding: 14px 16px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text); font-size: 15px; outline: none;
  transition: border-color .2s;
}
.auth-form input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--accent-glow); }
.error-msg { color: var(--danger); font-size: 14px; min-height: 20px; }
.success-msg { color: var(--success); font-size: 14px; min-height: 20px; }

/* Buttons */
.btn-primary {
  padding: 14px; background: linear-gradient(135deg, var(--primary), #c25080);
  color: #fff; border: none;
  border-radius: var(--radius); font-size: 16px; cursor: pointer;
  transition: all .25s; letter-spacing: 1px;
}
.btn-primary:hover { background: linear-gradient(135deg, var(--primary-hover), #d4608a); box-shadow: 0 4px 16px var(--accent-glow); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-icon {
  width: 36px; height: 36px; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 10px; color: var(--text); cursor: pointer; font-size: 18px;
  display: flex; align-items: center; justify-content: center; transition: all .2s;
}
.btn-icon:hover { background: var(--primary); border-color: var(--primary); }
.btn-sm {
  padding: 6px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg3); color: var(--text); cursor: pointer; font-size: 13px;
}
.btn-sm:hover { background: var(--border); }
.btn-danger { color: var(--danger); border-color: var(--danger); }

/* Chat Layout */
.chat-layout { display: flex; height: 100dvh; }

/* Sidebar */
.sidebar {
  width: 280px; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.sidebar-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; border-bottom: 1px solid var(--border);
}
.sidebar-header h2 { font-size: 16px; letter-spacing: 1px; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  padding: 12px 14px; border-radius: 10px; cursor: pointer;
  font-size: 14px; color: var(--text2); transition: all .2s;
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 2px;
}
.conv-item:hover { background: var(--bg3); color: var(--text); }
.conv-item.active { background: var(--primary); color: #fff; }
.conv-item .del-btn {
  opacity: 0; background: none; border: none; color: inherit; cursor: pointer; font-size: 16px;
  padding: 0 4px;
}
.conv-item:hover .del-btn { opacity: 0.6; }
.conv-item .del-btn:hover { opacity: 1; }
.sidebar-footer {
  padding: 12px 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-size: 13px; color: var(--text2);
}
.sidebar-footer > div { display: flex; gap: 6px; }

/* Chat Main */
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }

#chat-area { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.welcome {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 14px; color: var(--text2);
}
.welcome-icon { font-size: 3.5rem; margin-bottom: 4px; }
.welcome h2 { font-size: 1.6rem; color: var(--text); letter-spacing: 2px; }
.welcome p { font-size: 14px; letter-spacing: 1px; }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 20px; min-height: 0; }
.msg {
  max-width: 800px; margin: 0 auto 20px; padding: 16px 20px;
  border-radius: var(--radius); line-height: 1.8; font-size: 15px;
  word-break: break-word;
}
.msg-user-wrap {
  max-width: 70%; margin-left: auto; margin-right: 0;
  display: flex; flex-direction: column; align-items: flex-end;
  margin-bottom: 20px;
}
.msg-user-wrap.editing {
  max-width: 100%; align-items: stretch;
}
.msg-user-wrap .msg {
  margin-bottom: 0; margin-left: 0; margin-right: 0; max-width: 100%;
}
.msg-user {
  background: linear-gradient(135deg, var(--primary), #b8487a);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-assistant {
  background: var(--bg3); border: 1px solid var(--border);
  border-bottom-left-radius: 4px; margin-bottom: 20px;
}
.msg-label {
  font-size: 12px; color: var(--text2); margin-bottom: 6px;
  font-weight: 500;
}
.msg-user .msg-label { color: rgba(255,255,255,0.7); }

/* Scroll-to-bottom button */
.scroll-bottom-btn {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: #fff; border: none;
  font-size: 18px; line-height: 1; cursor: pointer;
  box-shadow: 0 2px 12px rgba(212, 96, 138, 0.35);
  display: flex; align-items: center; justify-content: center;
  opacity: 0.92; transition: opacity .2s, background .2s;
  z-index: 10;
}
.scroll-bottom-btn:hover { background: var(--primary-hover); opacity: 1; }
.scroll-bottom-btn.hidden { display: none !important; }

/* Input Area */
.input-area {
  padding: 16px 20px; border-top: 1px solid var(--border);
  display: flex; gap: 12px; align-items: flex-end;
  background: var(--bg);
}
.input-area textarea {
  flex: 1; padding: 14px 16px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text); font-size: 15px;
  resize: none; outline: none; font-family: inherit; line-height: 1.5;
}
.input-area textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--accent-glow); }
.btn-send {
  width: 48px; height: 48px; background: linear-gradient(135deg, var(--primary), #b8487a); border: none;
  border-radius: var(--radius); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all .25s; flex-shrink: 0;
}
.btn-send:hover { background: linear-gradient(135deg, var(--primary-hover), #d4608a); box-shadow: 0 3px 12px var(--accent-glow); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* Modal */
.modal {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-content {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 16px; width: 90%; max-width: 560px;
  max-height: 90vh; overflow-y: auto; padding: 24px;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-header h2 { font-size: 18px; }
.admin-form { display: flex; flex-direction: column; gap: 18px; }
.admin-form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--text2); }
.admin-form input, .admin-form textarea {
  padding: 12px 14px; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px; outline: none;
  font-family: inherit;
}
.admin-form input:focus, .admin-form textarea:focus { border-color: var(--primary); }
.admin-form small { color: var(--text2); font-size: 12px; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* Typing indicator */
.typing-dot { display: inline-block; animation: blink 1.4s infinite; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,20%{ opacity: .2; } 50%{ opacity: 1; } }

/* Message body */
.msg-body { white-space: pre-wrap; word-break: break-word; }

/* Edit button row — shown on wrap hover */
.msg-actions {
  display: flex; gap: 6px; margin-top: 5px;
  opacity: 0; transition: opacity .18s;
}
.msg-user-wrap:hover .msg-actions { opacity: 1; }
.edit-btn {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 10px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.08); color: rgba(255,255,255,0.8);
  font-size: 12px; cursor: pointer; transition: all .15s;
}
.edit-btn:hover { background: rgba(212,96,138,0.3); color: #fff; border-color: var(--primary); }
.edit-btn.hidden { display: none !important; }

/* Inline edit area (inside bubble) */
.edit-textarea {
  width: 100%; margin-top: 8px; padding: 14px 16px;
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.35);
  border-radius: 10px; color: #fff; font-size: 15px; font-family: inherit;
  resize: vertical; outline: none; line-height: 1.8; min-height: 120px;
}
.edit-textarea:focus { border-color: rgba(255,255,255,0.65); }

/* Confirm / cancel row */
.edit-actions { display: flex; gap: 8px; margin-top: 8px; justify-content: flex-end; }
.btn-confirm { background: rgba(255,255,255,0.2) !important; color: #fff !important; border-color: rgba(255,255,255,0.4) !important; font-weight: 600; }
.btn-confirm:hover { background: rgba(255,255,255,0.32) !important; }

/* Interaction choice buttons */
.choice-list {
  display: flex; flex-direction: column; gap: 8px;
  margin-top: 14px; padding-top: 12px;
  border-top: 1px solid var(--border);
}
.choice-btn {
  text-align: left; padding: 10px 14px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px;
  cursor: pointer; transition: all .18s; line-height: 1.5;
  font-family: inherit;
}
.choice-btn:hover {
  background: var(--primary); border-color: var(--primary); color: #fff;
}

/* Prologue message */
.msg-prologue {
  background: linear-gradient(135deg, #2a1828, #1e1428);
  border: 1px solid #5a3858;
  border-left: 3px solid var(--primary);
  margin-bottom: 20px; border-radius: var(--radius);
  padding: 16px 20px; font-size: 15px; line-height: 1.8;
  word-break: break-word;
}
.msg-label-prologue {
  font-size: 12px; color: #c490b0; margin-bottom: 8px; font-weight: 600;
  letter-spacing: 0.5px;
}
.msg-prologue .choice-list { border-top-color: #5a3858; }
.msg-prologue .choice-btn {
  background: rgba(212, 96, 138, 0.12); border-color: #5a3858; color: #e8b0cc;
}
.msg-prologue .choice-btn:hover {
  background: var(--primary); border-color: var(--primary); color: #fff;
}

/* Toast notification */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--danger); color: #fff; padding: 12px 20px;
  border-radius: 10px; font-size: 14px; z-index: 500;
  opacity: 0; transition: opacity .3s, transform .3s;
  white-space: nowrap; max-width: 88vw; pointer-events: none;
  text-overflow: ellipsis; overflow: hidden;
  box-shadow: 0 4px 16px rgba(231, 76, 111, 0.3);
}
.toast.toast-show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Mobile top bar — hidden on desktop */
.mobile-topbar {
  display: none;
  align-items: center; justify-content: space-between;
  padding: 10px 14px; background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0; gap: 10px;
}
.mobile-title {
  flex: 1; font-size: 15px; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  text-align: center;
}

/* Sidebar overlay mask */
.sidebar-mask {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.5); z-index: 49;
}
.sidebar-mask.visible { display: block; }

/* Responsive */
@media (max-width: 768px) {
  /* Show top bar, hide sidebar by default */
  .mobile-topbar { display: flex; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    width: 280px; z-index: 50;
    transform: translateX(-100%);
    transition: transform .25s ease;
  }
  .sidebar.open { transform: translateX(0); }

  /* Chat layout stacks vertically under topbar */
  .chat-layout { flex-direction: column; }
  .chat-main { flex: 1; min-height: 0; }

  /* Messages: tighter spacing on mobile */
  .messages { padding: 12px; }
  .msg { padding: 12px 14px; font-size: 14px; margin-bottom: 12px; }
  .msg-user-wrap { max-width: 88%; }

  /* Input area: let textarea auto-grow, reduce padding */
  .input-area { padding: 10px 12px; gap: 8px; }
  .input-area textarea {
    font-size: 16px; /* prevent iOS zoom */
    padding: 10px 12px; rows: 1;
    max-height: 120px; overflow-y: auto;
  }
  .btn-send { width: 42px; height: 42px; flex-shrink: 0; }

  /* Edit button: always visible on mobile (no hover) */
  .msg-actions { opacity: 1; }

  /* Auth page: tighter on small screens */
  .auth-container { padding: 40px 20px; }
  .auth-container h1 { font-size: 1.6rem; }

  /* Modal: full width */
  .modal-content { width: 96%; padding: 18px; max-height: 85vh; }

  /* Welcome screen */
  .welcome h2 { font-size: 1.4rem; }
}
