:root {
  --bg: #eef2f8;
  --panel: #ffffff;
  --ink: #111827;
  --muted: #667085;
  --line: #dbe2ee;
  --brand: #174ea6;
  --brand-strong: #0b3b8c;
  --danger: #b42318;
  --ok: #067647;
  --warning: #b54708;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
button, textarea, input { font: inherit; }
button { cursor: pointer; }

#app {
  display: flex;
  flex-direction: column;
  width: min(100%, 720px);
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
}

#app-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(135deg, #0b3b8c, #174ea6);
  color: #fff;
}
#app-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  font-size: 17px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#connection-status {
  flex: none;
  padding: 4px 9px;
  border-radius: 999px;
  background: rgb(255 255 255 / 16%);
  font-size: 12px;
}
#connection-status[data-state="online"] { background: rgb(6 118 71 / 55%); }
#connection-status[data-state="connecting"] { background: rgb(181 71 8 / 60%); }
.icon-button, .text-button {
  border: 0;
  background: transparent;
  color: #fff;
}
.icon-button { padding: 0 6px; font-size: 28px; line-height: 1; }
.text-button { padding: 4px 0; font-size: 13px; opacity: .9; }

main { flex: 1; min-height: 0; }
.view { padding: 18px 16px max(18px, env(safe-area-inset-bottom)); }
.view[hidden] { display: none; }
.view-title { margin: 4px 2px 12px; font-size: 20px; }
.hero { padding: 12px 4px 18px; text-align: center; }
.hero-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 18px;
  background: var(--brand);
  color: #fff;
  font-weight: 700;
  letter-spacing: 1px;
}
.hero h2 { margin: 0 0 8px; font-size: 22px; }
.hero p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.6; }
.pair-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
}
.field-label { color: var(--muted); font-size: 13px; }
.code-input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f8fafc;
  font-size: 28px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 8px;
  text-align: center;
}
.code-input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.primary, .danger {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  color: #fff;
  font-weight: 600;
}
.primary { background: var(--brand); }
.primary:disabled { opacity: .55; }
.danger { background: var(--danger); }
.danger:disabled { opacity: .55; }
.error { margin: 0; color: var(--danger); font-size: 13px; }
.footnote { margin: 18px 6px 0; color: var(--muted); font-size: 12px; line-height: 1.6; text-align: center; }
.empty { padding: 24px 8px; color: var(--muted); text-align: center; }
.list { display: flex; flex-direction: column; gap: 10px; }
.device-row, .session-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  text-align: left;
}
.device-row:active, .session-row:active { background: #f1f5fb; }
.device-dot { width: 10px; height: 10px; border-radius: 50%; background: #98a2b3; }
.device-dot.online { background: var(--ok); box-shadow: 0 0 0 4px rgb(6 118 71 / 14%); }
.device-main, .session-main { flex: 1; min-width: 0; }
.device-name, .session-title { font-size: 15px; font-weight: 600; }
.device-meta, .session-meta { margin-top: 3px; color: var(--muted); font-size: 12px; }
.pending-dot {
  display: grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: #f04438;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}
.session-view { display: flex; flex-direction: column; min-height: calc(100dvh - 56px); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.session-status-line {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 2px 10px;
  color: var(--muted);
  font-size: 13px;
}
.pending-badge { padding: 2px 8px; border-radius: 999px; background: #fff3e0; color: var(--warning); font-size: 12px; }
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 6px 2px 12px;
  overscroll-behavior: contain;
}
.older {
  display: block;
  margin: 4px auto 14px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
}
.bubble-row { display: flex; margin: 0 0 10px; }
.bubble-row.user { justify-content: flex-end; }
.bubble {
  max-width: 84%;
  padding: 10px 13px;
  border-radius: 15px;
  background: var(--panel);
  box-shadow: 0 1px 1px rgb(16 24 40 / 5%);
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.bubble-row.user .bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 5px; }
.bubble-row.assistant .bubble { border-bottom-left-radius: 5px; }
.bubble.failed, .bubble.cancelled { opacity: .7; }
.tool-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 0 10px;
  padding: 8px 10px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 12px;
}
.tool-status.success { color: var(--ok); }
.tool-status.failed { color: var(--danger); }
.tool-status.running { color: var(--warning); }
.tool-text { overflow-wrap: anywhere; }
.approval-area { display: flex; flex-direction: column; gap: 10px; }
.approval {
  padding: 13px 14px;
  border: 1px solid #f5c16c;
  border-radius: 14px;
  background: #fffaeb;
}
.approval-title { font-size: 14px; font-weight: 600; }
.approval-detail { margin-top: 6px; color: #5c4a1e; font-size: 13px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }
.approval-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.approval-actions button {
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-weight: 600;
}
.approval-actions .approve { border-color: var(--brand); background: var(--brand); color: #fff; }
.approval-actions .approve:disabled { opacity: .5; }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 0 0;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.composer textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel);
  color: var(--ink);
  resize: none;
}
.composer .send { min-height: 44px; }
.toast {
  position: fixed;
  right: 16px;
  bottom: max(82px, calc(env(safe-area-inset-bottom) + 82px));
  left: 16px;
  max-width: 688px;
  margin: 0 auto;
  padding: 10px 14px;
  border-radius: 12px;
  background: #101828;
  color: #fff;
  font-size: 13px;
  text-align: center;
  box-shadow: 0 8px 24px rgb(16 24 40 / 22%);
}
.toast.error { background: var(--danger); }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c111d;
    --panel: #161b26;
    --ink: #f2f4f7;
    --muted: #98a2b3;
    --line: #2a3242;
    --brand: #528bff;
    --brand-strong: #2f5fd0;
  }
  .code-input { background: #0f1520; }
  .device-row:active, .session-row:active { background: #1d2432; }
  .approval { border-color: #7a5a1e; background: #241d0f; }
  .approval-detail { color: #e4c98a; }
  .toast { background: #344054; }
}
