:root {
  color-scheme: light;
  --bg: #f8faf8;
  --panel: #fffefd;
  --panel-soft: #f1f5f2;
  --ink: #1f2528;
  --muted: #687579;
  --line: #dce4de;
  --accent: #0f8b8d;
  --accent-strong: #096466;
  --danger: #b24c63;
  --warning: #a56b18;
  --shadow: 0 18px 48px rgba(31, 37, 40, 0.12);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
}

button,
textarea,
input,
select {
  font: inherit;
}

button {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
  font-weight: 800;
}

button:disabled {
  cursor: wait;
  opacity: 0.62;
}

h1,
h2,
h3,
p {
  margin: 0;
}

.app-shell {
  position: relative;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
}

.chat-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
  overflow-x: clip;
  background: var(--panel);
}

.chat-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background-image: var(--chat-bg-image);
  background-position: center;
  background-size: contain;
  background-repeat: no-repeat;
  filter: blur(var(--chat-bg-blur, 0));
  opacity: 0;
  pointer-events: none;
}

.chat-panel.has-chat-background::before {
  opacity: var(--chat-bg-opacity, 0.18);
}

.chat-panel > * {
  position: relative;
  z-index: 1;
}

.topbar {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: 8px;
  align-items: center;
  min-height: 64px;
  padding: max(10px, env(safe-area-inset-top)) 12px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 254, 253, 0.94);
  backdrop-filter: blur(14px);
}

.agent-identity {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-width: 0;
}

.avatar,
.mini-avatar {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  overflow: hidden;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--danger));
  font-weight: 900;
}

.avatar img,
.mini-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar.has-avatar-image,
.mini-avatar.has-avatar-image {
  background: var(--panel-soft);
}

.avatar {
  width: 42px;
  height: 42px;
}

.mini-avatar {
  width: 38px;
  height: 38px;
}

.avatar-upload {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.topbar h1 {
  overflow: hidden;
  max-width: 100%;
  font-size: 18px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar p,
.drawer-header p,
.section-title span,
.agent-card small,
.message-meta,
.empty {
  color: var(--muted);
  font-size: 12px;
}

.icon-button,
.plain-icon {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  border: 1px solid var(--line);
  color: var(--accent-strong);
  background: var(--panel-soft);
}

.icon-button span {
  display: block;
  width: 18px;
  height: 2px;
  margin: 2px 0;
  border-radius: 99px;
  background: currentColor;
}

.settings-icon span {
  width: 18px;
  height: 18px;
  margin: 0;
  border: 2px solid currentColor;
  border-radius: 50%;
  background: radial-gradient(circle at center, currentColor 0 2px, transparent 3px);
}

.plain-icon {
  font-size: 28px;
  line-height: 1;
}

.quick-actions {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 254, 253, 0.88);
  scrollbar-width: none;
}

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

.quick-actions button {
  flex: 0 0 auto;
  max-width: 58vw;
  min-height: 34px;
  padding: 7px 10px;
  overflow: hidden;
  color: var(--accent-strong);
  background: #e4f4f1;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 12px 18px;
  overscroll-behavior: contain;
}

.chat-panel.has-chat-background .messages {
  background: transparent;
}

.empty {
  display: grid;
  place-items: center;
  min-height: 160px;
  text-align: center;
}

.message {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: fit-content;
  max-width: min(92%, 720px);
  margin: 0 0 14px;
}

.message.user {
  align-items: flex-end;
  margin-left: auto;
}

.message-meta {
  max-width: 100%;
  margin: 0 0 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-panel.has-chat-background .message-meta {
  width: max-content;
  max-width: 100%;
  padding: 3px 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 999px;
  color: rgba(255, 255, 255, 0.92);
  background: rgba(31, 37, 40, 0.38);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(8px) saturate(1.05);
}

.chat-panel.has-chat-background .message.user .message-meta {
  color: rgba(255, 255, 255, 0.94);
  background: rgba(15, 139, 141, 0.38);
}

.message-body {
  width: fit-content;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  padding: 11px 13px;
  border: 1px solid rgba(220, 228, 222, 0.94);
  border-radius: 8px;
  background: rgba(255, 254, 253, 0.94);
  line-height: 1.6;
}

.chat-panel.has-chat-background .message-body {
  border-color: rgba(255, 255, 255, 0.52);
  background: rgba(255, 255, 255, 0.58);
  box-shadow: 0 8px 24px rgba(31, 37, 40, 0.08);
  backdrop-filter: blur(8px) saturate(1.08);
}

.message.user .message-body {
  color: #fff;
  border-color: transparent;
  background: var(--accent);
}

.chat-panel.has-chat-background .message.user .message-body {
  color: #fff;
  background: rgba(15, 139, 141, 0.72);
  box-shadow: 0 8px 24px rgba(15, 139, 141, 0.18);
}

.message.system .message-body {
  color: #61410f;
  background: #fff7df;
}

.chat-panel.has-chat-background .message.system .message-body {
  background: rgba(255, 247, 223, 0.72);
}

.message.failed .message-body {
  border-color: rgba(178, 76, 99, 0.4);
}

.image-message-body {
  display: grid;
  gap: 8px;
}

.media-caption {
  font-size: 13px;
}

.generated-image {
  display: block;
  width: min(100%, 320px);
  height: auto;
  border-radius: 8px;
  object-fit: contain;
}

.voice-message-body {
  display: grid;
  gap: 8px;
}

.voice-bubble {
  display: inline-grid;
  grid-template-columns: auto auto auto;
  gap: 8px;
  align-items: center;
  width: max-content;
  min-height: 38px;
  padding: 8px 12px;
  color: var(--accent-strong);
  background: #e4f4f1;
}

.voice-bubble.playing {
  color: #fff;
  background: var(--accent);
}

.voice-wave {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}

.voice-wave i {
  display: block;
  width: 3px;
  height: 12px;
  border-radius: 99px;
  background: currentColor;
}

.voice-wave i:nth-child(2) {
  height: 18px;
}

.voice-transcript {
  max-width: min(72vw, 320px);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.message-actions {
  display: flex;
  gap: 8px;
  align-self: flex-start;
  margin-top: 8px;
}

.regenerate-button {
  min-height: 30px;
  padding: 5px 10px;
  border: 1px solid rgba(15, 139, 141, 0.26);
  border-radius: 8px;
  color: var(--accent-strong);
  background: #e4f4f1;
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
}

.regenerate-button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.message-delete {
  position: absolute;
  top: 18px;
  right: 7px;
  min-height: 28px;
  padding: 4px 8px;
  opacity: 0;
  pointer-events: none;
  color: var(--danger);
  background: #fae8ec;
  font-size: 12px;
}

.message.user .message-delete {
  right: auto;
  left: 7px;
}

.message:hover .message-delete,
.message:focus-within .message-delete {
  opacity: 1;
  pointer-events: auto;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 72px;
  gap: 8px;
  padding: 10px 10px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(255, 254, 253, 0.95);
  backdrop-filter: blur(14px);
}

.chat-panel.has-chat-background .composer,
.chat-panel.has-chat-background .topbar,
.chat-panel.has-chat-background .quick-actions {
  background: rgba(255, 254, 253, 0.72);
  backdrop-filter: blur(12px) saturate(1.08);
}

.chat-panel.has-chat-background .composer textarea {
  background: rgba(255, 255, 255, 0.64);
}

.composer textarea {
  width: 100%;
  min-height: 44px;
  max-height: 148px;
  resize: none;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  color: var(--ink);
  background: #fff;
  line-height: 1.45;
}

.composer textarea:focus,
.config-form input:focus,
.config-form textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 139, 141, 0.12);
}

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  width: min(86vw, 380px);
  height: 100dvh;
  overflow: hidden;
  background: var(--panel);
  box-shadow: var(--shadow);
  transition: transform 0.2s ease;
}

.drawer-left {
  left: 0;
  transform: translateX(-102%);
}

.drawer-right {
  right: 0;
  display: block;
  overflow-y: auto;
  overflow-x: hidden;
  transform: translateX(102%);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 14px 12px;
  border-bottom: 1px solid var(--line);
}

.drawer-header h2 {
  font-size: 20px;
}

.drawer-actions,
.button-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.drawer button:not(.plain-icon),
.drawer .import-button {
  min-height: 36px;
  padding: 0 10px;
  font-size: 14px;
  font-weight: 750;
  line-height: 1.15;
}

.drawer .button-row button,
.drawer .button-row .import-button {
  width: 100%;
}

.button-row > [hidden] {
  display: none;
}

.drawer-actions {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.secondary,
.import-button {
  color: var(--accent-strong);
  background: #e4f4f1;
}

.danger {
  color: var(--danger);
  background: #fae8ec;
}

.full {
  width: 100%;
}

.agent-list {
  display: grid;
  align-content: start;
  gap: 8px;
  overflow-y: auto;
  padding: 12px;
}

.agent-card {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 60px;
  padding: 10px;
  border: 1px solid transparent;
  color: var(--ink);
  background: var(--panel-soft);
  text-align: left;
}

.agent-card.active {
  border-color: rgba(15, 139, 141, 0.35);
  background: #e4f4f1;
}

.agent-card strong,
.agent-card small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.agent-card strong {
  font-size: 14px;
}

.panel-section {
  display: grid;
  gap: 10px;
  flex: 0 0 auto;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.drawer-right .panel-section {
  display: block;
  min-height: max-content;
}

.drawer-right .panel-section > * + * {
  margin-top: 10px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.section-title h3 {
  min-width: 0;
  font-size: 15px;
  line-height: 1.35;
}

.section-title span {
  flex: 0 0 auto;
  max-width: 46%;
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toggle-row,
.range-row,
.file-control {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.toggle-row {
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: start;
  color: var(--ink);
  line-height: 1.45;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  margin: 2px 0 0;
  flex: 0 0 auto;
}

.toggle-row span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.range-row {
  grid-template-columns: 68px minmax(0, 1fr);
  align-items: center;
}

.range-row input {
  width: 100%;
}

.file-control input {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  font-size: 13px;
}

.background-preview {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-height: 64px;
  padding: 8px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: var(--panel-soft);
  font-size: 13px;
}

.background-preview img {
  width: 64px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
}

.background-preview span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.config-form {
  display: grid;
  gap: 10px;
}

.config-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.config-form .config-field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.config-form input,
.config-form textarea,
.config-form select {
  width: 100%;
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  color: var(--ink);
  background: #fff;
  line-height: 1.45;
}

.config-form textarea {
  min-height: 44px;
  resize: vertical;
}

.auth-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.68);
}

.auth-tabs button {
  min-height: 34px;
  padding: 0 8px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  white-space: nowrap;
}

.auth-tabs button.active {
  color: var(--accent-strong);
  background: #e4f4f1;
}

.inline-input-action {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 76px;
  gap: 8px;
  align-items: center;
}

.inline-input-action input {
  min-width: 0;
}

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

.text-action {
  width: auto;
  min-height: auto;
  justify-self: start;
  padding: 0;
  color: var(--accent-strong);
  background: transparent;
  font-size: 12px;
}

.license-form {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
}

.compact-form {
  gap: 8px;
}

.reference-upload {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
}

.reference-preview {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-height: 64px;
  padding: 8px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: #fff;
  font-size: 13px;
}

.reference-preview img {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  object-fit: cover;
}

.reference-preview span,
.memory-item span {
  overflow-wrap: anywhere;
}

.memory-list {
  display: grid;
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
}

.memory-item {
  display: grid;
  gap: 4px;
  padding: 9px 10px;
  border-radius: 8px;
  color: #3c4246;
  background: var(--panel-soft);
  font-size: 13px;
  line-height: 1.45;
}

.memory-item strong {
  color: var(--accent-strong);
  font-size: 12px;
}

.import-button {
  display: grid;
  place-items: center;
  min-height: 40px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
}

.import-button input {
  display: none;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: rgba(31, 37, 40, 0.28);
}

@media (max-width: 720px) {
  .drawer {
    width: min(92vw, 390px);
  }

  .drawer-header {
    padding: max(12px, env(safe-area-inset-top)) 12px 10px;
  }

  .drawer-header h2 {
    font-size: 18px;
  }

  .drawer-actions {
    padding: 10px 12px;
  }

  .panel-section {
    gap: 8px;
    padding: 12px;
  }

  .drawer-right .panel-section > * + * {
    margin-top: 8px;
  }

  .config-form {
    gap: 8px;
  }

  .config-form label,
  .config-form .config-field {
    gap: 5px;
    font-size: 12px;
  }

  .config-form input,
  .config-form textarea,
  .config-form select {
    padding: 8px 9px;
  }

  .button-row {
    gap: 7px;
  }

  .drawer button:not(.plain-icon),
  .drawer .import-button {
    min-height: 34px;
    border-radius: 7px;
    font-size: 13px;
  }

  .auth-tabs button {
    min-height: 30px;
    font-size: 12px;
  }
}

@media (min-width: 721px) {
  body {
    background: #edf2ef;
  }

  .app-shell {
    display: grid;
    place-items: center;
  }

  .chat-panel {
    width: min(430px, 100vw);
    height: min(860px, 100dvh);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
  }
}
