* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: #0f1419;
  color: #e7ecf1;
  height: 100%;
  min-height: 100dvh;
}

/* 聊天页：禁止整页滚动，仅消息区滚动 */
body.in-chat {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
  height: 100dvh;
}

.hidden {
  display: none !important;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.screen {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.card {
  max-width: 420px;
  margin: auto;
  padding: 24px;
  width: 100%;
}

.card h1 {
  margin: 0 0 8px;
  font-size: 1.4rem;
}

.card p {
  color: #9aa7b5;
  font-size: 0.9rem;
}

label {
  display: block;
  margin: 14px 0 6px;
  font-size: 0.85rem;
  color: #9aa7b5;
}

input,
select,
button,
textarea {
  font: inherit;
}

input,
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #2a3440;
  background: #171d24;
  color: #e7ecf1;
}

button {
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  background: #3b82f6;
  color: #fff;
  cursor: pointer;
}

button.secondary {
  background: #2a3440;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-row {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.btn-row button {
  flex: 1;
}

.error {
  color: #f87171;
  font-size: 0.85rem;
  margin-top: 10px;
  min-height: 1.2em;
}

#chat-screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body.in-chat #chat-screen {
  position: fixed;
  inset: 0;
  z-index: 1;
}

.chat-layout {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

.sidebar {
  --sidebar-w: min(288px, 86vw);
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: #121820;
  border-right: 1px solid #2a3440;
  z-index: 30;
  transition: transform 0.22s ease, width 0.22s ease, margin 0.22s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 12px 10px;
  padding-top: max(14px, env(safe-area-inset-top));
  border-bottom: 1px solid #2a3440;
}

.sidebar-brand-name {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
}

.icon-btn-ghost {
  background: transparent;
  color: #9aa7b5;
  width: 36px;
  height: 36px;
}

.icon-btn-ghost:active {
  background: #1f2933;
  color: #e7ecf1;
}

.sidebar-user {
  padding: 12px 14px 0;
}

.sidebar-user-name {
  font-size: 0.92rem;
  font-weight: 600;
  color: #b8c5d3;
}

.sidebar-section {
  padding: 12px 14px 0;
}

.sidebar-section-grow {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: 8px;
}

.sidebar-label {
  display: block;
  margin: 0 0 6px;
  font-size: 0.78rem;
  color: #9aa7b5;
}

.sidebar-primary {
  margin: 12px 14px 0;
  width: calc(100% - 28px);
  padding: 11px 14px;
  font-weight: 600;
}

.sidebar-sessions-label {
  margin: 12px 0 6px;
  font-size: 0.78rem;
  color: #9aa7b5;
}

.session-list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.sidebar-footer {
  padding: 12px 14px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid #2a3440;
  flex-shrink: 0;
}

.sidebar-footer-actions {
  display: flex;
  gap: 8px;
}

.sidebar-footer-actions button {
  flex: 1;
  min-width: 0;
}

.chat-main-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.settings-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #0f1419;
}

.settings-panel.hidden {
  display: none;
}

.chat-main-view.hidden {
  display: none;
}

.settings-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  min-height: 52px;
  border-bottom: 1px solid #2a3440;
  background: #121820;
  flex-shrink: 0;
}

.settings-title {
  flex: 1;
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.settings-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 16px 14px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.settings-section-title {
  margin: 0 0 8px;
  font-size: 0.92rem;
  font-weight: 600;
  color: #e7ecf1;
}

.settings-desc {
  margin: 0 0 14px;
  font-size: 0.82rem;
  line-height: 1.45;
  color: #9aa7b5;
}

.settings-hint {
  margin: 10px 0 0;
  font-size: 0.78rem;
  color: #fbbf24;
  line-height: 1.4;
}

.settings-action {
  width: 100%;
}

.settings-recover-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
}

.settings-recover-actions button {
  width: 100%;
}

.settings-recover-actions button.danger {
  background: #7f1d1d;
  color: #fecaca;
}

.settings-recover-actions button.danger:hover:not(:disabled) {
  background: #991b1b;
}

.settings-recover-actions button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.recover-session-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: min(50vh, 360px);
  overflow: auto;
  margin-top: 8px;
  padding-right: 2px;
}

.recover-session-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  background: #1a222c;
  border: 1px solid #2a3440;
  color: #e7ecf1;
  cursor: pointer;
  font-size: 0.9rem;
}

.recover-session-item:hover {
  border-color: #3d4d5c;
}

.recover-session-item.selected {
  border-color: #3b82f6;
  background: #1e2a3a;
}

.recover-session-item-id {
  display: block;
  margin-top: 4px;
  font-size: 0.72rem;
  color: #6b7c8f;
  word-break: break-all;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(0, 0, 0, 0.45);
}

.chat-main {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #0f1419;
  overflow: hidden;
}

.chat-header {
  --chat-header-h: 52px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  min-height: var(--chat-header-h);
  border-bottom: 1px solid #2a3440;
  background: #121820;
  flex-shrink: 0;
  z-index: 40;
}

.chat-header-title {
  flex: 1;
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #1f2933;
  color: #e7ecf1;
  border: none;
  cursor: pointer;
}

.icon-btn:active {
  background: #2a3440;
}

.sidebar-toggle .toggle-icon {
  display: block;
}

.config-hint {
  margin-top: 6px;
  font-size: 0.75rem;
  color: #fbbf24;
  line-height: 1.35;
}

/* 手机：默认收起，展开时侧栏滑入 + 遮罩；顶栏固定不随消息滚动 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-105%);
  }

  .chat-layout:not(.sidebar-collapsed) .sidebar {
    transform: translateX(0);
  }

  .chat-layout:not(.sidebar-collapsed) .sidebar-backdrop {
    display: block;
  }

  .chat-layout:not(.sidebar-collapsed) {
    overflow: hidden;
  }

  #btn-sidebar-close {
    display: inline-flex;
  }

  .chat-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  }

  .chat-panel {
    padding-top: calc(var(--chat-header-h) + env(safe-area-inset-top, 0px));
  }

  /* 侧栏打开时顶栏仍可见，便于随时收起 */
  .chat-layout:not(.sidebar-collapsed) .chat-header {
    z-index: 35;
  }
}

/* Android WebView：始终固定顶栏（不依赖整页 media query） */
html.webview-app body.in-chat #chat-screen {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
}

html.webview-app .chat-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

html.webview-app .chat-panel {
  padding-top: calc(var(--chat-header-h) + env(safe-area-inset-top, 0px));
}

html.webview-app .messages {
  overscroll-behavior-y: contain;
}

/* 宽屏：侧栏占位，收起时宽度为 0（类似 DeepSeek） */
@media (min-width: 769px) {
  .sidebar-backdrop {
    display: none !important;
  }

  .sidebar {
    position: relative;
    transform: none !important;
  }

  .chat-layout.sidebar-collapsed .sidebar {
    width: 0;
    min-width: 0;
    border-right: none;
    overflow: hidden;
    pointer-events: none;
  }

  #btn-sidebar-close {
    display: inline-flex;
  }
}

.chat-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 10px;
  margin-bottom: 6px;
  background: #171d24;
  font-size: 0.85rem;
}

.session-item.session-item-blocked .session-label {
  opacity: 0.45;
  cursor: not-allowed;
}

.session-item.active {
  background: #1e3a5f;
  outline: 1px solid #3b82f6;
}

.session-label {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  padding: 4px 2px;
  font: inherit;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.session-actions button {
  padding: 6px 10px;
  font-size: 0.72rem;
  line-height: 1.2;
  border-radius: 6px;
  min-height: 36px;
  min-width: 44px;
}

.session-actions button.secondary {
  background: #2a3440;
}

.session-actions button.danger {
  background: #3f1d1d;
  color: #fca5a5;
}

.session-actions button.danger:hover {
  background: #5c2020;
}

.bookmark-list {
  margin-bottom: 10px;
  max-height: 160px;
  overflow-y: auto;
}

.bookmark-empty {
  font-size: 0.78rem;
  color: #6b7280;
  padding: 4px 2px 8px;
}

.bookmark-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  margin-bottom: 6px;
  border-radius: 10px;
  border: 1px solid #2a3440;
  background: #171d24;
  color: inherit;
  cursor: pointer;
  font: inherit;
}

.bookmark-item:hover {
  background: #1e2833;
}

.bookmark-item-title {
  font-size: 0.82rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bookmark-item-meta {
  font-size: 0.72rem;
  color: #9aa7b5;
  margin-top: 2px;
}

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 4px;
  margin-bottom: 10px;
}

.msg-row .msg {
  margin-bottom: 0;
  flex: 1;
  min-width: 0;
}

.msg-row.user-row {
  flex-direction: row-reverse;
}

.msg-bookmark-btn {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid #2a3440;
  background: #171d24;
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.55;
}

.msg-row:hover .msg-bookmark-btn,
.msg-bookmark-btn.active {
  opacity: 1;
}

.msg-bookmark-btn.active {
  border-color: #3b82f6;
  background: #1e3a5f;
}

.load-older-msgs {
  display: block;
  width: 100%;
  margin: 8px 0 12px;
  padding: 10px 12px;
  font-size: 0.85rem;
  border-radius: 10px;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid #2a3440;
  background: #121820;
  flex-shrink: 0;
}

#history-search {
  margin-bottom: 8px;
  font-size: 0.85rem;
}

.msg {
  max-width: 85%;
  padding: 10px 12px;
  border-radius: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
}

.msg.user,
.msg[data-role="user"] {
  margin-left: auto;
  background: #2563eb;
  color: #f1f5f9;
}

.msg.bot,
.msg[data-role="bot"] {
  background: #1f2933;
}

.search-results {
  max-height: 200px;
  overflow: auto;
  margin-bottom: 8px;
  border-radius: 8px;
  background: #121820;
  border: 1px solid #2a3440;
}

.search-results.hidden {
  display: none;
}

.search-hit {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: none;
  border-bottom: 1px solid #2a3440;
  background: transparent;
  color: #e7ecf1;
  cursor: pointer;
  border-radius: 0;
}

.search-hit:hover {
  background: #1f2933;
}

.search-hit-title {
  font-size: 0.75rem;
  color: #9aa7b5;
  margin-bottom: 4px;
}

.search-hit-snippet {
  font-size: 0.8rem;
  line-height: 1.35;
  word-break: break-word;
}

.search-hit-snippet mark {
  background: #854d0e;
  color: #fef3c7;
  padding: 0 2px;
  border-radius: 2px;
}

.search-empty,
.search-more {
  padding: 8px 10px;
  font-size: 0.8rem;
  color: #9aa7b5;
}

.msg.highlight {
  outline: 2px solid #fbbf24;
  outline-offset: 2px;
}

.msg.bot.md-body {
  white-space: normal;
}

.msg.md-body h1,
.msg.md-body h2,
.msg.md-body h3 {
  margin: 0.6em 0 0.35em;
  font-size: 1.05em;
  line-height: 1.3;
}

.msg.md-body h1 { font-size: 1.15em; }

.msg.md-body p {
  margin: 0.35em 0;
}

.msg.md-body ul,
.msg.md-body ol {
  margin: 0.35em 0;
  padding-left: 1.4em;
}

.msg.md-body li {
  margin: 0.2em 0;
}

.msg.md-body blockquote {
  margin: 0.5em 0;
  padding: 0.25em 0.75em;
  border-left: 3px solid #3b82f6;
  color: #b8c5d3;
}

.msg.md-body pre {
  margin: 0.5em 0;
  padding: 10px;
  border-radius: 8px;
  background: #0d1117;
  overflow-x: auto;
  font-size: 0.85em;
}

.msg.md-body code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
}

.msg.md-body :not(pre) > code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: #0d1117;
}

.msg.md-body table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.9em;
}

.msg.md-body th,
.msg.md-body td {
  border: 1px solid #2a3440;
  padding: 4px 8px;
}

.msg.md-body th {
  background: #171d24;
}

.msg.md-body a {
  color: #60a5fa;
}

.msg.md-body hr {
  border: none;
  border-top: 1px solid #2a3440;
  margin: 0.75em 0;
}

.msg.md-body strong {
  color: #f3f4f6;
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 120px;
}

.dialog-root {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.dialog-root.hidden {
  display: none !important;
}

.dialog-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.dialog-box {
  position: relative;
  width: 100%;
  max-width: 360px;
  background: #121820;
  border: 1px solid #2a3440;
  border-radius: 14px;
  padding: 18px 16px 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

.dialog-title {
  margin: 0 0 8px;
  font-size: 1.05rem;
  font-weight: 600;
}

.dialog-message {
  margin: 0 0 12px;
  font-size: 0.88rem;
  line-height: 1.5;
  color: #b8c5d3;
  white-space: pre-wrap;
}

.dialog-message.hidden {
  display: none;
}

.dialog-input {
  margin-bottom: 14px;
}

.dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.dialog-actions button {
  min-width: 72px;
}

.dialog-actions button.danger {
  background: #dc2626;
}

.dialog-actions button.hidden {
  display: none;
}
