:root {
  --bg: #f7f5f0;
  --panel: #efece4;
  --surface: #ffffff;
  --border: #e2ddd2;
  --text: #29261f;
  --muted: #77715f;
  --accent: #c2603c;
  --accent-ink: #ffffff;
  --user-bubble: #ebe7dd;
  --code-bg: #f1eee6;
  --ok: #3f8f5a;
  --warn: #b7791f;
  --err: #c0392b;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1e1c; --panel: #191816; --surface: #2a2926; --border: #3a3834;
    --text: #ece9e1; --muted: #a39d8f; --accent: #d9774f; --user-bubble: #34322e;
    --code-bg: #24231f; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  }
}
:root[data-theme="dark"] {
  --bg: #1f1e1c; --panel: #191816; --surface: #2a2926; --border: #3a3834;
  --text: #ece9e1; --muted: #a39d8f; --accent: #d9774f; --user-bubble: #34322e;
  --code-bg: #24231f;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.6 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error-text { color: var(--err); min-height: 1.4em; margin: 0; font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
.btn.primary:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; font-size: 18px; padding: 6px 10px; cursor: pointer; border-radius: 8px; color: var(--muted); }
.icon-btn:hover { background: var(--panel); color: var(--text); }

.brand { font-family: "Source Serif 4", Georgia, serif; font-size: 19px; font-weight: 500; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow);
}
.login-card .brand { font-size: 24px; }
.login-card p.muted { margin: -6px 0 4px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 15px; color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Layout */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar {
  width: 272px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 10px; gap: 6px; overflow: hidden;
}
.side-top { display: flex; justify-content: space-between; align-items: center; padding: 2px 8px 10px; }
.new-chat { justify-content: flex-start; margin: 0 4px 4px; font-weight: 500; }
.side-link {
  display: flex; align-items: center; gap: 8px; padding: 7px 12px; border-radius: 8px;
  border: 0; background: transparent; cursor: pointer; text-align: left; font-size: 14px;
}
.side-link:hover, .side-link.active { background: var(--border); }
.side-label { font-size: 12px; color: var(--muted); padding: 12px 12px 4px; font-weight: 500; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; min-height: 60px; }
.conv-item { display: block; padding: 7px 12px; border-radius: 8px; font-size: 14px; }
.conv-item:hover { background: var(--border); }
.conv-item.active { background: var(--surface); box-shadow: var(--shadow); }
.conv-item .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.conv-item .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.machine-list { display: flex; flex-direction: column; gap: 2px; padding: 0 12px 6px; font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; background: var(--muted); }
.dot.on { background: var(--ok); }
.dot.run { background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.logout { color: var(--muted); margin-top: 4px; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; font-size: 11px; padding: 0 7px; margin-left: auto; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 18px; min-height: 56px;
  border-bottom: 1px solid transparent;
}
.top-title { flex: 1; min-width: 0; }
.top-title .t { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-title .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: "JetBrains Mono", monospace; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.top-actions select { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 5px 8px; font-size: 13px; }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* Chat */
.messages { flex: 1; overflow-y: auto; padding: 8px 16px 24px; }
.thread { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }
.msg-user { align-self: flex-end; max-width: 85%; background: var(--user-bubble); padding: 10px 16px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-ai { font-family: "Source Serif 4", Georgia, serif; font-size: 16.5px; line-height: 1.65; overflow-wrap: anywhere; }
.msg-ai .md > :first-child { margin-top: 0; }
.msg-ai .md > :last-child { margin-bottom: 0; }
.msg-ai pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.msg-ai code { font-family: "JetBrains Mono", monospace; font-size: .86em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.msg-ai pre code { background: none; padding: 0; }
.msg-ai table { border-collapse: collapse; font-size: 14px; display: block; overflow-x: auto; }
.msg-ai th, .msg-ai td { border: 1px solid var(--border); padding: 5px 10px; }
.msg-ai a { color: var(--accent); text-decoration: underline; }
.live { white-space: pre-wrap; }

.tool {
  font-family: Inter, sans-serif; font-size: 13px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); margin: 10px 0; overflow: hidden;
}
.tool summary { list-style: none; cursor: pointer; padding: 7px 12px; display: flex; gap: 8px; align-items: center; }
.tool summary::-webkit-details-marker { display: none; }
.tool .name { font-weight: 600; flex-shrink: 0; }
.tool .arg { font-family: "JetBrains Mono", monospace; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 12px; }
.tool .st { margin-left: auto; flex-shrink: 0; }
.tool pre { margin: 0; border: 0; border-top: 1px solid var(--border); border-radius: 0; max-height: 260px; overflow: auto; font-size: 12px; white-space: pre-wrap; }
.tool.error .st { color: var(--err); }
.status-line { font-family: Inter, sans-serif; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.err-box { font-family: Inter, sans-serif; font-size: 13px; color: var(--err); background: color-mix(in srgb, var(--err) 8%, transparent); border: 1px solid color-mix(in srgb, var(--err) 30%, transparent); border-radius: 10px; padding: 8px 12px; margin-top: 8px; white-space: pre-wrap; max-height: 200px; overflow: auto; }

/* Composer */
.composer-wrap { padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.composer {
  max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; box-shadow: var(--shadow); padding: 10px 10px 10px 16px; display: flex; gap: 8px; align-items: flex-end;
}
.composer:focus-within { border-color: var(--muted); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: transparent; min-height: 26px; max-height: 240px; padding: 4px 0; line-height: 1.5; font-size: 15.5px; }
.send { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-ink); cursor: pointer; font-size: 17px; flex-shrink: 0; }
.send:disabled { opacity: .35; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }
.hint { max-width: 760px; margin: 6px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

/* New chat */
.welcome { flex: 1; overflow-y: auto; display: flex; flex-direction: column; justify-content: center; padding: 16px; }
.welcome h1 { font-family: "Source Serif 4", Georgia, serif; font-weight: 400; font-size: clamp(26px, 5vw, 36px); text-align: center; margin: 0 0 22px; }
.welcome h1 .logo { margin-right: 8px; }
.pickers { max-width: 760px; width: 100%; margin: 0 auto 12px; display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 8px; }
.pickers label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); min-width: 0; }
.pickers select, .pickers input { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 8px 10px; font-size: 14px; min-width: 0; color: var(--text); }
.custom-path { grid-column: 1 / -1; }
.offline-note { max-width: 760px; margin: 0 auto 12px; font-size: 14px; color: var(--warn); text-align: center; }

/* Reports */
.reports { flex: 1; overflow-y: auto; padding: 8px 16px 32px; }
.reports .inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.report { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 18px; }
.report .h { font-size: 12px; color: var(--muted); margin-bottom: 6px; display: flex; gap: 8px; }
.report .b { white-space: pre-wrap; font-size: 14.5px; overflow-wrap: anywhere; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* Mobile */
.only-mobile { display: none; }
.scrim { display: none; }
@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: 82vw; max-width: 300px; }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .topbar { padding: 8px 10px; }
  .pickers { grid-template-columns: 1fr 1fr; }
  .pickers .proj { grid-column: 1 / -1; order: -1; }
  .msg-ai { font-size: 16px; }
  .top-actions select { max-width: 120px; }
}
