:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fffefa;
  --surface-strong: #ffffff;
  --ink: #1f211f;
  --muted: #71756f;
  --line: #dedfd9;
  --line-soft: #eaeae5;
  --green: #168b57;
  --green-soft: #e5f3e9;
  --amber: #bb7419;
  --red: #b64c43;
  --charcoal: #282a28;
  --shadow: 0 18px 46px rgba(37, 41, 36, 0.12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink); }

body { overscroll-behavior: none; }

button, input, textarea, select { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.app-shell {
  min-height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}

.screen { min-height: 100dvh; }

.gate-view {
  min-height: 100dvh;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  padding: 32px;
  text-align: center;
}

.brand-mark {
  width: 58px;
  height: 58px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: var(--charcoal);
  color: white;
  font: 700 24px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  box-shadow: var(--shadow);
}

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.16em;
}

.gate-view h1 { margin: 4px 0 0; font-size: 28px; letter-spacing: -0.03em; }
.gate-view p { max-width: 330px; margin: 0; line-height: 1.55; }

.pair-form { width: min(100%, 330px); display: grid; gap: 12px; margin-top: 12px; }
.pair-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface-strong);
  padding: 14px 18px;
  text-align: center;
  font: 600 25px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.25em;
  outline: none;
}
.pair-form input:focus { border-color: var(--green); box-shadow: 0 0 0 4px rgba(22, 139, 87, 0.1); }
.form-error { min-height: 22px; color: var(--red); font-size: 13px; }

.primary-button {
  border: 0;
  border-radius: 14px;
  padding: 14px 18px;
  background: var(--green);
  color: white;
  font-weight: 700;
}

.loading-ring {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--green);
  animation: spin 0.85s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.home-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 22px) 20px 6px;
}
.home-header h1 { margin: 4px 0 0; font-size: 34px; letter-spacing: -0.045em; }
.header-actions { display: flex; gap: 9px; }
.icon-button, .new-task-button, .round-button, .voice-button, .send-button {
  border: 0;
  background: transparent;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-button, .new-task-button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--surface-strong);
  border: 1px solid var(--line-soft);
  font-size: 25px;
}
.new-task-button { background: var(--charcoal); color: white; border-color: var(--charcoal); }
.notification-button { position: relative; }
.notification-button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.notification-button.needs-attention::after {
  content: "";
  position: absolute;
  top: 5px;
  right: 5px;
  width: 8px;
  height: 8px;
  border: 2px solid var(--surface-strong);
  border-radius: 50%;
  background: var(--red);
}
.notification-button.enabled { color: var(--green); }

.connection-line {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 21px 14px;
  font-size: 13px;
}
.connection-spacer { flex: 1; }
.cloud-link {
  border: 1px solid rgba(22, 139, 87, .22);
  border-radius: 999px;
  padding: 4px 8px;
  background: var(--green-soft);
  color: var(--green);
  font-size: 10px;
  font-weight: 700;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-dot.online { background: var(--green); box-shadow: 0 0 0 4px rgba(22, 139, 87, 0.1); }

.agent-filters {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 20px 13px;
}
.agent-filters::-webkit-scrollbar { display: none; }
.agent-chip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  padding: 7px 11px 7px 8px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--surface-strong);
  font-weight: 650;
}
.agent-chip img { width: 24px; height: 24px; border-radius: 7px; object-fit: cover; }
.agent-chip small { color: var(--muted); font-size: 9px; font-weight: 600; }
.agent-chip.active { border-color: var(--charcoal); box-shadow: inset 0 0 0 1px var(--charcoal); }

.search-field {
  margin: 0 20px 18px;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 13px;
  background: var(--surface-strong);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  color: var(--muted);
}
.search-field input { width: 100%; border: 0; outline: 0; background: transparent; }

.thread-section { padding: 0 14px calc(var(--safe-bottom) + 24px); }
.section-heading { display: flex; align-items: center; justify-content: space-between; padding: 0 7px 9px; }
.section-heading h2 { margin: 0; font-size: 15px; }
.text-button { border: 0; background: transparent; color: var(--green); font-size: 13px; }

.thread-list { display: grid; gap: 9px; }
.thread-card {
  width: 100%;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  gap: 11px;
  align-items: start;
  padding: 14px;
  border: 1px solid var(--line-soft);
  border-radius: 17px;
  background: var(--surface-strong);
  text-align: left;
}
.thread-card:active { transform: scale(0.992); background: #fbfbf8; }
.thread-agent-icon { width: 42px; height: 42px; border-radius: 12px; object-fit: cover; }
.thread-copy { min-width: 0; }
.thread-title-row { display: flex; align-items: center; gap: 7px; min-width: 0; }
.thread-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 700;
}
.thread-preview {
  margin: 5px 0 0;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}
.thread-meta { margin-top: 7px; display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; }
.thread-meta .project { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-time { padding-top: 2px; color: var(--muted); font-size: 10px; white-space: nowrap; }
.state-pill { border-radius: 999px; padding: 2px 6px; color: var(--green); background: var(--green-soft); font-size: 9px; font-weight: 700; }
.state-pill.waiting { color: var(--amber); background: #fbf0df; }
.empty-state { padding: 52px 20px; text-align: center; color: var(--muted); line-height: 1.6; }

.chat-screen { display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto; height: 100dvh; }
.chat-header {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: center;
  padding: calc(var(--safe-top) + 7px) 10px 8px;
  background: rgba(246, 245, 242, 0.96);
  border-bottom: 1px solid var(--line-soft);
  z-index: 10;
}
.back-button { font-size: 34px; line-height: 1; }
.chat-title-wrap { min-width: 0; text-align: center; }
.chat-title-wrap h1 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 16px; }
.chat-title-wrap p { margin: 3px 0 0; color: var(--muted); font-size: 11px; }

.agent-status-bar {
  position: relative;
  z-index: 9;
  min-height: 48px;
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) 10px;
  align-items: center;
  gap: 9px;
  padding: 7px 15px 7px 17px;
  border-bottom: 1px solid var(--line-soft);
  background: rgba(255, 254, 250, .94);
}
.agent-status-bar img { width: 30px; height: 30px; border-radius: 9px; object-fit: cover; }
.agent-status-copy { min-width: 0; display: grid; gap: 1px; }
.agent-status-copy strong { font-size: 12px; line-height: 1.25; }
.agent-status-copy small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 10px; line-height: 1.35; }
.agent-status-signal { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.agent-status-bar.running .agent-status-signal { background: var(--green); box-shadow: 0 0 0 4px rgba(22, 139, 87, .11); animation: status-pulse 1.35s ease-in-out infinite; }
.agent-status-bar.complete .agent-status-signal,
.agent-status-bar.ready .agent-status-signal { background: var(--green); }
.agent-status-bar.waiting .agent-status-signal { background: var(--amber); box-shadow: 0 0 0 4px rgba(187, 116, 25, .1); }
.agent-status-bar.error .agent-status-signal { background: var(--red); box-shadow: 0 0 0 4px rgba(182, 76, 67, .1); }
.agent-status-bar.stopped .agent-status-signal,
.agent-status-bar.offline .agent-status-signal { background: #92968f; }
@keyframes status-pulse { 0%, 100% { opacity: .45; transform: scale(.8); } 50% { opacity: 1; transform: scale(1); } }

.chat-main { position: relative; min-height: 0; overflow: hidden; }
.message-scroller {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  padding: 15px 13px 24px 42px;
}
.message-list { display: grid; gap: 16px; }
.history-control {
  display: block;
  margin: 0 auto 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 12px;
  background: rgba(255, 255, 255, .82);
  color: var(--muted);
  font-size: 11px;
}
.history-control:disabled { opacity: .62; }
.turn-group { display: grid; gap: 9px; scroll-margin-top: 14px; }
.round-label { color: #a2a59f; font: 600 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.08em; }
.message { max-width: 94%; font-size: 14px; line-height: 1.58; overflow-wrap: anywhere; }
.message.user {
  justify-self: end;
  padding: 11px 13px;
  border-radius: 16px 16px 5px 16px;
  background: var(--green-soft);
}
.message.agent { justify-self: start; width: 100%; }
.agent-label { display: flex; align-items: center; gap: 7px; margin-bottom: 6px; color: var(--muted); font-size: 11px; }
.agent-label img { width: 24px; height: 24px; border-radius: 8px; }
.agent-bubble {
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 5px 16px 16px 16px;
  background: var(--surface-strong);
}
.agent-bubble p, .message.user p { margin: 0 0 0.75em; }
.agent-bubble p:last-child, .message.user p:last-child { margin-bottom: 0; }
.agent-bubble pre {
  margin: 10px 0;
  padding: 11px;
  overflow-x: auto;
  border-radius: 10px;
  background: #242724;
  color: #f4f4ef;
  font: 11px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.message-running { display: inline-flex; gap: 5px; padding: 10px 12px; color: var(--muted); }
.message-running i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: pulse 1.2s infinite; }
.message-running i:nth-child(2) { animation-delay: 0.18s; }
.message-running i:nth-child(3) { animation-delay: 0.36s; }
@keyframes pulse { 0%, 70%, 100% { opacity: .25; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-3px); } }

.tool-stack { display: grid; gap: 6px; }
.tool-card {
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: rgba(255, 255, 255, .68);
  font-size: 11px;
}
.tool-card summary { display: flex; align-items: center; gap: 7px; padding: 9px 11px; cursor: pointer; color: var(--muted); }
.tool-card summary::-webkit-details-marker { display: none; }
.tool-card pre { margin: 0; padding: 0 11px 11px; max-height: 220px; overflow: auto; white-space: pre-wrap; font: 10px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
.file-change-list { margin: 0; padding: 0 12px 11px 27px; color: var(--muted); }
.media-link { display: block; color: inherit; text-decoration: none; }
.transcript-image { display: block; width: 100%; max-height: 520px; object-fit: contain; border-radius: 10px; background: #eeeee9; }
.transcript-image.relay-media:not([src]) { min-height: 120px; }
.transcript-image.media-loading { opacity: .72; }
.transcript-image.media-failed { min-height: 90px; padding: 24px 12px; border: 1px dashed var(--line); color: var(--muted); cursor: pointer; }
.tool-card .media-link { padding: 0 8px 8px; }
.attachment-preview { max-width: min(88%, 430px); padding: 7px; border: 1px solid var(--line); border-radius: 12px; background: white; }
.attachment-preview .transcript-image { max-height: 280px; }
.attachment-preview span { display: block; padding: 6px 2px 1px; overflow: hidden; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }

.approval-stack { display: grid; gap: 8px; margin-bottom: 12px; }
.approval-card { border: 1px solid #e7cda9; border-radius: 14px; padding: 12px; background: #fff8ed; }
.approval-card strong { display: block; font-size: 13px; }
.approval-card p { margin: 5px 0 10px; color: #705b3f; font-size: 11px; line-height: 1.5; }
.approval-actions { display: flex; gap: 7px; }
.approval-actions button { border: 1px solid #dcc39e; border-radius: 9px; padding: 7px 10px; background: white; font-size: 11px; }
.approval-actions .approve { border-color: var(--green); background: var(--green); color: white; }

.queue-notice { margin: 16px 0 0; padding: 10px 12px; border-radius: 12px; background: #f0eee8; color: var(--muted); font-size: 11px; }

.turn-scrubber {
  position: absolute;
  z-index: 7;
  left: 2px;
  top: 10px;
  bottom: 10px;
  width: 26px;
  touch-action: none;
  cursor: ns-resize;
}
.scrubber-ticks { height: 100%; display: flex; flex-direction: column; justify-content: space-between; padding: 1px 0; }
.scrubber-tick { width: 6px; height: 2px; border-radius: 3px; background: #b8bab5; transition: width 100ms, background 100ms; }
.turn-scrubber.dragging .scrubber-tick.near { width: 12px; background: #777b76; }
.turn-scrubber.dragging .scrubber-tick.active { width: 24px; background: var(--charcoal); }
.turn-scrubber.dragging .scrubber-tick.far { width: 18px; background: #858985; }
.scrubber-preview {
  position: absolute;
  z-index: 12;
  left: 47px;
  width: min(76%, 430px);
  padding: 11px 13px;
  border-radius: 13px;
  background: rgba(40, 42, 40, .96);
  color: white;
  box-shadow: 0 10px 28px rgba(24, 26, 24, .24);
  pointer-events: none;
  transform: translateY(-50%);
}
.scrubber-preview strong, .scrubber-preview span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scrubber-preview strong { font-size: 12px; }
.scrubber-preview span { margin-top: 5px; color: rgba(255, 255, 255, .62); font-size: 10px; }

.composer {
  z-index: 20;
  padding: 9px 10px calc(var(--safe-bottom) + 9px);
  border-top: 1px solid var(--line-soft);
  background: rgba(250, 249, 246, .98);
}
.composer textarea {
  display: block;
  width: 100%;
  max-height: 130px;
  resize: none;
  border: 0;
  outline: 0;
  padding: 5px 7px 10px;
  background: transparent;
  font-size: 15px;
  line-height: 1.45;
}
.composer-controls, .composer-left, .composer-right { display: flex; align-items: center; }
.composer-controls { justify-content: space-between; gap: 8px; }
.composer-left, .composer-right { gap: 7px; min-width: 0; }
.round-button { width: 34px; height: 34px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 50%; background: white; font-size: 22px; }
.select-pill { display: block; min-width: 0; position: relative; }
.select-pill select {
  max-width: 132px;
  height: 34px;
  padding: 0 28px 0 11px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: white;
  appearance: none;
  outline: 0;
  font-size: 11px;
  text-overflow: ellipsis;
}
.select-pill::after { content: "⌄"; position: absolute; right: 10px; top: 7px; pointer-events: none; font-size: 11px; }
.model-pill select { max-width: 104px; }
.mode-pill:has(select[value="emergency"]) select { color: var(--red); }
.voice-button { width: 34px; height: 34px; border-radius: 50%; }
.voice-button svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.voice-button.listening { color: var(--red); background: #f8e7e4; animation: voice 1.15s ease-in-out infinite; }
@keyframes voice { 50% { box-shadow: 0 0 0 5px rgba(182, 76, 67, .09); } }
.send-button { width: 38px; height: 38px; flex: 0 0 auto; border-radius: 50%; background: var(--green); color: white; font-size: 23px; font-weight: 700; }
.send-button:disabled { opacity: .4; }
.icon-button.stop-task { color: var(--red); font-size: 13px; }
.attachment-tray { display: flex; gap: 7px; overflow-x: auto; padding: 0 4px 8px; }
.attachment-chip { flex: 0 0 auto; display: flex; align-items: center; gap: 6px; max-width: 210px; padding: 7px 9px; border: 1px solid var(--line); border-radius: 10px; background: white; font-size: 10px; }
.attachment-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-chip button { border: 0; background: transparent; color: var(--muted); }

.sheet-dialog { width: min(100% - 18px, 520px); margin: auto auto 8px; padding: 0; border: 0; border-radius: 24px; background: var(--surface-strong); box-shadow: var(--shadow); }
.sheet-dialog::backdrop { background: rgba(24, 26, 24, .35); backdrop-filter: blur(2px); }
.sheet-dialog form { display: grid; gap: 16px; padding: 10px 18px calc(var(--safe-bottom) + 19px); }
.sheet-handle { justify-self: center; width: 40px; height: 4px; border-radius: 99px; background: var(--line); }
.sheet-heading { display: flex; align-items: center; justify-content: space-between; }
.sheet-heading h2 { margin: 3px 0 0; font-size: 23px; }
.field-label { display: grid; gap: 7px; color: var(--muted); font-size: 11px; font-weight: 650; }
.field-label input, .field-label select { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: white; color: var(--ink); outline: 0; }

.toast { position: fixed; z-index: 100; left: 50%; bottom: calc(var(--safe-bottom) + 92px); transform: translateX(-50%); max-width: min(88vw, 420px); padding: 10px 14px; border-radius: 12px; background: var(--charcoal); color: white; box-shadow: var(--shadow); font-size: 12px; text-align: center; }

@media (min-width: 680px) {
  body { padding: 24px; }
  .app-shell { min-height: calc(100dvh - 48px); border: 1px solid var(--line); border-radius: 26px; box-shadow: var(--shadow); }
  .screen, .gate-view { min-height: calc(100dvh - 48px); }
  .chat-screen { height: calc(100dvh - 48px); }
}
