:root {
  --font: 'Playpen Sans', 'Comic Neue', 'Chalkboard SE', system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --accent: #F04E4E;
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --text: #0d0d0d;
  --muted: #5d5d5d;
  --faint: #8f8f8f;
  --border: rgba(0, 0, 0, .1);
  --hover: rgba(0, 0, 0, .05);
  --active: rgba(0, 0, 0, .08);
  --bubble: #f4f4f4;
  --composer: #ffffff;
  --composer-border: rgba(0, 0, 0, .12);
  --composer-shadow: 0 2px 14px rgba(0, 0, 0, .06);
  --menu: #ffffff;
  --shadow: 0 6px 28px rgba(0, 0, 0, .1), 0 0 0 1px rgba(0, 0, 0, .06);
  --code-bg: #f7f7f7;
  --code-head: #ededed;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --sidebar: #181818;
    --text: #ececec;
    --muted: #b4b4b4;
    --faint: #8b8b8b;
    --border: rgba(255, 255, 255, .1);
    --hover: rgba(255, 255, 255, .06);
    --active: rgba(255, 255, 255, .1);
    --bubble: #303030;
    --composer: #303030;
    --composer-border: rgba(255, 255, 255, .06);
    --composer-shadow: 0 2px 14px rgba(0, 0, 0, .25);
    --menu: #2f2f2f;
    --shadow: 0 10px 34px rgba(0, 0, 0, .45), 0 0 0 1px rgba(255, 255, 255, .08);
    --code-bg: #171717;
    --code-head: #2b2b2b;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

.icon-btn { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* ---------------------------------------------------------- layout */
.app { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: 260px; flex: none; display: flex; flex-direction: column;
  background: var(--sidebar);
  transition: margin-left .22s ease, transform .22s ease;
}
.app.sb-collapsed .sidebar { margin-left: -260px; }
.sb-top { display: flex; align-items: center; justify-content: space-between; height: 56px; padding: 0 8px 0 16px; }
.sb-logo img { height: 25px; display: block; }
.sb-new { display: flex; align-items: center; gap: 10px; margin: 4px 8px; padding: 8px 10px; border-radius: 10px; font-size: 15px; text-align: left; }
.sb-new:hover { background: var(--hover); }

.history { flex: 1; overflow-y: auto; padding: 4px 8px 16px; }
.hist-group h3 { font-size: 12.5px; font-weight: 600; color: var(--faint); margin: 18px 10px 4px; }
.hist-item {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 10px;
  text-decoration: none; font-size: 14.5px; white-space: nowrap;
}
.hist-item span { overflow: hidden; text-overflow: ellipsis; }
.hist-item:hover { background: var(--hover); }
.hist-item.active { background: var(--active); }
.hist-item.unread::after { content: ''; flex: none; width: 8px; height: 8px; margin-left: auto; border-radius: 50%; background: var(--accent); }
.hist-empty { color: var(--faint); font-size: 13.5px; padding: 14px 10px; line-height: 1.4; }

.sb-bottom { padding: 8px; border-top: 1px solid var(--border); }
.promo { display: flex; align-items: center; gap: 11px; width: 100%; padding: 9px 10px; border-radius: 12px; text-align: left; }
.promo:hover { background: var(--hover); }
.promo img { width: 32px; height: 32px; flex: none; }
.promo strong { display: block; font-size: 14.5px; line-height: 1.2; }
.promo span span { display: block; font-size: 12.5px; line-height: 1.25; color: var(--muted); }

.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }

.topbar { height: 56px; flex: none; display: flex; align-items: center; gap: 2px; padding: 0 12px 0 8px; }
.topbar .when-collapsed { display: none; }
.app.sb-collapsed .topbar .when-collapsed { display: grid; }

.model-picker { position: relative; }
.model-btn { display: flex; align-items: center; gap: 4px; padding: 5px 10px; border-radius: 10px; font-size: 18px; font-weight: 600; }
.model-btn .ver { color: var(--muted); font-weight: 400; }
.model-btn .chev { width: 17px; height: 17px; color: var(--muted); }
.model-btn:hover, .model-btn[aria-expanded="true"] { background: var(--hover); }

.menu {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; width: 320px; max-width: calc(100vw - 24px);
  background: var(--menu); border-radius: 18px; box-shadow: var(--shadow); padding: 6px;
}
.menu-label { font-size: 12.5px; color: var(--faint); padding: 8px 12px 4px; }
.menu-item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 9px 12px; border-radius: 12px; text-align: left; }
.menu-item:hover { background: var(--hover); }
.menu-item .name { font-size: 15.5px; line-height: 1.3; }
.menu-item .desc { font-size: 13px; line-height: 1.3; color: var(--muted); }
.menu-item .check { margin-left: auto; opacity: 0; }
.menu-item[aria-checked="true"] .check { opacity: 1; }

.human-badge {
  font-size: 12.5px; color: var(--accent); border: 1.5px solid color-mix(in srgb, var(--accent) 45%, transparent);
  padding: 2px 12px; border-radius: 999px; white-space: nowrap; transform: rotate(-1.5deg);
}

/* ---------------------------------------------------------- thread */
.thread { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.messages { max-width: 790px; margin: 0 auto; padding: 12px 24px 48px; display: flex; flex-direction: column; gap: 28px; }

.hero { display: none; text-align: center; }
.main.empty .hero { display: block; }
.main.empty .messages { display: none; }
.main.empty .thread { flex: 1 1 0; display: flex; align-items: flex-end; justify-content: center; padding: 0 20px 26px; }
.main.empty .composer-wrap { flex: 1 1 0; }
.hero-mark { width: 50px; height: 50px; margin-bottom: 8px; }
.hero h1 { font-size: 30px; font-weight: 600; line-height: 1.15; margin: 0; }
.tagline { margin: 8px 0 0; font-size: 16.5px; color: var(--muted); }

.msg.user { display: flex; justify-content: flex-end; }
.bubble {
  max-width: 72%; background: var(--bubble); padding: 9px 18px; border-radius: 22px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

.msg.assistant { display: flex; gap: 14px; }
.avatar {
  width: 32px; height: 32px; flex: none; margin-top: 1px; border-radius: 50%;
  display: grid; place-items: center; border: 1px solid var(--border); background: var(--bg);
}
.avatar img { width: 21px; height: 21px; display: block; }
.avatar.thinking img { animation: spin 1.4s linear infinite; }
.body { flex: 1; min-width: 0; padding-top: 3px; }
.thought { display: inline-flex; align-items: center; gap: 4px; font-size: 13.5px; color: var(--faint); margin-bottom: 4px; }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .75em; }
.md strong { font-weight: 650; }
.md ul, .md ol { margin: 0 0 .75em; padding-left: 1.5em; }
.md li { margin: .25em 0; }
.md li > p { margin: 0 0 .3em; }
.md h2, .md h3, .md h4, .md h5 { margin: 1.1em 0 .4em; line-height: 1.3; }
.md h2 { font-size: 1.5em; } .md h3 { font-size: 1.3em; } .md h4 { font-size: 1.12em; } .md h5 { font-size: 1em; }
.md a { color: var(--accent); text-underline-offset: 2px; }
.md code { font-family: var(--mono); font-size: .8em; background: var(--code-bg); border: 1px solid var(--border); padding: .1em .4em; border-radius: 6px; }
.md blockquote { margin: 0 0 .75em; padding-left: 1em; border-left: 3px solid var(--border); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.2em 0; }
.md del { opacity: .7; }
.codeblock { margin: 0 0 .9em; border-radius: 14px; overflow: hidden; background: var(--code-bg); border: 1px solid var(--border); }
.codehead { display: flex; justify-content: space-between; align-items: center; padding: 5px 14px; background: var(--code-head); font-size: 13px; color: var(--muted); }
.codehead button { font-size: 13px; color: var(--muted); }
.codehead button:hover { color: var(--text); }
.codeblock pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.codeblock code { background: none; border: 0; padding: 0; font-size: 13.5px; line-height: 1.55; }
.md.streaming > :last-child::after {
  content: ''; display: inline-block; width: .5em; height: .5em; margin-left: 5px; vertical-align: middle;
  border-radius: 50%; background: currentColor; animation: pulse 1s ease-in-out infinite;
}
.md.streaming > .codeblock:last-child::after, .md.streaming > ul:last-child::after, .md.streaming > ol:last-child::after { display: none; }

.actions { display: flex; gap: 2px; margin: 6px 0 0 -7px; opacity: 0; transition: opacity .15s; }
.msg.assistant:hover .actions, .msg.assistant.latest .actions, .actions:focus-within { opacity: 1; }
.act { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; color: var(--faint); }
.act svg.i { width: 17px; height: 17px; }
.act:hover { background: var(--hover); color: var(--text); }
.act.on { color: var(--text); }
.act.on svg.i { fill: currentColor; fill-opacity: .18; }

.shimmer {
  display: inline-block; font-size: 15.5px;
  background: linear-gradient(90deg, var(--faint) 0%, var(--faint) 38%, var(--text) 50%, var(--faint) 62%, var(--faint) 100%);
  background-size: 300% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 2.4s linear infinite;
}
.pending-note { font-size: 13.5px; color: var(--faint); margin-top: 3px; max-width: 540px; line-height: 1.4; }

.msg.error .body {
  color: #d23c3c; background: rgba(240, 78, 78, .07); border: 1px solid rgba(240, 78, 78, .3);
  border-radius: 14px; padding: 10px 14px; font-size: 15px;
}

/* ---------------------------------------------------------- composer */
.composer-wrap { flex: none; position: relative; padding: 0 16px calc(8px + env(safe-area-inset-bottom)); }
.composer {
  max-width: 768px; margin: 0 auto; display: flex; flex-direction: column;
  background: var(--composer); border: 1px solid var(--composer-border); border-radius: 28px;
  box-shadow: var(--composer-shadow); padding: 10px 10px 8px 12px; cursor: text;
}
.composer textarea {
  font: inherit; font-size: 16px; line-height: 1.55; color: inherit; background: none;
  border: 0; outline: none; resize: none; padding: 4px 8px 6px; max-height: 240px; min-height: 34px;
}
.composer textarea::placeholder { color: var(--faint); }
.composer-row { display: flex; align-items: center; gap: 6px; }
.composer-row .icon-btn { width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--border); }
.attach-tip { font-size: 13.5px; color: var(--muted); }
.send {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; transition: opacity .15s, transform .1s;
}
.send:disabled { opacity: .3; cursor: default; }
.send:not(:disabled):active { transform: scale(.93); }
.send .stop-ico { display: none; }
.send.stop { background: var(--text); color: var(--bg); opacity: 1; }
.send.stop .stop-ico { display: block; }
.send.stop .up-ico { display: none; }

.scroll-down {
  position: absolute; top: -50px; left: 50%; transform: translateX(-50%); z-index: 5;
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: var(--bg); border: 1px solid var(--border); box-shadow: var(--shadow); color: var(--text);
}
.scroll-down svg.i { width: 18px; height: 18px; }

.suggestions { display: none; max-width: 768px; margin: 16px auto 0; flex-wrap: wrap; justify-content: center; gap: 8px; }
.main.empty .suggestions { display: flex; }
.chip { display: flex; align-items: center; gap: 8px; padding: 7px 15px; border: 1px solid var(--border); border-radius: 999px; font-size: 14.5px; color: var(--muted); }
.chip:hover { background: var(--hover); color: var(--text); }
.chip svg.i { width: 18px; height: 18px; }

.disclaimer { text-align: center; font-size: 12.5px; color: var(--faint); margin: 8px 0 0; }
.disclaimer button { text-decoration: underline; text-underline-offset: 2px; font-size: inherit; }
.main.empty .disclaimer { position: absolute; left: 16px; right: 16px; bottom: calc(10px + env(safe-area-inset-bottom)); }

/* ---------------------------------------------------------- about dialog */
.about {
  width: min(460px, calc(100vw - 32px)); border: 0; border-radius: 22px; padding: 26px 28px 22px;
  background: var(--menu); color: var(--text); box-shadow: var(--shadow); font-size: 15px; line-height: 1.55;
}
.about::backdrop { background: rgba(0, 0, 0, .45); }
.about-logo { height: 34px; display: block; margin-bottom: 12px; }
.about-lead { font-size: 16.5px; margin: 0 0 16px; }
.about h2 { font-size: 15.5px; margin: 0 0 6px; }
.about p { margin: 0 0 10px; color: var(--muted); }
.about a { color: var(--accent); }
.about-close { margin-top: 8px; width: 100%; padding: 10px; border-radius: 999px; background: var(--text); color: var(--bg); font-weight: 600; font-size: 15px; }

/* ---------------------------------------------------------- motion */
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%, 100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg { animation: rise .25s ease-out; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}

/* ---------------------------------------------------------- mobile */
@media (max-width: 767px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 50; margin-left: 0 !important;
    transform: translateX(-100%); box-shadow: var(--shadow);
  }
  .app.sb-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .4); }
  .app.sb-open .scrim { display: block; }
  .topbar .when-collapsed { display: grid; }
  .human-badge { display: none; }
  .messages { padding: 8px 16px 32px; }
  .bubble { max-width: 86%; }
  .msg.assistant { gap: 10px; }
  .avatar { width: 28px; height: 28px; }
  .avatar img { width: 18px; height: 18px; }
  .hero h1 { font-size: 25px; }
  .tagline { font-size: 15px; }
  .chip:nth-child(n + 3) { display: none; }
  .composer-wrap { padding: 0 10px calc(6px + env(safe-area-inset-bottom)); }
}
