:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #242421;
  background: #f7f7f5;
  color-scheme: light;
  font-synthesis: none;
  --surface: #ffffff;
  --surface-subtle: #f2f2ef;
  --border: #deded9;
  --border-strong: #cecec8;
  --text: #242421;
  --muted: #777771;
  --faint: #9a9a93;
  --green: #2f8f5b;
  --amber: #b86f16;
  --red: #c7464f;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }

body {
  margin: 0;
  padding-bottom: calc(142px + env(safe-area-inset-bottom));
  background: #f7f7f5;
}

button, textarea, select { font: inherit; }
button, select, textarea { -webkit-tap-highlight-color: transparent; }

.shell {
  width: min(100%, 760px);
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px 32px;
}

.topbar {
  position: sticky;
  z-index: 30;
  top: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 66px;
  padding: 10px 0;
  background: rgba(247, 247, 245, 0.92);
  border-bottom: 1px solid rgba(222, 222, 217, 0.75);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.thread-picker { min-width: 0; }
.topbar-meta { display: flex; align-items: center; gap: 6px; }

.link-test-result {
  padding: 3px 7px;
  border: 1px solid currentColor;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  white-space: nowrap;
}

.link-test-result.success { color: var(--green); background: #f2f8f4; }
.link-test-result.failure { color: var(--red); background: #fff5f5; }

select {
  width: 100%;
  height: 44px;
  padding: 0 38px 0 13px;
  border: 1px solid var(--border);
  border-radius: 15px;
  outline: none;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 20, 18, 0.04);
  font-size: 14px;
  text-overflow: ellipsis;
}

select:focus { border-color: #a8a8a1; }
select:disabled { color: var(--muted); background: var(--surface-subtle); }

.connection {
  min-width: 0;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}

.refresh-button,
.message-collapse {
  border: 1px solid var(--border);
  color: #4f4f4a;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 20, 18, 0.04);
}

.refresh-button {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  aspect-ratio: 1;
}

.icon-button svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.refresh-button[aria-busy="true"] svg { animation: spin 0.8s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; transform: none; }

.output-card {
  margin-top: 20px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 20, 18, 0.03);
}

.dialogue-card {
  margin-top: 24px;
  padding: 0 2px;
}

.section-heading,
.message-meta-row,
.composer-status,
.composer-input-row {
  display: flex;
  align-items: center;
}

.section-heading {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 13px;
}

.section-label {
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.section-heading .section-label { margin-bottom: 0; }

.dnd-badge {
  padding: 3px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--faint);
  background: var(--surface-subtle);
  font-size: 10px;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: #343430;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.62;
}

.dialogue-list { display: grid; gap: 18px; }
.message-row { display: flex; width: 100%; }
.message-row.user { justify-content: flex-end; }
.message-row.assistant { justify-content: flex-start; }

.message-bubble {
  max-width: min(90%, 630px);
  padding: 0;
  border: 0;
  background: transparent;
}

.message-row.user .message-bubble {
  padding: 11px 13px;
  border: 1px solid #e1e1dc;
  border-radius: 19px 19px 7px 19px;
  background: #ededeb;
}

.message-bubble.pending {
  border-color: #e3c89f;
  background: #f4eee4;
}

.message-meta-row {
  gap: 10px;
  min-height: 26px;
}

.message-meta {
  margin-right: auto;
  color: var(--faint);
  font-size: 11px;
}

.message-collapse {
  width: 28px;
  height: 28px;
  min-width: 28px;
  padding: 0;
  border-radius: 50%;
}

.message-collapse svg { width: 15px; height: 15px; }

.message-body {
  margin-top: 7px;
  padding-top: 8px;
  border-top: 1px solid #e8e8e3;
}

.message-row.user .message-body { border-top-color: #dbdbd6; }
.message-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 15px;
  line-height: 1.58;
}

.message-text a,
#latest-output a {
  color: #315fbd;
  text-decoration: underline;
  text-decoration-color: rgba(49, 95, 189, 0.42);
  text-underline-offset: 2px;
  overflow-wrap: anywhere;
}

.message-text a:active,
#latest-output a:active { opacity: 0.66; }
.dialogue-empty { margin: 0; color: var(--muted); font-size: 14px; }

.composer {
  position: fixed;
  z-index: 40;
  left: 0;
  right: 0;
  bottom: 0;
  width: min(100%, 760px);
  margin: 0 auto;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, rgba(247, 247, 245, 0), #f7f7f5 22px);
  pointer-events: none;
}

.composer-glass {
  padding: 9px;
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  background: var(--surface);
  box-shadow: 0 8px 28px rgba(26, 26, 23, 0.1), 0 1px 2px rgba(26, 26, 23, 0.06);
  pointer-events: auto;
}

.composer[data-mode="waiting"] .composer-glass,
.composer[data-mode="sending"] .composer-glass { border-color: #d8c29f; }
.composer[data-mode="ready"] .composer-glass { border-color: #242421; }
.composer[data-mode="offline"] .composer-glass { border-color: #dbb4b8; }

.composer-status {
  gap: 7px;
  min-height: 23px;
  padding: 0 5px 6px;
}

.composer-state-dot {
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--faint);
}

.composer-state-dot.ready { background: var(--green); }
.composer-state-dot.waiting,
.composer-state-dot.sending {
  background: var(--amber);
  animation: pulse 1.55s ease-in-out infinite;
}
.composer-state-dot.layout { background: #8a8a84; }
.composer-state-dot.offline { background: var(--red); }

@keyframes pulse { 50% { opacity: 0.35; } }

.composer-status strong {
  color: #4c4c47;
  font-size: 12px;
  font-weight: 620;
}

.composer-hint {
  min-width: 0;
  margin-left: auto;
  overflow: hidden;
  color: var(--faint);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.composer-notice {
  margin: 0 2px 7px;
  padding: 8px 10px;
  border: 1px solid #e1b9bd;
  border-radius: 14px;
  color: #9f3942;
  background: #fff6f6;
  font-size: 12px;
  line-height: 1.45;
}

.composer-input-row { gap: 8px; }

.attachment-preview {
  padding: 2px 4px 8px;
}

.attachment-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 148px));
  gap: 8px;
  margin-top: 10px;
}

.attachment-grid.draft {
  display: flex;
  gap: 8px;
  margin-top: 0;
  overflow-x: auto;
}

.attachment-item {
  position: relative;
  min-width: 76px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface-subtle);
  aspect-ratio: 1.25;
}

.attachment-item img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  cursor: zoom-in;
  opacity: 0;
  transition: opacity 160ms ease;
}

.attachment-item.loaded img { opacity: 1; }

.attachment-item.loading {
  isolation: isolate;
  background: linear-gradient(135deg, #ededeb 0%, #f6f6f4 48%, #e8e8e5 100%);
}

.attachment-item.loading::before {
  content: "";
  position: absolute;
  z-index: 1;
  inset: 0;
  background: linear-gradient(105deg, transparent 24%, rgba(255, 255, 255, 0.72) 45%, transparent 66%);
  transform: translateX(-110%);
  animation: attachment-shimmer 1.35s ease-in-out infinite;
}

.attachment-item.loading::after,
.attachment-item.failed::after,
.attachment-item:not(.loaded):not(.loading):not(.failed)::after {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 10px;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

.attachment-item.loading::after {
  content: "";
  z-index: 2;
  inset: 50% auto auto 50%;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 2px solid rgba(24, 24, 22, 0.14);
  border-top-color: rgba(24, 24, 22, 0.72);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  animation: attachment-spin 720ms linear infinite;
}

@keyframes attachment-shimmer {
  0% { transform: translateX(-110%); }
  58%, 100% { transform: translateX(110%); }
}

@keyframes attachment-spin {
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

.attachment-item:not(.loaded):not(.loading):not(.failed)::after {
  content: "点击加载图片";
  cursor: pointer;
}

.image-preview-overlay {
  position: fixed;
  z-index: 1000;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  border: 0;
  border-radius: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.88);
  backdrop-filter: blur(18px);
  cursor: zoom-out;
}

.image-preview-overlay img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 18px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.38);
}

.attachment-item.failed::after {
  content: "加载失败，点击重试";
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .attachment-item.loading::before,
  .attachment-item.loading::after { animation: none; }
}

.attachment-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  min-width: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #fff;
  background: rgba(20, 20, 18, 0.72);
  font-size: 18px;
  line-height: 1;
}

.attach-button {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  min-width: 38px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  color: #4f4f4a;
  background: var(--surface-subtle);
}

textarea {
  width: 100%;
  min-height: 44px;
  max-height: 140px;
  resize: none;
  padding: 11px 6px 10px;
  border: 0;
  border-radius: 13px;
  outline: none;
  color: var(--text);
  background: transparent;
  caret-color: #1f1f1d;
  line-height: 1.4;
}

textarea::placeholder { color: #a0a09a; }
textarea:disabled { color: #9a9a94; background: #f6f6f3; }

.composer #send-button {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  min-width: 42px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: white;
  background: #242421;
  font-size: 13px;
  font-weight: 650;
}

.composer #send-button:disabled { color: #a5a59f; background: #e8e8e4; }

.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 (min-width: 700px) {
  .shell { padding-inline: 24px; }
  .composer { padding-inline: 20px; }
}

@media (max-width: 430px) {
  .shell { padding-inline: 10px; }
  .topbar { gap: 7px; }
  .topbar-meta { gap: 4px; }
  .connection { display: none; }
  .output-card { margin-top: 14px; padding: 14px; }
  .dialogue-card { margin-top: 20px; }
  .composer { padding-inline: 7px; }
  .composer-hint { max-width: 58%; }
}

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