:root {
  --page: #ffffff;
  --sidebar: #f7f8fa;
  --surface: #ffffff;
  --surface-hover: #f3f5f8;
  --text: #1f2329;
  --text-secondary: #646a73;
  --text-faint: #959ba5;
  --border: #e8eaf0;
  --border-strong: #dfe3ea;
  --accent: #315efb;
  --accent-hover: #244bd8;
  --accent-soft: #eef2ff;
  --user-bubble: #f0f2f5;
  --success: #26956f;
  --success-soft: #edf8f4;
  --danger: #c43d36;
  --danger-soft: #fff3f2;
  --sidebar-width: 256px;
  --content-width: 768px;
  --composer-shadow:
    0 2px 6px rgba(31, 35, 41, 0.03),
    0 12px 32px rgba(31, 35, 41, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  color: var(--text);
  background: var(--page);
  font-family:
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    "PingFang SC",
    "Hiragino Sans GB",
    "Microsoft YaHei",
    sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
textarea {
  color: inherit;
  font: inherit;
}

button {
  padding: 0;
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button:disabled {
  cursor: not-allowed;
}

button:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(49, 94, 251, 0.2);
  outline-offset: 2px;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

svg {
  display: block;
}

[hidden] {
  display: none !important;
}

.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;
}

.app-shell {
  display: flex;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: var(--page);
}

/* 左侧导航 */

.sidebar {
  position: relative;
  z-index: 30;
  display: flex;
  flex: 0 0 var(--sidebar-width);
  flex-direction: column;
  width: var(--sidebar-width);
  height: 100%;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  min-height: 72px;
  padding: 0 20px;
}

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

.brand-mark {
  display: grid;
  width: 34px;
  height: 34px;
  color: #ffffff;
  background: var(--accent);
  border-radius: 10px;
  place-items: center;
}

.brand-mark svg {
  width: 23px;
  height: 23px;
}

.brand-copy strong,
.brand-copy span {
  display: block;
}

.brand-copy strong {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.brand-copy span {
  margin-top: 2px;
  color: var(--text-faint);
  font-size: 10px;
}

.sidebar-content {
  flex: 1;
  min-height: 0;
  padding: 6px 14px 20px;
  overflow-y: auto;
  scrollbar-width: none;
}

.sidebar-content::-webkit-scrollbar {
  display: none;
}

.new-chat-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  gap: 7px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  transition:
    color 150ms ease,
    background 150ms ease,
    border-color 150ms ease;
}

.new-chat-button:hover {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: rgba(49, 94, 251, 0.2);
}

.new-chat-button:active {
  background: #e5ebff;
}

.new-chat-button svg {
  width: 17px;
  height: 17px;
}

.sidebar-footer {
  padding: 16px 16px 18px;
  border-top: 1px solid var(--border);
}

.voice-panel {
  padding: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.voice-panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
}

.voice-state {
  color: var(--text-faint);
  font-size: 10px;
  font-weight: 500;
  line-height: 1.5;
  text-align: right;
  overflow-wrap: anywhere;
}

.voice-state.granted {
  color: var(--success);
}

.voice-state.denied {
  color: var(--danger);
}

.voice-panel p {
  margin: 7px 0 11px;
  color: var(--text-faint);
  font-size: 10px;
  line-height: 1.6;
}

.voice-enable-button {
  width: 100%;
  min-height: 44px;
  color: var(--text-secondary);
  background: var(--surface-hover);
  border-radius: 8px;
  font-size: 11px;
  transition:
    color 150ms ease,
    background 150ms ease;
}

.voice-enable-button:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.voice-enable-button:active {
  background: #e5ebff;
}

.voice-enable-button:disabled {
  opacity: 0.55;
}

.privacy-note {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  margin-top: 13px;
  color: var(--text-faint);
  font-size: 9px;
}

.privacy-note svg {
  width: 12px;
  height: 12px;
}

.sidebar-overlay {
  display: none;
}

/* 顶部区域 */

.main-shell {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  background: var(--page);
}

.topbar {
  position: relative;
  z-index: 10;
  display: flex;
  flex: 0 0 60px;
  align-items: center;
  justify-content: space-between;
  min-height: 60px;
  gap: 12px;
  padding: 0 24px;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.topbar-left {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 8px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  min-width: 0;
  gap: 8px;
}

.conversation-title {
  display: flex;
  align-items: baseline;
  min-width: 0;
  gap: 8px;
}

.conversation-title strong {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.conversation-title span {
  color: var(--text-faint);
  font-size: 10px;
  white-space: nowrap;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 44px;
  flex: 0 0 auto;
  color: var(--text-secondary);
  background: transparent;
  border-radius: 9px;
  place-items: center;
  transition:
    color 150ms ease,
    background 150ms ease;
}

.icon-button:hover {
  color: var(--text);
  background: var(--surface-hover);
}

.icon-button:active {
  background: var(--border);
}

.icon-button svg {
  width: 20px;
  height: 20px;
}

.menu-button {
  display: none;
}

.server-status {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 6px;
  padding: 7px 9px;
  color: var(--text-secondary);
  background: var(--sidebar);
  border-radius: 999px;
  font-size: 10px;
  line-height: 1;
  white-space: nowrap;
}

.status-dot {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  background: var(--text-faint);
  border-radius: 50%;
}

.status-dot.connecting {
  background: var(--accent);
  animation: status-pulse 1.3s ease-in-out infinite;
}

.status-dot.online {
  background: var(--success);
  animation: none;
}

.status-dot.offline,
.status-dot.is-offline {
  background: var(--danger);
  animation: none;
}

@keyframes status-pulse {
  50% {
    opacity: 0.3;
  }
}

/* 自动朗读 */

.auto-speak-button {
  min-height: 44px;
  padding: 0 12px;
  color: var(--text-secondary);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 12px;
  line-height: 1;
  white-space: nowrap;
  transition:
    color 150ms ease,
    background 150ms ease,
    border-color 150ms ease;
}

.auto-speak-button:hover {
  color: var(--text);
  background: var(--surface-hover);
  border-color: var(--border-strong);
}

.auto-speak-button:active {
  background: var(--border);
}

.auto-speak-button.is-active {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: rgba(49, 94, 251, 0.24);
}

.auto-speak-button.is-active:hover {
  color: var(--accent-hover);
  background: #e5ebff;
}

/* 对话滚动区域 */

.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  scrollbar-color: #d8dce3 transparent;
  scrollbar-width: thin;
}

/* 首屏只保留一句请安 */

.welcome {
  display: grid;
  width: min(900px, calc(100% - 48px));
  min-height: 100%;
  margin: 0 auto;
  padding: 0 0 9vh;
  place-items: center;
}

.welcome h1 {
  margin: 0;
  color: var(--text);
  font-size: clamp(28px, 3.2vw, 38px);
  font-weight: 600;
  line-height: 1.4;
  letter-spacing: -0.035em;
  text-align: center;
  animation: welcome-enter 320ms ease-out;
}

@keyframes welcome-enter {
  from {
    opacity: 0;
    transform: translateY(6px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 对话内容 */

.messages {
  width: min(var(--content-width), calc(100% - 48px));
  margin: 0 auto;
  padding: 34px 0 46px;
}

/* 兼容原有消息结构 */

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  margin-bottom: 30px;
  animation: message-enter 170ms ease-out;
}

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

.message-avatar {
  display: grid;
  flex: 0 0 30px;
  width: 30px;
  height: 30px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 9px;
  font-size: 11px;
  font-weight: 600;
  place-items: center;
}

.message-content {
  max-width: calc(100% - 42px);
}

.message-author {
  margin: 1px 0 7px;
  color: var(--text-faint);
  font-size: 10px;
}

.message-bubble {
  font-size: 15px;
  line-height: 1.85;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.message-row.assistant .message-bubble {
  padding: 0;
  color: var(--text);
  background: transparent;
}

.message-row.user .message-content {
  max-width: min(620px, 78%);
}

.message-row.user .message-bubble {
  padding: 11px 16px;
  color: var(--text);
  background: var(--user-bubble);
  border-radius: 17px 4px 17px 17px;
  line-height: 1.7;
}

/* 当前 app.js 使用的消息结构 */

.kiki-message {
  display: flex;
  align-items: flex-start;
  width: 100%;
  margin-bottom: 30px;
  animation: message-enter 170ms ease-out;
}

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

.kiki-message-assistant {
  justify-content: flex-start;
}

.kiki-message-body {
  min-width: 0;
  max-width: calc(100% - 42px);
}

.kiki-message-assistant .kiki-message-body {
  max-width: 100%;
}

.kiki-message-author {
  margin: 1px 0 7px;
  color: var(--text-faint);
  font-size: 10px;
  line-height: 1.5;
}

.kiki-message-content {
  color: var(--text);
  font-size: 15px;
  line-height: 1.85;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.kiki-message-user .kiki-message-body {
  max-width: min(620px, 78%);
}

.kiki-message-user .kiki-message-content {
  padding: 11px 16px;
  color: var(--text);
  background: var(--user-bubble);
  border-radius: 17px 4px 17px 17px;
  line-height: 1.7;
}

@keyframes message-enter {
  from {
    opacity: 0;
    transform: translateY(4px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 每条 AI 回复下方的朗读按钮 */

.message-actions,
.kiki-message-actions {
  display: flex;
  align-items: center;
  min-height: 44px;
  margin-top: 4px;
}

.speak-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 72px;
  min-height: 44px;
  padding: 0 10px;
  color: var(--text-faint);
  background: transparent;
  border-radius: 8px;
  font-size: 12px;
  line-height: 1;
  white-space: nowrap;
  transition:
    color 150ms ease,
    background 150ms ease,
    opacity 150ms ease;
}

.speak-button:hover {
  color: var(--text);
  background: var(--surface-hover);
}

.speak-button:active {
  background: var(--border);
}

.speak-button.is-loading {
  color: var(--text-secondary);
  background: var(--surface-hover);
  cursor: wait;
}

.speak-button.is-playing {
  color: var(--accent);
  background: var(--accent-soft);
}

.speak-button.is-playing:hover {
  color: var(--accent-hover);
  background: #e5ebff;
}

.speak-button:disabled {
  opacity: 0.6;
  cursor: wait;
}

/* 正在生成文字回复 */

.typing-bubble {
  display: flex;
  align-items: center;
  min-width: 52px;
  min-height: 30px;
  gap: 5px;
}

.typing-bubble span {
  width: 5px;
  height: 5px;
  background: var(--text-faint);
  border-radius: 50%;
  animation: typing 1.1s ease-in-out infinite;
}

.typing-bubble span:nth-child(2) {
  animation-delay: 130ms;
}

.typing-bubble span:nth-child(3) {
  animation-delay: 260ms;
}

.typing-dots {
  display: inline-flex;
  align-items: center;
  min-width: 52px;
  min-height: 30px;
  gap: 5px;
}

.typing-dots span {
  width: 5px;
  height: 5px;
  background: var(--text-faint);
  border-radius: 50%;
  animation: typing 1.1s ease-in-out infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 130ms;
}

.typing-dots span:nth-child(3) {
  animation-delay: 260ms;
}

@keyframes typing {
  0%,
  60%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }

  30% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* 底部输入区 */

.composer-area {
  position: relative;
  z-index: 8;
  flex: 0 0 auto;
  padding: 10px 24px calc(12px + env(safe-area-inset-bottom));
  background: var(--page);
}

.composer-inner {
  width: min(800px, 100%);
  margin: 0 auto;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 9px 9px 9px 16px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  box-shadow: var(--composer-shadow);
  transition:
    border-color 150ms ease,
    box-shadow 150ms ease;
}

.composer:focus-within {
  border-color: rgba(49, 94, 251, 0.52);
  box-shadow:
    0 0 0 3px rgba(49, 94, 251, 0.05),
    var(--composer-shadow);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  max-height: 144px;
  padding: 9px 0 7px;
  overflow-y: auto;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;
  resize: none;
  font-size: 16px;
  line-height: 1.5;
}

.composer textarea::placeholder {
  color: var(--text-faint);
}

.composer textarea:disabled {
  opacity: 0.68;
}

.composer-actions {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 4px;
}

.composer-icon-button,
.send-button {
  display: grid;
  width: 40px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: 11px;
  place-items: center;
  transition:
    color 150ms ease,
    background 150ms ease,
    opacity 150ms ease;
}

.composer-icon-button {
  color: var(--text-secondary);
  background: transparent;
}

.composer-icon-button:hover {
  color: var(--text);
  background: var(--surface-hover);
}

.composer-icon-button:active {
  background: var(--border);
}

.composer-icon-button.granted {
  color: var(--success);
  background: var(--success-soft);
}

.composer-icon-button svg,
.send-button svg {
  width: 19px;
  height: 19px;
}

.send-button {
  color: #ffffff;
  background: var(--accent);
  border-radius: 50%;
  font-size: 11px;
  line-height: 1;
}

.send-button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.send-button:active:not(:disabled) {
  background: #1f40bd;
}

.send-button:disabled {
  color: #8f96a1;
  background: #eff1f4;
}

.composer-note {
  margin: 8px 0 0;
  color: var(--text-faint);
  font-size: 9px;
  line-height: 1.5;
  text-align: center;
}

/* 错误提示 */

.error-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 9px;
  padding: 10px 12px;
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid rgba(196, 61, 54, 0.12);
  border-radius: 10px;
  font-size: 11px;
  line-height: 1.5;
}

.error-banner button {
  display: grid;
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  color: inherit;
  background: transparent;
  border-radius: 7px;
  font-size: 19px;
  line-height: 1;
  place-items: center;
}

.error-banner button:hover {
  background: rgba(196, 61, 54, 0.08);
}

/* 轻提示 */

.toast {
  position: fixed;
  z-index: 100;
  bottom: calc(26px + env(safe-area-inset-bottom));
  left: 50%;
  max-width: min(420px, calc(100% - 32px));
  padding: 10px 14px;
  color: #ffffff;
  background: rgba(31, 35, 41, 0.94);
  border-radius: 9px;
  box-shadow: 0 10px 28px rgba(31, 35, 41, 0.16);
  font-size: 11px;
  line-height: 1.55;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition:
    opacity 170ms ease,
    transform 170ms ease;
}

.toast.visible,
.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* 平板和手机 */

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(288px, 84vw);
    transform: translateX(-102%);
    transition: transform 210ms ease;
    box-shadow: 16px 0 48px rgba(31, 35, 41, 0.1);
  }

  .sidebar.open,
  .sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-overlay {
    position: fixed;
    z-index: 20;
    inset: 0;
    display: block;
    visibility: hidden;
    background: rgba(31, 35, 41, 0.2);
    opacity: 0;
    transition:
      opacity 210ms ease,
      visibility 210ms ease;
  }

  .sidebar-overlay.visible,
  .sidebar-overlay.is-visible,
  .sidebar-overlay:not([hidden]) {
    visibility: visible;
    opacity: 1;
  }

  .menu-button {
    display: grid;
  }

  .topbar {
    padding: 0 13px;
  }

  .welcome {
    width: calc(100% - 36px);
  }

  .messages {
    width: calc(100% - 36px);
  }

  .composer-area {
    padding-right: 12px;
    padding-left: 12px;
  }
}

@media (max-width: 560px) {
  .topbar {
    gap: 7px;
  }

  .topbar-left {
    gap: 4px;
  }

  .topbar-actions {
    gap: 6px;
  }

  .conversation-title span {
    display: none;
  }

  .auto-speak-button {
    padding: 0 9px;
    font-size: 11px;
  }

  .server-status {
    padding: 6px 7px;
    font-size: 9px;
  }

  .welcome {
    width: calc(100% - 32px);
    padding-bottom: 8vh;
  }

  .welcome h1 {
    font-size: 27px;
    letter-spacing: -0.03em;
  }

  .messages {
    width: calc(100% - 28px);
    padding-top: 24px;
  }

  .message-row,
  .kiki-message {
    gap: 9px;
    margin-bottom: 25px;
  }

  .message-avatar {
    flex-basis: 28px;
    width: 28px;
    height: 28px;
  }

  .message-content {
    max-width: calc(100% - 37px);
  }

  .message-row.user .message-content,
  .kiki-message-user .kiki-message-body {
    max-width: 84%;
  }

  .message-bubble,
  .kiki-message-content {
    font-size: 15px;
    line-height: 1.8;
  }

  .composer {
    padding-left: 13px;
    border-radius: 17px;
  }

  .composer-note {
    font-size: 9px;
  }
}

@media (max-width: 390px) {
  .topbar {
    padding-right: 9px;
    padding-left: 9px;
  }

  .conversation-title strong {
    font-size: 13px;
  }

  .auto-speak-button {
    padding: 0 8px;
    font-size: 10px;
  }

  .server-status {
    gap: 4px;
    padding-right: 6px;
    padding-left: 6px;
  }

  .icon-button {
    width: 36px;
  }
}

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