/* ------------------------------------------------------------ tokens */
/* Palettes follow JetBrains New UI (IntelliJ Light / Dark). Theme is chosen in
   the menu: light (default), dark, or auto (follows the OS). */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #ebecf0;
  --border: #dfe1e5;
  --text: #1e1f22;
  --muted: #6c707e;
  --accent: #3574f0;
  --accent-text: #ffffff;
  --link: #2f65ca;
  --user-bubble: #edf3ff;
  --user-border: #c2d6fc;
  --code-bg: #f2f3f5;
  --code-text: #1e1f22;
  --ok: #208a3c;
  --warn: #a46704;
  --err: #db3b4b;
  --busy: #3574f0;
  --warn-bg: #fdf5e6;
  --err-bg: #fdecee;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  --radius: 10px;
  --gutter: 16px;
  --font: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #1e1f22;
  --surface: #2b2d30;
  --surface-2: #393b40;
  --border: #43454a;
  --text: #dfe1e5;
  --muted: #868a91;
  --accent: #3574f0;
  --accent-text: #ffffff;
  --link: #548af7;
  --user-bubble: #25324d;
  --user-border: #2e436e;
  --code-bg: #2b2d30;
  --code-text: #bcbec4;
  --ok: #5fb865;
  --warn: #e5a84b;
  --err: #f75464;
  --busy: #548af7;
  --warn-bg: #3d3223;
  --err-bg: #402929;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme='auto'] {
    --bg: #1e1f22;
    --surface: #2b2d30;
    --surface-2: #393b40;
    --border: #43454a;
    --text: #dfe1e5;
    --muted: #868a91;
    --accent: #3574f0;
    --accent-text: #ffffff;
    --link: #548af7;
    --user-bubble: #25324d;
    --user-border: #2e436e;
    --code-bg: #2b2d30;
    --code-text: #bcbec4;
    --ok: #5fb865;
    --warn: #e5a84b;
    --err: #f75464;
    --busy: #548af7;
    --warn-bg: #3d3223;
    --err-bg: #402929;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

/* ------------------------------------------------------------ base */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
#app { min-height: 100%; }
a { color: var(--link); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 12px; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; margin-top: 20px; }
code, pre { font-family: var(--mono); font-size: 0.86em; }
.icon { flex: none; display: block; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.err-text { color: var(--err); }
.center { display: grid; place-items: center; min-height: 100vh; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; margin-top: 12px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------ controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 0.95rem; cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); background: var(--accent); }
.btn.danger { color: var(--err); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.block { width: 100%; }
.btn.icon-only { padding: 0 10px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 10px; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.link { background: none; border: 0; padding: 0; color: var(--link); font: inherit; cursor: pointer; text-decoration: underline; }

input, select, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS from zooming on focus */
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 0.75rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.admin { color: var(--accent); }
.st-idle { color: var(--ok); }
.st-busy { color: var(--busy); }
.st-shell { color: var(--warn); }
.st-stopped, .st-unknown { color: var(--muted); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: currentColor; margin-right: 5px; vertical-align: 1px; }
.dot.st-busy { animation: pulse 1.2s infinite; }

.banner { padding: 10px 14px; border-radius: 10px; margin: 10px 0; font-size: 0.92rem; }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.banner.error { background: var(--err-bg); color: var(--err); }

.spinner-wrap { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 24px; color: var(--muted); }
.spinner {
  width: 18px; height: 18px; border-radius: 50%; display: inline-block; flex: none;
  border: 2px solid var(--border); border-top-color: var(--accent); animation: spin 0.8s linear infinite;
}
.spinner.light { border-color: rgba(255, 255, 255, 0.35); border-top-color: var(--accent-text); }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ------------------------------------------------------------ shell */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) var(--gutter) 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
.menu {
  position: absolute; right: var(--gutter); top: calc(100% + 4px); min-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column;
}
.menu a, .menu button {
  display: block; text-align: left; padding: 10px 12px; border-radius: 8px; border: 0; background: none;
  color: var(--text); font: inherit; text-decoration: none; cursor: pointer;
}
.menu a:hover, .menu button:hover { background: var(--surface-2); }
.menu-who { padding: 8px 12px; color: var(--muted); font-size: 0.85rem; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.conn-lost {
  position: fixed; left: 50%; top: calc(10px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 50;
  background: var(--warn-bg); color: var(--warn); padding: 6px 14px; border-radius: 99px; font-size: 0.85rem; box-shadow: var(--shadow);
}
.page { max-width: 820px; margin: 0 auto; padding: 16px var(--gutter) calc(32px + env(safe-area-inset-bottom)); }
.page.narrow { max-width: 480px; }

/* ------------------------------------------------------------ login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--gutter); }
.login {
  width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px 24px;
}
.login h1 { font-size: 1.35rem; text-align: center; margin: 0 0 6px; }
.login label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }
.logo { font-size: 40px; text-align: center; color: var(--accent); line-height: 1; }

/* ------------------------------------------------------------ sessions */
.toolbar { display: flex; gap: 8px; margin-bottom: 8px; }
.search { flex: 1; position: relative; display: flex; align-items: center; }
.search .icon { position: absolute; left: 12px; color: var(--muted); }
.search input { padding-left: 38px; min-height: 40px; }
.section-title {
  display: flex; align-items: center; gap: 6px; margin: 20px 0 8px; padding: 0;
  font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
  background: none; border: 0; font-family: inherit; cursor: default;
}
.section-title.toggle { cursor: pointer; }
.count { background: var(--surface-2); border-radius: 99px; padding: 0 7px; font-size: 0.75rem; }
.chev { display: inline-flex; transition: transform 0.15s; }
.chev.open { transform: rotate(90deg); }
.list { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.session-item {
  display: flex; gap: 12px; align-items: center; padding: 12px 14px; min-height: 60px;
  color: var(--text); text-decoration: none; border-bottom: 1px solid var(--border);
}
.session-item:last-child { border-bottom: 0; }
.session-item:hover { background: var(--surface-2); }
.si-main { flex: 1; min-width: 0; }
.si-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.si-sub { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.si-side { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex: none; }
.si-time { font-size: 0.75rem; color: var(--muted); }
.empty { text-align: center; padding: 48px 16px; }

.pick-list { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.pick {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left;
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; cursor: pointer;
}
.pick:hover:not(:disabled) { border-color: var(--accent); }
.pick:disabled { opacity: 0.55; cursor: default; }
.pick span:first-child { min-width: 0; }
.pick small { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ------------------------------------------------------------ chat */
.app.is-chat { height: 100dvh; }
.app.is-chat main { height: 100%; }
.chat { display: flex; flex-direction: column; height: 100%; position: relative; }
.chat-head {
  display: flex; align-items: center; gap: 4px; flex: none;
  padding: calc(6px + env(safe-area-inset-top)) 8px 6px; border-bottom: 1px solid var(--border); background: var(--bg);
}
.ch-title { flex: 1; min-width: 0; padding: 0 4px; }
.ch-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-sub { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.chat-inner { max-width: 820px; margin: 0 auto; padding: 12px var(--gutter) 20px; display: flex; flex-direction: column; gap: 10px; }
.more { align-self: center; }

.msg { max-width: 100%; }
.msg.user { align-self: flex-end; max-width: 88%; display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--user-bubble); border: 1px solid var(--user-border); border-radius: 14px 14px 4px 14px; padding: 9px 13px; min-width: 0; max-width: 100%; }
.msg.user.pending { opacity: 0.6; }
.plain { white-space: pre-wrap; overflow-wrap: anywhere; }
.meta { font-size: 0.72rem; color: var(--muted); margin-top: 2px; }
.msg.assistant { align-self: stretch; }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md table, .md blockquote { margin: 0 0 10px; }
.md ul, .md ol { padding-left: 22px; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.02rem; margin: 14px 0 6px; }
.md code { background: var(--code-bg); color: var(--code-text); padding: 1px 5px; border-radius: 4px; border: 1px solid var(--border); }
.md pre { background: var(--code-bg); color: var(--code-text); padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); overflow-x: auto; }
.md pre code { background: none; padding: 0; border: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 0.9rem; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; vertical-align: top; overflow-wrap: normal; min-width: 7ch; }
.md td code { white-space: nowrap; }
.md th { background: var(--surface-2); }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); }
.md strong { font-weight: 650; }
.md a { color: var(--link); }
.msg.user .md pre, .msg.user .md code { background: var(--surface); }

.tools { display: flex; flex-direction: column; gap: 4px; align-items: flex-start; }
.tool-group, .tool-head {
  display: flex; align-items: center; gap: 8px; max-width: 100%; text-align: left;
  padding: 6px 10px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); font: inherit; font-size: 0.85rem; cursor: pointer;
}
.tool-head b, .tool-group b { color: var(--text); font-weight: 600; flex: none; }
.tool-count { white-space: nowrap; flex: none; }
.tool-sum { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 0.8rem; min-width: 0; }
.tool { width: 100%; }
.tool-state { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--ok); }
.tool-state.err { background: var(--err); }
.tool-state.run { background: var(--busy); animation: pulse 1.2s infinite; }
.tool-body { margin: 4px 0 2px; }
.tool-body pre {
  margin: 0 0 4px; padding: 8px 10px; border-radius: 8px; background: var(--code-bg);
  max-height: 320px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.78rem;
}
.tool-out.err { color: var(--err); }
.sys { align-self: center; font-size: 0.8rem; color: var(--muted); text-align: center; padding: 0 12px; }
.turn-end { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 0.72rem; }
.turn-end::before, .turn-end::after { content: ''; flex: 1; border-top: 1px dashed var(--border); }
.working { display: flex; align-items: center; gap: 8px; color: var(--busy); font-size: 0.88rem; }
.pulse { width: 9px; height: 9px; border-radius: 50%; background: currentColor; animation: pulse 1.2s infinite; }
.to-bottom {
  position: absolute; right: var(--gutter); bottom: calc(84px + env(safe-area-inset-bottom));
  width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow); cursor: pointer;
}

.chat-foot { flex: none; border-top: 1px solid var(--border); background: var(--bg); padding: 8px var(--gutter) calc(8px + env(safe-area-inset-bottom)); }
.composer { display: flex; align-items: flex-end; gap: 8px; max-width: 820px; margin: 0 auto; }
.composer textarea { resize: none; min-height: 44px; max-height: 40vh; line-height: 1.4; padding: 11px 14px; border-radius: 22px; }
.composer .btn { min-height: 44px; border-radius: 22px; }
.composer .send { width: 52px; padding: 0; }
.composer .stop { width: 44px; padding: 0; color: var(--err); border-color: var(--err); }
.note { max-width: 820px; margin: 0 auto; font-size: 0.9rem; color: var(--muted); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.note.warn { color: var(--warn); }
.note code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
pre.screen {
  background: #111; color: #ddd; padding: 10px; border-radius: 8px; overflow: auto; max-height: 65vh;
  font-size: 11px; line-height: 1.3; white-space: pre;
}

/* ------------------------------------------------------------ admin */
.tabs { display: flex; gap: 4px; overflow-x: auto; margin: 0 calc(-1 * var(--gutter)) 16px; padding: 0 var(--gutter); scrollbar-width: none; }
.tab { padding: 8px 14px; border-radius: 99px; color: var(--muted); text-decoration: none; white-space: nowrap; font-size: 0.92rem; }
.tab.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.cards { display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.card.disabled { opacity: 0.65; }
.card-head { display: flex; justify-content: space-between; gap: 10px; }
.card-head > div { min-width: 0; overflow-wrap: anywhere; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.card-actions .btn { min-height: 36px; padding: 0 12px; font-size: 0.88rem; }
.grant-list { display: flex; flex-direction: column; gap: 6px; }
.grant { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.grant > div { min-width: 0; overflow-wrap: anywhere; }
.level { width: auto; flex: none; padding: 6px 8px; font-size: 14px; }
.sticky-actions { position: sticky; bottom: -16px; background: var(--surface); padding: 12px 0 4px; }
.audit { display: flex; flex-direction: column; }
.audit-row { padding: 9px 2px; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 0.9rem; }
.audit-row.bad .ar-head span:last-child { color: var(--err); }
.ar-head { display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.ar-time { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.ar-detail { color: var(--muted); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ar-detail.open { white-space: pre-wrap; overflow-wrap: anywhere; }
pre.copy { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 0.78rem; user-select: all; }

/* ------------------------------------------------------------ modal / toasts */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100; background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--surface); width: 100%; max-height: 92dvh; display: flex; flex-direction: column;
  border-radius: 18px 18px 0 0; box-shadow: var(--shadow); padding-bottom: env(safe-area-inset-bottom);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 8px 4px 18px; }
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 4px 18px 18px; overflow-y: auto; }
.confirm-text { margin-top: 0; }
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; padding: 24px; }
  .modal { max-width: 460px; border-radius: 16px; max-height: 85vh; }
  .modal.wide { max-width: 720px; }
}
.toasts {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 200;
  display: flex; flex-direction: column; gap: 8px; align-items: center; width: calc(100% - 32px); max-width: 420px; pointer-events: none;
}
.toast {
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 0.9rem;
  box-shadow: var(--shadow); transition: opacity 0.3s, transform 0.3s; text-align: center;
}
.toast.error { background: var(--err); color: #fff; }
.toast.hide { opacity: 0; transform: translateY(8px); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
