:root {
  --bg: #f6f5f1;
  --panel: #ffffff;
  --side: #efede7;
  --side-hover: #e5e2da;
  --line: #e2dfd7;
  --line-strong: #cfcbc1;
  --text: #1f1e1c;
  --muted: #6b6860;
  --faint: #8f8b82;
  --accent: #b9572f;
  --accent-hover: #a14a26;
  --accent-soft: #f6e6dd;
  --accent-text: #ffffff;
  --ok: #2f7d4f;
  --ok-soft: #e3f1e8;
  --warn: #9a6a00;
  --warn-soft: #fbf0d6;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --code-bg: #f1efe9;
  --user-bg: #ece9e1;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 24px rgba(0,0,0,.06);
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1c1a; --panel: #262523; --side: #191816; --side-hover: #2a2926; --line: #34322e; --line-strong: #46433d;
    --text: #ecebe7; --muted: #a6a39b; --faint: #85827a; --accent: #d9774f; --accent-hover: #e4875f; --accent-soft: #3a2a22;
    --ok: #5fbf85; --ok-soft: #1f3327; --warn: #e0b04a; --warn-soft: #3a311b; --bad: #f08a80; --bad-soft: #3d2220;
    --code-bg: #2e2c29; --user-bg: #302e2a; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  --bg: #1d1c1a; --panel: #262523; --side: #191816; --side-hover: #2a2926; --line: #34322e; --line-strong: #46433d;
  --text: #ecebe7; --muted: #a6a39b; --faint: #85827a; --accent: #d9774f; --accent-hover: #e4875f; --accent-soft: #3a2a22;
  --ok: #5fbf85; --ok-soft: #1f3327; --warn: #e0b04a; --warn-soft: #3a311b; --bad: #f08a80; --bad-soft: #3d2220;
  --code-bg: #2e2c29; --user-bg: #302e2a; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 var(--sans); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.boot { padding: 30vh 16px; text-align: center; color: var(--muted); }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
code, pre, .mono { font-family: var(--mono); font-size: .86em; }

/* ---------- buttons and fields ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 6px 14px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--panel); cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap; }
.btn:hover { background: var(--side); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--side-hover); }
.btn.small { min-height: 30px; padding: 3px 10px; font-size: 13.5px; }
.btn[disabled] { opacity: .55; cursor: default; }
.btn.block { width: 100%; }
label { display: block; font-weight: 550; margin: 14px 0 6px; }
label .hint, .hint { font-weight: 400; color: var(--muted); font-size: 13.5px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=url], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--panel); min-height: 40px; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 0; border-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; margin: 12px 0; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.error-box { background: var(--bad-soft); color: var(--bad); padding: 10px 12px; border-radius: 8px; margin: 12px 0; white-space: pre-wrap; }
.note { background: var(--warn-soft); color: var(--text); padding: 10px 12px; border-radius: 8px; margin: 12px 0; border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent); }
.ok-box { background: var(--ok-soft); padding: 10px 12px; border-radius: 8px; margin: 12px 0; }
.muted { color: var(--muted); }
.small { font-size: 13.5px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; background: var(--side); font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.running { background: var(--ok); }
.dot.starting { background: var(--warn); }
.dot.crashed { background: var(--bad); }
.dot.busy { background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- auth screens ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 32px 16px; }
.auth-card { width: 100%; max-width: 410px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px 26px; box-shadow: var(--shadow); }
.auth-card.wide { max-width: 540px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 17px; margin-bottom: 18px; }
.brand img { width: 28px; height: 28px; }
.auth h1 { font: 600 1.5rem/1.25 var(--serif); margin: 0 0 6px; }
.auth p { margin: 6px 0; }
.qr { display: grid; place-items: center; background: #fff; padding: 10px; border-radius: 10px; width: max-content; margin: 12px auto; }
.codes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; font-family: var(--mono); background: var(--code-bg); padding: 12px; border-radius: 8px; }
.secret { font-family: var(--mono); word-break: break-all; background: var(--code-bg); padding: 6px 8px; border-radius: 6px; font-size: 13px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 260px 1fr; height: 100vh; height: 100dvh; }
.side { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-top { padding: 14px 12px 8px; }
.side .brand { margin: 2px 6px 12px; }
.side nav { flex: 1; overflow-y: auto; padding: 0 8px; }
.side h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 16px 8px 6px; font-weight: 600; }
.nav-item { display: flex; align-items: center; gap: 9px; width: 100%; padding: 8px 10px; border-radius: 8px; border: 0; background: none; text-align: left; cursor: pointer; color: var(--text); text-decoration: none; min-height: 38px; }
.nav-item:hover { background: var(--side-hover); }
.nav-item.active { background: var(--panel); box-shadow: 0 1px 2px rgba(0,0,0,.06); font-weight: 560; }
.nav-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-bottom { border-top: 1px solid var(--line); padding: 8px; }
.me { display: flex; align-items: center; gap: 10px; padding: 8px 10px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 650; font-size: 13px; flex: none; }
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.topbar { display: none; }
.page { overflow-y: auto; padding: 28px 32px 60px; }
.page-inner { max-width: 980px; margin: 0 auto; }
.page h1 { font: 600 1.6rem/1.25 var(--serif); margin: 0 0 4px; }
.page h2 { font-size: 1.1rem; margin: 28px 0 10px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin: 12px 0; }
.card h3 { margin: 0 0 6px; font-size: 1rem; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12.5px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
table.list td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table-wrap { overflow-x: auto; }

/* ---------- project view ---------- */
.proj-head { padding: 14px 24px 0; border-bottom: 1px solid var(--line); background: var(--bg); }
.proj-title { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.proj-title h1 { font: 600 1.3rem/1.3 var(--serif); margin: 0; }
.svc-links { display: flex; gap: 8px; flex-wrap: wrap; }
.svc-link { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font-size: 13px; text-decoration: none; color: var(--text); }
.svc-link:hover { border-color: var(--line-strong); }
.tabs { display: flex; gap: 2px; margin-top: 10px; overflow-x: auto; }
.tab { padding: 9px 14px; border: 0; background: none; text-decoration: none; border-bottom: 2px solid transparent; cursor: pointer; color: var(--muted); white-space: nowrap; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 560; }
.tab .count { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 0 7px; font-size: 12px; margin-left: 4px; }
.proj-body { flex: 1; min-height: 0; display: flex; }

/* ---------- chat ---------- */
.chat-list { width: 240px; border-right: 1px solid var(--line); overflow-y: auto; padding: 10px 8px; flex: none; background: color-mix(in srgb, var(--side) 45%, var(--bg)); }
.chat-list .nav-item { font-size: 14px; min-height: 34px; padding: 7px 10px; }
.chat-list .sub { font-size: 12px; color: var(--faint); }
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.transcript { flex: 1; overflow-y: auto; padding: 24px 24px 12px; }
.transcript-inner { max-width: 780px; margin: 0 auto; }
.empty-chat { text-align: center; margin: 12vh auto 0; max-width: 520px; color: var(--muted); }
.empty-chat h2 { font: 500 1.7rem/1.3 var(--serif); color: var(--text); margin: 0 0 8px; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 8px; margin-top: 20px; text-align: left; }
.suggestion { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); cursor: pointer; font-size: 14px; color: var(--text); text-align: left; }
.suggestion:hover { border-color: var(--line-strong); }
.msg { margin: 0 0 18px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bg); padding: 10px 14px; border-radius: 14px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg .who { font-size: 12px; color: var(--faint); margin: 0 0 3px; text-align: right; }
.md { word-wrap: break-word; }
.md p { margin: 0 0 10px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.02rem; margin: 16px 0 6px; }
.md pre { background: var(--code-bg); padding: 10px 12px; border-radius: 8px; overflow-x: auto; margin: 0 0 10px; }
.md code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre code { background: none; padding: 0; }
.md a { word-break: break-all; }
.md table { border-collapse: collapse; margin: 0 0 10px; font-size: 14px; display: block; overflow-x: auto; }
.md td, .md th { border: 1px solid var(--line); padding: 4px 8px; }
.md blockquote { margin: 0 0 10px; padding-left: 12px; border-left: 3px solid var(--line-strong); color: var(--muted); }
.tool { margin: 4px 0 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); font-size: 13.5px; }
.tool summary { list-style: none; cursor: pointer; padding: 6px 10px; display: flex; gap: 8px; align-items: center; min-height: 34px; }
.tool summary::-webkit-details-marker { display: none; }
.tool .tname { font-weight: 600; font-family: var(--mono); font-size: 12.5px; }
.tool .tdesc { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; font-family: var(--mono); font-size: 12.5px; }
.tool .tstate { font-size: 12px; }
.tool .tstate.err { color: var(--bad); }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--line); background: var(--code-bg); max-height: 360px; overflow: auto; white-space: pre-wrap; word-break: break-word; font-size: 12.5px; }
.thinking { color: var(--muted); font-size: 13.5px; margin: 4px 0 8px; }
.thinking summary { cursor: pointer; }
.thinking div { white-space: pre-wrap; padding: 6px 0 0 14px; border-left: 2px solid var(--line); margin-top: 4px; }
.result-line { font-size: 12.5px; color: var(--faint); margin: -6px 0 22px; }
.notice { font-size: 13.5px; color: var(--muted); text-align: center; margin: 10px 0 18px; }
.chat-error { background: var(--bad-soft); color: var(--bad); padding: 10px 12px; border-radius: 8px; margin: 0 0 18px; white-space: pre-wrap; font-size: 14px; }
.working { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; margin: 4px 0 16px; }
.composer-wrap { padding: 8px 24px 18px; }
.composer { max-width: 780px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line-strong); border-radius: 16px; box-shadow: var(--shadow); padding: 10px 10px 8px 14px; }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { border: 0; padding: 4px 0; min-height: 52px; max-height: 40vh; resize: none; background: transparent; outline: none; line-height: 1.5; }
.composer textarea:focus { outline: none; }
.composer-bar { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.composer-bar select { width: auto; min-height: 32px; padding: 3px 8px; font-size: 13.5px; border-radius: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 4px 10px; font-size: 13.5px; cursor: pointer; min-height: 30px; }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.send { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; cursor: pointer; display: grid; place-items: center; }
.send[disabled] { opacity: .4; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }
.composer-hint { max-width: 780px; margin: 6px auto 0; font-size: 12px; color: var(--faint); text-align: center; }

/* ---------- services, logs, diff ---------- */
.svc-card .head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.svc-card .meta { font-size: 13px; color: var(--muted); margin-top: 4px; }
.logs { background: #161513; color: #e6e3dc; font-family: var(--mono); font-size: 12px; padding: 10px; border-radius: 8px; height: 420px; overflow: auto; white-space: pre-wrap; word-break: break-all; margin-top: 10px; }
.diff { font-family: var(--mono); font-size: 12.5px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; overflow: auto; max-height: 70vh; }
.diff div { white-space: pre; padding: 0 10px; min-height: 18px; }
.diff .add { background: color-mix(in srgb, var(--ok) 14%, transparent); }
.diff .del { background: color-mix(in srgb, var(--bad) 13%, transparent); }
.diff .hunk { color: var(--muted); background: var(--side); }
.diff .file { font-weight: 700; background: var(--side); border-top: 1px solid var(--line); padding-top: 4px; padding-bottom: 4px; }
.files { list-style: none; padding: 0; margin: 0; font-family: var(--mono); font-size: 13px; }
.files li { display: flex; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.files .code { width: 26px; color: var(--muted); }
.files .path { flex: 1; word-break: break-all; }
.file-browser { display: grid; grid-template-columns: 300px 1fr; gap: 14px; min-height: 0; }
.file-tree { max-height: 70vh; overflow: auto; font-family: var(--mono); font-size: 12.5px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.file-tree button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 4px 10px; cursor: pointer; word-break: break-all; }
.file-tree button:hover, .file-tree button.on { background: var(--side); }
.file-view { margin: 0; max-height: 70vh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-size: 12.5px; white-space: pre; }
.svc-edit { border: 1px solid var(--line); border-radius: 10px; padding: 4px 14px 14px; margin: 10px 0; background: var(--bg); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }

/* ---------- dialog and toasts ---------- */
dialog { border: 0; border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); width: min(560px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(20,18,15,.45); }
dialog form, .dialog-body { padding: 22px 22px 18px; }
dialog h2 { margin: 0 0 4px; font: 600 1.25rem/1.3 var(--serif); }
#toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px; }
.toast.err { background: var(--bad); color: #fff; }

/* ---------- phones ---------- */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .shell.menu-open .side { transform: none; }
  .shell.menu-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 20; }
  .topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--bg); }
  .page { padding: 20px 16px 50px; }
  .proj-head { padding: 10px 16px 0; }
  .chat-list { display: none; }
  .chat-list.show { display: block; position: absolute; z-index: 15; height: 70vh; box-shadow: var(--shadow); background: var(--side); }
  .transcript { padding: 16px 14px 8px; }
  .composer-wrap { padding: 6px 10px 12px; }
  .grid2, .file-browser { grid-template-columns: 1fr; }
  .msg.user .bubble { max-width: 92%; }
}
@media (min-width: 861px) { .only-phone { display: none !important; } }
