:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --side: #eef0f4;
  --text: #1d1f24;
  --muted: #6b7080;
  --border: #dde0e6;
  --accent: #6d4aff;
  --accent-text: #ffffff;
  --user-bubble: #ece8ff;
  --code-bg: #0d1117;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --panel: #1c1d22;
    --side: #111215;
    --text: #e8e9ed;
    --muted: #9095a3;
    --border: #2c2e35;
    --accent: #8b6dff;
    --user-bubble: #2a2446;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; cursor: pointer; }

/* Sidebar */
#sidebar {
  width: 260px; flex-shrink: 0; display: flex; flex-direction: column; gap: 8px;
  padding: 12px; background: var(--side); border-right: 1px solid var(--border);
}
#history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv {
  display: flex; align-items: center; gap: 4px; padding: 8px 10px; border-radius: 8px;
  color: var(--text); background: none; border: 0; text-align: left;
}
.conv:hover, .conv.active { background: var(--panel); }
.conv span { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv .del { visibility: hidden; border: 0; background: none; color: var(--muted); }
.conv:hover .del { visibility: visible; }
.side-head { display: flex; gap: 8px; }
.side-head #new-chat { flex: 1; }
#close-sidebar { display: none; }
.side-foot { display: flex; flex-direction: column; gap: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.profile {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border: 0; border-radius: 10px;
  background: none; color: var(--text); text-align: left;
}
.profile:hover { background: var(--panel); }
.avatar {
  flex-shrink: 0; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #3b82f6); color: #fff; font-weight: 700; font-size: 14px;
}
.who { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.who strong { font-size: 14px; font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.who small { font-size: 12px; color: var(--muted); }
.brand { font-size: 11px; color: var(--muted); padding: 0 8px; }
.icon { border: 0; background: none; color: var(--text); font-size: 20px; line-height: 1; padding: 6px 8px; border-radius: 8px; }
.icon:hover { background: var(--border); }

.logo {
  width: 48px; height: 48px; margin: 0 auto 16px; border-radius: 14px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #3b82f6); color: #fff; font-weight: 700; font-size: 24px;
}

.primary {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 10px 14px;
}

/* Main */
main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.topbar h1 { margin: 0; font-size: 18px; }
.topbar h1 span { color: var(--accent); }

#chat { flex: 1; overflow-y: auto; padding: 24px 16px; }
#messages, #welcome { max-width: 820px; margin: 0 auto; }
#welcome { text-align: center; padding-top: 16vh; }
#welcome h2 { margin: 0 0 6px; font-size: 26px; letter-spacing: -0.01em; }
#welcome p { color: var(--muted); margin: 0; }

/* Onboarding */
#onboarding {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 16px;
  background: rgba(10, 10, 15, 0.55); backdrop-filter: blur(4px);
}
#onboarding[hidden] { display: none; }
#onboarding .card {
  width: 100%; max-width: 520px; max-height: 100%; overflow-y: auto; text-align: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 28px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
#onboarding h2 { margin: 0 0 6px; }
#onboarding p { margin: 0 0 20px; color: var(--muted); }
.roles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.roles button {
  padding: 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); text-align: left;
}
.roles button:hover, .roles button.selected { border-color: var(--accent); background: var(--user-bubble); }

.msg { margin: 0 0 20px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .body {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 14px; max-width: 85%; white-space: pre-wrap;
}
.msg.assistant .body { overflow-wrap: break-word; }
.msg .body > :first-child { margin-top: 0; }
.msg .body > :last-child { margin-bottom: 0; }
.msg.error .body { color: #d64545; }
.status { color: var(--muted); font-style: italic; }

.body pre { position: relative; background: var(--code-bg); border-radius: 10px; padding: 14px; overflow-x: auto; }
.body pre code { background: none; padding: 0; color: #e6edf3; }
.body code { background: var(--border); padding: 1px 5px; border-radius: 5px; font-size: 0.92em; }
.copy {
  position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px;
  border: 1px solid #444; border-radius: 6px; background: #22262e; color: #cfd6e0;
}
.body table { border-collapse: collapse; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--border); padding: 6px 10px; min-width: 120px; vertical-align: top; }

/* Composer */
#composer {
  display: flex; gap: 8px; align-items: flex-end; width: 100%; max-width: 820px; margin: 0 auto; padding: 12px 16px 4px;
}
#input {
  flex: 1; min-width: 0; resize: none; max-height: 220px; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); font: inherit;
  font-size: 16px; /* below 16px, iOS Safari zooms the page on focus */
}
#input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.attach {
  flex-shrink: 0; width: 46px; height: 46px; display: grid; place-items: center; border-radius: 12px;
  border: 1px solid var(--border); background: var(--panel); color: var(--muted);
}
.attach:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); }
.attach:disabled { opacity: 0.5; cursor: default; }
#attach-preview {
  position: relative; width: fit-content; max-width: 820px; margin: 8px auto 0; padding: 0 16px;
  align-self: center;
}
#attach-preview[hidden] { display: none; }
#attach-preview img { display: block; height: 72px; border-radius: 10px; border: 1px solid var(--border); }
#attach-preview button {
  position: absolute; top: -8px; right: 8px; width: 22px; height: 22px; border-radius: 50%; border: 0;
  background: var(--text); color: var(--bg); font-size: 11px; line-height: 22px; padding: 0;
}
figure.gen { margin: 0 0 10px; width: fit-content; max-width: 100%; }
figure.gen img {
  display: block; max-width: min(100%, 512px); max-height: 512px; border-radius: 14px; border: 1px solid var(--border);
}
figure.gen a {
  display: inline-block; margin-top: 8px; padding: 6px 12px; border-radius: 8px; font-size: 13px;
  border: 1px solid var(--border); color: var(--text); text-decoration: none; background: var(--panel);
}
figure.gen a:hover { border-color: var(--accent); color: var(--accent); }
.msg.user .body .thumb { display: block; max-width: 240px; max-height: 240px; border-radius: 10px; margin-bottom: 6px; }
#stop { border: 1px solid var(--border); background: var(--panel); color: var(--text); border-radius: 10px; padding: 10px 14px; }
.disclaimer { text-align: center; font-size: 12px; color: var(--muted); margin: 4px 16px 10px; }

#backdrop { position: fixed; inset: 0; z-index: 9; background: rgba(0, 0, 0, 0.4); }

@media (max-width: 760px) {
  #sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 10; width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform .2s;
  }
  body.sidebar-open #sidebar { transform: none; }
  #close-sidebar { display: block; }
  .roles { grid-template-columns: 1fr; }
}
@media (min-width: 761px) {
  body.sidebar-closed #sidebar { display: none; }
}
