:root {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-input: #334155;
  --bg-hover: #2d3a50;
  --border: #475569;
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --accent: #38bdf8;
  --accent2: #818cf8;
  --green: #4ade80;
  --red: #f87171;
  --radius: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin:0; padding:0; box-sizing:border-box; }
html,body {
  height:100%; overflow-y:auto;
  font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',system-ui,sans-serif;
  background:var(--bg); color:var(--text);
  -webkit-tap-highlight-color:transparent;
}
#app {
  display:flex; flex-direction:column;
  min-height:100%; max-width:640px; margin:0 auto;
}

/* Header */
#header {
  display:flex; align-items:center; gap:12px;
  padding:12px 16px; padding-top:max(12px, var(--safe-top));
  background:var(--bg-card); border-bottom:1px solid var(--border);
  position:sticky; top:0; z-index:10;
}
.title { font-size:1.1rem; font-weight:600; flex:1; }
.icon-btn {
  background:none; border:none; color:var(--accent); cursor:pointer;
  padding:6px; border-radius:50%; transition:transform .3s;
  display:inline-flex; align-items:center; justify-content:center;
}
.icon-btn:active { background:var(--bg-input); }
.icon-btn.spinning svg { animation:spin 1s linear infinite; }
@keyframes spin { to { transform:rotate(360deg); } }

/* Grid */
.nodes-grid {
  display:flex; flex-direction:column; gap:12px; padding:12px 14px 40px;
}
.node-card {
  background:var(--bg-card); border:1px solid var(--border);
  border-radius:var(--radius); overflow:hidden;
}

/* Node header */
.node-header {
  display:flex; align-items:center; justify-content:space-between;
  padding:14px; gap:10px;
}
.node-name { font-weight:600; font-size:.9rem; }
.node-host {
  font-family:monospace; font-size:.7rem; color:var(--accent);
  background:var(--bg-input); padding:2px 8px; border-radius:6px;
  display:inline-block; margin-left:6px;
}
.node-actions { display:flex; align-items:center; gap:6px; }
.node-count { font-size:.7rem; color:var(--text-dim); white-space:nowrap; }
.node-btn-group { display:flex; gap:4px; align-items:center; }

/* Buttons */
.btn {
  padding:6px 14px; border-radius:8px; border:none;
  font-size:.75rem; font-weight:500; cursor:pointer;
  transition:opacity .15s; white-space:nowrap;
  display:inline-flex; align-items:center; justify-content:center; gap:4px;
}
.btn:active { opacity:.7; }
.btn-new { background:var(--accent2); color:#fff; min-width:28px; }
.btn-start {
  background:var(--accent); color:var(--bg);
  padding:8px 18px; border-radius:8px; font-weight:600;
  width:100%;
}
.btn-icon {
  background:var(--bg-input); color:var(--text-dim);
  padding:6px; border-radius:8px; width:28px; height:28px;
}
.btn-icon:hover { color:var(--text); background:var(--bg-hover); }
.btn-sm {
  background:var(--bg-input); color:var(--text); padding:4px 10px; font-size:.7rem;
}
.btn-tiny {
  background:transparent; border:none; color:var(--text-dim); cursor:pointer;
  font-size:.8rem; padding:2px 4px; border-radius:4px;
}
.btn-tiny:hover { background:var(--bg-input); color:var(--text); }

/* Active sessions */
.active-sessions { padding:0 14px 12px; }
.session-item {
  display:flex; align-items:center; gap:8px;
  padding:10px 12px; margin-bottom:4px;
  border-radius:8px; background:var(--bg-input);
  cursor:pointer; transition:background .15s;
  user-select:none;
}
.session-item:hover { background:var(--bg-hover); }
.session-item:active { opacity:.8; }
.session-dot {
  width:8px; height:8px; border-radius:50%;
  background:var(--green); box-shadow:0 0 6px var(--green);
  flex-shrink:0;
}
.session-name {
  font-size:.82rem; font-weight:500;
  flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
}
.session-port {
  font-family:monospace; font-size:.65rem;
  color:var(--text-dim); flex-shrink:0;
}

/* Expandable history panel */
.history-panel {
  border-top:1px solid var(--border);
  background:rgba(0,0,0,.15);
  transition:max-height .25s ease;
  max-height:400px;
  overflow:hidden;
}
.history-panel.collapsed {
  max-height:0; border-top:none;
}
.history-list { max-height:260px; overflow-y:auto; padding:8px 14px; }
.history-actions { padding:8px 14px 12px; }

.history-item {
  display:flex; flex-direction:column; gap:2px;
  padding:10px 12px; margin-bottom:4px;
  border-radius:8px; cursor:pointer;
  border:1px solid transparent;
  transition:border-color .15s,background .15s;
}
.history-item:hover { border-color:var(--border); background:var(--bg-input); }
.history-item:active { background:var(--accent2); opacity:.7; }
.history-name { font-size:.82rem; font-weight:500; }
.history-meta { font-size:.68rem; color:var(--text-dim); }

/* States */
.no-sessions { font-size:.78rem; color:var(--text-dim); text-align:center; padding:16px; }
.loading { text-align:center; padding:16px; color:var(--text-dim); font-size:.82rem; }
.empty-state { text-align:center; padding:40px 16px; color:var(--text-dim); font-size:.9rem; }

/* Toast */
.toast-container {
  position:fixed; bottom:80px; left:50%; transform:translateX(-50%);
  z-index:300; display:flex; flex-direction:column; gap:8px;
  pointer-events:none;
}
.toast {
  background:var(--bg-card); color:var(--text);
  padding:10px 20px; border-radius:20px;
  font-size:.8rem; border:1px solid var(--border);
  animation:toastIn .3s ease,toastOut .3s ease 2.5s forwards;
  text-align:center; white-space:nowrap;
}
.toast.error { border-color:var(--red); }
@keyframes toastIn { from { opacity:0; transform:translateY(20px); } to { opacity:1; transform:translateY(0); } }
@keyframes toastOut { to { opacity:0; transform:translateY(-10px); } }

/* ====== Overlay Panels (File Browser, Terminal) ====== */
.overlay-panel {
  position:fixed; top:0; left:0; right:0; bottom:0; width:100vw; max-width:100vw;
  z-index:200; background:var(--bg);
  display:flex; flex-direction:column;
  transform:translateX(100%);
  transition:transform .3s ease;
  padding-top:var(--safe-top);
  padding-bottom:var(--safe-bottom);
}
.overlay-panel.open { transform:translateX(0); }

.overlay-header {
  display:flex; align-items:center; gap:8px;
  padding:12px 16px; padding-top:max(12px, var(--safe-top));
  background:var(--bg-card); border-bottom:1px solid var(--border);
  flex-shrink:0;
}
.overlay-header h2 { font-size:1rem; font-weight:600; flex:1; }
.overlay-header .header-controls {
  display:flex; align-items:center; gap:6px;
}

/* File Browser */
.file-breadcrumb {
  display:flex; flex-wrap:wrap; gap:4px;
  padding:10px 14px; font-size:.78rem;
  background:rgba(0,0,0,.2); border-bottom:1px solid var(--border);
  flex-shrink:0;
  overflow-x:auto; white-space:nowrap;
}
.file-breadcrumb a { color:var(--accent); text-decoration:none; cursor:pointer; }
.file-breadcrumb a:hover { text-decoration:underline; }

.file-upload-zone {
  margin:8px 14px; padding:16px; border:2px dashed var(--border);
  border-radius:var(--radius); text-align:center;
  font-size:.78rem; color:var(--text-dim); cursor:pointer;
  transition:border-color .15s,background .15s;
  flex-shrink:0;
}
.file-upload-bar {
  margin:6px 14px; padding:10px 14px;
  background:var(--accent); color:var(--bg); font-weight:600;
  border-radius:8px; text-align:center; font-size:.8rem; cursor:pointer;
  transition:opacity .15s; flex-shrink:0;
}
.file-upload-bar:active { opacity:.7; }

.file-list {
  flex:1; overflow-y:auto; padding:4px 14px 14px;
}
.file-item {
  display:flex; align-items:center; gap:8px;
  padding:8px 10px; margin-bottom:2px;
  border-radius:6px; cursor:pointer;
  transition:background .15s;
}
.file-item:hover { background:var(--bg-input); }
.file-item:active { opacity:.7; }
.file-icon { flex-shrink:0; font-size:.9rem; }
.file-name { flex:1; font-size:.8rem; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.file-size { font-size:.65rem; color:var(--text-dim); min-width:48px; text-align:right; flex-shrink:0; }
.file-date { font-size:.65rem; color:var(--text-dim); min-width:60px; flex-shrink:0; }

.file-preview {
  display:flex; flex-direction:column;
  position:absolute; top:0; left:0; right:0; bottom:0;
  z-index:5; background:var(--bg);
  padding:0;
}
.file-preview-header {
  display:flex; align-items:center; gap:8px;
  padding:10px 14px; background:var(--bg-card);
  border-bottom:1px solid var(--border);
}
.file-preview-header span { flex:1; font-size:.85rem; font-weight:500; }
.file-preview pre {
  flex:1; overflow:auto; padding:14px;
  font-family:monospace; font-size:.78rem;
  white-space:pre-wrap; word-break:break-all;
  background:rgba(0,0,0,.3); margin:0;
}

/* Terminal Panel */
.term-panel.fullscreen { z-index:250; }

.term-body {
  flex:1; display:flex; flex-direction:column;
  min-height:0; overflow:hidden;
}
.term-xterm {
  flex:1; min-height:0;
  background:#000;
}

/* Terminal File Browser Sheet */
.term-files-panel {
  flex-shrink:0;
  border-top:1px solid var(--border);
  background:var(--bg-card);
  max-height:50%;
  display:flex;
  flex-direction:column;
}
.term-files-toggle {
  display:flex; align-items:center; gap:8px;
  padding:10px 14px; cursor:pointer;
  font-size:.82rem; font-weight:500;
  user-select:none;
}
.term-files-toggle:active { opacity:.7; }
.toggle-arrow {
  margin-left:auto;
  font-size:.75rem; color:var(--text-dim);
  transition:transform .2s;
}
.term-files-content {
  display:none;
  flex-direction:column;
  flex:1; min-height:0;
  border-top:1px solid var(--border);
}
.term-files-content.open {
  display:flex;
}
.term-files-breadcrumb {
  display:flex; flex-wrap:wrap; gap:4px;
  padding:8px 14px;
  font-size:.72rem; overflow-x:auto; white-space:nowrap;
  background:rgba(0,0,0,.15);
  flex-shrink:0;
}
.term-files-breadcrumb a { color:var(--accent); text-decoration:none; cursor:pointer; }
.term-files-breadcrumb a:hover { text-decoration:underline; }
.term-files-list {
  flex:1; overflow-y:auto; padding:4px 14px;
}
.term-file-item {
  display:flex; align-items:center; gap:8px;
  padding:7px 8px; margin-bottom:1px;
  border-radius:6px; cursor:pointer;
  font-size:.78rem;
  transition:background .15s;
}
.term-file-item:hover { background:var(--bg-input); }
.term-file-item:active { opacity:.7; }
.term-file-icon { flex-shrink:0; }
.term-file-name { flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.term-file-size { font-size:.65rem; color:var(--text-dim); flex-shrink:0; }
.term-files-upload {
  padding:6px 14px 10px;
  flex-shrink:0;
  text-align:center;
}
.term-files-upload .btn-sm {
  background:var(--accent); color:var(--bg); font-weight:600;
  padding:6px 16px; font-size:.78rem;
}


/* ====== Terminal Panel ====== */
.term-panel.fullscreen { z-index:250; }
.term-panel .term-xterm { flex:1; min-height:0; background:#000; }

/* File Browser */
.file-preview {
  display:flex; flex-direction:column;
  position:absolute; top:0; left:0; right:0; bottom:0;
  z-index:5; background:var(--bg); padding:0;
}
.file-preview-header {
  display:flex; align-items:center; gap:8px;
  padding:10px 14px; background:var(--bg-card);
  border-bottom:1px solid var(--border);
}
.file-preview-header span { flex:1; font-size:.85rem; font-weight:500; }
.file-preview pre {
  flex:1; overflow:auto; padding:14px;
  font-family:monospace; font-size:.78rem;
  white-space:pre-wrap; word-break:break-all;
  background:rgba(0,0,0,.3); margin:0;
}
.file-upload-bar {
  margin:6px 14px; padding:10px 14px;
  background:var(--accent); color:var(--bg); font-weight:600;
  border-radius:8px; text-align:center; font-size:.8rem; cursor:pointer;
  transition:opacity .15s; flex-shrink:0;
}
.file-upload-bar:active { opacity:.7; }
.file-breadcrumb {
  display:flex; flex-wrap:wrap; gap:4px;
  padding:10px 14px; font-size:.78rem;
  background:rgba(0,0,0,.2); border-bottom:1px solid var(--border);
  flex-shrink:0; overflow-x:auto; white-space:nowrap;
}
.file-breadcrumb a { color:var(--accent); text-decoration:none; cursor:pointer; }
.file-breadcrumb a:hover { text-decoration:underline; }
.file-list { flex:1; overflow-y:auto; padding:4px 14px 14px; }
.file-item {
  display:flex; align-items:center; gap:8px;
  padding:8px 10px; margin-bottom:2px;
  border-radius:6px; cursor:pointer;
  transition:background .15s;
}
.file-item:hover { background:var(--bg-input); }
.file-item:active { opacity:.7; }
.file-icon { flex-shrink:0; font-size:.9rem; }
.file-name { flex:1; font-size:.8rem; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.file-size { font-size:.65rem; color:var(--text-dim); min-width:48px; text-align:right; flex-shrink:0; }
.file-date { font-size:.65rem; color:var(--text-dim); min-width:60px; flex-shrink:0; }

/* Mobile */
@media (max-width:480px) {
  .node-header { flex-direction:column; align-items:stretch; gap:8px; }
  .node-actions { justify-content:space-between; }
  .file-size, .file-date { display:none; }
  .file-item { padding:10px 8px; }
  .overlay-header { padding:10px 12px; }
  .file-breadcrumb { font-size:.72rem; }
}

/* ====== Terminal Tabs ====== */
.term-tabs {
  display:flex; gap:2px; padding:4px 8px 0;
  background:rgba(0,0,0,.2); border-bottom:1px solid var(--border);
  flex-shrink:0; overflow-x:auto;
}
.term-tab {
  padding:5px 12px; font-size:.75rem;
  background:var(--bg-input); border:1px solid var(--border);
  border-bottom:none; border-radius:6px 6px 0 0;
  color:var(--text-dim); cursor:pointer;
  white-space:nowrap; font-family:inherit;
  transition:background .15s;
}
.term-tab.active {
  background:var(--bg-card); color:var(--text);
  border-color:var(--border); border-bottom-color:var(--bg-card);
}
.term-tab.dead { color:var(--red); text-decoration:line-through; }
.term-tab:hover:not(.active) { background:var(--bg-hover); }

.term-panes { flex:1; display:flex; min-height:0; position:relative; }
.term-pane { display:none; flex:1; min-height:0; }
.term-pane.active { display:flex; flex-direction:column; }
.term-pane .term-xterm { flex:1; min-height:0; width:100%; }
.term-tab { position:relative; padding-right:22px; }
.tab-close {
  position:absolute; right:4px; top:50%; transform:translateY(-50%);
  font-size:10px; color:var(--text-dim); cursor:pointer;
  padding:2px; border-radius:3px; line-height:1;
  display:flex; align-items:center; justify-content:center;
  width:14px; height:14px;
}
.tab-close:hover { background:var(--bg-hover); color:var(--text); }
.term-tab.dead .tab-close { color:var(--red); }

/* ====== Text Editor ====== */
.editor-body { flex:1; display:flex; min-height:0; }
.editor-textarea {
  flex:1; border:none; background:#111; color:#e2e8f0;
  font-family:"Courier New","Fira Code",monospace; font-size:13px;
  padding:12px; resize:none; outline:none; line-height:1.4;
  tab-size:2;
}
.editor-preview {
  flex:1; overflow-y:auto; padding:12px 16px;
  background:var(--bg); color:var(--text);
}
.md-rendered { max-width:720px; margin:0 auto; }
.md-rendered h1 { font-size:1.3rem; margin:0.5em 0 0.3em; color:var(--text); }
.md-rendered h2 { font-size:1.1rem; margin:0.5em 0 0.3em; color:var(--accent); }
.md-rendered h3 { font-size:1rem; margin:0.4em 0 0.2em; }
.md-rendered p { margin:0.4em 0; line-height:1.5; }
.md-rendered code {
  background:var(--bg-input); padding:1px 5px; border-radius:4px;
  font-family:monospace; font-size:.85em;
}
.md-rendered pre { background:var(--bg-input); padding:10px; border-radius:6px; overflow-x:auto; }
.md-rendered pre code { background:transparent; padding:0; }
.md-rendered li { margin:0.2em 0 0.2em 1.2em; list-style:disc; }
.md-rendered a { color:var(--accent); }

/* Full viewport for overlay panels */
.overlay-panel { width:100%; max-width:100%; }
.term-pane .term-xterm > div { width:100% !important; }
.term-pane .term-xterm .xterm { width:100% !important; }

/* ====== Terminal + File Sidebar ====== */
.term-main {
  flex:1; display:flex; min-height:0; overflow:hidden;
}
.term-panes { flex:1; display:flex; min-height:0; position:relative; }
.term-files-sidebar {
  width:280px; display:flex; flex-direction:column;
  border-left:1px solid var(--border);
  background:var(--bg-card);
  transition:width .2s ease, opacity .2s ease;
  overflow:hidden; flex-shrink:0;
}
.term-files-sidebar.collapsed {
  width:0; border-left:none; opacity:0; pointer-events:none;
}
.term-files-header {
  display:flex; align-items:center; gap:8px;
  padding:6px 10px; font-size:.75rem; font-weight:500;
  background:rgba(0,0,0,.15);
  border-bottom:1px solid var(--border);
  flex-shrink:0;
}
.term-files-header .btn-tiny { font-size:.65rem; padding:2px 6px; }
