:root {
  color-scheme: dark;
  --app-bg: #0f172a;
  --panel-bg: #182235;
  --panel-border: #273449;
  --msg-user: #1d4ed8;
  --msg-assistant: #263244;
  --msg-group: #182235;
  --text-soft: #94a3b8;
  --text-strong: #e5e7eb;
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--app-bg);
  color: var(--text-strong);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

#app, .app-root {
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.threads-pane {
  border-right: 1px solid var(--panel-border);
  background: var(--app-bg);
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
}

.chat-pane {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
  overflow: hidden;
  background: var(--app-bg);
}

.chat-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(17, 24, 39, 0.96);
  min-height: 61px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-tab {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text-strong);
  padding: 12px;
  margin-bottom: 4px;
}

.thread-tab.active {
  background: #1d4ed8;
  border-color: #2563eb;
}

.thread-tab .thread-title { font-weight: 700; }
.thread-tab .thread-mode { font-size: 12px; color: #cbd5e1; }

.messages-pane {
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message-row {
  display: flex;
  flex-direction: column;
  max-width: min(720px, 88%);
  position: relative;
  touch-action: pan-y;
  transition: transform 120ms ease;
  user-select: text;
}

.message-row.role-user { align-self: flex-end; }
.message-row.role-assistant, .message-row.role-group { align-self: flex-start; }

.message-row.is-editing .message-bubble {
  outline: 2px dashed #f59e0b;
}

.message-bubble {
  padding: 10px 12px;
  border-radius: 10px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  position: relative;
  transition: background 200ms ease;
}

.role-user .message-bubble { background: var(--msg-user); }
.role-assistant .message-bubble { background: var(--msg-assistant); }
.role-group .message-bubble { background: var(--msg-group); border: 1px solid var(--panel-border); }

.message-row.highlight .message-bubble {
  background: #f59e0b !important;
  color: #111;
}

.message-author {
  font-size: 12px;
  font-weight: 700;
  color: #bfdbfe;
  margin-bottom: 4px;
}

.message-deleted .message-bubble {
  font-style: italic;
  color: var(--text-soft);
  background: transparent !important;
  border: 1px dashed var(--panel-border) !important;
}

.message-edited-mark {
  font-size: 11px;
  color: var(--text-soft);
  margin-left: 6px;
}

.reply-quote {
  border-left: 3px solid #93c5fd;
  background: rgba(15, 23, 42, 0.55);
  padding: 6px 10px;
  border-radius: 6px;
  margin-bottom: 6px;
  cursor: pointer;
  font-size: 13px;
}

.reply-quote .reply-author {
  font-weight: 700;
  color: #93c5fd;
  font-size: 12px;
}

.reply-quote .reply-preview {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-soft);
}

.reactions-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.reaction-chip {
  border: 1px solid var(--panel-border);
  background: rgba(15, 23, 42, 0.6);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.reaction-chip.mine {
  border-color: #93c5fd;
  background: rgba(29, 78, 216, 0.35);
}

.swipe-reply-hint {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: 18px;
  color: #93c5fd;
  opacity: 0;
  transition: opacity 120ms ease;
  pointer-events: none;
}

.role-user .swipe-reply-hint { left: -28px; }
.role-assistant .swipe-reply-hint,
.role-group .swipe-reply-hint { left: -28px; }

.message-row.show-hint .swipe-reply-hint { opacity: 1; }

.attachments {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.attachment-card {
  display: grid;
  gap: 6px;
  border: 1px solid rgba(255,255,255,0.16);
  border-radius: 8px;
  padding: 8px;
}

.attachment-card img {
  max-width: min(360px, 100%);
  max-height: 320px;
  object-fit: contain;
  border-radius: 6px;
  background: #0f172a;
  cursor: zoom-in;
}

.attachment-card a {
  color: #bfdbfe;
  overflow-wrap: anywhere;
}

.chat-input-area {
  border-top: 1px solid var(--panel-border);
  background: var(--panel-bg);
  padding: 10px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
}

.chat-input-row {
  display: grid;
  grid-template-columns: 44px 1fr 52px;
  gap: 8px;
  align-items: end;
}

.chat-input-row textarea {
  resize: none;
  min-height: 44px;
  max-height: 140px;
  background: var(--app-bg);
  color: var(--text-strong);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 12px;
}

.send-btn {
  height: 44px;
  border-radius: 8px;
  background: #2563eb;
  border: 1px solid #2563eb;
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 20px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  border: 1px solid #475569;
  background: transparent;
  color: var(--text-strong);
  display: grid;
  place-items: center;
  font-size: 20px;
}

.icon-btn:disabled, .send-btn:disabled {
  opacity: 0.55;
  pointer-events: none;
}

.input-mode-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  margin-bottom: 6px;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--panel-border);
  font-size: 13px;
}

.input-mode-bar .mode-preview {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.input-mode-bar .mode-cancel {
  background: transparent;
  border: none;
  color: var(--text-soft);
  font-size: 18px;
}

.attachment-preview-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 96px));
  gap: 8px;
  margin-bottom: 8px;
}

.pending-thumb,
.pending-file {
  position: relative;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--app-bg);
  overflow: hidden;
  aspect-ratio: 1;
}

.pending-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.pending-file {
  display: grid;
  align-content: center;
  padding: 28px 6px 6px;
  font-size: 11px;
  color: #cbd5e1;
  text-align: center;
  overflow-wrap: anywhere;
  aspect-ratio: auto;
  min-height: 72px;
}

.pending-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.86);
  color: #fff;
  border: 1px solid rgba(15, 23, 42, 0.72);
  display: grid;
  place-items: center;
  font-size: 14px;
}

.thread-drawer,
.log-drawer {
  background: var(--app-bg);
}

.lightbox-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: grid;
  place-items: center;
  z-index: 1100;
}

.lightbox-backdrop img {
  max-width: 96vw;
  max-height: 92vh;
  object-fit: contain;
}

/* --- Logs drawer (preserves legacy look) --- */

.log-drawer .status-line { display: flex; align-items: center; gap: 8px; }
.log-drawer .status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: #64748b;
  flex: 0 0 auto;
}
.log-drawer .status-dot.running { background: #22c55e; box-shadow: 0 0 0 5px rgba(34,197,94,0.12); }
.log-drawer .status-dot.stale { background: #f59e0b; box-shadow: 0 0 0 5px rgba(245,158,11,0.12); }
.log-drawer .status-dot.failed { background: #ef4444; box-shadow: 0 0 0 5px rgba(239,68,68,0.12); }

.log-item {
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--panel-bg);
  padding: 10px;
  display: grid;
  gap: 6px;
}
.log-item.warn { border-color: rgba(245,158,11,0.55); }
.log-item.error { border-color: rgba(239,68,68,0.72); color: #fee2e2; }
.log-item .log-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.log-item .log-stage { font-size: 12px; color: #bfdbfe; font-weight: 800; text-transform: uppercase; }
.log-item .log-time { color: var(--text-soft); font-size: 12px; white-space: nowrap; }
.log-item .log-summary { overflow-wrap: anywhere; line-height: 1.4; }

@media (max-width: 720px) {
  .chat-layout { grid-template-columns: 1fr; }
  .threads-pane { display: none; }
  .chat-pane { grid-template-rows: auto 1fr auto; }
  .message-row { max-width: 96%; }
  .chat-input-row { grid-template-columns: 44px 1fr 52px; }
}
