:root {
  color-scheme: light dark;
  --bg: #faf9f5; --side: #f5f4ed; --input: #ffffff; --bubble: #f0eee6; --hover: #ebe8dd;
  --line: rgba(31, 30, 29, .15); --line-2: rgba(31, 30, 29, .08);
  --text: #141413; --text-2: #3d3d3a; --muted: #73726c;
  --accent: #d97757; --send: #c6613f; --accent-soft: #f4dfd5;
  --ok: #5a8a4a; --bad: #bf4d43; --code: #f5f4ed; --add: #e4efdc; --del: #f8e1dc;
  --shadow: 0 .25rem 1.25rem rgba(0, 0, 0, .035), 0 0 0 .5px rgba(31, 30, 29, .15);
  --serif: "Source Serif 4", "Tiempos Text", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624; --side: #1f1e1d; --input: #30302e; --bubble: #141413; --hover: #3a3936;
    --line: rgba(222, 220, 209, .15); --line-2: rgba(222, 220, 209, .08);
    --text: #faf9f5; --text-2: #c2c0b6; --muted: #9c9a92; --accent-soft: #4a3128;
    --code: #1f1e1d; --add: #253622; --del: #44231f;
    --shadow: 0 .25rem 1.25rem rgba(0, 0, 0, .25), 0 0 0 .5px rgba(222, 220, 209, .15);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 16px/1.5 var(--sans);
  overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round;
  stroke-linejoin: round; flex: none; }
#app { display: flex; flex-direction: column; height: 100dvh; }

/* header */
header { display: flex; align-items: center; gap: 4px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--bg); position: relative; z-index: 2; }
header::after { content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 14px;
  background: linear-gradient(var(--bg), transparent); pointer-events: none; }
.ib { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; color: var(--text-2); }
.ib:active { background: var(--hover); }
#head { flex: 1; min-width: 0; text-align: center; line-height: 1.2; }
#title { font-size: 15px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#sub:empty { display: none; }
#net { position: absolute; left: 40px; top: max(14px, calc(env(safe-area-inset-top) + 8px)); width: 7px; height: 7px;
  border-radius: 50%; background: var(--bad); }
#net.on { display: none; }

/* drawer */
#drawer { position: fixed; inset: 0; z-index: 20; pointer-events: none; }
#drawer .shade { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); opacity: 0; transition: opacity .2s; }
#drawer nav { position: absolute; inset: 0 auto 0 0; width: min(300px, 84vw); background: var(--side);
  border-right: .5px solid var(--line); display: flex; flex-direction: column; transform: translateX(-100%);
  transition: transform .22s ease; padding: 8px; padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: max(8px, env(safe-area-inset-bottom)); }
#drawer.open { pointer-events: auto; }
#drawer.open .shade { opacity: 1; }
#drawer.open nav { transform: none; }
.brand { display: flex; align-items: center; gap: 8px; padding: 6px 10px 12px; font: 500 21px/1 var(--serif); }
.brand svg { width: 22px; height: 22px; color: var(--accent); }
.nrow { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; font-size: 15px;
  width: 100%; text-align: left; }
.nrow:active { background: var(--hover); }
.plus { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; display: grid;
  place-items: center; }
.plus svg { width: 16px; height: 16px; stroke-width: 2.4; }
#drawer h3 { font-size: 12px; color: var(--muted); font-weight: 500; margin: 18px 10px 4px; }
#list { overflow-y: auto; flex: 1; margin: 0 -4px; padding: 0 4px; }
.sess { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; font-size: 14px; }
.sess.cur { background: var(--hover); }
.sess .t { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess .x { color: var(--muted); padding: 2px; opacity: .7; }
.sess .x svg { width: 16px; height: 16px; }
.busy { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite; flex: none; }
.me { display: flex; align-items: center; gap: 10px; padding: 10px; border-top: .5px solid var(--line); margin-top: 6px; }
.ava { width: 32px; height: 32px; border-radius: 50%; background: var(--text-2); color: var(--bg); display: grid;
  place-items: center; font-weight: 600; font-size: 14px; overflow: hidden; flex: none; }
.ava img { width: 100%; height: 100%; }
.me small { display: block; color: var(--muted); font-size: 12px; }

/* chat */
main { flex: 1; overflow-y: auto; scrollbar-width: thin; padding: 12px 16px 24px; scroll-behavior: auto; }
.msg { max-width: 720px; margin: 0 auto 16px; }
.user { display: flex; justify-content: flex-end; }
.user div { background: var(--bubble); padding: 10px 16px; border-radius: 18px; max-width: 86%; white-space: pre-wrap;
  word-break: break-word; font-size: 16px; }
.md { font: 17px/1.65 var(--serif); color: var(--text); word-break: break-word; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3 { font: 600 1.15em/1.3 var(--serif); margin: 20px 0 8px; }
.md ul, .md ol { padding-left: 24px; margin: 0 0 12px; }
.md li { margin: 4px 0; }
.md :not(pre) > code { font: .85em var(--mono); background: var(--code); border: .5px solid var(--line);
  padding: 1px 5px; border-radius: 5px; color: var(--send); }
.md blockquote { border-left: 3px solid var(--line); margin: 0 0 12px; padding-left: 12px; color: var(--text-2); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font: 14px var(--sans); margin-bottom: 12px; }
.md td, .md th { border: .5px solid var(--line); padding: 6px 10px; }
.md a { color: var(--send); }
.md hr { border: 0; border-top: .5px solid var(--line); }
.cb { border: .5px solid var(--line); border-radius: 10px; overflow: hidden; margin: 0 0 12px; background: var(--code); }
.cb .h { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font: 12px var(--sans);
  color: var(--muted); border-bottom: .5px solid var(--line-2); }
.cb .h button { display: flex; gap: 4px; align-items: center; font-size: 12px; }
.cb .h svg { width: 14px; height: 14px; }
.cb pre { margin: 0; padding: 12px; overflow-x: auto; font: 13px/1.5 var(--mono); }
.cb code { background: none !important; padding: 0 !important; }
.acts { display: flex; gap: 2px; margin-top: -8px; color: var(--muted); }
.acts button { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; }
.acts button:active { background: var(--hover); }
.acts svg { width: 17px; height: 17px; }
.acts .cost { font-size: 12px; align-self: center; margin-left: 6px; }

/* tool rows */
.row-x { border: .5px solid var(--line); border-radius: 12px; background: var(--bg); overflow: hidden; }
.row-x + .row-x { margin-top: -10px; }
.row-x summary { list-style: none; display: flex; gap: 10px; align-items: center; padding: 9px 12px; cursor: pointer;
  font-size: 14px; color: var(--text-2); }
.row-x summary::-webkit-details-marker { display: none; }
.row-x summary svg { width: 16px; height: 16px; color: var(--muted); }
.row-x summary .s { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-x summary .s code { font: 12.5px var(--mono); color: var(--muted); margin-left: 4px; }
.row-x .chev { transition: transform .15s; }
.row-x[open] .chev { transform: rotate(180deg); }
.row-x .body { padding: 0 12px 10px; font-size: 13px; }
.row-x pre { margin: 6px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--code); font: 12px/1.5 var(--mono);
  white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow-y: auto; }
pre.add { background: var(--add); } pre.del { background: var(--del); } pre.err { color: var(--bad); }
.spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex: none; }
.think summary { font-style: italic; }
.think .body { color: var(--muted); white-space: pre-wrap; font: 14px/1.55 var(--serif); }

/* permission card */
.perm { border: .5px solid var(--line); border-radius: 14px; background: var(--input); padding: 14px; box-shadow: var(--shadow); }
.perm .q { font-weight: 500; margin-bottom: 8px; display: flex; gap: 8px; align-items: center; }
.perm .q svg { color: var(--accent); }
.perm pre { margin: 0 0 6px; padding: 8px 10px; border-radius: 8px; background: var(--code); font: 12px/1.5 var(--mono);
  white-space: pre-wrap; word-break: break-word; max-height: 220px; overflow-y: auto; }
.perm .opts { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.btn { padding: 10px 14px; border-radius: 10px; border: .5px solid var(--line); background: var(--bg); font-size: 15px;
  text-align: left; }
.btn:active { background: var(--hover); }
.btn.main { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.pick { border-color: var(--accent); background: var(--accent-soft); }
.btn small { display: block; color: var(--muted); font-size: 12px; }
.note { color: var(--muted); font-size: 13px; text-align: center; }
.err { color: var(--bad); }
.typing { display: flex; align-items: center; height: 28px; }
.typing svg { width: 26px; height: 26px; color: var(--accent); animation: breathe 1.6s ease-in-out infinite; }

/* empty state */
.hello { min-height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center;
  text-align: center; gap: 18px; padding-bottom: 40px; }
.hello .greet { display: flex; align-items: center; gap: 10px; font: 400 30px/1.2 var(--serif); color: var(--text-2); }
.hello .greet svg { width: 32px; height: 32px; color: var(--accent); }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px;
  border: .5px solid var(--line); background: var(--input); font-size: 14px; color: var(--text-2); max-width: 90%; }
.chip span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip svg { width: 16px; height: 16px; }

/* composer */
footer { padding: 0 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); position: relative; }
#down { position: absolute; left: 50%; top: -48px; transform: translateX(-50%); width: 36px; height: 36px;
  border-radius: 50%; background: var(--input); box-shadow: var(--shadow); display: none; place-items: center;
  color: var(--text-2); }
#down.show { display: grid; }
.composer { max-width: 720px; margin: 0 auto; background: var(--input); border-radius: 20px; box-shadow: var(--shadow);
  padding: 12px 10px 8px 14px; }
textarea { width: 100%; border: 0; outline: 0; resize: none; background: none; color: var(--text); font: 16px/1.5 var(--sans);
  max-height: 38vh; padding: 0 4px 0 0; display: block; }
textarea::placeholder { color: var(--muted); }
.bar { display: flex; align-items: center; gap: 4px; margin-top: 8px; }
.bar .ib { width: 34px; height: 34px; border: .5px solid var(--line); border-radius: 9px; }
.bar .grow { flex: 1; }
#auto { display: none; align-items: center; gap: 4px; height: 34px; padding: 0 10px; border-radius: 9px;
  background: var(--accent-soft); color: var(--send); font-size: 13px; font-weight: 500; }
#auto.on { display: flex; }
#auto svg { width: 15px; height: 15px; }
.modelw { position: relative; display: flex; align-items: center; gap: 2px; height: 34px; padding: 0 8px;
  border-radius: 9px; font-size: 14px; color: var(--text-2); }
.modelw svg { width: 15px; height: 15px; }
.modelw select { position: absolute; inset: 0; opacity: 0; font-size: 16px; }
#send { width: 34px; height: 34px; border-radius: 9px; background: var(--send); color: #fff; display: grid; place-items: center; }
#send svg { stroke-width: 2.2; }
#send:disabled { opacity: .45; }
#send.stop { background: var(--text); color: var(--bg); }

/* bottom sheet */
#sheet { position: fixed; inset: 0; z-index: 30; pointer-events: none; }
#sheet .shade { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); opacity: 0; transition: opacity .2s; }
#sheet .panel { position: absolute; left: 0; right: 0; bottom: 0; background: var(--input); border-radius: 18px 18px 0 0;
  max-height: 75vh; overflow-y: auto; transform: translateY(100%); transition: transform .22s ease;
  padding: 8px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); }
#sheet.open { pointer-events: auto; }
#sheet.open .shade { opacity: 1; }
#sheet.open .panel { transform: none; }
.grab { width: 36px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 12px; }
.panel h4 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 500; }
.opt { display: flex; align-items: center; gap: 12px; padding: 12px 4px; width: 100%; text-align: left;
  border-bottom: .5px solid var(--line-2); font-size: 15px; }
.opt:last-child { border-bottom: 0; }
.opt .t { flex: 1; min-width: 0; }
.opt small { display: block; color: var(--muted); font-size: 13px; }
.opt .ck { color: var(--accent); }
.sw { width: 44px; height: 26px; border-radius: 13px; background: var(--line); position: relative; flex: none;
  transition: background .15s; }
.sw::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); }
.sw.on { background: var(--accent); }
.sw.on::after { transform: translateX(18px); }

/* login */
.login { height: 100dvh; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 22px;
  padding: 24px; text-align: center; }
.login h1 { font: 400 34px/1.2 var(--serif); margin: 0; }
.login p { color: var(--muted); margin: 0; }
.login a { display: inline-flex; gap: 10px; align-items: center; padding: 12px 22px; border-radius: 12px;
  background: var(--text); color: var(--bg); text-decoration: none; font-weight: 500; }

@keyframes pulse { 50% { opacity: .35; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes breathe { 0%, 100% { transform: scale(.85) rotate(0); opacity: .7; } 50% { transform: scale(1.05) rotate(45deg); opacity: 1; } }

/* highlight.js tokens */
.hljs-keyword, .hljs-built_in, .hljs-selector-tag { color: #a3478a; }
.hljs-string, .hljs-attr { color: #3c7a3a; }
.hljs-number, .hljs-literal { color: #b5612f; }
.hljs-comment { color: var(--muted); font-style: italic; }
.hljs-title, .hljs-function .hljs-title { color: #2f6aa8; }
@media (prefers-color-scheme: dark) {
  .hljs-keyword, .hljs-built_in, .hljs-selector-tag { color: #d78cc6; }
  .hljs-string, .hljs-attr { color: #9fcf86; }
  .hljs-number, .hljs-literal { color: #e9a273; }
  .hljs-title, .hljs-function .hljs-title { color: #84b6ea; }
}
