:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel2: #eef0f3;
  --text: #15171a;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #0a84ff;
  --green: #22a55a;
  --blue: #0a84ff;
  --orange: #f08c00;
  --red: #e5484d;
  --purple: #8e5cf7;
  --bubble-me: #dcf3e4;
  --code: #f1f3f6;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d10;
    --panel: #15181d;
    --panel2: #1d2128;
    --text: #e8eaed;
    --muted: #8b929c;
    --line: #262b33;
    --bubble-me: #183b28;
    --code: #0f1216;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
#app { height: 100dvh; display: flex; flex-direction: column; }

.top { display: flex; align-items: center; gap: 10px; padding: calc(env(safe-area-inset-top) + 8px) 14px 8px;
  background: var(--panel); border-bottom: 1px solid var(--line); min-height: 52px; }
.top h1 { font-size: 18px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .sub { font-size: 12px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); flex: none; }
.dot.on { background: var(--green); } .dot.off { background: var(--red); }
.chip { font-size: 12px; padding: 3px 8px; border-radius: 999px; background: var(--panel2); color: var(--muted); white-space: nowrap; }
.chip.warn { color: var(--orange); }
.iconbtn { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; font-size: 18px; }
.iconbtn:active { background: var(--panel2); }

.main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.tabs { display: flex; background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); }
.tabs button { flex: 1; padding: 8px 0 6px; font-size: 11px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; position: relative; }
.tabs button.active { color: var(--accent); }
.tabs .ico { font-size: 20px; line-height: 1; }
.tabs .badge { position: absolute; top: 4px; left: calc(50% + 6px); }

.search { margin: 10px 12px 4px; display: flex; gap: 8px; }
.search input { flex: 1; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); outline: none; }
.filters { display: flex; gap: 6px; padding: 6px 12px; overflow-x: auto; scrollbar-width: none; }
.filters button { padding: 5px 11px; border-radius: 999px; background: var(--panel2); font-size: 13px; white-space: nowrap; }
.filters button.active { background: var(--accent); color: #fff; }

.row { display: flex; gap: 12px; padding: 10px 14px; align-items: center; border-bottom: 1px solid var(--line); background: var(--panel); }
.row:active { background: var(--panel2); }
.avatar { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; color: #fff;
  font-weight: 600; font-size: 16px; position: relative; flex: none; }
.avatar.stopped { opacity: .5; }
.avatar .st { position: absolute; right: -1px; bottom: -1px; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--panel); }
.st.idle { background: var(--green); } .st.busy { background: var(--blue); } .st.waiting { background: var(--orange); } .st.locked { background: var(--red); }
.row .body { flex: 1; min-width: 0; }
.row .line1 { display: flex; gap: 6px; align-items: baseline; }
.row .name { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.unread .name { font-weight: 800; }
.row .time { font-size: 12px; color: var(--muted); flex: none; }
.row.unread .time { color: var(--green); font-weight: 600; }
.row .line2 { display: flex; gap: 6px; align-items: center; margin-top: 2px; }
.row .preview { flex: 1; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview.busy { color: var(--blue); } .preview.waiting { color: var(--orange); } .preview.locked { color: var(--red); }
.badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--green); color: #fff;
  font-size: 12px; font-weight: 700; display: inline-grid; place-items: center; }
.pill { font-size: 10px; font-weight: 700; color: #fff; padding: 1px 5px; border-radius: 4px; }
.pill.cr { background: var(--blue); } .pill.openrouter { background: var(--purple); } .pill.personal { background: var(--green); }
.flag { font-size: 11px; color: var(--orange); }
.section { padding: 14px 14px 6px; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.card { margin: 10px 12px; padding: 12px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); }
.card h3 { margin: 0 0 4px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.card .meta { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.question { margin: 10px 0; }
.question .q { font-weight: 600; margin-bottom: 6px; }
.question .opts { display: flex; flex-direction: column; gap: 6px; }
.opt { text-align: left; padding: 10px 12px; border-radius: 10px; background: var(--panel2); border: 1.5px solid transparent; }
.opt.sel { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--panel2)); }
.opt small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.free { width: 100%; margin-top: 6px; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.btn { padding: 9px 14px; border-radius: 10px; background: var(--panel2); font-weight: 600; font-size: 14px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { color: var(--red); }
.btn:disabled { opacity: .5; }

.chat { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.msgs { flex: 1; overflow-y: auto; padding: 10px 10px 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 88%; padding: 8px 11px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line);
  word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--bubble-me); border-color: transparent; }
.msg.assistant { align-self: stretch; max-width: none; background: none; border: 0; padding: 2px 4px; }
.msg.assistant .ts { text-align: left; }
.older { text-align: center; color: var(--muted); font-size: 12px; padding: 6px; }
.msg .ts { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; }
.msg pre { background: var(--code); padding: 8px; border-radius: 8px; overflow-x: auto; font-size: 12.5px; margin: 6px 0; }
.msg code { background: var(--code); padding: 1px 4px; border-radius: 4px; font-size: 13px; }
.msg pre code { background: none; padding: 0; }
.msg p { margin: 0 0 6px; } .msg p:last-child { margin: 0; }
.msg ul, .msg ol { margin: 4px 0; padding-left: 20px; }
.msg table { border-collapse: collapse; font-size: 12.5px; display: block; overflow-x: auto; }
.msg td, .msg th { border: 1px solid var(--line); padding: 3px 6px; }
.tools { align-self: flex-start; font-size: 12.5px; color: var(--muted); padding: 2px 8px; }
.tools summary { cursor: pointer; }
.tools div { padding: 2px 0 2px 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 85vw; }
.composer { display: flex; gap: 8px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 140px; min-height: 40px; padding: 9px 12px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--bg); outline: none; }
.send { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 18px; flex: none; }
.statusbar { padding: 6px 12px; font-size: 13px; background: var(--panel2); color: var(--muted); display: flex; gap: 8px; align-items: center; }
.statusbar.waiting { color: var(--orange); } .statusbar.busy { color: var(--blue); }

.screen { white-space: pre; font: 11.5px/1.35 ui-monospace, Menlo, monospace; padding: 10px; overflow: auto; flex: 1; background: var(--code); }
.keys { display: flex; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); overflow-x: auto; background: var(--panel); border-top: 1px solid var(--line); }
.keys button { padding: 8px 12px; border-radius: 8px; background: var(--panel2); font: 600 13px ui-monospace, Menlo, monospace; flex: none; }
.diff { white-space: pre; font: 11.5px/1.4 ui-monospace, Menlo, monospace; padding: 10px; overflow: auto; }
.diff .add { color: var(--green); } .diff .del { color: var(--red); } .diff .hunk { color: var(--blue); } .diff .file { font-weight: 700; }

.bar { height: 8px; border-radius: 4px; background: var(--panel2); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; background: var(--accent); }
.urow { padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
.urow .l { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.urow .l span:last-child { color: var(--muted); font-variant-numeric: tabular-nums; }

.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; z-index: 20; }
.sheet .inner { background: var(--panel); width: 100%; border-radius: 18px 18px 0 0; padding: 8px 0 calc(10px + env(safe-area-inset-bottom)); max-height: 80dvh; overflow-y: auto; }
.sheet .item { display: block; width: 100%; text-align: left; padding: 13px 18px; font-size: 16px; }
.sheet .item.danger { color: var(--red); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(80px + env(safe-area-inset-bottom)); background: #222; color: #fff;
  padding: 9px 14px; border-radius: 10px; font-size: 14px; z-index: 30; max-width: 90vw; }
.login { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; text-align: center; }
.login img { width: 84px; height: 84px; border-radius: 20px; }
.login input { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); width: 260px; }
.hit { padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
.hit .t { font-size: 13px; color: var(--muted); }
.hit mark { background: color-mix(in srgb, var(--orange) 35%, transparent); color: inherit; }
.toggle { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
#shell { height: 100dvh; display: flex; }
#side { display: none; }
#app { flex: 1; min-width: 0; }
.row { cursor: pointer; }
@media (hover: hover) { .row:hover { background: var(--panel2); } .btn:hover, .iconbtn:hover { filter: brightness(1.08); } }
.row.active { background: color-mix(in srgb, var(--accent) 14%, var(--panel)); }
@media (min-width: 760px) {
  .sheet { align-items: center; justify-content: center; }
  .sheet .inner { max-width: 480px; border-radius: 18px; }
  .toast { bottom: 28px; }
}
@media (min-width: 760px) and (max-width: 899px) {
  #app { max-width: 720px; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}
@media (min-width: 900px) {
  #side { display: flex; flex-direction: column; width: clamp(320px, 30vw, 420px); flex: none; height: 100dvh;
    border-right: 1px solid var(--line); background: var(--panel); }
  #side .main { background: var(--panel); }
  #side .tabs button { flex-direction: row; justify-content: center; gap: 6px; font-size: 13px; padding: 10px 0; }
  #side .tabs .ico { font-size: 15px; }
  #side .tabs .badge { position: static; }
  #app .tabs, #app .top [data-back] { display: none; }
  #app .main > *, #app .composer, #app .statusbar, #app .keys, #app .attachments { width: 100%; max-width: 900px; margin-left: auto; margin-right: auto; }
  #app .main > .card { width: auto; max-width: 876px; }
  #app .main > .card, #app .main > .section { margin-left: max(12px, calc((100% - 876px) / 2)); margin-right: max(12px, calc((100% - 876px) / 2)); }
  .msgs { padding: 16px max(16px, calc((100% - 900px) / 2)); }
  .msg.user { max-width: min(80%, 760px); }
  .tools div { max-width: 700px; }
  .toggle.setting input, .toggle.setting select { max-width: 320px; }
}
.toggle .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }
.toggle.setting { gap: 12px; }
.toggle.setting input, .toggle.setting select { flex: none; max-width: 45%; font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel2); color: inherit; }
.toggle.setting input[type=number] { width: 80px; }
#stoken { flex: 1; font: 600 16px ui-monospace, monospace; letter-spacing: 1px; align-self: center; user-select: all; }
* { scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--muted) 45%, transparent) transparent; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--muted) 45%, transparent); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }
#app { overflow: hidden; }
.composer textarea { overflow-y: hidden; }
.attach { width: 40px; height: 40px; border-radius: 50%; background: var(--panel2); font-size: 24px; font-weight: 300; line-height: 1; flex: none; color: var(--muted); }
.attachments { display: flex; gap: 8px; padding: 0 10px; overflow-x: auto; background: var(--panel); }
.attachments:not(:empty) { padding: 8px 10px 0; border-top: 1px solid var(--line); }
.att { display: flex; align-items: center; gap: 6px; padding: 4px 4px 4px 4px; border-radius: 10px; background: var(--panel2); flex: none; max-width: 220px; }
.att img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.att .fi { font-size: 22px; padding: 0 6px; }
.att .an { font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.att button { width: 22px; height: 22px; border-radius: 50%; font-size: 11px; color: var(--muted); flex: none; }
.chat.dropping { outline: 2px dashed var(--accent); outline-offset: -6px; }
.msg.sending { opacity: .6; }
.send:disabled { opacity: .6; font-size: 11px; }
.ctools { display: flex; gap: 6px; padding: 6px 10px 0; overflow-x: auto; scrollbar-width: none; background: var(--panel); border-top: 1px solid var(--line); }
.ctools::-webkit-scrollbar { display: none; }
.tchip { flex: none; font-size: 12px; padding: 4px 9px; border-radius: 999px; background: var(--panel2); color: var(--muted); white-space: nowrap; }
.tchip.on { color: #fff; background: var(--green); }
.tchip.warn { color: var(--orange); }
.tchip.muted { background: none; }
.composer { border-top: 0; }
.mic { width: 40px; height: 40px; border-radius: 50%; background: var(--panel2); font-size: 17px; flex: none; }
.mic.rec { background: var(--red); color: #fff; font-size: 12px; font-weight: 700; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .65; } }
.palette { max-height: 42dvh; overflow-y: auto; background: var(--panel); border-top: 1px solid var(--line); box-shadow: 0 -6px 18px rgba(0,0,0,.12); }
.pi { padding: 8px 14px; cursor: pointer; border-bottom: 1px solid var(--line); }
.pi.sel, .pi:hover { background: color-mix(in srgb, var(--accent) 14%, var(--panel)); }
.pi .k { margin-left: 8px; font-size: 11px; color: var(--muted); }
.pi .d { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.statusbar .act { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.termcard { padding: 8px 12px; background: var(--panel); border-top: 1px solid var(--line); }
.termcard .meta { color: var(--orange); font-size: 12.5px; margin-bottom: 4px; }
.termcard pre { margin: 0; max-height: 30dvh; overflow: auto; font: 11.5px/1.35 ui-monospace, Menlo, monospace; background: var(--code); padding: 8px; border-radius: 8px; white-space: pre-wrap; }
.tkeys { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tkeys button { padding: 7px 12px; border-radius: 8px; background: var(--panel2); font: 600 13px ui-monospace, Menlo, monospace; }
.files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.thumb { max-width: 180px; max-height: 140px; border-radius: 8px; display: block; background: var(--panel2); min-width: 40px; min-height: 40px; }
.fchip { font-size: 12.5px; padding: 3px 8px; border-radius: 8px; background: color-mix(in srgb, var(--text) 8%, transparent); }
body.embed #side, body.embed .tabs { display: none !important; }
@media (min-width: 900px) {
  #app .ctools, #app .palette, #app .termcard { width: 100%; max-width: 900px; margin-left: auto; margin-right: auto; }
}
body.embed #chathead { display: none; }
.now { height: 40px; padding: 0 10px; border-radius: 20px; background: var(--orange); color: #fff; font-size: 13px; font-weight: 700; flex: none; }
.msg.queued { opacity: .85; border: 1px dashed color-mix(in srgb, var(--green) 60%, transparent); }
.listroot { display: block; }
#side { overflow: hidden; }
@media (max-width: 899px) {
  .top { min-height: 46px; padding-top: calc(env(safe-area-inset-top) + 6px); padding-bottom: 6px; }
  .top h1 { font-size: 16.5px; }
  .top .sub { font-size: 11.5px; }
  .msgs { gap: 6px; padding: 8px 8px 12px; }
  .msg .ts { font-size: 10.5px; margin-top: 2px; }
  .msg.assistant .ts { opacity: .7; }
  .tools { padding: 0 6px; }
  .statusbar { padding: 4px 10px; font-size: 12.5px; }
  .statusbar .btn { padding: 5px 10px; font-size: 13px; }
  .ctools { gap: 5px; padding: 5px 8px 0; }
  .tchip { font-size: 11.5px; padding: 3px 9px; }
  .composer { gap: 6px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
  .composer textarea { min-height: 38px; padding: 8px 12px; }
  .attach, .send { width: 38px; height: 38px; }
  .attach { font-size: 22px; }
}
.send[data-mode="mic"] { background: var(--panel2); color: var(--text); font-size: 17px; }
.send.rec { background: var(--red); color: #fff; font-size: 12px; font-weight: 700; animation: pulse 1.2s ease-in-out infinite; }
.sendnow { margin-right: 8px; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 600; }
.msg .ts { display: flex; justify-content: flex-end; align-items: center; }
.msg.assistant .ts { justify-content: flex-start; }
.ic { width: 18px; height: 18px; flex: none; vertical-align: -4px; }
.tchip .ic { width: 14px; height: 14px; vertical-align: -2.5px; margin-right: 2px; }
.tchip { display: inline-flex; align-items: center; gap: 2px; }
.send .ic, .attach .ic { width: 20px; height: 20px; }
.tabs .ico .ic { width: 22px; height: 22px; }
.sheet .item .ic { margin-right: 8px; }
.att .fi .ic { width: 22px; height: 22px; }
.stopbtn { color: var(--red); }
.spin { display: inline-block; width: 12px; height: 12px; margin-right: 7px; border: 2px solid color-mix(in srgb, var(--blue) 30%, transparent); border-top-color: var(--blue); border-radius: 50%; vertical-align: 1px; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.top .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
