:root {
  color-scheme: light;
  --bg: #f7f7f4;
  --surface: #ffffff;
  --surface-soft: #efefeb;
  --surface-hover: #e7e8e2;
  --ink: #20221f;
  --muted: #70756d;
  --line: #dcded7;
  --accent: #186b4d;
  --accent-strong: #0d583e;
  --accent-ink: #ffffff;
  --danger: #a23930;
  --user-bg: #e5eee9;
  --shadow: 0 16px 44px rgba(31, 38, 33, 0.12);
  --sidebar-width: 274px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171816;
  --surface: #20221f;
  --surface-soft: #292c28;
  --surface-hover: #323630;
  --ink: #eff1eb;
  --muted: #a5aba1;
  --line: #383c36;
  --accent: #69bc91;
  --accent-strong: #83cda5;
  --accent-ink: #10251b;
  --danger: #e18a82;
  --user-bg: #294236;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.34);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #171816;
    --surface: #20221f;
    --surface-soft: #292c28;
    --surface-hover: #323630;
    --ink: #eff1eb;
    --muted: #a5aba1;
    --line: #383c36;
    --accent: #69bc91;
    --accent-strong: #83cda5;
    --accent-ink: #10251b;
    --danger: #e18a82;
    --user-bg: #294236;
    --shadow: 0 18px 48px rgba(0, 0, 0, 0.34);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
textarea {
  color: inherit;
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  width: 100%;
  height: 100dvh;
}

.sidebar {
  z-index: 20;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--surface-soft);
}

.sidebar-head,
.sidebar-foot {
  padding: 14px;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  color: var(--ink);
  font-size: 15px;
  font-weight: 700;
  text-decoration: none;
}

.brand img {
  flex: 0 0 auto;
  margin-right: 9px;
}

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

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

.icon-button svg,
.sidebar-action svg,
.message-action svg {
  width: 18px;
  height: 18px;
  stroke-width: 1.8;
}

.history-list {
  min-height: 0;
  padding: 4px 9px 16px;
  overflow: auto;
}

.history-empty {
  margin: 16px 10px;
  color: var(--muted);
  font-size: 13px;
}

.history-item {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px;
  align-items: center;
  width: 100%;
  min-height: 42px;
  margin: 2px 0;
  border-radius: 6px;
}

.history-item.active {
  background: var(--surface-hover);
}

.history-select {
  min-width: 0;
  height: 42px;
  padding: 0 8px 0 10px;
  overflow: hidden;
  border: 0;
  background: transparent;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-delete {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  opacity: 0;
}

.history-item:hover .history-delete,
.history-delete:focus-visible {
  opacity: 1;
}

.history-delete:hover {
  color: var(--danger);
  background: var(--surface);
}

.history-delete svg {
  width: 15px;
}

.sidebar-foot {
  border-top: 1px solid var(--line);
}

.sidebar-action {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
}

.sidebar-action:hover {
  background: var(--surface-hover);
}

.sidebar-action svg {
  margin-right: 10px;
}

.drawer-backdrop {
  display: none;
}

.chat-pane {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--surface);
}

.topbar {
  z-index: 5;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 36px;
  align-items: center;
  min-height: 62px;
  padding: 8px max(18px, env(safe-area-inset-right)) 8px max(18px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 93%, transparent);
  backdrop-filter: blur(14px);
}

.model-identity {
  grid-column: 2;
  display: flex;
  align-items: center;
  justify-self: center;
  min-width: 0;
}

.model-picker-control {
  position: relative;
  min-width: 0;
}

.model-menu-button {
  display: inline-flex;
  height: 36px;
  max-width: min(240px, 48vw);
  align-items: center;
  gap: 5px;
  padding: 0 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
}

.model-menu-button:hover,
.model-menu-button[aria-expanded="true"] {
  background: var(--surface-soft);
}

.model-menu-button strong {
  min-width: 0;
  overflow: hidden;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-menu-button svg {
  width: 15px;
  height: 15px;
  flex: 0 0 15px;
  color: var(--muted);
}

.model-menu {
  position: absolute;
  z-index: 40;
  top: 42px;
  left: 50%;
  width: min(246px, calc(100vw - 24px));
  max-height: min(520px, calc(100dvh - 88px));
  padding: 8px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transform: translateX(-50%);
}

.model-menu[hidden] {
  display: none;
}

.model-menu-label {
  padding: 7px 9px 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
}

.model-menu-options {
  display: grid;
  gap: 1px;
}

.model-menu-option {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 18px;
  align-items: center;
  min-height: 36px;
  padding: 0 9px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  text-align: left;
}

.model-menu-option:hover,
.model-menu-option.active {
  background: var(--surface-soft);
}

.model-menu-option svg {
  display: none;
  width: 15px;
  height: 15px;
  color: var(--accent);
}

.model-menu-option.active svg {
  display: block;
}

.model-menu-separator {
  height: 1px;
  margin: 7px 3px;
  background: var(--line);
}

.connection-label {
  display: inline-flex;
  align-items: center;
  margin-left: 10px;
  color: var(--muted);
  font-size: 12px;
}

.status-dot {
  width: 7px;
  height: 7px;
  margin-right: 5px;
  border-radius: 50%;
  background: #a39b67;
}

.status-dot.online {
  background: #2b8c61;
}

.status-dot.offline {
  background: var(--danger);
}

.mobile-only {
  visibility: hidden;
}

.conversation {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
}

.empty-state {
  display: flex;
  min-height: 100%;
  max-width: 740px;
  margin: 0 auto;
  padding: 12vh 28px 48px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

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

.empty-state h1 {
  margin: 0;
  font-family: "Songti SC", "STSong", "Noto Serif CJK SC", serif;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 0;
}

.message-list {
  width: min(100%, 820px);
  margin: 0 auto;
  padding: 42px 28px 64px;
}

.message {
  display: flex;
  margin: 0 0 30px;
  flex-direction: column;
}

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

.message.user .message-content {
  max-width: min(82%, 650px);
  padding: 11px 15px;
  border-radius: 8px 8px 2px 8px;
  background: var(--user-bg);
  white-space: pre-wrap;
}

.message.assistant {
  align-items: flex-start;
}

.message.assistant .message-content {
  width: 100%;
  font-family: "Songti SC", "STSong", "Noto Serif CJK SC", serif;
  font-size: 17px;
  line-height: 1.82;
}

.response-progress {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  gap: 7px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 12px;
}

.response-progress-spinner {
  width: 15px;
  height: 15px;
  animation: response-spin 900ms linear infinite;
}

.response-elapsed {
  padding-left: 7px;
  border-left: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}

.response-duration {
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.message-sources {
  width: min(100%, 720px);
  margin-top: 14px;
  padding-top: 11px;
  border-top: 1px solid var(--line);
  font-size: 13px;
}

.message-sources-heading {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 7px;
  color: var(--muted);
  font-weight: 600;
}

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

.message-sources ul {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.message-sources li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 12px;
  min-width: 0;
}

.message-sources a {
  min-width: 0;
  overflow-wrap: anywhere;
  color: var(--accent);
  text-decoration: none;
}

.message-sources a:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

.message-sources li > span {
  max-width: 210px;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.source-issue {
  border-left-color: var(--warning, #9a6a17);
  background: color-mix(in srgb, var(--warning, #9a6a17) 8%, transparent);
  color: var(--warning, #79530f);
}

.response-issue {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: start;
  gap: 8px;
  width: min(100%, 720px);
  margin-top: 12px;
  padding: 10px 12px;
  border-left: 3px solid var(--danger);
  background: color-mix(in srgb, var(--danger) 8%, transparent);
  color: var(--danger);
  font-size: 13px;
  line-height: 1.55;
}

.response-issue svg {
  width: 16px;
  height: 16px;
  margin-top: 2px;
}

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

.message-content > :first-child {
  margin-top: 0;
}

.message-content > :last-child {
  margin-bottom: 0;
}

.message-content p,
.message-content ul,
.message-content ol,
.message-content blockquote {
  margin: 0 0 1em;
}

.message-content h1,
.message-content h2,
.message-content h3 {
  margin: 1.4em 0 0.55em;
  font-size: 1.12em;
  line-height: 1.45;
}

.message-content blockquote {
  padding-left: 15px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.message-content a {
  color: var(--accent);
  text-underline-offset: 3px;
}

.message-content code {
  padding: 0.12em 0.35em;
  border-radius: 4px;
  background: var(--surface-soft);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 0.86em;
}

.message-content pre {
  max-width: 100%;
  padding: 14px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
}

.message-content pre code {
  padding: 0;
  background: transparent;
}

.message-actions {
  height: 30px;
  margin-top: 6px;
}

.message-action {
  display: inline-grid;
  width: 30px;
  height: 30px;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
}

.message-action:hover {
  background: var(--surface-soft);
  color: var(--ink);
}

.streaming-cursor::after {
  display: inline-block;
  width: 2px;
  height: 1em;
  margin-left: 3px;
  background: var(--accent);
  content: "";
  vertical-align: -0.12em;
  animation: blink 0.9s steps(2) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.composer-wrap {
  z-index: 6;
  padding: 10px max(22px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
  background: linear-gradient(to bottom, transparent, var(--surface) 18px);
}

.conversation-nav {
  display: flex;
  width: min(100%, 790px);
  margin: 0 auto 7px;
  justify-content: flex-end;
  gap: 6px;
}

.conversation-nav[hidden] {
  display: none;
}

.conversation-nav-button {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--muted);
  box-shadow: 0 3px 14px rgba(29, 36, 31, 0.08);
}

.conversation-nav-button:hover:not(:disabled) {
  background: var(--surface-soft);
  color: var(--ink);
}

.conversation-nav-button:disabled {
  cursor: default;
  opacity: 0.38;
}

.conversation-nav-button svg {
  width: 17px;
  height: 17px;
  stroke-width: 1.9;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  align-items: end;
  width: min(100%, 790px);
  min-height: 54px;
  margin: 0 auto;
  padding: 8px 8px 8px 17px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 4px 22px rgba(29, 36, 31, 0.08);
}

.composer:focus-within {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
  box-shadow: 0 4px 24px rgba(24, 107, 77, 0.1);
}

.composer textarea {
  width: 100%;
  max-height: 160px;
  min-height: 36px;
  padding: 7px 8px 5px 0;
  resize: none;
  overflow-y: auto;
  border: 0;
  outline: 0;
  background: transparent;
  line-height: 1.5;
}

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

.send-button {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-ink);
}

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

.send-button:disabled {
  cursor: default;
  opacity: 0.42;
}

.send-button svg {
  width: 19px;
  height: 19px;
  stroke-width: 2.2;
}

.composer-note {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

dialog {
  max-width: calc(100vw - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(17, 20, 18, 0.52);
  backdrop-filter: blur(3px);
}

.login-dialog {
  width: 390px;
}

.login-dialog[open] {
  animation: dialog-in 160ms ease-out;
}

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

.login-panel {
  display: flex;
  padding: 34px 34px 32px;
  flex-direction: column;
}

.login-panel > img {
  margin-bottom: 18px;
}

.login-panel h1 {
  margin: 0 0 28px;
  font-family: "Songti SC", "STSong", serif;
  font-size: 25px;
}

.login-panel label {
  margin: 12px 0 7px;
  font-size: 13px;
  font-weight: 600;
}

.text-field {
  width: 100%;
  height: 46px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 7px;
  outline: 0;
  background: transparent;
}

.text-field:focus {
  border-color: var(--accent);
}

.password-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 40px;
  align-items: center;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 7px;
}

.password-field:focus-within {
  border-color: var(--accent);
}

.password-field input {
  width: 100%;
  height: 44px;
  min-width: 0;
  padding: 0 13px;
  border: 0;
  outline: 0;
  background: transparent;
}

.captcha-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 126px;
  gap: 8px;
  height: 46px;
}

.captcha-field > input {
  min-width: 0;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 7px;
  outline: 0;
  background: transparent;
}

.captcha-field > input:focus {
  border-color: var(--accent);
}

.captcha-field button {
  min-width: 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-soft);
}

.captcha-field img {
  display: block;
  width: 100%;
  height: 44px;
  object-fit: contain;
  object-position: center;
}

.captcha-hint {
  min-height: 17px;
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.access-panel p {
  margin: -12px 0 24px;
  color: var(--muted);
  line-height: 1.7;
}

.form-error {
  min-height: 21px;
  margin: 7px 0;
  color: var(--danger);
  font-size: 13px;
}

.primary-button {
  min-height: 44px;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 650;
}

.primary-button:disabled {
  opacity: 0.55;
}

.settings-dialog {
  width: min(520px, calc(100vw - 32px));
}

.settings-panel {
  padding: 22px;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.dialog-head h2 {
  margin: 0;
  font-size: 18px;
}

.setting-row {
  display: flex;
  min-height: 82px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-top: 1px solid var(--line);
}

.setting-row > div:first-child {
  display: flex;
  min-width: 0;
  flex-direction: column;
}

.setting-row strong {
  margin-bottom: 3px;
  font-size: 14px;
}

.setting-row span {
  color: var(--muted);
  font-size: 12px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-soft);
}

.segmented-control button {
  min-width: 46px;
  min-height: 30px;
  padding: 0 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  font-size: 12px;
}

.segmented-control button.active {
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.danger-button {
  min-width: 58px;
  min-height: 34px;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line));
  border-radius: 6px;
  background: transparent;
  color: var(--danger);
}

.danger-button:hover {
  background: color-mix(in srgb, var(--danger) 9%, transparent);
}

.export-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.secondary-button {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-size: 12px;
}

.secondary-button:hover:not(:disabled) {
  background: var(--surface-soft);
}

.secondary-button:disabled {
  cursor: default;
  opacity: 0.45;
}

.secondary-button svg {
  width: 15px;
  height: 15px;
}

.toast {
  position: fixed;
  z-index: 100;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  border-radius: 6px;
  background: var(--ink);
  color: var(--surface);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: opacity 150ms ease, transform 150ms ease;
}

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

@media (max-width: 760px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(84vw, 310px);
    padding-top: env(safe-area-inset-top);
    transform: translateX(-101%);
    transition: transform 180ms ease;
    box-shadow: var(--shadow);
  }

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

  .drawer-backdrop {
    position: fixed;
    z-index: 15;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 0;
    background: rgba(17, 20, 18, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

  body.drawer-open .drawer-backdrop {
    display: block;
    opacity: 1;
    pointer-events: auto;
  }

  .chat-pane {
    width: 100%;
    height: 100dvh;
  }

  .topbar {
    min-height: calc(56px + env(safe-area-inset-top));
    padding-top: calc(6px + env(safe-area-inset-top));
  }

  .mobile-only {
    visibility: visible;
  }

  .topbar .mobile-only:last-child {
    grid-column: 3;
  }

  .connection-label {
    margin-left: 8px;
  }

  .connection-label span:last-child {
    display: none;
  }

  .empty-state {
    justify-content: flex-start;
    padding: 14vh 20px 32px;
  }

  .empty-state h1 {
    font-size: 24px;
  }

  .message-list {
    padding: 28px 18px 50px;
  }

  .message {
    margin-bottom: 25px;
  }

  .message.user .message-content {
    max-width: 89%;
  }

  .message.assistant .message-content {
    font-size: 16px;
    line-height: 1.78;
  }

  .composer-wrap {
    padding: 8px 12px max(8px, env(safe-area-inset-bottom));
  }

  .conversation-nav {
    margin-bottom: 6px;
  }

  .composer-note {
    display: none;
  }

  .settings-panel {
    padding: 18px;
  }

  .setting-row {
    align-items: flex-start;
    padding: 16px 0;
    flex-direction: column;
    gap: 12px;
  }

  .segmented-control {
    width: 100%;
  }

  .destructive-row {
    flex-direction: row;
    align-items: center;
  }
}

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