/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg:      #0d1117;
  --bg2:     #161b22;
  --bg3:     #1c2128;
  --bg4:     #21262d;
  --border:  #30363d;
  --accent:  #00bcd4;
  --accent2: #0097a7;
  --text:    #e6edf3;
  --muted:   #8b949e;
  --danger:  #f85149;
  --sidebar-w: 260px;
}
html, body { height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }

/* ── App Layout ── */
.app {
  display: flex;
  height: 100dvh;
  /* Full screen PWA: respect notch/Dynamic Island/home bar */
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.25s;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; color: var(--accent); }
.logo-img { width: 28px; height: 28px; border-radius: 6px; object-fit: cover; }
.icon-btn {
  background: none; border: none; font-size: 18px; cursor: pointer;
  padding: 6px 8px; border-radius: 8px; opacity: 0.7; transition: opacity 0.2s, background 0.2s;
}
.icon-btn:hover { opacity: 1; background: rgba(255,255,255,0.07); }

/* ── Agent Switcher ── */
.agent-switcher { padding: 8px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.agent-switcher select {
  width: 100%; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); padding: 7px 10px;
  font-size: 13px; outline: none; cursor: pointer;
}

/* ── Thread List ── */
.thread-list { flex: 1; overflow-y: auto; padding: 8px 0; }
.thread-list::-webkit-scrollbar { width: 4px; }
.thread-list::-webkit-scrollbar-track { background: transparent; }
.thread-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.thread-group-label {
  font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.8px;
  padding: 12px 16px 4px;
}
.thread-entry {
  display: flex; align-items: center;
  padding: 9px 12px 9px 16px;
  cursor: pointer; border-radius: 8px; margin: 1px 6px;
  transition: background 0.15s; position: relative; gap: 8px;
}
.thread-entry:hover { background: rgba(255,255,255,0.05); }
.thread-entry.active { background: rgba(0,188,212,0.12); }
.thread-entry.active .thread-entry-title { color: var(--accent); }
.thread-entry-title {
  font-size: 13px; color: var(--text); flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.thread-entry-del {
  background: none; border: none; color: var(--muted); font-size: 14px;
  cursor: pointer; padding: 2px 6px; border-radius: 4px;
  opacity: 0; transition: opacity 0.15s, color 0.15s; flex-shrink: 0;
}
.thread-entry:hover .thread-entry-del { opacity: 1; }
.thread-entry-del:hover { color: var(--danger); }

.loading-agents { padding: 16px; font-size: 13px; color: var(--muted); text-align: center; }

/* ── Sidebar Footer ── */
.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}
.footer-btn {
  width: 100%; background: none; border: none; color: var(--muted);
  font-size: 13px; padding: 8px 12px; border-radius: 8px;
  cursor: pointer; text-align: left; transition: background 0.15s, color 0.15s;
}
.footer-btn:hover { background: rgba(255,255,255,0.05); color: var(--text); }

/* ── Chat Panel ── */
.chat-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.back-btn {
  display: none; background: none; border: none; color: var(--text);
  font-size: 22px; cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.agent-avatar { font-size: 28px; flex-shrink: 0; }
.agent-info { flex: 1; min-width: 0; }
.agent-name { font-weight: 600; font-size: 15px; }
.agent-status { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Messages ── */
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.message { display: flex; flex-direction: column; max-width: 80%; }
.message.user { align-self: flex-end; align-items: flex-end; }
.message.agent { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px; border-radius: 18px;
  font-size: 15px; line-height: 1.5; word-break: break-word;
  white-space: pre-wrap;
}
.message.user .bubble  { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.message.agent .bubble { background: var(--bg3); color: var(--text); border-bottom-left-radius: 4px; }
.bubble img { max-width: 100%; border-radius: 10px; display: block; }

.timestamp { font-size: 11px; color: var(--muted); margin-top: 3px; padding: 0 4px; }

/* ── Typing ── */
.typing-indicator .bubble { display: flex; gap: 5px; align-items: center; padding: 12px 16px; }
.dot { width: 7px; height: 7px; background: var(--muted); border-radius: 50%; animation: bounce 1.2s infinite; }
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-6px); } }

/* ── Empty State ── */
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: var(--muted); text-align: center; }
.empty-icon { font-size: 48px; }
.empty-text { font-size: 16px; }
.new-thread-cta {
  background: var(--accent); border: none; border-radius: 10px;
  color: #fff; font-size: 14px; font-weight: 600;
  padding: 10px 22px; cursor: pointer; margin-top: 4px;
  transition: background 0.2s;
}
.new-thread-cta:hover { background: var(--accent2); }

/* ── Input Area ── */
.input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg2);
}
.attach-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  padding: 8px; border-radius: 8px; flex-shrink: 0; transition: color 0.2s;
}
.attach-btn:hover { color: var(--accent); }
.input-wrapper { flex: 1; min-width: 0; }
.image-preview { margin-bottom: 8px; position: relative; display: inline-block; }
.image-preview img { max-height: 120px; border-radius: 10px; display: block; }
.remove-img {
  position: absolute; top: -8px; right: -8px;
  background: var(--danger); border: none; color: #fff;
  width: 22px; height: 22px; border-radius: 50%; font-size: 12px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
textarea#messageInput {
  width: 100%; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 20px; color: var(--text); font-size: 15px;
  padding: 9px 16px; outline: none; resize: none; max-height: 120px;
  line-height: 1.4; font-family: inherit; transition: border-color 0.2s;
}
textarea#messageInput:focus { border-color: var(--accent); }
textarea#messageInput::placeholder { color: var(--muted); }
.send-btn {
  background: var(--accent); border: none; border-radius: 50%;
  width: 38px; height: 38px; flex-shrink: 0;
  color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.send-btn:hover { background: var(--accent2); }
.send-btn:disabled { background: var(--bg4); color: var(--muted); cursor: default; }

/* ── Modal ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal { background: var(--bg2); border: 1px solid var(--border); border-radius: 12px; width: 100%; max-width: 440px; max-height: 80vh; overflow-y: auto; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-title { font-weight: 700; font-size: 16px; }
.modal-close { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 16px 20px; }
.section-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 10px; }
.gateway-entry { display: flex; align-items: center; justify-content: space-between; background: var(--bg3); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; margin-bottom: 8px; }
.gw-name { font-weight: 600; font-size: 14px; }
.gw-url { font-size: 12px; color: var(--muted); margin-top: 2px; }
.gw-delete { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 4px 8px; border-radius: 4px; }
.gw-delete:hover { color: var(--danger); }
.modal-input { width: 100%; background: var(--bg3); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; color: var(--text); font-size: 14px; margin-bottom: 10px; outline: none; transition: border-color 0.2s; }
.modal-input:focus { border-color: var(--accent); }
.modal-input::placeholder { color: var(--muted); }
.modal-btn { width: 100%; background: var(--accent); border: none; border-radius: 8px; padding: 11px; color: #fff; font-size: 14px; font-weight: 600; cursor: pointer; transition: background 0.2s; }
.modal-btn:hover { background: var(--accent2); }
.rename-row { display: flex; gap: 8px; margin-bottom: 8px; }
.rename-row .modal-input { margin-bottom: 0; flex: 1; }
.modal-btn-sm { background: var(--accent); border: none; border-radius: 8px; padding: 10px 16px; color: #fff; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.modal-btn-sm:hover { background: var(--accent2); }

/* ── Mobile ── */
@media (max-width: 640px) {
  :root { --sidebar-w: 100vw; }
  .sidebar { position: fixed; inset: 0; z-index: 50; transform: translateX(0); }
  .sidebar.hidden { transform: translateX(-100%); }
  .back-btn { display: block; }
  .message { max-width: 92%; }
}

/* ── Swipe to delete ── */
.thread-entry {
  overflow: hidden;
  position: relative;
  touch-action: pan-y;
}
.thread-entry-inner {
  display: flex; align-items: center;
  padding: 9px 12px 9px 16px;
  gap: 8px;
  background: var(--bg2);
  transition: transform 0.2s ease;
  width: 100%;
}
.thread-entry-del-swipe {
  position: absolute; right: 0; top: 0; bottom: 0;
  width: 80px;
  background: var(--danger);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 13px; font-weight: 600;
  cursor: pointer; flex-shrink: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.thread-entry.swiped .thread-entry-inner { transform: translateX(-80px); }
.thread-entry.swiped .thread-entry-del-swipe { opacity: 1; pointer-events: auto; }

/* ── Search ── */
.search-bar {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-bar input {
  width: 100%;
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 7px 12px;
  font-size: 13px;
  outline: none;
  box-sizing: border-box;
}
.search-bar input:focus { border-color: var(--accent); }
.search-result .thread-entry-inner { background: transparent; }
.search-snippet {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-snippet mark {
  background: rgba(0,188,212,0.3);
  color: var(--accent);
  border-radius: 2px;
  padding: 0 2px;
}

/* ── Toggle switch ─────────────────────────────────────────────────────────── */
.cc-toggle { position:relative; display:inline-block; width:44px; height:24px; flex-shrink:0; }
.cc-toggle input { opacity:0; width:0; height:0; }
.cc-toggle-slider { position:absolute; cursor:pointer; inset:0; background:#334155; border-radius:24px; transition:.3s; }
.cc-toggle-slider:before { position:absolute; content:""; height:18px; width:18px; left:3px; bottom:3px; background:#fff; border-radius:50%; transition:.3s; }
.cc-toggle input:checked + .cc-toggle-slider { background:#6366f1; }
.cc-toggle input:checked + .cc-toggle-slider:before { transform:translateX(20px); }

.swipe-hint {
  font-size: 12px;
  font-weight: 700;
  color: #ef4444;
  margin-left: 8px;
  letter-spacing: 0.01em;
}
