:root {
  /* Color palette - inspired by Linear/Vercel/Claude */
  --bg-0: #0b0d12;
  --bg-1: #11141b;
  --bg-2: #181c25;
  --bg-3: #232834;
  --bg-hover: rgba(255, 255, 255, 0.04);
  --bg-active: rgba(255, 255, 255, 0.08);

  --text-1: #e8eaee;
  --text-2: #a1a8b4;
  --text-3: #6c7280;
  --text-disabled: #4a5161;

  --accent: #7c83f7;
  --accent-2: #5a62e8;
  --accent-glow: rgba(124, 131, 247, 0.15);

  --user-bubble: linear-gradient(135deg, #5a62e8 0%, #6d4bea 100%);
  --user-bubble-shadow: 0 2px 8px rgba(90, 98, 232, 0.25);

  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;

  --border: rgba(255, 255, 255, 0.06);
  --border-strong: rgba(255, 255, 255, 0.10);

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.4);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --sidebar-w: 280px;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
*::selection { background: var(--accent-glow); }

html {
  background: var(--bg-0);
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter",
               system-ui, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-1);
  background: var(--bg-0);
  height: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  overscroll-behavior: none;
}

button {
  font-family: inherit;
  font-size: 14px;
  background: transparent;
  color: var(--text-1);
  border: none;
  padding: 8px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button:hover { background: var(--bg-hover); }
button:active { background: var(--bg-active); transform: scale(0.97); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button:disabled:hover { background: transparent; }

button.primary {
  background: var(--accent-2);
  color: white;
  font-weight: 500;
}
button.primary:hover { background: var(--accent); }
button.primary:active { background: var(--accent-2); }

button.icon {
  width: 36px;
  height: 36px;
  padding: 0;
  justify-content: center;
  border-radius: var(--radius);
}

input, textarea {
  font-family: inherit;
  font-size: 15px;
  color: var(--text-1);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  outline: none;
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
input::placeholder, textarea::placeholder { color: var(--text-3); }

/* ============ Auth ============ */
.overlay {
  position: fixed; inset: 0;
  background: radial-gradient(ellipse at center, var(--bg-1) 0%, var(--bg-0) 70%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
}
.auth-box {
  width: 100%;
  max-width: 380px;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 32px 28px;
  box-shadow: var(--shadow-lg);
}
.auth-logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent) 0%, #d946ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 26px;
  box-shadow: 0 8px 24px var(--accent-glow);
}
.auth-box h1 {
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 6px;
}
.auth-box .subtitle {
  text-align: center;
  color: var(--text-2);
  font-size: 14px;
  margin-bottom: 24px;
}
.auth-box input { margin-bottom: 14px; }
.auth-box .primary { width: 100%; justify-content: center; padding: 12px; }

/* ============ App layout ============ */
#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ============ Sidebar ============ */
#sidebar {
  background: var(--bg-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
}

.sidebar-header {
  padding: 14px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 8px;
}
.brand-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent) 0%, #d946ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: white;
  font-size: 14px;
  flex-shrink: 0;
}
.brand-name {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

#new-conv-btn {
  width: 100%;
  justify-content: center;
  padding: 9px 12px;
  font-weight: 500;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
}
#new-conv-btn:hover { background: var(--bg-3); }
#new-conv-btn .plus {
  font-size: 18px;
  line-height: 1;
  font-weight: 300;
  opacity: 0.9;
}

#conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.conv-list-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
}

.conv-item {
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
  transition: background 0.12s ease;
  min-height: 38px;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--bg-active); }

.conv-item-content {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.conv-item-title {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}
.conv-item-preview {
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
  line-height: 1.3;
}
.conv-item.active .conv-item-title { color: var(--text-1); }

.conv-item-del {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--bg-3);
  color: var(--text-2);
  font-size: 16px;
  line-height: 1;
}
.conv-item:hover .conv-item-del { display: flex; }
.conv-item-del:hover { background: var(--danger); color: white; }

.sidebar-footer {
  padding: 10px;
  border-top: 1px solid var(--border);
  padding-bottom: max(10px, var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sidebar-footer button {
  width: 100%;
  justify-content: flex-start;
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text-2);
}
.sidebar-footer button:hover { color: var(--text-1); }

/* ============ Chat ============ */
#chat {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-0);
  padding-right: var(--safe-right);
  position: relative;
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  padding-top: max(10px, var(--safe-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg-0);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  min-height: 56px;
}
#toggle-sidebar { display: none; }
#conv-title {
  flex: 1;
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}
.token-badge {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-3);
  padding: 3px 8px;
  background: var(--bg-2);
  border-radius: 99px;
  font-family: var(--font-mono);
  cursor: help;
  user-select: none;
}

/* ============ Messages ============ */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
#messages.empty {
  align-items: center;
  justify-content: center;
}

.empty-state {
  text-align: center;
  color: var(--text-3);
  padding: 40px 20px;
  max-width: 320px;
}
.empty-state-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent) 0%, #d946ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 28px;
  font-weight: 700;
  opacity: 0.85;
}
.empty-state-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-1);
  margin-bottom: 6px;
}
.empty-state-text {
  font-size: 13.5px;
  line-height: 1.5;
}

.msg-row {
  display: flex;
  width: 100%;
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.msg {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.5;
  font-size: 15px;
  position: relative;
}
.msg.user {
  background: var(--user-bubble);
  color: white;
  border-bottom-right-radius: 4px;
  box-shadow: var(--user-bubble-shadow);
}
.msg.assistant {
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.assistant.streaming {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.msg-tool {
  align-self: flex-start;
  max-width: 92%;
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text-3);
  font-size: 12px;
  font-family: var(--font-mono);
  padding: 7px 12px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 6px;
}
.msg-tool-icon {
  font-size: 13px;
  opacity: 0.7;
}
.msg-tool-name {
  color: var(--text-2);
  font-weight: 500;
}

.msg-error {
  align-self: stretch;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  color: #fca5a5;
  text-align: center;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: var(--radius);
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  background: var(--accent);
  vertical-align: -2px;
  margin-left: 2px;
  border-radius: 1px;
  animation: blink 0.9s ease-in-out infinite;
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.2; }
}

/* Markdown rendering */
.msg p { margin: 0.4em 0; }
.msg p:first-child { margin-top: 0; }
.msg p:last-child { margin-bottom: 0; }
.msg strong { font-weight: 600; }
.msg em { font-style: italic; }
.msg a { color: var(--accent); text-decoration: underline; text-decoration-color: rgba(124, 131, 247, 0.4); }
.msg a:hover { text-decoration-color: var(--accent); }

.msg code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: rgba(255, 255, 255, 0.08);
  padding: 0.12em 0.4em;
  border-radius: 4px;
  color: #ffd591;
}
.msg.user code {
  background: rgba(255, 255, 255, 0.18);
  color: #fde68a;
}
.msg pre {
  background: var(--bg-0);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
}
.msg pre code {
  background: transparent;
  padding: 0;
  color: var(--text-1);
  font-size: 13px;
}
.msg ul, .msg ol { padding-left: 22px; margin: 0.4em 0; }
.msg li { margin: 0.15em 0; }

/* ============ Composer ============ */
#composer-wrap {
  padding: 8px 12px;
  padding-bottom: max(12px, var(--safe-bottom));
  background: var(--bg-0);
  border-top: 1px solid var(--border);
}
#composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 6px 6px 6px 14px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
#composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
#input {
  flex: 1;
  resize: none;
  max-height: 180px;
  min-height: 26px;
  font-size: 15px;
  line-height: 1.45;
  padding: 8px 0;
  background: transparent;
  border: none;
  outline: none;
  box-shadow: none !important;
}
#input::placeholder { color: var(--text-3); }
#send-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  padding: 0;
  justify-content: center;
  background: var(--accent-2);
  color: white;
  border-radius: 10px;
  font-size: 16px;
  align-self: flex-end;
  margin-bottom: 1px;
}
#send-btn:hover { background: var(--accent); }
#send-btn:disabled {
  background: var(--bg-3);
  color: var(--text-3);
}
#send-btn.stop {
  background: var(--bg-3);
  color: var(--text-1);
}
#send-btn.stop:hover { background: var(--danger); }

/* ============ Backdrop (mobile) ============ */
#backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.2s ease;
}
#backdrop.visible {
  display: block;
  opacity: 1;
}

/* ============ Mobile ============ */
@media (max-width: 768px) {
  #app { grid-template-columns: 1fr; }

  #sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(80vw, 320px);
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1);
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.6);
    border-right: none;
  }
  #sidebar.open { transform: translateX(0); }
  #toggle-sidebar { display: inline-flex; }

  .msg { max-width: 85%; font-size: 15px; }
  #messages { padding: 16px 12px 4px; gap: 12px; }
  #composer-wrap { padding: 8px 10px; padding-bottom: max(10px, var(--safe-bottom)); }
}

/* ============ Scrollbar ============ */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-disabled); }

/* ============ Loading dots ============ */
.loading-dots {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.loading-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-3);
  animation: bounce 1.2s ease-in-out infinite;
}
.loading-dots span:nth-child(2) { animation-delay: 0.15s; }
.loading-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* iOS-specific: prevent zoom on input focus */
@supports (-webkit-touch-callout: none) {
  input, textarea { font-size: 16px; }
  #input { font-size: 16px; }
}
