:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --good: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 2px 8px rgba(15, 23, 42, .04);
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #0f172a;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #1f2937;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 Inter, system-ui, -apple-system, Segoe UI, sans-serif; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; }
textarea { resize: vertical; min-height: 90px; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* Shell */
.shell { max-width: 760px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); padding: calc(10px + var(--safe-t)) 16px 8px; border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.biz-chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 2px; scrollbar-width: none; }
.biz-chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip .count { background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; line-height: 18px; }
main { flex: 1; padding: 14px 16px calc(var(--nav-h) + 24px + var(--safe-b)); }

.nav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; background: var(--surface); border-top: 1px solid var(--border); padding-bottom: var(--safe-b); }
.nav-inner { max-width: 760px; margin: 0 auto; display: grid; grid-template-columns: repeat(4, 1fr); height: var(--nav-h); }
.nav button { background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 12px; color: var(--muted); position: relative; }
.nav button svg { width: 22px; height: 22px; }
.nav button.active { color: var(--accent); font-weight: 600; }
.nav .badge { position: absolute; top: 8px; left: calc(50% + 6px); background: var(--bad); color: #fff; border-radius: 999px; font-size: 10px; line-height: 16px; min-width: 16px; padding: 0 4px; }

/* Cards */
.section-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 2px 8px; display: flex; align-items: center; gap: 8px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); margin-bottom: 10px; }
.card.tap { cursor: pointer; }
.card.tap:active { transform: scale(.995); }
.card h3 { font-size: 15px; font-weight: 600; }
.agent-tag { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.status { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.status.running, .status.approved { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.status.done, .status.executed { color: var(--good); }
.status.needs_attention, .status.proposed, .status.pending, .status.manual { color: var(--warn); }
.status.failed, .status.rejected { color: var(--bad); }
.steps { margin: 8px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.steps li { margin: 2px 0; }
.actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 8px 14px; font-weight: 600; font-size: 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; }
.btn.block { width: 100%; padding: 12px; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; }
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 12px; color: var(--muted); }
.meter { height: 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; }
.meter > i { display: block; height: 100%; background: var(--accent); }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.spin { width: 14px; height: 14px; border: 2px solid color-mix(in srgb, var(--accent) 25%, transparent); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3 { margin: 14px 0 6px; font-size: 16px; }
.md p { margin: 8px 0; }
.md ul, .md ol { padding-left: 20px; margin: 6px 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
.md pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md code { font-size: 13px; }
.md blockquote { margin: 8px 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }

/* Chat */
.chat-view { display: flex; flex-direction: column; min-height: calc(100dvh - 190px); }
.thread { display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; }
.bubble { max-width: 88%; padding: 10px 13px; border-radius: 16px; }
.bubble.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.bubble.user a { color: inherit; }
.bubble.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble .who { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.event { align-self: stretch; font-size: 13px; background: var(--surface-2); border: 1px dashed var(--border); border-radius: 12px; padding: 8px 12px; color: var(--text); }
.event.tap { cursor: pointer; }
.plan { align-self: stretch; margin-left: 0; }
.attach-chip { font-size: 12px; opacity: .85; display: block; margin-bottom: 4px; }
.composer { position: sticky; bottom: calc(var(--nav-h) + var(--safe-b)); background: var(--bg); padding: 8px 0 10px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 160px; border-radius: 22px; padding: 11px 16px; resize: none; }
.icon-btn { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface); display: grid; place-items: center; }
.icon-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.icon-btn svg { width: 20px; height: 20px; }
.pending-files { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; display: flex; gap: 8px; align-items: center; }
.conv-item { display: flex; gap: 10px; align-items: center; }
.conv-item .t { font-weight: 600; }
.conv-item .l { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.suggest { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 14px; }

/* Sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(2, 6, 23, .45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease; }
.sheet { background: var(--bg); width: 100%; max-width: 760px; max-height: 92dvh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 8px 16px calc(24px + var(--safe-b)); animation: up .2s ease; }
.sheet .grab { width: 40px; height: 4px; border-radius: 4px; background: var(--border); margin: 4px auto 12px; }
.sheet-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.sheet-head h2 { font-size: 18px; }
@keyframes up { from { transform: translateY(30px); opacity: .6; } }
@keyframes fade { from { opacity: 0; } }
label.field { display: block; margin: 12px 0; }
label.field > span { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.toggle { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.toggle input { width: auto; }

#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + 20px + var(--safe-b)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 360px; padding: 24px; }
.login h1 { font-size: 24px; margin-bottom: 4px; }

.list-item { display: flex; gap: 12px; align-items: center; padding: 14px; }
.list-item + .list-item { border-top: 1px solid var(--border); }
.list-card { padding: 0; }
.list-card .list-item.tap { cursor: pointer; }
.agent-icon { font-size: 22px; width: 40px; height: 40px; display: grid; place-items: center; background: var(--surface-2); border-radius: 12px; flex: none; }
.kv { display: grid; grid-template-columns: 80px 1fr; gap: 4px 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.bar-list .bar { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; margin: 8px 0; font-size: 14px; }
.bar-list .bar .meter { grid-column: 1 / -1; }
