:root {
  color-scheme: dark;
  --bg: #0a0c11;
  --bg-soft: #10131b;
  --card: #151926;
  --card-2: #1b2030;
  --line: #262d40;
  --text: #eef1f8;
  --muted: #98a1b8;
  --faint: #6b748c;
  --accent: #6f8cff;
  --accent-2: #55d6c2;
  --warn: #f2b35c;
  --danger: #ff6b6b;
  --ok: #4cd69a;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(111, 140, 255, .16), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(85, 214, 194, .10), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  overscroll-behavior-y: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
.hidden { display: none !important; }

#app {
  min-height: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 10px) 14px calc(var(--safe-bottom) + 22px);
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 2px 12px;
  background: linear-gradient(to bottom, var(--bg) 70%, rgba(10, 12, 17, 0));
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 13px;
  color: #dfe6ff;
  background: linear-gradient(145deg, #2a3560, #1a2140);
  border: 1px solid #33406e;
  box-shadow: 0 6px 18px rgba(36, 52, 110, .35);
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: 17px; letter-spacing: .3px; }
.brand-text span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 6px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px;
  display: grid; place-items: center;
  color: var(--muted);
  background: rgba(255,255,255,.03);
  border: 1px solid transparent;
  transition: .18s ease;
}
.icon-btn:active { transform: scale(.94); background: rgba(255,255,255,.07); color: var(--text); }

/* ---------- overview ---------- */
.overview {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  margin: 2px 0 14px;
}
.stat {
  background: linear-gradient(160deg, var(--card), var(--bg-soft));
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: 11px 12px;
}
.stat b { display: block; font-size: 19px; line-height: 1.2; font-weight: 650; }
.stat span { font-size: 11.5px; color: var(--muted); }
.stat.running b { color: var(--accent-2); }

/* ---------- session cards ---------- */
.session-list { display: flex; flex-direction: column; gap: 10px; }
.session-card {
  position: relative;
  text-align: left;
  width: 100%;
  background: linear-gradient(165deg, var(--card), #121622 65%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  transition: .18s ease;
  overflow: hidden;
}
.session-card:active { transform: scale(.985); border-color: #3a4567; }
.session-card::before {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 3px;
  background: linear-gradient(var(--accent), var(--accent-2));
  opacity: 0; transition: .2s;
}
.session-card.running::before { opacity: 1; }
.session-top { display: flex; align-items: center; gap: 8px; }
.session-title {
  flex: 1; min-width: 0;
  font-size: 15.5px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-time { font-size: 11.5px; color: var(--faint); flex: none; }
.session-cwd {
  margin-top: 5px;
  font-family: var(--mono); font-size: 11.5px; color: var(--faint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-meta { display: flex; align-items: center; gap: 8px; margin-top: 9px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; padding: 3px 8px; border-radius: 999px;
  color: var(--muted); background: rgba(255,255,255,.045);
  border: 1px solid rgba(255,255,255,.05);
  white-space: nowrap;
}
.pill.run { color: #07251d; background: var(--accent-2); border-color: transparent; font-weight: 700; }
.pill.idle { color: var(--muted); }
.pill.warn { color: #3a2400; background: var(--warn); border-color: transparent; font-weight: 700; }
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.progress { margin-top: 10px; }
.progress-bar { height: 5px; border-radius: 999px; background: rgba(255,255,255,.07); overflow: hidden; }
.progress-bar i { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .4s ease; }
.progress-label { display: flex; justify-content: space-between; font-size: 11px; color: var(--faint); margin-top: 5px; }
.session-preview {
  margin-top: 8px; font-size: 12.5px; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ---------- detail ---------- */
.detail-head {
  position: sticky; top: 0; z-index: 15;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 0 10px;
  background: linear-gradient(to bottom, var(--bg) 75%, rgba(10,12,17,0));
}
.detail-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.detail-title strong { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-title span { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.todo-panel {
  background: linear-gradient(160deg, #151d2b, #111624);
  border: 1px solid #29324a; border-radius: 16px;
  padding: 12px 14px; margin-bottom: 12px;
}
.todo-panel h3 { margin: 0 0 8px; font-size: 12px; letter-spacing: .4px; color: var(--muted); font-weight: 600; text-transform: uppercase; }
.todo-item { display: flex; gap: 9px; align-items: flex-start; padding: 4px 0; font-size: 13.5px; }
.todo-item .box { flex: none; width: 17px; height: 17px; margin-top: 1px; border-radius: 5px; border: 1.5px solid #46506d; display: grid; place-items: center; font-size: 11px; }
.todo-item.done .box { background: var(--accent-2); border-color: var(--accent-2); color: #07251d; }
.todo-item.done .txt { color: var(--faint); text-decoration: line-through; }
.todo-item.active .box { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(111,140,255,.15); }
.todo-item.active .txt { color: #cfd8ff; }

.timeline { display: flex; flex-direction: column; gap: 9px; padding-bottom: 18px; }
.tl-divider { display: flex; align-items: center; gap: 10px; color: var(--faint); font-size: 11px; margin: 8px 2px 2px; }
.tl-divider::before, .tl-divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.bubble {
  max-width: 92%;
  border-radius: 15px;
  padding: 10px 12px;
  font-size: 14px;
  word-break: break-word;
  white-space: pre-wrap;
}
.bubble.user {
  align-self: flex-end;
  background: linear-gradient(150deg, #2a3f8f, #24336e);
  border: 1px solid #3b4f9e;
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}
.bubble.assistant .role, .bubble.user .role { display: block; font-size: 10.5px; color: var(--muted); margin-bottom: 4px; letter-spacing: .3px; }
details.think {
  align-self: flex-start; max-width: 92%; width: 100%;
  background: rgba(255,255,255,.025); border: 1px dashed #2b3349;
  border-radius: 13px; padding: 8px 11px; font-size: 12.5px; color: var(--muted);
}
details.think summary { cursor: pointer; color: var(--faint); font-size: 11.5px; }
details.think p { white-space: pre-wrap; margin: 7px 0 0; }
.tool {
  align-self: flex-start; width: 100%;
  background: #10141f; border: 1px solid #232b3f;
  border-radius: 13px; overflow: hidden;
}
.tool-head { display: flex; align-items: center; gap: 8px; padding: 9px 11px; }
.tool-icon {
  width: 25px; height: 25px; border-radius: 8px; flex: none;
  display: grid; place-items: center;
  background: #1c2336; color: #9fb0ff; font-size: 13px;
}
.tool-name { font-size: 12.5px; font-weight: 650; color: #c7d0e8; }
.tool-state { margin-left: auto; font-size: 10.5px; color: var(--faint); }
.tool-state.err { color: var(--danger); }
.tool-state.ok { color: var(--ok); }
.tool-body { border-top: 1px solid #1e2537; padding: 8px 11px; }
.tool-cmd {
  font-family: var(--mono); font-size: 11.5px; color: #a9b6d8;
  white-space: pre-wrap; word-break: break-all;
  max-height: 130px; overflow: hidden;
}
.tool details summary { font-size: 11.5px; color: var(--faint); cursor: pointer; padding: 2px 0; }
.tool pre {
  margin: 6px 0 0; font-family: var(--mono); font-size: 11.5px;
  white-space: pre-wrap; word-break: break-word; color: #b8c3dd;
  max-height: 260px; overflow: auto;
}
.tl-note { align-self: center; font-size: 11.5px; color: var(--faint); padding: 2px 10px; }
.live-row { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; padding: 6px 2px; }
.pulse-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 0 0 rgba(85,214,194,.5); animation: pulse 1.6s infinite; }
.pulse-dot.warn { background: var(--warn); box-shadow: 0 0 0 0 rgba(242,179,92,.5); }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(85,214,194,.45); } 70% { box-shadow: 0 0 0 8px rgba(85,214,194,0); } 100% { box-shadow: 0 0 0 0 rgba(85,214,194,0); } }

/* ---------- approval ---------- */
.approval-layer {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(3, 5, 10, .55);
  backdrop-filter: blur(4px);
  padding: 12px 12px calc(var(--safe-bottom) + 12px);
  animation: fade .18s ease;
}
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
.approval-card {
  width: 100%; max-width: 620px;
  background: linear-gradient(165deg, #1b1a22, #141722 60%);
  border: 1px solid #4a3d2a;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
  animation: rise .22s cubic-bezier(.2,.9,.25,1);
}
@keyframes rise { from { transform: translateY(26px); opacity: .6 } to { transform: none; opacity: 1 } }
.approval-strip {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 15px;
  background: rgba(242,179,92,.13);
  color: #ffd79a; font-size: 12px; font-weight: 600;
}
.approval-body { padding: 13px 15px 4px; }
.approval-headline { font-size: 15px; font-weight: 650; line-height: 1.4; }
.approval-command {
  margin: 9px 0 0; padding: 9px 10px;
  background: #0d1018; border: 1px solid #262d40; border-radius: 10px;
  font-family: var(--mono); font-size: 11.5px; color: #b9c5e2;
  white-space: pre-wrap; word-break: break-all; max-height: 150px; overflow: auto;
}
.approval-meta { margin-top: 8px; font-size: 11.5px; color: var(--faint); }
.approval-actions { display: flex; gap: 9px; padding: 13px 15px 15px; }
.btn {
  flex: 1; height: 46px; border-radius: 13px;
  font-size: 15px; font-weight: 650;
  display: inline-flex; align-items: center; justify-content: center;
  transition: .16s ease;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(135deg, #6f8cff, #4f6df0); color: #fff; box-shadow: 0 8px 22px rgba(79,109,240,.3); }
.btn.ghost { background: rgba(255,255,255,.05); border: 1px solid var(--line); color: #c6cee2; }
.btn.ghost.danger { color: #ff9b9b; border-color: #4a2b2f; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; }

/* ---------- gate / settings / toast ---------- */
.gate {
  position: fixed; inset: 0; z-index: 60;
  display: grid; place-items: center;
  background: rgba(4,6,11,.92);
  padding: 22px;
}
.gate-card {
  width: 100%; max-width: 360px;
  background: linear-gradient(170deg, #171b28, #10131c);
  border: 1px solid var(--line); border-radius: 22px;
  padding: 24px 20px 20px; text-align: center;
  box-shadow: 0 30px 70px rgba(0,0,0,.55);
}
.gate-logo {
  width: 58px; height: 58px; margin: 0 auto 12px; border-radius: 18px;
  display: grid; place-items: center; font-weight: 800; letter-spacing: 1px;
  background: linear-gradient(145deg, #2c3a72, #1b2242);
  border: 1px solid #3d4c86; color: #dfe6ff;
}
.gate-card h2 { margin: 0 0 6px; font-size: 19px; }
.gate-card p { margin: 0 0 16px; color: var(--muted); font-size: 13px; }
#gate-input {
  width: 100%; height: 50px; text-align: center;
  letter-spacing: 8px; font-size: 21px; font-weight: 700;
  background: #0c0f17; border: 1px solid #2b3349; border-radius: 13px;
  outline: none; margin-bottom: 10px;
}
#gate-input:focus { border-color: #5068c9; box-shadow: 0 0 0 3px rgba(111,140,255,.16); }
.gate-error { min-height: 18px; color: var(--danger); font-size: 12px; margin-top: 8px; }
.sheet {
  position: fixed; inset: 0; z-index: 55;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(3,5,10,.55); backdrop-filter: blur(4px);
  padding: 12px 12px calc(var(--safe-bottom) + 12px);
}
.sheet-card {
  width: 100%; max-width: 560px;
  background: #141824; border: 1px solid var(--line); border-radius: 20px;
  padding: 16px;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.settings-row { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid #202738; font-size: 13px; color: var(--muted); }
.settings-row code { color: #cfd8ef; font-family: var(--mono); font-size: 12px; word-break: break-all; text-align: right; }
.settings-actions { display: flex; gap: 8px; margin-top: 14px; }
.settings-note { color: var(--faint); font-size: 11.5px; margin: 12px 0 0; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 26px);
  transform: translateX(-50%);
  background: #232a3d; color: #e8edf9; border: 1px solid #39435e;
  padding: 9px 15px; border-radius: 999px; font-size: 13px;
  z-index: 80; box-shadow: 0 10px 30px rgba(0,0,0,.4);
  animation: fade .18s ease;
}
.empty { text-align: center; padding: 54px 18px; color: var(--muted); }
.empty-emoji { font-size: 40px; margin-bottom: 8px; }
.empty h2 { font-size: 17px; color: var(--text); margin: 0 0 6px; }
.empty p { font-size: 13px; margin: 0; }

.composer {
  position: sticky; bottom: 0; z-index: 25;
  display: flex; align-items: flex-end; gap: 7px;
  padding: 8px 0 calc(8px + var(--safe-bottom));
  margin: 6px -2px 0;
  background: linear-gradient(to top, var(--bg) 72%, rgba(10,12,17,0));
  border-top: 1px solid var(--line);
}
.compose-mode {
  flex: none; height: 38px; padding: 0 10px; border-radius: 12px;
  background: #172033; color: #9fb0d6; border: 1px solid #2b3650;
  font-size: 12px; font-weight: 600;
}
.compose-mode.steer { color: #ffd479; border-color: #5a4720; background: #241d10; }
#compose-input {
  flex: 1; min-width: 0; min-height: 38px; max-height: 120px;
  resize: none; overflow-y: auto;
  padding: 9px 11px; border-radius: 12px;
  background: #121722; color: var(--text);
  border: 1px solid #2b3650;
  font: inherit; font-size: 14px; line-height: 1.35;
}
#compose-input:focus { outline: none; border-color: #4f6bd8; }
.compose-send {
  flex: none; height: 38px; padding: 0 15px; border-radius: 12px;
  background: #4f6bd8; color: #fff; border: 0;
  font-size: 14px; font-weight: 650;
}
.compose-send:disabled { opacity: .55; }
.compose-stop {
  flex: none; height: 38px; width: 38px; border-radius: 12px;
  background: #2a1518; color: #ff7b72; border: 1px solid #5a2a2e;
  font-size: 13px;
}

.top-btn {
  position: fixed; right: 14px;
  bottom: calc(var(--safe-bottom) + 82px);
  z-index: 60;
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: rgba(30, 38, 58, .92); color: #cfd8ef;
  border: 1px solid #39435e;
  box-shadow: 0 8px 24px rgba(0,0,0,.45);
  backdrop-filter: blur(8px);
}
.top-btn:active { transform: scale(.94); }

.ws-group { margin-bottom: 16px; }
.ws-group-head {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 2px 7px; margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
}
.ws-group-title { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.ws-group-title h3 {
  margin: 0; min-width: 0;
  font-size: 13px; font-weight: 650; color: #cfd8ef;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ws-ico { color: #7f8dad; font-size: 13px; flex: none; }
.ws-count {
  flex: none; min-width: 20px; padding: 1px 6px; border-radius: 999px;
  background: #1c2436; border: 1px solid #2c3650;
  color: #9fb0d6; font-size: 10.5px; text-align: center;
}
.ws-group-path {
  flex: none; max-width: 46%;
  color: var(--faint); font-size: 10.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ws-new {
  flex: none; width: 24px; height: 24px; border-radius: 8px;
  border: 1px solid #2c3650; background: #172033; color: #9fb0d6;
  font-size: 16px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.ws-new:active { background: #22304a; }

.ws-picker { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.ws-choice {
  width: 100%; text-align: left;
  background: #10151f; border: 1px solid #29324a; border-radius: 12px;
  padding: 10px 12px; color: var(--text);
}
.ws-choice:active { border-color: #4f6bd8; }
.ws-choice b { display: block; font-size: 14px; margin-bottom: 2px; }
.ws-choice span { display: block; color: var(--faint); font-size: 11px; word-break: break-all; }
.ws-choice small { color: #8fa2c9; font-size: 11px; }

.files-card { max-height: 84vh; display: flex; flex-direction: column; }
.files-cwd {
  font-family: var(--mono); font-size: 11px; color: var(--faint);
  word-break: break-all; margin: -2px 0 10px;
}
#files-body { overflow-y: auto; min-height: 80px; }
.files-section-title {
  font-size: 11.5px; color: #8fa2c9; text-transform: uppercase;
  letter-spacing: .04em; margin: 12px 2px 6px;
}
.files-section-title:first-child { margin-top: 0; }
.files-breadcrumb {
  display: flex; align-items: center; gap: 6px; margin-bottom: 6px;
}
.files-up {
  border: 1px solid #2c3650; background: #172033; color: #cfd8ef;
  border-radius: 8px; padding: 4px 9px; font-size: 12px;
}
.files-path { font-family: var(--mono); font-size: 11px; color: var(--faint); word-break: break-all; }
.file-row {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 9px 4px; border-bottom: 1px solid #202738;
  background: none; border-left: 0; border-right: 0; border-top: 0;
  color: var(--text); text-align: left; text-decoration: none;
}
.file-row:active { background: #182033; }
.file-ico { flex: none; font-size: 16px; width: 20px; text-align: center; }
.file-main { flex: 1; min-width: 0; }
.file-name {
  display: block; font-size: 13.5px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.file-meta { display: block; color: var(--faint); font-size: 10.5px; margin-top: 1px; }
.file-dl { flex: none; color: #8fa2c9; font-size: 16px; }
.files-empty { color: var(--faint); font-size: 12px; padding: 10px 2px; }

.compose-model {
  flex: none; max-width: 92px; height: 38px; padding: 0 9px; border-radius: 12px;
  background: #141b2b; color: #a9b8d8; border: 1px solid #2b3650;
  font-size: 11.5px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.compose-model:active { background: #1d2740; }

.model-card { max-height: 84vh; display: flex; flex-direction: column; }
.model-current {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 11px; margin-bottom: 10px;
  background: #10151f; border: 1px solid #29324a; border-radius: 12px;
  font-size: 12px; color: #9fb0d6;
}
.model-current b { color: #e8edf9; font-size: 13px; }
.model-groups { overflow-y: auto; min-height: 60px; }
.model-group-title {
  font-size: 11px; color: #7f8dad; margin: 12px 2px 5px;
  text-transform: uppercase; letter-spacing: .04em;
}
.model-group-title:first-child { margin-top: 0; }
.model-row {
  display: flex; align-items: flex-start; gap: 9px; width: 100%;
  padding: 10px 8px; border: 0; border-bottom: 1px solid #202738;
  background: none; color: var(--text); text-align: left;
}
.model-row:active { background: #182033; }
.model-row.active { background: #17223a; }
.model-mark {
  flex: none; width: 18px; height: 18px; margin-top: 1px;
  border-radius: 50%; border: 1px solid #39435e; color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px;
}
.model-row.active .model-mark { background: #4f6bd8; border-color: #4f6bd8; }
.model-main { flex: 1; min-width: 0; }
.model-name { display: block; font-size: 13.5px; font-weight: 600; }
.model-desc { display: block; color: var(--faint); font-size: 11px; margin-top: 2px; }
.model-badge {
  flex: none; padding: 1px 6px; border-radius: 999px;
  background: #1c2436; border: 1px solid #2c3650; color: #9fb0d6; font-size: 10px;
}

@media (min-width: 560px) {
  #app { padding-left: 20px; padding-right: 20px; }
  .bubble { max-width: 82%; }
}
