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

:root {
  --bg-primary: #0d0d0d;
  --bg-secondary: #1a1a1a;
  --bg-sidebar: #0d0d0d;
  --bg-input: #1e1e1e;
  --bg-hover: #1f1f1f;
  --bg-user-msg: #2b2b2b;
  --bg-code: #161616;
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0a0;
  --text-muted: #555;
  --border: #2a2a2a;
  --accent: #6c5ce7;
  --accent-glow: rgba(108, 92, 231, 0.15);
  --accent-light: #a29bfe;
  --scrollbar: #333;
  --radius: 12px;
  --radius-lg: 20px;
  --radius-xl: 24px;
  --max-width: 780px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100dvh;
}

/* ─── Sidebar ─── */
#sidebar {
  width: 280px;
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  padding: 12px;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  transition: transform var(--transition);
  z-index: 100;
}

#sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 16px;
}

#sidebar-header .logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #a29bfe);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  color: white;
  flex-shrink: 0;
}

#sidebar-header .logo-text {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

#new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 11px 16px;
  background: linear-gradient(135deg, var(--accent), #7c6cf0);
  border: none;
  border-radius: var(--radius);
  color: white;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  letter-spacing: -0.2px;
}

#new-chat:hover {
  filter: brightness(1.15);
  transform: translateY(-1px);
  box-shadow: 0 4px 20px var(--accent-glow);
}

#new-chat:active {
  transform: translateY(0);
}

#new-chat svg {
  flex-shrink: 0;
}

#chat-list {
  flex: 1;
  overflow-y: auto;
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-right: 4px;
}

#chat-list::-webkit-scrollbar {
  width: 4px;
}

#chat-list::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border-radius: 2px;
}

.chat-item {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all var(--transition);
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-item::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  transition: background var(--transition);
}

.chat-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.chat-item.active {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.chat-item.active::before {
  background: var(--accent);
}

.chat-item .chat-title {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.chat-item .delete-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 6px;
  border-radius: 6px;
  flex-shrink: 0;
  transition: all var(--transition);
  line-height: 1;
}

.chat-item:hover .delete-btn {
  display: flex;
}

.chat-item .delete-btn:hover {
  color: #f87171;
  background: rgba(248, 113, 113, 0.1);
}

#sidebar-footer {
  padding: 14px 8px 6px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  border-top: 1px solid var(--border);
  margin-top: 8px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}

/* ─── Mobile menu toggle ─── */
#menu-toggle {
  display: none;
  position: fixed;
  top: 14px;
  left: 14px;
  z-index: 200;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text-primary);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: all var(--transition);
}

#menu-toggle:hover {
  background: var(--bg-hover);
}

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 90;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

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

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  scroll-behavior: smooth;
}

#messages::-webkit-scrollbar {
  width: 5px;
}

#messages::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border-radius: 3px;
}

/* ─── Welcome ─── */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 16px;
  padding: 20px;
  text-align: center;
}

.welcome-logo {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent), #a29bfe);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: 800;
  color: white;
  margin-bottom: 4px;
  box-shadow: 0 8px 32px var(--accent-glow);
}

.welcome h1 {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome p {
  color: var(--text-muted);
  font-size: 15px;
  max-width: 360px;
  line-height: 1.5;
}

.welcome-hints {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
  max-width: 460px;
  width: 100%;
}

.hint-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  text-align: left;
  line-height: 1.45;
}

.hint-card:hover {
  background: var(--bg-hover);
  border-color: #3a3a3a;
  color: var(--text-primary);
  transform: translateY(-2px);
}

/* ─── Messages ─── */
.message {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 8px 24px;
  width: 100%;
  animation: fadeIn 0.25s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  display: flex;
  justify-content: flex-end;
  padding-top: 16px;
  padding-bottom: 4px;
}

.message.user .bubble {
  background: var(--bg-user-msg);
  padding: 12px 18px;
  border-radius: var(--radius-xl) var(--radius-xl) 6px var(--radius-xl);
  max-width: 80%;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.55;
  font-size: 14.5px;
}

.message.assistant {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  padding-top: 4px;
  padding-bottom: 16px;
}

.message.assistant .avatar {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), #a29bfe);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 13px;
  font-weight: 700;
  color: white;
  margin-top: 2px;
}

.message.assistant .bubble {
  line-height: 1.7;
  white-space: pre-wrap;
  word-wrap: break-word;
  min-width: 0;
  flex: 1;
  font-size: 14.5px;
  color: var(--text-primary);
}

.message.assistant .bubble p {
  margin-bottom: 10px;
}

.message.assistant .bubble p:last-child {
  margin-bottom: 0;
}

.message.assistant .bubble code {
  background: var(--bg-code);
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 13px;
  font-family: 'SF Mono', 'JetBrains Mono', 'Fira Code', monospace;
  border: 1px solid var(--border);
}

.message.assistant .bubble pre {
  background: var(--bg-code);
  padding: 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 14px 0;
  border: 1px solid var(--border);
}

.message.assistant .bubble pre code {
  background: none;
  padding: 0;
  border: none;
  font-size: 13px;
  line-height: 1.6;
}

.typing-cursor::after {
  content: '▋';
  animation: blink 0.8s step-end infinite;
  color: var(--accent-light);
  font-size: 16px;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ─── Input ─── */
#input-area {
  padding: 0 24px 28px;
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
}

#chat-form {
  width: 100%;
}

#input-wrapper {
  display: flex;
  align-items: flex-end;
  background: var(--bg-input);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 10px 14px 10px 18px;
  transition: all var(--transition);
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
}

#input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow), 0 2px 12px rgba(0,0,0,0.15);
}

#user-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 200px;
  padding: 4px 0;
  font-family: inherit;
}

#user-input::placeholder {
  color: var(--text-muted);
}

#send-btn {
  background: linear-gradient(135deg, var(--accent), #7c6cf0);
  border: none;
  border-radius: var(--radius);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  flex-shrink: 0;
  transition: all var(--transition);
}

#send-btn:disabled {
  opacity: 0.25;
  cursor: default;
}

#send-btn:not(:disabled):hover {
  filter: brightness(1.15);
  transform: scale(1.05);
  box-shadow: 0 4px 16px var(--accent-glow);
}

#send-btn:not(:disabled):active {
  transform: scale(0.97);
}

#stop-btn {
  display: none;
  background: #ef4444;
  border: none;
  border-radius: var(--radius);
  width: 36px;
  height: 36px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  flex-shrink: 0;
  transition: all var(--transition);
}

#stop-btn:hover {
  filter: brightness(1.15);
}

/* ─── Error ─── */
.error-msg {
  background: rgba(239, 68, 68, 0.08);
  color: #f87171;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  border: 1px solid rgba(239, 68, 68, 0.15);
}

/* ─── Mobile ─── */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 300px;
    transform: translateX(-100%);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #sidebar-overlay.open {
    display: block;
  }

  #menu-toggle {
    display: flex;
  }

  #main {
    padding-top: 56px;
  }

  .message {
    padding: 6px 16px;
  }

  .message.user .bubble {
    max-width: 88%;
  }

  #input-area {
    padding: 0 14px 20px;
  }

  .welcome h1 {
    font-size: 26px;
  }

  .welcome-hints {
    grid-template-columns: 1fr;
    padding: 0 8px;
  }

  .hint-card {
    font-size: 13px;
  }
}

@media (max-width: 380px) {
  .message.user .bubble {
    max-width: 92%;
    padding: 10px 14px;
  }

  #input-wrapper {
    padding: 8px 10px 8px 14px;
    border-radius: 16px;
  }
}

/* ─── Safe area for notch devices ─── */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #input-area {
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }
}
