:root {
  --bg: #f6f7f9; --card: #ffffff; --text: #1b1f24; --muted: #667085; --line: #d9dde3;
  --accent: #1f5fbf; --accent-text: #ffffff; --user: #e3edfb; --error: #b42318; --code: #eef0f3;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14171c; --card: #1c2128; --text: #e6e9ee; --muted: #98a2b3; --line: #2d343d;
          --accent: #5b9bff; --accent-text: #0b1220; --user: #22344f; --error: #ff8a7a; --code: #252c35; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.65 system-ui, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
       display: flex; flex-direction: column; height: 100vh; }
body.center { align-items: center; justify-content: center; padding: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: 100%; max-width: 360px; }
h1 { margin: 0 0 4px; font-size: 22px; }
.muted { color: var(--muted); }
.error { color: var(--error); margin: 12px 0 0; }
label { display: block; margin-top: 14px; font-size: 14px; color: var(--muted); }
input, textarea { width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px;
                  background: var(--bg); color: var(--text); font: inherit; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; padding: 10px 18px; background: var(--accent); color: var(--accent-text); }
button:disabled { opacity: .55; cursor: wait; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); padding: 6px 12px; }
.card button[type=submit] { width: 100%; margin-top: 20px; }
.bar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line); }
.bar form { margin: 0; }
.grow { flex: 1; }
.banner { margin: 0; padding: 8px 16px; background: #fff3cd; color: #664d03; }
#log { flex: 1; overflow-y: auto; padding: 16px; max-width: 900px; width: 100%; margin: 0 auto; }
.msg { margin: 0 0 14px; padding: 10px 14px; border-radius: 12px; max-width: 92%; overflow-wrap: anywhere; }
.msg.user { background: var(--user); margin-left: auto; white-space: pre-wrap; }
.msg.assistant { background: var(--card); border: 1px solid var(--line); }
.msg.error { background: var(--card); border: 1px solid var(--error); color: var(--error); }
.msg p { margin: 0 0 .6em; } .msg p:last-child { margin-bottom: 0; }
.msg h2, .msg h3 { margin: .8em 0 .4em; font-size: 1.05em; }
.msg ul, .msg ol { margin: .3em 0 .6em; padding-left: 1.4em; }
.msg pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 14px; }
.msg code { background: var(--code); padding: 1px 4px; border-radius: 4px; }
.msg table { display: block; overflow-x: auto; border-collapse: collapse; margin: .5em 0; font-size: 14px; }
.msg th, .msg td { border: 1px solid var(--line); padding: 3px 6px; text-align: center; white-space: nowrap; }
.msg a { color: var(--accent); }
.composer { display: flex; gap: 10px; padding: 10px 16px 4px; max-width: 900px; width: 100%; margin: 0 auto; align-items: flex-end; }
.composer textarea { resize: vertical; max-height: 200px; }
select { padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text); font: inherit; max-width: 55vw; }
a.btnlink { color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; text-decoration: none; font-size: 15px; }
body.admin { height: auto; min-height: 100vh; }
.adminmain { max-width: 1100px; width: 100%; margin: 0 auto; padding: 8px 16px 40px; }
.adminmain h2 { margin: 28px 0 4px; font-size: 19px; }
.adminmain p { margin: 4px 0 10px; }
.status { min-height: 1.6em; margin: 8px 0 0; color: var(--accent); }
.status.bad { color: var(--error); }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; align-items: end; margin-bottom: 14px; }
.grid label { margin-top: 0; }
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 15px; background: var(--card); }
th, td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; overflow-wrap: anywhere; }
th { background: var(--bg); white-space: nowrap; }
details summary { cursor: pointer; color: var(--accent); }
.edit { display: grid; gap: 8px; margin-top: 8px; min-width: 230px; }
.edit label { margin-top: 0; }
.checks { display: grid; gap: 4px; }
label.check { display: flex; align-items: center; gap: 6px; font-size: 15px; color: var(--text); margin-top: 0; }
label.check input, td input[type=checkbox] { width: auto; margin: 0; }
button.danger { background: transparent; color: var(--error); border: 1px solid var(--error); }
button.icon { padding: 8px 12px; font-size: 18px; line-height: 1; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; max-width: 900px; width: 100%; margin: 0 auto; padding: 4px 16px 0; }
.chips:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--user); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-size: 14px; max-width: 100%; overflow-wrap: anywhere; }
.chip.pending { color: var(--muted); }
.chip.err { background: transparent; border-color: var(--error); color: var(--error); }
.chip .x { background: transparent; color: var(--muted); border: 0; padding: 0 2px; font-size: 13px; cursor: pointer; }
.note { margin: 0; padding: 4px 16px 10px; text-align: center; font-size: 12px; color: var(--muted); }

/* billing */
.wallet { font-size: 14px; padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; white-space: nowrap; cursor: default; }
.wallet.low { border-color: var(--error); color: var(--error); }
.msg .cost { margin: 6px 0 0; font-size: 12px; }
fieldset.bill { border: 1px solid var(--line); border-radius: 8px; margin: 10px 0 0; padding: 8px 10px; display: grid; gap: 6px; }
fieldset.bill legend { padding: 0 4px; font-weight: 600; }
.ledger h4 { margin: 10px 0 4px; font-size: 14px; }
.ledger table { font-size: 13px; }
details.pricing { margin-top: 12px; }
details.pricing summary { cursor: pointer; font-weight: 600; }
details.pricing h4 { margin: 12px 0 4px; font-size: 14px; }
details.pricing ul { margin: 0; padding-left: 20px; }
details.pricing table { font-size: 13px; }

.msg.error .retry { margin-top: 8px; padding: 4px 12px; font-size: 14px; }
#health h3 { margin: 14px 0 6px; font-size: 15px; }
#health td.ok { color: inherit; white-space: nowrap; }
#health td.bad { color: var(--error); white-space: nowrap; font-weight: 600; }
.note .ver { margin-left: 8px; opacity: .6; }
#users .left { font-weight: 600; }
#users .left.low { color: var(--error); }

/* saved conversations */
.titlebtn { background: none; border: 0; color: inherit; font: inherit; max-width: 32vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; opacity: .85; padding: 2px 6px; }
.titlebtn:hover { opacity: 1; text-decoration: underline; }
.threads { position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 100vw); z-index: 20; background: var(--card); border-left: 1px solid var(--line); padding: 12px 14px; overflow-y: auto; box-shadow: -4px 0 16px rgba(0,0,0,.25); }
.threads-head { display: flex; justify-content: space-between; align-items: center; }
.threads .small, .small { font-size: 12px; }
#thread-list { list-style: none; margin: 8px 0 0; padding: 0; }
.thread { display: flex; align-items: center; gap: 4px; border-bottom: 1px solid var(--line); }
.thread.current { background: rgba(127,127,127,.12); border-radius: 6px; }
.thread-open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; color: inherit; padding: 8px 6px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.thread-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.notice { opacity: .8; font-size: 14px; }

/* live answer */
.msg.thinking { display: flex; align-items: center; gap: 8px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .3; animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .2s; } .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.msg.typing .typed { white-space: pre-wrap; display: inline; }
.caret { display: inline-block; width: 2px; height: 1.1em; margin-left: 2px; vertical-align: text-bottom; background: currentColor; animation: caret 1s steps(1) infinite; }
@keyframes caret { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .dots i, .caret { animation: none; opacity: .7; } }

.cost .why { background: none; border: 0; padding: 0 2px; font: inherit; color: inherit; text-decoration: underline dotted; cursor: pointer; opacity: .9; }
.cost-info { margin: 4px 0 0; font-size: 12px; line-height: 1.6; }

/* chat layout: conversation sidebar + chat column (like a chat app) */
body.chat { display: block; height: 100vh; overflow: hidden; }
.app { display: flex; height: 100vh; }
.chatcol { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100vh; }
.sidebar { width: 268px; flex: 0 0 268px; display: flex; flex-direction: column; background: var(--card); border-right: 1px solid var(--line); height: 100vh; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 6px; }
.side-head .icon, .bar .icon { padding: 4px 10px; }
.newchat { margin: 6px 12px 8px; text-align: left; background: transparent; color: var(--text); border: 1px solid var(--line); }
.newchat:hover { background: var(--bg); }
.thread-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.thread-group { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; }
.thread { display: flex; align-items: center; border-radius: 8px; }
.thread:hover, .thread.current { background: var(--bg); }
.thread.current .thread-open { font-weight: 600; }
.thread.stale .thread-open { opacity: .7; }
.thread-open { flex: 1; min-width: 0; text-align: left; background: none; color: inherit; padding: 7px 8px; border-radius: 8px;
               overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.thread-act { background: none; color: var(--muted); padding: 4px 6px; font-size: 13px; visibility: hidden; }
.thread:hover .thread-act, .thread.current .thread-act, .thread-act:focus { visibility: visible; }
.thread-act:hover { color: var(--text); }
.thread-edit { margin: 2px 0; padding: 6px 8px; font-size: 14px; }
.side-foot { padding: 8px 14px; border-top: 1px solid var(--line); font-size: 13px; display: flex; justify-content: space-between; }
.side-foot .ver { opacity: .7; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 29; }
#side-open { display: none; }
.app.collapsed .sidebar { display: none; }
.app.collapsed #side-open { display: inline-block; }
.empty { padding: 8px; }
@media (max-width: 900px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s ease; width: min(300px, 85vw); flex-basis: auto; }
  .app.drawer-open .sidebar { transform: none; box-shadow: 4px 0 16px rgba(0,0,0,.25); }
  .app .sidebar, .app.collapsed .sidebar { display: flex; }
  #side-open, .app.collapsed #side-open { display: inline-block; }
  .thread-act { visibility: visible; }
  .bar { gap: 8px; padding: 8px 10px; }
  .titlebtn { max-width: 40vw; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
