:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #1b2430;
  --muted: #5b6776;
  --border: #d8dee5;
  --accent: #0e3f51;   /* Tabulai navy */
  --accent-text: #ffffff;
  --danger: #b42318;
  --ok: #18794e;
  --warn: #9a6700;
  --muse: #7a4fd6;
  --instinct: #0f7c80;
  --claude: #c2591b;
  --radius: 12px;
  --tap: 44px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141a;
    --surface: #171e26;
    --surface-2: #202934;
    --text: #e6ebf1;
    --muted: #9aa7b6;
    --border: #2c3744;
    --accent: #e6ebf1;
    --accent-text: #0f141a;
    --danger: #ff8a80;
    --ok: #6fd6a6;
    --warn: #f2c36b;
    --muse: #b49cff;
    --instinct: #5ccfd3;
    --claude: #f0a06b;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f141a;
  --surface: #171e26;
  --surface-2: #202934;
  --text: #e6ebf1;
  --muted: #9aa7b6;
  --border: #2c3744;
  --accent: #e6ebf1;
  --accent-text: #0f141a;
  --danger: #ff8a80;
  --ok: #6fd6a6;
  --warn: #f2c36b;
  --muse: #b49cff;
  --instinct: #5ccfd3;
  --claude: #f0a06b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: anywhere;
}

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

button, select {
  min-height: var(--tap);
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 0 14px;
  cursor: pointer;
}
button:disabled { opacity: 0.6; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }

input, textarea {
  width: 100%;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  padding: 10px 12px;
}

:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.icon-btn { min-width: var(--tap); padding: 0; font-size: 20px; line-height: 1; }
.icon-btn.small { min-width: 32px; min-height: 32px; font-size: 18px; border-color: transparent; background: transparent; color: var(--muted); }

.status { margin: 0; color: var(--muted); }
.status:empty { display: none; }
.status.error, .updated.error { color: var(--danger); }
.hint { color: var(--muted); font-size: 14px; }

/* ---------------------------------------------------------------- login */

body.login { display: grid; place-items: center; padding: 16px; }

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px;
  display: grid;
  gap: 16px;
}
.login-card h1 { margin: 0; display: flex; align-items: center; gap: 10px; font-size: 24px; }
.login-card h2 { margin: 0; font-size: 18px; }
.login-card section { display: grid; gap: 12px; }
.login-card #signin-btn { width: 100%; }
.login-card details { border-top: 1px solid var(--border); padding-top: 4px; }
.login-card summary { min-height: var(--tap); display: flex; align-items: center; cursor: pointer; color: var(--muted); }
.login-card details > :not(summary) { margin: 0 0 12px; }
.login-card label { display: grid; margin-bottom: 12px; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card details button { width: 100%; }

.codes { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 15px; padding-left: 28px; margin: 0; display: grid; gap: 4px; }

/* ---------------------------------------------------------------- app layout */

body.app {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  height: 100dvh;
  max-width: 900px;
  margin: 0 auto;
}

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand picture, .login-card h1 picture { display: flex; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.updated { margin: 0 0 0 auto; color: var(--muted); font-size: 14px; }

.menu {
  position: absolute;
  right: 16px;
  top: calc(100% + 4px);
  z-index: 10;
  display: grid;
  min-width: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
}
.menu[hidden] { display: none; }
.menu button { border: 0; text-align: left; background: transparent; }
.menu button:hover { background: var(--surface-2); }

.agents {
  list-style: none;
  margin: 0;
  padding: 8px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.agent { display: flex; align-items: center; gap: 6px; }
.agent .name { font-weight: 600; }
.agent .state, .agent .count { color: var(--muted); }
.agent .count::before { content: "· "; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.online { background: var(--ok); }
.dot.paused { background: var(--warn); }

/* ---------------------------------------------------------------- feed */

.feed {
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
}
.empty { color: var(--muted); text-align: center; margin: auto; }

.msg {
  --agent: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--agent);
  border-radius: var(--radius);
  padding: 8px 12px 10px;
  max-width: 100%;
}
.msg.mine { background: var(--surface-2); }
.agent-muse { --agent: var(--muse); }
.agent-instinct { --agent: var(--instinct); }
.agent-claude { --agent: var(--claude); }

.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 13px; color: var(--muted); }
.meta .who { color: var(--agent); font-weight: 700; font-size: 14px; }
.meta .end { margin-left: auto; display: flex; align-items: center; gap: 2px; white-space: nowrap; }
.body { white-space: pre-wrap; margin-top: 4px; }
.quote { border-left: 2px solid var(--border); padding-left: 8px; color: var(--muted); font-size: 14px; margin-bottom: 6px; white-space: normal; }

.badge { border: 1px solid var(--border); border-radius: 999px; padding: 0 8px; font-size: 12px; line-height: 20px; }
.badge.sample { border-style: dashed; }
.badge.connected { color: var(--ok); border-color: currentColor; }
.badge.status-pending, .badge.status-claimed { color: var(--warn); border-color: currentColor; }
.badge.status-done { color: var(--ok); border-color: currentColor; }
.badge.status-failed { color: var(--danger); border-color: currentColor; }

/* ---------------------------------------------------------------- composer */

.composer {
  display: grid;
  gap: 8px;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer select { flex: 0 1 auto; min-width: 0; }
.request-toggle { display: flex; align-items: center; gap: 8px; min-height: var(--tap); font-size: 14px; color: var(--muted); cursor: pointer; }
.request-toggle input { width: 20px; min-height: 20px; height: 20px; margin: 0; }
.composer textarea { flex: 1; resize: none; max-height: 40vh; field-sizing: content; }
.composer #send { flex: none; }

dialog {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h2 { margin-top: 0; font-size: 18px; }
dialog form { margin-top: 16px; text-align: right; }

@media (min-width: 720px) {
  .msg { max-width: 85%; }
  .msg.mine { align-self: flex-end; }
}
