:root {
  color-scheme: light;
  --bg: #f8fafd;
  --sidebar: #eef3fb;
  --surface: #ffffff;
  --surface-strong: #edf2fa;
  --ink: #1f1f1f;
  --muted: #5f6368;
  --line: #dfe5ef;
  --accent: #1a73e8;
  --accent-strong: #0b57d0;
  --accent-soft: #e8f0fe;
  --danger: #b3261e;
  --shadow: 0 16px 40px rgba(60, 64, 67, 0.12);
  font-family: "Google Sans", "Google Sans Text", "Product Sans", Roboto, Arial, sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --sidebar: #171a21;
  --surface: #1c2029;
  --surface-strong: #252a35;
  --ink: #e8eaed;
  --muted: #a8adb7;
  --line: #333946;
  --accent: #8ab4f8;
  --accent-strong: #aecbfa;
  --accent-soft: #1f2f4a;
  --danger: #f28b82;
  --shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
}

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

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  min-height: 40px;
  padding: 0 14px;
  transition:
    background-color 160ms ease,
    border-color 160ms ease,
    color 160ms ease,
    transform 160ms ease;
}

button:hover:not(:disabled) {
  background: var(--accent-strong);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.app-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  height: 100dvh;
  max-height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  gap: 14px;
  min-height: 0;
  padding: 18px 14px;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
}

.sidebar-top,
.chat-header,
.composer-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  line-height: 1.1;
}

h1 {
  font-size: 1.55rem;
}

h2 {
  font-size: 1.25rem;
}

/* 可编辑标题 */
#chatTitle {
  cursor: text;
  border-radius: 6px;
  padding: 2px 6px;
  margin: 0 -6px;
  transition: background 160ms ease;
  outline: none;
}

#chatTitle:hover {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

#chatTitle[contenteditable="true"] {
  background: var(--accent-soft);
  box-shadow: 0 0 0 2px var(--accent);
  white-space: nowrap;
  overflow: hidden;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--muted) 18%, transparent);
  flex: 0 0 auto;
}

.status-dot.is-busy {
  background: var(--danger);
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--danger) 18%, transparent);
}

.status-dot.is-ready {
  background: var(--accent);
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent) 18%, transparent);
}

.new-chat {
  width: 100%;
  justify-content: flex-start;
  background: var(--accent-soft);
  color: var(--accent-strong);
  padding: 0 18px;
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 2px 4px 4px;
}

.conversation-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  border-radius: 18px;
}

.conversation-item {
  justify-content: flex-start;
  width: 100%;
  min-height: 36px;
  overflow: hidden;
  border: 1px solid transparent;
  border-radius: 18px;
  background: transparent;
  color: var(--ink);
  font-weight: 600;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
  transform: none !important;
}

.conversation-item:hover,
.conversation-row:hover .conversation-item {
  border-color: transparent;
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(60, 64, 67, 0.14);
  transform: none;
}

.conversation-row.is-active .conversation-item {
  border-color: transparent;
  background: var(--accent-soft);
  color: var(--accent-strong);
  box-shadow: none;
  font-weight: 700;
}

:root[data-theme="dark"] .conversation-row.is-active .conversation-item {
  background: color-mix(in srgb, var(--accent) 22%, var(--surface));
  border-color: transparent;
  color: var(--accent-strong);
  box-shadow: none;
}

.conversation-row.is-running .conversation-item::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 8px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: middle;
}

.conversation-delete {
  width: 32px;
  min-width: 32px;
  height: 32px;
  min-height: 32px;
  margin-left: 4px;
  border-color: transparent;
  background: transparent;
  color: var(--danger);
  opacity: 0;
  padding: 0;
  pointer-events: none;
  transition:
    opacity 160ms ease,
    background-color 160ms ease,
    transform 160ms ease;
}

.conversation-row:hover .conversation-delete {
  opacity: 1;
  pointer-events: auto;
}

.conversation-delete:hover:not(:disabled) {
  background: color-mix(in srgb, var(--danger) 18%, var(--surface)) !important;
  color: var(--danger) !important;
  transform: scale(1.1);
}

.settings {
  display: grid;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  outline: none;
  padding: 10px 11px;
}

textarea {
  line-height: 1.5;
  resize: vertical;
}

#systemInput {
  resize: none;
}

select {
  min-width: 0;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

input[type="range"] {
  accent-color: var(--accent);
  padding: 0;
}

.sidebar-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.sidebar-actions button,
#stopButton,
.icon-button {
  border-color: var(--line);
  background: var(--surface);
  color: var(--ink);
}

#stopButton.is-danger {
  border-color: color-mix(in srgb, var(--danger) 38%, var(--line));
  background: color-mix(in srgb, var(--danger) 16%, var(--surface));
  color: var(--danger);
}

#stopButton.is-danger:hover:not(:disabled) {
  background: var(--danger);
  color: #fff;
}

.sidebar-actions button:hover:not(:disabled),
#stopButton:hover:not(:disabled),
.icon-button:hover:not(:disabled) {
  background: color-mix(in srgb, var(--surface-strong) 82%, var(--accent));
}

.icon-button.is-active,
.icon-button[aria-pressed="true"] {
  border-color: color-mix(in srgb, var(--accent) 42%, var(--line));
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.chat-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

.chat-header {
  border-bottom: 1px solid var(--line);
  padding: 16px 22px;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(14px);
}

.chat-title-wrap {
  min-width: 0;
  flex: 1;
}

/* header 中的标题完整展示，不省略 */
.chat-header #chatTitle {
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
  word-break: break-word;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 22px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  color: var(--muted);
  font-weight: 700;
}

.message {
  flex: 0 0 auto;
  width: min(820px, 100%);
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.message.user {
  align-self: flex-end;
  background: var(--accent-soft);
}

.message.assistant,
.message.error {
  align-self: flex-start;
}

.message.error {
  border-color: color-mix(in srgb, var(--danger) 50%, var(--line));
}

.message-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  overflow-wrap: anywhere;
  padding: 9px 12px;
}

.message-edit {
  min-height: 28px;
  border-color: var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.74rem;
  padding: 0 10px;
  opacity: 0;
  transition: opacity 160ms ease;
}

.message.user:hover .message-edit {
  opacity: 1;
}

.message-edit:hover {
  color: var(--accent);
}

/* 行内编辑状态 */
.message.is-editing .message-body textarea.inline-edit {
  width: 100%;
  border: none;
  border-top: 1px solid var(--accent);
  border-radius: 0;
  background: color-mix(in srgb, var(--accent-soft) 60%, var(--surface));
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.68;
  min-height: 80px;
  padding: 12px 16px;
  resize: vertical;
  box-shadow: none;
  outline: none;
}

.message.is-editing .message-body textarea.inline-edit:focus {
  border-color: var(--accent);
  box-shadow: none;
}

.inline-edit-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 8px 12px 10px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.inline-edit-actions .btn-update {
  min-height: 32px;
  font-size: 0.82rem;
  padding: 0 14px;
  background: var(--accent);
  color: #fff;
}

.inline-edit-actions .btn-cancel {
  min-height: 32px;
  font-size: 0.82rem;
  padding: 0 14px;
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
}

.inline-edit-actions .btn-cancel:hover:not(:disabled) {
  background: var(--surface-strong);
  color: var(--ink);
}

.message-body {
  line-height: 1.68;
  overflow-wrap: anywhere;
  padding: 14px 16px 16px;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4,
.message-body h5,
.message-body h6 {
  margin: 0 0 10px;
  line-height: 1.25;
}

.message-body h1 {
  font-size: 1.24rem;
}

.message-body h2 {
  font-size: 1.16rem;
}

.message-body h3 {
  font-size: 1.08rem;
}

.message-body h4 {
  font-size: 1rem;
}

.message-body p {
  margin: 0 0 12px;
}

.message-body p:last-child,
.message-body ul:last-child,
.message-body ol:last-child,
.message-body pre:last-child {
  margin-bottom: 0;
}

.message-body ul,
.message-body ol {
  margin: 0 0 12px;
  padding-left: 22px;
}

.code-block {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #0f172a;
  margin: 0 0 12px;
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  color: #cbd5e1;
  font-family: "Google Sans Mono", "Roboto Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.74rem;
  font-weight: 700;
  padding: 8px 12px;
  text-transform: uppercase;
}

.message-body pre {
  overflow-x: auto;
  background: transparent;
  margin: 0;
  padding: 14px;
}

.message-body code {
  border-radius: 5px;
  background: var(--surface-strong);
  font-family: "Google Sans Mono", "Roboto Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.9em;
  padding: 2px 5px;
}

.message-body pre code {
  display: block;
  background: transparent;
  color: #e5e7eb;
  line-height: 1.6;
  padding: 0;
}

.code-keyword {
  color: #93c5fd;
  font-weight: 700;
}

.code-string {
  color: #86efac;
}

.code-comment {
  color: #94a3b8;
  font-style: italic;
}

.code-number {
  color: #fbbf24;
}

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  margin: 0 0 12px;
}

.message-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.message-body th,
.message-body td {
  border: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  vertical-align: top;
}

.message-body th {
  background: var(--surface-strong);
  font-weight: 800;
}

.citation {
  margin-left: 2px;
  font-size: 0.72em;
  line-height: 0;
  vertical-align: super;
}

.citation a {
  color: var(--accent);
  text-decoration: none;
}

.citation a:hover {
  text-decoration: underline;
}

.source-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.source-chip {
  max-width: 320px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-strong);
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  padding: 7px 10px;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.source-chip:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
  background: var(--accent-soft);
}

.message-text {
  white-space: pre-wrap;
}

.composer {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(14px);
  padding: 14px 22px 18px;
}

.composer-main {
  align-items: end;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 0 4px 16px rgba(60, 64, 67, 0.08);
  padding: 8px;
}

.icon-button {
  min-width: 86px;
  padding: 0;
}

.composer textarea {
  border: 0;
  box-shadow: none;
  min-height: 42px;
  max-height: 180px;
  resize: none;
}

.composer textarea:focus {
  border-color: transparent;
  box-shadow: none;
}

#sendButton {
  min-width: 82px;
}

.attachment-row,
.file-card-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.file-card {
  position: relative;
  display: inline-grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  max-width: 320px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-strong);
  color: var(--ink);
  padding: 8px 10px;
}

.file-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--surface);
  color: var(--accent);
  font-size: 0.68rem;
  font-weight: 900;
}

.file-info {
  display: grid;
  min-width: 0;
  gap: 3px;
}

.file-name,
.file-size {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-name {
  font-size: 0.86rem;
  font-weight: 700;
}

.file-size {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 700;
}

.file-remove {
  display: grid;
  place-items: center;
  width: 26px;
  min-width: 26px;
  height: 26px;
  min-height: 26px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--muted);
  padding: 0;
}

@media (max-width: 860px) {
  body {
    overflow: hidden;
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(240px, 38dvh) minmax(0, 1fr);
    height: 100dvh;
    min-height: 0;
  }

  .sidebar {
    grid-template-rows: auto auto auto auto auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow-y: auto;
  }

  .conversation-list {
    max-height: 180px;
  }

  .chat-pane {
    height: auto;
    min-height: 0;
  }

  .composer-main {
    align-items: stretch;
  }
}
