:root {
  --brand: #1677ff;
  --brand-strong: #0d65d9;
  --brand-soft: #eaf3ff;
  --success: #18a058;
  --text: #17233d;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --surface: #ffffff;
  --canvas: #f4f7fb;
  --line: #e6ebf2;
  --danger: #d9363e;
  --shadow-soft: 0 8px 24px rgba(31, 56, 88, 0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family:
    Inter, "SF Pro Text", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: #edf1f7;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  display: flex;
  justify-content: center;
  background: #edf1f7;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid rgba(22, 119, 255, 0.24);
  outline-offset: 2px;
}

.app-shell {
  position: relative;
  display: flex;
  width: min(100%, 480px);
  height: 100dvh;
  min-height: 100%;
  flex-direction: column;
  overflow: hidden;
  background: var(--canvas);
  box-shadow: 0 0 40px rgba(38, 55, 77, 0.12);
}

.topbar {
  position: relative;
  z-index: 20;
  display: grid;
  min-height: calc(62px + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 12px 0;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.97);
}

.icon-button {
  display: inline-grid;
  width: 40px;
  height: 40px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}

.icon-button:hover {
  background: #f3f6fa;
}

.icon-button:active {
  background: #eaf0f7;
}

.icon-button svg {
  width: 22px;
  height: 22px;
  stroke-width: 2;
}

.topbar-identity {
  min-width: 0;
  text-align: center;
}

.topbar-title {
  display: block;
  overflow: hidden;
  color: #101828;
  font-size: 16px;
  font-weight: 650;
  line-height: 22px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-status {
  display: inline-flex;
  max-width: 100%;
  align-items: center;
  gap: 5px;
  color: var(--text-secondary);
  font-size: 11px;
  line-height: 16px;
}

.topbar-status > :last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-dot {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px rgba(24, 160, 88, 0.1);
}

.menu-wrap {
  position: relative;
  display: flex;
  justify-content: flex-end;
}

.overflow-menu {
  position: absolute;
  top: 44px;
  right: 0;
  width: 148px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow-soft);
}

.menu-item {
  display: flex;
  width: 100%;
  min-height: 40px;
  padding: 0 10px;
  align-items: center;
  gap: 9px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: #334155;
  font-size: 14px;
  cursor: pointer;
}

.menu-item:hover,
.menu-item:focus-visible {
  background: #f3f6fa;
}

.menu-item svg {
  width: 17px;
  height: 17px;
}

.conversation {
  position: relative;
  min-height: 0;
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px 14px 12px;
  scroll-behavior: smooth;
  scrollbar-color: #cbd5e1 transparent;
  scrollbar-width: thin;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.system-state {
  display: flex;
  min-height: 180px;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
  color: var(--text-secondary);
  font-size: 14px;
  text-align: center;
}

.system-state[hidden] {
  display: none;
}

.system-state p {
  max-width: 280px;
  margin: 0;
  line-height: 1.6;
}

.loading-spinner {
  width: 25px;
  height: 25px;
  border: 2px solid #dbe7f5;
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.error-icon {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 50%;
  background: #fff0f0;
  color: var(--danger);
}

.error-icon svg {
  width: 21px;
  height: 21px;
}

.retry-button {
  min-height: 38px;
  padding: 0 18px;
  border: 1px solid #bfd7f7;
  border-radius: 5px;
  background: var(--surface);
  color: var(--brand-strong);
  font-weight: 600;
  cursor: pointer;
}

.message-group {
  display: flex;
  width: 100%;
  align-items: flex-start;
  gap: 9px;
}

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

.assistant-avatar {
  position: relative;
  display: grid;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  overflow: hidden;
  place-items: center;
  border: 1px solid rgba(22, 119, 255, 0.12);
  border-radius: 6px;
  background: var(--brand);
  box-shadow: 0 4px 12px rgba(22, 119, 255, 0.18);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}

.assistant-avatar img {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.message-column {
  display: flex;
  width: min(100% - 45px, 386px);
  min-width: 0;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
}

.user .message-column {
  width: auto;
  max-width: min(82%, 350px);
  align-items: flex-end;
}

.sender-name {
  padding-left: 2px;
  color: var(--text-secondary);
  font-size: 12px;
  line-height: 15px;
}

.bubble-stack {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
}

.bubble {
  min-width: 0;
  max-width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 4px 12px 12px 12px;
  background: var(--surface);
  box-shadow: 0 3px 10px rgba(37, 58, 86, 0.045);
  color: var(--text);
  font-size: 16px;
  line-height: 1.68;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.user .bubble {
  border-color: var(--brand);
  border-radius: 12px 4px 12px 12px;
  background: var(--brand);
  color: #fff;
  box-shadow: 0 5px 14px rgba(22, 119, 255, 0.18);
}

.link-bubble {
  width: 100%;
  padding: 12px;
  white-space: normal;
}

.link-description {
  margin-bottom: 10px;
  white-space: pre-wrap;
}

.link-url {
  display: flex;
  min-height: 38px;
  padding: 8px 10px;
  align-items: center;
  gap: 8px;
  border: 1px solid #d9e1ec;
  border-radius: 5px;
  background: #f7f9fc;
  color: #38506f;
  font-size: 13px;
  line-height: 19px;
  overflow-wrap: anywhere;
}

.link-url svg {
  width: 15px;
  height: 15px;
  flex: 0 0 auto;
  color: var(--text-muted);
}

.link-action {
  display: flex;
  width: 100%;
  min-height: 40px;
  margin-top: 9px;
  padding: 0 12px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 0;
  border-radius: 5px;
  background: var(--brand);
  color: #fff;
  font-size: 14px;
  font-weight: 650;
  cursor: pointer;
}

.link-action:hover {
  background: var(--brand-strong);
}

.link-action:disabled {
  background: #a8c9f3;
  cursor: not-allowed;
}

.link-action svg {
  width: 16px;
  height: 16px;
}

.option-list {
  display: grid;
  width: 100%;
  margin-top: 2px;
  gap: 8px;
}

.option-button {
  display: grid;
  width: 100%;
  min-height: 48px;
  padding: 7px 10px;
  grid-template-columns: minmax(30px, auto) minmax(0, 1fr) 18px;
  align-items: center;
  gap: 9px;
  border: 1px solid #cfe0f7;
  border-radius: 6px;
  background: var(--surface);
  color: #1e3b60;
  text-align: left;
  cursor: pointer;
  transition: border-color 140ms ease, background-color 140ms ease, transform 140ms ease;
}

.option-button:hover {
  border-color: #80b5f7;
  background: #f8fbff;
  transform: translateY(-1px);
}

.option-button:active {
  transform: translateY(0);
}

.option-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.option-keyword {
  display: grid;
  width: auto;
  min-width: 30px;
  max-width: 88px;
  height: 30px;
  padding: 0 8px;
  place-items: center;
  border-radius: 5px;
  background: var(--brand-soft);
  color: var(--brand-strong);
  font-size: 13px;
  font-weight: 750;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.option-label {
  min-width: 0;
  font-size: 14px;
  line-height: 20px;
  overflow-wrap: anywhere;
}

.option-button > svg {
  width: 17px;
  height: 17px;
  color: #8ca2bd;
}

.typing-bubble {
  display: flex;
  height: 42px;
  padding: 0 15px;
  align-items: center;
  gap: 5px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8ca2bd;
  animation: typing 1.2s ease-in-out infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 120ms;
}

.typing-dot:nth-child(3) {
  animation-delay: 240ms;
}

.footer-note {
  padding: 6px 16px;
  border-top: 1px solid rgba(230, 235, 242, 0.85);
  background: #f8fafc;
  color: var(--text-muted);
  font-size: 11px;
  line-height: 16px;
  text-align: center;
  overflow-wrap: anywhere;
}

.composer {
  display: grid;
  min-height: calc(66px + var(--safe-bottom));
  padding: 11px 12px calc(11px + var(--safe-bottom));
  grid-template-columns: minmax(0, 1fr) 44px;
  align-items: center;
  gap: 9px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.composer input {
  width: 100%;
  height: 43px;
  padding: 0 13px;
  border: 1px solid #dbe2ea;
  border-radius: 6px;
  background: #f7f9fc;
  color: var(--text);
  font-size: 16px;
}

.composer input::placeholder {
  color: #9aa8b8;
}

.composer input:focus {
  border-color: #7fb5f8;
  background: #fff;
}

.composer input:disabled {
  color: #9aa8b8;
  cursor: not-allowed;
}

.send-button {
  display: grid;
  width: 43px;
  height: 43px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
}

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

.send-button:disabled {
  background: #c7d3e0;
  cursor: not-allowed;
}

.send-button svg {
  width: 20px;
  height: 20px;
  stroke-width: 2.4;
}

.scroll-latest {
  position: absolute;
  right: 16px;
  bottom: calc(78px + var(--safe-bottom));
  z-index: 12;
  display: grid;
  width: 40px;
  height: 40px;
  padding: 0;
  place-items: center;
  border: 1px solid #d5e1ef;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 6px 18px rgba(31, 56, 88, 0.16);
  color: var(--brand-strong);
  cursor: pointer;
  animation: latest-in 150ms ease-out;
}

.app-shell.has-footer .scroll-latest {
  bottom: calc(104px + var(--safe-bottom));
}

.scroll-latest[hidden] {
  display: none;
}

.scroll-latest:hover {
  border-color: #aacbf4;
  background: #f8fbff;
}

.scroll-latest svg {
  width: 19px;
  height: 19px;
  stroke-width: 2.2;
}

.toast {
  position: fixed;
  z-index: 100;
  bottom: calc(84px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  max-width: min(calc(100% - 32px), 420px);
  padding: 10px 14px;
  transform: translateX(-50%);
  border-radius: 5px;
  background: rgba(20, 31, 48, 0.94);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.2);
  color: #fff;
  font-size: 13px;
  line-height: 19px;
  text-align: center;
  overflow-wrap: anywhere;
  animation: toast-in 160ms ease-out;
}

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

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

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

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

@keyframes toast-in {
  from {
    transform: translate(-50%, 5px);
    opacity: 0;
  }
}

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

@media (max-width: 520px) {
  .app-shell {
    width: 100%;
    box-shadow: none;
  }

  .conversation {
    padding-right: 12px;
    padding-left: 12px;
  }
}

@media (min-width: 700px) {
  .app-shell {
    height: min(100dvh, 900px);
    margin: auto 0;
    border-right: 1px solid var(--line);
    border-left: 1px solid var(--line);
  }
}

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