/* ─── AK Chat Platform — Global Styles ─────────────────────────────────────── */

/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
  --primary:       #555555;
  --primary-dark:  #444444;
  --primary-glow:  rgba(85, 85, 85, 0.25);
  --secondary:     #1a1a1a;
  --tertiary:      #333333;
  --text:          #ffffff;
  --text-muted:    #888888;
  --text-dim:      #555555;
  --bg-dark:       #0d0d0d;
  --bg-card:       #1a1a1a;
  --border:        #333333;
  --border-light:  #444444;
  --danger:        #e53e3e;
  --success:       #38a169;

  --sidebar-width: 270px;
  --header-height: 60px;
  --radius:        10px;
  --radius-sm:     6px;
  --transition:    0.2s ease;

  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

/* ── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: 14px;
  background: var(--bg-dark);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  font-family: var(--font);
  cursor: pointer;
  border: none;
  outline: none;
  background: none;
  color: inherit;
}

input, textarea, select {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

/* ── App Layout ─────────────────────────────────────────────────────────────── */
#app {
  display: flex;
  height: 100vh;
  height: 100dvh; /* iOS Safari: adjusts dynamically for browser chrome */
  overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform var(--transition), width var(--transition);
  z-index: 100;
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  min-height: var(--header-height);
}

.sidebar-user-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

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

/* Home agent item styling */
#home-agent-item {
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  flex-shrink: 0;
}

.user-details { flex: 1; min-width: 0; }
.user-name    { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role    { font-size: 11px; color: var(--text-muted); text-transform: capitalize; }

.btn-icon {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.btn-icon:hover { background: var(--tertiary); color: var(--text); }
.btn-icon.danger:hover { background: rgba(229,62,62,0.15); color: var(--danger); }

/* Sidebar scroll area */
#sidebar-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

#sidebar-body::-webkit-scrollbar { width: 4px; }
#sidebar-body::-webkit-scrollbar-track { background: transparent; }
#sidebar-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Agent list */
.sidebar-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--text-dim);
  padding: 14px 16px 6px;
}

.agent-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  cursor: pointer;
  border-radius: 0;
  transition: background var(--transition);
  position: relative;
}

.agent-item:hover    { background: var(--tertiary); }
.agent-item.active   { background: rgba(255, 107, 0, 0.12); }
.agent-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--primary);
  border-radius: 0 2px 2px 0;
}

.agent-icon {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--primary);
  flex-shrink: 0;
  overflow: hidden;
}

.agent-icon img          { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; position: absolute; top: 0; left: 0; }
.agent-icon img[hidden]  { display: none; }  /* iOS Safari needs explicit rule */
.agent-icon              { position: relative; }
.agent-name              { font-size: 13px; font-weight: 500; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Sidebar search */
#sidebar-search-wrap {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

#sidebar-search {
  width: 100%;
  padding: 7px 10px 7px 32px;
  background: var(--bg-dark);
  border-color: var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
}

.search-wrap { position: relative; }
.search-icon {
  position: absolute;
  left: 9px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

/* Message search results */
#search-results-box {
  border-bottom: 1px solid var(--border);
  max-height: 260px;
  overflow-y: auto;
}
.search-results-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-dim);
  padding: 6px 14px 4px;
}
.search-result-conv {
  padding: 7px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}
.search-result-conv:last-child { border-bottom: none; }
.search-result-conv:hover { background: var(--tertiary); }
.search-result-title { font-size: 12px; font-weight: 600; color: var(--text); margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-result-snippet { font-size: 11px; color: var(--text-muted); line-height: 1.4; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

/* Action buttons in sidebar */
.sidebar-actions {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.btn-sidebar-action {
  flex: 1;
  padding: 7px 10px;
  background: var(--tertiary);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-align: center;
  transition: background var(--transition), color var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.btn-sidebar-action:hover { background: var(--border-light); color: var(--text); }
.btn-sidebar-action.primary { background: var(--primary); color: #fff; }
.btn-sidebar-action.primary:hover { background: var(--primary-dark); }

/* Project / folder / conversation items */
.project-item { border-bottom: 1px solid var(--border); }

/* Project icon badge — no background, just colored SVG */
.proj-icon-badge {
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Project picker modal */
.proj-picker-preview {
  display: flex;
  justify-content: center;
  padding: 12px 0 8px;
}
#proj-preview-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s;
}
.proj-icon-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  max-height: 220px;
  overflow-y: auto;
  margin-top: 6px;
  padding: 2px;
}
.proj-icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 2px solid transparent;
  background: var(--tertiary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, background 0.15s;
  padding: 0;
}
.proj-icon-btn:hover    { background: var(--border); }
.proj-icon-btn.selected { border-color: currentColor; background: var(--border); }
.proj-color-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}
.proj-color-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.1s;
}
.proj-color-btn:hover    { transform: scale(1.15); }
.proj-color-btn.selected { border-color: var(--text); transform: scale(1.2); }

.project-header {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 6px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background var(--transition);
  position: relative;
}
.project-header:hover { background: var(--tertiary); }
.project-header .chevron { transition: transform var(--transition); color: var(--text-muted); flex-shrink: 0; }
.project-header.open .chevron { transform: rotate(90deg); }
.project-name { font-size: 12px; font-weight: 600; color: var(--text-muted); flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-pin  { color: var(--primary); }

.project-body { display: none; }
.project-body.open { display: block; }

.folder-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 24px;
  cursor: pointer;
  transition: background var(--transition);
}
.folder-header:hover { background: var(--tertiary); }
.folder-name { font-size: 12px; color: var(--text-muted); flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.folder-body { display: none; padding-left: 12px; }
.folder-body.open { display: block; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 6px 28px;
  cursor: pointer;
  transition: background var(--transition);
  border-radius: 0;
  position: relative;
}
.conv-item:hover   { background: var(--tertiary); }
.conv-item.active  { background: rgba(255, 107, 0, 0.1); }
.conv-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2px;
  background: var(--primary);
}
.conv-title    { font-size: 12px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); }
.conv-pin-icon { color: var(--primary); opacity: 0.7; flex-shrink: 0; }

/* Pin dot inline in conv-title */
.conv-pin-dot {
  display: inline-block;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--primary);
  margin-right: 5px;
  vertical-align: middle;
  flex-shrink: 0;
}

/* Indented conv items inside folders */
.conv-item.conv-item-folder { padding-left: 40px; }

/* Action buttons hidden until hover */
.conv-item-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--transition);
  flex-shrink: 0;
}
.conv-item:hover .conv-item-actions { opacity: 1; }

/* Project action buttons — inline on same row, visible on hover */
.project-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--transition);
  flex-shrink: 0;
  margin-left: auto;
}
.project-header:hover .project-actions { opacity: 1; }

/* Iconify badge inside project header — fixed size so it doesn't break flex row */
.proj-icon-badge {
  display: inline-flex !important;
  flex-shrink: 0;
  width: 15px !important;
  height: 15px !important;
}

/* Small count badge on projects */
.proj-count {
  font-size: 10px;
  background: var(--tertiary);
  color: var(--text-muted);
  border-radius: 10px;
  padding: 1px 6px;
  flex-shrink: 0;
}

/* Tiny icon buttons (xs) */
.btn-icon-xs {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px; height: 20px;
  border-radius: 4px;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.btn-icon-xs:hover       { background: var(--border-light); color: var(--text); }
.btn-icon-xs.danger:hover { background: rgba(220,53,69,0.15); color: #dc3545; }

/* Empty note text in sidebar */
.sidebar-empty-note {
  font-size: 11px;
  color: var(--text-muted);
  padding: 6px 16px;
  margin: 0;
}

/* ── Main content ───────────────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-dark);
  min-width: 0;
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
#chat-header {
  height: var(--header-height);
  background: var(--secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
}

#hamburger {
  display: none;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
#hamburger:hover { background: var(--tertiary); color: var(--text); }

.header-agent-icon {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--primary);
  flex-shrink: 0;
  overflow: hidden;
}
.header-agent-icon img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; }

.header-agent-info { flex: 1; min-width: 0; }
.header-agent-name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-conv-title { font-size: 11px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.header-conv-title:hover { color: var(--text); }
.header-title-edit { font-size: 11px; color: var(--text); background: var(--bg-dark); border: 1px solid var(--primary); border-radius: 4px; padding: 1px 6px; outline: none; width: 220px; max-width: 100%; }

.header-controls { display: flex; align-items: center; gap: 6px; }

.btn-header {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
}
.btn-header:hover { background: var(--tertiary); color: var(--text); }
.btn-header.active { color: var(--primary); }

/* ── Overlay (mobile) ───────────────────────────────────────────────────────── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 99;
  backdrop-filter: blur(2px);
}

/* ── Common UI components ───────────────────────────────────────────────────── */

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 18px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: background var(--transition), opacity var(--transition), transform 0.1s;
  cursor: pointer;
  border: none;
}
.btn:active { transform: scale(0.97); }
.btn-primary   { background: var(--primary);   color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--tertiary);  color: var(--text); }
.btn-secondary:hover { background: var(--border-light); }
.btn-danger    { background: var(--danger);    color: #fff; }
.btn-danger:hover { opacity: 0.85; }
.btn-ghost     { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--border-light); color: var(--text); }
.btn:disabled  { opacity: 0.45; cursor: not-allowed; }

/* Form elements */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}
.form-label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.form-input, .form-textarea, .form-select {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-select option { background: var(--secondary); }

/* Modal */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.modal-backdrop.open { display: flex; }

.modal {
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(-16px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0)     scale(1); }
}

.modal-title { font-size: 16px; font-weight: 700; margin-bottom: 20px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* Toast notifications */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
  color: var(--text);
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  animation: toastIn 0.3s ease;
  pointer-events: all;
  max-width: 320px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error   { border-left: 3px solid var(--danger); }
.toast.info    { border-left: 3px solid var(--primary); }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(20px); }
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 12px;
  color: var(--text-muted);
  padding: 40px;
  text-align: center;
}
.empty-state svg   { opacity: 0.3; }
.empty-state h3    { font-size: 16px; font-weight: 600; color: var(--text-dim); }
.empty-state p     { font-size: 13px; line-height: 1.6; }

/* Loading spinner */
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Scrollbar (global) */
::-webkit-scrollbar       { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* ── iOS Safe Area Insets (standalone PWA / notch / home indicator) ──────────── */
#sidebar {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
#chat-header {
  height: calc(var(--header-height) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
}

/* ── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
    z-index: 100;
    box-shadow: 4px 0 20px rgba(0,0,0,0.4);
  }
  #sidebar.open {
    transform: translateX(0);
  }
  #sidebar-overlay.open { display: block; }
  #hamburger { display: flex; }
}

/* ── Hub Widgets ─────────────────────────────────────────────────────────────── */
#widget-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 12px 6px;
  border-bottom: 1px solid var(--border);
}
.widget-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  flex: 1;
  min-width: 56px;
  max-width: 80px;
  transition: background 0.15s, border-color 0.15s;
  color: var(--text);
}
.widget-btn:hover {
  background: var(--bg-active);
  border-color: var(--primary);
}
.widget-btn-icon { font-size: 18px; line-height: 1; }
.widget-btn-label {
  font-size: 10px;
  color: var(--text-muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 68px;
}
.widget-btn-builtin .widget-btn-icon { font-size: 16px; }
