/* Agent Chat View — Parley Inbox & Halden Aesthetic */

.chat-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-rows: 100%;
  height: calc(100vh - var(--topbar-height) - 48px);
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  min-width: 0;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

/* Chat Sidebar (Sessions Drawer) */
.chat-sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  border-right: 1px solid var(--hair);
  background: var(--panel);
}

.chat-sidebar-header {
  padding: 16px 18px;
  border-bottom: 1px solid var(--hair);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.sidebar-title {
  font-size: 15px;
  font-weight: 800;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.session-search-box {
  padding: 10px 14px;
  border-bottom: 1px solid var(--hair);
}
.session-search-input {
  width: 100%;
  height: 34px;
  padding: 0 12px;
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--radius-pill);
  font-size: 13px;
  outline: none;
  color: var(--ink);
}
.session-search-input:focus {
  border-color: var(--accent);
}

.session-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  padding: 8px;
}

.session-item {
  padding: 12px 14px;
  border-radius: var(--radius-md);
  cursor: pointer;
  margin-bottom: 4px;
  transition: all 0.15s ease;
  border: 1px solid transparent;
}
.session-item:hover {
  background: var(--card);
}
.session-item.is-active {
  background: var(--card);
  border-color: var(--hair-strong);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04);
}
.session-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.session-item-title {
  font-size: 13.5px;
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}
.session-item-time {
  font-size: 11px;
  color: var(--muted);
  font-family: var(--font-mono);
}
.session-item-preview {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Chat Main Pane */
.chat-main-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  background: var(--card);
  overflow: hidden;
  position: relative;
}

/* Chat Header */
.chat-pane-header {
  padding: 14px 22px;
  border-bottom: 1px solid var(--hair);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card);
  z-index: 10;
}
.chat-header-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-current-title {
  font-size: 16px;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.chat-current-id {
  font-size: 11.5px;
  font-family: var(--font-mono);
  color: var(--muted);
}

.chat-header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* Agent Working Badge (Active Thinking / Running state) */
.agent-working-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: rgba(56, 189, 248, 0.12);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: var(--radius-pill);
  font-size: 11.5px;
  font-weight: 700;
  color: #38bdf8;
  user-select: none;
}
.working-pulse-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #38bdf8;
  box-shadow: 0 0 8px #38bdf8;
  animation: pulse-dot 1.2s infinite ease-in-out;
}
.working-badge-text {
  letter-spacing: 0.02em;
}

/* Messages Stream */
.messages-stream {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}

.message-row {
  display: flex;
  gap: 14px;
  max-width: 82%;
  min-width: 0;
  box-sizing: border-box;
  animation: msgFadeIn 0.2s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
@keyframes msgFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message-row.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.message-row.model {
  align-self: flex-start;
}

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
}
.message-row.user .msg-avatar {
  background: var(--accent);
  color: #fff;
}
.message-row.model .msg-avatar {
  background: var(--panel);
  border: 1px solid var(--hair);
  color: var(--accent);
}

.msg-bubble {
  padding: 14px 18px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  line-height: 1.6;
  position: relative;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
  box-sizing: border-box;
}
.message-row.user .msg-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.message-row.model .msg-bubble {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--hair);
  border-bottom-left-radius: 4px;
}

.msg-content {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.msg-bubble pre {
  background: #0f1516;
  color: #e5ecec;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12.5px;
  margin: 10px 0;
  border: 1px solid #253133;
  max-width: 100%;
  box-sizing: border-box;
  -webkit-overflow-scrolling: touch;
  white-space: pre;
}
.msg-bubble pre code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 0;
  background: transparent;
  border-radius: 0;
  white-space: pre;
  word-break: normal;
}
.msg-bubble code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 2px 6px;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 4px;
  word-break: break-word;
}
:root[data-theme="dark"] .msg-bubble code {
  background: rgba(255, 255, 255, 0.1);
}
.message-row.user .msg-bubble code {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.msg-time {
  font-size: 11px;
  margin-top: 5px;
  opacity: 0.7;
  font-family: var(--font-mono);
}

/* Attached Media in Messages */
.msg-attachments-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.msg-attached-img-card {
  display: block;
  max-width: 240px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--hair-strong);
  transition: transform 0.15s ease;
}
.msg-attached-img-card:hover {
  transform: scale(1.02);
}
.msg-attached-img {
  width: 100%;
  height: auto;
  max-height: 220px;
  object-fit: cover;
  display: block;
}
.msg-attached-file-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.06);
  border: 1px solid var(--hair);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-family: var(--font-mono);
  color: inherit;
  text-decoration: none;
  max-width: 100%;
  word-break: break-all;
}
.msg-attached-file-pill svg {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}
.message-row.user .msg-attached-file-pill {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.25);
  color: #fff;
}

/* Input Area & Attachment Preview Tray */
.chat-input-container {
  padding: 12px 18px 16px 18px;
  border-top: 1px solid var(--hair);
  background: var(--card);
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
}

.attachment-preview-tray {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 0 4px 10px 4px;
  align-items: center;
  -webkit-overflow-scrolling: touch;
}
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--hair-strong);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--ink);
  flex-shrink: 0;
  max-width: 220px;
}
.tray-thumb {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}
.tray-file-svg {
  width: 16px;
  height: 16px;
  color: var(--accent);
  flex-shrink: 0;
}
.attachment-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 11.5px;
  font-family: var(--font-mono);
}
.attachment-del-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
  line-height: 1;
}
.attachment-del-btn:hover {
  color: var(--danger);
}

.chat-input-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: var(--radius-lg);
  padding: 6px 10px;
  transition: border-color 0.2s, box-shadow 0.2s;
  min-width: 0;
}
.chat-input-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.chat-attach-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s ease;
}
.chat-attach-btn:hover {
  background: var(--card);
  color: var(--accent);
}
.chat-attach-btn svg {
  width: 19px;
  height: 19px;
}

.chat-textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  max-height: 140px;
  min-height: 38px;
  padding: 8px 4px;
  font-size: 14.5px;
  line-height: 1.45;
  color: var(--ink);
  box-sizing: border-box;
}

.chat-send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.15s, background 0.15s;
  box-shadow: 0 2px 8px var(--accent-glow);
}
.chat-send-btn:hover {
  background: var(--accent-hover);
  transform: scale(1.05);
}
.chat-send-btn:active {
  transform: scale(0.92);
}
.chat-send-btn svg {
  width: 18px;
  height: 18px;
}

/* Typing Indicator & Working State */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: dotPulse 1.4s infinite ease-in-out both;
}
.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }
@keyframes dotPulse {
  0%, 80%, 100% { transform: scale(0.4); opacity: 0.3; }
  40% { transform: scale(1.1); opacity: 1; }
}

.status-dot.working {
  background: #38bdf8 !important;
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.4) !important;
  animation: pulse-dot 1s infinite ease-in-out !important;
}

/* Mobile Responsive Chat Rules */
.mobile-drawer-btn {
  display: none;
}
.chat-sidebar-backdrop {
  display: none;
}

@media (min-width: 901px) {
  .chat-layout {
    display: grid !important;
    grid-template-columns: 310px 1fr !important;
    height: calc(100vh - var(--topbar-height) - 48px) !important;
  }
  .chat-sidebar {
    display: flex !important;
    position: static !important;
    width: 310px !important;
    max-width: none !important;
    transform: none !important;
    box-shadow: none !important;
  }
  .chat-main-pane {
    display: flex !important;
    min-width: 0 !important;
  }
}

@media (max-width: 900px) {
  .chat-layout {
    grid-template-columns: 1fr !important;
    flex: 1 !important;
    min-height: 0 !important;
    height: 100% !important;
    max-height: none !important;
    display: flex !important;
    flex-direction: column !important;
    border-radius: 0 !important;
    border: none !important;
    min-width: 0 !important;
    width: 100% !important;
    overflow: hidden !important;
    box-shadow: none !important;
  }
  .chat-sidebar {
    display: flex !important;
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    width: 290px !important;
    max-width: 85vw !important;
    z-index: 2200 !important;
    background: var(--card) !important;
    box-shadow: 4px 0 30px rgba(0, 0, 0, 0.5) !important;
    transform: translateX(-100%) !important;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }
  .chat-sidebar.is-open-mobile {
    transform: translateX(0) !important;
  }
  .chat-sidebar-backdrop {
    display: none;
    position: fixed !important;
    inset: 0 !important;
    background: rgba(0, 0, 0, 0.65) !important;
    backdrop-filter: blur(4px) !important;
    -webkit-backdrop-filter: blur(4px) !important;
    z-index: 2150 !important;
  }
  .chat-sidebar-backdrop.is-open-mobile {
    display: block !important;
  }
  .mobile-drawer-btn {
    display: inline-flex !important;
  }
  .chat-main-pane {
    flex: 1 !important;
    min-height: 0 !important;
    height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    min-width: 0 !important;
    width: 100% !important;
  }
  .chat-pane-header {
    flex-shrink: 0 !important;
    padding: 6px 10px !important;
    min-height: 44px !important;
    max-height: 48px !important;
    min-width: 0 !important;
    width: 100% !important;
    overflow: hidden !important;
    gap: 6px !important;
  }
  .chat-title-row {
    min-width: 0 !important;
    overflow: hidden !important;
    gap: 6px !important;
  }
  .chat-current-title {
    font-size: 13.5px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    max-width: 130px !important;
  }
  .chat-current-id {
    display: none !important;
  }
  .chat-header-controls {
    gap: 6px !important;
    min-width: 0 !important;
    flex-shrink: 0 !important;
  }
  .chat-header-controls .token-meter-pill {
    display: none !important; /* Hide duplicate token meter on mobile */
  }
  .chat-title-row #chat-status-pill {
    display: none !important; /* Hide duplicate status pill on mobile (topbar has it) */
  }
  #chat-engine-controls .btn-engine-launch span {
    display: none !important;
  }
  #chat-engine-controls .btn-engine-launch {
    padding: 0 8px !important;
    width: 34px !important;
    height: 34px !important;
    justify-content: center !important;
  }
  .agent-working-badge {
    padding: 3px 8px !important;
    font-size: 11px !important;
  }
  .agent-working-badge .working-badge-text {
    font-size: 11px !important;
  }
  .messages-stream {
    flex: 1 !important;
    min-height: 0 !important;
    padding: 10px 8px !important;
    gap: 10px !important;
    min-width: 0 !important;
    width: 100% !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior-y: contain !important;
    -webkit-overflow-scrolling: touch !important;
    touch-action: pan-y !important;
  }
  .message-row {
    max-width: 95% !important;
    gap: 8px !important;
    min-width: 0 !important;
  }
  .msg-avatar {
    width: 28px !important;
    height: 28px !important;
    font-size: 11px !important;
  }
  .msg-bubble {
    padding: 10px 12px !important;
    font-size: 14px !important;
    line-height: 1.5 !important;
    min-width: 0 !important;
    max-width: 100% !important;
    overflow-wrap: anywhere !important;
  }
  .chat-input-container {
    flex-shrink: 0 !important;
    padding: 8px 10px !important;
    background: var(--card) !important;
    border-top: 1px solid var(--hair) !important;
    min-width: 0 !important;
    width: 100% !important;
    position: relative !important;
    z-index: 50 !important;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.08) !important;
  }
  .chat-input-box {
    padding: 6px 8px !important;
    gap: 8px !important;
    background: var(--panel) !important;
    border: 1.5px solid var(--hair-strong) !important;
    border-radius: var(--radius-lg) !important;
  }
  .chat-attach-btn {
    width: 38px !important;
    height: 38px !important;
  }
  .chat-textarea {
    font-size: 16px !important; /* iOS auto-zoom prevention */
    min-height: 38px !important;
    padding: 8px 4px !important;
  }
  .chat-send-btn {
    width: 40px !important;
    height: 40px !important;
  }
}

/* Remote Permission Approver Card */
#permission-card-container {
  padding: 0 16px 10px 16px;
  position: relative;
  z-index: 10;
}

.permission-card {
  position: relative;
  background: var(--panel);
  border: 1px solid rgba(245, 158, 11, 0.4);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  box-shadow: 0 8px 30px rgba(245, 158, 11, 0.12);
  overflow: hidden;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.perm-card-glow {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, #f59e0b, #ef4444, #10b981);
}

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

.perm-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: 700;
  color: #f59e0b;
}

.perm-pulse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
  box-shadow: 0 0 10px #f59e0b;
  animation: pulse-dot 1.4s infinite ease-in-out;
}

.perm-time {
  font-size: 11px;
  color: var(--muted);
  font-family: var(--font-mono);
}

.perm-cmd-box {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--hair);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-bottom: 12px;
  font-size: 12.5px;
  color: var(--ink);
  font-family: var(--font-mono);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.perm-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.perm-hint {
  font-size: 11.5px;
  color: var(--muted);
}

.perm-buttons {
  display: flex;
  gap: 8px;
}

.btn-perm-allow {
  background: #10b981 !important;
  color: #fff !important;
  font-weight: 700 !important;
}

.btn-perm-allow:hover {
  background: #059669 !important;
}

.btn-perm-deny {
  background: rgba(239, 68, 68, 0.15) !important;
  color: #ef4444 !important;
  border: 1px solid rgba(239, 68, 68, 0.3) !important;
}

.btn-perm-deny:hover {
  background: rgba(239, 68, 68, 0.25) !important;
}

@keyframes pulse-dot {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.4); opacity: 0.5; }
}


