:root {
  --bg: #0d1117;
  --bg-2: #161b22;
  --bg-3: #1f2631;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #38bdf8;
  --accent-2: #6366f1;
  --green: #3fb950;
  --red: #f85149;
  --amber: #d29922;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.55;
}

.hidden { display: none !important; }

/* Login */
.login-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #182234 0%, #0d1117 60%);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}
.login-logo {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}
.login-logo h1 { margin: 0; font-size: 20px; font-weight: 600; }
.login-logo p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.login-card input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  outline: none;
  margin-bottom: 12px;
}
.login-card input:focus { border-color: var(--accent); }
.login-error { color: var(--red); font-size: 13px; margin-bottom: 12px; }

/* Buttons & Inputs */
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text);
}
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  width: 100%;
}
.btn-primary:active { opacity: 0.85; }
.btn-secondary {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 13px;
}
.btn-secondary:hover { border-color: #484f58; }
.btn-sm { padding: 6px 10px; font-size: 12px; border-radius: 8px; width: auto; }
.btn-xs { padding: 4px 8px; font-size: 12px; border-radius: 6px; width: auto; }
.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text);
  background: var(--bg-3);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.link-btn { color: var(--muted); font-size: 12px; }
.link-btn:hover { color: var(--text); }

/* App Layout */
.app {
  display: flex;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  padding-top: var(--safe-top);
}

/* Sidebar */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 30;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.brand-row { display: flex; align-items: center; gap: 8px; }
.brand-name { font-weight: 600; font-size: 15px; }
.host-pill {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 99px;
  font-weight: 500;
}
.host-pill.online { background: rgba(63,185,80,0.18); color: var(--green); border: 1px solid rgba(63,185,80,0.4); }
.host-pill.offline { background: rgba(248,81,73,0.16); color: var(--red); border: 1px solid rgba(248,81,73,0.35); }

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 8px;
  gap: 4px;
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  padding: 6px;
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}
.tab-btn.active { background: var(--bg-3); color: var(--text); }

.sidebar-panel {
  display: none;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}
.sidebar-panel.active { display: flex; }

.panel-actions {
  display: flex;
  gap: 6px;
  padding: 10px 10px 6px;
}
.panel-actions .btn-primary { flex: 1; }
.file-nav select { flex: 1; min-width: 0; }
.select-sm {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 12px;
  outline: none;
}
.path-crumb {
  padding: 0 10px 6px;
  font-size: 11px;
  font-family: var(--mono);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.list-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
  -webkit-overflow-scrolling: touch;
}

.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  margin-bottom: 3px;
  border: 1px solid transparent;
}
.session-item:hover { background: rgba(255,255,255,0.04); }
.session-item.active { background: rgba(56,189,248,0.12); border-color: rgba(56,189,248,0.35); }
.session-info { min-width: 0; flex: 1; }
.session-title {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}
.session-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.busy-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  animation: pulse 1.2s infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.25; } }
.del-btn { opacity: 0.45; font-size: 13px; padding: 4px; }
.del-btn:hover { opacity: 1; color: var(--red); }

.file-item, .git-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.file-item:hover, .git-item:hover { background: rgba(255,255,255,0.05); }
.file-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { font-size: 11px; color: var(--muted); font-family: var(--mono); }
.git-badge {
  font-family: var(--mono);
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(210,153,34,0.2);
  color: var(--amber);
}

.sidebar-footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.muted-xs { font-size: 11px; color: var(--muted); font-family: var(--mono); }

/* Main Column */
.main-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Top Bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.session-head { min-width: 0; }
.active-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.active-cwd { font-size: 11px; color: var(--muted); font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.select-pill {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 12px;
  max-width: 145px;
  outline: none;
}
.usage-pill {
  font-size: 11px;
  font-family: var(--mono);
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 4px 7px;
  border-radius: 8px;
}

/* Main Body */
.main-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* Viewer Pane */
.viewer-pane {
  position: absolute;
  inset: 0;
  z-index: 15;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}
.viewer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.viewer-path {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.viewer-content {
  flex: 1;
  margin: 0;
  padding: 12px 16px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre;
  -webkit-overflow-scrolling: touch;
}
.diff-add { color: #56d364; background: rgba(63,185,80,0.12); display: block; }
.diff-del { color: #ff7b72; background: rgba(248,81,73,0.12); display: block; }
.diff-hunk { color: #79c0ff; display: block; }
.diff-path { margin-top: 8px; color: var(--accent); }
.list-empty { padding: 14px; color: var(--muted); font-size: 12px; }
.list-empty-ok { padding: 12px; color: var(--green); font-size: 12px; }
.modal-status { padding: 16px; color: var(--muted); }
.term-tab-close { color: var(--muted); font-size: 11px; }

/* Chat Stream */
.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.chat-stream {
  max-width: 880px;
  margin: 0 auto;
  padding: 16px 14px 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty-state {
  text-align: center;
  padding: 56px 16px;
  color: var(--muted);
}
.empty-state h2 { color: var(--text); margin: 0 0 8px; font-size: 20px; }
.empty-state p { margin: 0 0 20px; font-size: 13px; }
.empty-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.empty-actions .btn-primary { width: auto; }

/* Chat Messages */
.msg-user {
  align-self: flex-end;
  max-width: 86%;
  background: linear-gradient(135deg, rgba(56,189,248,0.18), rgba(99,102,241,0.22));
  border: 1px solid rgba(99,102,241,0.4);
  border-radius: 14px 14px 4px 14px;
  padding: 10px 14px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-images { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.msg-images img { max-width: 180px; max-height: 160px; border-radius: 8px; border: 1px solid var(--border); }

.msg-assistant {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  word-break: break-word;
}
.msg-assistant h1, .msg-assistant h2, .msg-assistant h3 { margin: 12px 0 6px; font-size: 15px; color: #fff; }
.msg-assistant p { margin: 6px 0; }
.msg-assistant ul, .msg-assistant ol { margin: 6px 0; padding-left: 20px; }
.msg-assistant a { color: var(--accent); text-decoration: none; }
.msg-assistant blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
  background: rgba(255,255,255,0.02);
}
.msg-assistant code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: rgba(110,118,129,0.25);
  padding: 1px 5px;
  border-radius: 5px;
}
.code-block {
  margin: 10px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #0a0d12;
}
.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  background: var(--bg-3);
  font-size: 11px;
  color: var(--muted);
  font-family: var(--mono);
}
.code-block pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.45;
}
.code-block pre code { background: none; padding: 0; }

/* Thought Accordion */
.thought-box {
  background: rgba(255,255,255,0.02);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12.5px;
  color: var(--muted);
}
.thought-box summary { cursor: pointer; font-weight: 500; user-select: none; }
.thought-body { margin-top: 8px; white-space: pre-wrap; font-family: var(--mono); font-size: 12px; }

/* Tool Card */
.tool-card {
  background: #121720;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  font-size: 12.5px;
}
.tool-card summary {
  padding: 8px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  list-style: none;
}
.tool-title { font-family: var(--mono); color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-status { font-size: 11px; padding: 2px 7px; border-radius: 99px; flex-shrink: 0; }
.tool-status.completed { background: rgba(63,185,80,0.16); color: var(--green); }
.tool-status.failed { background: rgba(248,81,73,0.16); color: var(--red); }
.tool-status.running { background: rgba(56,189,248,0.18); color: var(--accent); }
.tool-body {
  padding: 8px 12px 12px;
  border-top: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 12px;
  max-height: 300px;
  overflow: auto;
  white-space: pre-wrap;
  color: var(--muted);
}

/* Permission Card */
.perm-card {
  background: rgba(210,153,34,0.1);
  border: 1.5px solid var(--amber);
  border-radius: 12px;
  padding: 12px 14px;
}
.perm-card.resolved {
  background: rgba(255,255,255,0.03);
  border-color: var(--border);
  opacity: 0.75;
}
.perm-head { font-weight: 600; color: var(--amber); margin-bottom: 6px; font-size: 13px; }
.perm-card.resolved .perm-head { color: var(--muted); }
.perm-cmd {
  font-family: var(--mono);
  font-size: 12px;
  background: rgba(0,0,0,0.35);
  padding: 8px 10px;
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre-wrap;
  margin-bottom: 10px;
}
.perm-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.perm-btn {
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  background: var(--bg-3);
  border: 1px solid var(--border);
}
.perm-btn.allow { background: rgba(63,185,80,0.22); border-color: var(--green); color: #7ee787; }
.perm-btn.deny { background: rgba(248,81,73,0.2); border-color: var(--red); color: #ff7b72; }

.msg-error {
  background: rgba(248,81,73,0.12);
  border: 1px solid rgba(248,81,73,0.45);
  color: #ffa198;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
}
.plan-card {
  background: rgba(56,189,248,0.08);
  border: 1px solid rgba(56,189,248,0.3);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
}

/* Composer */
.composer-wrap {
  padding: 8px 12px calc(10px + var(--safe-bottom));
  background: var(--bg-2);
  border-top: 1px solid var(--border);
}
.attach-bar {
  max-width: 880px;
  margin: 0 auto 8px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.attach-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
}
.attach-chip img { width: 28px; height: 28px; object-fit: cover; border-radius: 4px; }
.composer {
  max-width: 880px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 8px;
}
.composer:focus-within { border-color: var(--accent); }
.attach-btn { width: 34px; height: 34px; font-size: 20px; border: none; background: transparent; }
#prompt-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.45;
  resize: none;
  max-height: 160px;
  padding: 6px 2px;
  outline: none;
}
.btn-send {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  flex-shrink: 0;
}
.btn-stop {
  background: var(--red);
  color: #fff;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  flex-shrink: 0;
}

/* Terminal Drawer */
.term-drawer {
  height: 36vh;
  min-height: 220px;
  background: #080b10;
  border-top: 2px solid var(--accent-2);
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
}
.term-drawer.expanded { height: 72vh; }
.term-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.term-tabs { display: flex; gap: 6px; overflow-x: auto; flex: 1; }
.term-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  border-radius: 6px;
  font-size: 12px;
  background: var(--bg-3);
  border: 1px solid transparent;
  white-space: nowrap;
}
.term-tab.active { border-color: var(--accent); color: var(--accent); }
.term-actions { display: flex; gap: 6px; flex-shrink: 0; }
.term-screen {
  flex: 1;
  margin: 0;
  padding: 10px 12px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.4;
  color: #d0d7de;
  white-space: pre-wrap;
  word-break: break-all;
  -webkit-overflow-scrolling: touch;
}
.term-keys {
  display: flex;
  gap: 5px;
  padding: 5px 8px;
  overflow-x: auto;
  background: #10151e;
  border-top: 1px solid var(--border);
}
.key-btn {
  padding: 5px 9px;
  border-radius: 6px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
  white-space: nowrap;
  flex-shrink: 0;
}
.key-btn:active { background: var(--accent-2); }
.term-input-row {
  display: flex;
  gap: 6px;
  padding: 6px 8px;
  background: var(--bg-2);
  border-top: 1px solid var(--border);
}
.term-input-row input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  padding: 6px 10px;
  outline: none;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 560px;
  max-height: 82vh;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-desc { margin: 0; padding: 10px 16px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); }
.desktop-list { overflow-y: auto; padding: 10px; flex: 1; }
.desktop-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  background: var(--bg);
}
.desktop-item-info { min-width: 0; flex: 1; }
.desktop-item-title { font-weight: 600; font-size: 13px; margin-bottom: 3px; }
.desktop-item-preview { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.desktop-item-meta { font-size: 11px; color: var(--muted); margin-top: 3px; font-family: var(--mono); }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #232a36;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 99px;
  font-size: 13px;
  z-index: 80;
  box-shadow: 0 10px 30px rgba(0,0,0,0.45);
}

/* Responsive Mobile / iPad Portrait */
.mobile-only { display: none; }
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 25;
}
@media (max-width: 899px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 82vw;
    max-width: 310px;
    padding-top: var(--safe-top);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .topbar { padding: 7px 10px; }
  .topbar-controls { width: 100%; justify-content: flex-start; }
  .select-pill { font-size: 11.5px; padding: 4px 6px; max-width: 118px; }
  .chat-stream { padding: 12px 10px 20px; }
  .msg-user { max-width: 92%; }
}
