:root {
  --bg: #f5f4ed;
  --sidebar: #efede4;
  --surface: #ffffff;
  --text: #1f1e1d;
  --muted: #73726c;
  --faint: #a3a29b;
  --border: #e3e1d7;
  --border-strong: #d4d1c4;
  --user-bubble: #e8e5da;
  --accent: #c96442;
  --accent-hover: #b4583a;
  --accent-text: #ffffff;
  --hover: rgba(31, 30, 29, 0.06);
  --active: rgba(31, 30, 29, 0.1);
  --code-bg: #f0eee6;
  --pre-bg: #2b2a27;
  --pre-text: #f3f1ea;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 18px rgba(0, 0, 0, 0.06);
  --serif: ui-serif, 'Iowan Old Style', 'Palatino Linotype', Georgia, 'Songti SC', 'Noto Serif CJK SC', serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Noto Sans CJK SC', sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --sidebar: #1f1e1d;
    --surface: #30302e;
    --text: #ece9e1;
    --muted: #a6a39b;
    --faint: #7c7a73;
    --border: #3a3936;
    --border-strong: #4a4945;
    --user-bubble: #141413;
    --accent: #d97757;
    --accent-hover: #e0876a;
    --hover: rgba(255, 255, 255, 0.06);
    --active: rgba(255, 255, 255, 0.1);
    --code-bg: #1f1e1d;
    --pre-bg: #1a1918;
    --pre-text: #ece9e1;
    --danger: #f2877f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 18px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }

/* ---------- Login ---------- */
.login-page {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 16px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
}
.login-card h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 30px;
  margin: 8px 0 0;
}
.login-card p { margin: 0 0 8px; color: var(--muted); }
.login-card input {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
.login-card button {
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.login-card button:hover:not(:disabled) { background: var(--accent-hover); }
.login-card button:disabled { opacity: 0.7; }
.login-error { min-height: 1.5em; color: var(--danger); font-size: 14px; }

/* ---------- App layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: 272px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px 10px;
  gap: 4px;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 10px; }
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 20px; font-weight: 500; }
.new-chat {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border: 0; border-radius: 10px;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  text-align: left;
}
.new-chat:hover { background: var(--hover); }
.list-label { font-size: 12px; color: var(--muted); padding: 14px 10px 4px; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; margin: 0 -2px; padding: 0 2px; }
.conv-item {
  position: relative;
  display: flex; align-items: center;
  border-radius: 8px;
}
.conv-item:hover, .conv-item.active { background: var(--hover); }
.conv-item.active { background: var(--active); }
.conv-item button.open {
  flex: 1; min-width: 0;
  border: 0; background: transparent;
  text-align: left;
  padding: 7px 10px;
  font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item button.del {
  opacity: 0;
  border: 0; background: transparent;
  color: var(--muted);
  padding: 6px 8px; border-radius: 6px;
  display: grid; place-items: center;
}
.conv-item:hover button.del, .conv-item.active button.del { opacity: 1; }
.conv-item button.del:hover { color: var(--danger); }
.sidebar-bottom { padding: 8px 6px 2px; border-top: 1px solid var(--border); }
.link-btn { border: 0; background: transparent; color: var(--muted); font-size: 13px; padding: 4px; }
.link-btn:hover { color: var(--text); }

.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  height: 52px; padding: 0 12px;
  flex-shrink: 0;
}
.chat-title {
  flex: 1; min-width: 0;
  font-size: 15px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  border: 0; background: transparent;
  width: 36px; height: 36px; border-radius: 8px;
  display: grid; place-items: center;
  color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.only-mobile { display: none; }

.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; }

.empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px;
  min-height: 55%;
  padding: 24px 16px 0;
}
.empty h1 { font-family: var(--serif); font-weight: 400; font-size: clamp(28px, 5vw, 40px); margin: 0; text-align: center; }
.app.has-messages .empty { display: none; }

.messages { max-width: 760px; margin: 0 auto; padding: 8px 20px 32px; }

.msg { margin: 22px 0; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble);
  padding: 10px 16px;
  border-radius: 18px;
  max-width: 85%;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.assistant .content {
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.7;
  word-break: break-word;
}
.msg.assistant .content > :first-child { margin-top: 0; }
.msg.assistant .content > :last-child { margin-bottom: 0; }
.content p, .content ul, .content ol, .content blockquote, .content table, .content pre { margin: 0.7em 0; }
.content ul, .content ol { padding-left: 1.5em; }
.content li { margin: 0.25em 0; }
.content h1, .content h2, .content h3, .content h4 { font-family: var(--sans); line-height: 1.35; margin: 1.2em 0 0.5em; }
.content h1 { font-size: 1.35em; }
.content h2 { font-size: 1.2em; }
.content h3 { font-size: 1.05em; }
.content a { color: var(--accent); }
.content blockquote { border-left: 3px solid var(--border-strong); padding-left: 1em; color: var(--muted); }
.content hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.content code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--code-bg);
  padding: 0.12em 0.36em;
  border-radius: 5px;
}
.content pre {
  position: relative;
  background: var(--pre-bg);
  color: var(--pre-text);
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.55;
}
.content pre code { background: transparent; padding: 0; font-size: inherit; color: inherit; }
.content pre .copy-code {
  position: absolute; top: 6px; right: 6px;
  font-family: var(--sans); font-size: 12px;
  border: 0; border-radius: 6px;
  padding: 3px 8px;
  background: rgba(255, 255, 255, 0.1); color: var(--pre-text);
  opacity: 0; transition: opacity 0.15s;
}
.content pre:hover .copy-code, .content pre .copy-code:focus { opacity: 1; }
.content table { border-collapse: collapse; display: block; overflow-x: auto; font-family: var(--sans); font-size: 15px; }
.content th, .content td { border: 1px solid var(--border-strong); padding: 6px 10px; text-align: left; min-width: 4.2em; }
.content th { background: var(--code-bg); }

.msg-actions { display: flex; gap: 2px; margin-top: 6px; margin-left: -6px; min-height: 30px; }
.msg-actions button {
  border: 0; background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 8px; border-radius: 6px;
  display: inline-flex; align-items: center; gap: 4px;
}
.msg-actions button:hover { background: var(--hover); color: var(--text); }
.msg-meta { font-size: 12px; color: var(--faint); align-self: center; margin-left: 6px; }

.msg .notice { margin-top: 8px; font-family: var(--sans); font-size: 14px; color: var(--muted); }
.msg .notice.error { color: var(--danger); }

.thinking {
  display: inline-flex; align-items: center; gap: 8px;
  font-family: var(--sans); font-size: 14px; color: var(--muted);
}
.thinking .spark {
  width: 18px; height: 18px;
  animation: spin 2.4s linear infinite;
  color: var(--accent);
}
@keyframes spin { to { transform: rotate(360deg); } }
.cursor::after {
  content: '';
  display: inline-block;
  width: 0.55em; height: 0.55em;
  margin-left: 4px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: middle;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.25; } }

/* ---------- Composer ---------- */
.composer-wrap {
  flex-shrink: 0;
  padding: 0 16px calc(10px + env(safe-area-inset-bottom));
  max-width: 800px; width: 100%;
  margin: 0 auto;
}
.app:not(.has-messages) .composer-wrap { margin-bottom: 18vh; }
.composer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 12px 12px 10px 16px;
}
.composer:focus-within { border-color: var(--border-strong); }
.composer textarea {
  display: block;
  width: 100%;
  border: 0; outline: none; resize: none;
  background: transparent; color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.5;
  max-height: 40vh;
  padding: 2px 0 8px;
}
.composer textarea::placeholder { color: var(--faint); }
.composer-bar { display: flex; align-items: center; gap: 6px; }
.composer-bar .spacer { flex: 1; }
.pill { position: relative; display: inline-flex; min-width: 0; }
.pill select {
  appearance: none; -webkit-appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit; font-size: 13.5px;
  padding: 5px 24px 5px 9px;
  border-radius: 8px;
  cursor: pointer;
  max-width: 190px;
  text-overflow: ellipsis;
}
.pill select:hover { background: var(--hover); color: var(--text); }
.pill::after {
  content: '';
  position: absolute; right: 9px; top: 50%;
  width: 6px; height: 6px;
  border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted);
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}
.send-btn {
  width: 34px; height: 34px;
  flex-shrink: 0;
  border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center;
  transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.4; }
.send-btn .i-stop { display: none; }
.send-btn.streaming { background: var(--text); color: var(--bg); opacity: 1; }
.send-btn.streaming .i-send { display: none; }
.send-btn.streaming .i-stop { display: block; }
.hint { text-align: center; font-size: 12px; color: var(--faint); padding-top: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 110px;
  transform: translateX(-50%) translateY(10px);
  background: var(--text); color: var(--bg);
  padding: 8px 14px; border-radius: 10px;
  font-size: 14px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Mobile ---------- */
@media (max-width: 800px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    width: min(300px, 85vw);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
  }
  .app.sidebar-open .sidebar { transform: none; }
  .app.sidebar-open .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.3); }
  .conv-item button.del { opacity: 1; }
  .chat-title { text-align: center; }
  .messages { padding: 4px 16px 24px; }
  .msg.assistant .content { font-size: 16.5px; }
  .pill select { max-width: 150px; }
  .app:not(.has-messages) .composer-wrap { margin-bottom: 8vh; }
}

/* ---------- Documents ---------- */
.conv-item button.open svg { vertical-align: -3px; margin-right: 6px; color: var(--muted); }
.doc-list { flex: 0 0 auto; max-height: 28vh; }
.icon-btn:disabled { opacity: 0.35; }
.icon-btn:disabled:hover { background: transparent; color: var(--muted); }

.menu {
  position: absolute; z-index: 40;
  min-width: 220px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 4px;
  display: flex; flex-direction: column;
}
.menu[hidden] { display: none; }
.menu button {
  border: 0; background: transparent;
  text-align: left; font-size: 14px;
  padding: 9px 12px; border-radius: 8px;
}
.menu button:hover { background: var(--hover); }
.menu button.danger { color: var(--danger); }

.attach { position: relative; }
.attach-btn { width: 32px; height: 32px; }
.attach-menu { bottom: calc(100% + 6px); left: -4px; }

.doc-chip {
  display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%;
  margin: 0 0 8px -4px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 13.5px;
  color: var(--text);
}
.doc-chip[hidden] { display: none; }
.doc-chip svg { color: var(--accent); flex-shrink: 0; }
.doc-chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-chip small { color: var(--muted); font-size: 12px; white-space: nowrap; }
.doc-chip:hover { border-color: var(--border-strong); }

.doc-card {
  display: flex; align-items: center; gap: 12px;
  width: min(100%, 340px);
  margin-top: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-family: var(--sans);
  text-align: left;
}
.doc-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }
.doc-card > svg { width: 28px; height: 28px; color: var(--accent); flex-shrink: 0; }
.doc-card-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.35; }
.doc-card-text strong { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card-text small { color: var(--muted); font-size: 12.5px; }

.doc-panel {
  position: relative;
  flex: 0 0 min(52vw, 880px);
  min-width: 0;
  display: flex; flex-direction: column;
  background: var(--sidebar);
  border-left: 1px solid var(--border);
}
.doc-panel[hidden] { display: none; }
.doc-head {
  display: flex; align-items: center; gap: 4px;
  height: 52px; padding: 0 8px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.doc-name {
  flex: 1; min-width: 0;
  font-weight: 600; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding: 0 4px;
}
.doc-head .pill select { max-width: 240px; }
.doc-head .pill[hidden] { display: none; }
.doc-more { position: relative; }
.doc-menu { top: calc(100% + 4px); right: 0; }

.doc-banner {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px;
  padding: 8px 14px;
  font-size: 13.5px;
  background: color-mix(in srgb, var(--accent) 10%, var(--bg));
  border-bottom: 1px solid var(--border);
}
.doc-banner[hidden] { display: none; }
.doc-banner .spark { width: 16px; height: 16px; color: var(--accent); animation: spin 2.4s linear infinite; }
.doc-banner-actions { display: inline-flex; gap: 6px; margin-left: auto; }
.doc-banner button {
  border: 1px solid var(--border-strong); background: var(--surface);
  border-radius: 8px; padding: 3px 10px; font-size: 13px;
}
.doc-banner button:hover { border-color: var(--accent); }

.doc-view { flex: 1; overflow: auto; overscroll-behavior: contain; position: relative; }
.doc-view.loading .doc-pages { opacity: 0.45; transition: opacity 0.2s; }
.doc-pages .docx-wrapper { background: transparent !important; padding: 16px 0 32px !important; }
.doc-pages .docx-wrapper > section.docx {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 6px 20px rgba(0, 0, 0, 0.08) !important;
  margin-bottom: 16px !important;
  color: #1f1e1d;
}
.doc-pages mark.doc-mark {
  background: rgba(255, 196, 0, 0.38);
  color: inherit;
  border-bottom: 2px solid rgba(214, 150, 0, 0.9);
  cursor: pointer;
}
.doc-empty, .doc-error {
  max-width: 420px; margin: 18vh auto 0; padding: 0 24px;
  text-align: center; color: var(--muted); font-size: 15px;
}
.doc-empty[hidden] { display: none; }

.doc-selbtn {
  position: absolute; left: 50%; bottom: 64px; z-index: 5;
  transform: translateX(-50%);
  display: inline-flex; align-items: center; gap: 6px;
  border: 0; border-radius: 999px;
  padding: 9px 16px;
  background: var(--text); color: var(--bg);
  font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow);
}
.doc-selbtn[hidden] { display: none; }

.doc-selbar, .doc-comments {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.doc-selbar { padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.doc-selbar[hidden], .doc-comments[hidden] { display: none; }
.doc-selbar .quote, .doc-comment .quote {
  font-size: 13px; color: var(--muted);
  border-left: 3px solid rgba(214, 150, 0, 0.9);
  padding-left: 8px;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
  white-space: pre-wrap;
}
.doc-selbar textarea {
  width: 100%; resize: none;
  border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.45;
  padding: 8px 10px; outline: none;
}
.doc-selbar textarea:focus { border-color: var(--accent); }
.doc-selbar .row, .doc-foot, .doc-comment .row { display: flex; align-items: center; gap: 8px; }
.spacer { flex: 1; }
.doc-panel button.primary, .doc-panel button.plain, .doc-selbar .row button {
  border: 1px solid var(--border-strong); background: var(--surface);
  border-radius: 10px; padding: 6px 12px; font-size: 14px;
}
.doc-panel button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.doc-panel button.primary:hover { background: var(--accent-hover); }
.doc-panel button.plain { border-color: transparent; background: transparent; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.doc-panel button.plain:hover { color: var(--text); background: var(--hover); }
.doc-panel.busy #docApply, .doc-panel.busy #docAskNow, .doc-panel.busy #docRestore { opacity: 0.5; pointer-events: none; }

.doc-foot { flex-shrink: 0; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--bg); }
.doc-foot .count:not(:empty) {
  background: var(--accent); color: var(--accent-text);
  border-radius: 999px; padding: 0 7px; font-size: 12px; line-height: 18px;
}
.doc-comments { max-height: 40%; overflow-y: auto; padding: 6px 10px; }
.doc-comments-empty { color: var(--muted); font-size: 14px; padding: 10px 4px; }
.doc-comment {
  padding: 10px; margin: 4px 0;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: border-color 0.2s;
}
.doc-comment.flash { border-color: var(--accent); }
.doc-comment.resolved { opacity: 0.6; }
.doc-comment .note { font-size: 14.5px; margin: 4px 0 6px; white-space: pre-wrap; word-break: break-word; }
.doc-comment .row { font-size: 12.5px; color: var(--faint); }
.doc-comment .state { flex: 1; }
.doc-comment .row button { border: 0; background: transparent; color: var(--muted); font-size: 12.5px; padding: 2px 6px; border-radius: 6px; }
.doc-comment .row button:hover { background: var(--hover); color: var(--text); }

@media (max-width: 999px) {
  /* Phones and narrow windows: the document covers the chat. */
  .doc-panel {
    position: fixed; inset: 0; z-index: 30;
    border-left: 0;
  }
  .doc-head .pill select { max-width: 150px; }
}
