/* ═══════════════════════════════════════════════════════════
   AI ORCHESTRATOR — Chat + Task Manager UI
   ═══════════════════════════════════════════════════════════ */

/* Layout: chat left + task panel right */
.orch-layout {
  display: flex;
  height: 100%;
  overflow: hidden;
}

.orch-chat-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-right: 1px solid var(--border);
}

.orch-tasks-section {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
}

@media (max-width: 900px) {
  .orch-layout {
    flex-direction: column;
  }

  .orch-tasks-section {
    width: 100%;
    height: 200px;
    border-top: 1px solid var(--border);
    border-right: none;
  }

  .orch-chat-section {
    border-right: none;
  }
}

/* Messages area */
.orch-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Welcome screen */
.orch-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 48px 24px;
  gap: 12px;
  flex: 1;
}

.orch-welcome-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 60%, purple));
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  margin-bottom: 8px;
}

.orch-welcome h3 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
}

.orch-welcome p {
  max-width: 500px;
  line-height: 1.5;
}

.orch-quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}

.orch-quick-btn {
  font-size: .8rem !important;
  gap: 4px !important;
}

/* Message bubbles */
.orch-msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  max-width: 85%;
  animation: orchMsgIn .25s ease;
}

@keyframes orchMsgIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.orch-msg-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.orch-msg-assistant {
  align-self: flex-start;
}

.orch-msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 14px;
}

.orch-msg-user .orch-msg-avatar {
  background: var(--accent);
  color: #fff;
}

.orch-msg-assistant .orch-msg-avatar {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.orch-msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.55;
  font-size: .9rem;
  word-break: break-word;
}

.orch-msg-user .orch-msg-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.orch-msg-assistant .orch-msg-bubble {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* System note (inline) */
.orch-system-note {
  text-align: center;
  font-size: .78rem;
  color: var(--text-secondary);
  padding: 4px 12px;
  background: color-mix(in srgb, var(--accent) 6%, transparent);
  border-radius: 8px;
  align-self: center;
  max-width: 90%;
  transition: background .15s;
}

.orch-system-note[style*="cursor"] {
  border: 1px solid color-mix(in srgb, var(--accent) 15%, transparent);
}

.orch-system-note[style*="cursor"]:hover {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
}

.orch-nav-link {
  color: var(--accent);
  font-weight: 500;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Thinking animation */
.orch-thinking .orch-msg-bubble {
  display: flex;
  align-items: center;
  gap: 8px;
}

.orch-thinking-dots {
  display: flex;
  gap: 4px;
  align-items: center;
}

.orch-thinking-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: orchDot 1.4s infinite ease-in-out;
}

.orch-thinking-dots span:nth-child(2) {
  animation-delay: .2s;
}

.orch-thinking-dots span:nth-child(3) {
  animation-delay: .4s;
}

@keyframes orchDot {

  0%,
  80%,
  100% {
    transform: scale(.5);
    opacity: .4;
  }

  40% {
    transform: scale(1);
    opacity: 1;
  }
}

.orch-thinking-text {
  font-size: .82rem;
  color: var(--text-secondary);
  font-style: italic;
}

/* Input area */
.orch-input-area {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-primary);
  align-items: flex-end;
}

.orch-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: .9rem;
  font-family: inherit;
  background: var(--bg-secondary);
  color: var(--text-primary);
  outline: none;
  transition: border-color .2s;
  min-height: 42px;
  max-height: 140px;
}

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

.orch-send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50% !important;
  padding: 0 !important;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* ── Task Manager Panel ── */
.orch-tasks-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.orch-tasks-header h3 {
  font-size: .9rem;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
}

.orch-tasks-counter {
  font-size: .78rem;
  color: var(--text-secondary);
  background: var(--bg-primary);
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
}

.orch-tasks-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.orch-tasks-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 48px 16px;
  text-align: center;
}

.orch-task-item {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 4px;
  transition: background .2s;
}

.orch-task-item:hover {
  background: color-mix(in srgb, var(--text-primary) 4%, transparent);
}

.orch-task-status {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 1px;
}

.orch-task-body {
  flex: 1;
  min-width: 0;
}

.orch-task-desc {
  font-size: .85rem;
  line-height: 1.4;
}

.orch-task-result {
  margin-top: 2px;
}

.orch-task-error {
  margin-top: 4px;
  font-size: .78rem;
  color: var(--danger);
}

/* Running task highlight */
.orch-task-running {
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}

.orch-task-running .orch-task-desc {
  font-weight: 500;
}

/* Done task */
.orch-task-done .orch-task-desc {
  color: var(--text-secondary);
}

/* Error task */
.orch-task-error-state .orch-task-desc {
  color: var(--danger);
}

/* Spinner */
.orch-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: orchSpin .6s linear infinite;
}

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

/* Checkmark & cross inline */
.orch-check {
  color: var(--success, #22c55e);
}

.orch-cross {
  color: var(--danger, #ef4444);
}

/* ── Thought Group (collapsible AI thinking) ── */
.orch-thought-group {
  align-self: center;
  max-width: 85%;
  border-radius: 10px;
  background: color-mix(in srgb, var(--accent) 5%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 12%, transparent);
  overflow: hidden;
  font-size: .82rem;
  margin: 6px 0;
}

.orch-thought-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: background .15s;
  user-select: none;
}

.orch-thought-toggle:hover {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.orch-thought-chevron {
  margin-left: auto;
  transition: transform .2s;
}

.orch-thought-group.open .orch-thought-chevron {
  transform: rotate(90deg);
}

.orch-thought-items {
  display: none;
  padding: 0 12px 8px;
  color: var(--text-muted);
  font-size: .78rem;
}

.orch-thought-group.open .orch-thought-items {
  display: block;
}

.orch-thought-item {
  padding: 2px 0 2px 16px;
  position: relative;
}

.orch-thought-item::before {
  content: '\00b7';
  position: absolute;
  left: 4px;
  color: var(--text-muted);
  font-weight: bold;
}

/* ── Message Actions (edit, delete, copy) ── */
.orch-msg-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity .15s;
  align-self: flex-start;
  margin-top: 4px;
  flex-shrink: 0;
}

.orch-msg:hover .orch-msg-actions {
  opacity: 1;
}

.orch-msg-action-btn {
  width: 26px;
  height: 26px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background .15s, color .15s;
  padding: 0;
}

.orch-msg-action-btn:hover {
  background: color-mix(in srgb, var(--text-primary) 8%, transparent);
  color: var(--text-primary);
}

/* ── Edit Mode ── */
.orch-edit-area {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: .88rem;
  font-family: inherit;
  background: var(--bg-primary);
  color: var(--text-primary);
  outline: none;
  min-height: 60px;
  box-sizing: border-box;
}

.orch-edit-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  justify-content: flex-end;
}

/* ── Markdown in bubbles ── */
.orch-msg-bubble h1,
.orch-msg-bubble h2,
.orch-msg-bubble h3 {
  margin: .6em 0 .3em;
  font-size: 1em;
  font-weight: 600;
}

.orch-msg-bubble h1 {
  font-size: 1.1em;
}

.orch-msg-bubble h2 {
  font-size: 1.05em;
}

.orch-msg-bubble ul,
.orch-msg-bubble ol {
  margin: .3em 0;
  padding-left: 1.4em;
}

.orch-msg-bubble li {
  margin: .15em 0;
}

.orch-msg-bubble p {
  margin: .4em 0;
}

.orch-msg-bubble p:first-child {
  margin-top: 0;
}

.orch-msg-bubble p:last-child {
  margin-bottom: 0;
}

.orch-msg-bubble code {
  background: color-mix(in srgb, var(--text-primary) 6%, transparent);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: .85em;
}

.orch-msg-bubble pre {
  background: color-mix(in srgb, var(--text-primary) 6%, transparent);
  padding: 8px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .82em;
  margin: .4em 0;
}

.orch-msg-bubble pre code {
  background: none;
  padding: 0;
}

/* ═══════════════════════════════════════════════════════════
   MOBILE OVERRIDES (max-width: 768px)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* Layout: full height minus topbar + bottom nav */
  .orch-layout {
    flex-direction: column;
    height: calc(100vh - 120px);
    height: calc(100dvh - 120px);
  }

  .orch-chat-section {
    flex: 1;
    min-height: 0;
    border-right: none;
  }

  /* ── Tasks section: collapsible on mobile ── */
  .orch-tasks-section {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: 44px;
    overflow: hidden;
    transition: max-height .3s ease;
    flex-shrink: 0;
  }

  .orch-tasks-section.orch-tasks-open {
    max-height: 220px;
    overflow-y: auto;
  }

  .orch-tasks-header {
    cursor: pointer;
    padding: 10px 14px;
    -webkit-tap-highlight-color: transparent;
    position: relative;
  }

  .orch-tasks-header::after {
    content: '';
    width: 18px;
    height: 18px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-size: contain;
    transition: transform .3s;
    flex-shrink: 0;
  }

  .orch-tasks-open .orch-tasks-header::after {
    transform: rotate(180deg);
  }

  /* ── Messages area ── */
  .orch-messages {
    padding: 10px 10px;
    gap: 10px;
  }

  /* ── Welcome screen compact ── */
  .orch-welcome {
    padding: 24px 16px;
    gap: 8px;
  }

  .orch-welcome-icon {
    width: 56px;
    height: 56px;
  }

  .orch-welcome-icon i,
  .orch-welcome-icon svg {
    width: 32px !important;
    height: 32px !important;
  }

  .orch-welcome h3 {
    font-size: 1.1rem;
  }

  .orch-welcome p {
    font-size: .85rem;
  }

  /* Quick actions: 2 columns */
  .orch-quick-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    width: 100%;
  }

  .orch-quick-btn {
    font-size: .75rem !important;
    padding: 8px 10px !important;
    justify-content: center;
    text-align: center;
    white-space: normal;
    line-height: 1.3;
  }

  /* ── Message bubbles wider ── */
  .orch-msg {
    max-width: 95%;
    gap: 8px;
  }

  .orch-msg-avatar {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }

  .orch-msg-avatar i,
  .orch-msg-avatar svg {
    width: 14px !important;
    height: 14px !important;
  }

  .orch-msg-bubble {
    padding: 8px 12px;
    font-size: .85rem;
    border-radius: 12px;
  }

  /* Message actions always visible on touch */
  .orch-msg .orch-msg-actions {
    opacity: 1;
  }

  .orch-msg-action-btn {
    width: 30px;
    height: 30px;
  }

  /* ── Input area ── */
  .orch-input-area {
    padding: 8px 10px;
    gap: 6px;
  }

  .orch-input {
    padding: 8px 12px;
    font-size: .85rem;
    border-radius: 10px;
    min-height: 38px;
    max-height: 100px;
  }

  .orch-send-btn {
    width: 38px;
    height: 38px;
  }

  /* ── System note ── */
  .orch-system-note {
    font-size: .75rem;
    padding: 4px 10px;
  }

  /* ── Thought group ── */
  .orch-thought-group {
    max-width: 95%;
    font-size: .78rem;
  }

  /* ── Task items tighter ── */
  .orch-task-item {
    padding: 8px 10px;
    gap: 8px;
  }

  .orch-task-desc {
    font-size: .82rem;
  }

  .orch-tasks-empty {
    padding: 24px 12px;
  }

  /* ── WIP banner ── */
  .wip-banner {
    margin: 8px 10px !important;
    padding: 10px 12px !important;
    font-size: .8rem !important;
  }

  /* ── Edit mode ── */
  .orch-edit-area {
    font-size: .84rem;
    padding: 6px 8px;
    min-height: 50px;
  }
}

/* ── Extra small screens ── */
@media (max-width: 400px) {
  .orch-quick-actions {
    grid-template-columns: 1fr;
  }

  .orch-msg {
    max-width: 98%;
  }

  .orch-welcome {
    padding: 16px 12px;
  }
}
