:root {
  --bg: #1e1e20;
  --bg-elev: #2a2a2d;
  --bg-inset: #18181a;
  --text: #e4e4e7;
  --muted: #8b8b93;
  --user: #35353a;
  --shadow-out: 4px 4px 10px #121214, -2px -2px 8px #2e2e32;
  --shadow-in: inset 2px 2px 6px #141416, inset -2px -2px 6px #323236;
  --radius: 20px;
  --side-w: 320px;
  --chat-w: 580px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  overflow: hidden;
}

/* Выдвижная панель */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--side-w);
  min-width: var(--side-w);
  background: #26262b;
  border-right: 1px solid #3d3d45;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 40;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar.open { transform: translateX(0); }

.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 18px;
  border-bottom: 1px solid #3d3d45;
  flex-shrink: 0;
}
.sidebar-title {
  font-size: 15px;
  font-weight: 600;
  color: #f0f0f2;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: #35353c;
  color: #f0f0f2;
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
}
.icon-btn:hover { background: #45454e; }

.btn-new-chat {
  margin: 14px 16px 10px;
  padding: 12px 16px;
  border: none;
  border-radius: 12px;
  background: #404048;
  color: #ffffff;
  font-size: 15px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-out);
  display: flex;
  align-items: center;
  gap: 12px;
  width: calc(100% - 32px);
  flex-shrink: 0;
}
.btn-new-chat:hover { background: #4d4d58; }
.btn-new-icon { font-size: 22px; font-weight: 300; }
.btn-new-label { flex: 1; text-align: left; }

.history-empty {
  padding: 0 18px 8px;
  font-size: 13px;
  color: var(--muted);
}

.chat-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 4px 12px 16px;
  width: 100%;
}
.chat-list li {
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  margin-bottom: 8px;
  background: #323238;
  border: 1px solid #44444e;
  width: 100%;
}
.chat-list li:hover { background: #3c3c44; }
.chat-list li.active {
  background: #4a4a55;
  border-color: #62626e;
}
.chat-list li.marked {
  border-color: #8a6a9a;
  background: #3d3548;
}
.chat-list li.select-mode { cursor: default; display: flex; gap: 10px; align-items: flex-start; }
.chat-list li.select-mode .chat-item-body { flex: 1; min-width: 0; }
.chat-pick {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  flex-shrink: 0;
  accent-color: #9a7aaa;
  cursor: pointer;
}

.sidebar-footer {
  flex-shrink: 0;
  padding: 10px 12px 14px;
  border-top: 1px solid #3d3d45;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.select-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  width: 100%;
}
.select-actions[hidden] { display: none !important; }
.side-foot-btn {
  flex: 1;
  min-width: calc(50% - 4px);
  padding: 10px 8px;
  border: none;
  border-radius: 10px;
  background: #404048;
  color: #f0f0f2;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
}
.side-foot-btn:hover { background: #4d4d58; }
.side-foot-btn.ghost { background: #323238; color: #b0b0b8; }
.side-foot-btn.danger { background: #5c3848; color: #fff; }
.side-foot-btn.danger:hover:not(:disabled) { background: #734058; }
.side-foot-btn:disabled { opacity: 0.45; cursor: default; }
body.select-mode .btn-new-chat { opacity: 0.5; pointer-events: none; }
.chat-item-title {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #ffffff;
  line-height: 1.35;
  word-break: break-word;
}
.chat-item-preview {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  color: #b0b0b8;
  line-height: 1.4;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.chat-item-meta {
  display: block;
  margin-top: 5px;
  font-size: 11px;
  color: #787884;
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 35;
}
.overlay.show { display: block; }

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  width: 100%;
  transition: margin-left 0.25s ease;
}
body.sidebar-open .main {
  margin-left: var(--side-w);
}
/* меню сдвигает чат, затемнение не нужно */
body.sidebar-open .overlay.show {
  display: none;
}

.topbar {
  position: relative;
  padding: 12px 48px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
}
.topbar-menu {
  position: absolute;
  left: 12px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: #35353c;
  color: #f0f0f2;
  font-size: 18px;
  cursor: pointer;
  box-shadow: var(--shadow-out);
}
.topbar-menu:hover { background: #45454e; }
.mode-switch {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--bg-inset);
  box-shadow: var(--shadow-in);
}
.mode {
  border: none;
  padding: 8px 18px;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
}
.mode.active {
  background: var(--bg-elev);
  color: var(--text);
  box-shadow: var(--shadow-out);
}

.msg {
  line-height: 1.55;
  font-size: 15px;
  white-space: pre-wrap;
  word-break: break-word;
  max-width: 92%;
}
.msg.user {
  display: block;
  width: fit-content;
  margin: 0 0 16px auto;
  background: var(--user);
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-out);
}
.msg.assistant {
  display: block;
  width: fit-content;
  margin: 0 auto 16px 0;
}
.msg.assistant.streaming::after {
  content: "▋";
  animation: blink 0.8s step-end infinite;
  opacity: 0.6;
}
@keyframes blink { 50% { opacity: 0; } }

.msg.scene {
  display: block;
  width: fit-content;
  max-width: min(420px, 100%);
  margin: 0 auto 16px 0;
  padding: 10px;
  border-radius: var(--radius);
  background: var(--bg-elev);
  box-shadow: var(--shadow-out);
}
.scene-cap {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
}
.scene-img {
  width: 100%;
  border-radius: 10px;
  display: block;
}
.scene-hint {
  font-size: 11px;
  color: var(--muted);
  opacity: 0.75;
  margin-top: 8px;
  line-height: 1.35;
  word-break: break-word;
}
.scene-pending {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 28px 16px;
  color: var(--muted);
  font-size: 13px;
}
.scene-pending .media-spinner {
  width: 18px;
  height: 18px;
}

.composer-wrap {
  padding: 0 24px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  width: 100%;
  max-width: var(--chat-w);
  background: var(--bg-elev);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow-out);
}
.composer-btn {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  box-shadow: var(--shadow-out);
  font-size: 18px;
}
.composer-btn.send:not(:disabled) { color: var(--text); background: #404048; }
.composer-btn:disabled { opacity: 0.4; cursor: default; }

#input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  resize: none;
  max-height: 160px;
  outline: none;
  font-family: inherit;
  padding: 8px 4px;
}
.composer-meta {
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
  max-width: var(--chat-w);
  width: 100%;
}
.link-img { color: #9a7aaa; text-decoration: none; }
.link-img:hover { text-decoration: underline; }
.empty-hint {
  color: var(--muted);
  text-align: center;
  margin: auto;
  font-size: 14px;
}

/* 18+ — карточки персонажей (только выбор, не вместе с чатом) */
.chat-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.character-panel,
.messages {
  flex: 1;
  min-height: 0;
}
.character-panel {
  display: none;
  overflow-y: auto;
  padding: 16px clamp(20px, 8vw, 72px) 12px;
}
.messages {
  display: none;
}
.messages.active {
  display: block;
  overflow-y: auto;
  overflow-x: hidden;
  width: 100%;
  padding: 16px max(24px, calc((100% - var(--chat-w)) / 2)) 12px;
  scrollbar-gutter: stable;
}
.messages.active .msg,
.messages.active .empty-hint {
  max-width: var(--chat-w);
  margin-left: auto;
  margin-right: auto;
  padding: 0;
}
.messages.active .empty-hint {
  text-align: center;
  padding: 2rem 0;
}
.messages.active .msg.user {
  margin: 0 0 16px auto;
  max-width: 92%;
}
.messages.active .msg.assistant {
  margin: 0 auto 16px 0;
  max-width: 92%;
}
.messages.active::-webkit-scrollbar {
  width: 8px;
}
.messages.active::-webkit-scrollbar-thumb {
  background: #45454e;
  border-radius: 4px;
}
.character-panel.active {
  display: block;
  overflow-y: auto;
  padding: 16px max(24px, calc((100% - 480px) / 2)) 12px;
}
.character-panel.active .character-panel-hint,
.character-panel.active .character-grid {
  max-width: 480px;
  margin-left: auto;
  margin-right: auto;
}
.character-panel-hint {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 20px;
}
.character-grid {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  max-width: 480px;
  margin: 0 auto;
}
.character-card {
  width: 100%;
  display: flex;
  gap: 16px;
  text-align: left;
  padding: 18px;
  border: 1px solid #4a4a55;
  border-radius: 16px;
  background: #2e2e35;
  color: #f0f0f2;
  cursor: pointer;
  font-family: inherit;
  box-shadow: var(--shadow-out);
  transition: background 0.15s, border-color 0.15s;
}
.character-card:hover {
  background: #3a3a44;
  border-color: #6a6a78;
}
.character-avatar {
  width: 56px;
  height: 56px;
  flex-shrink: 0;
  border-radius: 50%;
  background: linear-gradient(145deg, #5c4a6a, #3d3548);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 600;
  overflow: hidden;
}
.character-avatar-img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}
.character-name {
  font-size: 18px;
  font-weight: 600;
}
.character-role {
  font-size: 13px;
  color: #c9a0b8;
  margin-top: 2px;
}
.character-tag {
  font-size: 12px;
  color: #9a9aa8;
  margin-top: 6px;
}
.character-bio {
  font-size: 13px;
  line-height: 1.45;
  color: #b8b8c0;
  margin-top: 10px;
}
body.tab-adult .topbar { border-bottom: 1px solid #3a3040; }
body.tab-media .topbar { border-bottom: 1px solid #30403a; }

.media-panel {
  display: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px max(24px, calc((100% - var(--chat-w)) / 2)) 12px;
}
.media-panel.active { display: block; }
.media-inner {
  max-width: var(--chat-w);
  margin: 0 auto;
}
.media-prompt {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
  line-height: 1.4;
  word-break: break-word;
}
.media-prompt-en {
  font-size: 11px;
  color: var(--muted);
  opacity: 0.75;
  margin: -8px 0 12px;
  line-height: 1.35;
  word-break: break-word;
}
.media-img {
  width: 100%;
  border-radius: 14px;
  box-shadow: var(--shadow-out);
  display: block;
}
.media-status {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 2rem 0;
}
.media-error { color: #e07070; padding: 1rem 0 0.5rem; }
.media-img.dim { opacity: 0.35; margin-top: 0.5rem; }
.media-spinner {
  display: inline-block;
  width: 28px;
  height: 28px;
  border: 3px solid #45454e;
  border-top-color: #9a7aaa;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.chat-list li.has-thumb { display: flex; gap: 10px; align-items: flex-start; }
.gen-thumb {
  width: 52px;
  height: 52px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: #323238;
}
.gen-thumb.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #666;
}
body.tab-media .mode-switch { flex-wrap: wrap; justify-content: center; max-width: 100%; }
body.tab-media .mode { padding: 8px 12px; font-size: 12px; }
