:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #185fa5;
  --accent-soft: #e6f1fb;
  --user-bubble: #185fa5;
  --user-text: #ffffff;
  --code-bg: #f1f3f5;
  --danger: #a32d2d;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --panel: #161b21;
    --text: #e6e9ed;
    --muted: #98a2ad;
    --border: #262d35;
    --accent: #85b7eb;
    --accent-soft: #12283d;
    --user-bubble: #245f9b;
    --user-text: #ffffff;
    --code-bg: #0d1117;
    --danger: #f09595;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  display: flex;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- 侧栏 ---------- */
.sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 17px; padding: 6px 8px 14px; }
.brand-icon { width: 28px; height: 28px; border-radius: 7px; }
.btn-new {
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 10px;
  padding: 10px 12px;
  text-align: left;
  cursor: pointer;
  font-weight: 500;
}
.btn-new:hover { filter: brightness(1.05); }
.conv-list { flex: 1; overflow-y: auto; margin-top: 10px; display: flex; flex-direction: column; gap: 2px; }
.conv-item {
  display: flex; align-items: center;
  border-radius: 9px;
  cursor: pointer;
  color: var(--text);
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: var(--accent-soft); }
.conv-item .name { flex: 1; padding: 8px 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .del { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; visibility: hidden; }
.conv-item:hover .del { visibility: visible; }
.conv-item .del:hover { color: var(--danger); background: var(--bg); }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 8px; }
.btn-ghost { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 8px 10px; border-radius: 8px; text-align: left; width: 100%; }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

/* ---------- 主区 ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  padding-top: calc(8px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.icon-btn { border: 0; background: transparent; color: var(--muted); font-size: 18px; width: 36px; height: 36px; border-radius: 9px; cursor: pointer; }
.icon-btn:hover { background: var(--bg); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn.stop { color: var(--danger); animation: pulse 1s infinite; }
.title-wrap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; justify-content: center; }
.conv-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 50%; }
.model {
  max-width: 45%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 13px;
}

.messages { flex: 1; overflow-y: auto; padding: 16px; scroll-behavior: smooth; }
.empty { text-align: center; color: var(--muted); margin-top: 18vh; }
.empty h1 { color: var(--text); font-size: 22px; margin: 0 0 6px; }
.empty p { margin: 0; }
.msg { max-width: 820px; margin: 0 auto 14px; display: flex; gap: 10px; }
.msg.user { justify-content: flex-end; }
.msg .bubble { border-radius: var(--radius); padding: 10px 14px; min-width: 0; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow); flex: 1; overflow: hidden; }
.avatar { width: 30px; height: 30px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); font-size: 15px; font-weight: 600; display: flex; align-items: center; justify-content: center; flex: 0 0 30px; margin-top: 4px; }
.msg .bubble p { margin: 0 0 .7em; }
.msg .bubble p:last-child { margin-bottom: 0; }
.msg .bubble h1, .msg .bubble h2, .msg .bubble h3 { font-size: 1.05em; margin: 1em 0 .4em; }
.msg .bubble ul, .msg .bubble ol { padding-left: 1.4em; margin: 0 0 .7em; }
.msg .bubble a { color: var(--accent); }
.msg .bubble table { border-collapse: collapse; display: block; overflow-x: auto; margin: 0 0 .8em; }
.msg .bubble th, .msg .bubble td { border: 1px solid var(--border); padding: 5px 9px; }
.msg .bubble blockquote { margin: 0 0 .7em; padding: 2px 12px; border-left: 3px solid var(--border); color: var(--muted); }
.msg .bubble pre {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  font-size: 13px;
  margin: 0 0 .8em;
}
.msg .bubble pre code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Cascadia Mono", monospace; background: transparent; padding: 0; }
.msg .bubble code:not(pre code) { background: var(--code-bg); padding: 2px 5px; border-radius: 5px; font-size: .9em; }
.copy-btn {
  position: absolute; top: 6px; right: 6px;
  border: 1px solid var(--border); background: var(--panel); color: var(--muted);
  border-radius: 6px; font-size: 11px; padding: 2px 7px; cursor: pointer;
}
.copy-btn:hover { color: var(--text); }
.reasoning { color: var(--muted); font-size: 13px; border-left: 3px solid var(--border); padding: 2px 10px; margin-bottom: 8px; }
.reasoning summary { cursor: pointer; }
.cursor { display: inline-block; width: 7px; height: 15px; background: var(--accent); vertical-align: -2px; margin-left: 2px; animation: blink 1s steps(2) infinite; }
.err { color: var(--danger); }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 50% { opacity: .5; } }

/* ---------- 输入区 ---------- */
.composer { padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--border); }
.composer-box { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; }
textarea {
  flex: 1; resize: none; max-height: 160px;
  border: 1px solid var(--border); background: var(--bg); border-radius: 12px;
  padding: 10px 12px; line-height: 1.5; outline: none;
}
textarea:focus { border-color: var(--accent); }
.send-btn {
  border: 0; background: var(--accent); color: #fff;
  width: 40px; height: 40px; border-radius: 12px; font-size: 18px; cursor: pointer;
  flex: 0 0 40px; margin-bottom: 1px;
}
.send-btn:disabled { opacity: .45; cursor: default; }

/* ---------- 登录 ---------- */
.login { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 100; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 28px 26px; width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; text-align: center; box-shadow: var(--shadow); }
.login-card h1 { margin: 0; font-size: 20px; }
.login-icon { width: 56px; height: 56px; border-radius: 14px; margin: 0 auto; }
.muted { color: var(--muted); margin: 0; font-size: 13px; }
.login-card input { border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 11px 12px; outline: none; text-align: center; }
.login-card input:focus { border-color: var(--accent); }
.btn-primary { border: 0; background: var(--accent); color: #fff; border-radius: 10px; padding: 11px; cursor: pointer; font-weight: 500; }
.error { color: var(--danger); font-size: 13px; margin: 0; min-height: 1em; }

/* ---------- 遮罩 / 提示 ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 25; }
.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--panel); border: 1px solid var(--border); color: var(--text); padding: 8px 14px; border-radius: 10px; box-shadow: var(--shadow); z-index: 200; max-width: 90vw; }

/* ---------- 手机 ---------- */
@media (max-width: 720px) {
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  body.sidebar-open .sidebar { transform: translateX(0); }
  #menu { display: inline-flex; align-items: center; justify-content: center; }
  .msg.assistant .bubble { flex: 1; }
  .sheet { width: 100%; right: 0; left: 0; top: auto; bottom: 0; max-height: 80vh; border-radius: 18px 18px 0 0; }
}
@media (min-width: 721px) {
  #menu { visibility: hidden; }
  .sheet { width: 360px; right: 0; top: 0; bottom: 0; border-width: 0 0 0 1px; }
}

/* ---------- 提醒 ---------- */
.icon-btn { position: relative; }
.badge {
  position: absolute; top: 2px; right: 2px;
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 8px; background: #d85a30; color: #fff;
  font-size: 11px; line-height: 16px; text-align: center; font-weight: 500;
}
.sheet {
  position: fixed; z-index: 40;
  background: var(--panel); border: 1px solid var(--border);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow);
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.hidden { display: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 10px 6px 16px; }
.sheet-head h2 { margin: 0; font-size: 16px; font-weight: 500; }
.rem-form { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; padding: 8px 14px 12px; border-bottom: 1px solid var(--border); }
.rem-form input { border: 1px solid var(--border); background: var(--bg); border-radius: 9px; padding: 9px 10px; outline: none; min-width: 0; font-size: 14px; }
.rem-form input:focus { border-color: var(--accent); }
.rem-form .btn-primary { padding: 8px 14px; }
@media (max-width: 420px) { .rem-form { grid-template-columns: 1fr 1fr; } .rem-form .btn-primary { grid-column: 1 / -1; } }
.rem-list { flex: 1; overflow-y: auto; padding: 6px 8px 14px; }
.rem-item { display: flex; align-items: center; gap: 10px; padding: 10px 8px; border-radius: 10px; }
.rem-item:hover { background: var(--bg); }
.rem-item input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); flex: 0 0 20px; }
.rem-item .body { flex: 1; min-width: 0; }
.rem-item .title { font-weight: 500; word-break: break-word; }
.rem-item.done .title { text-decoration: line-through; color: var(--muted); }
.rem-item .when { font-size: 13px; color: var(--muted); }
.rem-item.due .when { color: #d85a30; }
.rem-item .del { border: 0; background: transparent; color: var(--muted); font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.rem-item .del:hover { color: var(--danger); }
.rem-empty { text-align: center; color: var(--muted); padding: 30px 10px; font-size: 14px; }
.msg.system { justify-content: center; margin: -4px auto 14px; }
.reminder-card {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 12px;
  padding: 8px 14px; font-size: 14px; font-weight: 500;
}
.reminder-card .when { color: var(--text); font-weight: 400; }
.empty .hint { margin-top: 14px; font-size: 13px; color: var(--muted); background: var(--panel); border: 1px dashed var(--border); border-radius: 10px; padding: 8px 12px; display: inline-block; }
