:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --surface-2: #f4f4f3;
  --text: #18181b;
  --text-2: #52525b;
  --muted: #a1a1aa;
  --line: rgba(24, 24, 27, 0.08);
  --shadow: 0 1px 2px rgba(24, 24, 27, 0.04), 0 4px 16px rgba(24, 24, 27, 0.04);
  --brand: #d97757;
  --running: #2563eb;
  --running-soft: rgba(37, 99, 235, 0.1);
  --waiting: #d97706;
  --waiting-soft: rgba(217, 119, 6, 0.12);
  --error: #dc2626;
  --error-soft: rgba(220, 38, 38, 0.1);
  --done: #16a34a;
  --done-soft: rgba(22, 163, 74, 0.1);
  --idle: #71717a;
  --idle-soft: rgba(113, 113, 122, 0.1);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0c0e;
    --surface: #161619;
    --surface-2: #1f1f23;
    --text: #f4f4f5;
    --text-2: #a1a1aa;
    --muted: #71717a;
    --line: rgba(255, 255, 255, 0.07);
    --shadow: none;
    --running: #60a5fa;
    --running-soft: rgba(96, 165, 250, 0.13);
    --waiting: #fbbf24;
    --waiting-soft: rgba(251, 191, 36, 0.13);
    --error: #f87171;
    --error-soft: rgba(248, 113, 113, 0.13);
    --done: #4ade80;
    --done-soft: rgba(74, 222, 128, 0.12);
    --idle: #a1a1aa;
    --idle-soft: rgba(161, 161, 170, 0.12);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Noto Sans SC", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 0;
  background: none;
  padding: 0;
}

.wrap {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 16px calc(40px + env(safe-area-inset-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  padding-top: env(safe-area-inset-top);
}

.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.brand img {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  flex-shrink: 0;
}

.brand-text {
  min-width: 0;
}

.brand h1 {
  font-size: 16px;
  font-weight: 650;
  margin: 0;
  letter-spacing: -0.01em;
}

.host {
  font-size: 12px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.host .dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: 1px;
  background: var(--muted);
}

.host .dot.on {
  background: var(--done);
  box-shadow: 0 0 0 3px var(--done-soft);
}

.host .dot.off {
  background: var(--error);
  box-shadow: 0 0 0 3px var(--error-soft);
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: var(--text-2);
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.icon-btn.on {
  color: var(--brand);
}

.icon-btn svg {
  width: 19px;
  height: 19px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin: 4px 0 20px;
}

.stat {
  text-align: left;
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  border: 1.5px solid transparent;
  transition: border-color 0.15s, transform 0.15s;
}

.stat:active {
  transform: scale(0.98);
}

.stat.active {
  border-color: var(--c);
}

.stat .n {
  font-size: 26px;
  font-weight: 700;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: var(--c);
}

.stat .n.zero {
  color: var(--muted);
}

.stat .l {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 2px;
}

.section {
  margin-bottom: 22px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 4px 10px;
}

.section-title button {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
}

.section-title button:hover {
  color: var(--text);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 12px;
  align-items: start;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 14px 16px;
  min-width: 0;
  cursor: pointer;
  transition: border-color 0.15s;
}

.card:hover {
  border-color: color-mix(in srgb, var(--c) 40%, transparent);
}

.card .head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.card .ic {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  background: var(--cs);
  color: var(--c);
  flex-shrink: 0;
  margin-top: 1px;
}

.card .ic svg {
  width: 16px;
  height: 16px;
}

.card .ttl {
  min-width: 0;
  flex: 1;
}

.card .title {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card .path {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pill {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  color: var(--c);
  background: var(--cs);
  white-space: nowrap;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.s-running { --c: var(--running); --cs: var(--running-soft); }
.s-waiting, .s-interrupted { --c: var(--waiting); --cs: var(--waiting-soft); }
.s-error { --c: var(--error); --cs: var(--error-soft); }
.s-done { --c: var(--done); --cs: var(--done-soft); }
.s-idle, .s-unknown, .s-ended { --c: var(--idle); --cs: var(--idle-soft); }

.s-running .ic svg {
  animation: spin 1.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.activity {
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-2);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.activity.alert {
  color: var(--c);
  background: var(--cs);
  border-radius: 10px;
  padding: 8px 10px;
}

.activity .tool {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  font-weight: 600;
  color: var(--c);
}

.prompt {
  margin-top: 8px;
  font-size: 13px;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
  padding-left: 10px;
  border-left: 2px solid var(--line);
}

.foot {
  display: flex;
  align-items: center;
  gap: 6px 12px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.kv {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 4px 10px;
  font-size: 12px;
}

.kv dt {
  color: var(--muted);
}

.kv dd {
  margin: 0;
  color: var(--text-2);
  word-break: break-all;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 64px 16px;
}

.empty svg {
  width: 40px;
  height: 40px;
  opacity: 0.5;
  margin-bottom: 8px;
}

.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login form {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
}

.login img {
  width: 56px;
  height: 56px;
  border-radius: 14px;
}

.login h1 {
  font-size: 18px;
  margin: 14px 0 4px;
}

.login p {
  color: var(--text-2);
  font-size: 13px;
  margin: 0 0 20px;
}

.login input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  outline: none;
  text-align: center;
}

.login input:focus {
  border-color: var(--brand);
}

.login button {
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  font-size: 15px;
}

.login .err {
  color: var(--error);
  font-size: 13px;
  min-height: 20px;
  margin-top: 10px;
}


html.sheet-open,
html.sheet-open body {
  overflow: hidden;
}

.sheet-wrap {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
}

.sheet-wrap.show {
  pointer-events: auto;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  opacity: 0;
  transition: opacity 0.2s;
}

.sheet-wrap.show .sheet-backdrop {
  opacity: 1;
}

.sheet {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(760px, 100%);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.12);
}

.sheet-wrap.show .sheet {
  transform: none;
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px 6px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.sheet-head .ic {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  background: var(--cs);
  color: var(--c);
  flex-shrink: 0;
}

.sheet-head .ic svg {
  width: 16px;
  height: 16px;
}

.sheet-head.s-running .ic svg {
  animation: spin 1.6s linear infinite;
}

.sheet-head .ttl {
  min-width: 0;
  flex: 1;
}

.sheet-head .title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-head .path {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-info {
  display: none;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  max-height: 40vh;
  overflow: auto;
}

.sheet-info.show {
  display: block;
}

.info-reason {
  margin-top: 8px;
  font-size: 12px;
  color: var(--error);
}

.sheet-body {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sheet-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--text-2);
}

.sync .dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: 1px;
  background: var(--muted);
}

.sync.live .dot {
  background: var(--done);
  animation: pulse 1.6s ease-in-out infinite;
}

.sync.err {
  color: var(--error);
}

.sync.err .dot {
  background: var(--error);
}

@keyframes pulse {
  50% { opacity: 0.3; }
}

.link {
  color: var(--running);
  font-size: 12px;
}

.older {
  text-align: center;
}

.timeline-empty {
  text-align: center;
  color: var(--muted);
  padding: 48px 0;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--text-2);
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: -2px;
  animation: spin 0.8s linear infinite;
}

.msg {
  max-width: 100%;
  min-width: 0;
}

.msg.user {
  align-self: flex-end;
  max-width: 85%;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.msg.user .bubble {
  background: var(--text);
  color: var(--bg);
  padding: 9px 13px;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
}

.msg.user .time {
  font-size: 11px;
  color: var(--muted);
  margin-top: 3px;
}

.msg.ai .md {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.65;
}

.md code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12.5px;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
}

.md pre {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 6px 0;
  white-space: pre;
}

.md pre code {
  background: none;
  padding: 0;
}

.md a {
  color: var(--running);
  word-break: break-all;
}

.msg.note {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  max-width: 90%;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.note.error {
  color: var(--error);
  background: var(--error-soft);
  padding: 6px 12px;
  border-radius: 10px;
}

.msg.note.warn {
  color: var(--waiting);
}

.thinking,
.tool-row {
  border-radius: 10px;
  font-size: 13px;
}

.thinking summary,
.tool-row summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 10px;
  color: var(--text-2);
  min-width: 0;
}

.thinking summary::-webkit-details-marker,
.tool-row summary::-webkit-details-marker {
  display: none;
}

.thinking summary {
  font-style: italic;
  color: var(--muted);
}

.tool-row {
  background: var(--surface);
  border: 1px solid var(--line);
}

.tool-row summary:hover {
  background: var(--surface-2);
}

.tool-row .st {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--done);
}

.tool-row.run .st {
  background: var(--running);
  animation: pulse 1s ease-in-out infinite;
}

.tool-row.fail .st {
  background: var(--error);
}

.tool-row .name {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  flex-shrink: 0;
}

.tool-row .det {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  font-size: 12px;
}

.tool-io {
  padding: 4px 10px 10px;
}

.tool-io .lbl {
  font-size: 11px;
  color: var(--muted);
  margin: 6px 0 3px;
}

.pre {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 8px 10px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 320px;
  overflow: auto;
}

.thinking .pre {
  margin: 0 10px 6px;
  font-family: inherit;
  color: var(--text-2);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
  padding: 8px 14px;
  max-height: 160px;
  outline: none;
}

.composer textarea:focus {
  border-color: var(--brand);
}

.composer.disabled textarea {
  opacity: 0.6;
}

.c-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.c-btn svg {
  width: 18px;
  height: 18px;
}

.c-btn.send {
  background: var(--brand);
  color: #fff;
}

.c-btn.stop {
  background: var(--error-soft);
  color: var(--error);
}

.c-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.c-btn[hidden] {
  display: none;
}

@media (max-width: 640px) {
  .stats {
    gap: 8px;
  }
  .stat {
    padding: 10px 8px;
    text-align: center;
  }
  .stat .n {
    font-size: 22px;
  }
  .stat .l {
    font-size: 11px;
  }
  .grid {
    grid-template-columns: 1fr;
  }
  .sheet {
    width: 100%;
    box-shadow: none;
  }
  .sheet-body {
    padding: 12px;
  }
}
