:root {
  color-scheme: light dark;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --border: #dde2ea;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-2: #06b6d4;
  --ok: #16a34a;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #d97706;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.06), 0 4px 16px rgb(15 23 42 / 0.06);
  --radius: 14px;
  --mono: ui-monospace, 'Cascadia Code', 'JetBrains Mono', Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1016;
    --surface: #121922;
    --surface-2: #19222e;
    --border: #243040;
    --text: #e6edf3;
    --muted: #8b98a9;
    --accent: #818cf8;
    --accent-2: #22d3ee;
    --ok: #4ade80;
    --danger: #f87171;
    --danger-soft: #2a1618;
    --warn: #fbbf24;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.3);
  }
}

* {
  box-sizing: border-box;
}

[x-cloak] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

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

button {
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.5;
}

a {
  color: var(--accent);
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
  padding: 0.1em 0.35em;
  border-radius: 6px;
  background: var(--surface-2);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 60rem;
  margin: 0 auto;
}

/* Top bar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: calc(env(safe-area-inset-top) + 0.75rem) calc(env(safe-area-inset-right) + 1rem) 0.75rem calc(env(safe-area-inset-left) + 1rem);
  border-bottom: 1px solid var(--border);
}

.signed-out .topbar {
  border-bottom: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
}

.signed-out .brand {
  visibility: hidden;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--surface-2);
}

.dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: currentColor;
}

.is-online {
  color: var(--ok);
}

.is-online .dot {
  animation: pulse 2s ease-in-out infinite;
}

.is-offline {
  color: var(--danger);
}

@keyframes pulse {
  50% {
    box-shadow: 0 0 0 0.3rem color-mix(in srgb, currentColor 25%, transparent);
  }
}

.icon-button {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border: 0;
  border-radius: 50%;
  background: transparent;
}

.icon-button:hover,
.icon-button[aria-expanded="true"] {
  background: var(--surface-2);
}

.icon-button svg {
  fill: currentColor;
}

.menu {
  position: relative;
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 0.4rem);
  z-index: 10;
  display: grid;
  min-width: 12rem;
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.menu-panel a,
.menu-panel button {
  padding: 0.6rem 0.75rem;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  text-align: left;
  text-decoration: none;
}

.menu-panel a:hover,
.menu-panel button:hover {
  background: var(--surface-2);
}

.menu-user {
  padding: 0.5rem 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu-version {
  margin-top: 0.3rem;
  padding: 0.5rem 0.75rem 0.25rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.75rem;
  white-space: pre-line;
}

.agent-info {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 1rem;
  margin: 0.75rem 0 0;
  font-size: 0.85rem;
}

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

.agent-info dd {
  margin: 0;
  font-family: var(--mono);
  overflow-wrap: anywhere;
}

.banner {
  margin: 0;
  padding: 0.6rem 1rem;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.9rem;
  text-align: center;
}

main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem calc(env(safe-area-inset-right) + 1rem) 1rem calc(env(safe-area-inset-left) + 1rem);
}

.error {
  color: var(--danger);
}

/* Login */

.login {
  display: grid;
  justify-items: center;
  gap: 0.75rem;
  max-width: 24rem;
  margin: 8vh auto 0;
  text-align: center;
}

.login img {
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.login h1 {
  margin: 0.5rem 0 0;
  font-size: 2rem;
  letter-spacing: -0.02em;
}

.login > p {
  margin: 0 0 1rem;
  color: var(--muted);
}

.login form {
  display: grid;
  gap: 0.6rem;
  width: 100%;
  padding: 1.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
}

.login label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.field {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}

.field:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.field span {
  color: var(--muted);
}

.field input {
  flex: 1;
  min-width: 0;
  padding: 0.75rem 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 1rem;
}

.primary {
  padding: 0.8rem 1rem;
  border: 0;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #fff;
  font-weight: 600;
}

/* Console */

.console {
  display: grid;
  gap: 0.75rem;
}

.empty {
  margin: 12vh auto 0;
  max-width: 22rem;
  text-align: center;
  color: var(--muted);
}

.empty p {
  margin: 0.25rem 0;
}

.entry {
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-left: 3px solid var(--ok);
  border-radius: 10px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 0.9rem;
}

.status-failed,
.status-offline,
.status-timeout {
  border-left-color: var(--danger);
}

.status-busy {
  border-left-color: var(--warn);
}

.status-pending {
  border-left-color: var(--accent);
}

.entry-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.prompt {
  color: var(--accent);
  font-family: var(--mono);
}

.line {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.entry time {
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
}

.badge {
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--danger-soft);
  color: var(--danger);
  font-family: var(--sans);
  font-size: 0.75rem;
  font-weight: 600;
}

.status-busy .badge {
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 15%, transparent);
}

.entry pre {
  margin: 0.5rem 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: inherit;
  color: var(--muted);
}

.status-failed pre,
.status-offline pre,
.status-timeout pre {
  color: var(--danger);
}

.spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* Composer */

.composer {
  display: grid;
  gap: 0.6rem;
  padding: 0.75rem calc(env(safe-area-inset-right) + 1rem) calc(env(safe-area-inset-bottom) + 0.75rem) calc(env(safe-area-inset-left) + 1rem);
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.chips {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  scrollbar-width: none;
}

.chip {
  flex: none;
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 0.85rem;
}

.chip:not(:disabled):hover {
  border-color: var(--accent);
  color: var(--accent);
}

.composer form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.25rem 0.25rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.composer form:focus-within {
  border-color: var(--accent);
}

.composer input {
  flex: 1;
  min-width: 0;
  padding: 0.6rem 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-family: var(--mono);
  font-size: 1rem;
}

.send {
  display: grid;
  place-items: center;
  width: 2.6rem;
  height: 2.6rem;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
}

.send svg {
  fill: #fff;
}

/* Pi switcher */

.switcher {
  border: 0;
  cursor: pointer;
}

.switcher svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.switcher-name {
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.agent-option {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.agent-option.is-selected {
  background: var(--surface-2);
  font-weight: 600;
}

.menu-panel hr {
  width: 100%;
  margin: 0.3rem 0;
  border: 0;
  border-top: 1px solid var(--border);
}

/* Pi management */

.agents {
  display: grid;
  gap: 0.75rem;
  max-width: 36rem;
  margin: 0 auto;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-head h2 {
  margin: 0.25rem 0;
  font-size: 1.4rem;
}

.card {
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.ghost {
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
}

.ghost:hover {
  background: var(--surface-2);
}

.danger {
  color: var(--danger);
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

.token-card {
  border-color: var(--accent);
}

.token-card p {
  margin: 0 0 0.75rem;
}

.token-card pre {
  margin: 0 0 0.75rem;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 0.85rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  user-select: all;
}

.agent-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.agent-head strong {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.actions {
  margin-top: 0.75rem;
}

.inline-form,
.add-form .row {
  display: flex;
  flex: 1;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.inline-form input,
.add-form input {
  flex: 1;
  min-width: 8rem;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  outline: 0;
}

.inline-form input:focus,
.add-form input:focus {
  border-color: var(--accent);
}

.inline-form .primary,
.add-form .primary {
  padding: 0.55rem 1rem;
}

.add-form label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.empty .primary {
  margin-top: 0.75rem;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
