:root, html.dark {
  --bg: #0c0d10;
  --bg2: #14161c;
  --bg3: #1c1f27;
  --line: #2a2e38;
  --text: #e8eaef;
  --muted: #8b93a7;
  --accent: #f0b429;
  --accent2: #c99216;
  --ok: #3dd68c;
  --warn: #f5a524;
  --bad: #f76c6c;
  --user: #2a3344;
  --perm-bg: #2a2110;
  --perm-line: #6b5318;
  --safe: env(safe-area-inset-bottom, 0px);
  --top: env(safe-area-inset-top, 0px);
}
html.light {
  --bg: #f4f1ea;
  --bg2: #fffdf8;
  --bg3: #ece6da;
  --line: #d9d0c0;
  --text: #1c1914;
  --muted: #6b6458;
  --accent: #c99216;
  --accent2: #a67a10;
  --ok: #1a8a52;
  --warn: #b86a00;
  --bad: #c0392b;
  --user: #e8dfcc;
  --perm-bg: #f7ecd4;
  --perm-line: #d4b56a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 20px/1.4 system-ui, -apple-system, "Segoe UI", Inter, sans-serif;
  overscroll-behavior-x: none; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-body { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.auth-card { width: min(400px, 100%); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px; }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 22px; }
.mark { width: 42px; height: 42px; border-radius: 12px; background: var(--accent); color: #1a1403;
  display: grid; place-items: center; font-weight: 800; font-size: 20px; }
.brand h1 { margin: 0; font-size: 20px; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.auth-card input { width: 100%; margin-top: 6px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px; }
.auth-card button { width: 100%; margin-top: 8px; background: var(--accent); color: #1a1403;
  border: 0; border-radius: 10px; padding: 12px; font-weight: 700; }
.err { color: var(--bad); font-size: 13px; margin: 0 0 12px; }

.app { display: grid; grid-template-columns: 280px 1fr; height: 100dvh; }
.sidebar { background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  min-height: 0; padding-top: var(--top); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 8px; }
.side-head h2 { margin: 0; font-size: 18px; }
.icon-btn, .btn { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; color: var(--text); }
#photoBtn { flex-shrink: 0; font-weight: 700; }
#photoBtn.off { pointer-events: none; opacity: .5; }
.sr-file {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.btn.primary { background: var(--accent); color: #1a1403; border-color: transparent; font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.model-sel { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 8px; max-width: 42vw; color: var(--text); }
.chats { overflow: auto; flex: 1; padding: 6px; }
.chat-item { display: block; width: 100%; text-align: left; background: transparent; border: 0;
  border-radius: 12px; padding: 12px 12px 12px 12px; margin-bottom: 4px; position: relative; }
.chat-item.active, .chat-item:hover { background: var(--bg3); }
.chat-item.unread { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.chat-item.unread .t { font-weight: 800; }
.chat-item .dot { position: absolute; left: 8px; top: 20px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent); }
.chat-item.unread { padding-left: 22px; }
.chat-item .t { font-weight: 600; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .s { color: var(--muted); font-size: 14px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .m { color: var(--muted); font-size: 13px; margin-top: 2px; }
.think-bar { height: 3px; background: var(--bg3); overflow: hidden; flex-shrink: 0; }
.think-bar.hidden { display: none; }
.think-bar.slim { height: 3px; margin-top: 8px; border-radius: 99px; }
.think-bar i { display: block; height: 100%; width: 40%; background: var(--accent);
  animation: think 1.1s ease-in-out infinite; }
@keyframes think {
  0% { transform: translateX(-120%); }
  100% { transform: translateX(280%); }
}
.side-foot { padding: 10px 12px calc(12px + var(--safe)); border-top: 1px solid var(--line);
  display: flex; gap: 8px; }
.side-foot .who { flex: 1; color: var(--muted); font-size: 12px; align-self: center; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: calc(10px + var(--top)) 12px 10px;
  border-bottom: 1px solid var(--line); background: var(--bg); }
.topbar .meta { flex: 1; min-width: 0; }
.topbar .meta h1 { margin: 0; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .meta p { margin: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.usage { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.usage.hidden { display: none; }
.ubar { flex: 0 0 72px; height: 6px; background: var(--bg3); border-radius: 99px; overflow: hidden; }
.ubar i { display: block; height: 100%; width: 0; background: var(--ok); }
.ubar.mid i { background: var(--warn); }
.ubar.hi i { background: var(--bad); }
#usageText { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu { display: none; }

.stream { flex: 1; overflow: auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 14px; }
.stream > .btn { align-self: center; }
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 280px; }
.bubble { max-width: min(760px, 100%); }
.bubble.user { align-self: flex-end; background: var(--user); border-radius: 14px 14px 4px 14px; padding: 10px 12px;
  margin-top: 6px; }
.bubble.assistant { align-self: stretch; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 14px 14px 14px 4px; padding: 12px 14px; }
.thought { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  color: var(--muted); font-size: 18px; white-space: pre-wrap; }
.thought summary { cursor: pointer; color: var(--muted); }
.md { word-break: break-word; }
.md p { margin: 0.25em 0; }
.md p:first-child { margin-top: 0; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0.35em 0; padding-left: 1.3em; }
.md li { margin: 0.15em 0; }
.md li p { margin: 0; }
.md h1, .md h2, .md h3 { margin: 0.5em 0 0.2em; }
.md pre { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 10px;
  overflow: auto; font-size: 15px; }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 17px; }
.md :not(pre) > code { background: var(--bg3); padding: 1px 5px; border-radius: 5px; }
.tool { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; font-size: 13px; }
.tool .row { display: flex; justify-content: space-between; gap: 8px; }
.tool .kind { color: var(--accent); font-weight: 600; }
.tool .st { color: var(--muted); }
.tool .st.run { color: var(--warn); }
.tool .st.ok { color: var(--ok); }
.tool .st.fail { color: var(--bad); }
.tool pre { margin: 6px 0 0; white-space: pre-wrap; color: var(--muted); max-height: 180px; overflow: auto; }
.sys { color: var(--muted); font-size: 12px; text-align: center; }
.live { display: none; text-align: center; font-size: 13px; color: var(--warn);
  padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--bg2); }
.live.on { display: block; }
.perm { background: var(--perm-bg); border: 1px solid var(--perm-line); border-radius: 12px; padding: 10px; }
.perm .acts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.composer { border-top: 1px solid var(--line); padding: 10px 12px calc(12px + var(--safe)); background: var(--bg2); }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 44px; max-height: 160px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 0 8px; }
.chips:empty { display: none; }
.chip { display: flex; align-items: center; gap: 8px; background: var(--bg3); border: 1px solid var(--line);
  border-radius: 10px; padding: 6px 8px; font-size: 14px; max-width: 100%; }
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.chip .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; }
.chip .x { background: transparent; border: 0; color: var(--muted); padding: 0 4px; font-size: 18px; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.atts img { max-width: 180px; max-height: 180px; border-radius: 10px; display: block; }
.atts .filechip { background: var(--bg3); border-radius: 8px; padding: 6px 8px; font-size: 14px; }
.yolo { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; padding: 0 0 8px; }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: end center; z-index: 20; }
.modal { width: min(480px, 100%); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 16px 16px 0 0; padding: 16px 16px calc(16px + var(--safe)); }
.modal h3 { margin: 0 0 12px; }
.modal label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.modal input, .modal select { width: 100%; margin-top: 6px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px; }
.modal .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 15;
    transform: translateX(-105%); transition: transform .2s ease; }
  .sidebar.open { transform: none; box-shadow: 8px 0 40px rgba(0,0,0,.4); }
  .menu { display: inline-flex; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 14; }
  .app.home .sidebar { position: static; width: 100%; transform: none; box-shadow: none; }
  .app.home .main, .app.home .scrim { display: none; }
  .app.swiping .sidebar {
    transform: none;
    width: 100%;
    z-index: 1;
  }
  .app.swiping .main {
    position: relative;
    z-index: 12;
    background: var(--bg);
    box-shadow: -16px 0 40px rgba(0,0,0,.28);
    will-change: transform;
  }
  .app.swipe-snap .main { transition: transform .2s ease; }
}
