:root {
  --bg: #1a1a18;
  --bg-side: #161614;
  --bg-2: #242422;
  --bg-3: #2e2e2b;
  --line: #38383420;
  --border: #36363270;
  --text: #ece9e3;
  --muted: #9a958c;
  --accent: #cc785c;
  --accent-2: #d98c70;
  --user: #2b2b28;
  --ok: #6cab74;
  --err: #d9695f;
  --amber: #d8a657;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
button { cursor: pointer; font: inherit; color: inherit; border: none; background: none; }
input, textarea, select { font: inherit; color: var(--text); }
.icon-btn { font-size: 17px; color: var(--muted); padding: 5px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--bg-3); color: var(--text); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--err); transition: background .3s; flex: none; }
.dot.on { background: var(--ok); }

/* ---------- Login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: radial-gradient(120% 80% at 50% 0%, #26241f 0%, var(--bg) 60%); z-index: 100; }
.login-card { width: 100%; max-width: 360px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 20px; padding: 32px 24px; display: flex; flex-direction: column; gap: 12px; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.login-card .logo { font-size: 40px; color: var(--accent); }
.login-card h1 { margin: 0; font-size: 22px; font-weight: 650; }
.login-card p { margin: 0 0 8px; }
.login-card input, .login-card button { width: 100%; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg-3); }
.login-card button { background: var(--accent); color: #fff; border: none; font-weight: 650; margin-top: 4px; }
.error-text { color: var(--err); font-size: 14px; min-height: 18px; }

/* ---------- App shell ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }

/* Sidebar */
.sidebar { width: 272px; flex: none; background: var(--bg-side); border-right: 1px solid var(--border); display: flex; flex-direction: column; transition: margin-left .2s ease; }
.app.collapsed .sidebar { margin-left: -272px; }
.sb-top { display: flex; align-items: center; gap: 8px; padding: 12px 12px 8px; }
.sb-brand { font-weight: 650; color: var(--accent); }
.sb-actions { padding: 6px 12px; display: flex; gap: 6px; }
.sb-new { flex: 1; background: var(--accent); color: #fff; border-radius: 10px; padding: 9px 10px; font-size: 13.5px; font-weight: 600; }
.sb-new.ghost { flex: none; background: var(--bg-3); color: var(--text); border: 1px solid var(--border); }
.sb-new:active { filter: brightness(1.1); }
.sb-section { padding: 14px 16px 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.sb-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.sb-item { display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-radius: 9px; cursor: pointer; }
.sb-item:hover { background: var(--bg-2); }
.sb-item.active { background: var(--bg-3); }
.sb-item .bdot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.bdot.code { background: #9cc06a; } .bdot.chat { background: #6fa8d6; }
.sb-item .it { flex: 1; min-width: 0; }
.sb-item .it .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }
.sb-item .it .m { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-item .x { font-size: 15px; color: var(--muted); opacity: 0; padding: 0 2px; }
.sb-item:hover .x { opacity: 1; }
.sb-foot { display: flex; align-items: center; gap: 6px; padding: 10px 12px calc(10px + var(--safe-b)); border-top: 1px solid var(--border); }
.acct { flex: 1; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-backdrop { display: none; }

/* Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.main-top { display: flex; align-items: center; gap: 12px; padding: 8px 14px; border-bottom: 1px solid var(--border); }
#sbOpen { display: none; }
.seg { display: inline-flex; background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; }
.seg-btn { padding: 6px 12px; border-radius: 8px; font-size: 13px; color: var(--muted); font-weight: 550; }
.seg-btn.active { background: var(--bg-3); color: var(--text); }
.main-title { flex: 1; min-width: 0; text-align: center; font-size: 13.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn-banner { background: #3a2622; color: #e6b0a8; text-align: center; font-size: 13px; padding: 7px; border-bottom: 1px solid #5a3a34; }

.content { flex: 1; min-height: 0; display: flex; }
.messages { flex: 1; overflow-y: auto; padding: 22px 16px; display: flex; flex-direction: column; gap: 16px; }
.messages > * { width: 100%; max-width: 760px; margin: 0 auto; }

/* messages */
.msg { border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user); border: 1px solid var(--border); padding: 11px 14px; max-width: min(85%, 760px); border-bottom-right-radius: 5px; }
.msg.assistant { align-self: flex-start; padding: 2px 2px; }
.msg.system { align-self: center; color: var(--muted); font-size: 13px; text-align: center; }
.msg.error { align-self: center; background: #3a2622; color: var(--err); border: 1px solid #5a3a34; padding: 10px 14px; border-radius: 12px; font-size: 14px; max-width: 760px; }
.msg.user, .msg.system, .msg.error { margin-left: auto; margin-right: auto; }
.msg.user { margin-right: 0; } /* keep user bubble right within centered column */
.part { white-space: pre-wrap; }
.part + .part { margin-top: 8px; }
.thinking { color: var(--muted); font-size: 13px; font-style: italic; border-left: 2px solid var(--border); padding-left: 8px; }

.tool { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; margin-top: 8px; font-size: 13px; overflow: hidden; }
.tool summary { padding: 8px 12px; cursor: pointer; display: flex; gap: 8px; align-items: center; list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool .tname { font-weight: 650; color: var(--accent-2); }
.tool .tsum { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool pre { margin: 0; padding: 10px 12px; background: #1112; border-top: 1px solid var(--border); overflow-x: auto; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; color: #cfcabf; }
.tool pre.res { color: #b8d8bb; } .tool pre.err { color: #e0a39c; }
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

.perm { align-self: stretch; background: #2c2a20; border: 1px solid #4d4733; border-radius: 12px; padding: 12px; font-size: 14px; }
.perm .pq { margin-bottom: 10px; } .perm .pq b { color: var(--accent-2); }
.perm-actions { display: flex; gap: 10px; }
.perm-actions button { flex: 1; padding: 10px; border-radius: 10px; font-weight: 600; }
.perm-allow { background: var(--ok); color: #14210f; }
.perm-deny { background: var(--bg-3); border: 1px solid var(--border); }

/* Composer */
.composer { border-top: 1px solid var(--border); padding: 12px 16px calc(12px + var(--safe-b)); }
.composer > * { max-width: 760px; margin: 0 auto; }
.composer-box { display: flex; align-items: flex-end; gap: 8px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 16px; padding: 8px 8px 8px 14px; }
#input { flex: 1; resize: none; max-height: 180px; min-height: 26px; background: none; border: none; padding: 6px 0; outline: none; }
.send-btn, .stop-btn { width: 38px; height: 38px; border-radius: 11px; font-size: 14px; color: #fff; flex: none; }
.send-btn { background: var(--accent); } .send-btn:disabled { opacity: .5; }
.send-btn.wide { width: auto; padding: 0 20px; }
.stop-btn { background: var(--err); }
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.cb-right { display: flex; gap: 6px; }
.chip-sel { background: var(--bg-2); border: 1px solid var(--border); color: var(--muted); border-radius: 8px; padding: 5px 8px; font-size: 12px; max-width: 46vw; }
#modeSel[value] { color: var(--text); }
.chip-sel.amber { color: var(--amber); border-color: #574a2e; }

/* Files */
.files-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.files-toolbar { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.breadcrumb { flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; font-size: 14px; }
.breadcrumb a { color: var(--accent-2); text-decoration: none; cursor: pointer; }
.breadcrumb span.sep { color: var(--muted); margin: 0 4px; }
.files-actions { display: flex; gap: 6px; }
.ghost-btn { background: var(--bg-3); border: 1px solid var(--border); border-radius: 9px; padding: 7px 11px; font-size: 13px; white-space: nowrap; }
.ghost-btn:active { background: var(--line); }
.upload-bar { height: 4px; background: var(--bg-3); }
#uploadProgress { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.file-list { flex: 1; overflow-y: auto; }
.file-list > * , .file-row { max-width: 860px; margin: 0 auto; }
.file-row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.file-row .ic { font-size: 19px; flex: none; } .file-row .fn { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.file-row.dir .fn { font-weight: 550; }
.file-row .fmeta { font-size: 12px; color: var(--muted); flex: none; }
.file-row .fact { font-size: 17px; color: var(--muted); padding: 4px 6px; }
.empty { text-align: center; color: var(--muted); padding: 44px 20px; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; padding: 16px; z-index: 200; }
.modal-card { background: var(--bg-2); border: 1px solid var(--border); border-radius: 16px; width: 100%; max-width: 760px; height: 80dvh; display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head span { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modalText { flex: 1; resize: none; border: none; background: #1112; padding: 14px; font: 13px/1.5 ui-monospace, monospace; }
.modal-foot { padding: 10px 16px calc(10px + var(--safe-b)); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* Diagnostics */
.diag-body { flex: 1; overflow-y: auto; padding: 14px 16px; }
.health-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; margin-bottom: 14px; }
.health-grid div { font-size: 13px; }
.health-grid b { color: var(--muted); font-weight: 500; }
.logline { font: 12px/1.4 ui-monospace, monospace; padding: 4px 8px; border-radius: 6px; margin-bottom: 3px; white-space: pre-wrap; word-break: break-word; }
.logline.info { color: #b9b4aa; } .logline.warn { color: var(--amber); background: #2a2410; }
.logline.error { color: #e7a59d; background: #2e1d1a; }
.logline .lt { color: var(--muted); }

/* Toast */
.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); background: var(--bg-3); border: 1px solid var(--border); color: var(--text); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 300; max-width: 90%; box-shadow: 0 8px 24px rgba(0,0,0,.4); }

/* ---------- Mobile ---------- */
@media (max-width: 899px) {
  .sidebar { position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; margin-left: 0; transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .app.sb-open .sidebar { transform: translateX(0); }
  .app.collapsed .sidebar { margin-left: 0; }
  .app.sb-open .sb-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40; }
  #sbOpen { display: inline-block; }
  .main-title { display: none; }
  .seg-btn { padding: 6px 9px; font-size: 12.5px; }
}
