:root {
  --accent: #6d5ae6;
  --accent-soft: #efecff;
  --bg: #ffffff;
  --fg: #1b1b22;
  --muted: #6b6b7b;
  --line: #e7e7ef;
  --bubble: #f4f4f8;
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #9b8bff;
    --accent-soft: #262040;
    --bg: #14141a;
    --fg: #f2f2f6;
    --muted: #9a9aac;
    --line: #2a2a35;
    --bubble: #1f1f28;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

header {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 8px) 10px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
header h1 {
  flex: 1; margin: 0; font-size: 17px; font-weight: 600; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
button.icon {
  background: none; border: 0; color: var(--fg); padding: 8px; border-radius: 12px; cursor: pointer;
}
button.icon:active { background: var(--bubble); }

main {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 12px;
}
.msg { max-width: 86%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 6px; }
.msg.bot { align-self: flex-start; background: var(--bubble); border-bottom-left-radius: 6px; }
.msg.err { align-self: center; background: transparent; color: var(--muted); font-size: 14px; text-align: center; }
.msg code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; background: rgba(127,127,127,.18); padding: 1px 5px; border-radius: 6px; }
.msg pre { background: rgba(127,127,127,.14); padding: 10px 12px; border-radius: 12px; overflow-x: auto; margin: 8px 0; }
.msg pre code { background: none; padding: 0; }
.msg b { font-weight: 600; }
.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%, 60%, 100% { opacity: .25 } 30% { opacity: 1 } }

.empty { margin: auto; text-align: center; color: var(--muted); max-width: 320px; }
.empty .hi { font-size: 22px; font-weight: 600; color: var(--fg); }
.empty .note { font-size: 13px; margin-top: 22px; }

#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(var(--safe-b) + 8px);
  border-top: 1px solid var(--line); background: var(--bg);
}
#input {
  flex: 1; resize: none; max-height: 40vh;
  font: inherit; color: inherit;
  background: var(--bubble); border: 0; border-radius: 20px;
  padding: 10px 14px;
}
#input:focus { outline: 2px solid var(--accent); }
#send {
  border: 0; border-radius: 50%; width: 42px; height: 42px; flex: none;
  background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer;
}
#send:disabled { opacity: .4; }

#drawer {
  position: fixed; inset: 0 auto 0 0; width: min(78vw, 320px); z-index: 20;
  background: var(--bg); border-right: 1px solid var(--line);
  transform: translateX(-102%); transition: transform .22s ease;
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}
#drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
button.ghost { background: var(--accent-soft); color: var(--accent); border: 0; border-radius: 12px; padding: 7px 12px; font: inherit; font-size: 14px; cursor: pointer; }
#chat-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
#chat-list li { display: flex; align-items: center; gap: 6px; border-radius: 12px; }
#chat-list li.active { background: var(--accent-soft); }
#chat-list button.pick { flex: 1; text-align: left; background: none; border: 0; color: inherit; font: inherit; padding: 11px 10px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-list button.del { background: none; border: 0; color: var(--muted); padding: 8px; cursor: pointer; }
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; }

body.centered { display: grid; place-items: center; grid-template-rows: none; padding: 24px; }
.card { text-align: center; max-width: 320px; color: var(--muted); }
.card img { border-radius: 22px; }
.card h1 { color: var(--fg); font-size: 22px; margin: 12px 0 8px; }

#lock {
  position: fixed; inset: 0; z-index: 40; background: var(--bg);
  display: grid; place-items: center; padding: 24px calc(24px + env(safe-area-inset-left, 0px));
}
.lock-box { text-align: center; width: min(320px, 100%); }
.lock-box img { border-radius: 20px; }
.lock-title { font-size: 19px; font-weight: 600; margin: 14px 0 18px; }
.dots { display: flex; gap: 14px; justify-content: center; height: 16px; }
.dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--accent); display: block; }
.dots i.on { background: var(--accent); }
.lock-note { color: var(--muted); font-size: 14px; min-height: 20px; margin: 14px 0 6px; }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 10px; }
.keys button {
  height: 64px; border: 0; border-radius: 50%; background: var(--bubble); color: var(--fg);
  font: inherit; font-size: 24px; cursor: pointer; display: grid; place-items: center;
}
.keys button:active { background: var(--accent-soft); }
.keys button:disabled { opacity: .4; }

#install { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.35); display: grid; align-items: end; }
.sheet {
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 22px 20px calc(20px + var(--safe-b));
  text-align: center; animation: rise .22s ease;
}
@keyframes rise { from { transform: translateY(100%) } }
.sheet img { border-radius: 16px; }
.sheet-title { font-size: 18px; font-weight: 600; margin: 10px 0 6px; }
.sheet p { color: var(--muted); margin: 0 0 6px; }
.sheet .step { color: var(--fg); }
.sheet-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.sheet-buttons button { border: 0; border-radius: 14px; padding: 12px 20px; font: inherit; cursor: pointer; }
#install-go { background: var(--accent); color: #fff; }
.share-glyph { display: inline-block; vertical-align: -4px; width: 18px; height: 18px; }
