/* Apple + ChatGPT 设计系统 */
:root {
  --primary: #0066cc;
  --primary-focus: #0071e3;
  --primary-on-dark: #2997ff;
  --ink: #1d1d1f;
  --body-muted: #86868b;
  --canvas: #ffffff;
  --canvas-parchment: #f5f5f7;
  --surface-pearl: #fafafc;
  --surface-tile: #272729;
  --hairline: rgba(0, 0, 0, 0.08);
  --shadow-product: rgba(0, 0, 0, 0.22) 3px 5px 30px 0;
  --glass: rgba(255, 255, 255, 0.72);
  --glass-border: rgba(255, 255, 255, 0.5);
  --sidebar-w: 280px;
  --topbar-h: 52px;
  --composer-min: 120px;
  --radius-pill: 9999px;
  --radius-lg: 18px;
  --radius-md: 11px;
  --radius-sm: 8px;
  --spring: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --user-bubble: #0071e3;
  --assistant-bubble: var(--canvas);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --ink: #f5f5f7;
  --body-muted: #98989d;
  --canvas: #1c1c1e;
  --canvas-parchment: #000000;
  --surface-pearl: #2c2c2e;
  --hairline: rgba(255, 255, 255, 0.1);
  --glass: rgba(28, 28, 30, 0.82);
  --glass-border: rgba(255, 255, 255, 0.08);
  --assistant-bubble: #2c2c2e;
  --user-bubble: #0a84ff;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] {
  display: none !important;
}

html {
  font: 17px/1.47 system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  color: var(--ink);
  background: var(--canvas-parchment);
  overflow: hidden;
  height: 100dvh;
}

#three-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: 0.45;
}

[data-theme="dark"] #three-bg { opacity: 0.25; }

.icon {
  width: 20px;
  height: 20px;
  display: block;
  flex-shrink: 0;
}

[data-theme="dark"] .icon { filter: invert(1); opacity: 0.9; }
.btn-send .icon, .btn-new-chat .icon { filter: none !important; opacity: 1 !important; }
.btn-delete-chat .icon, .session-delete .icon, .btn-close-sidebar .icon {
  opacity: 1 !important;
}

.app-shell {
  position: relative;
  z-index: 1;
  display: flex;
  height: 100dvh;
}

.app-shell.is-sidebar-closed .sidebar {
  width: 0;
  min-width: 0;
  overflow: hidden;
  border-right: none;
  opacity: 0;
  pointer-events: none;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-right: 1px solid var(--hairline);
  display: flex;
  flex-direction: column;
  transition: transform 0.35s var(--spring);
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--hairline);
}

.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-pill);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 100ms ease-out;
}

.btn-new-chat:active { transform: scale(0.95); }
.btn-new-chat .icon { filter: brightness(0) invert(1); }

.btn-close-sidebar {
  display: flex;
  flex-shrink: 0;
}

.btn-delete-chat:hover {
  background: rgba(229, 72, 77, 0.12);
}

.btn-delete-chat:hover .icon {
  filter: brightness(0) saturate(100%) invert(27%) sepia(85%) saturate(2000%) hue-rotate(340deg) !important;
}

.sidebar-nav {
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--hairline);
}

.nav-tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--body-muted);
  font-size: 12px;
  cursor: pointer;
  transition: background 150ms, color 150ms;
}

.nav-tab.active {
  background: var(--surface-pearl);
  color: var(--ink);
  font-weight: 600;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}

.session-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 14px;
  color: var(--ink);
  transition: background 150ms;
  border: none;
  background: transparent;
  min-width: 0;
  text-align: left;
}

.session-row {
  display: flex;
  align-items: center;
  gap: 2px;
  border-radius: var(--radius-md);
  padding-right: 4px;
}

.session-row:hover { background: var(--surface-pearl); }
.session-row.active { background: var(--surface-pearl); }
.session-row.active .session-item { font-weight: 600; }

.session-delete {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.72;
  transition: opacity 150ms, background 150ms, transform 100ms;
}

.session-row:hover .session-delete,
.session-row:focus-within .session-delete {
  opacity: 1;
}

.session-delete:hover {
  opacity: 1 !important;
  background: rgba(229, 72, 77, 0.14);
}

.session-delete:active { transform: scale(0.92); }
.session-delete .icon {
  width: 18px;
  height: 18px;
  opacity: 1;
}

.session-delete:hover .icon {
  filter: brightness(0) saturate(100%) invert(27%) sepia(85%) saturate(2000%) hue-rotate(340deg) !important;
}

.session-item:hover { background: transparent; }
.session-item.active { background: transparent; font-weight: inherit; }

.session-item .icon { opacity: 0.5; width: 16px; height: 16px; }
.session-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-title { flex: 1; min-width: 0; }

.sidebar-footer {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--hairline);
}

.sidebar-copyright {
  padding: 0 12px 12px;
  font-size: 11px;
  color: var(--body-muted);
  text-align: center;
  line-height: 1.4;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: var(--surface-pearl);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform 100ms ease-out, background 150ms;
}

.btn-icon:active { transform: scale(0.95); }

/* ── Main ── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--canvas);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--topbar-h);
  padding: 0 16px;
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--hairline);
  flex-shrink: 0;
}

.btn-menu { display: none; }

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

.topbar-title h1 {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-brand { width: 22px; height: 22px; opacity: 0.8; }

.model-badge {
  font-size: 11px;
  padding: 3px 8px;
  background: var(--surface-pearl);
  border-radius: var(--radius-pill);
  color: var(--body-muted);
  flex-shrink: 0;
}

.app-shell.is-sidebar-closed .btn-menu {
  display: flex;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  margin-left: auto;
}

.view { display: none; flex: 1; flex-direction: column; min-height: 0; }
.view.active { display: flex; }

.gallery-item img.is-broken,
.msg-img-wrap img.is-broken,
.gen-reveal-wrap img.is-broken {
  object-fit: contain;
  background: var(--surface-pearl);
  padding: 24px;
  opacity: 0.45;
}

/* ── Messages ── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.messages-bottom {
  height: 0;
  width: 100%;
  flex-shrink: 0;
  pointer-events: none;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 50vh;
  text-align: center;
  padding: 32px;
  animation: fadeIn 0.6s var(--spring);
}

.empty-icon-wrap {
  width: 72px;
  height: 72px;
  background: var(--surface-pearl);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.empty-icon { width: 36px; height: 36px; opacity: 0.6; }

.empty-state h2 {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
}

.empty-state p {
  color: var(--body-muted);
  font-size: 15px;
  max-width: 360px;
}

.empty-tagline {
  margin-top: 10px;
  font-size: 13px;
  color: var(--body-muted);
  opacity: 0.85;
}

.quick-templates {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 24px;
}

.template-chip {
  padding: 8px 16px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  font-size: 13px;
  cursor: pointer;
  transition: transform 100ms, background 150ms, border-color 150ms;
  font-family: inherit;
  color: var(--ink);
}

.template-chip.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.template-chip:active { transform: scale(0.95); }
.template-chip:hover:not(.active) { background: var(--canvas-parchment); }

.msg {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  max-width: 820px;
  animation: msgIn 0.4s var(--spring);
}

.msg.user { margin-left: auto; flex-direction: row-reverse; }

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--surface-pearl);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.msg-avatar .icon { width: 16px; height: 16px; opacity: 0.6; }

.msg-body { flex: 1; min-width: 0; position: relative; }

.msg-hover-bar {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
  margin-bottom: 6px;
  opacity: 0;
  transition: opacity 150ms;
}

.msg.user .msg-hover-bar { justify-content: flex-start; }

.msg:hover .msg-hover-bar,
.msg.is-editing .msg-hover-bar {
  opacity: 1;
}

.msg-hover-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: var(--surface-pearl);
  cursor: pointer;
  transition: background 150ms, transform 100ms;
}

.msg-hover-btn:hover { background: var(--canvas-parchment); }
.msg-hover-btn:active { transform: scale(0.92); }
.msg-hover-btn .icon { width: 14px; height: 14px; opacity: 0.75; }
.msg-hover-btn-danger:hover { background: rgba(229, 72, 77, 0.12); }
.msg-hover-btn-danger:hover .icon {
  filter: brightness(0) saturate(100%) invert(27%) sepia(85%) saturate(2000%) hue-rotate(340deg);
  opacity: 1;
}

.msg-edit-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-edit-input {
  width: 100%;
  min-height: 72px;
  padding: 12px 14px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  font-size: 15px;
  line-height: 1.5;
  resize: vertical;
  font-family: inherit;
  background: var(--surface, #fff);
  color: var(--ink);
}

.msg-edit-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.12);
}

.msg-edit-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

[data-theme="dark"] .msg-edit-input {
  background: #2c2c2e;
  border-color: rgba(255, 255, 255, 0.12);
  color: #f5f5f7;
}

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant .msg-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--hairline);
  border-bottom-left-radius: 4px;
}

.msg-failed .msg-error-bubble {
  background: color-mix(in srgb, var(--danger, #dc3545) 8%, var(--assistant-bubble));
  border-color: color-mix(in srgb, var(--danger, #dc3545) 35%, var(--hairline));
  color: var(--text-primary);
}

.msg-failed .msg-error-bubble::before {
  content: "⚠ ";
  opacity: 0.85;
}

.msg-images {
  display: flex;
  width: max-content;
  max-width: 100%;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 8px;
  margin-top: 8px;
}

.msg-img-wrap {
  position: relative;
  display: inline-flex;
  width: fit-content;
  max-width: 100%;
  align-self: flex-start;
  border-radius: var(--radius-md);
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow-product);
  transition: transform 200ms var(--spring);
}

.msg-img-wrap:hover { transform: scale(1.02); }

.msg-generating .msg-images {
  display: inline-flex;
  width: fit-content;
  max-width: 100%;
  margin-top: 0;
}

.msg-img-wrap-generating {
  width: var(--gen-preview-fixed-width, var(--gen-preview-width, 100%));
  max-width: min(100%, var(--gen-preview-fixed-width, 100%));
  cursor: default;
  background: transparent;
  box-shadow: none;
}

.gen-placeholder-surface {
  position: relative;
  width: 100%;
  aspect-ratio: var(--gen-preview-aspect, 16 / 9);
  overflow: hidden;
  border-radius: inherit;
  background: linear-gradient(
    135deg,
    var(--surface-pearl) 0%,
    var(--canvas-parchment) 45%,
    var(--surface-pearl) 100%
  );
  box-shadow: var(--shadow-product);
}

.msg-img-wrap-generating:hover {
  transform: none;
}

.msg-img-wrap-generating .gen-center {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.msg-img-wrap img {
  display: block;
  max-width: 100%;
  max-height: 480px;
  width: auto;
  height: auto;
  object-fit: contain;
}

.msg-meta,
.msg-time {
  display: block;
  font-size: 11px;
  color: var(--body-muted);
  margin-top: 6px;
}

.msg-time {
  opacity: 0.78;
}

.msg.user .msg-time {
  text-align: right;
}

.msg-actions {
  display: flex;
  clear: both;
  gap: 4px;
  margin-top: 6px;
}

.msg-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  font-size: 12px;
  background: transparent;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--body-muted);
  cursor: pointer;
  transition: background 150ms;
}

.msg-action:hover { background: var(--surface-pearl); }
.msg-action .icon { width: 14px; height: 14px; }

/* ── Progress ── */
.progress-bar {
  position: relative;
  height: 3px;
  background: var(--surface-pearl);
  flex-shrink: 0;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary-focus));
  width: 0%;
  transition: width 0.6s var(--spring);
  box-shadow: 0 0 8px rgba(0, 102, 204, 0.35);
}

.progress-text {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  color: var(--body-muted);
  white-space: nowrap;
}

/* ── 图片生成中（ChatGPT 风格） ── */
.msg-generating .msg-bubble,
.msg.assistant.msg-generating .msg-bubble {
  padding: 0;
  border: none;
  background: transparent;
}

.msg-generating .gen-card-inner {
  background: transparent;
  box-shadow: none;
}

.gen-card {
  position: relative;
  --gen-max-height: 480px;
  width: var(--gen-preview-width, 100%);
  max-width: 100%;
}

.gen-card-inner {
  position: relative;
  width: 100%;
  aspect-ratio: var(--gen-preview-aspect, 16 / 9);
  overflow: hidden;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-product);
  background: linear-gradient(
    135deg,
    var(--surface-pearl) 0%,
    var(--canvas-parchment) 45%,
    var(--surface-pearl) 100%
  );
}

[data-theme="dark"] .gen-card-inner {
  background: linear-gradient(135deg, #1c1c1e 0%, #2c2c2e 50%, #1c1c1e 100%);
}

[data-theme="dark"] .msg-generating .gen-card-inner {
  background: transparent;
}

[data-theme="dark"] .gen-placeholder-surface {
  background: linear-gradient(135deg, #1c1c1e 0%, #2c2c2e 50%, #1c1c1e 100%);
}

.gen-shimmer {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    105deg,
    transparent 40%,
    rgba(255, 255, 255, 0.12) 45%,
    rgba(255, 255, 255, 0.28) 50%,
    rgba(255, 255, 255, 0.12) 55%,
    transparent 60%
  );
  background-size: 200% 100%;
  animation: genShimmer 2.2s ease-in-out infinite;
}

[data-theme="dark"] .gen-shimmer {
  background: linear-gradient(
    105deg,
    transparent 40%,
    rgba(255, 255, 255, 0.04) 45%,
    rgba(255, 255, 255, 0.1) 50%,
    rgba(255, 255, 255, 0.04) 55%,
    transparent 60%
  );
  background-size: 200% 100%;
}

.gen-noise {
  position: absolute;
  inset: 0;
  opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.5'/%3E%3C/svg%3E");
  animation: genNoise 0.8s steps(4) infinite;
  mix-blend-mode: overlay;
}

.gen-glow {
  position: absolute;
  inset: -20%;
  background: radial-gradient(
    ellipse at 50% 50%,
    rgba(0, 102, 204, 0.15) 0%,
    transparent 65%
  );
  animation: genPulse 2.5s ease-in-out infinite;
}

.gen-scan {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--primary), transparent);
  box-shadow: 0 0 12px var(--primary);
  animation: genScan 2.8s ease-in-out infinite;
  opacity: 0.7;
}

.gen-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 2;
  padding: 24px;
}

.gen-spinner {
  width: 36px;
  height: 36px;
  border: 2.5px solid rgba(0, 102, 204, 0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: genSpin 0.9s linear infinite;
}

.gen-status {
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  text-align: center;
  letter-spacing: -0.01em;
}

.gen-substatus {
  font-size: 12px;
  color: var(--body-muted);
  text-align: center;
}

.gen-progress-track {
  width: min(200px, 70%);
  height: 3px;
  background: rgba(0, 0, 0, 0.08);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 4px;
}

[data-theme="dark"] .gen-progress-track {
  background: rgba(255, 255, 255, 0.1);
}

.gen-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.6s var(--spring);
}

/* 生成完成 → 图片 reveal */
.gen-card.is-revealing {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}

.gen-card.is-revealing .gen-card-inner {
  aspect-ratio: auto;
  min-height: 0;
  max-height: none;
}

.gen-card.is-revealing .msg-images {
  display: flex;
  width: max-content;
  max-width: 100%;
  margin-top: 0;
  gap: 0;
}

.gen-reveal-wrap {
  position: relative;
  display: inline-flex;
  width: fit-content;
  max-width: 100%;
  align-self: flex-start;
  border-radius: var(--radius-md);
  overflow: hidden;
  opacity: 0;
  transform: scale(0.98);
  filter: blur(16px);
  animation: imageReveal 1.1s var(--spring) forwards;
}

.gen-reveal-wrap img {
  display: block;
  max-width: 100%;
  width: auto;
  height: auto;
  max-height: var(--gen-max-height, 480px);
  object-fit: contain;
}

.msg-img-wrap.is-revealing {
  opacity: 0;
  transform: scale(0.98);
  filter: blur(12px);
}

.msg-img-wrap.is-revealing.revealed {
  animation: imageReveal 0.9s var(--spring) forwards;
}

.msg-generating.is-done {
  animation: genDone 0.4s var(--spring);
}

.msg-generating.is-error .gen-card-inner {
  min-height: 120px;
}

.gen-error-text {
  color: #e5484d;
  font-size: 14px;
  text-align: center;
  padding: 8px;
}

@keyframes genShimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

@keyframes genNoise {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(-2%, 1%); }
  50% { transform: translate(1%, -2%); }
  75% { transform: translate(2%, 2%); }
}

@keyframes genPulse {
  0%, 100% { opacity: 0.5; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.05); }
}

@keyframes genScan {
  0% { top: 0; opacity: 0; }
  10% { opacity: 0.7; }
  90% { opacity: 0.7; }
  100% { top: 100%; opacity: 0; }
}

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

@keyframes imageReveal {
  0% {
    opacity: 0;
    transform: scale(0.96);
    filter: blur(20px);
  }
  60% {
    opacity: 1;
    filter: blur(4px);
  }
  100% {
    opacity: 1;
    transform: scale(1);
    filter: blur(0);
  }
}

@keyframes genDone {
  from { opacity: 0.85; }
  to { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .gen-shimmer, .gen-noise, .gen-glow, .gen-scan, .gen-spinner {
    animation: none !important;
  }
  .gen-reveal-wrap, .msg-img-wrap.is-revealing.revealed {
    animation: fadeIn 0.4s ease forwards !important;
    filter: none !important;
    transform: none !important;
  }
}

/* ── Composer ── */
.composer {
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-top: 1px solid var(--hairline);
  flex-shrink: 0;
}

.attachment-preview {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.attach-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--hairline);
}

.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }

.attach-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  background: rgba(0,0,0,0.6);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.attach-remove .icon { width: 12px; height: 12px; filter: invert(1); }

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  padding: 8px 12px;
}

.composer-row textarea {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font: inherit;
  color: var(--ink);
  max-height: 160px;
  outline: none;
  line-height: 1.4;
}

.composer-row textarea::placeholder { color: var(--body-muted); }

.btn-send {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 100ms, opacity 150ms;
  flex-shrink: 0;
}

.btn-send:active { transform: scale(0.95); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-send .icon { filter: brightness(0) invert(1); width: 18px; height: 18px; }

.composer-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chip-select {
  appearance: none;
  padding: 6px 12px;
  font-size: 12px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--ink);
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2386868b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
}

.chip-select:disabled,
.chip-select.is-disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.chip-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  font-size: 12px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--ink);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 150ms, border-color 150ms;
}
.chip-btn:hover { background: var(--surface); border-color: var(--ink-muted); }
.chip-btn .icon { width: 14px; height: 14px; opacity: 0.65; }

.stitch-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
}
.stitch-preview-grid {
  display: grid;
  gap: 3px;
  width: 72px;
  height: 48px;
  flex-shrink: 0;
}
.stitch-preview-cell {
  background: var(--canvas);
  border: 1px dashed var(--hairline);
  border-radius: 3px;
  font-size: 9px;
  color: var(--body-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.stitch-preview-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--body-muted);
}
.stitch-preview-meta .icon { width: 14px; height: 14px; opacity: 0.5; }

.session-generating-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: auto;
  flex-shrink: 0;
}
.session-generating-badge .icon {
  width: 14px;
  height: 14px;
  opacity: 0.7;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.btn-send.has-queue::after {
  content: attr(data-queue-count);
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  font-size: 10px;
  font-weight: 600;
  line-height: 16px;
  text-align: center;
  color: #fff;
  background: var(--accent, #0071e3);
  border-radius: 999px;
}
.btn-send { position: relative; }

.stitch-msg-grid {
  display: grid;
  gap: 4px;
  margin-top: 8px;
  max-width: 200px;
}
.stitch-msg-tile {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 4px 8px;
  font-size: 11px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  color: var(--body-muted);
}
.stitch-msg-tile button {
  padding: 2px 6px;
  font-size: 10px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  background: var(--canvas);
  color: var(--ink);
  cursor: pointer;
}
.stitch-msg-tile button:hover:not(:disabled) { border-color: var(--ink-muted); }
.stitch-msg-tile button:disabled { opacity: 0.4; cursor: not-allowed; }

.prompts-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(420px, calc(100vw - 32px));
  max-height: min(70vh, 520px);
  background: var(--canvas);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg, 0 16px 48px rgba(0,0,0,0.12));
  z-index: 150;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.prompts-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--hairline);
}
.prompts-header h3 { margin: 0; font-size: 15px; font-weight: 600; }
.prompts-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.prompt-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 150ms;
}
.prompt-item:hover { background: var(--surface-pearl); }
.prompt-item-body { flex: 1; min-width: 0; }
.prompt-item-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.prompt-item-content {
  font-size: 12px;
  color: var(--body-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.prompt-item-delete {
  flex-shrink: 0;
  padding: 4px;
  border: none;
  background: transparent;
  cursor: pointer;
  opacity: 0.4;
  border-radius: var(--radius-sm);
}
.prompt-item-delete:hover { opacity: 1; background: var(--surface-pearl); }
.prompt-item-delete .icon { width: 14px; height: 14px; }
.prompts-empty {
  text-align: center;
  padding: 32px 16px;
  color: var(--body-muted);
  font-size: 13px;
}

.chip-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 12px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--ink);
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  transition: background 150ms, border-color 150ms, color 150ms;
}

.chip-toggle input {
  width: 14px;
  height: 14px;
  margin: 0;
  accent-color: var(--primary);
  cursor: pointer;
}

.chip-toggle.is-active {
  background: rgba(0, 113, 227, 0.1);
  border-color: rgba(0, 113, 227, 0.35);
  color: var(--primary);
}

.chip-toggle.is-disabled {
  opacity: 0.45;
  pointer-events: none;
}

/* ── Gallery ── */
.gallery-grid {
  --gallery-gap: 12px;
  --gallery-row: 8px;
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  grid-auto-rows: var(--gallery-row);
  gap: var(--gallery-gap);
  align-items: start;
  -webkit-overflow-scrolling: touch;
}

.gallery-empty {
  grid-column: 1 / -1;
  color: var(--body-muted);
  padding: 48px 16px;
  text-align: center;
}

.gallery-item {
  width: 100%;
  display: block;
  align-self: start;
  margin: 0;
  border-radius: var(--radius-md);
  overflow: hidden;
  cursor: pointer;
  position: relative;
  background: var(--surface-pearl);
  box-shadow: var(--shadow-product);
  transition: transform 200ms var(--spring), box-shadow 200ms var(--spring);
}

.gallery-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.14);
}

.gallery-item img {
  display: block;
  width: 100%;
  min-height: 80px;
  height: auto;
  object-fit: contain;
}

.gallery-item-info {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 28px 8px 8px;
  background: linear-gradient(transparent, rgba(0,0,0,0.66));
  color: #fff;
  font-size: 11px;
  line-height: 1.3;
  pointer-events: none;
}

.gallery-item-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-sm);
  background: rgba(0, 0, 0, 0.55);
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 150ms, background 150ms, transform 100ms;
  touch-action: manipulation;
}

.gallery-item-delete .icon {
  width: 16px;
  height: 16px;
  filter: brightness(0) invert(1);
  opacity: 1 !important;
}

.gallery-item-delete:hover,
.gallery-item-delete:focus-visible {
  opacity: 1;
  background: rgba(229, 72, 77, 0.85);
}

.gallery-item-delete:active { transform: scale(0.92); }

@media (hover: none) {
  .gallery-item-delete { opacity: 1; }
}

.btn-pill-danger {
  color: #e5484d;
  border-color: rgba(229, 72, 77, 0.35);
}

.btn-pill-danger:hover {
  background: rgba(229, 72, 77, 0.1);
}

/* ── Stats ── */
.stats-cards {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  -webkit-overflow-scrolling: touch;
}

.stats-hero {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(220px, 280px);
  gap: 16px;
  align-items: stretch;
  padding: 22px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  background: rgba(250, 250, 252, 0.86);
  backdrop-filter: blur(20px) saturate(160%);
}

.stats-kicker {
  margin-bottom: 6px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
}

.stats-hero h2 {
  font-size: 28px;
  line-height: 1.12;
  font-weight: 650;
  letter-spacing: 0;
  margin-bottom: 8px;
}

.stats-subtitle {
  max-width: 560px;
  color: var(--body-muted);
  font-size: 14px;
  line-height: 1.55;
}

.stats-cost-card {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  padding: 18px;
  border-radius: var(--radius-md);
  background: var(--ink);
  color: #fff;
}

.stats-cost-label,
.stats-cost-note {
  font-size: 12px;
  opacity: 0.72;
}

.stats-cost-card strong {
  font-size: 34px;
  line-height: 1;
  font-weight: 650;
  letter-spacing: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.stat-card {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  min-height: 124px;
  background: var(--surface-pearl);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  padding: 18px;
}

.stat-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  background: var(--canvas-parchment);
}

.stat-icon .icon {
  width: 18px;
  height: 18px;
  opacity: 0.72;
}

.stat-value {
  font-size: 30px;
  line-height: 1;
  font-weight: 650;
  letter-spacing: 0;
  color: var(--ink);
}

.stat-label {
  font-size: 13px;
  color: var(--body-muted);
  margin-top: 6px;
}

.stat-detail {
  margin-top: 10px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--body-muted);
}

.stats-insights {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(220px, 0.8fr);
  gap: 12px;
}

.stats-panel {
  padding: 18px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  background: var(--surface-pearl);
}

.stats-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  color: var(--body-muted);
}

.stats-panel-head strong,
.stats-panel-compact strong {
  color: var(--ink);
  font-size: 24px;
  line-height: 1;
  font-weight: 650;
  letter-spacing: 0;
}

.stats-meter {
  height: 8px;
  overflow: hidden;
  border-radius: var(--radius-pill);
  background: var(--canvas-parchment);
  margin: 14px 0 10px;
}

.stats-meter div {
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
}

.stats-panel p {
  font-size: 12px;
  line-height: 1.45;
  color: var(--body-muted);
}

.stats-panel-compact {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
}

.stats-panel-compact span {
  color: var(--body-muted);
  font-size: 13px;
}

/* ── Lightbox ── */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadeIn 0.3s;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(255,255,255,0.15);
}

.lightbox-close .icon { filter: invert(1); }

.lightbox-content {
  max-width: 90vw;
  max-height: 70vh;
  overflow: auto;
}

.lightbox-content img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-product);
}

.lightbox-size {
  margin: 10px 0 0;
  text-align: center;
  font-size: 13px;
  color: var(--body-muted, rgba(255, 255, 255, 0.75));
}

.lightbox-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.btn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius-pill);
  font-size: 14px;
  cursor: pointer;
  transition: transform 100ms, background 150ms;
}

.btn-pill:active { transform: scale(0.95); }
.btn-pill .icon { filter: invert(1); width: 16px; height: 16px; }

/* ── Compare ── */
.compare-panel, .settings-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 90;
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(180%);
  border-top: 1px solid var(--hairline);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  max-height: 80vh;
  animation: slideUp 0.35s var(--spring);
}

.compare-header, .settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--hairline);
}

.compare-header h3, .settings-header h3 {
  font-size: 17px;
  font-weight: 600;
}

.compare-body {
  display: flex;
  gap: 0;
  padding: 16px;
  overflow: auto;
}

.compare-side {
  flex: 1;
  min-width: 0;
  text-align: center;
}

.compare-label {
  display: block;
  font-size: 12px;
  color: var(--body-muted);
  margin-bottom: 8px;
}

.compare-side img {
  max-width: 100%;
  max-height: 50vh;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-product);
}

.compare-divider {
  width: 1px;
  background: var(--hairline);
  margin: 0 12px;
  flex-shrink: 0;
}

.settings-body { padding: 20px; }
.settings-note { font-size: 14px; color: var(--body-muted); margin-bottom: 16px; }

.settings-about h4 {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 600;
}

.settings-brand-name {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
}

.settings-brand-tagline {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--body-muted);
  line-height: 1.45;
}

.settings-copyright {
  margin: 0;
  font-size: 12px;
  color: var(--body-muted);
}
.settings-dl { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; font-size: 14px; }
.settings-hr { border: none; border-top: 1px solid var(--hairline); margin: 16px 0; }
.settings-dl dt { color: var(--body-muted); }
.settings-dl code { font-size: 13px; background: var(--surface-pearl); padding: 2px 6px; border-radius: 4px; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.4);
  animation: fadeIn 0.2s;
}

.sidebar-overlay:not([hidden]) {
  display: block;
}

/* ── Drag overlay ── */
.drag-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(0, 102, 204, 0.12);
  border: 3px dashed var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: var(--primary);
  pointer-events: none;
}

/* ── Animations ── */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

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

/* ── Mobile ── */
@media (max-width: 834px) {
  /* 移动端用 .open 控制侧栏，不受桌面 is-sidebar-closed 影响 */
  .app-shell.is-sidebar-closed .sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    overflow: visible;
    border-right: 1px solid var(--hairline);
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 60;
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    transform: translateX(-100%);
    pointer-events: auto;
  }

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

  .session-row {
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
  }

  .session-item {
    min-height: 44px;
    touch-action: manipulation;
  }

  .btn-menu { display: flex; }

  /* 移动端 X 始终可见 */
  .btn-close-sidebar { display: flex !important; }

  .composer-toolbar { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .chip-select { flex-shrink: 0; }

  .compare-body { flex-direction: column; }
  .compare-divider { width: 100%; height: 1px; margin: 12px 0; }

  .stats-cards {
    padding: 12px;
    gap: 12px;
  }

  .stats-hero,
  .stats-insights {
    grid-template-columns: 1fr;
  }

  .stats-hero {
    padding: 18px;
  }

  .stats-hero h2 {
    font-size: 24px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .stat-card {
    min-height: 118px;
    padding: 14px;
    flex-direction: column;
    gap: 12px;
  }

  .stat-value {
    font-size: 28px;
  }

  .stats-cost-card {
    min-height: 112px;
  }

  .gallery-grid {
    --gallery-gap: 8px;
    --gallery-row: 6px;
    padding: 10px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--gallery-gap);
  }

  .gallery-item {
    border-radius: var(--radius-sm);
  }

  .gallery-item:hover {
    transform: none;
    box-shadow: var(--shadow-product);
  }

  .gallery-item-info {
    padding: 22px 7px 7px;
    font-size: 10px;
    opacity: 0.92;
  }

  .gallery-item-delete {
    top: 6px;
    right: 6px;
    width: 34px;
    height: 34px;
    opacity: 1;
  }

  .gallery-item-delete .icon {
    width: 15px;
    height: 15px;
  }

  .gallery-item:active {
    transform: scale(0.985);
  }

  .msg-img-wrap img { max-height: 320px; }

  .msg-img-wrap-generating {
    width: var(--gen-preview-fixed-width-mobile, var(--gen-preview-width-mobile, var(--gen-preview-width, 100%)));
    max-width: min(100%, var(--gen-preview-fixed-width-mobile, 100%));
  }

  .gen-card {
    --gen-max-height: 320px;
    width: var(--gen-preview-width-mobile, var(--gen-preview-width, 100%));
  }

}

@media (max-width: 480px) {
  .empty-state h2 { font-size: 22px; }
  .topbar-title h1 { font-size: 15px; }
  .model-badge { display: none; }

  .stats-hero,
  .stat-card,
  .stats-panel {
    border-radius: var(--radius-sm);
  }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
  }

  .stat-icon {
    width: 32px;
    height: 32px;
  }

  .stat-value {
    font-size: 26px;
  }

  .stats-cost-card strong {
    font-size: 30px;
  }

  .gallery-grid {
    --gallery-gap: 7px;
    padding: 8px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--gallery-gap);
  }

}

@media (max-width: 340px) {
  .gallery-grid {
    grid-template-columns: 1fr;
  }
}
