:root {
  --bg: #faf9f5; --bg-side: #f3f1ea; --bg-elev: #ffffff; --bg-hover: #ebe8de; --bg-active: #e4e0d3;
  --bg-user: #efece3; --bg-code: #f5f3ec;
  --text: #29261b; --muted: #77736a; --faint: #a6a297;
  --border: #e3dfd3; --border-strong: #d4cfbf;
  --accent: #c96442; --accent-hover: #b55636; --accent-soft: #f6e5dc;
  --danger: #b3261e; --ok: #3d7a4a;
  --shadow: 0 1px 2px rgba(40,30,10,.06), 0 4px 16px rgba(40,30,10,.06);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Georgia, "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624; --bg-side: #1f1e1d; --bg-elev: #30302e; --bg-hover: #353532; --bg-active: #3d3d3a;
    --bg-user: #141413; --bg-code: #1c1c1b;
    --text: #ecebe6; --muted: #a3a197; --faint: #75736b;
    --border: #3a3a37; --border-strong: #4a4945;
    --accent: #d97757; --accent-hover: #e48a6b; --accent-soft: #3b2d26;
    --danger: #f2817a; --ok: #7fc28d;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { width: 18px; height: 18px; flex: none; }
.mono { font-family: var(--mono); font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.grow { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > input { flex: 1; min-width: 0; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--bg-elev); cursor: pointer; text-decoration: none;
  color: var(--text); white-space: nowrap; }
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.danger-text { color: var(--danger); border-color: transparent; background: none; }
.icon-btn { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; border: 0;
  background: none; cursor: pointer; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.on { color: var(--accent); }
input, select, textarea { background: var(--bg-elev); border: 1px solid var(--border-strong); border-radius: 10px;
  padding: 8px 11px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
select { padding: 5px 8px; cursor: pointer; }
.err { color: var(--danger); font-size: 13.5px; min-height: 1em; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card h1 { font-family: var(--serif); font-weight: 500; margin: 0 0 8px; }
.logo-big { color: var(--accent); }
.logo-big svg { width: 44px; height: 44px; }

/* layout */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { width: 288px; flex: none; background: var(--bg-side); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 12px 10px; gap: 8px; min-height: 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 6px; }
.brand { display: flex; gap: 8px; align-items: center; font-family: var(--serif); font-size: 18px; }
.brand svg { color: var(--accent); width: 22px; height: 22px; }
.new-chat { justify-content: flex-start; background: none; border-color: transparent; color: var(--accent); font-weight: 600; }
.new-chat:hover { background: var(--bg-hover); }
.side-section { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 0;
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.search { padding: 6px 10px; font-size: 13.5px; background: var(--bg); }
.projects { flex: 1; overflow-y: auto; min-height: 0; margin: 0 -4px; padding: 0 4px; }
.proj { margin-bottom: 4px; }
.proj-head { display: flex; align-items: center; gap: 4px; padding: 5px 4px 5px 6px; border-radius: 8px; cursor: pointer;
  user-select: none; }
.proj-head:hover { background: var(--bg-hover); }
.proj-head .chev { width: 14px; height: 14px; color: var(--faint); transition: transform .15s; }
.proj.open .chev { transform: rotate(90deg); }
.proj-head .name { flex: 1; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj-head .icon-btn { width: 26px; height: 26px; opacity: 0; }
.proj-head:hover .icon-btn, .proj-head .icon-btn:focus { opacity: 1; }
.proj-head .missing { color: var(--danger); font-size: 11px; }
.chats { display: none; padding-left: 14px; }
.proj.open .chats { display: block; }
.chat-item { display: flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 8px; cursor: pointer;
  font-size: 14px; color: var(--text); }
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
.chats .empty { color: var(--faint); font-size: 13px; padding: 4px 8px; }
@keyframes pulse { 50% { opacity: .3; } }
.side-foot { display: flex; justify-content: space-between; align-items: center; padding: 6px 6px 0;
  border-top: 1px solid var(--border); color: var(--muted); font-size: 13.5px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.top { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border);
  min-height: 56px; position: relative; }
.top-title { flex: 1; min-width: 0; }
.chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
.chat-path { font-family: var(--mono); font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.top-controls { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.top-controls select { font-size: 13px; max-width: 170px; background: var(--bg); }
.menu { position: absolute; right: 56px; top: 50px; background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow); padding: 5px; z-index: 30; min-width: 200px; display: flex; flex-direction: column; }
.menu button { text-align: left; padding: 8px 12px; border: 0; background: none; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--bg-hover); }
.menu .danger { color: var(--danger); }

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 12px; scroll-behavior: auto; }
.msg { max-width: 780px; margin: 0 auto 22px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.bubble { background: var(--bg-user); border-radius: 16px; padding: 10px 16px; max-width: 85%;
  white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user .atts { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; max-width: 85%; }
.msg.user .atts img { max-height: 160px; max-width: 220px; border-radius: 10px; cursor: zoom-in; border: 1px solid var(--border); object-fit: cover; }
.assistant-body { font-size: 15.5px; }
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol { margin: .6em 0; }
.md h1, .md h2, .md h3 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 1.1em 0 .4em; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; }
.md code { font-family: var(--mono); font-size: .86em; background: var(--bg-code); border: 1px solid var(--border);
  padding: .08em .35em; border-radius: 5px; }
.md code.path { cursor: pointer; color: var(--accent); border-color: var(--accent-soft); }
.md code.path:hover { text-decoration: underline; }
.md pre { position: relative; background: var(--bg-code); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; overflow-x: auto; font-size: 13.5px; line-height: 1.5; }
.md pre code { background: none; border: 0; padding: 0; font-size: inherit; }
.md pre .hljs { background: none; padding: 0; }
.md .copy { position: absolute; top: 6px; right: 6px; opacity: 0; background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 6px; font-size: 12px; padding: 2px 8px; cursor: pointer; color: var(--muted); }
.md pre:hover .copy { opacity: 1; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.md blockquote { margin: .6em 0; padding-left: 14px; border-left: 3px solid var(--border-strong); color: var(--muted); }
.md img { max-width: 100%; border-radius: 8px; }
.md a { word-break: break-all; }

.tool { border: 1px solid var(--border); border-radius: 10px; margin: 8px 0; background: var(--bg-elev); font-size: 13.5px; }
.tool > summary { list-style: none; display: flex; align-items: center; gap: 8px; padding: 7px 10px; cursor: pointer;
  color: var(--muted); user-select: none; }
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary .tn { font-weight: 600; color: var(--text); }
.tool > summary .td { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12.5px; }
.tool > summary .st { font-size: 11.5px; }
.tool.err > summary .st { color: var(--danger); }
.tool.pending > summary .st::after { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  border: 2px solid var(--accent); border-right-color: transparent; animation: spin .8s linear infinite; vertical-align: -1px; }
@keyframes spin { to { transform: rotate(360deg); } }
.tool .tb { border-top: 1px solid var(--border); padding: 8px 10px; display: flex; flex-direction: column; gap: 8px; }
.tool .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--faint); }
.tool .path-lbl { text-transform: none; letter-spacing: 0; font-family: var(--mono); }
.tool pre { margin: 0; background: var(--bg-code); border-radius: 8px; padding: 8px 10px; overflow: auto; max-height: 360px;
  font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; overflow-wrap: anywhere; }
.diff-del { background: rgba(220,60,40,.12); }
.diff-add { background: rgba(40,160,70,.13); }
.thinking { color: var(--muted); font-style: italic; border-left: 2px solid var(--border-strong); padding-left: 12px; margin: 8px 0;
  white-space: pre-wrap; font-size: 14px; max-height: 240px; overflow: auto; }
.thinking-sum { color: var(--muted); font-size: 13px; cursor: pointer; }

.file-cards { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.fcard { display: flex; align-items: center; gap: 10px; border: 1px solid var(--border); background: var(--bg-elev);
  border-radius: 12px; padding: 8px 10px 8px 12px; max-width: 100%; min-width: 220px; cursor: pointer; }
.fcard:hover { border-color: var(--border-strong); }
.fcard .ic { width: 34px; height: 34px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; flex: none; overflow: hidden; }
.fcard .ic img { width: 100%; height: 100%; object-fit: cover; }
.fcard .fi { flex: 1; min-width: 0; }
.fcard .fn { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fcard .fp { font-size: 11.5px; color: var(--muted); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.turn-meta { display: flex; gap: 12px; color: var(--faint); font-size: 12px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.turn-meta .icon-btn { width: 26px; height: 26px; }
.turn-error { color: var(--danger); background: rgba(200,40,30,.07); border: 1px solid rgba(200,40,30,.25);
  border-radius: 10px; padding: 8px 12px; margin-top: 8px; white-space: pre-wrap; font-size: 13.5px; font-family: var(--mono); }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
.spark-live { color: var(--accent); animation: spin 3s linear infinite; }

/* welcome */
.welcome { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; gap: 4px; }
.welcome h2 { font-family: var(--serif); font-weight: 500; font-size: 30px; margin: 8px 0 0; }
.welcome p { color: var(--muted); max-width: 480px; }

/* composer */
.composer { padding: 6px 16px calc(14px + env(safe-area-inset-bottom, 0px)); position: relative; }
.composer-box { max-width: 780px; margin: 0 auto; background: var(--bg-elev); border: 1px solid var(--border-strong);
  border-radius: 18px; box-shadow: var(--shadow); padding: 10px 10px 8px 14px; }
.composer-box:focus-within { border-color: var(--faint); }
.composer textarea { width: 100%; border: 0; background: none; resize: none; padding: 4px 0; max-height: 300px; outline: none; }
.composer-row { display: flex; align-items: center; gap: 6px; }
.composer-row .hint { flex: 1; font-size: 12px; color: var(--faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.send-btn { width: 34px; height: 34px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; display: grid;
  place-items: center; cursor: pointer; }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); }
.attach-list { display: flex; flex-wrap: wrap; gap: 6px; }
.attach-list:not(:empty) { margin-bottom: 8px; }
.chip { display: flex; align-items: center; gap: 6px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 4px 4px 4px 8px; font-size: 13px; max-width: 240px; }
.chip img { width: 28px; height: 28px; object-fit: cover; border-radius: 6px; }
.chip .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .icon-btn { width: 22px; height: 22px; }
.chip.loading { opacity: .6; }
.drop-overlay { display: none; position: fixed; inset: 0; background: rgba(201,100,66,.12); border: 3px dashed var(--accent);
  z-index: 50; place-items: center; font-size: 20px; color: var(--accent); pointer-events: none; }
.dragging .drop-overlay { display: grid; }

/* files panel */
.files-panel { width: 340px; flex: none; border-left: 1px solid var(--border); background: var(--bg-side); display: flex; flex-direction: column; min-height: 0; }
.fp-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 6px 14px; }
.fp-actions { display: flex; gap: 2px; align-items: center; }
.fp-crumbs { padding: 0 14px 8px; font-family: var(--mono); font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.fp-crumbs span { cursor: pointer; }
.fp-crumbs span:hover { color: var(--accent); }
.fp-list { flex: 1; overflow-y: auto; padding: 0 6px 10px; }
.fp-item { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 8px; cursor: pointer; font-size: 13.5px; }
.fp-item:hover { background: var(--bg-hover); }
.fp-item svg { color: var(--muted); width: 16px; height: 16px; }
.fp-item.dir svg { color: var(--accent); }
.fp-item .n { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fp-item .s { color: var(--faint); font-size: 11.5px; }
.fp-item .icon-btn { width: 24px; height: 24px; opacity: 0; }
.fp-item:hover .icon-btn { opacity: 1; }

/* modals */
.modal { position: fixed; inset: 0; background: rgba(20,15,5,.45); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-card { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(620px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; overflow: hidden; }
.form-card { padding: 16px 18px; gap: 12px; overflow-y: auto; }
.small-card { width: min(420px, 100%); }
.form-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13.5px; font-weight: 500; }
.form-card label.check { flex-direction: row; align-items: center; font-weight: 400; }
.form-card label.check input { width: auto; }
.form-card textarea { resize: vertical; font-size: 14px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.preview-card .modal-head { padding: 10px 12px 10px 16px; border-bottom: 1px solid var(--border); }
.modal-foot { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dir-list { min-height: 260px; max-height: 50vh; border: 1px solid var(--border); border-radius: 10px; padding: 4px; background: var(--bg-side); }
.preview-card { width: min(1100px, 100%); height: calc(100dvh - 32px); }
.pv-title { display: flex; flex-direction: column; min-width: 0; }
.pv-title span { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pv-body { flex: 1; overflow: auto; min-height: 0; display: flex; }
.pv-body > * { margin: auto; }
.pv-body img, .pv-body video { max-width: 100%; max-height: 100%; object-fit: contain; }
.pv-body iframe { width: 100%; height: 100%; border: 0; background: #fff; margin: 0; }
.pv-body pre { margin: 0; width: 100%; align-self: stretch; padding: 14px 16px; font-family: var(--mono); font-size: 13px;
  line-height: 1.5; white-space: pre; tab-size: 4; background: var(--bg-code); }
.pv-body pre code.hljs { background: none; padding: 0; }
.pv-body .md { padding: 18px 26px; max-width: 860px; width: 100%; align-self: flex-start; }
.pv-msg { color: var(--muted); text-align: center; padding: 40px; }

.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); max-width: 90vw; }
.toast.error { background: var(--danger); color: #fff; }

.only-mobile { display: none; }
.scrim { display: none; }
@media (max-width: 1100px) {
  .files-panel { position: fixed; right: 0; top: 0; bottom: 0; z-index: 25; box-shadow: var(--shadow); width: min(360px, 92vw); }
}
@media (max-width: 760px) {
  .only-mobile { display: inline-grid; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 35; transform: translateX(-100%); transition: transform .2s; width: min(300px, 86vw); }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 34; }
  .top { padding: 8px 10px; flex-wrap: wrap; }
  .top-title { flex: 1 1 calc(100% - 50px); }
  .top-controls { width: 100%; justify-content: flex-start; }
  .top-controls select { max-width: 150px; flex: 1; }
  .menu { right: 10px; top: 96px; }
  .messages { padding: 16px 12px 8px; }
  .composer { padding-left: 10px; padding-right: 10px; }
  .bubble { max-width: 92%; }
  .fcard { min-width: 0; width: 100%; }
}
