:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #11161d;
  --muted: #6b7480;
  --border: #e3e6ea;
  --accent: #0a84ff;
  --accent-text: #ffffff;
  --user-bubble: #0a84ff;
  --user-text: #ffffff;
  --code-bg: #eef1f4;
  --glow: #4cc9f0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --surface: #131a22;
    --text: #e8edf2;
    --muted: #8a96a3;
    --border: #222c37;
    --accent: #4cc9f0;
    --accent-text: #04121a;
    --user-bubble: #1f6feb;
    --user-text: #ffffff;
    --code-bg: #1a232d;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

/* ---- layout ---- */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 820px;
  margin: 0 auto;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}
.icon-btn:active { background: var(--border); }

/* ---- messages ---- */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 8px;
  -webkit-overflow-scrolling: touch;
}
.msg { display: flex; margin: 0 0 14px; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
}
.msg.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.bubble p { margin: 0 0 8px; }
.bubble p:last-child, .bubble ul:last-child, .bubble ol:last-child, .bubble pre:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 8px; padding-left: 22px; }
.bubble h3 { margin: 4px 0 6px; font-size: 16px; }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 14px; }
.bubble pre { background: var(--code-bg); padding: 10px; border-radius: 10px; overflow-x: auto; margin: 0 0 8px; }
.bubble pre code { padding: 0; background: none; }
.bubble a { color: var(--accent); }
.status { color: var(--muted); font-size: 14px; font-style: italic; }
.error-msg { color: #e5484d; font-size: 14px; }
.typing span {
  display: inline-block; width: 7px; height: 7px; margin-right: 4px;
  border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.empty { height: 100%; display: grid; place-content: center; justify-items: center; color: var(--muted); }
.orb {
  width: 64px; height: 64px; border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, var(--glow) 0 18%, transparent 19% 34%, var(--glow) 35% 41%, transparent 42%);
  box-shadow: 0 0 28px color-mix(in srgb, var(--glow) 45%, transparent);
}

/* ---- composer ---- */
#composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  border-top: 1px solid var(--border);
  background: var(--bg);
}
#input {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* 16px stops iOS from zooming in */
  line-height: 1.4;
  outline: none;
}
#input:focus { border-color: var(--accent); }
.send-btn {
  display: grid; place-items: center;
  width: 42px; height: 42px; flex: none;
  border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  cursor: pointer;
}
.send-btn:disabled { opacity: .4; cursor: default; }

/* ---- drawer ---- */
#scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .4); z-index: 10; }
#drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 11;
  width: min(84vw, 320px);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform .2s ease;
}
#drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 16px; font-weight: 600; }
#convList { flex: 1; overflow-y: auto; margin: 0; padding: 0 8px; list-style: none; }
#convList li { display: flex; align-items: center; border-radius: 10px; }
#convList li.active { background: var(--border); }
#convList .conv-open {
  flex: 1; min-width: 0; padding: 10px; border: 0; background: none; text-align: left; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#convList .conv-del { opacity: .5; }
.connections { padding: 10px 16px; border-top: 1px solid var(--border); font-size: 14px; }
.connections-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }
.connection { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 32px; }
.connection a, .connection button { color: var(--accent); text-decoration: none; }
.muted { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-foot { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.link-btn { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 4px; }

/* ---- login ---- */
.login {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 24px;
  max-width: 360px; margin: 0 auto;
}
.login h1 { margin: 4px 0 8px; font-size: 26px; }
.login input, .login button {
  width: 100%; padding: 13px 16px; border-radius: 12px; font: inherit; font-size: 16px;
}
.login input { border: 1px solid var(--border); background: var(--surface); color: var(--text); outline: none; }
.login input:focus { border-color: var(--accent); }
.login button { border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600; cursor: pointer; }
.login button:disabled { opacity: .5; }
.error { color: #e5484d; min-height: 1.5em; margin: 0; text-align: center; }
