:root {
  --bg: #ffffff;
  --bg-elev: #f5f5f7;
  --bg-sidebar: #fbfbfd;
  --text: #1d1d1f;
  --text-dim: #6e6e73;
  --sep: rgba(0,0,0,0.10);
  --accent: #0071e3;
  --accent-press: #0060c0;
  --bubble-user: #0071e3;
  --bubble-user-text: #ffffff;
  --bubble-ai: #f0f0f2;
  --code-bg: #1d1d1f;
  --code-text: #f5f5f7;
  --danger: #ff3b30;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 8px 30px rgba(0,0,0,0.06);
  --header-blur: rgba(255,255,255,0.72);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elev: #1c1c1e;
    --bg-sidebar: #0d0d0f;
    --text: #f5f5f7;
    --text-dim: #98989d;
    --sep: rgba(255,255,255,0.12);
    --accent: #0a84ff;
    --accent-press: #409cff;
    --bubble-user: #0a84ff;
    --bubble-user-text: #ffffff;
    --bubble-ai: #1c1c1e;
    --code-bg: #000000;
    --code-text: #f5f5f7;
    --header-blur: rgba(20,20,22,0.72);
    --shadow: 0 1px 3px rgba(0,0,0,0.4), 0 8px 30px rgba(0,0,0,0.5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  font-size: 17px; line-height: 1.47;
}
body { overflow: hidden; }
#app { height: 100dvh; }
button, input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
[hidden] { display: none !important; }

/* ---------------- Login ---------------- */
.login {
  height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: calc(24px + var(--safe-top)) 24px calc(24px + var(--safe-bottom));
  background: var(--bg);
}
.login-card {
  width: 100%; max-width: 340px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  animation: rise .5s cubic-bezier(.2,.8,.2,1);
}
@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
.brand-logo {
  width: 84px; height: 84px; border-radius: 22px;
  background: linear-gradient(160deg, #3a3a3c, #000);
  display: grid; place-items: center; box-shadow: var(--shadow);
}
.brand-logo svg { width: 52px; height: 52px; }
.login h1 { font-size: 30px; font-weight: 600; letter-spacing: -0.02em; margin: 2px 0 0; }
.login p { margin: 0; color: var(--text-dim); font-size: 15px; }
.code-input {
  width: 100%; text-align: center; padding: 15px 16px; border-radius: 14px;
  background: var(--bg-elev); border: 1px solid var(--sep); outline: none;
  font-size: 20px; letter-spacing: 0.12em; transition: border-color .2s;
}
.code-input:focus { border-color: var(--accent); }
.btn-primary {
  width: 100%; padding: 14px 16px; border-radius: 14px; font-weight: 600;
  background: var(--accent); color: #fff; transition: background .15s, transform .05s;
}
.btn-primary:active { background: var(--accent-press); transform: scale(.99); }
.btn-primary:disabled { opacity: .5; }
.err { color: var(--danger); font-size: 14px; min-height: 18px; }

/* ---------------- Main layout ---------------- */
#main { height: 100dvh; display: flex; }
#sidebar {
  width: 288px; flex-shrink: 0; background: var(--bg-sidebar);
  border-right: 1px solid var(--sep); display: flex; flex-direction: column;
  padding-top: var(--safe-top); padding-left: var(--safe-left);
}
.side-head {
  padding: 14px 14px 10px; display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.side-title { font-weight: 700; font-size: 20px; letter-spacing: -.02em; }
.new-chat {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; border-radius: 12px;
  background: var(--bg-elev); font-size: 15px; font-weight: 500; transition: background .15s;
}
.new-chat:active { background: var(--sep); }
.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px calc(12px + var(--safe-bottom)); }
.chat-item {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 12px;
  cursor: pointer; color: var(--text); position: relative;
}
.chat-item:hover { background: var(--bg-elev); }
.chat-item.active { background: var(--bg-elev); }
.chat-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.chat-item .del {
  opacity: 0; color: var(--text-dim); padding: 2px 6px; border-radius: 8px; font-size: 13px; transition: opacity .15s;
}
.chat-item:hover .del, .chat-item.active .del { opacity: .7; }
.chat-item .del:active { color: var(--danger); }

/* ---------------- Chat pane ---------------- */
#chatPane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + var(--safe-top)) calc(14px + var(--safe-right)) 8px calc(14px + var(--safe-left));
  background: var(--header-blur); backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--sep);
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  color: var(--text); transition: background .15s;
}
.icon-btn:active { background: var(--bg-elev); }
.icon-btn svg { width: 22px; height: 22px; }
#menuBtn { display: none; }
.pill-select {
  appearance: none; -webkit-appearance: none; padding: 7px 30px 7px 13px; border-radius: 11px;
  background: var(--bg-elev); border: 1px solid var(--sep); font-size: 14px; font-weight: 500;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='3'><path d='M6 9l6 6 6-6'/></svg>");
  background-repeat: no-repeat; background-position: right 10px center;
}
.spacer { flex: 1; }

#messages { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.messages-inner { max-width: 760px; margin: 0 auto; padding: 22px 18px 8px; }
.empty {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; color: var(--text-dim); text-align: center; padding: 40px;
}
.empty .brand-logo { width: 64px; height: 64px; border-radius: 18px; }
.empty .brand-logo svg { width: 40px; height: 40px; }
.empty h2 { color: var(--text); font-weight: 600; letter-spacing: -.02em; margin: 4px 0 0; }

.msg { display: flex; margin: 14px 0; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 86%; padding: 11px 15px; border-radius: 20px; overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--bubble-user); color: var(--bubble-user-text); border-bottom-right-radius: 7px; }
.msg.ai .bubble { background: transparent; color: var(--text); max-width: 100%; padding-left: 0; padding-right: 0; }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1,.bubble h2,.bubble h3 { letter-spacing: -.01em; line-height: 1.3; margin: 14px 0 8px; }
.bubble h1 { font-size: 22px; } .bubble h2 { font-size: 20px; } .bubble h3 { font-size: 18px; }
.bubble ul,.bubble ol { margin: 6px 0 10px; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble code.inline {
  background: var(--bg-elev); padding: 1px 6px; border-radius: 6px; font-size: .9em;
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}
.msg.user .bubble code.inline { background: rgba(255,255,255,.22); }
.bubble blockquote { border-left: 3px solid var(--sep); margin: 8px 0; padding: 2px 0 2px 12px; color: var(--text-dim); }

.codeblock { margin: 12px 0; border-radius: 14px; overflow: hidden; background: var(--code-bg); box-shadow: var(--shadow); }
.codeblock .cb-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: rgba(255,255,255,0.06); color: #c7c7cc; font-size: 12.5px;
}
.codeblock .cb-name { flex: 1; font-family: "SF Mono", ui-monospace, Menlo, monospace; }
.codeblock .cb-btn { color: #c7c7cc; padding: 3px 9px; border-radius: 8px; font-size: 12.5px; background: rgba(255,255,255,.08); }
.codeblock .cb-btn:active { background: rgba(255,255,255,.18); }
.codeblock pre { margin: 0; padding: 13px 14px; overflow-x: auto; }
.codeblock code {
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 13.5px; line-height: 1.55;
  color: var(--code-text); white-space: pre;
}

.thinking {
  margin: 4px 0 10px; border-radius: 12px; background: var(--bg-elev); border: 1px solid var(--sep); overflow: hidden;
}
.thinking summary {
  list-style: none; cursor: pointer; padding: 8px 13px; font-size: 13.5px; color: var(--text-dim);
  display: flex; align-items: center; gap: 7px; user-select: none;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking[open] summary { border-bottom: 1px solid var(--sep); }
.thinking .think-body {
  padding: 10px 13px; font-size: 14px; color: var(--text-dim); white-space: pre-wrap;
  max-height: 320px; overflow-y: auto;
}
.think-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.1s infinite; }
@keyframes pulse { 0%,100%{opacity:.35;} 50%{opacity:1;} }

.att-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 2px; }
.att-chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 7px 11px; border-radius: 12px;
  background: var(--bg-elev); border: 1px solid var(--sep); font-size: 13.5px; max-width: 220px;
}
.msg.user .att-chip { background: rgba(255,255,255,.18); border-color: transparent; color: #fff; }
.att-chip img { width: 30px; height: 30px; border-radius: 7px; object-fit: cover; }
.att-chip .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.att-chip a { color: inherit; text-decoration: none; }

.typing { display: inline-flex; gap: 4px; padding: 6px 2px; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); animation: bounce 1.2s infinite; }
.typing span:nth-child(2){ animation-delay: .15s; } .typing span:nth-child(3){ animation-delay: .3s; }
@keyframes bounce { 0%,60%,100%{ transform: translateY(0); opacity:.4;} 30%{ transform: translateY(-5px); opacity:1;} }

/* ---------------- Composer ---------------- */
#composer {
  border-top: 1px solid var(--sep); background: var(--header-blur);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  padding: 8px calc(12px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(12px + var(--safe-left));
}
.composer-wrap { max-width: 760px; margin: 0 auto; }
#attachRow { display: flex; flex-wrap: wrap; gap: 8px; padding: 2px 2px 6px; }
#attachRow:empty { display: none; }
.pending-att {
  display: inline-flex; align-items: center; gap: 7px; padding: 5px 9px; border-radius: 11px;
  background: var(--bg-elev); border: 1px solid var(--sep); font-size: 13px; max-width: 200px;
}
.pending-att img { width: 26px; height: 26px; border-radius: 6px; object-fit: cover; }
.pending-att .x { color: var(--text-dim); font-weight: 700; padding: 0 2px; }
.pending-att.uploading { opacity: .6; }
.composer-inner {
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--bg-elev); border: 1px solid var(--sep); border-radius: 22px; padding: 5px 6px 5px 8px;
}
#input {
  flex: 1; resize: none; border: none; outline: none; background: transparent;
  max-height: 180px; padding: 8px 4px; line-height: 1.4;
}
.send-btn {
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; flex-shrink: 0; transition: background .15s, opacity .15s;
}
.send-btn:disabled { opacity: .35; }
.send-btn svg { width: 20px; height: 20px; }
.tool-btn { width: 36px; height: 36px; border-radius: 50%; color: var(--text-dim); display: grid; place-items: center; flex-shrink: 0; }
.tool-btn:active { background: var(--sep); }
.tool-btn svg { width: 22px; height: 22px; }

/* ---------------- Mobile ---------------- */
#scrim { display: none; }
@media (max-width: 760px) {
  #menuBtn { display: grid; }
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 50; width: 82%; max-width: 320px;
    transform: translateX(-102%); transition: transform .28s cubic-bezier(.2,.8,.2,1); box-shadow: var(--shadow);
  }
  #main.side-open #sidebar { transform: none; }
  #scrim { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .28s; }
  #main.side-open #scrim { opacity: 1; pointer-events: auto; }
  .bubble { max-width: 90%; }
  .messages-inner { padding: 16px 14px 8px; }
}

/* ---------------- Agent status / activity ---------------- */
.agent-status { display: flex; align-items: center; gap: 8px; color: var(--text-dim); font-size: 14px; margin: 2px 0 8px; }
.activity { margin: 2px 0 10px; border-radius: 12px; background: var(--bg-elev); border: 1px solid var(--sep); overflow: hidden; }
.activity summary { list-style: none; cursor: pointer; padding: 8px 13px; font-size: 13.5px; color: var(--text-dim); user-select: none; }
.activity summary::-webkit-details-marker { display: none; }
.activity[open] summary { border-bottom: 1px solid var(--sep); }
.activity-log { padding: 8px 12px; max-height: 260px; overflow-y: auto; }
.act-line { font-family: "SF Mono", ui-monospace, Menlo, monospace; font-size: 12.5px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 2px 0; }

/* ---------------- Artifacts (image / html) ---------------- */
.artifact { margin: 12px 0; border-radius: 14px; overflow: hidden; background: var(--bg-elev); border: 1px solid var(--sep); box-shadow: var(--shadow); }
.art-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--bg-sidebar); color: var(--text-dim); font-size: 12.5px; border-bottom: 1px solid var(--sep); }
.art-name { flex: 1; font-weight: 500; }
.art-head .cb-btn { color: var(--text); background: var(--bg-elev); border: 1px solid var(--sep); padding: 3px 10px; border-radius: 8px; font-size: 12.5px; }
.art-head .cb-btn:active { background: var(--sep); }
.art-body { display: flex; justify-content: center; align-items: center; padding: 14px; background:
  repeating-conic-gradient(var(--bg) 0% 25%, transparent 0% 50%) 50% / 20px 20px; }
@media (prefers-color-scheme: dark) { .art-body { background: #0d0d0f; } }
.artifact-img { max-width: 100%; max-height: 60vh; border-radius: 8px; }
.artifact-frame { width: 100%; height: 440px; border: 0; background: #fff; display: block; }
.art-code { margin: 0; padding: 12px 14px; overflow-x: auto; background: var(--code-bg); }
.art-code code { font-family: "SF Mono", ui-monospace, Menlo, monospace; font-size: 13px; color: var(--code-text); white-space: pre; }

/* ---------------- Math ---------------- */
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 2px; }
.katex { font-size: 1.02em; }

/* ---------------- Fullscreen / standalone hardening ---------------- */
html { height: 100%; }
@supports (-webkit-touch-callout: none) {
  #app, .login, #main { min-height: -webkit-fill-available; }
}

/* ============================================================
   Phase-1 refresh: compact UI, tables, animations, polish
   ============================================================ */

/* Slightly smaller, tighter overall */
html, body { font-size: 15.5px; line-height: 1.45; }
.messages-inner { max-width: 720px; padding: 18px 16px 8px; }
.msg { margin: 11px 0; }
.bubble { font-size: 15px; padding: 10px 14px; border-radius: 18px; }
.msg.ai .bubble { font-size: 15px; }
.side-title { font-size: 18px; }
.chat-item .title { font-size: 14px; }
.pill-select { font-size: 13px; padding: 6px 26px 6px 11px; }
.icon-btn { width: 36px; height: 36px; }
.icon-btn svg { width: 20px; height: 20px; }

/* Composer: sit lower, tighter, seamless (blur only, no heavy bar) */
#composer { padding: 6px calc(10px + var(--safe-right)) calc(max(6px, var(--safe-bottom))) calc(10px + var(--safe-left)); border-top: none; background: linear-gradient(to top, var(--bg) 62%, transparent); backdrop-filter: none; -webkit-backdrop-filter: none; }
.composer-inner { border-radius: 24px; box-shadow: var(--shadow); }
#topbar { border-bottom: none; background: linear-gradient(to bottom, var(--bg) 60%, transparent); backdrop-filter: none; -webkit-backdrop-filter: none; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 12px 0; border: 1px solid var(--sep); border-radius: 12px; }
.table-wrap table { border-collapse: collapse; width: 100%; font-size: 14px; }
.table-wrap th, .table-wrap td { padding: 8px 12px; border-bottom: 1px solid var(--sep); text-align: left; white-space: nowrap; }
.table-wrap th { background: var(--bg-elev); font-weight: 600; }
.table-wrap tr:last-child td { border-bottom: none; }

/* Math display sizing */
.katex-display { margin: 10px 0; }

/* ---- Animations ---- */
@keyframes msgIn { from { opacity: 0; transform: translateY(10px) scale(.99); } to { opacity: 1; transform: none; } }
.msg { animation: msgIn .32s cubic-bezier(.22,.8,.24,1) both; }
@keyframes artIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.artifact, .codeblock, .table-wrap { animation: artIn .35s cubic-bezier(.22,.8,.24,1) both; }
.send-btn { transition: transform .18s cubic-bezier(.2,.9,.3,1.4), background .15s, opacity .15s; }
.send-btn:not(:disabled):active { transform: scale(.88); }
.send-btn:not(:disabled):hover { transform: translateY(-1px); }
.new-chat, .chat-item, .pill-select, .tool-btn { transition: background .18s, transform .12s, box-shadow .18s; }
.new-chat:active, .tool-btn:active { transform: scale(.94); }
.chat-item { transform-origin: left center; }
.chat-item:active { transform: scale(.985); }
.brand-logo { transition: transform .4s cubic-bezier(.2,.8,.2,1); }
.brand-logo:hover { transform: rotate(-4deg) scale(1.04); }
@keyframes popIn { 0% { opacity: 0; transform: scale(.8); } 60% { transform: scale(1.05); } 100% { opacity: 1; transform: scale(1); } }
.artifact-img { animation: popIn .5s cubic-bezier(.22,.8,.24,1) both; }
.agent-status { animation: msgIn .25s ease both; }

/* Buttons feel a touch more premium */
.btn-primary { box-shadow: 0 6px 18px rgba(0,113,227,.28); }
.send-btn { box-shadow: 0 4px 14px rgba(0,113,227,.35); }

/* Reduce top/side padding a bit on mobile */
@media (max-width: 760px) {
  .messages-inner { padding: 14px 12px 8px; }
  .bubble { font-size: 15px; }
}
