:root {
  --bg: #f5f4ef;
  --panel: #ecebe4;
  --surface: #ffffff;
  --text: #1f1e1c;
  --muted: #73716b;
  --line: #dddbd2;
  --accent: #c2643f;
  --accent-text: #ffffff;
  --bubble: #e7e4da;
  --code: #efede6;
  --danger: #b3412e;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1d1b;
    --panel: #262522;
    --surface: #2c2b28;
    --text: #ecebe6;
    --muted: #a19e95;
    --line: #3a3935;
    --accent: #d97757;
    --bubble: #353430;
    --code: #2a2926;
    --danger: #e0715c;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 16px/1.65 -apple-system, BlinkMacSystemFont, "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif; -webkit-text-size-adjust: 100%; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 8px 14px; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.gate { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.gate-card { width: min(360px, 100%); text-align: center; display: grid; gap: 14px; }
.gate-icon { width: 64px; height: 64px; margin: 0 auto; }
.gate-card h1 { margin: 0; font-size: 24px; font-weight: 600; }
.gate-card p { margin: 0; color: var(--muted); }
.gate-card .primary { padding: 12px; font-size: 17px; }
.label-row { display: grid; gap: 6px; text-align: left; color: var(--muted); font-size: 14px; }
.label-row input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.error { color: var(--danger) !important; min-height: 1.5em; }

.app { display: flex; height: 100dvh; }
.sidebar { width: 268px; flex: none; display: flex; flex-direction: column; gap: 8px; padding: 12px; background: var(--panel); border-right: 1px solid var(--line); padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.new-chat { text-align: left; font-weight: 600; }
.list { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; }
.list .item { display: flex; gap: 8px; align-items: baseline; justify-content: space-between; text-align: left; border: 0; background: transparent; padding: 8px 10px; }
.list .item span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .item time { flex: none; color: var(--muted); font-size: 12px; }
.list .item.active, .list .item:hover { background: var(--bubble); }
.sidebar-foot { display: flex; gap: 8px; }
.sidebar-foot button { flex: 1; color: var(--muted); }

.usage { width: min(380px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 16px; padding: 18px; background: var(--surface); color: var(--text); }
.usage::backdrop { background: rgb(0 0 0 / .35); }
.usage header, .usage footer { display: flex; align-items: center; justify-content: space-between; }
.usage h3 { margin: 0; font-size: 17px; }
.usage-plan { margin: 6px 0 16px; color: var(--muted); font-size: 14px; }
.usage-limit { margin-bottom: 16px; }
.usage-limit .row { display: flex; justify-content: space-between; font-size: 15px; }
.usage-limit .bar { height: 8px; margin: 6px 0 4px; border-radius: 4px; background: var(--bubble); overflow: hidden; }
.usage-limit .bar i { display: block; height: 100%; background: var(--accent); }
.usage-limit.high .bar i { background: var(--danger); }
.usage-limit .reset { font-size: 13px; color: var(--muted); }
.usage footer { font-size: 13px; color: var(--muted); }
.usage footer button { font-size: 14px; padding: 4px 12px; }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.topbar h2 { flex: 1; margin: 0; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-button { border: 0; background: transparent; font-size: 20px; padding: 4px 10px; line-height: 1; }
#menu { display: none; }
.more { position: relative; }
.more-menu { position: absolute; right: 0; top: 100%; z-index: 5; display: grid; min-width: 150px; padding: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 24px rgb(0 0 0 / .12); }
.more-menu button, .more-menu a { border: 0; background: transparent; text-align: left; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none; font-size: 15px; }
.more-menu button:hover, .more-menu a:hover { background: var(--bubble); }
.more-menu .danger { color: var(--danger); }

.thread { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 24px 16px 12px; }
.empty { max-width: 720px; margin: 18vh auto 0; text-align: center; color: var(--muted); }
/* 网格列默认按内容最宽处撑开：长代码、表格、工具行会把整列撑出屏幕，这里限定列宽、允许子项收缩。 */
.messages { max-width: 760px; margin: 0 auto; display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; }
.message { min-width: 0; }
.message.user { display: flex; flex-direction: column; align-items: flex-end; }
.message.user .bubble { max-width: 85%; padding: 10px 14px; border-radius: 16px; background: var(--bubble); white-space: pre-wrap; overflow-wrap: anywhere; }
.message.assistant .content { overflow-wrap: anywhere; }
.message.assistant .content > :first-child { margin-top: 0; }
.message.assistant .content > :last-child { margin-bottom: 0; }
.content pre { background: var(--code); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 14px; line-height: 1.5; }
.content code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .9em; }
.content :not(pre) > code { background: var(--code); padding: 1px 5px; border-radius: 5px; }
.content table { border-collapse: collapse; display: block; max-width: 100%; overflow-x: auto; font-size: 15px; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; overflow-wrap: break-word; }
.content blockquote { margin: 0; padding-left: 14px; border-left: 3px solid var(--line); color: var(--muted); }
.content a { color: var(--accent); }
.content h1, .content h2, .content h3 { font-size: 1.1em; margin: 1.2em 0 .5em; }
.tool { margin: 8px 0; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool span { font-family: ui-monospace, Menlo, monospace; }
.status { margin-top: 8px; font-size: 13px; color: var(--muted); }
.status.error { color: var(--danger); }
.typing { display: flex; gap: 5px; height: 18px; align-items: center; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.actions { display: flex; gap: 2px; margin-top: 4px; margin-left: -8px; }
.message.user .actions { margin: 4px -8px 0 0; }
.actions button { border: 0; background: transparent; padding: 2px 8px; font-size: 13px; color: var(--muted); border-radius: 6px; }
.actions button:hover { background: var(--bubble); color: var(--text); }
/* 有鼠标时悬停才显示；手机上一直显示。 */
@media (hover: hover) {
  .actions { opacity: 0; transition: opacity .15s; }
  .message:hover .actions, .actions:focus-within { opacity: 1; }
}
/* 多选：点整条消息勾选，左侧圆圈表示选中状态。 */
.selecting .message { position: relative; padding: 6px 8px 6px 38px; border-radius: 12px; cursor: pointer; user-select: none; -webkit-user-select: none; }
.selecting .message::before { content: ""; position: absolute; left: 8px; top: 12px; width: 18px; height: 18px; border: 2px solid var(--muted); border-radius: 50%; }
.selecting .message.selected { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.selecting .message.selected::before { content: "✓"; border-color: var(--accent); background: var(--accent); color: var(--accent-text); font-size: 12px; line-height: 18px; text-align: center; }
.select-bar { display: flex; gap: 8px; align-items: center; max-width: 792px; width: 100%; margin: 0 auto; padding: 10px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.select-bar span { flex: 1; color: var(--muted); }
.select-bar button { height: 46px; }

.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 792px; width: 100%; margin: 0 auto; padding: 10px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; resize: none; min-height: 46px; max-height: 40vh; padding: 11px 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); outline: none; line-height: 1.5; }
.composer textarea:focus { border-color: var(--accent); }
.composer button { height: 46px; flex: none; }
.toast { position: absolute; left: 50%; bottom: 84px; transform: translateX(-50%); padding: 8px 14px; border-radius: 10px; background: var(--text); color: var(--bg); font-size: 14px; max-width: 90%; }

@media (max-width: 800px) {
  #menu { display: block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 84vw); transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar-open .sidebar { transform: none; }
  .sidebar-open .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgb(0 0 0 / .3); }
  .topbar { padding-left: 6px; }
  .thread { padding: 16px 14px 8px; }
  .composer, .select-bar { padding-left: 10px; padding-right: 10px; }
  /* 手机上代码也换行，不必左右拖动。 */
  .content pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; }
  .content table { font-size: 14px; }
  .content th, .content td { padding: 5px 8px; }
}
