/* ============================================================
   Nexus — Build Without Limits
   Dark, Copilot-inspired UI. No DB. Cookies + localStorage.
   ============================================================ */

:root {
  --bg-sidebar:    #1a1a1c;
  --bg-main:       #2a2a2e;
  --bg-input:      #38383d;
  --bg-elevated:   #323236;
  --bg-hover:      #3a3a3f;
  --border:        #3c3c42;
  --border-soft:   #2c2c30;

  --text:          #f5f5f7;
  --text-dim:      #b5b5bb;
  --text-faint:    #75757c;

  --accent:        #6e8cff;
  --accent-soft:   rgba(110,140,255,0.18);
  --danger:        #ff6b6b;
  --success:       #4ade80;
  --warn:          #fbbf24;

  --radius:        10px;
  --radius-lg:     16px;
  --transition:    180ms cubic-bezier(.4,0,.2,1);

  --sidebar-w:     260px;
  --font-sans:     -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono:     "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

  /* zoom factor — controlled by JS via --zoom multiplier */
  --zoom: 1;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { font-size: calc(16px * var(--zoom)); }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg-main);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
}

/* Prevent iOS auto-zoom on input focus by keeping input font-size >= 16px */
input, textarea, select { font-size: 16px !important; }

/* ============================================================
   Auth modal
   ============================================================ */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  animation: fadeIn 220ms ease;
}
.modal-overlay[aria-hidden="true"] { display: none; }

.modal {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px 32px;
  max-width: 420px;
  width: calc(100% - 32px);
  text-align: center;
  animation: slideUp 280ms cubic-bezier(.2,.8,.2,1);
}
.auth-icon { display: flex; justify-content: center; margin-bottom: 16px; }
.modal h1 { margin: 0 0 8px; font-size: 24px; font-weight: 600; }
.modal-sub { margin: 0 0 24px; color: var(--text-dim); font-size: 14px; line-height: 1.5; }

.tabs { display: flex; gap: 4px; margin-bottom: 18px; background: var(--bg-input); padding: 4px; border-radius: 8px; }
.tab {
  flex: 1; padding: 8px; background: transparent; color: var(--text-dim);
  border: 0; border-radius: 6px; cursor: pointer; font-size: 14px; font-weight: 500;
  transition: var(--transition);
}
.tab.active { background: var(--bg-elevated); color: var(--text); }

.modal label { display: block; text-align: left; font-size: 12px; color: var(--text-dim); margin: 0 0 6px 2px; }
.modal input[type="text"] {
  width: 100%; padding: 12px 14px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); outline: none; transition: var(--transition);
}
.modal input[type="text"]:focus { border-color: var(--accent); }

.auth-error {
  margin-top: 12px; padding: 10px 12px; background: rgba(255,107,107,0.1);
  border: 1px solid rgba(255,107,107,0.3); border-radius: 8px;
  color: var(--danger); font-size: 13px; text-align: left;
}

.primary-btn {
  width: 100%; margin-top: 18px; padding: 12px;
  background: var(--accent); color: #0b0b0d; border: 0; border-radius: 8px;
  font-size: 15px; font-weight: 600; cursor: pointer;
  transition: var(--transition);
}
.primary-btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.primary-btn:disabled { opacity: .6; cursor: not-allowed; transform: none; }

.modal-note {
  margin: 18px 0 0; font-size: 11px; color: var(--text-faint); line-height: 1.55;
}
.modal-note strong { color: var(--text-dim); }

/* ============================================================
   App shell
   ============================================================ */
.app {
  display: flex;
  height: 100vh;
  width: 100vw;
}
.app[aria-hidden="true"] { display: none; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  transition: width 240ms cubic-bezier(.4,0,.2,1), margin 240ms cubic-bezier(.4,0,.2,1);
  overflow: hidden;
}
.sidebar.closed {
  width: 0;
  margin-left: calc(-1 * var(--sidebar-w));
  border-right: 0;
}

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { filter: drop-shadow(0 0 6px rgba(255,255,255,0.12)); }
.brand-name {
  font-size: 17px; font-weight: 700; letter-spacing: 0.5px;
}

.icon-btn {
  background: transparent; border: 0; color: var(--text-dim);
  padding: 6px; border-radius: 6px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.new-chat-btn {
  margin: 4px 12px 12px;
  padding: 10px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px; font-weight: 500;
  cursor: pointer;
  display: flex; align-items: center; gap: 8px;
  transition: var(--transition);
}
.new-chat-btn:hover { background: var(--bg-hover); border-color: var(--accent-soft); }
.new-chat-btn svg { color: var(--text-dim); }

.search-wrap {
  position: relative;
  margin: 0 12px 8px;
}
.search-wrap .search-icon {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--text-faint);
}
.search-wrap input {
  width: 100%; padding: 8px 10px 8px 32px;
  background: var(--bg-input); border: 1px solid transparent; border-radius: 8px;
  color: var(--text); outline: none; transition: var(--transition);
}
.search-wrap input:focus { border-color: var(--accent-soft); background: var(--bg-elevated); }
.search-wrap input::placeholder { color: var(--text-faint); }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 8px;
  display: flex; flex-direction: column; gap: 2px;
}
.chat-list::-webkit-scrollbar { width: 8px; }
.chat-list::-webkit-scrollbar-thumb { background: var(--bg-input); border-radius: 4px; }

.chat-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-radius: 6px; cursor: pointer;
  color: var(--text-dim); font-size: 13px;
  transition: var(--transition);
  position: relative;
}
.chat-item:hover { background: var(--bg-hover); color: var(--text); }
.chat-item.active { background: var(--bg-elevated); color: var(--text); }
.chat-item.active::before {
  content: ''; position: absolute; left: -2px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 60%; background: var(--accent); border-radius: 2px;
}
.chat-item .chat-title-text {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .chat-delete {
  background: transparent; border: 0; color: var(--text-faint);
  padding: 2px 4px; border-radius: 4px; cursor: pointer; opacity: 0;
  transition: var(--transition);
}
.chat-item:hover .chat-delete { opacity: 1; }
.chat-item .chat-delete:hover { color: var(--danger); background: rgba(255,107,107,0.1); }

.empty-list {
  text-align: center; padding: 30px 12px; color: var(--text-faint); font-size: 13px;
}

.sidebar-footer {
  border-top: 1px solid var(--border-soft);
  padding: 10px 14px;
  display: flex; align-items: center; justify-content: space-between;
}
.current-user {
  font-size: 12px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.current-user::before {
  content: ''; display: inline-block; width: 7px; height: 7px;
  background: var(--success); border-radius: 50%; margin-right: 8px;
  vertical-align: middle;
}
.text-btn {
  background: transparent; border: 0; color: var(--text-faint);
  font-size: 12px; cursor: pointer; padding: 4px 6px; border-radius: 4px;
  transition: var(--transition);
}
.text-btn:hover { color: var(--text); background: var(--bg-hover); }

/* Main area */
.main {
  flex: 1; display: flex; flex-direction: column;
  min-width: 0; position: relative;
}

.main-header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg-main);
}
.sidebar-open { display: none; }
.sidebar.closed ~ .main .sidebar-open { display: inline-flex; }

.main-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
.title-icon { opacity: 0.9; }

.zoom-controls {
  margin-left: auto;
  display: flex; align-items: center; gap: 4px;
  background: var(--bg-input); padding: 4px 6px; border-radius: 8px;
}
.zoom-level {
  font-size: 12px; color: var(--text-dim); min-width: 42px; text-align: center;
  user-select: none;
}

/* Welcome screen */
.welcome {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 40px 24px; text-align: center;
  animation: fadeIn 400ms ease;
}
.welcome-icon {
  margin-bottom: 20px;
  animation: float 4s ease-in-out infinite;
}
.welcome-title {
  font-size: 32px; font-weight: 700; margin: 0 0 8px;
  background: linear-gradient(135deg, #fff 30%, var(--accent) 100%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.welcome-sub { margin: 0; color: var(--text-faint); font-size: 13px; }

/* Messages */
.messages {
  flex: 1; overflow-y: auto; padding: 24px 0 8px;
  scroll-behavior: smooth;
}
.messages::-webkit-scrollbar { width: 10px; }
.messages::-webkit-scrollbar-thumb { background: var(--bg-input); border-radius: 5px; }

.message {
  max-width: 780px;
  margin: 0 auto 22px;
  padding: 0 24px;
  animation: msgIn 320ms cubic-bezier(.2,.8,.2,1);
}
.message.user {
  background: var(--bg-elevated);
  padding: 14px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border-soft);
}
.message.assistant { padding-top: 4px; padding-bottom: 4px; }

.message-content { font-size: 14.5px; line-height: 1.65; color: var(--text); }
.message-content p { margin: 0 0 12px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content ul, .message-content ol { margin: 8px 0 12px; padding-left: 22px; }
.message-content li { margin: 4px 0; }
.message-content h1, .message-content h2, .message-content h3 {
  margin: 18px 0 8px; font-weight: 600; line-height: 1.3;
}
.message-content h1 { font-size: 22px; }
.message-content h2 { font-size: 19px; }
.message-content h3 { font-size: 16px; }
.message-content a { color: var(--accent); text-decoration: none; }
.message-content a:hover { text-decoration: underline; }
.message-content blockquote {
  margin: 10px 0; padding: 8px 14px;
  border-left: 3px solid var(--accent);
  background: rgba(110,140,255,0.06);
  color: var(--text-dim);
  border-radius: 0 6px 6px 0;
}
.message-content table {
  width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 13px;
}
.message-content th, .message-content td {
  border: 1px solid var(--border); padding: 6px 10px; text-align: left;
}
.message-content th { background: var(--bg-elevated); }

/* Inline code */
.message-content :not(pre) > code {
  background: var(--bg-elevated); padding: 2px 6px; border-radius: 4px;
  font-family: var(--font-mono); font-size: 13px; color: #f8c878;
}

/* Code blocks — dropdown, copy, download, line numbers */
.code-block {
  margin: 12px 0;
  background: #1a1a1c;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.code-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  background: #232325;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.code-lang {
  color: var(--text-dim); font-family: var(--font-mono); text-transform: lowercase;
  display: flex; align-items: center; gap: 8px;
}
.code-lang::before {
  content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
}
.code-actions { display: flex; gap: 4px; }
.code-action {
  background: transparent; border: 0; color: var(--text-faint);
  padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 11px;
  display: inline-flex; align-items: center; gap: 4px;
  transition: var(--transition);
}
.code-action:hover { background: var(--bg-hover); color: var(--text); }
.code-action svg { width: 13px; height: 13px; }
.code-collapse-btn { font-weight: 600; }

.code-body {
  position: relative;
  max-height: 600px;
  overflow: auto;
  transition: max-height 250ms ease;
}
.code-body.collapsed { max-height: 0; overflow: hidden; }

.code-table {
  display: flex;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
}
.line-numbers {
  flex-shrink: 0;
  padding: 12px 8px 12px 14px;
  text-align: right;
  background: #1a1a1c;
  color: var(--text-faint);
  user-select: none;
  border-right: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.line-numbers span { display: block; }
.code-content {
  flex: 1;
  padding: 12px 14px;
  overflow-x: auto;
  white-space: pre;
}
.code-content pre { margin: 0; }
.code-content code { font-family: var(--font-mono); font-size: 13px; }

/* Download links generated by AI */
a[download] {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent-soft); color: var(--accent);
  padding: 6px 12px; border-radius: 6px; font-size: 13px;
  text-decoration: none !important; margin: 4px 0;
  border: 1px solid rgba(110,140,255,0.3);
  transition: var(--transition);
}
a[download]:hover { background: rgba(110,140,255,0.28); transform: translateY(-1px); }

/* File attachments in messages */
.file-attachment {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px; margin: 4px 4px 4px 0;
  background: var(--bg-input); border-radius: 8px;
  font-size: 12px; color: var(--text-dim);
  border: 1px solid var(--border-soft);
}
.file-attachment img, .file-attachment video {
  max-width: 220px; max-height: 180px; border-radius: 6px; display: block; margin: 6px 0;
}

/* Streaming/loading indicator — animated Nexus icon */
.thinking {
  display: flex; align-items: center; gap: 10px;
  color: var(--text-dim); font-size: 14px;
  padding: 4px 0;
}
.thinking-icon {
  width: 28px; height: 28px;
  animation: nexus-spin 1.8s linear infinite;
}
.thinking-text {
  font-style: italic; color: var(--text-dim);
  animation: pulse 1.6s ease-in-out infinite;
}

/* Feedback bar */
.feedback-bar {
  max-width: 780px; margin: 0 auto 12px; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  animation: slideUp 260ms cubic-bezier(.2,.8,.2,1);
}
.feedback-text { font-size: 13px; color: var(--text-dim); }
.feedback-actions { display: flex; gap: 8px; }
.fb-btn {
  padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 500;
  border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text-dim);
  transition: var(--transition);
}
.fb-btn:hover { color: var(--text); border-color: var(--accent-soft); }
.fb-yes:hover { background: rgba(74,222,128,0.12); border-color: var(--success); color: var(--success); }
.fb-no:hover { background: rgba(255,107,107,0.12); border-color: var(--danger); color: var(--danger); }
.fb-keep:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* Input area */
.input-area {
  border-top: 1px solid var(--border-soft);
  padding: 12px 24px 14px;
  background: var(--bg-main);
}
.file-previews {
  display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px;
}
.file-previews:empty { display: none; }
.file-preview {
  position: relative;
  width: 64px; height: 64px;
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.file-preview img, .file-preview video { width: 100%; height: 100%; object-fit: cover; }
.file-preview .file-ext {
  font-size: 10px; color: var(--text-faint); font-family: var(--font-mono);
  text-align: center; padding: 4px;
}
.file-preview .remove-file {
  position: absolute; top: 2px; right: 2px;
  background: rgba(0,0,0,0.6); color: #fff; border: 0;
  width: 18px; height: 18px; border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center; font-size: 12px;
}

.input-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 8px;
  transition: var(--transition);
}
.input-row:focus-within { border-color: var(--accent-soft); }

.plus-btn {
  flex-shrink: 0; width: 36px; height: 36px; border-radius: 8px;
  background: var(--bg-elevated); border: 1px solid var(--border-soft);
}
.plus-btn:hover { background: var(--bg-hover); }

#prompt-input {
  flex: 1;
  background: transparent; border: 0; outline: none; resize: none;
  color: var(--text); font-family: var(--font-sans); font-size: 15px;
  line-height: 1.5; padding: 8px 4px; max-height: 200px;
  min-height: 24px;
}
#prompt-input::placeholder { color: var(--text-faint); }

.send-btn, .stop-btn {
  flex-shrink: 0; width: 36px; height: 36px; border-radius: 8px;
  background: var(--accent); color: #0b0b0d; border: 0; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.send-btn:hover { filter: brightness(1.08); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.stop-btn { background: var(--danger); color: #fff; }

.input-hint {
  margin: 8px 0 0; font-size: 11px; color: var(--text-faint); text-align: center;
}

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--bg-sidebar); border: 1px solid var(--border);
  padding: 10px 16px; border-radius: 8px;
  font-size: 13px; color: var(--text); z-index: 200;
  opacity: 0; transition: opacity 220ms ease, transform 220ms ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Animations */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.45; } }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes nexus-spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Mobile */
@media (max-width: 720px) {
  :root { --sidebar-w: 84vw; }
  .sidebar { position: absolute; top: 0; bottom: 0; left: 0; z-index: 50; box-shadow: 8px 0 24px rgba(0,0,0,0.4); }
  .sidebar.closed { margin-left: calc(-1 * var(--sidebar-w)); }
  .welcome-title { font-size: 24px; }
  .message { padding: 0 16px; }
  .input-area { padding: 10px 12px 12px; }
  .zoom-controls { display: none; }
  .main-header { padding: 8px 10px; }
  .sidebar-open { display: inline-flex !important; }
}
