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

:root {
  --bg: #212121;
  --sidebar: #171717;
  --surface: #2f2f2f;
  --surface-hover: #3a3a3a;
  --border: rgba(255,255,255,0.08);
  --text: #ececec;
  --text-muted: #8e8ea0;
  --accent: #10a37f;
  --accent-hover: #0d8a6b;
  --danger: #ef4444;
  --sidebar-w: 260px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  --mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  --radius: 8px;
  --radius-lg: 14px;
}

html, body { height: 100%; overflow: hidden; }
body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.6; }

/* ─── Layout ─────────────────────────────────────────────── */
.app { display: flex; height: 100vh; }

/* ─── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  transition: transform 0.25s ease;
  z-index: 20;
}

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

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}

.logo-icon { color: var(--accent); font-size: 17px; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.1) transparent;
}
.conv-list::-webkit-scrollbar { width: 4px; }
.conv-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

.conv-section-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 8px 10px 4px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
  min-width: 0;
}
.conv-item:hover { background: var(--surface); }
.conv-item:hover .conv-delete { opacity: 1; }
.conv-item.active { background: var(--surface); }

.conv-icon { font-size: 13px; flex-shrink: 0; color: var(--text-muted); }

.conv-title {
  flex: 1;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-delete {
  flex-shrink: 0;
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  transition: color 0.15s, opacity 0.15s;
}
.conv-delete:hover { color: var(--danger); }

.sidebar-bottom {
  padding: 8px;
  border-top: 1px solid var(--border);
}

.sidebar-footer-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 10px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius);
  transition: background 0.15s, color 0.15s;
}
.sidebar-footer-btn:hover { background: var(--surface); color: var(--text); }

/* ─── Main ───────────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

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

.header-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.model-select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  padding: 5px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}
.model-select:focus { border-color: var(--accent); }
.model-select option { background: #2a2a2a; }

/* Thinking toggle */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.toggle-label input { display: none; }

.toggle-track {
  width: 32px;
  height: 18px;
  background: var(--surface);
  border-radius: 9px;
  position: relative;
  transition: background 0.2s;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}
.toggle-label input:checked + .toggle-track { background: var(--accent); border-color: var(--accent); }
.toggle-label input:checked + .toggle-track .toggle-thumb { transform: translateX(14px); background: #fff; }

/* Personality select */
.personality-select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  padding: 5px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}
.personality-select:focus { border-color: var(--accent); }
.personality-select option { background: #2a2a2a; }

/* Personality active — header glow */
.chat-header.chaos-active { border-bottom-color: rgba(249,115,22,0.35); box-shadow: 0 1px 0 rgba(249,115,22,0.15); }
.chat-header.chaos-active .personality-select { border-color: #f97316; color: #f97316; }
.chat-header.motivational-active { border-bottom-color: rgba(34,197,94,0.35); box-shadow: 0 1px 0 rgba(34,197,94,0.15); }
.chat-header.motivational-active .personality-select { border-color: #22c55e; color: #22c55e; }

/* ─── Messages ───────────────────────────────────────────── */
.messages-wrap {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.1) transparent;
}
.messages-wrap::-webkit-scrollbar { width: 4px; }
.messages-wrap::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

.messages {
  max-width: 800px;
  margin: 0 auto;
  padding: 28px 24px 16px;
}

/* Welcome */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 0 40px;
  text-align: center;
}
.welcome-icon { font-size: 42px; color: var(--accent); margin-bottom: 18px; }
.welcome h1 { font-size: 26px; font-weight: 600; color: var(--text); }

/* Message */
.msg {
  display: flex;
  gap: 14px;
  margin-bottom: 26px;
  animation: fadeUp 0.18s ease;
}
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-top: 3px;
}
.msg.user .msg-avatar { background: #5a5af0; color: #fff; }
.msg.assistant .msg-avatar { background: var(--accent); color: #fff; font-size: 15px; }

.msg-content { flex: 1; min-width: 0; }

.msg-role {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 5px;
  color: var(--text);
}

.msg-text { color: var(--text); line-height: 1.75; word-break: break-word; }
.msg.user .msg-text { white-space: pre-wrap; }

/* Markdown */
.msg-text p { margin-bottom: 10px; }
.msg-text p:last-child { margin-bottom: 0; }
.msg-text h1, .msg-text h2, .msg-text h3 { margin: 18px 0 8px; font-weight: 600; }
.msg-text h1 { font-size: 20px; }
.msg-text h2 { font-size: 17px; }
.msg-text h3 { font-size: 15px; }
.msg-text ul, .msg-text ol { padding-left: 22px; margin-bottom: 10px; }
.msg-text li { margin-bottom: 3px; }
.msg-text blockquote { border-left: 3px solid var(--border); padding-left: 14px; color: var(--text-muted); margin: 10px 0; }
.msg-text a { color: var(--accent); text-decoration: none; }
.msg-text a:hover { text-decoration: underline; }
.msg-text table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 14px; }
.msg-text th, .msg-text td { border: 1px solid var(--border); padding: 7px 12px; text-align: left; }
.msg-text th { background: rgba(255,255,255,0.04); font-weight: 600; }
.msg-text hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* Inline code */
.msg-text code:not([class*="language-"]):not(.hljs) {
  background: rgba(255,255,255,0.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 13px;
}

/* Code block */
.code-block {
  background: #1a1a1a;
  border-radius: var(--radius);
  margin: 12px 0;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.06);
}
.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 14px;
  background: rgba(255,255,255,0.04);
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.code-lang { font-size: 12px; color: var(--text-muted); font-family: var(--mono); }
.copy-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
  font-family: var(--font);
}
.copy-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.code-block pre { margin: 0; padding: 14px; overflow-x: auto; }
.code-block pre code { background: none !important; padding: 0 !important; font-family: var(--mono); font-size: 13px; line-height: 1.6; }

/* ─── Thinking block ─────────────────────────────────────────── */
.thinking-block {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
  background: rgba(255,255,255,0.02);
}

.thinking-block summary {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  list-style: none;
  transition: background 0.15s;
}
.thinking-block summary::-webkit-details-marker { display: none; }
.thinking-block summary:hover { background: rgba(255,255,255,0.04); }

.thinking-block[open] summary {
  border-bottom: 1px solid var(--border);
}

.thinking-icon { flex-shrink: 0; opacity: 0.6; }

.thinking-label { font-weight: 500; }

/* Animated pulse when open (actively thinking) */
.thinking-block[open] .thinking-icon {
  animation: thinking-pulse 1.5s ease-in-out infinite;
}
@keyframes thinking-pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.thinking-content {
  padding: 12px 14px;
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 260px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.1) transparent;
}

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

/* ─── Input ──────────────────────────────────────────────── */
.input-area {
  padding: 14px 24px 20px;
  flex-shrink: 0;
}

.input-box {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: var(--radius-lg);
  padding: 10px 12px;
  transition: border-color 0.2s;
}
.input-box:focus-within { border-color: rgba(255,255,255,0.25); }

.input-box textarea {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  resize: none;
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}
.input-box textarea::placeholder { color: var(--text-muted); }

.send-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.send-btn.stop { background: #dc2626; }
.send-btn.stop:hover { background: #b91c1c; }

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

/* ─── Shared button ──────────────────────────────────────── */
.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }

/* ─── Modal ──────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeUp 0.15s ease;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: #272727;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 420px;
  max-width: calc(100vw - 32px);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}
.modal-header h2 { font-size: 17px; font-weight: 600; }
.modal-body { display: flex; flex-direction: column; gap: 14px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.form-hint { font-size: 12px; color: var(--text-muted); opacity: 0.7; }
.form-group input {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  font-family: var(--font);
}
.form-group input:focus { border-color: var(--accent); }
.modal-footer { margin-top: 22px; display: flex; justify-content: flex-end; }
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 9px 22px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  font-family: var(--font);
}
.btn-primary:hover { background: var(--accent-hover); }

/* ─── Mobile overlay ─────────────────────────────────────── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 10;
}

/* ─── Responsive ─────────────────────────────────────────── */
.app.sidebar-collapsed .sidebar { display: none; }

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
    display: flex !important;
  }
  .app.sidebar-collapsed .sidebar { transform: translateX(-100%); display: flex !important; }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay { display: none; }
  .sidebar-overlay.visible { display: block; }
  .messages { padding: 16px 14px 8px; }
  .input-area { padding: 10px 14px 14px; }
  .input-hint { display: none; }
}
