:root {
  --bg: #0b0d1a;
  --panel: #12152a;
  --panel2: #1a1e3a;
  --border: rgba(255, 255, 255, 0.09);
  --text: #e9ebf7;
  --muted: #9aa0c3;
  --accent: #7c5cff;
  --accent2: #22d3ee;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background:
    radial-gradient(900px 500px at 85% -10%, rgba(124, 92, 255, 0.28), transparent),
    radial-gradient(700px 400px at 0% 110%, rgba(34, 211, 238, 0.14), transparent),
    var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
}
.brand { font-size: 20px; font-weight: 700; letter-spacing: 0.2px; display: flex; align-items: center; gap: 8px; }
.logo {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; font-size: 16px;
}
.btn-primary {
  border: 0; cursor: pointer; color: #fff; font-weight: 600; font-size: 15px;
  padding: 12px 14px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #5b8cff);
  transition: transform 0.1s, filter 0.2s;
}
.btn-primary:hover { filter: brightness(1.1); }
.btn-primary:active { transform: scale(0.98); }
.btn-ghost {
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  padding: 10px; border-radius: 10px; cursor: pointer; font-size: 13px;
}
.btn-ghost:hover { color: var(--text); background: var(--panel2); }

.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; margin: 4px -4px; padding: 0 4px; }
.chat-item {
  display: flex; align-items: center; gap: 6px; padding: 10px 10px; border-radius: 10px;
  cursor: pointer; color: var(--muted); font-size: 14px; border: 1px solid transparent;
}
.chat-item:hover { background: var(--panel2); color: var(--text); }
.chat-item.active { background: var(--panel2); color: var(--text); border-color: var(--border); }
.chat-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .del {
  border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px;
  padding: 2px 6px; border-radius: 6px; opacity: 0.7;
}
.chat-item .del:hover { color: #ff7b8a; opacity: 1; }

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

.topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border); backdrop-filter: blur(8px);
}
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  display: none; border: 1px solid var(--border); background: var(--panel); color: var(--text);
  width: 40px; height: 40px; border-radius: 10px; font-size: 18px; cursor: pointer;
}

.messages { flex: 1; overflow-y: auto; padding: 20px 16px; scroll-behavior: smooth; }
.msg { display: flex; margin: 0 auto 14px; max-width: 820px; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 88%; padding: 12px 16px; border-radius: 18px; line-height: 1.55; font-size: 15.5px;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble { background: linear-gradient(135deg, var(--accent), #5b8cff); color: #fff; border-bottom-right-radius: 6px; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.msg.error .bubble { border-color: rgba(255, 123, 138, 0.5); color: #ffb3bd; }
.bubble code { background: rgba(255, 255, 255, 0.1); padding: 1px 6px; border-radius: 6px; font-size: 0.92em; }
.bubble pre {
  white-space: pre; overflow-x: auto; background: #080a16; border: 1px solid var(--border);
  padding: 12px; border-radius: 12px; margin: 8px 0; max-width: 100%;
}
.bubble pre code { background: transparent; padding: 0; }

/* Typing dots */
.typing { display: inline-flex; gap: 5px; padding: 4px 0; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); } 40% { opacity: 1; transform: scale(1); } }

/* Welcome screen */
.welcome { max-width: 640px; margin: 8vh auto 0; text-align: center; padding: 0 8px; }
.welcome h1 {
  font-size: clamp(28px, 6vw, 40px); margin: 0 0 8px;
  background: linear-gradient(135deg, #fff, var(--accent2)); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.welcome p { color: var(--muted); margin: 0 0 22px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.chip {
  border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 10px 14px;
  border-radius: 999px; cursor: pointer; font-size: 14px;
}
.chip:hover { border-color: var(--accent); background: var(--panel2); }

/* Composer */
.composer {
  display: flex; align-items: flex-end; gap: 10px; max-width: 820px; width: 100%;
  margin: 0 auto; padding: 8px 16px 0;
}
.composer textarea {
  flex: 1; resize: none; max-height: 180px; padding: 13px 16px; font: inherit; font-size: 16px;
  color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; outline: none;
}
.composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124, 92, 255, 0.25); }
.send-btn {
  border: 0; cursor: pointer; color: #fff; font-weight: 600; font-size: 15px; padding: 0 20px; height: 48px;
  border-radius: 14px; background: linear-gradient(135deg, var(--accent), var(--accent2));
}
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.hint {
  text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}

.overlay { display: none; }

/* ---------- Phones / small screens ---------- */
@media (max-width: 800px) {
  .icon-btn { display: grid; place-items: center; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 82%; max-width: 320px; z-index: 30;
    transform: translateX(-100%); transition: transform 0.25s ease;
  }
  .sidebar.open { transform: none; }
  .overlay.show { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 20; }
  .bubble { max-width: 94%; }
}
