/* Reset & Base Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --primary-color: #aa4b75;
  --primary-hover: #92375f;
  --primary-light: #fce5ef;
  --bg-app: #fff0f5;
  --bg-card: #fff9fc;
  --bg-header: #fff7fb;
  --text-main: #452d39;
  --text-muted: #856674;
  --border-color: #efd0dd;
  --user-msg-bg: #f4cddd;
  --user-msg-text: #5e2943;
  --bot-msg-bg: #ffffff;
  --bot-msg-text: #452d39;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --sidebar-width: 280px;
  --chat-content-width: 800px;
  --modal-padding: 20px;
}

html, body {
  width: 100%;
  height: 100%;
  height: var(--app-height, 100dvh);
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  background-color: var(--bg-app);
  color: var(--text-main);
  /* 锁定防止用户放大缩小 */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
body { position: fixed; inset: 0; }

/* App Container Layout */
.app-container {
  display: flex;
  width: 100%;
  height: 100%;
  height: var(--app-height, 100dvh);
  position: fixed;
  top: var(--viewport-top, 0px);
  left: 0;
  overflow: hidden;
}

/* Sidebar Styling */
.sidebar {
  width: var(--sidebar-width);
  height: 100%;
  background: var(--bg-card);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.site-logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  flex-shrink: 0;
  object-fit: cover;
}

.sidebar-header h2 {
  flex: 1;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
}

.sidebar .close-sidebar-btn {
  display: none;
  font-size: 1.5rem;
  line-height: 1;
}

.sidebar-actions {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

.guide-link { color: var(--primary-color, #9e416b); font-size: 13px; text-align: center; padding: 3px; }
.guide-link:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }

.sidebar-characters {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

.template-link { color: var(--primary-color); font-size: 12px; text-align: center; }
.role-code-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.role-code-toolbar .template-link { margin-left: auto; }
.form-group .role-code-editor { min-width: 0; min-height: 160px; max-height: min(400px, calc(var(--app-height, 100dvh) * 0.4)); resize: vertical; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; line-height: 1.45; tab-size: 2; white-space: pre-wrap; overflow-wrap: anywhere; }
.role-edit-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.chat-tools { max-width: var(--chat-content-width); margin: 0 auto 8px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 12px; color: var(--text-muted); }
.chat-tool-group { display: flex; align-items: center; gap: 6px; }
.tool-btn { padding: 5px 10px; border: 1px solid var(--border-color); border-radius: 20px; background: var(--bg-app); color: var(--text-main); font-family: inherit; cursor: pointer; min-height: 32px; }
.chat-tools select { font: inherit; color: var(--text-main); border: 1px solid var(--border-color); border-radius: 8px; background: var(--bg-card); padding: 6px; }
.emoji-picker { max-width: var(--chat-content-width); max-height: 168px; overflow-y: auto; margin: 0 auto 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.emoji-picker[hidden] { display: none; }
.emoji-picker button { font-size: 23px; min-width: 38px; min-height: 38px; border: 0; border-radius: 10px; background: var(--primary-light); cursor: pointer; }
.memory-facts { font-size: 13px; line-height: 1.7; white-space: pre-wrap; overflow-wrap: anywhere; }
.memory-fact { display: flex; align-items: start; justify-content: space-between; gap: 8px; border-bottom: 1px solid var(--border-color); padding: 8px 0; }
.memory-fact button { flex-shrink: 0; }
.backup-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: 100%; }
.backup-actions .btn { width: 100%; min-height: 44px; padding: 10px 12px; font-size: 14px; line-height: 22px; }
.memory-facts:empty, .form-hint:empty { display: none; }
[hidden] { display: none !important; }
.role-details-content { display: flex; flex-direction: column; gap: 12px; line-height: 1.7; font-size: 14px; overflow-wrap: anywhere; }
.role-details-content img { width: 84px; height: 84px; align-self: center; }
.role-details-content p { white-space: pre-wrap; }

.character-section {
  padding: 12px 16px;
  flex: 0 0 auto;
}

.character-section h3 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.character-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.character-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0;
  border-radius: var(--radius-md);
  cursor: pointer;
  background: transparent;
  border: 1px solid transparent;
  transition: all 0.2s ease;
  user-select: none;
  position: relative;
}

.character-card:hover {
  background: var(--bg-app);
}

.character-card.active {
  background: var(--primary-light);
  border-color: rgba(170, 75, 117, 0.2);
}

.character-card .avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  background: #f1f5f9;
}

.character-choice {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 0;
  border-radius: inherit;
  background: transparent;
  text-align: left;
  font-family: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.character-card .info {
  flex: 1;
  min-width: 0;
}

.character-card .name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.character-card .tag {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.character-card .delete-btn {
  display: inline-flex;
  padding: 10px;
  color: #ef4444;
  border-radius: 4px;
  background: transparent;
  border: none;
  cursor: pointer;
}

.character-card .edit-role-btn {
  flex-shrink: 0;
  min-height: 44px;
  padding: 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--primary-color);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.character-card:hover .delete-btn {
  display: block;
}

/* Sidebar Overlay for Mobile */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(2px);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* Chat Main Area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  background: var(--bg-app);
  min-width: 0;
}

/* Header */
.chat-header {
  height: 64px;
  padding: 0 20px;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  box-shadow: var(--shadow-sm);
  z-index: 10;
  flex-shrink: 0;
}

.menu-btn {
  display: none;
}

.current-character-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.avatar {
  border-radius: 50%;
  object-fit: cover;
  background: #f1f5f9;
}

.avatar-sm {
  width: 40px;
  height: 40px;
}

.char-details {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.char-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.char-tag {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Messages Container */
.messages-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.messages {
  max-width: var(--chat-content-width);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Message Item */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  max-width: 85%;
  animation: fadeIn 0.25s ease-out forwards;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message-row.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-row.bot {
  align-self: flex-start;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.message-avatar-button {
  padding: 0;
  border: 0;
  background: transparent;
  flex-shrink: 0;
  cursor: pointer;
  border-radius: 50%;
  line-height: 0;
}

.message-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 0.95rem;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.message-row.bot .message-bubble {
  background: var(--bot-msg-bg);
  color: var(--bot-msg-text);
  border-top-left-radius: 4px;
}

.message-row.user .message-bubble {
  background: var(--user-msg-bg);
  color: var(--user-msg-text);
  border-top-right-radius: 4px;
}

.message-time {
  font-size: 0.7rem;
  color: var(--text-muted);
  align-self: flex-end;
  padding: 0 4px;
}

.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Quick Replies */
.quick-replies {
  max-width: var(--chat-content-width);
  margin: 0 auto 8px auto;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px;
  scrollbar-width: none;
}

.quick-replies::-webkit-scrollbar {
  display: none;
}

.quick-chip {
  padding: 6px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  font-size: 0.825rem;
  color: var(--text-muted);
  white-space: nowrap;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm);
  font-family: inherit;
  min-height: 36px;
  touch-action: manipulation;
}

.quick-chip:disabled {
  opacity: 0.55;
  cursor: default;
}

.reply-status {
  max-width: var(--chat-content-width);
  margin: 6px auto 0;
  color: var(--text-muted);
  font-size: 0.75rem;
}
.reply-status:empty { display: none; }
.site-update {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--primary-light, #fce4ec);
  color: var(--text-primary, #4b303d);
  font-size: 13px;
  flex-shrink: 0;
}
.site-update .btn { padding: 5px 12px; font-size: 13px; white-space: nowrap; }

.reply-error {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: #fff7ed;
  color: #9a3412;
  font-size: 0.85rem;
}

.quick-chip:hover {
  background: var(--primary-light);
  color: var(--primary-color);
  border-color: var(--primary-color);
}

/* Input Container */
.input-container {
  background: var(--bg-card);
  border-top: 1px solid var(--border-color);
  padding: 12px 20px 16px 20px;
  flex-shrink: 0;
}

.input-area {
  max-width: var(--chat-content-width);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-app);
  border: 1px solid var(--border-color);
  border-radius: 24px;
  padding: 6px 6px 6px 16px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  flex: 1;
  min-width: 0;
}

.composer-row {
  max-width: var(--chat-content-width);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.user-avatar-button {
  width: 44px;
  flex-shrink: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  font-family: inherit;
}

.user-avatar-button img {
  width: 36px;
  height: 36px;
  border: 2px solid #fff;
  box-shadow: var(--shadow-sm);
}

.avatar-upload-status {
  max-width: var(--chat-content-width);
  margin: 6px auto 0;
  color: var(--text-muted);
  font-size: 12px;
}

.avatar-upload-status:empty {
  display: none;
}

.input-area:focus-within {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(170, 75, 117, 0.12);
  background: var(--bg-card);
}

#userInput {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  resize: none;
  max-height: min(120px, calc(var(--app-height, 100dvh) * 0.18));
  min-height: 24px;
  font-size: 16px;
  line-height: 1.4;
  outline: none;
  color: var(--text-main);
  padding: 6px 0;
  font-family: inherit;
}

/* Button Components */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--primary-color);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--primary-light);
  color: var(--text-main);
}

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

.btn-outline {
  background: transparent;
  border-color: var(--border-color);
  color: var(--text-main);
}

.btn-outline:hover {
  background: var(--bg-app);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.825rem;
  border-radius: var(--radius-sm);
}

.btn-block {
  width: 100%;
}

.btn-send {
  background: var(--primary-color);
  color: white;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  padding: 0;
  flex-shrink: 0;
}

button:focus-visible,
a:focus-visible,
select:focus-visible {
  outline: 3px solid var(--primary-color);
  outline-offset: 3px;
}

.btn-send span {
  display: none;
}

.btn-send:disabled {
  background: #d5b7c5;
  cursor: not-allowed;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, color 0.2s ease;
  touch-action: manipulation;
}

.icon-btn:hover {
  background: var(--bg-app);
  color: var(--text-main);
}

/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: var(--viewport-top, 0px);
  left: 0;
  width: 100%;
  height: var(--app-height, 100dvh);
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--bg-card);
  width: 100%;
  max-width: 500px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  transform: scale(0.95);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  display: flex;
  flex-direction: column;
  max-height: calc(var(--app-height, 100dvh) - 32px);
  min-height: 0;
}

.modal-overlay.active .modal {
  transform: scale(1);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 1.15rem;
  font-weight: 700;
}

.modal-body {
  padding: var(--modal-padding);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
  overscroll-behavior: contain;
}

.memory-panel, .memory-backup {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.form-group label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-main);
}

.required {
  color: #ef4444;
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 16px;
  outline: none;
  font-family: inherit;
  transition: border-color 0.2s ease;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(170, 75, 117, 0.12);
}

.avatar-picker {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.avatar-upload-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar-upload-preview {
  width: 72px;
  height: 72px;
  flex-shrink: 0;
  border: 2px solid var(--border-color);
}

.avatar-upload-controls {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.form-hint {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-muted);
}

.preset-avatars {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.avatar-picker-button {
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.avatar-option {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  border: 3px solid transparent;
  transition: all 0.2s ease;
}

.avatar-option.selected {
  border-color: var(--primary-color);
  transform: scale(1.08);
}

.modal-footer {
  position: sticky;
  bottom: calc(-1 * var(--modal-padding));
  margin: 0 calc(-1 * var(--modal-padding)) calc(-1 * var(--modal-padding));
  padding: 14px var(--modal-padding);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: var(--bg-card);
  z-index: 1;
}

/* Responsive Adaptation */

/* Tablet and desktop: persistent roles beside the conversation. */
@media (min-width: 768px) {
  :root { --sidebar-width: clamp(240px, 24vw, 300px); --chat-content-width: 960px; --modal-padding: 24px; }
  .sidebar-header { height: 72px; }
  .sidebar-actions { padding: 16px; }
  .character-section { padding: 12px; }
  .chat-header { height: 72px; padding-inline: max(24px, calc((100% - var(--chat-content-width)) / 2)); }
  .current-character-info { flex: 1; }
  .header-actions { flex-shrink: 0; }
  .menu-btn, #switchCharBtn { display: none; }
  .messages-container { padding: 24px; }
  .message-row { max-width: min(88%, 760px); }
  .message-bubble { line-height: 1.7; }
  .input-container { padding: 12px 24px max(16px, env(safe-area-inset-bottom)); }
  .btn { min-height: 44px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .tool-btn, .chat-tools select, .quick-chip { min-height: 40px; }
  .emoji-picker button { min-width: 44px; min-height: 44px; }
  .btn-send { width: 44px; height: 44px; }
  .user-avatar-button { min-height: 48px; }
  .user-avatar-button img, .message-avatar { width: 40px; height: 40px; }
  .modal { max-width: 560px; }
  #createModalOverlay .modal, #memoryModalOverlay .modal, #roleCodeModalOverlay .modal { max-width: 780px; }
  #createRoleForm, .memory-layout { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; gap: 20px; }
  .form-span-full, .modal-footer, .memory-backup { grid-column: 1 / -1; }
  .memory-backup { padding-top: 20px; border-top: 1px solid var(--border-color); }
}

@media (min-width: 1024px) {
  .btn-send { width: auto; min-width: 90px; padding: 0 16px; border-radius: 18px; }
  .btn-send span { display: inline; }
  .messages { gap: 20px; }
}

/* Compact windows, split-screen tablets and touch keyboards use a drawer. */
@media (max-width: 767px) {
  .sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
    width: min(320px, 88vw);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sidebar.active { transform: translateX(0); }
  .sidebar-overlay.active { display: block; opacity: 1; }
  .sidebar .close-sidebar-btn, .menu-btn { display: inline-flex; }
}

/* Mobile Layout */
@media (min-width: 768px) and (max-width: 1199px) {
  .keyboard-open .sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
    width: min(320px, 88vw);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .keyboard-open .sidebar.active { transform: translateX(0); }
  .keyboard-open .sidebar-overlay.active { display: block; opacity: 1; }
  .keyboard-open .sidebar .close-sidebar-btn, .keyboard-open .menu-btn { display: inline-flex; }
  .keyboard-open .chat-header { padding-inline: 16px; height: 64px; }
}

@media (max-width: 767px) {
  .chat-header {
    padding: 0 12px;
    padding-top: env(safe-area-inset-top);
    height: calc(64px + env(safe-area-inset-top));
    gap: 8px;
  }

  .current-character-info {
    flex: 1;
    gap: 8px;
  }

  .header-actions {
    gap: 6px;
    flex-shrink: 0;
  }

  .messages-container {
    padding: 12px;
  }

  .message-row {
    max-width: 90%;
  }

  .input-container {
    padding: 10px 12px 12px 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  #switchCharBtn {
    padding: 4px 8px;
    font-size: 0.75rem;
  }
}

.sidebar-overlay { top: var(--viewport-top, 0px); bottom: auto; height: var(--app-height, 100dvh); }
.keyboard-open .quick-replies { display: none; }
.keyboard-open .chat-tools { margin-bottom: 4px; }
.keyboard-open .emoji-picker { max-height: min(100px, calc(var(--app-height, 100dvh) * 0.2)); }
.keyboard-open .input-container { padding-top: 6px; padding-bottom: 6px; }
.keyboard-open .modal-overlay { padding: 8px; }
.keyboard-open .modal { max-height: calc(var(--app-height, 100dvh) - 16px); }
.keyboard-open .modal-body { --modal-padding: 12px; }

@media (max-height: 480px) {
  .chat-header { height: 56px; }
  .quick-replies { display: none; }
  .input-container { padding-top: 6px; padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .sidebar-actions { padding-block: 8px; gap: 6px; }
  .sidebar-actions .btn { min-height: 32px; padding-block: 6px; }
  .sidebar-header { height: 52px; padding-block: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
