:root {
  --bg: #f6f6fb; --card: #ffffff; --ink: #1b1b2f; --muted: #6b6b85; --line: #e4e4ef;
  --accent: #6d5efc; --accent-ink: #ffffff; --me: #6d5efc; --me-ink: #fff; --bot: #ececf6;
  --ok: #1f9d63; --warn: #c27a00; --bad: #d63a4a; --shadow: 0 1px 2px rgba(20,20,60,.06), 0 4px 16px rgba(20,20,60,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1020; --card: #181a30; --ink: #ececf8; --muted: #9a9ab8; --line: #2a2c48;
    --bot: #20233d; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1020; --card: #181a30; --ink: #ececf8; --muted: #9a9ab8; --line: #2a2c48;
  --bot: #20233d; --shadow: none; color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-top: env(safe-area-inset-top, 0px);
}
#app { height: 100%; display: flex; flex-direction: column; max-width: 760px; margin: 0 auto; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hdr {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--bg);
  border-bottom: 1px solid var(--line); min-height: 56px;
}
.hdr h1 { font-size: 20px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hdr .sub { font-size: 12px; color: var(--muted); font-weight: 400; display: block; }
.main { flex: 1; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; }
.tabs {
  display: flex; border-top: 1px solid var(--line); background: var(--card);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabs a {
  flex: 1; text-align: center; padding: 9px 4px 7px; text-decoration: none; color: var(--muted);
  font-size: 11px; display: flex; flex-direction: column; gap: 2px; align-items: center;
}
.tabs a .ic { font-size: 20px; line-height: 1; }
.tabs a.on { color: var(--accent); font-weight: 600; }
.icon-btn {
  border: 0; background: transparent; font-size: 20px; width: 40px; height: 40px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: var(--ink);
}
.icon-btn:active { background: var(--line); }
.btn {
  border: 0; background: var(--accent); color: var(--accent-ink); padding: 10px 18px; border-radius: 12px;
  font-weight: 600; text-decoration: none; display: inline-block;
}
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn.danger { background: var(--bad); }
.btn.small { padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .5; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.pcard {
  background: var(--card); border-radius: 18px; padding: 16px 12px 12px; text-align: center; position: relative;
  box-shadow: var(--shadow); border: 1px solid var(--line); cursor: pointer;
}
.pcard .edit { position: absolute; top: 6px; right: 6px; font-size: 16px; opacity: .7; }
.pcard .name { font-weight: 700; margin-top: 8px; }
.pcard .title { font-size: 13px; color: var(--muted); min-height: 18px; }
.pcard.add { display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); border-style: dashed; min-height: 140px; }
.av {
  width: 64px; height: 64px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-size: 32px; color: #fff;
}
.av.sm { width: 36px; height: 36px; font-size: 18px; }
.av.xl { width: 110px; height: 110px; font-size: 56px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); font-size: 13px; }
.pill { font-size: 12px; padding: 2px 9px; border-radius: 99px; background: var(--line); color: var(--muted); font-weight: 600; }
.pill.running { background: #dfe9ff; color: #2954c8; }
.pill.done { background: #d9f4e6; color: var(--ok); }
.pill.failed, .pill.cancelled { background: #fde1e4; color: var(--bad); }
.pill.queued { background: #fff0d1; color: var(--warn); }
@media (prefers-color-scheme: dark) { .pill.running{background:#1d2b57;color:#8fb0ff}.pill.done{background:#12382a;color:#6fe0a8}.pill.failed,.pill.cancelled{background:#4a1a22;color:#ff8b98}.pill.queued{background:#4a3510;color:#ffcb6b} }
label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 4px; color: var(--muted); }
input[type=text], input[type=email], input[type=number], select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 90px; }
.msgs { flex: 1; overflow-y: auto; padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 86%; padding: 9px 13px; border-radius: 18px; word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--me); color: var(--me-ink); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--bot); border-bottom-left-radius: 6px; }
.msg.assistant.agent { border-left: 3px solid var(--ok); }
.msg .src { font-size: 11px; opacity: .65; display: block; margin-top: 3px; }
.msg pre { background: rgba(0,0,0,.12); padding: 8px 10px; border-radius: 10px; overflow-x: auto; white-space: pre; margin: 6px 0; font-size: 13px; }
.msg code { font-family: ui-monospace, Consolas, monospace; font-size: .92em; background: rgba(0,0,0,.1); padding: 1px 4px; border-radius: 5px; }
.msg pre code { background: none; padding: 0; }
.msg.user a { color: #fff; }
.note { align-self: center; font-size: 12px; color: var(--muted); background: var(--card); border: 1px solid var(--line); padding: 4px 12px; border-radius: 99px; text-decoration: none; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.composer {
  display: flex; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--line);
  background: var(--bg); align-items: flex-end;
}
.composer textarea { min-height: 44px; max-height: 140px; border-radius: 22px; padding: 10px 16px; resize: none; }
.send { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 20px; flex: none; }
.send[disabled] { opacity: .45; }
.login { margin: auto; width: min(380px, 100%); padding: 24px 16px; text-align: center; }
.login .logo { width: 88px; height: 88px; border-radius: 24px; margin-bottom: 14px; }
.login input { text-align: center; font-size: 18px; margin: 14px 0 10px; }
.code { letter-spacing: 8px; font-size: 26px !important; }
.err { color: var(--bad); font-size: 14px; min-height: 20px; }
#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  background: #222; color: #fff; padding: 10px 18px; border-radius: 99px; font-size: 14px; z-index: 50; max-width: 90vw;
}
.call {
  position: fixed; inset: 0; z-index: 40; display: flex; flex-direction: column; align-items: center; background: var(--bg);
  padding: calc(24px + env(safe-area-inset-top, 0px)) 16px calc(24px + env(safe-area-inset-bottom, 0px));
}
.call .who { margin-top: 4vh; text-align: center; }
.call .state { color: var(--muted); margin-top: 6px; min-height: 22px; }
.call .ring { border-radius: 50%; padding: 10px; transition: box-shadow .2s; }
.call .ring.talk { box-shadow: 0 0 0 10px rgba(109,94,252,.25), 0 0 0 22px rgba(109,94,252,.12); }
.call .live { flex: 1; width: 100%; max-width: 560px; overflow-y: auto; margin: 16px 0; display: flex; flex-direction: column; gap: 6px; font-size: 15px; }
.call .live .l { padding: 6px 10px; border-radius: 12px; max-width: 92%; }
.call .live .l.user { align-self: flex-end; background: var(--me); color: #fff; }
.call .live .l.assistant { align-self: flex-start; background: var(--bot); }
.call .btns { display: flex; gap: 22px; }
.round { width: 64px; height: 64px; border-radius: 50%; border: 0; font-size: 26px; background: var(--card); box-shadow: var(--shadow); border: 1px solid var(--line); }
.round.end { background: var(--bad); color: #fff; border: 0; }
.round.off { background: var(--line); }
.log { font: 12.5px/1.5 ui-monospace, Consolas, monospace; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; max-height: 50vh; overflow: auto; }
.log .t { color: var(--ink); white-space: pre-wrap; margin-bottom: 6px; }
.log .tool { color: var(--accent); margin-bottom: 4px; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.sw { position: relative; width: 44px; height: 26px; flex: none; }
.sw input { opacity: 0; width: 100%; height: 100%; position: absolute; margin: 0; z-index: 1; }
.sw i { position: absolute; inset: 0; background: var(--line); border-radius: 99px; transition: .2s; }
.sw i::after { content: ""; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.sw input:checked + i { background: var(--accent); }
.sw input:checked + i::after { transform: translateX(18px); }
.mem { display: flex; gap: 8px; align-items: flex-start; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }

.msg.assistant { white-space: normal; }
.msg p { margin: 0 0 .55em; } .msg p:last-child { margin-bottom: 0; }
.msg h4 { margin: .7em 0 .3em; font-size: 1em; } .msg h4:first-child { margin-top: 0; }
.msg ul, .msg ol { margin: .3em 0 .55em; padding-left: 1.3em; }
.msg hr { border: 0; border-top: 1px solid var(--line); margin: .6em 0; }
.tbl { overflow-x: auto; margin: .4em 0 .6em; max-width: 100%; }
.msg table { border-collapse: collapse; font-size: 12.5px; white-space: normal; }
.msg th, .msg td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.msg th { background: rgba(0,0,0,.06); }
.call .live details.agentres { max-width: 100%; width: 100%; background: transparent; padding: 0; }
.call .live details.agentres summary { cursor: pointer; padding: 6px 10px; border-radius: 12px; background: var(--bot); display: inline-block; font-size: 14px; }
.call .live .mdbox { max-width: 100%; margin-top: 6px; max-height: 40vh; overflow: auto; font-size: 14px; }
.msg table { width: max-content; min-width: 100%; white-space: nowrap; }
.msg td { white-space: normal; min-width: 5.5em; max-width: 16em; }
.msg td:first-child, .msg th { white-space: nowrap; }
.msg.assistant.agent { max-width: 98%; }
