:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --text: #1c1c1f;
  --muted: #66666d;
  --line: #e2e2de;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --chip: #e8eefa;
  --user: #e9edf3;
  --danger: #b3261e;
  --danger-bg: #fbeceb;
  --code: #f0f0ed;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131315;
    --surface: #1c1c1f;
    --text: #ececef;
    --muted: #a0a0a8;
    --line: #2f2f34;
    --accent: #8ab0ff;
    --accent-text: #0e1b33;
    --chip: #22304a;
    --user: #262a31;
    --danger: #ffb4ab;
    --danger-bg: #3a1d1b;
    --code: #26262a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, "PingFang TC", "PingFang SC",
        "Noto Sans CJK TC", "Noto Sans CJK SC", "Microsoft JhengHei", "Microsoft YaHei", sans-serif;
}
.wrap { width: 100%; max-width: 780px; margin: 0 auto; padding: 0 16px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
button, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* header */
.top {
  position: sticky; top: 0; z-index: 2;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.bar { display: flex; align-items: center; gap: 12px; padding-top: 10px; padding-bottom: 10px; }
.brand { flex: 1; min-width: 0; }
.brand h1 { margin: 0; font-size: 1.05rem; line-height: 1.3; }
.brand p { margin: 0; font-size: .8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tools { display: flex; gap: 8px; align-items: center; }
select, .ghost {
  height: 34px; padding: 0 10px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.ghost:hover, select:hover { border-color: var(--muted); }

/* conversation */
main { flex: 1; padding-top: 20px; padding-bottom: 24px; }
.intro { padding: 8vh 0 0; }
.intro h2 { font-size: 1.35rem; margin: 0 0 6px; }
.intro p { margin: 0 0 20px; color: var(--muted); }
.examples { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; }
@media (max-width: 560px) { .examples { grid-template-columns: 1fr; } }
.examples button {
  text-align: left; padding: 10px 12px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); line-height: 1.4;
}
.examples button:hover { border-color: var(--accent); }

.turn { margin-bottom: 28px; }
.q {
  margin-left: auto; width: fit-content; max-width: 85%;
  background: var(--user); border-radius: var(--radius) var(--radius) 4px var(--radius);
  padding: 9px 14px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.a { margin-top: 14px; overflow-wrap: anywhere; }
.a > :first-child { margin-top: 0; }
.a p, .a ul, .a ol, .a pre, .a .tablewrap { margin: 0 0 .8em; }
.a ul, .a ol { padding-left: 1.4em; }
.a li + li { margin-top: .2em; }
.a h3, .a h4, .a h5, .a h6 { margin: 1em 0 .4em; font-size: 1rem; }
.a code { background: var(--code); padding: .1em .35em; border-radius: 5px; font-size: .9em; }
.a pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; }
.a pre code { background: none; padding: 0; }
.a hr { border: 0; border-top: 1px solid var(--line); }
.tablewrap { overflow-x: auto; }
.a table { border-collapse: collapse; font-size: .92em; min-width: 50%; }
.a th, .a td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.a th { background: var(--surface); }
.cite {
  display: inline-block; min-width: 1.3em; margin-left: .12em; padding: 0 .25em;
  font-size: .68em; line-height: 1.5; vertical-align: .3em; text-align: center;
  border-radius: .6em; background: transparent; color: var(--muted); text-decoration: none;
  border: 1px solid var(--line);
}
a.cite:hover { color: var(--accent); border-color: var(--accent); }

.status { color: var(--muted); font-size: .92rem; }
.status::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.rewritten { font-size: .8rem; color: var(--muted); margin: 0 0 8px; }
.notice { font-size: .9rem; color: var(--muted); font-style: italic; }
.error {
  margin-top: 10px; padding: 9px 12px; border-radius: 9px;
  background: var(--danger-bg); color: var(--danger); font-size: .92rem;
}

.sources { margin-top: 8px; font-size: .86rem; }
.sources h3 { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 4px; }
.sources ol { list-style: none; padding: 0; margin: 0; }
.sources li { display: flex; gap: 8px; padding: 3px 0; line-height: 1.4; }
.sources .n { flex: none; min-width: 1.6em; color: var(--muted); font-variant-numeric: tabular-nums; }
.sources .t { min-width: 0; }
.sources .u { display: block; color: var(--muted); font-size: .9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sources details { margin-top: 6px; }
.sources summary { cursor: pointer; color: var(--muted); }

.actions { display: flex; gap: 6px; align-items: center; margin-top: 8px; }
.actions button {
  font-size: .82rem; padding: 3px 10px; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--line); background: transparent; color: var(--muted);
}
.actions button:hover { color: var(--text); border-color: var(--muted); }
.actions .srcbtn::after { content: " ▸"; }
.actions .srcbtn[aria-expanded=true]::after { content: " ▾"; }

/* composer */
.composer {
  position: sticky; bottom: 0; z-index: 2;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  padding: 10px 0 calc(8px + env(safe-area-inset-bottom));
}
.box {
  display: flex; gap: 8px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px 6px 6px 12px;
}
.box:focus-within { border-color: var(--accent); }
textarea {
  flex: 1; min-width: 0; resize: none; border: 0; outline: none; background: transparent;
  font-size: 16px; line-height: 1.5; padding: 6px 0; max-height: 40vh;
}
#send {
  flex: none; height: 38px; min-width: 72px; padding: 0 14px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
#send:disabled { opacity: .45; cursor: default; }
#send.stop { background: var(--text); color: var(--bg); }
.meta { display: flex; gap: 12px; justify-content: space-between; font-size: .74rem; color: var(--muted); padding: 6px 2px 0; line-height: 1.4; }
#count { flex: none; font-variant-numeric: tabular-nums; }
#count.over { color: var(--danger); }

@media (max-width: 520px) {
  .brand p { display: none; }
  .q { max-width: 92%; }
}
@media (prefers-reduced-motion: reduce) {
  .status::after { animation: none; content: "…"; }
}
