:root {
  color-scheme: light dark;
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f3f4f6;
  --surface-3: #ececf1;
  --sidebar: #f9f9f9;
  --border: #e5e7eb;
  --text: #202123;
  --muted: #6b7280;
  --accent: #10a37f;
  --accent-dark: #0d8f70;
  --danger: #d92d20;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.14);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

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

button {
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 50% 8%, rgba(16, 163, 127, 0.14), transparent 32%),
    linear-gradient(180deg, #ffffff, #f4f4f5);
}

.login-card {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 32px;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark,
.welcome-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: #202123;
  font-weight: 750;
}

.brand-title {
  font-weight: 750;
}

.brand-subtitle {
  color: var(--muted);
  font-size: 13px;
}

.login-card h1 {
  margin: 28px 0 22px;
  font-size: 30px;
  letter-spacing: 0;
}

.login-form {
  display: grid;
  gap: 16px;
}

.login-form label {
  display: grid;
  gap: 8px;
  font-weight: 650;
}

.login-form input {
  height: 48px;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 0 14px;
  outline: none;
  background: var(--surface);
  color: var(--text);
}

.login-form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(16, 163, 127, 0.16);
}

.login-form button,
.new-chat-button,
.send-button {
  border: 0;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  height: 46px;
  font-weight: 700;
}

.login-form button:hover,
.send-button:hover,
.new-chat-button:hover {
  background: var(--accent-dark);
}

.form-message {
  min-height: 20px;
  color: var(--danger);
}

.app-view {
  height: 100vh;
  display: grid;
  grid-template-columns: 292px 1fr;
  overflow: hidden;
}

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sidebar-top {
  padding: 12px;
  display: flex;
  gap: 8px;
}

.new-chat-button {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: #202123;
}

.plus {
  font-size: 22px;
  line-height: 1;
}

.search-wrap {
  padding: 0 12px 10px;
}

.search-wrap input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0 12px;
  background: var(--surface);
  color: var(--text);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

.conversation-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px 30px;
  align-items: center;
  gap: 2px;
  border-radius: 10px;
  color: var(--text);
}

.conversation-item:hover,
.conversation-item.active {
  background: var(--surface-2);
}

.conversation-main,
.conversation-action {
  border: 0;
  background: transparent;
  color: inherit;
}

.conversation-main {
  min-width: 0;
  display: grid;
  gap: 3px;
  padding: 11px 6px 11px 12px;
  text-align: left;
}

.conversation-action {
  width: 30px;
  height: 30px;
  color: var(--muted);
  border-radius: 8px;
  opacity: 0;
}

.conversation-item:hover .conversation-action,
.conversation-item.active .conversation-action {
  opacity: 1;
}

.conversation-action:hover {
  background: var(--surface-3);
  color: var(--text);
}

.conversation-title,
.conversation-date {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.conversation-date {
  font-size: 12px;
  color: var(--muted);
}

.empty-list {
  color: var(--muted);
  padding: 16px 14px;
  font-size: 14px;
}

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

.profile-button {
  border: 0;
  background: transparent;
  color: var(--text);
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-width: 0;
  padding: 7px;
  border-radius: 12px;
  text-align: left;
}

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

.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

.user-avatar.large {
  width: 48px;
  height: 48px;
}

.profile-copy {
  min-width: 0;
}

.user-name,
.user-meta {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.user-meta {
  color: var(--muted);
  font-size: 12px;
}

.ghost-button,
.icon-button,
.stop-button,
.tool-button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  height: 38px;
}

.chat-shell {
  min-width: 0;
  display: grid;
  grid-template-rows: 58px 1fr auto;
  background: var(--surface);
}

.topbar {
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 12px;
  padding: 0 18px;
}

.model-controls {
  min-width: 0;
  justify-self: start;
}

.key-controls {
  min-width: 0;
  justify-self: end;
}

.model-select,
.key-select {
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  padding: 0 10px;
}

.model-select {
  min-width: 150px;
  max-width: min(240px, 30vw);
  font-weight: 650;
}

.key-select {
  min-width: 210px;
  max-width: min(280px, 30vw);
}

.connection-status {
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
  justify-self: end;
}

.messages {
  overflow-y: auto;
  padding: 28px max(24px, calc((100vw - 292px - 860px) / 2)) 28px;
}

.welcome {
  min-height: 58vh;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
}

.welcome h2 {
  font-size: 30px;
  margin: 22px 0 24px;
  letter-spacing: 0;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 220px));
  gap: 12px;
}

.suggestions button {
  min-height: 54px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-align: left;
  padding: 12px 14px;
}

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

.message {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 14px;
  margin: 0 auto 26px;
  max-width: 860px;
}

.message-avatar {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 750;
  background: #202123;
  color: #fff;
}

.message.user .message-avatar {
  background: var(--accent);
}

.message-content {
  line-height: 1.75;
  overflow-wrap: anywhere;
}

.message.assistant.streaming .message-content:empty::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 18px;
  border-radius: 8px;
  background: var(--muted);
  animation: blink 1s infinite;
}

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

.message-meta {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.message-content pre {
  overflow-x: auto;
  background: #111827;
  color: #e5e7eb;
  padding: 14px;
  border-radius: 10px;
}

.message-content code {
  background: var(--surface-2);
  padding: 2px 5px;
  border-radius: 6px;
}

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

.error-box {
  color: var(--danger);
  background: rgba(217, 45, 32, 0.08);
  border: 1px solid rgba(217, 45, 32, 0.22);
  border-radius: 12px;
  padding: 12px;
}

.composer-wrap {
  padding: 14px max(18px, calc((100vw - 292px - 860px) / 2)) 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0), var(--surface) 20%);
}

.composer {
  max-width: 860px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 8px;
  background: var(--surface);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.08);
}

.composer textarea {
  resize: none;
  border: 0;
  outline: none;
  min-height: 48px;
  max-height: 220px;
  padding: 12px 8px;
  background: transparent;
  color: var(--text);
}

.tool-button,
.send-button {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  align-self: end;
  font-size: 22px;
  line-height: 1;
}

.send-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.stop-button {
  width: 56px;
  align-self: end;
}

.composer-meta {
  max-width: 860px;
  margin: 8px auto 0;
  color: var(--muted);
  font-size: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
}

.composer-meta > :last-child {
  justify-self: end;
  text-align: right;
}

.composer-status {
  justify-self: center;
  color: var(--muted);
  min-height: 1em;
  text-align: center;
}

.composer-status.error {
  color: var(--danger);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.32);
}

.modal-panel {
  width: min(520px, 100%);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.modal-header {
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px 0 22px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 20px;
}

.profile-content {
  padding: 20px;
  display: grid;
  gap: 18px;
}

.profile-summary {
  display: flex;
  gap: 12px;
  align-items: center;
}

.profile-summary h3,
.profile-summary p {
  margin: 0;
}

.profile-summary p {
  color: var(--muted);
}

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

.stat-grid > div,
.key-row {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  padding: 12px;
}

.stat-grid span,
.key-row span {
  color: var(--muted);
  font-size: 13px;
}

.stat-grid strong {
  display: block;
  margin-top: 4px;
  font-size: 20px;
}

.key-list {
  display: grid;
  gap: 8px;
}

.key-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.key-row strong,
.key-row span {
  display: block;
}

.mobile-only {
  display: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171717;
    --surface: #212121;
    --surface-2: #2f2f2f;
    --surface-3: #3a3a3a;
    --sidebar: #171717;
    --border: #3f3f46;
    --text: #ececec;
    --muted: #a1a1aa;
    --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  }

  .login-view {
    background:
      radial-gradient(circle at 50% 8%, rgba(16, 163, 127, 0.12), transparent 34%),
      linear-gradient(180deg, #171717, #111);
  }
}

@media (max-width: 900px) {
  .app-view {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    z-index: 10;
    inset: 0 auto 0 0;
    width: min(86vw, 310px);
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    box-shadow: var(--shadow);
  }

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

  .mobile-only {
    display: inline-grid;
    place-items: center;
    width: 40px;
  }

  .messages,
  .composer-wrap {
    padding-left: 16px;
    padding-right: 16px;
  }

  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    padding: 0 12px;
  }

  .model-controls {
    flex: 1;
  }

  .key-controls {
    display: none;
  }

  .model-select {
    min-width: 0;
    width: 100%;
    max-width: none;
  }

  .connection-status {
    display: none;
  }

  .suggestions {
    grid-template-columns: 1fr;
    width: 100%;
  }

  .composer {
    grid-template-columns: auto 1fr auto;
  }

  .composer-meta {
    grid-template-columns: 1fr;
    text-align: center;
  }

  .composer-meta > :last-child,
  .composer-status {
    justify-self: center;
    text-align: center;
  }

  .stop-button {
    display: none;
  }
}

@media (max-width: 520px) {
  .login-card {
    padding: 24px;
  }

  .topbar {
    height: auto;
    min-height: 58px;
  }

  .model-controls {
    min-width: 0;
  }

  .model-select {
    width: 100%;
  }

  .message {
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 10px;
  }

  .message-avatar {
    width: 30px;
    height: 30px;
  }
}
