:root {
  --bg: #000;
  --bg-2: #0a0a0a;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --text: #f5f5f7;
  --muted: #8e8e93;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.16);
  --danger: #ff453a;
  --ok: #30d158;
  --warn: #ff9f0a;
  --line: rgba(255, 255, 255, 0.1);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 0; }

.btn { border: 0; border-radius: 10px; padding: 9px 16px; background: var(--surface-2); font-weight: 500; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger.ghost { color: var(--danger); border-color: rgba(255, 69, 58, 0.4); background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.armed, .danger-text.armed { background: var(--danger); color: #fff; }
.danger-text { color: var(--danger); }
.icon-btn {
  border: 0; background: transparent; width: 36px; height: 36px; border-radius: 10px; font-size: 18px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--muted); flex: none;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }
.icon-btn.small { width: auto; height: 28px; padding: 0 8px; font-size: 13px; }
.link-btn { border: 0; background: none; color: var(--muted); font-size: 13px; padding: 6px 8px; text-align: left; }
.field {
  width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 11px; outline: none;
}
.field:focus { border-color: var(--accent); }
textarea.field { min-height: 90px; resize: vertical; }
textarea.field.tall { min-height: 200px; }
.label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }

/* login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  padding: 32px 24px; display: grid; gap: 14px; text-align: center;
}
.login-mark {
  width: 56px; height: 56px; margin: 0 auto; border-radius: 14px; background: var(--accent);
  display: grid; place-items: center; font-size: 28px; font-weight: 700; color: #fff;
}
.login-card h1 { margin: 0; font-weight: 600; font-size: 22px; }
.login-card .field { background: var(--bg-2); }

/* layout */
.app { display: grid; grid-template-columns: 280px minmax(0, 1fr) auto; height: 100dvh; }
.sidebar {
  background: var(--bg-2); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  gap: 8px; padding: 14px 10px; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px 8px; }
.brand { font-weight: 600; font-size: 17px; }
.badge-btn { border: 0; border-radius: 999px; background: var(--warn); color: #000; font-size: 12px; font-weight: 600; padding: 3px 10px; }
.bot-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 0; }
.bot {
  display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: transparent;
  padding: 8px; border-radius: 12px; text-align: left;
}
.bot:hover { background: var(--surface); }
.bot.selected { background: var(--surface-2); }
.bot-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.bot-name { font-weight: 500; }
.bot-desc { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar {
  width: 34px; height: 34px; border-radius: 10px; display: inline-grid; place-items: center;
  font-weight: 600; color: #fff; flex: none;
}
.avatar.small { width: 26px; height: 26px; border-radius: 8px; font-size: 13px; }
.avatar.large { width: 64px; height: 64px; border-radius: 18px; font-size: 28px; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.dot.idle { background: #3a3a3c; }
.dot.working { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.dot.waiting { background: var(--warn); }
.dot.offline { background: transparent; border: 1px solid var(--muted); }
@keyframes pulse { 50% { opacity: 0.35; } }
.chat-list { display: flex; flex-direction: column; margin: 2px 0 8px 44px; gap: 1px; }
.chat-item {
  display: flex; justify-content: space-between; gap: 8px; border: 0; background: transparent;
  padding: 5px 8px; border-radius: 8px; color: var(--muted); font-size: 13px; text-align: left;
}
.chat-item:hover { color: var(--text); background: var(--surface); }
.chat-item.selected { color: var(--text); background: var(--accent-soft); }
.chat-item-title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-item-time { flex: none; font-size: 12px; }
.new-bot { width: 100%; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head { container-type: inline-size; display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--line); min-height: 57px; }
.chat-title { flex: 1 1 0; min-width: 0; overflow: hidden; display: flex; align-items: center; gap: 10px; }
.chat-title-name { flex: none; font-weight: 600; white-space: nowrap; }
.chat-title-chat {
  flex: 0 1 auto; min-width: 0; border: 0; background: transparent; color: var(--muted); overflow: hidden;
  white-space: nowrap; text-overflow: ellipsis; max-width: 40vw; padding: 4px 6px; border-radius: 8px;
}
.chat-delete { flex: none; }
/* Not enough room for everything (phone, or the side panel open): drop the avatar and let the name shorten
   so the Delete button is never cut off. */
@container (max-width: 480px) {
  .chat-title { gap: 6px; }
  .chat-title .avatar { display: none; }
  .chat-title-name { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
}
.chat-title-chat:hover { background: var(--surface); color: var(--text); }
.rename-input { background: var(--surface); border: 1px solid var(--accent); border-radius: 8px; padding: 4px 8px; min-width: 0; outline: none; }
.chat-head-actions { flex: none; display: flex; gap: 6px; align-items: center; }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px 12px; display: flex; flex-direction: column; gap: 14px; }
.messages > * { width: 100%; max-width: 760px; margin-left: auto; margin-right: auto; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg.user .msg-text {
  background: var(--accent); color: #fff; padding: 9px 14px; border-radius: 18px 18px 4px 18px;
  max-width: 80%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-img { max-width: 220px; max-height: 220px; border-radius: 12px; border: 1px solid var(--line); }
.msg.assistant { display: flex; flex-direction: column; gap: 8px; }
.msg-md { overflow-wrap: anywhere; }
.msg-md:empty { display: none; }
.msg-md > :first-child { margin-top: 0; }
.msg-md > :last-child { margin-bottom: 0; }
.msg-md pre { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; }
.msg-md code { font: 13px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.msg-md :not(pre) > code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.msg-md table { border-collapse: collapse; display: block; overflow-x: auto; }
.msg-md th, .msg-md td { border: 1px solid var(--line); padding: 5px 9px; }
.msg-md img { max-width: 100%; }
.steps { display: flex; flex-direction: column; gap: 6px; }
.steps:empty { display: none; }
.step { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; font-size: 13px; }
.step summary { display: flex; align-items: center; gap: 8px; padding: 7px 10px; cursor: pointer; list-style: none; min-width: 0; }
.step summary::-webkit-details-marker { display: none; }
.step-icon { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.step.running .step-icon { background: var(--accent); animation: pulse 1s infinite; }
.step.failed .step-icon { background: var(--danger); }
.step-name { font-weight: 500; flex: none; }
.step-preview {
  color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; min-width: 0;
  font: 12px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.step-result {
  margin: 0; padding: 8px 10px 10px; border-top: 1px solid var(--line); white-space: pre-wrap;
  overflow-wrap: anywhere; color: var(--muted); font-size: 12px; max-height: 260px; overflow-y: auto;
}
.step-result:empty { display: none; }
.commentary { color: var(--muted); font-size: 13px; }
.thinking { color: var(--muted); font-size: 13px; }
.thinking summary { cursor: pointer; }
.thinking-text { white-space: pre-wrap; padding: 6px 0 0 12px; border-left: 2px solid var(--line); margin-top: 4px; }
.approval {
  background: rgba(255, 159, 10, 0.08); border: 1px solid rgba(255, 159, 10, 0.45); border-radius: 12px;
  padding: 12px; display: grid; gap: 8px;
}
.approval-title { font-weight: 600; color: var(--warn); }
.approval-cmd {
  margin: 0; background: var(--bg); border-radius: 8px; padding: 8px 10px; font-size: 12px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.approval-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.approval.resolved { opacity: 0.7; }
.approval-done { color: var(--muted); font-size: 13px; }
.note { color: var(--muted); font-size: 13px; }
.note.error { color: var(--danger); }
.typing { display: flex; gap: 4px; padding: 4px 0; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: pulse 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
.empty { margin: auto; text-align: center; color: var(--muted); display: grid; justify-items: center; gap: 6px; padding: 40px 0; }
.empty h2 { color: var(--text); margin: 8px 0 0; font-weight: 600; }
.empty p { margin: 0; }

.composer { padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer-row {
  max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 22px; padding: 6px 6px 6px 8px;
}
.composer textarea { flex: 1; resize: none; border: 0; background: transparent; outline: none; padding: 7px 4px; max-height: 200px; min-height: 22px; }
.composer .btn { border-radius: 16px; }
.attach { cursor: pointer; }
.attachments { max-width: 760px; margin: 0 auto 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.attachments:empty { display: none; }
.thumb { position: relative; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); display: block; }
.thumb-x {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 0;
  background: var(--surface-2); font-size: 12px; line-height: 1;
}

.panel { width: 360px; border-left: 1px solid var(--line); background: var(--bg-2); display: flex; flex-direction: column; min-height: 0; }
.panel-tabs { display: flex; align-items: flex-start; gap: 4px; padding: 10px; border-bottom: 1px solid var(--line); }
.tab-row { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; gap: 4px; }
.tab { border: 0; background: transparent; color: var(--muted); padding: 6px 8px; border-radius: 8px; font-size: 13px; }
.tab.selected { background: var(--surface-2); color: var(--text); }
.panel-tabs .icon-btn { flex: none; width: 30px; height: 30px; }
.panel-body { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.panel-body h3 { margin: 6px 0 0; font-size: 14px; font-weight: 600; }
.panel-body p { margin: 0; font-size: 13px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 28px; height: 28px; border-radius: 8px; border: 2px solid transparent; }
.swatch.selected { border-color: var(--text); }
.file-list { display: flex; flex-direction: column; gap: 4px; }
.file { display: flex; flex-direction: column; padding: 8px 10px; border-radius: 10px; background: var(--surface); text-decoration: none; color: var(--text); }
.file:hover { background: var(--surface-2); }
.file-name { font-weight: 500; overflow-wrap: anywhere; }
.file-meta { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.routine-list { display: flex; flex-direction: column; gap: 8px; }
.routine { background: var(--surface); border-radius: 12px; padding: 10px 12px; display: grid; gap: 6px; }
.routine-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.routine-prompt { font-size: 13px; color: var(--muted); white-space: pre-wrap; max-height: 80px; overflow: hidden; }
.routine-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.on { background: var(--accent-soft); color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--line); width: 100%; margin: 6px 0; }

.modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px); display: grid; place-items: center; padding: 16px; z-index: 50;
}
.modal-card {
  width: min(520px, 100%); max-height: calc(100dvh - 32px); overflow-y: auto; background: var(--surface);
  border: 1px solid var(--line); border-radius: 20px; padding: 22px; display: grid; gap: 12px;
}
.modal-card h2 { margin: 0; font-weight: 600; }
.modal-card .field { background: var(--bg-2); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--surface-2);
  border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; z-index: 60; max-width: calc(100vw - 32px);
}
.scrim { display: none; }
.mobile-only { display: none; }

@media (max-width: 860px), (pointer: coarse) {
  input, textarea { font-size: 16px; }
}

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(84vw, 320px); z-index: 40;
    transform: translateX(-100%); transition: transform 0.2s ease;
  }
  body.sidebar-open .sidebar { transform: none; }
  body.sidebar-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); z-index: 35; }
  .panel { position: fixed; inset: 0; width: auto; z-index: 45; border-left: 0; }
  .mobile-only { display: inline-flex; }
  .chat-head { padding: 10px 12px; }
  .chat-title-chat { max-width: 28vw; }
  .messages { padding: 16px 16px 8px; }
  .composer { padding-left: 10px; padding-right: 10px; }
}
