:root {
  color-scheme: dark;
  --bg: #0d1117;
  --surface: #161b22;
  --elevated: #1c2128;
  --border: #30363d;
  --text: #e6edf3;
  --dim: #8b949e;
  --accent: #58a6ff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --user: #1f6feb;
  --radius: 8px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sidebar: 300px;
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
}

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

code,
pre {
  font-family: var(--mono);
}

.muted {
  color: var(--dim);
}

.small {
  font-size: 0.85em;
}

.center {
  text-align: center;
}

.pad {
  padding: 0.75rem 1rem;
}

.pad-x {
  padding: 0 1rem;
}

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

p.error {
  margin: 0.5rem 0;
}

/* Controls */

.btn {
  border: 1px solid var(--border);
  background: var(--elevated);
  border-radius: var(--radius);
  padding: 0.5rem 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--border);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.primary {
  background: #238636;
  border-color: #2ea043;
  color: #fff;
}

.btn.primary:hover:not(:disabled) {
  background: #2ea043;
}

.btn.danger {
  background: transparent;
  border-color: var(--red);
  color: var(--red);
}

.btn.danger:hover:not(:disabled) {
  background: rgba(248, 81, 73, 0.15);
}

.btn.small {
  padding: 0.25rem 0.6rem;
  font-size: 0.85rem;
}

.btn.wide {
  width: 100%;
}

.linklike {
  background: none;
  border: 0;
  padding: 0;
  color: var(--dim);
  cursor: pointer;
  font-size: 0.85rem;
}

.linklike:hover {
  color: var(--text);
  text-decoration: underline;
}

.input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  outline: none;
}

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

textarea.input {
  resize: none;
  display: block;
}

.row {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.row.end {
  justify-content: flex-end;
}

.badge {
  display: inline-block;
  padding: 0 0.45rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--dim);
  font-size: 0.72rem;
  line-height: 1.5;
  white-space: nowrap;
}

.badge.terminal {
  color: var(--accent);
  border-color: var(--accent);
}

.badge.web {
  color: var(--green);
  border-color: var(--green);
}

.badge.running {
  color: var(--yellow);
  border-color: var(--yellow);
}

.badge.offline {
  color: var(--red);
  border-color: var(--red);
}

/* Sign-in */

.login {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: radial-gradient(ellipse at center, var(--surface) 0%, var(--bg) 70%);
}

.card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.logo {
  margin: 0 0 1rem;
  font-size: 2.5rem;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.lead {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0 0 0.5rem;
}

.keybox {
  width: 100%;
  margin-top: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  word-break: break-all;
  resize: none;
}

.card code {
  background: var(--bg);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  font-size: 0.85em;
}

/* Shell */

.shell {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
}

.session-scroll {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 0.5rem;
}

.group {
  margin: 1rem 1rem 0.25rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--dim);
}

.sessions {
  list-style: none;
  margin: 0;
  padding: 0;
}

.session {
  display: flex;
  gap: 0.6rem;
  padding: 0.55rem 1rem;
  cursor: pointer;
  border-left: 3px solid transparent;
}

.session:hover {
  background: var(--elevated);
}

.session.active {
  background: var(--elevated);
  border-left-color: var(--accent);
}

.dot {
  flex-shrink: 0;
  width: 9px;
  height: 9px;
  margin-top: 0.45rem;
  border-radius: 50%;
  border: 1px solid var(--dim);
}

.dot.live {
  background: var(--green);
  border-color: var(--green);
}

.dot.running {
  background: var(--yellow);
  border-color: var(--yellow);
  animation: pulse 1.2s ease-in-out infinite;
}

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

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

.session-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-sub {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.78rem;
  color: var(--dim);
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-foot {
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
}

.scrim {
  display: none;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 3.4rem;
}

.topbar-title {
  flex: 1;
  min-width: 0;
}

.title-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.subtitle {
  font-size: 0.78rem;
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.subtitle .status {
  margin-left: 0.75rem;
}

.icon-btn {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem;
  cursor: pointer;
}

.burger,
.burger::before,
.burger::after {
  display: block;
  width: 16px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
}

.burger {
  position: relative;
}

.burger::before,
.burger::after {
  content: "";
  position: absolute;
  left: 0;
}

.burger::before {
  top: -5px;
}

.burger::after {
  top: 5px;
}

.welcome {
  margin: auto;
  max-width: 28rem;
  padding: 2rem 1rem;
  text-align: center;
}

/* Conversation */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  overflow-anchor: none;
}

.earlier {
  align-self: center;
}

.msg {
  max-width: min(100%, 52rem);
  min-width: 0;
}

.msg.user {
  align-self: flex-end;
  max-width: min(85%, 42rem);
}

.bubble {
  background: var(--user);
  color: #fff;
  padding: 0.6rem 0.9rem;
  border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.assistant {
  align-self: flex-start;
  width: 100%;
}

.msg.assistant > .meta {
  margin-top: 0.25rem;
  font-size: 0.72rem;
  color: var(--dim);
}

.md {
  overflow-wrap: anywhere;
}

.md > :first-child {
  margin-top: 0;
}

.md > :last-child {
  margin-bottom: 0;
}

.md p,
.md ul,
.md ol,
.md pre,
.md table,
.md blockquote {
  margin: 0.6em 0;
}

.md h1,
.md h2,
.md h3,
.md h4 {
  margin: 1em 0 0.4em;
  line-height: 1.25;
}

.md h1 {
  font-size: 1.4em;
}

.md h2 {
  font-size: 1.2em;
}

.md h3,
.md h4 {
  font-size: 1.05em;
}

.md code {
  background: var(--elevated);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.88em;
}

.md pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.9rem;
  overflow-x: auto;
}

.md pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
}

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

.md blockquote {
  border-left: 3px solid var(--border);
  padding-left: 0.9rem;
  color: var(--dim);
}

.md table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
}

.md th,
.md td {
  border: 1px solid var(--border);
  padding: 0.3rem 0.6rem;
}

.thinking {
  margin: 0.4rem 0;
  color: var(--dim);
  border-left: 2px solid var(--border);
  padding-left: 0.75rem;
}

.thinking summary {
  cursor: pointer;
  font-size: 0.85rem;
}

.cursor {
  display: inline-block;
  width: 0.55em;
  height: 1.05em;
  vertical-align: text-bottom;
  background: var(--accent);
  animation: pulse 1s steps(2, start) infinite;
}

.tool {
  margin: 0.4rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.tool > summary {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  list-style: none;
  font-size: 0.88rem;
}

.tool > summary::-webkit-details-marker {
  display: none;
}

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

.tool.pending .tool-dot,
.tool.running .tool-dot {
  background: var(--yellow);
  animation: pulse 1.2s ease-in-out infinite;
}

.tool.failed .tool-dot {
  background: var(--red);
}

.tool-name {
  font-family: var(--mono);
  color: var(--accent);
  flex-shrink: 0;
}

.tool-headline {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--mono);
  color: var(--dim);
}

.tool-state {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--dim);
}

.tool.failed .tool-state {
  color: var(--red);
}

.tool-body {
  border-top: 1px solid var(--border);
  padding: 0.5rem 0.7rem;
}

.tool-command,
.tool-output {
  margin: 0;
  padding: 0.35rem 0;
  font-size: 0.82rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 24rem;
  overflow-y: auto;
}

.tool-output {
  color: var(--dim);
  border-top: 1px dashed var(--border);
}

.tool-path {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--dim);
  margin-bottom: 0.3rem;
}

.diff {
  font-family: var(--mono);
  font-size: 0.82rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0.3rem 0;
  border-radius: 6px;
  overflow: hidden;
}

.diff .del {
  background: rgba(248, 81, 73, 0.15);
  color: #ffa198;
}

.diff .add {
  background: rgba(63, 185, 80, 0.15);
  color: #7ee787;
}

.note {
  align-self: center;
  max-width: min(100%, 44rem);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.9rem;
  color: var(--dim);
  font-size: 0.9rem;
}

.note-title {
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 0.2rem;
}

/* Composer */

.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0.6rem 1rem calc(0.6rem + env(safe-area-inset-bottom));
}

.composer-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.composer-buttons {
  display: flex;
  gap: 0.35rem;
}

.widget {
  margin: 0 0 0.5rem;
  padding: 0.4rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
  white-space: pre-wrap;
  overflow-x: auto;
}

/* Dialogs and toasts */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.65);
}

.modal {
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.modal h3 {
  margin: 0 0 0.75rem;
}

.modal label {
  display: block;
  margin-top: 0.75rem;
  font-size: 0.85rem;
  color: var(--dim);
}

.modal label .input {
  margin-top: 0.25rem;
  color: var(--text);
}

.options {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.toasts {
  position: fixed;
  right: 1rem;
  top: 4rem;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(24rem, calc(100vw - 2rem));
}

.toast {
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--elevated);
  font-size: 0.88rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  overflow-wrap: anywhere;
}

.toast.error {
  border-color: var(--red);
  color: #ffa198;
}

.toast.warning {
  border-color: var(--yellow);
}

/* Narrow screens: the session list becomes a drawer */

@media (max-width: 760px) {
  .icon-btn {
    display: block;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    width: min(var(--sidebar), 85vw);
    transform: translateX(-100%);
    transition: transform 0.18s ease-out;
  }

  .shell.menu-open .sidebar {
    transform: none;
  }

  .shell.menu-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(0, 0, 0, 0.55);
  }

  .messages {
    padding: 0.75rem;
  }

  .composer {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  .toasts {
    left: 1rem;
  }
}
