:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f3f5f8;
  --text: #1a1d21;
  --muted: #6b7480;
  --border: #dfe3ea;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --user-bg: #2f6fed;
  --user-ink: #ffffff;
  --ok: #1f9d55;
  --warn: #b7791f;
  --danger: #d64545;
  --shadow: 0 1px 2px rgba(20,30,50,.06), 0 4px 16px rgba(20,30,50,.06);
  --radius: 18px;
  font-size: 17px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171c23;
    --surface-2: #1f262f;
    --text: #e7eaee;
    --muted: #9aa4b0;
    --border: #2a323c;
    --accent: #5b8cff;
    --accent-ink: #0b1220;
    --user-bg: #3a63c0;
    --user-ink: #ffffff;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.5;
  overscroll-behavior-y: none;
}
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---- centered card screens (login/setup) ---- */
.screen { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 22px; box-shadow: var(--shadow); padding: 28px 24px;
}
.card h1 { font-size: 24px; margin: 0 0 4px; }
.card .sub { color: var(--muted); margin: 0 0 20px; font-size: 15px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 13px 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2); outline: none; transition: border-color .15s;
}
.field input:focus { border-color: var(--accent); }
.btn {
  width: 100%; padding: 14px; border: none; border-radius: 12px; background: var(--accent);
  color: var(--accent-ink); font-weight: 600; font-size: 16px; transition: filter .15s, opacity .15s;
}
.btn:active { filter: brightness(.94); }
.btn[disabled] { opacity: .5; cursor: default; }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.ghost { background: transparent; color: var(--accent); }
.err { color: var(--danger); font-size: 14px; margin-top: 10px; min-height: 18px; }
.logo { width: 56px; height: 56px; border-radius: 16px; display: block; margin: 0 auto 14px; }

/* ---- app shell ---- */
.app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar .title { font-weight: 600; font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iconbtn {
  width: 42px; height: 42px; border-radius: 12px; border: none; background: transparent; color: var(--text);
  display: grid; place-items: center; font-size: 22px; line-height: 1;
}
.iconbtn:active { background: var(--surface-2); }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 16px 12px 8px; -webkit-overflow-scrolling: touch; }
.msgwrap { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; }
.bubble {
  max-width: 86%; padding: 11px 15px; border-radius: var(--radius); box-shadow: var(--shadow);
  word-wrap: break-word; overflow-wrap: anywhere;
}
.bubble.user { align-self: flex-end; background: var(--user-bg); color: var(--user-ink); border-bottom-right-radius: 6px; }
.bubble.bot { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.bubble.bot p { margin: 0 0 8px; } .bubble.bot p:last-child { margin-bottom: 0; }
.bubble.bot ul, .bubble.bot ol { margin: 6px 0; padding-left: 22px; }
.bubble.bot code { background: var(--surface-2); padding: 1px 5px; border-radius: 6px; font-size: .9em; }
.bubble.bot pre { background: var(--surface-2); padding: 10px 12px; border-radius: 10px; overflow-x: auto; margin: 8px 0; }
.bubble.bot pre code { background: none; padding: 0; }
.bubble.bot a { color: var(--accent); }
.bubble.user a { color: #fff; text-decoration: underline; }

.hello { text-align: center; color: var(--muted); margin: 12vh 8px 0; }
.hello .big { font-size: 40px; margin-bottom: 10px; }
.hello h2 { color: var(--text); font-weight: 600; font-size: 20px; margin: 0 0 6px; }
.suggests { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.suggests button {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 9px 14px; border-radius: 999px; font-size: 15px; box-shadow: var(--shadow);
}

/* status chips + typing */
.status { align-self: flex-start; color: var(--muted); font-size: 14px; display: flex; align-items: center; gap: 8px; padding: 2px 6px; }
.dots { display: inline-flex; gap: 3px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: b 1.2s infinite; }
.dots i:nth-child(2){ animation-delay:.2s } .dots i:nth-child(3){ animation-delay:.4s }
@keyframes b { 0%,60%,100%{ opacity:.3; transform: translateY(0) } 30%{ opacity:1; transform: translateY(-3px) } }

/* confirmation card */
.confirm {
  align-self: stretch; max-width: 760px; margin: 2px auto; width: 100%;
  background: var(--surface); border: 1.5px solid var(--accent); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden;
}
.confirm .head { padding: 13px 16px 4px; font-weight: 700; }
.confirm .body { padding: 0 16px 10px; color: var(--text); }
.confirm .why { color: var(--muted); font-size: 14px; margin-top: 6px; }
.confirm details { margin-top: 8px; }
.confirm summary { color: var(--accent); font-size: 14px; cursor: pointer; }
.confirm pre { background: var(--surface-2); padding: 8px 10px; border-radius: 8px; overflow-x: auto; font-size: 13px; margin: 6px 0 0; }
.confirm .actions { display: flex; gap: 10px; padding: 4px 16px 16px; }
.confirm .actions button { flex: 1; padding: 13px; border-radius: 12px; border: none; font-weight: 600; font-size: 16px; }
.confirm .yes { background: var(--accent); color: var(--accent-ink); }
.confirm .no { background: var(--surface-2); color: var(--text); border: 1px solid var(--border) !important; }
.confirm.resolved { border-color: var(--border); opacity: .7; }
.confirm .resolved-note { padding: 0 16px 14px; color: var(--muted); font-size: 14px; }

/* composer */
.composer {
  padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer .row { max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 12px 15px; border: 1px solid var(--border);
  border-radius: 22px; background: var(--surface-2); outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send { width: 46px; height: 46px; flex: 0 0 auto; border-radius: 50%; border: none; background: var(--accent); color: #fff; font-size: 20px; display: grid; place-items: center; }
.send[disabled] { opacity: .4; }
.send.stop { background: var(--danger); }
.banner { max-width: 760px; margin: 0 auto 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 9px 12px; font-size: 14px; color: var(--muted); }

/* drawer */
.drawer-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s; }
.drawer-bg.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: min(84vw, 320px); background: var(--surface);
  border-right: 1px solid var(--border); z-index: 41; transform: translateX(-100%); transition: transform .22s;
  display: flex; flex-direction: column; padding-top: env(safe-area-inset-top);
}
.drawer.open { transform: none; }
.drawer .dh { padding: 16px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); }
.drawer .dh .me { flex: 1; } .drawer .dh .me b { display: block; } .drawer .dh .me span { color: var(--muted); font-size: 13px; }
.newchat { margin: 12px; padding: 12px; border-radius: 12px; border: 1px dashed var(--border); background: var(--surface-2); color: var(--accent); font-weight: 600; text-align: center; }
.chatlist { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chatlist .item { padding: 11px 12px; border-radius: 10px; display: flex; align-items: center; gap: 8px; }
.chatlist .item:active { background: var(--surface-2); }
.chatlist .item.active { background: var(--surface-2); }
.chatlist .item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
.chatlist .item .del { color: var(--muted); font-size: 18px; padding: 4px 8px; border: none; background: none; }
.dfoot { border-top: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.dfoot a, .dfoot button { text-align: left; background: none; border: none; color: var(--text); padding: 11px 12px; border-radius: 10px; font-size: 15px; text-decoration: none; display: block; }
.dfoot a:active, .dfoot button:active { background: var(--surface-2); }
