:root {
  --bg: #f3f5fb;
  --bg-glow-a: rgba(44, 110, 255, 0.22);
  --bg-glow-b: rgba(17, 185, 129, 0.16);
  --surface: #ffffff;
  --surface-soft: #f7f9ff;
  --surface-elevated: #f2f6ff;
  --text: #131a2b;
  --muted: #5d6782;
  --line: #dbe2f4;
  --primary: #3e63dd;
  --primary-strong: #2b4ec9;
  --chip: #eef2ff;
  --shadow: 0 18px 46px rgba(19, 26, 43, 0.12);
  --focus: rgba(62, 99, 221, 0.28);
}

html[data-theme='dark'] {
  --bg: #0e1320;
  --bg-glow-a: rgba(59, 130, 246, 0.2);
  --bg-glow-b: rgba(20, 184, 166, 0.16);
  --surface: #151c2e;
  --surface-soft: #1b243a;
  --surface-elevated: #202c45;
  --text: #f3f7ff;
  --muted: #a7b4d4;
  --line: #2b3858;
  --primary: #80a7ff;
  --primary-strong: #9bbbff;
  --chip: #223053;
  --shadow: 0 22px 52px rgba(0, 0, 0, 0.35);
  --focus: rgba(128, 167, 255, 0.32);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Plus Jakarta Sans", "Manrope", system-ui, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 700px at -8% -10%, var(--bg-glow-a), transparent 62%),
    radial-gradient(900px 560px at 100% 0, var(--bg-glow-b), transparent 58%),
    var(--bg);
}

a { color: inherit; }

.app-page { min-height: 100vh; padding: 1rem; }

.app-shell {
  width: min(1380px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: 1rem;
}

.compact-shell { grid-template-columns: minmax(0, 1fr); max-width: 980px; }

.app-sidebar {
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 1rem;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 0.9rem;
  min-height: calc(100vh - 2rem);
  position: sticky;
  top: 1rem;
}

.brand { display: grid; gap: 0.2rem; }

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: linear-gradient(150deg, #3e63dd, #06b6d4);
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.03em;
}

.brand h1, .brand p { margin: 0; }
.brand h1 { font-size: 1rem; }
.brand p { font-size: 0.84rem; color: var(--muted); }

.sidebar-nav { display: grid; gap: 0.32rem; }

.sidebar-link {
  text-decoration: none;
  padding: 0.56rem 0.64rem;
  border-radius: 12px;
  border: 1px solid transparent;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 600;
}

.sidebar-link:hover { border-color: var(--line); color: var(--text); background: var(--surface-soft); }
.sidebar-link[aria-current='page'] {
  background: var(--chip);
  border-color: color-mix(in srgb, var(--primary) 35%, var(--line));
  color: var(--primary-strong);
}

.sidebar-note {
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.74rem;
  font-size: 0.84rem;
  color: var(--muted);
  line-height: 1.45;
}

.app-main { display: grid; gap: 1rem; }

.app-header {
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 0.95rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-copy h2, .header-copy p { margin: 0; }
.header-copy h2 { font-size: 1.3rem; }
.header-copy p { margin-top: 0.32rem; color: var(--muted); font-size: 0.92rem; }

#theme-toggle {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--text);
  border-radius: 999px;
  padding: 0.48rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
}

.home-grid {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 320px;
  gap: 1rem;
}

.surface {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: var(--shadow);
  padding: 0.95rem;
}

.surface h3, .surface h4, .surface p { margin-top: 0; }

.character-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 0.72rem;
}

.character-panel h3 { margin: 0; font-size: 1rem; }
.body-text { margin: 0; color: var(--muted); font-size: 0.9rem; }

.character-search,
#chat-input,
.contact-input,
.contact-textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-soft);
  color: var(--text);
  font: inherit;
  padding: 0.62rem 0.72rem;
}

.character-search:focus,
#chat-input:focus,
.contact-input:focus,
.contact-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus);
}

.character-list {
  display: grid;
  gap: 0.45rem;
  max-height: 560px;
  overflow: auto;
  padding-right: 0.12rem;
}

.character-item {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--text);
  border-radius: 12px;
  padding: 0.58rem 0.62rem;
  cursor: pointer;
}
.character-item:hover { border-color: color-mix(in srgb, var(--primary) 55%, var(--line)); }
.character-item.is-active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 11%, var(--surface-soft));
}

.character-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 0.55rem;
  align-items: center;
}

.avatar-small {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  object-fit: cover;
  border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
}

.character-name { display: block; font-weight: 700; }
.character-detail { display: block; color: var(--muted); font-size: 0.79rem; }

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(360px, 1fr) auto auto auto;
  gap: 0.72rem;
}
.chat-header h3, .chat-header p { margin: 0; }
.chat-meta { margin-top: 0.28rem; color: var(--muted); font-size: 0.87rem; }

.chat-log {
  display: grid;
  gap: 0.58rem;
  overflow: auto;
  max-height: 560px;
  padding-right: 0.15rem;
}

.chat-message { display: flex; }
.user-message { justify-content: flex-end; }

.message-bubble {
  max-width: min(75ch, 86%);
  border-radius: 14px;
  border: 1px solid var(--line);
  padding: 0.62rem 0.74rem;
  line-height: 1.48;
  font-size: 0.92rem;
  white-space: pre-wrap;
}

.bot-message .message-bubble { background: var(--surface-soft); }
.user-message .message-bubble { background: var(--primary); border-color: transparent; color: #fff; }

.quick-prompts { display: flex; flex-wrap: wrap; gap: 0.45rem; }

.prompt-chip {
  border: 1px solid var(--line);
  background: var(--chip);
  color: var(--primary-strong);
  border-radius: 999px;
  padding: 0.3rem 0.62rem;
  cursor: pointer;
  font-size: 0.79rem;
  font-weight: 700;
}
.prompt-chip:hover { border-color: var(--primary); }

.chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.55rem;
}

#chat-input { min-height: 56px; max-height: 170px; resize: vertical; }

#send-btn,
.primary-btn {
  border: none;
  background: linear-gradient(140deg, var(--primary), var(--primary-strong));
  color: #fff;
  border-radius: 12px;
  padding: 0.68rem 0.95rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.secondary-btn {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--muted);
  border-radius: 12px;
  padding: 0.58rem 0.8rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.secondary-btn:hover { color: var(--text); border-color: var(--primary); }

#send-btn:disabled { opacity: 0.72; cursor: wait; }
.chat-hint { margin: 0; font-size: 0.8rem; color: var(--muted); }

.profile-panel {
  display: grid;
  align-content: start;
  gap: 0.8rem;
}

.profile-card {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 14px;
  padding: 0.7rem;
  display: grid;
  grid-template-columns: 68px minmax(0, 1fr);
  gap: 0.7rem;
  align-items: center;
}

.avatar-large {
  width: 68px;
  height: 68px;
  border-radius: 16px;
  object-fit: cover;
}

.profile-card h4 { margin: 0; font-size: 1rem; }
.profile-card p { margin: 0.25rem 0 0; }

.account-block h4 { margin: 0 0 0.42rem; }
.account-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.5rem;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0.45rem 0 0.7rem;
  display: grid;
  gap: 0.46rem;
}

.history-list li {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 10px;
  padding: 0.45rem 0.52rem;
  display: grid;
  gap: 0.12rem;
}

.history-list strong { font-size: 0.84rem; }
.history-list span { font-size: 0.74rem; color: var(--muted); }

.session-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.session-header h4 {
  margin: 0;
}

.session-item {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 10px;
  overflow: hidden;
}

.session-item.is-active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, var(--surface-soft));
}

.session-btn {
  width: 100%;
  border: none;
  background: transparent;
  text-align: left;
  color: var(--text);
  padding: 0.45rem 0.52rem;
  display: grid;
  gap: 0.12rem;
  cursor: pointer;
}

.session-btn strong {
  font-size: 0.84rem;
}

.session-btn span {
  font-size: 0.74rem;
  color: var(--muted);
}

.typing-cursor {
  display: inline-block;
  width: 0.54ch;
  height: 1.05em;
  margin-left: 0.1rem;
  vertical-align: -0.2em;
  background: currentColor;
  opacity: 0.85;
  animation: cursor-blink 0.9s steps(1, end) infinite;
}

@keyframes cursor-blink {
  50% { opacity: 0; }
}

.page-grid { display: grid; gap: 1rem; }
.surface.prose { line-height: 1.62; }
.surface.prose h3 { margin-bottom: 0.48rem; }
.surface.prose p, .surface.prose li { color: var(--muted); margin-top: 0.45rem; }
.surface.prose ul { margin: 0.45rem 0 0; padding-left: 1.2rem; }

.mini-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin-top: 0.1rem;
}

.mini-footer a {
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.28rem 0.6rem;
  color: var(--muted);
  background: var(--surface-soft);
  font-size: 0.78rem;
}

.mini-footer a:hover { color: var(--text); border-color: var(--primary); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 1180px) {
  .home-grid { grid-template-columns: 260px minmax(0, 1fr); }
  .profile-panel { grid-column: 1 / -1; }
}

@media (max-width: 920px) {
  .app-shell { grid-template-columns: 1fr; }
  .app-sidebar {
    position: static;
    min-height: 0;
    grid-template-rows: auto auto auto;
  }
  .home-grid { grid-template-columns: 1fr; }
  .chat-panel { grid-template-rows: auto minmax(290px, 1fr) auto auto auto; }
}

@media (max-width: 640px) {
  .app-page { padding: 0.72rem; }
  .app-header { align-items: flex-start; flex-direction: column; }
  .chat-form,
  .account-row { grid-template-columns: 1fr; }
  #send-btn,
  .primary-btn,
  .secondary-btn { width: 100%; }
}
