:root {
  --bg: #f7f6f3; --panel: #fff; --text: #1d1c1a; --muted: #74716a; --line: #e2dfd8; --hover: #ecebe6;
  --accent: #d97757; --black: #111; --user-bubble: #ecebe6; --code-bg: #f1efea; --ok: #3a9d5d; --bad: #d0463b;
  --font: -apple-system, "Segoe UI", Inter, Roboto, system-ui, sans-serif; --mono: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1c1b19; --panel: #262523; --text: #ecebe7; --muted: #9b978e; --line: #34322f; --hover: #2f2d2a; --black: #f4f3ef; --user-bubble: #2e2c29; --code-bg: #141312; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); }
body { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 13px; } .spacer { flex: 1; }
.err-text { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }
.warn-text { color: #b7791f; }
code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.screen { display: none; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

.btn { border: 1px solid var(--line); background: var(--panel); padding: 10px 16px; border-radius: 14px; }
.btn:active { background: var(--hover); }
.btn.primary { background: var(--black); color: var(--bg); border-color: transparent; font-weight: 600; }
.btn.wide { width: 100%; }
.btn.tiny { padding: 5px 10px; font-size: 13px; border-radius: 10px; }
.btn.danger-outline { color: var(--bad); border-color: rgba(208,70,59,.35); margin-top: 10px; }
.icon-btn { width: 40px; height: 40px; border: 0; background: none; border-radius: 50%; display: grid; place-items: center; color: var(--text); }
.icon-btn:active { background: var(--hover); }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--hover); color: var(--muted); }
.pill.on { background: rgba(58,157,93,.15); color: var(--ok); }

/* auth */
#auth { align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; text-align: center; }
.auth-logo { width: 84px; height: 84px; }
.auth-card h1 { margin: 6px 0 2px; font-size: 30px; }
.seg { display: flex; background: var(--hover); border-radius: 12px; padding: 3px; margin: 22px 0 14px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 10px; color: var(--muted); }
.seg button.on { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
#auth-form { display: grid; gap: 10px; }
#auth-form input, .search, select { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); outline: none; }
#auth-form input:focus, .search:focus { border-color: var(--accent); }

/* app */
#app { position: relative; }
#views { flex: 1; min-height: 0; }
.view { display: none; height: 100%; overflow-y: auto; padding: calc(var(--safe-t)) 0 16px; }
.view.on { display: block; }
.bar { display: flex; align-items: center; gap: 6px; padding: 10px 16px; position: sticky; top: 0; background: var(--bg); z-index: 2; }
.bar h2 { margin: 0; font-size: 24px; }
.bar.dark { background: #0f0f0f; color: #ddd; }
.bar.dark h2 { font-size: 16px; }
.bar.dark .icon-btn { color: #ddd; }
.bar.dark .btn { background: #222; border-color: #333; color: #ddd; }
.hint-card, .card, .wa-box { margin: 8px 16px; padding: 14px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.card .btn { margin-top: 6px; }
.sub { margin: 18px 16px 6px; font-size: 14px; color: var(--muted); font-weight: 600; }
.pad { padding: 0 16px; }
.empty-list { text-align: center; color: var(--muted); padding: 40px 30px; }
.chat-list { list-style: none; margin: 0; padding: 0 8px; }
.chat-list li { display: flex; align-items: center; gap: 12px; padding: 12px 10px; border-radius: 14px; }
.chat-list li:active { background: var(--hover); }
.chat-list .ico { width: 42px; height: 42px; border-radius: 50%; background: var(--hover); display: grid; place-items: center; flex: none; font-weight: 600; color: var(--muted); }
.chat-list .ico.busy { background: rgba(217,119,87,.15); color: var(--accent); }
.chat-list .ico.busy::after { content: ''; width: 16px; height: 16px; border: 2px solid var(--accent); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
.chat-list .txt { flex: 1; min-width: 0; }
.chat-list .t1 { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .t2 { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .when { font-size: 12px; color: var(--muted); flex: none; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { display: none; }
.switch i { position: absolute; inset: 0; border-radius: 13px; background: var(--line); transition: .15s; }
.switch i::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }
.inv-row { display: flex; gap: 8px; margin-top: 8px; } .inv-row .search { flex: 1; padding: 9px 12px; }
.inv-new { margin-top: 10px; padding: 10px; border-radius: 12px; background: var(--hover); display: grid; gap: 6px; }
.inv-new code { word-break: break-all; font-size: 12px; }
.sub0 { margin: 14px 0 4px; font-size: 13px; color: var(--muted); }
.acc-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.acc-row .btn { padding: 3px 8px; font-size: 12px; border-radius: 8px; }
.log-list { list-style: none; margin: 0; padding: 0 16px; font-size: 13px; }
.log-list li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.log-list b { font-weight: 600; }
#wa-qr { text-align: center; margin-top: 12px; }
#wa-qr img { width: 260px; max-width: 100%; border-radius: 12px; background: #fff; padding: 8px; }

/* tab bar */
#tabs { display: flex; border-top: 1px solid var(--line); background: var(--bg); padding-bottom: var(--safe-b); }
#tabs button { flex: 1; border: 0; background: none; padding: 8px 0 6px; display: grid; justify-items: center; gap: 2px; font-size: 11px; color: var(--muted); position: relative; }
#tabs button.on { color: var(--accent); }
.badge:not(:empty) { position: absolute; top: 4px; left: 54%; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--accent); color: #fff; font: 600 10px/16px var(--font); font-style: normal; }
.dot-on { position: absolute; top: 6px; left: 58%; width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.dot-on.on { background: var(--ok); }

/* chat view */
#chat, #term-view { position: absolute; inset: 0; background: var(--bg); display: flex; flex-direction: column; z-index: 10; padding-top: var(--safe-t); }
.chat-bar { border-bottom: 1px solid var(--line); }
.chat-head { flex: 1; min-width: 0; }
#chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#messages { flex: 1; overflow-y: auto; padding: 14px 14px 8px; }
.msg { margin: 0 0 16px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); padding: 9px 14px; border-radius: 18px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.agent .who { font-size: 12px; color: var(--muted); margin-bottom: 3px; display: flex; gap: 6px; align-items: center; }
.msg.agent .who b { color: var(--accent); font-size: 14px; }
.msg.agent.note .body { background: rgba(58,157,93,.08); border: 1px solid rgba(58,157,93,.25); border-radius: 14px; padding: 8px 12px; }
.body { word-wrap: break-word; }
.body p { margin: .45em 0; }
.body pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; overflow-x: auto; font: 12.5px/1.5 var(--mono); }
.body pre .copy { display: none; }
.body pre code { background: none; padding: 0; }
.body a { color: var(--accent); }
.body blockquote { border-left: 3px solid var(--line); margin: .4em 0; padding-left: 10px; color: var(--text); }
.body ul, .body ol { padding-left: 1.3em; }
.step { display: flex; gap: 8px; font-size: 13px; color: var(--muted); padding: 2px 0; min-width: 0; }
.step i { font-style: normal; color: var(--ok); flex: none; }
.step.failed i { color: var(--bad); }
.step span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12px; }
.err { color: var(--bad); background: rgba(208,70,59,.08); border-radius: 10px; padding: 8px 12px; margin: 6px 0; white-space: pre-wrap; font-size: 13px; }
.activity { display: inline-flex; align-items: center; gap: 8px; max-width: 100%; margin-top: 8px; padding: 7px 12px; border-radius: 10px; background: var(--hover); font-size: 13px; color: var(--muted); }
.activity .sp { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--accent); border-top-color: transparent; animation: spin .8s linear infinite; flex: none; }
.activity .done { width: 10px; height: 10px; border-radius: 50%; background: var(--ok); flex: none; }
.activity .tx { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); min-width: 0; }
.activity .tm { flex: none; font-variant-numeric: tabular-nums; }
.act-log { margin: 8px 0 0; max-height: 220px; overflow: auto; background: #0f0f0f; color: #d8d8d8; border-radius: 10px; padding: 10px; font: 11.5px/1.45 var(--mono); white-space: pre-wrap; word-break: break-word; }
#composer { display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px calc(8px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
#input { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 20px; padding: 10px 14px; background: var(--panel); outline: none; max-height: 160px; font-size: 16px; }
.round-btn { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--black); color: var(--bg); display: grid; place-items: center; flex: none; }
.round-btn .i-stop { display: none; }
.round-btn.stop .i-send { display: none; }
.round-btn.stop .i-stop { display: block; }

/* terminal */
#term-view { background: #0f0f0f; }
#term { flex: 1; min-height: 0; padding: 6px; }
.term-keys { display: flex; gap: 6px; padding: 8px 8px calc(8px + var(--safe-b)); background: #161616; overflow-x: auto; }
.term-keys button { flex: none; border: 1px solid #333; background: #222; color: #ddd; border-radius: 8px; padding: 6px 12px; font-size: 13px; }

#toast { position: absolute; left: 12px; right: 12px; bottom: calc(70px + var(--safe-b)); background: var(--black); color: var(--bg); padding: 12px 14px; border-radius: 14px; z-index: 50; font-size: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.25); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (min-width: 760px) { #app, #auth .auth-card { max-width: 760px; margin: 0 auto; width: 100%; } #chat, #term-view { max-width: 760px; margin: 0 auto; } }
