:root {
  --bg: #0f1115;
  --bg2: #151821;
  --bg3: #1b1f2a;
  --bg4: #232838;
  --line: #2a3042;
  --fg: #e6e8ee;
  --fg2: #a9afc0;
  --fg3: #6f7690;
  --accent: #d97757;
  --accent2: #e8916f;
  --ok: #3fb950;
  --warn: #d29922;
  --err: #f85149;
  --info: #58a6ff;
  --radius: 8px;
  --mono: ui-monospace, "JetBrains Mono", "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 14px/1.45 var(--sans); }
body { overflow: hidden; }
#app { height: 100%; }
a { color: var(--info); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #2c3245; border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* controls */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 7px; border: 1px solid var(--line); background: var(--bg3); cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--bg4); border-color: #39405a; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.danger { color: #ff8d86; }
.btn.danger:hover { background: #3a1d1f; border-color: #6e2a2a; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--bg3); }
.btn.sm { padding: 3px 8px; font-size: 12.5px; border-radius: 6px; }
.btn.icon { padding: 5px; }
input[type=text], input[type=password], input[type=number], input:not([type]), select, textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 7px 10px; outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; font-family: var(--mono); font-size: 12.5px; }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--fg2); }
label.check { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--fg2); }
.dim { color: var(--fg3); }
.small { font-size: 12.5px; }
.err-text { color: var(--err); }
.ok-text { color: var(--ok); }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px; background: var(--bg3); border: 1px solid var(--line); white-space: nowrap; }
.pill.ok { color: #7ee2a8; border-color: #23493a; background: #12241c; }
.pill.warn { color: #f2cc60; border-color: #4d3c12; background: #241d0c; }
.pill.err { color: #ff9a92; border-color: #5c2424; background: #2a1414; }
.pill.info { color: #9ecbff; border-color: #1f3a5f; background: #0f1d30; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fg3); flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px #3fb95022; }
.dot.warn { background: var(--warn); }
.dot.busy { background: var(--info); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 16px; }
.card h3 { margin: 0 0 12px; font-size: 15px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { text-align: left; font-weight: 500; color: var(--fg3); padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.t td { padding: 6px 8px; border-bottom: 1px solid #20253280; vertical-align: middle; }
table.t tr:hover td { background: #ffffff05; }
.kbd { font-family: var(--mono); font-size: 11.5px; padding: 1px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--bg3); }
.bar { height: 6px; background: var(--bg4); border-radius: 4px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--info); border-radius: 4px; }
.bar.hot > i { background: var(--warn); }
.bar.crit > i { background: var(--err); }

/* layout */
.app { display: grid; grid-template-rows: 44px 1fr; grid-template-columns: 264px 1fr; height: 100%; }
.app.nosb { grid-template-columns: 0 1fr; }
.top { grid-column: 1 / 3; display: flex; align-items: center; gap: 10px; padding: 0 10px; background: var(--bg2); border-bottom: 1px solid var(--line); min-width: 0; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: .2px; }
.brand .logo { width: 24px; height: 24px; border-radius: 6px; background: var(--accent); display: grid; place-items: center; }
.top .metrics { display: flex; gap: 12px; font-size: 12px; color: var(--fg2); margin-left: 8px; }
.top .metrics span b { color: var(--fg); font-weight: 500; }
.sb { background: var(--bg2); border-right: 1px solid var(--line); overflow: auto; display: flex; flex-direction: column; min-width: 0; }
.app.nosb .sb { display: none; }
.sb h4 { margin: 14px 12px 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--fg3); font-weight: 600; display: flex; align-items: center; justify-content: space-between; }
.sb .folder { margin: 8px 12px 2px; font-size: 11.5px; color: var(--fg3); }
.node { display: flex; align-items: center; gap: 8px; padding: 6px 10px 6px 12px; margin: 1px 6px; border-radius: 7px; cursor: pointer; min-width: 0; }
.node:hover { background: var(--bg3); }
.node.sel { background: var(--bg4); }
.node .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node .acts { display: none; gap: 2px; }
.node:hover .acts, .node.sel .acts { display: flex; }
.node .os { font-size: 10px; font-weight: 600; color: var(--fg3); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
.sb .foot { margin-top: auto; padding: 8px 6px; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.sb .foot .btn { justify-content: flex-start; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tabs { display: flex; align-items: stretch; background: var(--bg2); border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { display: flex; align-items: center; gap: 7px; padding: 0 8px 0 12px; height: 36px; border-right: 1px solid var(--line); cursor: pointer; color: var(--fg2); white-space: nowrap; max-width: 240px; user-select: none; }
.tab:hover { background: var(--bg3); }
.tab.act { background: var(--bg); color: var(--fg); box-shadow: inset 0 2px 0 var(--accent); }
.tab.act2 { background: var(--bg); color: var(--fg); box-shadow: inset 0 2px 0 var(--info); }
.tab .t { overflow: hidden; text-overflow: ellipsis; }
.tab .x { opacity: .5; border-radius: 4px; padding: 0 3px; }
.tab .x:hover { opacity: 1; background: var(--bg4); }
.tabs .tools { margin-left: auto; display: flex; align-items: center; padding: 0 6px; gap: 2px; position: sticky; right: 0; background: var(--bg2); }
.panes { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr; }
.panes.split { grid-template-columns: 1fr 1fr; }
.pane { grid-row: 1; min-width: 0; min-height: 0; display: none; flex-direction: column; overflow: hidden; }
.pane.show { display: flex; }
.panes.split .pane.right { border-left: 1px solid var(--line); }
.panes.split .pane.focus { outline: 1px solid #d9775744; outline-offset: -1px; }
.page { overflow: auto; padding: 18px; flex: 1; }
.page h2 { margin: 0 0 14px; font-size: 18px; display: flex; align-items: center; gap: 10px; }
.empty { display: grid; place-items: center; height: 100%; color: var(--fg3); text-align: center; padding: 24px; }
.empty .big { font-size: 22px; color: var(--fg); margin-bottom: 6px; }

/* terminal */
.term-wrap { display: flex; flex-direction: column; flex: 1; min-height: 0; background: #0b0d11; }
.term-bar { display: flex; align-items: center; gap: 6px; padding: 5px 8px; background: var(--bg2); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.term-bar .title { font-weight: 550; margin-right: 6px; }
.term { flex: 1; min-height: 0; padding: 6px 0 0 8px; position: relative; }
.term .xterm { height: 100%; }
.term-overlay { position: absolute; inset: 0; display: grid; place-items: center; background: #0b0d11d9; z-index: 5; text-align: center; }
.quick { display: flex; gap: 6px; padding: 6px 8px; background: var(--bg2); border-top: 1px solid var(--line); }
.quick textarea { flex: 1; min-height: 36px; max-height: 160px; resize: none; font-family: var(--sans); font-size: 14px; }

/* files */
.files { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.files .path { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); background: var(--bg2); align-items: center; flex-wrap: wrap; }
.files .path input { flex: 1; font-family: var(--mono); font-size: 12.5px; min-width: 160px; }
.files .list { flex: 1; overflow: auto; }
.files .list.drop { outline: 2px dashed var(--accent); outline-offset: -6px; background: #d977570a; }
.files tr.f td:first-child { cursor: pointer; }
.files .ico { width: 18px; display: inline-block; color: var(--fg3); }
.files .ico.d { color: #e3b341; }

/* editor */
.editor { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.editor .CodeMirror { flex: 1; height: auto; font-family: var(--mono); font-size: 13px; background: #0b0d11; }
.CodeMirror-gutters { background: #0b0d11 !important; border-right: 1px solid var(--line) !important; }
.cm-host { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.cm-host.box { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; height: 360px; flex: none; }
.cm-host .CodeMirror { flex: 1; height: 100%; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: #05070acc; display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; width: min(620px, 100%); max-height: calc(100vh - 32px); overflow: auto; padding: 20px; box-shadow: 0 20px 60px #000a; }
.modal h3 { margin: 0 0 14px; font-size: 17px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.cmdbox { display: flex; gap: 6px; align-items: stretch; }
.cmdbox code { flex: 1; background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 9px 11px; overflow-x: auto; white-space: nowrap; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 6px 12px; cursor: pointer; color: var(--fg2); }
.seg button.on { background: var(--bg4); color: var(--fg); }

/* toast */
.toasts { position: fixed; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: min(420px, calc(100vw - 28px)); }
.toast { background: var(--bg3); border: 1px solid var(--line); border-left: 3px solid var(--info); padding: 10px 14px; border-radius: 8px; box-shadow: 0 8px 30px #0008; white-space: pre-wrap; word-break: break-word; }
.toast.err { border-left-color: var(--err); }
.toast.ok { border-left-color: var(--ok); }

/* login */
.login { height: 100%; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 600px at 50% -10%, #d9775722, transparent); }
.login form { width: min(360px, 100%); background: var(--bg2); border: 1px solid var(--line); border-radius: 14px; padding: 26px; display: flex; flex-direction: column; gap: 14px; }
.login .brand { font-size: 20px; justify-content: center; margin-bottom: 4px; }

/* log */
pre.log { margin: 0; padding: 10px 12px; font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-all; overflow: auto; flex: 1; background: #0b0d11; }
.chat { display: flex; flex-direction: column; gap: 10px; }
.chat .m { padding: 10px 12px; border-radius: 10px; background: var(--bg3); white-space: pre-wrap; word-break: break-word; }
.chat .m.user { background: #2a1c16; border: 1px solid #4a2d22; align-self: flex-end; max-width: 85%; }
.chat .m.tool { background: transparent; color: var(--fg3); font-family: var(--mono); font-size: 12px; padding: 2px 12px; }
.shot { max-width: 100%; border: 1px solid var(--line); border-radius: 8px; }

@media (max-width: 820px) {
  .app { grid-template-columns: 0 1fr; }
  .app.sbopen .sb { display: flex; position: fixed; top: 48px; bottom: 0; left: 0; width: min(300px, 86vw); z-index: 40; box-shadow: 10px 0 40px #000a; }
  .app:not(.sbopen) .sb { display: none; }
  .top .metrics { display: none; }
  .panes.split { grid-template-columns: 1fr; }
  .panes.split .pane.right { display: none; }
  .page { padding: 12px; }
  .hide-m { display: none !important; }
}

/* ===== chat ===== */
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.seg.sm button { padding: 3px 10px; font-size: 12.5px; }
.chat-view { display: flex; flex-direction: column; flex: 1; min-height: 0; background: var(--bg); }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--bg2); border-bottom: 1px solid var(--line); }
.chat-list { flex: 1; overflow-y: auto; padding: 16px max(12px, calc((100% - 820px) / 2)); display: flex; flex-direction: column; gap: 10px; overscroll-behavior: contain; }
.chat-empty { text-align: center; color: var(--fg2); padding: 48px 16px; }
.chat-empty .big { font-size: 19px; color: var(--fg); margin-bottom: 6px; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg .bubble { max-width: min(88%, 720px); padding: 9px 13px; border-radius: 16px; line-height: 1.5; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .bubble { background: #3a241b; border: 1px solid #5a3626; border-bottom-right-radius: 5px; white-space: pre-wrap; }
.msg.assistant .bubble { background: transparent; padding-left: 2px; max-width: 100%; }
.md p { margin: 0 0 8px; }
.md p:last-child { margin-bottom: 0; }
.md h4 { margin: 10px 0 6px; font-size: 15px; }
.md ul, .md ol { margin: 4px 0 8px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md code { background: var(--bg3); border: 1px solid var(--line); border-radius: 5px; padding: 0 5px; font-size: 12.5px; }
.md pre.code { background: #0b0d11; border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; overflow-x: auto; font-family: var(--mono); font-size: 12.5px; margin: 6px 0 8px; white-space: pre; }
.tools-group { display: flex; flex-direction: column; gap: 2px; border-left: 2px solid var(--line); margin-left: 4px; padding-left: 10px; }
.tool-line { display: flex; gap: 8px; align-items: baseline; font-size: 12.5px; color: var(--fg2); padding: 2px 0; cursor: default; min-width: 0; }
.tool-line .ti { width: 16px; text-align: center; flex: none; color: var(--fg3); font-family: var(--mono); }
.tool-line .tn { flex: none; color: var(--fg3); }
.tool-line .td { font-family: var(--mono); font-size: 12px; color: var(--fg2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.tool-line.open .td { white-space: pre-wrap; word-break: break-all; }
.tool-more { font-size: 12px; color: var(--info); cursor: pointer; padding: 2px 0; }
.working { display: flex; align-items: center; gap: 8px; color: var(--fg2); font-size: 13px; padding: 6px 2px; }
.working.warn { color: #f2cc60; }
.perm { background: #241d0c; border: 1px solid #4d3c12; border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.perm-q { font-weight: 600; color: #f2cc60; }
.perm-ctx { margin: 0; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-all; color: var(--fg2); max-height: 200px; overflow: auto; background: #0b0d1180; border-radius: 8px; padding: 8px; }
.perm-opts { display: flex; flex-direction: column; gap: 6px; }
.perm-opts .btn { justify-content: flex-start; white-space: normal; text-align: left; padding: 10px 12px; }
.screen { margin: 0; font-family: var(--mono); font-size: 11px; line-height: 1.35; white-space: pre; overflow-x: auto; background: #0b0d11; border: 1px solid var(--line); border-radius: 8px; padding: 8px; color: var(--fg2); }
.composer { border-top: 1px solid var(--line); background: var(--bg2); padding: 6px max(10px, calc((100% - 820px) / 2)) calc(8px + env(safe-area-inset-bottom)); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--bg3); color: var(--fg2); border-radius: 999px; padding: 4px 11px; font-size: 12.5px; cursor: pointer; }
.chip:active, .chip.on { background: var(--bg4); color: var(--fg); }
.input-row { display: flex; align-items: flex-end; gap: 8px; }
.input-row textarea { flex: 1; resize: none; min-height: 40px; max-height: 180px; border-radius: 20px; padding: 9px 14px; font-family: var(--sans); font-size: 15px; line-height: 1.4; background: var(--bg); }
.btn.round { border-radius: 50%; width: 40px; height: 40px; justify-content: center; flex: none; padding: 0; }
.chat-ended { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 12px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--bg2); color: var(--fg2); flex-wrap: wrap; }
.chat-cards { display: flex; flex-direction: column; gap: 8px; max-width: 820px; }
.chat-card { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; cursor: pointer; }
.chat-card:active { background: var(--bg3); }

/* ===== mobile shell ===== */
.bottom-nav { display: none; }
.sb-backdrop { display: none; }
html, body { height: 100dvh; }
@media (max-width: 820px) {
  .app { grid-template-rows: 48px 1fr auto; height: var(--app-h, 100dvh); }
  html.kb .bottom-nav { display: none; }
  .main { grid-row: 2; grid-column: 1 / 3; }
  .top { padding-top: env(safe-area-inset-top); }
  .bottom-nav { display: flex; grid-column: 1 / 3; grid-row: 3; justify-content: space-around; align-items: center; background: var(--bg2); border-top: 1px solid var(--line); padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); }
  .bn { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; background: none; border: 0; color: var(--fg3); font-size: 10.5px; padding: 4px 10px; min-width: 56px; cursor: pointer; }
  .bn.on { color: var(--accent2); }
  .bn.new { background: var(--accent); color: #fff; border-radius: 16px; width: 52px; height: 40px; min-width: 0; padding: 0; justify-content: center; }
  .bn-badge { position: absolute; top: 0; right: 10px; background: var(--warn); color: #000; font-style: normal; font-size: 10px; font-weight: 700; border-radius: 999px; padding: 0 5px; }
  .sb-backdrop { display: block; position: fixed; inset: 48px 0 0 0; background: #0009; z-index: 39; }
  .app.sbopen .sb { padding-bottom: env(safe-area-inset-bottom); }
  /* inputs at 16px stop iOS from zooming on focus */
  input, select, textarea, .input-row textarea { font-size: 16px !important; }
  .btn { padding: 8px 12px; }
  .btn.sm { padding: 6px 10px; }
  .btn.icon { padding: 8px; }
  .node { padding: 10px 12px; }
  .node .acts { display: flex; }
  .tab { height: 38px; max-width: 170px; }
  .term-bar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .term-bar::-webkit-scrollbar { display: none; }
  .term-bar .title { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: none; }
  .modal-bg { padding: 0; align-items: end; }
  .modal { width: 100%; max-height: 92dvh; border-radius: 16px 16px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .page table.t { display: block; overflow-x: auto; white-space: nowrap; }
  .grid2 { grid-template-columns: 1fr; }
  .chat-head { padding: 8px 10px; }
  .chat-list { padding: 12px 10px; }
  .msg .bubble { max-width: 92%; }
  .toasts { bottom: calc(72px + env(safe-area-inset-bottom)); left: 12px; right: 12px; max-width: none; }
}

/* ===== phone terminal ===== */
.keys-row { display: none; }
@media (max-width: 820px) {
  /* Pin the app to the visible area: iOS pans the page when the keyboard opens. */
  .app { position: fixed; left: 0; right: 0; top: var(--app-top, 0); }
  /* While typing, give the screen to the terminal/chat: no top bar, no tabs. */
  html.kb .app { grid-template-rows: 0 1fr 0; }
  html.kb .top, html.kb .tabs { display: none; }
  .term-bar { padding: 4px 6px; gap: 4px; min-height: 0; }
  .term-bar .title { font-size: 13px; }
  .keys-row { display: flex; gap: 4px; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); background: var(--bg2); border-top: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; flex: none; }
  .keys-row::-webkit-scrollbar { display: none; }
  .keys-row button { flex: 1 0 auto; min-width: 40px; height: 36px; border: 1px solid var(--line); background: var(--bg3); color: var(--fg); border-radius: 7px; font-family: var(--mono); font-size: 13px; padding: 0 8px; }
  .keys-row button:active { background: var(--bg4); }
  .term { padding: 2px 0 0 4px; }
}
