:root {
  color-scheme: light dark;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --line: #deded8;
  --accent: #2f5fd0;
  --accent-text: #ffffff;
  --error: #b42318;
  --on: #1f9d55;
  --busy: #c27c0e;
  --partial: #8a8a84;
  --mine: #eef3ff;
  --directed: #fff7e6;
  --code: #f0f0ec;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --text: #e8e8e3;
    --muted: #9a9a93;
    --line: #34342f;
    --accent: #7d9cf0;
    --accent-text: #10131c;
    --error: #ff8a7a;
    --on: #4cc27e;
    --busy: #e2a53a;
    --partial: #77776f;
    --mine: #1f2638;
    --directed: #2e2716;
    --code: #2a2a27;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 1.15rem; }
h2 { font-size: 0.95rem; margin-bottom: 0.5rem; }
p { margin: 0.4rem 0; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.error { color: var(--error); margin: 0.3rem 0; min-height: 0; }
.error:empty { display: none; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.4rem 0.55rem;
  width: 100%;
}

textarea { resize: vertical; min-height: 4.5rem; }

button {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 6px;
  padding: 0.42rem 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.link {
  background: none;
  color: var(--accent);
  padding: 0 0.2rem;
  text-decoration: underline;
}

label { display: block; margin: 0.45rem 0; font-size: 0.9rem; }
label > input, label > select { margin-top: 0.2rem; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.topbar-run { display: flex; align-items: baseline; gap: 0.7rem; min-width: 0; }
.topbar-run h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-me { display: flex; align-items: center; gap: 0.7rem; font-size: 0.9rem; }

.link-state { font-size: 0.8rem; padding: 0.1rem 0.5rem; border-radius: 999px; }
.link-state:empty { display: none; }
.link-state[data-state="live"] { color: var(--on); border: 1px solid var(--on); }
.link-state[data-state="retrying"] { color: var(--busy); border: 1px solid var(--busy); }
.link-state[data-state="stopped"] { color: var(--error); border: 1px solid var(--error); }

.entry {
  max-width: 28rem;
  margin: 3rem auto;
  padding: 1.25rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.entry button[type="submit"] { margin-top: 0.6rem; }

.run-view {
  flex: 1 1 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 21rem;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
}

.conversation {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
}

.transcript-scroll { flex: 1; overflow-y: auto; padding: 0.75rem 1rem; }
.transcript { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.empty { text-align: center; margin-top: 2rem; }

.entry-item {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.5rem 0.7rem;
}
.entry-item.directed { background: var(--directed); }
.entry-item.mine { background: var(--mine); }

.msg-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.4rem; font-size: 0.85rem; }
.msg-from, .msg-to { font-weight: 600; }
.msg-head .reply-button { margin-left: auto; font-size: 0.8rem; }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 0.25rem; }
.reply-ref { font-size: 0.82rem; margin-top: 0.2rem; overflow-wrap: anywhere; }

.deliveries { list-style: none; margin: 0.35rem 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.3rem; }
.delivery {
  font-size: 0.78rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.05rem 0.5rem;
  cursor: help;
}
.delivery-submitted { border-color: var(--on); }
.delivery-uncertain { border-color: var(--busy); }
.delivery-failed { border-color: var(--error); }

.artifact-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.6rem; margin-top: 0.25rem; }
.artifact-name { font-weight: 600; overflow-wrap: anywhere; }
.artifact-sha { font-size: 0.8rem; }

.say { border-top: 1px solid var(--line); background: var(--surface); padding: 0.6rem 1rem 0.8rem; }
.say-row { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.4rem; }
.say-row select { width: auto; min-width: 10rem; }
.say-count { margin-left: auto; font-size: 0.8rem; }
.say-actions { margin: 0.4rem 0 0; justify-content: flex-end; }
.say-actions .error { margin-right: auto; }
.say-reply { display: flex; gap: 0.5rem; font-size: 0.85rem; margin-bottom: 0.4rem; color: var(--muted); }

.side { min-height: 0; overflow-y: auto; padding: 0.75rem; display: flex; flex-direction: column; gap: 0.75rem; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 0.7rem 0.8rem; }

.copy-row { display: flex; gap: 0.4rem; }

.roster { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.member { border-bottom: 1px solid var(--line); padding-bottom: 0.45rem; }
.member:last-child { border-bottom: 0; }
.member-line { display: flex; align-items: center; gap: 0.4rem; }
.member-name { font-weight: 600; overflow-wrap: anywhere; }
.member-tags { margin-left: auto; font-size: 0.8rem; }
.member-detail { display: flex; flex-wrap: wrap; gap: 0.15rem 0.6rem; font-size: 0.8rem; padding-left: 1rem; }
.member-worktree { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dot { width: 0.6rem; height: 0.6rem; border-radius: 50%; flex: none; border: 1.5px solid var(--partial); }
.dot-on { background: var(--on); border-color: var(--on); }
.dot-busy { background: var(--busy); border-color: var(--busy); }
.dot-partial { background: var(--partial); }
.dot-off { background: transparent; }

.agent-connect { margin-top: 0.4rem; }
.command {
  background: var(--code);
  border-radius: 6px;
  padding: 0.5rem;
  font-size: 0.78rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0.4rem 0;
}
.button-row { display: flex; gap: 0.4rem; flex-wrap: wrap; }

#publish-form button { margin-top: 0.4rem; }

.notice {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 0.4rem 0.9rem;
  border-radius: 6px;
  font-size: 0.9rem;
  max-width: calc(100vw - 2rem);
}

@media (max-width: 820px) {
  body { height: auto; min-height: 100vh; }
  .run-view { display: flex; flex-direction: column; flex: none; }
  .conversation { border-right: 0; border-bottom: 1px solid var(--line); }
  .transcript-scroll { flex: none; max-height: 65vh; }
  .side { overflow: visible; }
  .topbar { flex-wrap: wrap; }
}
