:root {
  --ink: #1c1410;
  --paper: #f4ead8;
  --panel: #fff8ee;
  --cinnabar: #b23a2d;
  --moss: #3d5a45;
  --line: rgba(28, 20, 16, 0.12);
  --muted: #7a6a58;
  --shadow: 0 12px 36px rgba(67, 42, 25, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body.sheet-open .composer { visibility: hidden; pointer-events: none; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(900px 460px at 18% -8%, rgba(255, 222, 178, 0.78), transparent 68%),
    var(--paper);
  color: var(--ink);
}

#app {
  max-width: 820px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; gap: 10px; align-items: center; }
.seal {
  width: 40px; height: 40px; border-radius: 6px;
  background: var(--cinnabar); color: #fff8ee;
  display: grid; place-items: center;
  font-weight: 700; letter-spacing: 0.1em;
}
h1 { font-size: 18px; margin: 0; font-weight: 700; }
#statsLine { margin: 2px 0 0; font-size: 12px; color: var(--muted); }

.ghost, .composer button, .sheet-panel button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 10px;
  padding: 8px 12px;
  font: inherit;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}
.ghost:hover, .composer button:hover:not(:disabled), .sheet-panel button:hover { transform: translateY(-1px); }
button:focus-visible, textarea:focus-visible, input:focus-visible {
  outline: 3px solid rgba(178, 58, 45, 0.2);
  outline-offset: 2px;
}

.agent-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 10px 0 4px;
}
.agent-switch button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 8px;
  font: inherit;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, border-color 140ms ease;
}
.agent-switch button:disabled { cursor: not-allowed; opacity: 0.55; }
.agent-switch button.on {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.sources {
  display: flex; flex-wrap: wrap; gap: 7px; padding: 8px 0;
  font-size: 14px; color: var(--moss);
}
.sources label {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 248, 238, 0.72);
  cursor: pointer;
}
.sources label:has(input:checked) { border-color: rgba(61, 90, 69, 0.42); background: #e8eee5; }
.sources input { accent-color: var(--moss); }
.run-status {
  margin: 0 0 6px;
  font-size: 12px;
  color: var(--muted);
  min-height: 1.2em;
}
.run-status.busy { color: var(--cinnabar); }
.run-status.busy::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.28; transform: scale(0.75); } }
.queue-bar {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 0 8px;
  font-size: 12px;
}
.queue-item {
  background: #efe3cc;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
  color: var(--moss);
}

.thread {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 2px 18px;
  scroll-behavior: smooth;
}
.msg {
  max-width: min(92%, 680px);
  padding: 11px 14px;
  border-radius: 16px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--ink);
  color: var(--paper);
  border-bottom-right-radius: 5px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--line);
  white-space: normal;
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow);
}
.msg.bot h1, .msg.bot h2, .msg.bot h3 {
  margin: 0.4em 0 0.3em;
  font-size: 1.05em;
}
.msg.bot p { margin: 0.35em 0; }
.msg.bot ul, .msg.bot ol { margin: 0.35em 0; padding-left: 1.2em; }
.msg.bot code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
}
.msg.sys {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.cites {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  padding: 2px 1px 9px;
  scrollbar-width: thin;
}
.cite {
  flex: 0 0 auto;
  font-size: 12px;
  border: 1px solid var(--line);
  background: #efe3cc;
  border-radius: 999px;
  padding: 4px 8px;
  color: var(--moss);
}

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  position: sticky;
  bottom: 0;
  z-index: 10;
  margin: 0 -4px -4px;
  padding: 8px 4px 4px;
  background: linear-gradient(180deg, rgba(244, 234, 216, 0), var(--paper) 24%);
}
.input-shell {
  flex: 1;
  position: relative;
}
.composer textarea {
  display: block;
  width: 100%;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  font-size: 16px;
  line-height: 1.5;
  padding: 10px 13px 26px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-family: inherit;
  box-shadow: 0 8px 30px rgba(67, 42, 25, 0.06);
}
.composer textarea:focus { border-color: rgba(178, 58, 45, 0.55); }
.key-hint {
  position: absolute;
  right: 11px;
  bottom: 7px;
  color: var(--muted);
  font-size: 11px;
  pointer-events: none;
}
.composer button {
  background: var(--cinnabar);
  color: #fff8ee;
  border: 0;
  min-height: 44px;
  min-width: 76px;
  font-weight: 650;
}
.composer button:disabled { cursor: not-allowed; opacity: 0.42; }

.sheet {
  position: fixed; inset: 0;
  z-index: 200;
  background: rgba(28, 20, 16, 0.4);
  display: grid;
  place-items: center;
  padding: 16px;
}
.sheet[hidden] { display: none !important; }
.sheet-panel {
  width: min(720px, 100%);
  max-height: min(86vh, 640px);
  overflow: auto;
  background: var(--panel);
  border-radius: 16px;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  display: grid; gap: 10px;
  position: relative;
  z-index: 201;
}
.sheet-panel label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.sheet-panel input {
  font-size: 16px; padding: 8px 10px;
  border-radius: 10px; border: 1px solid var(--line);
  font-family: inherit;
}
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.sheet-panel button {
  background: var(--ink);
  color: var(--paper);
  min-height: 44px;
  width: 100%;
  cursor: pointer;
  pointer-events: auto;
}

@media (max-width: 560px) {
  #app { padding: 8px 9px calc(9px + env(safe-area-inset-bottom)); }
  .seal { width: 36px; height: 36px; }
  .top { padding: 2px 1px; }
  .sources { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 9px; }
  .sources label { flex: 0 0 auto; min-height: 32px; font-size: 13px; }
  .msg { max-width: 94%; }
  .key-hint { display: none; }
  .composer textarea { padding-bottom: 10px; }
  .composer button { min-width: 62px; padding-inline: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
