/* ===== tokens ===== */
:root {
  --bg: #ffffff;
  --bg2: #f7f7f8;
  --bg3: #ececf1;
  --border: #e5e5e8;
  --fg: #0d0d0d;
  --muted: #6e6e80;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --green: #16a34a;
  --red: #dc2626;
  --yellow: #d97706;
  --purple: #7c3aed;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.6 -apple-system, "PingFang SC", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
#root { height: 100%; }

/* ===== base / form controls ===== */
button {
  background: var(--accent); color: var(--accent-fg); border: 0; border-radius: 8px;
  padding: 7px 14px; font-size: 14px; cursor: pointer; font-family: inherit;
}
button:hover { filter: brightness(0.95); }
button:disabled { opacity: .5; cursor: default; filter: none; }
button.ghost { background: transparent; color: var(--fg); border: 1px solid var(--border); }
button.ghost:hover { background: var(--bg3); }
button.ghost.link { border: none; padding: 0; align-self: flex-start; text-decoration: underline; font-size: 13px; }
button.allow { background: var(--green); }
button.deny { background: var(--red); }
input, textarea, select {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 14px; font-family: inherit; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: vertical; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
a { color: var(--accent); text-decoration: none; }
.muted { color: var(--muted); font-size: 12px; }
.err { color: var(--red); font-size: 13px; }
.ok-note { color: var(--green); font-size: 13px; }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; }

/* ===== login / register ===== */
.login-page { height: 100%; display: flex; align-items: center; justify-content: center; background: var(--bg2); }
.login-card { background: var(--bg); border: 1px solid var(--border); border-radius: 14px; padding: 32px; width: 340px; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 2px 16px rgba(0,0,0,.06); }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; color: var(--muted); }
.auth-tabs { display: flex; gap: 4px; background: var(--bg3); border-radius: 8px; padding: 3px; }
.auth-tabs button { flex: 1; background: transparent; color: var(--muted); padding: 6px; }
.auth-tabs button.active { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ===== app shell (sidebar + main pane) ===== */
.app-shell { height: 100%; display: flex; }
.sidebar {
  width: 272px; flex-shrink: 0; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; height: 100%;
}
.sidebar-top { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.brand { font-weight: 700; font-size: 15px; padding: 4px 4px 0; }
.new-chat-btn { width: 100%; text-align: left; background: var(--bg); color: var(--fg); border: 1px solid var(--border); }
.new-chat-btn:hover { background: var(--bg3); }
.sidebar-list { flex: 1; overflow-y: auto; padding: 4px 8px; display: flex; flex-direction: column; gap: 2px; }
.sidebar-item {
  display: flex; flex-direction: column; gap: 3px; padding: 8px 10px; border-radius: 8px;
  color: var(--fg); border-left: 3px solid transparent; cursor: grab;
}
.sidebar-item:active { cursor: grabbing; }
.sidebar-item:hover { background: var(--bg3); }
.sidebar-item.active { background: var(--bg3); border-left-color: var(--accent); }
.sidebar-item-title { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-item.open-pane .sidebar-item-title::before { content: '●'; color: var(--accent); font-size: 8px; margin-right: 5px; }
.sidebar-item-meta { display: flex; gap: 6px; align-items: center; }
.sidebar-empty { padding: 16px 10px; }
.sidebar-footer { border-top: 1px solid var(--border); padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.sidebar-btn { width: 100%; text-align: left; }
.node-strip { display: flex; gap: 6px; flex-wrap: wrap; }
.node-chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); background: var(--bg3); padding: 3px 9px; border-radius: 20px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--red); display: inline-block; }
.dot.online { background: var(--green); }
.user-menu { position: relative; }
.user-menu-btn { display: flex; justify-content: space-between; }
.user-menu-popover {
  position: absolute; bottom: calc(100% + 4px); left: 0; right: 0; background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.1);
  display: flex; flex-direction: column; padding: 4px; gap: 2px; z-index: 30;
}
.user-menu-popover button { text-align: left; border: none; }

.main-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; position: relative; }
.nav-toggle { display: none; position: absolute; top: 10px; left: 10px; z-index: 12; background: var(--bg); border: 1px solid var(--border); color: var(--fg); }
.sidebar-backdrop { display: none; }

/* ---- workspace: auto-tiling multi-pane area ---- */
.workspace {
  flex: 1; min-height: 0; overflow: auto;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  grid-auto-rows: minmax(0, 1fr); gap: 1px; background: var(--border);
}
.pane { background: var(--bg); min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.pane-close-btn { padding: 4px 9px; }
.empty-shell { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); grid-column: 1 / -1; }

/* ===== modal ===== */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: var(--bg); border: 1px solid var(--border); border-radius: 14px; padding: 22px; width: 520px; max-width: 100%; max-height: 90vh; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; box-shadow: 0 8px 32px rgba(0,0,0,.15); }
.modal h2 { margin: 0; font-size: 17px; }
.modal label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 2fr 1fr; gap: 10px; }
.row-inline { flex-direction: row !important; align-items: center; gap: 8px !important; }
.row-inline input[type="checkbox"] { width: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
.admin-section { border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; display: flex; flex-direction: column; gap: 10px; margin: 0; }
.admin-section legend { color: var(--muted); font-size: 12px; padding: 0 4px; }

/* ===== task pane ===== */
.task-pane { height: 100%; display: flex; flex-direction: column; }
.pane-header {
  display: flex; align-items: center; gap: 14px; padding: 10px 16px;
  border-bottom: 1px solid var(--border); background: var(--bg);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap;
}
.task-head { flex: 1; min-width: 0; }
.task-head h2 { margin: 0; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.chip { font-size: 11px; padding: 1px 8px; border-radius: 10px; background: var(--bg2); border: 1px solid var(--border); }
.st-queued { color: var(--muted); }
.st-running { color: var(--accent); border-color: var(--accent); }
.st-waiting { color: var(--yellow); border-color: var(--yellow); }
.st-review { color: var(--purple); border-color: var(--purple); }
.st-done { color: var(--green); border-color: var(--green); }
.st-failed { color: var(--red); border-color: var(--red); }

.tabs { display: flex; gap: 4px; }
.tabs button { background: transparent; color: var(--muted); border-radius: 8px; padding: 5px 12px; }
.tabs button.active { background: var(--bg3); color: var(--fg); }

/* ---- chat: full-width alternating rows, ChatGPT-style ---- */
.chat-list { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.chat-list > * { content-visibility: auto; contain-intrinsic-size: auto 60px; }
.msg-row { padding: 14px 16px; }
.msg-row.assistant { background: var(--bg2); }
.msg-col { max-width: 860px; margin: 0 auto; word-break: break-word; }
.md p { margin: 6px 0; }
.md pre { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; overflow-x: auto; }
.md code { background: var(--bg2); padding: 1px 5px; border-radius: 4px; }
.md pre code { padding: 0; background: none; }
.md img { max-width: 100%; }
.md table { border-collapse: collapse; }
.md td, .md th { border: 1px solid var(--border); padding: 4px 10px; }

.tool-row { border: 1px solid var(--border); border-radius: 8px; background: var(--bg); overflow: hidden; max-width: 860px; margin: 8px auto; }
.tool-row.tool-err { border-color: var(--red); }
.tool-head { width: 100%; display: flex; gap: 8px; align-items: center; background: none; color: var(--fg); text-align: left; padding: 6px 12px; border-radius: 0; }
.tool-icon { color: var(--muted); }
.tool-name { color: var(--purple); font-weight: 600; font-size: 12.5px; }
.tool-sum { color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.tool-status.ok { color: var(--green); }
.tool-status.bad { color: var(--red); }
.tool-body { border-top: 1px solid var(--border); padding: 8px 12px; }
.tool-section { color: var(--muted); font-size: 11px; text-transform: uppercase; margin: 6px 0 3px; }
.tool-body pre { margin: 0; max-height: 320px; overflow: auto; white-space: pre-wrap; word-break: break-all; }

.perm-card { border: 1px solid var(--yellow); border-radius: 10px; padding: 12px 14px; background: rgba(217,119,6,.06); max-width: 860px; margin: 8px auto; }
.perm-card.active { box-shadow: 0 0 0 1px var(--yellow); }
.perm-title { margin-bottom: 6px; }
.perm-input { background: var(--bg2); border-radius: 8px; padding: 8px 10px; overflow-x: auto; margin: 6px 0; }
.perm-actions { display: flex; gap: 10px; margin-top: 8px; }

.result-card { display: flex; gap: 10px; align-items: baseline; border: 1px dashed var(--border); border-radius: 10px; padding: 8px 14px; color: var(--fg); flex-wrap: wrap; max-width: 860px; margin: 8px auto; }
.sys-note { color: var(--muted); font-size: 12.5px; text-align: center; padding: 2px 0; }

.jump-latest { position: fixed; bottom: 110px; right: 24px; border-radius: 20px; box-shadow: 0 4px 14px rgba(0,0,0,.15); z-index: 20; }
.review-bar { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 8px 16px; background: rgba(124,58,237,.06); border-top: 1px solid var(--purple); }
.composer {
  display: flex; gap: 10px; align-items: flex-end; margin: 0 16px calc(14px + env(safe-area-inset-bottom));
  padding: 10px 14px; border: 1px solid var(--border); border-radius: 24px; background: var(--bg);
  box-shadow: 0 2px 10px rgba(0,0,0,.06);
}
.composer textarea { flex: 1; border: none; padding: 4px 2px; resize: none; }
.composer textarea:focus { outline: none; }
.composer button { border-radius: 18px; }

.tab-scroll { flex: 1; overflow-y: auto; padding: 16px; }
.diff-stat { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; overflow-x: auto; }
.diff-file { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; overflow: hidden; }
.diff-file-head { width: 100%; text-align: left; background: var(--bg2); color: var(--fg); font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; border-radius: 0; }
.diff-body { margin: 0; overflow-x: auto; background: var(--bg); padding: 6px 0; }
.dl { padding: 0 12px; white-space: pre; min-height: 18px; }
.dl-add { background: rgba(22,163,74,.12); color: #15803d; }
.dl-del { background: rgba(220,38,38,.12); color: #b91c1c; }
.dl-hunk { color: var(--accent); background: rgba(37,99,235,.06); }

/* ---- draft pane (instant new-chat composer with path picker) ---- */
.draft-body { flex: 1; overflow-y: auto; padding: 28px 16px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.path-chip-row { width: 100%; max-width: 640px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chip-with-picker { position: relative; }
.path-chip { background: var(--bg2); color: var(--fg); border: 1px solid var(--border); font-size: 13px; padding: 6px 12px; border-radius: 20px; display: inline-flex; align-items: center; gap: 6px; }
.path-chip.selected { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.inline-chip-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.path-picker {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 20; min-width: 280px; max-width: 420px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 4px 16px rgba(0,0,0,.1);
  padding: 6px; display: flex; flex-direction: column; gap: 2px;
}
.path-picker-item {
  display: flex; align-items: center; gap: 7px; text-align: left; background: transparent; color: var(--fg);
  border: none; padding: 7px 10px; border-radius: 6px; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.path-picker-item:hover { background: var(--bg3); }
.path-picker-custom { padding: 4px 6px; position: relative; }
.path-suggestions { display: flex; flex-direction: column; gap: 2px; margin-top: 4px; max-height: 180px; overflow-y: auto; }
.advanced-inline { width: 100%; max-width: 640px; }
.draft-empty { text-align: center; margin-top: 8px; }

/* ---- existing-session picker ---- */
.session-list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; }
.session-card {
  display: flex; flex-direction: column; gap: 4px; text-align: left; background: var(--bg2); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.session-card:hover { border-color: var(--accent); }
.session-preview {
  font-size: 13px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box;
  -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}

/* ---- settings: model list fetch + profile rows ---- */
.fetch-models { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
.fetch-models select { width: auto; }
.profile-row { justify-content: space-between; }
.profile-row span:first-child { font-weight: 600; }
.profile-row span.muted { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.info-view dl { display: grid; grid-template-columns: 110px 1fr; gap: 6px 14px; max-width: 700px; }
.info-view dt { color: var(--muted); }
.info-view dd { margin: 0; word-break: break-all; }
.info-actions { display: flex; gap: 10px; margin: 18px 0; }
.spec-box { border: 1px solid var(--border); border-radius: 10px; padding: 4px 16px 10px; max-width: 700px; }

/* ===== responsive: sidebar becomes an off-canvas drawer ===== */
@media (max-width: 720px) {
  .app-shell { position: relative; overflow: hidden; }
  .sidebar {
    position: absolute; inset: 0 25% 0 0; z-index: 15; transform: translateX(-100%);
    transition: transform .2s ease; box-shadow: 2px 0 12px rgba(0,0,0,.1);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop { display: block; position: absolute; inset: 0; background: rgba(0,0,0,.3); z-index: 14; }
  .nav-toggle { display: block; }
  .main-pane { width: 100%; }
  .workspace { grid-template-columns: 1fr; }
  .pane-header { gap: 8px; padding-left: 52px; }
  .tabs { width: 100%; justify-content: stretch; }
  .tabs button { flex: 1; }
  .row2 { grid-template-columns: 1fr; }
  .jump-latest { bottom: 130px; }
  .composer { margin-left: 8px; margin-right: 8px; }
}
