* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #f4f6f8;
  color: #111827;
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.login-card {
  width: min(420px, 92vw);
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  padding: 24px;
}

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

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

input,
textarea,
select {
  width: 100%;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 10px;
}

button {
  border: none;
  background: #2563eb;
  color: #fff;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
}

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

.btn-secondary {
  background: #6b7280;
  font-size: 13px;
  padding: 8px 12px;
}

.btn-secondary:hover {
  background: #4b5563;
}

.error {
  color: #b91c1c;
  min-height: 20px;
}

.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  position: relative;
}

.sidebar {
  border-right: 1px solid #e5e7eb;
  background: #ffffff;
  display: flex;
  flex-direction: column;
}

.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px;
  border-bottom: 1px solid #e5e7eb;
}

#conversation-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
}

#conversation-list li {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  margin-bottom: 8px;
  padding: 10px;
  cursor: pointer;
  background: #fff;
}

#conversation-list li.active {
  border-color: #2563eb;
  background: #eff6ff;
}

.sidebar-overlay {
  display: none;
}

.chat-area {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  background: #fff;
  gap: 8px;
  flex-wrap: wrap;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.think-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #374151;
  user-select: none;
}

.think-switch input {
  display: none;
}

.think-slider {
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: #d1d5db;
  position: relative;
  transition: background 0.2s ease;
}

.think-slider::after {
  content: "";
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ffffff;
  position: absolute;
  top: 3px;
  left: 3px;
  transition: transform 0.2s ease;
}

.think-switch input:checked + .think-slider {
  background: #2563eb;
}

.think-switch input:checked + .think-slider::after {
  transform: translateX(14px);
}

.think-text {
  color: #374151;
}

.sidebar-toggle {
  display: none;
  background: transparent;
  color: #111827;
  font-size: 22px;
  padding: 4px 8px;
  border-radius: 6px;
}

.sidebar-toggle:hover {
  background: #e5e7eb;
}

.context-label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #6b7280;
  white-space: nowrap;
}

.context-label input[type="range"] {
  width: 80px;
  height: 4px;
  cursor: pointer;
  accent-color: #2563eb;
  padding: 0;
  border: none;
}

#context-value {
  min-width: 18px;
  text-align: center;
  font-weight: 600;
  color: #111827;
}

.sync-status {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.sync-status.synced {
  background: #d1fae5;
  color: #065f46;
}

.sync-status.syncing {
  background: #fef3c7;
  color: #92400e;
}

.sync-status.error {
  background: #fee2e2;
  color: #991b1b;
}

.sync-status.offline {
  background: #e5e7eb;
  color: #6b7280;
}

.messages {
  padding: 16px;
  overflow-y: auto;
}

.load-more {
  text-align: center;
  padding: 10px 0 16px;
}

.load-more button {
  font-size: 13px;
  padding: 8px 16px;
}

.bubble {
  max-width: min(80%, 900px);
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 10px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.bubble.user {
  margin-left: auto;
  background: #2563eb;
  color: #fff;
}

.bubble.assistant {
  margin-right: auto;
  background: #fff;
  border: 1px solid #e5e7eb;
}

/* ── Markdown 渲染样式（assistant 气泡内） ── */

.bubble.assistant {
  white-space: normal;
}

.bubble.assistant p {
  margin: 0 0 8px;
  line-height: 1.6;
}

.bubble.assistant p:last-child {
  margin-bottom: 0;
}

.bubble.assistant h1,
.bubble.assistant h2,
.bubble.assistant h3,
.bubble.assistant h4 {
  margin: 12px 0 6px;
  line-height: 1.3;
}

.bubble.assistant h1 { font-size: 1.3em; }
.bubble.assistant h2 { font-size: 1.15em; }
.bubble.assistant h3 { font-size: 1.05em; }

.bubble.assistant ul,
.bubble.assistant ol {
  margin: 4px 0 8px;
  padding-left: 20px;
}

.bubble.assistant li {
  margin-bottom: 3px;
  line-height: 1.5;
}

.bubble.assistant code {
  background: #f3f4f6;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
  color: #d63384;
}

.bubble.assistant pre {
  background: #1e1e2e;
  color: #cdd6f4;
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.5;
}

.bubble.assistant pre code {
  background: transparent;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

.bubble.assistant blockquote {
  border-left: 3px solid #2563eb;
  margin: 8px 0;
  padding: 4px 12px;
  color: #4b5563;
  background: #f9fafb;
  border-radius: 0 6px 6px 0;
}

.bubble.assistant table {
  border-collapse: collapse;
  margin: 8px 0;
  width: 100%;
  font-size: 0.9em;
}

.bubble.assistant th,
.bubble.assistant td {
  border: 1px solid #e5e7eb;
  padding: 6px 10px;
  text-align: left;
}

.bubble.assistant th {
  background: #f3f4f6;
  font-weight: 600;
}

.bubble.assistant hr {
  border: none;
  border-top: 1px solid #e5e7eb;
  margin: 10px 0;
}

.bubble.assistant a {
  color: #2563eb;
  text-decoration: underline;
}

.bubble.assistant img {
  max-width: 100%;
  border-radius: 6px;
}

.meta {
  display: block;
  font-size: 12px;
  opacity: 0.7;
  margin-top: 6px;
}

/* ── 复制按钮（图标样式） ── */

.copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  margin-top: 6px;
  background: transparent;
  color: #9ca3af;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
}

.copy-btn:hover {
  color: #374151;
  background: #f3f4f6;
}

.bubble.user .copy-btn {
  color: rgba(255, 255, 255, 0.5);
}

.bubble.user .copy-btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.15);
}

.chat-form {
  border-top: 1px solid #e5e7eb;
  background: #fff;
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-template-areas:
    "input send top"
    "attach send top";
  gap: 10px;
}

#message-input {
  min-height: 76px;
  resize: vertical;
  grid-area: input;
}

.chat-form button[type="submit"] {
  grid-area: send;
  align-self: stretch;
}

#scroll-top-btn {
  grid-area: top;
  align-self: center;
  justify-self: center;
}

.attachment-row {
  grid-area: attach;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.attachment-label {
  font-size: 12px;
  color: #374151;
}

#attachment-input {
  width: auto;
  max-width: 60%;
  padding: 4px;
}

.attachment-hint {
  font-size: 12px;
  color: #6b7280;
}

.help-modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 20px;
}

.help-modal.open {
  display: flex;
}

.help-modal-card {
  width: min(860px, 100%);
  max-height: 85vh;
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  display: grid;
  grid-template-rows: auto 1fr;
}

.help-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #e5e7eb;
}

.help-modal-body {
  overflow: auto;
  padding: 14px 18px 18px;
  display: grid;
  gap: 10px;
}

.help-modal-body h4 {
  margin: 0 0 6px;
  font-size: 14px;
}

.help-modal-body ul {
  margin: 0;
  padding-left: 20px;
  color: #374151;
  line-height: 1.55;
}

.help-modal-body code {
  background: #f3f4f6;
  padding: 1px 4px;
  border-radius: 4px;
}

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

  .sidebar {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 199;
  }

  .sidebar-overlay.visible {
    display: block;
  }

  .sidebar-toggle {
    display: inline-flex;
  }

  .header-actions {
    gap: 6px;
  }

  .btn-secondary {
    font-size: 12px;
    padding: 6px 10px;
  }

  #model-select {
    max-width: 140px;
    font-size: 13px;
  }

  .context-label input[type="range"] {
    width: 60px;
  }

  .chat-form {
    grid-template-columns: 1fr;
    grid-template-areas:
      "input"
      "attach"
      "send"
      "top";
  }

  .chat-form button[type="submit"] {
    width: 100%;
  }

  #scroll-top-btn {
    width: 100%;
  }

  #attachment-input {
    max-width: 100%;
  }

  .help-modal {
    padding: 10px;
  }

  .help-modal-card {
    max-height: 92vh;
  }
}
