:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #e2e5ea;
  --text: #15171c;
  --muted: #6b7280;
  --accent: #5b6cff;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --ok: #2fb67c;
  --warn: #f5a524;
  --user-bubble: #5b6cff;
  --code-bg: #f1f3f6;
  --add: #e6f6ec;
  --add-text: #1a7f45;
  --del: #fdecec;
  --del-text: #b42318;
  --radius: 14px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --border: #2a2f3a;
    --text: #e8eaee;
    --muted: #8b93a3;
    --accent: #7b88ff;
    --user-bubble: #4c5bf0;
    --code-bg: #12151b;
    --add: #12301f;
    --add-text: #6fdc9c;
    --del: #3a1517;
    --del-text: #ff8f8f;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
#app { height: 100dvh; display: flex; flex-direction: column; max-width: 860px; margin: 0 auto; }

/* ---------- Top bar ---------- */
.bar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--bg);
  position: sticky; top: 0; z-index: 5;
}
.bar h1 { font-size: 20px; margin: 0; flex: 1; font-weight: 700; letter-spacing: -.01em; }
.bar .title { flex: 1; min-width: 0; }
.bar .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 16px; }
.bar .title small { color: var(--muted); font-size: 12px; display: flex; gap: 6px; align-items: center; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  font-size: 20px; color: var(--text); flex: none;
}
.icon-btn:active { background: var(--surface-2); }
select.mini {
  font-size: 12px; font-weight: 650; padding: 4px 4px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); max-width: 112px; flex: none;
}
.btn {
  background: var(--accent); color: var(--accent-text); border-radius: 12px;
  padding: 10px 16px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px;
}
.btn:disabled { opacity: .5; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--danger); }
.btn.block { width: 100%; justify-content: center; padding: 14px; }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: 4px; padding: 0 12px 8px; }
.tabs button {
  flex: 1; padding: 8px 4px; border-radius: 10px; color: var(--muted); font-weight: 600; font-size: 13px;
}
.tabs button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ---------- Lists ---------- */
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.list { padding: 4px 12px 24px; display: flex; flex-direction: column; gap: 8px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; display: flex; gap: 12px; align-items: center; text-align: left; width: 100%;
}
.card:active { background: var(--surface-2); }
.card .body { flex: 1; min-width: 0; }
.card .body b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.card .body small { color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 60px 24px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }

.badge {
  font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px; letter-spacing: .02em;
  background: var(--surface-2); color: var(--muted); flex: none;
}
.badge.claude { background: #f4e3d7; color: #9a4a1c; }
.badge.codex { background: #dfe9f7; color: #1e4f91; }
.badge.cursor { background: #e4e4e7; color: #27272a; }
@media (prefers-color-scheme: dark) {
  .badge.claude { background: #3a261a; color: #f0a874; }
  .badge.codex { background: #172a44; color: #8ab8f5; }
  .badge.cursor { background: #2a2a2e; color: #d4d4d8; }
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; opacity: .4; }
.dot.on { background: var(--ok); opacity: 1; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- Chat ---------- */
.msgs { padding: 8px 12px 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 100%; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end; max-width: 85%; background: var(--user-bubble); color: #fff;
  padding: 9px 13px; border-radius: 18px 18px 4px 18px; white-space: pre-wrap;
}
.msg.user .mode { display: block; font-size: 11px; opacity: .75; margin-top: 2px; }
.msg.assistant { padding: 2px 2px; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant p:last-child { margin: 0; }
.msg pre, .file-view pre {
  background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; overflow-x: auto; font: 12.5px/1.5 var(--mono); margin: 6px 0; white-space: pre;
}
.msg code { font: 13px var(--mono); background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.msg pre code { background: none; padding: 0; }
details.tool {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; font-size: 13px;
}
details.tool summary {
  padding: 7px 10px; list-style: none; display: flex; gap: 8px; align-items: center; color: var(--muted);
  white-space: nowrap; overflow: hidden;
}
details.tool summary::-webkit-details-marker { display: none; }
details.tool summary b { color: var(--text); font-weight: 600; flex: none; }
details.tool summary span { overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); font-size: 12px; }
details.tool pre { margin: 0; border: 0; border-top: 1px solid var(--border); border-radius: 0 0 10px 10px; max-height: 320px; }
details.process {
  font-size: 13px; border: 1px dashed var(--border); border-radius: 12px;
  background: color-mix(in srgb, var(--surface-2, var(--surface)) 85%, transparent);
}
details.process > summary {
  display: flex; gap: 8px; align-items: baseline; padding: 8px 12px; cursor: pointer;
  list-style: none; color: var(--muted);
}
details.process > summary::-webkit-details-marker { display: none; }
details.process > summary b { color: var(--muted); font-weight: 700; flex: none; }
details.process > summary span { font-size: 12px; }
details.process .process-body {
  display: flex; flex-direction: column; gap: 8px; padding: 0 8px 10px; max-height: 420px; overflow: auto;
}
.msg.assistant.nested { opacity: 0.92; font-size: 0.95em; }
.msg.system { align-self: center; color: var(--muted); font-size: 12px; text-align: center; }
.plan { margin: 8px 12px 4px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.plan summary { padding: 10px 12px; font-weight: 700; cursor: pointer; }
.plan pre { margin: 0; padding: 0 12px 8px; white-space: pre-wrap; font: 12px/1.45 var(--mono); max-height: 240px; overflow: auto; }
.plan p { margin: 0; padding: 0 12px 10px; color: var(--muted); font-size: 12px; }
.plan .todos { margin: 0; padding: 0 12px 4px 12px; list-style: none; font-size: 13px; }
.plan .todos li { margin: 4px 0; display: flex; gap: 8px; align-items: flex-start; }
.plan .todos .mark { width: 1em; flex: none; color: var(--accent, #2a6); }
.plan .todos li.done { color: var(--muted); text-decoration: line-through; }
.plan .todos li.done .mark { color: var(--muted); }
.plan .todo-count { padding-top: 0; }
.plan .todo-count.warn { color: #b45309; }
.plan-actions { display: flex; align-items: center; gap: 10px; padding: 0 12px 12px; flex-wrap: wrap; }
.plan-build { appearance: none; border: 0; border-radius: 10px; padding: 8px 14px; font: 600 13px/1.2 inherit; background: var(--accent, #1a7f4b); color: #fff; cursor: pointer; }
.plan-build:disabled { opacity: 0.45; cursor: not-allowed; }
.plan-hint { color: var(--muted); font-size: 12px; }
.who { margin-top: 8px; font-size: 11px; font-weight: 700; letter-spacing: .02em; color: var(--muted); }
.msg.error {
  background: var(--del); color: var(--del-text); border-radius: 10px; padding: 9px 12px;
  white-space: pre-wrap; font-size: 13px;
}
.msg.raw { font: 12px/1.4 var(--mono); color: var(--muted); white-space: pre-wrap; }
.typing { display: none; gap: 4px; padding: 6px 4px; }
.typing.on { display: flex; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer {
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 1px solid var(--border);
}
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--surface); border-radius: 20px;
  padding: 10px 14px; max-height: 160px; min-height: 42px; outline: none; font-size: 16px; line-height: 1.35;
}
.composer textarea:focus { border-color: var(--accent); }
.send {
  width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 18px; flex: none;
}
.send.stop { background: var(--danger); }
.composer .meta { display: flex; gap: 6px; margin-bottom: 6px; overflow-x: auto; }
.modes { display: flex; gap: 4px; margin-bottom: 6px; }
.modes button {
  font-size: 12px; padding: 3px 8px; border-radius: 8px; color: var(--muted); font-weight: 500; background: transparent;
}
.modes button.on { font-weight: 700; color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); }
.preview { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.preview-bar { display: flex; gap: 8px; align-items: center; padding: 0 12px 8px; }
.preview-bar .input { flex: 1; min-width: 0; }
.preview iframe { flex: 1; min-height: 280px; width: 100%; border: 0; background: #111; }
.chip {
  font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted);
  white-space: nowrap; font-weight: 600;
}
.chip.full { background: #fff1db; color: #a15c00; }
@media (prefers-color-scheme: dark) { .chip.full { background: #3a2a10; color: #f5b860; } }

/* ---------- Files & diff ---------- */
.crumbs { padding: 4px 14px 8px; font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 2px; }
.crumbs button { color: var(--accent); padding: 2px 4px; border-radius: 6px; }
.row-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 14px; border-bottom: 1px solid var(--border);
  width: 100%; text-align: left;
}
.row-item:active { background: var(--surface-2); }
.row-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor {
  flex: 1; width: 100%; border: 0; outline: none; resize: none; padding: 12px 14px;
  background: var(--surface); font: 13px/1.55 var(--mono); tab-size: 2; white-space: pre; overflow: auto;
}
.diff { font: 12px/1.5 var(--mono); padding: 0 0 24px; }
.diff div { padding: 0 12px; white-space: pre; }
.diff .a { background: var(--add); color: var(--add-text); }
.diff .d { background: var(--del); color: var(--del-text); }
.diff .h { color: var(--accent); background: var(--surface-2); padding-top: 4px; padding-bottom: 4px; }
.diff .f { font-weight: 700; padding-top: 14px; color: var(--text); }
.diff-wrap { overflow-x: auto; }
.st { font: 700 11px var(--mono); width: 22px; text-align: center; color: var(--warn); flex: none; }
.st.A, .st.\?\? { color: var(--ok); }
.st.D { color: var(--danger); }
.section-title { padding: 14px 14px 6px; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

/* ---------- Sheet ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--bg); width: 100%; max-width: 560px; max-height: 88dvh; border-radius: 20px 20px 0 0;
  display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom);
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet header { padding: 16px 16px 8px; display: flex; align-items: center; }
.sheet header h2 { margin: 0; font-size: 18px; flex: 1; }
.sheet .content { overflow-y: auto; padding: 0 16px 8px; }
.sheet footer { padding: 12px 16px 16px; }
.pair-actions { display: flex; gap: 8px; }
.pair-actions .btn { flex: 1; justify-content: center; }
.dev-panel {
  margin: 0 12px 8px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 8px 4px;
}
.device.warn-session { background: color-mix(in srgb, #b45309 12%, transparent); }
.device.warn-session .name small { color: #b45309; font-weight: 600; }
.device {
  display: flex; align-items: center; gap: 8px; padding: 8px 6px;
  border-bottom: 1px solid var(--border);
}
.device:last-child { border-bottom: 0; }
.device .name { flex: 1; min-width: 0; font-weight: 600; }
.device .name small { display: block; font-weight: 500; color: var(--muted); font-size: 12px; }
.device .btn { padding: 8px 10px; font-size: 13px; }
.label { font-size: 13px; font-weight: 600; color: var(--muted); margin: 12px 0 6px; }
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1; padding: 11px 6px; border-radius: 12px; background: var(--surface); border: 1.5px solid var(--border);
  font-weight: 600; font-size: 14px;
}
.seg button.on { border-color: var(--accent); color: var(--accent); }
.seg button:disabled { opacity: .4; }
.input {
  width: 100%; padding: 11px 13px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  outline: none; font-size: 16px;
}
.input:focus { border-color: var(--accent); }
.pick { max-height: 40dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); margin-top: 8px; }
#make { margin-top: 8px; }
.llm-pick { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.llm-pick span { font-size: 13px; font-weight: 650; color: var(--muted); flex: none; }
.llm-pick select {
  flex: 1; min-width: 0; font-size: 14px; font-weight: 650; padding: 8px 10px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.pick .row-item.on { background: var(--surface-2); color: var(--accent); font-weight: 600; }
.pick .row-item:last-child { border-bottom: 0; }

/* ---------- Pair ---------- */
.pair { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 24px; gap: 18px; text-align: center; }
.pair .logo { width: 64px; height: 64px; margin: 0 auto; }
.pair h1 { margin: 0; font-size: 24px; }
.pair p { margin: 0; color: var(--muted); }
.pin {
  font: 600 28px var(--mono); letter-spacing: .4em; text-align: center; padding: 14px; border-radius: 14px;
  border: 1.5px solid var(--border); background: var(--surface); outline: none; width: 100%;
}
.pin:focus { border-color: var(--accent); }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; transition: .2s; pointer-events: none; z-index: 30; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.offline { background: var(--warn); color: #000; text-align: center; font-size: 12px; padding: 3px; font-weight: 600; }

/* ---------- Login, pairing, scanner ---------- */
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack[hidden] { display: none; }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); }
.warn {
  background: #fff4dc; color: #7a4a00; border-radius: 12px; padding: 10px 12px; font-size: 13px; margin: 4px 0 10px;
}
@media (prefers-color-scheme: dark) { .warn { background: #3a2a10; color: #f5c77a; } }
.qr { display: flex; justify-content: center; margin: 8px 0; }
.qr svg { width: 100%; max-width: 240px; height: auto; background: #fff; border-radius: 14px; padding: 8px; }
.pinshow { font: 700 34px var(--mono); letter-spacing: .25em; text-align: center; margin: 2px 0 4px; }
details.apk { margin-top: 14px; border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; background: var(--surface); }
details.apk summary { font-weight: 600; cursor: pointer; }
.cam { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 16px; background: #000; display: block; }
.link { color: inherit; text-decoration: underline; font-weight: 700; padding: 0 4px; }
.offline[hidden] { display: none; }
