/* ═══════════════════════════════════════════════════════════════════
   WAI Chat UI — Wansaw AI
   ═══════════════════════════════════════════════════════════════════ */

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

:root {
  --bg: #0b0d10;
  --bg-2: #0f1117;
  --sidebar-bg: #0a0c0f;
  --surface: #16181f;
  --surface-2: #1c1f28;
  --surface-3: #22252f;
  --border: rgba(255,255,255,0.06);
  --border-2: rgba(255,255,255,0.1);
  --text: #e8eaf0;
  --text-2: #c8ccd4;
  --muted: #6b7280;
  --primary: #6c5ce7;
  --primary-light: #a29bfe;
  --accent: #00cec9;
  --accent-dim: rgba(0,206,201,0.08);
  --gradient: linear-gradient(135deg, #6c5ce7, #00cec9);
  --user-bg: #1a1d26;
  --code-bg: #0d0f14;
  --code-header: #14161c;
  --error: #f87171;
  --sidebar-w: 260px;
  --radius: 12px;
  --radius-sm: 8px;
}

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.6;
  display: flex;
  overflow: hidden;
  height: 100dvh;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.08); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.14); }

.gradient-text {
  background: var(--gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ════════════════════════════════
   SIDEBAR
════════════════════════════════ */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.25s ease;
}

#sidebar-top {
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.sidebar-brand-text {
  font-size: 1.1rem;
  font-weight: 700;
  background: var(--gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#new-chat-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}
#new-chat-btn:hover { background: var(--surface-2); border-color: var(--border-2); }

#history-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 8px 8px;
}

.hist-section-label {
  font-size: 0.68rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
  padding: 12px 8px 4px;
  user-select: none;
}

.hist-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.84rem;
  color: var(--muted);
  transition: all 0.12s;
  position: relative;
}
.hist-item:hover { background: var(--surface); color: var(--text); }
.hist-item.active { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.hist-item .hist-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hist-item .hist-actions {
  display: none;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.hist-item:hover .hist-actions { display: flex; }
.hist-item .hist-action-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 3px 4px;
  border-radius: 4px;
  font-size: 0.7rem;
  line-height: 1;
  transition: color 0.12s;
}
.hist-item .hist-action-btn:hover { color: var(--text); }
.hist-item .hist-action-btn.del:hover { color: var(--error); }

.hist-empty {
  text-align: center;
  font-size: 0.82rem;
  color: var(--muted);
  padding: 30px 10px;
}

#sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 8px;
  flex-shrink: 0;
}
.sb-footer-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  transition: all 0.12s;
  text-align: left;
  text-decoration: none;
}
.sb-footer-btn:hover { background: var(--surface); color: var(--text); }

/* ════════════════════════════════
   MAIN AREA
════════════════════════════════ */
#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ── Top bar ── */
#topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
  backdrop-filter: blur(12px);
}

#mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}
#mobile-menu-btn:hover { color: var(--text); }

#topbar-title-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

#topbar-title {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: default;
}

#topbar-rename-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
#topbar-title-wrap:hover #topbar-rename-btn { opacity: 1; }
#topbar-rename-btn:hover { color: var(--primary-light); }

.rename-input {
  background: var(--surface);
  border: 1px solid var(--primary);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  padding: 3px 8px;
  border-radius: 6px;
  outline: none;
  width: 200px;
}

#topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#model-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  max-width: 160px;
  transition: border-color 0.15s;
}
#model-select:focus { border-color: var(--primary); }
#model-select option { background: var(--bg); }

/* ── Messages ── */
#messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 20px 0 12px;
}

/* Empty state */
#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px 20px;
  text-align: center;
}
#empty-state h2 {
  font-size: 1.7rem;
  font-weight: 600;
  color: var(--text);
}
#empty-state p {
  font-size: 0.95rem;
  color: var(--muted);
  max-width: 360px;
}

.empty-orb {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: orbPulse 3s ease-in-out infinite;
  box-shadow: 0 0 40px rgba(108, 92, 231, 0.3), 0 0 80px rgba(0, 206, 201, 0.15);
}
.orb-inner {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: inset 0 0 20px rgba(108, 92, 231, 0.2);
}
@keyframes orbPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 40px rgba(108,92,231,0.3), 0 0 80px rgba(0,206,201,0.15); }
  50% { transform: scale(1.05); box-shadow: 0 0 60px rgba(108,92,231,0.4), 0 0 100px rgba(0,206,201,0.2); }
}

.empty-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}
.chip {
  padding: 8px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 100px;
  color: var(--text-2);
  font-size: 0.82rem;
  cursor: pointer;
  transition: all 0.15s;
}
.chip:hover {
  background: var(--surface-2);
  border-color: var(--primary);
  color: var(--text);
}

/* ── Message rows ── */
.msg-row { padding: 8px 0; animation: msgFadeIn 0.3s ease; }
@keyframes msgFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: flex-start;
  gap: 14px;
}

/* User message */
.user-row .avatar {
  width: 32px; height: 32px; border-radius: 10px;
  background: var(--surface-3); color: var(--text-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.72rem; font-weight: 700;
  flex-shrink: 0; margin-top: 2px;
}
.user-row .msg-content {
  background: var(--user-bg);
  border: 1px solid var(--border);
  border-radius: 16px 16px 16px 4px;
  padding: 12px 16px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.93rem;
  max-width: calc(100% - 46px);
}

/* Bot message */
.bot-row .avatar {
  width: 32px; height: 32px; border-radius: 10px;
  background: var(--gradient); color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.6rem; font-weight: 800;
  flex-shrink: 0; margin-top: 4px;
  letter-spacing: -0.5px;
}
.bot-row .msg-content {
  flex: 1;
  min-width: 0;
  font-size: 0.93rem;
  word-break: break-word;
  padding: 4px 0;
}

/* ── Thinking/Reasoning block ── */
.thinking-wrap {
  margin-bottom: 14px;
  border-radius: var(--radius);
  background: rgba(108, 92, 231, 0.04);
  border: 1px solid rgba(108, 92, 231, 0.12);
  overflow: hidden;
  transition: all 0.2s;
}

.thinking-header {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  font-size: 0.8rem;
  color: var(--primary-light);
  padding: 10px 14px;
  transition: background 0.12s;
}
.thinking-header:hover { background: rgba(108, 92, 231, 0.06); }
.thinking-header svg { flex-shrink: 0; opacity: 0.8; }

.thinking-icon {
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thinking-label {
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.thinking-preview {
  font-size: 0.8rem;
  color: var(--muted);
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.thinking-toggle-lbl {
  font-size: 0.72rem;
  color: var(--primary-light);
  white-space: nowrap;
  margin-left: auto;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(108, 92, 231, 0.1);
}

.thinking-body {
  padding: 0 14px 12px;
  font-size: 0.82rem;
  color: var(--text-2);
  white-space: pre-wrap;
  line-height: 1.7;
  overflow: hidden;
  border-top: 1px solid rgba(108, 92, 231, 0.08);
  max-height: 400px;
  overflow-y: auto;
}
.thinking-body.collapsed { display: none; }

/* Thinking animation during streaming */
.thinking-active .thinking-header::after {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--primary-light);
  animation: thinkBlink 1s ease-in-out infinite;
  margin-left: 4px;
}
@keyframes thinkBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ── Markdown content ── */
.md p { margin: 6px 0; }
.md p:first-child { margin-top: 0; }
.md p:last-child { margin-bottom: 0; }
.md h1 { font-size: 1.25em; font-weight: 600; margin: 16px 0 8px; color: var(--text); }
.md h2 { font-size: 1.1em; font-weight: 600; margin: 14px 0 6px; color: var(--text); }
.md h3 { font-size: 1em; font-weight: 600; margin: 12px 0 5px; color: var(--text); }
.md ul, .md ol { margin: 8px 0 8px 20px; }
.md li { margin: 4px 0; }
.md strong { font-weight: 600; color: var(--text); }
.md em { font-style: italic; }
.md a { color: var(--primary-light); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.md code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 5px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 0.83em;
  color: var(--accent);
}
.md pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 12px 0;
  overflow: hidden;
}
.md pre .code-hdr {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 14px;
  background: var(--code-header);
  border-bottom: 1px solid var(--border);
  font-size: 0.74rem;
  color: var(--muted);
  font-family: 'JetBrains Mono', monospace;
}
.copy-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.72rem;
  padding: 3px 8px;
  border-radius: 4px;
  transition: all 0.12s;
  font-family: inherit;
}
.copy-btn:hover { color: var(--text); border-color: var(--border-2); }
.md pre code {
  background: none;
  border: none;
  padding: 12px 16px;
  display: block;
  overflow-x: auto;
  font-size: 0.83rem;
  line-height: 1.65;
  color: #d4d4d8;
}

/* Cursor blink */
.cursor {
  display: inline-block;
  width: 2px; height: 1em;
  background: var(--primary-light);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
  border-radius: 1px;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }

/* ════════════════════════════════
   INPUT AREA
════════════════════════════════ */
#footer {
  flex-shrink: 0;
  padding: 14px 24px 16px;
  background: var(--bg);
}

.input-box {
  max-width: 760px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.input-box:focus-within {
  border-color: rgba(108, 92, 231, 0.4);
  box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.08);
}

#input {
  background: none;
  border: none;
  color: var(--text);
  font-size: 0.93rem;
  font-family: inherit;
  line-height: 1.6;
  resize: none;
  outline: none;
  padding: 14px 16px 0;
  max-height: 180px;
  overflow-y: auto;
  width: 100%;
}
#input::placeholder { color: var(--muted); }

.input-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 12px 10px;
  gap: 8px;
}

.search-pill {
  display: flex;
  align-items: center;
  gap: 5px;
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 0.76rem;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  font-family: inherit;
}
.search-pill:hover { border-color: var(--border-2); color: var(--text-2); }
.search-pill.on {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.research-pill {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(108, 92, 231, 0.08);
  border: 1px solid rgba(108, 92, 231, 0.3);
  color: var(--primary-light);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 0.76rem;
  cursor: default;
  white-space: nowrap;
  font-family: inherit;
}

/* Image size picker */
.img-size-wrap {
  display: flex;
  align-items: center;
  gap: 5px;
  border: 1px solid rgba(0, 206, 201, 0.3);
  background: var(--accent-dim);
  color: var(--accent);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.76rem;
  white-space: nowrap;
}
.img-size-wrap svg { opacity: 0.8; flex-shrink: 0; }
.img-size-wrap select {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.76rem;
  cursor: pointer;
  outline: none;
  font-family: inherit;
}
.img-size-wrap select option { background: var(--bg); color: var(--text); }

/* Generated image */
.img-generating {
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
  animation: imgPulse 1.4s ease-in-out infinite;
}
@keyframes imgPulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

.img-result { display: flex; flex-direction: column; gap: 8px; }
.gen-img {
  max-width: 100%;
  max-height: 480px;
  border-radius: var(--radius);
  object-fit: contain;
  cursor: pointer;
  border: 1px solid var(--border);
  transition: opacity 0.15s;
}
.gen-img:hover { opacity: 0.92; }
.img-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.75rem;
  color: var(--muted);
}
.img-dl {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--primary-light);
  text-decoration: none;
  font-size: 0.75rem;
}
.img-dl:hover { text-decoration: underline; }

#send-btn {
  width: 34px; height: 34px;
  background: var(--gradient);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.15s;
  color: white;
}
#send-btn:disabled { opacity: 0.3; cursor: not-allowed; transform: none; }
#send-btn:not(:disabled):hover { transform: scale(1.05); }
#send-btn:not(:disabled):active { transform: scale(0.95); }

.input-hint {
  text-align: center;
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 8px;
  max-width: 760px;
  margin-left: auto;
  margin-right: auto;
  opacity: 0.6;
}

/* ════════════════════════════════
   MOBILE
════════════════════════════════ */
@media (max-width: 700px) {
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(2px);
    z-index: 99;
  }
  #sidebar-overlay.show { display: block; }
  #mobile-menu-btn { display: flex; }
  .msg-inner { padding: 0 14px; }
  #footer { padding: 10px 14px 14px; }
  .empty-chips { flex-direction: column; align-items: center; }
  #topbar { padding: 10px 14px; }
}

/* ════════════════════════════════
   ANIMATIONS & EXTRAS
════════════════════════════════ */
/* Typing indicator dots */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 0;
}
.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--primary-light);
  animation: typingBounce 1.4s ease-in-out infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Rename input in history items */
.hist-rename-input {
  background: var(--surface);
  border: 1px solid var(--primary);
  color: var(--text);
  font-size: 0.82rem;
  padding: 2px 6px;
  border-radius: 4px;
  outline: none;
  width: 100%;
  font-family: inherit;
}
