:root {
  --bg: var(--tg-theme-bg-color, #1a1a2e);
  --text: var(--tg-theme-text-color, #e0e0e0);
  --hint: var(--tg-theme-hint-color, #8a8a9a);
  --link: var(--tg-theme-link-color, #6c9bff);
  --button: var(--tg-theme-button-color, #5b6eae);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #252540);
  --section-bg: var(--tg-theme-section-bg-color, #1e1e38);
  --section-header: var(--tg-theme-section-header-text-color, #8a8aaa);
  --destructive: var(--tg-theme-destructive-text-color, #ff5555);

  --radius: 12px;
  --radius-sm: 8px;
  --gap: 12px;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ── Header ─────────────────────────────────── */

.header {
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.header h1 {
  font-size: 18px;
  font-weight: 600;
}

.header .badge {
  background: #4caf50;
  color: white;
  font-size: 11px;
  font-weight: 600;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  animation: pulse 1.5s infinite;
}

/* ── Projects bar ───────────────────────────── */

.projects-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  overflow-x: auto;
  scrollbar-width: none;
}

.projects-bar::-webkit-scrollbar {
  display: none;
}

.project-chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 20px;
  background: var(--secondary-bg);
  color: var(--hint);
  font-size: 13px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
}

.project-chip:hover, .project-chip.active {
  background: var(--button);
  color: var(--button-text);
  border-color: var(--button);
}

/* ── Agent cards ────────────────────────────── */

.agents-list {
  padding: 0 16px 16px;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.agent-card {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
  border: 1px solid rgba(255,255,255,0.04);
}

.agent-card:active {
  transform: scale(0.98);
}

.agent-card .top-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.agent-card .agent-id {
  font-weight: 600;
  font-size: 14px;
}

.agent-card .agent-project {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(108,155,255,0.15);
  color: var(--link);
}

.agent-card .agent-time {
  font-size: 12px;
  color: var(--hint);
}

.agent-card .agent-task {
  font-size: 13px;
  color: var(--hint);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-running { background: #4caf50; animation: pulse 1.5s infinite; }
.status-completed { background: #4caf50; }
.status-failed { background: #f44336; }
.status-stopped { background: #ff9800; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ── Agent terminal view ────────────────────── */

.agent-view {
  display: none;
  flex-direction: column;
  height: 100vh;
}

.agent-view.active {
  display: flex;
}

.agent-view-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.btn-back {
  background: none;
  border: none;
  color: var(--link);
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
}

.agent-view-header .info {
  flex: 1;
}

.agent-view-header .info h2 {
  font-size: 15px;
  font-weight: 600;
}

.agent-view-header .info .meta {
  font-size: 12px;
  color: var(--hint);
}

/* ── Chat stream (ChatGPT / Claude style) ──── */

.terminal {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  font-size: 14px;
  line-height: 1.6;
  scroll-behavior: smooth;
}

/* ── Base event ────────────────────────────── */

.terminal .event {
  padding: 4px 20px;
  animation: fadeIn 0.15s ease;
  word-break: break-word;
}

/* ── User message ──────────────────────────── */

.terminal .event.ev-user_message {
  background: var(--secondary-bg);
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}

/* ── Assistant text ────────────────────────── */

.terminal .event.ev-text {
  padding: 16px 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ── Tool use / result ─────────────────────── */

.terminal .event.ev-tool_use {
  padding: 6px 20px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--hint);
}

.terminal .event.ev-tool_result {
  padding: 4px 20px 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--hint);
  border-bottom: 1px solid rgba(255,255,255,0.03);
}

/* ── Thinking ──────────────────────────────── */

.terminal .event.ev-thinking {
  padding: 6px 20px;
  font-size: 12px;
  color: var(--hint);
  opacity: 0.6;
}

/* ── Code blocks inside events ─────────────── */

.terminal .event code {
  display: block;
  white-space: pre-wrap;
  font-family: var(--font-mono);
  font-size: 12px;
  margin-top: 6px;
  padding: 8px 12px;
  background: rgba(0,0,0,0.3);
  border-radius: 6px;
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.06);
}

.terminal .event b {
  font-weight: 600;
}

.terminal .event i {
  color: var(--hint);
  font-style: italic;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.agent-actions {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid rgba(255,255,255,0.06);
  background: var(--bg);
}

.btn {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn:active { opacity: 0.7; }

.btn-stop {
  background: var(--destructive);
  color: white;
}

.btn-log {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.1);
}

/* ── New task form ──────────────────────────── */

.new-task-bar {
  padding: 12px 16px;
}

.new-task-btn {
  width: 100%;
  padding: 12px;
  border: 2px dashed rgba(255,255,255,0.12);
  border-radius: var(--radius);
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
}

.new-task-btn:hover {
  border-color: var(--button);
  color: var(--button);
}

.task-form {
  display: none;
  padding: 16px;
  background: var(--secondary-bg);
  border-radius: var(--radius);
  margin: 0 16px 16px;
}

.task-form.active {
  display: block;
}

.task-form select, .task-form textarea {
  width: 100%;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255,255,255,0.1);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  margin-bottom: 10px;
  font-family: inherit;
  resize: vertical;
}

.task-form textarea {
  min-height: 80px;
}

.task-form .form-actions {
  display: flex;
  gap: 8px;
}

.btn-submit {
  background: var(--button);
  color: var(--button-text);
}

.btn-cancel {
  background: transparent;
  color: var(--hint);
  border: 1px solid rgba(255,255,255,0.1);
}

/* ── Empty state ────────────────────────────── */

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--hint);
}

.empty-state .icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  font-size: 14px;
  line-height: 1.5;
}

/* ── Dashboard visible / agent view hidden ── */

.dashboard.hidden {
  display: none;
}

/* ── History section ───────────────────────── */

.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 16px 16px 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  cursor: pointer;
}

.history-item:active {
  background: rgba(255,255,255,0.03);
}

.history-item .hi-status {
  font-size: 14px;
  flex-shrink: 0;
}

.history-item .hi-body {
  flex: 1;
  min-width: 0;
}

.history-item .hi-project {
  font-size: 11px;
  color: var(--link);
}

.history-item .hi-task {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item .hi-meta {
  font-size: 11px;
  color: var(--hint);
  flex-shrink: 0;
  text-align: right;
}

/* ── Author footer ─────────────────────────── */

.footer-author {
  text-align: center;
  padding: 8px 0 12px;
  opacity: 0.3;
  transition: opacity 0.2s;
}

.footer-author:hover {
  opacity: 0.6;
}

.footer-author a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--hint);
  text-decoration: none;
}

.footer-author img {
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

/* ── Chat Input ────────────────────────────── */

.chat-input-container {
  display: none;
  padding: 12px 16px 16px;
  background: var(--bg);
  border-top: 1px solid rgba(255,255,255,0.06);
}

.chat-input-container.active {
  display: block;
}

.chat-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--secondary-bg);
  border-radius: 24px;
  padding: 10px 10px 10px 18px;
  border: 1px solid rgba(255,255,255,0.08);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chat-input-wrapper:focus-within {
  border-color: rgba(255,255,255,0.15);
  box-shadow: 0 0 0 2px rgba(108,155,255,0.1);
}

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 150px;
  min-height: 22px;
  line-height: 1.5;
}

#chat-input::placeholder {
  color: var(--hint);
}

.btn-send {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.btn-send:hover {
  opacity: 0.85;
}

.btn-send:active {
  transform: scale(0.93);
}

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