:root {
  color-scheme: dark;
  --bg: #0b0b0c;
  --raise: #141416;
  --hover: #121214;
  --sel: #19191c;
  --line: #1e1e21;
  --fg: #ececee;
  --fg2: #a0a0a6;
  --fg3: #636369;
  --accent: #e5484d;
  --ok: #5bb98b;
  --warn: #d6a24a;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
  --bar: 52px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; background: var(--bg); color: var(--fg); overscroll-behavior: none; }
body { font: 15px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; background: none; border: 0; outline: 0; border-radius: 0; -webkit-appearance: none; appearance: none; }
button { cursor: pointer; color: var(--fg2); white-space: nowrap; }
button:disabled { opacity: .4; cursor: default; }
input::placeholder, textarea::placeholder { color: var(--fg3); }
a { color: inherit; }
ul { list-style: none; }
[hidden] { display: none !important; }
svg { display: block; }
.mono { font-family: var(--mono); font-size: .9em; }
.dim { color: var(--fg3); }

#login { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding-bottom: 12vh; }
#login input { width: 220px; padding: 10px 0; text-align: center; font-size: 17px; letter-spacing: 4px; border-bottom: 1px solid var(--line); }
#login input:focus { border-color: var(--fg3); }
#err { color: var(--accent); font-size: 13px; min-height: 19px; }

#app { display: grid; grid-template-columns: 260px minmax(0, 1fr) 0; height: 100vh; height: 100dvh; }
#app.files-open { grid-template-columns: 260px minmax(0, 1fr) 360px; }
#app:not(.files-open) #files { display: none; }
.pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; border-right: 1px solid var(--line); background: var(--bg); }
#files { border-right: 0; border-left: 1px solid var(--line); }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

.bar { flex: none; display: flex; align-items: center; gap: 8px; height: calc(var(--bar) + var(--top)); padding: var(--top) 14px 0; border-bottom: 1px solid var(--line); position: relative; }
.bar .title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 8px; font-weight: 600; font-size: 15px; }
.bar .title span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .title .sub { font-weight: 400; font-size: 12px; color: var(--fg3); }
.brand { flex: 1; font-weight: 600; font-size: 16px; padding-left: 4px; }
.back { display: none; align-items: center; color: var(--fg2); padding: 8px 6px 8px 0; font-size: 15px; }
.back::before { content: ""; width: 9px; height: 9px; border-left: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); margin: 0 6px 0 4px; border-radius: 1px; }
.icon { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 8px; color: var(--fg2); flex: none; }
.icon:hover, .text:hover { color: var(--fg); }
.icon svg { width: 19px; height: 19px; }
.text { padding: 7px 9px; border-radius: 8px; font-size: 13px; color: var(--fg2); text-decoration: none; white-space: nowrap; }
.primary { background: var(--fg); color: var(--bg); padding: 7px 14px; border-radius: 8px; font-weight: 600; font-size: 14px; }
.only-wide { display: grid; }

#chats { padding: 6px 8px; }
#chats li { position: relative; padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--fg2); font-size: 14px; }
#chats li:hover { background: var(--hover); color: var(--fg); }
#chats li.on { background: var(--sel); color: var(--fg); }
#chats .t { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 14px; }
#chats .m { display: block; font-size: 11px; color: var(--fg3); font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
#chats li.busy::after { content: ""; position: absolute; right: 10px; top: 15px; width: 6px; height: 6px; border-radius: 50%; background: var(--ok); animation: pulse 1s infinite; }
#chats .empty { color: var(--fg3); padding: 24px 10px; cursor: default; }
#chats .empty:hover { background: none; color: var(--fg3); }
@keyframes pulse { 50% { opacity: .3; } }

.foot { flex: none; border-top: 1px solid var(--line); padding: 10px 12px calc(8px + var(--bottom)); display: flex; flex-direction: column; gap: 6px; }
#status { font-size: 12px; color: var(--fg3); line-height: 1.45; }
#status .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--fg3); margin-right: 6px; vertical-align: 1px; }
#status.on .dot { background: var(--ok); }
#status b { color: var(--fg2); font-weight: 500; }
#status .bars { display: grid; grid-template-columns: 34px 1fr; gap: 2px 8px; margin-top: 6px; font-family: var(--mono); font-size: 11px; }
#status .meter { height: 4px; margin-top: 6px; background: var(--line); border-radius: 2px; overflow: hidden; }
#status .meter i { display: block; height: 100%; background: var(--fg3); }
.foot-btns { display: flex; gap: 2px; flex-wrap: wrap; }
.foot-btns button { padding: 6px 8px; font-size: 12px; border-radius: 7px; color: var(--fg3); }
.foot-btns button:hover { color: var(--fg); }

.controls { display: flex; align-items: center; gap: 6px; flex: none; }
#model { max-width: 220px; background: var(--raise); border-radius: 8px; padding: 6px 26px 6px 10px; font-size: 13px; font-family: var(--mono); color: var(--fg2); cursor: pointer; background-image: linear-gradient(45deg, transparent 50%, var(--fg3) 50%), linear-gradient(135deg, var(--fg3) 50%, transparent 50%); background-position: calc(100% - 14px) 50%, calc(100% - 10px) 50%; background-size: 4px 4px; background-repeat: no-repeat; }
.seg { display: flex; background: var(--raise); border-radius: 8px; padding: 2px; }
.seg button { padding: 4px 10px; font-size: 13px; border-radius: 6px; color: var(--fg3); }
.seg button.on { background: #222226; color: var(--fg); }
.seg button:disabled { opacity: .35; }
#files-btn.on { color: var(--fg); background: var(--raise); }

#log { padding: 8px 0 24px; }
.msg { max-width: 820px; margin: 0 auto; padding: 14px 24px; }
.msg.user .bubble { background: var(--raise); border-radius: 12px; padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere; display: inline-block; max-width: 100%; }
.msg.user { text-align: right; }
.msg.user .bubble { text-align: left; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; }
.atts img { max-width: 180px; max-height: 140px; border-radius: 8px; display: block; }
.atts .file { font-size: 12px; font-family: var(--mono); background: var(--raise); padding: 5px 9px; border-radius: 7px; color: var(--fg2); }
.who { font-size: 11px; color: var(--fg3); margin-bottom: 4px; font-family: var(--mono); }
.md { overflow-wrap: anywhere; line-height: 1.6; }
.md > * + * { margin-top: .7em; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.05em; font-weight: 600; margin-top: 1.1em; }
.md h1 { font-size: 1.25em; } .md h2 { font-size: 1.15em; }
.md ul, .md ol { padding-left: 1.4em; }
.md ul { list-style: disc; } .md ol { list-style: decimal; }
.md li + li { margin-top: .25em; }
.md blockquote { border-left: 2px solid var(--line); padding-left: 12px; color: var(--fg2); }
.md code { font-family: var(--mono); font-size: .88em; background: var(--raise); padding: 1px 5px; border-radius: 5px; }
.md pre { position: relative; background: #0f0f11; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.md pre code { background: none; padding: 0; font-size: inherit; }
.md pre .lang { position: absolute; top: 6px; right: 10px; font-size: 11px; color: var(--fg3); font-family: var(--mono); }
.md pre .copy { position: absolute; bottom: 6px; right: 8px; font-size: 11px; color: var(--fg3); padding: 2px 6px; }
.md pre .copy:hover { color: var(--fg); }
.md a { color: var(--fg); text-decoration: underline; text-decoration-color: var(--fg3); text-underline-offset: 2px; }
.md table { border-collapse: collapse; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 10px; text-align: left; }
.md hr { border: 0; border-top: 1px solid var(--line); }
.think { margin: 4px 0 10px; }
.think summary { cursor: pointer; color: var(--fg3); font-size: 13px; list-style: none; }
.think summary::before { content: "▸ "; }
.think[open] summary::before { content: "▾ "; }
.think .t { white-space: pre-wrap; color: var(--fg3); font-size: 13px; border-left: 2px solid var(--line); padding: 4px 0 4px 12px; margin-top: 6px; }
.tool { margin: 8px 0; border: 1px solid var(--line); border-radius: 9px; font-size: 13px; overflow: hidden; }
.tool summary { display: flex; gap: 8px; align-items: center; padding: 7px 11px; cursor: pointer; list-style: none; color: var(--fg2); }
.tool summary .n { font-family: var(--mono); color: var(--fg); }
.tool summary .a { font-family: var(--mono); color: var(--fg3); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool .st { flex: none; font-size: 12px; }
.tool .st.ok { color: var(--ok); } .tool .st.bad { color: var(--accent); } .tool .st.run { color: var(--warn); animation: pulse 1s infinite; }
.tool pre { margin: 0; border-top: 1px solid var(--line); padding: 9px 11px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow-y: auto; color: var(--fg2); background: #0f0f11; }
.stats { margin-top: 8px; font-size: 11px; color: var(--fg3); font-family: var(--mono); }
.stats.err { color: var(--accent); font-family: inherit; font-size: 13px; }
.cursor::after { content: "▍"; color: var(--fg3); animation: pulse 1s infinite; }
.empty-chat { max-width: 520px; margin: 18vh auto 0; padding: 0 24px; text-align: center; color: var(--fg3); font-size: 14px; }
.empty-chat h2 { color: var(--fg); font-size: 18px; font-weight: 600; margin-bottom: 6px; }

#composer { flex: none; max-width: 868px; width: 100%; margin: 0 auto; padding: 8px 24px calc(14px + var(--bottom)); }
#composer .row { display: flex; align-items: flex-end; gap: 6px; background: var(--raise); border: 1px solid var(--line); border-radius: 14px; padding: 6px; }
#composer .row:focus-within { border-color: #2a2a2e; }
#input { flex: 1; resize: none; max-height: 40vh; padding: 8px 4px; font-size: 15px; line-height: 1.45; }
.send { width: 36px; height: 36px; border-radius: 10px; background: var(--fg); color: var(--bg); display: grid; place-items: center; flex: none; }
.send svg { width: 18px; height: 18px; }
.send.stop { background: var(--accent); color: #fff; }
.send:disabled { opacity: .25; }
#chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
#chips:empty { display: none; }
.chip { display: flex; align-items: center; gap: 6px; background: var(--raise); border: 1px solid var(--line); border-radius: 8px; padding: 4px 4px 4px 9px; font-size: 12px; font-family: var(--mono); color: var(--fg2); max-width: 240px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip img { width: 22px; height: 22px; object-fit: cover; border-radius: 4px; }
.chip button { padding: 0 6px; color: var(--fg3); font-size: 14px; }
.chip.up { opacity: .6; }

#tree { padding: 6px 8px; font-family: var(--mono); font-size: 13px; }
#tree li { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 7px; cursor: pointer; color: var(--fg2); }
#tree li:hover { background: var(--hover); color: var(--fg); }
#tree li.dir { color: var(--fg3); cursor: default; }
#tree li.dir:hover { background: none; }
#tree li .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tree li .sz { font-size: 11px; color: var(--fg3); }
#tree li.fresh { color: var(--ok); }
#tree .empty { color: var(--fg3); font-family: inherit; cursor: default; padding: 24px 8px; display: block; }
#viewer { flex: 1.4; min-height: 0; display: flex; flex-direction: column; border-top: 1px solid var(--line); }
#viewer:not([hidden]) ~ * { display: none; }
.vbar { display: flex; align-items: center; gap: 6px; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--line); }
.vbar .mono { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg2); }
#vbody { padding: 10px 0; }
#vbody pre { font-family: var(--mono); font-size: 12px; line-height: 1.55; counter-reset: ln; white-space: pre; }
#vbody pre span { display: block; padding: 0 14px 0 0; }
#vbody pre span::before { counter-increment: ln; content: counter(ln); display: inline-block; width: 38px; padding-right: 12px; text-align: right; color: var(--fg3); user-select: none; }
#vbody img { max-width: 100%; display: block; margin: 0 auto; }
#vbody .note { color: var(--fg3); padding: 14px; font-size: 13px; }

#modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .6); display: grid; place-items: center; z-index: 30; padding: 20px; }
#modal .sheet { width: min(720px, 100%); max-height: min(86vh, 900px); display: flex; flex-direction: column; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
#modal-body { padding: 18px 20px 22px; }
.set h3 { font-size: 12px; font-weight: 600; letter-spacing: .06em; color: var(--fg3); margin: 4px 0 10px; }
.set section + section { margin-top: 22px; padding-top: 20px; border-top: 1px solid var(--line); }
.set p { font-size: 13px; color: var(--fg2); line-height: 1.55; }
.set p + p { margin-top: 8px; }
.inline { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.inline input { flex: 1; min-width: 120px; background: var(--raise); border-radius: 9px; padding: 8px 11px; font-size: 14px; }
.tokenbox { font-family: var(--mono); font-size: 12px; background: var(--raise); border-radius: 9px; padding: 9px 11px; overflow-wrap: anywhere; color: var(--fg2); margin-top: 10px; user-select: all; }
.prompt { font-family: var(--mono); font-size: 12px; line-height: 1.5; background: #0f0f11; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 46vh; overflow-y: auto; color: var(--fg2); margin-top: 12px; }
.steps { padding-left: 1.3em; font-size: 13px; color: var(--fg2); line-height: 1.6; }
.steps li { list-style: decimal; }
.steps code { font-family: var(--mono); font-size: 12px; background: var(--raise); padding: 1px 5px; border-radius: 5px; }

#toast { position: fixed; left: 50%; bottom: calc(90px + var(--bottom)); transform: translateX(-50%); background: var(--raise); border: 1px solid var(--line); color: var(--fg); font-size: 14px; padding: 9px 16px; border-radius: 10px; z-index: 40; max-width: calc(100vw - 32px); }

@media (max-width: 1100px) and (min-width: 900px) {
  #app { grid-template-columns: 220px minmax(0, 1fr) 0; }
  #app.files-open { grid-template-columns: 220px minmax(0, 1fr) 320px; }
}

@media (max-width: 899px) {
  body { font-size: 16px; }
  #app, #app.files-open { display: block; position: relative; overflow: hidden; }
  #app:not(.files-open) #files { display: flex; }
  .pane { position: absolute; inset: 0; border: 0; visibility: hidden; transform: translateX(24px); opacity: 0; transition: transform .22s ease, opacity .22s ease, visibility 0s .22s; }
  #app[data-view="side"] #side, #app[data-view="chat"] #chat, #app[data-view="files"] #files { visibility: visible; transform: none; opacity: 1; transition: transform .22s ease, opacity .22s ease; }
  #files { border-left: 0; }
  .back { display: inline-flex; }
  .only-wide { display: none; }
  .bar { padding-left: 10px; padding-right: 10px; }
  #chat .bar { height: auto; min-height: calc(var(--bar) + var(--top)); flex-wrap: wrap; padding-bottom: 8px; row-gap: 6px; }
  #chat .bar .title { flex: 1 1 auto; }
  #chat .controls { width: 100%; }
  #model { flex: 1 1 0; width: 0; min-width: 0; max-width: none; font-size: 14px; }
  .seg button { padding: 5px 9px; }
  #files-btn { padding: 7px 8px; }
  .msg { padding: 12px 16px; }
  #composer { padding: 6px 10px calc(10px + var(--bottom)); }
  #input { font-size: 16px; }
  #chats li { font-size: 16px; padding: 11px 12px; }
  #tree { font-size: 14px; }
  #tree li { padding: 8px; }
  #modal { padding: 0; align-items: end; }
  #modal .sheet { max-height: 92vh; border-radius: 14px 14px 0 0; padding-bottom: var(--bottom); }
  .inline input { font-size: 16px; }
}
