/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0d0d0f;
  --bg2: #141416;
  --bg3: #1c1c1f;
  --bg4: #242428;
  --border: rgba(255,255,255,0.08);
  --border2: rgba(255,255,255,0.14);
  --text: #f0f0f0;
  --text2: #9a9a9a;
  --text3: #5a5a5a;
  --accent: #7c6fef;
  --accent2: #5a4fd4;
  --accent-bg: rgba(124,111,239,0.12);
  --accent-border: rgba(124,111,239,0.35);
  --success: #3ecf8e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-xl: 18px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --transition: 0.18s ease;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.6;
  overflow-x: hidden;
}

a { color: inherit; text-decoration: none; }
button { cursor: pointer; font-family: var(--font); border: none; outline: none; }
input, select, textarea { font-family: var(--font); outline: none; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg4); border-radius: 4px; }

/* ── Layout ── */
#app { display: flex; flex-direction: column; min-height: 100vh; }

/* ── Navbar ── */
#navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-logo {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.nav-logo span { color: var(--accent); }

.nav-right { display: flex; align-items: center; gap: 10px; }

.nav-btn {
  padding: 7px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  transition: var(--transition);
  border: 1px solid var(--border2);
  background: transparent;
  color: var(--text2);
}
.nav-btn:hover { background: var(--bg3); color: var(--text); }
.nav-btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.nav-btn.primary:hover { background: var(--accent2); }

.nav-user {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  border-radius: var(--radius);
  background: var(--bg3);
  border: 1px solid var(--border);
  font-size: 13px;
  color: var(--text2);
}
.nav-avatar {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600; color: #fff;
}

/* ── Main Content ── */
#main { flex: 1; display: flex; }

/* ── Sidebar ── */
#sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 6px;
  overflow-y: auto;
  max-height: calc(100vh - 56px);
}

.sidebar-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  padding: 8px 8px 4px;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: var(--transition);
  color: var(--text2);
  font-size: 13px;
  border: 1px solid transparent;
}
.sidebar-item:hover { background: var(--bg3); color: var(--text); }
.sidebar-item.active { background: var(--accent-bg); color: var(--accent); border-color: var(--accent-border); }
.sidebar-item .si-icon { font-size: 15px; width: 20px; text-align: center; }
.sidebar-item .si-meta { font-size: 11px; color: var(--text3); margin-top: 1px; }
.sidebar-item .si-text { flex: 1; overflow: hidden; }
.sidebar-item .si-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-divider { height: 1px; background: var(--border); margin: 6px 0; }

.sidebar-new-btn {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius);
  background: var(--accent-bg);
  border: 1px dashed var(--accent-border);
  color: var(--accent);
  font-size: 13px;
  width: 100%;
  transition: var(--transition);
}
.sidebar-new-btn:hover { background: rgba(124,111,239,0.2); }

/* ── Chat Area ── */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Welcome Screen ── */
#welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
}

.welcome-title {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 10px;
}
.welcome-title span { color: var(--accent); }
.welcome-sub { color: var(--text2); font-size: 15px; margin-bottom: 40px; max-width: 480px; }

/* ── Character Selector ── */
#char-selector { width: 100%; max-width: 800px; }

.selector-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

#search-input {
  flex: 1;
  min-width: 180px;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
}
#search-input::placeholder { color: var(--text3); }
#search-input:focus { border-color: var(--accent-border); }

#char-select {
  flex: 1;
  min-width: 180px;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%239a9a9a' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
  cursor: pointer;
}
#char-select:focus { border-color: var(--accent-border); }

.filter-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.chip {
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 12px;
  border: 1px solid var(--border2);
  background: transparent;
  color: var(--text2);
  transition: var(--transition);
}
.chip:hover { background: var(--bg3); color: var(--text); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
  gap: 10px;
  max-height: 360px;
  overflow-y: auto;
  padding-right: 4px;
}

.char-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 12px;
  cursor: pointer;
  text-align: center;
  transition: var(--transition);
}
.char-card:hover { border-color: var(--accent-border); background: var(--bg4); transform: translateY(-1px); }
.char-card.selected { border-color: var(--accent); background: var(--accent-bg); }
.char-card .cc-icon { font-size: 28px; margin-bottom: 7px; display: block; }
.char-card .cc-name { font-size: 12px; font-weight: 500; color: var(--text); line-height: 1.3; }
.char-card .cc-universe { font-size: 11px; color: var(--text3); margin-top: 2px; }
.char-card .cc-tag {
  display: inline-block;
  margin-top: 6px;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
  background: var(--accent-bg);
  color: var(--accent);
}

.start-btn {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-lg);
  font-size: 14px;
  font-weight: 600;
  transition: var(--transition);
}
.start-btn:hover { background: var(--accent2); }
.start-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Chat Interface ── */
#chat-interface {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Chat Header */
#chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
#chat-header .ch-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}
#chat-header .ch-name { font-size: 14px; font-weight: 600; }
#chat-header .ch-universe { font-size: 12px; color: var(--text2); }
#chat-header .ch-actions { margin-left: auto; display: flex; gap: 8px; }

.icon-btn {
  width: 34px; height: 34px;
  border-radius: var(--radius);
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
  transition: var(--transition);
}
.icon-btn:hover { background: var(--bg4); color: var(--text); border-color: var(--border2); }

.guest-badge {
  padding: 4px 10px;
  background: rgba(245,158,11,0.12);
  border: 1px solid rgba(245,158,11,0.3);
  border-radius: 20px;
  font-size: 11px;
  color: var(--warning);
  font-weight: 500;
}

/* Messages */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg-wrap { display: flex; gap: 10px; max-width: 75%; }
.msg-wrap.user { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  background: var(--bg3);
  border: 1px solid var(--border);
  margin-top: 2px;
}
.msg-wrap.user .msg-avatar { background: var(--accent-bg); border-color: var(--accent-border); }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}
.msg-wrap.ai .msg-bubble {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 3px 14px 14px 14px;
}
.msg-wrap.user .msg-bubble {
  background: var(--accent);
  color: #fff;
  border-radius: 14px 3px 14px 14px;
}
.msg-time { font-size: 10px; color: var(--text3); margin-top: 4px; text-align: right; }
.msg-wrap.ai .msg-time { text-align: left; }

.typing-indicator {
  display: flex; gap: 4px; align-items: center; padding: 4px 0;
}
.typing-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text3);
  animation: bounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

/* Suggestions */
#suggestions {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 20px 12px;
}
.sug-btn {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text2);
  transition: var(--transition);
}
.sug-btn:hover { background: var(--bg4); color: var(--text); border-color: var(--accent-border); }

/* Input Row */
#input-area {
  padding: 12px 20px 16px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
}
#input-row {
  display: flex;
  gap: 8px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius-lg);
  padding: 6px 6px 6px 14px;
  transition: var(--transition);
}
#input-row:focus-within { border-color: var(--accent-border); }
#msg-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  resize: none;
  max-height: 120px;
  padding: 4px 0;
}
#msg-input::placeholder { color: var(--text3); }
#send-btn {
  width: 36px; height: 36px;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: var(--transition);
  align-self: flex-end;
}
#send-btn:hover { background: var(--accent2); }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.input-hint {
  font-size: 11px;
  color: var(--text3);
  margin-top: 6px;
  text-align: center;
}

/* ── Modal ── */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.modal {
  background: var(--bg2);
  border: 1px solid var(--border2);
  border-radius: var(--radius-xl);
  padding: 28px;
  width: 100%;
  max-width: 400px;
}
.modal-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 6px;
  letter-spacing: -0.3px;
}
.modal-sub { color: var(--text2); font-size: 13px; margin-bottom: 22px; }

.form-group { margin-bottom: 14px; }
.form-label { font-size: 12px; font-weight: 500; color: var(--text2); margin-bottom: 5px; display: block; }
.form-input {
  width: 100%;
  padding: 10px 13px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  transition: var(--transition);
}
.form-input:focus { border-color: var(--accent-border); }
.form-input::placeholder { color: var(--text3); }

.form-btn {
  width: 100%;
  padding: 11px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  transition: var(--transition);
  margin-top: 6px;
}
.form-btn:hover { background: var(--accent2); }
.form-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.modal-switch {
  text-align: center;
  font-size: 13px;
  color: var(--text2);
  margin-top: 14px;
}
.modal-switch a { color: var(--accent); cursor: pointer; }

.modal-error {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  color: #fca5a5;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 12px;
  margin-bottom: 12px;
}

.modal-close {
  float: right;
  background: transparent;
  border: none;
  color: var(--text3);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
  margin-top: -4px;
}
.modal-close:hover { color: var(--text); }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 13px;
  color: var(--text);
  z-index: 9999;
  transform: translateY(80px);
  opacity: 0;
  transition: all 0.3s ease;
  max-width: 300px;
}
#toast.show { transform: translateY(0); opacity: 1; }
#toast.success { border-color: rgba(62,207,142,0.4); color: var(--success); }
#toast.error { border-color: rgba(239,68,68,0.4); color: #fca5a5; }

/* ── History Panel ── */
.history-empty {
  text-align: center;
  color: var(--text3);
  font-size: 13px;
  padding: 20px 10px;
}

.del-btn {
  background: transparent;
  border: none;
  color: var(--text3);
  font-size: 13px;
  padding: 2px 5px;
  border-radius: 4px;
  margin-left: auto;
  flex-shrink: 0;
}
.del-btn:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

/* ── Responsive ── */
@media (max-width: 640px) {
  #sidebar { display: none; }
  .char-grid { grid-template-columns: repeat(auto-fill, minmax(110px,1fr)); }
  .msg-wrap { max-width: 90%; }
  .welcome-title { font-size: 24px; }
}

/* ── Random Button ── */
.random-btn {
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text2);
  font-size: 13px;
  transition: var(--transition);
  white-space: nowrap;
}
.random-btn:hover { background: var(--bg4); color: var(--text); border-color: var(--accent-border); }

/* ── Mood Section ── */
.mood-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 8px;
  text-align: left;
}
.mood-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.mood-chip {
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 12px;
  border: 1px solid var(--border2);
  background: transparent;
  color: var(--text2);
  transition: var(--transition);
}
.mood-chip:hover { background: var(--bg3); color: var(--text); }
.mood-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── Scenario Section ── */
.scenario-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.scenario-chip {
  padding: 6px 13px;
  border-radius: 20px;
  font-size: 12px;
  border: 1px solid var(--border2);
  background: transparent;
  color: var(--text2);
  transition: var(--transition);
  text-align: left;
}
.scenario-chip:hover { background: var(--bg3); color: var(--text); border-color: var(--accent-border); }
.scenario-chip.active { background: rgba(62,207,142,0.1); border-color: rgba(62,207,142,0.4); color: var(--success); }

/* ── Mood Badge (in chat header) ── */
.mood-badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: rgba(245,158,11,0.12);
  border: 1px solid rgba(245,158,11,0.3);
  color: var(--warning);
  font-weight: 500;
  margin-left: 6px;
  vertical-align: middle;
}

/* ── Battle Mode ── */
#battle-setup {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  text-align: center;
  overflow-y: auto;
}
.battle-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.4px;
  margin-bottom: 8px;
}
.battle-title span { color: #ef4444; }
.battle-sub { color: var(--text2); font-size: 14px; margin-bottom: 28px; }

.battle-pickers {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  width: 100%;
  max-width: 760px;
  margin-bottom: 20px;
}
.battle-picker {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  min-width: 0;
}
.bp-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text3);
  margin-bottom: 8px;
}
.bp-placeholder {
  font-size: 13px;
  color: var(--text2);
  padding: 8px 0;
  margin-bottom: 10px;
  min-height: 28px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.bp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 6px;
  max-height: 260px;
  overflow-y: auto;
}
.bp-card {
  background: var(--bg4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 6px;
  cursor: pointer;
  text-align: center;
  font-size: 11px;
  color: var(--text2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: var(--transition);
}
.bp-card:hover { border-color: var(--accent-border); color: var(--text); }
.bp-card.selected { border-color: #ef4444; background: rgba(239,68,68,0.1); color: #ef4444; }
.bp-card span:first-child { font-size: 18px; }

.vs-badge {
  font-size: 22px;
  font-weight: 800;
  color: var(--text3);
  padding-top: 60px;
  flex-shrink: 0;
}

.battle-topic-row {
  width: 100%;
  max-width: 760px;
  margin-bottom: 14px;
}
.battle-topic-row input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
}
.battle-topic-row input:focus { border-color: rgba(239,68,68,0.5); outline: none; }
.battle-topic-row input::placeholder { color: var(--text3); }

.battle-speak-btn {
  padding: 8px 16px;
  border-radius: var(--radius);
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text2);
  font-size: 13px;
  transition: var(--transition);
}
.battle-speak-btn:hover { background: var(--bg4); color: var(--text); border-color: var(--accent-border); }
.battle-speak-btn:disabled { opacity: 0.4; cursor: not-allowed; }

@media (max-width: 640px) {
  .battle-pickers { flex-direction: column; }
  .vs-badge { padding-top: 0; }
}